three-d 0.7.2

A renderer which compiles to both desktop (OpenGL) and web (WebAssembly + WebGL).
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
use web_sys::WebGl2RenderingContext as InnerGl;

#[allow(non_camel_case_types)]
pub type consts = InnerGl;

pub type AttributeLocation = u32;
pub use web_sys::WebGlActiveInfo as ActiveInfo;
pub use web_sys::WebGlBuffer as Buffer;
pub use web_sys::WebGlFramebuffer as Framebuffer;
pub use web_sys::WebGlProgram as Program;
pub use web_sys::WebGlShader as Shader;
pub use web_sys::WebGlSync as Sync;
pub use web_sys::WebGlTexture as Texture;
pub use web_sys::WebGlUniformLocation as UniformLocation;
pub use web_sys::WebGlVertexArrayObject as VertexArrayObject;

#[derive(Clone)]
pub struct Context {
    inner: std::rc::Rc<InnerGl>,
}

impl Context {
    pub fn new(webgl_context: InnerGl) -> Self {
        Self {
            inner: std::rc::Rc::new(webgl_context),
        }
    }

    pub fn finish(&self) {
        self.inner.finish();
    }

    pub fn bind_buffer_base(&self, target: u32, index: u32, buffer: &Buffer) {
        self.inner.bind_buffer_base(target, index, Some(buffer));
    }

    pub fn bind_buffer(&self, target: u32, buffer: &Buffer) {
        self.inner.bind_buffer(target, Some(buffer));
    }

    pub fn delete_buffer(&self, buffer: &Buffer) {
        self.inner.delete_buffer(Some(buffer));
    }

    pub fn unbind_buffer(&self, target: u32) {
        self.inner.bind_buffer(target, None);
    }

    pub fn buffer_data(&self, target: u32, size_in_bytes: u32, usage: u32) {
        self.inner
            .buffer_data_with_i32(target, size_in_bytes as i32, usage);
    }

    pub fn buffer_data_u8(&self, target: u32, data: &[u8], usage: u32) {
        self.inner.buffer_data_with_u8_array(target, data, usage)
    }

    pub fn buffer_data_u32(&self, target: u32, data: &[u32], usage: u32) {
        use wasm_bindgen::JsCast;
        let memory_buffer = wasm_bindgen::memory()
            .dyn_into::<js_sys::WebAssembly::Memory>()
            .unwrap()
            .buffer();
        let data_location = data.as_ptr() as u32 / 4;
        let array = js_sys::Uint32Array::new(&memory_buffer)
            .subarray(data_location, data_location + data.len() as u32);

        self.inner
            .buffer_data_with_array_buffer_view(target, &array, usage);
    }

    pub fn buffer_data_f32(&self, target: u32, data: &[f32], usage: u32) {
        use wasm_bindgen::JsCast;
        let memory_buffer = wasm_bindgen::memory()
            .dyn_into::<js_sys::WebAssembly::Memory>()
            .unwrap()
            .buffer();
        let data_location = data.as_ptr() as u32 / 4;
        let array = js_sys::Float32Array::new(&memory_buffer)
            .subarray(data_location, data_location + data.len() as u32);

        self.inner
            .buffer_data_with_array_buffer_view(target, &array, usage);
    }

    pub fn compile_shader(&self, source: &str, shader: &Shader) {
        let header = "#version 300 es\nprecision highp float;\nprecision highp int;\nprecision highp sampler2DArray;\n";
        let s: &str = &[header, source].concat();

        self.inner.shader_source(shader, s);
        self.inner.compile_shader(shader);
    }

    pub fn create_program(&self) -> Program {
        self.inner.create_program().unwrap()
    }

    pub fn link_program(&self, program: &Program) -> bool {
        self.inner.link_program(program);
        self.inner
            .get_program_parameter(program, consts::LINK_STATUS)
            .as_bool()
            .unwrap_or(false)
    }

    pub fn bind_vertex_array(&self, array: &VertexArrayObject) {
        self.inner.bind_vertex_array(Some(array));
    }

    pub fn delete_texture(&self, texture: &Texture) {
        self.inner.delete_texture(Some(texture));
    }

    pub fn bind_texture(&self, target: u32, texture: &Texture) {
        self.inner.bind_texture(target, Some(texture));
    }

    pub fn tex_storage_2d(
        &self,
        target: u32,
        level: u32,
        internalformat: u32,
        width: u32,
        height: u32,
    ) {
        self.inner.tex_storage_2d(
            target,
            level as i32,
            internalformat,
            width as i32,
            height as i32,
        );
    }

