Stop aggregating texture/buffer access objects in GrFragmentProcessor parents.
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2349243002
Review-Url: https://codereview.chromium.org/2365943003
diff --git a/src/gpu/glsl/GrGLSLFragmentProcessor.cpp b/src/gpu/glsl/GrGLSLFragmentProcessor.cpp
index 82a07ca..5ae7fee 100644
--- a/src/gpu/glsl/GrGLSLFragmentProcessor.cpp
+++ b/src/gpu/glsl/GrGLSLFragmentProcessor.cpp
@@ -42,58 +42,13 @@
const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
- /*
- * TODO: Move textures and buffers to the iterator model used by coords.
- * We now want to find the subset of samplers that belong to the child and its descendants and
- * put that into childSamplers. To do so, we'll do a forwards linear search.
- *
- * Explanation:
- * Each GrFragmentProcessor has a copy of all the textures of itself and all procs in its
- * subtree. For example, suppose we have frag proc A, who has two children B and D. B has a
- * child C, and D has two children E and F. Each frag proc's textures array contains its own
- * textures, followed by the textures of all its descendants (i.e. preorder traversal). Suppose
- * procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 textures respectively.
- *
- * (A)
- * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
- * / \
- * / \
- * (B) (D)
- * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
- * / / \
- * / / \
- * (C) (E) (F)
- * [c1] [e1,e2,e3] [f1,f2]
- *
- * So if we're inside proc A's emitCode, and A is about to call emitCode on proc D, we want the
- * EmitArgs that's passed onto D to only contain its and its descendants' textures. The
- * EmitArgs given to A would contain the textures [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2], and we want
- * to extract the subset [d1,e1,e2,e3,f1,f2] to pass on to D. We can do this with a linear
- * search since we know that A has 1 texture (using A.numTexturesExclChildren()), and B's
- * subtree has 3 textures (using B.numTextures()), so we know the start of D's textures is
- * 4 after the start of A's textures.
- * Textures work the same way as textures.
- */
- int firstTextureAt = args.fFp.numTexturesExclChildren();
- int firstBufferAt = args.fFp.numBuffersExclChildren();
- for (int i = 0; i < childIndex; ++i) {
- firstTextureAt += args.fFp.childProcessor(i).numTextures();
- firstBufferAt += args.fFp.childProcessor(i).numBuffers();
- }
- const SamplerHandle* childTexSamplers = nullptr;
- const SamplerHandle* childBufferSamplers = nullptr;
- if (childProc.numTextures() > 0) {
- childTexSamplers = &args.fTexSamplers[firstTextureAt];
- }
- if (childProc.numBuffers() > 0) {
- childBufferSamplers = &args.fBufferSamplers[firstBufferAt];
- }
-
// emit the code for the child in its own scope
fragBuilder->codeAppend("{\n");
fragBuilder->codeAppendf("// Child Index %d (mangle: %s): %s\n", childIndex,
fragBuilder->getMangleString().c_str(), childProc.name());
- TransformedCoordVars coordVars = args.fTransformedCoords.childTransforms(childIndex);
+ TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
+ TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
+ BufferSamplers bufferSamplers = args.fBufferSamplers.childInputs(childIndex);
EmitArgs childArgs(fragBuilder,
args.fUniformHandler,
args.fGLSLCaps,
@@ -101,8 +56,8 @@
outputColor,
inputColor,
coordVars,
- childTexSamplers,
- childBufferSamplers,
+ textureSamplers,
+ bufferSamplers,
args.fGpImplementsDistanceVector);
this->childProcessor(childIndex)->emitCode(childArgs);
fragBuilder->codeAppend("}\n");
@@ -112,16 +67,14 @@
//////////////////////////////////////////////////////////////////////////////
-using TransformedCoordVars = GrGLSLFragmentProcessor::TransformedCoordVars;
-TransformedCoordVars TransformedCoordVars::childTransforms(int childIdx) const {
- const GrFragmentProcessor* child = &fFP->childProcessor(childIdx);
- GrFragmentProcessor::Iter iter(fFP);
- int numToSkip = 0;
- while (true) {
- const GrFragmentProcessor* fp = iter.next();
- if (fp == child) {
- return TransformedCoordVars(child, fTransformedVars + numToSkip);
- }
- numToSkip += fp->numCoordTransforms();
+GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::next() {
+ if (fFPStack.empty()) {
+ return nullptr;
}
+ GrGLSLFragmentProcessor* back = fFPStack.back();
+ fFPStack.pop_back();
+ for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
+ fFPStack.push_back(back->childProcessor(i));
+ }
+ return back;
}
diff --git a/src/gpu/glsl/GrGLSLFragmentProcessor.h b/src/gpu/glsl/GrGLSLFragmentProcessor.h
index 9889bcc..d2f00f8 100644
--- a/src/gpu/glsl/GrGLSLFragmentProcessor.h
+++ b/src/gpu/glsl/GrGLSLFragmentProcessor.h
@@ -33,30 +33,49 @@
typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
typedef GrGLSLProgramDataManager::UniformHandle SamplerHandle;
+private:
/**
- * When building a program from a GrPipeline this is used to provide the GrShaderVars that
- * contain the resulting transformed coords from each of a GrFragmentProcessor's
- * GrCoordTransforms. This allows the GrFragmentProcessor subclasses to refer to the transformed
- * coords in fragment code.
+ * This class allows the shader builder to provide each GrGLSLFragmentProcesor with an array of
+ * generated variables where each generated variable corresponds to an element of an array on
+ * the GrFragmentProcessor that generated the GLSLFP. For example, this is used to provide a
+ * variable holding transformed coords for each GrCoordTransform owned by the FP.
*/
- class TransformedCoordVars {
+ template <typename T, typename FPBASE, int (FPBASE::*COUNT)() const>
+ class BuilderInputProvider {
public:
- TransformedCoordVars(const GrFragmentProcessor* fp, const GrShaderVar* vars)
- : fFP(fp)
- , fTransformedVars(vars) {}
+ BuilderInputProvider(const GrFragmentProcessor* fp, const T* ts) : fFP(fp) , fTs(ts) {}
- const GrShaderVar& operator[] (int i) const {
- SkASSERT(i >= 0 && i < fFP->numCoordTransforms());
- return fTransformedVars[i];
+ const T& operator[] (int i) const {
+ SkASSERT(i >= 0 && i < (fFP->*COUNT)());
+ return fTs[i];
}
- TransformedCoordVars childTransforms(int childIdx) const;
+ BuilderInputProvider childInputs(int childIdx) const {
+ const GrFragmentProcessor* child = &fFP->childProcessor(childIdx);
+ GrFragmentProcessor::Iter iter(fFP);
+ int numToSkip = 0;
+ while (true) {
+ const GrFragmentProcessor* fp = iter.next();
+ if (fp == child) {
+ return BuilderInputProvider(child, fTs + numToSkip);
+ }
+ numToSkip += (fp->*COUNT)();
+ }
+ }
private:
const GrFragmentProcessor* fFP;
- const GrShaderVar* fTransformedVars;
+ const T* fTs;
};
+public:
+ using TransformedCoordVars = BuilderInputProvider<GrShaderVar, GrFragmentProcessor,
+ &GrFragmentProcessor::numCoordTransforms>;
+ using TextureSamplers = BuilderInputProvider<SamplerHandle, GrProcessor,
+ &GrProcessor::numTextures>;
+ using BufferSamplers = BuilderInputProvider<SamplerHandle, GrProcessor,
+ &GrProcessor::numBuffers>;
+
/** Called when the program stage should insert its code into the shaders. The code in each
shader will be in its own block ({}) and so locally scoped names will not collide across
stages.
@@ -90,8 +109,8 @@
const char* outputColor,
const char* inputColor,
const TransformedCoordVars& transformedCoordVars,
- const SamplerHandle* texSamplers,
- const SamplerHandle* bufferSamplers,
+ const TextureSamplers& textureSamplers,
+ const BufferSamplers& bufferSamplers,
bool gpImplementsDistanceVector)
: fFragBuilder(fragBuilder)
, fUniformHandler(uniformHandler)
@@ -100,7 +119,7 @@
, fOutputColor(outputColor)
, fInputColor(inputColor)
, fTransformedCoords(transformedCoordVars)
- , fTexSamplers(texSamplers)
+ , fTexSamplers(textureSamplers)
, fBufferSamplers(bufferSamplers)
, fGpImplementsDistanceVector(gpImplementsDistanceVector) {}
GrGLSLFPFragmentBuilder* fFragBuilder;
@@ -110,8 +129,8 @@
const char* fOutputColor;
const char* fInputColor;
const TransformedCoordVars& fTransformedCoords;
- const SamplerHandle* fTexSamplers;
- const SamplerHandle* fBufferSamplers;
+ const TextureSamplers& fTexSamplers;
+ const BufferSamplers& fBufferSamplers;
bool fGpImplementsDistanceVector;
};
@@ -123,7 +142,7 @@
int numChildProcessors() const { return fChildProcessors.count(); }
- GrGLSLFragmentProcessor* childProcessor(int index) const {
+ GrGLSLFragmentProcessor* childProcessor(int index) {
return fChildProcessors[index];
}
@@ -141,6 +160,24 @@
/** Variation that uses the parent's output color variable to hold the child's output.*/
void emitChild(int childIndex, const char* inputColor, EmitArgs& parentArgs);
+ /**
+ * Pre-order traversal of a GLSLFP hierarchy, or of multiple trees with roots in an array of
+ * GLSLFPS. This agrees with the traversal order of GrFragmentProcessor::Iter
+ */
+ class Iter : public SkNoncopyable {
+ public:
+ explicit Iter(GrGLSLFragmentProcessor* fp) { fFPStack.push_back(fp); }
+ explicit Iter(GrGLSLFragmentProcessor* fps[], int cnt) {
+ for (int i = cnt - 1; i >= 0; --i) {
+ fFPStack.push_back(fps[i]);
+ }
+ }
+ GrGLSLFragmentProcessor* next();
+
+ private:
+ SkSTArray<4, GrGLSLFragmentProcessor*, true> fFPStack;
+ };
+
protected:
/** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
the same stage key; this function reads data from a GrFragmentProcessor and uploads any
diff --git a/src/gpu/glsl/GrGLSLProgramBuilder.cpp b/src/gpu/glsl/GrGLSLProgramBuilder.cpp
index 29470c4..abfeafd 100644
--- a/src/gpu/glsl/GrGLSLProgramBuilder.cpp
+++ b/src/gpu/glsl/GrGLSLProgramBuilder.cpp
@@ -158,12 +158,17 @@
GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
- SkSTArray<4, SamplerHandle> texSamplers(fp.numTextures());
- SkSTArray<2, SamplerHandle> bufferSamplers(fp.numBuffers());
- this->emitSamplers(fp, &texSamplers, &bufferSamplers);
+ SkSTArray<4, SamplerHandle> textureSamplerArray(fp.numTextures());
+ SkSTArray<2, SamplerHandle> bufferSamplerArray(fp.numBuffers());
+ GrFragmentProcessor::Iter iter(&fp);
+ while (const GrFragmentProcessor* subFP = iter.next()) {
+ this->emitSamplers(*subFP, &textureSamplerArray, &bufferSamplerArray);
+ }
const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
+ GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, textureSamplerArray.begin());
+ GrGLSLFragmentProcessor::BufferSamplers bufferSamplers(&fp, bufferSamplerArray.begin());
GrGLSLFragmentProcessor::EmitArgs args(&fFS,
this->uniformHandler(),
this->glslCaps(),
@@ -171,8 +176,8 @@
output->c_str(),
input.isOnes() ? nullptr : input.c_str(),
coords,
- texSamplers.begin(),
- bufferSamplers.begin(),
+ textureSamplers,
+ bufferSamplers,
this->primitiveProcessor().implementsDistanceVector());
fragProc->emitCode(args);
@@ -248,7 +253,7 @@
1 << GrGLSLShaderBuilder::kExternalTexture_GLSLPrivateFeature,
externalFeatureString);
}
- name.printf("TextureSampler%d", t);
+ name.printf("TextureSampler_%d", outTexSamplers->count());
this->emitSampler(samplerType, access.getTexture()->config(),
name.c_str(), access.getVisibility(), outTexSamplers);
}
@@ -259,7 +264,7 @@
for (int b = 0; b < numBuffers; ++b) {
const GrBufferAccess& access = processor.bufferAccess(b);
- name.printf("BufferSampler%d", b);
+ name.printf("BufferSampler_%d", outBufferSamplers->count());
this->emitSampler(kTextureBufferSampler_GrSLType, access.texelConfig(), name.c_str(),
access.visibility(), outBufferSamplers);
texelBufferVisibility |= access.visibility();