7 releases

0.3.0-pre.3 Oct 19, 2022
0.3.0-pre.2 Sep 30, 2022
0.3.0-pre.1 Jan 19, 2022
0.2.0 Dec 11, 2018
0.1.2 Dec 2, 2018

#316 in Graphics APIs

Download history 4238/week @ 2024-01-01 2608/week @ 2024-01-08 4249/week @ 2024-01-15 4056/week @ 2024-01-22 3765/week @ 2024-01-29 6393/week @ 2024-02-05 3783/week @ 2024-02-12 5121/week @ 2024-02-19 4768/week @ 2024-02-26 2365/week @ 2024-03-04 5600/week @ 2024-03-11 2987/week @ 2024-03-18 3103/week @ 2024-03-25 1737/week @ 2024-04-01 1786/week @ 2024-04-08 2735/week @ 2024-04-15

9,364 downloads per month

MIT license

37KB
725 lines

🥽 shader-prepper

Crates.io Docs

shader-prepper is a shader include parser and crawler. It is mostly aimed at GLSL which doesn't provide include directive support out of the box.

This crate does not implement a full C-like preprocessor, only #include scanning. Other directives are instead copied into the expanded code, so they can be subsequently handled by the shader compiler.

The API supports user-driven include file providers, which enable custom virtual file systems, include paths, and allow build systems to track dependencies.

Source files are not concatenated together, but returned as a Vec of SourceChunk. If a single string is needed, a join over the source strings can be used. Otherwise, the individual chunks can be passed to the graphics API, and source info contained within SourceChunk can then remap the compiler's errors back to the original code.

Example

struct FileIncludeProvider;
impl shader_prepper::IncludeProvider for FileIncludeProvider {
    type IncludeContext = ();

    fn get_include(
        &mut self,
        path: &str,
        _context: &Self::IncludeContext,
    ) -> Result<(String, Self::IncludeContext), crate::BoxedIncludeProviderError> {
        Ok((std::fs::read_to_string(path)?, ()))
    }
}

// ...

let chunks = shader_prepper::process_file("myfile.glsl", &mut FileIncludeProvider, ());

License: MIT

Dependencies

~0.3–1.3MB
~26K SLoC