glslint 0.8.0

A GLSL checker and language server for WebGL shader toolkits (luma.gl/deck.gl, maplibre, ShaderToy) and any project
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! The core trick: luma.gl/deck.gl shaders aren't standalone translation units.
//! They reference UBO instances (`wind.*`, `blit.*`) declared in separate module
//! fragments and deck builtins (`project_position_to_clipspace`) injected at link
//! time. This module splices those in to form a complete `#version 300 es` unit a
//! validator will accept, while recording a per-line map back to the originals so
//! diagnostics land where the author can act on them.

use crate::config::Config;
use std::path::{Path, PathBuf};

/// Shader stage, inferred from the filename. Maps to glslangValidator's `-S` arg.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stage {
    Vertex,
    Fragment,
    Compute,
}

impl Stage {
    /// The `-S <stage>` argument glslangValidator expects.
    pub fn glslang_stage(self) -> &'static str {
        match self {
            Stage::Vertex => "vert",
            Stage::Fragment => "frag",
            Stage::Compute => "comp",
        }
    }
}

/// Used when the target has no `#version` of its own. WebGL2/luma shaders are
/// GLSL ES 3.00; glslangValidator validates that profile natively (combined
/// samplers, combined-sampler function params, and all) with no source rewrites.
const DEFAULT_VERSION: &str = "#version 300 es";

/// Injected right after the (hoisted) `#version`, before any prelude. GLSL ES
/// fragment shaders have no default `float` precision, and the deck prelude stubs
/// reference `float`/`vec*` ahead of the target's own `precision` statement — so
/// we set defaults up front. Re-declaring them later (as the shaders do) is legal.
const DEFAULT_PRECISION: &str = "precision highp float;\nprecision highp int;";

/// Where an assembled line came from. `line` is 1-based into `path`.
#[derive(Debug, Clone)]
pub struct Loc {
    pub path: PathBuf,
    pub line: u32,
}

pub struct Assembled {
    pub source: String,
    pub stage: Stage,
    /// One entry per assembled line: assembled line `i+1` -> `map[i]`. `None` for
    /// synthetic/injected-prelude lines we own (errors there are dropped).
    pub map: Vec<Option<Loc>>,
    pub target: PathBuf,
    /// Set when the file was wrapped because it's a module fragment, not a stage.
    /// Reserved for an info-level diagnostic once the LSP grows one.
    #[allow(dead_code)]
    pub note: Option<&'static str>,
}

struct Builder {
    lines: Vec<String>,
    map: Vec<Option<Loc>>,
}

impl Builder {
    fn new() -> Self {
        Builder {
            lines: Vec::new(),
            map: Vec::new(),
        }
    }
    fn push(&mut self, line: String, loc: Option<Loc>) {
        self.lines.push(line);
        self.map.push(loc);
    }
    /// Append a block from `path`, mapping each line back to it.
    fn push_block(&mut self, content: &str, path: &Path) {
        for (i, l) in content.lines().enumerate() {
            self.push(
                l.to_string(),
                Some(Loc {
                    path: path.to_path_buf(),
                    line: line_no(i),
                }),
            );
        }
    }
    /// Append lines we synthesized; errors here map nowhere.
    fn push_synthetic(&mut self, content: &str) {
        for l in content.lines() {
            self.push(l.to_string(), None);
        }
    }
    fn finish(self, stage: Stage, target: &Path, note: Option<&'static str>) -> Assembled {
        let mut source = self.lines.join("\n");
        source.push('\n');
        Assembled {
            source,
            stage,
            map: self.map,
            target: target.to_path_buf(),
            note,
        }
    }
}

/// Infer the shader stage from the filename. `None` => not a stage shader (a
/// module fragment like `windUniforms.glsl`), which we wrap for syntax-checking.
pub fn detect_stage(path: &Path) -> Option<Stage> {
    let name = path.file_name()?.to_str()?;
    let n = name.to_ascii_lowercase();
    if n.contains(".vert.")
        || n.contains(".vertex.")
        || n.ends_with(".vert")
        || n.ends_with(".vertex")
        || n.ends_with(".vs")
    {
        Some(Stage::Vertex)
    } else if n.contains(".frag.")
        || n.contains(".fragment.")
        || n.ends_with(".frag")
        || n.ends_with(".fragment")
        || n.ends_with(".fs")
    {
        Some(Stage::Fragment)
    } else if n.contains(".comp.")
        || n.contains(".compute.")
        || n.ends_with(".comp")
        || n.ends_with(".compute")
    {
        Some(Stage::Compute)
    } else {
        None
    }
}

