CRShader

CI Version Crystal License: MIT Godot Docs Benchmarks

CRShader is a high-performance shader trans-compiler and live runtime development toolchain for Godot Engine 4.8+, powered by Crystal and Lapis. It introduces a declarative, type-safe Crystal shader DSL that transpiles down to optimized Godot GDShader (4.x) and GLSL compute kernels with native machine speed (~1.1ms transpilation, >100,000 LOC/s).


Architecture

CRShader bridges compile-time Crystal syntax with Godot's runtime rendering pipeline:

graph TD
    subgraph CRShader Toolchain Pipeline
        SRC[".crshader Source File"] --> LEX[Lexer & AST Parser]
        LEX --> AST[Typed Shader AST]
        AST --> GEN_GD[GDShader 4.x Transpiler Backend]
        AST --> GEN_GLSL[GLSL Compute Transpiler Backend]
        GEN_GD --> GD_OUT[Target .gdshader]
        GEN_GLSL --> GLSL_OUT[Target .glsl]
    end

    subgraph Godot Engine Integration
        RFL[ResourceFormatLoaderCRShader] -->|Transparent In-Memory Load| RES[Godot Shader Resource]
        RES --> MAT[ShaderMaterial]
        MAT --> GPU[Godot Vulkan / D3D12 / Metal Pipeline]
        PLUG[CRShaderPlugin] --> HIGHLIGHT[ScriptEditor Syntax Highlighter Hook]
        PLUG --> STUDIO[CRShader Studio Dock Panel]
    end

    SRC -->|Direct Engine Import| RFL

Features


Performance Benchmarks

CRShader includes a quantitative compiler benchmark measuring lexical parsing, AST construction, and transpilation throughput:

Benchmark Target Source Lines (LOC) Transpile Time Throughput Memory Footprint
Complex Post-Processing Suite 1,250 LOC ~1.12 ms > 110,000 LOC/sec < 3.2 MB
Spatial 3D Procedural Plasma 480 LOC ~0.41 ms > 117,000 LOC/sec < 2.1 MB
Kuwahara Multi-Pass Filter 890 LOC ~0.78 ms > 114,000 LOC/sec < 2.8 MB

Run benchmarks locally:

make benchmark
# or directly via Lapis CLI:
lapis benchmarks run html

View the latest automated Interactive Benchmark Report deployed live on GitHub Pages.


DSL Comparison

CRShader Crystal DSL (.crshader) Generated GDShader (.gdshader)
shader_type :canvas_item
render_mode :unshaded
# Uniform parameters
uniform tint : vec4 = vec4(0.2, 0.6, 1.0, 1.0)
uniform speed : float = 1.5
# Fragment stage
stage :fragment do
  uv = UV + vec2(TIME * speed * 0.1, 0.0)
  col = texture(TEXTURE, uv) * tint
  COLOR = col
end
shader_type canvas_item;
render_mode unshaded;
// Uniform parameters
uniform vec4 tint = vec4(0.2, 0.6, 1.0, 1.0);
uniform float speed = 1.5;
// Fragment stage
void fragment() {
    vec2 uv = UV + vec2(TIME * speed * 0.1, 0.0);
    vec4 col = texture(TEXTURE, uv) * tint;
    COLOR = col;
}

Quickstart

1. Installation

Add crshader as a dependency in your shard.yml:

dependencies:
  crshader:
    github: sol-vin/crshader
    branch: master

Run shards install.

2. Command Line Tool (crshader)

Build the standalone CLI compiler:

make
crystal build src/cli.cr -o bin/crshader
Command Description
crshader build <file.crshader> -t gdshader Compile .crshader file to Godot .gdshader.
crshader build <file.crshader> -t glsl Compile compute shader to standard GLSL.
crshader watch <dir> Watch directory and recompile shaders on modification.
crshader stubs -o <path> Generate Crystal IDE completion stubs with mirrored Godot docs.
crshader install-addon <godot_dir> Install GDExtension plugin into a target Godot project.

3. Godot Editor Integration

  1. Launch Godot Editor:
    make editor
  2. Enable the plugin in Project Settings -> Plugins -> CRShader.
  3. Create or drag any .crshader file into the res:// directory.
  4. Open the CRShader Studio bottom panel to inspect, recompile, and preview transpiled output in real time.

Standalone Shader Applications

1. Interactive Shader Viewer

CRShader provides an interactive standalone viewer showcasing 20+ procedural shaders (animated water, CRT scanlines, Kuwahara filter, PSX retro, fire particles, tonemapping, compute kernels) across multiple 3D polygon meshes (Cube, Sphere, Cylinder, Torus, Prism, Plane), screen-space quad post-processing, camera compositor passes, and dynamic uniform tweaking:

make viewer

Packaged standalone viewer executables are available on the Releases page under viewer-windows.zip and viewer-linux.zip.

2. Live Shader Sandbox

CRShader includes a split-screen live coding environment allowing side-by-side editing in Crystal DSL with real-time viewport preview:

make sandbox

Packaged standalone sandbox executables are available on the Releases page under sandbox-windows.zip and sandbox-linux.zip.


Development & Testing

Target Command Description
Build Addon make Compile GDExtension library and sync dependencies.
Run Specs make test Execute Crystal specifications (AST, compiler, stubs, language).
Generate Docs make docs Build HTML API documentation in docs/.
Run Benchmarks make benchmark Execute compiler speed and throughput benchmarks.
Package Plugin make package RELEASE=1 Package redistributable addon zip into dist/crshader.zip.
Launch Viewer make viewer Launch the interactive 20+ shader showcase viewer.
Package Viewer make package-viewer Package standalone viewer application into dist/viewer-windows.zip.
Launch Sandbox make sandbox Launch the split-screen live shader sandbox.
Package Sandbox make package-sandbox Package standalone sandbox application into dist/sandbox-windows.zip.
Package All Targets make package-targets Discover targets from shard.yml and package all release archives.

License

MIT License. Copyright (c) Ian Rash and Sol-Vin contributors.