bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 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 | |
| 15 | /** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and |
| 16 | produce an output color. They may reference textures and uniforms. They may use |
| 17 | GrCoordTransforms to receive a transformation of the local coordinates that map from local space |
| 18 | to the fragment being processed. |
| 19 | */ |
| 20 | class GrFragmentProcessor : public GrProcessor { |
| 21 | public: |
| 22 | GrFragmentProcessor() |
| 23 | : INHERITED() |
| 24 | , fWillReadDstColor(false) |
| 25 | , fWillUseInputColor(true) {} |
| 26 | |
| 27 | virtual const GrBackendFragmentProcessorFactory& getFactory() const = 0; |
| 28 | |
| 29 | int numTransforms() const { return fCoordTransforms.count(); } |
| 30 | |
| 31 | /** Returns the coordinate transformation at index. index must be valid according to |
| 32 | numTransforms(). */ |
| 33 | const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; } |
| 34 | |
| 35 | /** Will this prceossor read the destination pixel value? */ |
| 36 | bool willReadDstColor() const { return fWillReadDstColor; } |
| 37 | |
| 38 | /** Will this prceossor read the source color value? */ |
| 39 | bool willUseInputColor() const { return fWillUseInputColor; } |
| 40 | |
| 41 | /** Returns true if this and other prceossor conservatively draw identically. It can only return |
| 42 | true when the two prceossor are of the same subclass (i.e. they return the same object from |
| 43 | from getFactory()). |
| 44 | |
| 45 | A return value of true from isEqual() should not be used to test whether the prceossor would |
| 46 | generate the same shader code. To test for identical code generation use the prceossor' keys |
| 47 | computed by the GrBackendProcessorFactory. */ |
| 48 | bool isEqual(const GrFragmentProcessor& other) const { |
| 49 | if (&this->getFactory() != &other.getFactory()) { |
| 50 | return false; |
| 51 | } |
| 52 | bool result = this->onIsEqual(other); |
| 53 | #ifdef SK_DEBUG |
| 54 | if (result) { |
| 55 | this->assertTexturesEqual(other); |
| 56 | } |
| 57 | #endif |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | protected: |
| 62 | /** |
| 63 | * Fragment Processor subclasses call this from their constructor to register coordinate |
| 64 | * transformations. The processor subclass manages the lifetime of the transformations (this |
| 65 | * function only stores a pointer). The GrCoordTransform is typically a member field of the |
| 66 | * GrProcessor subclass. When the matrix has perspective, the transformed coordinates will have |
| 67 | * 3 components. Otherwise they'll have 2. This must only be called from the constructor because |
| 68 | * GrProcessors are immutable. |
| 69 | */ |
| 70 | void addCoordTransform(const GrCoordTransform*); |
| 71 | |
| 72 | /** |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame^] | 73 | * If the prceossor subclass will read the destination pixel value then it must call this |
| 74 | * function from its constructor. Otherwise, when its generated backend-specific prceossor class |
| 75 | * attempts to generate code that reads the destination pixel it will fail. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 76 | */ |
| 77 | void setWillReadDstColor() { fWillReadDstColor = true; } |
| 78 | |
| 79 | /** |
| 80 | * If the prceossor will generate a result that does not depend on the input color value then it |
| 81 | * must call this function from its constructor. Otherwise, when its generated backend-specific |
| 82 | * code might fail during variable binding due to unused variables. |
| 83 | */ |
| 84 | void setWillNotUseInputColor() { fWillUseInputColor = false; } |
| 85 | |
| 86 | private: |
| 87 | /** Subclass implements this to support isEqual(). It will only be called if it is known that |
| 88 | the two prceossor are of the same subclass (i.e. they return the same object from |
| 89 | getFactory()).*/ |
| 90 | virtual bool onIsEqual(const GrFragmentProcessor& other) const = 0; |
| 91 | |
| 92 | SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; |
| 93 | bool fWillReadDstColor; |
| 94 | bool fWillUseInputColor; |
| 95 | |
| 96 | typedef GrProcessor INHERITED; |
| 97 | }; |
| 98 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 99 | #endif |