shader-sense 1.3.1

Library for runtime shader validation and symbol inspection
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Shader stage and specific helpers
use std::{
    collections::HashMap,
    ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not},
    path::PathBuf,
    str::FromStr,
};

use serde::{Deserialize, Serialize};

/// All shading language supported
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum ShadingLanguage {
    Wgsl,
    Hlsl,
    Glsl,
}

/// Mask for all shaders stages supported.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct ShaderStageMask(u32);

impl ShaderStageMask {
    pub const VERTEX: Self = Self(1 << 0);
    pub const FRAGMENT: Self = Self(1 << 1);
    pub const COMPUTE: Self = Self(1 << 2);
    pub const TESSELATION_CONTROL: Self = Self(1 << 3);
    pub const TESSELATION_EVALUATION: Self = Self(1 << 4);
    pub const MESH: Self = Self(1 << 5);
    pub const TASK: Self = Self(1 << 6);
    pub const GEOMETRY: Self = Self(1 << 7);
    pub const RAY_GENERATION: Self = Self(1 << 8);
    pub const CLOSEST_HIT: Self = Self(1 << 9);
    pub const ANY_HIT: Self = Self(1 << 10);
    pub const CALLABLE: Self = Self(1 << 11);
    pub const MISS: Self = Self(1 << 12);
    pub const INTERSECT: Self = Self(1 << 13);
}

impl Default for ShaderStageMask {
    fn default() -> Self {
        Self(0)
    }
}
impl ShaderStageMask {
    pub const fn from_u32(x: u32) -> Self {
        Self(x)
    }
    pub const fn as_u32(self) -> u32 {
        self.0
    }
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }
    pub const fn contains(self, other: &ShaderStage) -> bool {
        let mask = other.as_mask();
        self.0 & mask.0 == mask.0
    }
}
impl BitOr for ShaderStageMask {
    type Output = Self;
    #[inline]
    fn bitor(self, rhs: Self) -> Self {
        Self(self.0 | rhs.0)
    }
}
impl BitOrAssign for ShaderStageMask {
    #[inline]
    fn bitor_assign(&mut self, rhs: Self) {
        *self = *self | rhs
    }
}
impl BitAnd for ShaderStageMask {
    type Output = Self;
    #[inline]
    fn bitand(self, rhs: Self) -> Self {
        Self(self.0 & rhs.0)
    }
}
impl BitAndAssign for ShaderStageMask {
    #[inline]
    fn bitand_assign(&mut self, rhs: Self) {
        *self = *self & rhs
    }
}
impl BitXor for ShaderStageMask {
    type Output = Self;
    #[inline]
    fn bitxor(self, rhs: Self) -> Self {
        Self(self.0 ^ rhs.0)
    }
}
impl BitXorAssign for ShaderStageMask {
    #[inline]
    fn bitxor_assign(&mut self, rhs: Self) {
        *self = *self ^ rhs
    }
}
impl Not for ShaderStageMask {
    type Output = Self;
    #[inline]
    fn not(self) -> Self {
        Self(!self.0)
    }
}

/// All shader stage supported
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum ShaderStage {
    Vertex,
    Fragment, // aka pixel shader
    Compute,
    TesselationControl,    // aka hull shader
    TesselationEvaluation, // aka domain shader
    Mesh,
    Task, // aka amplification shader
    Geometry,
    RayGeneration,
    ClosestHit,
    AnyHit,
    Callable,
    Miss,
    Intersect,
}

