blob: 10ffc865f08f61a39c602f1cebb61fc9cb9fb3c1 [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"
joshualitt79f8fae2014-10-28 17:59:26 -070017#include "GrProgramDesc.h"
egdanielb109ac22014-10-07 06:45:44 -070018#include "GrStencil.h"
19#include "GrTypesPriv.h"
20#include "SkMatrix.h"
21#include "SkRefCnt.h"
22
joshualitt4d8da812015-01-28 12:53:54 -080023class GrBatch;
joshualitt79f8fae2014-10-28 17:59:26 -070024class GrDeviceCoordTexture;
egdaniel8dd688b2015-01-22 10:16:09 -080025class GrPipelineBuilder;
egdaniel3658f382014-09-15 07:01:59 -070026
ethannicholasff210322015-11-24 12:10:10 -080027struct GrBatchToXPOverrides {
ethannicholas3b7af782016-02-01 11:45:45 -080028 GrBatchToXPOverrides()
29 : fUsePLSDstRead(false) {}
30
ethannicholasff210322015-11-24 12:10:10 -080031 bool fUsePLSDstRead;
32};
33
34struct GrPipelineOptimizations {
35 GrProcOptInfo fColorPOI;
36 GrProcOptInfo fCoveragePOI;
37 GrBatchToXPOverrides fOverrides;
38};
39
egdaniel3658f382014-09-15 07:01:59 -070040/**
egdaniel8dd688b2015-01-22 10:16:09 -080041 * 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.
egdaniel3658f382014-09-15 07:01:59 -070043 */
joshualittdbe1e6f2015-07-16 08:12:45 -070044class GrPipeline : public GrNonAtomicRef {
egdaniel3658f382014-09-15 07:01:59 -070045public:
bsalomoncb02b382015-08-12 11:14:50 -070046 ///////////////////////////////////////////////////////////////////////////
47 /// @name Creation
48
bsalomona387a112015-08-11 14:47:42 -070049 struct CreateArgs {
50 const GrPipelineBuilder* fPipelineBuilder;
51 const GrCaps* fCaps;
ethannicholasff210322015-11-24 12:10:10 -080052 GrPipelineOptimizations fOpts;
cdaltond4727922015-11-10 12:49:06 -080053 const GrScissorState* fScissor;
bsalomona387a112015-08-11 14:47:42 -070054 GrXferProcessor::DstTexture fDstTexture;
55 };
56
bsalomon47dfc362015-08-10 08:23:11 -070057 /** Creates a pipeline into a pre-allocated buffer */
ethannicholasff210322015-11-24 12:10:10 -080058 static GrPipeline* CreateAt(void* memory, const CreateArgs&, GrXPOverridesForBatch*);
egdanielb109ac22014-10-07 06:45:44 -070059
bsalomoncb02b382015-08-12 11:14:50 -070060 /// @}
61
62 ///////////////////////////////////////////////////////////////////////////
63 /// @name Comparisons
64
65 /**
joshualitt2fe79232015-08-05 12:02:27 -070066 * 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."
joshualitt9b989322014-12-15 14:16:27 -080070 */
bsalomoncb02b382015-08-12 11:14:50 -070071 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 }
egdaniel89af44a2014-09-26 06:15:04 -070093
94 /// @}
95
96 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -080097 /// @name GrFragmentProcessors
egdaniel89af44a2014-09-26 06:15:04 -070098
robertphillips498d7ac2015-10-30 10:11:30 -070099 // Make the renderTarget's drawTarget (if it exists) be dependent on any
100 // drawTargets in this pipeline
101 void addDependenciesTo(GrRenderTarget* rt) const;
bsalomon6be6f7c2015-02-26 13:05:21 -0800102
bsalomonac856c92015-08-27 06:30:17 -0700103 int numColorFragmentProcessors() const { return fNumColorProcessors; }
104 int numCoverageFragmentProcessors() const {
105 return fFragmentProcessors.count() - fNumColorProcessors;
106 }
107 int numFragmentProcessors() const { return fFragmentProcessors.count(); }
egdaniel89af44a2014-09-26 06:15:04 -0700108
bsalomon2047b782015-12-21 13:12:54 -0800109 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 }
egdaniel378092f2014-12-03 10:40:13 -0800118
bsalomonac856c92015-08-27 06:30:17 -0700119 const GrFragmentProcessor& getColorFragmentProcessor(int idx) const {
120 SkASSERT(idx < this->numColorFragmentProcessors());
121 return *fFragmentProcessors[idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700122 }
bsalomonac856c92015-08-27 06:30:17 -0700123
124 const GrFragmentProcessor& getCoverageFragmentProcessor(int idx) const {
125 SkASSERT(idx < this->numCoverageFragmentProcessors());
126 return *fFragmentProcessors[fNumColorProcessors + idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700127 }
bsalomonac856c92015-08-27 06:30:17 -0700128
129 const GrFragmentProcessor& getFragmentProcessor(int idx) const {
130 return *fFragmentProcessors[idx].get();
bsalomonae59b772014-11-19 08:23:49 -0800131 }
egdaniel89af44a2014-09-26 06:15:04 -0700132
133 /// @}
134
egdaniel89af44a2014-09-26 06:15:04 -0700135 /**
136 * Retrieves the currently set render-target.
137 *
138 * @return The currently set render target.
139 */
bsalomon37dd3312014-11-03 08:47:23 -0800140 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
egdaniel89af44a2014-09-26 06:15:04 -0700141
egdaniel89af44a2014-09-26 06:15:04 -0700142 const GrStencilSettings& getStencil() const { return fStencilSettings; }
143
bsalomon3e791242014-12-17 13:43:13 -0800144 const GrScissorState& getScissorState() const { return fScissorState; }
joshualitt54e0c122014-11-19 09:38:51 -0800145
bsalomon04ddf892014-11-19 12:36:22 -0800146 bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAA_Flag); }
bsalomond79c5492015-04-27 10:07:04 -0700147 bool snapVerticesToPixelCenters() const { return SkToBool(fFlags & kSnapVertices_Flag); }
bsalomonae59b772014-11-19 08:23:49 -0800148
bsalomoncb02b382015-08-12 11:14:50 -0700149 GrXferBarrierType xferBarrierType(const GrCaps& caps) const {
bsalomon2047b782015-12-21 13:12:54 -0800150 return this->getXferProcessor().xferBarrierType(fRenderTarget.get(), caps);
bsalomoncb02b382015-08-12 11:14:50 -0700151 }
152
egdaniel89af44a2014-09-26 06:15:04 -0700153 /**
bsalomonae59b772014-11-19 08:23:49 -0800154 * Gets whether the target is drawing clockwise, counterclockwise,
155 * or both faces.
156 * @return the current draw face(s).
egdaniel89af44a2014-09-26 06:15:04 -0700157 */
egdaniel8dd688b2015-01-22 10:16:09 -0800158 GrPipelineBuilder::DrawFace getDrawFace() const { return fDrawFace; }
bsalomonae59b772014-11-19 08:23:49 -0800159
bsalomonae59b772014-11-19 08:23:49 -0800160
161 ///////////////////////////////////////////////////////////////////////////
162
bsalomon50785a32015-02-06 07:02:37 -0800163 bool readsFragPosition() const { return fReadsFragPosition; }
egdaniel56cf6dc2015-11-30 10:15:58 -0800164 bool ignoresCoverage() const { return fIgnoresCoverage; }
joshualittdafa4d02014-12-04 08:59:10 -0800165
bsalomonae59b772014-11-19 08:23:49 -0800166private:
bsalomonc6998732015-08-10 12:01:15 -0700167 GrPipeline() { /** Initialized in factory function*/ }
bsalomon47dfc362015-08-10 08:23:11 -0700168
egdaniel89af44a2014-09-26 06:15:04 -0700169 /**
bsalomon04ddf892014-11-19 12:36:22 -0800170 * Alter the program desc and inputs (attribs and processors) based on the blend optimization.
egdaniel170f90b2014-09-16 12:54:40 -0700171 */
egdaniel8dd688b2015-01-22 10:16:09 -0800172 void adjustProgramFromOptimizations(const GrPipelineBuilder& ds,
egdaniel95131432014-12-09 11:15:43 -0800173 GrXferProcessor::OptFlags,
174 const GrProcOptInfo& colorPOI,
175 const GrProcOptInfo& coveragePOI,
bsalomonac856c92015-08-27 06:30:17 -0700176 int* firstColorProcessorIdx,
177 int* firstCoverageProcessorIdx);
egdaniela7dc0a82014-09-17 08:25:05 -0700178
egdanielc0648242014-09-22 13:17:02 -0700179 /**
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 */
egdaniel8dd688b2015-01-22 10:16:09 -0800184 void setOutputStateInfo(const GrPipelineBuilder& ds, GrXferProcessor::OptFlags,
bsalomon4b91f762015-05-19 09:29:46 -0700185 const GrCaps&);
egdanielc0648242014-09-22 13:17:02 -0700186
bsalomon04ddf892014-11-19 12:36:22 -0800187 enum Flags {
bsalomonaca31fe2015-09-22 11:38:46 -0700188 kHWAA_Flag = 0x1,
189 kSnapVertices_Flag = 0x2,
bsalomon04ddf892014-11-19 12:36:22 -0800190 };
191
bsalomonae59b772014-11-19 08:23:49 -0800192 typedef GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> RenderTarget;
bsalomonac856c92015-08-27 06:30:17 -0700193 typedef GrPendingProgramElement<const GrFragmentProcessor> PendingFragmentProcessor;
194 typedef SkAutoSTArray<8, PendingFragmentProcessor> FragmentProcessorArray;
egdaniel378092f2014-12-03 10:40:13 -0800195 typedef GrPendingProgramElement<const GrXferProcessor> ProgramXferProcessor;
bsalomonae59b772014-11-19 08:23:49 -0800196 RenderTarget fRenderTarget;
bsalomon3e791242014-12-17 13:43:13 -0800197 GrScissorState fScissorState;
egdaniel89af44a2014-09-26 06:15:04 -0700198 GrStencilSettings fStencilSettings;
joshualitt4d8da812015-01-28 12:53:54 -0800199 GrPipelineBuilder::DrawFace fDrawFace;
bsalomon04ddf892014-11-19 12:36:22 -0800200 uint32_t fFlags;
egdaniel378092f2014-12-03 10:40:13 -0800201 ProgramXferProcessor fXferProcessor;
bsalomonac856c92015-08-27 06:30:17 -0700202 FragmentProcessorArray fFragmentProcessors;
bsalomon50785a32015-02-06 07:02:37 -0800203 bool fReadsFragPosition;
egdaniel56cf6dc2015-11-30 10:15:58 -0800204 bool fIgnoresCoverage;
egdanield9aa2182014-10-09 13:47:05 -0700205
bsalomonac856c92015-08-27 06:30:17 -0700206 // This value is also the index in fFragmentProcessors where coverage processors begin.
207 int fNumColorProcessors;
egdaniel89af44a2014-09-26 06:15:04 -0700208
egdaniel89af44a2014-09-26 06:15:04 -0700209 typedef SkRefCnt INHERITED;
egdaniel3658f382014-09-15 07:01:59 -0700210};
211
212#endif