2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrOptDrawState_DEFINED
9 #define GrOptDrawState_DEFINED
13 #include "GrProcessorStage.h"
14 #include "GrProgramDesc.h"
15 #include "GrStencil.h"
16 #include "GrTypesPriv.h"
20 class GrDeviceCoordTexture;
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.
27 class GrOptDrawState : public SkRefCnt {
30 * Returns a snapshot of the current optimized state. If the current drawState has a valid
31 * cached optimiezed state it will simply return a pointer to it otherwise it will create a new
32 * GrOptDrawState. In all cases the GrOptDrawState is reffed and ownership is given to the
35 static GrOptDrawState* Create(const GrDrawState& drawState, GrGpu*,
36 const GrDeviceCoordTexture* dstCopy, GrGpu::DrawType drawType);
38 bool operator== (const GrOptDrawState& that) const;
40 ///////////////////////////////////////////////////////////////////////////
41 /// @name Vertex Attributes
45 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4,
48 const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; }
49 int getVertexAttribCount() const { return fVACount; }
51 size_t getVertexStride() const { return fVAStride; }
55 ///////////////////////////////////////////////////////////////////////////
59 GrColor getColor() const { return fColor; }
63 ///////////////////////////////////////////////////////////////////////////
67 uint8_t getCoverage() const { return fCoverage; }
69 GrColor getCoverageColor() const {
70 return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
75 ///////////////////////////////////////////////////////////////////////////
76 /// @name Effect Stages
77 /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the
78 /// fragment shader. Its inputs are the output from the previous stage as well as some variables
79 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
80 /// the fragment position, local coordinates).
82 /// The stages are divided into two sets, color-computing and coverage-computing. The final
83 /// color stage produces the final pixel color. The coverage-computing stages function exactly
84 /// as the color-computing but the output of the final coverage stage is treated as a fractional
85 /// pixel coverage rather than as input to the src/dst color blend step.
87 /// The input color to the first color-stage is either the constant color or interpolated
88 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
89 /// (usually full-coverage) or interpolated per-vertex coverage.
91 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
92 /// the color / coverage distinction.
95 int numColorStages() const { return fNumColorStages; }
96 int numCoverageStages() const { return fFragmentStages.count() - fNumColorStages; }
97 int numFragmentStages() const { return fFragmentStages.count(); }
98 int numTotalStages() const {
99 return this->numFragmentStages() + (this->hasGeometryProcessor() ? 1 : 0);
102 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); }
103 const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryProcessor.get(); }
104 const GrFragmentStage& getColorStage(int idx) const {
105 SkASSERT(idx < this->numColorStages());
106 return fFragmentStages[idx];
108 const GrFragmentStage& getCoverageStage(int idx) const {
109 SkASSERT(idx < this->numCoverageStages());
110 return fFragmentStages[fNumColorStages + idx];
112 const GrFragmentStage& getFragmentStage(int idx) const { return fFragmentStages[idx]; }
116 ///////////////////////////////////////////////////////////////////////////
120 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
121 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
124 * Retrieves the last value set by setBlendConstant()
125 * @return the blending constant value
127 GrColor getBlendConstant() const { return fBlendConstant; }
131 ///////////////////////////////////////////////////////////////////////////
132 /// @name View Matrix
136 * Retrieves the current view matrix
137 * @return the current view matrix.
139 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
142 * Retrieves the inverse of the current view matrix.
144 * If the current view matrix is invertible, return true, and if matrix
145 * is non-null, copy the inverse into it. If the current view matrix is
146 * non-invertible, return false and ignore the matrix parameter.
148 * @param matrix if not null, will receive a copy of the current inverse.
150 bool getViewInverse(SkMatrix* matrix) const {
152 if (fViewMatrix.invert(&inverse)) {
163 ///////////////////////////////////////////////////////////////////////////
164 /// @name Render Target
168 * Retrieves the currently set render-target.
170 * @return The currently set render target.
172 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
176 ///////////////////////////////////////////////////////////////////////////
180 const GrStencilSettings& getStencil() const { return fStencilSettings; }
184 ///////////////////////////////////////////////////////////////////////////
185 /// @name State Flags
189 * Flags that affect rendering. Controlled using enable/disableState(). All
190 * default to disabled.
194 * Perform dithering. TODO: Re-evaluate whether we need this bit
196 kDither_StateBit = 0x01,
198 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
199 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
202 kHWAntialias_StateBit = 0x02,
204 * Draws will respect the clip, otherwise the clip is ignored.
206 kClip_StateBit = 0x04,
208 * Disables writing to the color buffer. Useful when performing stencil
211 kNoColorWrites_StateBit = 0x08,
214 * Usually coverage is applied after color blending. The color is blended using the coeffs
215 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
216 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
217 * this case there is no distinction between coverage and color and the caller needs direct
218 * control over the blend coeffs. When set, there will be a single blend step controlled by
219 * setBlendFunc() which will use coverage*color as the src color.
221 kCoverageDrawing_StateBit = 0x10,
223 // Users of the class may add additional bits to the vector
225 kLastPublicStateBit = kDummyStateBit-1,
228 bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & fFlagBits); }
230 bool isDitherState() const { return 0 != (fFlagBits & kDither_StateBit); }
231 bool isHWAntialiasState() const { return 0 != (fFlagBits & kHWAntialias_StateBit); }
232 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); }
233 bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_StateBit); }
234 bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_StateBit); }
238 ///////////////////////////////////////////////////////////////////////////
239 /// @name Face Culling
243 kInvalid_DrawFace = -1,
251 * Gets whether the target is drawing clockwise, counterclockwise,
253 * @return the current draw face(s).
255 DrawFace getDrawFace() const { return fDrawFace; }
259 ///////////////////////////////////////////////////////////////////////////
261 /** Return type for CombineIfPossible. */
263 /** The GrDrawStates cannot be combined. */
264 kIncompatible_CombinedState,
265 /** Either draw state can be used in place of the other. */
267 /** Use the first draw state. */
269 /** Use the second draw state. */
275 const GrProgramDesc& programDesc() const { return fDesc; }
279 * Optimizations for blending / coverage to that can be applied based on the current state.
289 kSkipDraw_BlendOptFlag = 0x1,
291 * The coverage value does not have to be computed separately from alpha, the the output
292 * color can be the modulation of the two.
294 kCoverageAsAlpha_BlendOptFlag = 0x2,
296 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
299 kEmitCoverage_BlendOptFlag = 0x4,
301 * Emit transparent black instead of the src color, no need to compute coverage.
303 kEmitTransBlack_BlendOptFlag = 0x8,
305 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
308 * Constructs and optimized drawState out of a GrRODrawState.
310 GrOptDrawState(const GrDrawState& drawState, BlendOptFlags blendOptFlags,
311 GrBlendCoeff optSrcCoeff, GrBlendCoeff optDstCoeff,
312 GrGpu*, const GrDeviceCoordTexture* dstCopy, GrGpu::DrawType);
315 * Loops through all the color stage effects to check if the stage will ignore color input or
316 * always output a constant color. In the ignore color input case we can ignore all previous
317 * stages. In the constant color case, we can ignore all previous stages and
318 * the current one and set the state color to the constant color.
320 void computeEffectiveColorStages(const GrDrawState& ds, GrProgramDesc::DescInfo*,
321 int* firstColorStageIdx, uint8_t* fixFunctionVAToRemove);
324 * Loops through all the coverage stage effects to check if the stage will ignore color input.
325 * If a coverage stage will ignore input, then we can ignore all coverage stages before it. We
326 * loop to determine the first effective coverage stage.
328 void computeEffectiveCoverageStages(const GrDrawState& ds, GrProgramDesc::DescInfo* descInfo,
329 int* firstCoverageStageIdx);
332 * This function takes in a flag and removes the corresponding fixed function vertex attributes.
333 * The flags are in the same order as GrVertexAttribBinding array. If bit i of removeVAFlags is
334 * set, then vertex attributes with binding (GrVertexAttribute)i will be removed.
336 void removeFixedFunctionVertexAttribs(uint8_t removeVAFlags, GrProgramDesc::DescInfo*);
339 * Alter the OptDrawState (adjusting stages, vertex attribs, flags, etc.) based on the
342 void adjustFromBlendOpts(const GrDrawState& ds, GrProgramDesc::DescInfo*,
343 int* firstColorStageIdx, int* firstCoverageStageIdx,
344 uint8_t* fixedFunctionVAToRemove);
347 * Loop over the effect stages to determine various info like what data they will read and what
348 * shaders they require.
350 void getStageStats(const GrDrawState& ds, int firstColorStageIdx, int firstCoverageStageIdx,
351 GrProgramDesc::DescInfo*);
354 * Calculates the primary and secondary output types of the shader. For certain output types
355 * the function may adjust the blend coefficients. After this function is called the src and dst
356 * blend coeffs will represent those used by backend API.
358 void setOutputStateInfo(const GrDrawState& ds, const GrDrawTargetCaps&,
359 int firstCoverageStageIdx, GrProgramDesc::DescInfo*,
360 bool* separateCoverageFromColor);
362 bool isEqual(const GrOptDrawState& that) const;
364 // These fields are roughly sorted by decreasing likelihood of being different in op==
365 typedef GrTGpuResourceRef<GrRenderTarget> ProgramRenderTarget;
366 ProgramRenderTarget fRenderTarget;
368 SkMatrix fViewMatrix;
369 GrColor fBlendConstant;
371 const GrVertexAttrib* fVAPtr;
374 GrStencilSettings fStencilSettings;
377 GrBlendCoeff fSrcBlend;
378 GrBlendCoeff fDstBlend;
380 typedef SkSTArray<8, GrFragmentStage> FragmentStageArray;
381 typedef GrProgramElementRef<const GrGeometryProcessor> ProgramGeometryProcessor;
382 ProgramGeometryProcessor fGeometryProcessor;
383 FragmentStageArray fFragmentStages;
385 // This function is equivalent to the offset into fFragmentStages where coverage stages begin.
388 SkAutoSTArray<4, GrVertexAttrib> fOptVA;
390 BlendOptFlags fBlendOptFlags;
394 typedef SkRefCnt INHERITED;
397 GR_MAKE_BITFIELD_OPS(GrOptDrawState::BlendOptFlags);