CPass.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #include <sstream>
  2. #include "CPass.h"
  3. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariable.h"
  4. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableFloat.h"
  5. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableInteger.h"
  6. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableVector2.h"
  7. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableVector3.h"
  8. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableVector4.h"
  9. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableFloatPointer.h"
  10. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableVector2Pointer.h"
  11. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.h"
  12. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstantFloat.h"
  13. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstantInteger.h"
  14. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstantVector3.h"
  15. using namespace WallpaperEngine::Core::Objects::Effects::Constants;
  16. using namespace WallpaperEngine::Render::Shaders::Variables;
  17. using namespace WallpaperEngine::Render::Objects::Effects;
  18. extern double g_Time;
  19. CPass::CPass (CMaterial* material, Core::Objects::Images::Materials::CPass* pass) :
  20. m_material (material),
  21. m_pass (pass)
  22. {
  23. this->m_fragShader = new Render::Shaders::Compiler (
  24. this->m_material->getImage ()->getContainer (),
  25. pass->getShader (),
  26. Shaders::Compiler::Type_Pixel,
  27. pass->getCombos (),
  28. pass->getConstants ()
  29. );
  30. this->m_fragShader->precompile ();
  31. this->m_vertShader = new Render::Shaders::Compiler (
  32. this->m_material->getImage ()->getContainer (),
  33. pass->getShader (),
  34. Shaders::Compiler::Type_Vertex,
  35. this->m_fragShader->getCombos (),
  36. pass->getConstants ()
  37. );
  38. this->m_vertShader->precompile ();
  39. this->setupTextures ();
  40. this->setupShaders ();
  41. this->setupShaderVariables ();
  42. }
  43. void CPass::render (GLuint drawTo, GLuint input)
  44. {
  45. if (drawTo == 0)
  46. std::cout << "FINAL PASS TO SCREEN --------------------------\n";
  47. else
  48. std::cout << "NEW PASS --------------------------------------\n";
  49. // clear whatever buffer we're drawing to if we're not drawing to screen
  50. if (drawTo > 0)
  51. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  52. // update variables used in the render process (like g_ModelViewProjectionMatrix)
  53. this->m_modelViewProjectionMatrix =
  54. this->m_material->getImage ()->getScene ()->getCamera ()->getProjection () *
  55. this->m_material->getImage ()->getScene ()->getCamera ()->getLookAt () *
  56. glm::mat4 (1.0f);
  57. // use the shader we have registered
  58. glUseProgram (this->m_programID);
  59. // bind the input texture
  60. glActiveTexture (GL_TEXTURE0);
  61. glBindTexture (GL_TEXTURE_2D, input);
  62. std::cout << "Binding texture " << 0 << " to " << input << "\n";
  63. // first bind the textures to their sampler place
  64. {
  65. // set texture slots for the shader
  66. auto cur = this->m_textures.begin ();
  67. auto end = this->m_textures.end ();
  68. for (int index = 1; cur != end; cur ++, index ++)
  69. {
  70. std::cout << "Binding texture " << index << " to " << (*cur)->getTextureID () << "\n";
  71. // set the active texture index
  72. glActiveTexture (GL_TEXTURE0 + index);
  73. // bind the correct texture there
  74. glBindTexture (GL_TEXTURE_2D, (*cur)->getTextureID ());
  75. }
  76. }
  77. // add uniforms
  78. {
  79. auto cur = this->m_uniforms.begin ();
  80. auto end = this->m_uniforms.end ();
  81. for (; cur != end; cur ++)
  82. {
  83. switch ((*cur)->type)
  84. {
  85. case Double:
  86. glUniform1d ((*cur)->id, *static_cast <const double*> ((*cur)->value));
  87. break;
  88. case Float:
  89. glUniform1f ((*cur)->id, *static_cast <const float*> ((*cur)->value));
  90. break;
  91. case Integer:
  92. glUniform1i ((*cur)->id, *static_cast <const int*> ((*cur)->value));
  93. break;
  94. case Vector4:
  95. glUniform4fv ((*cur)->id, 1, glm::value_ptr (*static_cast <const glm::vec4*> ((*cur)->value)));
  96. break;
  97. case Vector3:
  98. glUniform3fv ((*cur)->id, 1, glm::value_ptr (*static_cast <const glm::vec3*> ((*cur)->value)));
  99. break;
  100. case Vector2:
  101. glUniform2fv ((*cur)->id, 1, glm::value_ptr (*static_cast <const glm::vec2*> ((*cur)->value)));
  102. break;
  103. case Matrix4:
  104. glUniformMatrix4fv ((*cur)->id, 1, GL_FALSE, glm::value_ptr (*static_cast <const glm::mat4*> ((*cur)->value)));
  105. break;
  106. }
  107. }
  108. }
  109. if (this->g_Texture0Rotation != -1)
  110. {
  111. #ifdef DEBUG
  112. std::cout << "g_Texture0Rotation = {0.0, 0.0}\n";
  113. #endif /* DEBUG */
  114. glUniform2f (this->g_Texture0Rotation, 0.0f, 0.0f);
  115. }
  116. if (this->g_Texture0Translation != -1)
  117. {
  118. #ifdef DEBUG
  119. std::cout << "g_Texture0Translation = {0.0, 0.0, 0.0, 0.0}\n";
  120. #endif /* DEBUG */
  121. glUniform4f (this->g_Texture0Translation, 0.0f, 0.0f, 0.0f, 0.0f);
  122. }
  123. if (this->a_TexCoord != -1)
  124. {
  125. std::cout << "a_TexCoord present!\n";
  126. glEnableVertexAttribArray (this->a_TexCoord);
  127. glBindBuffer (GL_ARRAY_BUFFER, *this->m_material->getImage ()->getTexCoordBuffer ());
  128. glVertexAttribPointer (
  129. this->a_TexCoord,
  130. 2,
  131. GL_FLOAT,
  132. GL_FALSE,
  133. 0,
  134. nullptr
  135. );
  136. }
  137. if (this->a_Position != -1)
  138. {
  139. std::cout << "a_Position present!\n";
  140. glEnableVertexAttribArray (this->a_Position);
  141. glBindBuffer (GL_ARRAY_BUFFER, (drawTo > 0) ? *this->m_material->getImage ()->getPassVertexBuffer () : *this->m_material->getImage ()->getVertexBuffer ());
  142. glVertexAttribPointer (
  143. this->a_Position,
  144. 3,
  145. GL_FLOAT,
  146. GL_FALSE,
  147. 0,
  148. nullptr
  149. );
  150. }
  151. // start actual rendering now
  152. glBindBuffer (GL_ARRAY_BUFFER, (drawTo > 0) ? *this->m_material->getImage ()->getPassVertexBuffer () : *this->m_material->getImage ()->getVertexBuffer ());
  153. glDrawArrays (GL_TRIANGLES, 0, 6);
  154. if (this->a_Position != -1)
  155. glDisableVertexAttribArray (this->a_Position);
  156. if (this->a_TexCoord != -1)
  157. glDisableVertexAttribArray (this->a_TexCoord);
  158. }
  159. GLuint CPass::compileShader (Render::Shaders::Compiler* shader, GLuint type)
  160. {
  161. // reserve shaders in OpenGL
  162. GLuint shaderID = glCreateShader (type);
  163. // give shader's source code to OpenGL to be compiled
  164. const char* sourcePointer = shader->getCompiled ().c_str ();
  165. glShaderSource (shaderID, 1, &sourcePointer, nullptr);
  166. glCompileShader (shaderID);
  167. GLint result = GL_FALSE;
  168. int infoLogLength = 0;
  169. // ensure the vertex shader was correctly compiled
  170. glGetShaderiv (shaderID, GL_COMPILE_STATUS, &result);
  171. glGetShaderiv (shaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
  172. if (infoLogLength > 0)
  173. {
  174. char* logBuffer = new char [infoLogLength + 1];
  175. // ensure logBuffer ends with a \0
  176. memset (logBuffer, 0, infoLogLength + 1);
  177. // get information about the error
  178. glGetShaderInfoLog (shaderID, infoLogLength, nullptr, logBuffer);
  179. // throw an exception about the issue
  180. std::string message = logBuffer;
  181. // free the buffer
  182. delete[] logBuffer;
  183. // throw an exception
  184. throw std::runtime_error (message);
  185. }
  186. return shaderID;
  187. }
  188. void CPass::setupShaders ()
  189. {
  190. // compile the shaders
  191. GLuint vertexShaderID = compileShader (this->m_vertShader, GL_VERTEX_SHADER);
  192. GLuint fragmentShaderID = compileShader (this->m_fragShader, GL_FRAGMENT_SHADER);
  193. // create the final program
  194. this->m_programID = glCreateProgram ();
  195. // link the shaders together
  196. glAttachShader (this->m_programID, vertexShaderID);
  197. glAttachShader (this->m_programID, fragmentShaderID);
  198. glLinkProgram (this->m_programID);
  199. // check that the shader was properly linked
  200. GLint result = GL_FALSE;
  201. int infoLogLength = 0;
  202. glGetProgramiv (this->m_programID, GL_LINK_STATUS, &result);
  203. glGetProgramiv (this->m_programID, GL_INFO_LOG_LENGTH, &infoLogLength);
  204. if (infoLogLength > 0)
  205. {
  206. char* logBuffer = new char [infoLogLength + 1];
  207. // ensure logBuffer ends with a \0
  208. memset (logBuffer, 0, infoLogLength + 1);
  209. // get information about the error
  210. glGetProgramInfoLog (this->m_programID, infoLogLength, nullptr, logBuffer);
  211. // throw an exception about the issue
  212. std::string message = logBuffer;
  213. // free the buffer
  214. delete[] logBuffer;
  215. // throw an exception
  216. throw std::runtime_error (message);
  217. }
  218. // after being liked shaders can be dettached and deleted
  219. glDetachShader (this->m_programID, vertexShaderID);
  220. glDetachShader (this->m_programID, fragmentShaderID);
  221. glDeleteShader (vertexShaderID);
  222. glDeleteShader (fragmentShaderID);
  223. // setup uniforms
  224. this->setupUniforms ();
  225. // get information from the program, like uniforms, etc
  226. // support three textures for now
  227. this->g_Texture0Rotation = glGetUniformLocation (this->m_programID, "g_Texture0Rotation");
  228. this->g_Texture0Translation = glGetUniformLocation (this->m_programID, "g_Texture0Translation");
  229. // bind a_TexCoord and a_Position
  230. this->a_TexCoord = glGetAttribLocation (this->m_programID, "a_TexCoord");
  231. this->a_Position = glGetAttribLocation (this->m_programID, "a_Position");
  232. }
  233. void CPass::setupUniforms ()
  234. {
  235. // register all the texture uniforms with correct values
  236. this->addUniform ("g_Texture0", 0);
  237. this->addUniform ("g_Texture1", 1);
  238. this->addUniform ("g_Texture2", 2);
  239. this->addUniform ("g_Texture3", 3);
  240. this->addUniform ("g_Texture4", 4);
  241. this->addUniform ("g_Texture5", 5);
  242. this->addUniform ("g_Texture6", 6);
  243. this->addUniform ("g_Texture7", 7);
  244. // register all the texture sizes required
  245. this->addUniform ("g_Texture0Resolution", this->m_material->getImage ()->getTexture ()->getResolution ());
  246. // register the extra texture resolutions
  247. {
  248. auto cur = this->m_textures.begin ();
  249. auto end = this->m_textures.end ();
  250. for (int index = 1; cur != end; cur ++, index ++)
  251. {
  252. std::ostringstream namestream;
  253. namestream << "g_Texture" << index << "Resolution";
  254. this->addUniform (namestream.str (), (*cur)->getResolution ());
  255. }
  256. }
  257. // register variables like brightness and alpha with some default value
  258. this->addUniform ("g_Brightness", 1.0f);
  259. this->addUniform ("g_UserAlpha", 1.0f);
  260. // add some external variables
  261. this->addUniform ("g_Time", &g_Time);
  262. // add model-view-projection matrix
  263. this->addUniform ("g_ModelViewProjectionMatrix", &this->m_modelViewProjectionMatrix);
  264. }
  265. template <typename T>
  266. void CPass::addUniform (const std::string& name, UniformType type, T value)
  267. {
  268. GLint id = glGetUniformLocation (this->m_programID, name.c_str ());
  269. // parameter not found, can be ignored
  270. if (id == -1)
  271. return;
  272. // build a copy of the value and allocate it somewhere
  273. T* newValue = new T (value);
  274. // uniform found, add it to the list
  275. this->m_uniforms.emplace_back (
  276. new UniformEntry (id, name, type, newValue)
  277. );
  278. }
  279. template <typename T>
  280. void CPass::addUniform (const std::string& name, UniformType type, T* value)
  281. {
  282. // this version is used to reference to system variables so things like g_Time works fine
  283. GLint id = glGetUniformLocation (this->m_programID, name.c_str ());
  284. // parameter not found, can be ignored
  285. if (id == -1)
  286. return;
  287. // uniform found, add it to the list
  288. this->m_uniforms.emplace_back (
  289. new UniformEntry (id, name, type, value)
  290. );
  291. }
  292. void CPass::setupTextures ()
  293. {
  294. auto cur = this->m_pass->getTextures ().begin ();
  295. auto end = this->m_pass->getTextures ().end ();
  296. for (int index = 0; cur != end; cur ++, index ++)
  297. {
  298. // ignore first texture as that'll be the input of the last pass/image
  299. if (index == 0)
  300. continue;
  301. uint32_t textureSize = 0;
  302. // get the first texture on the first pass (this one represents the image assigned to this object)
  303. void* textureData = this->m_material->getImage ()->getContainer ()->readTexture (
  304. (*cur), &textureSize
  305. );
  306. // load a new texture and push it into the list
  307. this->m_textures.emplace_back (new CTexture (textureData));
  308. }
  309. }
  310. void CPass::setupShaderVariables ()
  311. {
  312. // find variables in the shaders and set the value with the constants if possible
  313. auto cur = this->m_pass->getConstants ().begin ();
  314. auto end = this->m_pass->getConstants ().end ();
  315. for (; cur != end; cur ++)
  316. {
  317. CShaderVariable* vertexVar = this->m_vertShader->findParameter ((*cur).first);
  318. CShaderVariable* pixelVar = this->m_fragShader->findParameter ((*cur).first);
  319. // variable not found, can be ignored
  320. if (vertexVar == nullptr && pixelVar == nullptr)
  321. continue;
  322. // if both can be found, ensure they're the correct type
  323. if (vertexVar != nullptr && pixelVar != nullptr)
  324. {
  325. if (vertexVar->getType () != pixelVar->getType ())
  326. throw std::runtime_error ("Pixel and vertex shader variable types do not match");
  327. }
  328. // get one instance of it
  329. CShaderVariable* var = vertexVar == nullptr ? pixelVar : vertexVar;
  330. // ensure the shader's and the constant are of the same type
  331. if ((*cur).second->getType () != var->getType ())
  332. throw std::runtime_error ("Constant and pixel/vertex variable are not of the same type");
  333. // now determine the constant's type and register the correct uniform for it
  334. if ((*cur).second->is <CShaderConstantFloat> ())
  335. this->addUniform (var->getName (), (*cur).second->as <CShaderConstantFloat> ()->getValue ());
  336. else if ((*cur).second->is <CShaderConstantInteger> ())
  337. this->addUniform (var->getName (), (*cur).second->as <CShaderConstantInteger> ()->getValue ());
  338. else if ((*cur).second->is <CShaderConstantVector3> ())
  339. this->addUniform (var->getName (), (*cur).second->as <CShaderConstantVector3> ()->getValue ());
  340. }
  341. }
  342. // define some basic methods for the template
  343. /*template double Core::jsonFindDefault (nlohmann::json& data, const char *key, double defaultValue);*/
  344. void CPass::addUniform (const std::string& name, int value)
  345. {
  346. this->addUniform (name, UniformType::Integer, value);
  347. }
  348. void CPass::addUniform (const std::string& name, const int* value)
  349. {
  350. this->addUniform (name, UniformType::Integer, value);
  351. }
  352. void CPass::addUniform (const std::string& name, double value)
  353. {
  354. this->addUniform (name, UniformType::Double, value);
  355. }
  356. void CPass::addUniform (const std::string& name, const double* value)
  357. {
  358. this->addUniform (name, UniformType::Double, value);
  359. }
  360. void CPass::addUniform (const std::string& name, float value)
  361. {
  362. this->addUniform (name, UniformType::Float, value);
  363. }
  364. void CPass::addUniform (const std::string& name, const float* value)
  365. {
  366. this->addUniform (name, UniformType::Float, value);
  367. }
  368. void CPass::addUniform (const std::string& name, glm::vec2 value)
  369. {
  370. this->addUniform (name, UniformType::Vector2, value);
  371. }
  372. void CPass::addUniform (const std::string& name, const glm::vec2* value)
  373. {
  374. this->addUniform (name, UniformType::Vector2, value);
  375. }
  376. void CPass::addUniform (const std::string& name, glm::vec3 value)
  377. {
  378. this->addUniform (name, UniformType::Vector3, value);
  379. }
  380. void CPass::addUniform (const std::string& name, const glm::vec3* value)
  381. {
  382. this->addUniform (name, UniformType::Vector3, value);
  383. }
  384. void CPass::addUniform (const std::string& name, glm::vec4 value)
  385. {
  386. this->addUniform (name, UniformType::Vector4, value);
  387. }
  388. void CPass::addUniform (const std::string& name, const glm::vec4* value)
  389. {
  390. this->addUniform (name, UniformType::Vector4, value);
  391. }
  392. void CPass::addUniform (const std::string& name, glm::mat4 value)
  393. {
  394. this->addUniform (name, UniformType::Matrix4, value);
  395. }
  396. void CPass::addUniform (const std::string& name, const glm::mat4* value)
  397. {
  398. this->addUniform (name, UniformType::Matrix4, value);
  399. }