include_shader!() { /* proc-macro */ }
Expand description

Includes a shader file as a string with dependencies support.

By default, the file is located relative to the workspace root directory. If the relative-path feature is enabled, then the file is located relative to the current file.

Panics

Panics if:

  • A file specified cannot be found
  • A circular dependency is detected

Examples

use include_shader::include_shader;

fn main() {
   // ...
   let frag_shader = compile_shader(
       &context,
       WebGl2RenderingContext::FRAGMENT_SHADER,
       include_shader!("src/shaders/fragment_shader.glsl"),
   )?;
   // ...
}

Dependencies

Dependencies are supported within shader files using the #include preprocessor directive.

rand.glsl:

float rand(vec2 co) {
    return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
}

fragment_shader.glsl:

uniform vec2 u_resolution;

#include "./src/shaders/functions/rand.glsl"

void main() {
   vec2 st = gl_FragCoord.xy / u_resolution.xy;

   gl_FragColor = vec4(vec3(rand(st)), 1.0);
}