summaryrefslogtreecommitdiff
path: root/webhogg/wasm/src/context/shader.rs
diff options
context:
space:
mode:
Diffstat (limited to 'webhogg/wasm/src/context/shader.rs')
-rw-r--r--webhogg/wasm/src/context/shader.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/webhogg/wasm/src/context/shader.rs b/webhogg/wasm/src/context/shader.rs
new file mode 100644
index 0000000..9ccb9fc
--- /dev/null
+++ b/webhogg/wasm/src/context/shader.rs
@@ -0,0 +1,34 @@
+use crate::error::WasmError;
+use super::webgl;
+use super::webgl::{WebGl2, ShaderType};
+
+pub const MAIN_VERTEX_SHADER: &str = include_str!("main.vs");
+pub const MAIN_FRAGMENT_SHADER: &str = include_str!("main.fs");
+
+pub struct ShaderProgram {
+ program: webgl::WebGlProgram,
+}
+
+impl ShaderProgram {
+ pub fn from_sources(gl: &WebGl2, sources: &[(ShaderType, String)]) -> Result<Self, WasmError> {
+ let program = gl.create_program()
+ .map_err(|_| WasmError::Shader(format!("glCreateProgram failed ({})", gl.get_error())))?;
+ for (shader_type, source) in sources {
+ let shader = gl.create_shader(shader_type)
+ .map_err(|_| WasmError::Shader(format!("glCreateShader failed ({})", gl.get_error())))?;
+ gl.shader_source(&shader, source);
+ gl.compile_shader(&shader)
+ .map_err(|e| WasmError::Shader(format!("compile error in {} shader: {}", shader_type, e)))?;
+ gl.attach_shader(&program, &shader)
+ }
+ gl.link_program(&program)
+ .map_err(|e| WasmError::Shader(format!("linker error in program: {}", e)))?;
+ Ok(Self {
+ program
+ })
+ }
+
+ pub fn run(&self, gl: &WebGl2) {
+ gl.use_program(&self.program)
+ }
+}