Skip to main content

alpha2_render/
pipeline.rs

1use std::sync::OnceLock;
2
3/// Current pipeline-cache occupancy.
4#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
5pub struct PipelineCacheStats {
6    /// Eagerly registered runtime-effect pipelines.
7    pub runtime_effects: usize,
8    /// Lazily initialized built-in pipelines.
9    pub builtins: usize,
10}
11
12/// Work completed by a graph warmup request.
13#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
14pub struct WarmupReport {
15    /// Runtime effect pipelines verified as present.
16    pub runtime_effects: usize,
17    /// Built-in pipelines required and initialized.
18    pub builtins: usize,
19}
20
21#[derive(Debug, Default)]
22pub(crate) struct PipelineCache {
23    pub(crate) composite: OnceLock<CachedCompositePipeline>,
24    pub(crate) sum: OnceLock<CachedPipeline>,
25    pub(crate) layout_pack: OnceLock<CachedLayoutPackPipeline>,
26    /// Test-only: the full-screen quad.
27    #[cfg(test)]
28    pub(crate) quad: OnceLock<CachedPipeline>,
29    pub(crate) readback_pack: OnceLock<CachedPipeline>,
30    pub(crate) rgba_present: OnceLock<CachedSampledPipeline>,
31    pub(crate) yuv_readback: OnceLock<CachedYuvReadbackPipeline>,
32    pub(crate) yuv: OnceLock<CachedYuvPipeline>,
33    pub(crate) yuv16: OnceLock<CachedYuvPipeline>,
34    pub(crate) yuv_repack: OnceLock<CachedYuvRepackPipeline>,
35    pub(crate) vector: OnceLock<crate::vector::CachedVectorPipeline>,
36}
37
38#[derive(Debug)]
39pub(crate) struct CachedPipeline {
40    pub(crate) bind_group_layout: wgpu::BindGroupLayout,
41    pub(crate) pipeline: wgpu::RenderPipeline,
42}
43
44#[derive(Debug)]
45pub(crate) struct CachedYuvPipeline {
46    pub(crate) bind_group_layout: wgpu::BindGroupLayout,
47    pub(crate) pipeline: wgpu::RenderPipeline,
48}
49
50#[derive(Debug)]
51pub(crate) struct CachedSampledPipeline {
52    pub(crate) bind_group_layout: wgpu::BindGroupLayout,
53    pub(crate) pipeline: wgpu::RenderPipeline,
54}
55
56#[derive(Debug)]
57pub(crate) struct CachedCompositePipeline {
58    pub(crate) bind_group_layout: wgpu::BindGroupLayout,
59    pub(crate) shader: wgpu::ShaderModule,
60    /// One pipeline per composite program (loop, then one-layer), built on first use.
61    pub(crate) programs: [std::sync::OnceLock<wgpu::RenderPipeline>; 2],
62}
63
64#[derive(Debug)]
65pub(crate) struct CachedYuvReadbackPipeline {
66    pub(crate) bind_group_layout: wgpu::BindGroupLayout,
67    pub(crate) y_pipeline: wgpu::RenderPipeline,
68    pub(crate) uv_pipeline: wgpu::RenderPipeline,
69    /// Writes both chroma channels into one half-size `Rg8Unorm` plane, the NV12 layout hardware
70    /// encoders expect.
71    pub(crate) uv_nv12_pipeline: wgpu::RenderPipeline,
72}
73
74/// Packs frames into the integer planes of an output layout (layout-pack.wesl).
75#[derive(Debug)]
76pub(crate) struct CachedLayoutPackPipeline {
77    pub(crate) bind_group_layout: wgpu::BindGroupLayout,
78    pub(crate) pipeline_layout: wgpu::PipelineLayout,
79    pub(crate) shader: wgpu::ShaderModule,
80    pub(crate) programs: [OnceLock<wgpu::RenderPipeline>; LayoutPackPass::COUNT],
81}
82
83impl CachedLayoutPackPipeline {
84    fn program_count(&self) -> usize {
85        self.programs
86            .iter()
87            .filter(|program| program.get().is_some())
88            .count()
89    }
90}
91
92#[derive(Copy, Clone, Debug, Eq, PartialEq)]
93pub(crate) enum LayoutPackPass {
94    Luma { ten_bit: bool, alpha: bool },
95    Chroma { ten_bit: bool, interleaved: bool },
96}
97
98impl LayoutPackPass {
99    const COUNT: usize = 8;
100
101    pub(crate) fn of_layout(layout: alpha2_media::PixelLayout) -> [Self; 2] {
102        let ten_bit = layout.bit_depth == alpha2_media::BitDepth::Ten;
103        [
104            Self::Luma {
105                ten_bit,
106                alpha: layout.alpha,
107            },
108            Self::Chroma {
109                ten_bit,
110                interleaved: layout.planes == alpha2_media::PlaneLayout::P010,
111            },
112        ]
113    }
114
115    pub(crate) const fn index(self) -> usize {
116        match self {
117            Self::Luma { ten_bit, alpha } => (ten_bit as usize) * 2 + alpha as usize,
118            Self::Chroma {
119                ten_bit,
120                interleaved,
121            } => 4 + (ten_bit as usize) * 2 + interleaved as usize,
122        }
123    }
124
125    pub(crate) const fn entry_point(self) -> &'static str {
126        match self {
127            Self::Luma { alpha: false, .. } => "luma_fs",
128            Self::Luma { alpha: true, .. } => "luma_alpha_fs",
129            Self::Chroma {
130                interleaved: false, ..
131            } => "chroma_fs",
132            Self::Chroma {
133                interleaved: true, ..
134            } => "chroma_interleaved_fs",
135        }
136    }
137
138    pub(crate) fn target_formats(self) -> Vec<wgpu::TextureFormat> {
139        let (ten_bit, count, pair) = match self {
140            Self::Luma { ten_bit, alpha } => (ten_bit, 1 + usize::from(alpha), false),
141            Self::Chroma {
142                ten_bit,
143                interleaved,
144            } => (ten_bit, 2 - usize::from(interleaved), interleaved),
145        };
146        let format = match (ten_bit, pair) {
147            (false, false) => wgpu::TextureFormat::R8Uint,
148            (false, true) => wgpu::TextureFormat::Rg8Uint,
149            (true, false) => wgpu::TextureFormat::R16Uint,
150            (true, true) => wgpu::TextureFormat::Rg16Uint,
151        };
152        vec![format; count]
153    }
154}
155
156/// Plane-for-plane copies of decoded 8-bit 4:2:0 frames into NV12 targets.
157#[derive(Debug)]
158pub(crate) struct CachedYuvRepackPipeline {
159    pub(crate) bind_group_layout: wgpu::BindGroupLayout,
160    /// Writes an `R8Unorm` luma target.
161    pub(crate) luma: wgpu::RenderPipeline,
162    /// Writes an `Rg8Unorm` chroma target from an interleaved UV plane.
163    pub(crate) interleaved_chroma: wgpu::RenderPipeline,
164    /// Writes an `Rg8Unorm` chroma target from separate U and V planes.
165    pub(crate) planar_chroma: wgpu::RenderPipeline,
166}
167
168impl PipelineCache {
169    pub(crate) fn builtin_count(&self) -> usize {
170        #[cfg(test)]
171        let quad = usize::from(self.quad.get().is_some());
172        #[cfg(not(test))]
173        let quad = 0;
174        usize::from(self.composite.get().is_some())
175            + usize::from(self.sum.get().is_some())
176            + quad
177            + usize::from(self.yuv.get().is_some())
178            + usize::from(self.yuv16.get().is_some())
179            + usize::from(self.rgba_present.get().is_some())
180            + usize::from(self.readback_pack.get().is_some())
181            + usize::from(self.yuv_readback.get().is_some())
182            + self
183                .layout_pack
184                .get()
185                .map_or(0, CachedLayoutPackPipeline::program_count)
186            + usize::from(self.yuv_repack.get().is_some())
187            + self
188                .vector
189                .get()
190                .map_or(0, crate::vector::CachedVectorPipeline::pipeline_count)
191    }
192}