module CrShader::Language

Overview

.crshader source files compile at build-time or on-demand inside the Godot Editor into high-performance Godot GDShader code (for 2D canvas, 3D spatial, sky, fog, and particle shaders) or Vulkan GLSL Compute code (for compute shaders and GPGPU tasks).

Submodule Reference & Language Architecture

Module Description Key Features
Language::PreProcessors Compiler directives, storage qualifiers, uniform hints, and render modes. shader_type, render_mode, uniform, varying, buffer, image2d
Language::Stages Pipeline execution entry points and processor functions. vertex, fragment, light, start, process, sky, fog, main
Language::Functions Mathematical, geometric, matrix, texture, compute, and std library functions. sin, cos, clamp, mix, texture, fresnel, imageStore
Language::Types Shader primitive vectors, matrices, samplers, colors, and qualifiers. Vec2..4, IVec2..4, Mat2..4, Sampler2D..3D, Color
Language::Builtins Engine-provided built-in variables scoped across all shader stages. CanvasItem, Spatial, Particles, Sky, Fog, Compute
Language::Std Standard library modules available via require "std/...". Math, Noise, Color, Lighting, Sdf, Tonemap, PostProcessing

Complete Shader Example

shader_type :spatial
render_mode :cull_disabled, :depth_draw_opaque

uniform albedo : Color = Color.new(0.2, 0.6, 1.0, 1.0), hint: :source_color
uniform roughness : Float32 = 0.3, hint: hint_range(0.0, 1.0)
uniform metallic : Float32 = 0.8, hint: hint_range(0.0, 1.0)
uniform wave_speed : Float32 = 2.0

varying v_world_normal : Vec3

def vertex
  v_world_normal = (MODEL_MATRIX * vec4(NORMAL, 0.0)).xyz
  VERTEX.y += sin(TIME * wave_speed + VERTEX.x) * 0.15
end

def fragment
  ALBEDO = albedo.rgb
  ROUGHNESS = roughness
  METALLIC = metallic
end

Included Modules

Defined in:

crshader/language.cr
crshader/language/builtins.cr
crshader/language/functions.cr
crshader/language/preprocessors.cr
crshader/language/stages.cr
crshader/language/std.cr
crshader/language/types.cr

Instance methods inherited from module CrShader::Language::Functions

