module CrShader::Language::Functions

Overview

Built-in mathematical, geometric, matrix, texture, compute, and standard library functions.

Mirrored directly from the Godot Engine 4.8 Shading Language specification. All methods transpile 1:1 to hardware-accelerated GPU intrinsic operations.

Extended Modules

Direct including types

Defined in:

crshader/language/functions.cr

Instance Method Summary

Instance Method Detail

def abs(x : Float32) : Float32 #

Returns the absolute value of x.


def aces_tonemap(x : Types::Vec3) : Types::Vec3 #

Standard library: Filmic ACES tonemapping curve for HDR to LDR display.


def acos(x : Float32) : Float32 #

Returns the arccosine of x in radians in range [0, PI].


def adjust_contrast(c : Types::Vec3, contrast : Float32) : Types::Vec3 #

Standard library: Adjusts contrast of an RGB color around midpoint 0.5.


def adjust_saturation(c : Types::Vec3, saturation : Float32) : Types::Vec3 #

Standard library: Adjusts saturation of an RGB color.


def asin(x : Float32) : Float32 #

Returns the arcsine of x in radians in range [-PI/2, PI/2].


def atan(y : Float32, x : Float32) : Float32 #

Returns the arctangent of y/x in radians in range [-PI, PI].


def atan(y_over_x : Float32) : Float32 #

Returns the arctangent of y_over_x in radians in range [-PI/2, PI/2].


def barrel_distortion(uv : Types::Vec2, k : Float32) : Types::Vec2 #

Standard library: Lens barrel / pincushion distortion for UV coordinate.


def blinn_phong(normal : Types::Vec3, light_dir : Types::Vec3, view_dir : Types::Vec3, shininess : Float32) : Float32 #

Standard library: Calculates Blinn-Phong specular highlight lobe.


def ceil(x : Float32) : Float32 #

Returns the smallest integer greater than or equal to x.


def cel_shade(n_dot_l : Float32, steps : Float32 = 3.0_f32) : Float32 #

Standard library: Quantizes lighting dot product into discrete cel/toon shading bands.


def clamp(x : Float32, min_val : Float32, max_val : Float32) : Float32 #

Clamps x to range [min_val, max_val].


def cos(x : Float32) : Float32 #

Returns the cosine of x (in radians).


def cross(a : Types::Vec3, b : Types::Vec3) : Types::Vec3 #

Returns 3D cross product: a x b.


def degrees(rad : Float32) : Float32 #

Converts radians to degrees: rad * (180.0 / PI).


def determinant(m : Types::Mat3 | Types::Mat4) : Float32 #

Calculates scalar determinant of matrix m.


def distance(p0 : Types::Vec2 | Types::Vec3, p1 : Types::Vec2 | Types::Vec3) : Float32 #

Returns Euclidean distance between points p0 and p1: #length(p0 - p1).


def dot(a : Types::Vec2, b : Types::Vec2) : Float32 #

Returns scalar dot product: sum(a[i] * b[i]).


def dot(a : Types::Vec3, b : Types::Vec3) : Float32 #

def dot(a : Types::Vec4, b : Types::Vec4) : Float32 #

def exp(x : Float32) : Float32 #

Returns natural exponential of x (e^x).


def exp2(x : Float32) : Float32 #

Returns base-2 exponential of x (2^x).


def faceforward(n : Types::Vec3, i : Types::Vec3, nref : Types::Vec3) : Types::Vec3 #

Orients vector n to face away from a surface based on incident vector i and reference normal nref.


def fbm(p : Types::Vec2, octaves : Int32 = 4) : Float32 #

Standard library: Multi-octave Fractal Brownian Motion (FBM) noise.


def floor(x : Float32) : Float32 #

Returns the largest integer less than or equal to x.


def fract(x : Float32) : Float32 #

Returns the fractional part of x: x - floor(x).


