From 32205e3097d2ae447e545eba3a1210df075f7c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Capucho?= Date: Mon, 3 Jan 2022 22:08:00 +0000 Subject: [PATCH] glsl-out: write array initializers --- src/back/glsl/mod.rs | 34 ++++++++++++++++++++++++++- tests/out/glsl/access.foo.Vertex.glsl | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/back/glsl/mod.rs b/src/back/glsl/mod.rs index eee1b34c2e..09cb1d7f0b 100644 --- a/src/back/glsl/mod.rs +++ b/src/back/glsl/mod.rs @@ -90,6 +90,16 @@ impl crate::StorageClass { _ => false, } } + + /// Whether a variable with this storage class can be initialized + fn initializable(&self) -> bool { + match *self { + crate::StorageClass::WorkGroup + | crate::StorageClass::Uniform + | crate::StorageClass::Storage { .. } => false, + _ => true, + } + } } //Note: similar to `back/spv/helpers.rs` @@ -941,7 +951,7 @@ impl<'a, W: Write> Writer<'a, W> { self.write_array_size(size)?; } - if is_value_init_supported(self.module, global.ty) { + if global.class.initializable() && is_value_init_supported(self.module, global.ty) { write!(self.out, " = ")?; if let Some(init) = global.init { self.write_constant(init)?; @@ -2770,6 +2780,25 @@ impl<'a, W: Write> Writer<'a, W> { self.write_zero_init_scalar(crate::ScalarKind::Float)?; write!(self.out, ")")?; } + TypeInner::Array { base, size, .. } => { + let count = match size + .to_indexable_length(self.module) + .expect("Bad array size") + { + proc::IndexableLength::Known(count) => count, + proc::IndexableLength::Dynamic => return Ok(()), + }; + self.write_type(base)?; + self.write_array_size(size)?; + write!(self.out, "(")?; + for _ in 1..count { + self.write_zero_init_value(base)?; + write!(self.out, ", ")?; + } + // write last parameter without comma and space + self.write_zero_init_value(base)?; + write!(self.out, ")")?; + } _ => {} // TODO: } @@ -3054,6 +3083,9 @@ fn glsl_storage_format(format: crate::StorageFormat) -> &'static str { fn is_value_init_supported(module: &crate::Module, ty: Handle) -> bool { match module.types[ty].inner { TypeInner::Scalar { .. } | TypeInner::Vector { .. } | TypeInner::Matrix { .. } => true, + TypeInner::Array { base, size, .. } => { + size != crate::ArraySize::Dynamic && is_value_init_supported(module, base) + } _ => false, } } diff --git a/tests/out/glsl/access.foo.Vertex.glsl b/tests/out/glsl/access.foo.Vertex.glsl index 9b24f67403..efac041484 100644 --- a/tests/out/glsl/access.foo.Vertex.glsl +++ b/tests/out/glsl/access.foo.Vertex.glsl @@ -20,7 +20,7 @@ float read_from_private(inout float foo_2) { void main() { uint vi = uint(gl_VertexID); float foo_1 = 0.0; - int c[5]; + int c[5] = int[5](0, 0, 0, 0, 0); float baz = foo_1; foo_1 = 1.0; mat4x4 matrix = _group_0_binding_0_vs.matrix;