effect.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. irr::f32* val = new irr::f32 [4];
  165. val [0] = vec->X;
  166. val [1] = vec->Y;
  167. val [2] = vec->Z;
  168. val [3] = 0.0f;
  169. parameter->value = val;
  170. parameter->type = ParameterType::TYPE_VEC4;
  171. }
  172. else if (param->type == "vec3")
  173. {
  174. irr::core::vector3df* vec = (irr::core::vector3df*) defaultValue;
  175. irr::f32* val = new irr::f32 [3];
  176. val [0] = vec->X;
  177. val [1] = vec->Y;
  178. val [2] = vec->Z;
  179. parameter->value = val;
  180. parameter->type = ParameterType::TYPE_VEC3;
  181. }
  182. else if (param->type == "vec2")
  183. {
  184. irr::core::vector2df* vec = (irr::core::vector2df*) defaultValue;
  185. irr::f32* val = new irr::f32 [2];
  186. val [0] = vec->X;
  187. val [1] = vec->Y;
  188. parameter->value = val;
  189. parameter->type = ParameterType::TYPE_VEC2;
  190. }
  191. else if (param->type == "float")
  192. {
  193. irr::f32* org = (irr::f32*) defaultValue;
  194. irr::f32* val = new irr::f32;
  195. *val = *org;
  196. parameter->value = val;
  197. parameter->type = ParameterType::TYPE_FLOAT;
  198. }
  199. else if (param->type == "int")
  200. {
  201. irr::s32* org = (irr::s32*) defaultValue;
  202. irr::s32* val = new irr::s32;
  203. *val = *org;
  204. parameter->value = val;
  205. parameter->type = ParameterType::TYPE_INT;
  206. }
  207. else if (param->type == "sampler2D")
  208. {
  209. // sampler2D are textures from wallpaper engine
  210. // so these can be ignored as they are later on defined
  211. continue;
  212. }
  213. this->m_vertexVariables.insert (std::pair <std::string, ShaderParameter*> (param->variableName, parameter));
  214. }
  215. }
  216. void effect::parseConstantValues (json data)
  217. {
  218. /*
  219. "ui_editor_properties_animation_speed" : 0.10000000149011612,
  220. "ui_editor_properties_ripple_scale" : 2.5,
  221. "ui_editor_properties_ripple_strength" : 0.070000000298023224
  222. */
  223. json::const_iterator cur = data.begin ();
  224. json::const_iterator end = data.end ();
  225. for (; cur != end; cur ++)
  226. {
  227. std::string name = cur.key ();
  228. void* value = nullptr;
  229. if ((*cur).is_number_float () == true)
  230. {
  231. value = new irr::f32;
  232. *(irr::f32*) value = (*cur).get <irr::f32> ();
  233. }
  234. else if ((*cur).is_number_integer () == true)
  235. {
  236. value = new irr::s32;
  237. *(irr::s32*) value = (*cur).get <irr::s32> ();
  238. }
  239. else if ((*cur).is_string () == true)
  240. {
  241. value = new irr::core::vector3df;
  242. *(irr::core::vector3df*) value = core::ato3vf ((*cur).get <std::string> ().c_str ());
  243. }
  244. this->m_constants.insert (std::pair <std::string, void*> (name, value));
  245. }
  246. }
  247. std::vector<wp::texture*>& effect::getTextureList ()
  248. {
  249. return this->m_textures;
  250. }
  251. irr::s32 effect::getMaterialType ()
  252. {
  253. return this->m_materialType;
  254. }
  255. void effect::OnSetConstants (irr::video::IMaterialRendererServices* services, int32_t userData)
  256. {
  257. // TODO: IMPLEMENT PROPER SHADER CODE
  258. irr::f32 g_AnimationSpeed = 0.1f;
  259. irr::f32 g_Scale = 2.5f;
  260. irr::f32 g_ScrollSpeed = 0.0f;
  261. irr::f32 g_Direction = 0.0f;
  262. irr::f32 g_Strength = 0.07f;
  263. irr::f32 g_SpecularPower = 1.0f;
  264. irr::f32 g_SpecularStrength = 1.0f;
  265. irr::f32 g_SpecularColor [3] = {1.0f, 1.0f, 1.0f};
  266. irr::f32 g_Texture1Resolution [4] = {1.0f, 1.0f, 1.0f, 1.0f};
  267. irr::f32 g_Texture0 = 0;
  268. irr::f32 g_Texture1 = 1;
  269. irr::f32 g_Texture2 = 2;
  270. irr::video::IVideoDriver* driver = services->getVideoDriver ();
  271. irr::core::matrix4 worldViewProj;
  272. worldViewProj = driver->getTransform(irr::video::ETS_PROJECTION);
  273. worldViewProj *= driver->getTransform(irr::video::ETS_VIEW);
  274. worldViewProj *= driver->getTransform(irr::video::ETS_WORLD);
  275. /*
  276. THESE ARE THE VARIABLES AVAILABLE ON SHADERS
  277. uniform float g_Alpha;
  278. uniform vec3 g_Color;
  279. // Application time, starts with 0
  280. uniform float g_Time;
  281. // Maps 24 hrs to [0, 1]
  282. uniform float g_Daytime;
  283. uniform vec2 g_TexelSize;
  284. uniform vec2 g_TexelSizeHalf;
  285. uniform mat4 g_ModelMatrix;
  286. uniform mat4 g_ViewProjectionMatrix;
  287. uniform mat4 g_ModelViewProjectionMatrix;
  288. uniform mat4 g_ModelViewProjectionMatrixInverse;
  289. uniform vec3 g_EyePosition;
  290. uniform vec3 g_ViewUp;
  291. uniform vec3 g_ViewRight;
  292. // Samplers that map to the textures[] array in the material
  293. uniform sampler2D g_Texture0;
  294. uniform sampler2D g_Texture1;
  295. uniform sampler2D g_Texture2;
  296. uniform sampler2D g_Texture3;
  297. uniform sampler2D g_Texture4;
  298. uniform sampler2D g_Texture5;
  299. uniform sampler2D g_Texture6;
  300. uniform sampler2D g_Texture7;
  301. // For sprite sheets (GIFs)
  302. uniform vec4 g_Texture0Rotation;
  303. uniform vec4 g_Texture1Rotation;
  304. uniform vec4 g_Texture2Rotation;
  305. uniform vec4 g_Texture3Rotation;
  306. uniform vec4 g_Texture4Rotation;
  307. uniform vec4 g_Texture5Rotation;
  308. uniform vec4 g_Texture6Rotation;
  309. uniform vec4 g_Texture7Rotation;
  310. uniform vec2 g_Texture0Translation;
  311. uniform vec2 g_Texture1Translation;
  312. uniform vec2 g_Texture2Translation;
  313. uniform vec2 g_Texture3Translation;
  314. uniform vec2 g_Texture4Translation;
  315. uniform vec2 g_Texture5Translation;
  316. uniform vec2 g_Texture6Translation;
  317. uniform vec2 g_Texture7Translation;
  318. uniform vec4 g_LightsColorRadius[4]; // color in XYZ, radius in W
  319. uniform vec3 g_LightsPosition[4];
  320. uniform vec3 g_LightAmbientColor;
  321. uniform vec3 g_LightSkylightColor;
  322. // lower frequencies at lower indices
  323. uniform float g_AudioSpectrum16Left[16];
  324. uniform float g_AudioSpectrum16Right[16];
  325. uniform float g_AudioSpectrum32Left[32];
  326. uniform float g_AudioSpectrum32Right[32];
  327. uniform float g_AudioSpectrum64Left[64];
  328. uniform float g_AudioSpectrum64Right[64];
  329. // Normalized in UV space [0, 1]
  330. uniform vec2 g_PointerPosition;
  331. */
  332. services->setVertexShaderConstant ("g_Time", &g_Time, 1);
  333. services->setPixelShaderConstant ("g_Time", &g_Time, 1);
  334. std::map<std::string, ShaderParameter*>::const_iterator vertcur = this->m_vertexVariables.begin ();
  335. std::map<std::string, ShaderParameter*>::const_iterator vertend = this->m_vertexVariables.end ();
  336. for (; vertcur != vertend; vertcur ++)
  337. {
  338. switch ((*vertcur).second->type)
  339. {
  340. case ParameterType::TYPE_FLOAT:
  341. services->setVertexShaderConstant ((*vertcur).first.c_str (), (irr::f32*) (*vertcur).second->value, 1);
  342. break;
  343. case ParameterType::TYPE_VEC2:
  344. services->setVertexShaderConstant ((*vertcur).first.c_str (), (irr::f32*) (*vertcur).second->value, 2);
  345. break;
  346. case ParameterType::TYPE_VEC3:
  347. services->setVertexShaderConstant ((*vertcur).first.c_str (), (irr::f32*) (*vertcur).second->value, 3);
  348. break;
  349. case ParameterType::TYPE_VEC4:
  350. services->setVertexShaderConstant ((*vertcur).first.c_str (), (irr::f32*) (*vertcur).second->value, 4);
  351. break;
  352. case ParameterType::TYPE_INT:
  353. services->setVertexShaderConstant ((*vertcur).first.c_str (), (irr::s32*) (*vertcur).second->value, 1);
  354. break;
  355. }
  356. }
  357. std::map<std::string, ShaderParameter*>::const_iterator pixelcur = this->m_pixelVariables.begin ();
  358. std::map<std::string, ShaderParameter*>::const_iterator pixelend = this->m_pixelVariables.end ();
  359. for (; pixelcur != pixelend; pixelcur ++)
  360. {
  361. switch ((*pixelcur).second->type)
  362. {
  363. case ParameterType::TYPE_FLOAT:
  364. services->setPixelShaderConstant ((*pixelcur).first.c_str (), (irr::f32*) (*pixelcur).second->value, 1);
  365. break;
  366. case ParameterType::TYPE_VEC2:
  367. services->setPixelShaderConstant ((*pixelcur).first.c_str (), (irr::f32*) (*pixelcur).second->value, 2);
  368. break;
  369. case ParameterType::TYPE_VEC3:
  370. services->setPixelShaderConstant ((*pixelcur).first.c_str (), (irr::f32*) (*pixelcur).second->value, 3);
  371. break;
  372. case ParameterType::TYPE_VEC4:
  373. services->setPixelShaderConstant ((*pixelcur).first.c_str (), (irr::f32*) (*pixelcur).second->value, 4);
  374. break;
  375. case ParameterType::TYPE_INT:
  376. services->setPixelShaderConstant ((*pixelcur).first.c_str (), (irr::s32*) (*pixelcur).second->value, 1);
  377. break;
  378. }
  379. }
  380. /*services->setVertexShaderConstant ("g_AnimationSpeed", &g_AnimationSpeed, 1);
  381. services->setVertexShaderConstant ("g_Scale", &g_Scale, 1);
  382. services->setVertexShaderConstant ("g_ScrollSpeed", &g_ScrollSpeed, 1);
  383. services->setVertexShaderConstant ("g_Direction", &g_Direction, 1);*/
  384. services->setVertexShaderConstant ("g_ModelViewProjectionMatrix", worldViewProj.pointer(), 16);
  385. services->setVertexShaderConstant ("g_Texture0Resolution", g_Texture1Resolution, 4);
  386. services->setVertexShaderConstant ("g_Texture1Resolution", g_Texture1Resolution, 4);
  387. services->setVertexShaderConstant ("g_Texture2Resolution", g_Texture1Resolution, 4);
  388. // TODO: Support up to 7 materials (as wallpaper engine)
  389. /*services->setPixelShaderConstant ("g_Strength", &g_Strength, 1);
  390. services->setPixelShaderConstant ("g_SpecularPower", &g_SpecularPower, 1);
  391. services->setPixelShaderConstant ("g_SpecularStrength", &g_SpecularStrength, 1);
  392. services->setPixelShaderConstant ("g_SpecularColor", g_SpecularColor, 3);*/
  393. services->setPixelShaderConstant ("g_Texture0", &g_Texture0, 1);
  394. services->setPixelShaderConstant ("g_Texture1", &g_Texture1, 1);
  395. services->setPixelShaderConstant ("g_Texture2", &g_Texture2, 1);
  396. }
  397. };