OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkSLCompiler.h" | 8 #include "SkSLCompiler.h" |
9 | 9 |
10 #include "Test.h" | 10 #include "Test.h" |
11 | 11 |
12 static void test(skiatest::Reporter* r, const char* src, SkSL::GLCaps caps, cons
t char* expected) { | 12 static void test(skiatest::Reporter* r, const char* src, SkSL::GLCaps caps, cons
t char* expected) { |
13 SkSL::Compiler compiler; | 13 SkSL::Compiler compiler; |
14 std::string output; | 14 std::string output; |
15 bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, src, caps, &out
put); | 15 bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, src, caps, &out
put); |
16 if (!result) { | 16 if (!result) { |
17 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().
c_str()); | 17 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().
c_str()); |
18 } | 18 } |
19 REPORTER_ASSERT(r, result); | 19 REPORTER_ASSERT(r, result); |
20 if (result) { | 20 if (result) { |
21 if (output != expected) { | 21 if (output != expected) { |
22 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived
:\n'%s'", src, | 22 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived
:\n'%s'", src, |
23 expected, output.c_str()); | 23 expected, output.c_str()); |
24 } | 24 } |
25 REPORTER_ASSERT(r, output == expected); | 25 REPORTER_ASSERT(r, output == expected); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
| 29 static SkSL::GLCaps default_caps() { |
| 30 return { |
| 31 400, |
| 32 SkSL::GLCaps::kGL_Standard, |
| 33 false, // isCoreProfile |
| 34 false, // usesPrecisionModifiers; |
| 35 false, // mustDeclareFragmentShaderOutput |
| 36 true // canUseMinAndAbsTogether |
| 37 }; |
| 38 } |
| 39 |
29 DEF_TEST(SkSLHelloWorld, r) { | 40 DEF_TEST(SkSLHelloWorld, r) { |
30 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; | |
31 test(r, | 41 test(r, |
32 "out vec4 fragColor; void main() { fragColor = vec4(0.75); }", | 42 "void main() { sk_FragColor = vec4(0.75); }", |
33 caps, | 43 default_caps(), |
34 "#version 400\n" | 44 "#version 400\n" |
35 "out vec4 fragColor;\n" | |
36 "void main() {\n" | 45 "void main() {\n" |
37 " fragColor = vec4(0.75);\n" | 46 " gl_FragColor = vec4(0.75);\n" |
38 "}\n"); | 47 "}\n"); |
39 } | 48 } |
40 | 49 |
41 DEF_TEST(SkSLControl, r) { | 50 DEF_TEST(SkSLControl, r) { |
42 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; | |
43 test(r, | 51 test(r, |
44 "out vec4 fragColor;" | |
45 "void main() {" | 52 "void main() {" |
46 "if (1 + 2 + 3 > 5) { fragColor = vec4(0.75); } else { discard; }" | 53 "if (1 + 2 + 3 > 5) { sk_FragColor = vec4(0.75); } else { discard; }" |
47 "int i = 0;" | 54 "int i = 0;" |
48 "while (i < 10) fragColor *= 0.5;" | 55 "while (i < 10) sk_FragColor *= 0.5;" |
49 "do { fragColor += 0.01; } while (fragColor.x < 0.7);" | 56 "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.7);" |
50 "for (int i = 0; i < 10; i++) {" | 57 "for (int i = 0; i < 10; i++) {" |
51 "if (i % 0 == 1) break; else continue;" | 58 "if (i % 0 == 1) break; else continue;" |
52 "}" | 59 "}" |
53 "return;" | 60 "return;" |
54 "}", | 61 "}", |
55 caps, | 62 default_caps(), |
56 "#version 400\n" | 63 "#version 400\n" |
57 "out vec4 fragColor;\n" | |
58 "void main() {\n" | 64 "void main() {\n" |
59 " if ((1 + 2) + 3 > 5) {\n" | 65 " if ((1 + 2) + 3 > 5) {\n" |
60 " fragColor = vec4(0.75);\n" | 66 " gl_FragColor = vec4(0.75);\n" |
61 " } else {\n" | 67 " } else {\n" |
62 " discard;\n" | 68 " discard;\n" |
63 " }\n" | 69 " }\n" |
64 " int i = 0;\n" | 70 " int i = 0;\n" |
65 " while (i < 10) fragColor *= 0.5;\n" | 71 " while (i < 10) gl_FragColor *= 0.5;\n" |
66 " do {\n" | 72 " do {\n" |
67 " fragColor += 0.01;\n" | 73 " gl_FragColor += 0.01;\n" |
68 " } while (fragColor.x < 0.7);\n" | 74 " } while (gl_FragColor.x < 0.7);\n" |
69 " for (int i = 0;i < 10; i++) {\n" | 75 " for (int i = 0;i < 10; i++) {\n" |
70 " if (i % 0 == 1) break; else continue;\n" | 76 " if (i % 0 == 1) break; else continue;\n" |
71 " }\n" | 77 " }\n" |
72 " return;\n" | 78 " return;\n" |
73 "}\n"); | 79 "}\n"); |
74 } | 80 } |
75 | 81 |
76 DEF_TEST(SkSLFunctions, r) { | 82 DEF_TEST(SkSLFunctions, r) { |
77 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; | |
78 test(r, | 83 test(r, |
79 "out vec4 fragColor;" | |
80 "float foo(float v[2]) { return v[0] * v[1]; }" | 84 "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; }" | 85 "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); }", | 86 "void main() { float x = 10; bar(x); sk_FragColor = vec4(x); }", |
83 caps, | 87 default_caps(), |
84 "#version 400\n" | 88 "#version 400\n" |
85 "out vec4 fragColor;\n" | 89 "float foo(in float v[2]) {\n" |
86 "float foo(in float[2] v) {\n" | |
87 " return v[0] * v[1];\n" | 90 " return v[0] * v[1];\n" |
88 "}\n" | 91 "}\n" |
89 "void bar(inout float x) {\n" | 92 "void bar(inout float x) {\n" |
90 " float y[2], z;\n" | 93 " float y[2], z;\n" |
91 " y[0] = x;\n" | 94 " y[0] = x;\n" |
92 " y[1] = x * 2;\n" | 95 " y[1] = x * 2.0;\n" |
93 " z = foo(y);\n" | 96 " z = foo(y);\n" |
94 " x = z;\n" | 97 " x = z;\n" |
95 "}\n" | 98 "}\n" |
96 "void main() {\n" | 99 "void main() {\n" |
97 " float x = 10;\n" | 100 " float x = 10.0;\n" |
98 " bar(x);\n" | 101 " bar(x);\n" |
99 " fragColor = vec4(x);\n" | 102 " gl_FragColor = vec4(x);\n" |
100 "}\n"); | 103 "}\n"); |
101 } | 104 } |
102 | 105 |
103 DEF_TEST(SkSLOperators, r) { | 106 DEF_TEST(SkSLOperators, r) { |
104 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; | |
105 test(r, | 107 test(r, |
106 "void main() {" | 108 "void main() {" |
107 "float x = 1, y = 2;" | 109 "float x = 1, y = 2;" |
108 "int z = 3;" | 110 "int z = 3;" |
109 "x = x + y * z * x * (y - z);" | 111 "x = x + y * z * x * (y - z);" |
110 "y = x / y / z;" | 112 "y = x / y / z;" |
111 "z = (z / 2 % 3 << 4) >> 2 << 1;" | 113 "z = (z / 2 % 3 << 4) >> 2 << 1;" |
112 "bool b = (x > 4) == x < 2 || 2 >= 5 && y <= z && 12 != 11;" | 114 "bool b = (x > 4) == x < 2 || 2 >= 5 && y <= z && 12 != 11;" |
113 "x += 12;" | 115 "x += 12;" |
114 "x -= 12;" | 116 "x -= 12;" |
115 "x *= y /= z = 10;" | 117 "x *= y /= z = 10;" |
116 "b ||= false;" | 118 "b ||= false;" |
117 "b &&= true;" | 119 "b &&= true;" |
118 "b ^^= false;" | 120 "b ^^= false;" |
119 "z |= 0;" | 121 "z |= 0;" |
120 "z &= -1;" | 122 "z &= -1;" |
121 "z ^= 0;" | 123 "z ^= 0;" |
122 "z >>= 2;" | 124 "z >>= 2;" |
123 "z <<= 4;" | 125 "z <<= 4;" |
124 "z %= 5;" | 126 "z %= 5;" |
125 "}", | 127 "}", |
126 caps, | 128 default_caps(), |
127 "#version 400\n" | 129 "#version 400\n" |
128 "void main() {\n" | 130 "void main() {\n" |
129 " float x = 1, y = 2;\n" | 131 " float x = 1.0, y = 2.0;\n" |
130 " int z = 3;\n" | 132 " int z = 3;\n" |
131 " x = x + ((y * float(z)) * x) * (y - float(z));\n" | 133 " x = x + ((y * float(z)) * x) * (y - float(z));\n" |
132 " y = (x / y) / float(z);\n" | 134 " y = (x / y) / float(z);\n" |
133 " z = (((z / 2) % 3 << 4) >> 2) << 1;\n" | 135 " z = (((z / 2) % 3 << 4) >> 2) << 1;\n" |
134 " bool b = x > 4 == x < 2 || (2 >= 5 && y <= float(z)) && 12 != 11;\
n" | 136 " bool b = x > 4.0 == x < 2.0 || (2 >= 5 && y <= float(z)) && 12 !=
11;\n" |
135 " x += 12;\n" | 137 " x += 12.0;\n" |
136 " x -= 12;\n" | 138 " x -= 12.0;\n" |
137 " x *= (y /= float(z = 10));\n" | 139 " x *= (y /= float(z = 10));\n" |
138 " b ||= false;\n" | 140 " b ||= false;\n" |
139 " b &&= true;\n" | 141 " b &&= true;\n" |
140 " b ^^= false;\n" | 142 " b ^^= false;\n" |
141 " z |= 0;\n" | 143 " z |= 0;\n" |
142 " z &= -1;\n" | 144 " z &= -1;\n" |
143 " z ^= 0;\n" | 145 " z ^= 0;\n" |
144 " z >>= 2;\n" | 146 " z >>= 2;\n" |
145 " z <<= 4;\n" | 147 " z <<= 4;\n" |
146 " z %= 5;\n" | 148 " z %= 5;\n" |
147 "}\n"); | 149 "}\n"); |
148 } | 150 } |
149 | 151 |
150 DEF_TEST(SkSLMatrices, r) { | 152 DEF_TEST(SkSLMatrices, r) { |
151 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; | |
152 test(r, | 153 test(r, |
153 "void main() {" | 154 "void main() {" |
154 "mat2x4 x = mat2x4(1);" | 155 "mat2x4 x = mat2x4(1);" |
155 "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));" | 156 "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));" |
156 "mat3x4 z = x * y;" | 157 "mat3x4 z = x * y;" |
157 "vec3 v1 = mat3(1) * vec3(1);" | 158 "vec3 v1 = mat3(1) * vec3(1);" |
158 "vec3 v2 = vec3(1) * mat3(1);" | 159 "vec3 v2 = vec3(1) * mat3(1);" |
159 "}", | 160 "}", |
160 caps, | 161 default_caps(), |
161 "#version 400\n" | 162 "#version 400\n" |
162 "void main() {\n" | 163 "void main() {\n" |
163 " mat2x4 x = mat2x4(1);\n" | 164 " mat2x4 x = mat2x4(1.0);\n" |
164 " mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));\n" | 165 " mat3x2 y = mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n" |
165 " mat3x4 z = x * y;\n" | 166 " mat3x4 z = x * y;\n" |
166 " vec3 v1 = mat3(1) * vec3(1);\n" | 167 " vec3 v1 = mat3(1.0) * vec3(1.0);\n" |
167 " vec3 v2 = vec3(1) * mat3(1);\n" | 168 " vec3 v2 = vec3(1.0) * mat3(1.0);\n" |
168 "}\n"); | 169 "}\n"); |
169 } | 170 } |
170 | 171 |
171 DEF_TEST(SkSLInterfaceBlock, r) { | 172 DEF_TEST(SkSLInterfaceBlock, r) { |
172 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; | |
173 test(r, | 173 test(r, |
174 "uniform testBlock {" | 174 "uniform testBlock {" |
175 "float x;" | 175 "float x;" |
176 "float y[2];" | 176 "float y[2];" |
177 "layout(binding=12) mat3x2 z;" | 177 "layout(binding=12) mat3x2 z;" |
178 "bool w;" | 178 "bool w;" |
179 "};" | 179 "};" |
180 "void main() {" | 180 "void main() {" |
181 "}", | 181 "}", |
182 caps, | 182 default_caps(), |
183 "#version 400\n" | 183 "#version 400\n" |
184 "uniform testBlock {\n" | 184 "uniform testBlock {\n" |
185 " float x;\n" | 185 " float x;\n" |
186 " float[2] y;\n" | 186 " float[2] y;\n" |
187 " layout (binding = 12)mat3x2 z;\n" | 187 " layout (binding = 12) mat3x2 z;\n" |
188 " bool w;\n" | 188 " bool w;\n" |
189 "};\n" | 189 "};\n" |
190 "void main() {\n" | 190 "void main() {\n" |
191 "}\n"); | 191 "}\n"); |
192 } | 192 } |
193 | 193 |
194 DEF_TEST(SkSLStructs, r) { | 194 DEF_TEST(SkSLStructs, r) { |
195 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; | |
196 test(r, | 195 test(r, |
197 "struct A {" | 196 "struct A {" |
198 "int x;" | 197 "int x;" |
199 "int y;" | 198 "int y;" |
200 "} a1, a2;" | 199 "} a1, a2;" |
201 "A a3;" | 200 "A a3;" |
202 "struct B {" | 201 "struct B {" |
203 "float x;" | 202 "float x;" |
204 "float y[2];" | 203 "float y[2];" |
205 "layout(binding=1) A z;" | 204 "layout(binding=1) A z;" |
206 "};" | 205 "};" |
207 "B b1, b2, b3;" | 206 "B b1, b2, b3;" |
208 "void main() {" | 207 "void main() {" |
209 "}", | 208 "}", |
210 caps, | 209 default_caps(), |
211 "#version 400\n" | 210 "#version 400\n" |
212 "struct A {\n" | 211 "struct A {\n" |
213 " int x;\n" | 212 " int x;\n" |
214 " int y;\n" | 213 " int y;\n" |
215 "}\n" | 214 "}\n" |
216 " a1, a2;\n" | 215 " a1, a2;\n" |
217 "A a3;\n" | 216 "A a3;\n" |
218 "struct B {\n" | 217 "struct B {\n" |
219 " float x;\n" | 218 " float x;\n" |
220 " float[2] y;\n" | 219 " float[2] y;\n" |
221 " layout (binding = 1)A z;\n" | 220 " layout (binding = 1) A z;\n" |
222 "}\n" | 221 "}\n" |
223 " b1, b2, b3;\n" | 222 " b1, b2, b3;\n" |
224 "void main() {\n" | 223 "void main() {\n" |
225 "}\n"); | 224 "}\n"); |
| 225 } |
226 | 226 |
| 227 DEF_TEST(SkSLVersion, r) { |
| 228 SkSL::GLCaps caps = default_caps(); |
| 229 caps.fVersion = 450; |
| 230 caps.fIsCoreProfile = true; |
| 231 test(r, |
| 232 "in float test; void main() { sk_FragColor = vec4(0.75); }", |
| 233 caps, |
| 234 "#version 450 core\n" |
| 235 "in float test;\n" |
| 236 "void main() {\n" |
| 237 " gl_FragColor = vec4(0.75);\n" |
| 238 "}\n"); |
| 239 caps.fVersion = 110; |
| 240 caps.fIsCoreProfile = false; |
| 241 test(r, |
| 242 "in float test; void main() { sk_FragColor = vec4(0.75); }", |
| 243 caps, |
| 244 "#version 110\n" |
| 245 "varying float test;\n" |
| 246 "void main() {\n" |
| 247 " gl_FragColor = vec4(0.75);\n" |
| 248 "}\n"); |
227 } | 249 } |
| 250 |
| 251 DEF_TEST(SkSLDeclareOutput, r) { |
| 252 SkSL::GLCaps caps = default_caps(); |
| 253 caps.fMustDeclareFragmentShaderOutput = true; |
| 254 test(r, |
| 255 "void main() { sk_FragColor = vec4(0.75); }", |
| 256 caps, |
| 257 "#version 400\n" |
| 258 "out vec4 sk_FragColor;\n" |
| 259 "void main() {\n" |
| 260 " sk_FragColor = vec4(0.75);\n" |
| 261 "}\n"); |
| 262 } |
| 263 |
| 264 DEF_TEST(SkSLUsesPrecisionModifiers, r) { |
| 265 SkSL::GLCaps caps = default_caps(); |
| 266 test(r, |
| 267 "void main() { float x = 0.75; highp float y = 1; }", |
| 268 caps, |
| 269 "#version 400\n" |
| 270 "void main() {\n" |
| 271 " float x = 0.75;\n" |
| 272 " float y = 1.0;\n" |
| 273 "}\n"); |
| 274 caps.fStandard = SkSL::GLCaps::kGLES_Standard; |
| 275 caps.fUsesPrecisionModifiers = true; |
| 276 test(r, |
| 277 "void main() { float x = 0.75; highp float y = 1; }", |
| 278 caps, |
| 279 "#version 400 es\n" |
| 280 "precision highp float;\n" |
| 281 "void main() {\n" |
| 282 " float x = 0.75;\n" |
| 283 " highp float y = 1.0;\n" |
| 284 "}\n"); |
| 285 } |
| 286 |
| 287 DEF_TEST(SkSLMinAbs, r) { |
| 288 test(r, |
| 289 "void main() {" |
| 290 "float x = -5;" |
| 291 "x = min(abs(x), 6);" |
| 292 "}", |
| 293 default_caps(), |
| 294 "#version 400\n" |
| 295 "void main() {\n" |
| 296 " float x = -5.0;\n" |
| 297 " x = min(abs(x), 6.0);\n" |
| 298 "}\n"); |
| 299 |
| 300 SkSL::GLCaps caps = default_caps(); |
| 301 caps.fCanUseMinAndAbsTogether = false; |
| 302 test(r, |
| 303 "void main() {" |
| 304 "float x = -5.0;" |
| 305 "x = min(abs(x), 6.0);" |
| 306 "}", |
| 307 caps, |
| 308 "#version 400\n" |
| 309 "void main() {\n" |
| 310 " float minAbsHackVar0;\n" |
| 311 " float minAbsHackVar1;\n" |
| 312 " float x = -5.0;\n" |
| 313 " x = ((minAbsHackVar0 = abs(x)) < (minAbsHackVar1 = 6.0) ? minAbsHa
ckVar0 : " |
| 314
"minAbsHackVar1);\n" |
| 315 "}\n"); |
| 316 } |
| 317 |
| 318 DEF_TEST(SkSLModifiersDeclaration, r) { |
| 319 test(r, |
| 320 "layout(blend_support_all_equations) out;" |
| 321 "void main() { }", |
| 322 default_caps(), |
| 323 "#version 400\n" |
| 324 "layout (blend_support_all_equations) out ;\n" |
| 325 "void main() {\n" |
| 326 "}\n"); |
| 327 } |
| 328 |
| 329 DEF_TEST(SkSLHex, r) { |
| 330 test(r, |
| 331 "void main() {" |
| 332 "int i1 = 0x0;" |
| 333 "int i2 = 0x1234abcd;" |
| 334 "int i3 = 0x7fffffff;" |
| 335 "int i4 = 0xffffffff;" |
| 336 "int i5 = -0xbeef;" |
| 337 "uint u1 = 0x0;" |
| 338 "uint u2 = 0x1234abcd;" |
| 339 "uint u3 = 0x7fffffff;" |
| 340 "uint u4 = 0xffffffff;" |
| 341 "}", |
| 342 default_caps(), |
| 343 "#version 400\n" |
| 344 "void main() {\n" |
| 345 " int i1 = 0;\n" |
| 346 " int i2 = 305441741;\n" |
| 347 " int i3 = 2147483647;\n" |
| 348 " int i4 = -1;\n" |
| 349 " int i5 = -48879;\n" |
| 350 " uint u1 = 0u;\n" |
| 351 " uint u2 = 305441741u;\n" |
| 352 " uint u3 = 2147483647u;\n" |
| 353 " uint u4 = 4294967295u;\n" |
| 354 "}\n"); |
| 355 } |
| 356 |
| 357 DEF_TEST(SkSLArrayConstructors, r) { |
| 358 test(r, |
| 359 "float test1[] = float[](1, 2, 3, 4);" |
| 360 "vec2 test2[] = vec2[](vec2(1, 2), vec2(3, 4));" |
| 361 "mat4 test3[] = mat4[]();", |
| 362 default_caps(), |
| 363 "#version 400\n" |
| 364 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n" |
| 365 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n" |
| 366 "mat4 test3[] = mat4[]();\n"); |
| 367 } |
OLD | NEW |