GrFragmentProcessor.h revision 54017d7e5b09e7ca58346ea72ff065677fcd7be1
16251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon/*
26251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon * Copyright 2014 Google Inc.
36251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon *
46251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon * Use of this source code is governed by a BSD-style license that can be
56251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon * found in the LICENSE file.
66251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon */
76251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
86251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon#ifndef GrFragmentProcessor_DEFINED
96251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon#define GrFragmentProcessor_DEFINED
106251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
116251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon#include "GrProcessor.h"
1258d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix#include "GrStagedProcessor.h"
136251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
146251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomonclass GrCoordTransform;
15e9c0fc616d2a1632c285885b9b656b68ca8d4f24jvanverthclass GrGLSLCaps;
16eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualittclass GrGLFragmentProcessor;
17eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualittclass GrProcessorKeyBuilder;
186251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
196251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon/** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and
206251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    produce an output color. They may reference textures and uniforms. They may use
216251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    GrCoordTransforms to receive a transformation of the local coordinates that map from local space
226251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    to the fragment being processed.
236251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon */
246251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomonclass GrFragmentProcessor : public GrProcessor {
256251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomonpublic:
266251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    GrFragmentProcessor()
276251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon        : INHERITED()
28290c09b8bbd8d221d363150e2ce87158f4668df0joshualitt        , fUsesLocalCoords(false) {}
296251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
30eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt    /** Returns a new instance of the appropriate *GL* implementation class
31eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt        for the given GrFragmentProcessor; caller is responsible for deleting
32eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt        the object. */
33eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt    virtual GrGLFragmentProcessor* createGLInstance() const = 0;
34eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt
35eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt    /** Human-meaningful string to identify this GrFragmentProcessor; may be embedded
36eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt        in generated shader code. */
37eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt    virtual const char* name() const = 0;
386251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
394b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix    void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
404b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix        this->onGetGLProcessorKey(caps, b);
414b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix        for (int i = 0; i < fChildProcessors.count(); ++i) {
4258d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix            fChildProcessors[i].processor()->getGLProcessorKey(caps, b);
434b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix        }
444b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix    }
454b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix
466251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    int numTransforms() const { return fCoordTransforms.count(); }
476251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
486251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    /** Returns the coordinate transformation at index. index must be valid according to
496251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon        numTransforms(). */
506251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
516251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
52abb52a1a70a81915c6196e0fb3e9bcb05e8be14djoshualitt    const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
53abb52a1a70a81915c6196e0fb3e9bcb05e8be14djoshualitt        return fCoordTransforms;
54abb52a1a70a81915c6196e0fb3e9bcb05e8be14djoshualitt    }
55abb52a1a70a81915c6196e0fb3e9bcb05e8be14djoshualitt
562fe7923f7e74f901a17afba7609d71a1220bdc60joshualitt    void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
5758d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix        if (!fCoordTransforms.empty()) {
5858d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix            outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
592fe7923f7e74f901a17afba7609d71a1220bdc60joshualitt        }
602fe7923f7e74f901a17afba7609d71a1220bdc60joshualitt    }
612fe7923f7e74f901a17afba7609d71a1220bdc60joshualitt
624b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix    int numChildProcessors() const { return fChildProcessors.count(); }
634b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix
6458d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix    const GrFragmentProcessor& childProcessor(int index) const {
6558d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix        return *fChildProcessors[index].processor();
664b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix    }
674b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix
68290c09b8bbd8d221d363150e2ce87158f4668df0joshualitt    /** Do any of the coordtransforms for this processor require local coords? */
69290c09b8bbd8d221d363150e2ce87158f4668df0joshualitt    bool usesLocalCoords() const { return fUsesLocalCoords; }
70290c09b8bbd8d221d363150e2ce87158f4668df0joshualitt
71eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt    /** Returns true if this and other processor conservatively draw identically. It can only return
72eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt        true when the two processor are of the same subclass (i.e. they return the same object from
736251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon        from getFactory()).
746251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
75eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt        A return value of true from isEqual() should not be used to test whether the processor would
76eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt        generate the same shader code. To test for identical code generation use getGLProcessorKey*/
7754017d7e5b09e7ca58346ea72ff065677fcd7be1wangyix    bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
786251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
7956995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    /**
8056995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt     * This function is used to perform optimizations. When called the invarientOuput param
8156995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt     * indicate whether the input components to this processor in the FS will have known values.
8256995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt     * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
8356995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt     * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
8456995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt     * inout to indicate known values of its output. A component of the color member only has
8556995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt     * meaning if the corresponding bit in validFlags is set.
8656995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt     */
8756995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    void computeInvariantOutput(GrInvariantOutput* inout) const;
8856995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt
896251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomonprotected:
906251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    /**
916251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon     * Fragment Processor subclasses call this from their constructor to register coordinate
92de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
93de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * in their FS code. The matrix expresses a transformation from local space. For a given
94de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * fragment the matrix will be applied to the local coordinate that maps to the fragment.
95de258cd6b402c4da78b66e88191ad02162d87916bsalomon     *
96de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * When the transformation has perspective, the transformed coordinates will have
97de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * 3 components. Otherwise they'll have 2.
98de258cd6b402c4da78b66e88191ad02162d87916bsalomon     *
99de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * This must only be called from the constructor because GrProcessors are immutable. The
100de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * processor subclass manages the lifetime of the transformations (this function only stores a
101de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
102de258cd6b402c4da78b66e88191ad02162d87916bsalomon     *
103de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * A processor subclass that has multiple methods of construction should always add its coord
104de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
105de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * compares transforms and will assume they line up across the two processor instances.
1066251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon     */
1076251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    void addCoordTransform(const GrCoordTransform*);
1086251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
1096251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    /**
11058d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix     * FragmentProcessor subclasses call this from their constructor to register any child
11158d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix     * FragmentProcessors they have.
1124b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix     * This is for processors whose shader code will be composed of nested processors whose output
1134b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix     * colors will be combined somehow to produce its output color.  Registering these child
11458d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix     * processors will allow the ProgramBuilder to automatically handle their transformed coords and
11558d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix     * texture accesses and mangle their uniform and output color names.
1164b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix     */
11758d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix    int registerChildProcessor(const GrFragmentProcessor* child);
1184b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix
1194b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix    /**
12056995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt     * Subclass implements this to support getConstantColorComponents(...).
12154017d7e5b09e7ca58346ea72ff065677fcd7be1wangyix     *
12254017d7e5b09e7ca58346ea72ff065677fcd7be1wangyix     * Note: it's up to the subclass implementation to do any recursive call to compute the child
12354017d7e5b09e7ca58346ea72ff065677fcd7be1wangyix     * procs' output invariants; computeInvariantOutput will not be recursive.
12456995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt     */
12556995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
12656995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt
1276251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomonprivate:
1284b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix    /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
1294b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix    virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
1304b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix                                     GrProcessorKeyBuilder* b) const = 0;
1314b3050b410254d0cb38df9a30ae2e209124fa1a2wangyix
132de258cd6b402c4da78b66e88191ad02162d87916bsalomon    /**
133de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * Subclass implements this to support isEqual(). It will only be called if it is known that
134de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * the two processors are of the same subclass (i.e. they return the same object from
135de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * getFactory()). The processor subclass should not compare its coord transforms as that will
136de258cd6b402c4da78b66e88191ad02162d87916bsalomon     * be performed automatically in the non-virtual isEqual().
137de258cd6b402c4da78b66e88191ad02162d87916bsalomon     */
138de258cd6b402c4da78b66e88191ad02162d87916bsalomon    virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
139de258cd6b402c4da78b66e88191ad02162d87916bsalomon
140de258cd6b402c4da78b66e88191ad02162d87916bsalomon    bool hasSameTransforms(const GrFragmentProcessor&) const;
1416251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
142290c09b8bbd8d221d363150e2ce87158f4668df0joshualitt    bool                                         fUsesLocalCoords;
14358d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix
14458d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix    /**
14569ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
14669ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     * proc's children. In other words, each proc stores all the transforms of its subtree as if
14769ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     * they were collected using preorder traversal.
14869ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *
14969ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     * Example:
15069ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
15169ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
15269ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     * respectively. The following shows what the fCoordTransforms array of each proc would contain:
15369ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *
15469ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *                                   (A)
15569ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *                        [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
15669ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *                                  /    \
15769ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *                                /        \
15869ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *                            (B)           (D)
15969ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *                        [b1,b2,c1]   [d1,e1,e2,e3,f1,f2]
16069ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *                          /             /    \
16169ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *                        /             /        \
16269ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *                      (C)          (E)          (F)
16369ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *                     [c1]      [e1,e2,e3]      [f1,f2]
16469ed114d388c56e83a2385d8b8182e6d05a0b073wangyix     *
16558d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix     * The same goes for fTextureAccesses with textures.
16658d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix     */
16758d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix    SkSTArray<4, const GrCoordTransform*, true>  fCoordTransforms;
16858d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix
16958d890bd45a201477ec6c4ba0ae61c574b4e5aefwangyix    SkTArray<GrFragmentStage, false>             fChildProcessors;
1706251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
1716251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon    typedef GrProcessor INHERITED;
1726251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon};
1736251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon
1746251d17dfadbbeba8a7e72affde5cbdbd0c0c95fbsalomon#endif
175