/// 1-based line number for a 0-based index. Saturating, so a pathological
/// (>4-billion-line) file can't wrap a line number into a wrong-but-plausible one.
pub(crate) fn line_no(i: usize) -> u32 {
    u32::try_from(i).unwrap_or(u32::MAX).saturating_add(1)
}

pub fn assemble(target: &Path, source: &str, config: &Config) -> Assembled {
    match detect_stage(target) {
        Some(stage) => assemble_stage(target, source, config, stage),
        None => wrap_fragment(target, source),
    }
}

/// Assemble a shader lifted from a JS/TS tagged template. The stage can't come
/// from the filename here (the host is a `.ts`/`.js` file), so it's passed in —
/// inferred from the shader's own text/binding by `embed`. `has_entry` is whether
/// the template has its own `main`: with one, it's a full stage shader; without,
/// it's a chunk wrapped in a synthetic `main` for syntax-only checking. Either way
/// the wrap/validate happens under `stage`, so a vertex-only builtin (e.g.
/// `gl_VertexID`) in a `gl_Position`-free shader isn't rejected under the fragment
/// stage. `target` is the host file, so every mapped line points there; `embed`
/// then offsets those lines into the template's span.
pub fn assemble_embedded(
    target: &Path,
    source: &str,
    config: &Config,
    stage: Stage,
    has_entry: bool,
) -> Assembled {
    if has_entry {
        assemble_stage(target, source, config, stage)
    } else {
        wrap_as(target, source, stage)
    }
}