    pub fn tex_storage_3d(
        &self,
        target: u32,
        level: u32,
        internalformat: u32,
        width: u32,
        height: u32,
        depth: u32,
    ) {
        self.inner.tex_storage_3d(
            target,
            level as i32,
            internalformat,
            width as i32,
            height as i32,
            depth as i32,
        );
    }

    pub fn tex_image_2d(
        &self,
        target: u32,
        level: u32,
        internalformat: u32,
        width: u32,
        height: u32,
        border: u32,
        format: u32,
        data_type: u32,
    ) {
        self.inner
            .tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_u8_array(
                target,
                level as i32,
                internalformat as i32,
                width as i32,
                height as i32,
                border as i32,
                format,
                data_type,
                None,
            )
            .unwrap();
    }

    pub fn tex_sub_image_2d_with_u8_data(
        &self,
        target: u32,
        level: u32,
        x_offset: u32,
        y_offset: u32,
        width: u32,
        height: u32,
        format: u32,
        data_type: u32,
        pixels: &[u8],
    ) {
        self.inner
            .tex_sub_image_2d_with_i32_and_i32_and_u32_and_type_and_opt_u8_array(
                target,
                level as i32,
                x_offset as i32,
                y_offset as i32,
                width as i32,
                height as i32,
                format,
                data_type,
                Some(pixels),
            )
            .unwrap();
    }

    pub fn tex_image_2d_with_u8_data(
        &self,
        target: u32,
        level: u32,
        internalformat: u32,
        width: u32,
        height: u32,
        border: u32,
        format: u32,
        data_type: u32,
        pixels: &[u8],
    ) {
        self.inner
            .tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_u8_array(
                target,
                level as i32,
                internalformat as i32,
                width as i32,
                height as i32,
                border as i32,
                format,
                data_type,
                Some(pixels),
            )
            .unwrap();
    }

    pub fn tex_sub_image_2d_with_f32_data(
        &self,
        target: u32,
        level: u32,
        x_offset: u32,
        y_offset: u32,
        width: u32,
        height: u32,
        format: u32,
        data_type: u32,
        pixels: &[f32],
    ) {
        use wasm_bindgen::JsCast;
        let memory_buffer = wasm_bindgen::memory()
            .dyn_into::<js_sys::WebAssembly::Memory>()
            .unwrap()
            .buffer();
        let data_location = pixels.as_ptr() as u32 / 4;
        let array = js_sys::Float32Array::new(&memory_buffer)
            .subarray(data_location, data_location + pixels.len() as u32);

        self.inner
            .tex_sub_image_2d_with_i32_and_i32_and_u32_and_type_and_opt_array_buffer_view(
                target,
                level as i32,
                x_offset as i32,
                y_offset as i32,
                width as i32,
                height as i32,
                format,
                data_type,
                Some(&array),
            )
            .unwrap();
    }

    pub fn tex_image_2d_with_f32_data(
        &self,
        target: u32,
        level: u32,
        internalformat: u32,
        width: u32,
        height: u32,
        border: u32,
        format: u32,
        data_type: u32,
        pixels: &[f32],
    ) {
        use wasm_bindgen::JsCast;
        let memory_buffer = wasm_bindgen::memory()
            .dyn_into::<js_sys::WebAssembly::Memory>()
            .unwrap()
            .buffer();
        let data_location = pixels.as_ptr() as u32 / 4;
        let array = js_sys::Float32Array::new(&memory_buffer)
            .subarray(data_location, data_location + pixels.len() as u32);

        self.inner
            .tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_array_buffer_view(
                target,
                level as i32,
                internalformat as i32,
                width as i32,
                height as i32,
                border as i32,
                format,
                data_type,
                Some(&array),
            )
            .unwrap();
    }

    pub fn tex_image_3d_with_u16_data(
        &self,
        target: u32,
        level: u32,
        internalformat: u32,
        width: u32,
        height: u32,
        depth: u32,
        border: u32,
        format: u32,
        data_type: u32,
        pixels: &[u16],
    ) {
        use wasm_bindgen::JsCast;
        let memory_buffer = wasm_bindgen::memory()
            .dyn_into::<js_sys::WebAssembly::Memory>()
            .unwrap()
            .buffer();
        let data_location = pixels.as_ptr() as u32 / 2;
        let array = js_sys::Uint16Array::new(&memory_buffer)
            .subarray(data_location, data_location + pixels.len() as u32);

        self.inner
            .tex_image_3d_with_opt_array_buffer_view(
                target,
                level as i32,
                internalformat as i32,
                width as i32,
                height as i32,
                depth as i32,
                border as i32,
                format,
                data_type,
                Some(&array),
            )
            .unwrap();
    }

