gloss-renderer 0.9.0

Core renderer for gloss
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
//! These represent GPU components which are usually added automatically and
//! uploaded from CPU to GPU from the corresponding CPU components. When
//! creating entities, you usually just add CPU components.

use easy_wgpu::texture::TexParams;
use ktx2;
use pollster::FutureExt;
use wgpu;

#[derive(Clone)]
pub struct VertsGPU {
    pub buf: easy_wgpu::buffer::Buffer,
    pub nr_vertices: u32,
    // #[cfg(feature = "burn-torch")]
    // pub tensor: Option<tch::Tensor>,
}
impl VertsGPU {
    //returning a vertex layout witha  dynamic shader location //https://github.com/gfx-rs/wgpu/discussions/2050
    pub fn vertex_buffer_layout<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (3 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x3
            ],
        }
    }
    pub fn vertex_buffer_layout_instanced<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (3 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Instance,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x3
            ],
        }
    }
}

#[derive(Clone)]
pub struct EdgesV1GPU {
    pub buf: easy_wgpu::buffer::Buffer,
    pub nr_vertices: u32,
}
impl EdgesV1GPU {
    //returning a vertex layout witha  dynamic shader location //https://github.com/gfx-rs/wgpu/discussions/2050
    pub fn vertex_buffer_layout<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (3 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x3
            ],
        }
    }
    pub fn vertex_buffer_layout_instanced<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (3 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Instance,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x3
            ],
        }
    }
}
#[derive(Clone)]
pub struct EdgesV2GPU {
    pub buf: easy_wgpu::buffer::Buffer,
    pub nr_vertices: u32,
}
impl EdgesV2GPU {
    //returning a vertex layout witha  dynamic shader location //https://github.com/gfx-rs/wgpu/discussions/2050
    pub fn vertex_buffer_layout<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (3 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x3
            ],
        }
    }
    pub fn vertex_buffer_layout_instanced<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (3 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Instance,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x3
            ],
        }
    }
}

pub struct UVsGPU {
    pub buf: easy_wgpu::buffer::Buffer,
    pub nr_vertices: u32,
}
impl UVsGPU {
    pub fn vertex_buffer_layout<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (2 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x2
            ],
        }
    }
}
pub struct NormalsGPU {
    pub buf: easy_wgpu::buffer::Buffer,
    pub nr_vertices: u32,
}
impl NormalsGPU {
    //returning a vertex layout witha  dynamic shader location //https://github.com/gfx-rs/wgpu/discussions/2050
    pub fn vertex_buffer_layout<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (3 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x3
            ],
        }
    }
}

pub struct TangentsGPU {
    pub buf: easy_wgpu::buffer::Buffer,
    pub nr_vertices: u32,
}
impl TangentsGPU {
    //returning a vertex layout witha  dynamic shader location //https://github.com/gfx-rs/wgpu/discussions/2050
    pub fn vertex_buffer_layout<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            //we make it 4 because we need to also store the handetness
            array_stride: (4 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x4
            ],
        }
    }
}

#[derive(Clone)]
pub struct ColorsGPU {
    pub buf: easy_wgpu::buffer::Buffer,
    pub nr_vertices: u32,
}
impl ColorsGPU {
    //returning a vertex layout witha  dynamic shader location //https://github.com/gfx-rs/wgpu/discussions/2050
    pub fn vertex_buffer_layout<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (3 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x3
            ],
        }
    }
    pub fn vertex_buffer_layout_instanced<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (3 * std::mem::size_of::<f32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Instance,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x3
            ],
        }
    }
}
#[derive(Clone)]
pub struct EdgesGPU {
    pub buf: easy_wgpu::buffer::Buffer,
    pub nr_edges: u32,
}
impl EdgesGPU {
    //returning a vertex layout witha  dynamic shader location //https://github.com/gfx-rs/wgpu/discussions/2050
    pub fn vertex_buffer_layout<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (2 * std::mem::size_of::<u32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x2
            ],
        }
    }
    pub fn vertex_buffer_layout_instanced<const SHADER_LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
        wgpu::VertexBufferLayout {
            array_stride: (2 * std::mem::size_of::<u32>()) as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Instance,
            attributes: &wgpu::vertex_attr_array![
            SHADER_LOCATION => Float32x2
            ],
        }
    }
}

pub struct FacesGPU {
    pub buf: easy_wgpu::buffer::Buffer,
    pub nr_triangles: u32,
}

#[derive(Clone)]
pub struct DiffuseTex(pub easy_wgpu::texture::Texture);
#[derive(Clone)]
pub struct NormalTex(pub easy_wgpu::texture::Texture);
#[derive(Clone)]
pub struct MetalnessTex(pub easy_wgpu::texture::Texture);
#[derive(Clone)]
pub struct RoughnessTex(pub easy_wgpu::texture::Texture);

