Skip to content

Commit

Permalink
fix(hlsl): emit constructor functions for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Mar 20, 2023
1 parent 8732b0e commit 35f5996
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/back/hlsl/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,12 @@ impl<'a, W: Write> super::Writer<'a, W> {
}
crate::TypeInner::Array { base, .. } => {
write_wrapped_constructor(writer, base, module)?;
let constructor = WrappedConstructor { ty };
if !writer.wrapped.constructors.contains(&constructor) {
writer
.write_wrapped_constructor_function(module, constructor)?;
writer.wrapped.constructors.insert(constructor);
}
}
_ => {}
};
Expand Down
8 changes: 6 additions & 2 deletions src/back/hlsl/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,16 @@ impl<W: fmt::Write> super::Writer<'_, W> {
size: crate::ArraySize::Constant(const_handle),
..
} => {
write!(self.out, "{{")?;
let constructor = super::help::WrappedConstructor {
ty: result_ty.handle().unwrap(),
};
self.write_wrapped_constructor_function_name(module, constructor)?;
write!(self.out, "(")?;
let count = module.constants[const_handle].to_array_length().unwrap();
let stride = module.types[base].inner.size(&module.constants);
let iter = (0..count).map(|i| (TypeResolution::Handle(base), stride * i));
self.write_storage_load_sequence(module, var_handle, iter, func_ctx)?;
write!(self.out, "}}")?;
write!(self.out, ")")?;
}
crate::TypeInner::Struct { ref members, .. } => {
let constructor = super::help::WrappedConstructor {
Expand Down
14 changes: 7 additions & 7 deletions tests/out/hlsl/access.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ void assign_array_through_ptr_fn(inout float4 foo_2[2])
return;
}

typedef uint2 ret_Constructarray2_uint2_[2];
ret_Constructarray2_uint2_ Constructarray2_uint2_(uint2 arg0, uint2 arg1) {
uint2 ret[2] = { arg0, arg1 };
return ret;
}

uint NagaBufferLengthRW(RWByteAddressBuffer buffer)
{
uint ret;
Expand All @@ -273,7 +279,7 @@ float4 foo_vert(uint vi : SV_VertexID) : SV_Position
test_matrix_within_struct_accesses();
test_matrix_within_array_within_struct_accesses();
float4x3 _matrix = float4x3(asfloat(bar.Load3(0+0)), asfloat(bar.Load3(0+16)), asfloat(bar.Load3(0+32)), asfloat(bar.Load3(0+48)));
uint2 arr_1[2] = {asuint(bar.Load2(144+0)), asuint(bar.Load2(144+8))};
uint2 arr_1[2] = Constructarray2_uint2_(asuint(bar.Load2(144+0)), asuint(bar.Load2(144+8)));
float b = asfloat(bar.Load(0+48+0));
int a_1 = asint(bar.Load(0+(((NagaBufferLengthRW(bar) - 160) / 8) - 2u)*8+160));
int2 c = asint(qux.Load2(0));
Expand All @@ -285,12 +291,6 @@ float4 foo_vert(uint vi : SV_VertexID) : SV_Position
return float4(mul(float4((value).xxxx), _matrix), 2.0);
}

typedef uint2 ret_Constructarray2_uint2_[2];
ret_Constructarray2_uint2_ Constructarray2_uint2_(uint2 arg0, uint2 arg1) {
uint2 ret[2] = { arg0, arg1 };
return ret;
}

float4 foo_frag() : SV_Target0
{
bar.Store(8+16+0, asuint(1.0));
Expand Down

0 comments on commit 35f5996

Please sign in to comment.