Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
[glsl-out] Fix array type writing and zero initialization for local v…
Browse files Browse the repository at this point in the history
…ariables
  • Loading branch information
Gordon-F authored and kvark committed Jul 24, 2021
1 parent 9bd0abb commit 526a29e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 40 deletions.
67 changes: 38 additions & 29 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,16 +825,13 @@ impl<'a, W: Write> Writer<'a, W> {
self.write_array_size(size)?;
}

match self.module.types[global.ty].inner {
TypeInner::Scalar { .. } | TypeInner::Vector { .. } | TypeInner::Matrix { .. } => {
write!(self.out, " = ")?;
if let Some(init) = global.init {
self.write_constant(&self.module.constants[init])?;
} else {
self.write_zero_init_value(&self.module.types[global.ty].inner)?;
}
if is_value_init_supported(&self.module, global.ty) {
write!(self.out, " = ")?;
if let Some(init) = global.init {
self.write_constant(init)?;
} else {
self.write_zero_init_value(global.ty)?;
}
_ => {}
}
writeln!(self.out, ";")?;

Expand Down Expand Up @@ -1096,7 +1093,10 @@ impl<'a, W: Write> Writer<'a, W> {
// Write the local name
// 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)?;
}
// Write the local initializer if needed
if let Some(init) = local.init {
// Put the equal signal only if there's a initializer
Expand All @@ -1105,7 +1105,10 @@ impl<'a, W: Write> Writer<'a, W> {

// Write the constant
// `write_constant` adds no trailing or leading space/newline
self.write_constant(&self.module.constants[init])?;
self.write_constant(init)?;
} else if is_value_init_supported(&self.module, local.ty) {
write!(self.out, " = ")?;
self.write_zero_init_value(local.ty)?;
}

// Finish the local with `;` and add a newline (only for readability)
Expand Down Expand Up @@ -1158,10 +1161,10 @@ impl<'a, W: Write> Writer<'a, W> {
///
/// # Notes
/// Adds no newlines or leading/trailing whitespace
fn write_constant(&mut self, constant: &crate::Constant) -> BackendResult {
fn write_constant(&mut self, handle: Handle<crate::Constant>) -> BackendResult {
use crate::ScalarValue as Sv;

match constant.inner {
match self.module.constants[handle].inner {
crate::ConstantInner::Scalar {
width: _,
ref value,
Expand All @@ -1186,9 +1189,7 @@ impl<'a, W: Write> Writer<'a, W> {
write!(self.out, "(")?;

// Write the comma separated constants
self.write_slice(components, |this, _, arg| {
this.write_constant(&this.module.constants[*arg])
})?;
self.write_slice(components, |this, _, arg| this.write_constant(*arg))?;

write!(self.out, ")")?
}
Expand Down Expand Up @@ -1744,9 +1745,7 @@ impl<'a, W: Write> Writer<'a, W> {
}
}
// Constants are delegated to `write_constant`
Expression::Constant(constant) => {
self.write_constant(&self.module.constants[constant])?
}
Expression::Constant(constant) => self.write_constant(constant)?,
// `Splat` needs to actually write down a vector, it's not always inferred in GLSL.
Expression::Splat { size: _, value } => {
let resolved = ctx.info[expr].ty.inner_with(&self.module.types);
Expand All @@ -1772,6 +1771,11 @@ impl<'a, W: Write> Writer<'a, W> {
Expression::Compose { ty, ref components } => {
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)?;
}

write!(self.out, "(")?;
self.write_slice(components, |this, _, arg| this.write_expr(*arg, ctx))?;
write!(self.out, ")")?
Expand Down Expand Up @@ -1912,7 +1916,7 @@ impl<'a, W: Write> Writer<'a, W> {

if let Some(constant) = offset {
write!(self.out, ", ")?;
self.write_constant(&self.module.constants[constant])?;
self.write_constant(constant)?;
}

// End the function
Expand Down Expand Up @@ -2361,14 +2365,11 @@ impl<'a, W: Write> Writer<'a, W> {
let base_ty_res = &ctx.info[handle].ty;
let resolved = base_ty_res.inner_with(&self.module.types);

// If rhs is a array type, we should write temp variable as a dynamic array
let array_str = if let TypeInner::Array { .. } = *resolved {
"[]"
} else {
""
};

write!(self.out, " {}{} = ", name, array_str)?;
write!(self.out, " {}", name)?;
if let TypeInner::Array { size, .. } = *resolved {
self.write_array_size(size)?;
}
write!(self.out, " = ")?;
self.write_expr(handle, ctx)?;
writeln!(self.out, ";")?;
self.named_expressions.insert(handle, name);
Expand All @@ -2377,7 +2378,8 @@ impl<'a, W: Write> Writer<'a, W> {
}

/// Helper function that write string with default zero initialization for supported types
fn write_zero_init_value(&mut self, inner: &TypeInner) -> BackendResult {
fn write_zero_init_value(&mut self, ty: Handle<crate::Type>) -> BackendResult {
let inner = &self.module.types[ty].inner;
match *inner {
TypeInner::Scalar { kind, .. } => {
self.write_zero_init_scalar(kind)?;
Expand Down Expand Up @@ -2671,3 +2673,10 @@ fn glsl_storage_access(storage_access: crate::StorageAccess) -> Option<&'static
None
}
}

fn is_value_init_supported(module: &crate::Module, ty: Handle<crate::Type>) -> bool {
match module.types[ty].inner {
TypeInner::Scalar { .. } | TypeInner::Vector { .. } | TypeInner::Matrix { .. } => true,
_ => false,
}
}
14 changes: 7 additions & 7 deletions tests/out/glsl/boids.main.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ buffer Particles_block_2Cs {

void main() {
uvec3 global_invocation_id = gl_GlobalInvocationID;
vec2 vPos;
vec2 vVel;
vec2 cMass;
vec2 cVel;
vec2 colVel;
vec2 vPos = vec2(0.0, 0.0);
vec2 vVel = vec2(0.0, 0.0);
vec2 cMass = vec2(0.0, 0.0);
vec2 cVel = vec2(0.0, 0.0);
vec2 colVel = vec2(0.0, 0.0);
int cMassCount = 0;
int cVelCount = 0;
vec2 pos;
vec2 vel;
vec2 pos = vec2(0.0, 0.0);
vec2 vel = vec2(0.0, 0.0);
uint i = 0u;
uint index = global_invocation_id.x;
if ((index >= 1500u)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/out/glsl/quad-vert.main.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ void main() {
vec2 _expr10 = v_uv;
vec4 _expr11 = perVertexStruct.gen_gl_Position;
float _expr12 = perVertexStruct.gen_gl_PointSize;
float _expr13[] = perVertexStruct.gen_gl_ClipDistance;
float _expr14[] = perVertexStruct.gen_gl_CullDistance;
float _expr13[1] = perVertexStruct.gen_gl_ClipDistance;
float _expr14[1] = perVertexStruct.gen_gl_CullDistance;
type10 _tmp_return = type10(_expr10, _expr11, _expr12, _expr13, _expr14);
_vs2fs_location0 = _tmp_return.member;
gl_Position = _tmp_return.gen_gl_Position;
Expand Down
4 changes: 2 additions & 2 deletions tests/out/glsl/skybox.vs_main.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ layout(location = 0) smooth out vec3 _vs2fs_location0;

void main() {
uint vertex_index = uint(gl_VertexID);
int tmp1_;
int tmp2_;
int tmp1_ = 0;
int tmp2_ = 0;
tmp1_ = (int(vertex_index) / 2);
tmp2_ = (int(vertex_index) & 1);
int _expr10 = tmp1_;
Expand Down

0 comments on commit 526a29e

Please sign in to comment.