1#include "PipelineStatesWebGPU.h"
3#include "GraphicsDeviceWebGPU.h"
5#include "Foundation/Logging/Logger.h"
10 WGPUBlendOperation wgpu(Cogs::BlendState::BlendOperation opp)
12 if(opp == Cogs::BlendState::BlendOperation::Add)
13 return WGPUBlendOperation_Add;
14 if(opp == Cogs::BlendState::BlendOperation::Subtract)
15 return WGPUBlendOperation_Subtract;
16 if(opp == Cogs::BlendState::BlendOperation::ReverseSubtract)
17 return WGPUBlendOperation_ReverseSubtract;
18 if(opp == Cogs::BlendState::BlendOperation::Min)
19 return WGPUBlendOperation_Min;
20 if(opp == Cogs::BlendState::BlendOperation::Max)
21 return WGPUBlendOperation_Max;
23 return WGPUBlendOperation_Add;
28 if(blend == Cogs::BlendState::Blend::Zero)
29 return WGPUBlendFactor_Zero;
30 if(blend == Cogs::BlendState::Blend::One)
31 return WGPUBlendFactor_One;
32 if(blend == Cogs::BlendState::Blend::SourceColor)
33 return WGPUBlendFactor_Src;
34 if(blend == Cogs::BlendState::Blend::InverseSourceColor)
35 return WGPUBlendFactor_OneMinusSrc;
36 if(blend == Cogs::BlendState::Blend::SourceAlpha)
37 return WGPUBlendFactor_SrcAlpha;
38 if(blend == Cogs::BlendState::Blend::InverseSourceAlpha)
39 return WGPUBlendFactor_OneMinusSrcAlpha;
40 if(blend == Cogs::BlendState::Blend::DestinationAlpha)
41 return WGPUBlendFactor_DstAlpha;
42 if(blend == Cogs::BlendState::Blend::InverseDestinationAlpha)
43 return WGPUBlendFactor_OneMinusDstAlpha;
44 if(blend == Cogs::BlendState::Blend::DestinationColor)
45 return WGPUBlendFactor_Dst;
46 if(blend == Cogs::BlendState::Blend::InverseDestinationColor)
47 return WGPUBlendFactor_OneMinusDst;
48 if(blend == Cogs::BlendState::Blend::SourceAlphaSaturate)
49 return WGPUBlendFactor_SrcAlphaSaturated;
50 if(blend == Cogs::BlendState::Blend::BlendFactor)
51 return WGPUBlendFactor_Constant;
52 if(blend == Cogs::BlendState::Blend::InverseBlendFactor)
53 return WGPUBlendFactor_OneMinusConstant;
55 return WGPUBlendFactor_Zero;
61 void PipelineStatesWebGPU::initialize(GraphicsDeviceWebGPU *device)
63 graphics_device = device;
66 size_t PipelineStatesWebGPU::renderPipelineHash(EffectHandle effectHandle,
67 InputLayoutHandle inputLayoutHandle,
69 RasterizerStateHandle rasterizeStateHandle,
70 DepthStencilStateHandle depthStencilStateHandle,
71 BlendStateHandle blendStateHandle,
72 RenderTargetHandle renderTargetHandle,
73 DepthStencilHandle depthStencilHandle)
75 EffectsWebGPU &effects = graphics_device->effects;
76 BuffersWebGPU &buffers = graphics_device->buffers;
77 RenderTargetsWebGPU &render_targets = graphics_device->renderTargets;
81 EffectWebGPU& effect = effects.effects[effectHandle];
82 pso_hash = effect.hash(pso_hash);
85 InputLayoutWebGPU& layout = buffers.inputLayouts[inputLayoutHandle];
86 pso_hash = layout.hash(pso_hash);
88 pso_hash =
Cogs::hash(primitiveType, pso_hash);
89 pso_hash =
Cogs::hash(rasterizeStateHandle.handle, pso_hash);
90 pso_hash =
Cogs::hash(depthStencilStateHandle.handle, pso_hash);
91 pso_hash =
Cogs::hash(blendStateHandle.handle, pso_hash);
93 RenderTargetWebGPU& render_target = render_targets.render_targets[renderTargetHandle];
94 pso_hash = render_target.hash(pso_hash);
97 DepthStencilTargetWebGPU& depth_target = render_targets.depth_stencil_targets[depthStencilHandle];
98 pso_hash = depth_target.hash(pso_hash);
102 RenderPipelineHandle PipelineStatesWebGPU::loadRenderPipeline(EffectHandle effectHandle,
103 InputLayoutHandle inputLayoutHandle,
105 RasterizerStateHandle rasterizeStateHandle,
106 DepthStencilStateHandle depthStencilStateHandle,
107 BlendStateHandle blendStateHandle,
108 RenderTargetHandle renderTargetHandle,
109 DepthStencilHandle depthStencilHandle)
111 WGPUDevice device = graphics_device->device;
112 BuffersWebGPU &buffers = graphics_device->buffers;
113 RenderTargetsWebGPU &renderTargets = graphics_device->renderTargets;
115 size_t pso_hash = renderPipelineHash(effectHandle,
118 rasterizeStateHandle,
119 depthStencilStateHandle,
123 auto iter = renderPipelineHashMap.find(pso_hash);
124 if(iter != renderPipelineHashMap.end()){
128 EffectWebGPU &effect = graphics_device->effects.effects[effectHandle];
129 const RasterizerState &rs =
130 rasterizeStateHandle ?
131 *
reinterpret_cast<RasterizerState*
>(rasterizeStateHandle.handle):
137 const DepthStencilState &ds =
138 depthStencilStateHandle ?
139 *
reinterpret_cast<DepthStencilState*
>(depthStencilStateHandle.handle) :
141 const BlendState &bsc =
143 reinterpret_cast<BlendState*
>(blendStateHandle.handle)[0] :
145 const BlendState &bsa =
147 reinterpret_cast<BlendState*
>(blendStateHandle.handle)[1] :
150 LOG_INFO(logger,
"PipelineStateLoad %s", effect.name.c_str());
152 WGPURenderPipelineDescriptor desc = {};
153 desc.label = {effect.name.c_str(), WGPU_STRLEN};
154 desc.layout =
nullptr;
155 WGPUBindGroupLayout bind_group_layout;
157 WGPUBindGroupLayoutEntry layoutEntryList[EffectWebGPU::maxConstantBuffers];
158 size_t numConstantBufferBindings = 0;
159 const WebGPUConstantBufferBinding* bindings;
160 bindings = graphics_device->effects.getConstantBufferBindings(effectHandle, numConstantBufferBindings);
161 for (
size_t i = 0; i < numConstantBufferBindings; i++) {
162 assert(bindings[i].group == 0);
163 layoutEntryList[i] = bindings[i].bg_ent;
165 WGPUBindGroupLayoutDescriptor bind_group_layout_desc = {};
166 bind_group_layout_desc.label = {effect.name.c_str(), WGPU_STRLEN};
167 bind_group_layout_desc.entryCount = numConstantBufferBindings;
168 bind_group_layout_desc.entries = layoutEntryList;
169 bind_group_layout = wgpuDeviceCreateBindGroupLayout(device, &bind_group_layout_desc);
171 WGPUPipelineLayoutDescriptor pipeline_layout_desc = {};
172 pipeline_layout_desc.bindGroupLayoutCount = 1;
173 pipeline_layout_desc.bindGroupLayouts = &bind_group_layout;
174 WGPUPipelineLayout pipeline_layout = wgpuDeviceCreatePipelineLayout(device, &pipeline_layout_desc);
175 desc.label = {effect.name.c_str(), WGPU_STRLEN};
176 desc.layout = pipeline_layout;
179 WGPUPrimitiveState &primitive_state = desc.primitive;
183 primitive_state.topology = WGPUPrimitiveTopology_PointList;
184 primitive_state.stripIndexFormat = WGPUIndexFormat_Undefined;
187 primitive_state.topology = WGPUPrimitiveTopology_LineList;
188 primitive_state.stripIndexFormat = WGPUIndexFormat_Undefined;
191 primitive_state.topology = WGPUPrimitiveTopology_LineStrip;
192 primitive_state.stripIndexFormat = WGPUIndexFormat_Uint32;
195 primitive_state.topology = WGPUPrimitiveTopology_TriangleList;
196 primitive_state.stripIndexFormat = WGPUIndexFormat_Undefined;
199 primitive_state.topology = WGPUPrimitiveTopology_TriangleStrip;
200 primitive_state.stripIndexFormat = WGPUIndexFormat_Uint32;
206 if(rs.frontCounterClockwise)
207 primitive_state.frontFace = WGPUFrontFace_CCW;
209 primitive_state.frontFace = WGPUFrontFace_CW;
211 primitive_state.cullMode = WGPUCullMode_Front;
213 primitive_state.cullMode = WGPUCullMode_Back;
215 primitive_state.cullMode = WGPUCullMode_None;
218 WGPUVertexState &vertex_state = desc.vertex;
220 vertex_state.module = effect.vs_module;
221 vertex_state.entryPoint = {effect.vs_entry.c_str(), WGPU_STRLEN};
222 vertex_state.constantCount = 0;
223 vertex_state.constants =
nullptr;
224 InputLayoutWebGPU &layout = buffers.inputLayouts[inputLayoutHandle];
225 vertex_state.bufferCount = (uint32_t)layout.vertex_buffer_layout.size();
226 vertex_state.buffers = layout.vertex_buffer_layout.data();
231 uint32_t samples = 0;
232 WGPUDepthStencilState depthStencil = {};
235 SwapChainWebGPU &defaultSwapChain = graphics_device->defaultSwapChain;
236 depthStencil.format = defaultSwapChain.depth_format;
237 samples = defaultSwapChain.samples;
240 DepthStencilTargetWebGPU &dst = renderTargets.depth_stencil_targets[depthStencilHandle];
241 depthStencil.format = dst.format;
242 samples = dst.samples;
246 depthStencil.depthWriteEnabled =
static_cast<WGPUOptionalBool
>(ds.writeEnabled);
249 depthStencil.depthCompare = WGPUCompareFunction_Never;
251 depthStencil.depthCompare = WGPUCompareFunction_Less;
253 depthStencil.depthCompare = WGPUCompareFunction_LessEqual;
255 depthStencil.depthCompare = WGPUCompareFunction_Equal;
257 depthStencil.depthCompare = WGPUCompareFunction_GreaterEqual;
259 depthStencil.depthCompare = WGPUCompareFunction_Greater;
261 depthStencil.depthCompare = WGPUCompareFunction_NotEqual;
263 depthStencil.depthCompare = WGPUCompareFunction_Always;
268 depthStencil.depthWriteEnabled = WGPUOptionalBool(
false);
269 depthStencil.depthCompare = WGPUCompareFunction_Always;
272 depthStencil.stencilFront.compare = WGPUCompareFunction_Always;
273 depthStencil.stencilFront.failOp = WGPUStencilOperation_Keep;
274 depthStencil.stencilFront.depthFailOp = WGPUStencilOperation_Keep;
275 depthStencil.stencilFront.passOp = WGPUStencilOperation_Keep;
277 depthStencil.stencilBack.compare = WGPUCompareFunction_Always;
278 depthStencil.stencilBack.failOp = WGPUStencilOperation_Keep;
279 depthStencil.stencilBack.depthFailOp = WGPUStencilOperation_Keep;
280 depthStencil.stencilBack.passOp = WGPUStencilOperation_Keep;
282 depthStencil.stencilReadMask = 0;
283 depthStencil.stencilWriteMask = 0;
285 depthStencil.depthBias = (int32_t)rs.depthBias;
286 depthStencil.depthBiasSlopeScale = rs.slopeScaledDepthBias;
287 depthStencil.depthBiasClamp = rs.depthBiasClamp;
289 desc.depthStencil = &depthStencil;
292 WGPUColorTargetState color_target[8] = {};
293 uint32_t color_target_count = 0;
295 RenderTargetWebGPU &rt = renderTargets.render_targets[renderTargetHandle];
296 samples = glm::max(rt.samples, samples);
297 color_target_count = rt.count;
298 for(uint32_t i=0; i<rt.count; i++){
299 color_target[i].format = rt.format[i];
300 color_target[i].writeMask = WGPUColorWriteMask_All;
303 else if(use_swap_chain){
304 SwapChainWebGPU &defaultSwapChain = graphics_device->defaultSwapChain;
305 color_target_count = 1;
306 color_target[0].format = defaultSwapChain.color_format;
307 color_target[0].writeMask = WGPUColorWriteMask_All;
310 WGPUBlendState blend_state = {};
311 if(bsc.enabled || bsa.enabled){
313 blend_state.color.operation = wgpu(bsc.operation);
314 blend_state.color.srcFactor = wgpu(bsc.sourceBlend);
315 blend_state.color.dstFactor = wgpu(bsc.destinationBlend);
316 blend_state.alpha.operation = wgpu(bsa.operation);
317 blend_state.alpha.srcFactor = wgpu(bsa.sourceBlend);
318 blend_state.alpha.dstFactor = wgpu(bsa.destinationBlend);
319 for(uint32_t i=0; i<color_target_count; i++){
320 color_target[i].blend = &blend_state;
324 WGPUFragmentState fragment = {};
325 if(effect.fs_module){
326 fragment.module = effect.fs_module;
327 fragment.entryPoint = {effect.fs_entry.c_str(), WGPU_STRLEN};
328 fragment.constantCount = 0;
329 fragment.constants =
nullptr;
330 if(color_target_count > 0){
331 fragment.targetCount = color_target_count;
332 fragment.targets = color_target;
334 desc.fragment = &fragment;
337 WGPUMultisampleState &multisample = desc.multisample;
339 multisample.count = samples;
340 multisample.mask = ~0x00000000u;
341 multisample.alphaToCoverageEnabled =
false;
344 WGPURenderPipeline pipeline = wgpuDeviceCreateRenderPipeline(device, &desc);
347 RenderPipelineWebGPU pipelineState = {};
348 pipelineState.hash = pso_hash;
349 pipelineState.effect = effectHandle;
350 pipelineState.inputLayoutHandle = inputLayoutHandle;
351 pipelineState.primitiveType = primitiveType;
352 pipelineState.rasterizeStateHandle = rasterizeStateHandle;
353 pipelineState.depthStencilStateHandle = depthStencilStateHandle;
354 pipelineState.blendStateHandle = blendStateHandle;
355 pipelineState.renderTargetHandle = renderTargetHandle;
356 pipelineState.depthStencilHandle = depthStencilHandle;
358 pipelineState.pipeline = pipeline;
359 pipelineState.layout = bind_group_layout;
361 auto handle = this->renderPipeline.addResource(std::move(pipelineState));
362 renderPipelineHashMap.insert({pso_hash, handle});
366 ComputePipelineHandle PipelineStatesWebGPU::loadComputePipeline(EffectHandle effectHandle)
368 WGPUDevice device = graphics_device->device;
371 pso_hash =
Cogs::hash(effectHandle.handle, pso_hash);
372 auto iter = computePipelineHashMap.find(pso_hash);
373 if(iter != computePipelineHashMap.end()){
377 EffectWebGPU &effect = graphics_device->effects.effects[effectHandle];
379 LOG_INFO(logger,
"PipelineStateLoad %s", effect.name.c_str());
381 WGPUComputePipelineDescriptor desc = {};
382 desc.label = {effect.name.c_str(), WGPU_STRLEN};
383 desc.layout =
nullptr;
384 WGPUComputeState &compute = desc.compute;
385 compute.module = effect.cs_module;
386 compute.entryPoint = {effect.cs_entry.c_str(), WGPU_STRLEN};
387 compute.constantCount = 0;
388 compute.constants =
nullptr;
389 WGPUComputePipeline pipeline = wgpuDeviceCreateComputePipeline(device, &desc);
392 uint32_t groupIndex = 0;
393 WGPUBindGroupLayout layout = wgpuComputePipelineGetBindGroupLayout(pipeline, groupIndex);
396 ComputePipelineWebGPU pipelineState = {};
397 pipelineState.hash = pso_hash;
398 pipelineState.effect = effectHandle;
400 pipelineState.pipeline = pipeline;
401 pipelineState.layout = layout;
403 auto handle = this->computePipeline.addResource(std::move(pipelineState));
404 computePipelineHashMap.insert({pso_hash, handle});
Log implementation class.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
PrimitiveType
Primitive types for interpreting vertex data sent to the graphics pipeline.
@ PointList
List of points.
@ TriangleStrip
Triangle strip.
@ TriangleListAdjacency
List of triangles with adjacency.
@ TriangleList
List of triangles.
static BlendState DefaultState()
Creates a blend state object initialized with the default settings.
Blend
Options for blend functions.
@ Never
Never evaluates to true. When using this, all objects will fail depth testing.
@ GreaterOrEqual
Greater or equal depth.
@ NotEqual
Depth not equal evaluates to true.
@ Always
Always evaluates to true.
@ LessOrEqual
Less or equal depth.
static DepthStencilState DefaultState()
Constructs a depth stencil state object initialized with the default values.
static RasterizerState DefaultState()
Constructs a rasterizer state initialized with the default values.
@ Back
Cull back facing primitives.
@ Front
Cull front facing primitives.