abs(x : Float32) : Float32 abs, aces_tonemap(x : Types::Vec3) : Types::Vec3 aces_tonemap, acos(x : Float32) : Float32 acos, adjust_contrast(c : Types::Vec3, contrast : Float32) : Types::Vec3 adjust_contrast, adjust_saturation(c : Types::Vec3, saturation : Float32) : Types::Vec3 adjust_saturation, asin(x : Float32) : Float32 asin, atan(y : Float32, x : Float32) : Float32
atan(y_over_x : Float32) : Float32
atan
, barrel_distortion(uv : Types::Vec2, k : Float32) : Types::Vec2 barrel_distortion, blinn_phong(normal : Types::Vec3, light_dir : Types::Vec3, view_dir : Types::Vec3, shininess : Float32) : Float32 blinn_phong, ceil(x : Float32) : Float32 ceil, cel_shade(n_dot_l : Float32, steps : Float32 = 3.0_f32) : Float32 cel_shade, clamp(x : Float32, min_val : Float32, max_val : Float32) : Float32 clamp, cos(x : Float32) : Float32 cos, cross(a : Types::Vec3, b : Types::Vec3) : Types::Vec3 cross, degrees(rad : Float32) : Float32 degrees, determinant(m : Types::Mat3 | Types::Mat4) : Float32 determinant, distance(p0 : Types::Vec2 | Types::Vec3, p1 : Types::Vec2 | Types::Vec3) : Float32 distance, dot(a : Types::Vec2, b : Types::Vec2) : Float32
dot(a : Types::Vec3, b : Types::Vec3) : Float32
dot(a : Types::Vec4, b : Types::Vec4) : Float32
dot
, exp(x : Float32) : Float32 exp, exp2(x : Float32) : Float32 exp2, faceforward(n : Types::Vec3, i : Types::Vec3, nref : Types::Vec3) : Types::Vec3 faceforward, fbm(p : Types::Vec2, octaves : Int32 = 4) : Float32 fbm, floor(x : Float32) : Float32 floor, fract(x : Float32) : Float32 fract, fresnel(power : Float32, normal : Types::Vec3, view : Types::Vec3) : Float32 fresnel, grayscale(c : Types::Vec3) : Float32 grayscale, groupMemoryBarrier : Nil groupMemoryBarrier, hash11(p : Float32) : Float32 hash11, hash12(p : Float32) : Types::Vec2 hash12, hash21(p : Types::Vec2) : Float32 hash21, hash22(p : Types::Vec2) : Types::Vec2 hash22, hash31(p : Types::Vec3) : Float32 hash31, hash33(p : Types::Vec3) : Types::Vec3 hash33, hsv2rgb(c : Types::Vec3) : Types::Vec3 hsv2rgb, imageLoad(image : Symbol | String, coord : Types::IVec2) : Types::Vec4 imageLoad, imageSize(image : Symbol | String) : Types::IVec2 imageSize, imageStore(image : Symbol | String, coord : Types::IVec2, color : Types::Vec4) : Nil imageStore, inverse(m : Types::Mat3) : Types::Mat3
inverse(m : Types::Mat4) : Types::Mat4
inverse
, inversesqrt(x : Float32) : Float32 inversesqrt, ivec2(x : Int32 = 0, y : Int32 = 0) : Types::IVec2
ivec2(v2 : Types::Vec2) : Types::IVec2
ivec2
, ivec3(x : Int32 = 0, y : Int32 = 0, z : Int32 = 0) : Types::IVec3 ivec3, ivec4(x : Int32 = 0, y : Int32 = 0, z : Int32 = 0, w : Int32 = 0) : Types::IVec4 ivec4, length(v : Types::Vec2 | Types::Vec3 | Types::Vec4) : Float32 length, lerp(a : Float32, b : Float32, t : Float32) : Float32 lerp, linear_depth(depth : Float32, inv_proj : Types::Mat4) : Float32 linear_depth, log(x : Float32) : Float32 log, log2(x : Float32) : Float32 log2, luminance(col : Types::Vec3) : Float32 luminance, mat2(col0 : Types::Vec2, col1 : Types::Vec2) : Types::Mat2 mat2, mat3(col0 : Types::Vec3, col1 : Types::Vec3, col2 : Types::Vec3) : Types::Mat3 mat3, mat4(col0 : Types::Vec4, col1 : Types::Vec4, col2 : Types::Vec4, col3 : Types::Vec4) : Types::Mat4 mat4, max(a : Float32, b : Float32) : Float32 max, memoryBarrier : Nil memoryBarrier, memoryBarrierImage : Nil memoryBarrierImage, memoryBarrierShared : Nil memoryBarrierShared, min(a : Float32, b : Float32) : Float32 min, mix(x : Float32, y : Float32, a : Float32) : Float32
mix(x : Types::Vec2, y : Types::Vec2, a : Float32) : Types::Vec2
mix(x : Types::Vec3, y : Types::Vec3, a : Float32) : Types::Vec3
mix(x : Types::Vec4, y : Types::Vec4, a : Float32) : Types::Vec4
mix(x : Types::Vec3, y : Types::Vec3, a : Types::Vec3) : Types::Vec3
mix
, mod(x : Float32, y : Float32) : Float32 mod, normalize(v : Types::Vec2) : Types::Vec2
normalize(v : Types::Vec3) : Types::Vec3
normalize(v : Types::Vec4) : Types::Vec4
normalize
, pow(x : Float32, y : Float32) : Float32 pow, radians(deg : Float32) : Float32 radians, rand(uv : Types::Vec2) : Float32 rand, reflect(i : Types::Vec3, n : Types::Vec3) : Types::Vec3 reflect, refract(i : Types::Vec3, n : Types::Vec3, eta : Float32) : Types::Vec3 refract, reinhard_tonemap(x : Types::Vec3) : Types::Vec3 reinhard_tonemap, remap(val : Float32, in_min : Float32, in_max : Float32, out_min : Float32, out_max : Float32) : Float32 remap, rgb2hsv(c : Types::Vec3) : Types::Vec3 rgb2hsv, rotate_2d(p : Types::Vec2, angle : Float32) : Types::Vec2 rotate_2d, round(x : Float32) : Float32 round, saturate(x : Float32) : Float32 saturate, sdf_box(p : Types::Vec3, b : Types::Vec3) : Float32 sdf_box, sdf_sphere(p : Types::Vec3, radius : Float32) : Float32 sdf_sphere, sign(x : Float32) : Float32 sign, simplex_noise_2d(p : Types::Vec2) : Float32 simplex_noise_2d, sin(x : Float32) : Float32 sin, smax(a : Float32, b : Float32, k : Float32 = 0.1_f32) : Float32 smax, smin(a : Float32, b : Float32, k : Float32 = 0.1_f32) : Float32 smin, smoothstep(edge0 : Float32, edge1 : Float32, x : Float32) : Float32 smoothstep, sqrt(x : Float32) : Float32 sqrt, step(edge : Float32, x : Float32) : Float32 step, tan(x : Float32) : Float32 tan, texelFetch(sampler : Types::Sampler2D, coord : Types::IVec2, lod : Int32 = 0) : Types::Vec4 texelFetch, texture(sampler : Types::Sampler2D, uv : Types::Vec2) : Types::Vec4 texture, textureLod(sampler : Types::Sampler2D, uv : Types::Vec2, lod : Float32) : Types::Vec4 textureLod, textureSize(sampler : Types::Sampler2D, lod : Int32 = 0) : Types::IVec2 textureSize, transpose(m : Types::Mat3) : Types::Mat3
transpose(m : Types::Mat4) : Types::Mat4
transpose
, triplanar_weights(normal : Types::Vec3) : Types::Vec3 triplanar_weights, trunc(x : Float32) : Float32 trunc, uvec2(x : UInt32 = 0_u32, y : UInt32 = 0_u32) : Types::UVec2 uvec2, uvec3(x : UInt32 = 0_u32, y : UInt32 = 0_u32, z : UInt32 = 0_u32) : Types::UVec3 uvec3, uvec4(x : UInt32 = 0_u32, y : UInt32 = 0_u32, z : UInt32 = 0_u32, w : UInt32 = 0_u32) : Types::UVec4 uvec4, value_noise(p : Types::Vec2) : Float32 value_noise, vec2(x : Float32 = 0.0_f32, y : Float32 = 0.0_f32) : Types::Vec2
vec2(x : Number, y : Number) : Types::Vec2
vec2
, vec3(x : Float32 = 0.0_f32, y : Float32 = 0.0_f32, z : Float32 = 0.0_f32) : Types::Vec3
vec3(x : Number, y : Number, z : Number) : Types::Vec3
vec3
, vec4(x : Float32 = 0.0_f32, y : Float32 = 0.0_f32, z : Float32 = 0.0_f32, w : Float32 = 1.0_f32) : Types::Vec4
vec4(x : Number, y : Number, z : Number, w : Number = 1.0) : Types::Vec4
vec4(v3 : Types::Vec3, w : Float32 = 1.0_f32) : Types::Vec4
vec4(val : Float32 | Number) : Types::Vec4
vec4
, vignette(uv : Types::Vec2, radius : Float32 = 0.75_f32, softness : Float32 = 0.45_f32) : Float32 vignette, voronoi(p : Types::Vec2) : Float32 voronoi

Instance methods inherited from module CrShader::Language::Stages

fog(&) : Nil fog, fragment(&) : Nil fragment, light(&) : Nil light, main(&) : Nil main, process(&) : Nil process, sky(&) : Nil sky, start(&) : Nil start, vertex(&) : Nil vertex

Instance methods inherited from module CrShader::Language::PreProcessors

group(name : String) : Nil group, local_size(x : Int32 = 8, y : Int32 = 8, z : Int32 = 1) : Nil local_size, render_mode(*modes : Symbol) : Nil render_mode, setup_gdshader : Nil setup_gdshader, shader_type(mode : Symbol) : Nil shader_type, subgroup(name : String) : Nil subgroup

Macros inherited from module CrShader::Language::PreProcessors

buffer(name, **options, &block) buffer, const(declaration) const, constant(declaration) constant, global_uniform(declaration, **options) global_uniform, image2d(name, **options) image2d, include(path) include, instance_uniform(declaration, **options) instance_uniform, push_constant(name, **options, &block) push_constant, require(path) require, shared(declaration) shared, uniform(declaration, **options) uniform, varying(declaration, **options) varying