fn assemble_stage(target: &Path, source: &str, config: &Config, stage: Stage) -> Assembled {
    let mut b = Builder::new();
    let lines: Vec<&str> = source.lines().collect();

    // Resolve the shader dialect (maplibre pragmas, shadertoy uniforms, project
    // rules) for this shader — detection keys on the source and its directory.
    // `None` => no dialect handling, the deck/luma path.
    let dir = target.parent().unwrap_or(Path::new("."));
    let dialect = crate::dialect::resolve(&config.dialect, source, dir);

    // A `#version` directive must precede all code, so hoist the target's own to
    // the top (it's dropped from the body below) and map it back to its real line
    // so a version error still points home. Default it when absent. Default
    // precision follows, before any prelude — see DEFAULT_PRECISION.
    let vidx = lines
        .iter()
        .position(|l| l.trim_start().starts_with("#version"));
    match vidx {
        Some(i) => b.push(
            lines[i].to_string(),
            Some(Loc {
                path: target.to_path_buf(),
                line: line_no(i),
            }),
        ),
        None => b.push_synthetic(DEFAULT_VERSION),
    }
    b.push_synthetic(DEFAULT_PRECISION);

    // A dialect's prelude declares its implicit globals (shadertoy's `iTime` &c.).
    // A deck dialect's prelude is the stub fallback, handled in the deck block below
    // (node_modules signatures supersede it), so it's skipped here.
    if let Some(d) = dialect.as_ref().filter(|d| !d.deck)
        && let Some(p) = d.prelude(stage)
    {
        b.push_synthetic(p);
    }
    if let Some(d) = &dialect
        && let Some(p) = &d.prelude_extra
    {
        b.push_synthetic(p);
    }

    // Default the `#define`s the project injects from JS at build time (maplibre's
    // `NUM_ILLUMINATION_SOURCES`), `#ifndef`-guarded so the real build's value still
    // wins. A preset lists them (`define_names`, fast); a preset may also opt in to
    // discovering them by scanning the project — only names it actually injects, so
    // a macro typo is still flagged.
    if let Some(d) = dialect.as_ref().filter(|d| !d.deck) {
        let mut defines = d.define_names.clone();
        if d.discover_defines {
            defines.extend(crate::discover::injected_defines(dir));
        }
        for name in defines {
            b.push_synthetic(&format!("#ifndef {name}\n#define {name} 1\n#endif"));
        }
    }

    // The shared shader library: the files a project's build concatenates ahead of
    // every shader (maplibre's `_prelude`/`_projection_*`, luma's UBO fragments), so
    // `projectTile` &c. resolve. A preset can name them (fast); otherwise they are
    // DISCOVERED by structure — any no-`main` `.glsl` sibling is a library — so this
    // works on an unfamiliar project with no filename baked into the linter.
    // Injected verbatim, mapped to their own path (a diagnostic inside one lands
    // there). Skipped when the project wired modules explicitly (`config.modules`,
    // e.g. luma `[[shader]]` bindings), for a deck dialect, or when the shader uses
    // `#include` — an explicit dependency list, which (like modules) supersedes
    // discovery so an included file isn't also injected as a library.
    let uses_include = lines.iter().any(|l| crate::include::parse(l).is_some());
    if dialect.as_ref().is_none_or(|d| !d.deck) && config.modules.is_empty() && !uses_include {
        let explicit: Vec<PathBuf> = dialect
            .as_ref()
            .map(|d| d.prelude_files(stage))
            .filter(|f| !f.is_empty())
            .map(|f| f.iter().map(|n| dir.join(n)).collect())
            .unwrap_or_default();
        let libraries = if explicit.is_empty() {
            crate::discover::shared_libraries(dir, stage, target)
        } else {
            explicit
        };
        for path in libraries {
            if same_file(&path, target) {
                continue;
            }
            if let Ok(c) = std::fs::read_to_string(&path) {
                b.push_block(&c, &path);
            }
        }
    }

    // deck builtins apply when a deck dialect is active (detected or selected), or
    // — with no dialect — when the config/luma derivation asks for them. A non-deck
    // dialect (maplibre, shadertoy) turns them off.
    let deck_active = match &dialect {
        Some(d) => d.deck,
        None => config.use_builtin_prelude,
    };
    if deck_active {
        // Prefer deck's real project functions from node_modules (extracted as
        // empty-body stubs); fall back to the `deck` preset's stub prelude.
        let fns = crate::deck::project_fns(target.parent().unwrap_or(Path::new(".")));
        if fns.is_empty() {
            b.push_synthetic(crate::preset::deck_prelude());
        } else {
            b.push_synthetic(&crate::deck::stubs(&fns));
        }
    }
    for p in &config.preludes {
        if let Ok(c) = std::fs::read_to_string(p) {
            b.push_block(&c, p);
        }
    }
    // Inject every configured module fragment except the file under check.
    for m in &config.modules {
        if same_file(m, target) {
            continue;
        }
        if let Ok(c) = std::fs::read_to_string(m) {
            b.push_block(&c, m);
        }
    }

    // The rest of the original, every line except the hoisted `#version`. Two line
    // rewrites can apply: a `#include` is spliced (resolved by glslint, since
    // glslang can't over stdin), and a dialect may expand a directive into several
    // declarations. Each spliced/expanded line keeps the loc of its true source, so
    // a diagnostic lands on the included file or the author's `#pragma`.
    let mut included: Vec<PathBuf> = target.canonicalize().ok().into_iter().collect();
    for (i, l) in lines.iter().enumerate() {
        if Some(i) == vidx {
            continue;
        }
        let loc = Loc {
            path: target.to_path_buf(),
            line: line_no(i),
        };
        if let Some(spliced) = crate::include::expand(l, dir, &mut included) {
            for (line, iloc) in spliced {
                b.push(line, Some(iloc));
            }
            continue;
        }
        match dialect.as_ref().and_then(|d| d.expand_line(l, stage)) {
            Some(expanded) => {
                for e in expanded {
                    b.push(e, Some(loc.clone()));
                }
            }
            None => b.push(l.to_string(), Some(loc)),
        }
    }

    // A dialect epilogue (shadertoy's `main` driving `mainImage`) is appended only
    // when the source has no entry point of its own.
    if let Some(d) = &dialect
        && let Some(ep) = &d.epilogue
        && !crate::dialect::has_main(source)
    {
        b.push_synthetic(ep);
    }

    b.finish(stage, target, None)
}

