Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
RenderInterface.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <RmlUi/Core.h>
4
5#include <cstddef>
6#include <cstdint>
7#include <memory>
8#include <optional>
9#include <unordered_map>
10#include <vector>
11
12#include "RmlUi/Config/Config.h"
13#include "RmlUi/Core/Colour.h"
14#include "RmlUi/Core/Math.h"
15#include "RmlUi/Core/Span.h"
16#include "RmlUi/Core/Types.h"
17#include "RmlUi/Core/Vertex.h"
18
19#include "core/Core.hpp"
20#include "resource/Texture.hpp"
21
22#include "utils/IRenderer.hpp"
23#include "utils/webgpu.hpp"
24
25namespace Rmlui::Utils {
27 public:
28 RenderInterface() = delete;
29 explicit RenderInterface(Engine::Core &core);
30 ~RenderInterface() override = default;
31
32 static RenderInterface *GetActive();
33 void FlushDrawCommands(wgpu::RenderPassEncoder const &renderPass);
34
35 Rml::CompiledGeometryHandle CompileGeometry(Rml::Span<const Rml::Vertex> vertices,
36 Rml::Span<const int> indices) override;
37 void RenderGeometry(Rml::CompiledGeometryHandle handle, Rml::Vector2f translation,
38 Rml::TextureHandle texture_handle) override;
39 void ReleaseGeometry(Rml::CompiledGeometryHandle handle) override;
40 Rml::TextureHandle LoadTexture(Rml::Vector2i &texture_dimensions, const Rml::String &source) override;
41 Rml::TextureHandle GenerateTexture(Rml::Span<const Rml::byte> source, Rml::Vector2i dimensions) override;
42 Rml::TextureHandle CreateTexture(Rml::Span<const Rml::byte> source_data, Rml::Vector2i source_dimensions,
43 wgpu::TextureFormat format);
44 void ReleaseTexture(Rml::TextureHandle handle) override;
45 void EnableScissorRegion(bool enable) override;
46 void SetScissorRegion(Rml::Rectanglei region) override;
47 void SetScissor(Rml::Rectanglei region, bool vertically_flip);
48 void BeginFrame() override;
49 void EndFrame() override;
50 void DrawFullscreenQuad();
51 void SetTransform(const Rml::Matrix4f *new_transform) override;
52
53 private:
54 struct GeometryData {
55 std::vector<Rml::Vertex> vertices;
56 std::vector<int> indices;
57 };
58
59 struct TextureData {
60 Rml::Vector2i size;
61 std::vector<Rml::byte> pixels;
62 std::unique_ptr<Graphic::Resource::Texture> gpuTexture;
63 wgpu::Sampler sampler;
64 wgpu::BindGroup bindGroup;
65 };
66
67 struct DrawCommand {
68 wgpu::Buffer vertexBuffer;
69 wgpu::Buffer indexBuffer;
70 uint32_t indexCount = 0;
71 wgpu::BindGroup textureBindGroup;
72 wgpu::BindGroup screenBindGroup;
73 bool scissorEnabled = false;
74 Rml::Rectanglei scissorRegion;
75 };
76
77 wgpu::BindGroup ResolveTextureBindGroup(Rml::TextureHandle texture_handle);
78
80
82 std::unordered_map<Rml::CompiledGeometryHandle, std::unique_ptr<GeometryData>> _geometries;
83 std::unordered_map<Rml::TextureHandle, std::unique_ptr<TextureData>> _textures;
84 size_t _textureCounter = 0;
85 Rml::TextureHandle _nextTextureHandle = 1;
86 std::vector<DrawCommand> _drawCommands;
87 std::unique_ptr<TextureData> _defaultTexture;
88 wgpu::Buffer _screenBuffer;
89 wgpu::BindGroup _screenBindGroup;
90 bool _scissorEnabled = false;
91 Rml::Rectanglei _scissorRegion;
92 std::optional<Rml::Matrix4f> _transform;
93};
94} // namespace Rmlui::Utils
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
Definition IRenderer.hpp:7
void SetScissorRegion(Rml::Rectanglei region) override
Definition RenderInterface.cpp:407
Engine::Core & _core
Definition RenderInterface.hpp:81
wgpu::BindGroup ResolveTextureBindGroup(Rml::TextureHandle texture_handle)
Definition RenderInterface.cpp:235
std::unordered_map< Rml::CompiledGeometryHandle, std::unique_ptr< GeometryData > > _geometries
Definition RenderInterface.hpp:82
Rml::TextureHandle GenerateTexture(Rml::Span< const Rml::byte > source, Rml::Vector2i dimensions) override
Definition RenderInterface.cpp:317
static RenderInterface * GetActive()
Definition RenderInterface.cpp:73
Rml::TextureHandle _nextTextureHandle
Definition RenderInterface.hpp:85
std::unique_ptr< TextureData > _defaultTexture
Definition RenderInterface.hpp:87
void SetTransform(const Rml::Matrix4f *new_transform) override
Definition RenderInterface.cpp:522
static RenderInterface * _active
Definition RenderInterface.hpp:79
void ReleaseGeometry(Rml::CompiledGeometryHandle handle) override
Definition RenderInterface.cpp:282
void ReleaseTexture(Rml::TextureHandle handle) override
Definition RenderInterface.cpp:403
std::unordered_map< Rml::TextureHandle, std::unique_ptr< TextureData > > _textures
Definition RenderInterface.hpp:83
wgpu::BindGroup _screenBindGroup
Definition RenderInterface.hpp:89
void SetScissor(Rml::Rectanglei region, bool vertically_flip)
Definition RenderInterface.cpp:409
Rml::TextureHandle CreateTexture(Rml::Span< const Rml::byte > source_data, Rml::Vector2i source_dimensions, wgpu::TextureFormat format)
Definition RenderInterface.cpp:322
size_t _textureCounter
Definition RenderInterface.hpp:84
void FlushDrawCommands(wgpu::RenderPassEncoder const &renderPass)
Definition RenderInterface.cpp:75
Rml::CompiledGeometryHandle CompileGeometry(Rml::Span< const Rml::Vertex > vertices, Rml::Span< const int > indices) override
Definition RenderInterface.cpp:126
void DrawFullscreenQuad()
Definition RenderInterface.cpp:516
void EndFrame() override
Definition RenderInterface.cpp:511
std::vector< DrawCommand > _drawCommands
Definition RenderInterface.hpp:86
std::optional< Rml::Matrix4f > _transform
Definition RenderInterface.hpp:92
void EnableScissorRegion(bool enable) override
Definition RenderInterface.cpp:405
bool _scissorEnabled
Definition RenderInterface.hpp:90
void BeginFrame() override
Definition RenderInterface.cpp:433
wgpu::Buffer _screenBuffer
Definition RenderInterface.hpp:88
void RenderGeometry(Rml::CompiledGeometryHandle handle, Rml::Vector2f translation, Rml::TextureHandle texture_handle) override
Definition RenderInterface.cpp:138
~RenderInterface() override=default
Rml::TextureHandle LoadTexture(Rml::Vector2i &texture_dimensions, const Rml::String &source) override
Definition RenderInterface.cpp:284
Rml::Rectanglei _scissorRegion
Definition RenderInterface.hpp:91
Definition IRenderer.hpp:6
Definition RenderInterface.hpp:67
bool scissorEnabled
Definition RenderInterface.hpp:73
wgpu::Buffer vertexBuffer
Definition RenderInterface.hpp:68
wgpu::BindGroup screenBindGroup
Definition RenderInterface.hpp:72
Rml::Rectanglei scissorRegion
Definition RenderInterface.hpp:74
uint32_t indexCount
Definition RenderInterface.hpp:70
wgpu::Buffer indexBuffer
Definition RenderInterface.hpp:69
wgpu::BindGroup textureBindGroup
Definition RenderInterface.hpp:71
Definition RenderInterface.hpp:54
std::vector< Rml::Vertex > vertices
Definition RenderInterface.hpp:55
std::vector< int > indices
Definition RenderInterface.hpp:56
Definition RenderInterface.hpp:59
std::unique_ptr< Graphic::Resource::Texture > gpuTexture
Definition RenderInterface.hpp:62
Rml::Vector2i size
Definition RenderInterface.hpp:60
std::vector< Rml::byte > pixels
Definition RenderInterface.hpp:61
wgpu::BindGroup bindGroup
Definition RenderInterface.hpp:64
wgpu::Sampler sampler
Definition RenderInterface.hpp:63