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