egdaniel | 3658f38 | 2014-09-15 07:01:59 -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 GrOptDrawState_DEFINED |
| 9 | #define GrOptDrawState_DEFINED |
| 10 | |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 11 | #include "GrColor.h" |
| 12 | #include "GrGpu.h" |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 13 | #include "GrPendingFragmentStage.h" |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 14 | #include "GrProgramDesc.h" |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 15 | #include "GrStencil.h" |
| 16 | #include "GrTypesPriv.h" |
| 17 | #include "SkMatrix.h" |
| 18 | #include "SkRefCnt.h" |
| 19 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 20 | class GrDeviceCoordTexture; |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 21 | class GrDrawState; |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 22 | class GrPathProcessor; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 23 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 24 | /** |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 25 | * Class that holds an optimized version of a GrDrawState. It is meant to be an immutable class, |
| 26 | * and contains all data needed to set the state for a gpu draw. |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 27 | */ |
bsalomon | 932f866 | 2014-11-24 06:47:48 -0800 | [diff] [blame] | 28 | class GrOptDrawState { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 29 | public: |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 30 | SK_DECLARE_INST_COUNT(GrOptDrawState) |
| 31 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 32 | GrOptDrawState(const GrDrawState& drawState, const GrGeometryProcessor*, const GrPathProcessor*, |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 33 | const GrDrawTargetCaps&, const GrScissorState&, |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 34 | const GrDeviceCoordTexture* dstCopy, GrGpu::DrawType); |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 35 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 36 | /* |
| 37 | * Returns true if it is possible to combine the two GrOptDrawStates and it will update 'this' |
| 38 | * to subsume 'that''s draw. |
| 39 | */ |
| 40 | bool combineIfPossible(const GrOptDrawState& that); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 41 | |
| 42 | /// @} |
| 43 | |
| 44 | /////////////////////////////////////////////////////////////////////////// |
| 45 | /// @name Effect Stages |
| 46 | /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the |
| 47 | /// fragment shader. Its inputs are the output from the previous stage as well as some variables |
| 48 | /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color, |
| 49 | /// the fragment position, local coordinates). |
| 50 | /// |
| 51 | /// The stages are divided into two sets, color-computing and coverage-computing. The final |
| 52 | /// color stage produces the final pixel color. The coverage-computing stages function exactly |
| 53 | /// as the color-computing but the output of the final coverage stage is treated as a fractional |
| 54 | /// pixel coverage rather than as input to the src/dst color blend step. |
| 55 | /// |
| 56 | /// The input color to the first color-stage is either the constant color or interpolated |
| 57 | /// per-vertex colors. The input to the first coverage stage is either a constant coverage |
| 58 | /// (usually full-coverage) or interpolated per-vertex coverage. |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 59 | //// |
| 60 | |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 61 | int numColorStages() const { return fNumColorStages; } |
| 62 | int numCoverageStages() const { return fFragmentStages.count() - fNumColorStages; } |
| 63 | int numFragmentStages() const { return fFragmentStages.count(); } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 64 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 65 | // TODO remove the GP specific calls when the PathProc can provide the same interface |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 66 | bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); } |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 67 | const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryProcessor.get(); } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 68 | const GrPrimitiveProcessor* getPrimitiveProcessor() const { return fPrimitiveProcessor.get(); } |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 69 | const GrBatchTracker& getBatchTracker() const { return fBatchTracker; } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 70 | |
| 71 | const GrXferProcessor* getXferProcessor() const { return fXferProcessor.get(); } |
| 72 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 73 | const GrPendingFragmentStage& getColorStage(int idx) const { |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 74 | SkASSERT(idx < this->numColorStages()); |
| 75 | return fFragmentStages[idx]; |
| 76 | } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 77 | const GrPendingFragmentStage& getCoverageStage(int idx) const { |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 78 | SkASSERT(idx < this->numCoverageStages()); |
| 79 | return fFragmentStages[fNumColorStages + idx]; |
| 80 | } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 81 | const GrPendingFragmentStage& getFragmentStage(int idx) const { |
| 82 | return fFragmentStages[idx]; |
| 83 | } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 84 | |
| 85 | /// @} |
| 86 | |
| 87 | /////////////////////////////////////////////////////////////////////////// |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 88 | /// @name Render Target |
| 89 | //// |
| 90 | |
| 91 | /** |
| 92 | * Retrieves the currently set render-target. |
| 93 | * |
| 94 | * @return The currently set render target. |
| 95 | */ |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 96 | GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 97 | |
| 98 | /// @} |
| 99 | |
| 100 | /////////////////////////////////////////////////////////////////////////// |
| 101 | /// @name Stencil |
| 102 | //// |
| 103 | |
| 104 | const GrStencilSettings& getStencil() const { return fStencilSettings; } |
| 105 | |
| 106 | /// @} |
| 107 | |
| 108 | /////////////////////////////////////////////////////////////////////////// |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 109 | /// @name ScissorState |
| 110 | //// |
| 111 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 112 | const GrScissorState& getScissorState() const { return fScissorState; } |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 113 | |
| 114 | /// @} |
| 115 | |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 116 | /////////////////////////////////////////////////////////////////////////// |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 117 | /// @name Boolean Queries |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 118 | //// |
| 119 | |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 120 | bool isDitherState() const { return SkToBool(fFlags & kDither_Flag); } |
| 121 | bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAA_Flag); } |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 122 | bool mustSkip() const { return NULL == this->getRenderTarget(); } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 123 | |
| 124 | /// @} |
| 125 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 126 | /** |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 127 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 128 | * or both faces. |
| 129 | * @return the current draw face(s). |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 130 | */ |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 131 | GrDrawState::DrawFace getDrawFace() const { return fDrawFace; } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 132 | |
| 133 | /// @} |
| 134 | |
| 135 | /////////////////////////////////////////////////////////////////////////// |
| 136 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 137 | GrGpu::DrawType drawType() const { return fDrawType; } |
| 138 | |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 139 | const GrDeviceCoordTexture* getDstCopy() const { return fDstCopy.texture() ? &fDstCopy : NULL; } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 140 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 141 | // Finalize *MUST* be called before programDesc() |
| 142 | void finalize(GrGpu*); |
| 143 | |
| 144 | const GrProgramDesc& programDesc() const { SkASSERT(fFinalized); return fDesc; } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 145 | |
| 146 | private: |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 147 | /** |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 148 | * 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] | 149 | */ |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 150 | void adjustProgramFromOptimizations(const GrDrawState& ds, |
| 151 | GrXferProcessor::OptFlags, |
| 152 | const GrProcOptInfo& colorPOI, |
| 153 | const GrProcOptInfo& coveragePOI, |
| 154 | int* firstColorStageIdx, |
| 155 | int* firstCoverageStageIdx); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 156 | |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Calculates the primary and secondary output types of the shader. For certain output types |
| 159 | * the function may adjust the blend coefficients. After this function is called the src and dst |
| 160 | * blend coeffs will represent those used by backend API. |
| 161 | */ |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 162 | void setOutputStateInfo(const GrDrawState& ds, GrXferProcessor::OptFlags, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 163 | const GrDrawTargetCaps&); |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 164 | |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 165 | enum Flags { |
| 166 | kDither_Flag = 0x1, |
| 167 | kHWAA_Flag = 0x2, |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 168 | }; |
| 169 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 170 | typedef GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> RenderTarget; |
| 171 | typedef SkSTArray<8, GrPendingFragmentStage> FragmentStageArray; |
| 172 | typedef GrPendingProgramElement<const GrGeometryProcessor> ProgramGeometryProcessor; |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 173 | typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor; |
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; |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 178 | GrDrawState::DrawFace fDrawFace; |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 179 | GrDeviceCoordTexture fDstCopy; |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 180 | uint32_t fFlags; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 181 | ProgramGeometryProcessor fGeometryProcessor; |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 182 | ProgramPrimitiveProcessor fPrimitiveProcessor; |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 183 | GrBatchTracker fBatchTracker; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 184 | ProgramXferProcessor fXferProcessor; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 185 | FragmentStageArray fFragmentStages; |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 186 | GrGpu::DrawType fDrawType; |
| 187 | GrProgramDesc::DescInfo fDescInfo; |
| 188 | bool fFinalized; |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 189 | |
| 190 | // This function is equivalent to the offset into fFragmentStages where coverage stages begin. |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 191 | int fNumColorStages; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 192 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 193 | GrProgramDesc fDesc; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 194 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 195 | typedef SkRefCnt INHERITED; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | #endif |