pub trait TextureGetter {
    fn texture(&self) -> &easy_wgpu::texture::Texture;
    fn texture_mut(&mut self) -> &mut easy_wgpu::texture::Texture;
}

impl TextureGetter for DiffuseTex {
    fn texture_mut(&mut self) -> &mut easy_wgpu::texture::Texture {
        &mut self.0
    }
    fn texture(&self) -> &easy_wgpu::texture::Texture {
        &self.0
    }
}

impl TextureGetter for NormalTex {
    fn texture_mut(&mut self) -> &mut easy_wgpu::texture::Texture {
        &mut self.0
    }
    fn texture(&self) -> &easy_wgpu::texture::Texture {
        &self.0
    }
}

impl TextureGetter for MetalnessTex {
    fn texture_mut(&mut self) -> &mut easy_wgpu::texture::Texture {
        &mut self.0
    }
    fn texture(&self) -> &easy_wgpu::texture::Texture {
        &self.0
    }
}

impl TextureGetter for RoughnessTex {
    fn texture_mut(&mut self) -> &mut easy_wgpu::texture::Texture {
        &mut self.0
    }
    fn texture(&self) -> &easy_wgpu::texture::Texture {
        &self.0
    }
}

#[derive(Clone)]
pub struct EnvironmentMapGpu {
    pub diffuse_tex: easy_wgpu::texture::Texture,
    pub specular_tex: easy_wgpu::texture::Texture,
}
impl EnvironmentMapGpu {
    pub fn new_dummy(device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
        let diffuse_tex = easy_wgpu::texture::Texture::create_default_cubemap(device, queue);
        let specular_tex = easy_wgpu::texture::Texture::create_default_cubemap(device, queue);
        Self { diffuse_tex, specular_tex }
    }

    /// # Panics
    /// Will panic if the environment map is not the expected format
    #[allow(clippy::similar_names)]
    pub fn reader2texture(reader: &ktx2::Reader<&[u8]>, device: &wgpu::Device, queue: &wgpu::Queue) -> easy_wgpu::texture::Texture {
        // Get general texture information.
        let header = reader.header();
        assert_eq!(header.format, Some(ktx2::Format::R16G16B16A16_SFLOAT));
        let nr_mips = header.level_count;
        let width = header.pixel_width;
        let height = header.pixel_height;
        // println!("nr mips {}", nr_mips);
        // println!("header is {:?}", header);

        //make texture
        let usages = wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST;
        let desc = wgpu::TextureDescriptor {
            label: None,
            size: wgpu::Extent3d {
                width,
                height,
                depth_or_array_layers: 6, //it's a cube txture so it's always 6 layers
            },
            mip_level_count: nr_mips,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Rgba16Float,
            usage: usages,
            view_formats: &[],
        };
        let tex_params = TexParams::from_desc(&desc);
        let texture = device.create_texture(&desc); //create with all mips but upload only 1 mip

        //TODO maybe possible to upload all the mips at the same time using reader.data
        // which gives you all the mips

        // Read iterator over slices of each mipmap level.
        let levels = reader.levels().collect::<Vec<_>>();
        for (mip, level_data) in levels.iter().enumerate() {
            easy_wgpu::texture::Texture::upload_single_mip(&texture, device, queue, &desc, level_data, None, u32::try_from(mip).unwrap())
                .block_on()
                .unwrap();
        }

        //view and sampler
        let view = texture.create_view(&wgpu::TextureViewDescriptor {
            dimension: Some(wgpu::TextureViewDimension::Cube),
            ..Default::default()
        });

        let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            mipmap_filter: wgpu::FilterMode::Linear,
            ..Default::default()
        });

        easy_wgpu::texture::Texture {
            texture,
            view,
            sampler,
            tex_params,
            #[cfg(feature = "burn-torch")]
            staging_buffer_backed_by_cuda_mem: None,
        }
    }
}

//implement some atributes for the vertex atributes so we can use them in a
// generic function
pub trait GpuAtrib {
    fn data_ref(&self) -> &wgpu::Buffer;
    fn new_from(buf: wgpu::Buffer, nr_rows: u32) -> Self;
}

