blob: 0e70679c867c7c70d87d24539f6442db668b895d [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/SkRefCnt.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrColor.h"
John Stiles87960de2021-06-03 16:44:53 -040013#include "src/gpu/GrDstProxyView.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrProcessorSet.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrScissorState.h"
Greg Daniel524e28b2019-11-01 11:48:53 -040017#include "src/gpu/GrSurfaceProxyView.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrUserStencilSettings.h"
19#include "src/gpu/GrWindowRectsState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
robertphillips5fa7f302016-07-21 09:21:04 -070021
Brian Salomon652ecb52017-01-17 12:39:53 -050022class GrAppliedClip;
Chris Daltonb832ce62020-01-06 19:49:37 -070023class GrAppliedHardClip;
John Stiles010d0882021-06-03 20:28:41 -040024struct GrGLSLBuiltinUniformHandles;
25class GrGLSLProgramDataManager;
Brian Salomon25a88092016-12-01 09:36:50 -050026class GrOp;
Robert Phillips550de7f2021-07-06 16:28:52 -040027class GrTextureEffect;
egdaniel3658f382014-09-15 07:01:59 -070028
Brian Salomon92aee3d2016-12-21 09:20:25 -050029/**
John Stiles7c328b42021-05-10 17:33:22 -040030 * This immutable object contains information needed to build a shader program and set API
Robert Phillips787fd9d2021-03-22 14:48:09 -040031 * state for a draw. It is used along with a GrGeometryProcessor and a source of geometric
Chris Daltoneb694b72020-03-16 09:25:50 -060032 * data to draw.
egdaniel3658f382014-09-15 07:01:59 -070033 */
Brian Salomon16351462017-07-19 16:35:31 -040034class GrPipeline {
egdaniel3658f382014-09-15 07:01:59 -070035public:
bsalomoncb02b382015-08-12 11:14:50 -070036 ///////////////////////////////////////////////////////////////////////////
37 /// @name Creation
38
Chris Daltonbaa1b352019-04-03 12:03:00 -060039 // Pipeline options that the caller may enable.
40 // NOTE: This enum is extended later by GrPipeline::Flags.
41 enum class InputFlags : uint8_t {
42 kNone = 0,
Brian Salomon189098e72017-01-19 09:55:19 -050043 /**
Chris Daltonce425af2019-12-16 10:39:03 -070044 * Cause every pixel to be rasterized that is touched by the triangle anywhere (not just at
45 * pixel center). Additionally, if using MSAA, the sample mask will always have 100%
46 * coverage.
47 * NOTE: The primitive type must be a triangle type.
48 */
49 kConservativeRaster = (1 << 1),
50 /**
Chris Dalton1215cda2019-12-17 21:44:04 -070051 * Draws triangles as outlines.
52 */
53 kWireframe = (1 << 2),
54 /**
Brian Salomon189098e72017-01-19 09:55:19 -050055 * Modifies the vertex shader so that vertices will be positioned at pixel centers.
56 */
Chris Dalton1215cda2019-12-17 21:44:04 -070057 kSnapVerticesToPixelCenters = (1 << 3), // This value must be last. (See kLastInputFlag.)
Brian Salomon189098e72017-01-19 09:55:19 -050058 };
59
Brian Salomonb5cb6832017-02-24 11:01:15 -050060 struct InitArgs {
Chris Daltonbaa1b352019-04-03 12:03:00 -060061 InputFlags fInputFlags = InputFlags::kNone;
Brian Salomon189098e72017-01-19 09:55:19 -050062 const GrCaps* fCaps = nullptr;
John Stiles52cb1d02021-06-02 11:58:05 -040063 GrDstProxyView fDstProxyView;
Brian Salomon982f5462020-03-30 12:52:33 -040064 GrSwizzle fWriteSwizzle;
bsalomona387a112015-08-11 14:47:42 -070065 };
66
Brian Salomonb5cb6832017-02-24 11:01:15 -050067 /**
csmartdalton119fb2b2017-02-08 14:41:05 -050068 * Creates a simple pipeline with default settings and no processors. The provided blend mode
Chris Dalton916c4982018-08-15 00:53:25 -060069 * must be "Porter Duff" (<= kLastCoeffMode). If using GrScissorTest::kEnabled, the caller must
Chris Dalton46983b72017-06-06 12:27:16 -060070 * specify a scissor rectangle through the DynamicState struct.
csmartdalton119fb2b2017-02-08 14:41:05 -050071 **/
Brian Salomon982f5462020-03-30 12:52:33 -040072 GrPipeline(GrScissorTest scissor,
73 SkBlendMode blend,
74 const GrSwizzle& writeSwizzle,
Chris Dalton1b6a43c2020-09-25 12:21:18 -060075 InputFlags flags = InputFlags::kNone)
Brian Salomon982f5462020-03-30 12:52:33 -040076 : GrPipeline(scissor,
77 GrPorterDuffXPFactory::MakeNoCoverageXP(blend),
78 writeSwizzle,
Chris Dalton1b6a43c2020-09-25 12:21:18 -060079 flags) {}
Chris Daltonc3318f02019-07-19 14:20:53 -060080
Brian Salomon982f5462020-03-30 12:52:33 -040081 GrPipeline(GrScissorTest,
82 sk_sp<const GrXferProcessor>,
83 const GrSwizzle& writeSwizzle,
Chris Dalton1b6a43c2020-09-25 12:21:18 -060084 InputFlags = InputFlags::kNone);
csmartdalton119fb2b2017-02-08 14:41:05 -050085
Chris Daltonb832ce62020-01-06 19:49:37 -070086 GrPipeline(const InitArgs& args, sk_sp<const GrXferProcessor>, const GrAppliedHardClip&);
Brian Salomonbfd18cd2017-08-09 16:27:09 -040087 GrPipeline(const InitArgs&, GrProcessorSet&&, GrAppliedClip&&);
Brian Salomon6d4b65e2017-05-03 17:06:09 -040088
Brian Salomon16351462017-07-19 16:35:31 -040089 GrPipeline(const GrPipeline&) = delete;
90 GrPipeline& operator=(const GrPipeline&) = delete;
91
egdaniel89af44a2014-09-26 06:15:04 -070092 /// @}
93
94 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -080095 /// @name GrFragmentProcessors
egdaniel89af44a2014-09-26 06:15:04 -070096
bsalomonac856c92015-08-27 06:30:17 -070097 int numFragmentProcessors() const { return fFragmentProcessors.count(); }
Brian Osman33167962021-03-04 13:05:49 -050098 int numColorFragmentProcessors() const { return fNumColorProcessors; }
John Stilesd3feb6f2020-07-23 18:18:12 -040099 bool isColorFragmentProcessor(int idx) const { return idx < fNumColorProcessors; }
100 bool isCoverageFragmentProcessor(int idx) const { return idx >= fNumColorProcessors; }
egdaniel89af44a2014-09-26 06:15:04 -0700101
Brian Salomon66b500a2021-08-02 12:37:14 -0400102 bool usesLocalCoords() const {
103 // The sample coords for the top level FPs are implicitly the GP's local coords.
Chris Dalton198ac152021-06-09 13:49:43 -0600104 for (const auto& fp : fFragmentProcessors) {
Brian Salomon66b500a2021-08-02 12:37:14 -0400105 if (fp->usesSampleCoords()) {
Chris Dalton198ac152021-06-09 13:49:43 -0600106 return true;
107 }
108 }
109 return false;
110 }
111
Brian Salomond90b3d32020-07-09 12:04:31 -0400112 void visitTextureEffects(const std::function<void(const GrTextureEffect&)>&) const;
113
bsalomon2047b782015-12-21 13:12:54 -0800114 const GrXferProcessor& getXferProcessor() const {
Brian Salomond61c9d92017-04-10 10:54:25 -0400115 if (fXferProcessor) {
John Stilesa008b0f2020-08-16 08:48:02 -0400116 return *fXferProcessor;
bsalomon2047b782015-12-21 13:12:54 -0800117 } else {
118 // A null xp member means the common src-over case. GrXferProcessor's ref'ing
119 // mechanism is not thread safe so we do not hold a ref on this global.
120 return GrPorterDuffXPFactory::SimpleSrcOverXP();
121 }
122 }
egdaniel378092f2014-12-03 10:40:13 -0800123
Greg Danield358cbe2020-09-11 09:33:54 -0400124 // Helper functions to quickly know if this GrPipeline will access the dst as a texture or an
125 // input attachment.
Greg Daniel87fab9f2021-06-07 15:18:23 -0400126 bool usesDstTexture() const { return this->dstProxyView() && !this->usesDstInputAttachment(); }
127 bool usesDstInputAttachment() const {
Robert Phillips550de7f2021-07-06 16:28:52 -0400128 return this->dstSampleFlags() & GrDstSampleFlags::kAsInputAttachment;
Greg Danield358cbe2020-09-11 09:33:54 -0400129 }
130
Brian Salomon18dfa982017-04-03 16:57:43 -0400131 /**
Greg Daniel524e28b2019-11-01 11:48:53 -0400132 * This returns the GrSurfaceProxyView for the texture used to access the dst color. If the
133 * GrXferProcessor does not use the dst color then the proxy on the GrSurfaceProxyView will be
134 * nullptr.
135 */
John Stilesbcfdc1d2021-06-04 17:19:43 -0400136 const GrSurfaceProxyView& dstProxyView() const { return fDstProxy.proxyView(); }
137
138 SkIPoint dstTextureOffset() const { return fDstProxy.offset(); }
139
Greg Daniel87fab9f2021-06-07 15:18:23 -0400140 GrDstSampleFlags dstSampleFlags() const { return fDstProxy.dstSampleFlags(); }
Greg Daniel524e28b2019-11-01 11:48:53 -0400141
John Stiles3e77d6e2021-06-04 15:30:50 -0400142 /** If this GrXferProcessor uses a texture to access the dst color, returns that texture. */
143 GrTexture* peekDstTexture() const {
Greg Danield358cbe2020-09-11 09:33:54 -0400144 if (!this->usesDstTexture()) {
145 return nullptr;
146 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400147
John Stilesbcfdc1d2021-06-04 17:19:43 -0400148 if (GrTextureProxy* dstProxy = this->dstProxyView().asTextureProxy()) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400149 return dstProxy->peekTexture();
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400150 }
151
152 return nullptr;
Brian Salomon18dfa982017-04-03 16:57:43 -0400153 }
154
bsalomonac856c92015-08-27 06:30:17 -0700155 const GrFragmentProcessor& getFragmentProcessor(int idx) const {
John Stilesa008b0f2020-08-16 08:48:02 -0400156 return *fFragmentProcessors[idx];
bsalomonae59b772014-11-19 08:23:49 -0800157 }
egdaniel89af44a2014-09-26 06:15:04 -0700158
159 /// @}
160
Chris Dalton2e7ed262020-02-21 15:17:59 -0700161 bool isScissorTestEnabled() const {
162 return SkToBool(fFlags & Flags::kScissorTestEnabled);
Brian Salomon49348902018-06-26 09:12:38 -0400163 }
joshualitt54e0c122014-11-19 09:38:51 -0800164
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700165 const GrWindowRectsState& getWindowRectsState() const { return fWindowRectsState; }
csmartdalton28341fa2016-08-17 10:00:21 -0700166
Chris Daltonce425af2019-12-16 10:39:03 -0700167 bool usesConservativeRaster() const { return fFlags & InputFlags::kConservativeRaster; }
Chris Dalton1215cda2019-12-17 21:44:04 -0700168 bool isWireframe() const { return fFlags & InputFlags::kWireframe; }
Brian Salomon189098e72017-01-19 09:55:19 -0500169 bool snapVerticesToPixelCenters() const {
Chris Daltonce425af2019-12-16 10:39:03 -0700170 return fFlags & InputFlags::kSnapVerticesToPixelCenters;
Brian Salomon189098e72017-01-19 09:55:19 -0500171 }
cdalton193d9cf2016-05-12 11:52:02 -0700172 bool hasStencilClip() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600173 return SkToBool(fFlags & Flags::kHasStencilClip);
cdalton193d9cf2016-05-12 11:52:02 -0700174 }
Robert Phillips8053c972019-11-21 10:44:53 -0500175#ifdef SK_DEBUG
176 bool allProxiesInstantiated() const {
177 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
178 if (!fFragmentProcessors[i]->isInstantiated()) {
179 return false;
180 }
181 }
John Stilesbcfdc1d2021-06-04 17:19:43 -0400182 if (this->dstProxyView().proxy()) {
183 return this->dstProxyView().proxy()->isInstantiated();
Robert Phillips8053c972019-11-21 10:44:53 -0500184 }
185
186 return true;
187 }
188#endif
bsalomonae59b772014-11-19 08:23:49 -0800189
Greg Danield358cbe2020-09-11 09:33:54 -0400190 GrXferBarrierType xferBarrierType(const GrCaps&) const;
bsalomoncb02b382015-08-12 11:14:50 -0700191
Jim Van Verth1223e7f2019-02-28 17:38:35 -0500192 // Used by Vulkan and Metal to cache their respective pipeline objects
Chris Daltonb204e4c2019-11-07 12:43:13 -0700193 void genKey(GrProcessorKeyBuilder*, const GrCaps&) const;
Jim Van Verth1223e7f2019-02-28 17:38:35 -0500194
Brian Salomon982f5462020-03-30 12:52:33 -0400195 const GrSwizzle& writeSwizzle() const { return fWriteSwizzle; }
Greg Daniel2c3398d2019-06-19 11:58:01 -0400196
Robert Phillips294723d2021-06-17 09:23:58 -0400197 void visitProxies(const GrVisitProxyFunc&) const;
Robert Phillips8053c972019-11-21 10:44:53 -0500198
John Stiles010d0882021-06-03 20:28:41 -0400199 void setDstTextureUniforms(const GrGLSLProgramDataManager& pdm,
200 GrGLSLBuiltinUniformHandles* fBuiltinUniformHandles) const;
201
bsalomonae59b772014-11-19 08:23:49 -0800202private:
Chris Daltonbaa1b352019-04-03 12:03:00 -0600203 static constexpr uint8_t kLastInputFlag = (uint8_t)InputFlags::kSnapVerticesToPixelCenters;
204
205 /** This is a continuation of the public "InputFlags" enum. */
206 enum class Flags : uint8_t {
207 kHasStencilClip = (kLastInputFlag << 1),
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600208 kScissorTestEnabled = (kLastInputFlag << 2),
bsalomon04ddf892014-11-19 12:36:22 -0800209 };
210
Chris Daltonbaa1b352019-04-03 12:03:00 -0600211 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(Flags);
212
213 friend bool operator&(Flags, InputFlags);
214
John Stiles59e18dc2020-07-22 18:18:12 -0400215 // A pipeline can contain up to three processors: color, paint coverage, and clip coverage.
216 using FragmentProcessorArray = SkAutoSTArray<3, std::unique_ptr<const GrFragmentProcessor>>;
Brian Salomon18dfa982017-04-03 16:57:43 -0400217
John Stilesbcfdc1d2021-06-04 17:19:43 -0400218 GrDstProxyView fDstProxy;
Brian Salomon18dfa982017-04-03 16:57:43 -0400219 GrWindowRectsState fWindowRectsState;
Chris Daltonbaa1b352019-04-03 12:03:00 -0600220 Flags fFlags;
Brian Salomond61c9d92017-04-10 10:54:25 -0400221 sk_sp<const GrXferProcessor> fXferProcessor;
Brian Salomon18dfa982017-04-03 16:57:43 -0400222 FragmentProcessorArray fFragmentProcessors;
egdanield9aa2182014-10-09 13:47:05 -0700223
bsalomonac856c92015-08-27 06:30:17 -0700224 // This value is also the index in fFragmentProcessors where coverage processors begin.
Chris Daltonb832ce62020-01-06 19:49:37 -0700225 int fNumColorProcessors = 0;
Greg Daniel2c3398d2019-06-19 11:58:01 -0400226
Brian Salomon982f5462020-03-30 12:52:33 -0400227 GrSwizzle fWriteSwizzle;
egdaniel3658f382014-09-15 07:01:59 -0700228};
229
Brian Osman2421b992021-06-19 10:44:54 -0400230GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::InputFlags)
231GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::Flags)
Chris Daltonbaa1b352019-04-03 12:03:00 -0600232
233inline bool operator&(GrPipeline::Flags flags, GrPipeline::InputFlags inputFlag) {
234 return (flags & (GrPipeline::Flags)inputFlag);
235}
236
egdaniel3658f382014-09-15 07:01:59 -0700237#endif