def fresnel(power : Float32, normal : Types::Vec3, view : Types::Vec3) : Float32 #

Standard library: Computes power-law Fresnel reflection factor for view vector against surface normal.


def grayscale(c : Types::Vec3) : Float32 #

Standard library: Converts RGB color vector to scalar grayscale luminance.


def groupMemoryBarrier : Nil #

Workgroup-wide execution and shared memory synchronization barrier.


def hash11(p : Float32) : Float32 #

Standard library: 1D -> 1D pseudo-random hash without texture lookups.


def hash12(p : Float32) : Types::Vec2 #

Standard library: 1D -> 2D pseudo-random hash.


def hash21(p : Types::Vec2) : Float32 #

Standard library: 2D -> 1D pseudo-random hash.


def hash22(p : Types::Vec2) : Types::Vec2 #

Standard library: 2D -> 2D pseudo-random hash.


def hash31(p : Types::Vec3) : Float32 #

Standard library: 3D -> 1D pseudo-random hash.


def hash33(p : Types::Vec3) : Types::Vec3 #

Standard library: 3D -> 3D pseudo-random hash.


def hsv2rgb(c : Types::Vec3) : Types::Vec3 #

Standard library: Converts HSV color vector to RGB.


def imageLoad(image : Symbol | String, coord : Types::IVec2) : Types::Vec4 #

Direct image fetch from a compute storage image binding at pixel coordinate coord.


def imageSize(image : Symbol | String) : Types::IVec2 #

Returns dimensions of storage image binding as #ivec2(width, height).


def imageStore(image : Symbol | String, coord : Types::IVec2, color : Types::Vec4) : Nil #

Direct image write into a compute storage image binding at pixel coordinate coord.


def inverse(m : Types::Mat3) : Types::Mat3 #

Calculates inverse matrix of m.


def inverse(m : Types::Mat4) : Types::Mat4 #

def inversesqrt(x : Float32) : Float32 #

Returns inverse square root of x (1.0 / sqrt(x)).


def ivec2(x : Int32 = 0, y : Int32 = 0) : Types::IVec2 #

Shorthand constructor for 2-component integer vector: #ivec2(x, y).


def ivec2(v2 : Types::Vec2) : Types::IVec2 #

def ivec3(x : Int32 = 0, y : Int32 = 0, z : Int32 = 0) : Types::IVec3 #

Shorthand constructor for 3-component integer vector: #ivec3(x, y, z).


def ivec4(x : Int32 = 0, y : Int32 = 0, z : Int32 = 0, w : Int32 = 0) : Types::IVec4 #

Shorthand constructor for 4-component integer vector: #ivec4(x, y, z, w).


def length(v : Types::Vec2 | Types::Vec3 | Types::Vec4) : Float32 #

Returns vector magnitude / length: sqrt(dot(v, v)).


def lerp(a : Float32, b : Float32, t : Float32) : Float32 #

Standard library: Linear interpolation alias for #mix.


def linear_depth(depth : Float32, inv_proj : Types::Mat4) : Float32 #

Standard library: Linearizes non-linear screen depth buffer sample to camera-space distance.


def log(x : Float32) : Float32 #

Returns natural logarithm of x (ln(x)).


def log2(x : Float32) : Float32 #

Returns base-2 logarithm of x (log2(x)).


def luminance(col : Types::Vec3) : Float32 #

Standard library: Calculates perceptual luminance from RGB color using ITU-R BT.709 weights.


def mat2(col0 : Types::Vec2, col1 : Types::Vec2) : Types::Mat2 #

Shorthand constructor for 2x2 matrix.


def mat3(col0 : Types::Vec3, col1 : Types::Vec3, col2 : Types::Vec3) : Types::Mat3 #

Shorthand constructor for 3x3 matrix.


def mat4(col0 : Types::Vec4, col1 : Types::Vec4, col2 : Types::Vec4, col3 : Types::Vec4) : Types::Mat4 #

