blob: 6366e8050f866991f05e24a5fa2ca277543f89ad [file] [log] [blame]
egdaniel3658f382014-09-15 07:01:59 -07001/*
egdaniel8dd688b2015-01-22 10:16:09 -08002 * Copyright 2015 Google Inc.
egdaniel3658f382014-09-15 07:01:59 -07003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
egdaniel8dd688b2015-01-22 10:16:09 -08008#ifndef GrPipeline_DEFINED
9#define GrPipeline_DEFINED
egdaniel3658f382014-09-15 07:01:59 -070010
egdanielb109ac22014-10-07 06:45:44 -070011#include "GrColor.h"
bsalomonac856c92015-08-27 06:30:17 -070012#include "GrFragmentProcessor.h"
egdanielb109ac22014-10-07 06:45:44 -070013#include "GrGpu.h"
joshualittdbe1e6f2015-07-16 08:12:45 -070014#include "GrNonAtomicRef.h"
bsalomonac856c92015-08-27 06:30:17 -070015#include "GrPendingProgramElement.h"
kkinnunencabe20c2015-06-01 01:37:26 -070016#include "GrPrimitiveProcessor.h"
robertphillips28a838e2016-06-23 14:07:00 -070017#include "GrProcOptInfo.h"
joshualitt79f8fae2014-10-28 17:59:26 -070018#include "GrProgramDesc.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070019#include "GrScissorState.h"
cdalton93a379b2016-05-11 13:58:08 -070020#include "GrStencilSettings.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070021#include "GrWindowRectsState.h"
egdanielb109ac22014-10-07 06:45:44 -070022#include "SkMatrix.h"
23#include "SkRefCnt.h"
24
robertphillips5fa7f302016-07-21 09:21:04 -070025#include "effects/GrCoverageSetOpXP.h"
26#include "effects/GrDisableColorXP.h"
27#include "effects/GrPorterDuffXferProcessor.h"
28#include "effects/GrSimpleTextureEffect.h"
29
joshualitt4d8da812015-01-28 12:53:54 -080030class GrBatch;
robertphillips55fdccc2016-06-06 06:16:20 -070031class GrDrawContext;
joshualitt79f8fae2014-10-28 17:59:26 -070032class GrDeviceCoordTexture;
egdaniel8dd688b2015-01-22 10:16:09 -080033class GrPipelineBuilder;
egdaniel3658f382014-09-15 07:01:59 -070034
ethannicholasff210322015-11-24 12:10:10 -080035struct GrBatchToXPOverrides {
ethannicholas3b7af782016-02-01 11:45:45 -080036 GrBatchToXPOverrides()
37 : fUsePLSDstRead(false) {}
38
ethannicholasff210322015-11-24 12:10:10 -080039 bool fUsePLSDstRead;
40};
41
42struct GrPipelineOptimizations {
43 GrProcOptInfo fColorPOI;
44 GrProcOptInfo fCoveragePOI;
45 GrBatchToXPOverrides fOverrides;
46};
47
egdaniel3658f382014-09-15 07:01:59 -070048/**
egdaniel8dd688b2015-01-22 10:16:09 -080049 * Class that holds an optimized version of a GrPipelineBuilder. It is meant to be an immutable
50 * class, and contains all data needed to set the state for a gpu draw.
egdaniel3658f382014-09-15 07:01:59 -070051 */
cdalton4833f392016-02-02 22:46:16 -080052class GrPipeline : public GrNonAtomicRef<GrPipeline> {
egdaniel3658f382014-09-15 07:01:59 -070053public:
bsalomoncb02b382015-08-12 11:14:50 -070054 ///////////////////////////////////////////////////////////////////////////
55 /// @name Creation
56
bsalomona387a112015-08-11 14:47:42 -070057 struct CreateArgs {
58 const GrPipelineBuilder* fPipelineBuilder;
robertphillips55fdccc2016-06-06 06:16:20 -070059 GrDrawContext* fDrawContext;
bsalomona387a112015-08-11 14:47:42 -070060 const GrCaps* fCaps;
ethannicholasff210322015-11-24 12:10:10 -080061 GrPipelineOptimizations fOpts;
cdaltond4727922015-11-10 12:49:06 -080062 const GrScissorState* fScissor;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070063 const GrWindowRectsState* fWindowRectsState;
cdalton93a379b2016-05-11 13:58:08 -070064 bool fHasStencilClip;
bsalomona387a112015-08-11 14:47:42 -070065 GrXferProcessor::DstTexture fDstTexture;
66 };
67
bsalomon47dfc362015-08-10 08:23:11 -070068 /** Creates a pipeline into a pre-allocated buffer */
ethannicholasff210322015-11-24 12:10:10 -080069 static GrPipeline* CreateAt(void* memory, const CreateArgs&, GrXPOverridesForBatch*);
egdanielb109ac22014-10-07 06:45:44 -070070
bsalomoncb02b382015-08-12 11:14:50 -070071 /// @}
72
73 ///////////////////////////////////////////////////////////////////////////
74 /// @name Comparisons
75
76 /**
joshualitt2fe79232015-08-05 12:02:27 -070077 * Returns true if these pipelines are equivalent. Coord transforms may be applied either on
78 * the GPU or the CPU. When we apply them on the CPU then the matrices need not agree in order
79 * to combine draws. Therefore we take a param that indicates whether coord transforms should be
80 * compared."
joshualitt9b989322014-12-15 14:16:27 -080081 */
bsalomon7312ff82016-09-12 08:55:38 -070082 static bool AreEqual(const GrPipeline& a, const GrPipeline& b);
bsalomoncb02b382015-08-12 11:14:50 -070083
84 /**
85 * Allows a GrBatch subclass to determine whether two GrBatches can combine. This is a stricter
86 * test than isEqual because it also considers blend barriers when the two batches' bounds
87 * overlap
88 */
89 static bool CanCombine(const GrPipeline& a, const SkRect& aBounds,
90 const GrPipeline& b, const SkRect& bBounds,
bsalomon7312ff82016-09-12 08:55:38 -070091 const GrCaps& caps) {
92 if (!AreEqual(a, b)) {
bsalomoncb02b382015-08-12 11:14:50 -070093 return false;
94 }
95 if (a.xferBarrierType(caps)) {
96 return aBounds.fRight <= bBounds.fLeft ||
97 aBounds.fBottom <= bBounds.fTop ||
98 bBounds.fRight <= aBounds.fLeft ||
99 bBounds.fBottom <= aBounds.fTop;
100 }
101 return true;
102 }
egdaniel89af44a2014-09-26 06:15:04 -0700103
104 /// @}
105
106 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -0800107 /// @name GrFragmentProcessors
egdaniel89af44a2014-09-26 06:15:04 -0700108
robertphillips498d7ac2015-10-30 10:11:30 -0700109 // Make the renderTarget's drawTarget (if it exists) be dependent on any
110 // drawTargets in this pipeline
111 void addDependenciesTo(GrRenderTarget* rt) const;
bsalomon6be6f7c2015-02-26 13:05:21 -0800112
bsalomonac856c92015-08-27 06:30:17 -0700113 int numColorFragmentProcessors() const { return fNumColorProcessors; }
114 int numCoverageFragmentProcessors() const {
115 return fFragmentProcessors.count() - fNumColorProcessors;
116 }
117 int numFragmentProcessors() const { return fFragmentProcessors.count(); }
egdaniel89af44a2014-09-26 06:15:04 -0700118
bsalomon2047b782015-12-21 13:12:54 -0800119 const GrXferProcessor& getXferProcessor() const {
120 if (fXferProcessor.get()) {
121 return *fXferProcessor.get();
122 } else {
123 // A null xp member means the common src-over case. GrXferProcessor's ref'ing
124 // mechanism is not thread safe so we do not hold a ref on this global.
125 return GrPorterDuffXPFactory::SimpleSrcOverXP();
126 }
127 }
egdaniel378092f2014-12-03 10:40:13 -0800128
bsalomonac856c92015-08-27 06:30:17 -0700129 const GrFragmentProcessor& getColorFragmentProcessor(int idx) const {
130 SkASSERT(idx < this->numColorFragmentProcessors());
131 return *fFragmentProcessors[idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700132 }
bsalomonac856c92015-08-27 06:30:17 -0700133
134 const GrFragmentProcessor& getCoverageFragmentProcessor(int idx) const {
135 SkASSERT(idx < this->numCoverageFragmentProcessors());
136 return *fFragmentProcessors[fNumColorProcessors + idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700137 }
bsalomonac856c92015-08-27 06:30:17 -0700138
139 const GrFragmentProcessor& getFragmentProcessor(int idx) const {
140 return *fFragmentProcessors[idx].get();
bsalomonae59b772014-11-19 08:23:49 -0800141 }
egdaniel89af44a2014-09-26 06:15:04 -0700142
143 /// @}
144
egdaniel89af44a2014-09-26 06:15:04 -0700145 /**
146 * Retrieves the currently set render-target.
147 *
148 * @return The currently set render target.
149 */
bsalomon37dd3312014-11-03 08:47:23 -0800150 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
egdaniel89af44a2014-09-26 06:15:04 -0700151
egdaniel89af44a2014-09-26 06:15:04 -0700152 const GrStencilSettings& getStencil() const { return fStencilSettings; }
153
bsalomon3e791242014-12-17 13:43:13 -0800154 const GrScissorState& getScissorState() const { return fScissorState; }
joshualitt54e0c122014-11-19 09:38:51 -0800155
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700156 const GrWindowRectsState& getWindowRectsState() const { return fWindowRectsState; }
csmartdalton28341fa2016-08-17 10:00:21 -0700157
bsalomon04ddf892014-11-19 12:36:22 -0800158 bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAA_Flag); }
bsalomond79c5492015-04-27 10:07:04 -0700159 bool snapVerticesToPixelCenters() const { return SkToBool(fFlags & kSnapVertices_Flag); }
brianosman64d094d2016-03-25 06:01:59 -0700160 bool getDisableOutputConversionToSRGB() const {
161 return SkToBool(fFlags & kDisableOutputConversionToSRGB_Flag);
162 }
brianosman898235c2016-04-06 07:38:23 -0700163 bool getAllowSRGBInputs() const {
164 return SkToBool(fFlags & kAllowSRGBInputs_Flag);
165 }
dvonbeck9b03e7b2016-08-01 11:01:56 -0700166 bool usesDistanceVectorField() const {
167 return SkToBool(fFlags & kUsesDistanceVectorField_Flag);
168 }
cdalton193d9cf2016-05-12 11:52:02 -0700169 bool hasStencilClip() const {
170 return SkToBool(fFlags & kHasStencilClip_Flag);
171 }
bsalomonae59b772014-11-19 08:23:49 -0800172
bsalomoncb02b382015-08-12 11:14:50 -0700173 GrXferBarrierType xferBarrierType(const GrCaps& caps) const {
bsalomon2047b782015-12-21 13:12:54 -0800174 return this->getXferProcessor().xferBarrierType(fRenderTarget.get(), caps);
bsalomoncb02b382015-08-12 11:14:50 -0700175 }
176
egdaniel89af44a2014-09-26 06:15:04 -0700177 /**
bsalomonae59b772014-11-19 08:23:49 -0800178 * Gets whether the target is drawing clockwise, counterclockwise,
179 * or both faces.
180 * @return the current draw face(s).
egdaniel89af44a2014-09-26 06:15:04 -0700181 */
robertphillips5fa7f302016-07-21 09:21:04 -0700182 GrDrawFace getDrawFace() const { return fDrawFace; }
bsalomonae59b772014-11-19 08:23:49 -0800183
bsalomonae59b772014-11-19 08:23:49 -0800184
185 ///////////////////////////////////////////////////////////////////////////
186
egdaniel56cf6dc2015-11-30 10:15:58 -0800187 bool ignoresCoverage() const { return fIgnoresCoverage; }
joshualittdafa4d02014-12-04 08:59:10 -0800188
bsalomonae59b772014-11-19 08:23:49 -0800189private:
bsalomonc6998732015-08-10 12:01:15 -0700190 GrPipeline() { /** Initialized in factory function*/ }
bsalomon47dfc362015-08-10 08:23:11 -0700191
egdaniel89af44a2014-09-26 06:15:04 -0700192 /**
bsalomon04ddf892014-11-19 12:36:22 -0800193 * Alter the program desc and inputs (attribs and processors) based on the blend optimization.
egdaniel170f90b2014-09-16 12:54:40 -0700194 */
egdaniel8dd688b2015-01-22 10:16:09 -0800195 void adjustProgramFromOptimizations(const GrPipelineBuilder& ds,
egdaniel95131432014-12-09 11:15:43 -0800196 GrXferProcessor::OptFlags,
197 const GrProcOptInfo& colorPOI,
198 const GrProcOptInfo& coveragePOI,
bsalomonac856c92015-08-27 06:30:17 -0700199 int* firstColorProcessorIdx,
200 int* firstCoverageProcessorIdx);
egdaniela7dc0a82014-09-17 08:25:05 -0700201
egdanielc0648242014-09-22 13:17:02 -0700202 /**
203 * Calculates the primary and secondary output types of the shader. For certain output types
204 * the function may adjust the blend coefficients. After this function is called the src and dst
205 * blend coeffs will represent those used by backend API.
206 */
egdaniel8dd688b2015-01-22 10:16:09 -0800207 void setOutputStateInfo(const GrPipelineBuilder& ds, GrXferProcessor::OptFlags,
bsalomon4b91f762015-05-19 09:29:46 -0700208 const GrCaps&);
egdanielc0648242014-09-22 13:17:02 -0700209
bsalomon04ddf892014-11-19 12:36:22 -0800210 enum Flags {
brianosman64d094d2016-03-25 06:01:59 -0700211 kHWAA_Flag = 0x1,
212 kSnapVertices_Flag = 0x2,
213 kDisableOutputConversionToSRGB_Flag = 0x4,
brianosman898235c2016-04-06 07:38:23 -0700214 kAllowSRGBInputs_Flag = 0x8,
dvonbeck9b03e7b2016-08-01 11:01:56 -0700215 kUsesDistanceVectorField_Flag = 0x10,
216 kHasStencilClip_Flag = 0x20,
bsalomon04ddf892014-11-19 12:36:22 -0800217 };
218
bsalomonae59b772014-11-19 08:23:49 -0800219 typedef GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> RenderTarget;
bsalomonac856c92015-08-27 06:30:17 -0700220 typedef GrPendingProgramElement<const GrFragmentProcessor> PendingFragmentProcessor;
221 typedef SkAutoSTArray<8, PendingFragmentProcessor> FragmentProcessorArray;
egdaniel378092f2014-12-03 10:40:13 -0800222 typedef GrPendingProgramElement<const GrXferProcessor> ProgramXferProcessor;
bsalomonae59b772014-11-19 08:23:49 -0800223 RenderTarget fRenderTarget;
bsalomon3e791242014-12-17 13:43:13 -0800224 GrScissorState fScissorState;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700225 GrWindowRectsState fWindowRectsState;
egdaniel89af44a2014-09-26 06:15:04 -0700226 GrStencilSettings fStencilSettings;
robertphillips5fa7f302016-07-21 09:21:04 -0700227 GrDrawFace fDrawFace;
bsalomon04ddf892014-11-19 12:36:22 -0800228 uint32_t fFlags;
egdaniel378092f2014-12-03 10:40:13 -0800229 ProgramXferProcessor fXferProcessor;
bsalomonac856c92015-08-27 06:30:17 -0700230 FragmentProcessorArray fFragmentProcessors;
egdaniel56cf6dc2015-11-30 10:15:58 -0800231 bool fIgnoresCoverage;
egdanield9aa2182014-10-09 13:47:05 -0700232
bsalomonac856c92015-08-27 06:30:17 -0700233 // This value is also the index in fFragmentProcessors where coverage processors begin.
234 int fNumColorProcessors;
egdaniel89af44a2014-09-26 06:15:04 -0700235
egdaniel89af44a2014-09-26 06:15:04 -0700236 typedef SkRefCnt INHERITED;
egdaniel3658f382014-09-15 07:01:59 -0700237};
238
239#endif