Interface
The BESL interface, from stage values and descriptors to shared storage, layout layers, and stage metadata.
Use the shader interface to connect a BESL program to stage inputs and outputs, GPU resources, shared memory, and the native pipeline contract.
Raster entry points
Declare values that cross a raster stage boundary with an anonymous
interface type. A vertex shader returns its interface. A fragment shader
receives the matching interface as a parameter:
in_position: input<vec2f, 0>;
main: fn (input: StageInput) -> interface { position: vec4f, uv: vec2f, instance_index: u32 } {
let position: vec4f = vec4f(in_position.x, in_position.y, 0.0, 1.0);
let uv: vec2f = in_position * 0.5 + vec2f(0.5, 0.5);
return { position, uv, instance_index: input.instance_index };
}main: fn (
pipeline_input: interface { instance_index: u32, uv: vec2f },
) -> output { color: vec4f } {
let color: vec4f = vec4f(pipeline_input.uv.x, pipeline_input.uv.y, 0.0, 1.0);
return { color };
}Use the same interface field names and types in adjacent stages. Their source
order doesn't need to match. BESL sorts the field names to assign stable native
locations, so the two examples agree even though they list uv and
instance_index differently. Integer fields use flat interpolation. A vertex
position: vec4f field is the native clip-space position, doesn't consume a
varying location, and isn't a fragment input.
An output record describes final fragment attachments. Declaration order sets
the color attachment locations, starting at 0.
Keep explicit top-level input<TYPE, LOCATION> declarations for vertex-buffer
attributes. Task payloads and mutable mesh output arrays also keep their
dedicated declarations:
out_instance_index: output<u32, 0, 126>;
out_uv: vertex_output<vec2f16, 2, 64>;A mesh output array holds one flat value per primitive, so every fragment of a
triangle reads the same element. A vertex_output array holds one value per
vertex, which rasterization interpolates across each triangle, like a vertex
shader output. Size it to the stage's maximum vertex count, and index it by
vertex. On Metal a vertex is written as one value, so write each
vertex_output element in the statements right next to the
set_mesh_vertex_position call with the same index.
Locations range from 0 through 255. Don't repeat a location within one
explicit input or output list.
Stage input values
Add a StageInput parameter when a shader needs values supplied by its stage.
Vertex shaders can read input.vertex_index or input.instance_index; both
values are u32 and don't consume vertex-attribute locations:
main: fn (input: StageInput) -> interface { instance_index: u32 } {
let vertex: u32 = input.vertex_index;
return { instance_index: input.instance_index };
}BESL maps both indices to the matching system values on each backend:
StageInput field | Vulkan GLSL | HLSL | MSL |
|---|---|---|---|
vertex_index | gl_VertexIndex | SV_VertexID | [[vertex_id]] |
instance_index | gl_InstanceIndex | SV_InstanceID | [[instance_id]] |
These values are available only in the vertex stage. In VM tests, bind them
through builtin_vertex_index_slot() and builtin_instance_index_slot().
Compute shaders can read their dispatch and lane values from the same parameter:
main: fn (input: StageInput) -> void {
let coordinate: vec2u = input.thread_id;
let local_index: u32 = input.thread_idx;
let workgroup: u32 = input.threadgroup_position;
let subgroup_lane: u32 = input.subgroup_lane_index;
}StageInput field | Type | Value |
|---|---|---|
thread_id | vec2u | Global two-dimensional compute invocation coordinate. |
thread_idx | u32 | Linear invocation index inside the local workgroup. |
threadgroup_position | u32 | Workgroup position. |
subgroup_lane_index | u32 | Lane index inside the current subgroup. |
These values are available only in the compute stage. Pass a value to a helper
function when code outside main needs it.
Task payload and workgroup storage
Task and mesh stages can share payload arrays. Task and compute stages can also declare storage shared by every invocation in one workgroup:
visible_meshlets: task_payload<u32, 32>;
visible_count: workgroup<atomicu32>;
scratch: workgroup<f32, 64>;Omit the count for one shared value. Add a positive count for shared arrays.
Use workgroup_barrier() before reading values written by other invocations.
Push constants
Use push constants for small uniform values outside normal descriptor bindings:
push_constant: push_constant {
material_index: u32,
}Write the block from the CPU with the same tight layout as buffer records,
using the ghi::pod types. Metal and Vulkan read that layout directly. DX12
reads push constants as a constant buffer, which counts in 16-byte registers, so
a push constant must also follow these rules:
- A vector must not cross a 16-byte boundary. For example, a
vec3fafter twof32members starts at byte 8 and ends at byte 20, so move it or add anf32of padding first. - Arrays and
mat4fmembers start on a 16-byte boundary, and array elements must each take a multiple of 16 bytes, such asvec4f[4]. Store eight scalars as twovec4fmembers instead of anf32[8]. - Use
mat4ffor matrices, andu32instead ofbooloru8.
Shader baking checks these rules on every platform. If a push constant breaks one, the error names the member and its byte offset on each backend. Reorder the members or add explicit padding.
Descriptors
A descriptor declares a named binding between the shader and one GPU resource. Declare its required properties inside the descriptor object:
name: descriptor<{
type: ResourceType,
binding: BINDING,
access: ACCESS,
}>;type, binding, and access are required. You can write the properties in
any order, and you can add a trailing comma. Use memory for a buffer memory
class and count for a fixed array of independent resources:
name: descriptor<{ type: ResourceType, binding: BINDING, access: ACCESS, count: COUNT }>;
name: descriptor<{ type: BufferStruct, binding: BINDING, access: ACCESS, memory: MEMORY_CLASS }>;
name: descriptor<{
type: BufferStruct,
binding: BINDING,
access: ACCESS,
memory: MEMORY_CLASS,
count: COUNT,
}>;
name: descriptor<{ type: ElementType[], binding: BINDING, access: ACCESS }>;This example declares buffer, texture, texture-array, and storage-image bindings:
Frame: struct {
view_projection: mat4f,
}
Vertex: struct {
position: vec3f,
}
frame: descriptor<{ type: Frame, binding: 0, access: read, memory: constant }>;
vertices: descriptor<{ type: Vertex[], binding: 1, access: read }>;
albedo: descriptor<{ type: Texture2D, binding: 2, access: read }>;
shadow_maps: descriptor<{ type: Texture2DArray, binding: 3, access: read }>;
gbuffers: descriptor<{ type: Texture2D, binding: 4, access: read, count: 4 }>;
output_image: descriptor<{ type: StorageImage<rgba16f>, binding: 7, access: write }>;The descriptor resource type selects the reflected binding kind. BESL supports these resource types:
| Resource type | Binding | Use |
|---|---|---|
A previously declared struct, such as Frame | Storage buffer | Bind structured data. Declare the struct before the descriptor. |
An element type followed by [], such as Instance[] | Runtime-sized storage buffer | Bind a contiguous sequence and index it directly. |
Texture2D | Combined 2D texture and sampler | Sample one two-dimensional texture. |
Texture2DArray | Combined 2D-array texture and sampler | Sample layers from one two-dimensional texture array. |
Texture3D | Combined 3D texture and sampler | Sample a three-dimensional texture. |
TextureCube | Combined cube texture and sampler | Sample one cube texture. |
TextureCubeArray | Combined cube-array texture and sampler | Sample cube textures from an array. |
StorageImage | Storage image | Load or store an image whose format remains backend-defined. |
StorageImage<FORMAT> | Typed storage image | Load or store an image with an explicit backend image format, such as r8, r32f, r32ui, or rgba16f. |
Set access to read, write, or read_write. The access must include every
operation the shader performs on the resource.
For a buffer, set memory to constant for small, read-only values shared
by a dispatch or draw. Use device for large buffers, data that varies between
shader threads, and every writable buffer. If you omit the memory class, a buffer
uses device. Image and texture descriptors can't declare a memory class.
Set count to a positive integer to declare an array of independent resources.
Write ElementType[] to bind one storage buffer whose element count comes from
the bound resource. Index it directly in a function, without a wrapper struct:
Instance: struct {
position: vec3f,
sprite_id: u32,
}
instances: descriptor<{ type: Instance[], binding: 1, access: read }>;
main: fn (pipeline_input: interface { instance_index: u32 }) -> void {
let instance: Instance = instances[pipeline_input.instance_index];
instance;
}ElementType[] describes the contents of one buffer. It doesn't declare an
array of descriptors. Reflection reports one storage-buffer binding and the
element stride.
Stored records use the same layout on every backend. Vector members such as
vec2f, vec3f, and vec4f take their component size and align to one
component, so you can declare them in any order without padding. Push constants
have extra rules. See Push constants. Mirror a
record on the CPU with the ghi::pod vector types, which use the same layout.
Matrices keep their backend alignment, so place mat4f members on 16-byte
offsets. If a shader writes a single vec4 member of a buffer record, keep that
member on a 16-byte offset too: on Apple GPUs, a lone write that crosses a
16-byte boundary is slower.
Texture2DArray also describes one resource: a layered two-dimensional
texture. Indexing it selects a layer from that resource before sampling:
sprites: descriptor<{ type: Texture2DArray, binding: 0, access: read }>;
main: fn (pipeline_input: interface { sprite_id: u32, uv: vec2f }) -> output { color: vec4f } {
let color: vec4f = sample(sprites[pipeline_input.sprite_id], pipeline_input.uv);
return { color };
}Use descriptor<{ type: Texture2D, binding: BINDING, access: read, count: COUNT }> when you need COUNT
independent 2D texture descriptors instead.
Reflection retains the authored name and flat slot for each descriptor reachable
from main.
Storage-buffer counters use atomicu32 or atomici32 members with the
atomic intrinsics.
Integer storage images can be read as scalars with image_load_u32:
Counters: struct {
values: atomicu32[1024],
}
counters: descriptor<{ type: Counters, binding: 5, access: read_write, memory: device }>;
index_image: descriptor<{ type: StorageImage<r32ui>, binding: 6, access: read }>;Standalone stage metadata
Standalone .besl assets require an adjacent .besl.bead file. Set stage to
Vertex, Fragment, Compute, Task, or Mesh.
Compute, task, and mesh stages can set workgroup with up to three positive
dimensions. Omitted dimensions default to 1, and omitting workgroup uses
[1, 1, 1].
{ "stage": "Compute", "workgroup": [8, 8, 1] }You can also name the dimensions. This JSON5 form is equivalent to [8, 8, 1]:
{ stage: "Compute", workgroup: { width: 8, height: 8 } }Task stages require a positive maximum_mesh_threadgroups value:
{ "stage": "Task", "workgroup": [32, 1, 1], "maximum_mesh_threadgroups": 32 }Mesh stages require positive maximum_vertices and maximum_primitives values:
{ "stage": "Mesh", "workgroup": [128, 1, 1], "maximum_vertices": 64, "maximum_primitives": 126 }Vertex and Fragment stages don't accept workgroup or mesh limits.
Layout layers
Use a layout layer to declare an interface once and share it between shaders. A
layer is a .besl file without a main function and without a .besl.bead
file. It holds the declarations several shaders need: structs, descriptors, a
push constant block, constants, and helper functions.
The descriptors of one layer form one descriptor set. Every shader that lists the layer reads the same set, and the application creates that set once and binds it wherever one of those shaders runs. Put the resources you bind together in one layer, and put each group you bind separately in a layer of its own. The descriptors a shader declares itself form one more set, which belongs to that shader.
// layouts/frame.besl
Frame: struct {
view_projection: mat4f,
}
frame: descriptor<{ type: Frame, binding: 0, access: read, memory: constant }>;List the layers a shader builds on under layouts in its .besl.bead file.
Write each path relative to the shader's directory. Use ../ to reach a layer
that shaders in several directories share:
{ "stage": "Vertex", "layouts": ["layouts/frame.besl", "layouts/geometry.besl"] }The shader then uses the layers' declarations as if it declared them itself:
in_position: input<vec3f, 0>;
main: fn () -> interface { position: vec4f } {
let position: vec4f = frame.view_projection * vec4f(in_position.x, in_position.y, in_position.z, 1.0);
return { position };
}Layers stack in the order you list them. Each layer can use the layers before it, and the shader can use all of them. A layer can't use a layer listed after it.
A name can be declared only once across a shader and its layers. Baking fails
if a shader keeps its own copy of a layer declaration, if two layers declare
the same name, or if a layer declares main. The error names the declaration
and the layer. Delete the copy to fix it. A shader and its layers can hold one
push_constant block between them.
A shader declares the whole set of every layer it lists, so that every shader
that shares a layer sees the same set. Only the descriptors that code reachable
from main uses count as the shader's resource accesses, and only those need
a resource before the shader runs. A shader that uses no descriptor of a layer
doesn't need that layer's set bound at all. A layer that declares only
structs, constants, and helper functions declares no set, so you can share
record types freely.
binding numbers are unique across a shader and all of its layers. Give each
layer its own range, so layers that are listed together never collide.
A descriptor's access applies to every shader that lists its layer. When some
shaders write a resource and others only read it, declare it in two layers:
one with access: write or read_write for the writers, and one with
access: read for the readers. Bind the same resource in both sets. A shader
that only reads then isn't ordered after other readers of the resource.
In Byte Engine, build the set a layer declares with
rendering::SetLayout::from_layers, and bind its resources by the names the
layer gives them. A set made from a layer matches every shader baked against
that version of the layer. If you edit a layer and run with shaders baked
against the old version, binding fails with an error that names the layer.
Baking tracks each listed layer as a dependency of the shader. When you edit a layer, the next bake rebuilds every shader that lists it.
Stage and backend support
The normal generators support these stage and output-language combinations:
| Stage | GLSL | HLSL | MSL |
|---|---|---|---|
| Vertex | Supported | Supported | Supported |
| Fragment | Supported | Supported | Supported |
| Compute | Supported | Supported | Supported |
| Task | Not supported | Supported as an amplification shader | Supported as an object stage |
| Mesh | Supported through GL_EXT_mesh_shader | Supported | Supported |
The generated language support doesn't guarantee that a device exposes the required shader stage or extension. Half-precision and subgroup operations also require their target capabilities. BESL validates subgroup intrinsics for compute stages only. See compute-stage intrinsics.
Next, use BESL intrinsics to read and update the values declared by the interface.