1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- Compute shader example
-- Demonstrates: shared memory, barriers, compute builtins, buffer bindings
struct Params
count: uint
end
uniform params: Params
buffer data: [1024]float
-- Workgroup-shared memory (visible to all threads in a workgroup)
shared tile: [64]float
@entry(compute) @workgroup_size(64, 1, 1)
function main(
@builtin(global_invocation_id) gid: uvec3,
@builtin(local_invocation_index) lid: uint
)
-- Load data into shared memory
if gid.x < params.count then
tile[lid] = data[gid.x]
else
tile[lid] = 0.0
end
-- Synchronize all threads in the workgroup
barrier()
-- Write back (example: each thread reads neighbor's value)
if gid.x < params.count then
if lid > uint(0) then
data[gid.x] = tile[lid] + tile[lid - uint(1)]
else
data[gid.x] = tile[lid] * 2.0
end
end
end