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