diff options
-rw-r--r-- | engine/src/lib.rs | 7 | ||||
-rw-r--r-- | engine/src/renderer/mod.rs | 66 |
2 files changed, 30 insertions, 43 deletions
diff --git a/engine/src/lib.rs b/engine/src/lib.rs index e39f1c6..332a011 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -141,10 +141,6 @@ where /// Will return `Err` if updating the window fails. pub fn start(&mut self, mut func: impl FnMut(&mut Self)) -> Result<(), Error> { - self.renderer - .create_shader_programs(self.objects.values().map(Object::shader)) - .map_err(Error::ShaderCreationFailed)?; - let mut prev_frame_start: Option<Instant> = None; while !self.window.should_close() { @@ -293,9 +289,6 @@ pub enum Error #[error("Rendering failed")] RenderingFailed(#[source] renderer::Error), - - #[error("Failed to create shaders")] - ShaderCreationFailed(#[source] renderer::Error), } #[derive(Debug)] 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 { |