载入中...
搜索中...
未找到
GraphicsPipelines.cpp
浏览该文件的文档.
3#include "graphics/Batcher.h"
6#include "graphics/GBuffer.h"
7#include "graphics/Light.h"
8#include "graphics/Mesh.h"
13#include "graphics/Shader.h"
14#include "graphics/Shadow.h"
15#include "graphics/Texture.h"
25
26#include "common/Exception.h"
27#include "common/Resource.h"
28#include "common/config.h"
29#include "filesystem/FileData.h"
31#include "image/Image.h"
32#include "image/ImageData.h"
33
34#include <algorithm>
35#include <bit>
36#include <cmath>
37#include <cstdio>
38#include <cstring>
39#include <fstream>
40#include <limits>
41#include <map>
42#include <memory>
43#include <mutex>
44#include <vector>
45
46#include <glm/gtc/constants.hpp>
47
48namespace eve::graphics::webgpu {
49using namespace detail;
50
51void Graphics::createPipelineResources() {
52 // Only layouts are required during initialization. Native Dawn compiles
53 // render pipelines synchronously, so eagerly materializing every blend,
54 // depth, cull, target and MSAA variant made each isolated CTest spend
55 // roughly a minute compiling pipelines it never used.
56 create2DPipelines();
57 createMesh3DPipelines();
58}
59
60// ---------------------------------------------------------------------------
61// Bind group layouts
62// ---------------------------------------------------------------------------
63
64wgpu::BindGroupLayout Graphics::make2DBindGroupLayout() {
65 BindGroupLayoutBuilder b;
66 // 0: color texture
67 b.texture(0, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
68 // 1: depth texture
69 b.texture(1, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
70 // 2: color sampler
71 b.sampler(2, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
72 // 3: depth sampler
73 b.sampler(3, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
74 // 4: Externals UBO (push-constant replacement, dynamic offset). Sized for
75 // the largest consumer: custom 2D shaders (128 B) and lit2D (Lighting2DUBO).
76 b.buffer(4, wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, true,
77 std::max<uint32_t>(Shader::kPushConstantBytes, uint32_t(sizeof(Lighting2DUBO))));
78 b.texture(5, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
79 b.sampler(6, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
80 b.texture(7, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
81 b.sampler(8, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
82 b.texture(9, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
83 b.sampler(10, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
84 return b.build(device, "eve_2d");
85}
86
87wgpu::BindGroupLayout Graphics::makeMesh3DBindGroupLayout() {
88 BindGroupLayoutBuilder b;
89 // 0: Frame UBO (dynamic)
90 b.buffer(0, wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, true,
91 sizeof(Mesh3DUBO));
92 // 1: albedo
93 b.texture(1, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
94 // 2: normal
95 b.texture(2, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
96 // 3: env cubemap
97 b.texture(3, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::Cube);
98 // 4: Shadow UBO (dynamic)
99 b.buffer(4, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, true, sizeof(ShadowUBO));
100 // 5: shadow depth array
101 b.texture(5, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Depth, wgpu::TextureViewDimension::e2DArray);
102 // 6: height
103 b.texture(6, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
104 // 7: shared filtering sampler (WGSL `mainSamp`)
105 b.sampler(7, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
106 // 8: shadow comparison sampler
107 b.sampler(8, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Comparison);
108 // 9: scene depth (G-buffer hwDepth; X-ray shaders sample it). Depth view,
109 // sampled via textureSampleLevel with the shared mainSamp (binding 7).
110 b.texture(9, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Depth, wgpu::TextureViewDimension::e2D);
111 // 10: SSAO occlusion texture (white when AO is disabled)
112 b.texture(10, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
113 // 11: shared AO sampler
114 b.sampler(11, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
115 for (uint32_t i = 12; i <= 14; ++i) {
116 b.texture(i, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
117 }
118 // Custom mesh shaders may use their external parameters for vertex
119 // deformation as well as fragment shading (for example grass billboards).
120 b.buffer(15, wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, true,
122 b.texture(16, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::Cube);
123 b.texture(17, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::Cube);
124 b.texture(18, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
125 b.sampler(19, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
126 b.buffer(21, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::ReadOnlyStorage, false, 64);
127 return b.build(device, "eve_mesh3d");
128}
129
130wgpu::BindGroupLayout Graphics::makeShadowBindGroupLayout() {
131 BindGroupLayoutBuilder b;
132 b.buffer(0, wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, true,
133 sizeof(SkinPassUBO));
134 b.texture(1, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
135 b.sampler(2, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
136 b.buffer(3, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::ReadOnlyStorage, false, 64);
137 return b.build(device, "eve_shadow");
138}
139
140wgpu::BindGroupLayout Graphics::makeGbufferBindGroupLayout() {
141 BindGroupLayoutBuilder b;
142 // 0: pass and skinning UBO (dynamic)
143 b.buffer(0, wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, true,
144 sizeof(SkinPassUBO));
145 // 1: albedo
146 b.texture(1, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
147 // 2: sampler
148 b.sampler(2, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
149 b.buffer(3, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::ReadOnlyStorage, false, 64);
150 return b.build(device, "eve_gbuffer");
151}
152
153wgpu::BindGroupLayout Graphics::makeDecalBindGroupLayout() {
154 BindGroupLayoutBuilder b;
155 b.buffer(0, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, true, 256);
156 for (uint32_t i = 1; i <= 3; ++i) {
157 b.texture(i, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
158 }
159 b.texture(4, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Depth, wgpu::TextureViewDimension::e2D);
160 b.texture(5, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
161 b.sampler(6, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
162 return b.build(device, "eve_decal");
163}
164
165wgpu::BindGroupLayout Graphics::makeVoxelBindGroupLayout() {
166 BindGroupLayoutBuilder b;
167 // 0: PC UBO (dynamic)
168 b.buffer(0, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::Uniform, true, 112);
169 // 1: atlas texture
170 b.texture(1, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
171 // 2: atlas sampler
172 b.sampler(2, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
173 return b.build(device, "eve_voxel");
174}
175
176wgpu::PipelineLayout Graphics::make2DPipelineLayout() {
177 wgpu::PipelineLayoutDescriptor d{};
178 d.label = "eve_2d_layout";
179 d.bindGroupLayoutCount = 1;
180 d.bindGroupLayouts = &tex2DSetLayout;
181 return device.CreatePipelineLayout(&d);
182}
183
184wgpu::PipelineLayout Graphics::makeMesh3DPipelineLayout() {
185 wgpu::PipelineLayoutDescriptor d{};
186 d.label = "eve_mesh3d_layout";
187 d.bindGroupLayoutCount = 1;
188 d.bindGroupLayouts = &mesh3dSetLayout;
189 return device.CreatePipelineLayout(&d);
190}
191
192wgpu::BindGroupLayout Graphics::makeMesh3DClusteredBindGroupLayout() {
193 BindGroupLayoutBuilder b;
194 // 0: Frame UBO (dynamic; clustered layout)
195 b.buffer(0, wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, true,
196 sizeof(Mesh3DClusteredUBO));
197 // 1: albedo
198 b.texture(1, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
199 // 2: normal map
200 b.texture(2, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
201 // 3: env cubemap
202 b.texture(3, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::Cube);
203 // 4: shadow UBO (dynamic)
204 b.buffer(4, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, true, sizeof(ShadowUBO));
205 // 5: shadow depth array
206 b.texture(5, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Depth, wgpu::TextureViewDimension::e2DArray);
207 // 6: height/parallax (unused fallback)
208 b.texture(6, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
209 // 7: shared filtering sampler
210 b.sampler(7, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
211 // 8: shadow comparison sampler
212 b.sampler(8, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Comparison);
213 // 9: scene depth (G-buffer hwDepth; sampled by X-ray variants)
214 b.texture(9, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Depth, wgpu::TextureViewDimension::e2D);
215 // 10..12: clustered-forward SSBOs
216 b.buffer(10, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::ReadOnlyStorage, false,
217 sizeof(ClusteredLightGpu));
218 b.buffer(11, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::ReadOnlyStorage, false,
219 sizeof(ClusterTableEntry));
220 b.buffer(12, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::ReadOnlyStorage, false, sizeof(uint32_t));
221 // 13/14: SSAO occlusion texture + sampler
222 b.texture(13, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
223 b.sampler(14, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
224 for (uint32_t i = 15; i <= 17; ++i) {
225 b.texture(i, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::e2D);
226 }
227
228 b.texture(18, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::Cube);
229 b.texture(19, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float, wgpu::TextureViewDimension::Cube);
230 return b.build(device, "eve_mesh3d_clustered");
231}
232
233wgpu::PipelineLayout Graphics::makeMesh3DClusteredPipelineLayout() {
234 wgpu::PipelineLayoutDescriptor d{};
235 d.label = "eve_mesh3d_clustered_layout";
236 d.bindGroupLayoutCount = 1;
237 d.bindGroupLayouts = &mesh3dClusteredSetLayout;
238 return device.CreatePipelineLayout(&d);
239}
240
241wgpu::PipelineLayout Graphics::makeShadowPipelineLayout() {
242 wgpu::PipelineLayoutDescriptor d{};
243 d.label = "eve_shadow_layout";
244 d.bindGroupLayoutCount = 1;
245 d.bindGroupLayouts = &shadowSetLayout;
246 return device.CreatePipelineLayout(&d);
247}
248
249wgpu::PipelineLayout Graphics::makeGbufferPipelineLayout() {
250 wgpu::PipelineLayoutDescriptor d{};
251 d.label = "eve_gbuffer_layout";
252 d.bindGroupLayoutCount = 1;
253 d.bindGroupLayouts = &gbufferSetLayout;
254 return device.CreatePipelineLayout(&d);
255}
256
257wgpu::PipelineLayout Graphics::makeDecalPipelineLayout() {
258 wgpu::PipelineLayoutDescriptor d{};
259 d.label = "eve_decal_layout";
260 d.bindGroupLayoutCount = 1;
261 d.bindGroupLayouts = &decalSetLayout;
262 return device.CreatePipelineLayout(&d);
263}
264
265wgpu::PipelineLayout Graphics::makeVoxelPipelineLayout() {
266 wgpu::PipelineLayoutDescriptor d{};
267 d.label = "eve_voxel_layout";
268 d.bindGroupLayoutCount = 1;
269 d.bindGroupLayouts = &voxelSetLayout;
270 return device.CreatePipelineLayout(&d);
271}
272
273// ---------------------------------------------------------------------------
274// Pipelines
275// ---------------------------------------------------------------------------
276
277namespace {
278
279wgpu::RenderPipeline make2DColorPipeline(wgpu::Device& dev, WGPUTextureFormat format, BlendMode mode) {
280 wgpu::VertexAttribute attrs[2] = {};
281 attrs[0].format = wgpu::VertexFormat::Float32x2;
282 attrs[0].offset = 0;
283 attrs[0].shaderLocation = 0;
284 attrs[1].format = wgpu::VertexFormat::Float32x4;
285 attrs[1].offset = 8;
286 attrs[1].shaderLocation = 1;
287
288 PipelineBuilder b;
289 // The solid-color shader declares no bindings, so it must use an empty
290 // (auto-derived) pipeline layout. Reusing the textured 2D layout here would
291 // require a bind group for every solid draw.
292 b.vertexLayout(24, wgpu::VertexStepMode::Vertex, attrs, 2);
293 b.shader(makeWgslModule(dev, kColorVertWgsl), "vs_main", makeWgslModule(dev, kColorFragWgsl), "fs_main");
294 b.colorTarget(format, mode);
295 // The WGSL vertex shader mirrors clip Y to match Vulkan, which flips
296 // object-space CCW winding in framebuffer space.
297 b.frontFace(wgpu::FrontFace::CW);
298 b.cull(wgpu::CullMode::Back);
299 return b.build(dev);
300}
301
302wgpu::RenderPipeline make2DTexturedPipeline(wgpu::Device& dev, wgpu::PipelineLayout layout, WGPUTextureFormat format,
303 BlendMode mode, bool toneMapScene = false) {
304 WGPUVertexAttribute attrs[3] = {};
305 attrs[0].format = WGPUVertexFormat_Float32x2;
306 attrs[0].offset = 0;
307 attrs[0].shaderLocation = 0;
308 attrs[1].format = WGPUVertexFormat_Float32x4;
309 attrs[1].offset = 8;
310 attrs[1].shaderLocation = 1;
311 attrs[2].format = WGPUVertexFormat_Float32x2;
312 attrs[2].offset = 24;
313 attrs[2].shaderLocation = 2;
314 WGPUVertexBufferLayout vb{};
315 fillVertexLayout(vb, 32, attrs, 3);
316
317 WGPUColorTargetState target{};
318 target.format = format;
319 target.writeMask = WGPUColorWriteMask_All;
320 WGPUBlendState bs = alphaBlend();
321 if (mode == BlendMode::Additive)
322 bs = additiveBlend();
323 else if (mode == BlendMode::Premultiplied)
324 bs = premultipliedBlend();
325 else if (mode == BlendMode::Multiply)
326 bs = multiplyBlend();
327 else if (mode == BlendMode::Opaque)
328 bs = noBlend();
329 if (mode != BlendMode::Opaque) target.blend = &bs;
330
331 WGPURenderPipelineDescriptor pd{};
332 pd.label = sv("eve_textured2d");
333 pd.layout = layout.Get();
334 wgpu::ShaderModule vertModule = makeWgslModule(dev, kTexturedVertWgsl);
335 static constexpr const char* kSceneTonemapFragWgsl = R"wgsl(
336struct FSIn {
337 @location(0) color: vec4f,
338 @location(1) uv: vec2f,
339};
340@group(0) @binding(0) var colorTex: texture_2d<f32>;
341@group(0) @binding(2) var colorSampler: sampler;
342@group(0) @binding(1) var rawSceneTex: texture_2d<f32>;
343@group(0) @binding(3) var rawSceneSampler: sampler;
344fn acesFitted(c0: vec3f) -> vec3f {
345 let c = max(c0, vec3f(0.0));
346 return clamp((c * (2.51 * c + vec3f(0.03))) /
347 (c * (2.43 * c + vec3f(0.59)) + vec3f(0.14)),
348 vec3f(0.0), vec3f(1.0));
349}
350fn linearToSrgb(c: vec3f) -> vec3f {
351 let low = c * 12.92;
352 let high = 1.055 * pow(max(c, vec3f(0.0)), vec3f(1.0 / 2.4)) - vec3f(0.055);
353 return select(low, high, c > vec3f(0.0031308));
354}
355fn automaticExposure(packedLimits: f32, raw: bool) -> f32 {
356 var logLuminance: array<f32, 16>;
357 var sampleIndex = 0;
358 for (var y = 0; y < 4; y = y + 1) {
359 for (var x = 0; x < 4; x = x + 1) {
360 let uv = (vec2f(f32(x), f32(y)) + vec2f(0.5)) * 0.25;
361 var sampleColor = textureSampleLevel(colorTex, colorSampler, uv, 0.0).rgb;
362 if (raw) {
363 sampleColor = textureSampleLevel(rawSceneTex, rawSceneSampler, uv, 0.0).rgb;
364 }
365 sampleColor = max(sampleColor, vec3f(0.0));
366 let luminance = dot(sampleColor, vec3f(0.2126, 0.7152, 0.0722));
367 logLuminance[sampleIndex] = log2(clamp(luminance, 0.0001, 65504.0));
368 sampleIndex += 1;
369 }
370 }
371 for (var i = 1; i < 16; i = i + 1) {
372 let value = logLuminance[i];
373 var j = i - 1;
374 while (j >= 0 && logLuminance[j] > value) {
375 logLuminance[j + 1] = logLuminance[j];
376 j -= 1;
377 }
378 logLuminance[j + 1] = value;
379 }
380 var trimmedLogSum = 0.0;
381 for (var i = 2; i < 14; i = i + 1) {
382 trimmedLogSum += logLuminance[i];
383 }
384 let geometricMean = exp2(trimmedLogSum * (1.0 / 12.0));
385 let packed = round(packedLimits);
386 let minEV = (packed - floor(packed * (1.0 / 256.0)) * 256.0) *
387 (1.0 / 8.0) - 16.0;
388 let maxEV = floor(packed * (1.0 / 256.0)) * (1.0 / 8.0) - 16.0;
389 return clamp(0.18 / max(geometricMean, 0.0001), exp2(minEV), exp2(maxEV));
390}
391fn bloomPrefilter(color: vec3f, threshold: f32) -> vec3f {
392 let brightness = max(color.r, max(color.g, color.b));
393 let knee = max(threshold * 0.1, 0.01);
394 let soft = clamp((brightness - threshold + knee) / (2.0 * knee), 0.0, 1.0);
395 let contribution = max(brightness - threshold, soft * soft * knee);
396 return color * (contribution / max(brightness, 0.0001));
397}
398fn sampleBloom(uv: vec2f, threshold: f32) -> vec3f {
399 let texel = 1.0 / vec2f(textureDimensions(colorTex));
400 var bloom = bloomPrefilter(textureSampleLevel(colorTex, colorSampler, uv, 0.0).rgb,
401 threshold) * 0.2;
402 let offsets = array<vec2f, 12>(
403 vec2f(2.00, 0.00), vec2f(-1.47, 1.35), vec2f(0.17, -1.99), vec2f(1.22, 1.58),
404 vec2f(4.22, -2.68), vec2f(-4.92, -0.87), vec2f(3.04, 3.97), vec2f(-0.44, -4.98),
405 vec2f(-5.28, 7.28), vec2f(8.89, -1.41), vec2f(-7.82, -4.46), vec2f(2.07, 8.76));
406 let weights = array<f32, 12>(
407 0.10, 0.10, 0.10, 0.10,
408 0.06, 0.06, 0.06, 0.06,
409 0.04, 0.04, 0.04, 0.04);
410 for (var q = 0; q < 12; q = q + 1) {
411 bloom += bloomPrefilter(textureSampleLevel(colorTex, colorSampler,
412 uv + offsets[q] * texel, 0.0).rgb, threshold) * weights[q];
413 }
414 return bloom;
415}
416@fragment
417fn fs_main(in: FSIn) -> @location(0) vec4f {
418 let hdr = textureSample(colorTex, colorSampler, in.uv);
419 let exposure = 1.0; // Applied once by the dedicated HDR pre-exposure pass.
420 let encodeSrgb = in.color.a >= 65536.0;
421 let bloomPacked = round(in.color.a) - floor(round(in.color.a) * (1.0 / 65536.0)) * 65536.0;
422 let bloomIntensity = (bloomPacked - floor(bloomPacked * (1.0 / 256.0)) * 256.0) *
423 (1.0 / 32.0);
424 let bloomThreshold = floor(bloomPacked * (1.0 / 256.0)) * (1.0 / 16.0);
425 var bloom = vec3f(0.0);
426 if (bloomIntensity > 0.0) {
427 bloom = sampleBloom(in.uv, bloomThreshold);
428 }
429 var displayColor = acesFitted((hdr.rgb + bloom * bloomIntensity) * exposure);
430 if (encodeSrgb) {
431 displayColor = linearToSrgb(displayColor);
432 }
433 return vec4f(displayColor, hdr.a);
434}
435)wgsl";
436 wgpu::ShaderModule fragModule = makeWgslModule(dev, toneMapScene ? kSceneTonemapFragWgsl : kTexturedFragWgsl);
437 pd.vertex.module = vertModule.Get();
438 pd.vertex.entryPoint = sv("vs_main");
439 pd.vertex.bufferCount = 1;
440 pd.vertex.buffers = &vb;
441 WGPUFragmentState fs{};
442 fs.module = fragModule.Get();
443 fs.entryPoint = sv("fs_main");
444 fs.targetCount = 1;
445 fs.targets = &target;
446 pd.fragment = &fs;
447 pd.primitive.topology = WGPUPrimitiveTopology_TriangleList;
448 pd.primitive.frontFace = WGPUFrontFace_CCW;
449 pd.primitive.cullMode = WGPUCullMode_None;
450 pd.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
451 pd.depthStencil = nullptr;
452 pd.multisample.count = 1;
453 // Zero-init would leave mask=0, which discards every fragment
454 // (sampleMask=0). The WebGPU default is 0xFFFFFFFF (all samples).
455 pd.multisample.mask = 0xFFFFFFFFu;
456 return dev.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
457}
458
459wgpu::RenderPipeline make2DLitPipeline(wgpu::Device& dev, wgpu::PipelineLayout layout, WGPUTextureFormat format,
461 wgpu::VertexAttribute attrs[3] = {};
462 attrs[0].format = wgpu::VertexFormat::Float32x2;
463 attrs[0].offset = 0;
464 attrs[0].shaderLocation = 0;
465 attrs[1].format = wgpu::VertexFormat::Float32x4;
466 attrs[1].offset = 8;
467 attrs[1].shaderLocation = 1;
468 attrs[2].format = wgpu::VertexFormat::Float32x2;
469 attrs[2].offset = 24;
470 attrs[2].shaderLocation = 2;
471
472 PipelineBuilder b;
473 b.vertexLayout(32, wgpu::VertexStepMode::Vertex, attrs, 3);
474 b.shader(makeWgslModule(dev, kLit2DVertWgsl), "vs_main", makeWgslModule(dev, kLit2DFragWgsl), "fs_main");
475 b.layout(layout);
476 b.colorTarget(format, blend);
477 return b.build(dev);
478}
479
480} // namespace
481
482void Graphics::create2DPipelines() {
483 tex2DSetLayout = make2DBindGroupLayout();
484 tex2DPipelineLayout = make2DPipelineLayout();
485
486 colorPipeline = make2DColorPipeline(device, surfaceFormat, BlendMode::Alpha);
487 texturedPipeline = make2DTexturedPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Alpha);
488 colorAdditivePipeline = make2DColorPipeline(device, surfaceFormat, BlendMode::Additive);
489 texturedAdditivePipeline = make2DTexturedPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Additive);
490 colorPremultipliedPipeline = make2DColorPipeline(device, surfaceFormat, BlendMode::Premultiplied);
491 texturedPremultipliedPipeline =
492 make2DTexturedPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Premultiplied);
493 colorMultiplyPipeline = make2DColorPipeline(device, surfaceFormat, BlendMode::Multiply);
494 texturedMultiplyPipeline = make2DTexturedPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Multiply);
495 colorOpaquePipeline = make2DColorPipeline(device, surfaceFormat, BlendMode::Opaque);
496 texturedOpaquePipeline = make2DTexturedPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Opaque);
497 sceneTonemapPipeline = make2DTexturedPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Opaque, true);
498 lit2dPipeline = make2DLitPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Alpha);
499 lit2dAdditivePipeline = make2DLitPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Additive);
500 lit2dPremultipliedPipeline =
501 make2DLitPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Premultiplied);
502 lit2dMultiplyPipeline = make2DLitPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Multiply);
503 lit2dOpaquePipeline = make2DLitPipeline(device, tex2DPipelineLayout, surfaceFormat, BlendMode::Opaque);
504
505 offscreenColorPipeline = make2DColorPipeline(device, WGPUTextureFormat_RGBA8Unorm, BlendMode::Alpha);
506 offscreenTexturedPipeline =
507 make2DTexturedPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA8Unorm, BlendMode::Alpha);
508 offscreenColorAdditivePipeline = make2DColorPipeline(device, WGPUTextureFormat_RGBA8Unorm, BlendMode::Additive);
509 offscreenTexturedAdditivePipeline =
510 make2DTexturedPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA8Unorm, BlendMode::Additive);
511 offscreenColorPremultipliedPipeline =
512 make2DColorPipeline(device, WGPUTextureFormat_RGBA8Unorm, BlendMode::Premultiplied);
513 offscreenTexturedPremultipliedPipeline =
514 make2DTexturedPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA8Unorm, BlendMode::Premultiplied);
515 offscreenColorMultiplyPipeline = make2DColorPipeline(device, WGPUTextureFormat_RGBA8Unorm, BlendMode::Multiply);
516 offscreenTexturedMultiplyPipeline =
517 make2DTexturedPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA8Unorm, BlendMode::Multiply);
518 offscreenColorOpaquePipeline = make2DColorPipeline(device, WGPUTextureFormat_RGBA8Unorm, BlendMode::Opaque);
519 offscreenTexturedOpaquePipeline =
520 make2DTexturedPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA8Unorm, BlendMode::Opaque);
521 hdrOffscreenTexturedPipeline =
522 make2DTexturedPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA16Float, BlendMode::Alpha);
523 hdrOffscreenTexturedOpaquePipeline =
524 make2DTexturedPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA16Float, BlendMode::Opaque);
525 offscreenLitPipeline =
526 make2DLitPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA8Unorm, BlendMode::Alpha);
527 offscreenLitAdditivePipeline =
528 make2DLitPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA8Unorm, BlendMode::Additive);
529 offscreenLitPremultipliedPipeline =
530 make2DLitPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA8Unorm, BlendMode::Premultiplied);
531 offscreenLitMultiplyPipeline =
532 make2DLitPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA8Unorm, BlendMode::Multiply);
533 offscreenLitOpaquePipeline =
534 make2DLitPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA8Unorm, BlendMode::Opaque);
535 hdrOffscreenLitPipeline =
536 make2DLitPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA16Float, BlendMode::Alpha);
537 hdrOffscreenLitAdditivePipeline =
538 make2DLitPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA16Float, BlendMode::Additive);
539 hdrOffscreenLitPremultipliedPipeline =
540 make2DLitPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA16Float, BlendMode::Premultiplied);
541 hdrOffscreenLitMultiplyPipeline =
542 make2DLitPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA16Float, BlendMode::Multiply);
543 hdrOffscreenLitOpaquePipeline =
544 make2DLitPipeline(device, tex2DPipelineLayout, WGPUTextureFormat_RGBA16Float, BlendMode::Opaque);
545}
546
547wgpu::RenderPipeline Graphics::get2DColorPipeline(BlendMode blend, bool offscreen) {
548 switch (blend) {
549 case BlendMode::Additive: return offscreen ? offscreenColorAdditivePipeline : colorAdditivePipeline;
551 return offscreen ? offscreenColorPremultipliedPipeline : colorPremultipliedPipeline;
552 case BlendMode::Multiply: return offscreen ? offscreenColorMultiplyPipeline : colorMultiplyPipeline;
553 case BlendMode::Opaque: return offscreen ? offscreenColorOpaquePipeline : colorOpaquePipeline;
554 case BlendMode::Alpha:
555 default: return offscreen ? offscreenColorPipeline : colorPipeline;
556 }
557}
558
559wgpu::RenderPipeline Graphics::get2DTexturedPipeline(BlendMode blend, bool offscreen) {
560 switch (blend) {
561 case BlendMode::Additive: return offscreen ? offscreenTexturedAdditivePipeline : texturedAdditivePipeline;
563 return offscreen ? offscreenTexturedPremultipliedPipeline : texturedPremultipliedPipeline;
564 case BlendMode::Multiply: return offscreen ? offscreenTexturedMultiplyPipeline : texturedMultiplyPipeline;
565 case BlendMode::Opaque: return offscreen ? offscreenTexturedOpaquePipeline : texturedOpaquePipeline;
566 case BlendMode::Alpha:
567 default: return offscreen ? offscreenTexturedPipeline : texturedPipeline;
568 }
569}
570
571wgpu::RenderPipeline Graphics::get2DLitPipeline(BlendMode blend, WGPUTextureFormat format) {
572 const bool hdr = format == WGPUTextureFormat_RGBA16Float;
573 const bool offscreen = hdr || uint32_t(format) != uint32_t(surfaceFormat);
574 auto pick = [](BlendMode mode, wgpu::RenderPipeline alpha, wgpu::RenderPipeline additive,
575 wgpu::RenderPipeline premultiplied, wgpu::RenderPipeline multiply,
576 wgpu::RenderPipeline opaque) -> wgpu::RenderPipeline {
577 switch (mode) {
578 case BlendMode::Additive: return additive;
579 case BlendMode::Premultiplied: return premultiplied;
580 case BlendMode::Multiply: return multiply;
581 case BlendMode::Opaque: return opaque;
582 case BlendMode::Alpha:
583 default: return alpha;
584 }
585 };
586 if (hdr) {
587 return pick(blend, hdrOffscreenLitPipeline, hdrOffscreenLitAdditivePipeline,
588 hdrOffscreenLitPremultipliedPipeline, hdrOffscreenLitMultiplyPipeline,
589 hdrOffscreenLitOpaquePipeline);
590 }
591 if (offscreen) {
592 return pick(blend, offscreenLitPipeline, offscreenLitAdditivePipeline, offscreenLitPremultipliedPipeline,
593 offscreenLitMultiplyPipeline, offscreenLitOpaquePipeline);
594 }
595 return pick(blend, lit2dPipeline, lit2dAdditivePipeline, lit2dPremultipliedPipeline, lit2dMultiplyPipeline,
596 lit2dOpaquePipeline);
597}
598
599void Graphics::createMesh3DPipelines() {
600 mesh3dSetLayout = makeMesh3DBindGroupLayout();
601 // Bind groups reference the layout; drop cached groups when it changes.
602 clearMeshBindGroupCache();
603 mesh3dPipelineLayout = makeMesh3DPipelineLayout();
604}
605
606wgpu::RenderPipeline Graphics::getMesh3DPipeline(BlendMode blend, bool depthWrite, bool doubleSided,
607 bool canvasTarget) {
608 const size_t index = meshPipelineIndex(blend, depthWrite, doubleSided);
609 wgpu::RenderPipeline& pipeline = canvasTarget ? mesh3dCanvasPipelines[index] : mesh3dPipelines[index];
610 if (pipeline) return pipeline;
611
612 WGPUVertexAttribute attrs[6] = {};
613 fillMeshAttributes(attrs);
614 WGPUVertexBufferLayout vb{};
615 fillVertexLayout(vb, sizeof(MeshVertex), attrs, 6);
616
617 WGPUDepthStencilState ds{};
618 ds.format = WGPUTextureFormat_Depth32Float;
619 ds.depthWriteEnabled = depthWrite ? WGPUOptionalBool_True : WGPUOptionalBool_False;
620 // Built-in opaque ForwardPlus replaces the equal-depth Hybrid result;
621 // alpha surfaces keep strict depth and their existing blend behavior.
622 ds.depthCompare =
623 blend == BlendMode::Opaque && depthWrite ? WGPUCompareFunction_LessEqual : WGPUCompareFunction_Less;
624
625 WGPUBlendState blendDesc = blendState(blend);
626 WGPUColorTargetState target{};
627 target.format = sceneColorFormat;
628 target.blend = blend == BlendMode::Opaque ? nullptr : &blendDesc;
629 target.writeMask = WGPUColorWriteMask_All;
630
631 wgpu::ShaderModule vertModule = makeWgslModule(device, kMesh3DVertWgsl);
632 wgpu::ShaderModule fragModule = makeWgslModule(device, kMesh3DFragWgsl);
633 WGPUFragmentState fs{};
634 fs.module = fragModule.Get();
635 fs.entryPoint = sv("fs_main");
636 fs.targetCount = 1;
637 fs.targets = &target;
638
639 WGPURenderPipelineDescriptor pd{};
640 pd.label = canvasTarget ? sv("eve_mesh3d_canvas_surface") : sv("eve_mesh3d_surface");
641 pd.layout = mesh3dPipelineLayout.Get();
642 pd.vertex.module = vertModule.Get();
643 pd.vertex.entryPoint = sv("vs_main");
644 pd.vertex.bufferCount = 1;
645 pd.vertex.buffers = &vb;
646 pd.fragment = &fs;
647 pd.primitive.topology = WGPUPrimitiveTopology_TriangleList;
648 pd.primitive.frontFace = WGPUFrontFace_CCW;
649 pd.primitive.cullMode = doubleSided ? WGPUCullMode_None : WGPUCullMode_Back;
650 pd.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
651 pd.depthStencil = &ds;
652 pd.multisample.count = canvasTarget ? 1 : sceneColorSamples;
653 pd.multisample.mask = 0xFFFFFFFFu;
654 pipeline = device.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
655
656 if (!canvasTarget && blend == BlendMode::Opaque && depthWrite && !doubleSided) mesh3dPipeline = pipeline;
657 if (!canvasTarget && blend == BlendMode::Alpha && !depthWrite && !doubleSided) mesh3dTransparentPipeline = pipeline;
658 if (canvasTarget && blend == BlendMode::Opaque && depthWrite && !doubleSided) mesh3dCanvasPipeline = pipeline;
659 return pipeline;
660}
661
662
663void Graphics::createMesh3DClusteredPipeline() {
664 mesh3dClusteredSetLayout = makeMesh3DClusteredBindGroupLayout();
665 mesh3dClusteredPipelineLayout = makeMesh3DClusteredPipelineLayout();
666
667 WGPUVertexAttribute attrs[6] = {};
668 fillMeshAttributes(attrs);
669 attrs[0].format = WGPUVertexFormat_Float32x3; // pos
670 attrs[0].offset = 0;
671 attrs[0].shaderLocation = 0;
672 attrs[1].format = WGPUVertexFormat_Float32x3; // normal
673 attrs[1].offset = 12;
674 attrs[1].shaderLocation = 1;
675 attrs[2].format = WGPUVertexFormat_Float32x2; // uv
676 attrs[2].offset = 24;
677 attrs[2].shaderLocation = 2;
678 WGPUVertexBufferLayout vb{};
679 fillVertexLayout(vb, sizeof(MeshVertex), attrs, 6);
680
681 WGPUDepthStencilState ds{};
682 ds.format = WGPUTextureFormat_Depth32Float;
683 ds.depthWriteEnabled = WGPUOptionalBool_True;
684 ds.depthCompare = WGPUCompareFunction_LessEqual;
685 ds.stencilReadMask = 0;
686 ds.stencilWriteMask = 0;
687
688 WGPUColorTargetState target{};
689 target.format = sceneColorFormat;
690 target.blend = nullptr;
691 target.writeMask = WGPUColorWriteMask_All;
692
693 WGPURenderPipelineDescriptor pd{};
694 pd.label = sv("eve_mesh3d_clustered");
695 pd.layout = mesh3dClusteredPipelineLayout.Get();
696 wgpu::ShaderModule vertModule = makeWgslModule(device, kMesh3DClusteredVertWgsl);
697 wgpu::ShaderModule fragModule = makeWgslModule(device, kMesh3DClusteredFragWgsl);
698 pd.vertex.module = vertModule.Get();
699 pd.vertex.entryPoint = sv("vs_main");
700 pd.vertex.bufferCount = 1;
701 pd.vertex.buffers = &vb;
702 WGPUFragmentState fs{};
703 fs.module = fragModule.Get();
704 fs.entryPoint = sv("fs_main");
705 fs.targetCount = 1;
706 fs.targets = &target;
707 pd.fragment = &fs;
708 pd.primitive.topology = WGPUPrimitiveTopology_TriangleList;
709 pd.primitive.frontFace = WGPUFrontFace_CCW;
710 pd.primitive.cullMode = WGPUCullMode_Back;
711 pd.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
712 pd.depthStencil = &ds;
713 pd.multisample.count = sceneColorSamples;
714 pd.multisample.mask = 0xFFFFFFFFu;
715 mesh3dClusteredPipeline = device.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
716}
717
718void Graphics::createShadowPipelines() {
719 shadowSetLayout = makeShadowBindGroupLayout();
720 shadowPipelineLayout = makeShadowPipelineLayout();
721
722 WGPUVertexAttribute attrs[6] = {};
723 fillMeshAttributes(attrs);
724 attrs[0].format = WGPUVertexFormat_Float32x3;
725 attrs[0].offset = 0;
726 attrs[0].shaderLocation = 0;
727 attrs[1].format = WGPUVertexFormat_Float32x3;
728 attrs[1].offset = 12;
729 attrs[1].shaderLocation = 1;
730 attrs[2].format = WGPUVertexFormat_Float32x2;
731 attrs[2].offset = 24;
732 attrs[2].shaderLocation = 2;
733 WGPUVertexBufferLayout vb{};
734 fillVertexLayout(vb, sizeof(MeshVertex), attrs, 6);
735
736 WGPUDepthStencilState ds{};
737 ds.format = WGPUTextureFormat_Depth32Float;
738 ds.depthWriteEnabled = WGPUOptionalBool_True;
739 ds.depthCompare = WGPUCompareFunction_Less;
740 ds.stencilReadMask = 0;
741 ds.stencilWriteMask = 0;
742
743 WGPURenderPipelineDescriptor pd{};
744 pd.label = sv("eve_shadow");
745 pd.layout = shadowPipelineLayout.Get();
746 wgpu::ShaderModule vertModule = makeWgslModule(device, kMesh3DShadowVertWgsl);
747 pd.vertex.module = vertModule.Get();
748 pd.vertex.entryPoint = sv("vs_main");
749 pd.vertex.bufferCount = 1;
750 pd.vertex.buffers = &vb;
751 pd.fragment = nullptr;
752 pd.primitive.topology = WGPUPrimitiveTopology_TriangleList;
753 pd.primitive.frontFace = WGPUFrontFace_CCW;
754 pd.primitive.cullMode = WGPUCullMode_None;
755 pd.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
756 pd.depthStencil = &ds;
757 pd.multisample.count = 1;
758 // Zero-init would leave mask=0, which discards every fragment
759 // (sampleMask=0). The WebGPU default is 0xFFFFFFFF (all samples).
760 pd.multisample.mask = 0xFFFFFFFFu;
761 mesh3dShadowPipeline = device.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
762 pd.label = sv("eve_shadow_single_sided");
763 pd.primitive.cullMode = WGPUCullMode_Back;
764 mesh3dShadowSingleSidedPipeline =
765 device.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
766
767 wgpu::ShaderModule alphaVertModule = makeWgslModule(device, kMesh3DShadowAlphaVertWgsl);
768 wgpu::ShaderModule alphaFragModule = makeWgslModule(device, kMesh3DShadowAlphaFragWgsl);
769 pd.label = sv("eve_shadow_alpha");
770 pd.primitive.cullMode = WGPUCullMode_None;
771 pd.vertex.module = alphaVertModule.Get();
772 WGPUFragmentState alphaFs{};
773 alphaFs.module = alphaFragModule.Get();
774 alphaFs.entryPoint = sv("fs_main");
775 alphaFs.targetCount = 0;
776 alphaFs.targets = nullptr;
777 pd.fragment = &alphaFs;
778 mesh3dShadowAlphaPipeline =
779 device.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
780 pd.label = sv("eve_shadow_alpha_single_sided");
781 pd.primitive.cullMode = WGPUCullMode_Back;
782 mesh3dShadowAlphaSingleSidedPipeline =
783 device.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
784}
785
786void Graphics::createGbufferPipelines() {
787 gbufferSetLayout = makeGbufferBindGroupLayout();
788 gbufferPipelineLayout = makeGbufferPipelineLayout();
789
790 WGPUVertexAttribute attrs[6] = {};
791 fillMeshAttributes(attrs);
792 attrs[0].format = WGPUVertexFormat_Float32x3;
793 attrs[0].offset = 0;
794 attrs[0].shaderLocation = 0;
795 attrs[1].format = WGPUVertexFormat_Float32x3;
796 attrs[1].offset = 12;
797 attrs[1].shaderLocation = 1;
798 attrs[2].format = WGPUVertexFormat_Float32x2;
799 attrs[2].offset = 24;
800 attrs[2].shaderLocation = 2;
801 WGPUVertexBufferLayout vb{};
802 fillVertexLayout(vb, sizeof(MeshVertex), attrs, 6);
803
804 WGPUDepthStencilState ds{};
805 ds.format = WGPUTextureFormat_Depth32Float;
806 ds.depthWriteEnabled = WGPUOptionalBool_True;
807 ds.depthCompare = WGPUCompareFunction_Less;
808 ds.stencilReadMask = 0;
809 ds.stencilWriteMask = 0;
810
811 WGPUColorTargetState targets[5] = {};
812 for (int i = 0; i < 5; ++i) {
813 targets[i].format = WGPUTextureFormat_RGBA8Unorm;
814 targets[i].blend = nullptr;
815 targets[i].writeMask = WGPUColorWriteMask_All;
816 }
817
818 WGPURenderPipelineDescriptor pd{};
819 targets[4].format = WGPUTextureFormat_RGBA16Float;
820 pd.label = sv("eve_gbuffer");
821 pd.layout = gbufferPipelineLayout.Get();
822 wgpu::ShaderModule vertModule = makeWgslModule(device, kMesh3DGbufferVertWgsl);
823 wgpu::ShaderModule fragModule = makeWgslModule(device, kMesh3DGbufferFragWgsl);
824 pd.vertex.module = vertModule.Get();
825 pd.vertex.entryPoint = sv("vs_main");
826 pd.vertex.bufferCount = 1;
827 pd.vertex.buffers = &vb;
828 WGPUFragmentState fs{};
829 fs.module = fragModule.Get();
830 fs.entryPoint = sv("fs_main");
831 fs.targetCount = 5;
832 fs.targets = targets;
833 pd.fragment = &fs;
834 pd.primitive.topology = WGPUPrimitiveTopology_TriangleList;
835 pd.primitive.frontFace = WGPUFrontFace_CCW;
836 pd.primitive.cullMode = WGPUCullMode_None;
837 pd.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
838 pd.depthStencil = &ds;
839 pd.multisample.count = 1;
840 // Zero-init would leave mask=0, which discards every fragment
841 // (sampleMask=0). The WebGPU default is 0xFFFFFFFF (all samples).
842 pd.multisample.mask = 0xFFFFFFFFu;
843 mesh3dGbufferPipeline = device.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
844
845 wgpu::ShaderModule alphaFragModule = makeWgslModule(device, kMesh3DGbufferAlphaFragWgsl);
846 pd.label = sv("eve_gbuffer_alpha");
847 fs.module = alphaFragModule.Get();
848 mesh3dGbufferAlphaPipeline =
849 device.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
850}
851
852void Graphics::createDecalPipeline() {
853 decalSetLayout = makeDecalBindGroupLayout();
854 decalPipelineLayout = makeDecalPipelineLayout();
855 wgpu::ShaderModule vert = makeWgslModule(device, kDecalVertWgsl);
856 wgpu::ShaderModule frag = makeWgslModule(device, kDecalFragWgsl);
857 WGPUBlendState blend = alphaBlend();
858 WGPUColorTargetState targets[3]{};
859 for (auto& target : targets) {
860 target.format = WGPUTextureFormat_RGBA8Unorm;
861 target.blend = &blend;
862 target.writeMask = WGPUColorWriteMask_All;
863 }
864 WGPUFragmentState fs{};
865 fs.module = frag.Get();
866 fs.entryPoint = sv("fs_main");
867 fs.targetCount = 3;
868 fs.targets = targets;
869 WGPURenderPipelineDescriptor pd{};
870 pd.label = sv("eve_decal");
871 pd.layout = decalPipelineLayout.Get();
872 pd.vertex.module = vert.Get();
873 pd.vertex.entryPoint = sv("vs_main");
874 pd.fragment = &fs;
875 pd.primitive.topology = WGPUPrimitiveTopology_TriangleList;
876 // kMesh3DVertWgsl mirrors clip-space Y, so object-space CCW becomes
877 // clockwise in framebuffer space (the Vulkan pipeline uses Clockwise).
878 pd.primitive.frontFace = WGPUFrontFace_CW;
879 pd.primitive.cullMode = WGPUCullMode_None;
880 pd.multisample.count = 1;
881 pd.multisample.mask = 0xFFFFFFFFu;
882 decalPipeline = device.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
883}
884
885
886// ---------------------------------------------------------------------------
887// SSAO (screen-space ambient occlusion)
888// ---------------------------------------------------------------------------
889
890void Graphics::ensureAOResources(int width, int height) {
891 if (!device || width <= 0 || height <= 0) return;
892 if (!aoSetLayout) {
893 BindGroupLayoutBuilder b;
894 b.buffer(0, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, false, 32);
895 b.texture(1, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Depth, wgpu::TextureViewDimension::e2D);
896 b.sampler(2, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering);
897 aoSetLayout = b.build(device, "eve_ao");
898 WGPUBindGroupLayout layouts[1] = {aoSetLayout.Get()};
899 WGPUPipelineLayoutDescriptor pl{};
900 pl.label = sv("eve_ao_layout");
901 pl.bindGroupLayoutCount = 1;
902 pl.bindGroupLayouts = layouts;
903 aoPipelineLayout = device.CreatePipelineLayout(reinterpret_cast<const wgpu::PipelineLayoutDescriptor*>(&pl));
904
905 WGPUVertexAttribute attrs[3]{};
906 attrs[0].format = WGPUVertexFormat_Float32x2;
907 attrs[0].offset = 0;
908 attrs[0].shaderLocation = 0;
909 attrs[1].format = WGPUVertexFormat_Float32x4;
910 attrs[1].offset = 8;
911 attrs[1].shaderLocation = 1;
912 attrs[2].format = WGPUVertexFormat_Float32x2;
913 attrs[2].offset = 24;
914 attrs[2].shaderLocation = 2;
915 WGPUVertexBufferLayout vb{};
916 fillVertexLayout(vb, 32, attrs, 3);
917
918 WGPUColorTargetState target{};
919 target.format = WGPUTextureFormat_RGBA8Unorm;
920 target.blend = nullptr;
921 target.writeMask = WGPUColorWriteMask_All;
922
923 WGPURenderPipelineDescriptor pd{};
924 pd.label = sv("eve_ssao");
925 pd.layout = aoPipelineLayout.Get();
926 wgpu::ShaderModule vertModule = makeWgslModule(device, kTexturedVertWgsl);
927 wgpu::ShaderModule fragModule = makeWgslModule(device, kSSAOFragWgsl);
928 pd.vertex.module = vertModule.Get();
929 pd.vertex.entryPoint = sv("vs_main");
930 pd.vertex.bufferCount = 1;
931 pd.vertex.buffers = &vb;
932 WGPUFragmentState fs{};
933 fs.module = fragModule.Get();
934 fs.entryPoint = sv("fs_main");
935 fs.targetCount = 1;
936 fs.targets = &target;
937 pd.fragment = &fs;
938 pd.primitive.topology = WGPUPrimitiveTopology_TriangleList;
939 pd.primitive.frontFace = WGPUFrontFace_CCW;
940 pd.primitive.cullMode = WGPUCullMode_None;
941 pd.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
942 pd.multisample.count = 1;
943 pd.multisample.mask = 0xFFFFFFFFu;
944 aoPipeline = device.CreateRenderPipeline(reinterpret_cast<const wgpu::RenderPipelineDescriptor*>(&pd));
945 }
946 if (!aoUbo) {
947 WGPUBufferDescriptor bd{};
948 bd.label = sv("eve_ao_ubo");
949 bd.size = 64;
950 bd.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform;
951 bd.mappedAtCreation = false;
952 aoUbo = device.CreateBuffer(reinterpret_cast<const wgpu::BufferDescriptor*>(&bd));
953 }
954 if (!aoTex[0]) {
955 for (int i = 0; i < 2; ++i) {
956 WGPUTextureDescriptor td{};
957 td.label = sv("eve_ao");
958 td.dimension = WGPUTextureDimension_2D;
959 // Half-resolution AO: the bilinear upsample in the mesh pass blurs
960 // the per-pixel sampling grain that otherwise reads as noise.
961 td.size = {static_cast<uint32_t>((width + 1) / 2), static_cast<uint32_t>((height + 1) / 2), 1};
962 td.sampleCount = 1;
963 td.format = WGPUTextureFormat_RGBA8Unorm;
964 td.mipLevelCount = 1;
965 td.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment;
966 aoTex[i] = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&td));
967 aoView[i] = aoTex[i].CreateView();
968 }
969 aoReady = true;
970 // New AO binding �?rebuild cached mesh bind groups.
971 clearMeshBindGroupCache();
972 }
973 if (!fullscreenQuadReady) {
974 float verts[32] = {
975 -1.f, -1.f, 1.f, 1.f, 1.f, 1.f, 0.f, 0.f, 1.f, -1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 0.f,
976 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, -1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 0.f, 1.f,
977 };
978 uint32_t indices[6] = {0, 1, 2, 2, 3, 0};
979 WGPUBufferDescriptor vbd{};
980 vbd.label = sv("eve_fullscreen_vb");
981 vbd.size = sizeof(verts);
982 vbd.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex;
983 fullscreenQuadVb = device.CreateBuffer(reinterpret_cast<const wgpu::BufferDescriptor*>(&vbd));
984 queue.WriteBuffer(fullscreenQuadVb, 0, verts, sizeof(verts));
985 WGPUBufferDescriptor ibd{};
986 ibd.label = sv("eve_fullscreen_ib");
987 ibd.size = sizeof(indices);
988 ibd.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Index;
989 fullscreenQuadIb = device.CreateBuffer(reinterpret_cast<const wgpu::BufferDescriptor*>(&ibd));
990 queue.WriteBuffer(fullscreenQuadIb, 0, indices, sizeof(indices));
991 fullscreenQuadReady = true;
992 }
993}
994
995wgpu::BindGroup Graphics::makeAOBindGroup(wgpu::TextureView depthView) {
996 WGPUBindGroupEntry entries[3]{};
997 entries[0].binding = 0;
998 entries[0].buffer = aoUbo.Get();
999 entries[0].size = 32;
1000 entries[1].binding = 1;
1001 entries[1].textureView = depthView.Get();
1002 entries[2].binding = 2;
1003 entries[2].sampler = mainSampler.Get();
1004 WGPUBindGroupDescriptor desc{};
1005 desc.label = sv("eve_ao_group");
1006 desc.layout = aoSetLayout.Get();
1007 desc.entryCount = 3;
1008 desc.entries = entries;
1009 return device.CreateBindGroup(reinterpret_cast<const wgpu::BindGroupDescriptor*>(&desc));
1010}
1011
1012// ---------------------------------------------------------------------------
1013// Arena helpers
1014// ---------------------------------------------------------------------------
1015
1016
1017} // namespace eve::graphics::webgpu
LogicalId target
std::unordered_map< std::string, QuestRuntime > entries
std::vector< std::uint32_t > verts
Definition Builder.cpp:27
scene::NodeDesc desc
std::string layout
std::vector< FieldTarget > targets
vk::ShaderModule vert
vk::ShaderModule frag
std::vector< std::uint32_t > indices
float blend
std::uint32_t height
std::uint32_t width
MeleePoint3 b
Definition MeleeHit.cpp:41
bool doubleSided
float d
int detail
Local-probe upload record kept off the high-fanout IGraphics3D header.
uint32_t index
static constexpr uint32_t kPushConstantBytes
Definition Shader.h:45
WGPUBlendState premultipliedBlend()
Internal WebGPU pipeline/descriptor helper; render-thread only.
WGPUBlendState noBlend()
Internal WebGPU pipeline/descriptor helper; render-thread only.
void fillMeshAttributes(WGPUVertexAttribute(&attrs)[6])
Internal WebGPU pipeline/descriptor helper; render-thread only.
WGPUBlendState alphaBlend()
Internal WebGPU pipeline/descriptor helper; render-thread only.
WGPUBlendState blendState(BlendMode mode)
Internal WebGPU pipeline/descriptor helper; render-thread only.
void fillVertexLayout(WGPUVertexBufferLayout &layout, uint64_t stride, const WGPUVertexAttribute *attrs, uint32_t attrCount)
Internal WebGPU pipeline/descriptor helper; render-thread only.
WGPUBlendState additiveBlend()
Internal WebGPU pipeline/descriptor helper; render-thread only.
WGPUBlendState multiplyBlend()
Internal WebGPU pipeline/descriptor helper; render-thread only.
size_t meshPipelineIndex(BlendMode blend, bool depthWrite, bool doubleSided)
Internal WebGPU pipeline/descriptor helper; render-thread only.
const char * kDecalFragWgsl
const char * kColorVertWgsl
const char * kMesh3DFragWgsl
const char * kDecalVertWgsl
const char * kColorFragWgsl
const char * kTexturedVertWgsl
WGPUStringView sv(const char *s)
Build a WGPUStringView from a C string (null-safe, length auto-computed).
Definition wgpu_types.h:20
const char * kMesh3DVertWgsl
Immutable shader source. @borrowed Static storage; valid for the process lifetime.
const char * kMesh3DGbufferFragWgsl
Immutable GBuffer fill WGSL. @borrowed Static storage; valid for the process lifetime.
const char * kMesh3DShadowAlphaVertWgsl
Immutable shader source. @borrowed Static storage; valid for the process lifetime.
const char * kLit2DVertWgsl
const char * kMesh3DShadowVertWgsl
Immutable shader source. @borrowed Static storage; valid for the process lifetime.
const char * kSSAOFragWgsl
const char * kTexturedFragWgsl
const char * kLit2DFragWgsl
wgpu::ShaderModule makeWgslModule(wgpu::Device &dev, const char *wgsl)
Create an owning shader module; retain it until pipeline creation completes.
Definition wgpu_types.h:34
const char * kMesh3DShadowAlphaFragWgsl
const char * kMesh3DClusteredVertWgsl
const char * kMesh3DGbufferAlphaFragWgsl
Immutable GBuffer alpha-cutout WGSL. @borrowed Static storage; valid for the process lifetime.
const char * kMesh3DClusteredFragWgsl
const char * kMesh3DGbufferVertWgsl
Immutable shader source. @borrowed Static storage; valid for the process lifetime.
Light3DGpu ClusteredLightGpu
eve::BlendMode BlendMode
Compatibility alias for the shared 2D blend mode.
Definition BlendMode.h:8
const HouseComponent * pick(const std::vector< std::reference_wrapper< const HouseComponent > > &choices, std::mt19937 &rng)
Borrow a weighted component from library-backed choices. @ownership Borrowed; the session's library o...
std::vector< int64_t > attrs(const Node &n, const char *key, std::vector< int64_t > fallback)
Attrs.