mugl 0.1.2

Minimalistic Low-level WebGL 2.0 / WebGPU 3D graphics abstraction layer for Rust and WebAssembly
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
//! GPU object descriptors.

use crate::gpu::{GPUWebExt, GPU};
use crate::primitive::{
    AddressMode, BlendFactor, BlendOperation, BufferSize, BufferUsage, Color, ColorWrite,
    CompareFunction, CullMode, Extent3D, FilterMode, FrontFace, IndexFormat, Origin2D, Origin3D,
    PrimitiveTopology, SamplerBindingType, ShaderStage, StencilOperation, TextureDimension,
    TextureFormat, TextureSampleType, TextureUsage, VertexFormat, VertexStepMode,
};

/// This specifies the options to use in creating a Buffer.
#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct BufferDescriptor {
    pub size: BufferSize,
    pub usage: BufferUsage,
}

/// This specifies the options to use in creating a Texture.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct TextureDescriptor {
    pub size: Extent3D,
    pub mip_level_count: u32,
    pub sample_count: u32,
    pub dimension: TextureDimension,
    pub format: TextureFormat,
    pub usage: TextureUsage,
}

impl Default for TextureDescriptor {
    fn default() -> Self {
        Self {
            size: Extent3D(0, 0, 0),
            mip_level_count: 1,
            sample_count: 1,
            dimension: TextureDimension::default(),
            format: TextureFormat::default(),
            usage: TextureUsage::default(),
        }
    }
}

/// This specifies a texture view.
#[derive(Debug)]
pub struct TextureView<'a, G: GPU> {
    pub texture: &'a G::Texture,
    pub mip_level: u32,
    pub slice: u32,
}

impl<'a, G: GPU> From<&'a G::Texture> for TextureView<'a, G> {
    fn from(texture: &'a G::Texture) -> Self {
        Self {
            texture,
            mip_level: 0,
            slice: 0,
        }
    }
}

impl<'a, G: GPU> Clone for TextureView<'a, G> {
    fn clone(&self) -> Self {
        Self {
            texture: self.texture,
            mip_level: self.mip_level,
            slice: self.slice,
        }
    }
}

impl<'a, G: GPU> Copy for TextureView<'a, G> {}

/// This specifies the texture with origin offset for a texture write operation.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpuimagecopytexture>
#[derive(Debug)]
pub struct ImageCopyTexture<'a, G: GPU> {
    pub texture: &'a G::Texture,
    pub mip_level: u32,
    pub origin: Origin3D,
}

impl<'a, G: GPU> From<&'a G::Texture> for ImageCopyTexture<'a, G> {
    fn from(texture: &'a G::Texture) -> Self {
        Self {
            texture,
            mip_level: 0,
            origin: Origin3D(0, 0, 0),
        }
    }
}

impl<'a, G: GPU> Clone for ImageCopyTexture<'a, G> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            texture: self.texture,
            mip_level: self.mip_level,
            origin: self.origin,
        }
    }
}

impl<'a, G: GPU> Copy for ImageCopyTexture<'a, G> {}

/// This specifies the external image source with origin offset for a texture write operation.
#[derive(Debug)]
pub struct ImageCopyExternalImage<'a, G: GPUWebExt> {
    pub src: &'a G::ImageSource,
    pub origin: Origin2D,
}

impl<'a, G: GPUWebExt> From<&'a G::ImageSource> for ImageCopyExternalImage<'a, G> {
    fn from(src: &'a G::ImageSource) -> Self {
        Self {
            src,
            origin: Origin2D(0, 0),
        }
    }
}

impl<'a, G: GPUWebExt> Clone for ImageCopyExternalImage<'a, G> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            src: self.src,
            origin: self.origin,
        }
    }
}

impl<'a, G: GPUWebExt> Copy for ImageCopyExternalImage<'a, G> {}

/// This specifies the layout of a texture image buffer data for a texture write.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpuimagedatalayout>
#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct ImageDataLayout {
    pub offset: BufferSize,
    pub bytes_per_row: u32,
    pub rows_per_image: u32,
}

/// This specifies the options to use in creating a Sampler.
#[derive(Clone, Copy, Debug)]
pub struct SamplerDescriptor {
    pub address_mode_u: AddressMode,
    pub address_mode_v: AddressMode,
    pub address_mode_w: AddressMode,
    pub mag_filter: FilterMode,
    pub min_filter: FilterMode,
    pub mipmap_filter: FilterMode,
    pub lod_min_clamp: f32,
    pub lod_max_clamp: f32,
    pub compare: Option<CompareFunction>,
    /// Max anisotropy level. Requires EXT_texture_filter_anisotropic extension.
    pub max_anisotropy: u8,
}

