2016-08-03 19:43:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkSLCompiler.h"
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
static void test(skiatest::Reporter* r, const char* src, SkSL::GLCaps caps, const char* expected) {
|
|
|
|
SkSL::Compiler compiler;
|
|
|
|
std::string output;
|
|
|
|
bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, src, caps, &output);
|
|
|
|
if (!result) {
|
|
|
|
SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(r, result);
|
|
|
|
if (result) {
|
|
|
|
if (output != expected) {
|
|
|
|
SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
|
|
|
|
expected, output.c_str());
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(r, output == expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkSLHelloWorld, r) {
|
2016-09-12 16:15:53 +00:00
|
|
|
SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard };
|
2016-08-03 19:43:36 +00:00
|
|
|
test(r,
|
2016-09-12 16:15:53 +00:00
|
|
|
"out vec4 fragColor; void main() { fragColor = vec4(0.75); }",
|
|
|
|
caps,
|
2016-08-03 19:43:36 +00:00
|
|
|
"#version 400\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
"out vec4 fragColor;\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
"void main() {\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" fragColor = vec4(0.75);\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
"}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkSLControl, r) {
|
2016-09-12 16:15:53 +00:00
|
|
|
SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard };
|
2016-08-03 19:43:36 +00:00
|
|
|
test(r,
|
2016-09-12 16:15:53 +00:00
|
|
|
"out vec4 fragColor;"
|
2016-08-03 19:43:36 +00:00
|
|
|
"void main() {"
|
2016-09-12 16:15:53 +00:00
|
|
|
"if (1 + 2 + 3 > 5) { fragColor = vec4(0.75); } else { discard; }"
|
2016-08-03 19:43:36 +00:00
|
|
|
"int i = 0;"
|
2016-09-12 16:15:53 +00:00
|
|
|
"while (i < 10) fragColor *= 0.5;"
|
|
|
|
"do { fragColor += 0.01; } while (fragColor.x < 0.7);"
|
2016-08-03 19:43:36 +00:00
|
|
|
"for (int i = 0; i < 10; i++) {"
|
|
|
|
"if (i % 0 == 1) break; else continue;"
|
|
|
|
"}"
|
|
|
|
"return;"
|
|
|
|
"}",
|
2016-09-12 16:15:53 +00:00
|
|
|
caps,
|
2016-08-03 19:43:36 +00:00
|
|
|
"#version 400\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
"out vec4 fragColor;\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
"void main() {\n"
|
|
|
|
" if ((1 + 2) + 3 > 5) {\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" fragColor = vec4(0.75);\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
" } else {\n"
|
|
|
|
" discard;\n"
|
|
|
|
" }\n"
|
|
|
|
" int i = 0;\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" while (i < 10) fragColor *= 0.5;\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
" do {\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" fragColor += 0.01;\n"
|
|
|
|
" } while (fragColor.x < 0.7);\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
" for (int i = 0;i < 10; i++) {\n"
|
|
|
|
" if (i % 0 == 1) break; else continue;\n"
|
|
|
|
" }\n"
|
|
|
|
" return;\n"
|
|
|
|
"}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkSLFunctions, r) {
|
2016-09-12 16:15:53 +00:00
|
|
|
SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard };
|
2016-08-03 19:43:36 +00:00
|
|
|
test(r,
|
2016-09-12 16:15:53 +00:00
|
|
|
"out vec4 fragColor;"
|
2016-08-03 19:43:36 +00:00
|
|
|
"float foo(float v[2]) { return v[0] * v[1]; }"
|
|
|
|
"void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }"
|
2016-09-12 16:15:53 +00:00
|
|
|
"void main() { float x = 10; bar(x); fragColor = vec4(x); }",
|
|
|
|
caps,
|
2016-08-03 19:43:36 +00:00
|
|
|
"#version 400\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
"out vec4 fragColor;\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
"float foo(in float[2] v) {\n"
|
|
|
|
" return v[0] * v[1];\n"
|
|
|
|
"}\n"
|
|
|
|
"void bar(inout float x) {\n"
|
|
|
|
" float y[2], z;\n"
|
|
|
|
" y[0] = x;\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" y[1] = x * 2;\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
" z = foo(y);\n"
|
|
|
|
" x = z;\n"
|
|
|
|
"}\n"
|
|
|
|
"void main() {\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" float x = 10;\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
" bar(x);\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" fragColor = vec4(x);\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
"}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkSLOperators, r) {
|
2016-09-12 16:15:53 +00:00
|
|
|
SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard };
|
2016-08-03 19:43:36 +00:00
|
|
|
test(r,
|
|
|
|
"void main() {"
|
|
|
|
"float x = 1, y = 2;"
|
|
|
|
"int z = 3;"
|
|
|
|
"x = x + y * z * x * (y - z);"
|
|
|
|
"y = x / y / z;"
|
|
|
|
"z = (z / 2 % 3 << 4) >> 2 << 1;"
|
|
|
|
"bool b = (x > 4) == x < 2 || 2 >= 5 && y <= z && 12 != 11;"
|
|
|
|
"x += 12;"
|
|
|
|
"x -= 12;"
|
|
|
|
"x *= y /= z = 10;"
|
|
|
|
"b ||= false;"
|
|
|
|
"b &&= true;"
|
|
|
|
"b ^^= false;"
|
|
|
|
"z |= 0;"
|
|
|
|
"z &= -1;"
|
|
|
|
"z ^= 0;"
|
|
|
|
"z >>= 2;"
|
|
|
|
"z <<= 4;"
|
|
|
|
"z %= 5;"
|
|
|
|
"}",
|
2016-09-12 16:15:53 +00:00
|
|
|
caps,
|
2016-08-03 19:43:36 +00:00
|
|
|
"#version 400\n"
|
|
|
|
"void main() {\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" float x = 1, y = 2;\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
" int z = 3;\n"
|
|
|
|
" x = x + ((y * float(z)) * x) * (y - float(z));\n"
|
|
|
|
" y = (x / y) / float(z);\n"
|
|
|
|
" z = (((z / 2) % 3 << 4) >> 2) << 1;\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" bool b = x > 4 == x < 2 || (2 >= 5 && y <= float(z)) && 12 != 11;\n"
|
|
|
|
" x += 12;\n"
|
|
|
|
" x -= 12;\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
" x *= (y /= float(z = 10));\n"
|
|
|
|
" b ||= false;\n"
|
|
|
|
" b &&= true;\n"
|
|
|
|
" b ^^= false;\n"
|
|
|
|
" z |= 0;\n"
|
|
|
|
" z &= -1;\n"
|
|
|
|
" z ^= 0;\n"
|
|
|
|
" z >>= 2;\n"
|
|
|
|
" z <<= 4;\n"
|
|
|
|
" z %= 5;\n"
|
|
|
|
"}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkSLMatrices, r) {
|
2016-09-12 16:15:53 +00:00
|
|
|
SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard };
|
2016-08-03 19:43:36 +00:00
|
|
|
test(r,
|
|
|
|
"void main() {"
|
|
|
|
"mat2x4 x = mat2x4(1);"
|
|
|
|
"mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));"
|
|
|
|
"mat3x4 z = x * y;"
|
|
|
|
"vec3 v1 = mat3(1) * vec3(1);"
|
|
|
|
"vec3 v2 = vec3(1) * mat3(1);"
|
|
|
|
"}",
|
2016-09-12 16:15:53 +00:00
|
|
|
caps,
|
2016-08-03 19:43:36 +00:00
|
|
|
"#version 400\n"
|
|
|
|
"void main() {\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" mat2x4 x = mat2x4(1);\n"
|
|
|
|
" mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
" mat3x4 z = x * y;\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" vec3 v1 = mat3(1) * vec3(1);\n"
|
|
|
|
" vec3 v2 = vec3(1) * mat3(1);\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
"}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkSLInterfaceBlock, r) {
|
2016-09-12 16:15:53 +00:00
|
|
|
SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard };
|
2016-08-03 19:43:36 +00:00
|
|
|
test(r,
|
|
|
|
"uniform testBlock {"
|
|
|
|
"float x;"
|
|
|
|
"float y[2];"
|
|
|
|
"layout(binding=12) mat3x2 z;"
|
|
|
|
"bool w;"
|
|
|
|
"};"
|
|
|
|
"void main() {"
|
|
|
|
"}",
|
2016-09-12 16:15:53 +00:00
|
|
|
caps,
|
2016-08-03 19:43:36 +00:00
|
|
|
"#version 400\n"
|
|
|
|
"uniform testBlock {\n"
|
|
|
|
" float x;\n"
|
|
|
|
" float[2] y;\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" layout (binding = 12)mat3x2 z;\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
" bool w;\n"
|
|
|
|
"};\n"
|
|
|
|
"void main() {\n"
|
|
|
|
"}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkSLStructs, r) {
|
2016-09-12 16:15:53 +00:00
|
|
|
SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard };
|
2016-08-03 19:43:36 +00:00
|
|
|
test(r,
|
|
|
|
"struct A {"
|
|
|
|
"int x;"
|
|
|
|
"int y;"
|
|
|
|
"} a1, a2;"
|
|
|
|
"A a3;"
|
|
|
|
"struct B {"
|
|
|
|
"float x;"
|
|
|
|
"float y[2];"
|
|
|
|
"layout(binding=1) A z;"
|
|
|
|
"};"
|
|
|
|
"B b1, b2, b3;"
|
|
|
|
"void main() {"
|
|
|
|
"}",
|
2016-09-12 16:15:53 +00:00
|
|
|
caps,
|
2016-08-03 19:43:36 +00:00
|
|
|
"#version 400\n"
|
|
|
|
"struct A {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int y;\n"
|
|
|
|
"}\n"
|
|
|
|
" a1, a2;\n"
|
|
|
|
"A a3;\n"
|
|
|
|
"struct B {\n"
|
|
|
|
" float x;\n"
|
|
|
|
" float[2] y;\n"
|
2016-09-12 16:15:53 +00:00
|
|
|
" layout (binding = 1)A z;\n"
|
2016-08-03 19:43:36 +00:00
|
|
|
"}\n"
|
|
|
|
" b1, b2, b3;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
"}\n");
|
|
|
|
|
|
|
|
}
|