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