blob: 8781058247fe438300ac2f5cb919c389aea31c1b [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
egdaniel3658f382014-09-15 07:01:59 -070027/**
egdaniel8dd688b2015-01-22 10:16:09 -080028 * Class that holds an optimized version of a GrPipelineBuilder. It is meant to be an immutable
29 * class, and contains all data needed to set the state for a gpu draw.
egdaniel3658f382014-09-15 07:01:59 -070030 */
joshualittdbe1e6f2015-07-16 08:12:45 -070031class GrPipeline : public GrNonAtomicRef {
egdaniel3658f382014-09-15 07:01:59 -070032public:
bsalomoncb02b382015-08-12 11:14:50 -070033 ///////////////////////////////////////////////////////////////////////////
34 /// @name Creation
35
bsalomona387a112015-08-11 14:47:42 -070036 struct CreateArgs {
37 const GrPipelineBuilder* fPipelineBuilder;
38 const GrCaps* fCaps;
39 GrProcOptInfo fColorPOI;
40 GrProcOptInfo fCoveragePOI;
cdaltond4727922015-11-10 12:49:06 -080041 const GrScissorState* fScissor;
bsalomona387a112015-08-11 14:47:42 -070042 GrXferProcessor::DstTexture fDstTexture;
43 };
44
bsalomon47dfc362015-08-10 08:23:11 -070045 /** Creates a pipeline into a pre-allocated buffer */
bsalomona387a112015-08-11 14:47:42 -070046 static GrPipeline* CreateAt(void* memory, const CreateArgs&, GrPipelineOptimizations*);
egdanielb109ac22014-10-07 06:45:44 -070047
bsalomoncb02b382015-08-12 11:14:50 -070048 /// @}
49
50 ///////////////////////////////////////////////////////////////////////////
51 /// @name Comparisons
52
53 /**
joshualitt2fe79232015-08-05 12:02:27 -070054 * Returns true if these pipelines are equivalent. Coord transforms may be applied either on
55 * the GPU or the CPU. When we apply them on the CPU then the matrices need not agree in order
56 * to combine draws. Therefore we take a param that indicates whether coord transforms should be
57 * compared."
joshualitt9b989322014-12-15 14:16:27 -080058 */
bsalomoncb02b382015-08-12 11:14:50 -070059 static bool AreEqual(const GrPipeline& a, const GrPipeline& b, bool ignoreCoordTransforms);
60
61 /**
62 * Allows a GrBatch subclass to determine whether two GrBatches can combine. This is a stricter
63 * test than isEqual because it also considers blend barriers when the two batches' bounds
64 * overlap
65 */
66 static bool CanCombine(const GrPipeline& a, const SkRect& aBounds,
67 const GrPipeline& b, const SkRect& bBounds,
68 const GrCaps& caps,
69 bool ignoreCoordTransforms = false) {
70 if (!AreEqual(a, b, ignoreCoordTransforms)) {
71 return false;
72 }
73 if (a.xferBarrierType(caps)) {
74 return aBounds.fRight <= bBounds.fLeft ||
75 aBounds.fBottom <= bBounds.fTop ||
76 bBounds.fRight <= aBounds.fLeft ||
77 bBounds.fBottom <= aBounds.fTop;
78 }
79 return true;
80 }
egdaniel89af44a2014-09-26 06:15:04 -070081
82 /// @}
83
84 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -080085 /// @name GrFragmentProcessors
egdaniel89af44a2014-09-26 06:15:04 -070086
robertphillips498d7ac2015-10-30 10:11:30 -070087 // Make the renderTarget's drawTarget (if it exists) be dependent on any
88 // drawTargets in this pipeline
89 void addDependenciesTo(GrRenderTarget* rt) const;
bsalomon6be6f7c2015-02-26 13:05:21 -080090
bsalomonac856c92015-08-27 06:30:17 -070091 int numColorFragmentProcessors() const { return fNumColorProcessors; }
92 int numCoverageFragmentProcessors() const {
93 return fFragmentProcessors.count() - fNumColorProcessors;
94 }
95 int numFragmentProcessors() const { return fFragmentProcessors.count(); }
egdaniel89af44a2014-09-26 06:15:04 -070096
egdaniel378092f2014-12-03 10:40:13 -080097 const GrXferProcessor* getXferProcessor() const { return fXferProcessor.get(); }
98
bsalomonac856c92015-08-27 06:30:17 -070099 const GrFragmentProcessor& getColorFragmentProcessor(int idx) const {
100 SkASSERT(idx < this->numColorFragmentProcessors());
101 return *fFragmentProcessors[idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700102 }
bsalomonac856c92015-08-27 06:30:17 -0700103
104 const GrFragmentProcessor& getCoverageFragmentProcessor(int idx) const {
105 SkASSERT(idx < this->numCoverageFragmentProcessors());
106 return *fFragmentProcessors[fNumColorProcessors + idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700107 }
bsalomonac856c92015-08-27 06:30:17 -0700108
109 const GrFragmentProcessor& getFragmentProcessor(int idx) const {
110 return *fFragmentProcessors[idx].get();
bsalomonae59b772014-11-19 08:23:49 -0800111 }
egdaniel89af44a2014-09-26 06:15:04 -0700112
113 /// @}
114
egdaniel89af44a2014-09-26 06:15:04 -0700115 /**
116 * Retrieves the currently set render-target.
117 *
118 * @return The currently set render target.
119 */
bsalomon37dd3312014-11-03 08:47:23 -0800120 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
egdaniel89af44a2014-09-26 06:15:04 -0700121
egdaniel89af44a2014-09-26 06:15:04 -0700122 const GrStencilSettings& getStencil() const { return fStencilSettings; }
123
bsalomon3e791242014-12-17 13:43:13 -0800124 const GrScissorState& getScissorState() const { return fScissorState; }
joshualitt54e0c122014-11-19 09:38:51 -0800125
bsalomon04ddf892014-11-19 12:36:22 -0800126 bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAA_Flag); }
bsalomond79c5492015-04-27 10:07:04 -0700127 bool snapVerticesToPixelCenters() const { return SkToBool(fFlags & kSnapVertices_Flag); }
bsalomonae59b772014-11-19 08:23:49 -0800128
bsalomoncb02b382015-08-12 11:14:50 -0700129 GrXferBarrierType xferBarrierType(const GrCaps& caps) const {
130 return fXferProcessor->xferBarrierType(fRenderTarget.get(), caps);
131 }
132
egdaniel89af44a2014-09-26 06:15:04 -0700133 /**
bsalomonae59b772014-11-19 08:23:49 -0800134 * Gets whether the target is drawing clockwise, counterclockwise,
135 * or both faces.
136 * @return the current draw face(s).
egdaniel89af44a2014-09-26 06:15:04 -0700137 */
egdaniel8dd688b2015-01-22 10:16:09 -0800138 GrPipelineBuilder::DrawFace getDrawFace() const { return fDrawFace; }
bsalomonae59b772014-11-19 08:23:49 -0800139
bsalomonae59b772014-11-19 08:23:49 -0800140
141 ///////////////////////////////////////////////////////////////////////////
142
bsalomon50785a32015-02-06 07:02:37 -0800143 bool readsFragPosition() const { return fReadsFragPosition; }
joshualittdafa4d02014-12-04 08:59:10 -0800144
bsalomonae59b772014-11-19 08:23:49 -0800145private:
bsalomonc6998732015-08-10 12:01:15 -0700146 GrPipeline() { /** Initialized in factory function*/ }
bsalomon47dfc362015-08-10 08:23:11 -0700147
egdaniel89af44a2014-09-26 06:15:04 -0700148 /**
bsalomon04ddf892014-11-19 12:36:22 -0800149 * Alter the program desc and inputs (attribs and processors) based on the blend optimization.
egdaniel170f90b2014-09-16 12:54:40 -0700150 */
egdaniel8dd688b2015-01-22 10:16:09 -0800151 void adjustProgramFromOptimizations(const GrPipelineBuilder& ds,
egdaniel95131432014-12-09 11:15:43 -0800152 GrXferProcessor::OptFlags,
153 const GrProcOptInfo& colorPOI,
154 const GrProcOptInfo& coveragePOI,
bsalomonac856c92015-08-27 06:30:17 -0700155 int* firstColorProcessorIdx,
156 int* firstCoverageProcessorIdx);
egdaniela7dc0a82014-09-17 08:25:05 -0700157
egdanielc0648242014-09-22 13:17:02 -0700158 /**
159 * Calculates the primary and secondary output types of the shader. For certain output types
160 * the function may adjust the blend coefficients. After this function is called the src and dst
161 * blend coeffs will represent those used by backend API.
162 */
egdaniel8dd688b2015-01-22 10:16:09 -0800163 void setOutputStateInfo(const GrPipelineBuilder& ds, GrXferProcessor::OptFlags,
bsalomon4b91f762015-05-19 09:29:46 -0700164 const GrCaps&);
egdanielc0648242014-09-22 13:17:02 -0700165
bsalomon04ddf892014-11-19 12:36:22 -0800166 enum Flags {
bsalomonaca31fe2015-09-22 11:38:46 -0700167 kHWAA_Flag = 0x1,
168 kSnapVertices_Flag = 0x2,
bsalomon04ddf892014-11-19 12:36:22 -0800169 };
170
bsalomonae59b772014-11-19 08:23:49 -0800171 typedef GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> RenderTarget;
bsalomonac856c92015-08-27 06:30:17 -0700172 typedef GrPendingProgramElement<const GrFragmentProcessor> PendingFragmentProcessor;
173 typedef SkAutoSTArray<8, PendingFragmentProcessor> FragmentProcessorArray;
egdaniel378092f2014-12-03 10:40:13 -0800174 typedef GrPendingProgramElement<const GrXferProcessor> ProgramXferProcessor;
bsalomonae59b772014-11-19 08:23:49 -0800175 RenderTarget fRenderTarget;
bsalomon3e791242014-12-17 13:43:13 -0800176 GrScissorState fScissorState;
egdaniel89af44a2014-09-26 06:15:04 -0700177 GrStencilSettings fStencilSettings;
joshualitt4d8da812015-01-28 12:53:54 -0800178 GrPipelineBuilder::DrawFace fDrawFace;
bsalomon04ddf892014-11-19 12:36:22 -0800179 uint32_t fFlags;
egdaniel378092f2014-12-03 10:40:13 -0800180 ProgramXferProcessor fXferProcessor;
bsalomonac856c92015-08-27 06:30:17 -0700181 FragmentProcessorArray fFragmentProcessors;
bsalomon50785a32015-02-06 07:02:37 -0800182 bool fReadsFragPosition;
egdanield9aa2182014-10-09 13:47:05 -0700183
bsalomonac856c92015-08-27 06:30:17 -0700184 // This value is also the index in fFragmentProcessors where coverage processors begin.
185 int fNumColorProcessors;
egdaniel89af44a2014-09-26 06:15:04 -0700186
egdaniel89af44a2014-09-26 06:15:04 -0700187 typedef SkRefCnt INHERITED;
egdaniel3658f382014-09-15 07:01:59 -0700188};
189
190#endif