From 653a60a90fd8ccb2fe6dcbd54b49937ba19d83e3 Mon Sep 17 00:00:00 2001 From: InnocentusLime Date: Fri, 16 May 2025 20:31:31 +0300 Subject: [PATCH 1/2] graphics: add missing uniform error --- src/graphics.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/graphics.rs b/src/graphics.rs index dbdfffc6..1ac936b8 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -284,6 +284,9 @@ impl Display for ShaderType { #[derive(Clone, Debug)] pub enum ShaderError { + MissingUniform { + uniform: String, + }, CompilationError { shader_type: ShaderType, error_message: String, @@ -302,6 +305,7 @@ impl From for ShaderError { impl Display for ShaderError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { + Self::MissingUniform { uniform } => write!(f, "No such uniform:{uniform}"), Self::CompilationError { shader_type, error_message, From 90bfcba666ef6d0c4df0231f7b7501bbe7cb1228 Mon Sep 17 00:00:00 2001 From: InnocentusLime Date: Fri, 16 May 2025 20:47:39 +0300 Subject: [PATCH 2/2] graphics: (gl) error when the uniform is missing --- src/graphics/gl.rs | 120 ++++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 55 deletions(-) diff --git a/src/graphics/gl.rs b/src/graphics/gl.rs index 9c8c9844..c05c9db5 100644 --- a/src/graphics/gl.rs +++ b/src/graphics/gl.rs @@ -472,7 +472,7 @@ pub(crate) struct PipelineInternal { params: PipelineParams, } -type UniformLocation = Option; +type UniformLocation = GLint; pub struct ShaderImage { gl_loc: UniformLocation, @@ -619,21 +619,33 @@ fn load_shader_internal( glUseProgram(program); - #[rustfmt::skip] - let images = meta.images.iter().map(|name| ShaderImage { - gl_loc: get_uniform_location(program, name), - }).collect(); - - #[rustfmt::skip] - let uniforms = meta.uniforms.uniforms.iter().scan(0, |offset, uniform| { - let res = ShaderUniform { - gl_loc: get_uniform_location(program, &uniform.name), - uniform_type: uniform.uniform_type, - array_count: uniform.array_count as _, - }; - *offset += uniform.uniform_type.size() * uniform.array_count; - Some(res) - }).collect(); + let images = meta + .images + .into_iter() + .map(|name| { + Ok(ShaderImage { + gl_loc: get_uniform_location(program, &name) + .ok_or(ShaderError::MissingUniform { uniform: name })?, + }) + }) + .collect::, ShaderError>>()?; + + let uniforms = meta + .uniforms + .uniforms + .into_iter() + .map(|uniform| { + Ok(ShaderUniform { + gl_loc: get_uniform_location(program, &uniform.name).ok_or( + ShaderError::MissingUniform { + uniform: uniform.name, + }, + )?, + uniform_type: uniform.uniform_type, + array_count: uniform.array_count as _, + }) + }) + .collect::, ShaderError>>()?; Ok(ShaderInternal { program, @@ -1456,16 +1468,15 @@ impl RenderingBackend for GlContext { let bindings_image = textures .get(n) .unwrap_or_else(|| panic!("Image count in bindings and shader did not match!")); - if let Some(gl_loc) = shader_image.gl_loc { - let texture = self.textures.get(*bindings_image); - let raw = match texture.raw { - TextureOrRenderbuffer::Texture(id) => id, - TextureOrRenderbuffer::Renderbuffer(id) => id, - }; - unsafe { - self.cache.bind_texture(n, texture.params.kind.into(), raw); - glUniform1i(gl_loc, n as i32); - } + let gl_loc = shader_image.gl_loc; + let texture = self.textures.get(*bindings_image); + let raw = match texture.raw { + TextureOrRenderbuffer::Texture(id) => id, + TextureOrRenderbuffer::Renderbuffer(id) => id, + }; + unsafe { + self.cache.bind_texture(n, texture.params.kind.into(), raw); + glUniform1i(gl_loc, n as i32); } } @@ -1557,36 +1568,35 @@ impl RenderingBackend for GlContext { unsafe { let data = (uniform_ptr as *const f32).add(offset); let data_int = (uniform_ptr as *const i32).add(offset); + let gl_loc = uniform.gl_loc; - if let Some(gl_loc) = uniform.gl_loc { - match uniform.uniform_type { - Float1 => { - glUniform1fv(gl_loc, uniform.array_count, data); - } - Float2 => { - glUniform2fv(gl_loc, uniform.array_count, data); - } - Float3 => { - glUniform3fv(gl_loc, uniform.array_count, data); - } - Float4 => { - glUniform4fv(gl_loc, uniform.array_count, data); - } - Int1 => { - glUniform1iv(gl_loc, uniform.array_count, data_int); - } - Int2 => { - glUniform2iv(gl_loc, uniform.array_count, data_int); - } - Int3 => { - glUniform3iv(gl_loc, uniform.array_count, data_int); - } - Int4 => { - glUniform4iv(gl_loc, uniform.array_count, data_int); - } - Mat4 => { - glUniformMatrix4fv(gl_loc, uniform.array_count, 0, data); - } + match uniform.uniform_type { + Float1 => { + glUniform1fv(gl_loc, uniform.array_count, data); + } + Float2 => { + glUniform2fv(gl_loc, uniform.array_count, data); + } + Float3 => { + glUniform3fv(gl_loc, uniform.array_count, data); + } + Float4 => { + glUniform4fv(gl_loc, uniform.array_count, data); + } + Int1 => { + glUniform1iv(gl_loc, uniform.array_count, data_int); + } + Int2 => { + glUniform2iv(gl_loc, uniform.array_count, data_int); + } + Int3 => { + glUniform3iv(gl_loc, uniform.array_count, data_int); + } + Int4 => { + glUniform4iv(gl_loc, uniform.array_count, data_int); + } + Mat4 => { + glUniformMatrix4fv(gl_loc, uniform.array_count, 0, data); } } }