diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp index 999df56851..dd043b304e 100644 --- a/src/sksl/SkSLIRGenerator.cpp +++ b/src/sksl/SkSLIRGenerator.cpp @@ -291,6 +291,7 @@ std::unique_ptr IRGenerator::convertVarDeclarations(const ASTVa } value = this->coerce(std::move(value), *type); var->fWriteCount = 1; + var->fInitialValue = value.get(); } if (storage == Variable::kGlobal_Storage && varDecl.fName == "sk_FragColor" && (*fSymbolTable)[varDecl.fName]) { diff --git a/src/sksl/ir/SkSLConstructor.h b/src/sksl/ir/SkSLConstructor.h index cea5265cea..b8824ba3d2 100644 --- a/src/sksl/ir/SkSLConstructor.h +++ b/src/sksl/ir/SkSLConstructor.h @@ -146,15 +146,11 @@ struct Constructor : public Expression { } double getFVecComponent(int index) const { - const Expression& c = this->getVecComponent(index); - ASSERT(c.fKind == Expression::kFloatLiteral_Kind); - return ((FloatLiteral&) c).fValue; + return this->getVecComponent(index).getConstantFloat(); } int64_t getIVecComponent(int index) const { - const Expression& c = this->getVecComponent(index); - ASSERT(c.fKind == Expression::kIntLiteral_Kind); - return ((IntLiteral&) c).fValue; + return this->getVecComponent(index).getConstantInt(); } // null return should be interpreted as zero diff --git a/src/sksl/ir/SkSLExpression.h b/src/sksl/ir/SkSLExpression.h index 555e66061d..52fa5d297f 100644 --- a/src/sksl/ir/SkSLExpression.h +++ b/src/sksl/ir/SkSLExpression.h @@ -66,6 +66,22 @@ struct Expression : public IRNode { ABORT("cannot call compareConstant on this type"); } + /** + * For an expression which evaluates to a constant int, returns the value. Otherwise calls + * ABORT. + */ + virtual int64_t getConstantInt() const { + ABORT("not a constant int"); + } + + /** + * For an expression which evaluates to a constant float, returns the value. Otherwise calls + * ABORT. + */ + virtual double getConstantFloat() const { + ABORT("not a constant float"); + } + /** * Returns true if evaluating the expression potentially has side effects. Expressions may never * return false if they actually have side effects, but it is legal (though suboptimal) to diff --git a/src/sksl/ir/SkSLFloatLiteral.h b/src/sksl/ir/SkSLFloatLiteral.h index e6a3062f36..82c15c032b 100644 --- a/src/sksl/ir/SkSLFloatLiteral.h +++ b/src/sksl/ir/SkSLFloatLiteral.h @@ -39,6 +39,10 @@ struct FloatLiteral : public Expression { return fValue == f.fValue; } + double getConstantFloat() const override { + return fValue; + } + const double fValue; typedef Expression INHERITED; diff --git a/src/sksl/ir/SkSLIntLiteral.h b/src/sksl/ir/SkSLIntLiteral.h index da2b4082de..50337bfe6f 100644 --- a/src/sksl/ir/SkSLIntLiteral.h +++ b/src/sksl/ir/SkSLIntLiteral.h @@ -47,6 +47,10 @@ struct IntLiteral : public Expression { return INHERITED::coercionCost(target); } + int64_t getConstantInt() const override { + return fValue; + } + const int64_t fValue; typedef Expression INHERITED;