blob: 11eb8b0366c7e1b3bc00a6d3e89be5552aa593d1 [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"
Brian Salomonb8f098d2020-01-07 11:15:44 -050024#include "src/gpu/effects/GrTextureEffect.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;
Chris Daltonb832ce62020-01-06 19:49:37 -070028class GrAppliedHardClip;
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
Chris Daltoneb694b72020-03-16 09:25:50 -060035 * data 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 /**
Chris Daltonce425af2019-12-16 10:39:03 -070053 * Cause every pixel to be rasterized that is touched by the triangle anywhere (not just at
54 * pixel center). Additionally, if using MSAA, the sample mask will always have 100%
55 * coverage.
56 * NOTE: The primitive type must be a triangle type.
57 */
58 kConservativeRaster = (1 << 1),
59 /**
Chris Dalton1215cda2019-12-17 21:44:04 -070060 * Draws triangles as outlines.
61 */
62 kWireframe = (1 << 2),
63 /**
Brian Salomon189098e72017-01-19 09:55:19 -050064 * Modifies the vertex shader so that vertices will be positioned at pixel centers.
65 */
Chris Dalton1215cda2019-12-17 21:44:04 -070066 kSnapVerticesToPixelCenters = (1 << 3), // This value must be last. (See kLastInputFlag.)
Brian Salomon189098e72017-01-19 09:55:19 -050067 };
68
Brian Salomonb5cb6832017-02-24 11:01:15 -050069 struct InitArgs {
Chris Daltonbaa1b352019-04-03 12:03:00 -060070 InputFlags fInputFlags = InputFlags::kNone;
Brian Salomon189098e72017-01-19 09:55:19 -050071 const GrUserStencilSettings* fUserStencil = &GrUserStencilSettings::kUnused;
Brian Salomon189098e72017-01-19 09:55:19 -050072 const GrCaps* fCaps = nullptr;
Greg Daniel524e28b2019-11-01 11:48:53 -040073 GrXferProcessor::DstProxyView fDstProxyView;
Brian Salomon982f5462020-03-30 12:52:33 -040074 GrSwizzle fWriteSwizzle;
bsalomona387a112015-08-11 14:47:42 -070075 };
76
Brian Salomonb5cb6832017-02-24 11:01:15 -050077 /**
csmartdalton119fb2b2017-02-08 14:41:05 -050078 * Creates a simple pipeline with default settings and no processors. The provided blend mode
Chris Dalton916c4982018-08-15 00:53:25 -060079 * must be "Porter Duff" (<= kLastCoeffMode). If using GrScissorTest::kEnabled, the caller must
Chris Dalton46983b72017-06-06 12:27:16 -060080 * specify a scissor rectangle through the DynamicState struct.
csmartdalton119fb2b2017-02-08 14:41:05 -050081 **/
Brian Salomon982f5462020-03-30 12:52:33 -040082 GrPipeline(GrScissorTest scissor,
83 SkBlendMode blend,
84 const GrSwizzle& writeSwizzle,
Chris Daltonc3318f02019-07-19 14:20:53 -060085 InputFlags flags = InputFlags::kNone,
86 const GrUserStencilSettings* stencil = &GrUserStencilSettings::kUnused)
Brian Salomon982f5462020-03-30 12:52:33 -040087 : GrPipeline(scissor,
88 GrPorterDuffXPFactory::MakeNoCoverageXP(blend),
89 writeSwizzle,
90 flags,
91 stencil) {}
Chris Daltonc3318f02019-07-19 14:20:53 -060092
Brian Salomon982f5462020-03-30 12:52:33 -040093 GrPipeline(GrScissorTest,
94 sk_sp<const GrXferProcessor>,
95 const GrSwizzle& writeSwizzle,
Greg Daniel2c3398d2019-06-19 11:58:01 -040096 InputFlags = InputFlags::kNone,
Chris Daltond7291ba2019-03-07 14:17:03 -070097 const GrUserStencilSettings* = &GrUserStencilSettings::kUnused);
csmartdalton119fb2b2017-02-08 14:41:05 -050098
Chris Daltonb832ce62020-01-06 19:49:37 -070099 GrPipeline(const InitArgs& args, sk_sp<const GrXferProcessor>, const GrAppliedHardClip&);
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400100 GrPipeline(const InitArgs&, GrProcessorSet&&, GrAppliedClip&&);
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400101
Brian Salomon16351462017-07-19 16:35:31 -0400102 GrPipeline(const GrPipeline&) = delete;
103 GrPipeline& operator=(const GrPipeline&) = delete;
104
egdaniel89af44a2014-09-26 06:15:04 -0700105 /// @}
106
107 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -0800108 /// @name GrFragmentProcessors
egdaniel89af44a2014-09-26 06:15:04 -0700109
bsalomonac856c92015-08-27 06:30:17 -0700110 int numColorFragmentProcessors() const { return fNumColorProcessors; }
111 int numCoverageFragmentProcessors() const {
112 return fFragmentProcessors.count() - fNumColorProcessors;
113 }
114 int numFragmentProcessors() const { return fFragmentProcessors.count(); }
egdaniel89af44a2014-09-26 06:15:04 -0700115
Brian Salomond90b3d32020-07-09 12:04:31 -0400116 void visitTextureEffects(const std::function<void(const GrTextureEffect&)>&) const;
117
bsalomon2047b782015-12-21 13:12:54 -0800118 const GrXferProcessor& getXferProcessor() const {
Brian Salomond61c9d92017-04-10 10:54:25 -0400119 if (fXferProcessor) {
bsalomon2047b782015-12-21 13:12:54 -0800120 return *fXferProcessor.get();
121 } else {
122 // A null xp member means the common src-over case. GrXferProcessor's ref'ing
123 // mechanism is not thread safe so we do not hold a ref on this global.
124 return GrPorterDuffXPFactory::SimpleSrcOverXP();
125 }
126 }
egdaniel378092f2014-12-03 10:40:13 -0800127
Brian Salomon18dfa982017-04-03 16:57:43 -0400128 /**
Greg Daniel524e28b2019-11-01 11:48:53 -0400129 * This returns the GrSurfaceProxyView for the texture used to access the dst color. If the
130 * GrXferProcessor does not use the dst color then the proxy on the GrSurfaceProxyView will be
131 * nullptr.
132 */
133 const GrSurfaceProxyView& dstProxyView() const {
134 return fDstProxyView;
135 }
136
137 /**
Brian Salomon18dfa982017-04-03 16:57:43 -0400138 * If the GrXferProcessor uses a texture to access the dst color, then this returns that
139 * texture and the offset to the dst contents within that texture.
140 */
Greg Daniel524e28b2019-11-01 11:48:53 -0400141 GrTexture* peekDstTexture(SkIPoint* offset = nullptr) const {
Brian Salomon18dfa982017-04-03 16:57:43 -0400142 if (offset) {
143 *offset = fDstTextureOffset;
144 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400145
Greg Daniel524e28b2019-11-01 11:48:53 -0400146 if (GrTextureProxy* dstProxy = fDstProxyView.asTextureProxy()) {
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; }
Chris Dalton4328e922020-01-29 13:16:14 -0700170 void setUserStencil(const GrUserStencilSettings* stencil) {
171 fUserStencilSettings = stencil;
172 if (!fUserStencilSettings->isDisabled(fFlags & Flags::kHasStencilClip)) {
173 fFlags |= Flags::kStencilEnabled;
174 }
175 }
egdaniel89af44a2014-09-26 06:15:04 -0700176
Chris Dalton2e7ed262020-02-21 15:17:59 -0700177 bool isScissorTestEnabled() const {
178 return SkToBool(fFlags & Flags::kScissorTestEnabled);
Brian Salomon49348902018-06-26 09:12:38 -0400179 }
joshualitt54e0c122014-11-19 09:38:51 -0800180
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700181 const GrWindowRectsState& getWindowRectsState() const { return fWindowRectsState; }
csmartdalton28341fa2016-08-17 10:00:21 -0700182
Chris Daltonce425af2019-12-16 10:39:03 -0700183 bool isHWAntialiasState() const { return fFlags & InputFlags::kHWAntialias; }
184 bool usesConservativeRaster() const { return fFlags & InputFlags::kConservativeRaster; }
Chris Dalton1215cda2019-12-17 21:44:04 -0700185 bool isWireframe() const { return fFlags & InputFlags::kWireframe; }
Brian Salomon189098e72017-01-19 09:55:19 -0500186 bool snapVerticesToPixelCenters() const {
Chris Daltonce425af2019-12-16 10:39:03 -0700187 return fFlags & InputFlags::kSnapVerticesToPixelCenters;
Brian Salomon189098e72017-01-19 09:55:19 -0500188 }
cdalton193d9cf2016-05-12 11:52:02 -0700189 bool hasStencilClip() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600190 return SkToBool(fFlags & Flags::kHasStencilClip);
cdalton193d9cf2016-05-12 11:52:02 -0700191 }
csmartdaltonc633abb2016-11-01 08:55:55 -0700192 bool isStencilEnabled() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600193 return SkToBool(fFlags & Flags::kStencilEnabled);
csmartdaltonc633abb2016-11-01 08:55:55 -0700194 }
Robert Phillips8053c972019-11-21 10:44:53 -0500195#ifdef SK_DEBUG
196 bool allProxiesInstantiated() const {
197 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
198 if (!fFragmentProcessors[i]->isInstantiated()) {
199 return false;
200 }
201 }
202 if (fDstProxyView.proxy()) {
203 return fDstProxyView.proxy()->isInstantiated();
204 }
205
206 return true;
207 }
208#endif
bsalomonae59b772014-11-19 08:23:49 -0800209
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500210 GrXferBarrierType xferBarrierType(GrTexture*, const GrCaps&) const;
bsalomoncb02b382015-08-12 11:14:50 -0700211
Jim Van Verth1223e7f2019-02-28 17:38:35 -0500212 // Used by Vulkan and Metal to cache their respective pipeline objects
Chris Daltonb204e4c2019-11-07 12:43:13 -0700213 void genKey(GrProcessorKeyBuilder*, const GrCaps&) const;
Jim Van Verth1223e7f2019-02-28 17:38:35 -0500214
Brian Salomon982f5462020-03-30 12:52:33 -0400215 const GrSwizzle& writeSwizzle() const { return fWriteSwizzle; }
Greg Daniel2c3398d2019-06-19 11:58:01 -0400216
Brian Salomonc241b582019-11-27 08:57:17 -0500217 void visitProxies(const GrOp::VisitProxyFunc&) const;
Robert Phillips8053c972019-11-21 10:44:53 -0500218
bsalomonae59b772014-11-19 08:23:49 -0800219private:
Chris Daltonbaa1b352019-04-03 12:03:00 -0600220 static constexpr uint8_t kLastInputFlag = (uint8_t)InputFlags::kSnapVerticesToPixelCenters;
221
222 /** This is a continuation of the public "InputFlags" enum. */
223 enum class Flags : uint8_t {
224 kHasStencilClip = (kLastInputFlag << 1),
225 kStencilEnabled = (kLastInputFlag << 2),
Chris Dalton2e7ed262020-02-21 15:17:59 -0700226 kScissorTestEnabled = (kLastInputFlag << 3),
bsalomon04ddf892014-11-19 12:36:22 -0800227 };
228
Chris Daltonbaa1b352019-04-03 12:03:00 -0600229 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(Flags);
230
231 friend bool operator&(Flags, InputFlags);
232
Brian Salomonaff329b2017-08-11 09:40:37 -0400233 using FragmentProcessorArray = SkAutoSTArray<8, std::unique_ptr<const GrFragmentProcessor>>;
Brian Salomon18dfa982017-04-03 16:57:43 -0400234
Greg Daniel524e28b2019-11-01 11:48:53 -0400235 GrSurfaceProxyView fDstProxyView;
Brian Salomon18dfa982017-04-03 16:57:43 -0400236 SkIPoint fDstTextureOffset;
Brian Salomon18dfa982017-04-03 16:57:43 -0400237 GrWindowRectsState fWindowRectsState;
238 const GrUserStencilSettings* fUserStencilSettings;
Chris Daltonbaa1b352019-04-03 12:03:00 -0600239 Flags fFlags;
Brian Salomond61c9d92017-04-10 10:54:25 -0400240 sk_sp<const GrXferProcessor> fXferProcessor;
Brian Salomon18dfa982017-04-03 16:57:43 -0400241 FragmentProcessorArray fFragmentProcessors;
egdanield9aa2182014-10-09 13:47:05 -0700242
bsalomonac856c92015-08-27 06:30:17 -0700243 // This value is also the index in fFragmentProcessors where coverage processors begin.
Chris Daltonb832ce62020-01-06 19:49:37 -0700244 int fNumColorProcessors = 0;
Greg Daniel2c3398d2019-06-19 11:58:01 -0400245
Brian Salomon982f5462020-03-30 12:52:33 -0400246 GrSwizzle fWriteSwizzle;
egdaniel3658f382014-09-15 07:01:59 -0700247};
248
Chris Daltonbaa1b352019-04-03 12:03:00 -0600249GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::InputFlags);
250GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::Flags);
251
252inline bool operator&(GrPipeline::Flags flags, GrPipeline::InputFlags inputFlag) {
253 return (flags & (GrPipeline::Flags)inputFlag);
254}
255
egdaniel3658f382014-09-15 07:01:59 -0700256#endif