From 9f0f87a772743ce67e37700516cae0ead17cced9 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 19:25:38 +0200 Subject: Create shaders --- WebInterface/wasm/asm-paint/src/shader.rs | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 WebInterface/wasm/asm-paint/src/shader.rs (limited to 'WebInterface/wasm/asm-paint/src/shader.rs') diff --git a/WebInterface/wasm/asm-paint/src/shader.rs b/WebInterface/wasm/asm-paint/src/shader.rs new file mode 100644 index 0000000..ac7c767 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/shader.rs @@ -0,0 +1,62 @@ +use web_sys::{WebGlProgram, WebGl2RenderingContext}; + +const VERTEX_SHADER: &str = r#"#version 300 es +in vec4 pos; +void main() { + gl_Position = pos; +} +"#; + +const FRAGMENT_SHADER: &str = r#"#version 300 es +precision mediump float; +out vec4 color; + +void main() { + color = vec4(1, 0, 0, 1); +} +"#; + +pub struct Shaders { + program: Option, +} + +impl Shaders { + pub fn new() -> Self { + Self { + program: None, + } + } + + pub fn create_program(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + self.program = Some(ctx.create_program().ok_or("could not create program id")?); + Ok(()) + } + + fn create_shader(&mut self, ctx: &WebGl2RenderingContext, + shader_type: u32, source: &str) -> Result<(), String> { + let program = self.program.as_ref().ok_or("could not find created program")?; + let shader = ctx.create_shader(shader_type) + .ok_or("could not create shader")?; + ctx.shader_source(&shader, source); + ctx.compile_shader(&shader); + let status = ctx.get_shader_parameter(&shader, WebGl2RenderingContext::COMPILE_STATUS); + if status == wasm_bindgen::JsValue::TRUE { + ctx.attach_shader(program, &shader); + Ok(()) + } else { + Err(format!("\n{}", ctx.get_shader_info_log(&shader).unwrap_or_default())) + } + } + + pub fn create_vertex_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + self.create_shader(ctx, WebGl2RenderingContext::VERTEX_SHADER, VERTEX_SHADER) + } + + pub fn create_fragment_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + self.create_shader(ctx, WebGl2RenderingContext::FRAGMENT_SHADER, FRAGMENT_SHADER) + } + + pub fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + Ok(()) + } +} -- cgit v1.2.3-70-g09d2 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(-) (limited to 'WebInterface/wasm/asm-paint/src/shader.rs') 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-70-g09d2 From 3c21251fec1a1ce4cee42337e727cc8dadfd9563 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sat, 21 Dec 2019 20:45:49 +0100 Subject: Draw violet background --- WebInterface/wasm/asm-paint/src/canvas.rs | 16 ++++------------ WebInterface/wasm/asm-paint/src/shader.rs | 30 ++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 16 deletions(-) (limited to 'WebInterface/wasm/asm-paint/src/shader.rs') diff --git a/WebInterface/wasm/asm-paint/src/canvas.rs b/WebInterface/wasm/asm-paint/src/canvas.rs index 54691e8..400e258 100644 --- a/WebInterface/wasm/asm-paint/src/canvas.rs +++ b/WebInterface/wasm/asm-paint/src/canvas.rs @@ -25,18 +25,10 @@ impl Canvas { } pub fn init(&mut self) -> Result<(), ()> { - debug!("create program"); - self.shaders.create_program(&self.ctx) - .map_err(|e| error!("webgl2 create program: {}", e))?; - debug!("create vertex shader"); - self.shaders.create_vertex_shader(&self.ctx) - .map_err(|e| error!("webgl2 create vertex shader: {}", e))?; - debug!("create fragment shader"); - self.shaders.create_fragment_shader(&self.ctx) - .map_err(|e| error!("webgl2 create fragment shader: {}", e))?; - debug!("compile shader program"); - self.shaders.compile(&self.ctx) - .map_err(|e| error!("webgl2 shader: {}", e)) + self.shaders.init(&self.ctx).map_err(|_|())?; + self.ctx.clear_color(1.0, 0.2, 1.0, 1.0); + self.ctx.clear(WebGl2RenderingContext::COLOR_BUFFER_BIT); + Ok(()) } } diff --git a/WebInterface/wasm/asm-paint/src/shader.rs b/WebInterface/wasm/asm-paint/src/shader.rs index 3311b6a..2823cd4 100644 --- a/WebInterface/wasm/asm-paint/src/shader.rs +++ b/WebInterface/wasm/asm-paint/src/shader.rs @@ -18,16 +18,18 @@ void main() { pub struct Shaders { program: Option, + pos_loc: i32, } impl Shaders { pub fn new() -> Self { Self { program: None, + pos_loc: -1 } } - pub fn create_program(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + fn create_program(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { self.program = Some(ctx.create_program().ok_or("could not create program id")?); Ok(()) } @@ -48,15 +50,15 @@ impl Shaders { } } - pub fn create_vertex_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + fn create_vertex_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { self.create_shader(ctx, WebGl2RenderingContext::VERTEX_SHADER, VERTEX_SHADER) } - pub fn create_fragment_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + fn create_fragment_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { self.create_shader(ctx, WebGl2RenderingContext::FRAGMENT_SHADER, FRAGMENT_SHADER) } - pub fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { 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); @@ -67,6 +69,26 @@ impl Shaders { } } + pub fn init(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + debug!("create program"); + self.create_program(ctx) + .map_err(|e| { error!("webgl2 create program: {}", e); e})?; + debug!("create vertex shader"); + self.create_vertex_shader(ctx) + .map_err(|e| { error!("webgl2 create vertex shader: {}", e); e})?; + debug!("create fragment shader"); + self.create_fragment_shader(ctx) + .map_err(|e| { error!("webgl2 create fragment shader: {}", e); e})?; + debug!("compile shader program"); + self.compile(ctx) + .map_err(|e| { error!("webgl2 shader: {}", e); e})?; + let program = self.program.as_ref().ok_or("could not find created program")?; + self.pos_loc = ctx.get_attrib_location(program, "pos"); + trace!("got attrib location 'pos'({})", self.pos_loc); + info!("initialised shader program"); + Ok(()) + } + pub fn remove(&mut self, ctx: &WebGl2RenderingContext) { ctx.delete_program(self.program.as_ref()) } -- cgit v1.2.3-70-g09d2 From a6d2d62fae44b6d8c96e51055f7222bc679efc48 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Wed, 29 May 2019 21:00:49 +0200 Subject: Reforge code snippets --- WebInterface/wasm/asm-paint/index.html | 3 + WebInterface/wasm/asm-paint/src/lib.rs | 119 ------------------------------ WebInterface/wasm/asm-paint/src/shader.rs | 6 +- 3 files changed, 7 insertions(+), 121 deletions(-) (limited to 'WebInterface/wasm/asm-paint/src/shader.rs') diff --git a/WebInterface/wasm/asm-paint/index.html b/WebInterface/wasm/asm-paint/index.html index 5a221e3..c4f3734 100644 --- a/WebInterface/wasm/asm-paint/index.html +++ b/WebInterface/wasm/asm-paint/index.html @@ -13,6 +13,9 @@ width: 100%; height: 100%; } + img { + background: violet; + } diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 945f2b1..6c773c5 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -18,122 +18,3 @@ pub fn entry() { let mut app = app::App::new().unwrap(); app.run(); } - -/* -use js_sys::WebAssembly; -use wasm_bindgen::prelude::*; -use wasm_bindgen::JsCast; -use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; - -#[wasm_bindgen(start)] -pub fn start() -> Result<(), JsValue> { - let document = web_sys::window().unwrap().document().unwrap(); - let canvas = document.get_element_by_id("canvas").unwrap(); - let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::()?; - - let context = canvas - .get_context("webgl")? - .unwrap() - .dyn_into::()?; - - let vert_shader = compile_shader( - &context, - WebGlRenderingContext::VERTEX_SHADER, - r#" - attribute vec4 position; - void main() { - gl_Position = position; - } - "#, - )?; - let frag_shader = compile_shader( - &context, - WebGlRenderingContext::FRAGMENT_SHADER, - r#" - void main() { - gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); - } - "#, - )?; - let program = link_program(&context, &vert_shader, &frag_shader)?; - context.use_program(Some(&program)); - - let vertices: [f32; 9] = [-0.7, -0.7, 0.0, 0.7, -0.7, 0.0, 0.0, 0.7, 0.0]; - let memory_buffer = wasm_bindgen::memory() - .dyn_into::()? - .buffer(); - let vertices_location = vertices.as_ptr() as u32 / 4; - let vert_array = js_sys::Float32Array::new(&memory_buffer) - .subarray(vertices_location, vertices_location + vertices.len() as u32); - - let buffer = context.create_buffer().ok_or("failed to create buffer")?; - context.bind_buffer(WebGlRenderingContext::ARRAY_BUFFER, Some(&buffer)); - context.buffer_data_with_array_buffer_view( - WebGlRenderingContext::ARRAY_BUFFER, - &vert_array, - WebGlRenderingContext::STATIC_DRAW, - ); - context.vertex_attrib_pointer_with_i32(0, 3, WebGlRenderingContext::FLOAT, false, 0, 0); - context.enable_vertex_attrib_array(0); - - context.clear_color(0.0, 0.0, 0.0, 1.0); - context.clear(WebGlRenderingContext::COLOR_BUFFER_BIT); - - context.draw_arrays( - WebGlRenderingContext::TRIANGLES, - 0, - (vertices.len() / 3) as i32, - ); - Ok(()) -} - -pub fn compile_shader( - context: &WebGlRenderingContext, - shader_type: u32, - source: &str, -) -> Result { - let shader = context - .create_shader(shader_type) - .ok_or_else(|| String::from("Unable to create shader object"))?; - context.shader_source(&shader, source); - context.compile_shader(&shader); - - if context - .get_shader_parameter(&shader, WebGlRenderingContext::COMPILE_STATUS) - .as_bool() - .unwrap_or(false) - { - Ok(shader) - } else { - Err(context - .get_shader_info_log(&shader) - .unwrap_or_else(|| String::from("Unknown error creating shader"))) - } -} - -pub fn link_program( - context: &WebGlRenderingContext, - vert_shader: &WebGlShader, - frag_shader: &WebGlShader, -) -> Result { - let program = context - .create_program() - .ok_or_else(|| String::from("Unable to create shader object"))?; - - context.attach_shader(&program, vert_shader); - context.attach_shader(&program, frag_shader); - context.link_program(&program); - - if context - .get_program_parameter(&program, WebGlRenderingContext::LINK_STATUS) - .as_bool() - .unwrap_or(false) - { - Ok(program) - } else { - Err(context - .get_program_info_log(&program) - .unwrap_or_else(|| String::from("Unknown error creating program object"))) - } -} -*/ diff --git a/WebInterface/wasm/asm-paint/src/shader.rs b/WebInterface/wasm/asm-paint/src/shader.rs index 2823cd4..3352bcf 100644 --- a/WebInterface/wasm/asm-paint/src/shader.rs +++ b/WebInterface/wasm/asm-paint/src/shader.rs @@ -1,13 +1,15 @@ use web_sys::{WebGlProgram, WebGl2RenderingContext}; -const VERTEX_SHADER: &str = r#"#version 300 es +const VERTEX_SHADER: &str = +r#"#version 300 es in vec4 pos; void main() { gl_Position = pos; } "#; -const FRAGMENT_SHADER: &str = r#"#version 300 es +const FRAGMENT_SHADER: &str = +r#"#version 300 es precision mediump float; out vec4 color; -- cgit v1.2.3-70-g09d2