mod3d_gl/
webgl_log.rs

1//a Log
2use wasm_bindgen::prelude::*;
3use web_sys::{WebGl2RenderingContext, WebGlBuffer, WebGlVertexArrayObject};
4
5#[wasm_bindgen]
6extern "C" {
7    // Use `js_namespace` here to bind `console.log(..)` instead of just
8    // `log(..)`
9    #[wasm_bindgen(js_namespace = console)]
10    pub fn log(s: &str);
11}
12
13#[macro_export]
14macro_rules! console_log {
15    // Note that this is using the `log` function imported above during
16    // `bare_bones`
17    ($($t:tt)*) => (
18        // #[allow(unused_unsafe)]
19        // unsafe {
20            $crate::webgl_log::log(&format_args!($($t)*).to_string())
21        // }
22    )
23}
24
25pub fn log_gl_buffer(
26    context: &WebGl2RenderingContext,
27    gl_buf: Option<&WebGlBuffer>,
28    reason: &str,
29    target: u32,
30    byte_offset: i32,
31    byte_length: usize,
32) {
33    if let Some(gl_buf) = gl_buf {
34        let reason = format!("{}: GlBuf={:?}", reason, gl_buf as *const WebGlBuffer);
35        log_gl_buffer_data(context, gl_buf, &reason, target, byte_offset, byte_length);
36    } else {
37        console_log!("{}: GlBuf=None", reason,);
38    }
39}
40
41pub fn log_gl_vao(
42    _context: &WebGl2RenderingContext,
43    gl_vao: Option<&WebGlVertexArrayObject>,
44    reason: &str,
45) {
46    if let Some(gl_vao) = gl_vao {
47        console_log!(
48            "{}: GlVao={:?}",
49            reason,
50            gl_vao as *const WebGlVertexArrayObject
51        );
52    } else {
53        console_log!("{}: GlVao=None", reason,);
54    }
55}
56
57pub fn log_gl_buffer_data(
58    context: &WebGl2RenderingContext,
59    gl_buf: &WebGlBuffer,
60    reason: &str,
61    target: u32,
62    byte_offset: i32,
63    byte_length: usize,
64) {
65    let data = {
66        if byte_length > 0 {
67            // let mut data = vec![0u8; byte_length];
68            context.bind_buffer(target, Some(gl_buf));
69
70            let mut dst_data = vec![0u8; byte_length];
71            let dst_array = js_sys::Uint8Array::new_with_length(byte_length as u32);
72            context.get_buffer_sub_data_with_i32_and_array_buffer_view(
73                target,
74                byte_offset,
75                &dst_array,
76            );
77            dst_array.copy_to(&mut dst_data);
78            // context.get_buffer_sub_data_with_i32_and_u8_array_and_dst_offset(
79            // target,
80            // byte_offset,
81            // &mut data,
82            // 0,
83            // );
84            context.bind_buffer(target, None);
85            dst_data
86        } else {
87            vec![0; 0]
88        }
89    };
90    console_log!("{}: data={:?}", reason, data);
91}