RmlUi_Renderer_SDL_GPU.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_SDL_GPU_H
  29. #define RMLUI_BACKENDS_RENDERER_SDL_GPU_H
  30. #include <RmlUi/Core/RenderInterface.h>
  31. #include <RmlUi/Core/Types.h>
  32. #include <SDL3/SDL.h>
  33. class RenderInterface_SDL_GPU : public Rml::RenderInterface {
  34. public:
  35. RenderInterface_SDL_GPU(SDL_GPUDevice* device, SDL_Window* window);
  36. void Shutdown();
  37. void BeginFrame(SDL_GPUCommandBuffer* command_buffer, SDL_GPUTexture* swapchain_texture, uint32_t width, uint32_t height);
  38. void EndFrame();
  39. Rml::CompiledGeometryHandle CompileGeometry(Rml::Span<const Rml::Vertex> vertices, Rml::Span<const int> indices) override;
  40. void ReleaseGeometry(Rml::CompiledGeometryHandle geometry) override;
  41. void RenderGeometry(Rml::CompiledGeometryHandle handle, Rml::Vector2f translation, Rml::TextureHandle texture) override;
  42. Rml::TextureHandle LoadTexture(Rml::Vector2i& texture_dimensions, const Rml::String& source) override;
  43. Rml::TextureHandle GenerateTexture(Rml::Span<const Rml::byte> source, Rml::Vector2i source_dimensions) override;
  44. void ReleaseTexture(Rml::TextureHandle texture_handle) override;
  45. void EnableScissorRegion(bool enable) override;
  46. void SetScissorRegion(Rml::Rectanglei region) override;
  47. void SetTransform(const Rml::Matrix4f* new_transform) override;
  48. private:
  49. SDL_GPUDevice* device;
  50. SDL_Window* window;
  51. SDL_GPUGraphicsPipeline* texture_pipeline;
  52. SDL_GPUGraphicsPipeline* color_pipeline;
  53. SDL_GPUSampler* linear_sampler;
  54. SDL_GPUCommandBuffer* command_buffer;
  55. SDL_GPUTexture* swapchain_texture;
  56. uint32_t swapchain_width;
  57. uint32_t swapchain_height;
  58. SDL_GPURenderPass* render_pass;
  59. SDL_GPUCopyPass* copy_pass;
  60. SDL_Rect scissor;
  61. Rml::Matrix4f transform;
  62. Rml::Matrix4f proj;
  63. struct Command {
  64. virtual void Update(RenderInterface_SDL_GPU& interface) = 0;
  65. };
  66. struct EnableScissorRegionCommand : Command {
  67. EnableScissorRegionCommand(bool enable) : enable(enable) {}
  68. void Update(RenderInterface_SDL_GPU& interface) override;
  69. bool enable;
  70. };
  71. struct SetScissorRegionCommand : Command {
  72. SetScissorRegionCommand(Rml::Rectanglei region) : region(region) {}
  73. void Update(RenderInterface_SDL_GPU& interface) override;
  74. Rml::Rectanglei region;
  75. };
  76. struct RenderGeometryCommand : Command {
  77. RenderGeometryCommand(Rml::CompiledGeometryHandle handle, Rml::Vector2f translation, Rml::TextureHandle texture) :
  78. handle(handle), translation(translation), texture(texture)
  79. {}
  80. void Update(RenderInterface_SDL_GPU& interface) override;
  81. Rml::CompiledGeometryHandle handle;
  82. Rml::Vector2f translation;
  83. Rml::TextureHandle texture;
  84. };
  85. struct ReleaseGeometryCommand : Command {
  86. ReleaseGeometryCommand(Rml::CompiledGeometryHandle handle) : handle(handle) {}
  87. void Update(RenderInterface_SDL_GPU& interface) override;
  88. Rml::CompiledGeometryHandle handle;
  89. };
  90. struct ReleaseTextureCommand : Command {
  91. ReleaseTextureCommand(Rml::TextureHandle handle) : handle(handle) {}
  92. void Update(RenderInterface_SDL_GPU& interface) override;
  93. Rml::TextureHandle handle;
  94. };
  95. struct SetTransformCommand : Command {
  96. SetTransformCommand(const Rml::Matrix4f* new_transform);
  97. void Update(RenderInterface_SDL_GPU& interface) override;
  98. Rml::Matrix4f transform;
  99. bool has_transform;
  100. };
  101. friend struct EnableScissorRegionCommand;
  102. friend struct SetScissorRegionCommand;
  103. friend struct RenderGeometryCommand;
  104. friend struct ReleaseGeometryCommand;
  105. friend struct ReleaseTextureCommand;
  106. friend struct SetTransformCommand;
  107. struct Buffer {
  108. SDL_GPUTransferBuffer* transfer_buffer;
  109. SDL_GPUBuffer* buffer;
  110. SDL_GPUBufferUsageFlags usage;
  111. int capacity;
  112. bool in_use;
  113. };
  114. struct GeometryView {
  115. Buffer* vertex_buffer;
  116. Buffer* index_buffer;
  117. int num_indices;
  118. };
  119. // List of ordered render commands
  120. Rml::Vector<Rml::UniquePtr<Command>> commands;
  121. // Sorted vertex/index buffers by capacities
  122. Rml::Vector<Rml::UniquePtr<Buffer>> buffers;
  123. void CreatePipelines();
  124. bool BeginCopyPass();
  125. bool BeginRenderPass();
  126. Buffer* RequestBuffer(int capacity, SDL_GPUBufferUsageFlags usage);
  127. };
  128. #endif