/// A module fragment (a bare UBO block, no stage / no `main`) can't be a shader
/// on its own. Wrap it in a minimal fragment shell so its declarations still get
/// a real syntax/type pass.
fn wrap_fragment(target: &Path, source: &str) -> Assembled {
    wrap_as(target, source, Stage::Fragment)
}

/// Wrap a fragment (no `main` of its own) in a minimal shell under `stage`, so its
/// declarations get a syntax/type pass. The stage matters: a helper that uses a
/// vertex-only builtin must be wrapped as a vertex shader, not a fragment one.
fn wrap_as(target: &Path, source: &str, stage: Stage) -> Assembled {
    let mut b = Builder::new();
    b.push_synthetic(DEFAULT_VERSION);
    b.push_synthetic(DEFAULT_PRECISION);
    for (i, l) in source.lines().enumerate() {
        b.push(
            l.to_string(),
            Some(Loc {
                path: target.to_path_buf(),
                line: line_no(i),
            }),
        );
    }
    b.push_synthetic("void main() {}");
    b.finish(stage, target, Some("module fragment (syntax-only)"))
}

/// True if two paths point at the same file. Canonicalize when possible; fall
/// back to a literal compare for not-yet-existing paths.
fn same_file(a: &Path, b: &Path) -> bool {
    match (a.canonicalize(), b.canonicalize()) {
        (Ok(x), Ok(y)) => x == y,
        _ => a == b,
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)] // test code: unwrap IS the assertion
mod tests {
    use super::*;

    fn no_config() -> Config {
        Config {
            preludes: vec![],
            modules: vec![],
            use_builtin_prelude: false,
            dialect: crate::dialect::Preference::default(),
        }
    }

    #[test]
    fn detect_stage_reads_the_filename() {
        assert_eq!(
            detect_stage(Path::new("draw.vert.glsl")),
            Some(Stage::Vertex)
        );
        assert_eq!(
            detect_stage(Path::new("draw.frag.glsl")),
            Some(Stage::Fragment)
        );
        assert_eq!(
            detect_stage(Path::new("sim.comp.glsl")),
            Some(Stage::Compute)
        );
        // The spelled-out convention (maplibre-gl-js and friends).
        assert_eq!(
            detect_stage(Path::new("line.vertex.glsl")),
            Some(Stage::Vertex)
        );
        assert_eq!(
            detect_stage(Path::new("line.fragment.glsl")),
            Some(Stage::Fragment)
        );
        // A bare module fragment is not a stage shader.
        assert_eq!(detect_stage(Path::new("windUniforms.glsl")), None);
    }

    #[test]
    fn spelled_out_vertex_stage_is_not_wrapped_as_fragment() {
        // A vertex-stage integer input needs no `flat`; the None fallback wraps as
        // a fragment shader, where the same declaration is an error. Regression
        // for maplibre-style `*.vertex.glsl` naming.
        let src = "#version 300 es\nlayout(location = 0) in ivec2 a_pos_normal;\nvoid main() { gl_Position = vec4(vec2(a_pos_normal >> 1), 0.0, 1.0); }\n";
        let a = assemble(Path::new("line.vertex.glsl"), src, &no_config());
        assert_eq!(a.stage, Stage::Vertex);
    }

    #[test]
    fn line_map_has_exactly_one_entry_per_assembled_line() {
        // The whole diagnostic translation indexes `map[asm_line - 1]`, so the map
        // must stay 1:1 with the assembled source's lines.
        let src =
            "#version 300 es\nprecision highp float;\nout vec4 c;\nvoid main(){ c = vec4(1.0); }\n";
        let a = assemble(Path::new("draw.frag.glsl"), src, &no_config());
        assert_eq!(a.source.lines().count(), a.map.len());
    }

