Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2014 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef GrFragmentProcessor_DEFINED
      9 #define GrFragmentProcessor_DEFINED
     10 
     11 #include "GrProcessor.h"
     12 
     13 class GrCoordTransform;
     14 class GrGLSLCaps;
     15 class GrGLSLFragmentProcessor;
     16 class GrInvariantOutput;
     17 class GrProcessorKeyBuilder;
     18 
     19 /** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and
     20     produce an output color. They may reference textures and uniforms. They may use
     21     GrCoordTransforms to receive a transformation of the local coordinates that map from local space
     22     to the fragment being processed.
     23  */
     24 class GrFragmentProcessor : public GrProcessor {
     25 public:
     26     /**
     27     *  In many instances (e.g. SkShader::asFragmentProcessor() implementations) it is desirable to
     28     *  only consider the input color's alpha. However, there is a competing desire to have reusable
     29     *  GrFragmentProcessor subclasses that can be used in other scenarios where the entire input
     30     *  color is considered. This function exists to filter the input color and pass it to a FP. It
     31     *  does so by returning a parent FP that multiplies the passed in FPs output by the parent's
     32     *  input alpha. The passed in FP will not receive an input color.
     33     */
     34     static const GrFragmentProcessor* MulOutputByInputAlpha(const GrFragmentProcessor*);
     35 
     36     /**
     37      *  Similar to the above but it modulates the output r,g,b of the child processor by the input
     38      *  rgb and then multiplies all the components by the input alpha. This effectively modulates
     39      *  the child processor's premul color by a unpremul'ed input and produces a premul output
     40      */
     41     static const GrFragmentProcessor* MulOutputByInputUnpremulColor(const GrFragmentProcessor*);
     42 
     43     /**
     44      *  Returns a parent fragment processor that adopts the passed fragment processor as a child.
     45      *  The parent will ignore its input color and instead feed the passed in color as input to the
     46      *  child.
     47      */
     48     static const GrFragmentProcessor* OverrideInput(const GrFragmentProcessor*, GrColor);
     49 
     50     /**
     51      * Returns a fragment processor that runs the passed in array of fragment processors in a
     52      * series. The original input is passed to the first, the first's output is passed to the
     53      * second, etc. The output of the returned processor is the output of the last processor of the
     54      * series.
     55      */
     56     static const GrFragmentProcessor* RunInSeries(const GrFragmentProcessor*[], int cnt);
     57 
     58     GrFragmentProcessor()
     59         : INHERITED()
     60         , fUsesLocalCoords(false)
     61         , fNumTexturesExclChildren(0)
     62         , fNumTransformsExclChildren(0) {}
     63 
     64     ~GrFragmentProcessor() override;
     65 
     66     GrGLSLFragmentProcessor* createGLSLInstance() const;
     67 
     68     void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
     69         this->onGetGLSLProcessorKey(caps, b);
     70         for (int i = 0; i < fChildProcessors.count(); ++i) {
     71             fChildProcessors[i]->getGLSLProcessorKey(caps, b);
     72         }
     73     }
     74 
     75     int numTexturesExclChildren() const { return fNumTexturesExclChildren; }
     76 
     77     int numTransformsExclChildren() const { return fNumTransformsExclChildren; }
     78 
     79     int numTransforms() const { return fCoordTransforms.count(); }
     80 
     81     /** Returns the coordinate transformation at index. index must be valid according to
     82         numTransforms(). */
     83     const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
     84 
     85     const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
     86         return fCoordTransforms;
     87     }
     88 
     89     void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
     90         if (!fCoordTransforms.empty()) {
     91             outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
     92         }
     93     }
     94 
     95     int numChildProcessors() const { return fChildProcessors.count(); }
     96 
     97     const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; }
     98 
     99     /** Do any of the coordtransforms for this processor require local coords? */
    100     bool usesLocalCoords() const { return fUsesLocalCoords; }
    101 
    102     /** Returns true if this and other processor conservatively draw identically. It can only return
    103         true when the two processor are of the same subclass (i.e. they return the same object from
    104         from getFactory()).
    105 
    106         A return value of true from isEqual() should not be used to test whether the processor would
    107         generate the same shader code. To test for identical code generation use getGLSLProcessorKey
    108      */
    109     bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
    110 
    111     /**
    112      * This function is used to perform optimizations. When called the invarientOuput param
    113      * indicate whether the input components to this processor in the FS will have known values.
    114      * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
    115      * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
    116      * inout to indicate known values of its output. A component of the color member only has
    117      * meaning if the corresponding bit in validFlags is set.
    118      */
    119     void computeInvariantOutput(GrInvariantOutput* inout) const {
    120         this->onComputeInvariantOutput(inout);
    121     }
    122 
    123 protected:
    124     void addTextureAccess(const GrTextureAccess* textureAccess) override;
    125 
    126     /**
    127      * Fragment Processor subclasses call this from their constructor to register coordinate
    128      * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
    129      * in their FS code. The matrix expresses a transformation from local space. For a given
    130      * fragment the matrix will be applied to the local coordinate that maps to the fragment.
    131      *
    132      * When the transformation has perspective, the transformed coordinates will have
    133      * 3 components. Otherwise they'll have 2.
    134      *
    135      * This must only be called from the constructor because GrProcessors are immutable. The
    136      * processor subclass manages the lifetime of the transformations (this function only stores a
    137      * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
    138      *
    139      * A processor subclass that has multiple methods of construction should always add its coord
    140      * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
    141      * compares transforms and will assume they line up across the two processor instances.
    142      */
    143     void addCoordTransform(const GrCoordTransform*);
    144 
    145     /**
    146      * FragmentProcessor subclasses call this from their constructor to register any child
    147      * FragmentProcessors they have. This must be called AFTER all texture accesses and coord
    148      * transforms have been added.
    149      * This is for processors whose shader code will be composed of nested processors whose output
    150      * colors will be combined somehow to produce its output color.  Registering these child
    151      * processors will allow the ProgramBuilder to automatically handle their transformed coords and
    152      * texture accesses and mangle their uniform and output color names.
    153      */
    154     int registerChildProcessor(const GrFragmentProcessor* child);
    155 
    156     /**
    157      * Subclass implements this to support getConstantColorComponents(...).
    158      *
    159      * Note: it's up to the subclass implementation to do any recursive call to compute the child
    160      * procs' output invariants; computeInvariantOutput will not be recursive.
    161      */
    162     virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
    163 
    164 private:
    165     void notifyRefCntIsZero() const final;
    166 
    167     /** Returns a new instance of the appropriate *GL* implementation class
    168         for the given GrFragmentProcessor; caller is responsible for deleting
    169         the object. */
    170     virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const = 0;
    171 
    172     /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
    173     virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
    174                                        GrProcessorKeyBuilder* b) const = 0;
    175 
    176     /**
    177      * Subclass implements this to support isEqual(). It will only be called if it is known that
    178      * the two processors are of the same subclass (i.e. they return the same object from
    179      * getFactory()). The processor subclass should not compare its coord transforms as that will
    180      * be performed automatically in the non-virtual isEqual().
    181      */
    182     virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
    183 
    184     bool hasSameTransforms(const GrFragmentProcessor&) const;
    185 
    186     bool                                            fUsesLocalCoords;
    187 
    188     /**
    189      * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
    190      * proc's children. In other words, each proc stores all the transforms of its subtree as if
    191      * they were collected using preorder traversal.
    192      *
    193      * Example:
    194      * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
    195      * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
    196      * respectively. The following shows what the fCoordTransforms array of each proc would contain:
    197      *
    198      *                                   (A)
    199      *                        [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
    200      *                                  /    \
    201      *                                /        \
    202      *                            (B)           (D)
    203      *                        [b1,b2,c1]   [d1,e1,e2,e3,f1,f2]
    204      *                          /             /    \
    205      *                        /             /        \
    206      *                      (C)          (E)          (F)
    207      *                     [c1]      [e1,e2,e3]      [f1,f2]
    208      *
    209      * The same goes for fTextureAccesses with textures.
    210      */
    211     SkSTArray<4, const GrCoordTransform*, true>     fCoordTransforms;
    212     int                                             fNumTexturesExclChildren;
    213     int                                             fNumTransformsExclChildren;
    214     SkSTArray<1, const GrFragmentProcessor*, true>  fChildProcessors;
    215 
    216     typedef GrProcessor INHERITED;
    217 };
    218 
    219 #endif
    220