From 6bb40ce27b41ebd263ef5f68dccd46f7bd253303 Mon Sep 17 00:00:00 2001 From: Pelle Johnsen Date: Tue, 15 Dec 2020 15:24:48 +0100 Subject: [PATCH] [glsl-in] Cast gl_VertexIndex to SInt (#332) * [glsl-in] Cast gl_VertexIndex to SInt * [glsl-in] Use conversion cast (gl_VertexIndex) - Also cast gl_InstanceIndex * [glsl-in] Simplify global var lookup * [glsl-in] Fix missing comma --- src/front/glsl/variables.rs | 154 ++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 77 deletions(-) diff --git a/src/front/glsl/variables.rs b/src/front/glsl/variables.rs index 7fb1a3518e..37335987d6 100644 --- a/src/front/glsl/variables.rs +++ b/src/front/glsl/variables.rs @@ -9,7 +9,12 @@ use super::token::TokenMetadata; impl Program { pub fn lookup_variable(&mut self, name: &str) -> Result>, ErrorKind> { - let mut expression: Option> = None; + if let Some(local_var) = self.context.lookup_local_var(name) { + return Ok(Some(local_var)); + } + if let Some(global_var) = self.context.lookup_global_var_exps.get(name) { + return Ok(Some(*global_var)); + } match name { "gl_Position" => { #[cfg(feature = "glsl-validate")] @@ -19,29 +24,26 @@ impl Program { return Err(ErrorKind::VariableNotAvailable(name.into())); } }; - let h = self - .module - .global_variables - .fetch_or_append(GlobalVariable { - name: Some(name.into()), - class: if self.shader_stage == ShaderStage::Vertex { - StorageClass::Output - } else { - StorageClass::Input + let h = self.module.global_variables.append(GlobalVariable { + name: Some(name.into()), + class: if self.shader_stage == ShaderStage::Vertex { + StorageClass::Output + } else { + StorageClass::Input + }, + binding: Some(Binding::BuiltIn(BuiltIn::Position)), + ty: self.module.types.fetch_or_append(Type { + name: None, + inner: TypeInner::Vector { + size: VectorSize::Quad, + kind: ScalarKind::Float, + width: 4, }, - binding: Some(Binding::BuiltIn(BuiltIn::Position)), - ty: self.module.types.fetch_or_append(Type { - name: None, - inner: TypeInner::Vector { - size: VectorSize::Quad, - kind: ScalarKind::Float, - width: 4, - }, - }), - init: None, - interpolation: None, - storage_access: StorageAccess::empty(), - }); + }), + init: None, + interpolation: None, + storage_access: StorageAccess::empty(), + }); self.lookup_global_variables.insert(name.into(), h); let exp = self .context @@ -49,7 +51,7 @@ impl Program { .append(Expression::GlobalVariable(h)); self.context.lookup_global_var_exps.insert(name.into(), exp); - expression = Some(exp); + Ok(Some(exp)) } "gl_VertexIndex" => { #[cfg(feature = "glsl-validate")] @@ -59,32 +61,36 @@ impl Program { return Err(ErrorKind::VariableNotAvailable(name.into())); } }; - let h = self - .module - .global_variables - .fetch_or_append(GlobalVariable { - name: Some(name.into()), - class: StorageClass::Input, - binding: Some(Binding::BuiltIn(BuiltIn::VertexIndex)), - ty: self.module.types.fetch_or_append(Type { - name: None, - inner: TypeInner::Scalar { - kind: ScalarKind::Uint, - width: 4, - }, - }), - init: None, - interpolation: None, - storage_access: StorageAccess::empty(), - }); + let h = self.module.global_variables.append(GlobalVariable { + name: Some(name.into()), + class: StorageClass::Input, + binding: Some(Binding::BuiltIn(BuiltIn::VertexIndex)), + ty: self.module.types.fetch_or_append(Type { + name: None, + inner: TypeInner::Scalar { + kind: ScalarKind::Uint, + width: 4, + }, + }), + init: None, + interpolation: None, + storage_access: StorageAccess::empty(), + }); self.lookup_global_variables.insert(name.into(), h); - let exp = self + let mut expr = self .context .expressions .append(Expression::GlobalVariable(h)); - self.context.lookup_global_var_exps.insert(name.into(), exp); + expr = self.context.expressions.append(Expression::As { + expr, + kind: ScalarKind::Sint, + convert: true, + }); + self.context + .lookup_global_var_exps + .insert(name.into(), expr); - expression = Some(exp); + Ok(Some(expr)) } "gl_InstanceIndex" => { #[cfg(feature = "glsl-validate")] @@ -94,44 +100,38 @@ impl Program { return Err(ErrorKind::VariableNotAvailable(name.into())); } }; - let h = self - .module - .global_variables - .fetch_or_append(GlobalVariable { - name: Some(name.into()), - class: StorageClass::Input, - binding: Some(Binding::BuiltIn(BuiltIn::InstanceIndex)), - ty: self.module.types.fetch_or_append(Type { - name: None, - inner: TypeInner::Scalar { - kind: ScalarKind::Uint, - width: 4, - }, - }), - init: None, - interpolation: None, - storage_access: StorageAccess::empty(), - }); + let h = self.module.global_variables.append(GlobalVariable { + name: Some(name.into()), + class: StorageClass::Input, + binding: Some(Binding::BuiltIn(BuiltIn::InstanceIndex)), + ty: self.module.types.fetch_or_append(Type { + name: None, + inner: TypeInner::Scalar { + kind: ScalarKind::Uint, + width: 4, + }, + }), + init: None, + interpolation: None, + storage_access: StorageAccess::empty(), + }); self.lookup_global_variables.insert(name.into(), h); - let exp = self + let mut expr = self .context .expressions .append(Expression::GlobalVariable(h)); - self.context.lookup_global_var_exps.insert(name.into(), exp); + expr = self.context.expressions.append(Expression::As { + expr, + kind: ScalarKind::Sint, + convert: true, + }); + self.context + .lookup_global_var_exps + .insert(name.into(), expr); - expression = Some(exp); + Ok(Some(expr)) } - _ => {} - } - - if let Some(expression) = expression { - Ok(Some(expression)) - } else if let Some(local_var) = self.context.lookup_local_var(name) { - Ok(Some(local_var)) - } else if let Some(global_var) = self.context.lookup_global_var_exps.get(name) { - Ok(Some(*global_var)) - } else { - Ok(None) + _ => Ok(None), } }