euv-engine 0.28.25

A high-performance 2D game engine built on the euv framework, featuring ECS, fixed-timestep game loop, canvas rendering, physics, collision detection, sprite animation, and audio.
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
use super::*;

/// The `loadOp` / `depthLoadOp` wire value.
///
/// The single place the renderer turns a [`LoadOp`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `LoadOp` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into the attachment's `loadOp` or
///   `depthLoadOp` field.
pub(crate) fn load_op_name(value: LoadOp) -> &'static str {
    match value {
        LoadOp::Clear => WEBGPU_LOAD_OP_CLEAR,

        LoadOp::Load => WEBGPU_LOAD_OP_LOAD,
    }
}

/// The `storeOp` / `depthStoreOp` wire value.
///
/// The single place the renderer turns a [`StoreOp`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `StoreOp` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into the attachment's `storeOp` or
///   `depthStoreOp` field.
pub(crate) fn store_op_name(value: StoreOp) -> &'static str {
    match value {
        StoreOp::Store => WEBGPU_STORE_OP_STORE,

        StoreOp::Discard => WEBGPU_STORE_OP_DISCARD,
    }
}

/// The `GPUBufferUsage` bit for one buffer use.
///
/// The single place the renderer turns a [`BufferUsage`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `BufferUsage` - The variant to translate.
///
/// # Returns
///
/// - `u32` - the bit this use occupies in the `usage` bitmask of a
///   `GpuBufferDescriptor`; combine several with `|`.
pub(crate) fn buffer_usage_bit(value: BufferUsage) -> u32 {
    match value {
        BufferUsage::MapRead => RENDER_USAGE_MAP_READ as u32,

        BufferUsage::MapWrite => RENDER_USAGE_MAP_WRITE as u32,

        BufferUsage::CopySource => RENDER_USAGE_COPY_SRC as u32,

        BufferUsage::CopyDestination => RENDER_USAGE_COPY_DST as u32,

        BufferUsage::Index => RENDER_USAGE_INDEX as u32,

        BufferUsage::Vertex => RENDER_USAGE_VERTEX as u32,

        BufferUsage::Uniform => RENDER_USAGE_UNIFORM as u32,

        BufferUsage::Storage => RENDER_USAGE_STORAGE as u32,

        BufferUsage::Indirect => RENDER_USAGE_INDIRECT as u32,

        BufferUsage::QueryResolve => RENDER_USAGE_QUERY_RESOLVE as u32,
    }
}

/// The `GPUTextureUsage` bit for one texture use.
///
/// The single place the renderer turns a [`TextureUsage`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `TextureUsage` - The variant to translate.
///
/// # Returns
///
/// - `u32` - the bit this use occupies in the `usage` bitmask of a
///   `GpuTextureDescriptor`; combine several with `|`.
pub(crate) fn texture_usage_bit(value: TextureUsage) -> u32 {
    match value {
        TextureUsage::CopySource => RENDER_TEXTURE_USAGE_COPY_SRC as u32,

        TextureUsage::CopyDestination => RENDER_TEXTURE_USAGE_COPY_DST as u32,

        TextureUsage::TextureBinding => RENDER_USAGE_TEXTURE_BINDING as u32,

        TextureUsage::StorageBinding => RENDER_USAGE_STORAGE_BINDING as u32,

        TextureUsage::RenderAttachment => RENDER_USAGE_RENDER_ATTACHMENT as u32,
    }
}

/// The `GPUShaderStage` bit for one pipeline stage.
///
/// The single place the renderer turns a [`ShaderStage`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `ShaderStage` - The variant to translate.
///
/// # Returns
///
/// - `u32` - the bit this stage occupies in the `visibility` bitmask of a bind
///   group layout entry; combine several with `|`.
pub(crate) fn shader_stage_bit(value: ShaderStage) -> u32 {
    match value {
        ShaderStage::Vertex => SHADER_STAGE_VERTEX as u32,

        ShaderStage::Fragment => SHADER_STAGE_FRAGMENT as u32,

        ShaderStage::Compute => SHADER_STAGE_COMPUTE as u32,
    }
}

