From 135e2376563205533c4638a4ef15816b4eb022c1 Mon Sep 17 00:00:00 2001 From: Ethan Nicholas Date: Tue, 29 Sep 2020 10:21:29 -0400 Subject: [PATCH] moved SkSL FloatLiteral data into IRNode Change-Id: Ic39e3e8169218cfdd0e1eee274858cc421abec7e Reviewed-on: https://skia-review.googlesource.com/c/skia/+/320221 Commit-Queue: Ethan Nicholas Reviewed-by: John Stiles --- .../generated/GrHSLToRGBFilterEffect.cpp | 2 +- .../generated/GrHighContrastFilterEffect.cpp | 8 +++--- .../generated/GrRGBToHSLFilterEffect.cpp | 2 +- src/sksl/SkSLByteCodeGenerator.cpp | 2 +- src/sksl/SkSLCompiler.cpp | 4 +-- src/sksl/SkSLDehydrator.cpp | 2 +- src/sksl/SkSLGLSLCodeGenerator.cpp | 2 +- src/sksl/SkSLIRGenerator.cpp | 16 +++++------ src/sksl/SkSLMetalCodeGenerator.cpp | 2 +- src/sksl/SkSLSPIRVCodeGenerator.cpp | 4 +-- src/sksl/SkSLSPIRVCodeGenerator.h | 6 ++--- src/sksl/ir/SkSLExpression.h | 6 ++++- src/sksl/ir/SkSLFloatLiteral.h | 27 ++++++++++--------- src/sksl/ir/SkSLIRNode.cpp | 5 ++++ src/sksl/ir/SkSLIRNode.h | 27 +++++++++++++++++++ src/sksl/ir/SkSLPrefixExpression.h | 6 ++--- src/sksl/ir/SkSLSwizzle.h | 2 +- 17 files changed, 80 insertions(+), 43 deletions(-) diff --git a/src/gpu/effects/generated/GrHSLToRGBFilterEffect.cpp b/src/gpu/effects/generated/GrHSLToRGBFilterEffect.cpp index 48100fdd46..a0bbe8f4ce 100644 --- a/src/gpu/effects/generated/GrHSLToRGBFilterEffect.cpp +++ b/src/gpu/effects/generated/GrHSLToRGBFilterEffect.cpp @@ -29,7 +29,7 @@ public: R"SkSL(half4 inputColor = %s; half3 hsl = inputColor.xyz; half C = (1.0 - abs(2.0 * hsl.z - 1.0)) * hsl.y; -half3 p = hsl.xxx + half3(0.0, 0.66666666666666663, 0.33333333333333331); +half3 p = hsl.xxx + half3(0.0, 0.66666668653488159, 0.3333333432674408); half3 q = clamp(abs(fract(p) * 6.0 - 3.0) - 1.0, 0.0, 1.0); half3 rgb = (q - 0.5) * C + hsl.z; %s = clamp(half4(rgb, inputColor.w), 0.0, 1.0); diff --git a/src/gpu/effects/generated/GrHighContrastFilterEffect.cpp b/src/gpu/effects/generated/GrHighContrastFilterEffect.cpp index cca3d7ca22..f00f0baf97 100644 --- a/src/gpu/effects/generated/GrHighContrastFilterEffect.cpp +++ b/src/gpu/effects/generated/GrHighContrastFilterEffect.cpp @@ -45,7 +45,7 @@ public: fragBuilder->emitFunction(kHalf_GrSLType, "HSLToRGB", 3, HSLToRGB_args, R"SkSL(if (t < 0.0) t += 1.0; if (t > 1.0) t -= 1.0; -return t < 0.16666666666666666 ? p + ((q - p) * 6.0) * t : (t < 0.5 ? q : (t < 0.66666666666666663 ? p + ((q - p) * (0.66666666666666663 - t)) * 6.0 : p)); +return t < 0.1666666716337204 ? p + ((q - p) * 6.0) * t : (t < 0.5 ? q : (t < 0.66666668653488159 ? p + ((q - p) * (0.66666668653488159 - t)) * 6.0 : p)); )SkSL", &HSLToRGB_name); SkString _sample896 = this->invokeChild(0, args); @@ -87,7 +87,7 @@ half4 color = _0_unpremul; } else { h = (color.x - color.y) / d + 4.0; } - h *= 0.16666666666666666; + h *= 0.1666666716337204; } l = 1.0 + l * -0.5; if (s == 0.0) { @@ -95,9 +95,9 @@ half4 color = _0_unpremul; } else { half q = l < 0.5 ? l * (1.0 + s) : (l + s) - l * s; half p = 2.0 * l - q; - color.x = %s(p, q, h + 0.33333333333333331); + color.x = %s(p, q, h + 0.3333333432674408); color.y = %s(p, q, h); - color.z = %s(p, q, h - 0.33333333333333331); + color.z = %s(p, q, h - 0.3333333432674408); } } @if (%s) { diff --git a/src/gpu/effects/generated/GrRGBToHSLFilterEffect.cpp b/src/gpu/effects/generated/GrRGBToHSLFilterEffect.cpp index bf18ed2386..5dbe823dea 100644 --- a/src/gpu/effects/generated/GrRGBToHSLFilterEffect.cpp +++ b/src/gpu/effects/generated/GrRGBToHSLFilterEffect.cpp @@ -27,7 +27,7 @@ public: SkString _sample1173 = this->invokeChild(0, args); fragBuilder->codeAppendf( R"SkSL(half4 c = %s; -half4 p = c.y < c.z ? half4(c.zy, -1.0, 0.66666666666666663) : half4(c.yz, 0.0, -0.33333333333333331); +half4 p = c.y < c.z ? half4(c.zy, -1.0, 0.66666668653488159) : half4(c.yz, 0.0, -0.3333333432674408); half4 q = c.x < p.x ? half4(p.x, c.x, p.yw) : half4(c.x, p.x, p.yz); half pmV = q.x; diff --git a/src/sksl/SkSLByteCodeGenerator.cpp b/src/sksl/SkSLByteCodeGenerator.cpp index 866504d617..868f20862f 100644 --- a/src/sksl/SkSLByteCodeGenerator.cpp +++ b/src/sksl/SkSLByteCodeGenerator.cpp @@ -1022,7 +1022,7 @@ static inline uint32_t float_to_bits(float x) { void ByteCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { this->write(ByteCodeInstruction::kPushImmediate); - this->write32(float_to_bits(f.fValue)); + this->write32(float_to_bits(f.value())); } static bool is_generic_type(const Type* type, const Type* generic) { diff --git a/src/sksl/SkSLCompiler.cpp b/src/sksl/SkSLCompiler.cpp index 02af10936f..f1bba51b2f 100644 --- a/src/sksl/SkSLCompiler.cpp +++ b/src/sksl/SkSLCompiler.cpp @@ -650,14 +650,14 @@ static bool try_replace_expression(BasicBlock* b, * Returns true if the expression is a constant numeric literal with the specified value, or a * constant vector with all elements equal to the specified value. */ -template +template static bool is_constant(const Expression& expr, T value) { switch (expr.kind()) { case Expression::Kind::kIntLiteral: return expr.as().value() == value; case Expression::Kind::kFloatLiteral: - return expr.as().fValue == value; + return expr.as().value() == value; case Expression::Kind::kConstructor: { const Constructor& constructor = expr.as(); diff --git a/src/sksl/SkSLDehydrator.cpp b/src/sksl/SkSLDehydrator.cpp index 57893b9d32..ab5a84e462 100644 --- a/src/sksl/SkSLDehydrator.cpp +++ b/src/sksl/SkSLDehydrator.cpp @@ -298,7 +298,7 @@ void Dehydrator::write(const Expression* e) { const FloatLiteral& f = e->as(); this->writeU8(Rehydrator::kFloatLiteral_Command); FloatIntUnion u; - u.fFloat = f.fValue; + u.fFloat = f.value(); this->writeS32(u.fInt); break; } diff --git a/src/sksl/SkSLGLSLCodeGenerator.cpp b/src/sksl/SkSLGLSLCodeGenerator.cpp index 695bb54e10..9596b7efc9 100644 --- a/src/sksl/SkSLGLSLCodeGenerator.cpp +++ b/src/sksl/SkSLGLSLCodeGenerator.cpp @@ -1025,7 +1025,7 @@ void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) { } void GLSLCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { - this->write(to_string(f.fValue)); + this->write(to_string(f.value())); } void GLSLCodeGenerator::writeSetting(const Setting& s) { diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp index 19d7f87eee..b3174e3836 100644 --- a/src/sksl/SkSLIRGenerator.cpp +++ b/src/sksl/SkSLIRGenerator.cpp @@ -1825,8 +1825,8 @@ std::unique_ptr IRGenerator::constantFold(const Expression& left, } if (left.kind() == Expression::Kind::kFloatLiteral && right.kind() == Expression::Kind::kFloatLiteral) { - double leftVal = left.as().fValue; - double rightVal = right.as().fValue; + SKSL_FLOAT leftVal = left.as().value(); + SKSL_FLOAT rightVal = right.as().value(); switch (op) { case Token::Kind::TK_PLUS: return RESULT(Float, +); case Token::Kind::TK_MINUS: return RESULT(Float, -); @@ -1853,8 +1853,8 @@ std::unique_ptr IRGenerator::constantFold(const Expression& left, std::vector> args; #define RETURN_VEC_COMPONENTWISE_RESULT(op) \ for (int i = 0; i < leftType.columns(); i++) { \ - float value = left.getFVecComponent(i) op \ - right.getFVecComponent(i); \ + SKSL_FLOAT value = left.getFVecComponent(i) op \ + right.getFVecComponent(i); \ args.emplace_back(new FloatLiteral(fContext, -1, value)); \ } \ return std::unique_ptr(new Constructor(-1, &leftType, \ @@ -1876,7 +1876,7 @@ std::unique_ptr IRGenerator::constantFold(const Expression& left, fErrors.error(right.fOffset, "division by zero"); return nullptr; } - float value = left.getFVecComponent(i) / rvalue; + SKSL_FLOAT value = left.getFVecComponent(i) / rvalue; args.emplace_back(new FloatLiteral(fContext, -1, value)); } return std::unique_ptr(new Constructor(-1, &leftType, @@ -2227,12 +2227,12 @@ std::unique_ptr IRGenerator::convertNumberConstructor( return std::move(args[0]); } if (type.isFloat() && args.size() == 1 && args[0]->is()) { - double value = args[0]->as().fValue; + SKSL_FLOAT value = args[0]->as().value(); return std::make_unique(offset, value, &type); } if (type.isFloat() && args.size() == 1 && args[0]->is()) { int64_t value = args[0]->as().value(); - return std::make_unique(offset, (double)value, &type); + return std::make_unique(offset, (float)value, &type); } if (args[0]->is() && (type == *fContext.fInt_Type || type == *fContext.fUInt_Type)) { @@ -2369,7 +2369,7 @@ std::unique_ptr IRGenerator::convertPrefixExpression(const ASTNode& } if (base->is()) { return std::make_unique(fContext, base->fOffset, - -base->as().fValue); + -base->as().value()); } if (!baseType.isNumber() && baseType.typeKind() != Type::TypeKind::kVector) { fErrors.error(expression.fOffset, diff --git a/src/sksl/SkSLMetalCodeGenerator.cpp b/src/sksl/SkSLMetalCodeGenerator.cpp index 976b4b9760..de89150d35 100644 --- a/src/sksl/SkSLMetalCodeGenerator.cpp +++ b/src/sksl/SkSLMetalCodeGenerator.cpp @@ -919,7 +919,7 @@ void MetalCodeGenerator::writeIntLiteral(const IntLiteral& i) { } void MetalCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { - this->write(to_string(f.fValue)); + this->write(to_string(f.value())); } void MetalCodeGenerator::writeSetting(const Setting& s) { diff --git a/src/sksl/SkSLSPIRVCodeGenerator.cpp b/src/sksl/SkSLSPIRVCodeGenerator.cpp index fb2e044b5f..35c9bc1f1a 100644 --- a/src/sksl/SkSLSPIRVCodeGenerator.cpp +++ b/src/sksl/SkSLSPIRVCodeGenerator.cpp @@ -2554,8 +2554,8 @@ SpvId SPIRVCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { } else { constantType = ConstantType::kFloat; } - float value = (float) f.fValue; - std::pair key(f.fValue, constantType); + float value = (float) f.value(); + std::pair key(f.value(), constantType); auto entry = fNumberConstants.find(key); if (entry == fNumberConstants.end()) { SpvId result = this->nextId(); diff --git a/src/sksl/SkSLSPIRVCodeGenerator.h b/src/sksl/SkSLSPIRVCodeGenerator.h index 9bbc805a5b..7c884eb9a2 100644 --- a/src/sksl/SkSLSPIRVCodeGenerator.h +++ b/src/sksl/SkSLSPIRVCodeGenerator.h @@ -47,15 +47,15 @@ union ConstantValue { ConstantValue(int64_t i) : fInt(i) {} - ConstantValue(double d) - : fDouble(d) {} + ConstantValue(SKSL_FLOAT f) + : fFloat(f) {} bool operator==(const ConstantValue& other) const { return fInt == other.fInt; } int64_t fInt; - double fDouble; + SKSL_FLOAT fFloat; }; enum class ConstantType { diff --git a/src/sksl/ir/SkSLExpression.h b/src/sksl/ir/SkSLExpression.h index 61fe17ac6a..2a4ead8837 100644 --- a/src/sksl/ir/SkSLExpression.h +++ b/src/sksl/ir/SkSLExpression.h @@ -64,6 +64,10 @@ struct Expression : public IRNode { : INHERITED(offset, (int) Kind::kIntLiteral, data) { } + Expression(int offset, FloatLiteralData data) + : INHERITED(offset, (int) Kind::kFloatLiteral, data) { + } + Expression(int offset, Kind kind, const Type* type) : INHERITED(offset, (int) kind, type) { SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast); @@ -131,7 +135,7 @@ struct Expression : public IRNode { * For an expression which evaluates to a constant float, returns the value. Otherwise calls * ABORT. */ - virtual double getConstantFloat() const { + virtual SKSL_FLOAT getConstantFloat() const { ABORT("not a constant float"); } diff --git a/src/sksl/ir/SkSLFloatLiteral.h b/src/sksl/ir/SkSLFloatLiteral.h index 1a0ca30dc2..ee68908cfe 100644 --- a/src/sksl/ir/SkSLFloatLiteral.h +++ b/src/sksl/ir/SkSLFloatLiteral.h @@ -19,16 +19,18 @@ namespace SkSL { struct FloatLiteral : public Expression { static constexpr Kind kExpressionKind = Kind::kFloatLiteral; - FloatLiteral(const Context& context, int offset, double value) - : INHERITED(offset, kExpressionKind, context.fFloatLiteral_Type.get()) - , fValue(value) {} + FloatLiteral(const Context& context, int offset, float value) + : INHERITED(offset, FloatLiteralData{context.fFloatLiteral_Type.get(), value}) {} - FloatLiteral(int offset, double value, const Type* type) - : INHERITED(offset, kExpressionKind, type) - , fValue(value) {} + FloatLiteral(int offset, float value, const Type* type) + : INHERITED(offset, FloatLiteralData{type, value}) {} + + float value() const { + return this->floatLiteralData().fValue; + } String description() const override { - return to_string(fValue); + return to_string(this->value()); } bool hasProperty(Property property) const override { @@ -47,19 +49,18 @@ struct FloatLiteral : public Expression { } bool compareConstant(const Context& context, const Expression& other) const override { - return fValue == other.as().fValue; + return this->value() == other.as().value(); } - double getConstantFloat() const override { - return fValue; + SKSL_FLOAT getConstantFloat() const override { + return this->value(); } std::unique_ptr clone() const override { - return std::unique_ptr(new FloatLiteral(fOffset, fValue, &this->type())); + return std::unique_ptr(new FloatLiteral(fOffset, this->value(), &this->type())); } - const double fValue; - +private: using INHERITED = Expression; }; diff --git a/src/sksl/ir/SkSLIRNode.cpp b/src/sksl/ir/SkSLIRNode.cpp index ed64710648..198547b569 100644 --- a/src/sksl/ir/SkSLIRNode.cpp +++ b/src/sksl/ir/SkSLIRNode.cpp @@ -28,6 +28,11 @@ IRNode::IRNode(int offset, int kind, const IntLiteralData& data) , fKind(kind) , fData(data) {} +IRNode::IRNode(int offset, int kind, const FloatLiteralData& data) +: fOffset(offset) +, fKind(kind) +, fData(data) {} + IRNode::IRNode(int offset, int kind, const Type* data) : fOffset(offset) , fKind(kind) diff --git a/src/sksl/ir/SkSLIRNode.h b/src/sksl/ir/SkSLIRNode.h index d0d7b83187..018a27afad 100644 --- a/src/sksl/ir/SkSLIRNode.h +++ b/src/sksl/ir/SkSLIRNode.h @@ -54,6 +54,8 @@ public: return *this->boolLiteralData().fType; case NodeData::Kind::kIntLiteral: return *this->intLiteralData().fType; + case NodeData::Kind::kFloatLiteral: + return *this->floatLiteralData().fType; case NodeData::Kind::kType: return *this->typeData(); case NodeData::Kind::kTypeToken: @@ -77,6 +79,11 @@ protected: bool fValue; }; + struct FloatLiteralData { + const Type* fType; + float fValue; + }; + struct IntLiteralData { const Type* fType; int64_t fValue; @@ -91,6 +98,7 @@ protected: enum class Kind { kBlock, kBoolLiteral, + kFloatLiteral, kIntLiteral, kType, kTypeToken, @@ -100,6 +108,7 @@ protected: union Contents { BlockData fBlock; BoolLiteralData fBoolLiteral; + FloatLiteralData fFloatLiteral; IntLiteralData fIntLiteral; const Type* fType; TypeTokenData fTypeToken; @@ -119,6 +128,11 @@ protected: *(new(&fContents) BoolLiteralData) = data; } + NodeData(const FloatLiteralData& data) + : fKind(Kind::kFloatLiteral) { + *(new(&fContents) FloatLiteralData) = data; + } + NodeData(IntLiteralData data) : fKind(Kind::kIntLiteral) { *(new(&fContents) IntLiteralData) = data; @@ -148,6 +162,9 @@ protected: case Kind::kBoolLiteral: *(new(&fContents) BoolLiteralData) = other.fContents.fBoolLiteral; break; + case Kind::kFloatLiteral: + *(new(&fContents) FloatLiteralData) = other.fContents.fFloatLiteral; + break; case Kind::kIntLiteral: *(new(&fContents) IntLiteralData) = other.fContents.fIntLiteral; break; @@ -174,6 +191,9 @@ protected: case Kind::kBoolLiteral: fContents.fBoolLiteral.~BoolLiteralData(); break; + case Kind::kFloatLiteral: + fContents.fFloatLiteral.~FloatLiteralData(); + break; case Kind::kIntLiteral: fContents.fIntLiteral.~IntLiteralData(); break; @@ -193,6 +213,8 @@ protected: IRNode(int offset, int kind, const IntLiteralData& data); + IRNode(int offset, int kind, const FloatLiteralData& data); + IRNode(int offset, int kind, const Type* data = nullptr); IRNode(int offset, int kind, const TypeTokenData& data); @@ -253,6 +275,11 @@ protected: return fData.fContents.fBoolLiteral; } + const FloatLiteralData& floatLiteralData() const { + SkASSERT(fData.fKind == NodeData::Kind::kFloatLiteral); + return fData.fContents.fFloatLiteral; + } + const IntLiteralData& intLiteralData() const { SkASSERT(fData.fKind == NodeData::Kind::kIntLiteral); return fData.fContents.fIntLiteral; diff --git a/src/sksl/ir/SkSLPrefixExpression.h b/src/sksl/ir/SkSLPrefixExpression.h index 8bc72b74ed..383abf5c4e 100644 --- a/src/sksl/ir/SkSLPrefixExpression.h +++ b/src/sksl/ir/SkSLPrefixExpression.h @@ -43,9 +43,9 @@ struct PrefixExpression : public Expression { const DefinitionMap& definitions) override { if (fOperand->kind() == Expression::Kind::kFloatLiteral) { return std::unique_ptr(new FloatLiteral( - irGenerator.fContext, - fOffset, - -fOperand->as().fValue)); + irGenerator.fContext, + fOffset, + -fOperand->as().value())); } return nullptr; diff --git a/src/sksl/ir/SkSLSwizzle.h b/src/sksl/ir/SkSLSwizzle.h index fc667ea1b0..a4ff1f16a6 100644 --- a/src/sksl/ir/SkSLSwizzle.h +++ b/src/sksl/ir/SkSLSwizzle.h @@ -45,7 +45,7 @@ struct Swizzle : public Expression { value); } else if (type.isFloat()) { SkASSERT(fComponents.size() == 1); - double value = constructor.getFVecComponent(fComponents[0]); + SKSL_FLOAT value = constructor.getFVecComponent(fComponents[0]); return std::make_unique(irGenerator.fContext, constructor.fOffset, value); }