Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLCompiler.h" |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "tests/Test.h" |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 11 | |
| 12 | static void test(skiatest::Reporter* r, const char* src, const SkSL::Program::Settings& settings, |
| 13 | const char* expected, SkSL::Program::Inputs* inputs, |
| 14 | SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { |
| 15 | SkSL::Compiler compiler; |
| 16 | SkSL::String output; |
| 17 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkSL::String(src), |
| 18 | settings); |
| 19 | if (!program) { |
| 20 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 21 | } |
| 22 | REPORTER_ASSERT(r, program); |
| 23 | *inputs = program->fInputs; |
| 24 | REPORTER_ASSERT(r, compiler.toMetal(*program, &output)); |
| 25 | if (program) { |
| 26 | SkSL::String skExpected(expected); |
| 27 | if (output != skExpected) { |
| 28 | SkDebugf("MSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src, |
| 29 | expected, output.c_str()); |
| 30 | } |
| 31 | REPORTER_ASSERT(r, output == skExpected); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps, |
| 36 | const char* expected, SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { |
| 37 | SkSL::Program::Settings settings; |
| 38 | settings.fCaps = ∩︀ |
| 39 | SkSL::Program::Inputs inputs; |
| 40 | test(r, src, settings, expected, &inputs, kind); |
| 41 | } |
| 42 | |
| 43 | DEF_TEST(SkSLMetalHelloWorld, r) { |
| 44 | test(r, |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 45 | "void main() { sk_FragColor = half4(0.75); }", |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 46 | *SkSL::ShaderCapsFactory::Default(), |
| 47 | "#include <metal_stdlib>\n" |
| 48 | "#include <simd/simd.h>\n" |
| 49 | "using namespace metal;\n" |
| 50 | "struct Inputs {\n" |
| 51 | "};\n" |
| 52 | "struct Outputs {\n" |
| 53 | " float4 sk_FragColor [[color(0)]];\n" |
| 54 | "};\n" |
Jim Van Verth | 6bc650e | 2019-02-07 14:53:23 -0500 | [diff] [blame] | 55 | "fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {\n" |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 56 | " Outputs _outputStruct;\n" |
| 57 | " thread Outputs* _out = &_outputStruct;\n" |
| 58 | " _out->sk_FragColor = float4(0.75);\n" |
| 59 | " return *_out;\n" |
| 60 | "}\n"); |
| 61 | } |
| 62 | |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame^] | 63 | DEF_TEST(SkSLMetal2x2MatrixCopyFromFloat2x2, r) { |
| 64 | test(r, R"__SkSL__( |
| 65 | void main() { |
| 66 | float2x2 m1 = float2x2(float2(1, 2), float2(3, 4)); |
| 67 | float2x2 m2 = m1; |
| 68 | float2x2 m3 = float2x2(m1); |
| 69 | sk_FragColor = half4(half(m1[0][0] + m2[0][0] + m3[0][0])); |
| 70 | })__SkSL__", |
| 71 | *SkSL::ShaderCapsFactory::Default(), |
| 72 | R"__MSL__(#include <metal_stdlib> |
| 73 | #include <simd/simd.h> |
| 74 | using namespace metal; |
| 75 | struct Inputs { |
| 76 | }; |
| 77 | struct Outputs { |
| 78 | float4 sk_FragColor [[color(0)]]; |
| 79 | }; |
| 80 | fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { |
| 81 | Outputs _outputStruct; |
| 82 | thread Outputs* _out = &_outputStruct; |
| 83 | _out->sk_FragColor = float4((float2x2(float2(1.0, 2.0), float2(3.0, 4.0))[0][0] + float2x2(float2(1.0, 2.0), float2(3.0, 4.0))[0][0]) + float2x2(float2(1.0, 2.0), float2(3.0, 4.0))[0][0]); |
| 84 | return *_out; |
| 85 | } |
| 86 | )__MSL__"); |
| 87 | } |
| 88 | |
| 89 | DEF_TEST(SkSLMetal2x2MatrixCopyFromConstantPropagatedFloat4, r) { |
| 90 | test(r, R"__SkSL__( |
| 91 | void main() { |
| 92 | float2x2 m1 = float2x2(float4(1, 2, 3, 4)); |
| 93 | float2x2 m2 = m1; |
| 94 | float2x2 m3 = float2x2(m1); |
| 95 | sk_FragColor = half4(half(m1[0][0] + m2[0][0] + m3[0][0])); |
| 96 | })__SkSL__", |
| 97 | *SkSL::ShaderCapsFactory::Default(), |
| 98 | R"__MSL__(#include <metal_stdlib> |
| 99 | #include <simd/simd.h> |
| 100 | using namespace metal; |
| 101 | struct Inputs { |
| 102 | }; |
| 103 | struct Outputs { |
| 104 | float4 sk_FragColor [[color(0)]]; |
| 105 | }; |
| 106 | float2x2 float2x2_from_float4(float4 x0) { |
| 107 | return float2x2(float2(x0[0], x0[1]), float2(x0[2], x0[3])); |
| 108 | } |
| 109 | fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { |
| 110 | Outputs _outputStruct; |
| 111 | thread Outputs* _out = &_outputStruct; |
| 112 | _out->sk_FragColor = float4((float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0] + float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]) + float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]); |
| 113 | return *_out; |
| 114 | } |
| 115 | )__MSL__"); |
| 116 | } |
| 117 | |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 118 | DEF_TEST(SkSLMetalMatrices, r) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame^] | 119 | test(r, R"__SkSL__( |
| 120 | void main() { |
| 121 | float2x2 m1 = float2x2(float4(1, 2, 3, 4)); |
| 122 | float2x2 m2 = float2x2(float4(0)); |
| 123 | float2x2 m3 = float2x2(m1); |
| 124 | float2x2 m4 = float2x2(1); |
| 125 | float2x2 m5 = float2x2(m1[0][0]); |
| 126 | float2x2 m6 = float2x2(1, 2, 3, 4); |
| 127 | float2x2 m7 = float2x2(5, float3(6, 7, 8)); |
| 128 | float3x2 m8 = float3x2(float2(1, 2), 3, float3(4, 5, 6)); |
| 129 | float3x3 m9 = float3x3(1); |
| 130 | float4x4 m10 = float4x4(1); |
| 131 | float4x4 m11 = float4x4(2); |
| 132 | sk_FragColor = half4(half(m1[0][0] + m2[0][0] + m3[0][0] + m4[0][0] + m5[0][0] + |
| 133 | m6[0][0] + m7[0][0] + m8[0][0] + m9[0][0] + m10[0][0] + m11[0][0])); |
| 134 | })__SkSL__", |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 135 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame^] | 136 | R"__MSL__(#include <metal_stdlib> |
| 137 | #include <simd/simd.h> |
| 138 | using namespace metal; |
| 139 | struct Inputs { |
| 140 | }; |
| 141 | struct Outputs { |
| 142 | float4 sk_FragColor [[color(0)]]; |
| 143 | }; |
| 144 | float2x2 float2x2_from_float4(float4 x0) { |
| 145 | return float2x2(float2(x0[0], x0[1]), float2(x0[2], x0[3])); |
| 146 | } |
| 147 | float2x2 float2x2_from_float_float3(float x0, float3 x1) { |
| 148 | return float2x2(float2(x0, x1[0]), float2(x1[1], x1[2])); |
| 149 | } |
| 150 | float3x2 float3x2_from_float2_float_float3(float2 x0, float x1, float3 x2) { |
| 151 | return float3x2(float2(x0[0], x0[1]), float2(x1, x2[0]), float2(x2[1], x2[2])); |
| 152 | } |
| 153 | fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { |
| 154 | Outputs _outputStruct; |
| 155 | thread Outputs* _out = &_outputStruct; |
| 156 | float2x2 m5 = float2x2(float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]); |
| 157 | _out->sk_FragColor = float4((((((((((float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0] + float2x2_from_float4(float4(0.0))[0][0]) + float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]) + float2x2(1.0)[0][0]) + m5[0][0]) + float2x2(float2(1.0, 2.0), float2(3.0, 4.0))[0][0]) + float2x2_from_float_float3(5.0, float3(6.0, 7.0, 8.0))[0][0]) + float3x2_from_float2_float_float3(float2(1.0, 2.0), 3.0, float3(4.0, 5.0, 6.0))[0][0]) + float3x3(1.0)[0][0]) + float4x4(1.0)[0][0]) + float4x4(2.0)[0][0]); |
| 158 | return *_out; |
| 159 | } |
| 160 | )__MSL__"); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 161 | } |
Ethan Nicholas | 5476f2e | 2019-03-07 15:11:31 -0500 | [diff] [blame] | 162 | |
| 163 | DEF_TEST(SkSLMetalConstantSwizzle, r) { |
| 164 | test(r, |
| 165 | "void main() {" |
| 166 | "sk_FragColor = half4(0.5).rgb1;" |
| 167 | "}", |
| 168 | *SkSL::ShaderCapsFactory::Default(), |
| 169 | "#include <metal_stdlib>\n" |
| 170 | "#include <simd/simd.h>\n" |
| 171 | "using namespace metal;\n" |
| 172 | "struct Inputs {\n" |
| 173 | "};\n" |
| 174 | "struct Outputs {\n" |
| 175 | " float4 sk_FragColor [[color(0)]];\n" |
| 176 | "};\n" |
| 177 | "fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {\n" |
| 178 | " Outputs _outputStruct;\n" |
| 179 | " thread Outputs* _out = &_outputStruct;\n" |
| 180 | " _out->sk_FragColor = float4(float4(0.5).xyz, 1);\n" |
| 181 | " return *_out;\n" |
| 182 | "}\n"); |
| 183 | } |