    #[test]
    fn version_is_hoisted_and_mapped_to_its_original_line() {
        let src = "#version 300 es\nout vec4 c;\nvoid main(){ c = vec4(1.0); }\n";
        let a = assemble(Path::new("draw.frag.glsl"), src, &no_config());
        assert!(a.source.starts_with("#version 300 es"));
        // Assembled line 1 (the hoisted directive) maps back to original line 1.
        assert_eq!(a.map[0].as_ref().unwrap().line, 1);
    }

    #[test]
    fn default_precision_is_injected() {
        let src = "#version 300 es\nout vec4 c;\nvoid main(){}\n";
        let a = assemble(Path::new("draw.frag.glsl"), src, &no_config());
        assert!(a.source.contains("precision highp float;"));
    }

    #[test]
    fn maplibre_pragma_is_expanded_and_stays_one_to_one_mapped() {
        // Auto-detected maplibre define/initialize expand into real declarations,
        // and every expanded line still has exactly one map entry so the diagnostic
        // translation can't drift.
        let src = "#pragma maplibre: define lowp float opacity\n\
                   void main() {\n\
                   #pragma maplibre: initialize lowp float opacity\n\
                   gl_Position = vec4(opacity);\n\
                   }\n";
        let a = assemble(Path::new("line.vertex.glsl"), src, &no_config());
        assert_eq!(a.stage, Stage::Vertex);
        assert_eq!(a.source.lines().count(), a.map.len());
        // The define produced the attribute/varying; the pragma line itself is gone.
        assert!(a.source.contains("in lowp float a_opacity;"));
        assert!(a.source.contains("out lowp float opacity;"));
        assert!(!a.source.contains("#pragma maplibre"));
        // An expanded declaration maps back to the pragma's original line (line 1).
        let idx = a
            .source
            .lines()
            .position(|l| l.contains("out lowp float opacity;"))
            .unwrap();
        assert_eq!(a.map[idx].as_ref().unwrap().line, 1);
    }

    #[test]
    fn no_dialect_leaves_a_plain_shader_untouched() {
        // A deck/luma shader without dialect signatures gets no expansion.
        let src = "out vec4 c;\nvoid main(){ c = vec4(1.0); }\n";
        let a = assemble(Path::new("draw.frag.glsl"), src, &no_config());
        assert!(a.source.contains("out vec4 c;"));
        assert_eq!(a.source.lines().count(), a.map.len());
    }

    #[test]
    fn bare_module_fragment_is_wrapped_for_syntax_checking() {
        // No stage in the name → wrapped with a synthetic main, still 1:1 mapped.
        let src = "layout(std140) uniform U { float a; } u;\n";
        let a = assemble(Path::new("windUniforms.glsl"), src, &no_config());
        assert_eq!(a.stage, Stage::Fragment);
        assert!(a.source.contains("void main()"));
        assert_eq!(a.source.lines().count(), a.map.len());
    }

    #[test]
    fn maplibre_injects_its_sibling_shared_library_mapped_to_its_own_file() {
        // A pragma-free maplibre shader next to the `_prelude.*.glsl` library gets
        // that library spliced in (so `projectTile` resolves), each injected line
        // mapped back to the library file — not the shader — and the map stays 1:1.
        let dir = std::env::temp_dir().join(format!(
            "glslint-asm-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
        ));
        std::fs::create_dir_all(&dir).unwrap();
        let lib = "vec4 projectTile(vec2 p) { return vec4(p, 0.0, 1.0); }\n";
        std::fs::write(dir.join("_prelude.vertex.glsl"), lib).unwrap();
        let shader = dir.join("background.vertex.glsl");
        let src = "void main() { gl_Position = projectTile(vec2(0.0)); }\n";

        let a = assemble(&shader, src, &no_config());
        assert!(
            a.source.contains("vec4 projectTile(vec2 p)"),
            "shared library was injected: {}",
            a.source
        );
        assert_eq!(a.source.lines().count(), a.map.len());
        let idx = a
            .source
            .lines()
            .position(|l| l.contains("projectTile(vec2 p)"))
            .unwrap();
        assert!(
            a.map[idx]
                .as_ref()
                .unwrap()
                .path
                .ends_with("_prelude.vertex.glsl")
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    static NEXT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
}