Arrays
Fixed-size arrays in BWSL - declaration, initializers, indexing, .length, constant arrays, and copy semantics.
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 PlaygroundBWSL arrays have a fixed size that is part of the type. They can be locals, function parameters, struct fields, shared compute memory and constants. Resources use their own forms (buffer<T>, texture arrays); see Resources.
Declaring Arrays
The size follows the element type: Type[N] name.
float[4] weights;
float3[8] offsets;
Light[4] lights; // Arrays of structs
float[2][3] grid; // Two rows of three
The C-style postfix form float weights[4] is not accepted; the compiler reports Array size must follow the type.
The size must be a positive integer literal or integer constant, at most 262,144 (256k) elements:
const int TAP_COUNT = 8;
float2[TAP_COUNT] taps;
Initializers
A brace list initializes the elements in order:
float[4] kernel = {0.25, 0.5, 0.75, 1.0};
float2[2] corners = {float2(0.0), float2(1.0, 0.5)};
Provide a value for every element. In a non-const array, elements without an initializer are left uninitialized. A const array must list exactly one value per element. Brace lists are only valid in declarations; you can't assign a brace list to an existing array.
When every element is a constant, the compiler emits the array as one initializer (for example float t[4] = float[](...) in GLSL) instead of one store per element.
Indexing
Index with any int or uint expression:
float w = kernel[2];
float v = kernel[tapIndex];
grid[1][2] = 4.0;
lights[i].intensity *= 0.5;
A constant index outside the array, including one that only becomes constant after folding (kernel[2 + 2]), is a compile error. A runtime index is not bounds-checked, so keep it in range.
Length
.length gives the element count of a local array or an array parameter as an int constant:
float sum = 0.0;
for (i in 0..kernel.length) {
sum += kernel[i];
}
Constant Arrays
Arrays can be const at module, pipeline, pass or local scope:
const float[4] WEIGHTS = {0.125, 0.25, 0.375, 0.25};
Every element must be given and no element can be assigned. See Constants for how they compile.
Copies and Parameters
Arrays are values. Assigning one array to another copies it, and each copy changes independently:
float[2] a = {1.0, 2.0};
float[2] b = a;
b[0] = 5.0; // a[0] is still 1.0
Array parameters are passed by value too. The function gets its own copy, so writing to it never changes the caller's array:
normalizeWeights :: (float[4] w) -> float {
float total = 0.0;
for (i in 0..w.length) {
total += w[i];
}
return total;
}
Functions cannot return arrays. Return a struct with an array field instead.
Arrays in Structs and Resources
Struct fields can be arrays, and those structs can be resource payloads:
struct SkinningData {
mat4[64] boneMatrices;
float4[64] weights;
}
Texture resources can also be arrays, e.g. units: texture2D[4]. See Resources.