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