Hymn to Beauty
C++ 3D Engine
WebGPURenderer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../Interface/LowLevelRenderer.hpp"
4
5#include "WebGPU.hpp"
6#include <glm/glm.hpp>
7#include <vector>
8
9namespace Utility {
10class Window;
11}
12
13namespace Video {
14
15class WebGPUBuffer;
16class WebGPUBufferAllocator;
17class WebGPUSampler;
18class WebGPURenderTargetAllocator;
19class WebGPUTexture;
20
23 public:
25
28 explicit WebGPURenderer(::Utility::Window* window);
29
31 ~WebGPURenderer() final;
32
34 void BeginFrame() final;
35 void Submit(CommandBuffer* commandBuffer) final;
36 void Present() final;
37 Buffer* CreateBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void* data = nullptr) final;
38 Buffer* CreateTemporaryBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void* data = nullptr) final;
39 VertexDescription* CreateVertexDescription(unsigned int attributeCount, const VertexDescription::Attribute* attributes, bool indexBuffer = false) final;
40 GeometryBinding* CreateGeometryBinding(const VertexDescription* vertexDescription, Buffer* vertexBuffer, GeometryBinding::IndexType indexType = GeometryBinding::IndexType::NONE, const Buffer* indexBuffer = nullptr) final;
41 Shader* CreateShader(const ShaderSource& shaderSource, Shader::Type type) final;
42 ShaderProgram* CreateShaderProgram(std::initializer_list<const Shader*> shaders) final;
43 Texture* CreateTexture(const glm::uvec2 size, Texture::Format format, int components, unsigned char* data) final;
44 const Sampler* GetSampler(Sampler::Filter filter, Sampler::Clamping clamping) const final;
45 Texture* CreateRenderTarget(const glm::uvec2& size, Texture::Format format) final;
46 void FreeRenderTarget(Texture* renderTarget) final;
47 GraphicsPipeline* CreateGraphicsPipeline(const ShaderProgram* shaderProgram, const GraphicsPipeline::Configuration& configuration, const VertexDescription* vertexDescription = nullptr) final;
48 ComputePipeline* CreateComputePipeline(const ShaderProgram* shaderProgram) final;
49 void Wait() final;
50 unsigned char* ReadImage(Texture* texture) final;
51 const std::vector<Profiling::Event>& GetTimeline() const final;
52 const OptionalFeatures& GetOptionalFeatures() const final;
53
55
59 WebGPUTexture* CreateAttachmentlessRenderTarget(const glm::uvec2& size, uint32_t sampleCount);
60
62
66
68
71 WGPUDevice GetDevice() const;
72
74
77 WGPUQueue GetQueue() const;
78
80
83 WGPUTextureView GetCurrentFrame() const;
84
86
90
92
95 bool HasDepthClipControl() const;
96
98
101 bool HasR11G11B10() const;
102
104
107 WGPUTextureFormat GetSwapChainFormat() const;
108
109 private:
110 WebGPURenderer(const WebGPURenderer& other) = delete;
111
112 void InitializeWebGPUBackend();
113 void CreateInstance();
114 void CreateSurface(::Utility::Window* window);
115 void RequestAdapter();
116 void CreateDevice();
117 void CreateSwapChain(::Utility::Window* window);
118 void CreateSamplers();
119
120 WGPUInstance instance;
121 WGPUSurface surface;
122 WGPUAdapter adapter;
123 WGPUDevice device;
124 WGPUSwapChain swapChain;
125 WGPUQueue queue;
126 WGPUTextureView currentFrame;
127
128 WebGPUSampler* samplers[static_cast<uint32_t>(Sampler::Filter::COUNT) * static_cast<uint32_t>(Sampler::Clamping::COUNT)];
129
130 WebGPUBufferAllocator* bufferAllocator;
131 WebGPURenderTargetAllocator* renderTargetAllocator;
132
133 OptionalFeatures optionalFeatures;
134
135 glm::uvec2 swapChainSize;
136 WGPUTextureFormat swapChainFormat;
137
138 // Blitting to swap chain.
139 Shader* blitVertexShader;
140 Shader* blitFragmentShader;
141 ShaderProgram* blitShaderProgram;
142 GraphicsPipeline* blitGraphicsPipeline;
143
144 std::vector<Profiling::Event> finishedEvents;
145
146 bool depthClipControlEnabled;
147 bool r11g11b10Enabled;
148};
149
150} // namespace Video
Definition: Window.hpp:14
A buffer containing GPU accessible data.
Definition: Buffer.hpp:8
BufferUsage
How the buffer is going to be used.
Definition: Buffer.hpp:11
A buffer into which rendering commands are recorded.
Definition: CommandBuffer.hpp:24
A compute pipeline.
Definition: ComputePipeline.hpp:11
Binds together a vertex description with buffers.
Definition: GeometryBinding.hpp:8
IndexType
The type of values in the index buffer.
Definition: GeometryBinding.hpp:11
A graphics pipeline.
Definition: GraphicsPipeline.hpp:11
Low level renderer abstracting the underlaying graphics API (OpenGL or Vulkan).
Definition: LowLevelRenderer.hpp:27
Determines how a texture should be sampled.
Definition: Sampler.hpp:6
Filter
The interpolation to apply.
Definition: Sampler.hpp:9
@ COUNT
Number of entries in enum.
Clamping
How to handle sampling outside the texture dimensions.
Definition: Sampler.hpp:16
@ COUNT
Number of entries in enum.
Compiles and handles a shader. Shaders should be linked together into a ShaderProgram.
Definition: Shader.hpp:8
Type
The type of shader.
Definition: Shader.hpp:11
A shader program.
Definition: ShaderProgram.hpp:11
A texture.
Definition: Texture.hpp:8
Format
The format of the texture.
Definition: Texture.hpp:18
Describes how a vertex buffer is accessed by a shader.
Definition: VertexDescription.hpp:8
WebGPU implementation of BufferAllocator.
Definition: WebGPUBufferAllocator.hpp:40
WebGPU implementation of RenderTargetAllocator.
Definition: WebGPURenderTargetAllocator.hpp:15
Low-level renderer implementing WebGPU.
Definition: WebGPURenderer.hpp:22
ComputePipeline * CreateComputePipeline(const ShaderProgram *shaderProgram) final
Create a compute pipeline.
Definition: WebGPURenderer.cpp:176
WGPUTextureFormat GetSwapChainFormat() const
Get the format of the swap chain.
Definition: WebGPURenderer.cpp:311
Buffer * CreateTemporaryBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void *data=nullptr) final
Definition: WebGPURenderer.cpp:131
const OptionalFeatures & GetOptionalFeatures() const final
Get which optional features are supported.
Definition: WebGPURenderer.cpp:275
const Sampler * GetSampler(Sampler::Filter filter, Sampler::Clamping clamping) const final
Get a sampler.
Definition: WebGPURenderer.cpp:157
GeometryBinding * CreateGeometryBinding(const VertexDescription *vertexDescription, Buffer *vertexBuffer, GeometryBinding::IndexType indexType=GeometryBinding::IndexType::NONE, const Buffer *indexBuffer=nullptr) final
Create a geometry binding.
Definition: WebGPURenderer.cpp:139
~WebGPURenderer() final
Destructor.
Definition: WebGPURenderer.cpp:75
Buffer * CreateBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void *data=nullptr) final
Definition: WebGPURenderer.cpp:127
WebGPUTexture * CreateAttachmentlessRenderTarget(const glm::uvec2 &size, uint32_t sampleCount)
Create a render target to use for "attachmentless" rendering.
Definition: WebGPURenderer.cpp:279
WebGPURenderer(::Utility::Window *window)
Create new WebGPU renderer.
Definition: WebGPURenderer.cpp:47
WGPUQueue GetQueue() const
Get the WebGPU queue.
Definition: WebGPURenderer.cpp:291
void FreeRenderTarget(Texture *renderTarget) final
Free a render target.
Definition: WebGPURenderer.cpp:168
Texture * CreateTexture(const glm::uvec2 size, Texture::Format format, int components, unsigned char *data) final
Create a texture.
Definition: WebGPURenderer.cpp:151
void Submit(CommandBuffer *commandBuffer) final
Submit a command buffer for execution.
Definition: WebGPURenderer.cpp:109
ShaderProgram * CreateShaderProgram(std::initializer_list< const Shader * > shaders) final
Create a shader program.
Definition: WebGPURenderer.cpp:147
bool HasR11G11B10() const
Get whether R11G11B10 render targets can be used.
Definition: WebGPURenderer.cpp:307
WGPUTextureView GetCurrentFrame() const
Get the texture view of the current frame's swap chain image.
Definition: WebGPURenderer.cpp:295
WGPUDevice GetDevice() const
Get the WebGPU device.
Definition: WebGPURenderer.cpp:287
void BeginFrame() final
Begin a new frame.
Definition: WebGPURenderer.cpp:101
bool HasDepthClipControl() const
Get whether depth-clip-control can be used.
Definition: WebGPURenderer.cpp:303
CommandBuffer * CreateCommandBuffer() final
Create a command buffer.
Definition: WebGPURenderer.cpp:97
unsigned char * ReadImage(Texture *texture) final
Read a render texture color image.
Definition: WebGPURenderer.cpp:199
void FreeAttachmentlessRenderTarget(WebGPUTexture *renderTarget)
Free an "attachmentless" render target.
Definition: WebGPURenderer.cpp:283
Texture * CreateRenderTarget(const glm::uvec2 &size, Texture::Format format) final
Create a render target.
Definition: WebGPURenderer.cpp:164
const std::vector< Profiling::Event > & GetTimeline() const final
Get profiling timeline.
Definition: WebGPURenderer.cpp:271
void Wait() final
Wait for all rendering to finish.
Definition: WebGPURenderer.cpp:180
VertexDescription * CreateVertexDescription(unsigned int attributeCount, const VertexDescription::Attribute *attributes, bool indexBuffer=false) final
Create a vertex description.
Definition: WebGPURenderer.cpp:135
void Present() final
Present the rendered image to the swap chain.
Definition: WebGPURenderer.cpp:123
GraphicsPipeline * GetBlitGraphicsPipeline() const
Get the graphics pipeline used for blitting to the swap chain.
Definition: WebGPURenderer.cpp:299
GraphicsPipeline * CreateGraphicsPipeline(const ShaderProgram *shaderProgram, const GraphicsPipeline::Configuration &configuration, const VertexDescription *vertexDescription=nullptr) final
Create a graphics pipeline.
Definition: WebGPURenderer.cpp:172
Shader * CreateShader(const ShaderSource &shaderSource, Shader::Type type) final
Create a shader.
Definition: WebGPURenderer.cpp:143
WebGPU implementation of Sampler.
Definition: WebGPUSampler.hpp:10
WebGPU implementation of Texture.
Definition: WebGPUTexture.hpp:13
Definition: ResourceView.hpp:12
Definition: Editor.hpp:18
The configuration of the graphics pipeline.
Definition: GraphicsPipeline.hpp:14
Optional features.
Definition: LowLevelRenderer.hpp:30
Describes an attribute used in a vertex shader.
Definition: VertexDescription.hpp:18