Hymn to Beauty
C++ 3D Engine
WebGPUCommandBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../Interface/CommandBuffer.hpp"
4
5#include "WebGPU.hpp"
6#include <vector>
8
9namespace Video {
10
11class WebGPURenderer;
12class WebGPUBuffer;
13class WebGPUComputePipeline;
14class WebGPUTexture;
15
18 public:
20
24 WebGPUCommandBuffer(WebGPURenderer* renderer, uint32_t frames = 1);
25
28
29 void BeginRenderPass(RenderPass* renderPass, const std::string& name) final;
30 void BeginRenderPass(Texture* colorAttachment, RenderPass::LoadOperation colorLoadOperation, Texture* depthAttachment, RenderPass::LoadOperation depthLoadOperation, const std::string& name) final;
31 void BeginAttachmentlessRenderPass(const glm::uvec2& size, uint32_t msaaSamples, const std::string& name) final;
32 void EndRenderPass() final;
33 void BindGraphicsPipeline(GraphicsPipeline* graphicsPipeline) final;
34 void SetViewport(const glm::uvec2& origin, const glm::uvec2& size) final;
35 void SetScissor(const glm::uvec2& origin, const glm::uvec2& size) final;
36 void SetLineWidth(float width) final;
37 void BindGeometry(GeometryBinding* geometryBinding) final;
38 void BindUniformBuffer(ShaderProgram::BindingType bindingType, Buffer* uniformBuffer) final;
39 void BindStorageBuffers(std::initializer_list<Buffer*> buffers) final;
40 void BindMaterial(std::initializer_list<std::pair<Texture*, const Sampler*>> textures) final;
41 void PushConstants(const void* data) final;
42 void Draw(unsigned int vertexCount, unsigned int firstVertex) final;
43 void DrawIndexed(unsigned int indexCount, unsigned int firstIndex, unsigned int baseVertex) final;
44 void DrawIndexedInstanced(unsigned int indexCount, unsigned int instanceCount, unsigned int firstIndex, unsigned int baseVertex) final;
45 void BlitToSwapChain(Texture* texture) final;
46 void BindComputePipeline(ComputePipeline* computePipeline) final;
47 void Dispatch(const glm::uvec3& numGroups, const std::string& name) final;
48 void ClearBuffer(Buffer* buffer) final;
49
51
54 WGPUCommandEncoder GetCommandEncoder();
55
57
60 WGPUCommandBuffer End();
61
63 void NextFrame();
64
66
69 WGPURenderPassEncoder GetRenderPassEncoder();
70
71 private:
72 WebGPUCommandBuffer(const WebGPUCommandBuffer& other) = delete;
73
74 void AllocatePushConstantBuffer();
75 void UpdateUniforms();
76 void EndComputePass();
77 const WebGPUShaderProgram* GetCurrentShaderProgram() const;
78
79 WebGPURenderer* renderer;
80 WGPUDevice device;
81 WGPUCommandEncoder commandEncoder = nullptr;
82 WGPURenderPassEncoder renderPassEncoder;
83 WGPUComputePassEncoder computePassEncoder;
84
86 WGPUBindGroup emptyBindGroup;
87
88 WebGPUGraphicsPipeline* currentGraphicsPipeline = nullptr;
89 WebGPUComputePipeline* currentComputePipeline = nullptr;
90
91 GraphicsPipeline* blitGraphicsPipeline;
92 const Sampler* blitSampler;
93
94 bool inRenderPass = false;
95 bool inComputePass = false;
96
97 // minUniformBufferOffsetAlignment is guaranteed to be at most 256.
98 static const uint32_t pushConstantSize = 256;
99 static const uint32_t pushConstantsPerBuffer = 1024;
100
101 struct PushConstantBuffer {
102 WebGPUBuffer* buffer;
103 uint8_t data[pushConstantSize * pushConstantsPerBuffer];
104 uint32_t currentPushConstant;
105 };
106
107 uint32_t frames;
108 uint32_t frame = 0;
109 std::vector<PushConstantBuffer>* pushConstantBuffers;
110 uint32_t currentPushConstantBuffer = 0;
111
112
113 bool uniformsHasChanged = false;
114 const WebGPUBuffer* currentUniformBuffer;
115
116 WebGPUTexture* dummyRenderTarget = nullptr;
117
118 std::vector<WGPUBindGroup> bindGroupsToRelease;
119};
120
121} // namespace Video
A buffer containing GPU accessible data.
Definition: Buffer.hpp:8
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
A graphics pipeline.
Definition: GraphicsPipeline.hpp:11
A render pass.
Definition: RenderPass.hpp:11
LoadOperation
What to do with an image being rendered to before rendering to it.
Definition: RenderPass.hpp:14
Determines how a texture should be sampled.
Definition: Sampler.hpp:6
BindingType
A type of bound resource.
Definition: ShaderProgram.hpp:14
A texture.
Definition: Texture.hpp:8
WebGPU implementation of Buffer.
Definition: WebGPUBuffer.hpp:12
WebGPU implementation of CommandBuffer.
Definition: WebGPUCommandBuffer.hpp:17
void BindComputePipeline(ComputePipeline *computePipeline) final
Bind compute pipeline.
Definition: WebGPUCommandBuffer.cpp:435
void BindUniformBuffer(ShaderProgram::BindingType bindingType, Buffer *uniformBuffer) final
Bind uniform buffer for use in a shader.
Definition: WebGPUCommandBuffer.cpp:245
void SetScissor(const glm::uvec2 &origin, const glm::uvec2 &size) final
Set the scissor box.
Definition: WebGPUCommandBuffer.cpp:201
void Dispatch(const glm::uvec3 &numGroups, const std::string &name) final
Dispatch compute shader.
Definition: WebGPUCommandBuffer.cpp:462
void EndRenderPass() final
End render pass.
Definition: WebGPUCommandBuffer.cpp:158
void BeginAttachmentlessRenderPass(const glm::uvec2 &size, uint32_t msaaSamples, const std::string &name) final
Begin attachmentless render pass.
Definition: WebGPUCommandBuffer.cpp:124
void SetViewport(const glm::uvec2 &origin, const glm::uvec2 &size) final
Set the viewport to render to.
Definition: WebGPUCommandBuffer.cpp:195
WGPUCommandEncoder GetCommandEncoder()
Get command encoder.
Definition: WebGPUCommandBuffer.cpp:484
void NextFrame()
Cycle the command buffer for the next frame.
Definition: WebGPUCommandBuffer.cpp:512
void BindGeometry(GeometryBinding *geometryBinding) final
Bind geometry to be used in upcoming draw calls.
Definition: WebGPUCommandBuffer.cpp:221
void ClearBuffer(Buffer *buffer) final
Clear a buffer (fill with 0).
Definition: WebGPUCommandBuffer.cpp:475
void PushConstants(const void *data) final
Update push constants.
Definition: WebGPUCommandBuffer.cpp:347
~WebGPUCommandBuffer() final
Destructor.
Definition: WebGPUCommandBuffer.cpp:38
void DrawIndexedInstanced(unsigned int indexCount, unsigned int instanceCount, unsigned int firstIndex, unsigned int baseVertex) final
Draw indexed instanced geometry.
Definition: WebGPUCommandBuffer.cpp:388
void BindGraphicsPipeline(GraphicsPipeline *graphicsPipeline) final
Bind graphics pipeline.
Definition: WebGPUCommandBuffer.cpp:175
void BeginRenderPass(RenderPass *renderPass, const std::string &name) final
Begin render pass.
Definition: WebGPUCommandBuffer.cpp:52
WebGPUCommandBuffer(WebGPURenderer *renderer, uint32_t frames=1)
Create new WebGPU command buffer.
Definition: WebGPUCommandBuffer.cpp:17
void SetLineWidth(float width) final
Set width of lines.
Definition: WebGPUCommandBuffer.cpp:215
WGPURenderPassEncoder GetRenderPassEncoder()
Get the current render pass encoder.
Definition: WebGPUCommandBuffer.cpp:531
void Draw(unsigned int vertexCount, unsigned int firstVertex) final
Draw geometry.
Definition: WebGPUCommandBuffer.cpp:372
void BindStorageBuffers(std::initializer_list< Buffer * > buffers) final
Bind storage buffers.
Definition: WebGPUCommandBuffer.cpp:282
void BindMaterial(std::initializer_list< std::pair< Texture *, const Sampler * > > textures) final
Bind a material.
Definition: WebGPUCommandBuffer.cpp:315
void DrawIndexed(unsigned int indexCount, unsigned int firstIndex, unsigned int baseVertex) final
Draw indexed geometry.
Definition: WebGPUCommandBuffer.cpp:380
void BlitToSwapChain(Texture *texture) final
Blit a texture to the current swap chain image.
Definition: WebGPUCommandBuffer.cpp:396
WGPUCommandBuffer End()
Finish recording the command buffer.
Definition: WebGPUCommandBuffer.cpp:488
WebGPU implementation of ComputePipeline.
Definition: WebGPUComputePipeline.hpp:13
WebGPU implementation of GraphicsPipeline.
Definition: WebGPUGraphicsPipeline.hpp:16
Low-level renderer implementing WebGPU.
Definition: WebGPURenderer.hpp:22
WebGPU implementation of ShaderProgram.
Definition: WebGPUShaderProgram.hpp:16
WebGPU implementation of Texture.
Definition: WebGPUTexture.hpp:13
Definition: Editor.hpp:18
A description of a render pass.
Definition: WebGPUGraphicsPipeline.hpp:19