From f408310ab7f7140d591ccb06fae898cf0e17cf45 Mon Sep 17 00:00:00 2001 From: David Jozis Date: Sun, 14 Oct 2018 12:38:54 -0400 Subject: [PATCH] Removed buffer copy from webgl example --- examples/webgl/src/lib.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/examples/webgl/src/lib.rs b/examples/webgl/src/lib.rs index 69c635f59..efb4bb5cd 100644 --- a/examples/webgl/src/lib.rs +++ b/examples/webgl/src/lib.rs @@ -6,6 +6,12 @@ use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; +#[wasm_bindgen] +extern { + #[wasm_bindgen(js_namespace = Reflect)] + fn get(obj: &JsValue, member: &str) -> JsValue; +} + #[wasm_bindgen] pub fn draw() { let document = web_sys::window().unwrap().document().unwrap(); @@ -44,17 +50,17 @@ pub fn draw() { let program = link_program(&context, [vert_shader, frag_shader].iter()).unwrap(); context.use_program(Some(&program)); - let vertices = [-0.7, -0.7, 0.0, 0.7, -0.7, 0.0, 0.0, 0.7, 0.0]; - let vert_array = js_sys::Float32Array::new(&wasm_bindgen::JsValue::from(vertices.len() as u32)); - for (i, f) in vertices.iter().enumerate() { - vert_array.fill(*f, i as u32, (i + 1) as u32); - } + let vertices: [f32; 9] = [-0.7, -0.7, 0.0, 0.7, -0.7, 0.0, 0.0, 0.7, 0.0]; + let vert_array = js_sys::Float32Array::new(&get(&wasm_bindgen::memory(), "buffer")).subarray( + &vertices as *const f32 as u32 / 4, + &vertices as *const f32 as u32 / 4 + vertices.len() as u32, + ); let buffer = context.create_buffer().unwrap(); context.bind_buffer(WebGlRenderingContext::ARRAY_BUFFER, Some(&buffer)); - context.buffer_data_with_opt_array_buffer( + context.buffer_data_with_array_buffer_view( WebGlRenderingContext::ARRAY_BUFFER, - Some(&vert_array.buffer()), + vert_array.as_ref(), WebGlRenderingContext::STATIC_DRAW, ); context.vertex_attrib_pointer_with_i32(0, 3, WebGlRenderingContext::FLOAT, false, 0, 0);