Skip to main content

alpha2_render/
shader_inventory.rs

1//! The complete WGSL the renderer hands to the GPU, for checking it with
2//! compilers other than the one this process links.
3//!
4//! wgpu validates WGSL with naga, which skips the fragment-stage uniformity
5//! rule that Tint (Chrome) and `WebKit` (Safari) enforce. A shader naga accepts
6//! can therefore fail to compile in a browser; these modules are what a
7//! browser-side check compiles.
8
9use alpha2_shaders::{
10    CANVAS_PRESENT_WGSL, COMPOSITE_WGSL, FULLSCREEN_TEXTURE_QUAD_WGSL, READBACK_PACK_WGSL,
11    RGB_TO_YUV420_WGSL, RGBA_PRESENT_WGSL, YUV_REPACK_WGSL, YUV_TO_LINEAR_WGSL,
12    YUV16_TO_LINEAR_WGSL,
13};
14
15use crate::EffectDef;
16use crate::registry::build_shader;
17
18/// Returns the complete module the renderer compiles for `definition`: the
19/// generated preamble for its parameters and resources, followed by its body.
20#[must_use]
21pub fn effect_wgsl(definition: &EffectDef) -> String {
22    build_shader(definition)
23}
24
25/// Returns every WGSL module the renderer can create, labelled: the built-in
26/// pipeline modules and the generated module of every built-in effect and
27/// transition.
28#[must_use]
29pub fn builtin_wgsl_modules() -> Vec<(String, String)> {
30    let mut modules = [
31        // The browser binding's canvas presenter: it runs only in a browser, so a browser
32        // compiler is all that checks it.
33        ("canvas-present", CANVAS_PRESENT_WGSL),
34        ("fullscreen-quad", FULLSCREEN_TEXTURE_QUAD_WGSL),
35        ("composite", COMPOSITE_WGSL),
36        ("readback-pack", READBACK_PACK_WGSL),
37        ("rgba-present", RGBA_PRESENT_WGSL),
38        ("rgb-to-yuv420", RGB_TO_YUV420_WGSL),
39        ("yuv-repack", YUV_REPACK_WGSL),
40        ("yuv-to-linear", YUV_TO_LINEAR_WGSL),
41        // The same module built for 16-bit containers uploaded as integers.
42        ("yuv16-to-linear", YUV16_TO_LINEAR_WGSL),
43    ]
44    .map(|(label, source)| (label.to_owned(), source.to_owned()))
45    .to_vec();
46    // The vector pass: tiles, layers, and the linear-light encode in one module.
47    modules.push(("vector".to_owned(), crate::vector::vector_wgsl()));
48    let mut definitions = crate::builtins::internal_effect_definitions();
49    definitions.extend(crate::builtin_effect_definitions());
50    definitions.extend(crate::builtin_transition_definitions());
51    modules.extend(
52        definitions
53            .iter()
54            .map(|definition| (definition.id.clone(), effect_wgsl(definition))),
55    );
56    modules
57}
58
59#[cfg(test)]
60#[path = "shader_inventory_tests.rs"]
61mod tests;