impl ShaderStage {
    /// Get a stage from its filename. Mostly follow glslang guideline
    pub fn from_file_name(file_name: &String) -> Option<ShaderStage> {
        // TODO: add control for these
        let paths = HashMap::from([
            ("vert", ShaderStage::Vertex),
            ("frag", ShaderStage::Fragment),
            ("comp", ShaderStage::Compute),
            ("task", ShaderStage::Task),
            ("mesh", ShaderStage::Mesh),
            ("tesc", ShaderStage::TesselationControl),
            ("tese", ShaderStage::TesselationEvaluation),
            ("geom", ShaderStage::Geometry),
            ("rgen", ShaderStage::RayGeneration),
            ("rchit", ShaderStage::ClosestHit),
            ("rahit", ShaderStage::AnyHit),
            ("rcall", ShaderStage::Callable),
            ("rmiss", ShaderStage::Miss),
            ("rint", ShaderStage::Intersect),
        ]);
        let extension_list = file_name.rsplit(".");
        for extension in extension_list {
            if let Some(stage) = paths.get(extension) {
                return Some(stage.clone());
            } else {
                continue;
            }
        }
        // For header files & undefined, will output issue with missing version...
        None
    }
    pub const fn as_mask(&self) -> ShaderStageMask {
        match self {
            ShaderStage::Vertex => ShaderStageMask::VERTEX,
            ShaderStage::Fragment => ShaderStageMask::FRAGMENT,
            ShaderStage::Compute => ShaderStageMask::COMPUTE,
            ShaderStage::TesselationControl => ShaderStageMask::TESSELATION_CONTROL,
            ShaderStage::TesselationEvaluation => ShaderStageMask::TESSELATION_EVALUATION,
            ShaderStage::Mesh => ShaderStageMask::MESH,
            ShaderStage::Task => ShaderStageMask::TASK,
            ShaderStage::Geometry => ShaderStageMask::GEOMETRY,
            ShaderStage::RayGeneration => ShaderStageMask::RAY_GENERATION,
            ShaderStage::ClosestHit => ShaderStageMask::CLOSEST_HIT,
            ShaderStage::AnyHit => ShaderStageMask::ANY_HIT,
            ShaderStage::Callable => ShaderStageMask::CALLABLE,
            ShaderStage::Miss => ShaderStageMask::MISS,
            ShaderStage::Intersect => ShaderStageMask::INTERSECT,
        }
    }
    /// All graphics pipeline stages.
    pub fn graphics() -> ShaderStageMask {
        ShaderStageMask::VERTEX
            | ShaderStageMask::FRAGMENT
            | ShaderStageMask::GEOMETRY
            | ShaderStageMask::TESSELATION_CONTROL
            | ShaderStageMask::TESSELATION_EVALUATION
            | ShaderStageMask::TASK
            | ShaderStageMask::MESH
    }
    /// All compute pipeline stages.
    pub fn compute() -> ShaderStageMask {
        ShaderStageMask::COMPUTE
    }
    /// All raytracing pipeline stages.
    pub fn raytracing() -> ShaderStageMask {
        ShaderStageMask::RAY_GENERATION
            | ShaderStageMask::INTERSECT
            | ShaderStageMask::CLOSEST_HIT
            | ShaderStageMask::ANY_HIT
            | ShaderStageMask::MISS
            | ShaderStageMask::INTERSECT
    }
}

impl FromStr for ShaderStage {
    type Err = ();

    fn from_str(input: &str) -> Result<ShaderStage, Self::Err> {
        // Be case insensitive for parsing.
        let lower_input = input.to_lowercase();
        match lower_input.as_str() {
            "vertex" => Ok(ShaderStage::Vertex),
            "fragment" | "pixel" => Ok(ShaderStage::Fragment),
            "compute" => Ok(ShaderStage::Compute),
            "tesselationcontrol" | "hull" => Ok(ShaderStage::TesselationControl),
            "tesselationevaluation" | "domain" => Ok(ShaderStage::TesselationEvaluation),
            "mesh" => Ok(ShaderStage::Mesh),
            "task" | "amplification" => Ok(ShaderStage::Task),
            "geometry" => Ok(ShaderStage::Geometry),
            "raygeneration" => Ok(ShaderStage::RayGeneration),
            "closesthit" => Ok(ShaderStage::ClosestHit),
            "anyhit" => Ok(ShaderStage::AnyHit),
            "callable" => Ok(ShaderStage::Callable),
            "miss" => Ok(ShaderStage::Miss),
            "intersect" => Ok(ShaderStage::Intersect),
            _ => Err(()),
        }
    }
}
impl ToString for ShaderStage {
    fn to_string(&self) -> String {
        match self {
            ShaderStage::Vertex => "vertex".to_string(),
            ShaderStage::Fragment => "fragment".to_string(),
            ShaderStage::Compute => "compute".to_string(),
            ShaderStage::TesselationControl => "tesselationcontrol".to_string(),
            ShaderStage::TesselationEvaluation => "tesselationevaluation".to_string(),
            ShaderStage::Mesh => "mesh".to_string(),
            ShaderStage::Task => "task".to_string(),
            ShaderStage::Geometry => "geometry".to_string(),
            ShaderStage::RayGeneration => "raygeneration".to_string(),
            ShaderStage::ClosestHit => "closesthit".to_string(),
            ShaderStage::AnyHit => "anyhit".to_string(),
            ShaderStage::Callable => "callable".to_string(),
            ShaderStage::Miss => "miss".to_string(),
            ShaderStage::Intersect => "intersect".to_string(),
        }
    }
}

impl FromStr for ShadingLanguage {
    type Err = ();

