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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use *;
/// Constructor inputs for [`WebGpuRenderer`]. Grouped into one struct so the
/// constructor stays a single-argument call (the renderer carries enough GPU
/// handles and canvas parameters that a positional parameter list would be
/// unwieldy). All fields are public so integration tests can assemble a
/// renderer around `Reflect` stand-in objects.
/// A WebGPU rendering backend wrapping the GPU device, queue, and canvas context
/// for GPU-accelerated rendering on the web.
///
/// Created asynchronously via `WebGpuRenderer::init` because adapter and
/// device acquisition returns JavaScript Promises that must be awaited.
/// Once initialized, the renderer provides methods to create GPU resources
/// (buffers, shader modules, command encoders) and execute render passes.
///
/// WebGPU types are stored as `JsValue` to avoid feature-gated import issues
/// with `web_sys`. Method calls are performed via `Reflect` and `JsCast`.
/// OPT 34: persistent render-pass descriptor and its inner
/// attachments, reused across `begin_render_pass_full` calls.
///
/// # Why
///
/// `begin_render_pass_full` historically allocated a fresh descriptor
/// `Object`, a `colorAttachments` `Array`, and one inner
/// `color_attachment` `Object` (plus an optional `clearValue` `Object`)
/// on every frame, then ran 8-15 `Reflect::set` calls to populate
/// them. WebGPU re-validates the descriptor each call, but the JS-side
/// `Object` / `Array` allocations and the per-property `Reflect::set`
/// crossings are pure overhead — only the `clearValue` and (rarely)
/// `view` / `loadOp` / `storeOp` fields change between frames.
///
/// # What we cache
///
/// - The top-level descriptor `Object` (the one passed to
/// `beginRenderPass`).
/// - The `colorAttachments` `Array` (always exactly one element —
/// we keep the same `Array` reference and mutate its slot 0 in
/// place).
/// - The inner color attachment `Object` (slot 0 of
/// `colorAttachments`).
/// - The `clearValue` `Object` (the `{r, g, b, a}` dictionary that
/// is the actual per-frame mutating field).
/// - Last-applied `loadOp` / `storeOp` string slices, to detect when
/// the caller switched ops and the cached descriptor must be
/// rebuilt (rare; WebGPU does not hot-swap ops every frame).
/// - Last-applied depth-stencil shape (present / absent), to detect
/// when the depth-stencil shape changes.
///
/// # Invalidation
///
/// The cache is invalidated (rebuilt from scratch) when any of:
/// - `load_op` changes between calls,
/// - `store_op` changes between calls,
/// - the depth-stencil shape changes (None → Some / Some → None),
/// - the resolve-target shape changes (MSAA on/off).
///
/// `view` / `resolveTarget` / `clearValue` are refreshed on every call
/// (the swap-chain view expires after each presented frame, so caching
/// it across frames silently invalidates every subsequent render pass).
///
/// These are all `&'static str` (they come from `WEBGPU_*_OP_*`
/// constants), so invalidation is a pointer-compare.
///
/// `Clone` is derived so the parent `WebGpuRenderer`'s `Data` derive
/// (which adds a `Clone` bound on every field) keeps compiling;
/// `js_sys::Object` and `js_sys::Array` both derive `Clone`, so the
/// derived `Clone` impl just clones the inner JS-side references
/// (cheap, no JS allocation).
/// Interior-mutable slot for the renderer's pending error-scope value.
///
/// This is the `euv-engine` analog of euv-core's `HandlerRegistryCell`
/// (`core/src/renderer/registry/struct.rs:62`): a single-element
/// `Sync` wrapper that holds an `Option<JsValue>` behind an
/// `UnsafeCell`.
///
/// # Why this type exists
///
/// `WebGpuRenderer::pending_error` needs interior mutability
/// because:
///
/// 1. `pop_error_sync` takes `&self` (the WebGPU hot path cannot
/// be `async`), but the spawned `wasm_bindgen_futures::spawn_local`
/// future must mutate the slot to store the resolved
/// `Promise<GPUError?>` value.
/// 2. `take_last_error` also takes `&self` and drains the slot
/// on the next render tick.
///
/// The first implementation used `Rc<RefCell<Option<JsValue>>>`,
/// which works but pays for:
///
/// - a `RefCell::borrow_mut` runtime borrow check on every
/// write (the panic path is unreachable in practice — only
/// the spawn_local future and `take_last_error` ever touch
/// the slot, and they never overlap because the future is
/// a microtask drained before the next render tick).
/// - a heap allocation for the `RefCell`'s borrow state.
///
/// The newtype keeps the interior-mutability primitive (`Rc`),
/// because the spawn_local future needs its own owning handle,
/// but swaps the inner cell from `RefCell` to `UnsafeCell`:
///
/// - zero runtime borrow check (the WASM single-threaded
/// scheduler makes the borrow impossible to violate).
/// - zero allocation (the cell is just a `*mut Option<JsValue>`
/// sitting inside the `Rc`-managed box).
///
/// # Sync safety
///
/// `PendingErrorCell` is **not** `Sync` by default (`UnsafeCell`
/// explicitly opts out). We hand-implement `Sync` for it because
/// the renderer is only ever used in the WASM single-threaded
/// runtime; the `Rc` ensures the same instance is never shared
/// across threads (it is not `Send`/`Sync` either), and the
/// WASM main thread is the only place that ever touches the
/// slot. This matches euv-core's pattern
/// (`unsafe impl Sync for HandlerRegistryCell {}`).
///
/// If the engine is ever compiled for a multi-threaded target
/// (native, `wasm-bindgen-rayon`), this `unsafe impl Sync` is
/// unsound and must be removed.
pub ` because the field is
/// only meant to be touched from inside the renderer module —
/// specifically from the `impl PendingErrorCell` block in
/// `impl.rs`. The struct itself stays `pub` so external code
/// can name the type, but the raw `UnsafeCell` is an
/// implementation detail.
pub ,
);