18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 07:22:42 +01:00

Merge pull request #570 from MaSzyna-EU07/fix/vulkan-kept-depth-decals

fix(vulkan): stickers on vehicles no longer vanish behind the body seen at an angle
This commit is contained in:
2026-11-30 19:10:33 +00:00
committed by GitHub
4 changed files with 82 additions and 13 deletions
+7 -1
View File
@@ -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};
+3
View File
@@ -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;
+11 -2
View File
@@ -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<int>(draw.extent.width), static_cast<int>(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;
}
+61 -10
View File
@@ -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