blob: de68e7fc8e54394ba5ed2f30d91226d4e8da9a9c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkMatrix.h"
12#include "include/core/SkRefCnt.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrFragmentProcessor.h"
15#include "src/gpu/GrNonAtomicRef.h"
16#include "src/gpu/GrPendingIOResource.h"
17#include "src/gpu/GrProcessorSet.h"
18#include "src/gpu/GrProgramDesc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrScissorState.h"
20#include "src/gpu/GrUserStencilSettings.h"
21#include "src/gpu/GrWindowRectsState.h"
22#include "src/gpu/effects/GrCoverageSetOpXP.h"
23#include "src/gpu/effects/GrDisableColorXP.h"
24#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
25#include "src/gpu/effects/generated/GrSimpleTextureEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040026#include "src/gpu/geometry/GrRect.h"
robertphillips5fa7f302016-07-21 09:21:04 -070027
Brian Salomon652ecb52017-01-17 12:39:53 -050028class GrAppliedClip;
Brian Salomon25a88092016-12-01 09:36:50 -050029class GrOp;
Brian Salomon25a88092016-12-01 09:36:50 -050030class GrRenderTargetContext;
egdaniel3658f382014-09-15 07:01:59 -070031
Brian Salomon92aee3d2016-12-21 09:20:25 -050032/**
Brian Salomone5b399e2017-07-19 13:50:54 -040033 * This immutable object contains information needed to set build a shader program and set API
34 * state for a draw. It is used along with a GrPrimitiveProcessor and a source of geometric
35 * data (GrMesh or GrPath) to draw.
egdaniel3658f382014-09-15 07:01:59 -070036 */
Brian Salomon16351462017-07-19 16:35:31 -040037class GrPipeline {
egdaniel3658f382014-09-15 07:01:59 -070038public:
bsalomoncb02b382015-08-12 11:14:50 -070039 ///////////////////////////////////////////////////////////////////////////
40 /// @name Creation
41
Chris Daltonbaa1b352019-04-03 12:03:00 -060042 // Pipeline options that the caller may enable.
43 // NOTE: This enum is extended later by GrPipeline::Flags.
44 enum class InputFlags : uint8_t {
45 kNone = 0,
Brian Salomon189098e72017-01-19 09:55:19 -050046 /**
47 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
48 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
49 * the 3D API.
50 */
Chris Daltonbaa1b352019-04-03 12:03:00 -060051 kHWAntialias = (1 << 0),
Brian Salomon189098e72017-01-19 09:55:19 -050052 /**
53 * Modifies the vertex shader so that vertices will be positioned at pixel centers.
54 */
Chris Daltonbaa1b352019-04-03 12:03:00 -060055 kSnapVerticesToPixelCenters = (1 << 1), // This value must be last. (See kLastInputFlag.)
Brian Salomon189098e72017-01-19 09:55:19 -050056 };
57
Brian Salomonb5cb6832017-02-24 11:01:15 -050058 struct InitArgs {
Chris Daltonbaa1b352019-04-03 12:03:00 -060059 InputFlags fInputFlags = InputFlags::kNone;
Brian Salomon189098e72017-01-19 09:55:19 -050060 const GrUserStencilSettings* fUserStencil = &GrUserStencilSettings::kUnused;
Brian Salomon189098e72017-01-19 09:55:19 -050061 const GrCaps* fCaps = nullptr;
Robert Phillips9bee2e52017-05-29 12:37:20 -040062 GrResourceProvider* fResourceProvider = nullptr;
Robert Phillipsbb581ce2017-05-29 15:05:15 -040063 GrXferProcessor::DstProxy fDstProxy;
Greg Daniel2c3398d2019-06-19 11:58:01 -040064 GrSwizzle fOutputSwizzle;
bsalomona387a112015-08-11 14:47:42 -070065 };
66
Brian Salomonb5cb6832017-02-24 11:01:15 -050067 /**
Brian Salomon49348902018-06-26 09:12:38 -040068 * Some state can be changed between GrMeshes without changing GrPipelines. This is generally
69 * less expensive then using multiple pipelines. Such state is called "dynamic state". It can
70 * be specified in two ways:
71 * 1) FixedDynamicState - use this to specify state that does not vary between GrMeshes.
72 * 2) DynamicStateArrays - use this to specify per mesh values for dynamic state.
Chris Dalton46983b72017-06-06 12:27:16 -060073 **/
Brian Salomon49348902018-06-26 09:12:38 -040074 struct FixedDynamicState {
Brian Salomon7eae3e02018-08-07 14:02:38 +000075 explicit FixedDynamicState(const SkIRect& scissorRect) : fScissorRect(scissorRect) {}
76 FixedDynamicState() = default;
77 SkIRect fScissorRect = SkIRect::EmptyIRect();
Brian Salomonf7232642018-09-19 08:58:08 -040078 // Must have GrPrimitiveProcessor::numTextureSamplers() entries. Can be null if no samplers
79 // or textures are passed using DynamicStateArrays.
Brian Salomon7eae3e02018-08-07 14:02:38 +000080 GrTextureProxy** fPrimitiveProcessorTextures = nullptr;
Chris Dalton46983b72017-06-06 12:27:16 -060081 };
82
83 /**
Brian Salomon49348902018-06-26 09:12:38 -040084 * Any non-null array overrides the FixedDynamicState on a mesh-by-mesh basis. Arrays must
85 * have one entry for each GrMesh.
86 */
87 struct DynamicStateArrays {
88 const SkIRect* fScissorRects = nullptr;
Brian Salomonf7232642018-09-19 08:58:08 -040089 // Must have GrPrimitiveProcessor::numTextureSamplers() * num_meshes entries.
90 // Can be null if no samplers or to use the same textures for all meshes via'
91 // FixedDynamicState.
92 GrTextureProxy** fPrimitiveProcessorTextures = nullptr;
Brian Salomon49348902018-06-26 09:12:38 -040093 };
94
95 /**
csmartdalton119fb2b2017-02-08 14:41:05 -050096 * Creates a simple pipeline with default settings and no processors. The provided blend mode
Chris Dalton916c4982018-08-15 00:53:25 -060097 * must be "Porter Duff" (<= kLastCoeffMode). If using GrScissorTest::kEnabled, the caller must
Chris Dalton46983b72017-06-06 12:27:16 -060098 * specify a scissor rectangle through the DynamicState struct.
csmartdalton119fb2b2017-02-08 14:41:05 -050099 **/
Greg Daniel2c3398d2019-06-19 11:58:01 -0400100 GrPipeline(GrScissorTest, SkBlendMode, const GrSwizzle& outputSwizzle,
101 InputFlags = InputFlags::kNone,
Chris Daltond7291ba2019-03-07 14:17:03 -0700102 const GrUserStencilSettings* = &GrUserStencilSettings::kUnused);
csmartdalton119fb2b2017-02-08 14:41:05 -0500103
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400104 GrPipeline(const InitArgs&, GrProcessorSet&&, GrAppliedClip&&);
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400105
Brian Salomon16351462017-07-19 16:35:31 -0400106 GrPipeline(const GrPipeline&) = delete;
107 GrPipeline& operator=(const GrPipeline&) = delete;
108
egdaniel89af44a2014-09-26 06:15:04 -0700109 /// @}
110
111 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -0800112 /// @name GrFragmentProcessors
egdaniel89af44a2014-09-26 06:15:04 -0700113
Robert Phillipsd261e102017-06-23 12:37:20 -0400114 // Make the renderTargetContext's GrOpList be dependent on any GrOpLists in this pipeline
115 void addDependenciesTo(GrOpList* recipient, const GrCaps&) const;
bsalomon6be6f7c2015-02-26 13:05:21 -0800116
bsalomonac856c92015-08-27 06:30:17 -0700117 int numColorFragmentProcessors() const { return fNumColorProcessors; }
118 int numCoverageFragmentProcessors() const {
119 return fFragmentProcessors.count() - fNumColorProcessors;
120 }
121 int numFragmentProcessors() const { return fFragmentProcessors.count(); }
egdaniel89af44a2014-09-26 06:15:04 -0700122
bsalomon2047b782015-12-21 13:12:54 -0800123 const GrXferProcessor& getXferProcessor() const {
Brian Salomond61c9d92017-04-10 10:54:25 -0400124 if (fXferProcessor) {
bsalomon2047b782015-12-21 13:12:54 -0800125 return *fXferProcessor.get();
126 } else {
127 // A null xp member means the common src-over case. GrXferProcessor's ref'ing
128 // mechanism is not thread safe so we do not hold a ref on this global.
129 return GrPorterDuffXPFactory::SimpleSrcOverXP();
130 }
131 }
egdaniel378092f2014-12-03 10:40:13 -0800132
Brian Salomon18dfa982017-04-03 16:57:43 -0400133 /**
134 * If the GrXferProcessor uses a texture to access the dst color, then this returns that
135 * texture and the offset to the dst contents within that texture.
136 */
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400137 GrTextureProxy* dstTextureProxy(SkIPoint* offset = nullptr) const {
Brian Salomon18dfa982017-04-03 16:57:43 -0400138 if (offset) {
139 *offset = fDstTextureOffset;
140 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400141
142 return fDstTextureProxy ? fDstTextureProxy->asTextureProxy() : nullptr;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400143 }
144
145 GrTexture* peekDstTexture(SkIPoint* offset = nullptr) const {
146 if (GrTextureProxy* dstProxy = this->dstTextureProxy(offset)) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400147 return dstProxy->peekTexture();
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400148 }
149
150 return nullptr;
Brian Salomon18dfa982017-04-03 16:57:43 -0400151 }
152
bsalomonac856c92015-08-27 06:30:17 -0700153 const GrFragmentProcessor& getColorFragmentProcessor(int idx) const {
154 SkASSERT(idx < this->numColorFragmentProcessors());
155 return *fFragmentProcessors[idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700156 }
bsalomonac856c92015-08-27 06:30:17 -0700157
158 const GrFragmentProcessor& getCoverageFragmentProcessor(int idx) const {
159 SkASSERT(idx < this->numCoverageFragmentProcessors());
160 return *fFragmentProcessors[fNumColorProcessors + idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700161 }
bsalomonac856c92015-08-27 06:30:17 -0700162
163 const GrFragmentProcessor& getFragmentProcessor(int idx) const {
164 return *fFragmentProcessors[idx].get();
bsalomonae59b772014-11-19 08:23:49 -0800165 }
egdaniel89af44a2014-09-26 06:15:04 -0700166
167 /// @}
168
csmartdaltonc633abb2016-11-01 08:55:55 -0700169 const GrUserStencilSettings* getUserStencil() const { return fUserStencilSettings; }
egdaniel89af44a2014-09-26 06:15:04 -0700170
Chris Dalton916c4982018-08-15 00:53:25 -0600171 bool isScissorEnabled() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600172 return SkToBool(fFlags & Flags::kScissorEnabled);
Brian Salomon49348902018-06-26 09:12:38 -0400173 }
joshualitt54e0c122014-11-19 09:38:51 -0800174
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700175 const GrWindowRectsState& getWindowRectsState() const { return fWindowRectsState; }
csmartdalton28341fa2016-08-17 10:00:21 -0700176
Chris Daltonbaa1b352019-04-03 12:03:00 -0600177 bool isHWAntialiasState() const { return SkToBool(fFlags & InputFlags::kHWAntialias); }
Brian Salomon189098e72017-01-19 09:55:19 -0500178 bool snapVerticesToPixelCenters() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600179 return SkToBool(fFlags & InputFlags::kSnapVerticesToPixelCenters);
Brian Salomon189098e72017-01-19 09:55:19 -0500180 }
cdalton193d9cf2016-05-12 11:52:02 -0700181 bool hasStencilClip() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600182 return SkToBool(fFlags & Flags::kHasStencilClip);
cdalton193d9cf2016-05-12 11:52:02 -0700183 }
csmartdaltonc633abb2016-11-01 08:55:55 -0700184 bool isStencilEnabled() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600185 return SkToBool(fFlags & Flags::kStencilEnabled);
csmartdaltonc633abb2016-11-01 08:55:55 -0700186 }
Chris Daltonbaa1b352019-04-03 12:03:00 -0600187 bool isBad() const { return SkToBool(fFlags & Flags::kIsBad); }
bsalomonae59b772014-11-19 08:23:49 -0800188
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500189 GrXferBarrierType xferBarrierType(GrTexture*, const GrCaps&) const;
bsalomoncb02b382015-08-12 11:14:50 -0700190
Jim Van Verth1223e7f2019-02-28 17:38:35 -0500191 // Used by Vulkan and Metal to cache their respective pipeline objects
192 uint32_t getBlendInfoKey() const;
193
Greg Daniel2c3398d2019-06-19 11:58:01 -0400194 const GrSwizzle& outputSwizzle() const { return fOutputSwizzle; }
195
bsalomonae59b772014-11-19 08:23:49 -0800196private:
Chris Daltonbaa1b352019-04-03 12:03:00 -0600197 void markAsBad() { fFlags |= Flags::kIsBad; }
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400198
Chris Daltonbaa1b352019-04-03 12:03:00 -0600199 static constexpr uint8_t kLastInputFlag = (uint8_t)InputFlags::kSnapVerticesToPixelCenters;
200
201 /** This is a continuation of the public "InputFlags" enum. */
202 enum class Flags : uint8_t {
203 kHasStencilClip = (kLastInputFlag << 1),
204 kStencilEnabled = (kLastInputFlag << 2),
205 kScissorEnabled = (kLastInputFlag << 3),
206 kIsBad = (kLastInputFlag << 4),
bsalomon04ddf892014-11-19 12:36:22 -0800207 };
208
Chris Daltonbaa1b352019-04-03 12:03:00 -0600209 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(Flags);
210
211 friend bool operator&(Flags, InputFlags);
212
Brian Salomonaff329b2017-08-11 09:40:37 -0400213 using FragmentProcessorArray = SkAutoSTArray<8, std::unique_ptr<const GrFragmentProcessor>>;
Brian Salomon18dfa982017-04-03 16:57:43 -0400214
Robert Phillips3d4cac52019-06-11 08:08:08 -0400215 GrProxyPendingIO fDstTextureProxy;
Brian Salomon18dfa982017-04-03 16:57:43 -0400216 SkIPoint fDstTextureOffset;
Brian Salomon18dfa982017-04-03 16:57:43 -0400217 GrWindowRectsState fWindowRectsState;
218 const GrUserStencilSettings* fUserStencilSettings;
Chris Daltonbaa1b352019-04-03 12:03:00 -0600219 Flags fFlags;
Brian Salomond61c9d92017-04-10 10:54:25 -0400220 sk_sp<const GrXferProcessor> fXferProcessor;
Brian Salomon18dfa982017-04-03 16:57:43 -0400221 FragmentProcessorArray fFragmentProcessors;
egdanield9aa2182014-10-09 13:47:05 -0700222
bsalomonac856c92015-08-27 06:30:17 -0700223 // This value is also the index in fFragmentProcessors where coverage processors begin.
Brian Salomon18dfa982017-04-03 16:57:43 -0400224 int fNumColorProcessors;
Greg Daniel2c3398d2019-06-19 11:58:01 -0400225
226 GrSwizzle fOutputSwizzle;
egdaniel3658f382014-09-15 07:01:59 -0700227};
228
Chris Daltonbaa1b352019-04-03 12:03:00 -0600229GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::InputFlags);
230GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::Flags);
231
232inline bool operator&(GrPipeline::Flags flags, GrPipeline::InputFlags inputFlag) {
233 return (flags & (GrPipeline::Flags)inputFlag);
234}
235
egdaniel3658f382014-09-15 07:01:59 -0700236#endif