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
10namespace Cogs {
11 class GraphicsDeviceWebGPU;
12 class EffectsWebGPU;
13
14 class ContextWebGPU : public IContext {
15 public:
16 void initialize(GraphicsDeviceWebGPU *graphicsDevice);
17
18 virtual const FrameStatistics& getLastFrameStatistics() override { return frameStatistics; }
19 virtual const UploadStatistics& getLastUploadStatistics() override { return uploadStatistics; }
20 virtual void frameStatisticsConfigure(bool /*enable*/) override;
21 virtual void signal(FenceHandle /*fenceHandle*/) override;
22
23 virtual void clearRenderTarget(const float* /*value*/) override;
24 virtual void clearRenderTarget(const float** /*values*/, const int /*numvalues*/) override;
25 virtual void clearDepth(const float /*depth*/ = 1.0f) override;
26
27 virtual void beginRenderPass(const RenderPassInfo &info) override;
28 virtual void endRenderPass() override;
29
30 virtual void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override;
31 virtual void setViewport(const float /*x*/, const float /*y*/, const float /*width*/, const float /*height*/) override;
32 virtual void setScissor(const int /*x*/, const int /*y*/, const int /*width*/, const int /*height*/) override;
33 virtual void setDepthStencilState(const DepthStencilStateHandle /*handle*/) override;
34 virtual void setBlendState(const BlendStateHandle /*handle*/, const float* /*constant*/) override;
35 virtual void setRasterizerState(const RasterizerStateHandle /*handle*/) override;
36 void setDefaults();
37 virtual void setEffect(EffectHandle /*handle*/) override;
38
39 virtual void setVariable(const EffectVariableHandle /*index*/, const uint8_t* /*data*/, size_t /*size*/) override;
40
41 virtual void setScalarVariable(const StringView& /*name*/, const float /*value*/) override;
42 virtual void setScalarVariable(const StringView& /*name*/, int /*value*/) override;
43 virtual void setScalarVariable(const EffectVariableHandle /*variableHandle*/, const float /*value*/) override;
44 virtual void setScalarVariable(const EffectVariableHandle /*variableHandle*/, const float* /*value*/, size_t /*count*/) override;
45 virtual void setScalarVariable(const EffectVariableHandle /*variableHandle*/, int /*value*/) override;
46 virtual void setScalarVariable(const EffectVariableHandle /*variableHandle*/, const int* /*value*/, size_t /*count*/) override;
47
48 virtual void setVector2Variable(const StringView& /*name*/, const float* /*value*/) override;
49 virtual void setVector2Variable(const EffectVariableHandle /*variableHandle*/, const float* /*value*/, size_t /*count*/ = 1) override;
50
51 virtual void setVector3Variable(const StringView& /*name*/, const float* /*value*/) override;
52 virtual void setVector3Variable(const EffectVariableHandle /*variableHandle*/, const float* /*value*/, size_t /*count*/ = 1) override;
53
54 virtual void setVector4Variable(const StringView& /*name*/, const float* /*value*/) override;
55 virtual void setVector4Variable(const EffectVariableHandle /*variableHandle*/, const float* /*value*/, size_t /*count*/ = 1) override;
56 virtual void setVector4Variable(const EffectVariableHandle /*variableHandle*/, const int* /*value*/, size_t /*count*/ = 1) override;
57
58 virtual void setMatrixVariable(const StringView& /*name*/, const float* /*value*/) override;
59 virtual void setMatrixVariable(const EffectVariableHandle /*variableHandle*/, const float* /*value*/) override;
60 virtual void setMatrixVariable(const EffectVariableHandle /*variableHandle*/, const float* /*value*/, size_t /*count*/) override;
61
62 virtual void setTexture(const StringView& /*name*/, unsigned int /*unit*/, TextureHandle /*textureHandle*/) override;
63 virtual void setTexture(const TextureBindingHandle /*textureBindingHandle*/, const TextureHandle /*textureHandle*/) override;
64 virtual void setTexture(const StringView& /*name*/, TextureViewHandle /*textureViewHandle*/) override;
65 virtual void setTexture(const TextureBindingHandle /*textureBindingHandle*/, TextureViewHandle /*textureViewHandle*/) override;
66
67 virtual void setSamplerState(const StringView& /*name*/, unsigned int /*unit*/, SamplerStateHandle /*samplerStateHandle*/) override;
68 virtual void setSamplerState(const SamplerStateBindingHandle /*samplerStateBindingHandle*/, const SamplerStateHandle /*samplerStateHandle*/) override;
69
70 virtual void setInputLayout(const InputLayoutHandle /*inputLayoutHandle*/) override;
71
72 virtual void setVertexBuffers(const VertexBufferHandle* /*vertexBufferHandles*/, const size_t /*count*/, const uint32_t* /*strides*/, const uint32_t* /*offsets*/) override;
73 virtual void setVertexBuffers(const VertexBufferHandle* /*vertexBufferHandles*/, const size_t /*count*/) override;
74
75 virtual void setIndexBuffer(IndexBufferHandle /*bufferHandle*/, uint32_t /*stride*/ = 4, uint32_t /*offset*/ = 0) override;
76
77 virtual void setVertexArrayObject(VertexArrayObjectHandle /*vertexArrayObject*/) override;
78
79 virtual void setConstantBuffer(const StringView& /*name*/, const BufferHandle /*bufferHandle*/, const uint32_t /*offset*/ = 0, const uint32_t /*size*/ = ~0u) override;
80 virtual void setConstantBuffer(const ConstantBufferBindingHandle /*bufferBindingHandle*/, const BufferHandle /*bufferHandle*/, const uint32_t /*offset*/ = 0, const uint32_t /*size*/ = ~0u) override;
81 virtual void setBuffer(const StringView& /*name*/, BufferHandle /*bufferHandle*/) override;
82 virtual void setBuffer(const BufferBindingHandle /*bufferBindingHandle*/, BufferHandle /*bufferHandle*/) override;
83 virtual void setBufferCounter(BufferHandle /*bufferHandle*/, uint32_t /*value*/) override;
84 virtual void setBufferCounter(BufferHandle /*bufferHandle*/, BufferHandle /*sourceBufferHandle*/) override;
85
86 virtual void getBufferCounter(BufferHandle /*bufferHandle*/, BufferHandle /*destinationBufferHandle*/) override;
87 virtual uint32_t getBufferCounter(BufferHandle /*bufferHandle*/) override { return 0; }
88
89 virtual void draw(PrimitiveType::EPrimitiveType /*primitiveType*/, const size_t /*startVertex*/, const size_t /*numVertexes*/) override;
90 virtual void drawIndexed(PrimitiveType::EPrimitiveType /*primitiveType*/, const size_t /*startIndex*/, const size_t /*numIndexes*/, const size_t /*startVertex*/ = 0) override;
91 virtual void drawInstanced(PrimitiveType::EPrimitiveType /*primitiveType*/, const size_t /*startVertex*/, const size_t /*numVertexes*/, const size_t /*startInstance*/, const size_t /*numInstances*/) override;
92 virtual void drawInstancedIndexed(PrimitiveType::EPrimitiveType /*primitiveType*/, const size_t /*startInstance*/, const size_t /*numInstances*/, const size_t /*startIndex*/, const size_t /*numIndexes*/) override;
93
94 virtual void dispatchCompute(const unsigned int /*threadGroupsX*/, const unsigned int /*threadGroupsY*/, const unsigned int /*threadGroupsZ*/) override;
95
96 virtual void readDepthBuffer(BufferHandle /*bufferHandle*/, int /*x*/, int /*y*/, int /*width*/, int /*height*/, Framebuffer::EFrameBuffer /*framebuffer*/) override;
97 virtual void readColorBuffer(BufferHandle /*bufferHandle*/, int /*x*/, int /*y*/, int /*width*/, int /*height*/, Framebuffer::EFrameBuffer /*framebuffer*/) override;
98
99 virtual void*map(BufferHandle /*bufferHandle*/, MapMode::EMapMode /*mapMode*/, uint32_t* /*stride*/ = nullptr) override;
100 virtual void*map(TextureHandle /*textureHandle*/, MapMode::EMapMode /*accessMode*/, uint32_t* /*rowPitch*/, uint32_t* /*depthPitch*/) override;
101 virtual void unmap(BufferHandle /*bufferHandle*/) override;
102 virtual void unmap(TextureHandle /*textureHandle*/) override;
103 virtual void updateBuffer(BufferHandle /*bufferHandle*/, const void* /*data*/, const size_t /*size*/) override;
104
105 virtual void updateSubTexture(TextureHandle /*textureHandle*/, const size_t /*level*/, const void* /*data*/) override;
106 virtual void updateSubBuffer(BufferHandle /*bufferHandle*/, const size_t /*offset*/, const size_t /*size*/, const void* /*data*/) override;
107
108 virtual void resolveResource(TextureHandle /*source*/, TextureHandle /*destination*/) override;
109 virtual void copyResource(BufferHandle /*destinationHandle*/, BufferHandle /*sourceHandle*/) override;
110 virtual void copyResource(TextureHandle /*destinationHandle*/, TextureHandle /*sourceHandle*/) override;
111 virtual void copyTexture(TextureHandle /*dstHandle*/, unsigned /*dstSub*/, unsigned /*dstX*/, unsigned /*dstY*/, unsigned /*dstZ*/, TextureHandle /*sourceHandle*/, unsigned /*srcSub*/) override;
112
113 virtual void clearResource(BufferHandle /*destinationHandle*/, uint32_t* /*values*/) override;
114 virtual void clearResource(BufferHandle /*destinationHandle*/, float* /*values*/) override;
115
116 private:
117 FrameStatistics frameStatistics;
118 UploadStatistics uploadStatistics;
119
120 EffectHandle getCurrentEffect();
121 EffectsWebGPU *getEffects();
122 public:
123 void beginRenderPassInt();
124 void endRenderPassInt();
125 void updateRenderPass();
126 bool updateRenderPipeline(PrimitiveType::EPrimitiveType primitiveType);
127
128 void beginComputePass();
129 void endComputePass();
130 void updateComputePass();
131 void updateComputePipeline();
132
133 GraphicsDeviceWebGPU *graphicsDevice = nullptr;
134
135 WGPURenderPassEncoder renderPassEncoder = 0;
136 WGPUComputePassEncoder computePassEncoder = 0;
137
138 EffectHandle effect = {};
139 InputLayoutHandle inputLayoutHandle = {};
140 RasterizerStateHandle rasterizeStateHandle = {};
141 DepthStencilStateHandle depthStencilStateHandle = {};
142 BlendStateHandle blendStateHandle = {};
143
144 RenderPipelineHandle currentRenderPipeline = {};
145 ComputePipelineHandle currentComputePipeline = {};
146
147 bool inRenderPass = false;
148 RenderTargetHandle renderTargetHandle = {};
149 DepthStencilHandle depthStencilHandle = {};
150 WGPUColor clearColor[8] = {};
151 float clearDepthVal = 1.0f;
152 bool do_clear_render_target = false;
153 bool do_clear_depth = false;
154 bool update_render_target = true;
155
156 WGPUBindGroup current_bind_group = {};
157
158 bool update_descriptors = false;
159 std::map<uint32_t, WGPUBindGroupEntry> descriptors;
160 };
161}
virtual void setMatrixVariable(const StringView &, const float *) override
Sets the matrix variable with the given name in the current effect to value.
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 getBufferCounter(BufferHandle, BufferHandle) override
Get the associated counter of a buffer.
virtual void setScalarVariable(const StringView &, const float) override
Sets the scalar floating point variable with the given name to the given value.
virtual void setVector2Variable(const StringView &, const float *) override
Sets the vector variable with the given name to the given two-component value.
virtual void setEffect(EffectHandle) override
Set the current effect.
virtual void setVector4Variable(const StringView &, const float *) override
Sets the vector variable with the given name to the given four-component value.
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 draw(PrimitiveType::EPrimitiveType, const size_t, const size_t) override
Draws non-indexed, non-instanced primitives.
virtual void setVertexBuffers(const VertexBufferHandle *, const size_t, const uint32_t *, const uint32_t *) override
Sets the current vertex buffers.
virtual void setBuffer(const StringView &, BufferHandle) override
Sets the given buffer to the buffer binding slot with the given name.
virtual void setVariable(const EffectVariableHandle, const uint8_t *, size_t) override
Sets the variable with the given name and size.
virtual uint32_t getBufferCounter(BufferHandle) override
Get the associated counter of a buffer.
Definition: ContextWebGPU.h:87
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 drawIndexed(PrimitiveType::EPrimitiveType, const size_t, const size_t, const size_t=0) override
Draws indexed, non-instanced primitives.
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 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 drawInstanced(PrimitiveType::EPrimitiveType, const size_t, const size_t, const size_t, const size_t) override
Draws non-indexed, instanced primitives.
virtual void endRenderPass() override
End a render pass.
virtual void readColorBuffer(BufferHandle, int, int, int, int, Framebuffer::EFrameBuffer) override
Reads data from the current render target into the given bufferHandle.
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 setVector3Variable(const StringView &, const float *) override
Sets the vector variable with the given name to the given three-component value.
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 drawInstancedIndexed(PrimitiveType::EPrimitiveType, const size_t, const size_t, const size_t, const size_t) override
Draws indexed, instanced primitives.
virtual void readDepthBuffer(BufferHandle, int, int, int, int, Framebuffer::EFrameBuffer) override
Reads data from the current depth target into the given bufferHandle.
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:24
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Handle template class used to provide opaque, non-converting handles.
Definition: Common.h:22
Represents a graphics device context which can receive rendering commands.
Definition: IContext.h:43
EMapMode
Mapping mode enumeration.
Definition: Flags.h:93
EPrimitiveType
Primitive type enumeration.
Definition: Common.h:114