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
use std::iter;
use std::mem;
use std::num::NonZeroU32;

use bytemuck::{Pod, Zeroable};
use cgmath::Matrix4;
use wgpu::{vertex_attr_array, BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingResource, BindingType, Device, Face, FrontFace, PolygonMode, PrimitiveState, PrimitiveTopology, Sampler, SamplerBindingType, ShaderStages, TextureView, TextureSampleType, TextureViewDimension, VertexAttribute, VertexBufferLayout, VertexStepMode};

use crate::indexmap::IndexMap;

// Vertex

const VERTEX_POS_ATTRS: [VertexAttribute; 1] = vertex_attr_array![ // See vertex shader->@location().
    0 => Float32x3, // pos
];

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
pub struct VertexPos {
    pub pos: [f32; 3],
}

const VERTEX_POSNORMAL_ATTRS: [VertexAttribute; 2] = vertex_attr_array![ // See vertex shader->@location().
    0 => Float32x3, // pos
    1 => Float32x3, // normal
];

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
pub struct VertexPosNormal {
    pub pos: [f32; 3],
    pub normal: [f32; 3],
}

#[derive(Clone, Eq, Hash, PartialEq)]
pub enum VertexShaderType {
    Pos,
    PosNormal,
}

impl VertexShaderType {
    pub fn get_name(&self) -> &str {
        match self {
            VertexShaderType::Pos => "p",
            VertexShaderType::PosNormal => "pn",
        }
    }

    pub fn get_layout(&self) -> VertexBufferLayout<'_> {
        let (array_stride, attributes) = match self {
            VertexShaderType::Pos => (mem::size_of::<VertexPos>(), VERTEX_POS_ATTRS.as_slice()),
            VertexShaderType::PosNormal => (mem::size_of::<VertexPosNormal>(), VERTEX_POSNORMAL_ATTRS.as_slice()),
        };

        VertexBufferLayout {
            array_stride: array_stride.try_into().unwrap(),
            step_mode: VertexStepMode::Vertex,
            attributes,
        }
    }
}

// PrimitiveState

#[derive(Clone, Eq, Hash, PartialEq)]
pub enum PrimitiveStateType {
    LineList,
    TriangleList,
}

impl PrimitiveStateType {
    pub fn get_primitive(&self) -> PrimitiveState {
        let topology = match self {
            PrimitiveStateType::LineList => PrimitiveTopology::LineList,
            PrimitiveStateType::TriangleList => PrimitiveTopology::TriangleList,
        };

        PrimitiveState {
            topology,
            strip_index_format: None,
            front_face: FrontFace::Ccw,
            cull_mode: Some(Face::Back),
            unclipped_depth: false,
            polygon_mode: PolygonMode::Fill,
            conservative: false,
        }
    }
}

// Instance

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
pub struct Color(pub [f32; 3]);

pub const COLOR_WHITE: Color = Color([1.0, 1.0, 1.0]);

impl Color {
    pub fn from_srgb_byte(r: u8, g: u8, b: u8) -> Self {
        // Convert sRGB to linear, see https://physicallybased.info/tools/ .

        let convert = |srgb: u8| {
            let srgb: f32 = srgb as f32 / u8::MAX as f32;

            #[allow(clippy::excessive_precision)]
            if srgb < 0.04045 {
                srgb * 0.0773993808
            } else {
                (srgb * 0.9478672986 + 0.0521327014).powf(2.4)
            }
        };

        Self([convert(r), convert(g), convert(b)])
    }

    pub fn from_srgb_float(r: f32, g: f32, b: f32) -> Self {
        Self::from_srgb_byte((r * 255.0) as u8, (g * 255.0) as u8, (b * 255.0) as u8) // TODO: clamping?
    }
}

#[derive(Clone, Copy)]
pub struct SamplerId(u32);

#[derive(Clone, Copy)]
pub struct TextureId(u32);

#[derive(Clone, Eq, Hash, PartialEq)]
pub enum BindLayoutType {
    Sampler,
    Texture,
}

type BindLayout = (BindLayoutType, u32);

type BindLayouts = Box<[BindLayout]>;

fn empty_bind_layouts() -> BindLayouts {
    Box::from_iter(iter::empty())
}

const INST_SIMPLECOLOR_ATTRS: [VertexAttribute; 5] = vertex_attr_array![ // See vertex shader->@location().
    11 => Float32x3, // color
    12 => Float32x4, // model_m
    13 => Float32x4,
    14 => Float32x4,
    15 => Float32x4,
];

pub struct InstSimpleColor;

impl InstSimpleColor {
    fn new() -> Self {
        Self {
        }
    }

    fn get_bind_layouts(&self) -> BindLayouts {
        empty_bind_layouts()
    }

