Skip to main content

vk_video/
wgpu_helpers.rs

1mod nv12_to_rgba;
2mod rgba_to_nv12;
3
4pub use nv12_to_rgba::*;
5pub use rgba_to_nv12::*;
6
7use crate::device::{ColorRange, ColorSpace};
8
9#[derive(Debug, thiserror::Error)]
10pub enum WgpuConverterInitError {
11    // TODO: Remove once we add more converters
12    #[error("Only limited range BT709 is supported")]
13    OnlyLimitedBT709Supported,
14
15    #[error(
16        "Provided frame does not match the converter's parameters. Expected {expected:?}, actual {actual:?}"
17    )]
18    IncompatibleFrame {
19        expected: (ColorSpace, ColorRange),
20        actual: (ColorSpace, ColorRange),
21    },
22}
23
24/// Parameters for NV12 <-> RGBA texture conversion.
25///
26/// Used by [`WgpuNv12ToRgbaConverter`] and [`WgpuRgbaToNv12Converter`] to describe
27/// the color properties of the NV12 textures.
28#[derive(Debug, Clone, Copy)]
29pub struct WgpuConverterParameters {
30    /// The color space of the NV12 data.
31    pub color_space: ColorSpace,
32
33    /// Whether the NV12 data uses full or limited sample range.
34    pub color_range: ColorRange,
35}
36
37struct WgpuSampler {
38    bgl: wgpu::BindGroupLayout,
39    bg: wgpu::BindGroup,
40}
41
42impl WgpuSampler {
43    fn new(device: &wgpu::Device) -> Self {
44        let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
45            mag_filter: wgpu::FilterMode::Linear,
46            min_filter: wgpu::FilterMode::Linear,
47            mipmap_filter: wgpu::MipmapFilterMode::Linear,
48            ..Default::default()
49        });
50        let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
51            label: None,
52            entries: &[wgpu::BindGroupLayoutEntry {
53                binding: 0,
54                visibility: wgpu::ShaderStages::FRAGMENT,
55                ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
56                count: None,
57            }],
58        });
59        let bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
60            label: None,
61            layout: &bgl,
62            entries: &[wgpu::BindGroupEntry {
63                binding: 0,
64                resource: wgpu::BindingResource::Sampler(&sampler),
65            }],
66        });
67
68        Self { bgl, bg }
69    }
70}