euv_engine/renderer/enum.rs
1use super::*;
2
3/// Defines how new pixels are composited with existing pixels on the canvas.
4///
5/// Maps directly to the CSS `globalCompositeOperation` property.
6#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
7pub enum BlendMode {
8 /// The source is drawn over the destination (default alpha blending).
9 #[default]
10 Normal,
11 /// The source color is multiplied with the destination, producing a darker result.
12 Multiply,
13 /// The source and destination are inverted, multiplied, then inverted again.
14 Screen,
15 /// The source and destination colors are added together, clamped to maximum brightness.
16 Lighter,
17 /// Combines `Multiply` and `Screen` based on the destination color.
18 Overlay,
19 /// Keeps the darker of the source and destination per channel.
20 Darken,
21 /// Keeps the lighter of the source and destination per channel.
22 Lighten,
23 /// Dodges the destination color brightening it based on the source.
24 ColorDodge,
25 /// Burns the destination color darkening it based on the source.
26 ColorBurn,
27 /// A harsher version of `Overlay` using the source color as the filter.
28 HardLight,
29 /// A softer version of `Overlay` using the source color as the filter.
30 SoftLight,
31 /// Subtracts the darker color from the lighter color per channel.
32 Difference,
33 /// Similar to `Difference` but with lower contrast.
34 Exclusion,
35 /// Uses the hue of the source with the saturation and luminosity of the destination.
36 Hue,
37 /// Uses the saturation of the source with the hue and luminosity of the destination.
38 Saturation,
39 /// Uses the hue and saturation of the source with the luminosity of the destination.
40 Color,
41 /// Uses the luminosity of the source with the hue and saturation of the destination.
42 Luminosity,
43}
44
45/// A single deferred draw operation recorded into a `DrawList`.
46///
47/// Commands carry the resolved style for the operation (fill/stroke color, line
48/// width) so that replay can group consecutive same-style shapes into a single
49/// path and skip redundant canvas state changes. Colors are stored as `Color`
50/// and converted to CSS strings only at replay time.
51#[derive(Clone, Debug, PartialEq)]
52pub enum DrawCommand {
53 /// Fills a rectangle. Carries the fill color.
54 FillRect {
55 /// The top-left position in world space.
56 position: Vector2D,
57 /// The width in pixels.
58 width: f64,
59 /// The height in pixels.
60 height: f64,
61 /// The fill color.
62 color: Color,
63 },
64 /// Strokes the outline of a rectangle. Carries stroke color and line width.
65 StrokeRect {
66 /// The top-left position in world space.
67 position: Vector2D,
68 /// The width in pixels.
69 width: f64,
70 /// The height in pixels.
71 height: f64,
72 /// The stroke color.
73 color: Color,
74 /// The stroke line width in pixels.
75 line_width: f64,
76 },
77 /// Fills a circle. Carries the fill color.
78 FillCircle {
79 /// The center in world space.
80 center: Vector2D,
81 /// The radius in pixels.
82 radius: f64,
83 /// The fill color.
84 color: Color,
85 },
86 /// Strokes the outline of a circle. Carries stroke color and line width.
87 StrokeCircle {
88 /// The center in world space.
89 center: Vector2D,
90 /// The radius in pixels.
91 radius: f64,
92 /// The stroke color.
93 color: Color,
94 /// The stroke line width in pixels.
95 line_width: f64,
96 },
97 /// Draws a line segment. Carries stroke color and line width.
98 Line {
99 /// The start point in world space.
100 start: Vector2D,
101 /// The end point in world space.
102 end: Vector2D,
103 /// The stroke color.
104 color: Color,
105 /// The stroke line width in pixels.
106 line_width: f64,
107 },
108 /// Fills text at a position. Carries the fill color and font.
109 FillText {
110 /// The text to draw.
111 text: String,
112 /// The position in world space.
113 position: Vector2D,
114 /// The fill color.
115 color: Color,
116 /// The CSS font string.
117 font: String,
118 },
119 /// Draws a transformed sprite sub-region (image, source rect, TRS transform).
120 DrawSprite {
121 /// The image to draw.
122 image: HtmlImageElement,
123 /// The source rectangle within the image.
124 source: Rect,
125 /// The world-space transform (position, rotation, scale). Scale signs flip.
126 transform: Transform2D,
127 },
128 /// Draws an image sub-region at a destination rect (no rotation).
129 DrawImageRect {
130 /// The image to draw.
131 image: HtmlImageElement,
132 /// The source rectangle within the image.
133 source: Rect,
134 /// The destination top-left position in world space.
135 dest_position: Vector2D,
136 /// The destination width in pixels.
137 dest_width: f64,
138 /// The destination height in pixels.
139 dest_height: f64,
140 },
141 /// Applies a global alpha to all subsequent commands until changed.
142 SetGlobalAlpha {
143 /// The alpha value in the range 0.0 to 1.0.
144 alpha: f64,
145 },
146 /// Applies a blend mode to all subsequent commands until changed.
147 SetBlendMode {
148 /// The blend mode to apply.
149 mode: BlendMode,
150 },
151}
152
153/// Rendering quality preset controlling anti-aliasing smoothing strategy.
154///
155/// Maps to the canvas `imageSmoothingQuality` value plus an explicit
156/// `imageSmoothingEnabled` toggle. Combined with a CSS `image-rendering:
157/// pixelated` rule on the consumer side, `Low` produces crisp pixel-art
158/// rendering while `High` produces smooth vector-style rendering.
159#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
160pub enum RenderQuality {
161 /// Fastest rendering, pixelated scaling.
162 ///
163 /// Disables `imageSmoothingEnabled` on the canvas context and sets
164 /// `imageSmoothingQuality = "low"`. Pair with CSS `image-rendering:
165 /// pixelated` for sharp nearest-neighbour scaling.
166 Low,
167 /// Balanced rendering with default smoothing quality.
168 ///
169 /// Sets `imageSmoothingQuality = "medium"`.
170 Medium,
171 /// Highest fidelity rendering with smooth edges and high-quality scaling.
172 ///
173 /// Sets `imageSmoothingQuality = "high"`. Best for vector-style content
174 /// on HiDPI displays. This is the default — when no explicit quality is
175 /// requested, the engine errs on the side of visual fidelity rather than
176 /// performance, since users typically notice aliasing artifacts before
177 /// they notice a few extra milliseconds of GPU time.
178 #[default]
179 High,
180}
181
182/// Errors that can occur while asynchronously initializing a `WebGpuRenderer`.
183///
184/// Each variant maps to one specific failure mode that the WebGPU init
185/// pipeline can encounter when calling into the browser's GPU API. The
186/// underlying JS error (when available) is carried as a `JsValue` so callers
187/// can surface the exact diagnostic string without losing fidelity.
188///
189/// Instead of logging diagnostics inside the engine, `WebGpuRenderer::init`
190/// returns `Result<WebGpuRenderer, WebGpuInitError>` and lets the caller
191/// decide how to react — typically via `Console::error` on the example side
192/// or by falling back to the Canvas 2D backend.
193#[derive(Clone, Debug)]
194pub enum WebGpuInitError {
195 /// `Reflect::get(navigator, "webgpu")` threw an exception.
196 ///
197 /// Surfaced when the JavaScript binding lookup itself fails rather than
198 /// simply returning `undefined`/`null`. Carries the original JS error.
199 NavigatorLookup(JsValue),
200 /// `navigator.gpu` is `undefined` or `null`.
201 ///
202 /// The browser does not expose WebGPU on the current origin. The most
203 /// common causes are serving over an insecure origin (must be HTTPS or
204 /// `localhost`) or running in a browser that lacks the WebGPU feature.
205 NavigatorGpuMissing,
206 /// `Reflect::get(gpu, "requestAdapter")` threw an exception.
207 ///
208 /// Carries the original JS error returned by the reflect call.
209 RequestAdapterLookup(JsValue),
210 /// `gpu.requestAdapter()` threw an exception synchronously.
211 ///
212 /// Carries the thrown JS error or value.
213 RequestAdapterCall(JsValue),
214 /// The adapter promise rejected, or the `INIT_PROMISE_TIMEOUT_MILLIS`
215 /// race timer fired before the adapter was produced.
216 ///
217 /// Carries the rejection value, which may be a string, an error object,
218 /// or `undefined` when the timeout won the race.
219 AdapterPromise(JsValue),
220 /// `requestAdapter()` resolved to `null` or `undefined`.
221 ///
222 /// No compatible GPU adapter exists for the requested `powerPreference`.
223 AdapterUnavailable,
224 /// `Reflect::get(adapter, "requestDevice")` threw an exception.
225 RequestDeviceLookup(JsValue),
226 /// `adapter.requestDevice()` threw an exception synchronously.
227 RequestDeviceCall(JsValue),
228 /// The device promise rejected, or the `INIT_PROMISE_TIMEOUT_MILLIS`
229 /// race timer fired before the device was produced.
230 DevicePromise(JsValue),
231 /// `requestDevice()` resolved to `null` or `undefined`.
232 ///
233 /// The adapter could not allocate a device, typically because the
234 /// adapter is in a `device-lost` state.
235 DeviceUnavailable,
236 /// `document.querySelector(canvas_selector)` returned `None`.
237 ///
238 /// The canvas element is not in the DOM yet (or its selector is wrong).
239 /// Carries the selector string that was queried.
240 CanvasNotFound(String),
241 /// `document.querySelector(canvas_selector)` threw an exception.
242 CanvasQuery(JsValue),
243 /// `canvas.get_context("webgpu")` returned `None`.
244 ///
245 /// The canvas is already using a different context type, or WebGPU is
246 /// disabled for this canvas.
247 CanvasContextUnavailable,
248 /// `Reflect::get(gpu, "getPreferredCanvasFormat")` threw an exception.
249 PreferredFormatLookup(JsValue),
250 /// `gpu.getPreferredCanvasFormat()` threw an exception synchronously.
251 PreferredFormatCall(JsValue),
252 /// `getPreferredCanvasFormat()` resolved to a value that is not a string.
253 ///
254 /// Carries the offending JS value so callers can log its type/name.
255 PreferredFormatType(JsValue),
256 /// `Reflect::get(context, "configure")` threw an exception.
257 ConfigureLookup(JsValue),
258 /// `Reflect::get(device, "queue")` threw an exception.
259 QueueLookup(JsValue),
260}
261
262/// Errors that can occur while initializing a `WebGlRenderer`.
263///
264/// WebGL context acquisition is synchronous, so the failure modes are far
265/// fewer than `WebGpuInitError` - the canvas must resolve and the browser
266/// must hand back a `WebGl2RenderingContext`. Each variant maps to one
267/// specific failure mode; the caller decides how to surface it (typically
268/// via `Console::error` on the example side).
269#[derive(Clone, Debug)]
270pub enum WebGlInitError {
271 /// `document.querySelector(canvas_selector)` returned `None`.
272 ///
273 /// The canvas element is not in the DOM yet (or its selector is wrong).
274 /// Carries the selector string that was queried.
275 CanvasNotFound(String),
276 /// `document.querySelector(canvas_selector)` threw an exception.
277 CanvasQuery(JsValue),
278 /// `canvas.get_context("webgl2")` returned `None`.
279 ///
280 /// The browser does not support WebGL 2, or the canvas is already bound
281 /// to a different context type.
282 ContextUnavailable,
283 /// `canvas.get_context("webgl2")` threw an exception.
284 ContextLookup(JsValue),
285 /// The object returned by `canvas.get_context("webgl2")` could not be
286 /// cast to `WebGl2RenderingContext`.
287 ContextCast,
288}
289
290/// Errors that can occur while building a WebGL shader program.
291///
292/// Each variant carries the browser-provided info log so the caller can
293/// surface the exact GLSL diagnostic without losing fidelity.
294#[derive(Clone, Debug)]
295pub enum WebGlProgramError {
296 /// Vertex or fragment shader compilation failed.
297 ///
298 /// Carries the shader info log returned by `getShaderInfoLog`.
299 ShaderCompile(String),
300 /// Program linking failed (or `createProgram` returned `None`).
301 ///
302 /// Carries the program info log returned by `getProgramInfoLog`.
303 ProgramLink(String),
304}
305
306/// Whether a vertex buffer is consumed per-vertex or per-instance.
307#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
308pub enum VertexStepMode {
309 /// Advance the buffer one vertex at a time.
310 #[default]
311 Vertex,
312 /// Advance the buffer one entry at a time, for all vertices of an
313 /// instance.
314 Instance,
315}
316
317/// A single binding entry inside a `BindGroupDescriptor`.
318#[derive(Clone, Debug)]
319pub enum BindGroupEntry {
320 /// A uniform / storage buffer binding.
321 ///
322 /// In WGSL terms, the buffer's `usage` must include `UNIFORM` for
323 /// `var<uniform>` bindings and `STORAGE` for `var<storage>` bindings.
324 Buffer {
325 /// The binding slot (matches `@binding(N)` in the shader).
326 binding: u32,
327 /// The `GpuBuffer` handle.
328 buffer: JsValue,
329 /// The byte offset into the buffer where the binding starts.
330 offset: u64,
331 /// The size in bytes of the binding. `None` means "until the end
332 /// of the buffer".
333 size: Option<u64>,
334 },
335 /// A read-write storage texture binding.
336 ///
337 /// The `GpuTexture` must have been created with `STORAGE_BINDING`
338 /// in its `usage` flag. Combine with `view` (a `GpuTextureView`)
339 /// obtained from `GpuTexture.createView()`.
340 StorageTexture {
341 /// The binding slot.
342 binding: u32,
343 /// The `GpuTextureView` handle.
344 view: JsValue,
345 /// `true` for `texture_storage_2d<format, read>` bindings,
346 /// `false` for `texture_storage_2d<format, read_write>` bindings.
347 read_only: bool,
348 },
349 /// A sampled texture binding.
350 Texture {
351 /// The binding slot.
352 binding: u32,
353 /// The `GpuTextureView` handle.
354 view: JsValue,
355 },
356 /// A sampler binding.
357 Sampler {
358 /// The binding slot.
359 binding: u32,
360 /// The `GpuSampler` handle.
361 sampler: JsValue,
362 },
363}
364
365/// The resource kind bound at a single slot of a `BindGroupLayoutEntry`.
366#[derive(Clone, Debug)]
367pub enum BindGroupEntryType {
368 /// `GpuBufferBindingLayout { type: "uniform" }`.
369 UniformBuffer,
370 /// `GpuBufferBindingLayout { type: "storage" | "read-only-storage" }`.
371 StorageBuffer {
372 /// `true` → `"read-only-storage"`, `false` → `"storage"`.
373 read_only: bool,
374 },
375 /// `GpuTextureBindingLayout`.
376 SampledTexture {
377 /// One of `"float"`, `"unfilterable-float"`, `"depth"`, `"sint"`, `"uint"`.
378 sample_type: String,
379 /// `true` if the bound texture is multisampled (matches MSAA render-target sampling).
380 multisampled: bool,
381 },
382 /// `GpuStorageTextureBindingLayout`.
383 StorageTexture {
384 /// `true` → `"read-only"`, `false` → `"read-write"`.
385 read_only: bool,
386 /// Texture format string (e.g. `"rgba8unorm"`, `"r32float"`).
387 format: String,
388 },
389 /// `GpuSamplerBindingLayout`.
390 Sampler {
391 /// `true` for filtering samplers (linear interpolation).
392 filtering: bool,
393 /// `true` for comparison samplers (depth-texture sampling).
394 comparison: bool,
395 },
396}
397
398/// WebGPU receiver-class discriminator for the `cached_method` cache.
399///
400/// JS class methods live on the prototype, so the same `Function` instance
401/// is returned for every receiver of a given class — the `(class, method)`
402/// pair uniquely identifies the cached `Function` and no receiver identity
403/// is needed. This replaces the previous stack-address keying: per-frame
404/// temporary `JsValue`s (pass encoders, command encoders) reuse stack
405/// slots across frames, so an address-keyed cache could return the wrong
406/// class's `Function` when a `GPUComputePassEncoder` landed on a slot that
407/// previously held a `GPURenderPassEncoder` (both share `setPipeline` /
408/// `setBindGroup` / `end`), producing a swallowed TypeError and a silently
409/// skipped GPU call.
410#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
411pub(crate) enum GpuReceiverClass {
412 /// `GPUDevice` (immortal, renderer-owned).
413 Device,
414 /// `GPUQueue` (immortal, renderer-owned).
415 Queue,
416 /// `GPUCanvasContext` (immortal, renderer-owned).
417 Context,
418 /// `GPUTexture` (per-call temporary).
419 Texture,
420 /// `GPUCommandEncoder` (per-frame temporary).
421 CommandEncoder,
422 /// `GPURenderPassEncoder` (per-pass temporary).
423 RenderPass,
424 /// `GPUComputePassEncoder` (per-pass temporary).
425 ComputePass,
426}