Added SkSL DSL SampleChild function

Change-Id: Icd94540bbf0afcc57cfd93f3b576249e26e25724
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/369776
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Auto-Submit: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
This commit is contained in:
Ethan Nicholas 2021-02-16 13:02:57 -05:00
parent 2478c70a03
commit 840f58197e
7 changed files with 70 additions and 0 deletions

View File

@ -381,6 +381,9 @@ void CFGGenerator::addExpression(CFG& cfg, std::unique_ptr<Expression>* e, bool
} }
break; break;
} }
case Expression::Kind::kCodeString:
SkDEBUGFAIL("shouldn't be able to receive kCodeString here");
break;
case Expression::Kind::kConstructor: { case Expression::Kind::kConstructor: {
Constructor& c = e->get()->as<Constructor>(); Constructor& c = e->get()->as<Constructor>();
for (auto& arg : c.arguments()) { for (auto& arg : c.arguments()) {

View File

@ -273,6 +273,9 @@ void Dehydrator::write(const Expression* e) {
this->writeU8(b.value()); this->writeU8(b.value());
break; break;
} }
case Expression::Kind::kCodeString:
SkDEBUGFAIL("shouldn't be able to receive kCodeString here");
break;
case Expression::Kind::kConstructor: { case Expression::Kind::kConstructor: {
const Constructor& c = e->as<Constructor>(); const Constructor& c = e->as<Constructor>();
this->writeCommand(Rehydrator::kConstructor_Command); this->writeCommand(Rehydrator::kConstructor_Command);

View File

@ -109,6 +109,8 @@ private:
std::unique_ptr<SkSL::Expression> fExpression; std::unique_ptr<SkSL::Expression> fExpression;
friend DSLExpression SampleChild(int index, DSLExpression coords);
template<class... Cases> template<class... Cases>
friend DSLStatement Switch(DSLExpression value, Cases... cases); friend DSLStatement Switch(DSLExpression value, Cases... cases);

View File

@ -8,6 +8,7 @@
#include "src/sksl/dsl/priv/DSLFPs.h" #include "src/sksl/dsl/priv/DSLFPs.h"
#include "src/sksl/dsl/priv/DSLWriter.h" #include "src/sksl/dsl/priv/DSLWriter.h"
#include "src/sksl/ir/SkSLCodeStringExpression.h"
#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
@ -24,6 +25,15 @@ void EndFragmentProcessor() {
DSLWriter::EndFragmentProcessor(); DSLWriter::EndFragmentProcessor();
} }
DSLExpression SampleChild(int index, DSLExpression coords) {
std::unique_ptr<SkSL::Expression> coordsExpr = coords.release();
SkString code = DSLWriter::CurrentProcessor()->invokeChild(index, *DSLWriter::CurrentEmitArgs(),
coordsExpr ? coordsExpr->description()
: "");
return DSLExpression(std::make_unique<SkSL::CodeStringExpression>(code.c_str(),
DSLWriter::Context().fTypes.fHalf4.get()));
}
GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLVar& var) { GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLVar& var) {
return DSLWriter::VarUniformHandle(var); return DSLWriter::VarUniformHandle(var);
} }

View File

@ -23,6 +23,8 @@ void StartFragmentProcessor(GrGLSLFragmentProcessor* processor,
void EndFragmentProcessor(); void EndFragmentProcessor();
DSLExpression SampleChild(int index, DSLExpression coords = DSLExpression());
GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLVar& var); GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLVar& var);
} // namespace dsl } // namespace dsl

View File

@ -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<Expression> clone() const override {
return std::make_unique<CodeStringExpression>(fCode, &this->type());
}
String description() const override {
return fCode;
}
private:
String fCode;
using INHERITED = Expression;
};
} // namespace SkSL
#endif

View File

@ -29,6 +29,7 @@ public:
enum class Kind { enum class Kind {
kBinary = (int) Statement::Kind::kLast + 1, kBinary = (int) Statement::Kind::kLast + 1,
kBoolLiteral, kBoolLiteral,
kCodeString,
kConstructor, kConstructor,
kDefined, kDefined,
kExternalFunctionCall, kExternalFunctionCall,