RmlUi_Renderer_GL3.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 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. namespace Gfx {
  33. struct ShadersData;
  34. }
  35. class RenderInterface_GL3 : public Rml::RenderInterface {
  36. public:
  37. RenderInterface_GL3();
  38. ~RenderInterface_GL3();
  39. // Returns true if the renderer was successfully constructed.
  40. explicit operator bool() const { return static_cast<bool>(shaders); }
  41. // The viewport should be updated whenever the window size changes.
  42. void SetViewport(int viewport_width, int viewport_height);
  43. // Sets up OpenGL states for taking rendering commands from RmlUi.
  44. void BeginFrame();
  45. void EndFrame();
  46. // Optional, can be used to clear the framebuffer.
  47. void Clear();
  48. // -- Inherited from Rml::RenderInterface --
  49. void RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture,
  50. const Rml::Vector2f& translation) override;
  51. Rml::CompiledGeometryHandle CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices,
  52. Rml::TextureHandle texture) override;
  53. void RenderCompiledGeometry(Rml::CompiledGeometryHandle geometry, const Rml::Vector2f& translation) override;
  54. void ReleaseCompiledGeometry(Rml::CompiledGeometryHandle geometry) override;
  55. void EnableScissorRegion(bool enable) override;
  56. void SetScissorRegion(int x, int y, int width, int height) override;
  57. bool LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source) override;
  58. bool GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions) override;
  59. void ReleaseTexture(Rml::TextureHandle texture_handle) override;
  60. void SetTransform(const Rml::Matrix4f* transform) override;
  61. // Can be passed to RenderGeometry() to enable texture rendering without changing the bound texture.
  62. static const Rml::TextureHandle TextureEnableWithoutBinding = Rml::TextureHandle(-1);
  63. private:
  64. enum class ProgramId { None, Texture = 1, All = (Texture) };
  65. void SubmitTransformUniform(ProgramId program_id, int uniform_location);
  66. Rml::Matrix4f transform, projection;
  67. ProgramId transform_dirty_state = ProgramId::All;
  68. bool transform_active = false;
  69. enum class ScissoringState { Disable, Scissor, Stencil };
  70. ScissoringState scissoring_state = ScissoringState::Disable;
  71. Rml::TextureHandle texture_white = {};
  72. Rml::TextureHandle active_texture = {};
  73. unsigned int active_vao = {};
  74. int viewport_width = 0;
  75. int viewport_height = 0;
  76. Rml::UniquePtr<Gfx::ShadersData> shaders;
  77. };
  78. /**
  79. Helper functions for the OpenGL 3 renderer.
  80. */
  81. namespace RmlGL3 {
  82. // Loads OpenGL functions. Optionally, the out message describes the loaded GL version or an error message on failure.
  83. bool Initialize(Rml::String* out_message = nullptr);
  84. // Unloads OpenGL functions.
  85. void Shutdown();
  86. } // namespace RmlGL3
  87. #endif