From cc91c77f7ac7e22b615bf2e188f659ce623c1fe9 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 23 Jul 2021 02:07:42 -0400 Subject: [PATCH] hlsl: fix vector multiplication --- src/back/hlsl/writer.rs | 31 ++++++++++++++++++++++++------- tests/out/hlsl/quad.hlsl | 4 ++-- tests/out/hlsl/shadow.hlsl | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/back/hlsl/writer.rs b/src/back/hlsl/writer.rs index 3ce01ef48c..9aa878c268 100644 --- a/src/back/hlsl/writer.rs +++ b/src/back/hlsl/writer.rs @@ -14,6 +14,15 @@ const LOCATION_SEMANTIC: &str = "LOC"; /// Shorthand result used internally by the backend pub(super) type BackendResult = Result<(), Error>; +impl TypeInner { + fn is_matrix(&self) -> bool { + match *self { + Self::Matrix { .. } => true, + _ => false, + } + } +} + /// Structure contains information required for generating /// wrapped structure of all entry points arguments struct EntryPointBinding { @@ -100,7 +109,7 @@ impl<'a, W: Write> Writer<'a, W> { // Write all structs for (handle, ty) in module.types.iter() { - if let crate::TypeInner::Struct { + if let TypeInner::Struct { top_level, ref members, .. @@ -500,7 +509,11 @@ impl<'a, W: Write> Writer<'a, W> { _ => unreachable!(), } } - crate::ArraySize::Dynamic => write!(self.out, "1")?, + crate::ArraySize::Dynamic => { + //TODO: https://github.com/gfx-rs/naga/issues/1127 + log::warn!("Dynamically sized arrays are not properly supported yet"); + write!(self.out, "1")? + } } write!(self.out, "]")?; @@ -1088,13 +1101,17 @@ impl<'a, W: Write> Writer<'a, W> { write!(self.out, ")")? } } - // Matrix * Vector has to be written as `mul(Matrix, Vector)` + // All of the multiplication can be expressed as `mul`, + // except vector * vector, which needs to use the "*" operator. Expression::Binary { op: crate::BinaryOperator::Multiply, left, right, - } if func_ctx.info[left].ty.inner_with(&module.types) - != func_ctx.info[right].ty.inner_with(&module.types) => + } if func_ctx.info[left].ty.inner_with(&module.types).is_matrix() + || func_ctx.info[right] + .ty + .inner_with(&module.types) + .is_matrix() => { write!(self.out, "mul(")?; self.write_expr(module, left, func_ctx)?; @@ -1267,11 +1284,11 @@ impl<'a, W: Write> Writer<'a, W> { } => { // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-load let ms = match *func_ctx.info[image].ty.inner_with(&module.types) { - crate::TypeInner::Image { + TypeInner::Image { class: crate::ImageClass::Sampled { multi, .. }, .. } - | crate::TypeInner::Image { + | TypeInner::Image { class: crate::ImageClass::Depth { multi }, .. } => multi, diff --git a/tests/out/hlsl/quad.hlsl b/tests/out/hlsl/quad.hlsl index f69f51ad52..90cd392740 100644 --- a/tests/out/hlsl/quad.hlsl +++ b/tests/out/hlsl/quad.hlsl @@ -19,7 +19,7 @@ struct FragmentInput_main { VertexOutput main(VertexInput_main vertexinput_main) { - const VertexOutput vertexoutput1 = { vertexinput_main.uv2, float4(mul(c_scale, vertexinput_main.pos1), 0.0, 1.0) }; + const VertexOutput vertexoutput1 = { vertexinput_main.uv2, float4((c_scale * vertexinput_main.pos1), 0.0, 1.0) }; return vertexoutput1; } @@ -29,7 +29,7 @@ float4 main1(FragmentInput_main fragmentinput_main) : SV_Target0 if ((color.w == 0.0)) { discard; } - float4 premultiplied = mul(color.w, color); + float4 premultiplied = (color.w * color); return premultiplied; } diff --git a/tests/out/hlsl/shadow.hlsl b/tests/out/hlsl/shadow.hlsl index 44c002b8f0..5cd2adc204 100644 --- a/tests/out/hlsl/shadow.hlsl +++ b/tests/out/hlsl/shadow.hlsl @@ -56,7 +56,7 @@ float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0 float3 light_dir = normalize((light.pos.xyz - fragmentinput_fs_main.position1.xyz)); float diffuse = max(0.0, dot(normal, light_dir)); float3 _expr34 = color; - color = (_expr34 + mul((_e25 * diffuse), light.color.xyz)); + color = (_expr34 + ((_e25 * diffuse) * light.color.xyz)); uint _expr40 = i; i = (_expr40 + 1u); }