载入中...
搜索中...
未找到
GraphicsPasses.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::createSceneColorResources(int width, int height) {
52 if (!device) return;
53 // Keep the main WebGPU scene single-sampled. Dawn/Metal does not complete
54 // the current MSAA resolve followed by synchronous scene readback chain.
55 // Temporal AA remains available; Vulkan owns the multisampled main path.
56 const uint32_t want = 1u;
57 if (sceneColorWidth == width && sceneColorHeight == height && sceneColorSamples == want && !sceneColorSlots.empty())
58 return;
59
60 destroySceneColorResources();
61 sceneColorWidth = width;
62 sceneColorHeight = height;
63 const uint32_t oldSamples = sceneColorSamples;
64 const bool samplesChanged = oldSamples != want;
65 sceneColorSamples = want;
66 sceneColorSlots.reserve(kFramesInFlight);
67 for (int s = 0; s < int(kFramesInFlight); ++s) {
68 sceneColorSlots.emplace_back();
69 SceneColorSlot& slot = sceneColorSlots.back();
70 slot.sampleCount = sceneColorSamples;
71
72 WGPUTextureDescriptor cd{};
73 cd.label = sv("eve_scene_color");
74 cd.dimension = WGPUTextureDimension_2D;
75 cd.size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1};
76 // Single-sample resolve target: composited to the swapchain and used
77 // as the frame-readback source. The multisampled color lives in
78 // msaaColor (created below) and is resolved into this texture.
79 cd.sampleCount = 1;
80 cd.format = sceneColorFormat;
81 cd.mipLevelCount = 1;
82 cd.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc;
83 slot.color = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&cd));
84 slot.colorView = slot.color.CreateView();
85
86 if (sceneColorSamples > 1) {
87 // Multisampled color target; the scene pass resolves it into the
88 // single-sample `color` above for compositing / readback.
89 WGPUTextureDescriptor mcd{};
90 mcd.label = sv("eve_scene_color_msaa");
91 mcd.dimension = WGPUTextureDimension_2D;
92 mcd.size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1};
93 mcd.sampleCount = sceneColorSamples;
94 mcd.format = sceneColorFormat;
95 mcd.mipLevelCount = 1;
96 mcd.usage = WGPUTextureUsage_RenderAttachment;
97 slot.msaaColor = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&mcd));
98 slot.msaaView = slot.msaaColor.CreateView();
99 }
100
101 WGPUTextureDescriptor dd{};
102 dd.label = sv("eve_scene_depth");
103 dd.dimension = WGPUTextureDimension_2D;
104 dd.size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1};
105 dd.sampleCount = sceneColorSamples;
106 dd.format = WGPUTextureFormat_Depth32Float;
107 dd.mipLevelCount = 1;
108 dd.usage = WGPUTextureUsage_RenderAttachment;
109 slot.depth = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&dd));
110 slot.depthView = slot.depth.CreateView();
111
112 slot.colorGpu.texture = slot.color;
113 slot.colorGpu.view = slot.colorView;
114 slot.colorGpu.width = width;
115 slot.colorGpu.height = height;
116 slot.colorGpu.sampler = createLinearSampler(device);
117 slot.colorGpu.samplerState = TextureSampler::linearMipmap();
118
119 slot.colorTex.gpuHandle = &slot.colorGpu;
120 slot.colorTex.width = width;
121 slot.colorTex.height = height;
122 slot.colorTex.mipmapCount = 1;
123 }
124 sceneColorTexture = &sceneColorSlots[0].colorTex;
125 // 3D pipelines that render into the scene target must match its sample
126 // count. Invalidate only the affected cached variants; they are rebuilt
127 // individually if a later draw actually needs them.
128 if (samplesChanged) {
129 mesh3dPipelines = {};
130 mesh3dPipeline = {};
131 mesh3dTransparentPipeline = {};
132 mesh3dClusteredPipeline = {};
133 voxelRectPipeline = {};
134 // Deferred lighting is 1x-only; recreate when scene sample count changes.
135 destroyDeferredLightingResources();
136 }
137 if (samplesChanged && gpuDrivenCullPipeline_) {
138 // The forward indirect pipeline inherits the scene target sample
139 // count. Recreate the GPU-driven layouts/pipelines lazily on the next
140 // cull after an MSAA toggle.
141 gpuDrivenCullPipeline_ = {};
142 gpuDrivenRenderPipeline_ = {};
143 gpuDrivenCanvasPipeline_ = {};
144 gpuDrivenComputePipelineLayout_ = {};
145 gpuDrivenRenderPipelineLayout_ = {};
146 gpuDrivenComputeSetLayout_ = {};
147 gpuDrivenRenderSetLayout_ = {};
148 gpuDrivenComputeBindGroup_ = {};
149 gpuDrivenRenderBindGroup_ = {};
150 }
151}
152
153void Graphics::destroySceneColorResources() {
154 sceneColorSlots.clear();
155 sceneColorTexture = nullptr;
156 sceneColorHistoryValid = false;
157}
158
159void Graphics::setMsaaSamples(int samples) {
160 msaaSamples = samples > 0 ? samples : 0;
161 if (!initialized || sceneColorWidth <= 0 || sceneColorHeight <= 0) return;
162 // Force the offscreen targets (and the 3D pipelines that bind them) to
163 // rebuild at the new sample count.
164 destroySceneColorResources();
165 createSceneColorResources(sceneColorWidth, sceneColorHeight);
166}
167
168void Graphics::createShadowResources() {
169 if (!device) return;
171 if (shadowDepthArray) {
172 if (shadowMapSize == wantSize) return;
173 destroyShadowResources();
174 }
175 shadowMapSize = wantSize;
176
177 // Depth array: CSM + local layers, renderable + sampleable.
178 auto* gpu = new GpuTexture();
179 WGPUTextureDescriptor td{};
180 td.label = sv("eve_shadow_depth");
181 td.dimension = WGPUTextureDimension_2D;
182 td.size = {static_cast<uint32_t>(shadowMapSize), static_cast<uint32_t>(shadowMapSize),
183 static_cast<uint32_t>(ShadowConfig::kTotalLayers)};
184 td.sampleCount = 1;
185 td.format = WGPUTextureFormat_Depth32Float;
186 td.mipLevelCount = 1;
187 td.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc |
188 WGPUTextureUsage_CopyDst;
189 gpu->texture = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&td));
190
191 WGPUTextureViewDescriptor avd{};
192 avd.format = WGPUTextureFormat_Depth32Float;
193 avd.dimension = WGPUTextureViewDimension_2DArray;
194 avd.baseMipLevel = 0;
195 avd.mipLevelCount = 1;
196 avd.baseArrayLayer = 0;
197 avd.arrayLayerCount = 3;
198 gpu->view = gpu->texture.CreateView(reinterpret_cast<const wgpu::TextureViewDescriptor*>(&avd));
199
200 WGPUSamplerDescriptor sd{};
201 sd.label = sv("eve_shadow_sampler");
202 sd.addressModeU = WGPUAddressMode_ClampToEdge;
203 sd.addressModeV = WGPUAddressMode_ClampToEdge;
204 sd.addressModeW = WGPUAddressMode_ClampToEdge;
205 sd.magFilter = WGPUFilterMode_Linear;
206 sd.minFilter = WGPUFilterMode_Linear;
207 sd.mipmapFilter = WGPUMipmapFilterMode_Nearest;
208 sd.compare = WGPUCompareFunction_LessEqual;
209 sd.maxAnisotropy = 1;
210 gpu->sampler = device.CreateSampler(reinterpret_cast<const wgpu::SamplerDescriptor*>(&sd));
211 shadowDepthArray = gpu;
212}
213
214void Graphics::destroyShadowResources() {
215 shadowDepthArray = nullptr;
216 shadowMapSize = 0;
217}
218
219void Graphics::createGbufferResources(int width, int height) {
220 if (!device) return;
221 if (!caps.supportsFiveTargetGBuffer())
222 throw Exception("WebGPU: five-target GBuffer requires 40 color attachment bytes per sample");
223 if (gbufferWidth == width && gbufferHeight == height && !gbufferSlots.empty()) return;
224 destroyGbufferResources();
225 gbufferWidth = width;
226 gbufferHeight = height;
227
228 WGPUTextureDescriptor td{};
229 td.label = sv("eve_gbuffer_target");
230 td.dimension = WGPUTextureDimension_2D;
231 td.size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1};
232 td.sampleCount = 1;
233 td.format = WGPUTextureFormat_RGBA8Unorm;
234 td.mipLevelCount = 1;
235 td.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc;
236
237 WGPUTextureDescriptor dd{};
238 dd.label = sv("eve_gbuffer_depth");
239 dd.dimension = WGPUTextureDimension_2D;
240 dd.size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1};
241 dd.sampleCount = 1;
242 dd.format = WGPUTextureFormat_Depth32Float;
243 dd.mipLevelCount = 1;
244 // TextureBinding so X-ray (and AO) can sample the scene depth in a later pass.
245 dd.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment;
246
247 WGPUTextureDescriptor visIdDesc = td;
248 visIdDesc.label = sv("eve_visibility_id");
249 visIdDesc.format = WGPUTextureFormat_RG32Uint;
250 WGPUTextureDescriptor visBaryDesc = td;
251 visBaryDesc.label = sv("eve_visibility_bary");
252 visBaryDesc.format = WGPUTextureFormat_RG16Float;
253
254 gbufferSlots.reserve(kFramesInFlight);
255 for (int s = 0; s < int(kFramesInFlight); ++s) {
256 gbufferSlots.emplace_back();
257 GbufferSlot& slot = gbufferSlots.back();
258 slot.normal = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&td));
259 slot.normalView = slot.normal.CreateView();
260 slot.depthColor = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&td));
261 slot.depthColorView = slot.depthColor.CreateView();
262 slot.albedo = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&td));
263 slot.albedoView = slot.albedo.CreateView();
264 slot.pbrParams = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&td));
265 slot.pbrParamsView = slot.pbrParams.CreateView();
266 auto emissiveDesc = td;
267 emissiveDesc.format = WGPUTextureFormat_RGBA16Float;
268 slot.emissive = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&emissiveDesc));
269 slot.emissiveView = slot.emissive.CreateView();
270 slot.depth = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&dd));
271 slot.depthView = slot.depth.CreateView();
272 slot.visID = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&visIdDesc));
273 slot.visIDView = slot.visID.CreateView();
274 slot.visBary = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&visBaryDesc));
275 slot.visBaryView = slot.visBary.CreateView();
276
277 slot.normalGpu.texture = slot.normal;
278 slot.normalGpu.view = slot.normalView;
279 slot.normalGpu.sampler = createLinearSampler(device);
280 slot.depthColorGpu.texture = slot.depthColor;
281 slot.depthColorGpu.view = slot.depthColorView;
282 slot.depthColorGpu.sampler = createLinearSampler(device);
283 slot.albedoGpu.texture = slot.albedo;
284 slot.albedoGpu.view = slot.albedoView;
285 slot.albedoGpu.sampler = createLinearSampler(device);
286 slot.pbrParamsGpu.texture = slot.pbrParams;
287 slot.pbrParamsGpu.view = slot.pbrParamsView;
288 slot.pbrParamsGpu.sampler = createLinearSampler(device);
289 slot.emissiveGpu.texture = slot.emissive;
290 slot.emissiveGpu.view = slot.emissiveView;
291 slot.emissiveGpu.sampler = createLinearSampler(device);
292 slot.depthGpu.texture = slot.depth;
293 slot.depthGpu.view = slot.depthView;
294 slot.depthGpu.sampler = createLinearSampler(device);
295
296 slot.normalTex.gpuHandle = &slot.normalGpu;
297 slot.normalTex.width = width;
298 slot.normalTex.height = height;
299 slot.depthColorTex.gpuHandle = &slot.depthColorGpu;
300 slot.depthColorTex.width = width;
301 slot.depthColorTex.height = height;
302 slot.albedoTex.gpuHandle = &slot.albedoGpu;
303 slot.albedoTex.width = width;
304 slot.albedoTex.height = height;
305 slot.pbrParamsTex.gpuHandle = &slot.pbrParamsGpu;
306 slot.pbrParamsTex.width = width;
307 slot.pbrParamsTex.height = height;
308 slot.emissiveTex.gpuHandle = &slot.emissiveGpu;
309 slot.emissiveTex.width = width;
310 slot.emissiveTex.height = height;
311 slot.depthTex.gpuHandle = &slot.depthGpu;
312 slot.depthTex.width = width;
313 slot.depthTex.height = height;
314 }
315}
316
317void Graphics::destroyGbufferResources() {
318 gbufferSlots.clear();
319 gbufferDepthValid_ = false;
320}
321
322void Graphics::createDecalResources(int width, int height) {
323 if (!device) return;
324 if (decalWidth == width && decalHeight == height && !decalSlots.empty()) return;
325 destroyDecalResources();
326 decalWidth = width;
327 decalHeight = height;
328 WGPUTextureDescriptor td{};
329 td.label = sv("eve_decal_target");
330 td.dimension = WGPUTextureDimension_2D;
331 td.size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1};
332 td.sampleCount = 1;
333 td.format = WGPUTextureFormat_RGBA8Unorm;
334 td.mipLevelCount = 1;
335 td.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc;
336 decalSlots.resize(kFramesInFlight);
337 for (auto& slot : decalSlots) {
338 slot.albedo = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&td));
339 slot.albedoView = slot.albedo.CreateView();
340 slot.normal = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&td));
341 slot.normalView = slot.normal.CreateView();
342 slot.params = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&td));
343 slot.paramsView = slot.params.CreateView();
344 }
345}
346
347void Graphics::destroyDecalResources() {
348 decalSlots.clear();
349 decalWidth = 0;
350 decalHeight = 0;
351}
352
353// ---------------------------------------------------------------------------
354// Flush helpers
355// ---------------------------------------------------------------------------
356
357
358void Graphics::flushMesh3D(wgpu::RenderPassEncoder pass, WGPUTextureFormat format, bool canvasTarget) {
359 if (mesh3dDraws.empty()) return;
360
361 if (mesh3dClusteredActive && !canvasTarget && !mesh3dClusteredPipeline) {
362 for (const auto& d : mesh3dDraws) {
363 const bool customShader = d.shader && d.shader->gpuHandle;
364 if (!customShader && !d.doubleSided && d.surfaceMode != SurfaceMode::Transparent && d.mesh &&
365 !d.mesh->hasGpuSkinning()) {
366 createMesh3DClusteredPipeline();
367 break;
368 }
369 }
370 }
371
372 auto& uboArena = currentUboArena();
373 ensureUboArena(uboArena, uboArena.used + mesh3dDraws.size() * 10240);
374
375 // Pre-allocate per-draw UBO slots.
376 for (auto& d : mesh3dDraws) {
377 d.frameUboOffset = uboArena.alloc(sizeof(Mesh3DUBO), 256);
378 d.shadowUboOffset = uboArena.alloc(sizeof(ShadowUBO), 256);
379 d.clusteredUboOffset = 0;
380 if (mesh3dClusteredActive && !canvasTarget && d.mesh && !d.mesh->hasGpuSkinning())
381 d.clusteredUboOffset = uboArena.alloc(sizeof(Mesh3DClusteredUBO), 256);
382 d.pushUboOffset = 0;
383 if (d.shader && d.shader->pushConstantSize() > 0)
384 d.pushUboOffset = uboArena.alloc(Shader::kPushConstantBytes, 256);
385 }
386
387 // Upload the per-draw frame UBO + shared shadow UBO.
388 for (auto& d : mesh3dDraws) {
389 if (mesh3dClusteredActive && !canvasTarget && mesh3dClusteredPipeline && d.clusteredUboOffset) {
390 Mesh3DClusteredUBO cubo;
391 cubo.mvp = mesh3dViewProj * d.model;
392 cubo.model = d.model;
393 cubo.view = mesh3dView;
394 cubo.lightDir = glm::vec4(glm::vec3(mesh3dClustered.primaryDir), mesh3dClustered.primaryDir.w);
395 cubo.lightColor = glm::vec4(glm::vec3(mesh3dClustered.primaryColor), mesh3dEnvIntensity);
396 cubo.tint = d.tint;
397 cubo.cameraPos = glm::vec4(mesh3dCameraPos, mesh3dRoughness);
398 cubo.ambient = glm::vec4(glm::vec3(mesh3dClustered.ambient), mesh3dMetallic);
399 cubo.gridInfo = mesh3dClustered.gridInfo;
400 cubo.clipInfo = mesh3dClustered.clipInfo;
401 cubo.texBomb = glm::vec4(mesh3dTexBombScale, mesh3dTexBombStrength, mesh3dTexBombRot, 0.f);
402 cubo.parallax = glm::vec4(mesh3dParallaxScale, mesh3dParallaxMin, mesh3dParallaxMax, 0.f);
403 cubo.virtualTexture = d.virtualTexture;
404 cubo.virtualAtlas = d.virtualAtlas;
405 float surfaceCode = float(int(d.surfaceMode));
406 if (d.surfaceMode == SurfaceMode::Masked && d.alphaTechnique == "dither")
407 surfaceCode = 3.f;
408 else if (d.surfaceMode == SurfaceMode::Masked && d.alphaTechnique == "coverage")
409 surfaceCode = 4.f;
410 const float aoStrength = (renderControl_ && renderControl_->isEnabled("ao")) ? mesh3dSsaoIntensity : 0.f;
411 cubo.surface = glm::vec4(surfaceCode, d.alphaCutoff, aoStrength, 0.f);
412 cubo.envProbeCenter = glm::vec4(mesh3dEnvProbeCenter, 1.f);
413 cubo.envProbeExtent = glm::vec4(mesh3dEnvProbeExtent, 0.f);
414 for (int i = 0; i < ReflectionProbeUpload::kMaxProbes; ++i) {
415 if (i >= mesh3dReflectionProbes.count) continue;
416 const auto& probe = mesh3dReflectionProbes.probes[i];
417 GpuTexture* gpu = gpuForTexture(probe.cubemap);
418 if (!gpu || !gpu->isCube) continue;
419 cubo.reflectionProbeCenter[i] = glm::vec4(probe.center, probe.intensity);
420 cubo.reflectionProbeExtent[i] = glm::vec4(probe.extent, probe.blendDistance);
421 }
422 queue.WriteBuffer(uboArena.buffer, d.clusteredUboOffset, &cubo, sizeof(cubo));
423 continue;
424 }
425 Mesh3DUBO ubo;
426 ubo.mvp = mesh3dViewProj * d.model;
427 ubo.model = d.model;
428 ubo.lightDir = glm::vec4(glm::vec3(mesh3dLighting.lights[0].posRadius), float(mesh3dLighting.count));
429 ubo.lightColor = mesh3dLighting.lights[0].color;
430 ubo.tint = d.tint;
431 ubo.cameraPos = glm::vec4(mesh3dCameraPos, mesh3dRoughness);
432 ubo.ambient = glm::vec4(glm::vec3(mesh3dLighting.ambient), mesh3dMetallic);
433 for (int i = 0; i < Lighting3DPack::kMaxLights; ++i) ubo.lights[i] = mesh3dLighting.lights[i];
434 for (size_t i = 0; i < mesh3dLighting.diffuseProbeSh.size(); ++i)
435 ubo.diffuseProbeSh[i] = mesh3dLighting.diffuseProbeSh[i];
436 ubo.diffuseProbeInfo.x = mesh3dLighting.diffuseProbeShEnabled ? 1.f : 0.f;
437 for (size_t i = 0; i < mesh3dLighting.diffuseVolumePosition.size(); ++i) {
438 ubo.diffuseVolumePosition[i] = mesh3dLighting.diffuseVolumePosition[i];
439 ubo.diffuseVolumeExtent[i] = mesh3dLighting.diffuseVolumeExtent[i];
440 }
441 for (size_t i = 0; i < mesh3dLighting.diffuseVolumeSh.size(); ++i)
442 ubo.diffuseVolumeSh[i] = mesh3dLighting.diffuseVolumeSh[i];
443 ubo.diffuseVolumeInfo.x = static_cast<float>(mesh3dLighting.diffuseVolumeProbeCount);
444 ubo.diffuseVolumeInfo.y = mesh3dLighting.diffuseVolumeTrilinearCell ? 1.f : 0.f;
445 ubo.lodFade = d.lodFade;
446 float surfaceCode = float(int(d.surfaceMode));
447 if (d.surfaceMode == SurfaceMode::Masked && d.alphaTechnique == "dither")
448 surfaceCode = 3.f;
449 else if (d.surfaceMode == SurfaceMode::Masked && d.alphaTechnique == "coverage")
450 surfaceCode = 4.f;
451 ubo.texBomb = glm::vec4(mesh3dTexBombScale, mesh3dTexBombStrength, mesh3dTexBombRot, 0.f);
452 ubo.parallax = glm::vec4(mesh3dParallaxScale, mesh3dParallaxMin, mesh3dParallaxMax, d.alphaCutoff);
453 ubo.virtualTexture = d.virtualTexture;
454 ubo.virtualAtlas = d.virtualAtlas;
455 const float aoStrength = (renderControl_ && renderControl_->isEnabled("ao")) ? mesh3dSsaoIntensity : 0.f;
456 ubo.surface = glm::vec4(surfaceCode, d.alphaCutoff, aoStrength, 0.f);
457 ubo.view = mesh3dView;
458 ubo.clipInfo = glm::vec4(mesh3dNear, mesh3dFar, 0.f, 0.f);
459 ubo.cloud = mesh3dCloud;
460 ubo.cloudWind = mesh3dCloudWind;
461 ubo.envProbeCenter = glm::vec4(mesh3dEnvProbeCenter, 1.f);
462 ubo.envProbeExtent = glm::vec4(mesh3dEnvProbeExtent, 0.f);
463 for (int i = 0; i < ReflectionProbeUpload::kMaxProbes; ++i) {
464 if (i >= mesh3dReflectionProbes.count) continue;
465 const auto& probe = mesh3dReflectionProbes.probes[i];
466 GpuTexture* gpu = gpuForTexture(probe.cubemap);
467 if (!gpu || !gpu->isCube) continue;
468 ubo.reflectionProbeCenter[i] = glm::vec4(probe.center, probe.intensity);
469 ubo.reflectionProbeExtent[i] = glm::vec4(probe.extent, probe.blendDistance);
470 }
471 ubo.lightColor.w = mesh3dEnvIntensity;
472 if (d.mesh && d.mesh->hasGpuSkinning()) ubo.skinInfo.x = static_cast<float>(d.mesh->getSkinPaletteCount());
473 ubo.skinInfo.y = static_cast<float>(d.skinInfluenceLimit);
474 d.skinBuffer = uploadSkinPalette(d.mesh);
475 // X-ray params travel through the Frame UBO (no extra binding). Packed
476 // in bindMeshUniforms("xray") order: colorR..G..B, bias, screenW, screenH,
477 // rimPower, rimStrength, alpha.
478 if (d.shader && d.shader->isXray() && d.shader->pushConstantSize() >= 9 * sizeof(float)) {
479 const float* pc = d.shader->pushConstantData();
480 ubo.texBomb = glm::vec4(pc[0], pc[1], pc[2], pc[8]); // color.xyz + alpha
481 ubo.parallax = glm::vec4(pc[3], pc[4], pc[5], pc[7]); // bias, screenW, screenH, rimStrength
482 ubo.clipInfo.z = pc[6]; // rimPower
483 }
484 queue.WriteBuffer(uboArena.buffer, d.frameUboOffset, &ubo, sizeof(ubo));
485 if (d.shader && d.shader->pushConstantSize() > 0)
486 queue.WriteBuffer(uboArena.buffer, d.pushUboOffset, d.shader->pushConstantData(),
488 }
489
490 // Capture receive-shadow per draw; RenderSystem3D changes it between queued meshes.
491 for (auto& d : mesh3dDraws) {
492 ShadowUBO shadowUbo = mesh3dShadows.ubo;
493 if (!mesh3dShadows.active || !d.shadowReceive) shadowUbo.bias.y = 0.f;
494 queue.WriteBuffer(uboArena.buffer, d.shadowUboOffset, &shadowUbo, sizeof(shadowUbo));
495 }
496
497 for (auto& d : mesh3dDraws) {
498 auto* gpuMesh = static_cast<GpuMesh*>(d.mesh->gpuHandle);
499 if (!gpuMesh || !gpuMesh->vertexBuffer) continue;
500
501 const bool transparent = d.surfaceMode == SurfaceMode::Transparent;
502 const BlendMode blend = transparent ? d.surfaceBlend : BlendMode::Opaque;
503 const bool depthWrite = !transparent || d.depthWrite;
504 // Canvas targets are 1-sample; scene pipelines follow the active MSAA
505 // count. Both sets preserve the per-draw material raster state.
506 const bool customShader = d.shader && d.shader->gpuHandle;
507 if (d.pbrSurface && !customShader && drawPbrMesh(pass, format, canvasTarget, d, *gpuMesh)) continue;
508 wgpu::RenderPipeline pipe;
509 if (d.shader && d.shader->gpuHandle) {
510 auto* gs = static_cast<GpuShader*>(d.shader->gpuHandle);
511 if (gs->isMesh3D && gs->mesh3dPipeline) {
512 if (d.shader->isXray() && gs->mesh3dXrayPipeline)
513 pipe = gs->mesh3dXrayPipeline;
514 else
515 pipe = gs->mesh3dPipeline;
516 }
517 }
518 if (!pipe) pipe = getMesh3DPipeline(blend, depthWrite, d.doubleSided, canvasTarget);
519 const bool useClustered = !canvasTarget && !customShader && !d.doubleSided && mesh3dClusteredActive &&
520 d.surfaceMode != SurfaceMode::Transparent && mesh3dClusteredPipeline &&
521 d.clusteredUboOffset && !d.mesh->hasGpuSkinning();
522 if (useClustered) pipe = mesh3dClusteredPipeline;
523 if (!pipe) continue;
524 pass.SetPipeline(pipe);
525
526 GpuTexture* albedo = gpuForTexture(d.texture);
527 GpuTexture* normal = gpuForTexture(d.normalTexture);
528 GpuTexture* env = gpuForTexture(mesh3dEnvTexture);
529 GpuTexture* height = gpuForTexture(d.heightTexture);
530 GpuTexture* depth = mesh3dSceneDepthTexture ? gpuForTexture(mesh3dSceneDepthTexture) : flatDepthTexture3D;
531 GpuTexture* sceneColor = mesh3dSceneColorTexture ? gpuForTexture(mesh3dSceneColorTexture)
532 : sceneColorHistoryValid && lastPresentSlot < sceneColorSlots.size()
533 ? &sceneColorSlots[lastPresentSlot].colorGpu
534 : whiteTexture;
535 wgpu::BindGroup bg;
536 uint32_t offsets[3];
537 if (useClustered) {
538 wgpu::TextureView aoView_ =
539 aoReady ? aoView[(aoWriteIndex + 1) % 2] : (whiteTexture ? whiteTexture->view : wgpu::TextureView());
540 bg = makeMesh3DClusteredBindGroup(albedo, normal, env, height, depth, aoView_, d.clusteredUboOffset,
541 d.shadowUboOffset);
542 offsets[0] = d.clusteredUboOffset;
543 offsets[1] = d.shadowUboOffset;
544 pass.SetBindGroup(0, bg, 2, offsets);
545 } else {
546 bg = makeMeshBindGroup(albedo, normal, env, height, depth, sceneColor, d.frameUboOffset, d.shadowUboOffset,
547 d.pushUboOffset, d.skinBuffer);
548 offsets[0] = d.frameUboOffset;
549 offsets[1] = d.shadowUboOffset;
550 offsets[2] = d.pushUboOffset;
551 pass.SetBindGroup(0, bg, 3, offsets);
552 }
553
554 if (gpuMesh->indexBuffer) {
555 pass.SetVertexBuffer(0, gpuMesh->vertexBuffer, 0, uint64_t(gpuMesh->vertexCount) * gpuMesh->vertexStride);
556 const uint64_t indexBytes = gpuMesh->indexFormat == wgpu::IndexFormat::Uint16 ? 2u : 4u;
557 pass.SetIndexBuffer(gpuMesh->indexBuffer, gpuMesh->indexFormat, 0,
558 uint64_t(gpuMesh->indexCount) * indexBytes);
559 pass.DrawIndexed(gpuMesh->indexCount, 1, 0, 0, 0);
560 } else {
561 pass.SetVertexBuffer(0, gpuMesh->vertexBuffer, 0, uint64_t(gpuMesh->vertexCount) * gpuMesh->vertexStride);
562 pass.Draw(gpuMesh->vertexCount, 1, 0, 0);
563 }
564 }
565 mesh3dDraws.clear();
566}
567
568void Graphics::recordDirectionalShadowPasses(wgpu::CommandEncoder encoder) {
569 // 1. Shadow passes (CSM cascade layers).
570 if (shadowDepthArray) {
571 // Every cascade is sampled by receivers, including cascades with no
572 // caster draw this frame. Clear all layers while shadows are active so
573 // an empty cascade deterministically means fully lit (depth 1) instead
574 // of retaining undefined or previous-frame depth.
575 if (mesh3dShadows.active) {
576 for (int c = 0; c < ShadowConfig::kCascades; ++c) {
577 WGPUTextureViewDescriptor lvd{};
578 lvd.format = WGPUTextureFormat_Depth32Float;
579 lvd.dimension = WGPUTextureViewDimension_2D;
580 lvd.baseMipLevel = 0;
581 lvd.mipLevelCount = 1;
582 lvd.baseArrayLayer = static_cast<uint32_t>(c);
583 lvd.arrayLayerCount = 1;
584 wgpu::TextureView layerView =
585 shadowDepthArray->texture.CreateView(reinterpret_cast<const wgpu::TextureViewDescriptor*>(&lvd));
586 WGPURenderPassDepthStencilAttachment ds{};
587 ds.view = layerView.Get();
588 ds.depthClearValue = 1.f;
589 ds.depthLoadOp = WGPULoadOp_Clear;
590 ds.depthStoreOp = WGPUStoreOp_Store;
591 ds.stencilClearValue = 0;
592 ds.stencilLoadOp = WGPULoadOp_Undefined;
593 ds.stencilStoreOp = WGPUStoreOp_Undefined;
594 WGPURenderPassDescriptor rp{};
595 rp.depthStencilAttachment = &ds;
596 wgpu::RenderPassEncoder pass =
597 encoder.BeginRenderPass(reinterpret_cast<const wgpu::RenderPassDescriptor*>(&rp));
598 flushShadowPass(pass, c);
599 pass.End();
600 }
601 }
602 }
603}
604
605void Graphics::flushShadowPass(wgpu::RenderPassEncoder pass, int cascade) {
606 auto& uboArena = currentUboArena();
607 if (cascade < 0 || cascade >= ShadowConfig::kTotalLayers) return;
608 if (!mesh3dShadowPipeline) createShadowPipelines();
609 // Adaptive CSM: render into a top-left viewport matching the cascade's
610 // allocated map size; shaders remap UVs via cascadeUvScale.
611 float viewport = float(shadowMapSize > 0 ? shadowMapSize : ShadowConfig::kMapSize);
612 if (cascade >= 0 && cascade < ShadowConfig::kCascades && mesh3dShadows.active) {
613 viewport = float(std::max(1, mesh3dShadows.cascadeMapSizes[cascade]));
614 }
615 pass.SetViewport(0.f, 0.f, viewport, viewport, 0.f, 1.f);
616 pass.SetScissorRect(0, 0, uint32_t(viewport), uint32_t(viewport));
617 ensureUboArena(uboArena, uboArena.used + shadowCascadeDraws[cascade].size() * 256);
618 for (auto& d : shadowCascadeDraws[cascade]) {
619 auto* gpuMesh = static_cast<GpuMesh*>(d.mesh->gpuHandle);
620 if (!gpuMesh || !gpuMesh->vertexBuffer) continue;
621
622 pass.SetPipeline(d.alphaTest
623 ? (d.doubleSided ? mesh3dShadowAlphaPipeline : mesh3dShadowAlphaSingleSidedPipeline)
624 : (d.doubleSided ? mesh3dShadowPipeline : mesh3dShadowSingleSidedPipeline));
625
626 SkinPassUBO ubo;
627 ubo.mvp = d.mvp;
628 ubo.clip = d.lodFade;
629 if (d.mesh->hasGpuSkinning()) ubo.skinInfo.x = static_cast<float>(d.mesh->getSkinPaletteCount());
630 ubo.skinInfo.y = static_cast<float>(d.skinInfluenceLimit);
631 const auto skinBuffer = uploadSkinPalette(d.mesh);
632 uint32_t offset = uboArena.alloc(sizeof(SkinPassUBO), 256);
633 queue.WriteBuffer(uboArena.buffer, offset, &ubo, sizeof(ubo));
634
635 GpuTexture* albedo = gpuForTextureOrWhite(d.albedo);
636 WGPUBindGroupEntry entries[4]{};
637 entries[0].binding = 0;
638 entries[0].buffer = uboArena.buffer.Get();
639 entries[0].size = sizeof(SkinPassUBO);
640 entries[1].binding = 1;
641 entries[1].textureView = albedo->view.Get();
642 entries[2].binding = 2;
643 entries[2].sampler = albedo->sampler.Get();
644 entries[3].binding = 3;
645 entries[3].buffer = skinBuffer.Get();
646 entries[3].size = skinBuffer.GetSize();
647 WGPUBindGroupDescriptor bgd{};
648 bgd.layout = shadowSetLayout.Get();
649 bgd.entryCount = 4;
650 bgd.entries = entries;
651 wgpu::BindGroup bg = device.CreateBindGroup(reinterpret_cast<const wgpu::BindGroupDescriptor*>(&bgd));
652 uint32_t offsets[1] = {offset};
653 pass.SetBindGroup(0, bg, 1, offsets);
654
655 if (gpuMesh->indexBuffer) {
656 pass.SetVertexBuffer(0, gpuMesh->vertexBuffer, 0, uint64_t(gpuMesh->vertexCount) * gpuMesh->vertexStride);
657 const uint64_t indexBytes = gpuMesh->indexFormat == wgpu::IndexFormat::Uint16 ? 2u : 4u;
658 pass.SetIndexBuffer(gpuMesh->indexBuffer, gpuMesh->indexFormat, 0,
659 uint64_t(gpuMesh->indexCount) * indexBytes);
660 pass.DrawIndexed(gpuMesh->indexCount, 1, 0, 0, 0);
661 } else {
662 pass.SetVertexBuffer(0, gpuMesh->vertexBuffer, 0, uint64_t(gpuMesh->vertexCount) * gpuMesh->vertexStride);
663 pass.Draw(gpuMesh->vertexCount, 1, 0, 0);
664 }
665 }
666 shadowCascadeDraws[cascade].clear();
667}
668
669void Graphics::flushGbufferPass(wgpu::RenderPassEncoder pass) {
670 if (gbufferPassDraws.empty() || gbufferSlots.empty()) return;
671 if (!mesh3dGbufferPipeline) createGbufferPipelines();
672 auto& uboArena = currentUboArena();
673 ensureUboArena(uboArena, uboArena.used + gbufferPassDraws.size() * 4096);
674 for (auto& d : gbufferPassDraws) {
675 auto* gpuMesh = static_cast<GpuMesh*>(d.mesh->gpuHandle);
676 if (!gpuMesh || !gpuMesh->vertexBuffer) continue;
677
678 if (d.pbrDraw) {
679 drawPbrMesh(pass, WGPUTextureFormat_RGBA8Unorm, true, *d.pbrDraw, *gpuMesh);
680 continue;
681 }
682 pass.SetPipeline(d.alphaTest ? mesh3dGbufferAlphaPipeline : mesh3dGbufferPipeline);
683
684 SkinPassUBO ubo;
685 ubo.mvp = d.mvp;
686 ubo.model = d.model;
687 auto u6 = [](float value) { return uint32_t(std::lround(std::clamp(value, 0.f, 1.f) * 63.f)); };
688 // Phase B: tint RGB6 | rough7 | metal7 — bit-cast (exceeds float's 24-bit exact ints).
689 const uint32_t rough7 = uint32_t(std::lround(std::clamp(d.roughness, 0.f, 1.f) * 127.f));
690 const uint32_t metal7 = uint32_t(std::lround(std::clamp(d.metallic, 0.f, 1.f) * 127.f));
691 const uint32_t packedTint =
692 u6(d.tint.r) | (u6(d.tint.g) << 6) | (u6(d.tint.b) << 12) | (rough7 << 18) | (metal7 << 25);
693 auto motion12 = [](float value) {
694 return uint32_t(std::lround(std::clamp(value, -1.f, 1.f) * 2047.f)) + 2047u;
695 };
696 const uint32_t packedMotion = motion12(d.motion.x) | (motion12(d.motion.y) << 12);
697 ubo.clip = glm::vec4(d.nearZ, d.farZ, std::bit_cast<float>(packedTint), float(packedMotion));
698 if (d.mesh->hasGpuSkinning()) ubo.skinInfo.x = static_cast<float>(d.mesh->getSkinPaletteCount());
699 ubo.skinInfo.y = static_cast<float>(d.skinInfluenceLimit);
700 const auto skinBuffer = uploadSkinPalette(d.mesh);
701
702 uint32_t offset = uboArena.alloc(sizeof(SkinPassUBO), 256);
703 queue.WriteBuffer(uboArena.buffer, offset, &ubo, sizeof(ubo));
704
705 GpuTexture* albedo = gpuForTextureOrWhite(d.albedo);
706 WGPUBindGroupEntry entries[4]{};
707 entries[0].binding = 0;
708 entries[0].buffer = uboArena.buffer.Get();
709 entries[0].size = sizeof(SkinPassUBO);
710 entries[1].binding = 1;
711 entries[1].textureView = albedo->view.Get();
712 entries[2].binding = 2;
713 entries[2].sampler = albedo->sampler.Get();
714 entries[3].binding = 3;
715 entries[3].buffer = skinBuffer.Get();
716 entries[3].size = skinBuffer.GetSize();
717 WGPUBindGroupDescriptor bgd{};
718 bgd.layout = gbufferSetLayout.Get();
719 bgd.entryCount = 4;
720 bgd.entries = entries;
721 wgpu::BindGroup bg = device.CreateBindGroup(reinterpret_cast<const wgpu::BindGroupDescriptor*>(&bgd));
722 uint32_t offsets[1] = {offset};
723 pass.SetBindGroup(0, bg, 1, offsets);
724
725 if (gpuMesh->indexBuffer) {
726 pass.SetVertexBuffer(0, gpuMesh->vertexBuffer, 0, uint64_t(gpuMesh->vertexCount) * gpuMesh->vertexStride);
727 const uint64_t indexBytes = gpuMesh->indexFormat == wgpu::IndexFormat::Uint16 ? 2u : 4u;
728 pass.SetIndexBuffer(gpuMesh->indexBuffer, gpuMesh->indexFormat, 0,
729 uint64_t(gpuMesh->indexCount) * indexBytes);
730 pass.DrawIndexed(gpuMesh->indexCount, 1, 0, 0, 0);
731 } else {
732 pass.SetVertexBuffer(0, gpuMesh->vertexBuffer, 0, uint64_t(gpuMesh->vertexCount) * gpuMesh->vertexStride);
733 pass.Draw(gpuMesh->vertexCount, 1, 0, 0);
734 }
735 }
736 gbufferPassDraws.clear();
737 gbufferPassPending = false;
738}
739
740void Graphics::flushDecalPass(wgpu::RenderPassEncoder pass) {
741 if (decalPassDraws.empty() || gbufferSlots.empty()) {
742 decalPassPending = false;
743 return;
744 }
745 if (!decalPipeline) createDecalPipeline();
746 auto& uboArena = currentUboArena();
747 ensureUboArena(uboArena, uboArena.used + decalPassDraws.size() * 512);
748 GbufferSlot& gbuffer = gbufferSlots[lastGbufferSlot];
749 pass.SetPipeline(decalPipeline);
750 struct DecalUniforms {
751 glm::mat4 invViewProj;
752 glm::mat4 invModel;
753 glm::vec4 modelR0;
754 glm::vec4 modelR1;
755 glm::vec4 modelR2;
756 glm::vec4 uvRect;
757 glm::vec4 fadeParams;
758 glm::vec4 extraParams;
759 glm::vec4 surfaceParams;
760 glm::vec4 texel;
761 };
762 static_assert(sizeof(DecalUniforms) == 256);
763 for (const auto& draw : decalPassDraws) {
764 DecalUniforms uniforms{};
765 uniforms.invViewProj = glm::inverse(decalViewProj);
766 uniforms.invModel = glm::inverse(draw.model);
767 uniforms.modelR0 = draw.model[0];
768 uniforms.modelR1 = draw.model[1];
769 uniforms.modelR2 = draw.model[2];
770 uniforms.uvRect = draw.uvRect;
771 uniforms.fadeParams = draw.fadeParams;
772 uniforms.extraParams = draw.extraParams;
773 uniforms.surfaceParams = draw.surfaceParams;
774 uniforms.texel = glm::vec4(1.f / float(decalWidth), 1.f / float(decalHeight), 0.f, 0.f);
775 uint32_t offset = uboArena.alloc(256, 256);
776 queue.WriteBuffer(uboArena.buffer, offset, &uniforms, sizeof(uniforms));
777 GpuTexture* albedo = gpuForTextureOrWhite(draw.albedo);
778 GpuTexture* normal = gpuForTextureOrWhite(draw.normal);
779 GpuTexture* params = gpuForTextureOrWhite(draw.params);
780 WGPUBindGroupEntry entries[7]{};
781 entries[0].binding = 0;
782 entries[0].buffer = uboArena.buffer.Get();
783 entries[0].size = sizeof(uniforms);
784 entries[1].binding = 1;
785 entries[1].textureView = albedo->view.Get();
786 entries[2].binding = 2;
787 entries[2].textureView = normal->view.Get();
788 entries[3].binding = 3;
789 entries[3].textureView = params->view.Get();
790 entries[4].binding = 4;
791 entries[4].textureView = gbuffer.depthView.Get();
792 entries[5].binding = 5;
793 entries[5].textureView = gbuffer.normalView.Get();
794 entries[6].binding = 6;
795 entries[6].sampler = mainSampler.Get();
796 WGPUBindGroupDescriptor descriptor{};
797 descriptor.layout = decalSetLayout.Get();
798 descriptor.entryCount = 7;
799 descriptor.entries = entries;
800 wgpu::BindGroup group = device.CreateBindGroup(reinterpret_cast<const wgpu::BindGroupDescriptor*>(&descriptor));
801 uint32_t offsets[1] = {offset};
802 pass.SetBindGroup(0, group, 1, offsets);
803 pass.Draw(3, 1, 0, 0);
804 }
805 decalPassDraws.clear();
806 decalPassPending = false;
807 decalReady = true;
808 clearMeshBindGroupCache();
809}
810
811bool Graphics::flushGBufferPassInto(wgpu::CommandEncoder& encoder, bool runAo) {
812 if (!gbufferPassPending || gbufferSlots.empty()) return false;
813 lastGbufferSlot = currentFrameSlot();
814 GbufferSlot& slot = gbufferSlots[lastGbufferSlot];
815 WGPURenderPassColorAttachment colorAtts[5]{};
816 for (int i = 0; i < 5; ++i) {
817 colorAtts[i].depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
818 colorAtts[i].loadOp = WGPULoadOp_Clear;
819 colorAtts[i].storeOp = WGPUStoreOp_Store;
820 colorAtts[i].clearValue = {0.f, 0.f, 0.f, 1.f};
821 }
822 // Match Vulkan GBuffer clears: depthColor=1, pbrParams=(0,1,1,1).
823 colorAtts[1].clearValue = {1.f, 1.f, 1.f, 1.f};
824 colorAtts[3].clearValue = {0.f, 1.f, 1.f, 1.f};
825 colorAtts[0].view = slot.normalView.Get();
826 colorAtts[1].view = slot.depthColorView.Get();
827 colorAtts[2].view = slot.albedoView.Get();
828 colorAtts[3].view = slot.pbrParamsView.Get();
829 colorAtts[4].view = slot.emissiveView.Get();
830 WGPURenderPassDepthStencilAttachment ds{};
831 ds.view = slot.depthView.Get();
832 ds.depthClearValue = 1.f;
833 ds.depthLoadOp = WGPULoadOp_Clear;
834 ds.depthStoreOp = WGPUStoreOp_Store;
835 ds.stencilClearValue = 0;
836 ds.stencilLoadOp = WGPULoadOp_Undefined;
837 ds.stencilStoreOp = WGPUStoreOp_Undefined;
838 WGPURenderPassDescriptor rp{};
839 rp.colorAttachmentCount = 5;
840 rp.colorAttachments = colorAtts;
841 rp.depthStencilAttachment = &ds;
842 wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(reinterpret_cast<const wgpu::RenderPassDescriptor*>(&rp));
843 flushGbufferPass(pass);
844 pass.End();
845 gbufferPassPending = false;
846 gbufferDepthValid_ = true;
847
848 if (runAo) {
849 ensureAOResources(sceneColorWidth, sceneColorHeight);
850 if (aoPipeline && aoTex[0]) {
852 GbufferSlot& gslot = gbufferSlots[currentFrameSlot()];
853 struct AOUbo {
854 glm::vec4 params; // radius, power, nearZ, farZ
855 float intensity;
856 float invScale; // AO target size / depth size
857 float pad;
858 } aou;
859 aou.params = glm::vec4(0.05f, 1.1f, mesh3dNear, mesh3dFar);
860 aou.intensity = 1.0f;
861 aou.invScale = 0.5f;
862 aou.pad = 0.f;
863 queue.WriteBuffer(aoUbo, 0, &aou, sizeof(aou));
864 wgpu::BindGroup aoBg = makeAOBindGroup(gslot.depthView);
865
866 WGPURenderPassColorAttachment colorAtt{};
867 colorAtt.view = aoView[aoWriteIndex].Get();
868 colorAtt.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
869 colorAtt.loadOp = WGPULoadOp_Clear;
870 colorAtt.storeOp = WGPUStoreOp_Store;
871 colorAtt.clearValue = {1.f, 1.f, 1.f, 1.f};
872 WGPURenderPassDescriptor aoRp{};
873 aoRp.colorAttachmentCount = 1;
874 aoRp.colorAttachments = &colorAtt;
875 wgpu::RenderPassEncoder apass =
876 encoder.BeginRenderPass(reinterpret_cast<const wgpu::RenderPassDescriptor*>(&aoRp));
877 apass.SetPipeline(aoPipeline);
878 apass.SetBindGroup(0, aoBg, 0, nullptr);
879 apass.SetVertexBuffer(0, fullscreenQuadVb, 0, 4 * 32);
880 apass.SetIndexBuffer(fullscreenQuadIb, wgpu::IndexFormat::Uint32, 0, 24);
881 apass.DrawIndexed(6, 1, 0, 0, 0);
882 apass.End();
884 aoWriteIndex ^= 1;
885 }
886 }
887 return true;
888}
889
890void Graphics::submitPendingDeferredPasses() {
891 if ((!gbufferPassPending || gbufferSlots.empty()) && (!decalPassPending || decalSlots.empty())) return;
892 auto& uboArena = currentUboArena();
893 uboArena.reset();
894 ensureUboArena(uboArena, 4096 + (gbufferPassDraws.size() + decalPassDraws.size()) * 512);
895 wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
896 if (gbufferPassPending && !gbufferSlots.empty()) {
897 lastGbufferSlot = currentFrameSlot();
898 GbufferSlot& slot = gbufferSlots[lastGbufferSlot];
899 WGPURenderPassColorAttachment colors[5]{};
900 for (auto& color : colors) {
901 color.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
902 color.loadOp = WGPULoadOp_Clear;
903 color.storeOp = WGPUStoreOp_Store;
904 color.clearValue = {0.f, 0.f, 0.f, 0.f};
905 }
906 colors[1].clearValue = {1.f, 1.f, 1.f, 1.f};
907 colors[3].clearValue = {0.f, 1.f, 1.f, 1.f}; // metal=0, rough/ao/spec=1
908 colors[0].view = slot.normalView.Get();
909 colors[1].view = slot.depthColorView.Get();
910 colors[2].view = slot.albedoView.Get();
911 colors[3].view = slot.pbrParamsView.Get();
912 colors[4].view = slot.emissiveView.Get();
913 WGPURenderPassDepthStencilAttachment depth{};
914 depth.view = slot.depthView.Get();
915 depth.depthClearValue = 1.f;
916 depth.depthLoadOp = WGPULoadOp_Clear;
917 depth.depthStoreOp = WGPUStoreOp_Store;
918 depth.stencilLoadOp = WGPULoadOp_Undefined;
919 depth.stencilStoreOp = WGPUStoreOp_Undefined;
920 WGPURenderPassDescriptor descriptor{};
921 descriptor.colorAttachmentCount = 5;
922 descriptor.colorAttachments = colors;
923 descriptor.depthStencilAttachment = &depth;
924 wgpu::RenderPassEncoder pass =
925 encoder.BeginRenderPass(reinterpret_cast<const wgpu::RenderPassDescriptor*>(&descriptor));
926 flushGbufferPass(pass);
927 pass.End();
928 gbufferDepthValid_ = true;
929 }
930 if (decalPassPending && !decalSlots.empty() && !gbufferSlots.empty()) {
931 lastDecalSlot = currentFrameSlot();
932 DecalSlot& slot = decalSlots[lastDecalSlot];
933 WGPURenderPassColorAttachment colors[3]{};
934 for (auto& color : colors) {
935 color.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
936 color.loadOp = WGPULoadOp_Clear;
937 color.storeOp = WGPUStoreOp_Store;
938 color.clearValue = {0.f, 0.f, 0.f, 0.f};
939 }
940 colors[0].view = slot.albedoView.Get();
941 colors[1].view = slot.normalView.Get();
942 colors[2].view = slot.paramsView.Get();
943 WGPURenderPassDescriptor descriptor{};
944 descriptor.colorAttachmentCount = 3;
945 descriptor.colorAttachments = colors;
946 wgpu::RenderPassEncoder pass =
947 encoder.BeginRenderPass(reinterpret_cast<const wgpu::RenderPassDescriptor*>(&descriptor));
948 flushDecalPass(pass);
949 pass.End();
950 }
951 wgpu::CommandBuffer command = encoder.Finish();
952 queue.Submit(1, &command);
953}
954
955
956// ---------------------------------------------------------------------------
957// Present
958// ---------------------------------------------------------------------------
959
960
961} // namespace eve::graphics::webgpu
std::string descriptor
const std::string & s
std::unordered_map< std::string, QuestRuntime > entries
float aoStrength
graphics::Texture * albedo
std::int32_t c
float blend
std::uint32_t height
std::uint32_t width
size_t offset
std::vector< RecipeParam > params
MeshVfxBatchedDraw draw
bool doubleSided
Texture * normal
ScriptMotionSinkStore< ScriptMotionSink< MotionColorTraits > > colors
std::shared_ptr< OceanGpuSpectrum > gpu
OceanSprayView view
Object::Ptr value
float d
int detail
std::string slot
Local-probe upload record kept off the high-fanout IGraphics3D header.
AbilityActivationGroup group
float size
Definition TreeMesh.cpp:158
std::uint32_t depth
std::unique_ptr< RenderControl > renderControl_
Definition Graphics.h:2340
static constexpr uint32_t kPushConstantBytes
Definition Shader.h:45
bool supportsFiveTargetGBuffer() const
True when the granted device limit supports five RGBA8 GBuffer targets.
void pushValidationScope() override
Pushes validation scope.
Definition Graphics.cpp:438
void setMsaaSamples(int samples) override
Sets the msaa samples.
void popValidationScope() override
Pops validation scope.
Definition Graphics.cpp:446
wgpu::Sampler createLinearSampler(wgpu::Device &dev)
Internal WebGPU pipeline/descriptor helper; render-thread only.
WGPUStringView sv(const char *s)
Build a WGPUStringView from a C string (null-safe, length auto-computed).
Definition wgpu_types.h:20
eve::BlendMode BlendMode
Compatibility alias for the shared 2D blend mode.
Definition BlendMode.h:8
WidgetDesc viewport(std::string id, float width, float height)
Viewport.
Definition Widget.cpp:523
std::array< glm::vec4, kMaxDiffuseVolumeProbes > diffuseVolumePosition
Definition Light.h:179
std::array< glm::vec4, kMaxDiffuseVolumeProbes > diffuseVolumeExtent
Definition Light.h:180
std::array< glm::vec4, kMaxDiffuseVolumeProbes *9 > diffuseVolumeSh
Definition Light.h:181
static constexpr int kMaxLights
Definition Light.h:172
Light3DGpu lights[kMaxLights]
Definition Light.h:174
std::array< glm::vec4, 9 > diffuseProbeSh
Definition Light.h:176
static constexpr int kCascades
Definition Shadow.h:24
static constexpr int kTotalLayers
Definition Shadow.h:27
int resolvedAtlasMapSize() const
Atlas edge length actually allocated for GPU shadow maps.
Definition Shadow.cpp:242
static ShadowSchemeSettings & current()
Borrow the process-wide settings object.
Definition Shadow.cpp:216
int cascadeMapSizes[ShadowConfig::kCascades]
Per-cascade render viewport edge length (texels); drives shadow pass viewport.
Definition Shadow.h:66
static TextureSampler linearMipmap()
Trilinear (linear + linear mips). Caller should create the texture with generateMipmaps.
glm::mat4 invViewProj
uint32_t pad[2]
glm::vec4 color