blob: 76adc9eadc56e3eb076b29b14567f2300cfc63cd [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrProcessorSet.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrScissorState.h"
Greg Daniel524e28b2019-11-01 11:48:53 -040018#include "src/gpu/GrSurfaceProxyView.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrUserStencilSettings.h"
20#include "src/gpu/GrWindowRectsState.h"
21#include "src/gpu/effects/GrCoverageSetOpXP.h"
22#include "src/gpu/effects/GrDisableColorXP.h"
23#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
24#include "src/gpu/effects/generated/GrSimpleTextureEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040025#include "src/gpu/geometry/GrRect.h"
robertphillips5fa7f302016-07-21 09:21:04 -070026
Brian Salomon652ecb52017-01-17 12:39:53 -050027class GrAppliedClip;
Brian Salomon25a88092016-12-01 09:36:50 -050028class GrOp;
Brian Salomon25a88092016-12-01 09:36:50 -050029class GrRenderTargetContext;
egdaniel3658f382014-09-15 07:01:59 -070030
Brian Salomon92aee3d2016-12-21 09:20:25 -050031/**
Brian Salomone5b399e2017-07-19 13:50:54 -040032 * This immutable object contains information needed to set build a shader program and set API
33 * state for a draw. It is used along with a GrPrimitiveProcessor and a source of geometric
34 * data (GrMesh or GrPath) to draw.
egdaniel3658f382014-09-15 07:01:59 -070035 */
Brian Salomon16351462017-07-19 16:35:31 -040036class GrPipeline {
egdaniel3658f382014-09-15 07:01:59 -070037public:
bsalomoncb02b382015-08-12 11:14:50 -070038 ///////////////////////////////////////////////////////////////////////////
39 /// @name Creation
40
Chris Daltonbaa1b352019-04-03 12:03:00 -060041 // Pipeline options that the caller may enable.
42 // NOTE: This enum is extended later by GrPipeline::Flags.
43 enum class InputFlags : uint8_t {
44 kNone = 0,
Brian Salomon189098e72017-01-19 09:55:19 -050045 /**
46 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
47 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
48 * the 3D API.
49 */
Chris Daltonbaa1b352019-04-03 12:03:00 -060050 kHWAntialias = (1 << 0),
Brian Salomon189098e72017-01-19 09:55:19 -050051 /**
Chris Daltonce425af2019-12-16 10:39:03 -070052 * Cause every pixel to be rasterized that is touched by the triangle anywhere (not just at
53 * pixel center). Additionally, if using MSAA, the sample mask will always have 100%
54 * coverage.
55 * NOTE: The primitive type must be a triangle type.
56 */
57 kConservativeRaster = (1 << 1),
58 /**
Brian Salomon189098e72017-01-19 09:55:19 -050059 * Modifies the vertex shader so that vertices will be positioned at pixel centers.
60 */
Chris Daltonce425af2019-12-16 10:39:03 -070061 kSnapVerticesToPixelCenters = (1 << 2), // This value must be last. (See kLastInputFlag.)
Brian Salomon189098e72017-01-19 09:55:19 -050062 };
63
Brian Salomonb5cb6832017-02-24 11:01:15 -050064 struct InitArgs {
Chris Daltonbaa1b352019-04-03 12:03:00 -060065 InputFlags fInputFlags = InputFlags::kNone;
Brian Salomon189098e72017-01-19 09:55:19 -050066 const GrUserStencilSettings* fUserStencil = &GrUserStencilSettings::kUnused;
Brian Salomon189098e72017-01-19 09:55:19 -050067 const GrCaps* fCaps = nullptr;
Greg Daniel524e28b2019-11-01 11:48:53 -040068 GrXferProcessor::DstProxyView fDstProxyView;
Greg Daniel2c3398d2019-06-19 11:58:01 -040069 GrSwizzle fOutputSwizzle;
bsalomona387a112015-08-11 14:47:42 -070070 };
71
Brian Salomonb5cb6832017-02-24 11:01:15 -050072 /**
Brian Salomon49348902018-06-26 09:12:38 -040073 * Some state can be changed between GrMeshes without changing GrPipelines. This is generally
74 * less expensive then using multiple pipelines. Such state is called "dynamic state". It can
75 * be specified in two ways:
76 * 1) FixedDynamicState - use this to specify state that does not vary between GrMeshes.
77 * 2) DynamicStateArrays - use this to specify per mesh values for dynamic state.
Chris Dalton46983b72017-06-06 12:27:16 -060078 **/
Brian Salomon49348902018-06-26 09:12:38 -040079 struct FixedDynamicState {
Brian Salomon7eae3e02018-08-07 14:02:38 +000080 explicit FixedDynamicState(const SkIRect& scissorRect) : fScissorRect(scissorRect) {}
81 FixedDynamicState() = default;
82 SkIRect fScissorRect = SkIRect::EmptyIRect();
Brian Salomonf7232642018-09-19 08:58:08 -040083 // Must have GrPrimitiveProcessor::numTextureSamplers() entries. Can be null if no samplers
84 // or textures are passed using DynamicStateArrays.
Michael Ludwigfcdd0612019-11-25 08:34:31 -050085 GrSurfaceProxy** fPrimitiveProcessorTextures = nullptr;
Chris Dalton46983b72017-06-06 12:27:16 -060086 };
87
88 /**
Brian Salomon49348902018-06-26 09:12:38 -040089 * Any non-null array overrides the FixedDynamicState on a mesh-by-mesh basis. Arrays must
90 * have one entry for each GrMesh.
91 */
92 struct DynamicStateArrays {
93 const SkIRect* fScissorRects = nullptr;
Brian Salomonf7232642018-09-19 08:58:08 -040094 // Must have GrPrimitiveProcessor::numTextureSamplers() * num_meshes entries.
95 // Can be null if no samplers or to use the same textures for all meshes via'
96 // FixedDynamicState.
Michael Ludwigfcdd0612019-11-25 08:34:31 -050097 GrSurfaceProxy** fPrimitiveProcessorTextures = nullptr;
Brian Salomon49348902018-06-26 09:12:38 -040098 };
99
100 /**
csmartdalton119fb2b2017-02-08 14:41:05 -0500101 * Creates a simple pipeline with default settings and no processors. The provided blend mode
Chris Dalton916c4982018-08-15 00:53:25 -0600102 * must be "Porter Duff" (<= kLastCoeffMode). If using GrScissorTest::kEnabled, the caller must
Chris Dalton46983b72017-06-06 12:27:16 -0600103 * specify a scissor rectangle through the DynamicState struct.
csmartdalton119fb2b2017-02-08 14:41:05 -0500104 **/
Chris Daltonc3318f02019-07-19 14:20:53 -0600105 GrPipeline(GrScissorTest scissor, SkBlendMode blend, const GrSwizzle& outputSwizzle,
106 InputFlags flags = InputFlags::kNone,
107 const GrUserStencilSettings* stencil = &GrUserStencilSettings::kUnused)
108 : GrPipeline(scissor, GrPorterDuffXPFactory::MakeNoCoverageXP(blend), outputSwizzle,
109 flags, stencil) {
110 }
111
112 GrPipeline(GrScissorTest, sk_sp<const GrXferProcessor>, const GrSwizzle& outputSwizzle,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400113 InputFlags = InputFlags::kNone,
Chris Daltond7291ba2019-03-07 14:17:03 -0700114 const GrUserStencilSettings* = &GrUserStencilSettings::kUnused);
csmartdalton119fb2b2017-02-08 14:41:05 -0500115
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400116 GrPipeline(const InitArgs&, GrProcessorSet&&, GrAppliedClip&&);
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400117
Brian Salomon16351462017-07-19 16:35:31 -0400118 GrPipeline(const GrPipeline&) = delete;
119 GrPipeline& operator=(const GrPipeline&) = delete;
120
egdaniel89af44a2014-09-26 06:15:04 -0700121 /// @}
122
123 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -0800124 /// @name GrFragmentProcessors
egdaniel89af44a2014-09-26 06:15:04 -0700125
bsalomonac856c92015-08-27 06:30:17 -0700126 int numColorFragmentProcessors() const { return fNumColorProcessors; }
127 int numCoverageFragmentProcessors() const {
128 return fFragmentProcessors.count() - fNumColorProcessors;
129 }
130 int numFragmentProcessors() const { return fFragmentProcessors.count(); }
egdaniel89af44a2014-09-26 06:15:04 -0700131
bsalomon2047b782015-12-21 13:12:54 -0800132 const GrXferProcessor& getXferProcessor() const {
Brian Salomond61c9d92017-04-10 10:54:25 -0400133 if (fXferProcessor) {
bsalomon2047b782015-12-21 13:12:54 -0800134 return *fXferProcessor.get();
135 } else {
136 // A null xp member means the common src-over case. GrXferProcessor's ref'ing
137 // mechanism is not thread safe so we do not hold a ref on this global.
138 return GrPorterDuffXPFactory::SimpleSrcOverXP();
139 }
140 }
egdaniel378092f2014-12-03 10:40:13 -0800141
Brian Salomon18dfa982017-04-03 16:57:43 -0400142 /**
Greg Daniel524e28b2019-11-01 11:48:53 -0400143 * This returns the GrSurfaceProxyView for the texture used to access the dst color. If the
144 * GrXferProcessor does not use the dst color then the proxy on the GrSurfaceProxyView will be
145 * nullptr.
146 */
147 const GrSurfaceProxyView& dstProxyView() const {
148 return fDstProxyView;
149 }
150
151 /**
Brian Salomon18dfa982017-04-03 16:57:43 -0400152 * If the GrXferProcessor uses a texture to access the dst color, then this returns that
153 * texture and the offset to the dst contents within that texture.
154 */
Greg Daniel524e28b2019-11-01 11:48:53 -0400155 GrTexture* peekDstTexture(SkIPoint* offset = nullptr) const {
Brian Salomon18dfa982017-04-03 16:57:43 -0400156 if (offset) {
157 *offset = fDstTextureOffset;
158 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400159
Greg Daniel524e28b2019-11-01 11:48:53 -0400160 if (GrTextureProxy* dstProxy = fDstProxyView.asTextureProxy()) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400161 return dstProxy->peekTexture();
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400162 }
163
164 return nullptr;
Brian Salomon18dfa982017-04-03 16:57:43 -0400165 }
166
bsalomonac856c92015-08-27 06:30:17 -0700167 const GrFragmentProcessor& getColorFragmentProcessor(int idx) const {
168 SkASSERT(idx < this->numColorFragmentProcessors());
169 return *fFragmentProcessors[idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700170 }
bsalomonac856c92015-08-27 06:30:17 -0700171
172 const GrFragmentProcessor& getCoverageFragmentProcessor(int idx) const {
173 SkASSERT(idx < this->numCoverageFragmentProcessors());
174 return *fFragmentProcessors[fNumColorProcessors + idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700175 }
bsalomonac856c92015-08-27 06:30:17 -0700176
177 const GrFragmentProcessor& getFragmentProcessor(int idx) const {
178 return *fFragmentProcessors[idx].get();
bsalomonae59b772014-11-19 08:23:49 -0800179 }
egdaniel89af44a2014-09-26 06:15:04 -0700180
181 /// @}
182
csmartdaltonc633abb2016-11-01 08:55:55 -0700183 const GrUserStencilSettings* getUserStencil() const { return fUserStencilSettings; }
egdaniel89af44a2014-09-26 06:15:04 -0700184
Chris Dalton916c4982018-08-15 00:53:25 -0600185 bool isScissorEnabled() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600186 return SkToBool(fFlags & Flags::kScissorEnabled);
Brian Salomon49348902018-06-26 09:12:38 -0400187 }
joshualitt54e0c122014-11-19 09:38:51 -0800188
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700189 const GrWindowRectsState& getWindowRectsState() const { return fWindowRectsState; }
csmartdalton28341fa2016-08-17 10:00:21 -0700190
Chris Daltonce425af2019-12-16 10:39:03 -0700191 bool isHWAntialiasState() const { return fFlags & InputFlags::kHWAntialias; }
192 bool usesConservativeRaster() const { return fFlags & InputFlags::kConservativeRaster; }
Brian Salomon189098e72017-01-19 09:55:19 -0500193 bool snapVerticesToPixelCenters() const {
Chris Daltonce425af2019-12-16 10:39:03 -0700194 return fFlags & InputFlags::kSnapVerticesToPixelCenters;
Brian Salomon189098e72017-01-19 09:55:19 -0500195 }
cdalton193d9cf2016-05-12 11:52:02 -0700196 bool hasStencilClip() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600197 return SkToBool(fFlags & Flags::kHasStencilClip);
cdalton193d9cf2016-05-12 11:52:02 -0700198 }
csmartdaltonc633abb2016-11-01 08:55:55 -0700199 bool isStencilEnabled() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600200 return SkToBool(fFlags & Flags::kStencilEnabled);
csmartdaltonc633abb2016-11-01 08:55:55 -0700201 }
Robert Phillips8053c972019-11-21 10:44:53 -0500202#ifdef SK_DEBUG
203 bool allProxiesInstantiated() const {
204 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
205 if (!fFragmentProcessors[i]->isInstantiated()) {
206 return false;
207 }
208 }
209 if (fDstProxyView.proxy()) {
210 return fDstProxyView.proxy()->isInstantiated();
211 }
212
213 return true;
214 }
215#endif
bsalomonae59b772014-11-19 08:23:49 -0800216
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500217 GrXferBarrierType xferBarrierType(GrTexture*, const GrCaps&) const;
bsalomoncb02b382015-08-12 11:14:50 -0700218
Jim Van Verth1223e7f2019-02-28 17:38:35 -0500219 // Used by Vulkan and Metal to cache their respective pipeline objects
Chris Daltonb204e4c2019-11-07 12:43:13 -0700220 void genKey(GrProcessorKeyBuilder*, const GrCaps&) const;
Jim Van Verth1223e7f2019-02-28 17:38:35 -0500221
Greg Daniel2c3398d2019-06-19 11:58:01 -0400222 const GrSwizzle& outputSwizzle() const { return fOutputSwizzle; }
223
Brian Salomonc241b582019-11-27 08:57:17 -0500224 void visitProxies(const GrOp::VisitProxyFunc&) const;
Robert Phillips8053c972019-11-21 10:44:53 -0500225
bsalomonae59b772014-11-19 08:23:49 -0800226private:
Chris Daltonbaa1b352019-04-03 12:03:00 -0600227 static constexpr uint8_t kLastInputFlag = (uint8_t)InputFlags::kSnapVerticesToPixelCenters;
228
229 /** This is a continuation of the public "InputFlags" enum. */
230 enum class Flags : uint8_t {
231 kHasStencilClip = (kLastInputFlag << 1),
232 kStencilEnabled = (kLastInputFlag << 2),
233 kScissorEnabled = (kLastInputFlag << 3),
bsalomon04ddf892014-11-19 12:36:22 -0800234 };
235
Chris Daltonbaa1b352019-04-03 12:03:00 -0600236 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(Flags);
237
238 friend bool operator&(Flags, InputFlags);
239
Brian Salomonaff329b2017-08-11 09:40:37 -0400240 using FragmentProcessorArray = SkAutoSTArray<8, std::unique_ptr<const GrFragmentProcessor>>;
Brian Salomon18dfa982017-04-03 16:57:43 -0400241
Greg Daniel524e28b2019-11-01 11:48:53 -0400242 GrSurfaceProxyView fDstProxyView;
Brian Salomon18dfa982017-04-03 16:57:43 -0400243 SkIPoint fDstTextureOffset;
Brian Salomon18dfa982017-04-03 16:57:43 -0400244 GrWindowRectsState fWindowRectsState;
245 const GrUserStencilSettings* fUserStencilSettings;
Chris Daltonbaa1b352019-04-03 12:03:00 -0600246 Flags fFlags;
Brian Salomond61c9d92017-04-10 10:54:25 -0400247 sk_sp<const GrXferProcessor> fXferProcessor;
Brian Salomon18dfa982017-04-03 16:57:43 -0400248 FragmentProcessorArray fFragmentProcessors;
egdanield9aa2182014-10-09 13:47:05 -0700249
bsalomonac856c92015-08-27 06:30:17 -0700250 // This value is also the index in fFragmentProcessors where coverage processors begin.
Brian Salomon18dfa982017-04-03 16:57:43 -0400251 int fNumColorProcessors;
Greg Daniel2c3398d2019-06-19 11:58:01 -0400252
253 GrSwizzle fOutputSwizzle;
egdaniel3658f382014-09-15 07:01:59 -0700254};
255
Chris Daltonbaa1b352019-04-03 12:03:00 -0600256GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::InputFlags);
257GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::Flags);
258
259inline bool operator&(GrPipeline::Flags flags, GrPipeline::InputFlags inputFlag) {
260 return (flags & (GrPipeline::Flags)inputFlag);
261}
262
egdaniel3658f382014-09-15 07:01:59 -0700263#endif