From 3317705d9c60c84827264784f1cd15112b6c0c5c Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Thu, 5 Nov 2026 22:31:04 +0100 Subject: [PATCH] Fixing depth clashing and struct padding --- res/shaders/UIPass.slang | 9 +++--- src/Engine/Graphics/RenderPass/UIPass.cpp | 36 +++++++++++++++++++---- src/Engine/Graphics/RenderPass/UIPass.h | 17 +++++++++-- src/Engine/UI/Element/Text.h | 5 +++- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/res/shaders/UIPass.slang b/res/shaders/UIPass.slang index 06cad4f..30da771 100644 --- a/res/shaders/UIPass.slang +++ b/res/shaders/UIPass.slang @@ -4,9 +4,7 @@ import Common; // CPU side by UIPass as SE_DESCRIPTOR_TYPE_STORAGE_BUFFER). We read it here as a plain SSBO so // every value (x/y/w/h, color, opacity, uvOffset/uvSize, textureIndex) is EXACT — not barycentric // interpolated the way the old code passed it as a STYLE vertex attribute (which corrupted the -// transparency and shifted glyphs). The C++ RenderElementStyle layout (glm: vec3=12B, vec2=8B) -// is byte-identical to this slang struct (float3=12B, float2=8B); Slang's default SSBO packing -// (offsets 0,4,8,12,16,28,32,36,40,48,52) matches it 1:1. +// transparency and shifted glyphs). struct RenderElementStyle { float x; @@ -63,7 +61,10 @@ VertexOutput vertexMain(uint vertexId : SV_VertexID, uint instanceId : SV_Instan coordinates[vertexId].x * pViewParams.invScreenDimensions.x * 2.0 - 1.0, 1.0 - coordinates[vertexId].y * pViewParams.invScreenDimensions.y * 2.0 ); - output.position = float4(ndc, 1 - style.z, 1.0); + // style.z is ALREADY the final depth: the CPU maps the tree depth (render.level) into [0,1] + // and REVERSES it for the depth buffer's SE_COMPARE_OP_GREATER (nearer = LARGER). Write it + // verbatim — do NOT negate it again (the old `1 - style.z` double-inverted the depth). + output.position = float4(ndc, style.z, 1.0); output.texCoords = coordinates[vertexId].zw; // quad-local uv, interpolated per-fragment output.instanceId = instanceId; // uint varying: exact per-fragment (not interpolated) return output; diff --git a/src/Engine/Graphics/RenderPass/UIPass.cpp b/src/Engine/Graphics/RenderPass/UIPass.cpp index 810a627..3b07407 100644 --- a/src/Engine/Graphics/RenderPass/UIPass.cpp +++ b/src/Engine/Graphics/RenderPass/UIPass.cpp @@ -42,10 +42,32 @@ void UIPass::beginFrame(const Component::Camera& cam, const Component::Transform renderElements = system->render(); usedTextures.clear(); elements.clear(); + // Depth buffer uses SE_COMPARE_OP_GREATER (Vulkan >=): nearer = LARGER depth value. So the + // tree depth (render.level, 0 = root/nearest) is mapped onto [0,1] and REVERSED — level 0 + // gets a depth nearest 1.0 (closest), and each deeper level recedes by 1/(MAX+1) toward 0. + // The shader writes this value verbatim as SV_Position.z (see UIPass.slang), so we must NOT + // re-negate it there. + // + // CRITICAL: every level must map to a depth STRICTLY greater than 0.0. The engine's global + // compare op is GREATER (>=, see Vulkan/Enums.cpp), and the depth buffer is cleared to 0 + // (createRenderPass). If any element's depth were exactly 0.0, its 0.0 >= 0.0 test would + // fail and the element would be culled. The clamp below is therefore a MAXIMUM (saturate to + // 0 at level >= MAX) — do NOT "fix" it into a minimum (e.g. max(depth, kDepthStep)); that + // would push deep levels to exactly kDepthStep while their shallower ancestors are at + // 2*kDepthStep or less, inverting the order and making deep text vanish under its parent. + // For level < MAX the depth is always >= kDepthStep > 0 (kMaxTreeDepth is a multiple of 32, + // so the float multiply is exact and 1 - (MAX-1)*step == step > 0), so the 0.0 saturation is + // only ever reached for level >= MAX — at which point correct ordering is already lost anyway + // (all such levels saturate to the same 0.0, which is "farther" than any shallower level: + // correct relative order, and they never alias with a shallower level because those are > 0). + constexpr float kMaxTreeDepth = 64.0f; + const float kDepthStep = 1.0f / (kMaxTreeDepth + 1.0f); for (auto& render : renderElements) { float x = render.position.x; float y = render.position.y; - //todo: convert using actual tree depth + // Reversed, clamped tree-depth -> [0,1] (nearer/larger = closer to the camera). + const float rawDepth = 1.0f - (float)render.level * kDepthStep; + const float depth = rawDepth > 0.0f ? rawDepth : 0.0f; uint32 textureIndex = std::numeric_limits::max(); if (render.backgroundTexture != nullptr) { textureIndex = (uint32)usedTextures.size(); @@ -57,7 +79,7 @@ void UIPass::beginFrame(const Component::Camera& cam, const Component::Transform .w = render.dimensions.x, .h = render.dimensions.y, .color = render.backgroundColor, - .z = render.level / 100.f, + .z = depth, // already reversed+clamped tree depth (see above); shader writes it verbatim .textureIndex = textureIndex, .uvOffset = render.uvOffset, .uvSize = render.uvSize, @@ -103,9 +125,9 @@ void UIPass::createRenderPass() { }; depthBuffer = graphics->createTexture2D(depthBufferInfo); - depthAttachment = - Gfx::RenderTargetAttachment(depthBuffer->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, - Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE); + depthAttachment = Gfx::RenderTargetAttachment(depthBuffer->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, + Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, + Gfx::SE_ATTACHMENT_STORE_OP_STORE); depthAttachment.clear.depthStencil.depth = 0; colorAttachment = Gfx::RenderTargetAttachment(viewport, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, @@ -171,6 +193,10 @@ void UIPass::createRenderPass() { { .cullMode = Gfx::SE_CULL_MODE_NONE, }, + .depthStencilState = + { + .depthCompareOp = Gfx::SE_COMPARE_OP_GREATER_OR_EQUAL, + }, .colorBlend = { diff --git a/src/Engine/Graphics/RenderPass/UIPass.h b/src/Engine/Graphics/RenderPass/UIPass.h index 9992c55..fc5425f 100644 --- a/src/Engine/Graphics/RenderPass/UIPass.h +++ b/src/Engine/Graphics/RenderPass/UIPass.h @@ -32,14 +32,27 @@ class UIPass : public RenderPass { float y; float w; float h; - Vector color; + // Layout is locked 1:1 to the Slang std430 struct (UIPass.slang::RenderElementStyle). + // Offsets (both sides): x/y/w/h @0/4/8/12, color @16 (12B, 16B-aligned in std430), + // opacity @28, z @32, textureIndex @36, uvOffset @40, uvSize @48, pad @56, sizeof=64. + // + // The trailing `pad` (Vector2) is LOAD-BEARING: the Slang std430 struct is 16B-aligned + // (because of the float3), so its stride is sizeof=64. On platforms where the C++ side + // aligns to 4 (alignof(glm::vec3)==4), the natural sizeof would be 56 and the CPU would + // pack elements at 56B — desyncing element 1's data against the GPU's 64B stride and + // corrupting every field from uvSize onward. `pad` forces sizeof==64 on ALL platforms. + // + // Do NOT reorder, remove, or shrink `pad`, and do NOT insert fields between existing ones + // without updating BOTH this struct AND the Slang struct AND re-verifying the offsets + // (slangc + spirv-dis, and a static_assert on the C++ side if available). + Vector color; // glm::vec3 (12B; std430 aligns it to 16B, C++ aligns per ABI) float opacity = 1; float z = 0; uint32 textureIndex = std::numeric_limits::max(); // UV rect for textured quads (glyphs: their rect in the font atlas); unused for untextured quads Vector2 uvOffset = Vector2(0); Vector2 uvSize = Vector2(1); - Vector2 pad; + Vector2 pad; // do not remove — see layout comment above }; Gfx::RenderTargetAttachment colorAttachment; diff --git a/src/Engine/UI/Element/Text.h b/src/Engine/UI/Element/Text.h index 7f12ff0..d7b28e0 100644 --- a/src/Engine/UI/Element/Text.h +++ b/src/Engine/UI/Element/Text.h @@ -93,7 +93,10 @@ class Text : public Element { .position = pos + Vector2(g.position.x, g.position.y), .dimensions = Vector2(g.dimensions.x, g.dimensions.y), .z = style.z, - .level = level, + // level+1: a glyph must sit strictly in FRONT of its parent's own quad + // (same render.level => same depth value => GREATER-or-EQUAL test fails + // against the parent's already-written depth, and the glyph is culled). + .level = level + 1, .element = this, .uvOffset = g.uvOffset, .uvSize = g.uvSize,