Shorthand constructor for 4x4 matrix.


def max(a : Float32, b : Float32) : Float32 #

Returns the maximum of a and b.


def memoryBarrier : Nil #

Memory barrier ensuring global memory writes complete across all invocations.


def memoryBarrierImage : Nil #

Memory barrier ensuring storage image writes complete.


def memoryBarrierShared : Nil #

Memory barrier ensuring workgroup shared memory writes complete.


def min(a : Float32, b : Float32) : Float32 #

Returns the minimum of a and b.


def mix(x : Float32, y : Float32, a : Float32) : Float32 #

Linear interpolation between x and y using weight a: x * (1 - a) + y * a.


def mix(x : Types::Vec2, y : Types::Vec2, a : Float32) : Types::Vec2 #

def mix(x : Types::Vec3, y : Types::Vec3, a : Float32) : Types::Vec3 #

def mix(x : Types::Vec4, y : Types::Vec4, a : Float32) : Types::Vec4 #

def mix(x : Types::Vec3, y : Types::Vec3, a : Types::Vec3) : Types::Vec3 #

def mod(x : Float32, y : Float32) : Float32 #

Returns x modulo y: x - y * floor(x / y).


def normalize(v : Types::Vec2) : Types::Vec2 #

Returns vector v scaled to unit length: v / length(v).


def normalize(v : Types::Vec3) : Types::Vec3 #

def normalize(v : Types::Vec4) : Types::Vec4 #

def pow(x : Float32, y : Float32) : Float32 #

Returns x raised to power y (x^y).


def radians(deg : Float32) : Float32 #

Converts degrees to radians: deg * (PI / 180.0).


def rand(uv : Types::Vec2) : Float32 #

Standard library: Fast pseudo-random number generator for UV coordinates.


def reflect(i : Types::Vec3, n : Types::Vec3) : Types::Vec3 #

Calculates reflection vector for incident i against surface normal n: i - 2.0 * dot(n, i) * n.


def refract(i : Types::Vec3, n : Types::Vec3, eta : Float32) : Types::Vec3 #

Calculates refraction vector for incident i through normal n with refraction index ratio eta.


def reinhard_tonemap(x : Types::Vec3) : Types::Vec3 #

Standard library: Reinhard tonemapping curve: x / (1.0 + x).


def remap(val : Float32, in_min : Float32, in_max : Float32, out_min : Float32, out_max : Float32) : Float32 #

Standard library: Remaps value from input range [in_min, in_max] to output range [out_min, out_max].


def rgb2hsv(c : Types::Vec3) : Types::Vec3 #

Standard library: Converts RGB color vector to HSV.


def rotate_2d(p : Types::Vec2, angle : Float32) : Types::Vec2 #

Standard library: Rotates a 2D vector by angle radians.


def round(x : Float32) : Float32 #

Returns x rounded to the nearest integer.


def saturate(x : Float32) : Float32 #

Standard library: Clamps value between 0.0 and 1.0.


def sdf_box(p : Types::Vec3, b : Types::Vec3) : Float32 #

Standard library: Signed distance field calculation for a 3D box of half-extents b.


def sdf_sphere(p : Types::Vec3, radius : Float32) : Float32 #

Standard library: Signed distance field calculation for a sphere of radius.


def sign(x : Float32) : Float32 #

Returns 1.0 if x > 0, 0.0 if x == 0, and -1.0 if x < 0.


def simplex_noise_2d(p : Types::Vec2) : Float32 #

Standard library: Smooth 2D simplex noise gradient.


def sin(x : Float32) : Float32 #

Returns the sine of x (in radians).


def smax(a : Float32, b : Float32, k : Float32 = 0.1_f32) : Float32 #

Standard library: Polynomial smooth maximum for carving SDF surfaces.


def smin(a : Float32, b : Float32, k : Float32 = 0.1_f32) : Float32 #

Standard library: Polynomial smooth minimum for blending SDF surfaces.


