Skip to main content

cranpose_render_wgpu/
scene_depth.rs

1//! A frame drawn against a depth that the host owns.
2
3use crate::{WgpuRenderer, WgpuRendererError};
4
5#[expect(
6    clippy::multiple_inherent_impl,
7    reason = "the host's depth in its own file"
8)]
9impl WgpuRenderer {
10    /// Renders like [`render`](Self::render), with the 3D scenes of the frame drawn against `depth`
11    /// and leaving their depth there, for a host that hands the depth on, such as an XR compositor.
12    ///
13    /// `depth` is a view of a texture of [`SCENE_DEPTH_FORMAT`](crate::SCENE_DEPTH_FORMAT) with
14    /// `width` by `height` pixels and the render attachment usage. The scenes clear it and write
15    /// the distance of what they draw as `1 - near / z`, where `z` is the depth along the camera's
16    /// axis and `near` is
17    /// [`Camera::near`](cranpose_ui_graphics::space::Camera::near): 0 at the near plane, 1 at
18    /// infinity, and 1 where no scene drew. Each scene pass clears it again, so a frame that draws
19    /// several scenes leaves the depth of the last one. A scene drawn into an intermediate layer,
20    /// such as one under a blur, uses depth of the renderer's own.
21    ///
22    /// A scene with [`Multisample::Off`](cranpose_ui_graphics::space::Multisample::Off) stores its
23    /// depth in its own pass. A scene with four samples cannot, because a render pass holds one
24    /// sample count and wgpu has no depth resolve. Such a scene that covers a page holding only
25    /// its transparent clear averages its colour straight into the page, then a second pass with
26    /// one sample draws its opaque surfaces and volumes again with the depth pipelines. Each pixel
27    /// holds the depth of its centre, so a pixel that an edge covers in part carries the depth of
28    /// the surface at its centre, or the far plane where none covers the centre. Any other scene
29    /// of four samples leaves the far plane.
30    pub fn render_with_scene_depth(
31        &mut self,
32        texture: &wgpu::Texture,
33        view: &wgpu::TextureView,
34        depth: &wgpu::TextureView,
35        (width, height): (u32, u32),
36    ) -> Result<(), WgpuRendererError> {
37        self.backend
38            .sync_mut()?
39            .renderer
40            .depth_targets
41            .host_scene(Some(((width, height), depth.clone())));
42        let drawn = self.render(texture, view, width, height);
43        if let Ok(sync) = self.backend.sync_mut() {
44            sync.renderer.depth_targets.host_scene(None);
45        }
46        drawn
47    }
48}