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/include/gpu/GrFragmentProcessor.h b/include/gpu/GrFragmentProcessor.h
index 270ee16..d7011f8 100644
--- a/include/gpu/GrFragmentProcessor.h
+++ b/include/gpu/GrFragmentProcessor.h
@@ -67,9 +67,7 @@
     GrFragmentProcessor()
         : INHERITED()
         , fUsesDistanceVectorField(false)
-        , fUsesLocalCoords(false)
-        , fNumTexturesExclChildren(0)
-        , fNumBuffersExclChildren(0) {}
+        , fUsesLocalCoords(false) {}
 
     ~GrFragmentProcessor() override;
 
@@ -82,10 +80,6 @@
         }
     }
 
-    int numTexturesExclChildren() const { return fNumTexturesExclChildren; }
-
-    int numBuffersExclChildren() const { return fNumBuffersExclChildren; }
-
     int numCoordTransforms() const { return fCoordTransforms.count(); }
 
     /** Returns the coordinate transformation at index. index must be valid according to
@@ -96,12 +90,6 @@
         return fCoordTransforms;
     }
 
-    void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
-        if (!fCoordTransforms.empty()) {
-            outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
-        }
-    }
-
     int numChildProcessors() const { return fChildProcessors.count(); }
 
     const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; }
@@ -136,7 +124,7 @@
     /**
      * Pre-order traversal of a FP hierarchy, or of the forest of FPs in a GrPipeline. In the latter
      * case the tree rooted at each FP in the GrPipeline is visited successively.
-     * */
+     */
     class Iter : public SkNoncopyable {
     public:
         explicit Iter(const GrFragmentProcessor* fp) { fFPStack.push_back(fp); }
@@ -148,18 +136,41 @@
     };
 
     /**
-     * Iterates over all the GrCoordTransforms in a GrPipeline's GrFragmentProcessors. FPs are
-     * visited in the same order as Iter and each of an FP's coord transforms are visited linearly.
+     * Iterates over all the Ts owned by a GrFragmentProcessor and its children or over all the Ts
+     * owned by the forest of GrFragmentProcessors in a GrPipeline. FPs are visited in the same
+     * order as Iter and each of an FP's Ts are visited in order.
      */
