FrameMaterial.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "FrameMaterial.h"
  2. #include "WallpaperEngine/Data/Parsers/MaterialParser.h"
  3. #include "WallpaperEngine/Logging/Log.h"
  4. #include "WallpaperEngine/Render/CFBO.h"
  5. #include "WallpaperEngine/Render/Shaders/GLSLContext.h"
  6. #include "WallpaperEngine/Render/Wallpapers/CScene.h"
  7. using namespace WallpaperEngine::Render;
  8. using namespace WallpaperEngine::Data::Model;
  9. namespace {
  10. // x, y, z, u, v strip of WE's fullscreen quad, v up
  11. constexpr float kQuad[] = {
  12. -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
  13. -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
  14. };
  15. GLuint compile (const std::string& source, const GLenum type, const std::string& name) {
  16. const GLuint shader = glCreateShader (type);
  17. const char* text = source.c_str ();
  18. glShaderSource (shader, 1, &text, nullptr);
  19. glCompileShader (shader);
  20. GLint compiled = GL_FALSE;
  21. glGetShaderiv (shader, GL_COMPILE_STATUS, &compiled);
  22. if (compiled == GL_FALSE) {
  23. char log[2048] = {};
  24. glGetShaderInfoLog (shader, sizeof (log) - 1, nullptr, log);
  25. sLog.error ("Frame material ", name, " failed to compile: ", log);
  26. }
  27. return shader;
  28. }
  29. }
  30. FrameMaterial::FrameMaterial (
  31. const CWallpaper& owner, const Project& project, const std::string& file, const ComboMap& combos
  32. ) : m_owner (owner), m_file (file) {
  33. this->m_material = Data::Parsers::MaterialParser::load (project, file);
  34. if (this->m_material == nullptr || this->m_material->passes.empty ()) {
  35. sLog.error ("Frame material ", file, " could not be loaded");
  36. return;
  37. }
  38. this->m_pass = this->m_material->passes.front ().get ();
  39. this->m_combos = this->m_pass->combos;
  40. for (const auto& [name, value] : combos) {
  41. this->m_combos.insert_or_assign (name, value);
  42. }
  43. // scene wide defines (sub_1401A5C40)
  44. if (const auto* scene = owner.is<Wallpapers::CScene> () ? owner.as<Wallpapers::CScene> () : nullptr) {
  45. if (scene->getCamera ().isOrthogonal ()) {
  46. this->m_combos.insert_or_assign ("SCENE_ORTHO", 1);
  47. }
  48. if (scene->isHDR ()) {
  49. this->m_combos.insert_or_assign ("HDR", 1);
  50. }
  51. }
  52. static const ShaderConstantMap noConstants;
  53. try {
  54. this->m_shader = std::make_unique<Shaders::Shader> (
  55. *project.assetLocator, this->m_pass->shader, this->m_combos, this->m_overrideCombos, this->m_pass->textures,
  56. this->m_noTextures, noConstants
  57. );
  58. const auto sources = Shaders::GLSLContext::get ().toGlsl (
  59. this->m_shader->vertex (), this->m_shader->fragment (), this->m_pass->shader
  60. );
  61. this->link (sources.vertex, sources.fragment, this->m_pass->shader);
  62. } catch (const std::exception& e) {
  63. sLog.error ("Frame material ", file, " has no usable shader: ", e.what ());
  64. this->m_shader = nullptr;
  65. return;
  66. }
  67. for (const auto* unit : { &this->m_shader->getVertex (), &this->m_shader->getFragment () }) {
  68. for (const auto* parameter : unit->getParameters ()) {
  69. switch (parameter->getType ()) {
  70. case DynamicValue::Float:
  71. this->setValue (parameter->getName (), glm::vec4 (parameter->getFloat ()), 1);
  72. break;
  73. case DynamicValue::Vec2:
  74. this->setValue (parameter->getName (), glm::vec4 (parameter->getVec2 (), 0.0f, 0.0f), 2);
  75. break;
  76. case DynamicValue::Vec3:
  77. this->setValue (parameter->getName (), glm::vec4 (parameter->getVec3 (), 0.0f), 3);
  78. break;
  79. case DynamicValue::Vec4:
  80. this->setValue (parameter->getName (), parameter->getVec4 (), 4);
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. }
  87. for (const auto& [name, setting] : this->m_pass->constants) {
  88. const auto& value = *setting->value;
  89. switch (value.getType ()) {
  90. case DynamicValue::Float:
  91. this->setConstant (name, value.getFloat ());
  92. break;
  93. case DynamicValue::Vec3:
  94. this->setConstant (name, value.getVec3 ());
  95. break;
  96. case DynamicValue::Vec4:
  97. this->setConstant (name, value.getVec4 ());
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. GLint previousVao = 0;
  104. glGetIntegerv (GL_VERTEX_ARRAY_BINDING, &previousVao);
  105. glGenVertexArrays (1, &this->m_vao);
  106. glBindVertexArray (this->m_vao);
  107. glGenBuffers (1, &this->m_vertices);
  108. glBindBuffer (GL_ARRAY_BUFFER, this->m_vertices);
  109. glBufferData (GL_ARRAY_BUFFER, sizeof (kQuad), kQuad, GL_STATIC_DRAW);
  110. if (this->a_Position != -1) {
  111. glEnableVertexAttribArray (this->a_Position);
  112. glVertexAttribPointer (this->a_Position, 3, GL_FLOAT, GL_FALSE, 5 * sizeof (float), nullptr);
  113. }
  114. if (this->a_TexCoord != -1) {
  115. glEnableVertexAttribArray (this->a_TexCoord);
  116. glVertexAttribPointer (
  117. this->a_TexCoord, 2, GL_FLOAT, GL_FALSE, 5 * sizeof (float),
  118. std::bit_cast<void*> (static_cast<uintptr_t> (3 * sizeof (float)))
  119. );
  120. }
  121. glBindVertexArray (previousVao);
  122. }
  123. FrameMaterial::~FrameMaterial () {
  124. if (this->m_program != GL_NONE) {
  125. glDeleteProgram (this->m_program);
  126. }
  127. if (this->m_vertices != GL_NONE) {
  128. glDeleteBuffers (1, &this->m_vertices);
  129. }
  130. if (this->m_vao != GL_NONE) {
  131. glDeleteVertexArrays (1, &this->m_vao);
  132. }
  133. }
  134. void FrameMaterial::link (const std::string& vertex, const std::string& fragment, const std::string& name) {
  135. const GLuint vertexShader = compile (vertex, GL_VERTEX_SHADER, name);
  136. const GLuint fragmentShader = compile (fragment, GL_FRAGMENT_SHADER, name);
  137. this->m_program = glCreateProgram ();
  138. glAttachShader (this->m_program, vertexShader);
  139. glAttachShader (this->m_program, fragmentShader);
  140. glLinkProgram (this->m_program);
  141. glDeleteShader (vertexShader);
  142. glDeleteShader (fragmentShader);
  143. GLint linked = GL_FALSE;
  144. glGetProgramiv (this->m_program, GL_LINK_STATUS, &linked);
  145. if (linked == GL_FALSE) {
  146. char log[2048] = {};
  147. glGetProgramInfoLog (this->m_program, sizeof (log) - 1, nullptr, log);
  148. sLog.error ("Frame material ", name, " failed to link: ", log);
  149. glDeleteProgram (this->m_program);
  150. this->m_program = GL_NONE;
  151. return;
  152. }
  153. #if !NDEBUG
  154. glObjectLabel (GL_PROGRAM, this->m_program, -1, this->m_file.c_str ());
  155. #endif
  156. this->a_Position = glGetAttribLocation (this->m_program, "a_Position");
  157. this->a_TexCoord = glGetAttribLocation (this->m_program, "a_TexCoord");
  158. // translated GLSL collapses sampler bindings to 0
  159. glUseProgram (this->m_program);
  160. for (int index = 0; index <= 9; index++) {
  161. const std::string sampler = "g_Texture" + std::to_string (index);
  162. if (const GLint location = glGetUniformLocation (this->m_program, sampler.c_str ()); location != -1) {
  163. glUniform1i (location, index);
  164. }
  165. }
  166. }
  167. std::string FrameMaterial::uniformOf (const std::string& constant) const {
  168. if (this->m_shader == nullptr) {
  169. return {};
  170. }
  171. for (const auto* unit : { &this->m_shader->getFragment (), &this->m_shader->getVertex () }) {
  172. for (const auto* parameter : unit->getParameters ()) {
  173. if (parameter->getIdentifierName () == constant) {
  174. return parameter->getName ();
  175. }
  176. }
  177. }
  178. return {};
  179. }
  180. void FrameMaterial::setValue (const std::string& uniform, const glm::vec4& value, const int components) {
  181. if (uniform.empty ()) {
  182. return;
  183. }
  184. this->m_values.insert_or_assign (uniform, Value { .value = value, .components = components });
  185. }
  186. void FrameMaterial::setTexture (const int index, const GLuint texture, const GLenum target) {
  187. this->m_textures.insert_or_assign (index, Texture { .texture = texture, .target = target });
  188. }
  189. void FrameMaterial::setConstant (const std::string& name, const float value) {
  190. this->setValue (this->uniformOf (name), glm::vec4 (value), 1);
  191. }
  192. void FrameMaterial::setConstant (const std::string& name, const glm::vec3& value) {
  193. this->setValue (this->uniformOf (name), glm::vec4 (value, 0.0f), 3);
  194. }
  195. void FrameMaterial::setConstant (const std::string& name, const glm::vec4& value) {
  196. this->setValue (this->uniformOf (name), value, 4);
  197. }
  198. void FrameMaterial::setUniform (const std::string& name, const float value) {
  199. this->setValue (name, glm::vec4 (value), 1);
  200. }
  201. void FrameMaterial::setUniform (const std::string& name, const glm::vec2& value) {
  202. this->setValue (name, glm::vec4 (value, 0.0f, 0.0f), 2);
  203. }
  204. void FrameMaterial::setUniform (const std::string& name, const glm::vec4& value) { this->setValue (name, value, 4); }
  205. void FrameMaterial::draw (const CFBO& target) const {
  206. this->draw (target.getFramebuffer (), { target.getRealWidth (), target.getRealHeight () });
  207. }
  208. void FrameMaterial::draw (const GLuint framebuffer, const glm::ivec2& size) const {
  209. if (this->m_program == GL_NONE) {
  210. return;
  211. }
  212. glBindFramebuffer (GL_FRAMEBUFFER, framebuffer);
  213. glViewport (0, 0, size.x, size.y);
  214. glUseProgram (this->m_program);
  215. // util materials: no cull, no depth; additive (bloom), translucent (fade), else replace
  216. glDisable (GL_DEPTH_TEST);
  217. glDepthMask (GL_FALSE);
  218. glDisable (GL_CULL_FACE);
  219. glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  220. if (this->m_pass->blending == BlendingMode_Additive) {
  221. glEnable (GL_BLEND);
  222. glBlendFunc (GL_ONE, GL_ONE);
  223. } else if (this->m_pass->blending == BlendingMode_Translucent) {
  224. glEnable (GL_BLEND);
  225. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  226. } else {
  227. glDisable (GL_BLEND);
  228. }
  229. for (const auto& [uniform, value] : this->m_values) {
  230. const GLint location = glGetUniformLocation (this->m_program, uniform.c_str ());
  231. if (location == -1) {
  232. continue;
  233. }
  234. switch (value.components) {
  235. case 1:
  236. glUniform1f (location, value.value.x);
  237. break;
  238. case 2:
  239. glUniform2fv (location, 1, &value.value.x);
  240. break;
  241. case 3:
  242. glUniform3fv (location, 1, &value.value.x);
  243. break;
  244. default:
  245. glUniform4fv (location, 1, &value.value.x);
  246. break;
  247. }
  248. }
  249. std::map<int, Texture> textures = this->m_textures;
  250. for (const auto& [index, name] : this->m_pass->textures) {
  251. if (textures.contains (index)) {
  252. continue;
  253. }
  254. if (const auto fbo = this->m_owner.find (name); fbo != nullptr) {
  255. textures.emplace (index, Texture { .texture = fbo->getTextureID (0) });
  256. }
  257. }
  258. for (const auto& [index, texture] : textures) {
  259. glActiveTexture (GL_TEXTURE0 + index);
  260. glBindTexture (texture.target, texture.texture);
  261. }
  262. glActiveTexture (GL_TEXTURE0);
  263. GLint previousVao = 0;
  264. glGetIntegerv (GL_VERTEX_ARRAY_BINDING, &previousVao);
  265. glBindVertexArray (this->m_vao);
  266. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  267. glBindVertexArray (previousVao);
  268. glDisable (GL_BLEND);
  269. }