Language
The BESL language, from types and values to functions, expressions, and control flow.
Use BESL declarations and statements to build the semantic program that Byte
Engine validates and generates for each shader backend. BESL uses explicit
types and // line comments.
// Values and return types are always explicit.
scale: fn (value: f32, factor: f32) -> f32 {
return value * factor;
}Built-in types
These are the built-in value types available to authored BESL:
| Category | Types | Use |
|---|---|---|
| Control | void | A function that doesn't return a value. |
| Boolean | bool | Conditions and the literals true and false. |
| Unsigned integers | u8, u16, u32 | Integer data and indices. Most arithmetic intrinsics use u32. |
| Signed integers | i32 | Signed integer data and arithmetic. |
| Floating point | f16, f32 | Half-precision and single-precision values. |
| Atomic integers | atomicu32, atomici32 | Atomic unsigned or signed storage in a buffer or workgroup value. Access it through atomic intrinsics. |
| Unsigned vectors | vec2u16, vec4u16, vec2u, vec3u, vec4u | Packed unsigned values, coordinates, indices, and subgroup masks. |
| Signed vectors | vec2i | Two signed integer values. |
| Half vectors | vec2f16, vec3f16, vec4f16 | Two, three, or four half-precision values. |
| Float vectors | vec2f, vec3f, vec4f | Two, three, or four single-precision values. |
| Matrices | mat4f, mat4x3f | Four-by-four and four-column-by-three-row single-precision matrices. |
User-declared structs and fixed-size arrays are also types. Resource handle types are listed with descriptors.
Structs
Declare a struct with Name: struct. Write each member as name: Type and
separate members with commas. You can add a trailing comma.
Light: struct {
position: vec3f,
color: vec3f,
intensity: f32,
}Arrays
BESL arrays have a fixed element type and count. Write the type as
ElementType[COUNT]. You can use array types for constants, local variables,
struct members, function parameters, and function return values.
Declare and construct an array
Call the array type with one value for each element:
let indices: u32[3] = u32[3](4, 8, 15);Declare a module-level constant array in the same way:
WEIGHTS: const f32[3] = f32[3](0.5, 0.25, 0.125);Read an array element
Index an array with brackets. Use a u32 value for a dynamic index:
let indices: u32[3] = u32[3](4, 8, 15);
let first: u32 = indices[0];
let weight_index: u32 = 2;
let selected_weight: f32 = WEIGHTS[weight_index];The count is part of the array type. BESL doesn't provide an array-length intrinsic, so use the declared count as the loop bound:
let total: f32 = 0.0;
for (let i: u32 = 0; i < 3; i = i + 1) {
total = total + WEIGHTS[i];
}Write an array element
Assign to one element of a local array, or to one component of a vector, with an index:
main: fn () -> void {
let weights: f32[8] = f32[8](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
let index: u32 = 2;
weights[index] = 0.5;
let words: vec4u = vec4u(0, 0, 0, 0);
words[index] = words[index] | 1;
}Keep the index inside the array or vector. The GPU result of an index past the end is undefined, and the VM reports it as an error.
You can also assign through an indexed buffer member, workgroup array, task payload, or mesh output array. The resource must allow writes.
Counters: struct {
values: u32[8],
}
counters: descriptor<{
type: Counters,
binding: 0,
access: read_write,
memory: device,
}>;
main: fn () -> void {
let index: u32 = 2;
counters.values[index] = 7;
}Pass and return arrays
Function parameters accept arrays of any fixed size. Arrays pass by value, so the function receives a copy. The array type, including its count, must match at the call boundary.
sum: fn (values: f32[16], count: u32) -> f32 {
let total: f32 = 0.0;
for (let i: u32 = 0; i < count; i = i + 1) {
total = total + values[i];
}
return total;
}Return values support only compact scalar arrays with two through four f32,
u16, or u32 elements, because HLSL can't return arrays. BESL rejects a
function that returns a larger array. Return a vector or a struct with the
values the caller needs instead.
copy_indices: fn (indices: u32[3]) -> u32[3] {
return indices;
}
main: fn () -> void {
let copied: u32[3] = copy_indices(u32[3](4, 8, 15));
let middle: u32 = copied[1];
}BESL lowers these compact arrays to native vectors on backends that can't pass
or return arrays directly. HLSL also can't build a larger array inside an
expression, so use a constructor for a larger array only to initialize a let
or a constant.
Value arrays such as u32[3], arrays of resource descriptors, and layered
textures such as Texture2DArray are separate concepts. See
Descriptors for resource arrays.
Empty brackets are valid only in a descriptor resource type, such as
descriptor<{ type: Instance[], binding: 1, access: read }>, where they declare runtime-sized buffer
contents.
Constants
Declare a module-level constant when a value is known at compile time:
MAX_LIGHTS: const u32 = 64;Functions
Declare a function with name: fn, typed parameters, and an explicit return
type:
mod_by_16: fn (x: i32) -> i32 {
return x % 16;
}Standalone shaders require main. It becomes the stage entry point after any
render-model transformation.
main: fn () -> void {
return;
}Raster entry points can use contextual record types. Use interface for values
that cross a stage boundary and output for final fragment attachments:
main: fn (pipeline_input: interface { uv: vec2f }) -> output { color: vec4f } {
let color: vec4f = vec4f(pipeline_input.uv.x, pipeline_input.uv.y, 0.0, 1.0);
return { color };
}Anonymous interface and output types are valid only in main parameters and
its return type. Separate their fields with commas. You can add a trailing
comma, but you can't use semicolons as field separators.
Variables and expressions
Declare a typed local variable with let:
let color: vec4f = vec4f(1.0, 0.0, 1.0, 1.0);BESL expressions include literals, grouped expressions, function and intrinsic
calls, constructors, assignments, member access, contextual record values, and
if and match values:
let scaled: f32 = (value + bias) * factor;
output.color = material.color;
return { color, instance_index: input.instance_index };In a record value, color is shorthand for color: color. Separate fields with
commas. A trailing comma is optional. A record returned from main must provide
every declared field exactly once. Return it directly from the top-level entry
body so the compiler can finalize the native stage output on every backend.
Numeric literals contain digits and an optional decimal point. They don't use
type suffixes or scientific notation. Write a negative value with the prefix -,
such as -1.6.
Construct vectors, matrices, structs, and arrays by calling the type name. Use the conversion calls listed under constructors and casts when the source and destination types differ.
Operators
| Operation | Operators |
|---|---|
| Arithmetic | +, -, *, /, % |
| Shift | <<, >> |
| Bitwise | &, ` |
| Assignment | = |
| Comparison | ==, !=, <, >, <=, >= |
| Logical | &&, ` |
&& and || take bool operands and run their right side only when the left
side doesn't decide the result, so the right side can read what the left side
guards, as in index < count && values[index] > 0.
Put a prefix operator before a value to negate it or flip it:
| Operator | Applies to | Result |
|---|---|---|
-value | An integer or float scalar or vector | The negated value. An unsigned value wraps, so -one is 4294967295 when one is the u32 1. |
!value | A bool | The opposite bool. |
~value | An integer scalar or vector | The value with every bit flipped. |
To pick one of two values with a bool, use an
if value. BESL evaluates only the value it
picks, so that value can read what the condition guards:
let depth: f32 = if (index < count) { depths[index] } else { 0.0 };Operators bind in this order, tightest first:
- Member access
.and indexing[] - The prefix operators
-,!, and~ *,/,%+,-<<,>>- Comparisons
&,&&^|,||=
Add parentheses when you need a different order, or to mix bitwise and logical operators that share a level.
Half-precision floats
Use f16, vec2f16, vec3f16, and vec4f16 when a value can tolerate
16-bit floating-point precision. Arithmetic between matching half-precision
values stays half precision.
let uv32: vec2f = vec2f(0.25, 0.75);
let uv16: vec2f16 = vec2f16(uv32);
let sampled_uv: vec2f = vec2f(uv16);
let weight16: f16 = f16(0.5);
let weight32: f32 = f32(weight16);Texture intrinsics use vec2f or vec3f coordinates. Convert half-precision
coordinates at the sampling boundary.
Generated GLSL requires GL_EXT_shader_explicit_arithmetic_types_float16 when
half types are used. Choose them only for targets that support native 16-bit
floating-point arithmetic. The DX12 backend requires native 16-bit operations
and compiles every HLSL stage as Shader Model 6.9, so f16 stays a native half
instead of widening to f32.
Control flow
Use if/else, match, C-style for, return, continue, and break to control program
flow. Use discard to stop a fragment invocation without producing its normal
outputs.
main: fn () -> void {
for (let i: u32 = 0; i < 4; i = i + 1) {
if (i >= 2) {
continue;
}
}
return;
}continue skips to the loop's update clause. break leaves the innermost loop
and resumes after it, which lets a loop over sorted data stop at the first
element that can no longer matter:
for (let i: u32 = 0; i < count; i = i + 1) {
if (values[i].key < threshold) {
break;
}
total = total + values[i].weight;
}Add an else branch to run code when the condition is false. Chain more
conditions with else if:
if (roughness < 0.25) {
lobe = 0;
} else if (roughness < 0.75) {
lobe = 1;
} else {
lobe = 2;
}Variables you declare inside a branch are only visible in that branch.
BESL doesn't have while.
Match
Use match to pick a branch by the value of a bool, u8, u16, u32, or
i32. It works like Rust's match: BESL runs the first arm whose pattern
matches, so later arms never see a value an earlier arm took.
match material_kind {
0 => lobe = 0,
1 | 2 => {
lobe = 1;
}
_ => lobe = 2,
}Write each pattern as a literal such as 3, -1, or true, join patterns with
| to share an arm, or use _ to match every other value. Put a comma after an
arm unless its body is a block, an if, or a match. A match can also produce
a value. See Use if and match as values.
A match must cover every value. Add a _ arm, or for a bool, list both
true and false. If an arm can't run because earlier arms cover its values,
BESL ignores it.
Inside a loop, break and continue in an arm act on the loop, as in Rust.
Variables you declare inside an arm are only visible in that arm.
BESL doesn't support match guards (if after a pattern), ranges, bindings, or
matching on floating-point, vector, or struct values yet.
Use if and match as values
An if or a match can produce a value, as in Rust. End each branch with its
value, written without a ;:
let shade: f32 = if (lit) { 1.0 } else if (rim) { 0.5 } else { 0.25 };
let weight: f32 = match lobe {
0 => 0.1,
1 | 2 => {
let squared: f32 = roughness * roughness;
squared * 0.5
}
_ => 1.0,
};Use a value anywhere an expression can go, such as an operand, an argument, or a
return, for example albedo * if (lit) { 1.0 } else { 0.25 }. As the value of
a typed let, an assignment, an argument, or a return, it takes that type, so
literal branches such as 1 also work for a u8. These rules apply:
-
An
ifvalue needs anelsebranch. -
Every branch ends in a value of the same type, or leaves with
return,break,continue, ordiscard, directly or through aniformatchwhose branches all leave:let depth: f32 = if (index < count) { depths[index] } else { return; }; -
A branch can run statements before its value. Variables you declare inside a branch are only visible in that branch.
-
Only the branch that's taken runs, so a branch can read what its condition guards. Evaluation stays left to right: the parts of a statement before the value run first, even when the value's branches run statements.
At the end of a block that isn't a value, a final expression without ; must be
an assignment or a call that returns nothing. BESL functions return with
return, so get: fn () -> f32 { value } is an error.
How a value compiles affects performance:
- An
ifwhose branches are only values compiles to the target language's conditional operator,?:. - An
ifwhose branches run statements, and everymatch, compile to a temporary that aniforswitchstatement sets just before the statement that uses it. The GPU compiler keeps the temporary in a register, so this costs the same as writing the branches yourself. To keep left-to-right order, BESL also copies operands that run before such a value into temporaries.
A for loop's condition and update run on every iteration, and a constant runs
before any statement, so they can only use an if whose branches are only
values. Compute other values with a let first. On HLSL, an atomic that returns
a value can't be in a branch of such an if; assign its result to a let first.
Next, declare the shader interface that connects the program to its stage and resources.