impl Default for SamplerDescriptor {
    fn default() -> Self {
        Self {
            address_mode_u: AddressMode::default(),
            address_mode_v: AddressMode::default(),
            address_mode_w: AddressMode::default(),
            mag_filter: FilterMode::default(),
            min_filter: FilterMode::default(),
            mipmap_filter: FilterMode::default(),
            lod_min_clamp: 0.,
            lod_max_clamp: 32.,
            compare: None,
            max_anisotropy: 1,
        }
    }
}

/// This specifies the options to use in creating a Shader.
#[derive(Clone, Copy, Debug)]
pub struct ShaderDescriptor<'a> {
    pub code: &'a str,
    pub usage: ShaderStage,
}

/// This describes the state of a render pipeline.
#[derive(Clone, Copy, Debug)]
pub struct RenderPipelineDescriptor<'a, G: GPU> {
    pub vertex: &'a G::Shader,
    pub fragment: &'a G::Shader,
    pub buffers: &'a [VertexBufferLayout<'a>],
    pub bind_groups: &'a [&'a G::BindGroupLayout],
    pub primitive: PrimitiveState,
    pub multisample: MultisampleState,
    pub depth_stencil: Option<DepthStencilState>,
    pub targets: ColorTargetStates<'a>,
}

/// This describes the primitive state of a render pipeline.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpuprimitivestate>
#[derive(Clone, Copy, Debug, Default)]
pub struct PrimitiveState {
    pub topology: PrimitiveTopology,
    pub index_format: Option<IndexFormat>,
    pub front_face: FrontFace,
    pub cull_mode: CullMode,
}

/// This describes the multisample state of a render pipeline.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpumultisamplestate>
#[derive(Clone, Copy, Debug)]
pub struct MultisampleState {
    pub count: u32,
    pub alpha_to_coverage: bool,
}

impl Default for MultisampleState {
    fn default() -> Self {
        Self {
            count: 1,
            alpha_to_coverage: false,
        }
    }
}

/// This describes the depth-stencil state of a render pipeline.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpudepthstencilstate>
#[derive(Clone, Copy, Debug)]
pub struct DepthStencilState {
    pub format: TextureFormat,
    pub depth_write: bool,
    pub depth_compare: CompareFunction,
    pub stencil_front: StencilFaceState,
    pub stencil_back: StencilFaceState,
    pub stencil_read_mask: u32,
    pub stencil_write_mask: u32,
    pub depth_bias: f32,
    pub depth_bias_slope_scale: f32,
    pub depth_bias_clamp: f32,
}

impl Default for DepthStencilState {
    fn default() -> Self {
        Self {
            format: TextureFormat::DEPTH16,
            depth_write: false,
            depth_compare: CompareFunction::default(),
            stencil_front: StencilFaceState::default(),
            stencil_back: StencilFaceState::default(),
            stencil_read_mask: 0xFFFFFFFF,
            stencil_write_mask: 0xFFFFFFFF,
            depth_bias: 0.,
            depth_bias_slope_scale: 0.,
            depth_bias_clamp: 0.,
        }
    }
}

/// This describes a stencil face state of a DepthStencilState.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpudepthstencilstate>
#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct StencilFaceState {
    pub compare: CompareFunction,
    pub fail_op: StencilOperation,
    pub depth_fail_op: StencilOperation,
    pub pass_op: StencilOperation,
}

/// This describes the color target states of a render pipeline.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpucolortargetstate>
#[derive(Clone, Copy, Debug)]
pub enum ColorTargetStates<'a> {
    /// Default render pass color target states.
    Default {
        write_mask: ColorWrite,
        blend: Option<BlendState>,
    },
    /// Offscreen render pass color target states.
    Offscreen { targets: &'a [ColorTargetState] },
}

impl<'a> Default for ColorTargetStates<'a> {
    #[inline]
    fn default() -> Self {
        Self::Default {
            write_mask: Default::default(),
            blend: Default::default(),
        }
    }
}

/// This describes a color target state of a render pipeline.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpucolortargetstate>
#[derive(Clone, Copy, Debug, Default)]
pub struct ColorTargetState {
    pub format: TextureFormat,
    pub write_mask: ColorWrite,
    pub blend: Option<BlendState>,
}

/// This describes the blend state of a color target.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpublendstate>
#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct BlendState {
    pub color: BlendComponent,
    pub alpha: BlendComponent,
}

