OLD | NEW |
(Empty) | |
| 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, cons
t char* expected) { |
| 13 SkSL::Compiler compiler; |
| 14 std::string output; |
| 15 bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, src, caps, &out
put); |
| 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) { |
| 30 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
| 31 test(r, |
| 32 "out vec4 fragColor; void main() { fragColor = vec4(0.75); }", |
| 33 caps, |
| 34 "#version 400\n" |
| 35 "out vec4 fragColor;\n" |
| 36 "void main() {\n" |
| 37 " fragColor = vec4(0.75);\n" |
| 38 "}\n"); |
| 39 } |
| 40 |
| 41 DEF_TEST(SkSLControl, r) { |
| 42 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
| 43 test(r, |
| 44 "out vec4 fragColor;" |
| 45 "void main() {" |
| 46 "if (1 + 2 + 3 > 5) { fragColor = vec4(0.75); } else { discard; }" |
| 47 "int i = 0;" |
| 48 "while (i < 10) fragColor *= 0.5;" |
| 49 "do { fragColor += 0.01; } while (fragColor.x < 0.7);" |
| 50 "for (int i = 0; i < 10; i++) {" |
| 51 "if (i % 0 == 1) break; else continue;" |
| 52 "}" |
| 53 "return;" |
| 54 "}", |
| 55 caps, |
| 56 "#version 400\n" |
| 57 "out vec4 fragColor;\n" |
| 58 "void main() {\n" |
| 59 " if ((1 + 2) + 3 > 5) {\n" |
| 60 " fragColor = vec4(0.75);\n" |
| 61 " } else {\n" |
| 62 " discard;\n" |
| 63 " }\n" |
| 64 " int i = 0;\n" |
| 65 " while (i < 10) fragColor *= 0.5;\n" |
| 66 " do {\n" |
| 67 " fragColor += 0.01;\n" |
| 68 " } while (fragColor.x < 0.7);\n" |
| 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) { |
| 77 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
| 78 test(r, |
| 79 "out vec4 fragColor;" |
| 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 = f
oo(y); x = z; }" |
| 82 "void main() { float x = 10; bar(x); fragColor = vec4(x); }", |
| 83 caps, |
| 84 "#version 400\n" |
| 85 "out vec4 fragColor;\n" |
| 86 "float foo(in float[2] v) {\n" |
| 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" |
| 92 " y[1] = x * 2;\n" |
| 93 " z = foo(y);\n" |
| 94 " x = z;\n" |
| 95 "}\n" |
| 96 "void main() {\n" |
| 97 " float x = 10;\n" |
| 98 " bar(x);\n" |
| 99 " fragColor = vec4(x);\n" |
| 100 "}\n"); |
| 101 } |
| 102 |
| 103 DEF_TEST(SkSLOperators, r) { |
| 104 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
| 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 "}", |
| 126 caps, |
| 127 "#version 400\n" |
| 128 "void main() {\n" |
| 129 " float x = 1, y = 2;\n" |
| 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" |
| 134 " bool b = x > 4 == x < 2 || (2 >= 5 && y <= float(z)) && 12 != 11;\
n" |
| 135 " x += 12;\n" |
| 136 " x -= 12;\n" |
| 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) { |
| 151 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
| 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 "}", |
| 160 caps, |
| 161 "#version 400\n" |
| 162 "void main() {\n" |
| 163 " mat2x4 x = mat2x4(1);\n" |
| 164 " mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));\n" |
| 165 " mat3x4 z = x * y;\n" |
| 166 " vec3 v1 = mat3(1) * vec3(1);\n" |
| 167 " vec3 v2 = vec3(1) * mat3(1);\n" |
| 168 "}\n"); |
| 169 } |
| 170 |
| 171 DEF_TEST(SkSLInterfaceBlock, r) { |
| 172 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
| 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 "}", |
| 182 caps, |
| 183 "#version 400\n" |
| 184 "uniform testBlock {\n" |
| 185 " float x;\n" |
| 186 " float[2] y;\n" |
| 187 " layout (binding = 12)mat3x2 z;\n" |
| 188 " bool w;\n" |
| 189 "};\n" |
| 190 "void main() {\n" |
| 191 "}\n"); |
| 192 } |
| 193 |
| 194 DEF_TEST(SkSLStructs, r) { |
| 195 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
| 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 "}", |
| 210 caps, |
| 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" |
| 221 " layout (binding = 1)A z;\n" |
| 222 "}\n" |
| 223 " b1, b2, b3;\n" |
| 224 "void main() {\n" |
| 225 "}\n"); |
| 226 |
| 227 } |
OLD | NEW |