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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! Synchronization Built-in Functions
//!
//! WGSL synchronization functions for compute shader workgroup coordination.
//! See: <https://gpuweb.github.io/gpuweb/wgsl/#sync-builtin-functions>
//!
//! All synchronization functions execute a control barrier with Acquire/Release
//! memory ordering. They use the `Workgroup` memory scope and execution scope.
//!
//! All synchronization functions must only be used in the compute shader stage
//! and must only be invoked in uniform control flow.
//!
//! ## CPU-Side Behavior
//!
//! On the CPU side, the barrier functions are currently no-ops because there
//! is no parallel dispatch runtime. When a multi-threaded compute dispatch
//! is implemented, these should be revisited to use proper thread barriers.
use crateWgsl;
use ;
/// Executes a control barrier synchronization function that affects memory
/// and atomic operations in the storage address space.
///
/// Ensures all storage buffer writes by invocations in the workgroup are
/// visible to all other invocations before any subsequent storage memory
/// or atomic operations execute.
///
/// # Compute Shader Only
/// This function must only be called from compute shader entry points
/// in uniform control flow.
///
/// # CPU-Side Behavior
/// When the `dispatch-runtime` feature is enabled and the calling thread is
/// within a `dispatch_workgroups` invocation, this function synchronizes via
/// a shared barrier. Otherwise, it is a no-op.
/// Executes a control barrier synchronization function that affects memory
/// operations in the handle address space.
///
/// Ensures all texture memory operations by invocations in the workgroup
/// are visible to all other invocations before any subsequent handle memory
/// operations execute.
///
/// # Compute Shader Only
/// This function must only be called from compute shader entry points
/// in uniform control flow.
///
/// # CPU-Side Behavior
/// When the `dispatch-runtime` feature is enabled and the calling thread is
/// within a `dispatch_workgroups` invocation, this function synchronizes via
/// a shared barrier. Otherwise, it is a no-op.
/// Executes a control barrier synchronization function that affects memory
/// and atomic operations in the workgroup address space.
///
/// Ensures all workgroup memory writes by invocations are visible to all
/// other invocations, and synchronizes execution across the workgroup.
///
/// # Compute Shader Only
/// This function must only be called from compute shader entry points
/// in uniform control flow.
///
/// # CPU-Side Behavior
/// When the `dispatch-runtime` feature is enabled and the calling thread is
/// within a `dispatch_workgroups` invocation, this function synchronizes via
/// a shared barrier. Otherwise, it is a no-op.
/// Trait for types that support `workgroupUniformLoad`.
///
/// Returns the value from a workgroup variable, ensuring the value is
/// uniform across the workgroup. Also executes a workgroup barrier.
///
/// This trait has two families of implementations:
/// - `Workgroup<T>` where `T: Clone` — returns a clone of the stored value
/// - `Workgroup<Atomic<T>>` — atomically loads and returns the inner scalar
/// Returns the value pointed to by a workgroup variable, ensuring the
/// value is uniform across the workgroup.
///
/// This is the Rust equivalent of WGSL's
/// `workgroupUniformLoad(p: ptr<workgroup, T>) -> T`.
///
/// The return value is uniform — all invocations in the workgroup observe
/// the same value. Also executes a workgroup barrier.
///
/// # Overloads
///
/// In WGSL, `workgroupUniformLoad` has two overloads:
/// - `fn workgroupUniformLoad(p: ptr<workgroup, T>) -> T` for constructible
/// types
/// - `fn workgroupUniformLoad(p: ptr<workgroup, atomic<T>, read_write>) -> T`
/// for atomic types (returns the inner scalar, not the atomic wrapper)
///
/// Both overloads are handled by the [`WorkgroupUniformLoad`] trait.
///
/// # Compute Shader Only
/// This function must only be called from compute shader entry points
/// in uniform control flow.
///
/// # Example
///
/// ```ignore
/// use wgsl_rs::std::*;
///
/// workgroup!(SHARED_VALUE: u32);
///
/// #[compute]
/// #[workgroup_size(64)]
/// fn main(#[builtin(local_invocation_index)] local_idx: u32) {
/// let value: u32 = workgroup_uniform_load(&SHARED_VALUE);
/// }
/// ```
/// Specialized implementation for `Workgroup<Atomic<u32>>`.
///
/// Returns the `u32` scalar value via an atomic load, matching the WGSL
/// overload `workgroupUniformLoad(p: ptr<workgroup, atomic<u32>, read_write>)
/// -> u32`.
/// Specialized implementation for `Workgroup<Atomic<i32>>`.
///
/// Returns the `i32` scalar value via an atomic load, matching the WGSL
/// overload `workgroupUniformLoad(p: ptr<workgroup, atomic<i32>, read_write>)
/// -> i32`.