Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[glsl-out] Fix initializers not being written #1644

Merged
merged 4 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -1343,6 +1353,9 @@ 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)?;
}
write!(self.out, "(")?;

// Write the comma separated constants
Expand Down Expand Up @@ -2767,6 +2780,36 @@ 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, ")")?;
}
TypeInner::Struct { ref members, .. } => {
let name = &self.names[&NameKey::Type(ty)];
write!(self.out, "{}(", name)?;
for (i, member) in members.iter().enumerate() {
self.write_zero_init_value(member.ty)?;
if i != members.len().saturating_sub(1) {
write!(self.out, ", ")?;
}
}
write!(self.out, ")")?;
}
_ => {} // TODO:
}

Expand Down Expand Up @@ -3051,6 +3094,12 @@ fn glsl_storage_format(format: crate::StorageFormat) -> &'static str {
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,
TypeInner::Array { base, size, .. } => {
size != crate::ArraySize::Dynamic && is_value_init_supported(module, base)
}
TypeInner::Struct { ref members, .. } => members
.iter()
.all(|member| is_value_init_supported(module, member.ty)),
_ => false,
}
}
2 changes: 1 addition & 1 deletion tests/out/glsl/access.foo.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/out/glsl/interpolate.vert_main.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ smooth centroid out float _vs2fs_location5;
smooth sample out float _vs2fs_location6;

void main() {
FragmentInput out_;
FragmentInput out_ = FragmentInput(vec4(0.0, 0.0, 0.0, 0.0), 0u, 0.0, vec2(0.0, 0.0), vec3(0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), 0.0, 0.0);
out_.position = vec4(2.0, 4.0, 5.0, 6.0);
out_.flat_ = 8u;
out_.linear = 27.0;
Expand Down
2 changes: 1 addition & 1 deletion tests/out/glsl/operators.main.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ vec3 bool_cast(vec3 x) {
}

float constructors() {
Foo foo;
Foo foo = Foo(vec4(0.0, 0.0, 0.0, 0.0), 0);
JCapucho marked this conversation as resolved.
Show resolved Hide resolved
foo = Foo(vec4(1.0), 1);
mat2x2 mat2comp = mat2x2(vec2(1.0, 0.0), vec2(0.0, 1.0));
mat4x4 mat4comp = mat4x4(vec4(1.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
Expand Down
2 changes: 1 addition & 1 deletion tests/out/glsl/quad-vert.main.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ vec2 v_uv = vec2(0.0, 0.0);

vec2 a_uv_1 = vec2(0.0, 0.0);

gen_gl_PerVertex perVertexStruct;
gen_gl_PerVertex perVertexStruct = gen_gl_PerVertex(vec4(0.0, 0.0, 0.0, 1.0), 1.0, float[1](0.0), float[1](0.0));

vec2 a_pos_1 = vec2(0.0, 0.0);

Expand Down