Skip to main content

mln_plugin/
program.rs

1//! Host-agnostic shader program descriptors and owned [`Program`] handles.
2
3use std::sync::Arc;
4
5use crate::backend::ProgramHandle;
6use crate::error::Result;
7
8/// Which shader stages read a buffer, paint parameters, or texture.
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum ShaderStages {
11    Vertex,
12    Fragment,
13    Both,
14}
15
16/// How a shader program receives its tile/frame projection uniforms.
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
18pub enum ProjectionMode {
19    #[default]
20    None,
21    PerDraw,
22    Own,
23}
24
25/// Which compiled shader variants include a buffer, attribute, or texture.
26#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
27pub enum Variants<'a> {
28    #[default]
29    All,
30    Standing,
31    PerVertex,
32    Defined(&'a str),
33}
34
35/// Member type inside a shader uniform or storage block.
36#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub enum BlockMemberType {
38    Float,
39    Int,
40    Uint,
41    Vec2,
42    Vec3,
43    Vec4,
44    Mat4,
45    Vec4Array(u32),
46}
47
48/// Field description inside a [`BlockDesc`].
49#[derive(Clone, Copy, Debug)]
50pub struct BlockMember<'a> {
51    pub name: &'a str,
52    pub ty: BlockMemberType,
53    pub offset: u32,
54}
55
56impl<'a> BlockMember<'a> {
57    #[must_use]
58    pub const fn new(name: &'a str, ty: BlockMemberType, offset: u32) -> Self {
59        Self { name, ty, offset }
60    }
61}
62
63/// Std140/std430 buffer block layout description.
64#[derive(Clone, Copy, Debug)]
65pub struct BlockDesc<'a> {
66    pub name: &'a str,
67    pub byte_size: u32,
68    pub members: &'a [BlockMember<'a>],
69}
70
71/// Binding kind for a shader buffer.
72#[derive(Clone, Copy, Debug, PartialEq, Eq)]
73pub enum BufferKind {
74    Uniform,
75    DrawUniform,
76    Storage,
77    VerticesUint,
78    VerticesFloat,
79}
80
81/// Descriptor for one uniform, storage, or vertex-data buffer declared by a program.
82#[derive(Clone, Copy, Debug)]
83pub struct BufferDesc<'a> {
84    pub name: &'a str,
85    pub kind: BufferKind,
86    pub slot: u32,
87    pub stages: ShaderStages,
88    pub variants: Variants<'a>,
89    pub block: Option<BlockDesc<'a>>,
90    pub storage_stride: u32,
91}
92
93/// Vertex or instance attribute data format.
94#[derive(Clone, Copy, Debug, PartialEq, Eq)]
95pub enum AttributeFormat {
96    Byte,
97    Byte2,
98    Byte3,
99    Byte4,
100    UByte,
101    UByte2,
102    UByte3,
103    UByte4,
104    Short,
105    Short2,
106    Short3,
107    Short4,
108    UShort,
109    UShort2,
110    UShort3,
111    UShort4,
112    Int,
113    Int2,
114    Int3,
115    Int4,
116    UInt,
117    UInt2,
118    UInt3,
119    UInt4,
120    Float,
121    Float2,
122    Float3,
123    Float4,
124    UByte4Normalized,
125}
126
127/// Vertex or instance attribute declared by a program.
128#[derive(Clone, Copy, Debug)]
129pub struct AttributeDesc<'a> {
130    pub name: &'a str,
131    pub format: AttributeFormat,
132    pub variants: Variants<'a>,
133}
134
135impl<'a> AttributeDesc<'a> {
136    #[must_use]
137    pub const fn new(name: &'a str, format: AttributeFormat) -> Self {
138        Self { name, format, variants: Variants::All }
139    }
140
141    #[must_use]
142    pub const fn standing(name: &'a str, format: AttributeFormat) -> Self {
143        Self { name, format, variants: Variants::Standing }
144    }
145}
146
147/// How a shader samples a texture binding.
148#[derive(Clone, Copy, Debug, PartialEq, Eq)]
149pub enum TextureRead {
150    Filtered,
151    Unfiltered,
152    Texels,
153}
154
155/// Texture binding slot declared by a program.
156///
157/// Use [`TextureDesc::UNUSED`] for an unoccupied slot to keep binding indices
158/// aligned with shader source declarations.
159#[derive(Clone, Copy, Debug)]
160pub struct TextureDesc<'a> {
161    pub name: &'a str,
162    pub read: TextureRead,
163    pub stages: ShaderStages,
164    pub variants: Variants<'a>,
165}
166
167impl TextureDesc<'_> {
168    /// Placeholder for an unused texture slot.
169    pub const UNUSED: Self =
170        Self { name: "", read: TextureRead::Filtered, stages: ShaderStages::Fragment, variants: Variants::All };
171}
172
173/// GLSL ES precision qualifier.
174#[derive(Clone, Copy, Debug, PartialEq, Eq)]
175pub enum GlslPrecision {
176    Low,
177    Medium,
178    High,
179}
180
181/// WGSL shader source strings.
182#[derive(Clone, Copy, Debug)]
183pub struct WgslSource<'a> {
184    pub vertex: &'a str,
185    pub fragment: &'a str,
186    pub prelude: &'a str,
187}
188
189/// Metal Shading Language source strings.
190#[derive(Clone, Copy, Debug)]
191pub struct MslSource<'a> {
192    pub vertex_main_function: &'a str,
193    pub fragment_main_function: &'a str,
194    pub source: &'a str,
195    pub fast_math: bool,
196}
197
198/// Vulkan GLSL 450 shader source strings.
199#[derive(Clone, Copy, Debug)]
200pub struct GlslSource<'a> {
201    pub vertex: &'a str,
202    pub fragment: &'a str,
203}
204
205/// GLSL ES precision override for members of a uniform block.
206#[derive(Clone, Copy, Debug)]
207pub struct GlslEsMemberPrecision<'a> {
208    pub block_name: &'a str,
209    pub precision: GlslPrecision,
210    pub member_names: &'a [&'a str],
211}
212
213/// GLSL ES precision override for vertex attributes.
214#[derive(Clone, Copy, Debug)]
215pub struct GlslEsAttributePrecision<'a> {
216    pub precision: GlslPrecision,
217    pub attribute_names: &'a [&'a str],
218}
219
220/// GLSL ES 3.00 shader source strings and precision overrides.
221#[derive(Clone, Copy, Debug)]
222pub struct GlslEsSource<'a> {
223    pub vertex: &'a str,
224    pub fragment: &'a str,
225    pub member_precisions: &'a [GlslEsMemberPrecision<'a>],
226    pub attribute_precisions: &'a [GlslEsAttributePrecision<'a>],
227    pub highp_samplers: &'a [&'a str],
228}
229
230/// Complete multi-backend shader program descriptor.
231#[derive(Clone, Copy, Debug)]
232pub struct ProgramDesc<'a> {
233    pub name: &'a str,
234    pub buffers: &'a [BufferDesc<'a>],
235    pub paint_params: Option<ShaderStages>,
236    pub projection: ProjectionMode,
237    pub attributes: &'a [AttributeDesc<'a>],
238    pub instance_attributes: &'a [AttributeDesc<'a>],
239    pub textures: &'a [TextureDesc<'a>],
240    pub properties: &'a [&'a str],
241    pub wgsl: Option<WgslSource<'a>>,
242    pub msl: Option<MslSource<'a>>,
243    pub glsl: Option<GlslSource<'a>>,
244    pub glsl_es: Option<GlslEsSource<'a>>,
245}
246
247/// Owned handle to a validated host shader program.
248#[derive(Clone, Debug)]
249pub struct Program {
250    inner: Arc<dyn ProgramHandle>,
251}
252
253impl Program {
254    /// Wraps a host-provided program handle.
255    #[doc(hidden)]
256    #[must_use]
257    pub fn from_handle(inner: Arc<dyn ProgramHandle>) -> Self {
258        Self { inner }
259    }
260
261    /// Accesses the underlying host handle (for host backend downcasting).
262    #[doc(hidden)]
263    #[must_use]
264    pub fn handle(&self) -> &dyn ProgramHandle {
265        &*self.inner
266    }
267
268    /// Resolves the binding slot ID of a named buffer declared by this program.
269    pub fn buffer_id(&self, name: &str) -> Result<u32> {
270        self.inner.buffer_id(name)
271    }
272
273    /// Resolves the binding slot index of a named texture declared by this program.
274    pub fn texture_slot(&self, name: &str) -> Result<u32> {
275        self.inner.texture_slot(name)
276    }
277}