    fn from_str(input: &str) -> Result<ShadingLanguage, Self::Err> {
        match input {
            "wgsl" => Ok(ShadingLanguage::Wgsl),
            "hlsl" => Ok(ShadingLanguage::Hlsl),
            "glsl" => Ok(ShadingLanguage::Glsl),
            _ => Err(()),
        }
    }
}
impl ToString for ShadingLanguage {
    fn to_string(&self) -> String {
        String::from(match &self {
            ShadingLanguage::Wgsl => "wgsl",
            ShadingLanguage::Hlsl => "hlsl",
            ShadingLanguage::Glsl => "glsl",
        })
    }
}

/// Generic tag to define a language to be used in template situations
pub trait ShadingLanguageTag {
    /// Get the language of the tag.
    fn get_language() -> ShadingLanguage;
}

/// Hlsl tag
pub struct HlslShadingLanguageTag {}
impl ShadingLanguageTag for HlslShadingLanguageTag {
    fn get_language() -> ShadingLanguage {
        ShadingLanguage::Hlsl
    }
}
/// Glsl tag
pub struct GlslShadingLanguageTag {}
impl ShadingLanguageTag for GlslShadingLanguageTag {
    fn get_language() -> ShadingLanguage {
        ShadingLanguage::Glsl
    }
}
/// Wgsl tag
pub struct WgslShadingLanguageTag {}
impl ShadingLanguageTag for WgslShadingLanguageTag {
    fn get_language() -> ShadingLanguage {
        ShadingLanguage::Wgsl
    }
}

/// All HLSL shader model existing.
///
/// Note that DXC only support shader model up to 6.0, and FXC is not supported.
/// So shader model below 6 are only present for documentation purpose.
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum HlslShaderModel {
    ShaderModel1,
    ShaderModel1_1,
    ShaderModel1_2,
    ShaderModel1_3,
    ShaderModel1_4,
    ShaderModel2,
    ShaderModel3,
    ShaderModel4,
    ShaderModel4_1,
    ShaderModel5,
    ShaderModel5_1,
    ShaderModel6,
    ShaderModel6_1,
    ShaderModel6_2,
    ShaderModel6_3,
    ShaderModel6_4,
    ShaderModel6_5,
    ShaderModel6_6,
    ShaderModel6_7,
    #[default]
    ShaderModel6_8,
}

impl HlslShaderModel {
    /// Get first shader model version
    pub fn earliest() -> HlslShaderModel {
        HlslShaderModel::ShaderModel1
    }
    /// Get last shader model version
    pub fn latest() -> HlslShaderModel {
        HlslShaderModel::ShaderModel6_8
    }
}

/// All HLSL version supported
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum HlslVersion {
    V2016,
    V2017,
    V2018,
    #[default]
    V2021,
}

/// Hlsl compilation parameters for DXC.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct HlslCompilationParams {
    pub shader_model: HlslShaderModel,
    pub version: HlslVersion,
    pub enable16bit_types: bool,
    pub spirv: bool,
}

/// Glsl target client
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum GlslTargetClient {
    Vulkan1_0,
    Vulkan1_1,
    Vulkan1_2,
    #[default]
    Vulkan1_3,
    OpenGL450,
}

impl GlslTargetClient {
    /// Check if glsl is for OpenGL or Vulkan
    pub fn is_opengl(&self) -> bool {
        match *self {
            GlslTargetClient::OpenGL450 => true,
            _ => false,
        }
    }
}

/// All SPIRV version supported for glsl
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum GlslSpirvVersion {
    SPIRV1_0,
    SPIRV1_1,
    SPIRV1_2,
    SPIRV1_3,
    SPIRV1_4,
    SPIRV1_5,
    #[default]
    SPIRV1_6,
}
/// Glsl compilation parameters for glslang.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct GlslCompilationParams {
    pub client: GlslTargetClient,
    pub spirv: GlslSpirvVersion,
    pub preamble: String,
}

/// Wgsl compilation parameters for naga.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct WgslCompilationParams {}

/// Parameters for includes.
#[derive(Default, Debug, Clone)]
pub struct ShaderContextParams {
    pub defines: HashMap<String, String>,
    pub includes: Vec<PathBuf>,
    pub path_remapping: HashMap<PathBuf, PathBuf>,
}

/// Parameters for compilation
#[derive(Default, Debug, Clone)]
pub struct ShaderCompilationParams {
    pub entry_point: Option<String>,
    pub shader_stage: Option<ShaderStage>,
    pub experimental_macro_expansion: bool,
    pub hlsl: HlslCompilationParams,
    pub glsl: GlslCompilationParams,
    pub wgsl: WgslCompilationParams,
}

/// Generic parameters passed to validation and inspection.
#[derive(Default, Debug, Clone)]
pub struct ShaderParams {
    pub context: ShaderContextParams,
    pub compilation: ShaderCompilationParams,
}