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

[hlsl-out] Use wrapped constructors when loading from storage address space #1893

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 42 additions & 0 deletions src/back/hlsl/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,48 @@ impl<'a, W: Write> super::Writer<'a, W> {
self.wrapped.image_queries.insert(wiq);
}
}
// Write `WrappedConstructor` for structs that are loaded from `AddressSpace::Storage`
// since they will later be used by the fn `write_storage_load`
crate::Expression::Load { pointer } => {
let pointer_space = func_ctx.info[pointer]
.ty
.inner_with(&module.types)
.pointer_space();

if let Some(crate::AddressSpace::Storage { .. }) = pointer_space {
if let Some(ty) = func_ctx.info[handle].ty.handle() {
write_wrapped_constructor(self, ty, module, func_ctx)?;
}
}

fn write_wrapped_constructor<W: Write>(
writer: &mut super::Writer<'_, W>,
ty: Handle<crate::Type>,
module: &crate::Module,
func_ctx: &FunctionCtx,
) -> BackendResult {
match module.types[ty].inner {
crate::TypeInner::Struct { ref members, .. } => {
for member in members {
write_wrapped_constructor(writer, member.ty, module, func_ctx)?;
}

let constructor = WrappedConstructor { ty };
if !writer.wrapped.constructors.contains(&constructor) {
writer
.write_wrapped_constructor_function(module, constructor)?;
writer.wrapped.constructors.insert(constructor);
}
}
crate::TypeInner::Array { base, .. } => {
write_wrapped_constructor(writer, base, module, func_ctx)?;
}
_ => {}
};

Ok(())
}
}
crate::Expression::Compose { ty, components: _ } => {
let constructor = match module.types[ty].inner {
crate::TypeInner::Struct { .. } | crate::TypeInner::Array { .. } => {
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 @@ -156,12 +156,16 @@ impl<W: fmt::Write> super::Writer<'_, W> {
write!(self.out, "}}")?;
}
crate::TypeInner::Struct { ref members, .. } => {
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 iter = members
.iter()
.map(|m| (TypeResolution::Handle(m.ty), m.offset));
self.write_storage_load_sequence(module, var_handle, iter, func_ctx)?;
write!(self.out, "}}")?;
write!(self.out, ")")?;
}
_ => unreachable!(),
}
Expand Down
9 changes: 8 additions & 1 deletion tests/out/hlsl/globals.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ void test_msl_packed_vec3_as_arg(float3 arg)
return;
}

Foo ConstructFoo(float3 arg0, float arg1) {
Foo ret = (Foo)0;
ret.v3_ = arg0;
ret.v1_ = arg1;
return ret;
}

void test_msl_packed_vec3_()
{
int idx = 1;
Expand All @@ -27,7 +34,7 @@ void test_msl_packed_vec3_()
alignment.Store(0+0, asuint(2.0));
int _expr21 = idx;
alignment.Store(_expr21*4+0, asuint(3.0));
Foo data = {asfloat(alignment.Load3(0)), asfloat(alignment.Load(12))};
Foo data = ConstructFoo(asfloat(alignment.Load3(0)), asfloat(alignment.Load(12)));
float3 unnamed = data.v3_;
float2 unnamed_1 = data.v3_.zx;
test_msl_packed_vec3_as_arg(data.v3_);
Expand Down
10 changes: 9 additions & 1 deletion tests/out/hlsl/shadow.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ VertexOutput_vs_main vs_main(int4 position : LOC0, int4 normal : LOC1)
return vertexoutput_1;
}

Light ConstructLight(float4x4 arg0, float4 arg1, float4 arg2) {
Light ret = (Light)0;
ret.proj = arg0;
ret.pos = arg1;
ret.color = arg2;
return ret;
}

float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
{
VertexOutput in_ = { fragmentinput_fs_main.proj_position_1, fragmentinput_fs_main.world_normal_1, fragmentinput_fs_main.world_position_1 };
Expand All @@ -98,7 +106,7 @@ float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
break;
}
uint _expr23 = i;
Light light = {float4x4(asfloat(s_lights.Load4(_expr23*96+0+0)), asfloat(s_lights.Load4(_expr23*96+0+16)), asfloat(s_lights.Load4(_expr23*96+0+32)), asfloat(s_lights.Load4(_expr23*96+0+48))), asfloat(s_lights.Load4(_expr23*96+64)), asfloat(s_lights.Load4(_expr23*96+80))};
Light light = ConstructLight(float4x4(asfloat(s_lights.Load4(_expr23*96+0+0)), asfloat(s_lights.Load4(_expr23*96+0+16)), asfloat(s_lights.Load4(_expr23*96+0+32)), asfloat(s_lights.Load4(_expr23*96+0+48))), asfloat(s_lights.Load4(_expr23*96+64)), asfloat(s_lights.Load4(_expr23*96+80)));
uint _expr26 = i;
const float _e30 = fetch_shadow(_expr26, mul(in_.world_position, light.proj));
float3 light_dir = normalize((light.pos.xyz - in_.world_position.xyz));
Expand Down