From cbe8b36256603aae5b0709fdde0cda2b75586b8c Mon Sep 17 00:00:00 2001 From: Antonio Maiorano Date: Mon, 29 Aug 2022 20:52:00 +0000 Subject: [PATCH] tint: spir-v reader: add support for GLSLstd450Determinant Bug: tint:1061 Change-Id: I18f6d0726c2fac192b8893f35dcbf45bf4a3d752 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/100466 Reviewed-by: Dan Sinclair Kokoro: Kokoro Commit-Queue: Antonio Maiorano --- src/tint/reader/spirv/function.cc | 9 +- .../spirv/function_glsl_std_450_test.cc | 36 ++++++ test/tint/bug/tint/1061.spvasm | 76 +++++++++++++ .../bug/tint/1061.spvasm.expected.dxc.hlsl | 39 +++++++ .../bug/tint/1061.spvasm.expected.fxc.hlsl | 39 +++++++ test/tint/bug/tint/1061.spvasm.expected.glsl | 43 +++++++ test/tint/bug/tint/1061.spvasm.expected.msl | 48 ++++++++ .../tint/bug/tint/1061.spvasm.expected.spvasm | 105 ++++++++++++++++++ test/tint/bug/tint/1061.spvasm.expected.wgsl | 37 ++++++ 9 files changed, 431 insertions(+), 1 deletion(-) create mode 100644 test/tint/bug/tint/1061.spvasm create mode 100644 test/tint/bug/tint/1061.spvasm.expected.dxc.hlsl create mode 100644 test/tint/bug/tint/1061.spvasm.expected.fxc.hlsl create mode 100644 test/tint/bug/tint/1061.spvasm.expected.glsl create mode 100644 test/tint/bug/tint/1061.spvasm.expected.msl create mode 100644 test/tint/bug/tint/1061.spvasm.expected.spvasm create mode 100644 test/tint/bug/tint/1061.spvasm.expected.wgsl diff --git a/src/tint/reader/spirv/function.cc b/src/tint/reader/spirv/function.cc index a57984b820..fcf149b682 100644 --- a/src/tint/reader/spirv/function.cc +++ b/src/tint/reader/spirv/function.cc @@ -315,6 +315,8 @@ std::string GetGlslStd450FuncName(uint32_t ext_opcode) { return "cross"; case GLSLstd450Degrees: return "degrees"; + case GLSLstd450Determinant: + return "determinant"; case GLSLstd450Distance: return "distance"; case GLSLstd450Exp: @@ -413,7 +415,6 @@ std::string GetGlslStd450FuncName(uint32_t ext_opcode) { case GLSLstd450Acosh: case GLSLstd450Atanh: - case GLSLstd450Determinant: case GLSLstd450MatrixInverse: case GLSLstd450Modf: @@ -3960,6 +3961,12 @@ TypedExpression FunctionEmitter::EmitGlslStd450ExtInst(const spvtools::opt::Inst // Some GLSLstd450 builtins have scalar forms not supported by WGSL. // Emulate them. switch (ext_opcode) { + case GLSLstd450Determinant: { + auto m = MakeOperand(inst, 2); + TINT_ASSERT(Reader, m.type->Is()); + return {ty_.F32(), builder_.Call(Source{}, "determinant", m.expr)}; + } + case GLSLstd450Normalize: // WGSL does not have scalar form of the normalize builtin. // The answer would be 1 anyway, so return that directly. diff --git a/src/tint/reader/spirv/function_glsl_std_450_test.cc b/src/tint/reader/spirv/function_glsl_std_450_test.cc index 4836a6851c..f97f3e97ff 100644 --- a/src/tint/reader/spirv/function_glsl_std_450_test.cc +++ b/src/tint/reader/spirv/function_glsl_std_450_test.cc @@ -52,6 +52,9 @@ std::string Preamble() { OpName %v3f2 "v3f2" OpName %v4f1 "v4f1" OpName %v4f2 "v4f2" + OpName %m2x2f1 "m2x2f1" + OpName %m3x3f1 "m3x3f1" + OpName %m4x4f1 "m4x4f1" %void = OpTypeVoid %voidfn = OpTypeFunction %void @@ -75,6 +78,9 @@ std::string Preamble() { %v2float = OpTypeVector %float 2 %v3float = OpTypeVector %float 3 %v4float = OpTypeVector %float 4 + %mat2v2float = OpTypeMatrix %v2float 2 + %mat3v3float = OpTypeMatrix %v3float 3 + %mat4v4float = OpTypeMatrix %v4float 4 %v2uint_10_20 = OpConstantComposite %v2uint %uint_10 %uint_20 %v2uint_20_10 = OpConstantComposite %v2uint %uint_20 %uint_10 @@ -91,6 +97,10 @@ std::string Preamble() { %v4float_50_50_50_50 = OpConstantComposite %v4float %float_50 %float_50 %float_50 %float_50 + %mat2v2float_50_60 = OpConstantComposite %mat2v2float %v2float_50_60 %v2float_50_60 + %mat3v3float_50_60_70 = OpConstantComposite %mat2v2float %v3float_50_60_70 %v3float_50_60_70 %v3float_50_60_70 + %mat4v4float_50_50_50_50 = OpConstantComposite %mat2v2float %v4float_50_50_50_50 %v4float_50_50_50_50 %v4float_50_50_50_50 %v4float_50_50_50_50 + %100 = OpFunction %void None %voidfn %entry = OpLabel @@ -123,6 +133,10 @@ std::string Preamble() { %v4f1 = OpCopyObject %v4float %v4float_50_50_50_50 %v4f2 = OpCopyObject %v4float %v4f1 + + %m2x2f1 = OpCopyObject %mat2v2float %mat2v2float_50_60 + %m3x3f1 = OpCopyObject %mat3v3float %mat3v3float_50_60_70 + %m4x4f1 = OpCopyObject %mat4v4float %mat4v4float_50_50_50_50 )"; } @@ -1122,5 +1136,27 @@ TEST_F(SpvParserTest, GlslStd450_Ldexp_Vector_Floatvec_Uintvec) { EXPECT_THAT(body, HasSubstr(expected)) << body; } +using GlslStd450_Determinant = SpvParserTestBase<::testing::TestWithParam>; +TEST_P(GlslStd450_Determinant, Test) { + const auto assembly = Preamble() + R"( + %1 = OpExtInst %float %glsl Determinant %)" + + GetParam() + R"( + OpReturn + OpFunctionEnd + )"; + auto p = parser(test::Assemble(assembly)); + ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); + auto fe = p->function_emitter(100); + EXPECT_TRUE(fe.EmitBody()) << p->error(); + auto ast_body = fe.ast_body(); + const auto body = test::ToString(p->program(), ast_body); + std::string expected = "let x_1 : f32 = determinant(" + GetParam() + ");"; + + EXPECT_THAT(body, HasSubstr(expected)) << body; +} +INSTANTIATE_TEST_SUITE_P(Test, + GlslStd450_Determinant, + ::testing::Values("m2x2f1", "m3x3f1", "m4x4f1")); + } // namespace } // namespace tint::reader::spirv diff --git a/test/tint/bug/tint/1061.spvasm b/test/tint/bug/tint/1061.spvasm new file mode 100644 index 0000000000..f4cbcab5b0 --- /dev/null +++ b/test/tint/bug/tint/1061.spvasm @@ -0,0 +1,76 @@ + OpCapability Shader + %1 = OpExtInstImport "GLSL.std.450" + OpMemoryModel Logical GLSL450 + OpEntryPoint Fragment %main "main" %_GLF_color + OpExecutionMode %main OriginUpperLeft + OpSource ESSL 310 + OpName %main "main" + OpName %f "f" + OpName %v "v" + OpName %buf0 "buf0" + OpMemberName %buf0 0 "ref" + OpName %_ "" + OpName %_GLF_color "_GLF_color" + OpMemberDecorate %buf0 0 Offset 0 + OpDecorate %buf0 Block + OpDecorate %_ DescriptorSet 0 + OpDecorate %_ Binding 0 + OpDecorate %_GLF_color Location 0 + %void = OpTypeVoid + %9 = OpTypeFunction %void + %float = OpTypeFloat 32 +%_ptr_Function_float = OpTypePointer Function %float + %v3float = OpTypeVector %float 3 +%mat3v3float = OpTypeMatrix %v3float 3 + %float_1 = OpConstant %float 1 + %float_0 = OpConstant %float 0 + %16 = OpConstantComposite %v3float %float_1 %float_0 %float_0 + %17 = OpConstantComposite %v3float %float_0 %float_1 %float_0 + %18 = OpConstantComposite %v3float %float_0 %float_0 %float_1 + %19 = OpConstantComposite %mat3v3float %16 %17 %18 + %v4float = OpTypeVector %float 4 +%_ptr_Function_v4float = OpTypePointer Function %v4float + %buf0 = OpTypeStruct %v4float +%_ptr_Uniform_buf0 = OpTypePointer Uniform %buf0 + %_ = OpVariable %_ptr_Uniform_buf0 Uniform + %int = OpTypeInt 32 1 + %int_0 = OpConstant %int 0 +%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float +%float_0_100000001 = OpConstant %float 0.100000001 + %bool = OpTypeBool +%_ptr_Output_v4float = OpTypePointer Output %v4float + %_GLF_color = OpVariable %_ptr_Output_v4float Output + %29 = OpConstantComposite %v4float %float_1 %float_0 %float_0 %float_1 + %30 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0 + %main = OpFunction %void None %9 + %31 = OpLabel + %f = OpVariable %_ptr_Function_float Function + %v = OpVariable %_ptr_Function_v4float Function + %32 = OpExtInst %float %1 Determinant %19 + OpStore %f %32 + %33 = OpLoad %float %f + %34 = OpExtInst %float %1 Sin %33 + %35 = OpLoad %float %f + %36 = OpExtInst %float %1 Cos %35 + %37 = OpLoad %float %f + %38 = OpExtInst %float %1 Exp2 %37 + %39 = OpLoad %float %f + %40 = OpExtInst %float %1 Log %39 + %41 = OpCompositeConstruct %v4float %34 %36 %38 %40 + OpStore %v %41 + %42 = OpLoad %v4float %v + %43 = OpAccessChain %_ptr_Uniform_v4float %_ %int_0 + %44 = OpLoad %v4float %43 + %45 = OpExtInst %float %1 Distance %42 %44 + %46 = OpFOrdLessThan %bool %45 %float_0_100000001 + OpSelectionMerge %47 None + OpBranchConditional %46 %48 %49 + %48 = OpLabel + OpStore %_GLF_color %29 + OpBranch %47 + %49 = OpLabel + OpStore %_GLF_color %30 + OpBranch %47 + %47 = OpLabel + OpReturn + OpFunctionEnd diff --git a/test/tint/bug/tint/1061.spvasm.expected.dxc.hlsl b/test/tint/bug/tint/1061.spvasm.expected.dxc.hlsl new file mode 100644 index 0000000000..a7c2d355a8 --- /dev/null +++ b/test/tint/bug/tint/1061.spvasm.expected.dxc.hlsl @@ -0,0 +1,39 @@ +cbuffer cbuffer_x_7 : register(b0, space0) { + uint4 x_7[1]; +}; +static float4 x_GLF_color = float4(0.0f, 0.0f, 0.0f, 0.0f); + +void main_1() { + float f = 0.0f; + float4 v = float4(0.0f, 0.0f, 0.0f, 0.0f); + f = determinant(float3x3(float3(1.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), float3(0.0f, 0.0f, 1.0f))); + v = float4(sin(f), cos(f), exp2(f), log(f)); + const float4 x_42 = v; + const float4 x_44 = asfloat(x_7[0]); + if ((distance(x_42, x_44) < 0.100000001f)) { + x_GLF_color = float4(1.0f, 0.0f, 0.0f, 1.0f); + } else { + x_GLF_color = (0.0f).xxxx; + } + return; +} + +struct main_out { + float4 x_GLF_color_1; +}; +struct tint_symbol { + float4 x_GLF_color_1 : SV_Target0; +}; + +main_out main_inner() { + main_1(); + const main_out tint_symbol_2 = {x_GLF_color}; + return tint_symbol_2; +} + +tint_symbol main() { + const main_out inner_result = main_inner(); + tint_symbol wrapper_result = (tint_symbol)0; + wrapper_result.x_GLF_color_1 = inner_result.x_GLF_color_1; + return wrapper_result; +} diff --git a/test/tint/bug/tint/1061.spvasm.expected.fxc.hlsl b/test/tint/bug/tint/1061.spvasm.expected.fxc.hlsl new file mode 100644 index 0000000000..a7c2d355a8 --- /dev/null +++ b/test/tint/bug/tint/1061.spvasm.expected.fxc.hlsl @@ -0,0 +1,39 @@ +cbuffer cbuffer_x_7 : register(b0, space0) { + uint4 x_7[1]; +}; +static float4 x_GLF_color = float4(0.0f, 0.0f, 0.0f, 0.0f); + +void main_1() { + float f = 0.0f; + float4 v = float4(0.0f, 0.0f, 0.0f, 0.0f); + f = determinant(float3x3(float3(1.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), float3(0.0f, 0.0f, 1.0f))); + v = float4(sin(f), cos(f), exp2(f), log(f)); + const float4 x_42 = v; + const float4 x_44 = asfloat(x_7[0]); + if ((distance(x_42, x_44) < 0.100000001f)) { + x_GLF_color = float4(1.0f, 0.0f, 0.0f, 1.0f); + } else { + x_GLF_color = (0.0f).xxxx; + } + return; +} + +struct main_out { + float4 x_GLF_color_1; +}; +struct tint_symbol { + float4 x_GLF_color_1 : SV_Target0; +}; + +main_out main_inner() { + main_1(); + const main_out tint_symbol_2 = {x_GLF_color}; + return tint_symbol_2; +} + +tint_symbol main() { + const main_out inner_result = main_inner(); + tint_symbol wrapper_result = (tint_symbol)0; + wrapper_result.x_GLF_color_1 = inner_result.x_GLF_color_1; + return wrapper_result; +} diff --git a/test/tint/bug/tint/1061.spvasm.expected.glsl b/test/tint/bug/tint/1061.spvasm.expected.glsl new file mode 100644 index 0000000000..57d9959419 --- /dev/null +++ b/test/tint/bug/tint/1061.spvasm.expected.glsl @@ -0,0 +1,43 @@ +#version 310 es +precision mediump float; + +layout(location = 0) out vec4 x_GLF_color_1_1; +struct buf0 { + vec4 ref; +}; + +layout(binding = 0) uniform buf0_1 { + vec4 ref; +} x_7; + +vec4 x_GLF_color = vec4(0.0f, 0.0f, 0.0f, 0.0f); +void main_1() { + float f = 0.0f; + vec4 v = vec4(0.0f, 0.0f, 0.0f, 0.0f); + f = determinant(mat3(vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f))); + v = vec4(sin(f), cos(f), exp2(f), log(f)); + vec4 x_42 = v; + vec4 x_44 = x_7.ref; + if ((distance(x_42, x_44) < 0.100000001f)) { + x_GLF_color = vec4(1.0f, 0.0f, 0.0f, 1.0f); + } else { + x_GLF_color = vec4(0.0f); + } + return; +} + +struct main_out { + vec4 x_GLF_color_1; +}; + +main_out tint_symbol() { + main_1(); + main_out tint_symbol_1 = main_out(x_GLF_color); + return tint_symbol_1; +} + +void main() { + main_out inner_result = tint_symbol(); + x_GLF_color_1_1 = inner_result.x_GLF_color_1; + return; +} diff --git a/test/tint/bug/tint/1061.spvasm.expected.msl b/test/tint/bug/tint/1061.spvasm.expected.msl new file mode 100644 index 0000000000..3c1cdb11e0 --- /dev/null +++ b/test/tint/bug/tint/1061.spvasm.expected.msl @@ -0,0 +1,48 @@ +#include + +using namespace metal; +struct buf0 { + /* 0x0000 */ float4 ref; +}; + +void main_1(const constant buf0* const tint_symbol_3, thread float4* const tint_symbol_4) { + float f = 0.0f; + float4 v = 0.0f; + f = determinant(float3x3(float3(1.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), float3(0.0f, 0.0f, 1.0f))); + float const x_33 = f; + float const x_35 = f; + float const x_37 = f; + float const x_39 = f; + v = float4(sin(x_33), cos(x_35), exp2(x_37), log(x_39)); + float4 const x_42 = v; + float4 const x_44 = (*(tint_symbol_3)).ref; + if ((distance(x_42, x_44) < 0.100000001f)) { + *(tint_symbol_4) = float4(1.0f, 0.0f, 0.0f, 1.0f); + } else { + *(tint_symbol_4) = float4(0.0f); + } + return; +} + +struct main_out { + float4 x_GLF_color_1; +}; + +struct tint_symbol_1 { + float4 x_GLF_color_1 [[color(0)]]; +}; + +main_out tint_symbol_inner(const constant buf0* const tint_symbol_5, thread float4* const tint_symbol_6) { + main_1(tint_symbol_5, tint_symbol_6); + main_out const tint_symbol_2 = {.x_GLF_color_1=*(tint_symbol_6)}; + return tint_symbol_2; +} + +fragment tint_symbol_1 tint_symbol(const constant buf0* tint_symbol_7 [[buffer(0)]]) { + thread float4 tint_symbol_8 = 0.0f; + main_out const inner_result = tint_symbol_inner(tint_symbol_7, &(tint_symbol_8)); + tint_symbol_1 wrapper_result = {}; + wrapper_result.x_GLF_color_1 = inner_result.x_GLF_color_1; + return wrapper_result; +} + diff --git a/test/tint/bug/tint/1061.spvasm.expected.spvasm b/test/tint/bug/tint/1061.spvasm.expected.spvasm new file mode 100644 index 0000000000..727e9caa30 --- /dev/null +++ b/test/tint/bug/tint/1061.spvasm.expected.spvasm @@ -0,0 +1,105 @@ +; SPIR-V +; Version: 1.3 +; Generator: Google Tint Compiler; 0 +; Bound: 63 +; Schema: 0 + OpCapability Shader + %21 = OpExtInstImport "GLSL.std.450" + OpMemoryModel Logical GLSL450 + OpEntryPoint Fragment %main "main" %x_GLF_color_1_1 + OpExecutionMode %main OriginUpperLeft + OpName %x_GLF_color_1_1 "x_GLF_color_1_1" + OpName %buf0 "buf0" + OpMemberName %buf0 0 "ref" + OpName %x_7 "x_7" + OpName %x_GLF_color "x_GLF_color" + OpName %main_1 "main_1" + OpName %f "f" + OpName %v "v" + OpName %main_out "main_out" + OpMemberName %main_out 0 "x_GLF_color_1" + OpName %main_inner "main_inner" + OpName %main "main" + OpDecorate %x_GLF_color_1_1 Location 0 + OpDecorate %buf0 Block + OpMemberDecorate %buf0 0 Offset 0 + OpDecorate %x_7 NonWritable + OpDecorate %x_7 DescriptorSet 0 + OpDecorate %x_7 Binding 0 + OpMemberDecorate %main_out 0 Offset 0 + %float = OpTypeFloat 32 + %v4float = OpTypeVector %float 4 +%_ptr_Output_v4float = OpTypePointer Output %v4float + %5 = OpConstantNull %v4float +%x_GLF_color_1_1 = OpVariable %_ptr_Output_v4float Output %5 + %buf0 = OpTypeStruct %v4float +%_ptr_Uniform_buf0 = OpTypePointer Uniform %buf0 + %x_7 = OpVariable %_ptr_Uniform_buf0 Uniform +%_ptr_Private_v4float = OpTypePointer Private %v4float +%x_GLF_color = OpVariable %_ptr_Private_v4float Private %5 + %void = OpTypeVoid + %11 = OpTypeFunction %void +%_ptr_Function_float = OpTypePointer Function %float + %17 = OpConstantNull %float +%_ptr_Function_v4float = OpTypePointer Function %v4float + %v3float = OpTypeVector %float 3 +%mat3v3float = OpTypeMatrix %v3float 3 + %float_1 = OpConstant %float 1 + %25 = OpConstantComposite %v3float %float_1 %17 %17 + %26 = OpConstantComposite %v3float %17 %float_1 %17 + %27 = OpConstantComposite %v3float %17 %17 %float_1 + %28 = OpConstantComposite %mat3v3float %25 %26 %27 + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 +%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float +%float_0_100000001 = OpConstant %float 0.100000001 + %bool = OpTypeBool + %51 = OpConstantComposite %v4float %float_1 %17 %17 %float_1 + %main_out = OpTypeStruct %v4float + %52 = OpTypeFunction %main_out + %main_1 = OpFunction %void None %11 + %14 = OpLabel + %f = OpVariable %_ptr_Function_float Function %17 + %v = OpVariable %_ptr_Function_v4float Function %5 + %20 = OpExtInst %float %21 Determinant %28 + OpStore %f %20 + %29 = OpLoad %float %f + %30 = OpLoad %float %f + %31 = OpLoad %float %f + %32 = OpLoad %float %f + %33 = OpExtInst %float %21 Sin %29 + %34 = OpExtInst %float %21 Cos %30 + %35 = OpExtInst %float %21 Exp2 %31 + %36 = OpExtInst %float %21 Log %32 + %37 = OpCompositeConstruct %v4float %33 %34 %35 %36 + OpStore %v %37 + %38 = OpLoad %v4float %v + %42 = OpAccessChain %_ptr_Uniform_v4float %x_7 %uint_0 + %43 = OpLoad %v4float %42 + %44 = OpExtInst %float %21 Distance %38 %43 + %46 = OpFOrdLessThan %bool %44 %float_0_100000001 + OpSelectionMerge %48 None + OpBranchConditional %46 %49 %50 + %49 = OpLabel + OpStore %x_GLF_color %51 + OpBranch %48 + %50 = OpLabel + OpStore %x_GLF_color %5 + OpBranch %48 + %48 = OpLabel + OpReturn + OpFunctionEnd + %main_inner = OpFunction %main_out None %52 + %55 = OpLabel + %56 = OpFunctionCall %void %main_1 + %57 = OpLoad %v4float %x_GLF_color + %58 = OpCompositeConstruct %main_out %57 + OpReturnValue %58 + OpFunctionEnd + %main = OpFunction %void None %11 + %60 = OpLabel + %61 = OpFunctionCall %main_out %main_inner + %62 = OpCompositeExtract %v4float %61 0 + OpStore %x_GLF_color_1_1 %62 + OpReturn + OpFunctionEnd diff --git a/test/tint/bug/tint/1061.spvasm.expected.wgsl b/test/tint/bug/tint/1061.spvasm.expected.wgsl new file mode 100644 index 0000000000..dca0137136 --- /dev/null +++ b/test/tint/bug/tint/1061.spvasm.expected.wgsl @@ -0,0 +1,37 @@ +struct buf0 { + ref : vec4, +} + +@group(0) @binding(0) var x_7 : buf0; + +var x_GLF_color : vec4; + +fn main_1() { + var f : f32; + var v : vec4; + f = determinant(mat3x3(vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f))); + let x_33 : f32 = f; + let x_35 : f32 = f; + let x_37 : f32 = f; + let x_39 : f32 = f; + v = vec4(sin(x_33), cos(x_35), exp2(x_37), log(x_39)); + let x_42 : vec4 = v; + let x_44 : vec4 = x_7.ref; + if ((distance(x_42, x_44) < 0.100000001f)) { + x_GLF_color = vec4(1.0f, 0.0f, 0.0f, 1.0f); + } else { + x_GLF_color = vec4(0.0f, 0.0f, 0.0f, 0.0f); + } + return; +} + +struct main_out { + @location(0) + x_GLF_color_1 : vec4, +} + +@fragment +fn main() -> main_out { + main_1(); + return main_out(x_GLF_color); +}