-    class CoordTransformIter : public SkNoncopyable {
+    template <typename T, typename BASE,
+              int (BASE::*COUNT)() const,
+              const T& (BASE::*GET)(int) const>
+    class FPItemIter : public SkNoncopyable {
     public:
-        explicit CoordTransformIter(const GrPipeline& pipeline)
+        explicit FPItemIter(const GrFragmentProcessor* fp)
+                : fCurrFP(nullptr)
+                , fCTIdx(0)
+                , fFPIter(fp) {
+            fCurrFP = fFPIter.next();
+        }
+        explicit FPItemIter(const GrPipeline& pipeline)
                 : fCurrFP(nullptr)
                 , fCTIdx(0)
                 , fFPIter(pipeline) {
             fCurrFP = fFPIter.next();
         }
-        const GrCoordTransform* next();
+
+        const T* next() {
+            if (!fCurrFP) {
+                return nullptr;
+            }
+            while (fCTIdx == (fCurrFP->*COUNT)()) {
+                fCTIdx = 0;
+                fCurrFP = fFPIter.next();
+                if (!fCurrFP) {
+                    return nullptr;
+                }
+            }
+            return &(fCurrFP->*GET)(fCTIdx++);
+        }
 
     private:
         const GrFragmentProcessor*  fCurrFP;
@@ -167,6 +178,16 @@
         GrFragmentProcessor::Iter   fFPIter;
     };
 
+    using CoordTransformIter = FPItemIter<GrCoordTransform,
+                                          GrFragmentProcessor,
+                                          &GrFragmentProcessor::numCoordTransforms,
+                                          &GrFragmentProcessor::coordTransform>;
+
+    using TextureAccessIter = FPItemIter<GrTextureAccess,
+                                         GrProcessor,
+                                         &GrProcessor::numTextures,
+                                         &GrProcessor::textureAccess>;
+
 protected:
     void addTextureAccess(const GrTextureAccess* textureAccess) override;
     void addBufferAccess(const GrBufferAccess*) override;
@@ -241,33 +262,6 @@
     SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
 
     /**
-     * A processor stores the texture accesses of this proc, followed by all the accesses of this
-     * proc's children. In other words, each proc stores all the accesses of its subtree as if
-     * they were collected using preorder traversal.
-     *
-     * 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. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 accesses
-     * respectively. The following shows what the array of each proc's texture accesses would
-     * contain:
-     *
-     *                                   (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]
-     *
-     * The same goes for buffer accesses.
-     */
-    int                                         fNumTexturesExclChildren;
-    int                                         fNumBuffersExclChildren;
-
-    /**
      * This is not SkSTArray<1, sk_sp<GrFragmentProcessor>> because this class holds strong
      * references until notifyRefCntIsZero and then it holds pending executions.
      */
diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp
index d625d8e..31ff9ab 100644
--- a/src/effects/gradients/SkGradientShader.cpp
+++ b/src/effects/gradients/SkGradientShader.cpp
@@ -1248,7 +1248,7 @@
                                                 const char* gradientTValue,
                                                 const char* outputColor,
                                                 const char* inputColor,
-                                                const SamplerHandle* texSamplers) {
+                                                const TextureSamplers& texSamplers) {
     switch (ge.getColorType()) {
 #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS
         case kHardStopCentered_ColorType: {
diff --git a/src/effects/gradients/SkGradientShaderPriv.h b/src/effects/gradients/SkGradientShaderPriv.h
index d8d50e8..4e6a325 100644
--- a/src/effects/gradients/SkGradientShaderPriv.h
+++ b/src/effects/gradients/SkGradientShaderPriv.h
@@ -470,7 +470,7 @@
                    const char* gradientTValue,
                    const char* outputColor,
                    const char* inputColor,
-                   const SamplerHandle* texSamplers);
+                   const TextureSamplers&);
 
 private:
     enum {
diff --git a/src/gpu/GrFragmentProcessor.cpp b/src/gpu/GrFragmentProcessor.cpp
index 3dfe25e..7233658 100644
--- a/src/gpu/GrFragmentProcessor.cpp
+++ b/src/gpu/GrFragmentProcessor.cpp
@@ -57,40 +57,20 @@
 }
 
 void GrFragmentProcessor::addTextureAccess(const GrTextureAccess* textureAccess) {
-    // Can't add texture accesses after registering any children since their texture accesses have
-    // already been bubbled up into our fTextureAccesses array
-    SkASSERT(fChildProcessors.empty());
-
     INHERITED::addTextureAccess(textureAccess);
-    fNumTexturesExclChildren++;
 }
 
 void GrFragmentProcessor::addBufferAccess(const GrBufferAccess* bufferAccess) {
-    // Can't add buffer accesses after registering any children since their buffer accesses have
-    // already been bubbled up into our fBufferAccesses array
-    SkASSERT(fChildProcessors.empty());
-
     INHERITED::addBufferAccess(bufferAccess);
-    fNumBuffersExclChildren++;
 }
 
 void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
-    // Can't add transforms after registering any children since their transforms have already been
-    // bubbled up into our fCoordTransforms array
-    SkASSERT(fChildProcessors.empty());
-
     fCoordTransforms.push_back(transform);
     fUsesLocalCoords = fUsesLocalCoords || transform->sourceCoords() == kLocal_GrCoordSet;
     SkDEBUGCODE(transform->setInProcessor();)
 }
 
 int GrFragmentProcessor::registerChildProcessor(sk_sp<GrFragmentProcessor> child) {
-    // Append the child's textures array to our textures array
-    if (!child->fTextureAccesses.empty()) {
-        fTextureAccesses.push_back_n(child->fTextureAccesses.count(),
-                                     child->fTextureAccesses.begin());
-    }
-
     this->combineRequiredFeatures(*child);
 
     if (child->usesLocalCoords()) {
@@ -424,18 +404,3 @@
     return back;
 }
 
-//////////////////////////////////////////////////////////////////////////////
-
-const GrCoordTransform* GrFragmentProcessor::CoordTransformIter::next() {
-    if (!fCurrFP) {
-        return nullptr;
-    }
-    while (fCTIdx == fCurrFP->numCoordTransforms()) {
-        fCTIdx = 0;
-        fCurrFP = fFPIter.next();
-        if (!fCurrFP) {
-            return nullptr;
-        }
-    }
-    return &fCurrFP->coordTransform(fCTIdx++);
-}
diff --git a/src/gpu/GrPipeline.cpp b/src/gpu/GrPipeline.cpp
index f7ad882..d9ebcf8 100644
--- a/src/gpu/GrPipeline.cpp
+++ b/src/gpu/GrPipeline.cpp
@@ -176,15 +176,10 @@
 }
 
 static void add_dependencies_for_processor(const GrFragmentProcessor* proc, GrRenderTarget* rt) {
-    for (int i = 0; i < proc->numChildProcessors(); ++i) {
-        // need to recurse
-        add_dependencies_for_processor(&proc->childProcessor(i), rt);
-    }
-
-    for (int i = 0; i < proc->numTextures(); ++i) {
-        GrTexture* texture = proc->textureAccess(i).getTexture();
+    GrFragmentProcessor::TextureAccessIter iter(proc);
+    while (const GrTextureAccess* access = iter.next()) {
         SkASSERT(rt->getLastDrawTarget());
-        rt->getLastDrawTarget()->addDependency(texture);
+        rt->getLastDrawTarget()->addDependency(access->getTexture());
     }
 }
 
diff --git a/src/gpu/gl/GrGLProgram.cpp b/src/gpu/gl/GrGLProgram.cpp
index f34fce4..dd73d01 100644
--- a/src/gpu/gl/GrGLProgram.cpp
+++ b/src/gpu/gl/GrGLProgram.cpp
@@ -88,10 +88,9 @@
                                   const GrPipeline& pipeline) {
     this->generateMipmaps(primProc, pipeline.getAllowSRGBInputs());
 
-    int numProcessors = fFragmentProcessors.count();
-    for (int i = 0; i < numProcessors; ++i) {
-        const GrFragmentProcessor& processor = pipeline.getFragmentProcessor(i);
-        this->generateMipmaps(processor, pipeline.getAllowSRGBInputs());
+    GrFragmentProcessor::Iter iter(pipeline);
+    while (const GrFragmentProcessor* fp  = iter.next()) {
+        this->generateMipmaps(*fp, pipeline.getAllowSRGBInputs());
     }
 
     if (primProc.getPixelLocalStorageState() !=
@@ -104,12 +103,17 @@
 void GrGLProgram::setFragmentData(const GrPrimitiveProcessor& primProc,
                                   const GrPipeline& pipeline,
                                   int* nextSamplerIdx) {
-    int numProcessors = fFragmentProcessors.count();
-    for (int i = 0; i < numProcessors; ++i) {
-        const GrFragmentProcessor& processor = pipeline.getFragmentProcessor(i);
-        fFragmentProcessors[i]->setData(fProgramDataManager, processor);
-        this->bindTextures(processor, pipeline.getAllowSRGBInputs(), nextSamplerIdx);
+    GrFragmentProcessor::Iter iter(pipeline);
+    GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.begin(),
+                                           fFragmentProcessors.count());
+    const GrFragmentProcessor* fp = iter.next();
+    GrGLSLFragmentProcessor* glslFP = glslIter.next();
+    while (fp && glslFP) {
+        glslFP->setData(fProgramDataManager, *fp);
+        this->bindTextures(*fp, pipeline.getAllowSRGBInputs(), nextSamplerIdx);
+        fp = iter.next(), glslFP = glslIter.next();
     }
+    SkASSERT(!fp && !glslFP);
 }
 
 
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();
diff --git a/src/gpu/vk/GrVkGpuCommandBuffer.cpp b/src/gpu/vk/GrVkGpuCommandBuffer.cpp
index a584069..419e1b9 100644
--- a/src/gpu/vk/GrVkGpuCommandBuffer.cpp
+++ b/src/gpu/vk/GrVkGpuCommandBuffer.cpp
@@ -395,8 +395,9 @@
     SkASSERT(renderPass);
 
     prepare_sampled_images(primProc, fGpu);
-    for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
-        prepare_sampled_images(pipeline.getFragmentProcessor(i), fGpu);
+    GrFragmentProcessor::Iter iter(pipeline);
+    while (const GrFragmentProcessor* fp = iter.next()) {
+        prepare_sampled_images(*fp, fGpu);
     }
     prepare_sampled_images(pipeline.getXferProcessor(), fGpu);
 
diff --git a/src/gpu/vk/GrVkPipelineState.cpp b/src/gpu/vk/GrVkPipelineState.cpp
index 73ff62e..48724c0 100644
--- a/src/gpu/vk/GrVkPipelineState.cpp
+++ b/src/gpu/vk/GrVkPipelineState.cpp
@@ -198,11 +198,17 @@
                                 GrFragmentProcessor::CoordTransformIter(pipeline));
     append_texture_bindings(primProc, &textureBindings);
 
-    for (int i = 0; i < fFragmentProcessors.count(); ++i) {
-        const GrFragmentProcessor& processor = pipeline.getFragmentProcessor(i);
-        fFragmentProcessors[i]->setData(fDataManager, processor);
-        append_texture_bindings(processor, &textureBindings);
+    GrFragmentProcessor::Iter iter(pipeline);
+    GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.begin(),
+                                           fFragmentProcessors.count());
+    const GrFragmentProcessor* fp = iter.next();
+    GrGLSLFragmentProcessor* glslFP = glslIter.next();
+    while (fp && glslFP) {
+        glslFP->setData(fDataManager, *fp);
+        append_texture_bindings(*fp, &textureBindings);
+        fp = iter.next(), glslFP = glslIter.next();
     }
+    SkASSERT(!fp && !glslFP);
 
     fXferProcessor->setData(fDataManager, pipeline.getXferProcessor());
     append_texture_bindings(pipeline.getXferProcessor(), &textureBindings);