1use std::sync::Arc;
4
5use crate::backend::ProgramHandle;
6use crate::error::Result;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum ShaderStages {
11 Vertex,
12 Fragment,
13 Both,
14}
15
16#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
18pub enum ProjectionMode {
19 #[default]
20 None,
21 PerDraw,
22 Own,
23}
24
25#[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#[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#[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#[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#[derive(Clone, Copy, Debug, PartialEq, Eq)]
73pub enum BufferKind {
74 Uniform,
75 DrawUniform,
76 Storage,
77 VerticesUint,
78 VerticesFloat,
79}
80
81#[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#[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#[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#[derive(Clone, Copy, Debug, PartialEq, Eq)]
149pub enum TextureRead {
150 Filtered,
151 Unfiltered,
152 Texels,
153}
154
155#[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 pub const UNUSED: Self =
170 Self { name: "", read: TextureRead::Filtered, stages: ShaderStages::Fragment, variants: Variants::All };
171}
172
173#[derive(Clone, Copy, Debug, PartialEq, Eq)]
175pub enum GlslPrecision {
176 Low,
177 Medium,
178 High,
179}
180
181#[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#[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#[derive(Clone, Copy, Debug)]
200pub struct GlslSource<'a> {
201 pub vertex: &'a str,
202 pub fragment: &'a str,
203}
204
205#[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#[derive(Clone, Copy, Debug)]
215pub struct GlslEsAttributePrecision<'a> {
216 pub precision: GlslPrecision,
217 pub attribute_names: &'a [&'a str],
218}
219
220#[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#[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#[derive(Clone, Debug)]
249pub struct Program {
250 inner: Arc<dyn ProgramHandle>,
251}
252
253impl Program {
254 #[doc(hidden)]
256 #[must_use]
257 pub fn from_handle(inner: Arc<dyn ProgramHandle>) -> Self {
258 Self { inner }
259 }
260
261 #[doc(hidden)]
263 #[must_use]
264 pub fn handle(&self) -> &dyn ProgramHandle {
265 &*self.inner
266 }
267
268 pub fn buffer_id(&self, name: &str) -> Result<u32> {
270 self.inner.buffer_id(name)
271 }
272
273 pub fn texture_slot(&self, name: &str) -> Result<u32> {
275 self.inner.texture_slot(name)
276 }
277}