euv_engine/renderer/struct.rs
1use super::*;
2
3/// A 2D camera that defines the viewport into the game world.
4#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
5pub struct Camera2D {
6 /// The world-space position of the camera center.
7 #[get(type(copy))]
8 pub(crate) position: Vector2D,
9 /// The zoom factor (1.0 = no zoom, 2.0 = 2x magnification).
10 #[get(type(copy))]
11 pub(crate) zoom: f64,
12 /// The rotation angle in radians.
13 #[get(type(copy))]
14 pub(crate) rotation: f64,
15 /// The viewport width in screen pixels.
16 #[get(type(copy))]
17 pub(crate) viewport_width: f64,
18 /// The viewport height in screen pixels.
19 #[get(type(copy))]
20 pub(crate) viewport_height: f64,
21}
22
23/// A 3D camera that defines the viewport into a 3D world using perspective
24/// or orthographic projection.
25#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
26pub struct Camera3D {
27 /// The world-space position of the camera (eye).
28 #[get(type(copy))]
29 pub(crate) position: Vector3D,
30 /// The point the camera is looking at (target).
31 #[get(type(copy))]
32 pub(crate) target: Vector3D,
33 /// The up direction for the camera.
34 #[get(type(copy))]
35 #[new(skip)]
36 pub(crate) up: Vector3D,
37 /// The vertical field of view in radians.
38 #[get(type(copy))]
39 #[new(skip)]
40 pub(crate) fov: f64,
41 /// The near clipping plane distance.
42 #[get(type(copy))]
43 #[new(skip)]
44 pub(crate) near: f64,
45 /// The far clipping plane distance.
46 #[get(type(copy))]
47 #[new(skip)]
48 pub(crate) far: f64,
49 /// The viewport width in pixels.
50 #[get(type(copy))]
51 pub(crate) viewport_width: f64,
52 /// The viewport height in pixels.
53 #[get(type(copy))]
54 pub(crate) viewport_height: f64,
55}
56
57/// A wrapper around `CanvasRenderingContext2d` providing convenience
58/// drawing methods and camera management for the game engine.
59#[derive(Clone, Data, New)]
60pub struct CanvasRenderer {
61 /// The underlying canvas 2D rendering context.
62 pub(crate) context: CanvasRenderingContext2d,
63 /// The active camera controlling the viewport.
64 #[get(type(copy))]
65 pub(crate) camera: Camera2D,
66 /// The active rendering quality preset.
67 ///
68 /// Controls `imageSmoothingEnabled`, `imageSmoothingQuality`, and
69 /// `textRendering` on the underlying context. Defaults to `Medium`.
70 #[get(type(copy))]
71 pub(crate) quality: RenderQuality,
72}
73
74/// A linear gradient defined by two endpoints and a list of color stops.
75///
76/// Used to create smooth color transitions along a straight line
77/// for fill or stroke operations on the canvas.
78#[derive(Clone, Data, Debug, New, PartialEq)]
79pub struct LinearGradient {
80 /// The starting point of the gradient in world space.
81 #[get(type(copy))]
82 pub(crate) start: Vector2D,
83 /// The ending point of the gradient in world space.
84 #[get(type(copy))]
85 pub(crate) end: Vector2D,
86 /// The ordered list of color stops, each containing a position (0.0 to 1.0) and a CSS color string.
87 pub(crate) stops: Vec<(f64, String)>,
88}
89
90/// A radial gradient defined by inner and outer circles and a list of color stops.
91///
92/// Used to create smooth color transitions radiating outward from a center point
93/// for fill or stroke operations on the canvas.
94#[derive(Clone, Data, Debug, New, PartialEq)]
95pub struct RadialGradient {
96 /// The center of the inner circle of the gradient.
97 #[get(type(copy))]
98 pub(crate) inner_center: Vector2D,
99 /// The radius of the inner circle.
100 #[get(type(copy))]
101 pub(crate) inner_radius: f64,
102 /// The center of the outer circle of the gradient.
103 #[get(type(copy))]
104 pub(crate) outer_center: Vector2D,
105 /// The radius of the outer circle.
106 #[get(type(copy))]
107 pub(crate) outer_radius: f64,
108 /// The ordered list of color stops, each containing a position (0.0 to 1.0) and a CSS color string.
109 pub(crate) stops: Vec<(f64, String)>,
110}
111
112/// Shadow rendering configuration for drop shadow effects on canvas primitives.
113///
114/// When applied, all subsequent fill, stroke, and draw operations will cast
115/// a shadow with the specified color, blur radius, and offset.
116#[derive(Clone, Data, Debug, New, PartialEq, PartialOrd)]
117pub struct ShadowConfig {
118 /// The CSS color string of the shadow (e.g., `"rgba(0,0,0,0.5)"`).
119 #[get(type(clone))]
120 pub(crate) color: String,
121 /// The blur radius of the shadow in pixels.
122 #[get(type(copy))]
123 pub(crate) blur: f64,
124 /// The horizontal offset of the shadow in pixels.
125 #[get(type(copy))]
126 pub(crate) offset_x: f64,
127 /// The vertical offset of the shadow in pixels.
128 #[get(type(copy))]
129 pub(crate) offset_y: f64,
130}
131
132/// Represents the rendering priority layer for draw call ordering.
133///
134/// Higher z-index values are drawn on top of lower values,
135/// enabling correct visual layering of game objects.
136#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
137pub struct RenderLayer {
138 /// The z-index determining draw order. Higher values draw later (on top).
139 #[get(type(copy))]
140 pub(crate) z_index: i32,
141 /// Whether objects in this layer should be rendered.
142 #[get(type(copy))]
143 pub(crate) visible: bool,
144}
145
146/// An ordered buffer of deferred draw commands recorded during a frame.
147///
148/// Scenes and components push `DrawCommand`s into the list during `on_render`
149/// instead of drawing immediately. The engine then replays the whole list once
150/// per frame via `CanvasRenderer::replay`, which batches consecutive same-style
151/// shapes into a single path and skips redundant canvas state changes. The
152/// backing `Vec` is reused across frames via `clear()` to avoid reallocation.
153#[derive(Clone, Data, Debug, Default, New)]
154pub struct DrawList {
155 /// The recorded draw commands for the current frame.
156 #[get(pub(crate))]
157 #[get_mut(pub(crate))]
158 #[set(pub(crate))]
159 pub(crate) commands: Vec<DrawCommand>,
160}
161
162/// A supersampling anti-aliasing (SSAA) canvas wrapper that renders at a higher
163/// resolution on an offscreen canvas and downscales to the display canvas for
164/// smoother polygon edges in software-rendered 3D scenes.
165///
166/// The offscreen context is scaled by `scale_factor` so that all drawing
167/// code can use logical pixel coordinates without modification. After
168/// rendering, call `present()` to draw the high-resolution buffer onto the
169/// visible canvas with high-quality image smoothing.
170#[derive(Clone, Data, New)]
171pub struct SsaaCanvas {
172 /// The display canvas element visible to the user.
173 pub(crate) display_canvas: HtmlCanvasElement,
174 /// The 2D rendering context of the display canvas used for final presentation.
175 pub(crate) display_context: CanvasRenderingContext2d,
176 /// The offscreen canvas used for high-resolution rendering.
177 pub(crate) offscreen_canvas: HtmlCanvasElement,
178 /// The 2D rendering context of the offscreen canvas, pre-scaled by `scale_factor`.
179 pub(crate) offscreen_context: CanvasRenderingContext2d,
180 /// The supersampling scale factor (e.g., 2.0 means 4x SSAA).
181 #[get(type(copy))]
182 pub(crate) scale_factor: f64,
183 /// The rendering quality preset for the downscaling present step.
184 ///
185 /// Controls the smoothing strategy when the offscreen buffer is
186 /// downscaled onto the display canvas. Defaults to `Medium`.
187 #[new(skip)]
188 #[get(type(copy))]
189 pub(crate) quality: RenderQuality,
190 /// The logical display width in CSS pixels.
191 #[get(type(copy))]
192 pub(crate) width: f64,
193 /// The logical display height in CSS pixels.
194 #[get(type(copy))]
195 pub(crate) height: f64,
196}
197
198/// A WebGPU rendering backend wrapping the GPU device, queue, and canvas context
199/// for GPU-accelerated rendering on the web.
200///
201/// Created asynchronously via `WebGpuRenderer::init` because adapter and
202/// device acquisition returns JavaScript Promises that must be awaited.
203/// Once initialized, the renderer provides methods to create GPU resources
204/// (buffers, shader modules, command encoders) and execute render passes.
205///
206/// WebGPU types are stored as `JsValue` to avoid feature-gated import issues
207/// with `web_sys`. Method calls are performed via `Reflect` and `JsCast`.
208#[derive(Clone, Data)]
209pub struct WebGpuRenderer {
210 /// The WebGPU device (`GpuDevice`) used to create GPU resources.
211 pub(crate) device: JsValue,
212 /// The device's command queue (`GpuQueue`) for submitting command buffers.
213 pub(crate) queue: JsValue,
214 /// The WebGPU canvas rendering context (`GpuCanvasContext`).
215 pub(crate) context: JsValue,
216 /// The HTML canvas element backing the WebGPU context.
217 pub(crate) canvas: HtmlCanvasElement,
218 /// The texture format string used by the canvas's swap chain (e.g., `"bgra8unorm"`).
219 #[get(type(clone))]
220 pub(crate) format: String,
221 /// The physical pixel width of the canvas backing store.
222 #[get(type(copy))]
223 pub(crate) width: u32,
224 /// The physical pixel height of the canvas backing store.
225 #[get(type(copy))]
226 pub(crate) height: u32,
227 /// Whether MSAA anti-aliasing is enabled for render pipelines.
228 ///
229 /// When `true`, the renderer allocates a multisampled intermediate texture
230 /// (`sampleCount: 4`) and resolves into the swap chain each frame; when
231 /// `false`, render passes attach directly to the swap chain view at
232 /// `sampleCount: 1`.
233 #[get(type(copy))]
234 pub(crate) antialias: bool,
235 /// The multisampled color texture used when `antialias` is `true`.
236 ///
237 /// `None` when MSAA is disabled. Rebuilt on every resize because the
238 /// `width`/`height` are immutable for a given `GpuTexture`.
239 #[get(type(clone))]
240 pub(crate) multisample_texture: Option<JsValue>,
241 /// The default `GpuTextureView` into `multisample_texture`.
242 ///
243 /// Cached at texture-create time so `begin_render_pass` does not have to
244 /// recreate the view each frame. `None` when MSAA is disabled.
245 #[get(type(clone))]
246 pub(crate) multisample_view: Option<JsValue>,
247 /// The depth-stencil texture used for depth-tested passes.
248 ///
249 /// Created lazily on the first call to [`WebGpuRenderer::begin_render_pass`]
250 /// that includes a `depthStencil` attachment. Rebuilt on every resize
251 /// because the dimensions are immutable for a given `GpuTexture`. The
252 /// matching default view is cached in `depth_view`.
253 ///
254 /// `None` until the first depth-tested render pass is opened.
255 #[get(type(clone))]
256 pub(crate) depth_texture: Option<JsValue>,
257 /// The default `GpuTextureView` into `depth_texture`.
258 ///
259 /// `None` when no depth texture has been allocated.
260 #[get(type(clone))]
261 pub(crate) depth_view: Option<JsValue>,
262 /// The depth-stencil format used for `depth_texture`.
263 ///
264 /// Stored so subsequent render-pass openers can pass the same format
265 /// to the pipeline layout without having to remember it externally.
266 /// `None` until the first depth texture is allocated.
267 #[get(type(clone))]
268 pub(crate) depth_format: Option<String>,
269 /// User-supplied closure fired when the underlying `GpuDevice` enters
270 /// the `lost` state (browser-initiated context loss, OS driver crash,
271 /// `device.destroy()`, ...).
272 ///
273 /// `None` until the caller calls [`WebGpuRenderer::on_device_lost`].
274 /// The renderer also stores a separate `device_lost_handle` that
275 /// forwards the `GPUDeviceLostInfo` JS value into this callback.
276 #[get(type(clone))]
277 pub(crate) device_lost_callback: Option<js_sys::Function>,
278 /// Whether the device is currently in the `lost` state.
279 ///
280 /// Once flipped to `true`, every GPU operation returns
281 /// `Err(WebGpuError::RendererDisposed)` until the caller destroys the
282 /// renderer and creates a new one (WebGPU has no "recover from lost
283 /// device" API).
284 #[get(type(copy))]
285 pub(crate) device_lost: bool,
286 /// Shared slot for the most recent popped error-scope value.
287 ///
288 /// `device.popErrorScope()` returns a `Promise<GPUError?>`; we
289 /// cannot `.await` it from a sync call site. Instead, every
290 /// `push_error_scope` + `pop_error_scope` pair registers a
291 /// microtask via `wasm_bindgen_futures::spawn_local` that stores
292 /// the resolved value here. Callers that want the error
293 /// synchronously call [`WebGpuRenderer::take_last_error`] to
294 /// drain the slot.
295 ///
296 /// Holding a `Rc<PendingErrorCell>` lets the spawn_local future
297 /// own its own handle independently of `&self`, so the
298 /// renderer's borrow checker stays happy. The slot is empty
299 /// (`None`) by default and after each successful take.
300 ///
301 /// The cell is intentionally `PendingErrorCell` (a `Sync`
302 /// `UnsafeCell` newtype, see [`crate::renderer::static`]) rather
303 /// than `Rc<RefCell<...>>`: the WASM single-threaded scheduler
304 /// makes the runtime borrow check `RefCell` provides unreachable
305 /// in practice, so we trade it for a raw `UnsafeCell` deref
306 /// confined to two call sites. This mirrors how euv-core
307 /// implements its global registries
308 /// (`core/src/renderer/registry/struct.rs:62`).
309 pub(crate) pending_error: Rc<PendingErrorCell>,
310 /// The currently-open `GpuCommandEncoder`, if any.
311 ///
312 /// WebGPU expects the application to encode all work for a
313 /// frame (clear, render passes, compute passes, copy ops) into
314 /// a single command encoder, then call `encoder.finish()` to
315 /// produce a `GpuCommandBuffer` and submit it to the queue.
316 /// The encoder is `None` after `submit()` finishes and must
317 /// be re-acquired via `device.createCommandEncoder()` before
318 /// the next frame.
319 #[get(type(clone))]
320 pub(crate) command_encoder: Option<JsValue>,
321 /// OPT 34: cached render-pass descriptor, allocated lazily on the
322 /// first call to [`WebGpuRenderer::begin_render_pass_full`].
323 ///
324 /// The pre-WebGPU-audit path allocated a fresh `Object` +
325 /// `Array` + 8-15 `Reflect::set` calls every frame, even though
326 /// only the `clearValue` actually changes. We keep the descriptor
327 /// Object alive for the renderer's lifetime, mutating only the
328 /// fields whose values differ from the last call. The cache
329 /// invalidates itself automatically when `load_op` / `store_op`
330 /// or the depth-stencil shape changes.
331 ///
332 /// `None` until the first `begin_render_pass_full` call; `Some(_)`
333 /// afterwards and persists for the lifetime of the renderer.
334 #[get(type(clone))]
335 pub(crate) render_pass_descriptor_cache: Option<RenderPassDescriptorCache>,
336}
337
338/// OPT 34: persistent render-pass descriptor and its inner
339/// attachments, reused across `begin_render_pass_full` calls.
340///
341/// # Why
342///
343/// `begin_render_pass_full` historically allocated a fresh descriptor
344/// `Object`, a `colorAttachments` `Array`, and one inner
345/// `color_attachment` `Object` (plus an optional `clearValue` `Object`)
346/// on every frame, then ran 8-15 `Reflect::set` calls to populate
347/// them. WebGPU re-validates the descriptor each call, but the JS-side
348/// `Object` / `Array` allocations and the per-property `Reflect::set`
349/// crossings are pure overhead — only the `clearValue` and (rarely)
350/// `view` / `loadOp` / `storeOp` fields change between frames.
351///
352/// # What we cache
353///
354/// - The top-level descriptor `Object` (the one passed to
355/// `beginRenderPass`).
356/// - The `colorAttachments` `Array` (always exactly one element —
357/// we keep the same `Array` reference and mutate its slot 0 in
358/// place).
359/// - The inner color attachment `Object` (slot 0 of
360/// `colorAttachments`).
361/// - The `clearValue` `Object` (the `{r, g, b, a}` dictionary that
362/// is the actual per-frame mutating field).
363/// - Last-applied `loadOp` / `storeOp` string slices, to detect when
364/// the caller switched ops and the cached descriptor must be
365/// rebuilt (rare; WebGPU does not hot-swap ops every frame).
366/// - Last-applied depth-stencil shape (present / absent), to detect
367/// when the depth-stencil shape changes.
368///
369/// # Invalidation
370///
371/// The cache is invalidated (rebuilt from scratch) when any of:
372/// - `load_op` changes between calls,
373/// - `store_op` changes between calls,
374/// - the depth-stencil shape changes (None → Some / Some → None).
375///
376/// These are all `&'static str` (they come from `WEBGPU_*_OP_*`
377/// constants), so invalidation is a pointer-compare.
378///
379/// `Clone` is derived so the parent `WebGpuRenderer`'s `Data` derive
380/// (which adds a `Clone` bound on every field) keeps compiling;
381/// `js_sys::Object` and `js_sys::Array` both derive `Clone`, so the
382/// derived `Clone` impl just clones the inner JS-side references
383/// (cheap, no JS allocation).
384#[derive(Clone, Debug)]
385pub(crate) struct RenderPassDescriptorCache {
386 /// The cached top-level `GpuRenderPassDescriptor` Object.
387 /// Pass directly to `encoder.beginRenderPass(descriptor)`.
388 pub(crate) descriptor: Object,
389 /// The cached inner color attachment Object.
390 /// `descriptor.colorAttachments[0]` in JS terms.
391 pub(crate) attachment: Object,
392 /// The cached `clearValue` Object (the `{r, g, b, a}` dictionary
393 /// under `attachment.clearValue`). The hot-path field — only
394 /// this is mutated on most frames.
395 pub(crate) clear_value: Object,
396 /// Last applied `loadOp` (as a `&'static str`). Used to detect
397 /// op changes that invalidate the descriptor.
398 pub(crate) last_load_op: Option<&'static str>,
399 /// Last applied `storeOp` (as a `&'static str`). Used to detect
400 /// op changes that invalidate the descriptor.
401 pub(crate) last_store_op: Option<&'static str>,
402 /// Whether the last applied descriptor had a depth-stencil
403 /// attachment (`true`) or not (`false`). Used to detect shape
404 /// changes that invalidate the descriptor.
405 pub(crate) last_has_depth: bool,
406}
407
408/// Describes a 2D viewport rectangle plus optional depth range, in the same
409/// pixel space as the destination render target.
410///
411/// Used by [`WebGpuRenderer::set_viewport`] (and any future caller that needs
412/// to push a `GpuViewport`-shaped JS object through `Reflect::set`). The
413/// depth-range fields are omitted from the `::new` constructor via
414/// `#[new(skip)]`; they default to zero-initialised `f32` and are typically
415/// overwritten by [`WebGpuRenderer::set_viewport`] to the WebGPU spec
416/// defaults of `0.0` / `1.0`.
417#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
418pub struct ViewportDescriptor {
419 /// X coordinate of the viewport's top-left in pixels.
420 pub(crate) x: f32,
421 /// Y coordinate of the viewport's top-left in pixels.
422 pub(crate) y: f32,
423 /// Viewport width in pixels.
424 pub(crate) width: f32,
425 /// Viewport height in pixels.
426 pub(crate) height: f32,
427 /// Minimum depth, clamped to `[0, 1]`. Set to `0.0` to disable.
428 #[new(skip)]
429 pub(crate) min_depth: f32,
430 /// Maximum depth, clamped to `[0, 1]`. Set to `1.0` to disable.
431 #[new(skip)]
432 pub(crate) max_depth: f32,
433}
434
435/// A WebGL 2 rendering backend wrapping the `WebGl2RenderingContext`.
436///
437/// Unlike `WebGpuRenderer`, which stores all GPU handles as opaque `JsValue`s,
438/// WebGL exposes concrete `web_sys` types, so the context and canvas are kept
439/// as strongly typed values. Shader programs created via
440/// [`WebGlRenderer::create_program`] are managed by the caller.
441///
442/// Construct via [`WebGlRenderer::init`], which resolves the canvas from the
443/// [`RenderConfig`], applies device-pixel-ratio scaling to the backing store,
444/// and acquires the `webgl2` context.
445#[derive(Clone, Data)]
446pub struct WebGlRenderer {
447 /// The WebGL 2 rendering context used for all GL calls.
448 pub(crate) context: WebGl2RenderingContext,
449 /// The HTML canvas element backing the WebGL context.
450 pub(crate) canvas: HtmlCanvasElement,
451 /// The physical pixel width of the canvas backing store.
452 #[get(type(copy))]
453 pub(crate) width: u32,
454 /// The physical pixel height of the canvas backing store.
455 #[get(type(copy))]
456 pub(crate) height: u32,
457}
458
459// =====================================================================
460// WebGPU: descriptor & data structs (consumed by the WebGpuRenderer API)
461// =====================================================================
462
463/// A single vertex attribute within a vertex buffer layout.
464///
465/// Mirrors the fields of `GPUVertexAttribute` exactly. The shader location
466/// is the `@location(N)` qualifier in the WGSL source. The offset is in
467/// bytes from the start of the vertex, and `format` is one of the
468/// WGSL vertex format strings (e.g. `"float32x4"`, `"unorm8x4"`).
469#[derive(Clone, Copy, Debug, Eq, Getter, Hash, New, PartialEq)]
470pub struct VertexAttribute {
471 /// The shader location the attribute maps to.
472 #[get(type(copy))]
473 pub(crate) shader_location: u32,
474 /// The byte offset from the start of the vertex.
475 #[get(type(copy))]
476 pub(crate) offset: u64,
477 /// The WGSL vertex format (e.g. `"float32x4"`).
478 #[get(type(clone))]
479 pub(crate) format: &'static str,
480}
481
482/// The layout of a single vertex buffer, expressed as an array stride plus
483/// a list of attributes.
484///
485/// Mirrors `GPUVertexBufferLayout` from the WebGPU spec. The renderer
486/// passes the assembled descriptor straight to `createRenderPipeline` via
487/// `Reflect`.
488#[derive(Clone, Debug, Getter, New)]
489pub struct VertexBufferLayout {
490 /// The byte stride of one vertex in the buffer.
491 #[get(type(copy))]
492 pub(crate) array_stride: u64,
493 /// Whether the buffer should be advanced per-instance (`true`) or
494 /// per-vertex (`false`).
495 #[get(type(copy))]
496 pub(crate) step_mode: VertexStepMode,
497 /// The attributes that describe how to interpret the bytes of one
498 /// vertex.
499 pub(crate) attributes: Vec<VertexAttribute>,
500}
501
502/// A 2D texture descriptor for `create_texture_2d`.
503///
504/// Defaults produce a 1x1 RGBA8 texture with `TEXTURE_BINDING | COPY_DST
505/// | COPY_SRC` usage, which is the right baseline for a sampled color
506/// texture that is uploaded to via `queue.writeTexture`. Override fields
507/// after constructing to set `mip_level_count`, `sample_count`, or
508/// different `usage` flags.
509#[derive(Clone, Debug, Getter, New)]
510pub struct Texture2DDescriptor {
511 /// The texture width in pixels. Must be > 0.
512 #[get(type(copy))]
513 pub(crate) width: u32,
514 /// The texture height in pixels. Must be > 0.
515 #[get(type(copy))]
516 pub(crate) height: u32,
517 /// The WGSL texture format (e.g. `"rgba8unorm"`, `"bgra8unorm"`,
518 /// `"rgba16float"`, `"depth24plus-stencil8"`).
519 #[get(type(clone))]
520 pub(crate) format: &'static str,
521 /// The number of mip levels. `0` is treated as `1`.
522 #[get(type(copy))]
523 #[new(skip)]
524 pub(crate) mip_level_count: u32,
525 /// The number of samples per texel (`1` for non-MSAA, `4` for MSAA).
526 #[get(type(copy))]
527 #[new(skip)]
528 pub(crate) sample_count: u32,
529 /// The WGSL usage flags (e.g. `"RENDER_ATTACHMENT | TEXTURE_BINDING |
530 /// COPY_DST | COPY_SRC"`).
531 #[get(type(clone))]
532 #[new(skip)]
533 pub(crate) usage: &'static str,
534}
535
536/// A sampler descriptor for `create_sampler`.
537///
538/// Defaults produce a non-filtering clamp-to-edge sampler. Override
539/// fields after constructing to enable linear filtering, repeat
540/// addressing, or depth comparison.
541#[derive(Clone, Debug, Getter, New)]
542pub struct GpuSamplerDescriptor {
543 /// Minification filter.
544 #[get(type(clone))]
545 #[new(skip)]
546 pub(crate) mag_filter: &'static str,
547 /// Magnification filter.
548 #[get(type(clone))]
549 #[new(skip)]
550 pub(crate) min_filter: &'static str,
551 /// Mipmap filter.
552 #[get(type(clone))]
553 #[new(skip)]
554 pub(crate) mipmap_filter: &'static str,
555 /// U address mode.
556 #[get(type(clone))]
557 #[new(skip)]
558 pub(crate) address_mode_u: &'static str,
559 /// V address mode.
560 #[get(type(clone))]
561 #[new(skip)]
562 pub(crate) address_mode_v: &'static str,
563 /// W address mode.
564 #[get(type(clone))]
565 #[new(skip)]
566 pub(crate) address_mode_w: &'static str,
567 /// Whether the sampler is a comparison sampler.
568 #[get(type(copy))]
569 #[new(skip)]
570 pub(crate) compare: bool,
571}
572
573/// The descriptor for a single (color or depth-stencil) render pass
574/// attachment, used as input to `begin_render_pass` / `begin_render_pass_to_texture`.
575#[derive(Clone, Debug)]
576pub struct RenderPassColorAttachment {
577 /// The texture view to draw into.
578 ///
579 /// When `None`, the renderer uses the swap-chain view (or the MSAA
580 /// intermediate view if `antialias == true`).
581 pub(crate) view: Option<JsValue>,
582 /// An optional resolve target for MSAA.
583 ///
584 /// `None` when MSAA is disabled. The renderer fills in the default
585 /// resolve target (the swap-chain view) when MSAA is enabled and the
586 /// caller leaves this as `None`.
587 pub(crate) resolve_target: Option<JsValue>,
588 /// The clear color as `(r, g, b, a)` in `0.0..=1.0`. `None` means
589 /// `"load"` (keep the previous contents).
590 pub(crate) clear_value: Option<(f64, f64, f64, f64)>,
591 /// The load operation. `None` → `"clear"` when `clear_value` is
592 /// `Some`, otherwise `"load"`.
593 pub(crate) load_op: Option<&'static str>,
594 /// The store operation. `None` → `"store"`.
595 pub(crate) store_op: Option<&'static str>,
596}
597
598/// The depth-stencil portion of a `RenderPassDescriptor`, used as input to
599/// `begin_render_pass` / `begin_render_pass_to_texture`.
600#[derive(Clone, Debug)]
601pub struct RenderPassDepthStencilAttachment {
602 /// The depth-stencil texture view to use.
603 ///
604 /// When `None`, the renderer uses the default view into its
605 /// `depth_texture` field, allocating the depth texture lazily if
606 /// needed.
607 pub(crate) view: Option<JsValue>,
608 /// The depth clear value in `0.0..=1.0`. `None` means
609 /// `"load"` (keep previous depth).
610 pub(crate) depth_clear_value: Option<f32>,
611 /// The depth load op. `None` → `"clear"` when
612 /// `depth_clear_value` is `Some`, otherwise `"load"`.
613 pub(crate) depth_load_op: Option<&'static str>,
614 /// The depth store op. `None` → `"store"`.
615 pub(crate) depth_store_op: Option<&'static str>,
616 /// Whether depth reads should be enabled. `None` → `false`.
617 pub(crate) depth_read_only: Option<bool>,
618}
619
620/// Descriptor for `GpuTexture.createView(descriptor)`.
621///
622/// Sub-selects a single cube face / mip / array slice / depth-aspect of a
623/// texture. When you need the full texture as a 2D view (the common case),
624/// just call `create_view` without a descriptor; the new method accepts an
625/// `Option<&TextureViewDescriptor>` for callers that need the full
626/// flexibility of the WebGPU spec.
627#[derive(Clone, Debug, Getter, New)]
628pub struct TextureViewDescriptor {
629 /// View format override, or `None` to use the texture's own format.
630 #[get(type(clone))]
631 #[new(value = "None")]
632 pub(crate) format: Option<&'static str>,
633 /// View dimension (`"2d"`, `"2d-array"`, `"cube"`, `"cube-array"`, ...).
634 /// `None` means the dimension is inferred from the texture.
635 #[get(type(clone))]
636 #[new(value = "None")]
637 pub(crate) dimension: Option<&'static str>,
638 /// Most significant mip level (inclusive). `None` → `0`.
639 #[get(type(copy))]
640 #[new(value = "0")]
641 pub(crate) base_mip_level: u32,
642 /// Number of mip levels in the view. `0` → all the way to the top.
643 #[get(type(copy))]
644 #[new(value = "0")]
645 pub(crate) mip_level_count: u32,
646 /// First array layer (inclusive). `None` → `0`. Only meaningful for
647 /// `2d-array` / `cube` / `cube-array` views.
648 #[get(type(copy))]
649 #[new(value = "0")]
650 pub(crate) base_array_layer: u32,
651 /// Number of array layers. `0` → all remaining layers.
652 #[get(type(copy))]
653 #[new(value = "0")]
654 pub(crate) array_layer_count: u32,
655 /// Which aspect of the texture to expose. One of:
656 /// `"all"`, `"depth-only"`, `"stencil-only"`. `None` → `"all"`.
657 #[get(type(clone))]
658 #[new(value = "None")]
659 pub(crate) aspect: Option<&'static str>,
660}
661
662/// Descriptor for `queue.writeTexture(destination, data, dataLayout, size)`.
663///
664/// WebGPU's `writeTexture` lets you upload CPU-side pixel data directly to a
665/// texture without staging through a buffer. Use it for: ImGui font atlases,
666/// procedural noise textures, sprite sheets, `ImageBitmap` pixels, etc.
667#[derive(Clone, Debug, Getter, New)]
668pub struct TextureWriteDescriptor {
669 /// The pixel data to upload. Bytes are laid out according to
670 /// `bytes_per_row` / `rows_per_image`.
671 #[get(type(clone))]
672 pub(crate) data: Vec<u8>,
673 /// Bytes per row of the source data. Must be a multiple of 256.
674 #[get(type(copy))]
675 pub(crate) bytes_per_row: u32,
676 /// Number of rows per image. `0` for 2D textures without mip chains.
677 #[get(type(copy))]
678 pub(crate) rows_per_image: u32,
679 /// Destination mip level to write into.
680 #[get(type(copy))]
681 pub(crate) mip_level: u32,
682 /// Destination texture to write into.
683 #[get(type(clone))]
684 pub(crate) texture: JsValue,
685 /// Origin within the destination texture. `None` → `(0, 0, 0)`.
686 #[get(type(clone))]
687 #[new(value = "None")]
688 pub(crate) origin: Option<JsValue>,
689 /// Whether to flip the source data vertically before writing.
690 /// `true` is essential when uploading from `<img>` / `<canvas>` whose
691 /// rows are top-to-bottom but WebGPU textures are bottom-to-top.
692 #[get(type(copy))]
693 #[new(value = "false")]
694 pub(crate) flip_y: bool,
695}
696
697/// Interior-mutable slot for the renderer's pending error-scope value.
698///
699/// This is the `euv-engine` analog of euv-core's `HandlerRegistryCell`
700/// (`core/src/renderer/registry/struct.rs:62`): a single-element
701/// `Sync` wrapper that holds an `Option<JsValue>` behind an
702/// `UnsafeCell`.
703///
704/// # Why this type exists
705///
706/// `WebGpuRenderer::pending_error` needs interior mutability
707/// because:
708///
709/// 1. `pop_error_sync` takes `&self` (the WebGPU hot path cannot
710/// be `async`), but the spawned `wasm_bindgen_futures::spawn_local`
711/// future must mutate the slot to store the resolved
712/// `Promise<GPUError?>` value.
713/// 2. `take_last_error` also takes `&self` and drains the slot
714/// on the next render tick.
715///
716/// The first implementation used `Rc<RefCell<Option<JsValue>>>`,
717/// which works but pays for:
718///
719/// - a `RefCell::borrow_mut` runtime borrow check on every
720/// write (the panic path is unreachable in practice — only
721/// the spawn_local future and `take_last_error` ever touch
722/// the slot, and they never overlap because the future is
723/// a microtask drained before the next render tick).
724/// - a heap allocation for the `RefCell`'s borrow state.
725///
726/// The newtype keeps the interior-mutability primitive (`Rc`),
727/// because the spawn_local future needs its own owning handle,
728/// but swaps the inner cell from `RefCell` to `UnsafeCell`:
729///
730/// - zero runtime borrow check (the WASM single-threaded
731/// scheduler makes the borrow impossible to violate).
732/// - zero allocation (the cell is just a `*mut Option<JsValue>`
733/// sitting inside the `Rc`-managed box).
734///
735/// # Sync safety
736///
737/// `PendingErrorCell` is **not** `Sync` by default (`UnsafeCell`
738/// explicitly opts out). We hand-implement `Sync` for it because
739/// the renderer is only ever used in the WASM single-threaded
740/// runtime; the `Rc` ensures the same instance is never shared
741/// across threads (it is not `Send`/`Sync` either), and the
742/// WASM main thread is the only place that ever touches the
743/// slot. This matches euv-core's pattern
744/// (`unsafe impl Sync for HandlerRegistryCell {}`).
745///
746/// If the engine is ever compiled for a multi-threaded target
747/// (native, `wasm-bindgen-rayon`), this `unsafe impl Sync` is
748/// unsound and must be removed.
749pub struct PendingErrorCell(
750 /// Interior-mutable storage for the optional `JsValue`.
751 ///
752 /// Marked `pub(crate)` (not just `pub`) because the field is
753 /// only meant to be touched from inside the renderer module —
754 /// specifically from the `impl PendingErrorCell` block in
755 /// `impl.rs`. The struct itself stays `pub` so external code
756 /// can name the type, but the raw `UnsafeCell` is an
757 /// implementation detail.
758 pub(crate) UnsafeCell<Option<JsValue>>,
759);