impl GpuAtrib for VertsGPU {
    fn data_ref(&self) -> &wgpu::Buffer {
        &self.buf.buffer
    }
    fn new_from(buf: wgpu::Buffer, nr_vertices: u32) -> Self {
        Self {
            buf: easy_wgpu::buffer::Buffer::new_from_buffer(buf),
            nr_vertices,
            // #[cfg(feature = "burn-torch")]
            // None,
        }
    }
}
impl GpuAtrib for EdgesV1GPU {
    fn data_ref(&self) -> &wgpu::Buffer {
        &self.buf.buffer
    }
    fn new_from(buf: wgpu::Buffer, nr_vertices: u32) -> Self {
        Self {
            buf: easy_wgpu::buffer::Buffer::new_from_buffer(buf),
            nr_vertices,
        }
    }
}
impl GpuAtrib for EdgesV2GPU {
    fn data_ref(&self) -> &wgpu::Buffer {
        &self.buf.buffer
    }
    fn new_from(buf: wgpu::Buffer, nr_vertices: u32) -> Self {
        Self {
            buf: easy_wgpu::buffer::Buffer::new_from_buffer(buf),
            nr_vertices,
        }
    }
}
impl GpuAtrib for EdgesGPU {
    fn data_ref(&self) -> &wgpu::Buffer {
        &self.buf.buffer
    }
    fn new_from(buf: wgpu::Buffer, nr_edges: u32) -> Self {
        Self {
            buf: easy_wgpu::buffer::Buffer::new_from_buffer(buf),
            nr_edges,
        }
    }
}
impl GpuAtrib for FacesGPU {
    fn data_ref(&self) -> &wgpu::Buffer {
        &self.buf.buffer
    }
    fn new_from(buf: wgpu::Buffer, nr_triangles: u32) -> Self {
        Self {
            buf: easy_wgpu::buffer::Buffer::new_from_buffer(buf),
            nr_triangles,
        }
    }
}
impl GpuAtrib for UVsGPU {
    fn data_ref(&self) -> &wgpu::Buffer {
        &self.buf.buffer
    }
    fn new_from(buf: wgpu::Buffer, nr_vertices: u32) -> Self {
        Self {
            buf: easy_wgpu::buffer::Buffer::new_from_buffer(buf),
            nr_vertices,
        }
    }
}
impl GpuAtrib for NormalsGPU {
    fn data_ref(&self) -> &wgpu::Buffer {
        &self.buf.buffer
    }
    fn new_from(buf: wgpu::Buffer, nr_vertices: u32) -> Self {
        Self {
            buf: easy_wgpu::buffer::Buffer::new_from_buffer(buf),
            nr_vertices,
        }
    }
}
impl GpuAtrib for TangentsGPU {
    fn data_ref(&self) -> &wgpu::Buffer {
        &self.buf.buffer
    }
    fn new_from(buf: wgpu::Buffer, nr_vertices: u32) -> Self {
        Self {
            buf: easy_wgpu::buffer::Buffer::new_from_buffer(buf),
            nr_vertices,
        }
    }
}
impl GpuAtrib for ColorsGPU {
    fn data_ref(&self) -> &wgpu::Buffer {
        &self.buf.buffer
    }
    fn new_from(buf: wgpu::Buffer, nr_vertices: u32) -> Self {
        Self {
            buf: easy_wgpu::buffer::Buffer::new_from_buffer(buf),
            nr_vertices,
        }
    }
}

//so we can use the Components inside the Mutex<Hashmap> in the scene and wasm
// https://stackoverflow.com/a/73773940/22166964
// shenanigans
//vertsgpu
#[cfg(target_arch = "wasm32")]
unsafe impl Send for VertsGPU {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for VertsGPU {}
//vertsgpue1
#[cfg(target_arch = "wasm32")]
unsafe impl Send for EdgesV1GPU {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for EdgesV1GPU {}
//vertsgpue2
#[cfg(target_arch = "wasm32")]
unsafe impl Send for EdgesV2GPU {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for EdgesV2GPU {}
//edgesgpu
#[cfg(target_arch = "wasm32")]
unsafe impl Send for EdgesGPU {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for EdgesGPU {}
//facesgpu
#[cfg(target_arch = "wasm32")]
unsafe impl Send for FacesGPU {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for FacesGPU {}
//uvsgpu
#[cfg(target_arch = "wasm32")]
unsafe impl Send for UVsGPU {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for UVsGPU {}
//normalsgpu
#[cfg(target_arch = "wasm32")]
unsafe impl Send for NormalsGPU {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for NormalsGPU {}
//tangentsgpu
#[cfg(target_arch = "wasm32")]
unsafe impl Send for TangentsGPU {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for TangentsGPU {}
//colors
#[cfg(target_arch = "wasm32")]
unsafe impl Send for ColorsGPU {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for ColorsGPU {}
//Diffusetex
#[cfg(target_arch = "wasm32")]
unsafe impl Send for DiffuseTex {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for DiffuseTex {}
//Normaltex
#[cfg(target_arch = "wasm32")]
unsafe impl Send for NormalTex {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for NormalTex {}
//Metalnesstex
#[cfg(target_arch = "wasm32")]
unsafe impl Send for MetalnessTex {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for MetalnessTex {}
//Roughnesstex
#[cfg(target_arch = "wasm32")]
unsafe impl Send for RoughnessTex {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for RoughnessTex {}
//EnvironmentMap
#[cfg(target_arch = "wasm32")]
unsafe impl Send for EnvironmentMapGpu {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for EnvironmentMapGpu {}
// #[cfg(target_arch = "wasm32")]
// unsafe impl Send for torch_sys::C_tensor {}
// #[cfg(target_arch = "wasm32")]
// unsafe impl Sync for torch_sys::C_tensor {}