Skip to content

Commit

Permalink
Revert "Revert "SkSL now supports null child processors""
Browse files Browse the repository at this point in the history
This reverts commit bce7d86.

Bug: skia:
Change-Id: I3ccaf47b3d4b9dcb9e049671117183a374526032
Reviewed-on: https://skia-review.googlesource.com/c/194185
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
  • Loading branch information
Ethan Nicholas authored and Skia Commit-Bot committed Feb 22, 2019
1 parent 587e419 commit ee1c8a7
Show file tree
Hide file tree
Showing 26 changed files with 741 additions and 505 deletions.
8 changes: 0 additions & 8 deletions gn/run_sksllex.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@
shell=True)
subprocess.check_call(clangFormat + " -i \"" + src +
"/sksl/SkSLLexer.cpp\"", shell=True)
subprocess.check_output([sksllex, src + "/sksl/lex/layout.lex",
"LayoutLexer", "LayoutToken",
src + "/sksl/SkSLLayoutLexer.h",
src + "/sksl/SkSLLayoutLexer.cpp"])
subprocess.check_call(clangFormat + " -i \"" + src +
"/sksl/SkSLLayoutLexer.h\"", shell=True)
subprocess.check_call(clangFormat + " -i \"" + src +
"/sksl/SkSLLayoutLexer.cpp\"", shell=True)
except subprocess.CalledProcessError as err:
print("### Lexer error:")
print(err.output)
Expand Down
13 changes: 8 additions & 5 deletions src/gpu/gradients/GrClampedGradientEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,19 @@ class GrGLSLClampedGradientEffect : public GrGLSLFragmentProcessor {
args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
kDefault_GrSLPrecision, "rightBorderColor");
SkString _child1("_child1");
this->emitChild(1, &_child1, args);
this->emitChild(_outer.gradLayout_index(), &_child1, args);
fragBuilder->codeAppendf(
"half4 t = %s;\nif (!%s && t.y < 0.0) {\n %s = half4(0.0);\n} else if (t.x < "
"0.0) {\n %s = %s;\n} else if (t.x > 1.0) {\n %s = %s;\n} else {",
_child1.c_str(),
(_outer.childProcessor(1).preservesOpaqueInput() ? "true" : "false"),
(_outer.childProcessor(_outer.gradLayout_index()).preservesOpaqueInput() ? "true"
: "false"),
args.fOutputColor, args.fOutputColor,
args.fUniformHandler->getUniformCStr(fLeftBorderColorVar), args.fOutputColor,
args.fUniformHandler->getUniformCStr(fRightBorderColorVar));
SkString _input0("t");
SkString _child0("_child0");
this->emitChild(0, _input0.c_str(), &_child0, args);
this->emitChild(_outer.colorizer_index(), _input0.c_str(), &_child0, args);
fragBuilder->codeAppendf("\n %s = %s;\n}\n@if (%s) {\n %s.xyz *= %s.w;\n}\n",
args.fOutputColor, _child0.c_str(),
(_outer.makePremul() ? "true" : "false"), args.fOutputColor,
Expand Down Expand Up @@ -94,12 +95,14 @@ bool GrClampedGradientEffect::onIsEqual(const GrFragmentProcessor& other) const
}
GrClampedGradientEffect::GrClampedGradientEffect(const GrClampedGradientEffect& src)
: INHERITED(kGrClampedGradientEffect_ClassID, src.optimizationFlags())
, fColorizer_index(src.fColorizer_index)
, fGradLayout_index(src.fGradLayout_index)
, fLeftBorderColor(src.fLeftBorderColor)
, fRightBorderColor(src.fRightBorderColor)
, fMakePremul(src.fMakePremul)
, fColorsAreOpaque(src.fColorsAreOpaque) {
this->registerChildProcessor(src.childProcessor(0).clone());
this->registerChildProcessor(src.childProcessor(1).clone());
this->registerChildProcessor(src.childProcessor(fColorizer_index).clone());
this->registerChildProcessor(src.childProcessor(fGradLayout_index).clone());
}
std::unique_ptr<GrFragmentProcessor> GrClampedGradientEffect::clone() const {
return std::unique_ptr<GrFragmentProcessor>(new GrClampedGradientEffect(*this));
Expand Down
8 changes: 8 additions & 0 deletions src/gpu/gradients/GrClampedGradientEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "GrCoordTransform.h"
class GrClampedGradientEffect : public GrFragmentProcessor {
public:
int colorizer_index() const { return fColorizer_index; }
int gradLayout_index() const { return fGradLayout_index; }
const SkPMColor4f& leftBorderColor() const { return fLeftBorderColor; }
const SkPMColor4f& rightBorderColor() const { return fRightBorderColor; }
bool makePremul() const { return fMakePremul; }
Expand Down Expand Up @@ -45,13 +47,19 @@ class GrClampedGradientEffect : public GrFragmentProcessor {
, fRightBorderColor(rightBorderColor)
, fMakePremul(makePremul)
, fColorsAreOpaque(colorsAreOpaque) {
SkASSERT(colorizer);
fColorizer_index = this->numChildProcessors();
this->registerChildProcessor(std::move(colorizer));
SkASSERT(gradLayout);
fGradLayout_index = this->numChildProcessors();
this->registerChildProcessor(std::move(gradLayout));
}
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
bool onIsEqual(const GrFragmentProcessor&) const override;
GR_DECLARE_FRAGMENT_PROCESSOR_TEST
int fColorizer_index = -1;
int fGradLayout_index = -1;
SkPMColor4f fLeftBorderColor;
SkPMColor4f fRightBorderColor;
bool fMakePremul;
Expand Down
13 changes: 8 additions & 5 deletions src/gpu/gradients/GrTiledGradientEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ class GrGLSLTiledGradientEffect : public GrGLSLFragmentProcessor {
auto colorsAreOpaque = _outer.colorsAreOpaque();
(void)colorsAreOpaque;
SkString _child1("_child1");
this->emitChild(1, &_child1, args);
this->emitChild(_outer.gradLayout_index(), &_child1, args);
fragBuilder->codeAppendf(
"half4 t = %s;\nif (!%s && t.y < 0.0) {\n %s = half4(0.0);\n} else {\n @if "
"(%s) {\n half t_1 = t.x - 1.0;\n half tiled_t = (t_1 - 2.0 * "
"floor(t_1 * 0.5)) - 1.0;\n if (sk_Caps.mustDoOpBetweenFloorAndAbs) {\n "
" tiled_t = clamp(tiled_t, -1.0, 1.0);\n }\n t.x = "
"abs(tiled_t);\n } else {\n t.x = fract(t.x);\n }",
_child1.c_str(),
(_outer.childProcessor(1).preservesOpaqueInput() ? "true" : "false"),
(_outer.childProcessor(_outer.gradLayout_index()).preservesOpaqueInput() ? "true"
: "false"),
args.fOutputColor, (_outer.mirror() ? "true" : "false"));
SkString _input0("t");
SkString _child0("_child0");
this->emitChild(0, _input0.c_str(), &_child0, args);
this->emitChild(_outer.colorizer_index(), _input0.c_str(), &_child0, args);
fragBuilder->codeAppendf("\n %s = %s;\n}\n@if (%s) {\n %s.xyz *= %s.w;\n}\n",
args.fOutputColor, _child0.c_str(),
(_outer.makePremul() ? "true" : "false"), args.fOutputColor,
Expand Down Expand Up @@ -70,11 +71,13 @@ bool GrTiledGradientEffect::onIsEqual(const GrFragmentProcessor& other) const {
}
GrTiledGradientEffect::GrTiledGradientEffect(const GrTiledGradientEffect& src)
: INHERITED(kGrTiledGradientEffect_ClassID, src.optimizationFlags())
, fColorizer_index(src.fColorizer_index)
, fGradLayout_index(src.fGradLayout_index)
, fMirror(src.fMirror)
, fMakePremul(src.fMakePremul)
, fColorsAreOpaque(src.fColorsAreOpaque) {
this->registerChildProcessor(src.childProcessor(0).clone());
this->registerChildProcessor(src.childProcessor(1).clone());
this->registerChildProcessor(src.childProcessor(fColorizer_index).clone());
this->registerChildProcessor(src.childProcessor(fGradLayout_index).clone());
}
std::unique_ptr<GrFragmentProcessor> GrTiledGradientEffect::clone() const {
return std::unique_ptr<GrFragmentProcessor>(new GrTiledGradientEffect(*this));
Expand Down
8 changes: 8 additions & 0 deletions src/gpu/gradients/GrTiledGradientEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "GrCoordTransform.h"
class GrTiledGradientEffect : public GrFragmentProcessor {
public:
int colorizer_index() const { return fColorizer_index; }
int gradLayout_index() const { return fGradLayout_index; }
bool mirror() const { return fMirror; }
bool makePremul() const { return fMakePremul; }
bool colorsAreOpaque() const { return fColorsAreOpaque; }
Expand All @@ -41,13 +43,19 @@ class GrTiledGradientEffect : public GrFragmentProcessor {
, fMirror(mirror)
, fMakePremul(makePremul)
, fColorsAreOpaque(colorsAreOpaque) {
SkASSERT(colorizer);
fColorizer_index = this->numChildProcessors();
this->registerChildProcessor(std::move(colorizer));
SkASSERT(gradLayout);
fGradLayout_index = this->numChildProcessors();
this->registerChildProcessor(std::move(gradLayout));
}
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
bool onIsEqual(const GrFragmentProcessor&) const override;
GR_DECLARE_FRAGMENT_PROCESSOR_TEST
int fColorizer_index = -1;
int fGradLayout_index = -1;
bool fMirror;
bool fMakePremul;
bool fColorsAreOpaque;
Expand Down
4 changes: 4 additions & 0 deletions src/sksl/README
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ Within an '.fp' fragment processor file:
The first variant emits the child with a solid white input color. The second
variant emits the child with the result of the 2nd argument's expression,
which must evaluate to a half4. The process function returns a half4.
* By default, fragment processors must be non-null. The type for a nullable
fragment processor is 'fragmentProcessor?', as in
'in fragmentProcessor? <name>'. You can check for the presence of such a
fragment processor by comparing it to 'null'.


Creating a new .fp file
Expand Down
1 change: 1 addition & 0 deletions src/sksl/SkSLCFGGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ void CFGGenerator::addExpression(CFG& cfg, std::unique_ptr<Expression>* e, bool
case Expression::kBoolLiteral_Kind: // fall through
case Expression::kFloatLiteral_Kind: // fall through
case Expression::kIntLiteral_Kind: // fall through
case Expression::kNullLiteral_Kind: // fall through
case Expression::kSetting_Kind: // fall through
case Expression::kVariableReference_Kind:
cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind,
Expand Down
89 changes: 66 additions & 23 deletions src/sksl/SkSLCPPCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
if (precedence >= parentPrecedence) {
this->write(")");
}
} else if (b.fLeft->fKind == Expression::kNullLiteral_Kind ||
b.fRight->fKind == Expression::kNullLiteral_Kind) {
const Variable* var;
if (b.fLeft->fKind != Expression::kNullLiteral_Kind) {
SkASSERT(b.fLeft->fKind == Expression::kVariableReference_Kind);
var = &((VariableReference&) *b.fLeft).fVariable;
} else {
SkASSERT(b.fRight->fKind == Expression::kVariableReference_Kind);
var = &((VariableReference&) *b.fRight).fVariable;
}
SkASSERT(var->fType.kind() == Type::kNullable_Kind &&
var->fType.componentType() == *fContext.fFragmentProcessor_Type);
this->write("%s");
const char* op;
switch (b.fOperator) {
case Token::EQEQ:
op = "<";
break;
case Token::NEQ:
op = ">=";
break;
default:
SkASSERT(false);
}
fFormatArgs.push_back("_outer." + String(var->fName) + "_index() " + op + " 0 ? \"true\" "
": \"false\"");
} else {
INHERITED::writeBinaryExpression(b, parentPrecedence);
}
Expand Down Expand Up @@ -333,9 +359,10 @@ void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
}

const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
int index = getChildFPIndex((const VariableReference&) *access.fBase);
String cppAccess = String::printf("_outer.childProcessor(%s).%s()",
to_string(index).c_str(), String(field.fName).c_str());
const Variable& var = ((const VariableReference&) *access.fBase).fVariable;
String cppAccess = String::printf("_outer.childProcessor(_outer.%s_index()).%s()",
String(var.fName).c_str(),
String(field.fName).c_str());

if (fCPPMode) {
this->write(cppAccess.c_str());
Expand All @@ -347,17 +374,17 @@ void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
INHERITED::writeFieldAccess(access);
}

int CPPCodeGenerator::getChildFPIndex(const VariableReference& reference) const {
int CPPCodeGenerator::getChildFPIndex(const Variable& var) const {
int index = 0;
bool found = false;
for (const auto& p : fProgram) {
if (ProgramElement::kVar_Kind == p.fKind) {
const VarDeclarations& decls = (const VarDeclarations&) p;
for (const auto& raw : decls.fVars) {
const VarDeclaration& decl = (VarDeclaration&) *raw;
if (decl.fVar == &reference.fVariable) {
if (decl.fVar == &var) {
found = true;
} else if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
} else if (decl.fVar->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
++index;
}
}
Expand Down Expand Up @@ -388,7 +415,8 @@ void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
// Second argument must also be a half4 expression
SkASSERT("half4" == c.fArguments[1]->fType.name());
}
int index = getChildFPIndex((const VariableReference&) *c.fArguments[0]);
const Variable& child = ((const VariableReference&) *c.fArguments[0]).fVariable;
int index = getChildFPIndex(child);

// Start a new extra emit code section so that the emitted child processor can depend on
// sksl variables defined in earlier sksl code.
Expand All @@ -412,9 +440,14 @@ void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
// Write the output handling after the possible input handling
String childName = "_child" + to_string(index);
addExtraEmitCodeLine("SkString " + childName + "(\"" + childName + "\");");
addExtraEmitCodeLine("this->emitChild(" + to_string(index) + inputArg +
", &" + childName + ", args);");

if (c.fArguments[0]->fType.kind() == Type::kNullable_Kind) {
addExtraEmitCodeLine("if (_outer." + String(child.fName) + "_index() >= 0) {\n ");
}
addExtraEmitCodeLine("this->emitChild(_outer." + String(child.fName) + "_index()" +
inputArg + ", &" + childName + ", args);");
if (c.fArguments[0]->fType.kind() == Type::kNullable_Kind) {
addExtraEmitCodeLine("}");
}
this->write("%s");
fFormatArgs.push_back(childName + ".c_str()");
return;
Expand Down Expand Up @@ -597,8 +630,9 @@ void CPPCodeGenerator::writePrivateVarValues() {
}

static bool is_accessible(const Variable& var) {
return Type::kSampler_Kind != var.fType.kind() &&
Type::kOther_Kind != var.fType.kind();
const Type& type = var.fType.nonnullable();
return Type::kSampler_Kind != type.kind() &&
Type::kOther_Kind != type.kind();
}

void CPPCodeGenerator::newExtraEmitCodeBlock() {
Expand Down Expand Up @@ -991,13 +1025,16 @@ void CPPCodeGenerator::writeClone() {
": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
if (param->fType == *fContext.fFragmentProcessor_Type) {
continue;
}
String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
this->writef("\n, %s(src.%s)",
fieldName.c_str(),
fieldName.c_str());
if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
this->writef("\n, %s_index(src.%s_index)",
fieldName.c_str(),
fieldName.c_str());
} else {
this->writef("\n, %s(src.%s)",
fieldName.c_str(),
fieldName.c_str());
}
}
const auto transforms = fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION);
for (size_t i = 0; i < transforms.size(); ++i) {
Expand All @@ -1006,14 +1043,20 @@ void CPPCodeGenerator::writeClone() {
this->writef("\n, %s(src.%s)", fieldName.c_str(), fieldName.c_str());
}
this->writef(" {\n");
int childCount = 0;
int samplerCount = 0;
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
if (param->fType.kind() == Type::kSampler_Kind) {
++samplerCount;
} else if (param->fType == *fContext.fFragmentProcessor_Type) {
this->writef(" this->registerChildProcessor(src.childProcessor(%d).clone());"
"\n", childCount++);
} else if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
if (param->fType.kind() == Type::kNullable_Kind) {
this->writef(" if (%s_index >= 0) {\n ", fieldName.c_str());
}
this->writef(" this->registerChildProcessor(src.childProcessor(%s_index)."
"clone());\n", fieldName.c_str());
if (param->fType.kind() == Type::kNullable_Kind) {
this->writef(" }\n");
}
}
}
if (samplerCount) {
Expand Down Expand Up @@ -1182,7 +1225,7 @@ bool CPPCodeGenerator::generateCode() {
" (void) that;\n",
fullName, fullName, fullName);
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
if (param->fType == *fContext.fFragmentProcessor_Type) {
if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
continue;
}
String nameString(param->fName);
Expand Down
2 changes: 1 addition & 1 deletion src/sksl/SkSLCPPCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class CPPCodeGenerator : public GLSLCodeGenerator {
// Append CPP code to the current extra emit code block.
void addExtraEmitCodeLine(const String& toAppend);

int getChildFPIndex(const VariableReference& reference) const;
int getChildFPIndex(const Variable& var) const;

String fName;
String fFullName;
Expand Down
2 changes: 2 additions & 0 deletions src/sksl/SkSLContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Context {
Context()
: fInvalid_Type(new Type("<INVALID>"))
, fVoid_Type(new Type("void"))
, fNull_Type(new Type("null"))
, fFloatLiteral_Type(new Type("$floatLiteral", Type::kFloat_NumberKind, 3))
, fIntLiteral_Type(new Type("$intLiteral", Type::kSigned_NumberKind, 1))
, fDouble_Type(new Type("double", Type::kFloat_NumberKind, 6))
Expand Down Expand Up @@ -208,6 +209,7 @@ class Context {

const std::unique_ptr<Type> fInvalid_Type;
const std::unique_ptr<Type> fVoid_Type;
const std::unique_ptr<Type> fNull_Type;
const std::unique_ptr<Type> fFloatLiteral_Type;
const std::unique_ptr<Type> fIntLiteral_Type;

Expand Down

0 comments on commit ee1c8a7

Please sign in to comment.