effect.cpp 18 KB

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