egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 1 | /* |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 2 | * Copyright 2015 Google Inc. |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 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 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 8 | #ifndef GrPipeline_DEFINED |
| 9 | #define GrPipeline_DEFINED |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 10 | |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 11 | #include "GrColor.h" |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 12 | #include "GrFragmentProcessor.h" |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 13 | #include "GrGpu.h" |
joshualitt | dbe1e6f | 2015-07-16 08:12:45 -0700 | [diff] [blame] | 14 | #include "GrNonAtomicRef.h" |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 15 | #include "GrPendingProgramElement.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 16 | #include "GrPrimitiveProcessor.h" |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 17 | #include "GrProgramDesc.h" |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 18 | #include "GrStencil.h" |
| 19 | #include "GrTypesPriv.h" |
| 20 | #include "SkMatrix.h" |
| 21 | #include "SkRefCnt.h" |
| 22 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 23 | class GrBatch; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 24 | class GrDeviceCoordTexture; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 25 | class GrPipelineBuilder; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 26 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 27 | /** |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 28 | * Class that holds an optimized version of a GrPipelineBuilder. It is meant to be an immutable |
| 29 | * class, and contains all data needed to set the state for a gpu draw. |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 30 | */ |
joshualitt | dbe1e6f | 2015-07-16 08:12:45 -0700 | [diff] [blame] | 31 | class GrPipeline : public GrNonAtomicRef { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 32 | public: |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 33 | /////////////////////////////////////////////////////////////////////////// |
| 34 | /// @name Creation |
| 35 | |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 36 | struct CreateArgs { |
| 37 | const GrPipelineBuilder* fPipelineBuilder; |
| 38 | const GrCaps* fCaps; |
| 39 | GrProcOptInfo fColorPOI; |
| 40 | GrProcOptInfo fCoveragePOI; |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame^] | 41 | const GrScissorState* fScissor; |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 42 | GrXferProcessor::DstTexture fDstTexture; |
| 43 | }; |
| 44 | |
bsalomon | 47dfc36 | 2015-08-10 08:23:11 -0700 | [diff] [blame] | 45 | /** Creates a pipeline into a pre-allocated buffer */ |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 46 | static GrPipeline* CreateAt(void* memory, const CreateArgs&, GrPipelineOptimizations*); |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 47 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 48 | /// @} |
| 49 | |
| 50 | /////////////////////////////////////////////////////////////////////////// |
| 51 | /// @name Comparisons |
| 52 | |
| 53 | /** |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 54 | * Returns true if these pipelines are equivalent. Coord transforms may be applied either on |
| 55 | * the GPU or the CPU. When we apply them on the CPU then the matrices need not agree in order |
| 56 | * to combine draws. Therefore we take a param that indicates whether coord transforms should be |
| 57 | * compared." |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 58 | */ |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 59 | static bool AreEqual(const GrPipeline& a, const GrPipeline& b, bool ignoreCoordTransforms); |
| 60 | |
| 61 | /** |
| 62 | * Allows a GrBatch subclass to determine whether two GrBatches can combine. This is a stricter |
| 63 | * test than isEqual because it also considers blend barriers when the two batches' bounds |
| 64 | * overlap |
| 65 | */ |
| 66 | static bool CanCombine(const GrPipeline& a, const SkRect& aBounds, |
| 67 | const GrPipeline& b, const SkRect& bBounds, |
| 68 | const GrCaps& caps, |
| 69 | bool ignoreCoordTransforms = false) { |
| 70 | if (!AreEqual(a, b, ignoreCoordTransforms)) { |
| 71 | return false; |
| 72 | } |
| 73 | if (a.xferBarrierType(caps)) { |
| 74 | return aBounds.fRight <= bBounds.fLeft || |
| 75 | aBounds.fBottom <= bBounds.fTop || |
| 76 | bBounds.fRight <= aBounds.fLeft || |
| 77 | bBounds.fBottom <= aBounds.fTop; |
| 78 | } |
| 79 | return true; |
| 80 | } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 81 | |
| 82 | /// @} |
| 83 | |
| 84 | /////////////////////////////////////////////////////////////////////////// |
bsalomon | 6be6f7c | 2015-02-26 13:05:21 -0800 | [diff] [blame] | 85 | /// @name GrFragmentProcessors |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 86 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 87 | // Make the renderTarget's drawTarget (if it exists) be dependent on any |
| 88 | // drawTargets in this pipeline |
| 89 | void addDependenciesTo(GrRenderTarget* rt) const; |
bsalomon | 6be6f7c | 2015-02-26 13:05:21 -0800 | [diff] [blame] | 90 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 91 | int numColorFragmentProcessors() const { return fNumColorProcessors; } |
| 92 | int numCoverageFragmentProcessors() const { |
| 93 | return fFragmentProcessors.count() - fNumColorProcessors; |
| 94 | } |
| 95 | int numFragmentProcessors() const { return fFragmentProcessors.count(); } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 96 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 97 | const GrXferProcessor* getXferProcessor() const { return fXferProcessor.get(); } |
| 98 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 99 | const GrFragmentProcessor& getColorFragmentProcessor(int idx) const { |
| 100 | SkASSERT(idx < this->numColorFragmentProcessors()); |
| 101 | return *fFragmentProcessors[idx].get(); |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 102 | } |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 103 | |
| 104 | const GrFragmentProcessor& getCoverageFragmentProcessor(int idx) const { |
| 105 | SkASSERT(idx < this->numCoverageFragmentProcessors()); |
| 106 | return *fFragmentProcessors[fNumColorProcessors + idx].get(); |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 107 | } |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 108 | |
| 109 | const GrFragmentProcessor& getFragmentProcessor(int idx) const { |
| 110 | return *fFragmentProcessors[idx].get(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 111 | } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 112 | |
| 113 | /// @} |
| 114 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Retrieves the currently set render-target. |
| 117 | * |
| 118 | * @return The currently set render target. |
| 119 | */ |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 120 | GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 121 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 122 | const GrStencilSettings& getStencil() const { return fStencilSettings; } |
| 123 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 124 | const GrScissorState& getScissorState() const { return fScissorState; } |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 125 | |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 126 | bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAA_Flag); } |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 127 | bool snapVerticesToPixelCenters() const { return SkToBool(fFlags & kSnapVertices_Flag); } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 128 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 129 | GrXferBarrierType xferBarrierType(const GrCaps& caps) const { |
| 130 | return fXferProcessor->xferBarrierType(fRenderTarget.get(), caps); |
| 131 | } |
| 132 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 133 | /** |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 134 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 135 | * or both faces. |
| 136 | * @return the current draw face(s). |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 137 | */ |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 138 | GrPipelineBuilder::DrawFace getDrawFace() const { return fDrawFace; } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 139 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 140 | |
| 141 | /////////////////////////////////////////////////////////////////////////// |
| 142 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 143 | bool readsFragPosition() const { return fReadsFragPosition; } |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 144 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 145 | private: |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 146 | GrPipeline() { /** Initialized in factory function*/ } |
bsalomon | 47dfc36 | 2015-08-10 08:23:11 -0700 | [diff] [blame] | 147 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 148 | /** |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 149 | * Alter the program desc and inputs (attribs and processors) based on the blend optimization. |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 150 | */ |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 151 | void adjustProgramFromOptimizations(const GrPipelineBuilder& ds, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 152 | GrXferProcessor::OptFlags, |
| 153 | const GrProcOptInfo& colorPOI, |
| 154 | const GrProcOptInfo& coveragePOI, |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 155 | int* firstColorProcessorIdx, |
| 156 | int* firstCoverageProcessorIdx); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 157 | |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Calculates the primary and secondary output types of the shader. For certain output types |
| 160 | * the function may adjust the blend coefficients. After this function is called the src and dst |
| 161 | * blend coeffs will represent those used by backend API. |
| 162 | */ |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 163 | void setOutputStateInfo(const GrPipelineBuilder& ds, GrXferProcessor::OptFlags, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 164 | const GrCaps&); |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 165 | |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 166 | enum Flags { |
bsalomon | aca31fe | 2015-09-22 11:38:46 -0700 | [diff] [blame] | 167 | kHWAA_Flag = 0x1, |
| 168 | kSnapVertices_Flag = 0x2, |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 171 | typedef GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> RenderTarget; |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 172 | typedef GrPendingProgramElement<const GrFragmentProcessor> PendingFragmentProcessor; |
| 173 | typedef SkAutoSTArray<8, PendingFragmentProcessor> FragmentProcessorArray; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 174 | typedef GrPendingProgramElement<const GrXferProcessor> ProgramXferProcessor; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 175 | RenderTarget fRenderTarget; |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 176 | GrScissorState fScissorState; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 177 | GrStencilSettings fStencilSettings; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 178 | GrPipelineBuilder::DrawFace fDrawFace; |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 179 | uint32_t fFlags; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 180 | ProgramXferProcessor fXferProcessor; |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 181 | FragmentProcessorArray fFragmentProcessors; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 182 | bool fReadsFragPosition; |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 183 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 184 | // This value is also the index in fFragmentProcessors where coverage processors begin. |
| 185 | int fNumColorProcessors; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 186 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 187 | typedef SkRefCnt INHERITED; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | #endif |