From 840f58197e5c0cb0a3194848a0fd7bc7d8201b5d Mon Sep 17 00:00:00 2001 From: Ethan Nicholas Date: Tue, 16 Feb 2021 13:02:57 -0500 Subject: [PATCH] Added SkSL DSL SampleChild function Change-Id: Icd94540bbf0afcc57cfd93f3b576249e26e25724 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/369776 Commit-Queue: Ethan Nicholas Auto-Submit: Ethan Nicholas Reviewed-by: John Stiles --- src/sksl/SkSLCFGGenerator.cpp | 3 ++ src/sksl/SkSLDehydrator.cpp | 3 ++ src/sksl/dsl/DSLExpression.h | 2 ++ src/sksl/dsl/priv/DSLFPs.cpp | 10 ++++++ src/sksl/dsl/priv/DSLFPs.h | 2 ++ src/sksl/ir/SkSLCodeStringExpression.h | 49 ++++++++++++++++++++++++++ src/sksl/ir/SkSLExpression.h | 1 + 7 files changed, 70 insertions(+) create mode 100644 src/sksl/ir/SkSLCodeStringExpression.h diff --git a/src/sksl/SkSLCFGGenerator.cpp b/src/sksl/SkSLCFGGenerator.cpp index 6932369214..17470aa2ed 100644 --- a/src/sksl/SkSLCFGGenerator.cpp +++ b/src/sksl/SkSLCFGGenerator.cpp @@ -381,6 +381,9 @@ void CFGGenerator::addExpression(CFG& cfg, std::unique_ptr* e, bool } break; } + case Expression::Kind::kCodeString: + SkDEBUGFAIL("shouldn't be able to receive kCodeString here"); + break; case Expression::Kind::kConstructor: { Constructor& c = e->get()->as(); for (auto& arg : c.arguments()) { diff --git a/src/sksl/SkSLDehydrator.cpp b/src/sksl/SkSLDehydrator.cpp index d6d63ce635..7b7a7b497c 100644 --- a/src/sksl/SkSLDehydrator.cpp +++ b/src/sksl/SkSLDehydrator.cpp @@ -273,6 +273,9 @@ void Dehydrator::write(const Expression* e) { this->writeU8(b.value()); break; } + case Expression::Kind::kCodeString: + SkDEBUGFAIL("shouldn't be able to receive kCodeString here"); + break; case Expression::Kind::kConstructor: { const Constructor& c = e->as(); this->writeCommand(Rehydrator::kConstructor_Command); diff --git a/src/sksl/dsl/DSLExpression.h b/src/sksl/dsl/DSLExpression.h index 453b8181cf..431cac1838 100644 --- a/src/sksl/dsl/DSLExpression.h +++ b/src/sksl/dsl/DSLExpression.h @@ -109,6 +109,8 @@ private: std::unique_ptr fExpression; + friend DSLExpression SampleChild(int index, DSLExpression coords); + template friend DSLStatement Switch(DSLExpression value, Cases... cases); diff --git a/src/sksl/dsl/priv/DSLFPs.cpp b/src/sksl/dsl/priv/DSLFPs.cpp index cf2090d38d..018266da9a 100644 --- a/src/sksl/dsl/priv/DSLFPs.cpp +++ b/src/sksl/dsl/priv/DSLFPs.cpp @@ -8,6 +8,7 @@ #include "src/sksl/dsl/priv/DSLFPs.h" #include "src/sksl/dsl/priv/DSLWriter.h" +#include "src/sksl/ir/SkSLCodeStringExpression.h" #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU @@ -24,6 +25,15 @@ void EndFragmentProcessor() { DSLWriter::EndFragmentProcessor(); } +DSLExpression SampleChild(int index, DSLExpression coords) { + std::unique_ptr coordsExpr = coords.release(); + SkString code = DSLWriter::CurrentProcessor()->invokeChild(index, *DSLWriter::CurrentEmitArgs(), + coordsExpr ? coordsExpr->description() + : ""); + return DSLExpression(std::make_unique(code.c_str(), + DSLWriter::Context().fTypes.fHalf4.get())); +} + GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLVar& var) { return DSLWriter::VarUniformHandle(var); } diff --git a/src/sksl/dsl/priv/DSLFPs.h b/src/sksl/dsl/priv/DSLFPs.h index 598c7da2e4..9127045ac3 100644 --- a/src/sksl/dsl/priv/DSLFPs.h +++ b/src/sksl/dsl/priv/DSLFPs.h @@ -23,6 +23,8 @@ void StartFragmentProcessor(GrGLSLFragmentProcessor* processor, void EndFragmentProcessor(); +DSLExpression SampleChild(int index, DSLExpression coords = DSLExpression()); + GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLVar& var); } // namespace dsl diff --git a/src/sksl/ir/SkSLCodeStringExpression.h b/src/sksl/ir/SkSLCodeStringExpression.h new file mode 100644 index 0000000000..7559cbe8ec --- /dev/null +++ b/src/sksl/ir/SkSLCodeStringExpression.h @@ -0,0 +1,49 @@ +/* + * Copyright 2020 Google LLC. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SKSL_CODESTRINGEXPRESSION +#define SKSL_CODESTRINGEXPRESSION + +#include "src/sksl/ir/SkSLExpression.h" + +namespace SkSL { + +/** + * Represents a literal string of SkSL code. This is only valid within SkSL DSL code. + * TODO(skia:11330): This class is intended as a temporary measure to support a couple of spots + * within Skia that are currently generating raw strings of code. These will eventually transition + * to producing Expressions, allowing this class to be deleted. + */ +class CodeStringExpression final : public Expression { +public: + static constexpr Kind kExpressionKind = Kind::kCodeString; + + CodeStringExpression(String code, const Type* type) + : INHERITED(/*offset=*/-1, kExpressionKind, type) + , fCode(std::move(code)) {} + + bool hasProperty(Property property) const override { + return false; + } + + std::unique_ptr clone() const override { + return std::make_unique(fCode, &this->type()); + } + + String description() const override { + return fCode; + } + +private: + String fCode; + + using INHERITED = Expression; +}; + +} // namespace SkSL + +#endif diff --git a/src/sksl/ir/SkSLExpression.h b/src/sksl/ir/SkSLExpression.h index a1e918160e..3daf52441e 100644 --- a/src/sksl/ir/SkSLExpression.h +++ b/src/sksl/ir/SkSLExpression.h @@ -29,6 +29,7 @@ public: enum class Kind { kBinary = (int) Statement::Kind::kLast + 1, kBoolLiteral, + kCodeString, kConstructor, kDefined, kExternalFunctionCall,