Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 87e3bef

Browse files
brianosmanSkia Commit-Bot
authored andcommitted
Reland "Switch runtime SkSL to always sample at explicit coords"
This reverts commit d4bf54e. Change-Id: I65bfea4d880de29394e25d44d781fd18508fe337 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/266942 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Osman <brianosman@google.com>
1 parent f4413d6 commit 87e3bef

File tree

6 files changed

+41
-21
lines changed

6 files changed

+41
-21
lines changed

src/effects/imagefilters/SkArithmeticImageFilter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ uniform float4 k;
3636
in bool enforcePMColor;
3737
in fragmentProcessor child;
3838
39-
void main(inout half4 color) {
40-
half4 dst = sample(child);
39+
void main(float x, float y, inout half4 color) {
40+
half4 dst = sample(child, float2(x, y));
4141
color = saturate(half(k.x) * color * dst + half(k.y) * color + half(k.z) * dst + half(k.w));
4242
@if (enforcePMColor) {
4343
color.rgb = min(color.rgb, color.a);

src/gpu/effects/GrSkSLFP.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ class GrGLSLSkSLFP : public GrGLSLFragmentProcessor {
2222
GrGLSLSkSLFP(SkSL::PipelineStageArgs&& args) : fArgs(std::move(args)) {}
2323

2424
SkSL::String expandFormatArgs(const SkSL::String& raw,
25-
const EmitArgs& args,
26-
const std::vector<SkSL::Compiler::FormatArg> formatArgs,
27-
const char* coordsName,
28-
const std::vector<SkString>& childNames) {
25+
EmitArgs& args,
26+
std::vector<SkSL::Compiler::FormatArg>::const_iterator& fmtArg,
27+
const char* coordsName) {
2928
SkSL::String result;
3029
int substringStartIndex = 0;
31-
int formatArgIndex = 0;
3230
for (size_t i = 0; i < raw.length(); ++i) {
3331
char c = raw[i];
3432
if (c == '%') {
@@ -38,7 +36,7 @@ class GrGLSLSkSLFP : public GrGLSLFragmentProcessor {
3836
c = raw[i];
3937
switch (c) {
4038
case 's': {
41-
const SkSL::Compiler::FormatArg& arg = formatArgs[formatArgIndex++];
39+
const SkSL::Compiler::FormatArg& arg = *fmtArg++;
4240
switch (arg.fKind) {
4341
case SkSL::Compiler::FormatArg::Kind::kInput:
4442
result += args.fInputColor;
@@ -58,9 +56,12 @@ class GrGLSLSkSLFP : public GrGLSLFragmentProcessor {
5856
result += args.fUniformHandler->getUniformCStr(
5957
fUniformHandles[arg.fIndex]);
6058
break;
61-
case SkSL::Compiler::FormatArg::Kind::kChildProcessor:
62-
result += childNames[arg.fIndex].c_str();
59+
case SkSL::Compiler::FormatArg::Kind::kChildProcessor: {
60+
SkSL::String coords = this->expandFormatArgs(arg.fCoords, args,
61+
fmtArg, coordsName);
62+
result += this->invokeChild(arg.fIndex, args, coords).c_str();
6363
break;
64+
}
6465
case SkSL::Compiler::FormatArg::Kind::kFunctionName:
6566
SkASSERT((int) fFunctionNames.size() > arg.fIndex);
6667
result += fFunctionNames[arg.fIndex].c_str();
@@ -94,22 +95,28 @@ class GrGLSLSkSLFP : public GrGLSLFragmentProcessor {
9495
SkASSERT(args.fTransformedCoords.count() == 1);
9596
SkString coords = fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint);
9697
std::vector<SkString> childNames;
98+
// We need to ensure that we call invokeChild on each child FP at least once.
99+
// Any child FP that isn't sampled won't trigger a call otherwise, leading to asserts later.
97100
for (int i = 0; i < this->numChildProcessors(); ++i) {
98-
childNames.push_back(this->invokeChild(i, args));
101+
(void)this->invokeChild(i, args, SkSL::String("_coords"));
99102
}
100103
for (const auto& f : fArgs.fFunctions) {
101104
fFunctionNames.emplace_back();
102-
SkSL::String body = this->expandFormatArgs(f.fBody.c_str(), args, f.fFormatArgs,
103-
coords.c_str(), childNames);
105+
auto fmtArgIter = f.fFormatArgs.cbegin();
106+
SkSL::String body =
107+
this->expandFormatArgs(f.fBody.c_str(), args, fmtArgIter, coords.c_str());
108+
SkASSERT(fmtArgIter == f.fFormatArgs.cend());
104109
fragBuilder->emitFunction(f.fReturnType,
105110
f.fName.c_str(),
106111
f.fParameters.size(),
107112
f.fParameters.data(),
108113
body.c_str(),
109114
&fFunctionNames.back());
110115
}
111-
fragBuilder->codeAppend(this->expandFormatArgs(fArgs.fCode.c_str(), args, fArgs.fFormatArgs,
112-
coords.c_str(), childNames).c_str());
116+
auto fmtArgIter = fArgs.fFormatArgs.cbegin();
117+
fragBuilder->codeAppend(this->expandFormatArgs(fArgs.fCode.c_str(), args, fmtArgIter,
118+
coords.c_str()).c_str());
119+
SkASSERT(fmtArgIter == fArgs.fFormatArgs.cend());
113120
}
114121

115122
void onSetData(const GrGLSLProgramDataManager& pdman,
@@ -198,6 +205,7 @@ const char* GrSkSLFP::name() const {
198205
}
199206

200207
void GrSkSLFP::addChild(std::unique_ptr<GrFragmentProcessor> child) {
208+
child->setSampledWithExplicitCoords(true);
201209
this->registerChildProcessor(std::move(child));
202210
}
203211

@@ -244,7 +252,7 @@ bool GrSkSLFP::onIsEqual(const GrFragmentProcessor& other) const {
244252
std::unique_ptr<GrFragmentProcessor> GrSkSLFP::clone() const {
245253
std::unique_ptr<GrSkSLFP> result(new GrSkSLFP(*this));
246254
for (int i = 0; i < this->numChildProcessors(); ++i) {
247-
result->registerChildProcessor(this->childProcessor(i).clone());
255+
result->addChild(this->childProcessor(i).clone());
248256
}
249257
return std::unique_ptr<GrFragmentProcessor>(result.release());
250258
}

src/sksl/SkSLCompiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ class SK_API Compiler : public ErrorReporter {
9292
, fIndex(index) {}
9393

9494
Kind fKind;
95-
9695
int fIndex;
96+
String fCoords;
9797
};
9898

9999
#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU

src/sksl/SkSLPipelineStageCodeGenerator.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ void PipelineStageCodeGenerator::writeBinaryExpression(const BinaryExpression& b
5656
void PipelineStageCodeGenerator::writeFunctionCall(const FunctionCall& c) {
5757
if (c.fFunction.fBuiltin && c.fFunction.fName == "sample" &&
5858
c.fArguments[0]->fType.kind() != Type::Kind::kSampler_Kind) {
59-
SkASSERT(c.fArguments.size() == 1);
59+
SkASSERT(c.fArguments.size() == 2);
6060
SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name() ||
6161
"fragmentProcessor?" == c.fArguments[0]->fType.name());
62+
SkASSERT("float2" == c.fArguments[1]->fType.name());
6263
SkASSERT(Expression::kVariableReference_Kind == c.fArguments[0]->fKind);
6364
int index = 0;
6465
bool found = false;
@@ -80,8 +81,15 @@ void PipelineStageCodeGenerator::writeFunctionCall(const FunctionCall& c) {
8081
}
8182
SkASSERT(found);
8283
this->write("%s");
84+
size_t childCallIndex = fArgs->fFormatArgs.size();
8385
fArgs->fFormatArgs.push_back(
8486
Compiler::FormatArg(Compiler::FormatArg::Kind::kChildProcessor, index));
87+
OutputStream* oldOut = fOut;
88+
StringStream buffer;
89+
fOut = &buffer;
90+
this->writeExpression(*c.fArguments[1], kSequence_Precedence);
91+
fOut = oldOut;
92+
fArgs->fFormatArgs[childCallIndex].fCoords = buffer.str();
8593
return;
8694
}
8795
if (c.fFunction.fBuiltin) {

src/sksl/sksl_pipeline.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
STRINGIFY(
2-
half4 sample(fragmentProcessor fp);
2+
half4 sample(fragmentProcessor fp, float2 coords);
33
)

tools/viewer/SkSLSlide.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "tools/viewer/SkSLSlide.h"
99

1010
#include "include/effects/SkGradientShader.h"
11+
#include "include/effects/SkPerlinNoiseShader.h"
1112
#include "src/core/SkEnumerate.h"
1213
#include "tools/Resources.h"
1314
#include "tools/viewer/ImGuiLayer.h"
@@ -36,10 +37,10 @@ SkSLSlide::SkSLSlide() {
3637

3738
fSkSL =
3839

39-
"uniform half4 gColor;\n"
40+
"in fragmentProcessor fp;\n"
4041
"\n"
4142
"void main(float x, float y, inout half4 color) {\n"
42-
" color = half4(half(x)*(1.0/255), half(y)*(1.0/255), gColor.b, 1);\n"
43+
" color = sample(fp, float2(x, y));\n"
4344
"}\n";
4445
}
4546

@@ -62,6 +63,9 @@ void SkSLSlide::load(SkScalar winWidth, SkScalar winHeight) {
6263
shader = GetResourceAsImage("images/mandrill_256.png")->makeShader();
6364
fShaders.push_back(std::make_pair("Mandrill", shader));
6465

66+
shader = SkPerlinNoiseShader::MakeImprovedNoise(0.025f, 0.025f, 3, 0.0f);
67+
fShaders.push_back(std::make_pair("Perlin Noise", shader));
68+
6569
this->rebuild();
6670
}
6771

0 commit comments

Comments
 (0)