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

Commit fb5ede5

Browse files
Ethan NicholasSkia Commit-Bot
authored andcommitted
fixed sample(..., matrix) with runtime effects
Change-Id: Id5b7f1b5e992c587be000e112706bedfe00c90fd Reviewed-on: https://skia-review.googlesource.com/c/skia/+/294697 Commit-Queue: Ethan Nicholas <[email protected]> Reviewed-by: Brian Osman <[email protected]>
1 parent 3fd5b8f commit fb5ede5

File tree

8 files changed

+42
-14
lines changed

8 files changed

+42
-14
lines changed

gm/runtimeshader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ class ThresholdRT : public skiagm::GM {
115115
}
116116
117117
void main(float2 xy, inout half4 color) {
118+
half4 after = sample(after_map, float3x3(1));
118119
half4 before = sample(before_map, xy);
119-
half4 after = sample(after_map, xy);
120120
121121
float m = smooth_cutoff(sample(threshold_map, xy).r);
122122
color = mix(before, after, half(m));

include/effects/SkRuntimeEffect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace SkSL {
2727
class ByteCode;
2828
struct PipelineStageArgs;
2929
struct Program;
30+
struct SampleMatrix;
3031
class SharedCompiler;
3132
}
3233

src/core/SkRuntimeEffect.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#if SK_SUPPORT_GPU
2828
#include "include/private/GrRecordingContext.h"
2929
#include "src/gpu/GrColorInfo.h"
30-
#include "src/gpu/GrFPArgs.h"
3130
#include "src/gpu/effects/GrMatrixEffect.h"
3231
#include "src/gpu/effects/GrSkSLFP.h"
3332
#endif

src/gpu/effects/GrSkSLFP.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
class GrGLSLSkSLFP : public GrGLSLFragmentProcessor {
2121
public:
22-
GrGLSLSkSLFP(SkSL::PipelineStageArgs&& args) : fArgs(std::move(args)) {}
22+
GrGLSLSkSLFP(const SkSL::PipelineStageArgs& args) : fArgs(std::move(args)) {}
2323

2424
SkSL::String expandFormatArgs(const SkSL::String& raw,
2525
EmitArgs& args,
@@ -57,6 +57,13 @@ class GrGLSLSkSLFP : public GrGLSLFragmentProcessor {
5757
result += this->invokeChild(arg.fIndex, args, coords).c_str();
5858
break;
5959
}
60+
case SkSL::Compiler::FormatArg::Kind::kChildProcessorWithMatrix: {
61+
SkSL::String coords = this->expandFormatArgs(arg.fCoords, args,
62+
fmtArg, coordsName);
63+
result += this->invokeChildWithMatrix(arg.fIndex, args,
64+
coords).c_str();
65+
break;
66+
}
6067
case SkSL::Compiler::FormatArg::Kind::kFunctionName:
6168
SkASSERT((int) fFunctionNames.size() > arg.fIndex);
6269
result += fFunctionNames[arg.fIndex].c_str();
@@ -95,7 +102,8 @@ class GrGLSLSkSLFP : public GrGLSLFragmentProcessor {
95102
// We need to ensure that we call invokeChild on each child FP at least once.
96103
// Any child FP that isn't sampled won't trigger a call otherwise, leading to asserts later.
97104
for (int i = 0; i < this->numChildProcessors(); ++i) {
98-
(void)this->invokeChild(i, args, SkSL::String("_coords"));
105+
bool isExplicit = args.fFp.childProcessor(i).isSampledWithExplicitCoords();
106+
(void)this->invokeChild(i, args, isExplicit ? SkSL::String("_coords") : "");
99107
}
100108
for (const auto& f : fArgs.fFunctions) {
101109
fFunctionNames.emplace_back();
@@ -181,6 +189,7 @@ GrSkSLFP::GrSkSLFP(sk_sp<const GrShaderCaps> shaderCaps, ShaderErrorHandler* sha
181189
, fName(name)
182190
, fInputs(std::move(inputs)) {
183191
this->addCoordTransform(&fCoordTransform);
192+
fEffect->toPipelineStage(fInputs->data(), fShaderCaps.get(), fShaderErrorHandler, &fArgs);
184193
}
185194

186195
GrSkSLFP::GrSkSLFP(const GrSkSLFP& other)
@@ -189,7 +198,8 @@ GrSkSLFP::GrSkSLFP(const GrSkSLFP& other)
189198
, fShaderErrorHandler(other.fShaderErrorHandler)
190199
, fEffect(other.fEffect)
191200
, fName(other.fName)
192-
, fInputs(other.fInputs) {
201+
, fInputs(other.fInputs)
202+
, fArgs(other.fArgs) {
193203
this->addCoordTransform(&fCoordTransform);
194204
}
195205

@@ -198,15 +208,18 @@ const char* GrSkSLFP::name() const {
198208
}
199209

200210
void GrSkSLFP::addChild(std::unique_ptr<GrFragmentProcessor> child) {
201-
child->setSampledWithExplicitCoords();
211+
int newIndex = this->numChildProcessors();
212+
if ((int) fArgs.fSampleMatrices.size() > newIndex &&
213+
fArgs.fSampleMatrices[newIndex].fKind != SkSL::SampleMatrix::Kind::kNone) {
214+
child->setSampleMatrix(fArgs.fSampleMatrices[newIndex]);
215+
} else {
216+
child->setSampledWithExplicitCoords();
217+
}
202218
this->registerChildProcessor(std::move(child));
203219
}
204220

205221
GrGLSLFragmentProcessor* GrSkSLFP::onCreateGLSLInstance() const {
206-
// Note: This is actually SkSL (again) but with inline format specifiers.
207-
SkSL::PipelineStageArgs args;
208-
fEffect->toPipelineStage(fInputs->data(), fShaderCaps.get(), fShaderErrorHandler, &args);
209-
return new GrGLSLSkSLFP(std::move(args));
222+
return new GrGLSLSkSLFP(fArgs);
210223
}
211224

212225
void GrSkSLFP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {

src/gpu/effects/GrSkSLFP.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ class GrSkSLFP : public GrFragmentProcessor {
9494
sk_sp<const GrShaderCaps> fShaderCaps;
9595
ShaderErrorHandler* fShaderErrorHandler;
9696

97-
sk_sp<SkRuntimeEffect> fEffect;
98-
const char* fName;
99-
sk_sp<SkData> fInputs;
97+
sk_sp<SkRuntimeEffect> fEffect;
98+
const char* fName;
99+
sk_sp<SkData> fInputs;
100+
SkSL::PipelineStageArgs fArgs;
100101

101102
GrCoordTransform fCoordTransform;
102103

src/gpu/glsl/GrGLSLFragmentProcessor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ SkString GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputC
8181
SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor,
8282
EmitArgs& args,
8383
SkSL::String skslMatrix) {
84+
SkASSERT(!args.fFp.isSampledWithExplicitCoords());
8485
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
8586
while (childIndex >= (int) fFunctionNames.size()) {
8687
fFunctionNames.emplace_back();

src/sksl/SkSLCompiler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "src/sksl/SkSLContext.h"
1818
#include "src/sksl/SkSLErrorReporter.h"
1919
#include "src/sksl/SkSLLexer.h"
20+
#include "src/sksl/SkSLSampleMatrix.h"
2021
#include "src/sksl/ir/SkSLProgram.h"
2122
#include "src/sksl/ir/SkSLSymbolTable.h"
2223

@@ -79,6 +80,7 @@ class SK_API Compiler : public ErrorReporter {
7980
kCoords,
8081
kUniform,
8182
kChildProcessor,
83+
kChildProcessorWithMatrix,
8284
kFunctionName
8385
};
8486

@@ -245,6 +247,7 @@ struct PipelineStageArgs {
245247
String fCode;
246248
std::vector<Compiler::FormatArg> fFormatArgs;
247249
std::vector<Compiler::GLSLFunction> fFunctions;
250+
std::vector<SkSL::SampleMatrix> fSampleMatrices;
248251
};
249252
#endif
250253

src/sksl/SkSLPipelineStageCodeGenerator.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,18 @@ void PipelineStageCodeGenerator::writeFunctionCall(const FunctionCall& c) {
8181
SkASSERT(found);
8282
this->write("%s");
8383
size_t childCallIndex = fArgs->fFormatArgs.size();
84+
bool matrixCall = c.fArguments.size() == 2 &&
85+
c.fArguments[1]->fType.kind() == Type::kMatrix_Kind;
8486
fArgs->fFormatArgs.push_back(
85-
Compiler::FormatArg(Compiler::FormatArg::Kind::kChildProcessor, index));
87+
Compiler::FormatArg(matrixCall ? Compiler::FormatArg::Kind::kChildProcessorWithMatrix
88+
: Compiler::FormatArg::Kind::kChildProcessor,
89+
index));
90+
while ((int) fArgs->fSampleMatrices.size() <= index) {
91+
fArgs->fSampleMatrices.push_back(SampleMatrix(SampleMatrix::Kind::kNone));
92+
}
93+
if (matrixCall) {
94+
fArgs->fSampleMatrices[index] = SampleMatrix(SampleMatrix::Kind::kVariable);
95+
}
8696
OutputStream* oldOut = fOut;
8797
StringStream buffer;
8898
fOut = &buffer;

0 commit comments

Comments
 (0)