Fixing depth clashing and struct padding

This commit is contained in:
Dynamitos committed 2026-11-05 22:31:04 +01:00
1 parent ff2e4e642d
commit 3317705d9c
4 files changed
+55 -12

No files matched your search

+31 -5
View File
@@ -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<uint32>::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 =
{
+15 -2
View File
@@ -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<uint32>::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;
+4 -1
View File
@@ -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,