Added DSL flags
This adds some basic flags to the DSL initialization which allows us to
toggle things like optimization and validation.
Change-Id: I35b10526af1678c88901eaacd5a2c4a9f5cd21a7
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/406896
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
diff --git a/include/sksl/DSLCore.h b/include/sksl/DSLCore.h
index 5b43ed1..62ac361 100644
--- a/include/sksl/DSLCore.h
+++ b/include/sksl/DSLCore.h
@@ -32,11 +32,22 @@
// shouldn't pollute the SkSL::dsl namespace with anything else.
using namespace SkSL::SwizzleComponent;
+enum DSLFlag {
+ kNo_Flag = 0,
+ kMangle_Flag = 1 << 0,
+ kOptimize_Flag = 1 << 1,
+ kValidate_Flag = 1 << 2,
+ kMarkVarsDeclared_Flag = 1 << 3,
+};
+
+constexpr int kDefaultDSLFlags = kMangle_Flag | kOptimize_Flag | kValidate_Flag;
+
/**
* Starts DSL output on the current thread using the specified compiler. This must be called
* prior to any other DSL functions.
*/
-void Start(SkSL::Compiler* compiler, SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment);
+void Start(SkSL::Compiler* compiler, SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment,
+ int flags = kDefaultDSLFlags);
/**
* Signals the end of DSL output. This must be called sometime between a call to Start() and the
diff --git a/src/sksl/dsl/DSLCore.cpp b/src/sksl/dsl/DSLCore.cpp
index cfaaecf..3c71b18 100644
--- a/src/sksl/dsl/DSLCore.cpp
+++ b/src/sksl/dsl/DSLCore.cpp
@@ -25,8 +25,8 @@
namespace dsl {
-void Start(SkSL::Compiler* compiler, ProgramKind kind) {
- DSLWriter::SetInstance(std::make_unique<DSLWriter>(compiler, kind));
+void Start(SkSL::Compiler* compiler, ProgramKind kind, int flags) {
+ DSLWriter::SetInstance(std::make_unique<DSLWriter>(compiler, kind, flags));
}
void End() {
diff --git a/src/sksl/dsl/priv/DSLWriter.cpp b/src/sksl/dsl/priv/DSLWriter.cpp
index 7af36d8..ed15fd8 100644
--- a/src/sksl/dsl/priv/DSLWriter.cpp
+++ b/src/sksl/dsl/priv/DSLWriter.cpp
@@ -31,8 +31,10 @@
namespace dsl {
-DSLWriter::DSLWriter(SkSL::Compiler* compiler, SkSL::ProgramKind kind)
- : fCompiler(compiler) {
+DSLWriter::DSLWriter(SkSL::Compiler* compiler, SkSL::ProgramKind kind, int flags)
+ : fCompiler(compiler)
+ , fMangle(flags & kMangle_Flag)
+ , fMarkVarsDeclared(flags & kMarkVarsDeclared_Flag) {
SkSL::ParsedModule module = fCompiler->moduleForProgramKind(kind);
fModifiersPool = std::make_unique<ModifiersPool>();
@@ -41,6 +43,8 @@
fConfig = std::make_unique<ProgramConfig>();
fConfig->fKind = kind;
+ fConfig->fSettings.fOptimize = flags & kOptimize_Flag;
+ fConfig->fSettings.fValidateSPIRV = flags & kValidate_Flag;
fOldConfig = fCompiler->fContext->fConfig;
fCompiler->fContext->fConfig = fConfig.get();
diff --git a/src/sksl/dsl/priv/DSLWriter.h b/src/sksl/dsl/priv/DSLWriter.h
index fa79fdd..8e8af4b 100644
--- a/src/sksl/dsl/priv/DSLWriter.h
+++ b/src/sksl/dsl/priv/DSLWriter.h
@@ -44,7 +44,7 @@
*/
class DSLWriter {
public:
- DSLWriter(SkSL::Compiler* compiler, SkSL::ProgramKind kind);
+ DSLWriter(SkSL::Compiler* compiler, SkSL::ProgramKind kind, int flags);
~DSLWriter();
@@ -242,7 +242,6 @@
friend class DSLCore;
friend class DSLVar;
- friend class ::AutoDSLContext;
};
} // namespace dsl
diff --git a/tests/SkSLDSLTest.cpp b/tests/SkSLDSLTest.cpp
index bd07534..874063e 100644
--- a/tests/SkSLDSLTest.cpp
+++ b/tests/SkSLDSLTest.cpp
@@ -18,6 +18,9 @@
using namespace SkSL::dsl;
+constexpr int kDefaultTestFlags = (kDefaultDSLFlags & ~kMangle_Flag) | kMarkVarsDeclared_Flag;
+constexpr int kNoDeclareTestFlags = kDefaultDSLFlags & ~kMangle_Flag;
+
/**
* In addition to issuing an automatic Start() and End(), disables mangling and optionally
* auto-declares variables during its lifetime. Variable auto-declaration simplifies testing so we
@@ -27,11 +30,9 @@
*/
class AutoDSLContext {
public:
- AutoDSLContext(GrGpu* gpu, bool markVarsDeclared = true,
+ AutoDSLContext(GrGpu* gpu, int flags = kDefaultTestFlags,
SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment) {
- Start(gpu->shaderCompiler(), kind);
- DSLWriter::Instance().fMangle = false;
- DSLWriter::Instance().fMarkVarsDeclared = markVarsDeclared;
+ Start(gpu->shaderCompiler(), kind, flags);
}
~AutoDSLContext() {
@@ -134,6 +135,28 @@
#define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b))
+DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFlags, r, ctxInfo) {
+ {
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kOptimize_Flag);
+ EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))), "true");
+
+ Var x(kInt_Type, "x");
+ EXPECT_EQUAL(Declare(x), "int x;");
+ }
+
+ {
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNo_Flag);
+ EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))),
+ "all(greaterThan(float4(1.0), float4(0.0)))");
+ }
+
+ {
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kMangle_Flag);
+ Var x(kInt_Type, "x");
+ EXPECT_EQUAL(Declare(x), "int _0_x;");
+ }
+}
+
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Expression e1 = Float(std::numeric_limits<float>::max());
@@ -1205,7 +1228,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
EXPECT_EQUAL(Block(), "{ }");
Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
EXPECT_EQUAL(Block(Declare(a), Declare(b), a = b), "{ int a = 1; int b = 2; (a = b); }");
@@ -1219,7 +1242,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Var i(kInt_Type, "i", 0);
DSLFunction(kVoid_Type, "success").define(
For(Declare(i), i < 10, ++i, Block(
@@ -1239,7 +1262,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Var i(kInt_Type, "i", 0);
DSLFunction(kVoid_Type, "success").define(
For(Declare(i), i < 10, ++i, Block(
@@ -1259,7 +1282,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Statement x = Declare(a);
EXPECT_EQUAL(x, "half4 a;");
@@ -1287,7 +1310,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclareGlobal, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Var x(kInt_Type, "x", 0);
DeclareGlobal(x);
Var y(kUniform_Modifier, kFloat2_Type, "y");
@@ -1319,7 +1342,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
EXPECT_EQUAL(For(Statement(), Expression(), Expression(), Block()),
"for (;;) {}");
@@ -1349,7 +1372,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Var coords(kFloat2_Type, "coords");
DSLFunction(kVoid_Type, "main", coords).define(
sk_FragColor() = Half4(coords, 0, 1)
@@ -1576,7 +1599,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kDefaultTestFlags);
Var a(kFloat4_Type, "a");
EXPECT_EQUAL(a.x(),
@@ -1609,7 +1632,7 @@
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
// We should be able to convert `a` into a proper var by swapping it, even from within a scope.
Var a;
@@ -1721,7 +1744,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Statement d1 = Declare(v1);
@@ -1853,7 +1876,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleFragmentProcessor, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kDefaultTestFlags,
SkSL::ProgramKind::kFragmentProcessor);
DSLVar child(kUniform_Modifier, kFragmentProcessor_Type, "child");
EXPECT_EQUAL(Sample(child), "sample(child)");
@@ -1868,7 +1891,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kDefaultTestFlags,
SkSL::ProgramKind::kRuntimeShader);
DSLVar shader(kUniform_Modifier, kShader_Type, "shader");
EXPECT_EQUAL(Sample(shader, Float2(0, 0)), "sample(shader, float2(0.0, 0.0))");
@@ -1880,7 +1903,7 @@
}
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
- AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
+ AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
DSLType simpleStruct = Struct("SimpleStruct",
Field(kFloat_Type, "x"),