mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 14:50:03 -04:00
Add depth buffer support for egui-wgpu's render pass (#2002)
* add a depth texture for wgpu callbacks * egui-wgpu: use depth from native_options * add wgpu caveat to depth_buffer docstring
This commit is contained in:
@@ -121,6 +121,7 @@ struct SizedBuffer {
|
||||
/// Render pass to render a egui based GUI.
|
||||
pub struct RenderPass {
|
||||
render_pipeline: wgpu::RenderPipeline,
|
||||
depth_texture: Option<(wgpu::Texture, wgpu::TextureView)>,
|
||||
index_buffers: Vec<SizedBuffer>,
|
||||
vertex_buffers: Vec<SizedBuffer>,
|
||||
uniform_buffer: SizedBuffer,
|
||||
@@ -144,6 +145,7 @@ impl RenderPass {
|
||||
device: &wgpu::Device,
|
||||
output_format: wgpu::TextureFormat,
|
||||
msaa_samples: u32,
|
||||
depth_bits: u8,
|
||||
) -> Self {
|
||||
let shader = wgpu::ShaderModuleDescriptor {
|
||||
label: Some("egui_shader"),
|
||||
@@ -221,6 +223,14 @@ impl RenderPass {
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
let depth_stencil = (depth_bits > 0).then(|| wgpu::DepthStencilState {
|
||||
format: wgpu::TextureFormat::Depth32Float,
|
||||
depth_write_enabled: false,
|
||||
depth_compare: wgpu::CompareFunction::Always,
|
||||
stencil: wgpu::StencilState::default(),
|
||||
bias: wgpu::DepthBiasState::default(),
|
||||
});
|
||||
|
||||
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("egui_pipeline"),
|
||||
layout: Some(&pipeline_layout),
|
||||
@@ -249,7 +259,7 @@ impl RenderPass {
|
||||
polygon_mode: wgpu::PolygonMode::default(),
|
||||
strip_index_format: None,
|
||||
},
|
||||
depth_stencil: None,
|
||||
depth_stencil,
|
||||
multisample: wgpu::MultisampleState {
|
||||
alpha_to_coverage_enabled: false,
|
||||
count: msaa_samples,
|
||||
@@ -289,9 +299,30 @@ impl RenderPass {
|
||||
textures: HashMap::new(),
|
||||
next_user_texture_id: 0,
|
||||
paint_callback_resources: TypeMap::default(),
|
||||
depth_texture: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_depth_texture(&mut self, device: &wgpu::Device, width: u32, height: u32) {
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: None,
|
||||
size: wgpu::Extent3d {
|
||||
width,
|
||||
height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Depth32Float,
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
|
||||
});
|
||||
|
||||
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
|
||||
self.depth_texture = Some((texture, view));
|
||||
}
|
||||
|
||||
/// Executes the egui render pass.
|
||||
pub fn execute(
|
||||
&self,
|
||||
@@ -307,6 +338,17 @@ impl RenderPass {
|
||||
wgpu::LoadOp::Load
|
||||
};
|
||||
|
||||
let depth_stencil_attachment = self.depth_texture.as_ref().map(|(_texture, view)| {
|
||||
wgpu::RenderPassDepthStencilAttachment {
|
||||
view,
|
||||
depth_ops: Some(wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(1.0),
|
||||
store: true,
|
||||
}),
|
||||
stencil_ops: None,
|
||||
}
|
||||
});
|
||||
|
||||
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view: color_attachment,
|
||||
@@ -316,7 +358,7 @@ impl RenderPass {
|
||||
store: true,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
depth_stencil_attachment,
|
||||
label: Some("egui main render pass"),
|
||||
});
|
||||
rpass.push_debug_group("egui_pass");
|
||||
|
||||
Reference in New Issue
Block a user