CPass.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #pragma once
  2. #include <functional>
  3. #include <glm/gtc/type_ptr.hpp>
  4. #include <utility>
  5. #include "../../TextureProvider.h"
  6. #include "WallpaperEngine/Data/Model/Material.h"
  7. #include "WallpaperEngine/Render/CFBO.h"
  8. #include "WallpaperEngine/Render/FBOProvider.h"
  9. #include "WallpaperEngine/Render/Helpers/ContextAware.h"
  10. #include "WallpaperEngine/Render/Shaders/Shader.h"
  11. #include "WallpaperEngine/Render/Shaders/Variables/ShaderVariable.h"
  12. namespace WallpaperEngine::Render::Objects {
  13. class CRenderable;
  14. }
  15. namespace WallpaperEngine::Render::Objects::Effects {
  16. using namespace WallpaperEngine::Render;
  17. using namespace WallpaperEngine::Render::Shaders::Variables;
  18. using namespace WallpaperEngine::Data::Model;
  19. class CPass final : public Helpers::ContextAware {
  20. public:
  21. CPass (
  22. CRenderable& renderable, std::shared_ptr<const FBOProvider> fboProvider, const MaterialPass& pass,
  23. std::optional<std::reference_wrapper<const ImageEffectPassOverride>> override,
  24. std::optional<std::reference_wrapper<const TextureMap>> binds,
  25. std::optional<std::reference_wrapper<std::string>> target
  26. );
  27. ~CPass ();
  28. void render ();
  29. void setDestination (std::shared_ptr<const CFBO> drawTo);
  30. void setInput (std::shared_ptr<const TextureProvider> input);
  31. void setPreviousInput (std::shared_ptr<const TextureProvider> input);
  32. void setTexCoord (GLuint texcoord);
  33. void setPosition (GLuint position);
  34. void setModelViewProjectionMatrix (const glm::mat4* projection);
  35. void setModelViewProjectionMatrixInverse (const glm::mat4* projection);
  36. void setModelMatrix (const glm::mat4* model);
  37. void setViewProjectionMatrix (const glm::mat4* viewProjection);
  38. void setBlendingMode (BlendingMode blendingmode);
  39. [[nodiscard]] BlendingMode getBlendingMode () const;
  40. [[nodiscard]] std::shared_ptr<const CFBO> resolveFBO (const std::string& name) const;
  41. [[nodiscard]] std::shared_ptr<const FBOProvider> getFBOProvider () const;
  42. [[nodiscard]] const CRenderable& getRenderable () const;
  43. [[nodiscard]] const MaterialPass& getPass () const;
  44. [[nodiscard]] std::optional<std::reference_wrapper<std::string>> getTarget () const;
  45. [[nodiscard]] Render::Shaders::Shader* getShader () const;
  46. [[nodiscard]] GLuint getProgramID () const;
  47. // Custom geometry rendering support (for particles, etc.)
  48. using GeometryCallback = std::function<void ()>;
  49. void
  50. setGeometryCallback (GeometryCallback setupAttribs, GeometryCallback drawGeometry, GeometryCallback cleanupAttribs);
  51. // Public uniform setters for external callers (pointer-based, updated per-frame)
  52. void addUniform (const std::string& name, const float* value, int count = 1);
  53. void addUniform (const std::string& name, const glm::vec3* value);
  54. void addUniform (const std::string& name, const glm::vec4* value);
  55. void addUniform (const std::string& name, const glm::mat4* value);
  56. private:
  57. enum UniformType {
  58. Float = 0,
  59. Matrix3 = 1,
  60. Matrix4 = 2,
  61. Integer = 3,
  62. Vector2 = 4,
  63. Vector3 = 5,
  64. Vector4 = 6,
  65. Double = 7
  66. };
  67. class UniformEntry {
  68. public:
  69. UniformEntry (const GLint id, std::string name, UniformType type, const void* value, int count) :
  70. id (id), name (std::move (name)), type (type), value (value), count (count) { }
  71. const GLint id;
  72. std::string name;
  73. UniformType type;
  74. const void* value;
  75. int count;
  76. };
  77. class ReferenceUniformEntry {
  78. public:
  79. ReferenceUniformEntry (const GLint id, std::string name, UniformType type, const void** value) :
  80. id (id), name (std::move (name)), type (type), value (value) { }
  81. const GLint id;
  82. std::string name;
  83. UniformType type;
  84. const void** value;
  85. };
  86. class AttribEntry {
  87. public:
  88. AttribEntry (const GLint id, std::string name, GLint type, GLint elements, const GLuint* value) :
  89. id (id), name (std::move (name)), type (type), elements (elements), value (value) { }
  90. const GLint id;
  91. std::string name;
  92. GLint type;
  93. GLint elements;
  94. const GLuint* value;
  95. };
  96. struct TextureAnimationState {
  97. uint32_t currentTexture = 0;
  98. glm::vec2 translation = { 0.0f, 0.0f };
  99. glm::vec4 rotation = { 0.0f, 0.0f, 0.0f, 0.0f };
  100. };
  101. static GLuint compileShader (const char* shader, GLuint type);
  102. void setupShaders ();
  103. void setupShaderVariables ();
  104. void setupUniforms ();
  105. void setupTextureUniforms ();
  106. void setupAttributes ();
  107. void addAttribute (const std::string& name, GLint type, GLint elements, const GLuint* value);
  108. void addUniform (ShaderVariable* value);
  109. void addUniform (const ShaderVariable* value, const DynamicValue* setting);
  110. void addUniform (const std::string& name, int value);
  111. void addUniform (const std::string& name, double value);
  112. void addUniform (const std::string& name, float value);
  113. void addUniform (const std::string& name, glm::vec2 value);
  114. void addUniform (const std::string& name, glm::vec3 value);
  115. void addUniform (const std::string& name, glm::vec4 value);
  116. void addUniform (const std::string& name, const glm::mat3& value);
  117. void addUniform (const std::string& name, glm::mat4 value);
  118. void addUniform (const std::string& name, const int* value, int count = 1);
  119. void addUniform (const std::string& name, const double* value, int count = 1);
  120. void addUniform (const std::string& name, const glm::vec2* value);
  121. void addUniform (const std::string& name, const glm::mat3* value);
  122. void addUniform (const std::string& name, const int** value);
  123. void addUniform (const std::string& name, const double** value);
  124. void addUniform (const std::string& name, const float** value);
  125. void addUniform (const std::string& name, const glm::vec2** value);
  126. void addUniform (const std::string& name, const glm::vec3** value);
  127. void addUniform (const std::string& name, const glm::vec4** value);
  128. void addUniform (const std::string& name, const glm::mat3** value);
  129. void addUniform (const std::string& name, const glm::mat4** value);
  130. template <typename T> void addUniform (const std::string& name, UniformType type, T value);
  131. template <typename T> void addUniform (const std::string& name, UniformType type, T* value, int count = 1);
  132. template <typename T> void addUniform (const std::string& name, UniformType type, T** value);
  133. void setupRenderFramebuffer () const;
  134. void setupRenderTexture ();
  135. [[nodiscard]] std::shared_ptr<const TextureProvider> resolveTexture0 ();
  136. [[nodiscard]] TextureAnimationState
  137. resolveTextureAnimationState (const std::shared_ptr<const TextureProvider>& texture) const;
  138. void bindTextureUnit (int index, const std::shared_ptr<const TextureProvider>& texture, uint32_t frame) const;
  139. void bindTextureOverrides (uint32_t currentTexture, std::shared_ptr<const TextureProvider>& texture0) const;
  140. void setupRenderUniforms ();
  141. void setupRenderReferenceUniforms ();
  142. void setupRenderAttributes () const;
  143. void renderGeometry () const;
  144. void cleanupRenderSetup ();
  145. std::shared_ptr<const TextureProvider> resolveTexture (
  146. std::shared_ptr<const TextureProvider> expected, int index,
  147. std::shared_ptr<const TextureProvider> previous = nullptr
  148. );
  149. CRenderable& m_renderable;
  150. std::shared_ptr<const FBOProvider> m_fboProvider;
  151. const MaterialPass& m_pass;
  152. const TextureMap& m_binds;
  153. const ImageEffectPassOverride& m_override;
  154. std::optional<std::reference_wrapper<std::string>> m_target;
  155. std::map<int, std::shared_ptr<const CFBO>> m_fbos = {};
  156. std::map<std::string, int> m_combos = {};
  157. std::vector<AttribEntry*> m_attribs = {};
  158. std::map<std::string, UniformEntry*> m_uniforms = {};
  159. std::map<std::string, ReferenceUniformEntry*> m_referenceUniforms = {};
  160. BlendingMode m_blendingmode = BlendingMode_Normal;
  161. const glm::mat4* m_modelViewProjectionMatrix;
  162. const glm::mat4* m_modelViewProjectionMatrixInverse;
  163. const glm::mat4* m_modelMatrix;
  164. const glm::mat4* m_viewProjectionMatrix;
  165. /**
  166. * Contains the final map of textures to be used
  167. */
  168. std::map<int, std::shared_ptr<const TextureProvider>> m_textures = {};
  169. Render::Shaders::Shader* m_shader = nullptr;
  170. std::shared_ptr<const CFBO> m_drawTo = nullptr;
  171. std::shared_ptr<const TextureProvider> m_input = nullptr;
  172. std::shared_ptr<const TextureProvider> m_previousInput = nullptr;
  173. glm::vec4 m_texture0Resolution = {};
  174. GLuint m_programID;
  175. // shader variables used temporary
  176. GLint g_Texture0Rotation;
  177. GLint g_Texture0Translation;
  178. GLuint a_TexCoord;
  179. GLuint a_Position;
  180. GLuint m_vao;
  181. // Custom geometry callbacks (for particles, etc.)
  182. GeometryCallback m_setupAttribsCallback;
  183. GeometryCallback m_drawGeometryCallback;
  184. GeometryCallback m_cleanupAttribsCallback;
  185. };
  186. } // namespace WallpaperEngine::Render::Objects::Effects