diff options
author | Ethan Nicholas <ethannicholas@google.com> | 2016-11-10 16:42:58 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2016-11-11 14:33:34 +0000 |
commit | 8af38a96475ac5ce83b20c16d9cf82bf1006b8c4 (patch) | |
tree | 1815e5aa6bd534f038e28fb17fee897eec540df6 /src/sksl | |
parent | c51a3a4e3c81a1d4100d5d29d31bf3a02eda2a7c (diff) |
skslc now uses standard Skia caps
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=4660
Change-Id: Idaedae3f81426b97f5052bb872cdf0610e47a84f
Reviewed-on: https://skia-review.googlesource.com/4660
Reviewed-by: Ben Wagner <benjaminwagner@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Diffstat (limited to 'src/sksl')
-rw-r--r-- | src/sksl/SkSLCompiler.cpp | 6 | ||||
-rw-r--r-- | src/sksl/SkSLCompiler.h | 6 | ||||
-rw-r--r-- | src/sksl/SkSLGLSLCodeGenerator.cpp | 35 | ||||
-rw-r--r-- | src/sksl/SkSLGLSLCodeGenerator.h | 29 | ||||
-rw-r--r-- | src/sksl/SkSLMain.cpp | 3 | ||||
-rw-r--r-- | src/sksl/SkSLUtil.h | 58 |
6 files changed, 85 insertions, 52 deletions
diff --git a/src/sksl/SkSLCompiler.cpp b/src/sksl/SkSLCompiler.cpp index c3adaea682..27fd662732 100644 --- a/src/sksl/SkSLCompiler.cpp +++ b/src/sksl/SkSLCompiler.cpp @@ -444,18 +444,18 @@ bool Compiler::toSPIRV(Program::Kind kind, const std::string& text, std::string* return result; } -bool Compiler::toGLSL(Program::Kind kind, const std::string& text, GLCaps caps, +bool Compiler::toGLSL(Program::Kind kind, const std::string& text, const GrGLSLCaps& caps, std::ostream& out) { auto program = this->convertProgram(kind, text); if (fErrorCount == 0) { - SkSL::GLSLCodeGenerator cg(&fContext, caps); + SkSL::GLSLCodeGenerator cg(&fContext, &caps); cg.generateCode(*program.get(), out); ASSERT(!out.rdstate()); } return fErrorCount == 0; } -bool Compiler::toGLSL(Program::Kind kind, const std::string& text, GLCaps caps, +bool Compiler::toGLSL(Program::Kind kind, const std::string& text, const GrGLSLCaps& caps, std::string* out) { std::stringstream buffer; bool result = this->toGLSL(kind, text, caps, buffer); diff --git a/src/sksl/SkSLCompiler.h b/src/sksl/SkSLCompiler.h index 275fc58b3f..e69db5997d 100644 --- a/src/sksl/SkSLCompiler.h +++ b/src/sksl/SkSLCompiler.h @@ -43,9 +43,11 @@ public: bool toSPIRV(Program::Kind kind, const std::string& text, std::string* out); - bool toGLSL(Program::Kind kind, const std::string& text, GLCaps caps, std::ostream& out); + bool toGLSL(Program::Kind kind, const std::string& text, const GrGLSLCaps& caps, + std::ostream& out); - bool toGLSL(Program::Kind kind, const std::string& text, GLCaps caps, std::string* out); + bool toGLSL(Program::Kind kind, const std::string& text, const GrGLSLCaps& caps, + std::string* out); void error(Position position, std::string msg) override; diff --git a/src/sksl/SkSLGLSLCodeGenerator.cpp b/src/sksl/SkSLGLSLCodeGenerator.cpp index 8a26f6a713..c6d2e6ed66 100644 --- a/src/sksl/SkSLGLSLCodeGenerator.cpp +++ b/src/sksl/SkSLGLSLCodeGenerator.cpp @@ -137,7 +137,7 @@ static bool is_abs(Expression& expr) { // turns min(abs(x), y) into ((tmpVar1 = abs(x)) < (tmpVar2 = y) ? tmpVar1 : tmpVar2) to avoid a // Tegra3 compiler bug. void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherExpr) { - ASSERT(!fCaps.fCanUseMinAndAbsTogether); + ASSERT(!fCaps.canUseMinAndAbsTogether()); std::string tmpVar1 = "minAbsHackVar" + to_string(fVarCount++); std::string tmpVar2 = "minAbsHackVar" + to_string(fVarCount++); this->fFunctionHeader += " " + absExpr.fType.name() + " " + tmpVar1 + ";\n"; @@ -150,7 +150,7 @@ void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherEx } void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { - if (!fCaps.fCanUseMinAndAbsTogether && c.fFunction.fName == "min" && c.fFunction.fBuiltin) { + if (!fCaps.canUseMinAndAbsTogether() && c.fFunction.fName == "min" && c.fFunction.fBuiltin) { ASSERT(c.fArguments.size() == 2); if (is_abs(*c.fArguments[0])) { this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]); @@ -163,7 +163,7 @@ void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { return; } } - if (fCaps.fMustForceNegatedAtanParamToFloat && c.fFunction.fName == "atan" && + if (fCaps.mustForceNegatedAtanParamToFloat() && c.fFunction.fName == "atan" && c.fFunction.fBuiltin && c.fArguments.size() == 2 && c.fArguments[1]->fKind == Expression::kPrefix_Kind) { const PrefixExpression& p = (PrefixExpression&) *c.fArguments[1]; @@ -176,10 +176,10 @@ void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { return; } } - if (!fFoundDerivatives && fCaps.fShaderDerivativeExtensionString != "" && - (c.fFunction.fName == "dFdx" || c.fFunction.fName == "dFdy") && c.fFunction.fBuiltin) { - ASSERT(fCaps.fShaderDerivativeSupport); - fHeader << "#extension " << fCaps.fShaderDerivativeExtensionString << " : require\n"; + if (!fFoundDerivatives && (c.fFunction.fName == "dFdx" || c.fFunction.fName == "dFdy") && + c.fFunction.fBuiltin && fCaps.shaderDerivativeExtensionString()) { + ASSERT(fCaps.shaderDerivativeSupport()); + fHeader << "#extension " << fCaps.shaderDerivativeExtensionString() << " : require\n"; fFoundDerivatives = true; } this->write(c.fFunction.fName + "("); @@ -205,7 +205,7 @@ void GLSLCodeGenerator::writeConstructor(const Constructor& c) { void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN) { - if (fCaps.fMustDeclareFragmentShaderOutput) { + if (fCaps.mustDeclareFragmentShaderOutput()) { this->write("sk_FragColor"); } else { this->write("gl_FragColor"); @@ -405,14 +405,14 @@ void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers, (modifiers.fFlags & Modifiers::kOut_Flag)) { this->write("inout "); } else if (modifiers.fFlags & Modifiers::kIn_Flag) { - if (globalContext && fCaps.fVersion < 130) { + if (globalContext && fCaps.generation() < GrGLSLGeneration::k130_GrGLSLGeneration) { this->write(fProgramKind == Program::kVertex_Kind ? "attribute " : "varying "); } else { this->write("in "); } } else if (modifiers.fFlags & Modifiers::kOut_Flag) { - if (globalContext && fCaps.fVersion < 130) { + if (globalContext && fCaps.generation() < GrGLSLGeneration::k130_GrGLSLGeneration) { this->write("varying "); } else { this->write("out "); @@ -424,7 +424,7 @@ void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers, if (modifiers.fFlags & Modifiers::kConst_Flag) { this->write("const "); } - if (fCaps.fUsesPrecisionModifiers) { + if (fCaps.usesPrecisionModifiers()) { if (modifiers.fFlags & Modifiers::kLowp_Flag) { this->write("lowp "); } @@ -587,12 +587,7 @@ void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out) ASSERT(fOut == nullptr); fOut = &fHeader; fProgramKind = program.fKind; - this->write("#version " + to_string(fCaps.fVersion)); - if (fCaps.fStandard == GLCaps::kGLES_Standard && fCaps.fVersion >= 300) { - this->write(" es"); - } else if (fCaps.fIsCoreProfile) { - this->write(" core"); - } + this->write(fCaps.versionDeclString()); this->writeLine(); for (const auto& e : program.fElements) { if (e->fKind == ProgramElement::kExtension_Kind) { @@ -601,7 +596,7 @@ void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out) } std::stringstream body; fOut = &body; - if (fCaps.fStandard == GLCaps::kGLES_Standard) { + if (fCaps.usesPrecisionModifiers()) { this->write("precision "); switch (program.fDefaultPrecision) { case Modifiers::kLowp_Flag: @@ -632,9 +627,9 @@ void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out) this->writeVarDeclarations(decl, true); this->writeLine(); } else if (builtin == SK_FRAGCOLOR_BUILTIN && - fCaps.fMustDeclareFragmentShaderOutput) { + fCaps.mustDeclareFragmentShaderOutput()) { this->write("out "); - if (fCaps.fUsesPrecisionModifiers) { + if (fCaps.usesPrecisionModifiers()) { this->write("mediump "); } this->writeLine("vec4 sk_FragColor;"); diff --git a/src/sksl/SkSLGLSLCodeGenerator.h b/src/sksl/SkSLGLSLCodeGenerator.h index 97e6038146..5ed6104a09 100644 --- a/src/sksl/SkSLGLSLCodeGenerator.h +++ b/src/sksl/SkSLGLSLCodeGenerator.h @@ -12,6 +12,7 @@ #include <tuple> #include <unordered_map> +#include "glsl/GrGLSLCaps.h" #include "SkSLCodeGenerator.h" #include "ir/SkSLBinaryExpression.h" #include "ir/SkSLBoolLiteral.h" @@ -44,28 +45,6 @@ namespace SkSL { #define kLast_Capability SpvCapabilityMultiViewport -struct GLCaps { - GLCaps() {} - - int fVersion = 400; - enum { - kGL_Standard, - kGLES_Standard - } fStandard = kGL_Standard; - bool fIsCoreProfile = false; - bool fUsesPrecisionModifiers = false; - bool fMustDeclareFragmentShaderOutput = false; - bool fShaderDerivativeSupport = true; - // extension string to enable derivative support, or null if unnecessary - std::string fShaderDerivativeExtensionString; - // The Tegra3 compiler will sometimes never return if we have min(abs(x), y) - bool fCanUseMinAndAbsTogether = true; - // On Intel GPU there is an issue where it misinterprets an atan argument (second argument only, - // apparently) of the form "-<expr>" as an int, so we rewrite it as "-1.0 * <expr>" to avoid - // this problem - bool fMustForceNegatedAtanParamToFloat = false; -}; - /** * Converts a Program into GLSL code. */ @@ -92,9 +71,9 @@ public: kTopLevel_Precedence = 18 }; - GLSLCodeGenerator(const Context* context, GLCaps caps) + GLSLCodeGenerator(const Context* context, const GrGLSLCaps* caps) : fContext(*context) - , fCaps(caps) {} + , fCaps(*caps) {} void generateCode(const Program& program, std::ostream& out) override; @@ -176,7 +155,7 @@ private: void writeReturnStatement(const ReturnStatement& r); const Context& fContext; - const GLCaps fCaps; + const GrGLSLCaps& fCaps; std::ostream* fOut = nullptr; std::stringstream fHeader; std::string fFunctionHeader; diff --git a/src/sksl/SkSLMain.cpp b/src/sksl/SkSLMain.cpp index eb07b4d032..3658992412 100644 --- a/src/sksl/SkSLMain.cpp +++ b/src/sksl/SkSLMain.cpp @@ -8,6 +8,7 @@ #include "stdio.h" #include <fstream> #include "SkSLCompiler.h" +#include "GrContextOptions.h" bool endsWith(const std::string& s, const std::string& ending) { if (s.length() >= ending.length()) { @@ -57,7 +58,7 @@ int main(int argc, const char** argv) { } else if (endsWith(name, ".glsl")) { std::ofstream out(argv[2], std::ofstream::binary); SkSL::Compiler compiler; - if (!compiler.toGLSL(kind, text, SkSL::GLCaps(), out)) { + if (!compiler.toGLSL(kind, text, *SkSL::GLSLCapsFactory::Default(), out)) { printf("%s", compiler.errorText().c_str()); exit(3); } diff --git a/src/sksl/SkSLUtil.h b/src/sksl/SkSLUtil.h index efffaaec01..ede21830e5 100644 --- a/src/sksl/SkSLUtil.h +++ b/src/sksl/SkSLUtil.h @@ -13,10 +13,66 @@ #include <sstream> #include "stdlib.h" #include "assert.h" -#include "SkTypes.h" +#include "SkRefCnt.h" +#include "SkTypes.h" +#include "glsl/GrGLSLCaps.h" +#include "GrContextOptions.h" namespace SkSL { +// Various sets of caps for use in tests +class GLSLCapsFactory { +public: + static sk_sp<GrGLSLCaps> Default() { + sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); + result->fVersionDeclString = "#version 400"; + result->fShaderDerivativeSupport = true; + return result; + } + + static sk_sp<GrGLSLCaps> Version450Core() { + sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); + result->fVersionDeclString = "#version 450 core"; + return result; + } + + static sk_sp<GrGLSLCaps> Version110() { + sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); + result->fVersionDeclString = "#version 110"; + result->fGLSLGeneration = GrGLSLGeneration::k110_GrGLSLGeneration; + return result; + } + + static sk_sp<GrGLSLCaps> UsesPrecisionModifiers() { + sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); + result->fVersionDeclString = "#version 400"; + result->fUsesPrecisionModifiers = true; + return result; + } + + static sk_sp<GrGLSLCaps> CannotUseMinAndAbsTogether() { + sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); + result->fVersionDeclString = "#version 400"; + result->fCanUseMinAndAbsTogether = false; + return result; + } + + static sk_sp<GrGLSLCaps> MustForceNegatedAtanParamToFloat() { + sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); + result->fVersionDeclString = "#version 400"; + result->fMustForceNegatedAtanParamToFloat = true; + return result; + } + + static sk_sp<GrGLSLCaps> ShaderDerivativeExtensionString() { + sk_sp<GrGLSLCaps> result = sk_make_sp<GrGLSLCaps>(GrContextOptions()); + result->fVersionDeclString = "#version 400"; + result->fShaderDerivativeSupport = true; + result->fShaderDerivativeExtensionString = "GL_OES_standard_derivatives"; + return result; + } +}; + // our own definitions of certain std:: functions, because they are not always present on Android std::string to_string(double value); |