    fn create_bind_group(&self, _device: &Device, _bg_layout: &BindGroupLayout) -> BindGroup {
        panic!("No bind entries");
    }
}

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
pub struct InstSimpleColorBuf {
    color: Color,
    model_m: [[f32; 4]; 4],
}

impl InstSimpleColorBuf {
    pub fn fill(&mut self, color: &Color, model_m: &Matrix4<f32>) {
        self.color = *color;
        self.model_m = (*model_m).into();
    }
}

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
pub struct PhongParam {
    ambient: f32,
    diffuse: f32,
    specular: f32,
    shininess: f32,
}

impl PhongParam {
    pub const fn new(ambient: f32, diffuse: f32, specular: f32, shininess: f32) -> Self {
        Self {
            ambient,
            diffuse,
            specular,
            shininess,
        }
    }
}

const INST_PHONGCOLOR_ATTRS: [VertexAttribute; 6] = vertex_attr_array![ // See vertex shader->@location().
    10 => Float32x3, // color
    11 => Float32x4, // phong_param
    12 => Float32x4, // model_m
    13 => Float32x4,
    14 => Float32x4,
    15 => Float32x4,
];

pub struct InstPhongColor;

impl InstPhongColor {
    fn new() -> Self {
        Self {
        }
    }

    fn get_bind_layouts(&self) -> BindLayouts {
        empty_bind_layouts()
    }

    fn create_bind_group(&self, _device: &Device, _bg_layout: &BindGroupLayout) -> BindGroup {
        panic!("No bind entries");
    }
}

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
pub struct InstPhongColorBuf {
    color: Color,
    phong_param: PhongParam,
    model_m: [[f32; 4]; 4],
}

impl InstPhongColorBuf {
    pub fn fill(&mut self, color: &Color, phong_param: &PhongParam, model_m: &Matrix4<f32>) {
        self.color = *color;
        self.phong_param = *phong_param;
        self.model_m = (*model_m).into();
    }
}

const INST_GRID_ATTRS: [VertexAttribute; 5] = vertex_attr_array![ // See vertex shader->@location().
    11 => Float32x3, // color
    12 => Float32x4, // model_m
    13 => Float32x4,
    14 => Float32x4,
    15 => Float32x4,
];

pub struct InstGrid;

impl InstGrid {
    fn new() -> Self {
        Self {
        }
    }

    fn get_bind_layouts(&self) -> BindLayouts {
        empty_bind_layouts()
    }

    fn create_bind_group(&self, _device: &Device, _bg_layout: &BindGroupLayout) -> BindGroup {
        panic!("No bind entries");
    }
}

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
pub struct InstGridBuf {
    color: Color,
    model_m: [[f32; 4]; 4],
}

impl InstGridBuf {
    pub fn fill(&mut self, color: &Color, model_m: &Matrix4<f32>) {
        self.color = *color;
        self.model_m = (*model_m).into();
    }
}

const INST_WINDOW_ATTRS: [VertexAttribute; 5] = vertex_attr_array![ // See vertex shader->@location().
    11 => Uint32x2, // bind_id
    12 => Float32x4, // model_m
    13 => Float32x4,
    14 => Float32x4,
    15 => Float32x4,
];

pub struct InstWindow {
    samplers: IndexMap<Sampler>,
    textures: IndexMap<TextureView>, // TODO: What to store? Texture or TextureView?
}

impl InstWindow {
    fn new() -> Self {
        Self {
            samplers: IndexMap::new(),
            textures: IndexMap::new(),
        }
    }

    pub fn add_sampler(&mut self, sampler: &Sampler) -> SamplerId {
        SamplerId(self.samplers.add(sampler.clone()).try_into().unwrap())
    }

    pub fn add_texture(&mut self, texture: &TextureView) -> TextureId {
        TextureId(self.textures.add(texture.clone()).try_into().unwrap())
    }

    fn get_bind_layouts(&self) -> BindLayouts {
        Box::from([
            (BindLayoutType::Sampler, self.samplers.len().try_into().unwrap()), // 0
            (BindLayoutType::Texture, self.textures.len().try_into().unwrap()), // 1
        ])
    }

    fn create_bind_group(&self, device: &Device, bg_layout: &BindGroupLayout) -> BindGroup {
        let sampler_refs = Box::from_iter(self.samplers.iter());
        let texture_refs = Box::from_iter(self.textures.iter());

        device.create_bind_group(&BindGroupDescriptor {
            label: None,
            layout: bg_layout,
            entries: &[
                BindGroupEntry {
                    binding: 0, // See vertex/fragment shader->@binding().
                    resource: BindingResource::SamplerArray(&sampler_refs),
                },
                BindGroupEntry {
                    binding: 1, // See vertex/fragment shader->@binding().
                    resource: BindingResource::TextureViewArray(&texture_refs),
                },
            ]
        })
    }
}

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
pub struct InstWindowBuf {
    bind_id: [u32; 2],
    model_m: [[f32; 4]; 4],
}

