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
23 explicit WebGPUCommandBuffer(WebGPURenderer* renderer);
24
27
28 void BeginRenderPass(RenderPass* renderPass, const std::string& name) final;
29 void BeginRenderPass(Texture* colorAttachment, RenderPass::LoadOperation colorLoadOperation, Texture* depthAttachment, RenderPass::LoadOperation depthLoadOperation, const std::string& name) final;
30 void BeginAttachmentlessRenderPass(const glm::uvec2& size, uint32_t msaaSamples, const std::string& name) final;
31 void EndRenderPass() final;
32 void BindGraphicsPipeline(GraphicsPipeline* graphicsPipeline) final;
33 void SetViewport(const glm::uvec2& origin, const glm::uvec2& size) final;
34 void SetScissor(const glm::uvec2& origin, const glm::uvec2& size) final;
35 void SetLineWidth(float width) final;
36 void BindGeometry(GeometryBinding* geometryBinding) final;
37 void BindUniformBuffer(ShaderProgram::BindingType bindingType, Buffer* uniformBuffer) final;
38 void BindStorageBuffers(std::initializer_list<Buffer*> buffers) final;
39 void BindMaterial(std::initializer_list<std::pair<Texture*, const Sampler*>> textures) final;
40 void PushConstants(const void* data) final;
41 void Draw(unsigned int vertexCount, unsigned int firstVertex) final;
42 void DrawIndexed(unsigned int indexCount, unsigned int firstIndex, unsigned int baseVertex) final;
43 void DrawIndexedInstanced(unsigned int indexCount, unsigned int instanceCount, unsigned int firstIndex, unsigned int baseVertex) final;
44 void BlitToSwapChain(Texture* texture) final;
45 void BindComputePipeline(ComputePipeline* computePipeline) final;
46 void Dispatch(const glm::uvec3& numGroups, const std::string& name) final;
47 void ClearBuffer(Buffer* buffer) final;
48
50
53 WGPUCommandEncoder GetCommandEncoder();
54
56
59 WGPUCommandBuffer End();
60
62 void NextFrame();
63
65
68 WGPURenderPassEncoder GetRenderPassEncoder();
69
70 private:
71 WebGPUCommandBuffer(const WebGPUCommandBuffer& other) = delete;
72
73 void AllocatePushConstantBuffer();
74 void UpdateUniforms();
75 void EndComputePass();
76 const WebGPUShaderProgram* GetCurrentShaderProgram() const;
77
78 WebGPURenderer* renderer;
79 WGPUDevice device;
80 WGPUCommandEncoder commandEncoder = nullptr;
81 WGPURenderPassEncoder renderPassEncoder;
82 WGPUComputePassEncoder computePassEncoder;
83
85 WGPUBindGroup emptyBindGroup;
86
87 WebGPUGraphicsPipeline* currentGraphicsPipeline = nullptr;
88 WebGPUComputePipeline* currentComputePipeline = nullptr;
89
90 GraphicsPipeline* blitGraphicsPipeline;
91 const Sampler* blitSampler;
92
93 bool inRenderPass = false;
94 bool inComputePass = false;
95
96 // minUniformBufferOffsetAlignment is guaranteed to be at most 256.
97 static const uint32_t pushConstantSize = 256;
98 static const uint32_t pushConstantsPerBuffer = 1024;
99
100 struct PushConstantBuffer {
101 WebGPUBuffer* buffer;
102 uint8_t data[pushConstantSize * pushConstantsPerBuffer];
103 uint32_t currentPushConstant;
104 };
105
106 std::vector<PushConstantBuffer> pushConstantBuffers;
107 std::uint32_t currentPushConstantBuffer = 0;
108
109 bool uniformsHasChanged = false;
110 const WebGPUBuffer* currentUniformBuffer;
111
112 WebGPUTexture* dummyRenderTarget = nullptr;
113
114 std::vector<WGPUBindGroup> bindGroupsToRelease;
115};
116
117} // 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:426
void BindUniformBuffer(ShaderProgram::BindingType bindingType, Buffer *uniformBuffer) final
Bind uniform buffer for use in a shader.
Definition: WebGPUCommandBuffer.cpp:239
WebGPUCommandBuffer(WebGPURenderer *renderer)
Create new WebGPU command buffer.
Definition: WebGPUCommandBuffer.cpp:17
void SetScissor(const glm::uvec2 &origin, const glm::uvec2 &size) final
Set the scissor box.
Definition: WebGPUCommandBuffer.cpp:195
void Dispatch(const glm::uvec3 &numGroups, const std::string &name) final
Dispatch compute shader.
Definition: WebGPUCommandBuffer.cpp:453
void EndRenderPass() final
End render pass.
Definition: WebGPUCommandBuffer.cpp:152
void BeginAttachmentlessRenderPass(const glm::uvec2 &size, uint32_t msaaSamples, const std::string &name) final
Begin attachmentless render pass.
Definition: WebGPUCommandBuffer.cpp:119
void SetViewport(const glm::uvec2 &origin, const glm::uvec2 &size) final
Set the viewport to render to.
Definition: WebGPUCommandBuffer.cpp:189
WGPUCommandEncoder GetCommandEncoder()
Get command encoder.
Definition: WebGPUCommandBuffer.cpp:475
void NextFrame()
Cycle the command buffer for the next frame.
Definition: WebGPUCommandBuffer.cpp:503
void BindGeometry(GeometryBinding *geometryBinding) final
Bind geometry to be used in upcoming draw calls.
Definition: WebGPUCommandBuffer.cpp:215
void ClearBuffer(Buffer *buffer) final
Clear a buffer (fill with 0).
Definition: WebGPUCommandBuffer.cpp:466
void PushConstants(const void *data) final
Update push constants.
Definition: WebGPUCommandBuffer.cpp:338
~WebGPUCommandBuffer() final
Destructor.
Definition: WebGPUCommandBuffer.cpp:36
void DrawIndexedInstanced(unsigned int indexCount, unsigned int instanceCount, unsigned int firstIndex, unsigned int baseVertex) final
Draw indexed instanced geometry.
Definition: WebGPUCommandBuffer.cpp:379
void BindGraphicsPipeline(GraphicsPipeline *graphicsPipeline) final
Bind graphics pipeline.
Definition: WebGPUCommandBuffer.cpp:169
void BeginRenderPass(RenderPass *renderPass, const std::string &name) final
Begin render pass.
Definition: WebGPUCommandBuffer.cpp:48
void SetLineWidth(float width) final
Set width of lines.
Definition: WebGPUCommandBuffer.cpp:209
WGPURenderPassEncoder GetRenderPassEncoder()
Get the current render pass encoder.
Definition: WebGPUCommandBuffer.cpp:521
void Draw(unsigned int vertexCount, unsigned int firstVertex) final
Draw geometry.
Definition: WebGPUCommandBuffer.cpp:363
void BindStorageBuffers(std::initializer_list< Buffer * > buffers) final
Bind storage buffers.
Definition: WebGPUCommandBuffer.cpp:273
void BindMaterial(std::initializer_list< std::pair< Texture *, const Sampler * > > textures) final
Bind a material.
Definition: WebGPUCommandBuffer.cpp:306
void DrawIndexed(unsigned int indexCount, unsigned int firstIndex, unsigned int baseVertex) final
Draw indexed geometry.
Definition: WebGPUCommandBuffer.cpp:371
void BlitToSwapChain(Texture *texture) final
Blit a texture to the current swap chain image.
Definition: WebGPUCommandBuffer.cpp:387
WGPUCommandBuffer End()
Finish recording the command buffer.
Definition: WebGPUCommandBuffer.cpp:479
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