blob: d8639ff86149fad5050ab9fdd08a4c519fe13106 [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"
John Stiles87960de2021-06-03 16:44:53 -040014#include "src/gpu/GrDstProxyView.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrFragmentProcessor.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;
John Stiles010d0882021-06-03 20:28:41 -040029struct GrGLSLBuiltinUniformHandles;
30class GrGLSLProgramDataManager;
Brian Salomon25a88092016-12-01 09:36:50 -050031class GrOp;
Brian Salomoneebe7352020-12-09 16:37:04 -050032class GrSurfaceDrawContext;
egdaniel3658f382014-09-15 07:01:59 -070033
Brian Salomon92aee3d2016-12-21 09:20:25 -050034/**
John Stiles7c328b42021-05-10 17:33:22 -040035 * This immutable object contains information needed to build a shader program and set API
Robert Phillips787fd9d2021-03-22 14:48:09 -040036 * state for a draw. It is used along with a GrGeometryProcessor and a source of geometric
Chris Daltoneb694b72020-03-16 09:25:50 -060037 * data to draw.
egdaniel3658f382014-09-15 07:01:59 -070038 */
Brian Salomon16351462017-07-19 16:35:31 -040039class GrPipeline {
egdaniel3658f382014-09-15 07:01:59 -070040public:
bsalomoncb02b382015-08-12 11:14:50 -070041 ///////////////////////////////////////////////////////////////////////////
42 /// @name Creation
43
Chris Daltonbaa1b352019-04-03 12:03:00 -060044 // Pipeline options that the caller may enable.
45 // NOTE: This enum is extended later by GrPipeline::Flags.
46 enum class InputFlags : uint8_t {
47 kNone = 0,
Brian Salomon189098e72017-01-19 09:55:19 -050048 /**
49 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
50 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
51 * the 3D API.
52 */
Chris Daltonbaa1b352019-04-03 12:03:00 -060053 kHWAntialias = (1 << 0),
Brian Salomon189098e72017-01-19 09:55:19 -050054 /**
Chris Daltonce425af2019-12-16 10:39:03 -070055 * Cause every pixel to be rasterized that is touched by the triangle anywhere (not just at
56 * pixel center). Additionally, if using MSAA, the sample mask will always have 100%
57 * coverage.
58 * NOTE: The primitive type must be a triangle type.
59 */
60 kConservativeRaster = (1 << 1),
61 /**
Chris Dalton1215cda2019-12-17 21:44:04 -070062 * Draws triangles as outlines.
63 */
64 kWireframe = (1 << 2),
65 /**
Brian Salomon189098e72017-01-19 09:55:19 -050066 * Modifies the vertex shader so that vertices will be positioned at pixel centers.
67 */
Chris Dalton1215cda2019-12-17 21:44:04 -070068 kSnapVerticesToPixelCenters = (1 << 3), // This value must be last. (See kLastInputFlag.)
Brian Salomon189098e72017-01-19 09:55:19 -050069 };
70
Brian Salomonb5cb6832017-02-24 11:01:15 -050071 struct InitArgs {
Chris Daltonbaa1b352019-04-03 12:03:00 -060072 InputFlags fInputFlags = InputFlags::kNone;
Brian Salomon189098e72017-01-19 09:55:19 -050073 const GrCaps* fCaps = nullptr;
John Stiles52cb1d02021-06-02 11:58:05 -040074 GrDstProxyView fDstProxyView;
Brian Salomon982f5462020-03-30 12:52:33 -040075 GrSwizzle fWriteSwizzle;
bsalomona387a112015-08-11 14:47:42 -070076 };
77
Brian Salomonb5cb6832017-02-24 11:01:15 -050078 /**
csmartdalton119fb2b2017-02-08 14:41:05 -050079 * Creates a simple pipeline with default settings and no processors. The provided blend mode
Chris Dalton916c4982018-08-15 00:53:25 -060080 * must be "Porter Duff" (<= kLastCoeffMode). If using GrScissorTest::kEnabled, the caller must
Chris Dalton46983b72017-06-06 12:27:16 -060081 * specify a scissor rectangle through the DynamicState struct.
csmartdalton119fb2b2017-02-08 14:41:05 -050082 **/
Brian Salomon982f5462020-03-30 12:52:33 -040083 GrPipeline(GrScissorTest scissor,
84 SkBlendMode blend,
85 const GrSwizzle& writeSwizzle,
Chris Dalton1b6a43c2020-09-25 12:21:18 -060086 InputFlags flags = InputFlags::kNone)
Brian Salomon982f5462020-03-30 12:52:33 -040087 : GrPipeline(scissor,
88 GrPorterDuffXPFactory::MakeNoCoverageXP(blend),
89 writeSwizzle,
Chris Dalton1b6a43c2020-09-25 12:21:18 -060090 flags) {}
Chris Daltonc3318f02019-07-19 14:20:53 -060091
Brian Salomon982f5462020-03-30 12:52:33 -040092 GrPipeline(GrScissorTest,
93 sk_sp<const GrXferProcessor>,
94 const GrSwizzle& writeSwizzle,
Chris Dalton1b6a43c2020-09-25 12:21:18 -060095 InputFlags = InputFlags::kNone);
csmartdalton119fb2b2017-02-08 14:41:05 -050096
Chris Daltonb832ce62020-01-06 19:49:37 -070097 GrPipeline(const InitArgs& args, sk_sp<const GrXferProcessor>, const GrAppliedHardClip&);
Brian Salomonbfd18cd2017-08-09 16:27:09 -040098 GrPipeline(const InitArgs&, GrProcessorSet&&, GrAppliedClip&&);
Brian Salomon6d4b65e2017-05-03 17:06:09 -040099
Brian Salomon16351462017-07-19 16:35:31 -0400100 GrPipeline(const GrPipeline&) = delete;
101 GrPipeline& operator=(const GrPipeline&) = delete;
102
egdaniel89af44a2014-09-26 06:15:04 -0700103 /// @}
104
105 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -0800106 /// @name GrFragmentProcessors
egdaniel89af44a2014-09-26 06:15:04 -0700107
bsalomonac856c92015-08-27 06:30:17 -0700108 int numFragmentProcessors() const { return fFragmentProcessors.count(); }
Brian Osman33167962021-03-04 13:05:49 -0500109 int numColorFragmentProcessors() const { return fNumColorProcessors; }
John Stilesd3feb6f2020-07-23 18:18:12 -0400110 bool isColorFragmentProcessor(int idx) const { return idx < fNumColorProcessors; }
111 bool isCoverageFragmentProcessor(int idx) const { return idx >= fNumColorProcessors; }
egdaniel89af44a2014-09-26 06:15:04 -0700112
Brian Salomond90b3d32020-07-09 12:04:31 -0400113 void visitTextureEffects(const std::function<void(const GrTextureEffect&)>&) const;
114
bsalomon2047b782015-12-21 13:12:54 -0800115 const GrXferProcessor& getXferProcessor() const {
Brian Salomond61c9d92017-04-10 10:54:25 -0400116 if (fXferProcessor) {
John Stilesa008b0f2020-08-16 08:48:02 -0400117 return *fXferProcessor;
bsalomon2047b782015-12-21 13:12:54 -0800118 } else {
119 // A null xp member means the common src-over case. GrXferProcessor's ref'ing
120 // mechanism is not thread safe so we do not hold a ref on this global.
121 return GrPorterDuffXPFactory::SimpleSrcOverXP();
122 }
123 }
egdaniel378092f2014-12-03 10:40:13 -0800124
Greg Danield358cbe2020-09-11 09:33:54 -0400125 // Helper functions to quickly know if this GrPipeline will access the dst as a texture or an
126 // input attachment.
127 bool usesDstTexture() const {
John Stilesbcfdc1d2021-06-04 17:19:43 -0400128 return GrDstSampleTypeUsesTexture(this->dstSampleType());
Greg Danield358cbe2020-09-11 09:33:54 -0400129 }
130 bool usesInputAttachment() const {
John Stilesbcfdc1d2021-06-04 17:19:43 -0400131 return this->dstSampleType() == GrDstSampleType::kAsInputAttachment;
Greg Danield358cbe2020-09-11 09:33:54 -0400132 }
133
Brian Salomon18dfa982017-04-03 16:57:43 -0400134 /**
Greg Daniel524e28b2019-11-01 11:48:53 -0400135 * This returns the GrSurfaceProxyView for the texture used to access the dst color. If the
136 * GrXferProcessor does not use the dst color then the proxy on the GrSurfaceProxyView will be
137 * nullptr.
138 */
John Stilesbcfdc1d2021-06-04 17:19:43 -0400139 const GrSurfaceProxyView& dstProxyView() const { return fDstProxy.proxyView(); }
140
141 SkIPoint dstTextureOffset() const { return fDstProxy.offset(); }
142
143 GrDstSampleType dstSampleType() const { return fDstProxy.dstSampleType(); }
Greg Daniel524e28b2019-11-01 11:48:53 -0400144
John Stiles3e77d6e2021-06-04 15:30:50 -0400145 /** If this GrXferProcessor uses a texture to access the dst color, returns that texture. */
146 GrTexture* peekDstTexture() const {
Greg Danield358cbe2020-09-11 09:33:54 -0400147 if (!this->usesDstTexture()) {
148 return nullptr;
149 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400150
John Stilesbcfdc1d2021-06-04 17:19:43 -0400151 if (GrTextureProxy* dstProxy = this->dstProxyView().asTextureProxy()) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400152 return dstProxy->peekTexture();
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400153 }
154
155 return nullptr;
Brian Salomon18dfa982017-04-03 16:57:43 -0400156 }
157
bsalomonac856c92015-08-27 06:30:17 -0700158 const GrFragmentProcessor& getFragmentProcessor(int idx) const {
John Stilesa008b0f2020-08-16 08:48:02 -0400159 return *fFragmentProcessors[idx];
bsalomonae59b772014-11-19 08:23:49 -0800160 }
egdaniel89af44a2014-09-26 06:15:04 -0700161
162 /// @}
163
Chris Dalton2e7ed262020-02-21 15:17:59 -0700164 bool isScissorTestEnabled() const {
165 return SkToBool(fFlags & Flags::kScissorTestEnabled);
Brian Salomon49348902018-06-26 09:12:38 -0400166 }
joshualitt54e0c122014-11-19 09:38:51 -0800167
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700168 const GrWindowRectsState& getWindowRectsState() const { return fWindowRectsState; }
csmartdalton28341fa2016-08-17 10:00:21 -0700169
Chris Daltonce425af2019-12-16 10:39:03 -0700170 bool isHWAntialiasState() const { return fFlags & InputFlags::kHWAntialias; }
171 bool usesConservativeRaster() const { return fFlags & InputFlags::kConservativeRaster; }
Chris Dalton1215cda2019-12-17 21:44:04 -0700172 bool isWireframe() const { return fFlags & InputFlags::kWireframe; }
Brian Salomon189098e72017-01-19 09:55:19 -0500173 bool snapVerticesToPixelCenters() const {
Chris Daltonce425af2019-12-16 10:39:03 -0700174 return fFlags & InputFlags::kSnapVerticesToPixelCenters;
Brian Salomon189098e72017-01-19 09:55:19 -0500175 }
cdalton193d9cf2016-05-12 11:52:02 -0700176 bool hasStencilClip() const {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600177 return SkToBool(fFlags & Flags::kHasStencilClip);
cdalton193d9cf2016-05-12 11:52:02 -0700178 }
Robert Phillips8053c972019-11-21 10:44:53 -0500179#ifdef SK_DEBUG
180 bool allProxiesInstantiated() const {
181 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
182 if (!fFragmentProcessors[i]->isInstantiated()) {
183 return false;
184 }
185 }
John Stilesbcfdc1d2021-06-04 17:19:43 -0400186 if (this->dstProxyView().proxy()) {
187 return this->dstProxyView().proxy()->isInstantiated();
Robert Phillips8053c972019-11-21 10:44:53 -0500188 }
189
190 return true;
191 }
192#endif
bsalomonae59b772014-11-19 08:23:49 -0800193
Greg Danield358cbe2020-09-11 09:33:54 -0400194 GrXferBarrierType xferBarrierType(const GrCaps&) const;
bsalomoncb02b382015-08-12 11:14:50 -0700195
Jim Van Verth1223e7f2019-02-28 17:38:35 -0500196 // Used by Vulkan and Metal to cache their respective pipeline objects
Chris Daltonb204e4c2019-11-07 12:43:13 -0700197 void genKey(GrProcessorKeyBuilder*, const GrCaps&) const;
Jim Van Verth1223e7f2019-02-28 17:38:35 -0500198
Brian Salomon982f5462020-03-30 12:52:33 -0400199 const GrSwizzle& writeSwizzle() const { return fWriteSwizzle; }
Greg Daniel2c3398d2019-06-19 11:58:01 -0400200
Brian Salomonc241b582019-11-27 08:57:17 -0500201 void visitProxies(const GrOp::VisitProxyFunc&) const;
Robert Phillips8053c972019-11-21 10:44:53 -0500202
John Stiles010d0882021-06-03 20:28:41 -0400203 void setDstTextureUniforms(const GrGLSLProgramDataManager& pdm,
204 GrGLSLBuiltinUniformHandles* fBuiltinUniformHandles) const;
205
bsalomonae59b772014-11-19 08:23:49 -0800206private:
Chris Daltonbaa1b352019-04-03 12:03:00 -0600207 static constexpr uint8_t kLastInputFlag = (uint8_t)InputFlags::kSnapVerticesToPixelCenters;
208
209 /** This is a continuation of the public "InputFlags" enum. */
210 enum class Flags : uint8_t {
211 kHasStencilClip = (kLastInputFlag << 1),
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600212 kScissorTestEnabled = (kLastInputFlag << 2),
bsalomon04ddf892014-11-19 12:36:22 -0800213 };
214
Chris Daltonbaa1b352019-04-03 12:03:00 -0600215 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(Flags);
216
217 friend bool operator&(Flags, InputFlags);
218
John Stiles59e18dc2020-07-22 18:18:12 -0400219 // A pipeline can contain up to three processors: color, paint coverage, and clip coverage.
220 using FragmentProcessorArray = SkAutoSTArray<3, std::unique_ptr<const GrFragmentProcessor>>;
Brian Salomon18dfa982017-04-03 16:57:43 -0400221
John Stilesbcfdc1d2021-06-04 17:19:43 -0400222 GrDstProxyView fDstProxy;
Brian Salomon18dfa982017-04-03 16:57:43 -0400223 GrWindowRectsState fWindowRectsState;
Chris Daltonbaa1b352019-04-03 12:03:00 -0600224 Flags fFlags;
Brian Salomond61c9d92017-04-10 10:54:25 -0400225 sk_sp<const GrXferProcessor> fXferProcessor;
Brian Salomon18dfa982017-04-03 16:57:43 -0400226 FragmentProcessorArray fFragmentProcessors;
egdanield9aa2182014-10-09 13:47:05 -0700227
bsalomonac856c92015-08-27 06:30:17 -0700228 // This value is also the index in fFragmentProcessors where coverage processors begin.
Chris Daltonb832ce62020-01-06 19:49:37 -0700229 int fNumColorProcessors = 0;
Greg Daniel2c3398d2019-06-19 11:58:01 -0400230
Brian Salomon982f5462020-03-30 12:52:33 -0400231 GrSwizzle fWriteSwizzle;
egdaniel3658f382014-09-15 07:01:59 -0700232};
233
Chris Daltonbaa1b352019-04-03 12:03:00 -0600234GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::InputFlags);
235GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::Flags);
236
237inline bool operator&(GrPipeline::Flags flags, GrPipeline::InputFlags inputFlag) {
238 return (flags & (GrPipeline::Flags)inputFlag);
239}
240
egdaniel3658f382014-09-15 07:01:59 -0700241#endif