Skip to content

Commit fb6fdfe

Browse files
[WebGPU, Dawn]: Update Dawn and adapt the WebGPU backend to the new Tint API
1 parent afeba46 commit fb6fdfe

7 files changed

Lines changed: 72 additions & 32 deletions

File tree

Graphics/Archiver/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ if(WEBGPU_SUPPORTED)
171171
target_link_libraries(Diligent-Archiver-static PRIVATE Diligent-GraphicsEngineWebGPU-static)
172172
if (NOT PLATFORM_WEB)
173173
target_link_libraries(Diligent-Archiver-static PRIVATE dawn_native dawn_proc)
174+
# dawn_version's generated header propagates here as a source; re-mark it GENERATED in this scope (CMP0118 is OLD).
175+
if (TARGET dawn_version)
176+
get_target_property(DAWN_VERSION_HEADERS dawn_version INTERFACE_SOURCES)
177+
if (DAWN_VERSION_HEADERS)
178+
set_source_files_properties(${DAWN_VERSION_HEADERS} PROPERTIES GENERATED TRUE)
179+
endif()
180+
endif()
174181
endif()
175182
target_include_directories(Diligent-Archiver-static
176183
PRIVATE

Graphics/GraphicsEngine/include/ShaderResourceVariableBase.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ struct ShaderVariableBase : public ResourceVariableBaseInterface
840840
const PipelineResourceDesc& ResDesc = pThis->GetDesc();
841841

842842
const SHADER_RESOURCE_VARIABLE_TYPE_FLAGS VarTypeFlag = static_cast<SHADER_RESOURCE_VARIABLE_TYPE_FLAGS>(1u << ResDesc.VarType);
843-
if ((Flags & VarTypeFlag) == 0)
843+
if ((static_cast<Uint32>(Flags) & static_cast<Uint32>(VarTypeFlag)) == 0)
844844
return; // This variable type is not being processed
845845

846846
if ((StaleVarTypes & VarTypeFlag) != 0)

Graphics/GraphicsEngineWebGPU/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ endif()
136136

137137
if (NOT PLATFORM_WEB)
138138
list(APPEND PRIVATE_DEPENDENCIES dawn_native dawn_proc)
139+
140+
# dawn_version's generated header propagates here as a source; re-mark it GENERATED in this scope (CMP0118 is OLD).
141+
if (TARGET dawn_version)
142+
get_target_property(DAWN_VERSION_HEADERS dawn_version INTERFACE_SOURCES)
143+
if (DAWN_VERSION_HEADERS)
144+
set_source_files_properties(${DAWN_VERSION_HEADERS} PROPERTIES GENERATED TRUE)
145+
endif()
146+
endif()
139147
endif()
140148

141149
target_link_libraries(Diligent-GraphicsEngineWebGPU-static

Graphics/GraphicsEngineWebGPU/include/PipelineStateWebGPUImpl.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <vector>
3333
#include <array>
3434
#include <string>
35+
#include <utility>
3536

3637
#include "EngineWebGPUImplTraits.hpp"
3738
#include "PipelineStateBase.hpp"
@@ -85,11 +86,9 @@ class PipelineStateWebGPUImpl final : public PipelineStateBase<EngineWebGPUImplT
8586
ShaderWebGPUImpl* const pShader;
8687
std::string PatchedWGSL;
8788

88-
// Per-stage specialization constant entries, built from user input
89-
// and WGSL override reflection during InitializePipeline().
90-
// Entries reference names in the shader resource name pool,
91-
// which is kept alive by ShaderWebGPUImpl.
92-
std::vector<WGPUConstantEntry> SpecConstEntries;
89+
// Per-stage specialization constants as (override @id string, value)
90+
// pairs, materialized into WGPUConstantEntry[] at pipeline creation.
91+
std::vector<std::pair<std::string, double>> SpecConstEntries;
9392

9493
ShaderStageInfo(ShaderWebGPUImpl* _pShader) :
9594
Type{_pShader->GetDesc().ShaderType},

Graphics/GraphicsEngineWebGPU/src/PipelineStateWebGPUImpl.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ double ConvertSpecConstantToDouble(const void* pData, Uint32 DataSize, SHADER_CO
8686
}
8787
}
8888

89+
std::vector<WGPUConstantEntry> GetWGPUConstantEntries(const std::vector<std::pair<std::string, double>>& SpecConsts)
90+
{
91+
std::vector<WGPUConstantEntry> Entries;
92+
Entries.reserve(SpecConsts.size());
93+
for (const std::pair<std::string, double>& SpecConst : SpecConsts)
94+
{
95+
WGPUConstantEntry Entry{};
96+
Entry.key = GetWGPUStringView(SpecConst.first);
97+
Entry.value = SpecConst.second;
98+
Entries.push_back(Entry);
99+
}
100+
return Entries;
101+
}
102+
89103
void BuildSpecializationDataWebGPU(PipelineStateWebGPUImpl::TShaderStages& ShaderStages,
90104
Uint32 NumSpecializationConstants,
91105
const SpecializationConstant* pSpecializationConstants,
@@ -103,6 +117,8 @@ void BuildSpecializationDataWebGPU(PipelineStateWebGPUImpl::TShaderStages& Shade
103117
if (!pShaderResources)
104118
continue;
105119

120+
Stage.SpecConstEntries.reserve(pShaderResources->GetNumSpecConstants());
121+
106122
for (Uint32 r = 0; r < pShaderResources->GetNumSpecConstants(); ++r)
107123
{
108124
const WGSLSpecializationConstantAttribs& ReflectedSC = pShaderResources->GetSpecConstant(r);
@@ -140,11 +156,8 @@ void BuildSpecializationDataWebGPU(PipelineStateWebGPUImpl::TShaderStages& Shade
140156
" (", ReflectedSize, " bytes).");
141157
}
142158

143-
WGPUConstantEntry Entry{};
144-
Entry.key = GetWGPUStringView(ReflectedSC.Name);
145-
Entry.value = ConvertSpecConstantToDouble(pUserConst->pData, pUserConst->Size, ReflectedType);
146-
147-
Stage.SpecConstEntries.push_back(Entry);
159+
Stage.SpecConstEntries.emplace_back(std::to_string(ReflectedSC.OverrideId),
160+
ConvertSpecConstantToDouble(pUserConst->pData, pUserConst->Size, ReflectedType));
148161
}
149162
}
150163
}
@@ -498,7 +511,9 @@ void PipelineStateWebGPUImpl::InitializeWebGPURenderPipeline(const TShaderStages
498511

499512
WGPUFragmentState wgpuFragmentState{};
500513

501-
std::vector<WebGPUShaderModuleWrapper> wgpuShaderModules{ShaderStages.size()};
514+
std::vector<WebGPUShaderModuleWrapper> wgpuShaderModules{ShaderStages.size()};
515+
std::vector<std::vector<WGPUConstantEntry>> wgpuStageConstants(ShaderStages.size());
516+
502517
for (size_t ShaderIdx = 0; ShaderIdx < ShaderStages.size(); ++ShaderIdx)
503518
{
504519
const ShaderStageInfo& Stage = ShaderStages[ShaderIdx];
@@ -517,18 +532,20 @@ void PipelineStateWebGPUImpl::InitializeWebGPURenderPipeline(const TShaderStages
517532
{
518533
case SHADER_TYPE_VERTEX:
519534
VERIFY(wgpuRenderPipelineDesc.vertex.module == nullptr, "Only one vertex shader is allowed");
535+
wgpuStageConstants[ShaderIdx] = GetWGPUConstantEntries(Stage.SpecConstEntries);
520536
wgpuRenderPipelineDesc.vertex.module = wgpuShaderModules[ShaderIdx].Get();
521537
wgpuRenderPipelineDesc.vertex.entryPoint = GetWGPUStringView(Stage.pShader->GetEntryPoint());
522-
wgpuRenderPipelineDesc.vertex.constantCount = Stage.SpecConstEntries.size();
523-
wgpuRenderPipelineDesc.vertex.constants = Stage.SpecConstEntries.empty() ? nullptr : Stage.SpecConstEntries.data();
538+
wgpuRenderPipelineDesc.vertex.constantCount = wgpuStageConstants[ShaderIdx].size();
539+
wgpuRenderPipelineDesc.vertex.constants = wgpuStageConstants[ShaderIdx].empty() ? nullptr : wgpuStageConstants[ShaderIdx].data();
524540
break;
525541

526542
case SHADER_TYPE_PIXEL:
527543
VERIFY(wgpuFragmentState.module == nullptr, "Only one vertex shader is allowed");
544+
wgpuStageConstants[ShaderIdx] = GetWGPUConstantEntries(Stage.SpecConstEntries);
528545
wgpuFragmentState.module = wgpuShaderModules[ShaderIdx].Get();
529546
wgpuFragmentState.entryPoint = GetWGPUStringView(Stage.pShader->GetEntryPoint());
530-
wgpuFragmentState.constantCount = Stage.SpecConstEntries.size();
531-
wgpuFragmentState.constants = Stage.SpecConstEntries.empty() ? nullptr : Stage.SpecConstEntries.data();
547+
wgpuFragmentState.constantCount = wgpuStageConstants[ShaderIdx].size();
548+
wgpuFragmentState.constants = wgpuStageConstants[ShaderIdx].empty() ? nullptr : wgpuStageConstants[ShaderIdx].data();
532549
wgpuRenderPipelineDesc.fragment = &wgpuFragmentState;
533550
break;
534551

@@ -721,9 +738,9 @@ void PipelineStateWebGPUImpl::InitializeWebGPUComputePipeline(const TShaderStage
721738
wgpuComputePipelineDesc.compute.entryPoint = GetWGPUStringView(pShaderWebGPU->GetEntryPoint());
722739
wgpuComputePipelineDesc.layout = m_PipelineLayout.GetWebGPUPipelineLayout();
723740

724-
const std::vector<WGPUConstantEntry>& SpecConstEntries = ShaderStages[0].SpecConstEntries;
725-
wgpuComputePipelineDesc.compute.constantCount = SpecConstEntries.size();
726-
wgpuComputePipelineDesc.compute.constants = SpecConstEntries.empty() ? nullptr : SpecConstEntries.data();
741+
const std::vector<WGPUConstantEntry> wgpuConstants = GetWGPUConstantEntries(ShaderStages[0].SpecConstEntries);
742+
wgpuComputePipelineDesc.compute.constantCount = wgpuConstants.size();
743+
wgpuComputePipelineDesc.compute.constants = wgpuConstants.empty() ? nullptr : wgpuConstants.data();
727744

728745
if (AsyncBuilder)
729746
{

Graphics/ShaderTools/src/WGSLShaderResources.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ SHADER_TYPE TintPipelineStageToShaderType(tint::inspector::PipelineStage Stage)
8686
}
8787
}
8888

89-
WGSLShaderResourceAttribs::ResourceType TintResourceTypeToWGSLShaderAttribsResourceType(tint::inspector::ResourceBinding::ResourceType TintResType)
89+
WGSLShaderResourceAttribs::ResourceType TintResourceTypeToWGSLShaderAttribsResourceType(const tint::inspector::ResourceBinding& TintBinding)
9090
{
9191
using TintResourceType = tint::inspector::ResourceBinding::ResourceType;
92-
switch (TintResType)
92+
using TintSamplerType = tint::inspector::ResourceBinding::SamplerType;
93+
switch (TintBinding.resource_type)
9394
{
9495
case TintResourceType::kUniformBuffer:
9596
return WGSLShaderResourceAttribs::ResourceType::UniformBuffer;
@@ -101,10 +102,9 @@ WGSLShaderResourceAttribs::ResourceType TintResourceTypeToWGSLShaderAttribsResou
101102
return WGSLShaderResourceAttribs::ResourceType::ROStorageBuffer;
102103

103104
case TintResourceType::kSampler:
104-
return WGSLShaderResourceAttribs::ResourceType::Sampler;
105-
106-
case TintResourceType::kComparisonSampler:
107-
return WGSLShaderResourceAttribs::ResourceType::ComparisonSampler;
105+
return TintBinding.sampler_type == TintSamplerType::kComparison ?
106+
WGSLShaderResourceAttribs::ResourceType::ComparisonSampler :
107+
WGSLShaderResourceAttribs::ResourceType::Sampler;
108108

109109
case TintResourceType::kSampledTexture:
110110
return WGSLShaderResourceAttribs::ResourceType::Texture;
@@ -155,6 +155,8 @@ WGSLShaderResourceAttribs::TextureSampleType TintSampleKindToWGSLShaderAttribsSa
155155
switch (TintBinding.sampled_kind)
156156
{
157157
case TintSampledKind::kFloat:
158+
case TintSampledKind::kFilterable:
159+
case TintSampledKind::kUnknownFilterable:
158160
return WGSLShaderResourceAttribs::TextureSampleType::Float;
159161

160162
case TintSampledKind::kSInt:
@@ -163,8 +165,8 @@ WGSLShaderResourceAttribs::TextureSampleType TintSampleKindToWGSLShaderAttribsSa
163165
case TintSampledKind::kUInt:
164166
return WGSLShaderResourceAttribs::TextureSampleType::UInt;
165167

166-
case TintSampledKind::kUnknown:
167-
return WGSLShaderResourceAttribs::TextureSampleType::Unknown;
168+
case TintSampledKind::kUnfilterable:
169+
return WGSLShaderResourceAttribs::TextureSampleType::UnfilterableFloat;
168170

169171
default:
170172
UNEXPECTED("Unexpected sample kind");
@@ -225,7 +227,6 @@ RESOURCE_DIMENSION TintBindingToResourceDimension(const tint::inspector::Resourc
225227
return RESOURCE_DIM_BUFFER;
226228

227229
case TintResourceType::kSampler:
228-
case TintResourceType::kComparisonSampler:
229230
return RESOURCE_DIM_UNDEFINED;
230231

231232
case TintResourceType::kSampledTexture:
@@ -373,7 +374,7 @@ WGSLShaderResourceAttribs::WGSLShaderResourceAttribs(const char*
373374
// clang-format off
374375
Name {_Name},
375376
ArraySize {static_cast<Uint16>(_ArraySize)},
376-
Type {TintResourceTypeToWGSLShaderAttribsResourceType(TintBinding.resource_type)},
377+
Type {TintResourceTypeToWGSLShaderAttribsResourceType(TintBinding)},
377378
ResourceDim {TintBindingToResourceDimension(TintBinding)},
378379
Format {TintTexelFormatToTextureFormat(TintBinding)},
379380
BindGroup {static_cast<Uint16>(TintBinding.bind_group)},
@@ -901,7 +902,6 @@ WGSLShaderResources::WGSLShaderResources(IMemoryAllocator& Allocator,
901902
break;
902903

903904
case TintResourceType::kSampler:
904-
case TintResourceType::kComparisonSampler:
905905
++ResCounters.NumSamplers;
906906
break;
907907

@@ -926,6 +926,11 @@ WGSLShaderResources::WGSLShaderResources(IMemoryAllocator& Allocator,
926926
UNSUPPORTED("Input attachments are not currently supported");
927927
break;
928928

929+
case TintResourceType::kReadOnlyTexelBuffer:
930+
case TintResourceType::kReadWriteTexelBuffer:
931+
UNSUPPORTED("Texel buffers are not currently supported");
932+
break;
933+
929934
default:
930935
UNEXPECTED("Unexpected resource type");
931936
}
@@ -986,7 +991,6 @@ WGSLShaderResources::WGSLShaderResources(IMemoryAllocator& Allocator,
986991
break;
987992

988993
case TintResourceType::kSampler:
989-
case TintResourceType::kComparisonSampler:
990994
{
991995
new (&GetSampler(CurrRes.NumSamplers++)) WGSLShaderResourceAttribs{Name, Binding, ArraySize};
992996
}
@@ -1019,6 +1023,11 @@ WGSLShaderResources::WGSLShaderResources(IMemoryAllocator& Allocator,
10191023
UNSUPPORTED("Input attachments are not currently supported");
10201024
break;
10211025

1026+
case TintResourceType::kReadOnlyTexelBuffer:
1027+
case TintResourceType::kReadWriteTexelBuffer:
1028+
UNSUPPORTED("Texel buffers are not currently supported");
1029+
break;
1030+
10221031
default:
10231032
UNEXPECTED("Unexpected resource type");
10241033
}

ThirdParty/dawn/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ set(DAWN_SUBMODULES
1212
FetchContent_DeclareShallowGit(
1313
dawn
1414
GIT_REPOSITORY https://dawn.googlesource.com/dawn
15-
GIT_TAG 958dff171579e885fcfd02ec86fd6e0f5081a35f
15+
GIT_TAG a1d768eb6b371d557f6f4de330015bd2468237c6
1616
GIT_SUBMODULES "${DAWN_SUBMODULES}"
1717
)
1818

0 commit comments

Comments
 (0)