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 | |
| 29 | DEF_TEST(SkSLHelloWorld, r) { |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 30 | SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 31 | test(r, |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 32 | "out vec4 fragColor; void main() { fragColor = vec4(0.75); }", |
| 33 | caps, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 34 | "#version 400\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 35 | "out vec4 fragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 36 | "void main() {\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 37 | " fragColor = vec4(0.75);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 38 | "}\n"); |
| 39 | } |
| 40 | |
| 41 | DEF_TEST(SkSLControl, r) { |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 42 | SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 43 | test(r, |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 44 | "out vec4 fragColor;" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 45 | "void main() {" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 46 | "if (1 + 2 + 3 > 5) { fragColor = vec4(0.75); } else { discard; }" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 47 | "int i = 0;" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 48 | "while (i < 10) fragColor *= 0.5;" |
| 49 | "do { fragColor += 0.01; } while (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 | "}", |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 55 | caps, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 56 | "#version 400\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 57 | "out vec4 fragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 58 | "void main() {\n" |
| 59 | " if ((1 + 2) + 3 > 5) {\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 60 | " 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" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 65 | " while (i < 10) fragColor *= 0.5;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 66 | " do {\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 67 | " fragColor += 0.01;\n" |
| 68 | " } while (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) { |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 77 | SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 78 | test(r, |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 79 | "out vec4 fragColor;" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 80 | "float foo(float v[2]) { return v[0] * v[1]; }" |
| 81 | "void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 82 | "void main() { float x = 10; bar(x); fragColor = vec4(x); }", |
| 83 | caps, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 84 | "#version 400\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 85 | "out vec4 fragColor;\n" |
| 86 | "float foo(in float[2] v) {\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 87 | " return v[0] * v[1];\n" |
| 88 | "}\n" |
| 89 | "void bar(inout float x) {\n" |
| 90 | " float y[2], z;\n" |
| 91 | " y[0] = x;\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 92 | " y[1] = x * 2;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 93 | " z = foo(y);\n" |
| 94 | " x = z;\n" |
| 95 | "}\n" |
| 96 | "void main() {\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 97 | " float x = 10;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 98 | " bar(x);\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 99 | " fragColor = vec4(x);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 100 | "}\n"); |
| 101 | } |
| 102 | |
| 103 | DEF_TEST(SkSLOperators, r) { |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 104 | SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 105 | test(r, |
| 106 | "void main() {" |
| 107 | "float x = 1, y = 2;" |
| 108 | "int z = 3;" |
| 109 | "x = x + y * z * x * (y - z);" |
| 110 | "y = x / y / z;" |
| 111 | "z = (z / 2 % 3 << 4) >> 2 << 1;" |
| 112 | "bool b = (x > 4) == x < 2 || 2 >= 5 && y <= z && 12 != 11;" |
| 113 | "x += 12;" |
| 114 | "x -= 12;" |
| 115 | "x *= y /= z = 10;" |
| 116 | "b ||= false;" |
| 117 | "b &&= true;" |
| 118 | "b ^^= false;" |
| 119 | "z |= 0;" |
| 120 | "z &= -1;" |
| 121 | "z ^= 0;" |
| 122 | "z >>= 2;" |
| 123 | "z <<= 4;" |
| 124 | "z %= 5;" |
| 125 | "}", |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 126 | caps, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 127 | "#version 400\n" |
| 128 | "void main() {\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 129 | " float x = 1, y = 2;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 130 | " int z = 3;\n" |
| 131 | " x = x + ((y * float(z)) * x) * (y - float(z));\n" |
| 132 | " y = (x / y) / float(z);\n" |
| 133 | " z = (((z / 2) % 3 << 4) >> 2) << 1;\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 134 | " bool b = x > 4 == x < 2 || (2 >= 5 && y <= float(z)) && 12 != 11;\n" |
| 135 | " x += 12;\n" |
| 136 | " x -= 12;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 137 | " x *= (y /= float(z = 10));\n" |
| 138 | " b ||= false;\n" |
| 139 | " b &&= true;\n" |
| 140 | " b ^^= false;\n" |
| 141 | " z |= 0;\n" |
| 142 | " z &= -1;\n" |
| 143 | " z ^= 0;\n" |
| 144 | " z >>= 2;\n" |
| 145 | " z <<= 4;\n" |
| 146 | " z %= 5;\n" |
| 147 | "}\n"); |
| 148 | } |
| 149 | |
| 150 | DEF_TEST(SkSLMatrices, r) { |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 151 | SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 152 | test(r, |
| 153 | "void main() {" |
| 154 | "mat2x4 x = mat2x4(1);" |
| 155 | "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));" |
| 156 | "mat3x4 z = x * y;" |
| 157 | "vec3 v1 = mat3(1) * vec3(1);" |
| 158 | "vec3 v2 = vec3(1) * mat3(1);" |
| 159 | "}", |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 160 | caps, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 161 | "#version 400\n" |
| 162 | "void main() {\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 163 | " mat2x4 x = mat2x4(1);\n" |
| 164 | " mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 165 | " mat3x4 z = x * y;\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 166 | " vec3 v1 = mat3(1) * vec3(1);\n" |
| 167 | " vec3 v2 = vec3(1) * mat3(1);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 168 | "}\n"); |
| 169 | } |
| 170 | |
| 171 | DEF_TEST(SkSLInterfaceBlock, r) { |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 172 | SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 173 | test(r, |
| 174 | "uniform testBlock {" |
| 175 | "float x;" |
| 176 | "float y[2];" |
| 177 | "layout(binding=12) mat3x2 z;" |
| 178 | "bool w;" |
| 179 | "};" |
| 180 | "void main() {" |
| 181 | "}", |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 182 | caps, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 183 | "#version 400\n" |
| 184 | "uniform testBlock {\n" |
| 185 | " float x;\n" |
| 186 | " float[2] y;\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 187 | " layout (binding = 12)mat3x2 z;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 188 | " bool w;\n" |
| 189 | "};\n" |
| 190 | "void main() {\n" |
| 191 | "}\n"); |
| 192 | } |
| 193 | |
| 194 | DEF_TEST(SkSLStructs, r) { |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 195 | SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 196 | test(r, |
| 197 | "struct A {" |
| 198 | "int x;" |
| 199 | "int y;" |
| 200 | "} a1, a2;" |
| 201 | "A a3;" |
| 202 | "struct B {" |
| 203 | "float x;" |
| 204 | "float y[2];" |
| 205 | "layout(binding=1) A z;" |
| 206 | "};" |
| 207 | "B b1, b2, b3;" |
| 208 | "void main() {" |
| 209 | "}", |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 210 | caps, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 211 | "#version 400\n" |
| 212 | "struct A {\n" |
| 213 | " int x;\n" |
| 214 | " int y;\n" |
| 215 | "}\n" |
| 216 | " a1, a2;\n" |
| 217 | "A a3;\n" |
| 218 | "struct B {\n" |
| 219 | " float x;\n" |
| 220 | " float[2] y;\n" |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 221 | " layout (binding = 1)A z;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 222 | "}\n" |
| 223 | " b1, b2, b3;\n" |
| 224 | "void main() {\n" |
| 225 | "}\n"); |
| 226 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 227 | } |