From 5f74e093a4679cdc0fe9fa42822a14fecc7b1cbb Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 20:59:02 +0200 Subject: Compile webgl shader program --- WebInterface/wasm/asm-paint/Cargo.toml | 6 +++--- WebInterface/wasm/asm-paint/src/canvas.rs | 6 ++++++ WebInterface/wasm/asm-paint/src/shader.rs | 13 ++++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index 269d77d..ceb1866 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -14,16 +14,16 @@ crate-type = ["cdylib"] wasm-bindgen = "0.2" log = "0.4" fern = "0.5" -js-sys = "0.3" [dependencies.web-sys] -version = "0.3" +version = "0.3.22" features = [ 'Document', 'Element', 'HtmlCanvasElement', 'WebGl2RenderingContext', - 'WebGlProgram', 'WebGlShader', + 'WebGlProgram', + 'WebGlBuffer', 'Window' ] diff --git a/WebInterface/wasm/asm-paint/src/canvas.rs b/WebInterface/wasm/asm-paint/src/canvas.rs index ddf6d51..54691e8 100644 --- a/WebInterface/wasm/asm-paint/src/canvas.rs +++ b/WebInterface/wasm/asm-paint/src/canvas.rs @@ -39,3 +39,9 @@ impl Canvas { .map_err(|e| error!("webgl2 shader: {}", e)) } } + +impl Drop for Canvas { + fn drop(&mut self) { + self.shaders.remove(&self.ctx); + } +} diff --git a/WebInterface/wasm/asm-paint/src/shader.rs b/WebInterface/wasm/asm-paint/src/shader.rs index ac7c767..3311b6a 100644 --- a/WebInterface/wasm/asm-paint/src/shader.rs +++ b/WebInterface/wasm/asm-paint/src/shader.rs @@ -57,6 +57,17 @@ impl Shaders { } pub fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { - Ok(()) + let program = self.program.as_ref().ok_or("could not find created program")?; + ctx.link_program(program); + let status = ctx.get_program_parameter(program, WebGl2RenderingContext::LINK_STATUS); + if status == wasm_bindgen::JsValue::TRUE { + Ok(()) + } else { + Err(format!("\n{}", ctx.get_program_info_log(program).unwrap_or_default())) + } + } + + pub fn remove(&mut self, ctx: &WebGl2RenderingContext) { + ctx.delete_program(self.program.as_ref()) } } -- cgit v1.2.3-54-g00ecf