Cogs.Core
ContextWebGPU.h
1#pragma once
2
3#include "CommonWebGPU.h"
4
5#include "../IContext.h"
6#include "../Statistics.h"
7
8#include <map>
9#include <vector>
10
11namespace Cogs {
12 class GraphicsDeviceWebGPU;
13 class EffectsWebGPU;
14
15 // enum WGPUAnnotationType{
16 // ANNOTATION_TYPE_RENDER_PASS,
17 // ANNOTATION_TYPE_COMPUTE_PASS,
18 // ANNOTATION_TYPE_RENDER_BUNDLE,
19 // ANNOTATION_TYPE_COMMAND_ENCODER,
20 // };
21
22 class ContextWebGPU : public IContext {
23 public:
24 void initialize(GraphicsDeviceWebGPU *graphicsDevice);
25
26 virtual const FrameStatistics& getLastFrameStatistics() override { return prevStats; }
27 virtual const UploadStatistics& getLastUploadStatistics() override { return prevUploadStats; }
28
29 virtual void frameStatisticsConfigure(bool enable) override { frameStatisticsEnabled = enable; }
30
31 virtual void signal(FenceHandle /*fenceHandle*/) override;
32
33 virtual void clearRenderTarget(const float* /*value*/) override;
34 virtual void clearRenderTarget(const float** /*values*/, const int /*numvalues*/) override;
35 virtual void clearDepth(const float /*depth*/ = 1.0f) override;
36
37 virtual void pushCommandGroupAnnotation(const StringView & name) override;
38 virtual void popCommandGroupAnnotation() override;
39 virtual void setAnnotationMarker(const StringView & name) override;
40
41 virtual void beginRenderPass(const RenderPassInfo &info) override;
42 virtual void endRenderPass() override;
43
44 virtual void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override;
45 virtual void setViewport(const float /*x*/, const float /*y*/, const float /*width*/, const float /*height*/) override;
46 virtual void setScissor(const int /*x*/, const int /*y*/, const int /*width*/, const int /*height*/) override;
47 virtual void setDepthStencilState(const DepthStencilStateHandle /*handle*/) override;
48 virtual void setBlendState(const BlendStateHandle /*handle*/, const float* /*constant*/) override;
49 virtual void setRasterizerState(const RasterizerStateHandle /*handle*/) override;
50 void setDefaults();
51 virtual void setEffect(EffectHandle /*handle*/) override;
52
53 virtual void setTexture(const StringView& /*name*/, unsigned int /*unit*/, TextureHandle /*textureHandle*/) override;
54 virtual void setTexture(const TextureBindingHandle /*textureBindingHandle*/, const TextureHandle /*textureHandle*/) override;
55 virtual void setTexture(const StringView& /*name*/, TextureViewHandle /*textureViewHandle*/) override;
56 virtual void setTexture(const TextureBindingHandle /*textureBindingHandle*/, TextureViewHandle /*textureViewHandle*/) override;
57
58 virtual void setSamplerState(const StringView& /*name*/, unsigned int /*unit*/, SamplerStateHandle /*samplerStateHandle*/) override;
59 virtual void setSamplerState(const SamplerStateBindingHandle /*samplerStateBindingHandle*/, const SamplerStateHandle /*samplerStateHandle*/) override;
60
61 virtual void setInputLayout(const InputLayoutHandle /*inputLayoutHandle*/) override;
62
63 virtual void setVertexBuffers(const VertexBufferHandle* /*vertexBufferHandles*/, const size_t /*count*/, const uint32_t* /*strides*/, const uint32_t* /*offsets*/) override;
64 virtual void setVertexBuffers(const VertexBufferHandle* /*vertexBufferHandles*/, const size_t /*count*/) override;
65
66 virtual void setIndexBuffer(IndexBufferHandle /*bufferHandle*/, uint32_t /*stride*/ = 4, uint32_t /*offset*/ = 0) override;
67
68 virtual void setVertexArrayObject(VertexArrayObjectHandle /*vertexArrayObject*/) override;
69
70 virtual void setConstantBuffer(const StringView& /*name*/, const BufferHandle /*bufferHandle*/, const uint32_t /*offset*/ = 0, const uint32_t /*size*/ = ~0u) override;
71 virtual void setConstantBuffer(const ConstantBufferBindingHandle /*bufferBindingHandle*/, const BufferHandle /*bufferHandle*/, const uint32_t /*offset*/ = 0, const uint32_t /*size*/ = ~0u) override;
72 virtual void setBuffer(const StringView& /*name*/, BufferHandle /*bufferHandle*/) override;
73 virtual void setBuffer(const BufferBindingHandle /*bufferBindingHandle*/, BufferHandle /*bufferHandle*/) override;
74 virtual void setBufferCounter(BufferHandle /*bufferHandle*/, uint32_t /*value*/) override;
75 virtual void setBufferCounter(BufferHandle /*bufferHandle*/, BufferHandle /*sourceBufferHandle*/) override;
76
77 virtual void getBufferCounter(BufferHandle /*bufferHandle*/, BufferHandle /*destinationBufferHandle*/) override;
78 virtual uint32_t getBufferCounter(BufferHandle /*bufferHandle*/) override { return 0; }
79
80 virtual void draw(PrimitiveType /*primitiveType*/, const size_t /*startVertex*/, const size_t /*numVertexes*/) override;
81 virtual void drawIndexed(PrimitiveType /*primitiveType*/, const size_t /*startIndex*/, const size_t /*numIndexes*/, const size_t /*startVertex*/ = 0) override;
82 virtual void drawInstanced(PrimitiveType /*primitiveType*/, const size_t /*startVertex*/, const size_t /*numVertexes*/, const size_t /*startInstance*/, const size_t /*numInstances*/) override;
83 virtual void drawInstancedIndexed(PrimitiveType /*primitiveType*/, const size_t /*startInstance*/, const size_t /*numInstances*/, const size_t /*startIndex*/, const size_t /*numIndexes*/) override;
84
85 virtual void dispatchCompute(const unsigned int /*threadGroupsX*/, const unsigned int /*threadGroupsY*/, const unsigned int /*threadGroupsZ*/) override;
86
87 virtual void readDepthBuffer(BufferHandle /*bufferHandle*/, int /*x*/, int /*y*/, int /*width*/, int /*height*/, Framebuffer /*framebuffer*/) override;
88 virtual void readColorBuffer(BufferHandle /*bufferHandle*/, int /*x*/, int /*y*/, int /*width*/, int /*height*/, Framebuffer /*framebuffer*/) override;
89
90 virtual void*map(BufferHandle /*bufferHandle*/, MapMode::EMapMode /*mapMode*/, uint32_t* /*stride*/ = nullptr) override;
91 virtual void*map(TextureHandle /*textureHandle*/, MapMode::EMapMode /*accessMode*/, uint32_t* /*rowPitch*/, uint32_t* /*depthPitch*/) override;
92 virtual void unmap(BufferHandle /*bufferHandle*/) override;
93 virtual void unmap(TextureHandle /*textureHandle*/) override;
94 virtual void updateBuffer(BufferHandle /*bufferHandle*/, const void* /*data*/, const size_t /*size*/) override;
95
96 virtual void updateSubTexture(TextureHandle /*textureHandle*/, const size_t /*level*/, const void* /*data*/) override;
97 virtual void updateSubBuffer(BufferHandle /*bufferHandle*/, const size_t /*offset*/, const size_t /*size*/, const void* /*data*/) override;
98
99 virtual void resolveResource(TextureHandle /*source*/, TextureHandle /*destination*/) override;
100 virtual void copyResource(BufferHandle /*destinationHandle*/, BufferHandle /*sourceHandle*/) override;
101 virtual void copyResource(TextureHandle /*destinationHandle*/, TextureHandle /*sourceHandle*/) override;
102 virtual void copyTexture(TextureHandle /*dstHandle*/, unsigned /*dstSub*/, unsigned /*dstX*/, unsigned /*dstY*/, unsigned /*dstZ*/, TextureHandle /*sourceHandle*/, unsigned /*srcSub*/) override;
103
104 virtual void clearResource(BufferHandle /*destinationHandle*/, uint32_t* /*values*/) override;
105 virtual void clearResource(BufferHandle /*destinationHandle*/, float* /*values*/) override;
106
107 private:
108 FrameStatistics prevStats{};
109 FrameStatistics currStats{};
110
111 UploadStatistics prevUploadStats{};
112 UploadStatistics currUploadStats{};
113
114
115 bool frameStatisticsEnabled = false;
116 bool uploadStatisticsEnabled = true;
117
118 // Invoked by draw call to account for submission.
119 void frameStatisticsAccountDrawCall(size_t count, bool indexed);
120
121 // Invoked by upload calls
122 void uploadStatisticsBufferUpload(size_t size);
123 void uploadStatisticsTextureUpload(size_t size);
124
125 EffectHandle getCurrentEffect();
126 EffectsWebGPU *getEffects();
127 public:
128 void beginFrame();
129
130 void beginRenderPassInt();
131 void endRenderPassInt();
132 void updateRenderPass();
133 bool updateRenderPipeline(PrimitiveType primitiveType);
134
135 void beginComputePass();
136 void endComputePass();
137 void updateComputePass();
138 void updateComputePipeline();
139
140 void ClearBindGroups();
141 void SwapBindGroups();
142
143 GraphicsDeviceWebGPU *graphicsDevice = nullptr;
144
145 WGPURenderPassEncoder renderPassEncoder = 0;
146 WGPUComputePassEncoder computePassEncoder = 0;
147
148 EffectHandle effect = {};
149 InputLayoutHandle inputLayoutHandle = {};
150 RasterizerStateHandle rasterizeStateHandle = {};
151 DepthStencilStateHandle depthStencilStateHandle = {};
152 BlendStateHandle blendStateHandle = {};
153
154 RenderPipelineHandle currentRenderPipeline = {};
155 ComputePipelineHandle currentComputePipeline = {};
156 WGPUIndexFormat currentIndexFormat = WGPUIndexFormat_Uint32;
157
158 bool inRenderPass = false;
159 RenderTargetHandle renderTargetHandle = {};
160 DepthStencilHandle depthStencilHandle = {};
161 WGPUColor clearColor[8] = {};
162 float clearDepthVal = 1.0f;
163 bool do_clear_render_target = false;
164 bool do_clear_depth = false;
165 bool update_render_target = true;
166
167 WGPUBindGroup current_bind_group = {};
168
169 bool update_descriptors = false;
170 std::map<uint32_t, WGPUBindGroupEntry> descriptors;
171
172 // std::vector<WGPUAnnotationType> annotation_type;
173
174 std::vector<WGPUBindGroup> bind_groups;
175 std::vector<WGPUBindGroup> last_bind_groups;
176 };
177}
virtual void setSamplerState(const StringView &, unsigned int, SamplerStateHandle) override
Sets the sampler slot given by unit with the given name to contain the given sampler state.
virtual void pushCommandGroupAnnotation(const StringView &name) override
Begin to tag a sequence of commands as a group in graphics debugger.
virtual void getBufferCounter(BufferHandle, BufferHandle) override
Get the associated counter of a buffer.
virtual void setAnnotationMarker(const StringView &name) override
Add a tag in the sequence of commands in graphics debugger.
virtual void setEffect(EffectHandle) override
Set the current effect.
virtual void draw(PrimitiveType, const size_t, const size_t) override
Draws non-indexed, non-instanced primitives.
virtual void readColorBuffer(BufferHandle, int, int, int, int, Framebuffer) override
Reads data from the current render target into the given bufferHandle.
virtual void resolveResource(TextureHandle, TextureHandle) override
Resolves the given source resource target into the given destination texture.
virtual void clearDepth(const float=1.0f) override
Clear the currently set depth/stencil target to the given depth.
virtual void setBlendState(const BlendStateHandle, const float *) override
Set the current blend state.
virtual void updateSubBuffer(BufferHandle, const size_t, const size_t, const void *) override
Update a region of data in a buffer.
virtual void setVertexBuffers(const VertexBufferHandle *, const size_t, const uint32_t *, const uint32_t *) override
Sets the current vertex buffers.
virtual void readDepthBuffer(BufferHandle, int, int, int, int, Framebuffer) override
Reads data from the current depth target into the given bufferHandle.
virtual void drawInstanced(PrimitiveType, const size_t, const size_t, const size_t, const size_t) override
Draws non-indexed, instanced primitives.
virtual void setBuffer(const StringView &, BufferHandle) override
Sets the given buffer to the buffer binding slot with the given name.
virtual void popCommandGroupAnnotation() override
End to tag a sequence of commands as a group in graphics debugger.
virtual uint32_t getBufferCounter(BufferHandle) override
Get the associated counter of a buffer.
Definition: ContextWebGPU.h:78
virtual void dispatchCompute(const unsigned int, const unsigned int, const unsigned int) override
Dispatch computing work on the graphics device using the desired thread group count.
virtual void setConstantBuffer(const StringView &, const BufferHandle, const uint32_t=0, const uint32_t=~0u) override
Sets a constant buffer to be bound to the given name and slot.
virtual void setBufferCounter(BufferHandle, uint32_t) override
Set the associated counter of a buffer.
virtual void unmap(BufferHandle) override
Unmaps the given buffer, applying any synchronization necessary to reflect changes in the mapped memo...
virtual void drawIndexed(PrimitiveType, const size_t, const size_t, const size_t=0) override
Draws indexed, non-instanced primitives.
virtual void setDepthStencilState(const DepthStencilStateHandle) override
Set the current depth stencil state.
virtual void setScissor(const int, const int, const int, const int) override
Sets the current scissor rectangle.
virtual void setRasterizerState(const RasterizerStateHandle) override
Set the current rasterizer state.
virtual void endRenderPass() override
End a render pass.
virtual void drawInstancedIndexed(PrimitiveType, const size_t, const size_t, const size_t, const size_t) override
Draws indexed, instanced primitives.
virtual void updateSubTexture(TextureHandle, const size_t, const void *) override
Update the data of a level in the given texture.
virtual void beginRenderPass(const RenderPassInfo &info) override
Begin a render pass.
virtual void setVertexArrayObject(VertexArrayObjectHandle) override
Sets vertexBuffers and index buffers using a prevalidated vertex array object.
virtual void signal(FenceHandle) override
Insert a fence in the command stream that will signal when all commands before the fence are complete...
virtual void setIndexBuffer(IndexBufferHandle, uint32_t=4, uint32_t=0) override
Sets the current index buffer.
virtual void clearRenderTarget(const float *) override
Clear the currently set render target to the given value (4 component floating point RGBA).
virtual void setTexture(const StringView &, unsigned int, TextureHandle) override
Sets the texture slot given by unit with the given name to contain the given texture.
virtual void setInputLayout(const InputLayoutHandle) override
Sets the current input layout.
virtual void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override
Sets the current render target and an associated depth stencil target.
virtual void setViewport(const float, const float, const float, const float) override
Sets the current viewport to the given location and dimensions.
virtual void updateBuffer(BufferHandle, const void *, const size_t) override
Replace contents of buffer with new data.
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:50
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Framebuffer
Framebuffers to select from when doing framebuffer operations.
Definition: Common.h:147
PrimitiveType
Primitive types for interpreting vertex data sent to the graphics pipeline.
Definition: Common.h:112
Handle template class used to provide opaque, non-converting handles.
Definition: Common.h:23
Represents a graphics device context which can receive rendering commands.
Definition: IContext.h:43
EMapMode
Mapping mode enumeration.
Definition: Flags.h:93