ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 8 | #include "SkSLCompiler.h" |
| 9 | |
| 10 | #include "Test.h" |
| 11 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 13 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame^] | 14 | static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps, |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 15 | const char* expected) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 16 | SkSL::Compiler compiler; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 17 | SkString output; |
| 18 | bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, SkString(src), caps, &output); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 19 | if (!result) { |
| 20 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 21 | } |
| 22 | REPORTER_ASSERT(r, result); |
| 23 | if (result) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 24 | SkString skExpected(expected); |
| 25 | if (output != skExpected) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 26 | SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src, |
| 27 | expected, output.c_str()); |
| 28 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 29 | REPORTER_ASSERT(r, output == skExpected); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | DEF_TEST(SkSLHelloWorld, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 34 | test(r, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 35 | "void main() { sk_FragColor = vec4(0.75); }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 36 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 37 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 38 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 39 | "void main() {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 40 | " sk_FragColor = vec4(0.75);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 41 | "}\n"); |
| 42 | } |
| 43 | |
| 44 | DEF_TEST(SkSLControl, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 45 | test(r, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 46 | "void main() {" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 47 | "if (sqrt(2) > 5) { sk_FragColor = vec4(0.75); } else { discard; }" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 48 | "int i = 0;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 49 | "while (i < 10) sk_FragColor *= 0.5;" |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 50 | "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.75);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 51 | "for (int i = 0; i < 10; i++) {" |
| 52 | "if (i % 0 == 1) break; else continue;" |
| 53 | "}" |
| 54 | "return;" |
| 55 | "}", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 56 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 57 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 58 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 59 | "void main() {\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 60 | " if (sqrt(2.0) > 5.0) {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 61 | " sk_FragColor = vec4(0.75);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 62 | " } else {\n" |
| 63 | " discard;\n" |
| 64 | " }\n" |
| 65 | " int i = 0;\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 66 | " while (i < 10) sk_FragColor *= 0.5;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 67 | " do {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 68 | " sk_FragColor += 0.01;\n" |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 69 | " } while (sk_FragColor.x < 0.75);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 70 | " for (int i = 0;i < 10; i++) {\n" |
| 71 | " if (i % 0 == 1) break; else continue;\n" |
| 72 | " }\n" |
| 73 | " return;\n" |
| 74 | "}\n"); |
| 75 | } |
| 76 | |
| 77 | DEF_TEST(SkSLFunctions, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 78 | test(r, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 79 | "float foo(float v[2]) { return v[0] * v[1]; }" |
| 80 | "void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 81 | "void main() { float x = 10; bar(x); sk_FragColor = vec4(x); }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 82 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 83 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 84 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 85 | "float foo(in float v[2]) {\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 86 | " return v[0] * v[1];\n" |
| 87 | "}\n" |
| 88 | "void bar(inout float x) {\n" |
| 89 | " float y[2], z;\n" |
| 90 | " y[0] = x;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 91 | " y[1] = x * 2.0;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 92 | " z = foo(y);\n" |
| 93 | " x = z;\n" |
| 94 | "}\n" |
| 95 | "void main() {\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 96 | " float x = 10.0;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 97 | " bar(x);\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 98 | " sk_FragColor = vec4(x);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 99 | "}\n"); |
| 100 | } |
| 101 | |
| 102 | DEF_TEST(SkSLOperators, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 103 | test(r, |
| 104 | "void main() {" |
| 105 | "float x = 1, y = 2;" |
| 106 | "int z = 3;" |
| 107 | "x = x + y * z * x * (y - z);" |
| 108 | "y = x / y / z;" |
| 109 | "z = (z / 2 % 3 << 4) >> 2 << 1;" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 110 | "bool b = (x > 4) == x < 2 || 2 >= sqrt(2) && y <= z;" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 111 | "x += 12;" |
| 112 | "x -= 12;" |
| 113 | "x *= y /= z = 10;" |
| 114 | "b ||= false;" |
| 115 | "b &&= true;" |
| 116 | "b ^^= false;" |
| 117 | "z |= 0;" |
| 118 | "z &= -1;" |
| 119 | "z ^= 0;" |
| 120 | "z >>= 2;" |
| 121 | "z <<= 4;" |
| 122 | "z %= 5;" |
| 123 | "}", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 124 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 125 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 126 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 127 | "void main() {\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 128 | " float x = 1.0, y = 2.0;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 129 | " int z = 3;\n" |
| 130 | " x = x + ((y * float(z)) * x) * (y - float(z));\n" |
| 131 | " y = (x / y) / float(z);\n" |
| 132 | " z = (((z / 2) % 3 << 4) >> 2) << 1;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 133 | " bool b = x > 4.0 == x < 2.0 || 2.0 >= sqrt(2.0) && y <= float(z);\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 134 | " x += 12.0;\n" |
| 135 | " x -= 12.0;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 136 | " x *= (y /= float(z = 10));\n" |
| 137 | " b ||= false;\n" |
| 138 | " b &&= true;\n" |
| 139 | " b ^^= false;\n" |
| 140 | " z |= 0;\n" |
| 141 | " z &= -1;\n" |
| 142 | " z ^= 0;\n" |
| 143 | " z >>= 2;\n" |
| 144 | " z <<= 4;\n" |
| 145 | " z %= 5;\n" |
| 146 | "}\n"); |
| 147 | } |
| 148 | |
| 149 | DEF_TEST(SkSLMatrices, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 150 | test(r, |
| 151 | "void main() {" |
| 152 | "mat2x4 x = mat2x4(1);" |
| 153 | "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));" |
| 154 | "mat3x4 z = x * y;" |
| 155 | "vec3 v1 = mat3(1) * vec3(1);" |
| 156 | "vec3 v2 = vec3(1) * mat3(1);" |
| 157 | "}", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 158 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 159 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 160 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 161 | "void main() {\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 162 | " mat2x4 x = mat2x4(1.0);\n" |
| 163 | " mat3x2 y = mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 164 | " mat3x4 z = x * y;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 165 | " vec3 v1 = mat3(1.0) * vec3(1.0);\n" |
| 166 | " vec3 v2 = vec3(1.0) * mat3(1.0);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 167 | "}\n"); |
| 168 | } |
| 169 | |
| 170 | DEF_TEST(SkSLInterfaceBlock, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 171 | test(r, |
| 172 | "uniform testBlock {" |
| 173 | "float x;" |
| 174 | "float y[2];" |
| 175 | "layout(binding=12) mat3x2 z;" |
| 176 | "bool w;" |
| 177 | "};" |
| 178 | "void main() {" |
| 179 | "}", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 180 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 181 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 182 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 183 | "uniform testBlock {\n" |
| 184 | " float x;\n" |
| 185 | " float[2] y;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 186 | " layout (binding = 12) mat3x2 z;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 187 | " bool w;\n" |
| 188 | "};\n" |
| 189 | "void main() {\n" |
| 190 | "}\n"); |
| 191 | } |
| 192 | |
| 193 | DEF_TEST(SkSLStructs, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 194 | test(r, |
| 195 | "struct A {" |
| 196 | "int x;" |
| 197 | "int y;" |
| 198 | "} a1, a2;" |
| 199 | "A a3;" |
| 200 | "struct B {" |
| 201 | "float x;" |
| 202 | "float y[2];" |
| 203 | "layout(binding=1) A z;" |
| 204 | "};" |
| 205 | "B b1, b2, b3;" |
| 206 | "void main() {" |
| 207 | "}", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 208 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 209 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 210 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 211 | "struct A {\n" |
| 212 | " int x;\n" |
| 213 | " int y;\n" |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 214 | "} a1, a2;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 215 | "A a3;\n" |
| 216 | "struct B {\n" |
| 217 | " float x;\n" |
| 218 | " float[2] y;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 219 | " layout (binding = 1) A z;\n" |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 220 | "} b1, b2, b3;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 221 | "void main() {\n" |
| 222 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 223 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 224 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 225 | DEF_TEST(SkSLVersion, r) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 226 | test(r, |
| 227 | "in float test; void main() { sk_FragColor = vec4(0.75); }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 228 | *SkSL::GLSLCapsFactory::Version450Core(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 229 | "#version 450 core\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 230 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 231 | "in float test;\n" |
| 232 | "void main() {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 233 | " sk_FragColor = vec4(0.75);\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 234 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 235 | test(r, |
| 236 | "in float test; void main() { sk_FragColor = vec4(0.75); }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 237 | *SkSL::GLSLCapsFactory::Version110(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 238 | "#version 110\n" |
| 239 | "varying float test;\n" |
| 240 | "void main() {\n" |
| 241 | " gl_FragColor = vec4(0.75);\n" |
| 242 | "}\n"); |
| 243 | } |
| 244 | |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 245 | DEF_TEST(SkSLUsesPrecisionModifiers, r) { |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 246 | test(r, |
| 247 | "void main() { float x = 0.75; highp float y = 1; }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 248 | *SkSL::GLSLCapsFactory::Default(), |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 249 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 250 | "out vec4 sk_FragColor;\n" |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 251 | "void main() {\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 252 | " float x = 0.75;\n" |
| 253 | " float y = 1.0;\n" |
| 254 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 255 | test(r, |
| 256 | "void main() { float x = 0.75; highp float y = 1; }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 257 | *SkSL::GLSLCapsFactory::UsesPrecisionModifiers(), |
| 258 | "#version 400\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 259 | "precision highp float;\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 260 | "out mediump vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 261 | "void main() {\n" |
| 262 | " float x = 0.75;\n" |
| 263 | " highp float y = 1.0;\n" |
| 264 | "}\n"); |
| 265 | } |
| 266 | |
| 267 | DEF_TEST(SkSLMinAbs, r) { |
| 268 | test(r, |
| 269 | "void main() {" |
| 270 | "float x = -5;" |
| 271 | "x = min(abs(x), 6);" |
| 272 | "}", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 273 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 274 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 275 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 276 | "void main() {\n" |
| 277 | " float x = -5.0;\n" |
| 278 | " x = min(abs(x), 6.0);\n" |
| 279 | "}\n"); |
| 280 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 281 | test(r, |
| 282 | "void main() {" |
| 283 | "float x = -5.0;" |
| 284 | "x = min(abs(x), 6.0);" |
| 285 | "}", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 286 | *SkSL::GLSLCapsFactory::CannotUseMinAndAbsTogether(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 287 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 288 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 289 | "void main() {\n" |
| 290 | " float minAbsHackVar0;\n" |
| 291 | " float minAbsHackVar1;\n" |
| 292 | " float x = -5.0;\n" |
| 293 | " x = ((minAbsHackVar0 = abs(x)) < (minAbsHackVar1 = 6.0) ? minAbsHackVar0 : " |
| 294 | "minAbsHackVar1);\n" |
| 295 | "}\n"); |
| 296 | } |
| 297 | |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 298 | DEF_TEST(SkSLNegatedAtan, r) { |
| 299 | test(r, |
| 300 | "void main() { vec2 x = vec2(1, 2); float y = atan(x.x, -(2 * x.y)); }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 301 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 302 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 303 | "out vec4 sk_FragColor;\n" |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 304 | "void main() {\n" |
| 305 | " vec2 x = vec2(1.0, 2.0);\n" |
| 306 | " float y = atan(x.x, -(2.0 * x.y));\n" |
| 307 | "}\n"); |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 308 | test(r, |
| 309 | "void main() { vec2 x = vec2(1, 2); float y = atan(x.x, -(2 * x.y)); }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 310 | *SkSL::GLSLCapsFactory::MustForceNegatedAtanParamToFloat(), |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 311 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 312 | "out vec4 sk_FragColor;\n" |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 313 | "void main() {\n" |
| 314 | " vec2 x = vec2(1.0, 2.0);\n" |
| 315 | " float y = atan(x.x, -1.0 * (2.0 * x.y));\n" |
| 316 | "}\n"); |
| 317 | } |
| 318 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 319 | DEF_TEST(SkSLModifiersDeclaration, r) { |
| 320 | test(r, |
| 321 | "layout(blend_support_all_equations) out;" |
| 322 | "void main() { }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 323 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 324 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 325 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 326 | "layout (blend_support_all_equations) out ;\n" |
| 327 | "void main() {\n" |
| 328 | "}\n"); |
| 329 | } |
| 330 | |
| 331 | DEF_TEST(SkSLHex, r) { |
| 332 | test(r, |
| 333 | "void main() {" |
| 334 | "int i1 = 0x0;" |
| 335 | "int i2 = 0x1234abcd;" |
| 336 | "int i3 = 0x7fffffff;" |
| 337 | "int i4 = 0xffffffff;" |
| 338 | "int i5 = -0xbeef;" |
| 339 | "uint u1 = 0x0;" |
| 340 | "uint u2 = 0x1234abcd;" |
| 341 | "uint u3 = 0x7fffffff;" |
| 342 | "uint u4 = 0xffffffff;" |
| 343 | "}", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 344 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 345 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 346 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 347 | "void main() {\n" |
| 348 | " int i1 = 0;\n" |
| 349 | " int i2 = 305441741;\n" |
| 350 | " int i3 = 2147483647;\n" |
| 351 | " int i4 = -1;\n" |
| 352 | " int i5 = -48879;\n" |
| 353 | " uint u1 = 0u;\n" |
| 354 | " uint u2 = 305441741u;\n" |
| 355 | " uint u3 = 2147483647u;\n" |
| 356 | " uint u4 = 4294967295u;\n" |
| 357 | "}\n"); |
| 358 | } |
| 359 | |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 360 | DEF_TEST(SkSLVectorConstructors, r) { |
| 361 | test(r, |
| 362 | "vec2 v1 = vec2(1);" |
| 363 | "vec2 v2 = vec2(1, 2);" |
| 364 | "vec2 v3 = vec2(vec2(1));" |
| 365 | "vec2 v4 = vec2(vec3(1));" |
| 366 | "vec3 v5 = vec3(vec2(1), 1.0);" |
| 367 | "vec3 v6 = vec3(vec4(1, 2, 3, 4));" |
| 368 | "ivec2 v7 = ivec2(1);" |
| 369 | "ivec2 v8 = ivec2(vec2(1, 2));" |
| 370 | "vec2 v9 = vec2(ivec2(1, 2));", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 371 | *SkSL::GLSLCapsFactory::Default(), |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 372 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 373 | "out vec4 sk_FragColor;\n" |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 374 | "vec2 v1 = vec2(1.0);\n" |
| 375 | "vec2 v2 = vec2(1.0, 2.0);\n" |
| 376 | "vec2 v3 = vec2(1.0);\n" |
| 377 | "vec2 v4 = vec2(vec3(1.0));\n" |
| 378 | "vec3 v5 = vec3(vec2(1.0), 1.0);\n" |
| 379 | "vec3 v6 = vec3(vec4(1.0, 2.0, 3.0, 4.0));\n" |
| 380 | "ivec2 v7 = ivec2(1);\n" |
| 381 | "ivec2 v8 = ivec2(vec2(1.0, 2.0));\n" |
| 382 | "vec2 v9 = vec2(ivec2(1, 2));\n"); |
| 383 | } |
| 384 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 385 | DEF_TEST(SkSLArrayConstructors, r) { |
| 386 | test(r, |
| 387 | "float test1[] = float[](1, 2, 3, 4);" |
| 388 | "vec2 test2[] = vec2[](vec2(1, 2), vec2(3, 4));" |
| 389 | "mat4 test3[] = mat4[]();", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 390 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 391 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 392 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 393 | "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n" |
| 394 | "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n" |
| 395 | "mat4 test3[] = mat4[]();\n"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 396 | } |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 397 | |
| 398 | DEF_TEST(SkSLDerivatives, r) { |
| 399 | test(r, |
| 400 | "void main() { float x = dFdx(1); }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 401 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 402 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 403 | "out vec4 sk_FragColor;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 404 | "void main() {\n" |
| 405 | " float x = dFdx(1.0);\n" |
| 406 | "}\n"); |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 407 | test(r, |
| 408 | "void main() { float x = 1; }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 409 | *SkSL::GLSLCapsFactory::ShaderDerivativeExtensionString(), |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 410 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 411 | "out vec4 sk_FragColor;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 412 | "void main() {\n" |
| 413 | " float x = 1.0;\n" |
| 414 | "}\n"); |
| 415 | test(r, |
| 416 | "void main() { float x = dFdx(1); }", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 417 | *SkSL::GLSLCapsFactory::ShaderDerivativeExtensionString(), |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 418 | "#version 400\n" |
| 419 | "#extension GL_OES_standard_derivatives : require\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 420 | "out vec4 sk_FragColor;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 421 | "void main() {\n" |
| 422 | " float x = dFdx(1.0);\n" |
| 423 | "}\n"); |
| 424 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 425 | |
| 426 | DEF_TEST(SkSLConstantFolding, r) { |
| 427 | test(r, |
| 428 | "void main() {" |
| 429 | "float f_add = 32 + 2;" |
| 430 | "float f_sub = 32 - 2;" |
| 431 | "float f_mul = 32 * 2;" |
| 432 | "float f_div = 32 / 2;" |
| 433 | "float mixed = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;" |
| 434 | "int i_add = 32 + 2;" |
| 435 | "int i_sub = 32 - 2;" |
| 436 | "int i_mul = 32 * 2;" |
| 437 | "int i_div = 32 / 2;" |
| 438 | "int i_or = 12 | 6;" |
| 439 | "int i_and = 254 & 7;" |
| 440 | "int i_xor = 2 ^ 7;" |
| 441 | "int i_shl = 1 << 4;" |
| 442 | "int i_shr = 128 >> 2;" |
| 443 | "bool gt_it = 6 > 5;" |
| 444 | "bool gt_if = 6 > 6;" |
| 445 | "bool gt_ft = 6.0 > 5.0;" |
| 446 | "bool gt_ff = 6.0 > 6.0;" |
| 447 | "bool gte_it = 6 >= 6;" |
| 448 | "bool gte_if = 6 >= 7;" |
| 449 | "bool gte_ft = 6.0 >= 6.0;" |
| 450 | "bool gte_ff = 6.0 >= 7.0;" |
| 451 | "bool lte_it = 6 <= 6;" |
| 452 | "bool lte_if = 6 <= 5;" |
| 453 | "bool lte_ft = 6.0 <= 6.0;" |
| 454 | "bool lte_ff = 6.0 <= 5.0;" |
| 455 | "bool or_t = 1 == 1 || 2 == 8;" |
| 456 | "bool or_f = 1 > 1 || 2 == 8;" |
| 457 | "bool and_t = 1 == 1 && 2 <= 8;" |
| 458 | "bool and_f = 1 == 2 && 2 == 8;" |
| 459 | "bool xor_t = 1 == 1 ^^ 1 != 1;" |
| 460 | "bool xor_f = 1 == 1 ^^ 1 == 1;" |
| 461 | "int ternary = 10 > 5 ? 10 : 5;" |
| 462 | "}", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 463 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 464 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 465 | "out vec4 sk_FragColor;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 466 | "void main() {\n" |
| 467 | " float f_add = 34.0;\n" |
| 468 | " float f_sub = 30.0;\n" |
| 469 | " float f_mul = 64.0;\n" |
| 470 | " float f_div = 16.0;\n" |
| 471 | " float mixed = 19.0;\n" |
| 472 | " int i_add = 34;\n" |
| 473 | " int i_sub = 30;\n" |
| 474 | " int i_mul = 64;\n" |
| 475 | " int i_div = 16;\n" |
| 476 | " int i_or = 14;\n" |
| 477 | " int i_and = 6;\n" |
| 478 | " int i_xor = 5;\n" |
| 479 | " int i_shl = 16;\n" |
| 480 | " int i_shr = 32;\n" |
| 481 | " bool gt_it = true;\n" |
| 482 | " bool gt_if = false;\n" |
| 483 | " bool gt_ft = true;\n" |
| 484 | " bool gt_ff = false;\n" |
| 485 | " bool gte_it = true;\n" |
| 486 | " bool gte_if = false;\n" |
| 487 | " bool gte_ft = true;\n" |
| 488 | " bool gte_ff = false;\n" |
| 489 | " bool lte_it = true;\n" |
| 490 | " bool lte_if = false;\n" |
| 491 | " bool lte_ft = true;\n" |
| 492 | " bool lte_ff = false;\n" |
| 493 | " bool or_t = true;\n" |
| 494 | " bool or_f = false;\n" |
| 495 | " bool and_t = true;\n" |
| 496 | " bool and_f = false;\n" |
| 497 | " bool xor_t = true;\n" |
| 498 | " bool xor_f = false;\n" |
| 499 | " int ternary = 10;\n" |
| 500 | "}\n"); |
| 501 | } |
| 502 | |
| 503 | DEF_TEST(SkSLStaticIf, r) { |
| 504 | test(r, |
| 505 | "void main() {" |
| 506 | "int x;" |
| 507 | "if (true) x = 1;" |
| 508 | "if (2 > 1) x = 2; else x = 3;" |
| 509 | "if (1 > 2) x = 4; else x = 5;" |
| 510 | "if (false) x = 6;" |
| 511 | "}", |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 512 | *SkSL::GLSLCapsFactory::Default(), |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 513 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 514 | "out vec4 sk_FragColor;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 515 | "void main() {\n" |
| 516 | " int x;\n" |
| 517 | " x = 1;\n" |
| 518 | " x = 2;\n" |
| 519 | " x = 5;\n" |
| 520 | " {\n" |
| 521 | " }\n" |
| 522 | "}\n"); |
| 523 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 524 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 525 | DEF_TEST(SkSLCaps, r) { |
| 526 | test(r, |
| 527 | "void main() {" |
| 528 | "int x;" |
| 529 | "if (sk_Caps.externalTextureSupport) x = 1;" |
| 530 | "if (sk_Caps.fbFetchSupport) x = 2;" |
| 531 | "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.texelFetchSupport) x = 3;" |
| 532 | "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.canUseAnyFunctionInShader) x = 4;" |
| 533 | "}", |
| 534 | *SkSL::GLSLCapsFactory::VariousCaps(), |
| 535 | "#version 400\n" |
| 536 | "out vec4 sk_FragColor;\n" |
| 537 | "void main() {\n" |
| 538 | " int x;\n" |
| 539 | " x = 1;\n" |
| 540 | " {\n" |
| 541 | " }\n" |
| 542 | " x = 3;\n" |
| 543 | " {\n" |
| 544 | " }\n" |
| 545 | "}\n"); |
| 546 | } |
| 547 | |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 548 | DEF_TEST(SkSLTexture, r) { |
| 549 | test(r, |
| 550 | "uniform sampler1D one;" |
| 551 | "uniform sampler2D two;" |
| 552 | "void main() {" |
| 553 | "vec4 a = texture(one, 0);" |
| 554 | "vec4 b = texture(two, vec2(0));" |
| 555 | "vec4 c = texture(one, vec2(0));" |
| 556 | "vec4 d = texture(two, vec3(0));" |
| 557 | "}", |
| 558 | *SkSL::GLSLCapsFactory::Default(), |
| 559 | "#version 400\n" |
| 560 | "out vec4 sk_FragColor;\n" |
| 561 | "uniform sampler1D one;\n" |
| 562 | "uniform sampler2D two;\n" |
| 563 | "void main() {\n" |
| 564 | " vec4 a = texture(one, 0.0);\n" |
| 565 | " vec4 b = texture(two, vec2(0.0));\n" |
| 566 | " vec4 c = textureProj(one, vec2(0.0));\n" |
| 567 | " vec4 d = textureProj(two, vec3(0.0));\n" |
| 568 | "}\n"); |
| 569 | test(r, |
| 570 | "uniform sampler1D one;" |
| 571 | "uniform sampler2D two;" |
| 572 | "void main() {" |
| 573 | "vec4 a = texture(one, 0);" |
| 574 | "vec4 b = texture(two, vec2(0));" |
| 575 | "vec4 c = texture(one, vec2(0));" |
| 576 | "vec4 d = texture(two, vec3(0));" |
| 577 | "}", |
| 578 | *SkSL::GLSLCapsFactory::Version110(), |
| 579 | "#version 110\n" |
| 580 | "uniform sampler1D one;\n" |
| 581 | "uniform sampler2D two;\n" |
| 582 | "void main() {\n" |
| 583 | " vec4 a = texture1D(one, 0.0);\n" |
| 584 | " vec4 b = texture2D(two, vec2(0.0));\n" |
| 585 | " vec4 c = texture1DProj(one, vec2(0.0));\n" |
| 586 | " vec4 d = texture2DProj(two, vec3(0.0));\n" |
| 587 | "}\n"); |
| 588 | } |
| 589 | |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 590 | DEF_TEST(SkSLOffset, r) { |
| 591 | test(r, |
| 592 | "struct Test {" |
| 593 | "layout(offset = 0) int x;" |
| 594 | "layout(offset = 4) int y;" |
| 595 | "int z;" |
| 596 | "} test;", |
| 597 | *SkSL::GLSLCapsFactory::Default(), |
| 598 | "#version 400\n" |
| 599 | "out vec4 sk_FragColor;\n" |
| 600 | "struct Test {\n" |
| 601 | " layout (offset = 0) int x;\n" |
| 602 | " layout (offset = 4) int y;\n" |
| 603 | " int z;\n" |
| 604 | "} test;\n"); |
| 605 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 606 | #endif |