/// This describes the blend component state.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpublendcomponent>
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct BlendComponent {
    pub operation: BlendOperation,
    pub src_factor: BlendFactor,
    pub dst_factor: BlendFactor,
}

impl Default for BlendComponent {
    #[inline]
    fn default() -> Self {
        Self {
            operation: BlendOperation::default(),
            src_factor: BlendFactor::One,
            dst_factor: BlendFactor::Zero,
        }
    }
}

/// This describes the layout of a vertex buffer.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpuvertexbufferlayout>
#[derive(Clone, Copy, Debug, Default)]
pub struct VertexBufferLayout<'a> {
    pub stride: BufferSize,
    pub step_mode: VertexStepMode,
    pub attributes: &'a [VertexAttribute],
}

/// This describes the layout of a vertex buffer.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpuvertexbufferlayout>
#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct VertexAttribute {
    pub format: VertexFormat,
    pub offset: BufferSize,
    pub shader_location: u32,
}

/// This describes a render pass.
/// See: <https://www.w3.org/TR/webgpu/#render-pass-encoder-creation>
#[derive(Clone, Copy, Debug)]
pub enum RenderPassDescriptor<'a, 'b, G: GPU> {
    /// Default render pass
    Default {
        clear_color: Option<Color>,
        clear_depth: Option<f32>,
        clear_stencil: Option<u32>,
    },

    /// Offscreen render pass
    Offscreen {
        colors: &'b [ColorAttachment<'a, G>],
        depth_stencil: Option<TextureView<'a, G>>,
        clear_depth: Option<f32>,
        clear_stencil: Option<u32>,
    },
}

impl<'a, 'b, G: GPU> Default for RenderPassDescriptor<'a, 'b, G> {
    #[inline]
    fn default() -> Self {
        Self::Default {
            clear_color: None,
            clear_depth: None,
            clear_stencil: None,
        }
    }
}

/// This describes a color attachment for a render pass.
/// See: <https://www.w3.org/TR/webgpu/#dictdef-gpurenderpasscolorattachment>
#[derive(Clone, Copy, Debug)]
pub struct ColorAttachment<'a, G: GPU> {
    pub view: TextureView<'a, G>,
    pub clear: Option<Color>,
}

/// This describes the layout of a uniform resource binding group.
#[derive(Clone, Copy, Debug)]
pub struct BindGroupLayoutDescriptor<'a> {
    pub entries: &'a [BindGroupLayoutEntry<'a>],
}

/// This describes the layout of a single shader uniform resource binding.
#[derive(Clone, Copy, Debug)]
pub struct BindGroupLayoutEntry<'a> {
    pub label: &'a str,
    pub binding: u32,
    pub visibility: ShaderStage,
    pub ty: BindingType,
}

/// This describes the type of a uniform resource binding.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub enum BindingType {
    Buffer {
        dynamic_offset: bool,
    },
    Sampler {
        ty: SamplerBindingType,
    },
    Texture {
        sample_type: TextureSampleType,
        dimension: TextureDimension,
        multisampled: bool,
    },
}

/// This describes a uniform resource binding group.
#[derive(Clone, Copy, Debug)]
pub struct BindGroupDescriptor<'a, G: GPU> {
    pub layout: &'a G::BindGroupLayout,
    pub entries: &'a [BindGroupEntry<'a, G>],
}

/// This describes a single shader uniform resource binding.
#[derive(Clone, Copy, Debug)]
pub struct BindGroupEntry<'a, G: GPU> {
    pub binding: u32,
    pub resource: BindingResource<'a, G>,
}

/// This describes a single shader uniform resource to be bound.
#[derive(Debug)]
pub enum BindingResource<'a, G: GPU> {
    Buffer {
        buffer: &'a G::Buffer,
        offset: BufferSize,
        size: BufferSize,
    },
    Sampler(&'a G::Sampler),
    Texture(&'a G::Texture),
}

impl<'a, G: GPU> Clone for BindingResource<'a, G> {
    fn clone(&self) -> Self {
        match self {
            BindingResource::Buffer {
                buffer,
                offset,
                size,
            } => BindingResource::Buffer {
                buffer,
                offset: *offset,
                size: *size,
            },
            BindingResource::Sampler(ref sampler) => BindingResource::<'a, G>::Sampler(sampler),
            BindingResource::Texture(ref texture) => BindingResource::<'a, G>::Texture(texture),
        }
    }
}

impl<'a, G: GPU> Copy for BindingResource<'a, G> {}