From e246f90bc33fd489e300366e30354e34a05b8107 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 3 Mar 2024 22:33:53 +0100 Subject: feat(engine): create shader programs when needed --- engine/src/renderer/mod.rs | 66 +++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 36 deletions(-) (limited to 'engine/src/renderer') diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 6c04323..839c0d3 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -83,40 +83,6 @@ where }) } - pub fn create_shader_programs<'program>( - &mut self, - programs: impl IntoIterator, - ) -> Result<(), Error> - { - for program in programs { - let gl_shaders = program - .shaders() - .iter() - .map(|shader| { - let gl_shader = GlShader::new(shader.kind()); - - gl_shader.set_source(shader.source())?; - gl_shader.compile()?; - - Ok::<_, Error>(gl_shader) - }) - .collect::, _>>()?; - - let gl_shader_program = GlShaderProgram::new(); - - for gl_shader in &gl_shaders { - gl_shader_program.attach(gl_shader); - } - - gl_shader_program.link()?; - - self.shader_programs - .insert(program.u64_hash(), gl_shader_program); - } - - Ok(()) - } - pub fn render<'obj>( &mut self, objects: impl IntoIterator, @@ -129,8 +95,8 @@ where for obj in objects { let shader_program = self .shader_programs - .get(&obj.shader().u64_hash()) - .ok_or(Error::MissingShaderProgram)?; + .entry(obj.shader().u64_hash()) + .or_insert_with(|| create_gl_shader_program(obj.shader()).unwrap()); shader_program.activate(|shader_program_curr_bound| { apply_transformation_matrices( @@ -249,6 +215,34 @@ fn create_gl_texture(texture: &Texture) -> GlTexture gl_texture } +fn create_gl_shader_program( + shader_program: &ShaderProgram, +) -> Result +{ + let gl_shaders = shader_program + .shaders() + .iter() + .map(|shader| { + let gl_shader = GlShader::new(shader.kind()); + + gl_shader.set_source(shader.source())?; + gl_shader.compile()?; + + Ok(gl_shader) + }) + .collect::, _>>()?; + + let gl_shader_program = GlShaderProgram::new(); + + for gl_shader in &gl_shaders { + gl_shader_program.attach(gl_shader); + } + + gl_shader_program.link()?; + + Ok(gl_shader_program) +} + #[derive(Debug)] pub struct Renderable { -- cgit v1.2.3-18-g5258