def smoothstep(edge0 : Float32, edge1 : Float32, x : Float32) : Float32 #

Smooth Hermite interpolation between 0.0 and 1.0 when edge0 < x < edge1.


def sqrt(x : Float32) : Float32 #

Returns square root of x.


def step(edge : Float32, x : Float32) : Float32 #

Returns 0.0 if x < edge, otherwise 1.0.


def tan(x : Float32) : Float32 #

Returns the tangent of x (in radians).


def texelFetch(sampler : Types::Sampler2D, coord : Types::IVec2, lod : Int32 = 0) : Types::Vec4 #

Fetches texel at integer pixel coordinates coord without bilinear filtering.


def texture(sampler : Types::Sampler2D, uv : Types::Vec2) : Types::Vec4 #

Samples texture sampler at normalized coordinates uv, returning an RGBA vector.


def textureLod(sampler : Types::Sampler2D, uv : Types::Vec2, lod : Float32) : Types::Vec4 #

Samples texture sampler at normalized coordinates uv with explicit level-of-detail lod.


def textureSize(sampler : Types::Sampler2D, lod : Int32 = 0) : Types::IVec2 #

Returns texture dimensions as #ivec2(width, height) at mipmap level lod.


def transpose(m : Types::Mat3) : Types::Mat3 #

Transposes matrix m.


def transpose(m : Types::Mat4) : Types::Mat4 #

def triplanar_weights(normal : Types::Vec3) : Types::Vec3 #

Standard library: Computes blend weights for seamless triplanar texture mapping.


def trunc(x : Float32) : Float32 #

Returns x truncated towards zero.


def uvec2(x : UInt32 = 0_u32, y : UInt32 = 0_u32) : Types::UVec2 #

Shorthand constructor for 2-component unsigned integer vector: #uvec2(x, y).


def uvec3(x : UInt32 = 0_u32, y : UInt32 = 0_u32, z : UInt32 = 0_u32) : Types::UVec3 #

Shorthand constructor for 3-component unsigned integer vector: #uvec3(x, y, z).


def uvec4(x : UInt32 = 0_u32, y : UInt32 = 0_u32, z : UInt32 = 0_u32, w : UInt32 = 0_u32) : Types::UVec4 #

Shorthand constructor for 4-component unsigned integer vector: #uvec4(x, y, z, w).


def value_noise(p : Types::Vec2) : Float32 #

Standard library: Smooth 2D value noise interpolation.


def vec2(x : Float32 = 0.0_f32, y : Float32 = 0.0_f32) : Types::Vec2 #

Shorthand constructor for 2-component vector: #vec2(x, y).


def vec2(x : Number, y : Number) : Types::Vec2 #

def vec3(x : Float32 = 0.0_f32, y : Float32 = 0.0_f32, z : Float32 = 0.0_f32) : Types::Vec3 #

Shorthand constructor for 3-component vector: #vec3(x, y, z).


def vec3(x : Number, y : Number, z : Number) : Types::Vec3 #

def vec4(x : Float32 = 0.0_f32, y : Float32 = 0.0_f32, z : Float32 = 0.0_f32, w : Float32 = 1.0_f32) : Types::Vec4 #

Shorthand constructor for 4-component vector: #vec4(x, y, z, w).


def vec4(x : Number, y : Number, z : Number, w : Number = 1.0) : Types::Vec4 #

def vec4(v3 : Types::Vec3, w : Float32 = 1.0_f32) : Types::Vec4 #

def vec4(val : Float32 | Number) : Types::Vec4 #

def vignette(uv : Types::Vec2, radius : Float32 = 0.75_f32, softness : Float32 = 0.45_f32) : Float32 #

Standard library: Smooth radial vignette factor between 0.0 (edge) and 1.0 (center).


def voronoi(p : Types::Vec2) : Float32 #

Standard library: Cellular Voronoi distance calculation.