28#include "common/config.h"
46#include <glm/gtc/constants.hpp>
51void Graphics::createSceneColorResources(
int width,
int height) {
56 const uint32_t want = 1u;
57 if (sceneColorWidth ==
width && sceneColorHeight ==
height && sceneColorSamples == want && !sceneColorSlots.empty())
60 destroySceneColorResources();
61 sceneColorWidth =
width;
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;
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};
80 cd.format = sceneColorFormat;
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();
86 if (sceneColorSamples > 1) {
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();
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();
113 slot.colorGpu.view =
slot.colorView;
119 slot.colorTex.gpuHandle = &
slot.colorGpu;
122 slot.colorTex.mipmapCount = 1;
124 sceneColorTexture = &sceneColorSlots[0].colorTex;
128 if (samplesChanged) {
129 mesh3dPipelines = {};
131 mesh3dTransparentPipeline = {};
132 mesh3dClusteredPipeline = {};
133 voxelRectPipeline = {};
135 destroyDeferredLightingResources();
137 if (samplesChanged && gpuDrivenCullPipeline_) {
141 gpuDrivenCullPipeline_ = {};
142 gpuDrivenRenderPipeline_ = {};
143 gpuDrivenCanvasPipeline_ = {};
144 gpuDrivenComputePipelineLayout_ = {};
145 gpuDrivenRenderPipelineLayout_ = {};
146 gpuDrivenComputeSetLayout_ = {};
147 gpuDrivenRenderSetLayout_ = {};
148 gpuDrivenComputeBindGroup_ = {};
149 gpuDrivenRenderBindGroup_ = {};
153void Graphics::destroySceneColorResources() {
154 sceneColorSlots.clear();
155 sceneColorTexture =
nullptr;
156 sceneColorHistoryValid =
false;
161 if (!initialized || sceneColorWidth <= 0 || sceneColorHeight <= 0)
return;
164 destroySceneColorResources();
165 createSceneColorResources(sceneColorWidth, sceneColorHeight);
168void Graphics::createShadowResources() {
171 if (shadowDepthArray) {
172 if (shadowMapSize == wantSize)
return;
173 destroyShadowResources();
175 shadowMapSize = wantSize;
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),
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));
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));
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;
214void Graphics::destroyShadowResources() {
215 shadowDepthArray =
nullptr;
219void Graphics::createGbufferResources(
int width,
int height) {
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;
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};
233 td.format = WGPUTextureFormat_RGBA8Unorm;
234 td.mipLevelCount = 1;
235 td.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc;
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};
242 dd.format = WGPUTextureFormat_Depth32Float;
243 dd.mipLevelCount = 1;
245 dd.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment;
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;
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();
277 slot.normalGpu.texture =
slot.normal;
278 slot.normalGpu.view =
slot.normalView;
280 slot.depthColorGpu.texture =
slot.depthColor;
281 slot.depthColorGpu.view =
slot.depthColorView;
283 slot.albedoGpu.texture =
slot.albedo;
284 slot.albedoGpu.view =
slot.albedoView;
286 slot.pbrParamsGpu.texture =
slot.pbrParams;
287 slot.pbrParamsGpu.view =
slot.pbrParamsView;
289 slot.emissiveGpu.texture =
slot.emissive;
290 slot.emissiveGpu.view =
slot.emissiveView;
293 slot.depthGpu.view =
slot.depthView;
296 slot.normalTex.gpuHandle = &
slot.normalGpu;
299 slot.depthColorTex.gpuHandle = &
slot.depthColorGpu;
302 slot.albedoTex.gpuHandle = &
slot.albedoGpu;
305 slot.pbrParamsTex.gpuHandle = &
slot.pbrParamsGpu;
308 slot.emissiveTex.gpuHandle = &
slot.emissiveGpu;
311 slot.depthTex.gpuHandle = &
slot.depthGpu;
317void Graphics::destroyGbufferResources() {
318 gbufferSlots.clear();
319 gbufferDepthValid_ =
false;
322void Graphics::createDecalResources(
int width,
int height) {
324 if (decalWidth ==
width && decalHeight ==
height && !decalSlots.empty())
return;
325 destroyDecalResources();
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};
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();
347void Graphics::destroyDecalResources() {
358void Graphics::flushMesh3D(wgpu::RenderPassEncoder pass, WGPUTextureFormat format,
bool canvasTarget) {
359 if (mesh3dDraws.empty())
return;
361 if (mesh3dClusteredActive && !canvasTarget && !mesh3dClusteredPipeline) {
362 for (
const auto&
d : mesh3dDraws) {
363 const bool customShader =
d.shader &&
d.shader->gpuHandle;
365 !
d.mesh->hasGpuSkinning()) {
366 createMesh3DClusteredPipeline();
372 auto& uboArena = currentUboArena();
373 ensureUboArena(uboArena, uboArena.used + mesh3dDraws.size() * 10240);
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);
383 if (
d.shader &&
d.shader->pushConstantSize() > 0)
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;
395 cubo.lightColor = glm::vec4(glm::vec3(mesh3dClustered.
primaryColor), mesh3dEnvIntensity);
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));
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);
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);
422 queue.WriteBuffer(uboArena.buffer,
d.clusteredUboOffset, &cubo,
sizeof(cubo));
426 ubo.mvp = mesh3dViewProj *
d.model;
428 ubo.lightDir = glm::vec4(glm::vec3(mesh3dLighting.
lights[0].
posRadius),
float(mesh3dLighting.
count));
431 ubo.cameraPos = glm::vec4(mesh3dCameraPos, mesh3dRoughness);
432 ubo.ambient = glm::vec4(glm::vec3(mesh3dLighting.
ambient), mesh3dMetallic);
445 ubo.lodFade =
d.lodFade;
446 float surfaceCode = float(
int(
d.surfaceMode));
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;
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);
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);
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);
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]);
481 ubo.parallax = glm::vec4(pc[3], pc[4], pc[5], pc[7]);
482 ubo.clipInfo.z = pc[6];
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(),
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));
497 for (
auto&
d : mesh3dDraws) {
498 auto* gpuMesh =
static_cast<GpuMesh*
>(
d.mesh->gpuHandle);
499 if (!gpuMesh || !gpuMesh->vertexBuffer)
continue;
503 const bool depthWrite = !transparent ||
d.depthWrite;
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;
515 pipe = gs->mesh3dPipeline;
518 if (!pipe) pipe = getMesh3DPipeline(
blend, depthWrite,
d.doubleSided, canvasTarget);
519 const bool useClustered = !canvasTarget && !customShader && !
d.doubleSided && mesh3dClusteredActive &&
521 d.clusteredUboOffset && !
d.mesh->hasGpuSkinning();
522 if (useClustered) pipe = mesh3dClusteredPipeline;
524 pass.SetPipeline(pipe);
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
538 wgpu::TextureView aoView_ =
539 aoReady ? aoView[(aoWriteIndex + 1) % 2] : (whiteTexture ? whiteTexture->
view : wgpu::TextureView());
542 offsets[0] =
d.clusteredUboOffset;
543 offsets[1] =
d.shadowUboOffset;
544 pass.SetBindGroup(0, bg, 2, offsets);
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);
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);
561 pass.SetVertexBuffer(0, gpuMesh->vertexBuffer, 0, uint64_t(gpuMesh->vertexCount) * gpuMesh->vertexStride);
562 pass.Draw(gpuMesh->vertexCount, 1, 0, 0);
568void Graphics::recordDirectionalShadowPasses(wgpu::CommandEncoder encoder) {
570 if (shadowDepthArray) {
575 if (mesh3dShadows.
active) {
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);
605void Graphics::flushShadowPass(wgpu::RenderPassEncoder pass,
int cascade) {
606 auto& uboArena = currentUboArena();
608 if (!mesh3dShadowPipeline) createShadowPipelines();
611 float viewport = float(shadowMapSize > 0 ? shadowMapSize : ShadowConfig::kMapSize);
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;
622 pass.SetPipeline(
d.alphaTest
623 ? (
d.doubleSided ? mesh3dShadowAlphaPipeline : mesh3dShadowAlphaSingleSidedPipeline)
624 : (
d.
doubleSided ? mesh3dShadowPipeline : mesh3dShadowSingleSidedPipeline));
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));
635 GpuTexture*
albedo = gpuForTextureOrWhite(
d.albedo);
636 WGPUBindGroupEntry
entries[4]{};
638 entries[0].buffer = uboArena.buffer.Get();
639 entries[0].size =
sizeof(SkinPassUBO);
645 entries[3].buffer = skinBuffer.Get();
646 entries[3].size = skinBuffer.GetSize();
647 WGPUBindGroupDescriptor bgd{};
648 bgd.layout = shadowSetLayout.Get();
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);
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);
662 pass.SetVertexBuffer(0, gpuMesh->vertexBuffer, 0, uint64_t(gpuMesh->vertexCount) * gpuMesh->vertexStride);
663 pass.Draw(gpuMesh->vertexCount, 1, 0, 0);
666 shadowCascadeDraws[cascade].clear();
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;
679 drawPbrMesh(pass, WGPUTextureFormat_RGBA8Unorm,
true, *
d.pbrDraw, *gpuMesh);
682 pass.SetPipeline(
d.alphaTest ? mesh3dGbufferAlphaPipeline : mesh3dGbufferPipeline);
687 auto u6 = [](
float value) {
return uint32_t(std::lround(std::clamp(
value, 0.f, 1.f) * 63.f)); };
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;
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);
702 uint32_t
offset = uboArena.alloc(
sizeof(SkinPassUBO), 256);
703 queue.WriteBuffer(uboArena.buffer,
offset, &ubo,
sizeof(ubo));
705 GpuTexture*
albedo = gpuForTextureOrWhite(
d.albedo);
706 WGPUBindGroupEntry
entries[4]{};
708 entries[0].buffer = uboArena.buffer.Get();
709 entries[0].size =
sizeof(SkinPassUBO);
715 entries[3].buffer = skinBuffer.Get();
716 entries[3].size = skinBuffer.GetSize();
717 WGPUBindGroupDescriptor bgd{};
718 bgd.layout = gbufferSetLayout.Get();
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);
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);
732 pass.SetVertexBuffer(0, gpuMesh->vertexBuffer, 0, uint64_t(gpuMesh->vertexCount) * gpuMesh->vertexStride);
733 pass.Draw(gpuMesh->vertexCount, 1, 0, 0);
736 gbufferPassDraws.clear();
737 gbufferPassPending =
false;
740void Graphics::flushDecalPass(wgpu::RenderPassEncoder pass) {
741 if (decalPassDraws.empty() || gbufferSlots.empty()) {
742 decalPassPending =
false;
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 {
757 glm::vec4 fadeParams;
758 glm::vec4 extraParams;
759 glm::vec4 surfaceParams;
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]{};
782 entries[0].buffer = uboArena.buffer.Get();
783 entries[0].size =
sizeof(uniforms);
791 entries[4].textureView = gbuffer.depthView.Get();
793 entries[5].textureView = gbuffer.normalView.Get();
795 entries[6].sampler = mainSampler.Get();
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);
805 decalPassDraws.clear();
806 decalPassPending =
false;
808 clearMeshBindGroupCache();
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};
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);
845 gbufferPassPending =
false;
846 gbufferDepthValid_ =
true;
849 ensureAOResources(sceneColorWidth, sceneColorHeight);
850 if (aoPipeline && aoTex[0]) {
852 GbufferSlot& gslot = gbufferSlots[currentFrameSlot()];
859 aou.params = glm::vec4(0.05f, 1.1f, mesh3dNear, mesh3dFar);
860 aou.intensity = 1.0f;
863 queue.WriteBuffer(aoUbo, 0, &aou,
sizeof(aou));
864 wgpu::BindGroup aoBg = makeAOBindGroup(gslot.depthView);
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);
890void Graphics::submitPendingDeferredPasses() {
891 if ((!gbufferPassPending || gbufferSlots.empty()) && (!decalPassPending || decalSlots.empty()))
return;
892 auto& uboArena = currentUboArena();
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]{};
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};
906 colors[1].clearValue = {1.f, 1.f, 1.f, 1.f};
907 colors[3].clearValue = {0.f, 1.f, 1.f, 1.f};
913 WGPURenderPassDepthStencilAttachment
depth{};
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;
924 wgpu::RenderPassEncoder pass =
925 encoder.BeginRenderPass(
reinterpret_cast<const wgpu::RenderPassDescriptor*
>(&
descriptor));
926 flushGbufferPass(pass);
928 gbufferDepthValid_ =
true;
930 if (decalPassPending && !decalSlots.empty() && !gbufferSlots.empty()) {
931 lastDecalSlot = currentFrameSlot();
932 DecalSlot&
slot = decalSlots[lastDecalSlot];
933 WGPURenderPassColorAttachment
colors[3]{};
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};
946 wgpu::RenderPassEncoder pass =
947 encoder.BeginRenderPass(
reinterpret_cast<const wgpu::RenderPassDescriptor*
>(&
descriptor));
948 flushDecalPass(pass);
951 wgpu::CommandBuffer command = encoder.Finish();
952 queue.Submit(1, &command);
std::unordered_map< std::string, QuestRuntime > entries
graphics::Texture * albedo
ScriptMotionSinkStore< ScriptMotionSink< MotionColorTraits > > colors
std::shared_ptr< OceanGpuSpectrum > gpu
Local-probe upload record kept off the high-fanout IGraphics3D header.
AbilityActivationGroup group
std::unique_ptr< RenderControl > renderControl_
static constexpr uint32_t kPushConstantBytes
bool supportsFiveTargetGBuffer() const
True when the granted device limit supports five RGBA8 GBuffer targets.
void pushValidationScope() override
Pushes validation scope.
void setMsaaSamples(int samples) override
Sets the msaa samples.
void popValidationScope() override
Pops validation scope.
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).
eve::BlendMode BlendMode
Compatibility alias for the shared 2D blend mode.
WidgetDesc viewport(std::string id, float width, float height)
Viewport.
std::array< glm::vec4, kMaxDiffuseVolumeProbes > diffuseVolumePosition
std::array< glm::vec4, kMaxDiffuseVolumeProbes > diffuseVolumeExtent
bool diffuseVolumeTrilinearCell
std::array< glm::vec4, kMaxDiffuseVolumeProbes *9 > diffuseVolumeSh
static constexpr int kMaxLights
Light3DGpu lights[kMaxLights]
bool diffuseProbeShEnabled
int diffuseVolumeProbeCount
std::array< glm::vec4, 9 > diffuseProbeSh
static constexpr int kMaxProbes
static constexpr int kCascades
static constexpr int kTotalLayers
int resolvedAtlasMapSize() const
Atlas edge length actually allocated for GPU shadow maps.
static ShadowSchemeSettings & current()
Borrow the process-wide settings object.
int cascadeMapSizes[ShadowConfig::kCascades]
Per-cascade render viewport edge length (texels); drives shadow pass viewport.
static TextureSampler linearMipmap()
Trilinear (linear + linear mips). Caller should create the texture with generateMipmaps.