Skip to main content

lumen_engine_gpu/
program.rs

1use crate::{NodeKey, ProgramId};
2
3#[derive(Debug, Clone)]
4pub struct BindingLayoutEntry {
5    pub binding: u32,
6    pub visibility: wgpu::ShaderStages,
7    pub ty: wgpu::BindingType,
8}
9
10impl BindingLayoutEntry {
11    pub fn texture(binding: u32, visibility: wgpu::ShaderStages) -> Self {
12        Self {
13            binding,
14            visibility,
15            ty: wgpu::BindingType::Texture {
16                sample_type: wgpu::TextureSampleType::Float { filterable: true },
17                view_dimension: wgpu::TextureViewDimension::D2,
18                multisampled: false,
19            },
20        }
21    }
22
23    pub fn storage_texture(
24        binding: u32,
25        visibility: wgpu::ShaderStages,
26        format: wgpu::TextureFormat,
27        access: wgpu::StorageTextureAccess,
28    ) -> Self {
29        Self {
30            binding,
31            visibility,
32            ty: wgpu::BindingType::StorageTexture {
33                access,
34                format,
35                view_dimension: wgpu::TextureViewDimension::D2,
36            },
37        }
38    }
39
40    pub fn sampler(binding: u32, visibility: wgpu::ShaderStages) -> Self {
41        Self {
42            binding,
43            visibility,
44            ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
45        }
46    }
47
48    pub fn uniform(binding: u32, visibility: wgpu::ShaderStages) -> Self {
49        Self {
50            binding,
51            visibility,
52            ty: wgpu::BindingType::Buffer {
53                ty: wgpu::BufferBindingType::Uniform,
54                has_dynamic_offset: false,
55                min_binding_size: None,
56            },
57        }
58    }
59
60    pub fn storage(binding: u32, visibility: wgpu::ShaderStages, read_only: bool) -> Self {
61        Self {
62            binding,
63            visibility,
64            ty: wgpu::BindingType::Buffer {
65                ty: wgpu::BufferBindingType::Storage { read_only },
66                has_dynamic_offset: false,
67                min_binding_size: None,
68            },
69        }
70    }
71}
72
73#[derive(Debug, Clone)]
74pub struct BindGroupLayoutSpec {
75    pub label: Option<String>,
76    pub entries: Vec<BindingLayoutEntry>,
77}
78
79impl BindGroupLayoutSpec {
80    pub fn single(entries: Vec<BindingLayoutEntry>) -> Vec<Self> {
81        vec![Self {
82            label: None,
83            entries,
84        }]
85    }
86}
87
88#[derive(Debug, Clone)]
89pub struct RenderProgramDesc {
90    pub label: Option<String>,
91    pub shader: String,
92    pub vertex_entry: String,
93    pub fragment_entry: String,
94    pub bind_groups: Vec<BindGroupLayoutSpec>,
95    pub targets: Vec<Option<wgpu::ColorTargetState>>,
96    pub vertex_buffers: Vec<wgpu::VertexBufferLayout<'static>>,
97    pub primitive: wgpu::PrimitiveState,
98}
99
100#[derive(Debug, Clone)]
101pub struct ComputeProgramDesc {
102    pub label: Option<String>,
103    pub shader: String,
104    pub entry: String,
105    pub bind_groups: Vec<BindGroupLayoutSpec>,
106}
107
108#[derive(Debug, Clone)]
109pub enum ProgramDesc {
110    Render(RenderProgramDesc),
111    Compute(ComputeProgramDesc),
112}
113
114#[derive(Debug, Clone)]
115pub struct Program {
116    pub id: ProgramId,
117    pub owner: Option<NodeKey>,
118    pub desc: ProgramDesc,
119}