diegp.fInPosition->fName,
args.fFPCoordTransformHandler);
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
// for outer curve
fragBuilder->codeAppendf("vec2 scaledOffset = %s.xy;", offsets0.fsIn());
fragBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
qe.localMatrix(),
args.fFPCoordTransformHandler);
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
fragBuilder->codeAppendf("float edgeAlpha;");
// keep the derivative instructions outside the conditional
GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder;
SkAssertResult(fsBuilder->enableFeature(
GrGLSLFragmentShaderBuilder::kPixelLocalStorage_GLSLFeature));
- SkAssertResult(fsBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
fsBuilder->declAppendf(GR_GL_PLS_PATH_DATA_DECL);
// Compute four subsamples, each shifted a quarter pixel along x and y from
// gl_FragCoord. The oriented box positioning of the subsamples is of course not
GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder;
SkAssertResult(fsBuilder->enableFeature(
GrGLSLFragmentShaderBuilder::kPixelLocalStorage_GLSLFeature));
- SkAssertResult(fsBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
static const int QUAD_ARGS = 2;
GrGLSLShaderVar inQuadArgs[QUAD_ARGS] = {
GrGLSLShaderVar("dot", kFloat_GrSLType, 0, kHigh_GrSLPrecision),
switch (fEdgeType) {
case kHairlineAA_GrProcessorEdgeType: {
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
fragBuilder->codeAppendf("%s = 2.0 * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
break;
}
case kFillAA_GrProcessorEdgeType: {
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
fragBuilder->codeAppendf("%s ="
switch (fEdgeType) {
case kHairlineAA_GrProcessorEdgeType: {
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
fragBuilder->codeAppendf("vec2 duvdx = dFdx(%s.xy);", v.fsIn());
fragBuilder->codeAppendf("vec2 duvdy = dFdy(%s.xy);", v.fsIn());
fragBuilder->codeAppendf("vec2 gF = vec2(2.0 * %s.x * duvdx.x - duvdx.y,"
break;
}
case kFillAA_GrProcessorEdgeType: {
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
fragBuilder->codeAppendf("vec2 duvdx = dFdx(%s.xy);", v.fsIn());
fragBuilder->codeAppendf("vec2 duvdy = dFdy(%s.xy);", v.fsIn());
fragBuilder->codeAppendf("vec2 gF = vec2(2.0 * %s.x * duvdx.x - duvdx.y,"
switch (fEdgeType) {
case kHairlineAA_GrProcessorEdgeType: {
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
fragBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
break;
}
case kFillAA_GrProcessorEdgeType: {
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
fragBuilder->codeAppendf("%s ="
const GrDistanceFieldA8TextGeoProc& dfTexEffect =
args.fGP.cast<GrDistanceFieldA8TextGeoProc>();
GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
const GrDistanceFieldPathGeoProc& dfTexEffect = args.fGP.cast<GrDistanceFieldPathGeoProc>();
GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
// add frag shader code
- SkAssertResult(fragBuilder->enableFeature(
- GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
-
// create LCD offset adjusted by inverse of transform
// Use highp to work around aliasing issues
fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision);
result.fIsCoreProfile = caps->isCoreProfile();
result.fUsesPrecisionModifiers = glslCaps->usesPrecisionModifiers();
result.fMustDeclareFragmentShaderOutput = glslCaps->mustDeclareFragmentShaderOutput();
+ result.fShaderDerivativeSupport = glslCaps->shaderDerivativeSupport();
+ if (glslCaps->shaderDerivativeExtensionString()) {
+ result.fShaderDerivativeExtensionString = glslCaps->shaderDerivativeExtensionString();
+ }
result.fCanUseMinAndAbsTogether = glslCaps->canUseMinAndAbsTogether();
result.fMustForceNegatedAtanParamToFloat = glslCaps->mustForceNegatedAtanParamToFloat();
return result;
bool GrGLSLFragmentShaderBuilder::enableFeature(GLSLFeature feature) {
const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps();
switch (feature) {
- case kStandardDerivatives_GLSLFeature:
- if (!glslCaps.shaderDerivativeSupport()) {
- return false;
- }
- if (const char* extension = glslCaps.shaderDerivativeExtensionString()) {
- this->addFeature(1 << kStandardDerivatives_GLSLFeature, extension);
- }
- return true;
case kPixelLocalStorage_GLSLFeature:
if (glslCaps.pixelLocalStorageSize() <= 0) {
return false;
* if code is added that uses one of these features without calling enableFeature()
*/
enum GLSLFeature {
- kStandardDerivatives_GLSLFeature = kLastGLSLPrivateFeature + 1,
- kPixelLocalStorage_GLSLFeature,
+ kPixelLocalStorage_GLSLFeature = kLastGLSLPrivateFeature + 1,
kMultisampleInterpolation_GLSLFeature
};
if (arcTest && fBatchInfo.fHasPerspective) {
// The non-perspective version accounts for fwidth() in the vertex shader.
// We make sure to take the derivative here, before a neighbor pixel may early accept.
- f->enableFeature(GrGLSLPPFragmentBuilder::kStandardDerivatives_GLSLFeature);
f->appendPrecisionModifier(kHigh_GrSLPrecision);
f->codeAppendf("vec2 arcTest = %s - 0.5 * fwidth(%s);",
fArcTest.fsIn(), fArcTest.fsIn());
#include "ir/SkSLIntLiteral.h"
#include "ir/SkSLModifiersDeclaration.h"
#include "ir/SkSLSymbolTable.h"
+#include "ir/SkSLUnresolvedFunction.h"
#include "ir/SkSLVarDeclarations.h"
#include "SkMutex.h"
Modifiers::Flag ignored1;
std::vector<std::unique_ptr<ProgramElement>> ignored2;
this->internalConvertProgram(SKSL_INCLUDE, &ignored1, &ignored2);
+ fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
ASSERT(!fErrorCount);
}
this->internalConvertProgram(SKSL_FRAG_INCLUDE, &ignored, &elements);
break;
}
+ fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
Modifiers::Flag defaultPrecision;
this->internalConvertProgram(text, &defaultPrecision, &elements);
auto result = std::unique_ptr<Program>(new Program(kind, defaultPrecision, std::move(elements),
- fIRGenerator->fSymbolTable));;
+ fIRGenerator->fSymbolTable));
fIRGenerator->popSymbolTable();
this->writeErrorCount();
return result;
}
void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
- if (!fCaps.fCanUseMinAndAbsTogether && c.fFunction.fName == "min") {
+ if (!fCaps.fCanUseMinAndAbsTogether && 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]);
}
}
if (fCaps.fMustForceNegatedAtanParamToFloat && c.fFunction.fName == "atan" &&
- c.fArguments.size() == 2 && c.fArguments[1]->fKind == Expression::kPrefix_Kind) {
+ c.fFunction.fBuiltin && c.fArguments.size() == 2 &&
+ c.fArguments[1]->fKind == Expression::kPrefix_Kind) {
const PrefixExpression& p = (PrefixExpression&) *c.fArguments[1];
if (p.fOperator == Token::MINUS) {
this->write("atan(");
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";
+ fFoundDerivatives = true;
+ }
this->write(c.fFunction.fName + "(");
const char* separator = "";
for (const auto& arg : c.fArguments) {
void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out) {
ASSERT(fOut == nullptr);
- fOut = &out;
+ fOut = &fHeader;
fProgramKind = program.fKind;
this->write("#version " + to_string(fCaps.fVersion));
if (fCaps.fStandard == GLCaps::kGLES_Standard && fCaps.fVersion >= 300) {
}
this->writeLine(" float;");
}
+ std::stringstream body;
+ fOut = &body;
for (const auto& e : program.fElements) {
switch (e->fKind) {
case ProgramElement::kExtension_Kind:
}
}
fOut = nullptr;
+
+ out << fHeader.str();
+ out << body.str();
}
}
#define kLast_Capability SpvCapabilityMultiViewport
struct GLCaps {
- int fVersion;
+ GLCaps() {}
+
+ int fVersion = 400;
enum {
kGL_Standard,
kGLES_Standard
- } fStandard;
- bool fIsCoreProfile;
- bool fUsesPrecisionModifiers;
- bool fMustDeclareFragmentShaderOutput;
+ } 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;
+ 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;
+ bool fMustForceNegatedAtanParamToFloat = false;
};
/**
GLSLCodeGenerator(const Context* context, GLCaps caps)
: fContext(*context)
- , fCaps(caps)
- , fOut(nullptr)
- , fVarCount(0)
- , fIndentation(0)
- , fAtLineStart(true) {}
+ , fCaps(caps) {}
void generateCode(const Program& program, std::ostream& out) override;
const Context& fContext;
const GLCaps fCaps;
- std::ostream* fOut;
+ std::ostream* fOut = nullptr;
+ std::stringstream fHeader;
std::string fFunctionHeader;
Program::Kind fProgramKind;
- int fVarCount;
- int fIndentation;
- bool fAtLineStart;
+ int fVarCount = 0;
+ int fIndentation = 0;
+ bool fAtLineStart = false;
// Keeps track of which struct types we have written. Given that we are unlikely to ever write
// more than one or two structs per shader, a simple linear search will be faster than anything
// fancier.
std::vector<const Type*> fWrittenStructs;
+ // true if we have run into usages of dFdx / dFdy
+ bool fFoundDerivatives = false;
};
}
return false;
}
-static SkSL::GLCaps default_caps() {
- return {
- 400,
- SkSL::GLCaps::kGL_Standard,
- false, // isCoreProfile
- false, // usesPrecisionModifiers;
- false, // mustDeclareFragmentShaderOutput
- true, // canUseMinAndAbsTogether
- false // mustForceNegatedAtanParamToFloat
- };
-}
-
/**
* Very simple standalone executable to facilitate testing.
*/
} else if (endsWith(name, ".glsl")) {
std::ofstream out(argv[2], std::ofstream::binary);
SkSL::Compiler compiler;
- if (!compiler.toGLSL(kind, text, default_caps(), out)) {
+ if (!compiler.toGLSL(kind, text, SkSL::GLCaps(), out)) {
printf("%s", compiler.errorText().c_str());
exit(3);
}
std::vector<const Variable*> parameters, const Type& returnType)
: INHERITED(position, kFunctionDeclaration_Kind, std::move(name))
, fDefined(false)
+ , fBuiltin(false)
, fParameters(std::move(parameters))
, fReturnType(returnType) {}
}
mutable bool fDefined;
+ bool fBuiltin;
const std::vector<const Variable*> fParameters;
const Type& fReturnType;
}
}
+
+void SymbolTable::markAllFunctionsBuiltin() {
+ for (const auto& pair : fSymbols) {
+ switch (pair.second->fKind) {
+ case Symbol::kFunctionDeclaration_Kind:
+ ((FunctionDeclaration&) *pair.second).fBuiltin = true;
+ break;
+ case Symbol::kUnresolvedFunction_Kind:
+ for (auto& f : ((UnresolvedFunction&) *pair.second).fFunctions) {
+ ((FunctionDeclaration*) f)->fBuiltin = true;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+}
+
} // namespace
Symbol* takeOwnership(Symbol* s);
+ void markAllFunctionsBuiltin();
+
const std::shared_ptr<SymbolTable> fParent;
private:
}
static SkSL::GLCaps default_caps() {
- return {
- 400,
- SkSL::GLCaps::kGL_Standard,
- false, // isCoreProfile
- false, // usesPrecisionModifiers;
- false, // mustDeclareFragmentShaderOutput
- true, // canUseMinAndAbsTogether
- false // mustForceNegatedAtanParamToFloat
- };
+ return SkSL::GLCaps();
}
DEF_TEST(SkSLHelloWorld, r) {
"vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n"
"mat4 test3[] = mat4[]();\n");
}
+
+DEF_TEST(SkSLDerivatives, r) {
+ test(r,
+ "void main() { float x = dFdx(1); }",
+ default_caps(),
+ "#version 400\n"
+ "void main() {\n"
+ " float x = dFdx(1.0);\n"
+ "}\n");
+ SkSL::GLCaps caps = default_caps();
+ caps.fShaderDerivativeExtensionString = "GL_OES_standard_derivatives";
+ test(r,
+ "void main() { float x = 1; }",
+ caps,
+ "#version 400\n"
+ "void main() {\n"
+ " float x = 1.0;\n"
+ "}\n");
+ test(r,
+ "void main() { float x = dFdx(1); }",
+ caps,
+ "#version 400\n"
+ "#extension GL_OES_standard_derivatives : require\n"
+ "void main() {\n"
+ " float x = dFdx(1.0);\n"
+ "}\n");
+}