/// The `magFilter` / `minFilter` wire value.
///
/// The single place the renderer turns a [`FilterMode`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `FilterMode` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into a sampler's magnification or
///   minification filter field.
pub fn filter_mode_name(value: FilterMode) -> &'static str {
    match value {
        FilterMode::Nearest => WEBGPU_FILTER_MODE_NEAREST,

        FilterMode::Linear => WEBGPU_FILTER_MODE_LINEAR,
    }
}

/// The `mipmapFilter` wire value.
///
/// The single place the renderer turns a [`MipmapFilter`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `MipmapFilter` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into a sampler's `mipmapFilter` field.
pub fn mipmap_filter_name(value: MipmapFilter) -> &'static str {
    match value {
        MipmapFilter::Nearest => WEBGPU_FILTER_MODE_NEAREST,

        MipmapFilter::Linear => WEBGPU_FILTER_MODE_LINEAR,
    }
}

/// The `addressModeU` / `V` / `W` wire value.
///
/// The single place the renderer turns a [`AddressMode`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `AddressMode` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into the requested axis of a sampler's
///   address-mode field.
pub fn address_mode_name(value: AddressMode) -> &'static str {
    match value {
        AddressMode::ClampToEdge => WEBGPU_ADDRESS_MODE_CLAMP_TO_EDGE,

        AddressMode::MirrorRepeat => WEBGPU_ADDRESS_MODE_MIRROR_REPEAT,

        AddressMode::Repeat => WEBGPU_ADDRESS_MODE_REPEAT,
    }
}

/// The WebGPU comparison-operator wire value.
///
/// The single place the renderer turns a [`CompareFunction`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `CompareFunction` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into a depth-stencil state's
///   `depthCompare` field or a comparison sampler's `compare` field.
pub(crate) fn compare_function_name(value: CompareFunction) -> &'static str {
    match value {
        CompareFunction::Never => WEBGPU_COMPARE_NEVER,

        CompareFunction::Less => WEBGPU_COMPARE_LESS,

        CompareFunction::Equal => WEBGPU_COMPARE_EQUAL,

        CompareFunction::LessEqual => WEBGPU_COMPARE_LESS_EQUAL,

        CompareFunction::Greater => WEBGPU_COMPARE_GREATER,

        CompareFunction::NotEqual => WEBGPU_COMPARE_NOT_EQUAL,

        CompareFunction::GreaterEqual => WEBGPU_COMPARE_GREATER_EQUAL,

        CompareFunction::Always => WEBGPU_COMPARE_ALWAYS,
    }
}

/// The WebGPU blend-factor wire value.
///
/// The single place the renderer turns a [`BlendFactor`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `BlendFactor` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into a blend component's `srcFactor`
///   or `dstFactor` field.
pub(crate) fn blend_factor_name(value: BlendFactor) -> &'static str {
    match value {
        BlendFactor::Zero => WEBGPU_BLEND_FACTOR_ZERO,

        BlendFactor::One => WEBGPU_BLEND_FACTOR_ONE,

        BlendFactor::SourceColor => WEBGPU_BLEND_FACTOR_SRC,

        BlendFactor::OneMinusSourceColor => WEBGPU_BLEND_FACTOR_ONE_MINUS_SRC,

        BlendFactor::DestinationColor => WEBGPU_BLEND_FACTOR_DST,

        BlendFactor::OneMinusDestinationColor => WEBGPU_BLEND_FACTOR_ONE_MINUS_DST,

        BlendFactor::SourceAlpha => WEBGPU_BLEND_FACTOR_SRC_ALPHA,

        BlendFactor::OneMinusSourceAlpha => WEBGPU_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,

        BlendFactor::DestinationAlpha => WEBGPU_BLEND_FACTOR_DST_ALPHA,

        BlendFactor::OneMinusDestinationAlpha => WEBGPU_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,

        BlendFactor::ConstantColor => WEBGPU_BLEND_FACTOR_CONSTANT,

        BlendFactor::OneMinusConstantColor => WEBGPU_BLEND_FACTOR_ONE_MINUS_CONSTANT,

        BlendFactor::ConstantAlpha => WEBGPU_BLEND_FACTOR_CONSTANT_ALPHA,

        BlendFactor::OneMinusConstantAlpha => WEBGPU_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA,

        BlendFactor::SourceAlphaSaturated => WEBGPU_BLEND_FACTOR_SRC_ALPHA_SATURATED,
    }
}

