diff options
author | HampusM <hampus@hampusmat.com> | 2024-03-03 22:33:53 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-03-03 22:33:53 +0100 |
commit | e246f90bc33fd489e300366e30354e34a05b8107 (patch) | |
tree | eff886808f3feff276ca9ca410572b8a8907ccd9 /engine/src/renderer/mod.rs | |
parent | 6f8aeb236725f673f199bce7a6f3942eb56a8318 (diff) |
feat(engine): create shader programs when needed
Diffstat (limited to 'engine/src/renderer/mod.rs')
-rw-r--r-- | engine/src/renderer/mod.rs | 66 |
1 files changed, 30 insertions, 36 deletions
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<Item = &'program ShaderProgram>, - ) -> 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::<Result<Vec<_>, _>>()?; - - 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<Item = &'obj Object>, @@ -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<GlShaderProgram, GlShaderError> +{ + 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::<Result<Vec<_>, _>>()?; + + 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 { |