    pub fn framebuffer_texture_2d(
        &self,
        target: u32,
        attachment: u32,
        textarget: u32,
        texture: &Texture,
        level: u32,
    ) {
        self.inner.framebuffer_texture_2d(
            target,
            attachment,
            textarget,
            Some(texture),
            level as i32,
        );
    }

    pub fn framebuffer_texture_layer(
        &self,
        target: u32,
        attachment: u32,
        texture: &Texture,
        level: u32,
        layer: u32,
    ) {
        self.inner.framebuffer_texture_layer(
            target,
            attachment,
            Some(texture),
            level as i32,
            layer as i32,
        );
    }

    pub fn read_pixels_with_u8_data(
        &self,
        x: u32,
        y: u32,
        width: u32,
        height: u32,
        format: u32,
        data_type: u32,
        dst_data: &mut [u8],
    ) {
        self.inner
            .read_pixels_with_opt_u8_array(
                x as i32,
                y as i32,
                width as i32,
                height as i32,
                format,
                data_type,
                Some(dst_data),
            )
            .unwrap()
    }

    pub fn read_pixels_with_f32_data(
        &self,
        x: u32,
        y: u32,
        width: u32,
        height: u32,
        format: u32,
        data_type: u32,
        dst_data: &mut [f32],
    ) {
        use wasm_bindgen::JsCast;
        let memory_buffer = wasm_bindgen::memory()
            .dyn_into::<js_sys::WebAssembly::Memory>()
            .unwrap()
            .buffer();
        let data_location = dst_data.as_ptr() as u32 / 4;
        let array = js_sys::Float32Array::new(&memory_buffer)
            .subarray(data_location, data_location + dst_data.len() as u32);
        self.inner
            .read_pixels_with_opt_array_buffer_view(
                x as i32,
                y as i32,
                width as i32,
                height as i32,
                format,
                data_type,
                Some(&array),
            )
            .unwrap();
    }

    pub fn viewport(&self, x: i32, y: i32, width: usize, height: usize) {
        self.inner.viewport(x, y, width as i32, height as i32);
    }

    pub fn get_attrib_location(&self, program: &Program, name: &str) -> Option<AttributeLocation> {
        Some(self.inner.get_attrib_location(program, name) as u32)
    }

    pub fn use_program(&self, program: &Program) {
        self.inner.use_program(Some(program));
    }

    pub fn unuse_program(&self) {
        self.inner.use_program(None);
    }

    pub fn delete_program(&self, program: &Program) {
        self.inner.delete_program(Some(program));
    }

    pub fn draw_arrays(&self, mode: u32, first: u32, count: u32) {
        self.inner.draw_arrays(
            mode,
            first as i32, // starting index in the enabled arrays
            count as i32, // number of vertices to be rendered
        );
    }

    pub fn draw_arrays_instanced(&self, mode: u32, first: u32, count: u32, instance_count: u32) {
        self.inner.draw_arrays_instanced(
            mode,
            first as i32, // starting index in the enabled arrays
            count as i32, // number of vertices to be rendered
            instance_count as i32,
        );
    }

    pub fn draw_elements(&self, mode: u32, count: u32, data_type: u32, offset: u32) {
        self.inner
            .draw_elements_with_i32(mode, count as i32, data_type, offset as i32);
    }

    pub fn draw_elements_instanced(
        &self,
        mode: u32,
        count: u32,
        data_type: u32,
        offset: u32,
        instance_count: u32,
    ) {
        self.inner.draw_elements_instanced_with_i32(
            mode,
            count as i32,
            data_type,
            offset as i32,
            instance_count as i32,
        );
    }

    pub fn blit_framebuffer(
        &self,
        src_x0: u32,
        src_y0: u32,
        src_x1: u32,
        src_y1: u32,
        dst_x0: u32,
        dst_y0: u32,
        dst_x1: u32,
        dst_y1: u32,
        mask: u32,
        filter: u32,
    ) {
        self.inner.blit_framebuffer(
            src_x0 as i32,
            src_y0 as i32,
            src_x1 as i32,
            src_y1 as i32,
            dst_x0 as i32,
            dst_y0 as i32,
            dst_x1 as i32,
            dst_y1 as i32,
            mask,
            filter,
        );
    }