/// The WebGPU blend-operation wire value.
///
/// The single place the renderer turns a [`BlendOperation`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `BlendOperation` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into a blend component's `operation`
///   field.
pub(crate) fn blend_operation_name(value: BlendOperation) -> &'static str {
    match value {
        BlendOperation::Add => WEBGPU_BLEND_OPERATION_ADD,

        BlendOperation::Subtract => WEBGPU_BLEND_OPERATION_SUBTRACT,

        BlendOperation::ReverseSubtract => WEBGPU_BLEND_OPERATION_REVERSE_SUBTRACT,

        BlendOperation::Min => WEBGPU_BLEND_OPERATION_MIN,

        BlendOperation::Max => WEBGPU_BLEND_OPERATION_MAX,
    }
}

/// The WebGPU primitive-topology wire value.
///
/// The single place the renderer turns a [`PrimitiveTopology`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `PrimitiveTopology` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into a pipeline's `primitive.topology`
///   field.
pub(crate) fn primitive_topology_name(value: PrimitiveTopology) -> &'static str {
    match value {
        PrimitiveTopology::PointList => WEBGPU_PRIMITIVE_TOPOLOGY_POINT_LIST,

        PrimitiveTopology::LineList => WEBGPU_PRIMITIVE_TOPOLOGY_LINE_LIST,

        PrimitiveTopology::LineStrip => WEBGPU_PRIMITIVE_TOPOLOGY_LINE_STRIP,

        PrimitiveTopology::TriangleList => WEBGPU_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,

        PrimitiveTopology::TriangleStrip => WEBGPU_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
    }
}

/// The WebGPU index-format wire value.
///
/// The single place the renderer turns a [`IndexFormat`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `IndexFormat` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into a strip primitive's
///   `stripIndexFormat` field or into a `setIndexBuffer` call.
pub(crate) fn index_format_name(value: IndexFormat) -> &'static str {
    match value {
        IndexFormat::Uint16 => WEBGPU_INDEX_FORMAT_UINT16,

        IndexFormat::Uint32 => WEBGPU_INDEX_FORMAT_UINT32,
    }
}

/// The WebGPU front-face wire value.
///
/// The single place the renderer turns a [`FrontFace`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `FrontFace` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into a pipeline's `primitive.frontFace`
///   field.
pub(crate) fn front_face_name(value: FrontFace) -> &'static str {
    match value {
        FrontFace::CounterClockwise => WEBGPU_FRONT_FACE_COUNTER_CLOCKWISE,

        FrontFace::Clockwise => WEBGPU_FRONT_FACE_CLOCKWISE,
    }
}

/// The WebGPU cull-mode wire value.
///
/// The single place the renderer turns a [`CullMode`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `CullMode` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into a pipeline's `primitive.cullMode`
///   field.
pub(crate) fn cull_mode_name(value: CullMode) -> &'static str {
    match value {
        CullMode::None => WEBGPU_CULL_MODE_NONE,

        CullMode::Front => WEBGPU_CULL_MODE_FRONT,

        CullMode::Back => WEBGPU_CULL_MODE_BACK,
    }
}

/// The WebGPU vertex-attribute-format wire value.
///
/// The single place the renderer turns a [`VertexAttributeFormat`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `VertexAttributeFormat` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into one vertex attribute's `format`
///   field.
pub(crate) fn vertex_attribute_format_name(value: VertexAttributeFormat) -> &'static str {
    match value {
        VertexAttributeFormat::Float32 => WEBGPU_VERTEX_FORMAT_FLOAT32,

        VertexAttributeFormat::Float32x2 => WEBGPU_VERTEX_FORMAT_FLOAT32X2,

        VertexAttributeFormat::Float32x3 => WEBGPU_VERTEX_FORMAT_FLOAT32X3,

        VertexAttributeFormat::Float32x4 => WEBGPU_VERTEX_FORMAT_FLOAT32X4,

        VertexAttributeFormat::Unorm8x4 => WEBGPU_VERTEX_FORMAT_UNORM8X4,

        VertexAttributeFormat::Uint32 => WEBGPU_VERTEX_FORMAT_UINT32,

        VertexAttributeFormat::Sint32 => WEBGPU_VERTEX_FORMAT_SINT32,
    }
}

