Skip to content

Conversation

@jgraef
Copy link

@jgraef jgraef commented Nov 11, 2025

I'm using eframe to run an app with the wgpu backend and built a custom widget that renders a 3D scene. I want to use the stencil buffer. eframe allows me to specify the depth and stencil buffer and egui-wgpu chooses the right texture format. It does attach the depth texture when rendering, but leaves the stencil attachment empty.

It makes sense to me that if a depth/stencil texture is created both are attached.

I tested that this doesn't break anything if the depth texture doesn't have a stencil part. I ran all tests and only 2 snapshot tests failed with superficial diffs. I also ran the hello_world app with the wgpu feature to make sure an app without a depth texture would still work.

./scripts/check.sh doesn't run for me (typos not found)

Is this something you want to merge into egui-wgpu at all? If so, please let me know if there is more I can do to make this easier to merge.

I can understand if this is a niche use case (in contrast to depth buffer only), but the only workaround would be to create my own render pass, render to a texture and then blit the result. For more complex setups this might be the way to go anyway.

@jgraef jgraef requested a review from Wumpf as a code owner November 11, 2025 20:00
@github-actions
Copy link

github-actions bot commented Nov 11, 2025

Preview available at https://egui-pr-preview.github.io/pr/7702-switchwgpu-stencil-buffer
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

View snapshot changes at kitdiff

Copy link
Collaborator

@Wumpf Wumpf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked what wgpu's validation does today, but WebGPU spec says that if there is no stencil attachment, the stencil ops musn't be set, see https://www.w3.org/TR/webgpu/#depth-stencil-attachments
so this should be set only if there is a stencil aspect
For consistency the web backend should do the same as well.

Generally I'm a bit skeptical about the fact that egui_wgpu provides a depth(/stencil) buffer integration in the first place. Instead it should just be easier to own the final render output yourself for situations where you need a full screen depth buffer etc.
This is already possible today, but quite cumbersome and requires a lot of context.
Given that we already have an optional depth buffer that is also cleared with an arbitrary value it's only consistent I figure.
Alternatively, we could make more of these settings things that one can optionally configure on the renderer 🤔

@jgraef
Copy link
Author

jgraef commented Nov 12, 2025

if there is no stencil attachment, the stencil ops musn't be set

The same applies for the depth aspect, which the current implementation doesn't check. depth_format_from_bits will create a stencil-only texture. I'm adding checks against self.options.depth_stencil_format for both.

For consistency the web backend should do the same as well.

Oh I see the code is duplicated in eframe for web.

Generally I'm a bit skeptical about the fact that egui_wgpu provides a depth(/stencil) buffer integration in the first place.

I agree.

Instead it should just be easier to own the final render output yourself for situations where you need a full screen depth buffer etc.

Can you clarify what you mean by that? Do you mean egui-wgpu should just provide the target texture to the callback trait and have it create the render pass itself?

@jgraef
Copy link
Author

jgraef commented Nov 12, 2025

Alternatively, we could make more of these settings things that one can optionally configure on the renderer

Yes, I missed that a bit. Also you have to dig through the source code to figure out how the render pass is actually being configured. But the defaults work well enough I think and one can always clear the depth-stencil buffer with whatever they want with a single draw call (which you might do anyway to set the background color).

@Wumpf
Copy link
Collaborator

Wumpf commented Nov 12, 2025

Can you clarify what you mean by that? Do you mean egui-wgpu should just provide the target texture to the callback trait and have it create the render pass itself?

tbh I haven't thought it through entirely, but I'm thinking that the egui/eframe/wgpu integration should make it easier to just swap out the entire "main render pass" setup against something custom without having to redo half of eframe from scratch as it's the case now :/

Copy link
Collaborator

@Wumpf Wumpf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great, thank you!

.options
.depth_stencil_format
.is_some_and(|depth_stencil_format| {
depth_stencil_format.has_depth_aspect()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really nice catch there, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants