Cogs.Core
EffectsWebGPU.h
1#pragma once
2
3#include "CommonWebGPU.h"
4
5#include "../Base/EffectsCommon.h"
6
7#include <map>
8#include <unordered_map>
9#include "BindGroupsWebGPU.h"
10
11namespace Cogs {
12 class GraphicsDeviceWebGPU;
13 struct BindGroupWebGPU;
14
15 enum WebGPUConstantBufferType { UniformBuffer, Texture, Sampler };
16
18 size_t group =0;
19 size_t nameHash =0;
20 WGPUBindGroupLayoutEntry bg_ent;
21 std::string name;
22 };
23
25 uint8_t semantic;
26 uint8_t slot;
27 uint8_t binding;
28 WGPUVertexFormat format;
29 size_t nameHash;
30 };
31
32 struct EffectWebGPU : public Effect
33 {
34 static const size_t maxTexUnits = 128;
35 static const size_t maxConstantBuffers = 128;
36 static const size_t maxVertexAttribs = 32;
37 WGPUShaderModule vs_module;
38 WGPUShaderModule fs_module;
39 WGPUShaderModule cs_module;
40 std::string vs_entry;
41 std::string fs_entry;
42 std::string cs_entry;
43 std::string name;
44 size_t num_bindings;
45 size_t num_attribs;
46 WebGPUConstantBufferBinding constantBufferBindings[maxConstantBuffers];
47 SemanticSlotBinding semanticSlotBindings[maxVertexAttribs];
48 BindGroupSetDescription bindGroupSetDescription;
53 size_t bindGroupDescHash[MaxBindGroups] = {};
54
57 {
58 for (size_t g = 0; g < MaxBindGroups; ++g) {
59 bindGroupDescHash[g] = bindGroupSetDescription.groups[g].hash();
60 }
61 }
62
63 size_t hash(size_t base) const
64 {
65 base = Cogs::hash((size_t)vs_module, base);
66 base = Cogs::hash((size_t)fs_module, base);
67 base = Cogs::hash((size_t)cs_module, base);
68 base = Cogs::hash(vs_entry.c_str(), base);
69 base = Cogs::hash(fs_entry.c_str(), base);
70 base = Cogs::hash(cs_entry.c_str(), base);
71 return base;
72 }
73 };
74
75
77 {
78 public:
79 void initialize(GraphicsDeviceWebGPU *device, IBuffers * buffers);
80
81 virtual void releaseEffect(EffectHandle effectHandle) override;
82 virtual void releaseResources() override;
83
84 virtual void annotate(EffectHandle /*handle*/, const StringView& /*name*/) override {}
85 virtual void annotateVS(EffectHandle /*handle*/, const StringView& /*name*/) override {}
86 virtual void annotateGS(EffectHandle /*handle*/, const StringView& /*name*/) override {}
87 virtual void annotatePS(EffectHandle /*handle*/, const StringView& /*name*/) override {}
88 virtual void annotateCS(EffectHandle /*handle*/, const StringView& /*name*/) override {}
89
90 virtual EffectHandle loadComputeEffect(const StringView& /*fileName*/, EffectFlags::EEffectFlags /*effectFlags*/) override;
91 virtual EffectHandle loadComputeEffect(const StringView& /*fileName*/, const PreprocessorDefinitions& /*defines*/, EffectFlags::EEffectFlags /*effectFlags*/) override;
92
93 virtual EffectHandle load(const ProcessedContent& /*vsSource*/,
94 const ProcessedContent& /*hsSource*/,
95 const ProcessedContent& /*dsSource*/,
96 const ProcessedContent& /*gsSource*/,
97 const ProcessedContent& /*psSource*/,
98 const StringView& /*vsEntryPoint*/,
99 const StringView& /*hsEntryPoint*/,
100 const StringView& /*dsEntryPoint*/,
101 const StringView& /*gsEntryPoint*/,
102 const StringView& /*psEntryPoint*/,
103 const EffectDescription& /*desc*/) override;
104
105 virtual const BindGroupDescription& getBindGroupDescription(EffectHandle effectHandle, uint32_t groupIndex) override;
106
113 size_t getBindGroupDescHash(EffectHandle effectHandle, uint32_t groupIndex);
114
116 BufferBindingHandle getBufferBinding(EffectHandle effectHandle, const StringView& name) override;
117 void releaseBufferBinding(BufferBindingHandle /*bufferBindingHandle*/) override
118 {
119 }
120 TextureBindingHandle getTextureBinding(EffectHandle effectHandle, const StringView& name, const unsigned int slot) override;
121 SamplerStateBindingHandle getSamplerStateBinding(EffectHandle effectHandle, const StringView& /*name*/, const unsigned int slot) override;
122
123 bool addConstantBufferBindings(EffectWebGPU& effect, const std::vector<Cogs::WebGPUConstantBufferBinding>& bindings);
124
125 uint32_t getNumBindGroups(EffectHandle effectHandle) override;
126
127 GraphicsDeviceWebGPU *graphicsDevice = nullptr;
128
130
131 private:
132 };
133}
uint32_t getNumBindGroups(EffectHandle effectHandle) override
Query the number of bind-group layouts exposed by the given effect.
virtual void annotate(EffectHandle, const StringView &) override
Associate a name with an object for use in graphics debugging.
Definition: EffectsWebGPU.h:84
virtual void annotateVS(EffectHandle, const StringView &) override
Associate a name with an object for use in graphics debugging.
Definition: EffectsWebGPU.h:85
void releaseBufferBinding(BufferBindingHandle) override
Release a handle to a buffer binding.
TextureBindingHandle getTextureBinding(EffectHandle effectHandle, const StringView &name, const unsigned int slot) override
Get a handle to a texture object binding, mapping how to bind textures to the given effect.
virtual void annotateGS(EffectHandle, const StringView &) override
Associate a name with an object for use in graphics debugging.
Definition: EffectsWebGPU.h:86
virtual void releaseEffect(EffectHandle effectHandle) override
Release the effect with the given handle, freeing all resources generated during program loading.
BufferBindingHandle getBufferBinding(EffectHandle effectHandle, const StringView &name) override
Get a handle to a buffer binding.
virtual void annotateCS(EffectHandle, const StringView &) override
Associate a name with an object for use in graphics debugging.
Definition: EffectsWebGPU.h:88
virtual EffectHandle loadComputeEffect(const StringView &, EffectFlags::EEffectFlags) override
Load the compute shader with the given file name and create an effect.
ConstantBufferBindingHandle getConstantBufferBinding(EffectHandle effectHandle, const StringView &name) override
Get a handle to a constant buffer binding, mapping how to bind a constant buffer to the given effect.
virtual void releaseResources() override
Release all allocated effect resources.
SamplerStateBindingHandle getSamplerStateBinding(EffectHandle effectHandle, const StringView &, const unsigned int slot) override
Get a handle to a sampler state object binding, mapping how to bind the sampler state to the given ef...
size_t getBindGroupDescHash(EffectHandle effectHandle, uint32_t groupIndex)
Cached hash of the description returned by getBindGroupDescription, matching it for out-of-range grou...
virtual void annotatePS(EffectHandle, const StringView &) override
Associate a name with an object for use in graphics debugging.
Definition: EffectsWebGPU.h:87
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:50
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
Definition: HashFunctions.h:62
std::vector< PreprocessorDefinition > PreprocessorDefinitions
A set of preprocessor definitions.
Definition: IEffects.h:20
Contains an effect description used to load a single effect.
Definition: IEffects.h:62
EEffectFlags
Effect source flags.
Definition: IEffects.h:27
void updateBindGroupDescHashes()
Refresh bindGroupDescHash. Call once bindGroupSetDescription is final.
Definition: EffectsWebGPU.h:56
size_t bindGroupDescHash[MaxBindGroups]
Hash of each entry in bindGroupSetDescription.groups, letting bind group compatibility be tested with...
Definition: EffectsWebGPU.h:53
Provides buffer management functionality.
Definition: IBuffers.h:13