Skip to content

Commit

Permalink
[glsl-out] support multidimensional arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Apr 11, 2022
1 parent a3cb1c0 commit e1f211b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
41 changes: 25 additions & 16 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,11 @@ impl<'a, W: Write> Writer<'a, W> {
self.collect_reflection_info()
}

fn write_array_size(&mut self, size: crate::ArraySize) -> BackendResult {
fn write_array_size(
&mut self,
base: Handle<crate::Type>,
size: crate::ArraySize,
) -> BackendResult {
write!(self.out, "[")?;

// Write the array size
Expand All @@ -751,6 +755,11 @@ impl<'a, W: Write> Writer<'a, W> {
}

write!(self.out, "]")?;

if let TypeInner::Array { base, size, .. } = self.module.types[base].inner {
self.write_array_size(base, size)?;
}

Ok(())
}

Expand Down Expand Up @@ -807,7 +816,7 @@ impl<'a, W: Write> Writer<'a, W> {
// GLSL arrays are written as `type name[size]`
// Current code is written arrays only as `[size]`
// Base `type` and `name` should be written outside
TypeInner::Array { size, .. } => self.write_array_size(size)?,
TypeInner::Array { base, size, .. } => self.write_array_size(base, size)?,
// Panic if either Image, Sampler, Pointer, or a Struct is being written
//
// Write all variants instead of `_` so that if new variants are added a
Expand Down Expand Up @@ -994,8 +1003,8 @@ impl<'a, W: Write> Writer<'a, W> {
write!(self.out, " ")?;
self.write_global_name(handle, global)?;

if let TypeInner::Array { size, .. } = self.module.types[global.ty].inner {
self.write_array_size(size)?;
if let TypeInner::Array { base, size, .. } = self.module.types[global.ty].inner {
self.write_array_size(base, size)?;
}

if global.space.initializable() && is_value_init_supported(self.module, global.ty) {
Expand Down Expand Up @@ -1299,8 +1308,8 @@ impl<'a, W: Write> Writer<'a, W> {
write!(this.out, " {}", &this.names[&ctx.argument_key(i as u32)])?;

// Write array size
if let TypeInner::Array { size, .. } = this.module.types[arg.ty].inner {
this.write_array_size(size)?;
if let TypeInner::Array { base, size, .. } = this.module.types[arg.ty].inner {
this.write_array_size(base, size)?;
}

Ok(())
Expand Down Expand Up @@ -1361,8 +1370,8 @@ impl<'a, W: Write> Writer<'a, W> {
// The leading space is important
write!(self.out, " {}", self.names[&ctx.name_key(handle)])?;
// Write size for array type
if let TypeInner::Array { size, .. } = self.module.types[local.ty].inner {
self.write_array_size(size)?;
if let TypeInner::Array { base, size, .. } = self.module.types[local.ty].inner {
self.write_array_size(base, size)?;
}
// Write the local initializer if needed
if let Some(init) = local.init {
Expand Down Expand Up @@ -1453,8 +1462,8 @@ impl<'a, W: Write> Writer<'a, W> {
// `type(components)` where `components` is a comma separated list of constants
crate::ConstantInner::Composite { ty, ref components } => {
self.write_type(ty)?;
if let TypeInner::Array { size, .. } = self.module.types[ty].inner {
self.write_array_size(size)?;
if let TypeInner::Array { base, size, .. } = self.module.types[ty].inner {
self.write_array_size(base, size)?;
}
write!(self.out, "(")?;

Expand Down Expand Up @@ -1530,7 +1539,7 @@ impl<'a, W: Write> Writer<'a, W> {
&self.names[&NameKey::StructMember(handle, idx as u32)]
)?;
// Write [size]
self.write_array_size(size)?;
self.write_array_size(base, size)?;
// Newline is important
writeln!(self.out, ";")?;
}
Expand Down Expand Up @@ -2061,8 +2070,8 @@ impl<'a, W: Write> Writer<'a, W> {
self.write_type(ty)?;

let resolved = ctx.info[expr].ty.inner_with(&self.module.types);
if let TypeInner::Array { size, .. } = *resolved {
self.write_array_size(size)?;
if let TypeInner::Array { base, size, .. } = *resolved {
self.write_array_size(base, size)?;
}

write!(self.out, "(")?;
Expand Down Expand Up @@ -2914,8 +2923,8 @@ impl<'a, W: Write> Writer<'a, W> {
let resolved = base_ty_res.inner_with(&self.module.types);

write!(self.out, " {}", name)?;
if let TypeInner::Array { size, .. } = *resolved {
self.write_array_size(size)?;
if let TypeInner::Array { base, size, .. } = *resolved {
self.write_array_size(base, size)?;
}
write!(self.out, " = ")?;
self.write_expr(handle, ctx)?;
Expand Down Expand Up @@ -2953,7 +2962,7 @@ impl<'a, W: Write> Writer<'a, W> {
proc::IndexableLength::Dynamic => return Ok(()),
};
self.write_type(base)?;
self.write_array_size(size)?;
self.write_array_size(base, size)?;
write!(self.out, "(")?;
for _ in 1..count {
self.write_zero_init_value(base)?;
Expand Down
2 changes: 1 addition & 1 deletion tests/out/glsl/access.atomics.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ float read_from_private(inout float foo_1) {
return _e3;
}

float test_arr_as_arg(float a[5]) {
float test_arr_as_arg(float a[5][10]) {
return a[4][9];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/out/glsl/access.foo_frag.Fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ float read_from_private(inout float foo_1) {
return _e3;
}

float test_arr_as_arg(float a[5]) {
float test_arr_as_arg(float a[5][10]) {
return a[4][9];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/out/glsl/access.foo_vert.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ float read_from_private(inout float foo_1) {
return _e3;
}

float test_arr_as_arg(float a[5]) {
float test_arr_as_arg(float a[5][10]) {
return a[4][9];
}

Expand All @@ -94,7 +94,7 @@ void main() {
c = int[5](a_1, int(b), 3, 4, 5);
c[(vi + 1u)] = 42;
int value = c[vi];
float _e42 = test_arr_as_arg(float[5](float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
float _e42 = test_arr_as_arg(float[5][10](float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
unnamed = _e42;
gl_Position = vec4((matrix * vec4(ivec4(value))), 2.0);
gl_Position.yz = vec2(-gl_Position.y, gl_Position.z * 2.0 - gl_Position.w);
Expand Down

0 comments on commit e1f211b

Please sign in to comment.