blob: 126be45246838e3dcbc2844622bcda98ebbe26b6 [file] [log] [blame]
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -040011
Ethan Nicholas5ac13c22017-05-10 15:06:17 -040012static void test_failure(skiatest::Reporter* r, const char* src, const char* error) {
13 SkSL::Compiler compiler;
14 SkSL::Program::Settings settings;
15 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
16 settings.fCaps = caps.get();
17 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
Brian Osman93ba0a42017-08-14 14:48:10 -040018 SkSL::String(src), settings);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -040019 if (program) {
20 SkSL::String ignored;
21 compiler.toSPIRV(*program, &ignored);
22 }
23 SkSL::String skError(error);
24 if (compiler.errorText() != skError) {
25 SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
26 compiler.errorText().c_str());
27 }
28 REPORTER_ASSERT(r, compiler.errorText() == skError);
29}
30
31DEF_TEST(SkSLBadOffset, r) {
32 test_failure(r,
Ethan Nicholas68dd2c12018-03-01 15:05:17 -050033 "struct Bad { layout (offset = 5) int x; } bad; void main() { bad.x = 5; "
Ethan Nicholase1f55022019-02-05 17:17:40 -050034 "sk_FragColor.r = half(bad.x); }",
Ethan Nicholas5ac13c22017-05-10 15:06:17 -040035 "error: 1: offset of field 'x' must be a multiple of 4\n1 error\n");
36 test_failure(r,
Ethan Nicholas68dd2c12018-03-01 15:05:17 -050037 "struct Bad { int x; layout (offset = 0) int y; } bad; void main() { bad.x = 5; "
Ethan Nicholase1f55022019-02-05 17:17:40 -050038 "sk_FragColor.r = half(bad.x); }",
Ethan Nicholas5ac13c22017-05-10 15:06:17 -040039 "error: 1: offset of field 'y' must be at least 4\n1 error\n");
40}