blob: 68e70ac6b9af500946d68eff83eabbdcc3a9f04b [file] [log] [blame]
tomhudson@google.com93813632011-10-27 20:21:16 +00001/*
egdaniel8dd688b2015-01-22 10:16:09 -08002 * Copyright 2015 Google Inc.
tomhudson@google.com93813632011-10-27 20:21:16 +00003 *
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 GrPipelineBuilder_DEFINED
9#define GrPipelineBuilder_DEFINED
tomhudson@google.com93813632011-10-27 20:21:16 +000010
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000011#include "GrBlend.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070012#include "GrCaps.h"
bsalomonf96ba022014-09-17 08:05:40 -070013#include "GrGpuResourceRef.h"
egdanielb6cbc382014-11-13 11:00:34 -080014#include "GrProcOptInfo.h"
egdaniel89af44a2014-09-26 06:15:04 -070015#include "GrRenderTarget.h"
cdalton93a379b2016-05-11 13:58:08 -070016#include "GrUserStencilSettings.h"
egdaniel95131432014-12-09 11:15:43 -080017#include "GrXferProcessor.h"
egdaniel89af44a2014-09-26 06:15:04 -070018#include "SkMatrix.h"
egdaniel87509242014-12-17 13:37:13 -080019#include "effects/GrCoverageSetOpXP.h"
egdaniel080e6732014-12-22 07:35:52 -080020#include "effects/GrDisableColorXP.h"
egdaniel95131432014-12-09 11:15:43 -080021#include "effects/GrPorterDuffXferProcessor.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000022#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000023
bsalomonabd30f52015-08-13 13:34:48 -070024class GrDrawBatch;
bsalomon4b91f762015-05-19 09:29:46 -070025class GrCaps;
egdaniel89af44a2014-09-26 06:15:04 -070026class GrPaint;
27class GrTexture;
egdaniel170f90b2014-09-16 12:54:40 -070028
bsalomonac856c92015-08-27 06:30:17 -070029class GrPipelineBuilder : public SkNoncopyable {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000030public:
egdaniel8dd688b2015-01-22 10:16:09 -080031 GrPipelineBuilder();
32
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000033 /**
robertphillips55fdccc2016-06-06 06:16:20 -070034 * Initializes the GrPipelineBuilder based on a GrPaint and MSAA availability. Note
egdaniel8dd688b2015-01-22 10:16:09 -080035 * that GrPipelineBuilder encompasses more than GrPaint. Aspects of GrPipelineBuilder that have
36 * no GrPaint equivalents are set to default values with the exception of vertex attribute state
37 * which is unmodified by this function and clipping which will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000038 */
robertphillips40ff8ed2016-05-28 08:51:06 -070039 GrPipelineBuilder(const GrPaint&, bool targetHasUnifiedMultisampling);
joshualitt7b670db2015-07-09 13:25:02 -070040
41 virtual ~GrPipelineBuilder();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000042
bsalomon@google.com2401ae82012-01-17 21:03:05 +000043 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -080044 /// @name Fragment Processors
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000045 ///
bsalomon6be6f7c2015-02-26 13:05:21 -080046 /// GrFragmentProcessors are used to compute per-pixel color and per-pixel fractional coverage.
47 /// There are two chains of FPs, one for color and one for coverage. The first FP in each
48 /// chain gets the initial color/coverage from the GrPrimitiveProcessor. It computes an output
49 /// color/coverage which is fed to the next FP in the chain. The last color and coverage FPs
50 /// feed their output to the GrXferProcessor which controls blending.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000051 ////
52
bsalomonac856c92015-08-27 06:30:17 -070053 int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); }
54 int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); }
55 int numFragmentProcessors() const { return this->numColorFragmentProcessors() +
56 this->numCoverageFragmentProcessors(); }
egdaniel378092f2014-12-03 10:40:13 -080057
bsalomonac856c92015-08-27 06:30:17 -070058 const GrFragmentProcessor* getColorFragmentProcessor(int idx) const {
59 return fColorFragmentProcessors[idx];
60 }
61 const GrFragmentProcessor* getCoverageFragmentProcessor(int idx) const {
62 return fCoverageFragmentProcessors[idx];
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000063 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +000064
bsalomonac856c92015-08-27 06:30:17 -070065 const GrFragmentProcessor* addColorFragmentProcessor(const GrFragmentProcessor* processor) {
66 SkASSERT(processor);
67 fColorFragmentProcessors.push_back(SkRef(processor));
68 return processor;
69 }
70
71 const GrFragmentProcessor* addCoverageFragmentProcessor(const GrFragmentProcessor* processor) {
72 SkASSERT(processor);
73 fCoverageFragmentProcessors.push_back(SkRef(processor));
74 return processor;
bsalomon@google.comadc65362013-01-28 14:26:09 +000075 }
76
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000077 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +000078 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000079 */
joshualittb0a8a372014-09-23 09:50:21 -070080 void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
bsalomon4a339522015-10-06 08:40:50 -070081 this->addColorFragmentProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000082 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000083
joshualittb0a8a372014-09-23 09:50:21 -070084 void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
bsalomon4a339522015-10-06 08:40:50 -070085 this->addCoverageFragmentProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000086 }
87
joshualittb0a8a372014-09-23 09:50:21 -070088 void addColorTextureProcessor(GrTexture* texture,
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000089 const SkMatrix& matrix,
90 const GrTextureParams& params) {
bsalomon4a339522015-10-06 08:40:50 -070091 this->addColorFragmentProcessor(GrSimpleTextureEffect::Create(texture, matrix,
bsalomonac856c92015-08-27 06:30:17 -070092 params))->unref();
joshualittb0a8a372014-09-23 09:50:21 -070093 }
94
95 void addCoverageTextureProcessor(GrTexture* texture,
96 const SkMatrix& matrix,
97 const GrTextureParams& params) {
bsalomon4a339522015-10-06 08:40:50 -070098 this->addCoverageFragmentProcessor(GrSimpleTextureEffect::Create(texture, matrix,
99 params))->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000100 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000101
robertphillips@google.com972265d2012-06-13 18:49:30 +0000102 /**
bsalomon6be6f7c2015-02-26 13:05:21 -0800103 * When this object is destroyed it will remove any color/coverage FPs from the pipeline builder
bsalomon4a339522015-10-06 08:40:50 -0700104 * that were added after its constructor.
joshualitt5e6ba212015-07-13 07:35:05 -0700105 * This class can transiently modify its "const" GrPipelineBuilder object but will restore it
106 * when done - so it is notionally "const" correct.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000107 */
joshualitt4421a4c2015-07-13 09:36:41 -0700108 class AutoRestoreFragmentProcessorState : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000109 public:
halcanary9d524f22016-03-29 09:03:52 -0700110 AutoRestoreFragmentProcessorState()
halcanary96fcdcc2015-08-27 07:41:13 -0700111 : fPipelineBuilder(nullptr)
bsalomon9b536522014-09-05 09:18:51 -0700112 , fColorEffectCnt(0)
joshualittaf2533a2015-09-09 10:00:12 -0700113 , fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000114
joshualitt4421a4c2015-07-13 09:36:41 -0700115 AutoRestoreFragmentProcessorState(const GrPipelineBuilder& ds)
halcanary96fcdcc2015-08-27 07:41:13 -0700116 : fPipelineBuilder(nullptr)
bsalomon9b536522014-09-05 09:18:51 -0700117 , fColorEffectCnt(0)
joshualittaf2533a2015-09-09 10:00:12 -0700118 , fCoverageEffectCnt(0) {
joshualitt4421a4c2015-07-13 09:36:41 -0700119 this->set(&ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000120 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000121
halcanary96fcdcc2015-08-27 07:41:13 -0700122 ~AutoRestoreFragmentProcessorState() { this->set(nullptr); }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000123
joshualitt5e6ba212015-07-13 07:35:05 -0700124 void set(const GrPipelineBuilder* ds);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000125
egdaniel8dd688b2015-01-22 10:16:09 -0800126 bool isSet() const { return SkToBool(fPipelineBuilder); }
bsalomon8af05232014-06-03 06:34:58 -0700127
bsalomon4a339522015-10-06 08:40:50 -0700128 const GrFragmentProcessor* addCoverageFragmentProcessor(
129 const GrFragmentProcessor* processor) {
joshualitt5e6ba212015-07-13 07:35:05 -0700130 SkASSERT(this->isSet());
bsalomonac856c92015-08-27 06:30:17 -0700131 return fPipelineBuilder->addCoverageFragmentProcessor(processor);
joshualitt5e6ba212015-07-13 07:35:05 -0700132 }
133
robertphillips@google.com972265d2012-06-13 18:49:30 +0000134 private:
joshualitt5e6ba212015-07-13 07:35:05 -0700135 // notionally const (as marginalia)
egdaniel8dd688b2015-01-22 10:16:09 -0800136 GrPipelineBuilder* fPipelineBuilder;
joshualitt5e6ba212015-07-13 07:35:05 -0700137 int fColorEffectCnt;
138 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000139 };
140
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000141 /// @}
142
143 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000144 /// @name Blending
145 ////
146
egdaniel89af44a2014-09-26 06:15:04 -0700147 /**
bsalomon6be6f7c2015-02-26 13:05:21 -0800148 * Installs a GrXPFactory. This object controls how src color, fractional pixel coverage,
149 * and the dst color are blended.
150 */
151 const GrXPFactory* setXPFactory(const GrXPFactory* xpFactory) {
egdanielc4b72722015-11-23 13:20:41 -0800152 fXPFactory.reset(SkSafeRef(xpFactory));
bsalomon6be6f7c2015-02-26 13:05:21 -0800153 return xpFactory;
154 }
155
156 /**
bsalomon6be6f7c2015-02-26 13:05:21 -0800157 * Sets a GrXPFactory that disables color writes to the destination. This is useful when
158 * rendering to the stencil buffer.
159 */
160 void setDisableColorXPFactory() {
161 fXPFactory.reset(GrDisableColorXPFactory::Create());
162 }
163
164 const GrXPFactory* getXPFactory() const {
egdanielc4b72722015-11-23 13:20:41 -0800165 return fXPFactory;
bsalomon6be6f7c2015-02-26 13:05:21 -0800166 }
167
168 /**
bsalomon6a44c6a2015-05-26 09:49:05 -0700169 * Checks whether the xp will need destination in a texture to correctly blend.
bsalomon6be6f7c2015-02-26 13:05:21 -0800170 */
halcanary9d524f22016-03-29 09:03:52 -0700171 bool willXPNeedDstTexture(const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800172 const GrPipelineOptimizations& optimizations) const;
joshualittd27f73e2014-12-29 07:43:36 -0800173
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000174 /// @}
175
bsalomon6be6f7c2015-02-26 13:05:21 -0800176
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000177 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000178 /// @name Stencil
179 ////
180
robertphillips976f5f02016-06-03 10:59:20 -0700181 bool hasUserStencilSettings() const { return !fUserStencilSettings->isUnused(); }
cdalton93a379b2016-05-11 13:58:08 -0700182 const GrUserStencilSettings* getUserStencil() const { return fUserStencilSettings; }
egdaniel89af44a2014-09-26 06:15:04 -0700183
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000184 /**
cdalton93a379b2016-05-11 13:58:08 -0700185 * Sets the user stencil settings for the next draw.
186 * This class only stores pointers to stencil settings objects.
187 * The caller guarantees the pointer will remain valid until it
188 * changes or goes out of scope.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000189 * @param settings the stencil settings to use.
190 */
cdalton93a379b2016-05-11 13:58:08 -0700191 void setUserStencil(const GrUserStencilSettings* settings) { fUserStencilSettings = settings; }
192 void disableUserStencil() { fUserStencilSettings = &GrUserStencilSettings::kUnused; }
bsalomon6be6f7c2015-02-26 13:05:21 -0800193
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000194 /// @}
195
196 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000197 /// @name State Flags
198 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000199
egdaniel89af44a2014-09-26 06:15:04 -0700200 /**
201 * Flags that affect rendering. Controlled using enable/disableState(). All
202 * default to disabled.
203 */
bsalomond79c5492015-04-27 10:07:04 -0700204 enum Flags {
egdaniel89af44a2014-09-26 06:15:04 -0700205 /**
egdaniel89af44a2014-09-26 06:15:04 -0700206 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
207 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
208 * the 3D API.
209 */
bsalomonaca31fe2015-09-22 11:38:46 -0700210 kHWAntialias_Flag = 0x01,
egdaniel89af44a2014-09-26 06:15:04 -0700211
bsalomond79c5492015-04-27 10:07:04 -0700212 /**
213 * Modifies the vertex shader so that vertices will be positioned at pixel centers.
214 */
bsalomonaca31fe2015-09-22 11:38:46 -0700215 kSnapVerticesToPixelCenters_Flag = 0x02,
bsalomond79c5492015-04-27 10:07:04 -0700216
brianosman64d094d2016-03-25 06:01:59 -0700217 /**
218 * Suppress linear -> sRGB conversion when rendering to sRGB render targets.
219 */
220 kDisableOutputConversionToSRGB_Flag = 0x04,
221
brianosman898235c2016-04-06 07:38:23 -0700222 /**
223 * Allow sRGB -> linear conversion when reading from sRGB inputs.
224 */
225 kAllowSRGBInputs_Flag = 0x08,
226
227 kLast_Flag = kAllowSRGBInputs_Flag,
egdaniel89af44a2014-09-26 06:15:04 -0700228 };
229
bsalomond79c5492015-04-27 10:07:04 -0700230 bool isHWAntialias() const { return SkToBool(fFlags & kHWAntialias_Flag); }
231 bool snapVerticesToPixelCenters() const {
232 return SkToBool(fFlags & kSnapVerticesToPixelCenters_Flag); }
brianosman64d094d2016-03-25 06:01:59 -0700233 bool getDisableOutputConversionToSRGB() const {
234 return SkToBool(fFlags & kDisableOutputConversionToSRGB_Flag); }
brianosman898235c2016-04-06 07:38:23 -0700235 bool getAllowSRGBInputs() const {
236 return SkToBool(fFlags & kAllowSRGBInputs_Flag); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000237
238 /**
239 * Enable render state settings.
240 *
bsalomond79c5492015-04-27 10:07:04 -0700241 * @param flags bitfield of Flags specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000242 */
bsalomond79c5492015-04-27 10:07:04 -0700243 void enableState(uint32_t flags) { fFlags |= flags; }
halcanary9d524f22016-03-29 09:03:52 -0700244
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000245 /**
246 * Disable render state settings.
247 *
bsalomond79c5492015-04-27 10:07:04 -0700248 * @param flags bitfield of Flags specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000249 */
bsalomond79c5492015-04-27 10:07:04 -0700250 void disableState(uint32_t flags) { fFlags &= ~(flags); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000251
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000252 /**
bsalomond79c5492015-04-27 10:07:04 -0700253 * Enable or disable flags based on a boolean.
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000254 *
bsalomond79c5492015-04-27 10:07:04 -0700255 * @param flags bitfield of Flags to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000256 * @param enable if true enable stateBits, otherwise disable
257 */
bsalomond79c5492015-04-27 10:07:04 -0700258 void setState(uint32_t flags, bool enable) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000259 if (enable) {
bsalomond79c5492015-04-27 10:07:04 -0700260 this->enableState(flags);
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000261 } else {
bsalomond79c5492015-04-27 10:07:04 -0700262 this->disableState(flags);
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000263 }
264 }
265
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000266 /// @}
267
268 ///////////////////////////////////////////////////////////////////////////
269 /// @name Face Culling
270 ////
271
egdaniel89af44a2014-09-26 06:15:04 -0700272 enum DrawFace {
273 kInvalid_DrawFace = -1,
274
275 kBoth_DrawFace,
276 kCCW_DrawFace,
277 kCW_DrawFace,
278 };
279
280 /**
281 * Gets whether the target is drawing clockwise, counterclockwise,
282 * or both faces.
283 * @return the current draw face(s).
284 */
285 DrawFace getDrawFace() const { return fDrawFace; }
286
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000287 /**
288 * Controls whether clockwise, counterclockwise, or both faces are drawn.
289 * @param face the face(s) to draw.
290 */
291 void setDrawFace(DrawFace face) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000292 SkASSERT(kInvalid_DrawFace != face);
bsalomon2ed5ef82014-07-07 08:44:05 -0700293 fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000294 }
295
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000296 /// @}
297
298 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000299
ethannicholasff210322015-11-24 12:10:10 -0800300 bool usePLSDstRead(const GrDrawBatch* batch) const;
joshualitt44701df2015-02-23 14:44:57 -0800301
egdaniele36914c2015-02-13 09:00:33 -0800302private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000303 // Some of the auto restore objects assume that no effects are removed during their lifetime.
304 // This is used to assert that this condition holds.
joshualitt5e6ba212015-07-13 07:35:05 -0700305 SkDEBUGCODE(mutable int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000306
bsalomonac856c92015-08-27 06:30:17 -0700307 typedef SkSTArray<4, const GrFragmentProcessor*, true> FragmentProcessorArray;
egdaniel89af44a2014-09-26 06:15:04 -0700308
bsalomond79c5492015-04-27 10:07:04 -0700309 uint32_t fFlags;
cdalton93a379b2016-05-11 13:58:08 -0700310 const GrUserStencilSettings* fUserStencilSettings;
bsalomonae59b772014-11-19 08:23:49 -0800311 DrawFace fDrawFace;
joshualitt2fdeda02015-01-22 07:11:44 -0800312 mutable SkAutoTUnref<const GrXPFactory> fXPFactory;
bsalomonac856c92015-08-27 06:30:17 -0700313 FragmentProcessorArray fColorFragmentProcessors;
314 FragmentProcessorArray fCoverageFragmentProcessors;
egdaniel89af44a2014-09-26 06:15:04 -0700315
egdaniel8dd688b2015-01-22 10:16:09 -0800316 friend class GrPipeline;
ethannicholasff210322015-11-24 12:10:10 -0800317 friend class GrDrawTarget;
tomhudson@google.com93813632011-10-27 20:21:16 +0000318};
319
320#endif