/// The WebGPU texture-format wire value.
///
/// The single place the renderer turns a [`GpuTextureFormat`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `GpuTextureFormat` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer writes into a texture descriptor's `format`
///   field or a color target state's format.
pub(crate) fn gpu_texture_format_name(value: GpuTextureFormat) -> &'static str {
    match value {
        GpuTextureFormat::Rgba8Unorm => WEBGPU_FORMAT_RGBA8UNORM,

        GpuTextureFormat::Bgra8Unorm => WEBGPU_FORMAT_BGRA8UNORM,

        GpuTextureFormat::Rgba16Float => WEBGPU_FORMAT_RGBA16FLOAT,

        GpuTextureFormat::Depth24Plus => WEBGPU_DEPTH_FORMAT_DEPTH24_PLUS,

        GpuTextureFormat::Depth24PlusStencil8 => WEBGPU_DEPTH_FORMAT_DEPTH24_PLUS_STENCIL8,

        GpuTextureFormat::Depth32Float => WEBGPU_DEPTH_FORMAT_DEPTH32_FLOAT,

        GpuTextureFormat::R32Float => WEBGPU_FORMAT_R32FLOAT,

        GpuTextureFormat::Rgba32Float => WEBGPU_FORMAT_RGBA32FLOAT,
    }
}

/// The WebGPU error-filter wire value.
///
/// The single place the renderer turns a [`GpuErrorFilter`] into the value the
/// WebGPU API expects, so a variant can never reach the wire as a
/// mistyped literal.
///
/// # Arguments
///
/// - `GpuErrorFilter` - The variant to translate.
///
/// # Returns
///
/// - `&'static str` - the value the renderer passes to `GPUDevice.pushErrorScope`.
pub(crate) fn gpu_error_filter_name(value: GpuErrorFilter) -> &'static str {
    match value {
        GpuErrorFilter::Validation => WEBGPU_ERROR_FILTER_VALIDATION,

        GpuErrorFilter::OutOfMemory => WEBGPU_ERROR_FILTER_OUT_OF_MEMORY,

        GpuErrorFilter::Internal => WEBGPU_ERROR_FILTER_INTERNAL,
    }
}

/// Combines several texture uses into one `GPUTextureUsage` bitmask.
///
/// `|` is left-associative and each step yields a `u32`, so it cannot
/// fold an arbitrary number of variants. This helper is the variadic
/// form for the call sites that need more than two uses - the
/// four-way storage-texture mask the compute path allocates, for
/// instance.
///
/// # Arguments
///
/// - `&[TextureUsage]` - The uses to combine, in any order and with
///   no repeats.
///
/// # Returns
///
/// - `u32` - The bitmask carrying every listed use.
pub fn texture_usage_mask(usages: &[TextureUsage]) -> u32 {
    let mut mask: u32 = 0;
    for usage in usages {
        mask |= texture_usage_bit(*usage);
    }
    mask
}

/// Combines several buffer uses into one `GPUBufferUsage` bitmask.
///
/// The buffer counterpart of the variadic
/// [`texture_usage_mask`], for the call sites that need three or more
/// uses at once.
///
/// # Arguments
///
/// - `&[BufferUsage]` - The uses to combine, in any order and with no
///   repeats.
///
/// # Returns
///
/// - `u32` - The bitmask carrying every listed use.
pub(crate) fn buffer_usage_mask(usages: &[BufferUsage]) -> u32 {
    let mut mask: u32 = 0;
    for usage in usages {
        mask |= buffer_usage_bit(*usage);
    }
    mask
}