Using separate depth buffer for forward facing and backward facing triangles in wgpu

⚓ Rust    📅 2025-07-20    👤 surdeus    👁️ 2      

surdeus

I want to achieve this, using wgpu-rs: for each triangle to render, if it's front facing, I want it's depth values to be compared and written to one depth buffer (... and normally render it to the color attachment, if it's visible according to that buffer's depth values), and if it's back facing, I want it's depth values to be compared and written to another (... and not render it).

(Why? I'm working on a cartoon-style renderer for a game, and having a depth buffer to which only back-facing faces are written will be very useful for rendering outlines).

The easiest solution - the one I'm currently using - is to have two separate render pipelines: one for actually rendering the faces, which uses the "forward" buffer, and one for filling the "backward" buffer, which is identical as the first one, just with fragment state set to None, primitive state's cull_mode set to Some(Face::Front) and a different depth state, of course.

The downside is that all the vertices will have to pass through the vertex shader twice. This is not a big problem - the vertex shader doesn't do anything extremely expensive, just a few matrix multiplies - but is there any easy way that this could be avoided?

1 post - 1 participant

Read full topic

🏷️ rust_feed