From ae2ce61e9a6a7b21ed7a12b82a97192c79f3b73b Mon Sep 17 00:00:00 2001 From: Hirek193 Date: Mon, 30 Nov 2026 20:09:18 +0100 Subject: [PATCH] fix(vulkan): stickers on vehicles no longer vanish behind the body seen at an angle With the depth prepass of the scene, the colour pass of the alpha tested materials (default, normalmap, pbr, ...) doesn't write the depth the prepass drew (keepdepth) and tested it without a bias. The prepass draws everything a little farther (constant -1, slope -1), so a surface lying up to a pixel's worth of the slope behind the front one passed as well, and the one drawn last covered the other. Seen from the side a few metres away that is more than a millimetre: the warning stickers 1 mm off the buffer beam of the EP07 (303e) vanished under the beam, drawn after them. Those draws now test their depth with the bias of the prepass, nearer by two units and 1/16 of the slope (the position is invariant in both programs). The colour pass sets its bias in apply_draw_state() for the kept draws and the decals together; the depth drawn inside the colour pass holds its own. The OpenGL 4.6 renderer writes the depth in its colour pass and isn't affected. No asset changes. --- rendering/vulkan14/vulkan14context.h | 8 ++- rendering/vulkan14/vulkan14frame.cpp | 3 ++ rendering/vulkan14/vulkan14indirect.cpp | 13 ++++- rendering/vulkan14/vulkan14scene.cpp | 71 +++++++++++++++++++++---- 4 files changed, 82 insertions(+), 13 deletions(-) diff --git a/rendering/vulkan14/vulkan14context.h b/rendering/vulkan14/vulkan14context.h index 26010523c..b676f7d39 100644 --- a/rendering/vulkan14/vulkan14context.h +++ b/rendering/vulkan14/vulkan14context.h @@ -796,6 +796,7 @@ struct vulkan14_renderer::draw_state VkCullModeFlags cull{VK_CULL_MODE_FLAG_BITS_MAX_ENUM}; int depthtest{-1}; int depthwrite{-1}; + int depthBias{-1}; // the bias of the colour pass apply_draw_state() set (colourDepthBias()), -1 not known std::uint32_t pushed_draw{~0u}; // the state of the walk (setup_drawing() and the glare of the opengl renderer) bool blending{false}; @@ -805,9 +806,14 @@ struct vulkan14_renderer::draw_state // the colour pass of the opaque scene after its depth (render_scene_depth()), keepdepth: the draws whose depth the prepass drew with // the alpha test (prepassed, set by walk_material()) find it in the depth buffer and don't write it again. without the write the // depth is tested before their fragment shaders, which discard, and the hidden leaves aren't lit. the solid surfaces write theirs - // as they did, so what's in front of what stays as it was + // as they did. what's in front of what among the draws which don't write it is kept by the bias they test it with + // (colourDepthBias()) bool keepdepth{false}; bool prepassed{false}; + // a decal drawn pulled towards the camera, over the surface it lies on (walk_decal()) + bool decal{false}; + // the bias is the one of the depth drawn inside the colour pass (draw_indirect_depth()), apply_draw_state() leaves it as it is + bool biasHeld{false}; // the pipelines and the textures a draw needs first may be made by the background threads, the draw left out till then (the walks // of the scene, gfx.vulkan.asyncresources); the draws outside of them (the cab, the sky, the particles) wait for what they need bool async{false}; diff --git a/rendering/vulkan14/vulkan14frame.cpp b/rendering/vulkan14/vulkan14frame.cpp index 1cd33df8b..e6682fd5d 100644 --- a/rendering/vulkan14/vulkan14frame.cpp +++ b/rendering/vulkan14/vulkan14frame.cpp @@ -1195,6 +1195,9 @@ void vulkan14_renderer::start_draws(vk14::target_formats const &Targets, int con draw.cull = VK_CULL_MODE_FLAG_BITS_MAX_ENUM; draw.depthtest = -1; draw.depthwrite = -1; + draw.depthBias = -1; + draw.decal = false; + draw.biasHeld = false; draw.pushed_draw = ~0u; draw.block = ~0u; draw.cullface = true; diff --git a/rendering/vulkan14/vulkan14indirect.cpp b/rendering/vulkan14/vulkan14indirect.cpp index d47428fe7..236e7e542 100644 --- a/rendering/vulkan14/vulkan14indirect.cpp +++ b/rendering/vulkan14/vulkan14indirect.cpp @@ -257,6 +257,8 @@ void vulkan14_renderer::resume_rendering() auto const cullface{draw.cullface}; auto const glare{draw.glare}; auto const depthmask_off{draw.depthmask_off}; + auto const decal{draw.decal}; + auto const biasHeld{draw.biasHeld}; auto *program{draw.program}; auto const textures{draw.textures}; start_draws(*draw.targets, static_cast(draw.extent.width), static_cast(draw.extent.height), set, shadows); @@ -264,11 +266,14 @@ void vulkan14_renderer::resume_rendering() draw.cullface = cullface; draw.glare = glare; draw.depthmask_off = depthmask_off; + draw.decal = decal; + draw.biasHeld = biasHeld; draw.program = program; draw.textures = textures; - if (draw.depthpass) + if (draw.depthpass || draw.biasHeld) { - // the bias of the depth pass of the scene (render_scene_depth()) + // the bias of the depth pass of the scene (render_scene_depth()) and of the depth drawn inside the colour pass + // (draw_indirect_depth()) commands->set_depth_bias_enable(VK_TRUE); commands->set_depth_bias(-1.f, 0.f, -1.f); } @@ -565,6 +570,7 @@ vulkan14_renderer::indirect_counts vulkan14_renderer::draw_indirect_depth(vk14:: { commands->set_depth_bias_enable(VK_TRUE); commands->set_depth_bias(-1.f, 0.f, -1.f); + draw.biasHeld = true; } walk_blending(false); // the draws without the alpha test first, then the ones with it, one program each @@ -616,6 +622,9 @@ vulkan14_renderer::indirect_counts vulkan14_renderer::draw_indirect_depth(vk14:: if (insidecolour) { commands->set_depth_bias_enable(VK_FALSE); + // the draws of the colour pass set theirs again (apply_draw_state()) + draw.biasHeld = false; + draw.depthBias = -1; } return counts; } diff --git a/rendering/vulkan14/vulkan14scene.cpp b/rendering/vulkan14/vulkan14scene.cpp index 0e221446a..9598f7f9f 100644 --- a/rendering/vulkan14/vulkan14scene.cpp +++ b/rendering/vulkan14/vulkan14scene.cpp @@ -309,6 +309,46 @@ VkImageView layer_view(VkDevice Device, vk14::image const &Image, std::uint32_t return view; } +// the biases of the depth, constant and slope. the depth is reversed (VK_COMPARE_OP_GREATER_OR_EQUAL): a positive bias comes nearer +// the camera. the depth pass of the scene draws everything a little farther than it lies (render_scene_depth()) +struct DepthBias +{ + float constant{0.f}; + float slope{0.f}; +}; +constexpr DepthBias prepassBias{-1.f, -1.f}; +// a decal goes as much nearer as glPolygonOffset(2, 8) of the opengl renderer takes it (slope 2, constant 8) +constexpr DepthBias decalBias{8.f, 2.f}; +// a draw whose depth the prepass kept (draw_state::keepdepth) doesn't write it, and passes wherever its own depth is as near as the +// one there. tested without a bias, it passed on a surface lying up to the whole bias of the prepass behind the one the prepass found +// in front (a pixel's worth of the slope: at a grazing angle a few millimetres some metres away), and the draw made last covered the +// other one: a sticker a millimetre off the body of a vehicle, drawn before the body, was lost. so it tests its depth moved as the +// prepass moved it, nearer by two units and a sixteenth of the slope for what the two programs may still work out apart (the +// position is invariant in both) +constexpr DepthBias keptBias{prepassBias.constant + 2.f, prepassBias.slope + 1.f / 16.f}; + +// the bias of a draw of the colour pass of the kind colourDepthKind() gives +DepthBias colourDepthBias(int const Kind) +{ + DepthBias bias; + if ((Kind & 2) != 0) + { + bias = keptBias; + } + if ((Kind & 1) != 0) + { + bias.constant += decalBias.constant; + bias.slope += decalBias.slope; + } + return bias; +} + +// which bias a draw of the colour pass tests its depth with: 0 none, 1 a decal, 2 its depth kept from the prepass, 3 both +int colourDepthKind(bool const Kept, bool const Decal) +{ + return (Kept ? 2 : 0) | (Decal ? 1 : 0); +} + } // namespace // the resources of the scene @@ -1871,22 +1911,17 @@ void vulkan14_renderer::walk_decal(bool const Enabled) { return; } - // the depth is reversed (VK_COMPARE_OP_GREATER_OR_EQUAL): a positive bias comes nearer the camera, as glPolygonOffset(2, 8) of - // the opengl renderer (slope 2, constant 8). the depth pass of the scene draws everything a little farther (-1, -1, - // render_scene_depth()), the decal goes as much nearer from there + // the depth pass of the scene draws everything a little farther (render_scene_depth()), the decal goes as much nearer from there if (draw.depthpass) { if (Enabled) - draw.commands->set_depth_bias(-1.f + 8.f, 0.f, -1.f + 2.f); + draw.commands->set_depth_bias(prepassBias.constant + decalBias.constant, 0.f, prepassBias.slope + decalBias.slope); else - draw.commands->set_depth_bias(-1.f, 0.f, -1.f); + draw.commands->set_depth_bias(prepassBias.constant, 0.f, prepassBias.slope); return; } - draw.commands->set_depth_bias_enable(Enabled ? VK_TRUE : VK_FALSE); - if (Enabled) - { - draw.commands->set_depth_bias(8.f, 0.f, 2.f); - } + // the colour pass sets it with the draws (apply_draw_state()) + draw.decal = Enabled; } void vulkan14_renderer::walk_glare(bool const Enabled) @@ -2876,6 +2911,22 @@ void vulkan14_renderer::apply_draw_state(bool const Triangles) draw.commands->set_depth_write_enable(depthwrite ? VK_TRUE : VK_FALSE); draw.depthwrite = depthwrite; } + // the bias of the colour pass; the depth pass of the scene, the depth drawn inside the colour pass and the shadows set theirs + if (draw.shadows || draw.depthpass || draw.biasHeld) + { + return; + } + auto const biasKind{colourDepthKind(kept, draw.decal)}; + if (biasKind != draw.depthBias) + { + draw.commands->set_depth_bias_enable(biasKind != 0 ? VK_TRUE : VK_FALSE); + if (biasKind != 0) + { + auto const bias{colourDepthBias(biasKind)}; + draw.commands->set_depth_bias(bias.constant, 0.f, bias.slope); + } + draw.depthBias = biasKind; + } } // the textures of the draw pushed: the material's, the white stand-in where there is none or it isn't there yet