Types2 min read

Types

Core data types in BWSL for shader programming.

Reading Time
2 min
Word Count
287
Sections
13
Try It Live

Turn the guide into code

Take the key idea from this page into the playground and validate it in a real shader instead of leaving it as theory.

Open Playground

BWSL's current type surface is centered on 32-bit scalars, vectors, matrices, enums, structs, arrays, and pointers.

Scalar Types

Basic single-value types for fundamental operations.

bool

Boolean value representing true or false.

int

32-bit signed integer.

uint

32-bit unsigned integer.

float

32-bit floating point number.

Vector Types

Multi-component types for spatial and color operations.

Integer Vectors

int2

2-component signed integer vector.

int3

3-component signed integer vector.

int4

4-component signed integer vector.

Unsigned Integer Vectors

uint2

2-component unsigned integer vector.

uint3

3-component unsigned integer vector.

uint4

4-component unsigned integer vector.

Float Vectors

float2

2-component floating point vector. Commonly used for texture coordinates.

float3

3-component floating point vector. Used for positions, normals, and RGB colors.

float4

4-component floating point vector. Used for homogeneous coordinates and RGBA colors.

Matrix Types

Square matrices for transformations and linear algebra operations. BWSL source uses mat2, mat3, and mat4; backend-style names such as float2x2, float3x3, and float4x4 are not accepted as aliases.

mat2

2x2 floating point matrix.

mat3

3x3 floating point matrix. Used for rotations and normal transformations.

mat4

4x4 floating point matrix. Used for full 3D transformations including translation.

Type Aliases

Use using Alias = Type to give an existing type a scoped local name:

bwsl
import PBR as BRDF
using Material = BRDF::PBRMaterial
shade :: (Material mat, float3 normal) -> float3 {
return mat.albedo * saturate(normal.y);
}

Aliases can point at built-in types, local custom types, or module-qualified types. They do not create a new nominal type; Material above is still BRDF::PBRMaterial.

Structs

BWSL structs define nominal shader data types with fields, fixed-size array fields, nested structs, constructors, and methods.

bwsl
struct Material {
float3 albedo;
float roughness;
float metallic;
energy :: () const -> float {
return max(albedo.x, max(albedo.y, albedo.z)) * (1.0 - roughness);
}
}
Material mat = Material(float3(0.9, 0.4, 0.2), 0.35, 0.0);
float e = mat.energy();

See the Structs page for declarations, constructors, module-qualified structs, resource payloads, methods, const methods, and overload behavior.

Enums

BWSL enums support variants with associated data, pattern matching, and compile-time evaluated methods.

bwsl
enum SDFShape {
Sphere(float radius),
Box(float3 size),
Torus(float major, float minor),
eval distance :: (float3 p) -> float {
Sphere(r): length(p) - r
Box(size): {
float3 d = abs(p) - size;
float outside = length(max(d, 0.0));
float inside = min(max(d.x, max(d.y, d.z)), 0.0);
return outside + inside;
}
Torus(major, minor): {
float2 q = float2(length(p.xz) - major, p.y);
return length(q) - minor;
}
}
}

See the Enums page for full documentation on variants, pattern matching, bitflags, and eval methods.

Arrays and Pointers

Fixed-size arrays are part of the active language surface and can appear in locals, parameters, resource buffers, and struct fields. Array sizes are attached to the type, so write declarations as Type[N] name:

bwsl
struct SkinningData {
mat4[64] boneMatrices;
float4[64] weights;
}

Pointers use ^ for address-of and dereference:

bwsl
int x = 42;
int^ ptr = ^x;
int value = ptr^;
ptr^ = 100;

Type Constraints

Type constraints define sets of types for use with generic functions:

bwsl
constraint FloatVectors = float2 | float3 | float4;
constraint AllFloats = float | float2 | float3 | float4;
constraint Scalars = float | int;

Constraints enable generic functions that work with any type in the set:

bwsl
scale :: (FloatVectors v, float s) -> FloatVectors {
return v * s;
}

See the Generics page for complete documentation on type constraints and type-pattern dispatch.

Usage Examples

bwsl
bool enabled = true;
int count = 42;
uint flags = 0xFFu;
float intensity = 0.75;
float3 position = float3(1.0, 2.0, 3.0);
float4 color = float4(1.0, 0.5, 0.0, 1.0);
int2 dimensions = int2(1920, 1080);
mat4 transform = mat4(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
);

Type Conversions

BWSL supports explicit conversions between compatible types:

bwsl
float f = 3.14;
int i = int(f);
uint u = uint(i);
float3 v3 = float3(1.0, 2.0, 3.0);
float4 v4 = float4(v3, 1.0);
float2 v2 = v3.xy;

The examples on this page match the current compiler and regression suite rather than older draft syntax.