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; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 22 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 23 | /** |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 24 | * Class that holds an optimized version of a GrDrawState. It is meant to be an immutable class, |
| 25 | * and contains all data needed to set the state for a gpu draw. |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 26 | */ |
bsalomon | 932f866 | 2014-11-24 06:47:48 -0800 | [diff] [blame] | 27 | class GrOptDrawState { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 28 | public: |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 29 | SK_DECLARE_INST_COUNT(GrOptDrawState) |
| 30 | |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 31 | typedef GrClipMaskManager::ScissorState ScissorState; |
| 32 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 33 | GrOptDrawState(const GrDrawState& drawState, const GrDrawTargetCaps&, const ScissorState&, |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 34 | const GrDeviceCoordTexture* dstCopy, GrGpu::DrawType); |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 35 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 36 | bool operator== (const GrOptDrawState& that) const; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 37 | bool operator!= (const GrOptDrawState& that) const { return !(*this == that); } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 38 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 39 | /// @} |
| 40 | |
| 41 | /////////////////////////////////////////////////////////////////////////// |
| 42 | /// @name Color |
| 43 | //// |
| 44 | |
| 45 | GrColor getColor() const { return fColor; } |
| 46 | |
| 47 | /// @} |
| 48 | |
| 49 | /////////////////////////////////////////////////////////////////////////// |
| 50 | /// @name Coverage |
| 51 | //// |
| 52 | |
| 53 | uint8_t getCoverage() const { return fCoverage; } |
| 54 | |
| 55 | GrColor getCoverageColor() const { |
| 56 | return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage); |
| 57 | } |
| 58 | |
| 59 | /// @} |
| 60 | |
| 61 | /////////////////////////////////////////////////////////////////////////// |
| 62 | /// @name Effect Stages |
| 63 | /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the |
| 64 | /// fragment shader. Its inputs are the output from the previous stage as well as some variables |
| 65 | /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color, |
| 66 | /// the fragment position, local coordinates). |
| 67 | /// |
| 68 | /// The stages are divided into two sets, color-computing and coverage-computing. The final |
| 69 | /// color stage produces the final pixel color. The coverage-computing stages function exactly |
| 70 | /// as the color-computing but the output of the final coverage stage is treated as a fractional |
| 71 | /// pixel coverage rather than as input to the src/dst color blend step. |
| 72 | /// |
| 73 | /// The input color to the first color-stage is either the constant color or interpolated |
| 74 | /// per-vertex colors. The input to the first coverage stage is either a constant coverage |
| 75 | /// (usually full-coverage) or interpolated per-vertex coverage. |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 76 | //// |
| 77 | |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 78 | int numColorStages() const { return fNumColorStages; } |
| 79 | int numCoverageStages() const { return fFragmentStages.count() - fNumColorStages; } |
| 80 | int numFragmentStages() const { return fFragmentStages.count(); } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 81 | int numTotalStages() const { |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 82 | return this->numFragmentStages() + (this->hasGeometryProcessor() ? 1 : 0); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); } |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 86 | const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryProcessor.get(); } |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 87 | const GrBatchTracker& getBatchTracker() const { return fBatchTracker; } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 88 | |
| 89 | const GrXferProcessor* getXferProcessor() const { return fXferProcessor.get(); } |
| 90 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 91 | const GrPendingFragmentStage& getColorStage(int idx) const { |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 92 | SkASSERT(idx < this->numColorStages()); |
| 93 | return fFragmentStages[idx]; |
| 94 | } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 95 | const GrPendingFragmentStage& getCoverageStage(int idx) const { |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 96 | SkASSERT(idx < this->numCoverageStages()); |
| 97 | return fFragmentStages[fNumColorStages + idx]; |
| 98 | } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 99 | const GrPendingFragmentStage& getFragmentStage(int idx) const { |
| 100 | return fFragmentStages[idx]; |
| 101 | } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 102 | |
| 103 | /// @} |
| 104 | |
| 105 | /////////////////////////////////////////////////////////////////////////// |
| 106 | /// @name Blending |
| 107 | //// |
| 108 | |
| 109 | GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; } |
| 110 | GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; } |
| 111 | |
| 112 | /** |
| 113 | * Retrieves the last value set by setBlendConstant() |
| 114 | * @return the blending constant value |
| 115 | */ |
| 116 | GrColor getBlendConstant() const { return fBlendConstant; } |
| 117 | |
| 118 | /// @} |
| 119 | |
| 120 | /////////////////////////////////////////////////////////////////////////// |
| 121 | /// @name View Matrix |
| 122 | //// |
| 123 | |
| 124 | /** |
| 125 | * Retrieves the current view matrix |
| 126 | * @return the current view matrix. |
| 127 | */ |
| 128 | const SkMatrix& getViewMatrix() const { return fViewMatrix; } |
| 129 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 130 | /// @} |
| 131 | |
| 132 | /////////////////////////////////////////////////////////////////////////// |
| 133 | /// @name Render Target |
| 134 | //// |
| 135 | |
| 136 | /** |
| 137 | * Retrieves the currently set render-target. |
| 138 | * |
| 139 | * @return The currently set render target. |
| 140 | */ |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 141 | GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 142 | |
| 143 | /// @} |
| 144 | |
| 145 | /////////////////////////////////////////////////////////////////////////// |
| 146 | /// @name Stencil |
| 147 | //// |
| 148 | |
| 149 | const GrStencilSettings& getStencil() const { return fStencilSettings; } |
| 150 | |
| 151 | /// @} |
| 152 | |
| 153 | /////////////////////////////////////////////////////////////////////////// |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 154 | /// @name ScissorState |
| 155 | //// |
| 156 | |
| 157 | const ScissorState& getScissorState() const { return fScissorState; } |
| 158 | |
| 159 | /// @} |
| 160 | |
| 161 | |
| 162 | /////////////////////////////////////////////////////////////////////////// |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 163 | /// @name Boolean Queries |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 164 | //// |
| 165 | |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 166 | bool isDitherState() const { return SkToBool(fFlags & kDither_Flag); } |
| 167 | bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAA_Flag); } |
| 168 | bool isColorWriteDisabled() const { return SkToBool(fFlags & kDisableColorWrite_Flag); } |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 169 | bool mustSkip() const { return NULL == this->getRenderTarget(); } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 170 | |
| 171 | /// @} |
| 172 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 173 | /** |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 174 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 175 | * or both faces. |
| 176 | * @return the current draw face(s). |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 177 | */ |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 178 | GrDrawState::DrawFace getDrawFace() const { return fDrawFace; } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 179 | |
| 180 | /// @} |
| 181 | |
| 182 | /////////////////////////////////////////////////////////////////////////// |
| 183 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 184 | GrGpu::DrawType drawType() const { return fDrawType; } |
| 185 | |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 186 | const GrDeviceCoordTexture* getDstCopy() const { return fDstCopy.texture() ? &fDstCopy : NULL; } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 187 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 188 | // Finalize *MUST* be called before programDesc() |
| 189 | void finalize(GrGpu*); |
| 190 | |
| 191 | const GrProgramDesc& programDesc() const { SkASSERT(fFinalized); return fDesc; } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 192 | |
| 193 | private: |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 194 | /** |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 195 | * 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] | 196 | */ |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame^] | 197 | void adjustProgramFromOptimizations(const GrDrawState& ds, |
| 198 | GrXferProcessor::OptFlags, |
| 199 | const GrProcOptInfo& colorPOI, |
| 200 | const GrProcOptInfo& coveragePOI, |
| 201 | int* firstColorStageIdx, |
| 202 | int* firstCoverageStageIdx); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 203 | |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Calculates the primary and secondary output types of the shader. For certain output types |
| 206 | * the function may adjust the blend coefficients. After this function is called the src and dst |
| 207 | * blend coeffs will represent those used by backend API. |
| 208 | */ |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame^] | 209 | void setOutputStateInfo(const GrDrawState& ds, GrXferProcessor::OptFlags, |
| 210 | const GrDrawTargetCaps&); |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 211 | |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 212 | enum Flags { |
| 213 | kDither_Flag = 0x1, |
| 214 | kHWAA_Flag = 0x2, |
| 215 | kDisableColorWrite_Flag = 0x4, |
| 216 | }; |
| 217 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 218 | typedef GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> RenderTarget; |
| 219 | typedef SkSTArray<8, GrPendingFragmentStage> FragmentStageArray; |
| 220 | typedef GrPendingProgramElement<const GrGeometryProcessor> ProgramGeometryProcessor; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 221 | typedef GrPendingProgramElement<const GrXferProcessor> ProgramXferProcessor; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 222 | RenderTarget fRenderTarget; |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 223 | ScissorState fScissorState; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 224 | GrColor fColor; |
| 225 | SkMatrix fViewMatrix; |
| 226 | GrColor fBlendConstant; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 227 | GrStencilSettings fStencilSettings; |
| 228 | uint8_t fCoverage; |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 229 | GrDrawState::DrawFace fDrawFace; |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 230 | GrDeviceCoordTexture fDstCopy; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 231 | GrBlendCoeff fSrcBlend; |
| 232 | GrBlendCoeff fDstBlend; |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 233 | uint32_t fFlags; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 234 | ProgramGeometryProcessor fGeometryProcessor; |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 235 | GrBatchTracker fBatchTracker; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 236 | ProgramXferProcessor fXferProcessor; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 237 | FragmentStageArray fFragmentStages; |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 238 | GrGpu::DrawType fDrawType; |
| 239 | GrProgramDesc::DescInfo fDescInfo; |
| 240 | bool fFinalized; |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 241 | |
| 242 | // This function is equivalent to the offset into fFragmentStages where coverage stages begin. |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 243 | int fNumColorStages; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 244 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 245 | GrProgramDesc fDesc; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 246 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 247 | typedef SkRefCnt INHERITED; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | #endif |