CPass.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. struct TextureChainEntry {
  58. std::shared_ptr<const TextureProvider> texture;
  59. std::shared_ptr<TextureChainEntry> next;
  60. };
  61. enum UniformType {
  62. Float = 0,
  63. Matrix3 = 1,
  64. Matrix4 = 2,
  65. Integer = 3,
  66. Vector2 = 4,
  67. Vector3 = 5,
  68. Vector4 = 6,
  69. Double = 7
  70. };
  71. class UniformEntry {
  72. public:
  73. UniformEntry (
  74. const GLint id, std::string name, UniformType type, const void* value, int count, bool owned = false
  75. ) : id (id), name (std::move (name)), type (type), value (value), count (count), owned (owned) { }
  76. ~UniformEntry ();
  77. const GLint id;
  78. std::string name;
  79. UniformType type;
  80. const void* value;
  81. int count;
  82. // true when "value" is a heap copy this entry allocated (addUniform(name, type, T) by-value
  83. // overload) rather than a pointer into a scene/shader-owned field (addUniform(..., T*, count)),
  84. // so only owned entries may free their value.
  85. bool owned;
  86. };
  87. class ReferenceUniformEntry {
  88. public:
  89. ReferenceUniformEntry (const GLint id, std::string name, UniformType type, const void** value) :
  90. id (id), name (std::move (name)), type (type), value (value) { }
  91. const GLint id;
  92. std::string name;
  93. UniformType type;
  94. const void** value;
  95. };
  96. class AttribEntry {
  97. public:
  98. AttribEntry (const GLint id, std::string name, GLint type, GLint elements, const GLuint* value) :
  99. id (id), name (std::move (name)), type (type), elements (elements), value (value) { }
  100. const GLint id;
  101. std::string name;
  102. GLint type;
  103. GLint elements;
  104. const GLuint* value;
  105. };
  106. struct TextureAnimationState {
  107. uint32_t currentTexture = 0;
  108. glm::vec2 translation = { 0.0f, 0.0f };
  109. glm::vec4 rotation = { 0.0f, 0.0f, 0.0f, 0.0f };
  110. };
  111. static GLuint compileShader (const char* shader, GLuint type);
  112. void setupShaders ();
  113. void setupShaderVariables ();
  114. void setupUniforms ();
  115. void setupTextureUniforms ();
  116. void setupAttributes ();
  117. void addAttribute (const std::string& name, GLint type, GLint elements, const GLuint* value);
  118. void addUniform (ShaderVariable* value);
  119. void addUniform (const ShaderVariable* value, const DynamicValue* setting);
  120. void addUniform (const std::string& name, int value);
  121. void addUniform (const std::string& name, double value);
  122. void addUniform (const std::string& name, float value);
  123. void addUniform (const std::string& name, glm::vec2 value);
  124. void addUniform (const std::string& name, glm::vec3 value);
  125. void addUniform (const std::string& name, glm::vec4 value);
  126. void addUniform (const std::string& name, const glm::mat3& value);
  127. void addUniform (const std::string& name, glm::mat4 value);
  128. void addUniform (const std::string& name, const int* value, int count = 1);
  129. void addUniform (const std::string& name, const double* value, int count = 1);
  130. void addUniform (const std::string& name, const glm::vec2* value);
  131. void addUniform (const std::string& name, const glm::mat3* value);
  132. void addUniform (const std::string& name, const int** value);
  133. void addUniform (const std::string& name, const double** value);
  134. void addUniform (const std::string& name, const float** value);
  135. void addUniform (const std::string& name, const glm::vec2** value);
  136. void addUniform (const std::string& name, const glm::vec3** value);
  137. void addUniform (const std::string& name, const glm::vec4** value);
  138. void addUniform (const std::string& name, const glm::mat3** value);
  139. void addUniform (const std::string& name, const glm::mat4** value);
  140. template <typename T> void addUniform (const std::string& name, UniformType type, T value);
  141. template <typename T> void addUniform (const std::string& name, UniformType type, T* value, int count = 1);
  142. template <typename T> void addUniform (const std::string& name, UniformType type, T** value);
  143. void setupRenderFramebuffer () const;
  144. void setupRenderTexture ();
  145. [[nodiscard]] std::shared_ptr<const TextureProvider> resolveTexture0 ();
  146. [[nodiscard]] TextureAnimationState
  147. resolveTextureAnimationState (const std::shared_ptr<const TextureProvider>& texture) const;
  148. void bindTextureUnit (int index, const std::shared_ptr<const TextureProvider>& texture, uint32_t frame) const;
  149. void bindTextureOverrides (uint32_t currentTexture, std::shared_ptr<const TextureProvider>& texture0) const;
  150. void setupRenderUniforms ();
  151. void setupRenderReferenceUniforms ();
  152. void setupRenderAttributes () const;
  153. void renderGeometry () const;
  154. void cleanupRenderSetup ();
  155. std::shared_ptr<const TextureProvider> resolveTexture (
  156. std::shared_ptr<const TextureProvider> expected, int index,
  157. std::shared_ptr<const TextureProvider> previous = nullptr
  158. );
  159. CRenderable& m_renderable;
  160. std::shared_ptr<const FBOProvider> m_fboProvider;
  161. const MaterialPass& m_pass;
  162. const TextureMap& m_binds;
  163. const ImageEffectPassOverride& m_override;
  164. std::optional<std::reference_wrapper<std::string>> m_target;
  165. std::map<int, std::shared_ptr<const CFBO>> m_fbos = {};
  166. std::map<std::string, int> m_combos = {};
  167. std::vector<AttribEntry*> m_attribs = {};
  168. std::map<std::string, UniformEntry*> m_uniforms = {};
  169. std::map<std::string, ReferenceUniformEntry*> m_referenceUniforms = {};
  170. BlendingMode m_blendingmode = BlendingMode_Normal;
  171. const glm::mat4* m_modelViewProjectionMatrix;
  172. const glm::mat4* m_modelViewProjectionMatrixInverse;
  173. const glm::mat4* m_modelMatrix;
  174. const glm::mat4* m_viewProjectionMatrix;
  175. // full xray support: 0.0/1.0 fed to the g_XrayFullReveal uniform injected by patchXrayFullRevealBypass(),
  176. // updated each frame from state.xray.fullReveal (see render()); m_xrayFullRevealPatched records whether
  177. // that injection actually found its anchors in the compiled shader, for diagnostics
  178. float m_xrayFullReveal = 0.0f;
  179. bool m_xrayFullRevealPatched = false;
  180. /**
  181. * Contains the final map of textures to be used
  182. */
  183. std::map<int, std::shared_ptr<TextureChainEntry>> m_textures = {};
  184. Render::Shaders::Shader* m_shader = nullptr;
  185. std::shared_ptr<const CFBO> m_drawTo = nullptr;
  186. std::shared_ptr<const TextureProvider> m_input = nullptr;
  187. std::shared_ptr<const TextureProvider> m_previousInput = nullptr;
  188. glm::vec4 m_texture0Resolution = {};
  189. GLuint m_programID;
  190. // shader variables used temporary
  191. GLint g_Texture0Rotation;
  192. GLint g_Texture0Translation;
  193. GLuint a_TexCoord;
  194. GLuint a_Position;
  195. GLuint m_vao;
  196. // Custom geometry callbacks (for particles, etc.)
  197. GeometryCallback m_setupAttribsCallback;
  198. GeometryCallback m_drawGeometryCallback;
  199. GeometryCallback m_cleanupAttribsCallback;
  200. };
  201. } // namespace WallpaperEngine::Render::Objects::Effects