skia2/tests/SkSLSPIRVTest.cpp
John Stiles 1d75778cbf Disallow opaque types in structs and interface blocks.
This is a followup to http://review.skia.org/335196. This detects opaque
types (samplers and textures) at parsing or IR generation time and
reports an error regardless of backend. This check occurs before Metal
or SPIR-V would have a chance to detect the error, so it changes their
output to a slightly more focused error message. The Metal/SPIR-V fix in
the prior CL is still a nice broad catch-all for preventing spurious
ABORTs, though.

Change-Id: I4cce92a8767d72b5d3d7277a8afde8ce5ce86db2
Bug: skia:10956
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/335217
Commit-Queue: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
2020-11-17 15:25:43 +00:00

53 lines
2.0 KiB
C++

/*
* 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 "src/sksl/SkSLCompiler.h"
#include "tests/Test.h"
static void test_failure(skiatest::Reporter* r, const char* src, const char* error) {
sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
SkSL::Compiler compiler(caps.get());
SkSL::Program::Settings settings;
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
SkSL::String(src), settings);
if (program) {
SkSL::String ignored;
compiler.toSPIRV(*program, &ignored);
}
SkSL::String skError(error);
if (compiler.errorText() != skError) {
SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
compiler.errorText().c_str());
}
REPORTER_ASSERT(r, compiler.errorText() == skError);
}
DEF_TEST(SkSLSPIRVBadOffset, r) {
test_failure(r,
"struct Bad { layout (offset = 5) int x; } bad; void main() { bad.x = 5; "
"sk_FragColor.r = half(bad.x); }",
"error: 1: offset of field 'x' must be a multiple of 4\n1 error\n");
test_failure(r,
"struct Bad { int x; layout (offset = 0) int y; } bad; void main() { bad.x = 5; "
"sk_FragColor.r = half(bad.x); }",
"error: 1: offset of field 'y' must be at least 4\n1 error\n");
}
DEF_TEST(SkSLSPIRVBadTypeInStruct, r) {
test_failure(r,
"struct Bad { sampler x; }; uniform Bad b; void main() {}",
"error: 1: opaque type 'sampler' is not permitted in a struct\n1 error\n");
}
DEF_TEST(SkSLSPIRVBadTypeInInterfaceBlock, r) {
test_failure(r,
"Bad { sampler x; }; void main() {}",
"error: 1: opaque type 'sampler' is not permitted in an interface block\n"
"1 error\n");
}