9#include "graphics/shaders/pbr_gbuffer.frag_wgsl.inc"
10#include "graphics/shaders/pbr_surface.frag_wgsl.inc"
11#include "graphics/shaders/pbr_surface.vert_wgsl.inc"
15std::string_view sourceView(
const std::uint32_t* words, std::size_t
count) {
16 std::string_view
source(
reinterpret_cast<const char*
>(words),
count *
sizeof(std::uint32_t));
21void eraseDeclaration(std::string&
source, std::string_view variable) {
22 const std::string marker =
" var " + std::string(variable) +
" :";
24 if (
position == std::string::npos)
return;
32void eraseKeepalive(std::string&
source, std::string_view variable) {
33 const std::string marker =
"_ = " + std::string(variable) +
";";
44void replaceAll(std::string&
source, std::string_view
from, std::string_view
to) {
52std::string samplerName(std::uint32_t resource) {
54 if (resource < canonicalCount)
return "map" + std::to_string(resource) +
"_sampler";
55 return "detailMap" + std::to_string(resource - canonicalCount) +
"_sampler";
58void coalesceSampler(std::string&
source, std::string_view resource, std::uint32_t resourceIndex,
59 std::uint32_t representative) {
61 const std::string
sampler = std::string(resource) +
"_sampler";
67void replaceResourceCalls(std::string&
source, std::string_view
function, std::string_view resource,
68 std::string_view replacement) {
69 const std::string prefix = std::string(
function) +
"(" + std::string(resource);
70 std::size_t
begin = 0;
72 const auto delimiter =
begin + prefix.size();
73 if (delimiter >=
source.size() || (
source[delimiter] !=
',' &&
source[delimiter] !=
')')) {
77 std::size_t
depth = 0;
87 begin += replacement.size();
91void removeSampledResource(std::string&
source, std::string_view base,
92 std::string_view replacement =
"vec4<f32>(1.0f)") {
93 const std::string
image = std::string(base) +
"_image";
94 const std::string
sampler = std::string(base) +
"_sampler";
95 replaceResourceCalls(
source,
"textureSampleLevel", image, replacement);
96 replaceResourceCalls(
source,
"textureSampleCompare", image,
"1.0f");
97 replaceResourceCalls(
source,
"textureSample", image, replacement);
98 eraseDeclaration(
source, image);
100 eraseKeepalive(
source, image);
106 return {sourceView(pbr_surface_vert_wgsl, pbr_surface_vert_wgsl_count),
107 sourceView(pbr_surface_frag_wgsl, pbr_surface_frag_wgsl_count)};
112 if (gbuffer) base.fragment = sourceView(pbr_gbuffer_frag_wgsl, pbr_gbuffer_frag_wgsl_count);
114 static constexpr std::array<std::string_view, 11> maps{
115 "map0",
"map1",
"map2",
"map3",
"map4",
"map5",
"map6",
"map7",
"map8",
"map9",
"map10"};
116 static constexpr std::array<std::string_view, 3> detailMaps{
"detailMap0",
"detailMap1",
"detailMap2"};
118 if ((plan.canonicalTextureMask & (1u <<
index)) == 0
u)
119 removeSampledResource(
result.fragment, maps[
index]);
121 if ((plan.detailTextureMask & (1u <<
index)) == 0
u)
122 removeSampledResource(
result.fragment, detailMaps[
index]);
124 if ((plan.canonicalTextureMask & (1u <<
index)) != 0
u)
126 plan.canonicalSamplerRepresentatives[
index]);
128 if ((plan.detailTextureMask & (1u <<
index)) != 0
u)
129 coalesceSampler(
result.fragment, detailMaps[
index],
131 plan.detailSamplerRepresentatives[
index]);
132 if ((plan.fragmentFlags &
PbrExtras) == 0
u) removeSampledResource(
result.fragment,
"extrasMap");
133 if ((plan.fragmentFlags &
PbrColors) == 0
u) removeSampledResource(
result.fragment,
"colorsMap");
135 removeSampledResource(
result.fragment,
"vegetationFadeNoise",
136 "vec4<f32>(0.0f, 0.0f, 0.0f, 1.0f)");
138 replaceResourceCalls(
result.fragment,
"textureNumLevels",
"environmentMap_image",
"1u");
139 removeSampledResource(
result.fragment,
"environmentMap");
142 replaceResourceCalls(
result.fragment,
"textureDimensions",
"shadowMap_image",
"vec2<u32>(1u)");
143 replaceResourceCalls(
result.fragment,
"textureNumLayers",
"shadowMap_image",
"1u");
144 removeSampledResource(
result.fragment,
"shadowMap",
"1.0f");
157 if (!device || !instance)
160 device.PushErrorScope(wgpu::ErrorFilter::Validation);
161 auto makeModule = [&](std::string_view
source) {
162 wgpu::ShaderSourceWGSL wgsl{};
163 wgsl.code =
source.data();
166 return device.CreateShaderModule(&
descriptor);
168 auto vertex = makeModule(
sources.vertex);
169 auto fragment = makeModule(
sources.fragment);
172 wgpu::ErrorType
error = wgpu::ErrorType::Unknown;
174 device.PopErrorScope(
175 wgpu::CallbackMode::AllowProcessEvents,
176 [&](wgpu::PopErrorScopeStatus
status, wgpu::ErrorType
type, wgpu::StringView
text) {
177 error =
status == wgpu::PopErrorScopeStatus::Success ?
type : wgpu::ErrorType::Unknown;
181 while (!
done) wgpuInstanceProcessEvents(instance.Get());
182 if (!vertex || !fragment ||
error != wgpu::ErrorType::NoError)
185 message.empty() ?
"WebGPU rejected generated PBR WGSL" :
message));
191 if (!modules)
return modules;
193 device.PushErrorScope(wgpu::ErrorFilter::Validation);
194 auto makeModule = [&](std::string_view
source) {
195 wgpu::ShaderSourceWGSL wgsl{};
196 wgsl.code =
source.data();
197 wgpu::ShaderModuleDescriptor moduleDescriptor{};
198 moduleDescriptor.nextInChain = &wgsl;
199 return device.CreateShaderModule(&moduleDescriptor);
201 auto vertex = makeModule(
sources.vertex);
202 auto fragment = makeModule(
sources.fragment);
204 std::array<wgpu::VertexAttribute, 16>
attributes{};
206 std::uint32_t location) {
211 attribute(0, wgpu::VertexFormat::Float32x3, 0, 0);
212 attribute(1, wgpu::VertexFormat::Float32x3, 12, 1);
213 attribute(2, wgpu::VertexFormat::Float32x2, 24, 2);
214 attribute(3, wgpu::VertexFormat::Uint16x4, 32, 3);
215 attribute(4, wgpu::VertexFormat::Float32x4, 40, 4);
218 std::array<wgpu::VertexBufferLayout, 2> buffers{};
219 buffers[0].arrayStride = 56;
220 buffers[0].attributeCount = 5;
222 buffers[1].arrayStride = 88;
223 buffers[1].attributeCount = 11;
224 buffers[1].attributes =
attributes.data() + 5;
226 wgpu::ColorTargetState
target{};
227 target.format = wgpu::TextureFormat::RGBA8Unorm;
228 wgpu::FragmentState fragmentState{};
229 fragmentState.module = fragment;
230 fragmentState.entryPoint =
"main";
231 fragmentState.targetCount = 1;
232 fragmentState.targets = &
target;
236 descriptor.vertex.bufferCount = buffers.size();
239 descriptor.primitive.topology = wgpu::PrimitiveTopology::TriangleList;
241 auto pipeline = device.CreateRenderPipeline(&
descriptor);
244 wgpu::ErrorType
error = wgpu::ErrorType::Unknown;
246 device.PopErrorScope(
247 wgpu::CallbackMode::AllowProcessEvents,
248 [&](wgpu::PopErrorScopeStatus
status, wgpu::ErrorType
type, wgpu::StringView
text) {
249 error =
status == wgpu::PopErrorScopeStatus::Success ?
type : wgpu::ErrorType::Unknown;
253 while (!
done) wgpuInstanceProcessEvents(instance.Get());
254 if (!pipeline ||
error != wgpu::ErrorType::NoError)
vk::UniqueSampler sampler
wgpu::PopErrorScopeStatus status
std::array< float, 3 > position
std::vector< std::pair< std::uint64_t, OceanBoatReceipt > > sources
std::map< std::string, double > attributes
MigrationFunction function
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
Result< void > debugValidatePbrVariantBaseSources()
Compile the generated full PBR WGSL stages under a validation scope.
Result< void > debugValidatePbrVariantSources(const PbrVariantSources &sources)
Compile caller-provided PBR WGSL stages under a validation scope.
Result< void > debugValidatePbrVariantPipeline(const PbrVariantSources &sources)
Compile and link a specialized PBR pair through a real Dawn render pipeline.
PbrSpecializedSources specializePbrVariantSources(const PbrVariantPlan &plan, bool gbuffer)
Specialize the generated WGSL for the resource reachability encoded by a checked plan.
PbrVariantSources pbrVariantBaseSources()
Return immutable full PBR WGSL generated from the canonical GLSL implementation.
Owning WGSL stages after unreachable texture operations and declarations are removed.
Stable resource-layout key and checked stage counts for a PBR snapshot.
static constexpr std::uint32_t NoSampler
Reproducibly generated full PBR WGSL sources before resource specialization.