impl InstWindowBuf {
    pub fn fill(&mut self, sampler_id: SamplerId, texture_id: TextureId, model_m: &Matrix4<f32>) {
        self.bind_id = [sampler_id.0, texture_id.0];
        self.model_m = (*model_m).into();
    }
}

#[derive(Clone, Eq, Hash, PartialEq)]
pub enum InstShaderType {
    SimpleColor,
    PhongColor,
    Grid,
    Window,
}

impl InstShaderType {
    pub fn get_name(&self) -> &str {
        match self {
            InstShaderType::SimpleColor => "simplec",
            InstShaderType::PhongColor => "phongc",
            InstShaderType::Grid => "grid",
            InstShaderType::Window => "window",
        }
    }

    pub fn get_layout(&self) -> VertexBufferLayout<'_> {
        let (array_stride, attributes) = match self {
            InstShaderType::SimpleColor => (mem::size_of::<InstSimpleColorBuf>(), INST_SIMPLECOLOR_ATTRS.as_slice()),
            InstShaderType::PhongColor => (mem::size_of::<InstPhongColorBuf>(), INST_PHONGCOLOR_ATTRS.as_slice()),
            InstShaderType::Grid => (mem::size_of::<InstGridBuf>(), INST_GRID_ATTRS.as_slice()),
            InstShaderType::Window => (mem::size_of::<InstWindowBuf>(), INST_WINDOW_ATTRS.as_slice()),
        };

        VertexBufferLayout {
            array_stride: array_stride.try_into().unwrap(),
            step_mode: VertexStepMode::Instance,
            attributes,
        }
    }

    pub fn create_impl(&self) -> InstShaderImplType {
        match self { // TODO: Refactor match.
            InstShaderType::SimpleColor => InstShaderImplType::SimpleColor(InstSimpleColor::new()),
            InstShaderType::PhongColor => InstShaderImplType::PhongColor(InstPhongColor::new()),
            InstShaderType::Grid => InstShaderImplType::Grid(InstGrid::new()),
            InstShaderType::Window => InstShaderImplType::Window(InstWindow::new()),
        }
    }
}

pub enum InstShaderImplType {
    SimpleColor(InstSimpleColor),
    PhongColor(InstPhongColor),
    Grid(InstGrid),
    Window(InstWindow),
}

impl InstShaderImplType {
    pub fn get_key(&self) -> BindLayouts {
        self.get_bind_layouts()
    }

    pub fn create_bind_group_layout(&self, device: &Device) -> Option<BindGroupLayout> {
        let layouts = self.get_bind_layouts();

        if !layouts.is_empty() {
            let entries = Box::from_iter(layouts.iter().enumerate().map(|(i, (t, count))| {
                assert!(*count > 0);

                BindGroupLayoutEntry {
                    binding: i.try_into().unwrap(), // See vertex/fragment shader->@binding().
                    visibility: ShaderStages::VERTEX | ShaderStages::FRAGMENT,
                    ty: match t {
                        BindLayoutType::Sampler => BindingType::Sampler(SamplerBindingType::Filtering),
                        BindLayoutType::Texture => BindingType::Texture {
                            sample_type: TextureSampleType::Float {
                                filterable: true,
                            },
                            view_dimension: TextureViewDimension::D2,
                            multisampled: false,
                        }
                    },  
                    count: NonZeroU32::new(*count),
                }
            }));

            Some(device.create_bind_group_layout(&BindGroupLayoutDescriptor {
                label: None,
                entries: &entries
            }))
        } else {
            None
        }
    }

    pub fn create_bind_group(&self, device: &Device, bg_layout: &BindGroupLayout) -> BindGroup {
        // Since BindGroupEntry->BindingResource contains references, we need to create
        // bind group here.

        match self { // TODO: Refactor match.
            InstShaderImplType::SimpleColor(inst_sh_impl) => inst_sh_impl.create_bind_group(device, bg_layout),
            InstShaderImplType::PhongColor(inst_sh_impl) => inst_sh_impl.create_bind_group(device, bg_layout),
            InstShaderImplType::Grid(inst_sh_impl) => inst_sh_impl.create_bind_group(device, bg_layout),
            InstShaderImplType::Window(inst_sh_impl) => inst_sh_impl.create_bind_group(device, bg_layout),
        }
    }

    fn get_bind_layouts(&self) -> BindLayouts {
        match self { // TODO: Refactor match.
            InstShaderImplType::SimpleColor(inst_sh_impl) => inst_sh_impl.get_bind_layouts(),
            InstShaderImplType::PhongColor(inst_sh_impl) => inst_sh_impl.get_bind_layouts(),
            InstShaderImplType::Grid(inst_sh_impl) => inst_sh_impl.get_bind_layouts(),
            InstShaderImplType::Window(inst_sh_impl) => inst_sh_impl.get_bind_layouts(),
        }
    }
}