RmlUi_Renderer_GL3.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_BACKENDS_RENDERER_GL3_H
  29. #define RMLUI_BACKENDS_RENDERER_GL3_H
  30. #include <RmlUi/Core/RenderInterface.h>
  31. #include <RmlUi/Core/Types.h>
  32. #include <bitset>
  33. enum class ProgramId;
  34. enum class UniformId;
  35. class RenderLayerStack;
  36. namespace Gfx {
  37. struct ProgramData;
  38. struct FramebufferData;
  39. } // namespace Gfx
  40. class RenderInterface_GL3 : public Rml::RenderInterface {
  41. public:
  42. RenderInterface_GL3();
  43. ~RenderInterface_GL3();
  44. // Returns true if the renderer was successfully constructed.
  45. explicit operator bool() const { return static_cast<bool>(program_data); }
  46. // The viewport should be updated whenever the window size changes.
  47. void SetViewport(int viewport_width, int viewport_height, int viewport_offset_x = 0, int viewport_offset_y = 0);
  48. // Sets up OpenGL states for taking rendering commands from RmlUi.
  49. void BeginFrame();
  50. // Draws the result to the backbuffer and restores OpenGL state.
  51. void EndFrame();
  52. // Optional, can be used to clear the active framebuffer.
  53. void Clear();
  54. // -- Inherited from Rml::RenderInterface --
  55. Rml::CompiledGeometryHandle CompileGeometry(Rml::Span<const Rml::Vertex> vertices, Rml::Span<const int> indices) override;
  56. void RenderGeometry(Rml::CompiledGeometryHandle handle, Rml::Vector2f translation, Rml::TextureHandle texture) override;
  57. void ReleaseGeometry(Rml::CompiledGeometryHandle handle) override;
  58. Rml::TextureHandle LoadTexture(Rml::Vector2i& texture_dimensions, const Rml::String& source) override;
  59. Rml::TextureHandle GenerateTexture(Rml::Span<const Rml::byte> source_data, Rml::Vector2i source_dimensions) override;
  60. void ReleaseTexture(Rml::TextureHandle texture_handle) override;
  61. void EnableScissorRegion(bool enable) override;
  62. void SetScissorRegion(Rml::Rectanglei region) override;
  63. void EnableClipMask(bool enable) override;
  64. void RenderToClipMask(Rml::ClipMaskOperation mask_operation, Rml::CompiledGeometryHandle geometry, Rml::Vector2f translation) override;
  65. void SetTransform(const Rml::Matrix4f* transform) override;
  66. Rml::LayerHandle PushLayer() override;
  67. void CompositeLayers(Rml::LayerHandle source, Rml::LayerHandle destination, Rml::BlendMode blend_mode,
  68. Rml::Span<const Rml::CompiledFilterHandle> filters) override;
  69. void PopLayer() override;
  70. Rml::TextureHandle SaveLayerAsTexture() override;
  71. Rml::CompiledFilterHandle SaveLayerAsMaskImage() override;
  72. Rml::CompiledFilterHandle CompileFilter(const Rml::String& name, const Rml::Dictionary& parameters) override;
  73. void ReleaseFilter(Rml::CompiledFilterHandle filter) override;
  74. Rml::CompiledShaderHandle CompileShader(const Rml::String& name, const Rml::Dictionary& parameters) override;
  75. void RenderShader(Rml::CompiledShaderHandle shader_handle, Rml::CompiledGeometryHandle geometry_handle, Rml::Vector2f translation,
  76. Rml::TextureHandle texture) override;
  77. void ReleaseShader(Rml::CompiledShaderHandle effect_handle) override;
  78. // Can be passed to RenderGeometry() to enable texture rendering without changing the bound texture.
  79. static constexpr Rml::TextureHandle TextureEnableWithoutBinding = Rml::TextureHandle(-1);
  80. // Can be passed to RenderGeometry() to leave the bound texture and used program unchanged.
  81. static constexpr Rml::TextureHandle TexturePostprocess = Rml::TextureHandle(-2);
  82. // -- Utility functions for clients --
  83. const Rml::Matrix4f& GetTransform() const;
  84. void ResetProgram();
  85. private:
  86. void UseProgram(ProgramId program_id);
  87. int GetUniformLocation(UniformId uniform_id) const;
  88. void SubmitTransformUniform(Rml::Vector2f translation);
  89. void BlitLayerToPostprocessPrimary(Rml::LayerHandle layer_handle);
  90. void RenderFilters(Rml::Span<const Rml::CompiledFilterHandle> filter_handles);
  91. void SetScissor(Rml::Rectanglei region, bool vertically_flip = false);
  92. void DrawFullscreenQuad();
  93. void DrawFullscreenQuad(Rml::Vector2f uv_offset, Rml::Vector2f uv_scaling = Rml::Vector2f(1.f));
  94. void RenderBlur(float sigma, const Gfx::FramebufferData& source_destination, const Gfx::FramebufferData& temp, Rml::Rectanglei window_flipped);
  95. static constexpr size_t MaxNumPrograms = 32;
  96. std::bitset<MaxNumPrograms> program_transform_dirty;
  97. Rml::Matrix4f transform;
  98. Rml::Matrix4f projection;
  99. ProgramId active_program = {};
  100. Rml::Rectanglei scissor_state;
  101. int viewport_width = 0;
  102. int viewport_height = 0;
  103. int viewport_offset_x = 0;
  104. int viewport_offset_y = 0;
  105. Rml::CompiledGeometryHandle fullscreen_quad_geometry = {};
  106. Rml::UniquePtr<const Gfx::ProgramData> program_data;
  107. /*
  108. Manages render targets, including the layer stack and postprocessing framebuffers.
  109. Layers can be pushed and popped, creating new framebuffers as needed. Typically, geometry is rendered to the top
  110. layer. The layer framebuffers may have MSAA enabled.
  111. Postprocessing framebuffers are separate from the layers, and are commonly used to apply texture-wide effects
  112. such as filters. They are used both as input and output during rendering, and do not use MSAA.
  113. */
  114. class RenderLayerStack {
  115. public:
  116. RenderLayerStack();
  117. ~RenderLayerStack();
  118. // Push a new layer. All references to previously retrieved layers are invalidated.
  119. Rml::LayerHandle PushLayer();
  120. // Pop the top layer. All references to previously retrieved layers are invalidated.
  121. void PopLayer();
  122. const Gfx::FramebufferData& GetLayer(Rml::LayerHandle layer) const;
  123. const Gfx::FramebufferData& GetTopLayer() const;
  124. Rml::LayerHandle GetTopLayerHandle() const;
  125. const Gfx::FramebufferData& GetPostprocessPrimary() { return EnsureFramebufferPostprocess(0); }
  126. const Gfx::FramebufferData& GetPostprocessSecondary() { return EnsureFramebufferPostprocess(1); }
  127. const Gfx::FramebufferData& GetPostprocessTertiary() { return EnsureFramebufferPostprocess(2); }
  128. const Gfx::FramebufferData& GetBlendMask() { return EnsureFramebufferPostprocess(3); }
  129. void SwapPostprocessPrimarySecondary();
  130. void BeginFrame(int new_width, int new_height);
  131. void EndFrame();
  132. private:
  133. void DestroyFramebuffers();
  134. const Gfx::FramebufferData& EnsureFramebufferPostprocess(int index);
  135. int width = 0, height = 0;
  136. // The number of active layers is manually tracked since we re-use the framebuffers stored in the fb_layers stack.
  137. int layers_size = 0;
  138. Rml::Vector<Gfx::FramebufferData> fb_layers;
  139. Rml::Vector<Gfx::FramebufferData> fb_postprocess;
  140. };
  141. RenderLayerStack render_layers;
  142. struct GLStateBackup {
  143. bool enable_cull_face;
  144. bool enable_blend;
  145. bool enable_stencil_test;
  146. bool enable_scissor_test;
  147. bool enable_depth_test;
  148. int viewport[4];
  149. int scissor[4];
  150. int active_texture;
  151. int stencil_clear_value;
  152. float color_clear_value[4];
  153. unsigned char color_writemask[4];
  154. int blend_equation_rgb;
  155. int blend_equation_alpha;
  156. int blend_src_rgb;
  157. int blend_dst_rgb;
  158. int blend_src_alpha;
  159. int blend_dst_alpha;
  160. struct Stencil {
  161. int func;
  162. int ref;
  163. int value_mask;
  164. int writemask;
  165. int fail;
  166. int pass_depth_fail;
  167. int pass_depth_pass;
  168. };
  169. Stencil stencil_front;
  170. Stencil stencil_back;
  171. };
  172. GLStateBackup glstate_backup = {};
  173. };
  174. /**
  175. Helper functions for the OpenGL 3 renderer.
  176. */
  177. namespace RmlGL3 {
  178. // Loads OpenGL functions. Optionally, the out message describes the loaded GL version or an error message on failure.
  179. bool Initialize(Rml::String* out_message = nullptr);
  180. // Unloads OpenGL functions.
  181. void Shutdown();
  182. } // namespace RmlGL3
  183. #endif