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 | |
John Stiles | 88a4107 | 2020-07-06 09:59:45 -0400 | [diff] [blame^] | 12 | static void test(skiatest::Reporter* r, const SkSL::Program::Settings& settings, const char* src, |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 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 | |
John Stiles | 88a4107 | 2020-07-06 09:59:45 -0400 | [diff] [blame^] | 35 | static void test(skiatest::Reporter* r, const GrShaderCaps& caps, const char* src, |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 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; |
John Stiles | 88a4107 | 2020-07-06 09:59:45 -0400 | [diff] [blame^] | 40 | test(r, settings, src, expected, &inputs, kind); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | DEF_TEST(SkSLMetalHelloWorld, r) { |
John Stiles | 88a4107 | 2020-07-06 09:59:45 -0400 | [diff] [blame^] | 44 | test(r, *SkSL::ShaderCapsFactory::Default(), |
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 | "#include <metal_stdlib>\n" |
| 47 | "#include <simd/simd.h>\n" |
| 48 | "using namespace metal;\n" |
| 49 | "struct Inputs {\n" |
| 50 | "};\n" |
| 51 | "struct Outputs {\n" |
| 52 | " float4 sk_FragColor [[color(0)]];\n" |
| 53 | "};\n" |
Jim Van Verth | 6bc650e | 2019-02-07 14:53:23 -0500 | [diff] [blame] | 54 | "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] | 55 | " Outputs _outputStruct;\n" |
| 56 | " thread Outputs* _out = &_outputStruct;\n" |
| 57 | " _out->sk_FragColor = float4(0.75);\n" |
| 58 | " return *_out;\n" |
| 59 | "}\n"); |
| 60 | } |
| 61 | |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 62 | DEF_TEST(SkSLMetal2x2MatrixCopyFromFloat2x2, r) { |
John Stiles | 88a4107 | 2020-07-06 09:59:45 -0400 | [diff] [blame^] | 63 | test(r, *SkSL::ShaderCapsFactory::Default(), |
| 64 | R"__SkSL__( |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 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__", |
John Stiles | 88a4107 | 2020-07-06 09:59:45 -0400 | [diff] [blame^] | 71 | R"__MSL__(#include <metal_stdlib> |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 72 | #include <simd/simd.h> |
| 73 | using namespace metal; |
| 74 | struct Inputs { |
| 75 | }; |
| 76 | struct Outputs { |
| 77 | float4 sk_FragColor [[color(0)]]; |
| 78 | }; |
| 79 | fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { |
| 80 | Outputs _outputStruct; |
| 81 | thread Outputs* _out = &_outputStruct; |
| 82 | _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]); |
| 83 | return *_out; |
| 84 | } |
| 85 | )__MSL__"); |
| 86 | } |
| 87 | |
| 88 | DEF_TEST(SkSLMetal2x2MatrixCopyFromConstantPropagatedFloat4, r) { |
John Stiles | 88a4107 | 2020-07-06 09:59:45 -0400 | [diff] [blame^] | 89 | test(r, *SkSL::ShaderCapsFactory::Default(), |
| 90 | R"__SkSL__( |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 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__", |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 97 | R"__MSL__(#include <metal_stdlib> |
| 98 | #include <simd/simd.h> |
| 99 | using namespace metal; |
| 100 | struct Inputs { |
| 101 | }; |
| 102 | struct Outputs { |
| 103 | float4 sk_FragColor [[color(0)]]; |
| 104 | }; |
| 105 | float2x2 float2x2_from_float4(float4 x0) { |
| 106 | return float2x2(float2(x0[0], x0[1]), float2(x0[2], x0[3])); |
| 107 | } |
| 108 | fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { |
| 109 | Outputs _outputStruct; |
| 110 | thread Outputs* _out = &_outputStruct; |
| 111 | _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]); |
| 112 | return *_out; |
| 113 | } |
| 114 | )__MSL__"); |
| 115 | } |
| 116 | |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 117 | DEF_TEST(SkSLMetalMatrices, r) { |
John Stiles | 88a4107 | 2020-07-06 09:59:45 -0400 | [diff] [blame^] | 118 | test(r, *SkSL::ShaderCapsFactory::Default(), |
| 119 | R"__SkSL__( |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 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__", |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 135 | R"__MSL__(#include <metal_stdlib> |
| 136 | #include <simd/simd.h> |
| 137 | using namespace metal; |
| 138 | struct Inputs { |
| 139 | }; |
| 140 | struct Outputs { |
| 141 | float4 sk_FragColor [[color(0)]]; |
| 142 | }; |
| 143 | float2x2 float2x2_from_float4(float4 x0) { |
| 144 | return float2x2(float2(x0[0], x0[1]), float2(x0[2], x0[3])); |
| 145 | } |
| 146 | float2x2 float2x2_from_float_float3(float x0, float3 x1) { |
| 147 | return float2x2(float2(x0, x1[0]), float2(x1[1], x1[2])); |
| 148 | } |
| 149 | float3x2 float3x2_from_float2_float_float3(float2 x0, float x1, float3 x2) { |
| 150 | return float3x2(float2(x0[0], x0[1]), float2(x1, x2[0]), float2(x2[1], x2[2])); |
| 151 | } |
| 152 | fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { |
| 153 | Outputs _outputStruct; |
| 154 | thread Outputs* _out = &_outputStruct; |
| 155 | float2x2 m5 = float2x2(float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]); |
| 156 | _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]); |
| 157 | return *_out; |
| 158 | } |
| 159 | )__MSL__"); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 160 | } |
Ethan Nicholas | 5476f2e | 2019-03-07 15:11:31 -0500 | [diff] [blame] | 161 | |
| 162 | DEF_TEST(SkSLMetalConstantSwizzle, r) { |
John Stiles | 88a4107 | 2020-07-06 09:59:45 -0400 | [diff] [blame^] | 163 | test(r, *SkSL::ShaderCapsFactory::Default(), |
Ethan Nicholas | 5476f2e | 2019-03-07 15:11:31 -0500 | [diff] [blame] | 164 | "void main() {" |
| 165 | "sk_FragColor = half4(0.5).rgb1;" |
| 166 | "}", |
Ethan Nicholas | 5476f2e | 2019-03-07 15:11:31 -0500 | [diff] [blame] | 167 | "#include <metal_stdlib>\n" |
| 168 | "#include <simd/simd.h>\n" |
| 169 | "using namespace metal;\n" |
| 170 | "struct Inputs {\n" |
| 171 | "};\n" |
| 172 | "struct Outputs {\n" |
| 173 | " float4 sk_FragColor [[color(0)]];\n" |
| 174 | "};\n" |
| 175 | "fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {\n" |
| 176 | " Outputs _outputStruct;\n" |
| 177 | " thread Outputs* _out = &_outputStruct;\n" |
| 178 | " _out->sk_FragColor = float4(float4(0.5).xyz, 1);\n" |
| 179 | " return *_out;\n" |
| 180 | "}\n"); |
| 181 | } |
John Stiles | 88a4107 | 2020-07-06 09:59:45 -0400 | [diff] [blame^] | 182 | |
| 183 | DEF_TEST(SkSLMetalNumericGlobals, r) { |
| 184 | test(r, *SkSL::ShaderCapsFactory::Default(), |
| 185 | R"__SkSL__( |
| 186 | half attr1; |
| 187 | int attr2 = 123; |
| 188 | float attr3; |
| 189 | half4 attr4 = half4(4, 5, 6, 7); |
| 190 | void main() |
| 191 | { |
| 192 | sk_FragColor = half4(attr1, attr2, half(attr3), attr4.x); |
| 193 | } |
| 194 | )__SkSL__", |
| 195 | R"__MSL__(#include <metal_stdlib> |
| 196 | #include <simd/simd.h> |
| 197 | using namespace metal; |
| 198 | struct Inputs { |
| 199 | }; |
| 200 | struct Outputs { |
| 201 | float4 sk_FragColor [[color(0)]]; |
| 202 | }; |
| 203 | struct Globals { |
| 204 | float attr1; |
| 205 | int attr2; |
| 206 | float attr3; |
| 207 | float4 attr4; |
| 208 | }; |
| 209 | |
| 210 | |
| 211 | |
| 212 | fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { |
| 213 | Globals globalStruct{{}, 123, {}, float4(4.0, 5.0, 6.0, 7.0)}; |
| 214 | thread Globals* _globals = &globalStruct; |
| 215 | (void)_globals; |
| 216 | Outputs _outputStruct; |
| 217 | thread Outputs* _out = &_outputStruct; |
| 218 | _out->sk_FragColor = float4(_globals->attr1, float(_globals->attr2), _globals->attr3, _globals->attr4.x); |
| 219 | return *_out; |
| 220 | } |
| 221 | )__MSL__"); |
| 222 | } |
| 223 | |
| 224 | DEF_TEST(SkSLMetalSamplerGlobals, r) { |
| 225 | test(r, *SkSL::ShaderCapsFactory::Default(), |
| 226 | R"__SkSL__( |
| 227 | layout(binding=1) uniform sampler2D texA; |
| 228 | layout(binding=0) uniform sampler2D texB; |
| 229 | void main() |
| 230 | { |
| 231 | sk_FragColor = sample(texA, half2(0)) * sample(texB, half2(0)); |
| 232 | } |
| 233 | )__SkSL__", |
| 234 | R"__MSL__(#include <metal_stdlib> |
| 235 | #include <simd/simd.h> |
| 236 | using namespace metal; |
| 237 | struct Inputs { |
| 238 | }; |
| 239 | struct Outputs { |
| 240 | float4 sk_FragColor [[color(0)]]; |
| 241 | }; |
| 242 | struct Globals { |
| 243 | texture2d<float> texA; |
| 244 | sampler texASmplr; |
| 245 | texture2d<float> texB; |
| 246 | sampler texBSmplr; |
| 247 | }; |
| 248 | |
| 249 | fragment Outputs fragmentMain(Inputs _in [[stage_in]], texture2d<float> texA[[texture(1)]], sampler texASmplr[[sampler(1)]], texture2d<float> texB[[texture(0)]], sampler texBSmplr[[sampler(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { |
| 250 | Globals globalStruct{texA, texASmplr, texB, texBSmplr}; |
| 251 | thread Globals* _globals = &globalStruct; |
| 252 | (void)_globals; |
| 253 | Outputs _outputStruct; |
| 254 | thread Outputs* _out = &_outputStruct; |
| 255 | _out->sk_FragColor = _globals->texA.sample(_globals->texASmplr, float2(0.0)) * _globals->texB.sample(_globals->texBSmplr, float2(0.0)); |
| 256 | return *_out; |
| 257 | } |
| 258 | )__MSL__"); |
| 259 | } |