    pub fn draw_buffers(&self, draw_buffers: &[u32]) {
        use wasm_bindgen::JsCast;
        let memory_buffer = wasm_bindgen::memory()
            .dyn_into::<js_sys::WebAssembly::Memory>()
            .unwrap()
            .buffer();
        let data_location = draw_buffers.as_ptr() as u32 / 4;
        let array = js_sys::Uint32Array::new(&memory_buffer)
            .subarray(data_location, data_location + draw_buffers.len() as u32);

        self.inner.draw_buffers(&array);
    }

    pub fn check_framebuffer_status(&self) -> Result<(), String> {
        let status = self.inner.check_framebuffer_status(consts::FRAMEBUFFER);

        match status {
            consts::FRAMEBUFFER_COMPLETE => Ok(()),
            consts::FRAMEBUFFER_INCOMPLETE_ATTACHMENT => {
                Err("FRAMEBUFFER_INCOMPLETE_ATTACHMENT".to_string())
            }
            consts::FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT => {
                Err("FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT".to_string())
            }
            consts::FRAMEBUFFER_UNSUPPORTED => Err("FRAMEBUFFER_UNSUPPORTED".to_string()),
            consts::FRAMEBUFFER_INCOMPLETE_MULTISAMPLE => {
                Err("FRAMEBUFFER_INCOMPLETE_MULTISAMPLE".to_string())
            }
            _ => Err("Unknown framebuffer error".to_string()),
        }
    }

    pub fn uniform1f(&self, location: &UniformLocation, data: f32) {
        self.inner.uniform1f(Some(location), data);
    }

    pub fn uniform1i(&self, location: &UniformLocation, data: i32) {
        self.inner.uniform1i(Some(location), data);
    }

    pub fn uniform2fv(&self, location: &UniformLocation, data: &[f32]) {
        self.inner.uniform2fv_with_f32_array(Some(location), data);
    }

    pub fn uniform3fv(&self, location: &UniformLocation, data: &[f32]) {
        self.inner.uniform3fv_with_f32_array(Some(location), data);
    }

    pub fn uniform4fv(&self, location: &UniformLocation, data: &[f32]) {
        self.inner.uniform4fv_with_f32_array(Some(location), data);
    }

    pub fn uniform_matrix2fv(&self, location: &UniformLocation, data: &[f32]) {
        self.inner
            .uniform_matrix2fv_with_f32_array(Some(location), false, data);
    }

    pub fn uniform_matrix3fv(&self, location: &UniformLocation, data: &[f32]) {
        self.inner
            .uniform_matrix3fv_with_f32_array(Some(location), false, data);
    }

    pub fn uniform_matrix4fv(&self, location: &UniformLocation, data: &[f32]) {
        self.inner
            .uniform_matrix4fv_with_f32_array(Some(location), false, data);
    }

    pub fn vertex_attrib_pointer(
        &self,
        location: AttributeLocation,
        size: u32,
        data_type: u32,
        normalized: bool,
        stride: u32,
        offset: u32,
    ) {
        self.inner.vertex_attrib_pointer_with_i32(
            location,
            size as i32,
            data_type,
            normalized,
            byte_size_for_type(data_type, stride) as i32,
            byte_size_for_type(data_type, offset) as i32,
        );
    }

    pub fn get_program_parameter(&self, program: &Program, pname: u32) -> u32 {
        let result = self.inner.get_program_parameter(program, pname);
        result.as_f64().unwrap() as u32
    }

    pub fn get_active_attrib(&self, program: &Program, index: u32) -> ActiveInfo {
        self.inner.get_active_attrib(program, index).unwrap()
    }

    pub fn get_active_uniform(&self, program: &Program, index: u32) -> ActiveInfo {
        self.inner.get_active_uniform(program, index).unwrap()
    }

    pub fn fence_sync(&self) -> Sync {
        self.inner
            .fence_sync(consts::SYNC_GPU_COMMANDS_COMPLETE, 0)
            .unwrap()
    }

    pub fn client_wait_sync(&self, sync: &Sync, flags: u32, timeout: u32) -> u32 {
        self.inner.client_wait_sync_with_u32(sync, flags, timeout)
    }

    pub fn delete_sync(&self, sync: &Sync) {
        self.inner.delete_sync(Some(sync));
    }
}

impl std::ops::Deref for Context {
    type Target = InnerGl;

    fn deref(&self) -> &InnerGl {
        &self.inner
    }
}

pub fn byte_size_for_type(data_type: u32, count: u32) -> u32 {
    match data_type {
        consts::FLOAT => count * std::mem::size_of::<f32>() as u32,
        consts::UNSIGNED_INT => count * std::mem::size_of::<u32>() as u32,
        _ => 0,
    }
}