effect.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #include <wallpaperengine/fs/utils.h>
  2. #include "shaders/compiler.h"
  3. #include "effect.h"
  4. #include "irrlicht.h"
  5. #include "core.h"
  6. extern irr::f32 g_Time;
  7. namespace wp
  8. {
  9. effect::effect (json json_data)
  10. {
  11. json::const_iterator file = json_data.find ("file");
  12. json::const_iterator pass = json_data.find ("passes");
  13. if (file != json_data.end () && (*file).is_string () == true)
  14. {
  15. this->m_file = (*file).get <std::string> ().c_str ();
  16. }
  17. // passes list is optional
  18. if (pass == json_data.end () || (*pass).is_array () == false)
  19. return;
  20. json::const_iterator curpass = (*pass).begin ();
  21. json::const_iterator endpass = (*pass).end ();
  22. if (curpass == endpass)
  23. return;
  24. json::const_iterator textures = (*curpass).find ("textures");
  25. if (textures == (*curpass).end () || (*textures).is_array () == false)
  26. return;
  27. json::const_iterator curtex = (*textures).begin ();
  28. json::const_iterator endtex = (*textures).end ();
  29. for (; curtex != endtex; curtex ++)
  30. {
  31. if ((*curtex).is_null () == true)
  32. {
  33. this->m_textures.push_back (nullptr);
  34. }
  35. else if ((*curtex).is_string () == true)
  36. {
  37. irr::io::path texturepath = ("materials/" + (*curtex).get <std::string> () + ".tex").c_str ();
  38. this->m_textures.push_back (new wp::texture (texturepath));
  39. }
  40. }
  41. this->m_content = wp::fs::utils::loadFullFile (this->m_file);
  42. this->m_json = json::parse (this->m_content);
  43. json::const_iterator passes = this->m_json.find ("passes");
  44. if (passes == this->m_json.end () || (*passes).is_array () == false)
  45. return;
  46. json::const_iterator curpassmaterial = (*passes).begin ();
  47. json::const_iterator endpassmaterial = (*passes).end ();
  48. for (; curpassmaterial != endpassmaterial; curpassmaterial ++)
  49. {
  50. json::const_iterator material_it = (*curpassmaterial).find ("material");
  51. if (material_it == (*curpassmaterial).end () || (*material_it).is_string () == false)
  52. continue;
  53. irr::io::path material = (*material_it).get <std::string> ().c_str ();
  54. std::string content = wp::fs::utils::loadFullFile (material);
  55. json material_json = json::parse (content);
  56. json::const_iterator shader_passes = material_json.find ("passes");
  57. if (shader_passes == material_json.end () || (*shader_passes).is_array () == false)
  58. continue;
  59. json::const_iterator shaderpasscur = (*shader_passes).begin ();
  60. json::const_iterator shader = (*shaderpasscur).find ("shader");
  61. if (shader == (*shaderpasscur).end () || (*shader).is_string () == false)
  62. continue;
  63. irr::io::path shaderpath = ("shaders/" + (*shader).get <std::string> ()).c_str ();
  64. irr::io::path fragpath = shaderpath + ".frag";
  65. irr::io::path vertpath = shaderpath + ".vert";
  66. this->m_fragShader = new wp::shaders::compiler (fragpath, wp::shaders::compiler::Type::Type_Pixel, false);
  67. this->m_vertShader = new wp::shaders::compiler (vertpath, wp::shaders::compiler::Type::Type_Vertex, false);
  68. this->m_materialType = wp::irrlicht::driver->getGPUProgrammingServices ()
  69. ->addHighLevelShaderMaterial (
  70. this->m_vertShader->precompile ().c_str (), "main", irr::video::EVST_VS_2_0,
  71. this->m_fragShader->precompile ().c_str (), "main", irr::video::EPST_PS_2_0,
  72. this, irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL, 0, irr::video::EGSL_DEFAULT
  73. );
  74. }
  75. json::const_iterator constantvalues = (*curpass).find ("constantshadervalues");
  76. if (constantvalues != (*curpass).end () && (*constantvalues).is_object () == true)
  77. this->parseConstantValues ((*constantvalues));
  78. // last step is creating the actual variables for the shaders
  79. std::vector <shaders::compiler::ShaderParameter*>::const_iterator cur;
  80. std::vector <shaders::compiler::ShaderParameter*>::const_iterator end;
  81. cur = this->m_fragShader->getParameters ().begin ();
  82. end = this->m_fragShader->getParameters ().end ();
  83. // first do fragment shaders
  84. for (; cur != end; cur ++)
  85. {
  86. wp::shaders::compiler::ShaderParameter* param = (*cur);
  87. ShaderParameter* parameter = new ShaderParameter;
  88. void* defaultValue = param->defaultValue;
  89. std::map<std::string,void*>::const_iterator constant = this->m_constants.find (param->identifierName);
  90. if (constant != this->m_constants.end ())
  91. defaultValue = (*constant).second;
  92. parameter->value = nullptr;
  93. if (param->type == "vec4")
  94. {
  95. irr::core::vector3df* vec = (irr::core::vector3df*) defaultValue;
  96. irr::f32* val = new irr::f32 [4];
  97. val [0] = vec->X;
  98. val [1] = vec->Y;
  99. val [2] = vec->Z;
  100. val [3] = 0.0f;
  101. parameter->value = val;
  102. parameter->type = ParameterType::TYPE_VEC4;
  103. }
  104. else if (param->type == "vec3")
  105. {
  106. irr::core::vector3df* vec = (irr::core::vector3df*) defaultValue;
  107. irr::f32* val = new irr::f32 [3];
  108. val [0] = vec->X;
  109. val [1] = vec->Y;
  110. val [2] = vec->Z;
  111. parameter->value = val;
  112. parameter->type = ParameterType::TYPE_VEC3;
  113. }
  114. else if (param->type == "vec2")
  115. {
  116. irr::core::vector2df* vec = (irr::core::vector2df*) defaultValue;
  117. irr::f32* val = new irr::f32 [2];
  118. val [0] = vec->X;
  119. val [1] = vec->Y;
  120. parameter->value = val;
  121. parameter->type = ParameterType::TYPE_VEC2;
  122. }
  123. else if (param->type == "float")
  124. {
  125. irr::f32* org = (irr::f32*) defaultValue;
  126. irr::f32* val = new irr::f32;
  127. *val = *org;
  128. parameter->value = val;
  129. parameter->type = ParameterType::TYPE_FLOAT;
  130. }
  131. else if (param->type == "int")
  132. {
  133. irr::s32* org = (irr::s32*) defaultValue;
  134. irr::s32* val = new irr::s32;
  135. *val = *org;
  136. parameter->value = val;
  137. parameter->type = ParameterType::TYPE_INT;
  138. }
  139. else if (param->type == "sampler2D")
  140. {
  141. // sampler2D are textures from wallpaper engine
  142. // so these can be ignored as they are later on defined
  143. continue;
  144. }
  145. this->m_pixelVariables.insert (std::pair <std::string, ShaderParameter*> (param->variableName, parameter));
  146. }
  147. cur = this->m_vertShader->getParameters ().begin ();
  148. end = this->m_vertShader->getParameters ().end ();
  149. // second do vertex shaders
  150. for (;cur != end; cur ++)
  151. {
  152. wp::shaders::compiler::ShaderParameter* param = (*cur);
  153. if (param == nullptr)
  154. continue;
  155. void* defaultValue = param->defaultValue;
  156. std::map<std::string,void*>::const_iterator constant = this->m_constants.find (param->identifierName);
  157. if (constant != this->m_constants.end ())
  158. defaultValue = (*constant).second;
  159. ShaderParameter* parameter = new ShaderParameter;
  160. parameter->value = nullptr;
  161. if (param->type == "vec4")
  162. {
  163. irr::core::vector3df* vec = (irr::core::vector3df*) defaultValue;
  164. parameter->value = new irr::f32 [4];
  165. (*(irr::f32**) parameter->value) [0] = vec->X;
  166. (*(irr::f32**) parameter->value) [1] = vec->Y;
  167. (*(irr::f32**) parameter->value) [2] = vec->Z;
  168. (*(irr::f32**) parameter->value) [3] = 0.0f;
  169. parameter->type = ParameterType::TYPE_VEC4;
  170. }
  171. else if (param->type == "vec3")
  172. {
  173. irr::core::vector3df* vec = (irr::core::vector3df*) defaultValue;
  174. parameter->value = new irr::f32 [3];
  175. (*(irr::f32**) parameter->value) [0] = vec->X;
  176. (*(irr::f32**) parameter->value) [1] = vec->Y;
  177. (*(irr::f32**) parameter->value) [2] = vec->Z;
  178. parameter->type = ParameterType::TYPE_VEC3;
  179. }
  180. else if (param->type == "vec2")
  181. {
  182. irr::core::vector2df* vec = (irr::core::vector2df*) defaultValue;
  183. parameter->value = new irr::f32 [2];
  184. (*(irr::f32**) parameter->value) [0] = vec->X;
  185. (*(irr::f32**) parameter->value) [1] = vec->Y;
  186. parameter->type = ParameterType::TYPE_VEC2;
  187. }
  188. else if (param->type == "float")
  189. {
  190. parameter->value = new irr::f32;
  191. *(irr::f32*) parameter->value = *(irr::f32*) defaultValue;
  192. parameter->type = ParameterType::TYPE_FLOAT;
  193. }
  194. else if (param->type == "int")
  195. {
  196. parameter->value = new irr::s32;
  197. *(irr::s32*) parameter->value = *(irr::s32*) defaultValue;
  198. parameter->type = ParameterType::TYPE_INT;
  199. }
  200. else if (param->type == "sampler2D")
  201. {
  202. // sampler2D are textures from wallpaper engine
  203. // so these can be ignored as they are later on defined
  204. continue;
  205. }
  206. this->m_vertexVariables.insert (std::pair <std::string, ShaderParameter*> (param->variableName, parameter));
  207. }
  208. }
  209. void effect::parseConstantValues (json data)
  210. {
  211. /*
  212. "ui_editor_properties_animation_speed" : 0.10000000149011612,
  213. "ui_editor_properties_ripple_scale" : 2.5,
  214. "ui_editor_properties_ripple_strength" : 0.070000000298023224
  215. */
  216. json::const_iterator cur = data.begin ();
  217. json::const_iterator end = data.end ();
  218. for (; cur != end; cur ++)
  219. {
  220. std::string name = cur.key ();
  221. void* value = nullptr;
  222. if ((*cur).is_number_float () == true)
  223. {
  224. value = new irr::f32;
  225. *(irr::f32*) value = (*cur).get <irr::f32> ();
  226. }
  227. else if ((*cur).is_number_integer () == true)
  228. {
  229. value = new irr::s32;
  230. *(irr::s32*) value = (*cur).get <irr::s32> ();
  231. }
  232. else if ((*cur).is_string () == true)
  233. {
  234. value = new irr::core::vector3df;
  235. *(irr::core::vector3df*) value = core::ato3vf ((*cur).get <std::string> ().c_str ());
  236. }
  237. this->m_constants.insert (std::pair <std::string, void*> (name, value));
  238. }
  239. }
  240. std::vector<wp::texture*>& effect::getTextureList ()
  241. {
  242. return this->m_textures;
  243. }
  244. irr::s32 effect::getMaterialType ()
  245. {
  246. return this->m_materialType;
  247. }
  248. void effect::OnSetConstants (irr::video::IMaterialRendererServices* services, int32_t userData)
  249. {
  250. // TODO: IMPLEMENT PROPER SHADER CODE
  251. irr::f32 g_AnimationSpeed = 0.1f;
  252. irr::f32 g_Scale = 2.5f;
  253. irr::f32 g_ScrollSpeed = 0.0f;
  254. irr::f32 g_Direction = 0.0f;
  255. irr::f32 g_Strength = 0.07f;
  256. irr::f32 g_SpecularPower = 1.0f;
  257. irr::f32 g_SpecularStrength = 1.0f;
  258. irr::f32 g_SpecularColor [3] = {1.0f, 1.0f, 1.0f};
  259. irr::f32 g_Texture1Resolution [4] = {1.0f, 1.0f, 1.0f, 1.0f};
  260. irr::f32 g_Texture0 = 0;
  261. irr::f32 g_Texture1 = 1;
  262. irr::f32 g_Texture2 = 2;
  263. irr::video::IVideoDriver* driver = services->getVideoDriver ();
  264. irr::core::matrix4 worldViewProj;
  265. worldViewProj = driver->getTransform(irr::video::ETS_PROJECTION);
  266. worldViewProj *= driver->getTransform(irr::video::ETS_VIEW);
  267. worldViewProj *= driver->getTransform(irr::video::ETS_WORLD);
  268. /*
  269. THESE ARE THE VARIABLES AVAILABLE ON SHADERS
  270. uniform float g_Alpha;
  271. uniform vec3 g_Color;
  272. // Application time, starts with 0
  273. uniform float g_Time;
  274. // Maps 24 hrs to [0, 1]
  275. uniform float g_Daytime;
  276. uniform vec2 g_TexelSize;
  277. uniform vec2 g_TexelSizeHalf;
  278. uniform mat4 g_ModelMatrix;
  279. uniform mat4 g_ViewProjectionMatrix;
  280. uniform mat4 g_ModelViewProjectionMatrix;
  281. uniform mat4 g_ModelViewProjectionMatrixInverse;
  282. uniform vec3 g_EyePosition;
  283. uniform vec3 g_ViewUp;
  284. uniform vec3 g_ViewRight;
  285. // Samplers that map to the textures[] array in the material
  286. uniform sampler2D g_Texture0;
  287. uniform sampler2D g_Texture1;
  288. uniform sampler2D g_Texture2;
  289. uniform sampler2D g_Texture3;
  290. uniform sampler2D g_Texture4;
  291. uniform sampler2D g_Texture5;
  292. uniform sampler2D g_Texture6;
  293. uniform sampler2D g_Texture7;
  294. // For sprite sheets (GIFs)
  295. uniform vec4 g_Texture0Rotation;
  296. uniform vec4 g_Texture1Rotation;
  297. uniform vec4 g_Texture2Rotation;
  298. uniform vec4 g_Texture3Rotation;
  299. uniform vec4 g_Texture4Rotation;
  300. uniform vec4 g_Texture5Rotation;
  301. uniform vec4 g_Texture6Rotation;
  302. uniform vec4 g_Texture7Rotation;
  303. uniform vec2 g_Texture0Translation;
  304. uniform vec2 g_Texture1Translation;
  305. uniform vec2 g_Texture2Translation;
  306. uniform vec2 g_Texture3Translation;
  307. uniform vec2 g_Texture4Translation;
  308. uniform vec2 g_Texture5Translation;
  309. uniform vec2 g_Texture6Translation;
  310. uniform vec2 g_Texture7Translation;
  311. uniform vec4 g_LightsColorRadius[4]; // color in XYZ, radius in W
  312. uniform vec3 g_LightsPosition[4];
  313. uniform vec3 g_LightAmbientColor;
  314. uniform vec3 g_LightSkylightColor;
  315. // lower frequencies at lower indices
  316. uniform float g_AudioSpectrum16Left[16];
  317. uniform float g_AudioSpectrum16Right[16];
  318. uniform float g_AudioSpectrum32Left[32];
  319. uniform float g_AudioSpectrum32Right[32];
  320. uniform float g_AudioSpectrum64Left[64];
  321. uniform float g_AudioSpectrum64Right[64];
  322. // Normalized in UV space [0, 1]
  323. uniform vec2 g_PointerPosition;
  324. */
  325. services->setVertexShaderConstant ("g_Time", &g_Time, 1);
  326. services->setPixelShaderConstant ("g_Time", &g_Time, 1);
  327. std::map<std::string, ShaderParameter*>::const_iterator vertcur = this->m_vertexVariables.begin ();
  328. std::map<std::string, ShaderParameter*>::const_iterator vertend = this->m_vertexVariables.end ();
  329. for (; vertcur != vertend; vertcur ++)
  330. {
  331. switch ((*vertcur).second->type)
  332. {
  333. case ParameterType::TYPE_FLOAT:
  334. services->setVertexShaderConstant ((*vertcur).first.c_str (), (irr::f32*) (*vertcur).second->value, 1);
  335. break;
  336. case ParameterType::TYPE_VEC2:
  337. services->setVertexShaderConstant ((*vertcur).first.c_str (), (irr::f32*) (*vertcur).second->value, 2);
  338. break;
  339. case ParameterType::TYPE_VEC3:
  340. services->setVertexShaderConstant ((*vertcur).first.c_str (), (irr::f32*) (*vertcur).second->value, 3);
  341. break;
  342. case ParameterType::TYPE_VEC4:
  343. services->setVertexShaderConstant ((*vertcur).first.c_str (), (irr::f32*) (*vertcur).second->value, 4);
  344. break;
  345. case ParameterType::TYPE_INT:
  346. services->setVertexShaderConstant ((*vertcur).first.c_str (), (irr::s32*) (*vertcur).second->value, 1);
  347. break;
  348. }
  349. }
  350. std::map<std::string, ShaderParameter*>::const_iterator pixelcur = this->m_pixelVariables.begin ();
  351. std::map<std::string, ShaderParameter*>::const_iterator pixelend = this->m_pixelVariables.end ();
  352. for (; pixelcur != pixelend; pixelcur ++)
  353. {
  354. switch ((*pixelcur).second->type)
  355. {
  356. case ParameterType::TYPE_FLOAT:
  357. services->setPixelShaderConstant ((*pixelcur).first.c_str (), (irr::f32*) (*pixelcur).second->value, 1);
  358. break;
  359. case ParameterType::TYPE_VEC2:
  360. services->setPixelShaderConstant ((*pixelcur).first.c_str (), (irr::f32*) (*pixelcur).second->value, 2);
  361. break;
  362. case ParameterType::TYPE_VEC3:
  363. services->setPixelShaderConstant ((*pixelcur).first.c_str (), (irr::f32*) (*pixelcur).second->value, 3);
  364. break;
  365. case ParameterType::TYPE_VEC4:
  366. services->setPixelShaderConstant ((*pixelcur).first.c_str (), (irr::f32*) (*pixelcur).second->value, 4);
  367. break;
  368. case ParameterType::TYPE_INT:
  369. services->setPixelShaderConstant ((*pixelcur).first.c_str (), (irr::s32*) (*pixelcur).second->value, 1);
  370. break;
  371. }
  372. }
  373. /*services->setVertexShaderConstant ("g_AnimationSpeed", &g_AnimationSpeed, 1);
  374. services->setVertexShaderConstant ("g_Scale", &g_Scale, 1);
  375. services->setVertexShaderConstant ("g_ScrollSpeed", &g_ScrollSpeed, 1);
  376. services->setVertexShaderConstant ("g_Direction", &g_Direction, 1);*/
  377. services->setVertexShaderConstant ("g_ModelViewProjectionMatrix", worldViewProj.pointer(), 16);
  378. services->setVertexShaderConstant ("g_Texture0Resolution", g_Texture1Resolution, 4);
  379. services->setVertexShaderConstant ("g_Texture1Resolution", g_Texture1Resolution, 4);
  380. services->setVertexShaderConstant ("g_Texture2Resolution", g_Texture1Resolution, 4);
  381. // TODO: Support up to 7 materials (as wallpaper engine)
  382. /*services->setPixelShaderConstant ("g_Strength", &g_Strength, 1);
  383. services->setPixelShaderConstant ("g_SpecularPower", &g_SpecularPower, 1);
  384. services->setPixelShaderConstant ("g_SpecularStrength", &g_SpecularStrength, 1);
  385. services->setPixelShaderConstant ("g_SpecularColor", g_SpecularColor, 3);*/
  386. services->setPixelShaderConstant ("g_Texture0", &g_Texture0, 1);
  387. services->setPixelShaderConstant ("g_Texture1", &g_Texture1, 1);
  388. services->setPixelShaderConstant ("g_Texture2", &g_Texture2, 1);
  389. }
  390. };