blob: 0c33eb344f8ce75442667817f7d25b67e0413c68 [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"
bungeman06ca8ec2016-06-09 08:01:03 -070019#include "SkRefCnt.h"
egdaniel87509242014-12-17 13:37:13 -080020#include "effects/GrCoverageSetOpXP.h"
egdaniel080e6732014-12-22 07:35:52 -080021#include "effects/GrDisableColorXP.h"
egdaniel95131432014-12-09 11:15:43 -080022#include "effects/GrPorterDuffXferProcessor.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000023#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000024
bsalomonabd30f52015-08-13 13:34:48 -070025class GrDrawBatch;
bsalomon4b91f762015-05-19 09:29:46 -070026class GrCaps;
egdaniel89af44a2014-09-26 06:15:04 -070027class GrPaint;
28class GrTexture;
egdaniel170f90b2014-09-16 12:54:40 -070029
bsalomonac856c92015-08-27 06:30:17 -070030class GrPipelineBuilder : public SkNoncopyable {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000031public:
egdaniel8dd688b2015-01-22 10:16:09 -080032 GrPipelineBuilder();
33
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000034 /**
robertphillips55fdccc2016-06-06 06:16:20 -070035 * Initializes the GrPipelineBuilder based on a GrPaint and MSAA availability. Note
egdaniel8dd688b2015-01-22 10:16:09 -080036 * that GrPipelineBuilder encompasses more than GrPaint. Aspects of GrPipelineBuilder that have
37 * no GrPaint equivalents are set to default values with the exception of vertex attribute state
38 * which is unmodified by this function and clipping which will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000039 */
csmartdaltonecbc12b2016-06-08 10:08:43 -070040 GrPipelineBuilder(const GrPaint&, bool useHWAA = false);
joshualitt7b670db2015-07-09 13:25:02 -070041
42 virtual ~GrPipelineBuilder();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000043
bsalomon@google.com2401ae82012-01-17 21:03:05 +000044 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -080045 /// @name Fragment Processors
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000046 ///
bsalomon6be6f7c2015-02-26 13:05:21 -080047 /// GrFragmentProcessors are used to compute per-pixel color and per-pixel fractional coverage.
48 /// There are two chains of FPs, one for color and one for coverage. The first FP in each
49 /// chain gets the initial color/coverage from the GrPrimitiveProcessor. It computes an output
50 /// color/coverage which is fed to the next FP in the chain. The last color and coverage FPs
51 /// feed their output to the GrXferProcessor which controls blending.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000052 ////
53
bsalomonac856c92015-08-27 06:30:17 -070054 int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); }
55 int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); }
56 int numFragmentProcessors() const { return this->numColorFragmentProcessors() +
57 this->numCoverageFragmentProcessors(); }
egdaniel378092f2014-12-03 10:40:13 -080058
bsalomonac856c92015-08-27 06:30:17 -070059 const GrFragmentProcessor* getColorFragmentProcessor(int idx) const {
bungeman06ca8ec2016-06-09 08:01:03 -070060 return fColorFragmentProcessors[idx].get();
bsalomonac856c92015-08-27 06:30:17 -070061 }
62 const GrFragmentProcessor* getCoverageFragmentProcessor(int idx) const {
bungeman06ca8ec2016-06-09 08:01:03 -070063 return fCoverageFragmentProcessors[idx].get();
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000064 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +000065
bungeman06ca8ec2016-06-09 08:01:03 -070066 void addColorFragmentProcessor(sk_sp<GrFragmentProcessor> processor) {
bsalomonac856c92015-08-27 06:30:17 -070067 SkASSERT(processor);
bungeman06ca8ec2016-06-09 08:01:03 -070068 fColorFragmentProcessors.push_back(std::move(processor));
bsalomonac856c92015-08-27 06:30:17 -070069 }
70
bungeman06ca8ec2016-06-09 08:01:03 -070071 void addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor> processor) {
bsalomonac856c92015-08-27 06:30:17 -070072 SkASSERT(processor);
bungeman06ca8ec2016-06-09 08:01:03 -070073 fCoverageFragmentProcessors.push_back(std::move(processor));
bsalomon@google.comadc65362013-01-28 14:26:09 +000074 }
75
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000076 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +000077 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000078 */
joshualittb0a8a372014-09-23 09:50:21 -070079 void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
brianosman54f30c12016-07-18 10:53:52 -070080 this->addColorFragmentProcessor(GrSimpleTextureEffect::Make(texture, nullptr, matrix));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000081 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000082
joshualittb0a8a372014-09-23 09:50:21 -070083 void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
brianosman54f30c12016-07-18 10:53:52 -070084 this->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(texture, nullptr, matrix));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000085 }
86
joshualittb0a8a372014-09-23 09:50:21 -070087 void addColorTextureProcessor(GrTexture* texture,
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000088 const SkMatrix& matrix,
89 const GrTextureParams& params) {
brianosman54f30c12016-07-18 10:53:52 -070090 this->addColorFragmentProcessor(GrSimpleTextureEffect::Make(texture, nullptr, matrix,
91 params));
joshualittb0a8a372014-09-23 09:50:21 -070092 }
93
94 void addCoverageTextureProcessor(GrTexture* texture,
95 const SkMatrix& matrix,
96 const GrTextureParams& params) {
brianosman54f30c12016-07-18 10:53:52 -070097 this->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(texture, nullptr, matrix,
98 params));
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000099 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000100
robertphillips@google.com972265d2012-06-13 18:49:30 +0000101 /**
bsalomon6be6f7c2015-02-26 13:05:21 -0800102 * When this object is destroyed it will remove any color/coverage FPs from the pipeline builder
bsalomon4a339522015-10-06 08:40:50 -0700103 * that were added after its constructor.
joshualitt5e6ba212015-07-13 07:35:05 -0700104 * This class can transiently modify its "const" GrPipelineBuilder object but will restore it
105 * when done - so it is notionally "const" correct.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000106 */
joshualitt4421a4c2015-07-13 09:36:41 -0700107 class AutoRestoreFragmentProcessorState : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000108 public:
halcanary9d524f22016-03-29 09:03:52 -0700109 AutoRestoreFragmentProcessorState()
halcanary96fcdcc2015-08-27 07:41:13 -0700110 : fPipelineBuilder(nullptr)
bsalomon9b536522014-09-05 09:18:51 -0700111 , fColorEffectCnt(0)
joshualittaf2533a2015-09-09 10:00:12 -0700112 , fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000113
joshualitt4421a4c2015-07-13 09:36:41 -0700114 AutoRestoreFragmentProcessorState(const GrPipelineBuilder& ds)
halcanary96fcdcc2015-08-27 07:41:13 -0700115 : fPipelineBuilder(nullptr)
bsalomon9b536522014-09-05 09:18:51 -0700116 , fColorEffectCnt(0)
joshualittaf2533a2015-09-09 10:00:12 -0700117 , fCoverageEffectCnt(0) {
joshualitt4421a4c2015-07-13 09:36:41 -0700118 this->set(&ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000119 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000120
halcanary96fcdcc2015-08-27 07:41:13 -0700121 ~AutoRestoreFragmentProcessorState() { this->set(nullptr); }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000122
joshualitt5e6ba212015-07-13 07:35:05 -0700123 void set(const GrPipelineBuilder* ds);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000124
egdaniel8dd688b2015-01-22 10:16:09 -0800125 bool isSet() const { return SkToBool(fPipelineBuilder); }
bsalomon8af05232014-06-03 06:34:58 -0700126
bungeman06ca8ec2016-06-09 08:01:03 -0700127 void addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor> processor) {
joshualitt5e6ba212015-07-13 07:35:05 -0700128 SkASSERT(this->isSet());
bungeman06ca8ec2016-06-09 08:01:03 -0700129 return fPipelineBuilder->addCoverageFragmentProcessor(std::move(processor));
joshualitt5e6ba212015-07-13 07:35:05 -0700130 }
131
robertphillips@google.com972265d2012-06-13 18:49:30 +0000132 private:
joshualitt5e6ba212015-07-13 07:35:05 -0700133 // notionally const (as marginalia)
egdaniel8dd688b2015-01-22 10:16:09 -0800134 GrPipelineBuilder* fPipelineBuilder;
joshualitt5e6ba212015-07-13 07:35:05 -0700135 int fColorEffectCnt;
136 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000137 };
138
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000139 /// @}
140
141 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000142 /// @name Blending
143 ////
144
egdaniel89af44a2014-09-26 06:15:04 -0700145 /**
bsalomon6be6f7c2015-02-26 13:05:21 -0800146 * Installs a GrXPFactory. This object controls how src color, fractional pixel coverage,
147 * and the dst color are blended.
148 */
bungeman06ca8ec2016-06-09 08:01:03 -0700149 void setXPFactory(sk_sp<GrXPFactory> xpFactory) {
150 fXPFactory = std::move(xpFactory);
bsalomon6be6f7c2015-02-26 13:05:21 -0800151 }
152
153 /**
bsalomon6be6f7c2015-02-26 13:05:21 -0800154 * Sets a GrXPFactory that disables color writes to the destination. This is useful when
155 * rendering to the stencil buffer.
156 */
157 void setDisableColorXPFactory() {
bungeman06ca8ec2016-06-09 08:01:03 -0700158 fXPFactory = GrDisableColorXPFactory::Make();
bsalomon6be6f7c2015-02-26 13:05:21 -0800159 }
160
161 const GrXPFactory* getXPFactory() const {
bungeman06ca8ec2016-06-09 08:01:03 -0700162 return fXPFactory.get();
bsalomon6be6f7c2015-02-26 13:05:21 -0800163 }
164
165 /**
bsalomon6a44c6a2015-05-26 09:49:05 -0700166 * Checks whether the xp will need destination in a texture to correctly blend.
bsalomon6be6f7c2015-02-26 13:05:21 -0800167 */
halcanary9d524f22016-03-29 09:03:52 -0700168 bool willXPNeedDstTexture(const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800169 const GrPipelineOptimizations& optimizations) const;
joshualittd27f73e2014-12-29 07:43:36 -0800170
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000171 /// @}
172
bsalomon6be6f7c2015-02-26 13:05:21 -0800173
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000174 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000175 /// @name Stencil
176 ////
177
robertphillips976f5f02016-06-03 10:59:20 -0700178 bool hasUserStencilSettings() const { return !fUserStencilSettings->isUnused(); }
cdalton93a379b2016-05-11 13:58:08 -0700179 const GrUserStencilSettings* getUserStencil() const { return fUserStencilSettings; }
egdaniel89af44a2014-09-26 06:15:04 -0700180
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000181 /**
cdalton93a379b2016-05-11 13:58:08 -0700182 * Sets the user stencil settings for the next draw.
183 * This class only stores pointers to stencil settings objects.
184 * The caller guarantees the pointer will remain valid until it
185 * changes or goes out of scope.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000186 * @param settings the stencil settings to use.
187 */
cdalton93a379b2016-05-11 13:58:08 -0700188 void setUserStencil(const GrUserStencilSettings* settings) { fUserStencilSettings = settings; }
189 void disableUserStencil() { fUserStencilSettings = &GrUserStencilSettings::kUnused; }
bsalomon6be6f7c2015-02-26 13:05:21 -0800190
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000191 /// @}
192
193 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000194 /// @name State Flags
195 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000196
egdaniel89af44a2014-09-26 06:15:04 -0700197 /**
198 * Flags that affect rendering. Controlled using enable/disableState(). All
199 * default to disabled.
200 */
bsalomond79c5492015-04-27 10:07:04 -0700201 enum Flags {
egdaniel89af44a2014-09-26 06:15:04 -0700202 /**
egdaniel89af44a2014-09-26 06:15:04 -0700203 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
204 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
205 * the 3D API.
206 */
bsalomonaca31fe2015-09-22 11:38:46 -0700207 kHWAntialias_Flag = 0x01,
egdaniel89af44a2014-09-26 06:15:04 -0700208
bsalomond79c5492015-04-27 10:07:04 -0700209 /**
210 * Modifies the vertex shader so that vertices will be positioned at pixel centers.
211 */
bsalomonaca31fe2015-09-22 11:38:46 -0700212 kSnapVerticesToPixelCenters_Flag = 0x02,
bsalomond79c5492015-04-27 10:07:04 -0700213
brianosman64d094d2016-03-25 06:01:59 -0700214 /**
215 * Suppress linear -> sRGB conversion when rendering to sRGB render targets.
216 */
217 kDisableOutputConversionToSRGB_Flag = 0x04,
218
brianosman898235c2016-04-06 07:38:23 -0700219 /**
220 * Allow sRGB -> linear conversion when reading from sRGB inputs.
221 */
222 kAllowSRGBInputs_Flag = 0x08,
223
dvonbeck9b03e7b2016-08-01 11:01:56 -0700224 /**
225 * Signals that one or more FPs need access to the distance vector field to the nearest
226 * edge
227 */
228 kUsesDistanceVectorField_Flag = 0x10,
229
230 kLast_Flag = kUsesDistanceVectorField_Flag,
egdaniel89af44a2014-09-26 06:15:04 -0700231 };
232
bsalomond79c5492015-04-27 10:07:04 -0700233 bool isHWAntialias() const { return SkToBool(fFlags & kHWAntialias_Flag); }
234 bool snapVerticesToPixelCenters() const {
235 return SkToBool(fFlags & kSnapVerticesToPixelCenters_Flag); }
brianosman64d094d2016-03-25 06:01:59 -0700236 bool getDisableOutputConversionToSRGB() const {
237 return SkToBool(fFlags & kDisableOutputConversionToSRGB_Flag); }
brianosman898235c2016-04-06 07:38:23 -0700238 bool getAllowSRGBInputs() const {
239 return SkToBool(fFlags & kAllowSRGBInputs_Flag); }
dvonbeck9b03e7b2016-08-01 11:01:56 -0700240 bool getUsesDistanceVectorField() const {
241 return SkToBool(fFlags & kUsesDistanceVectorField_Flag); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000242
243 /**
244 * Enable render state settings.
245 *
bsalomond79c5492015-04-27 10:07:04 -0700246 * @param flags bitfield of Flags specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000247 */
bsalomond79c5492015-04-27 10:07:04 -0700248 void enableState(uint32_t flags) { fFlags |= flags; }
halcanary9d524f22016-03-29 09:03:52 -0700249
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000250 /**
251 * Disable render state settings.
252 *
bsalomond79c5492015-04-27 10:07:04 -0700253 * @param flags bitfield of Flags specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000254 */
bsalomond79c5492015-04-27 10:07:04 -0700255 void disableState(uint32_t flags) { fFlags &= ~(flags); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000256
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000257 /**
bsalomond79c5492015-04-27 10:07:04 -0700258 * Enable or disable flags based on a boolean.
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000259 *
bsalomond79c5492015-04-27 10:07:04 -0700260 * @param flags bitfield of Flags to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000261 * @param enable if true enable stateBits, otherwise disable
262 */
bsalomond79c5492015-04-27 10:07:04 -0700263 void setState(uint32_t flags, bool enable) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000264 if (enable) {
bsalomond79c5492015-04-27 10:07:04 -0700265 this->enableState(flags);
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000266 } else {
bsalomond79c5492015-04-27 10:07:04 -0700267 this->disableState(flags);
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000268 }
269 }
270
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000271 /// @}
272
273 ///////////////////////////////////////////////////////////////////////////
274 /// @name Face Culling
275 ////
276
egdaniel89af44a2014-09-26 06:15:04 -0700277 /**
278 * Gets whether the target is drawing clockwise, counterclockwise,
279 * or both faces.
280 * @return the current draw face(s).
281 */
robertphillips5fa7f302016-07-21 09:21:04 -0700282 GrDrawFace getDrawFace() const { return fDrawFace; }
egdaniel89af44a2014-09-26 06:15:04 -0700283
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000284 /**
285 * Controls whether clockwise, counterclockwise, or both faces are drawn.
286 * @param face the face(s) to draw.
287 */
robertphillips5fa7f302016-07-21 09:21:04 -0700288 void setDrawFace(GrDrawFace face) {
289 SkASSERT(GrDrawFace::kInvalid != face);
bsalomon2ed5ef82014-07-07 08:44:05 -0700290 fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000291 }
292
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000293 /// @}
294
295 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000296
ethannicholasff210322015-11-24 12:10:10 -0800297 bool usePLSDstRead(const GrDrawBatch* batch) const;
joshualitt44701df2015-02-23 14:44:57 -0800298
egdaniele36914c2015-02-13 09:00:33 -0800299private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000300 // Some of the auto restore objects assume that no effects are removed during their lifetime.
301 // This is used to assert that this condition holds.
joshualitt5e6ba212015-07-13 07:35:05 -0700302 SkDEBUGCODE(mutable int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000303
bungeman06ca8ec2016-06-09 08:01:03 -0700304 typedef SkSTArray<4, sk_sp<GrFragmentProcessor>> FragmentProcessorArray;
egdaniel89af44a2014-09-26 06:15:04 -0700305
bsalomond79c5492015-04-27 10:07:04 -0700306 uint32_t fFlags;
cdalton93a379b2016-05-11 13:58:08 -0700307 const GrUserStencilSettings* fUserStencilSettings;
robertphillips5fa7f302016-07-21 09:21:04 -0700308 GrDrawFace fDrawFace;
bungeman06ca8ec2016-06-09 08:01:03 -0700309 mutable sk_sp<GrXPFactory> fXPFactory;
bsalomonac856c92015-08-27 06:30:17 -0700310 FragmentProcessorArray fColorFragmentProcessors;
311 FragmentProcessorArray fCoverageFragmentProcessors;
egdaniel89af44a2014-09-26 06:15:04 -0700312
egdaniel8dd688b2015-01-22 10:16:09 -0800313 friend class GrPipeline;
ethannicholasff210322015-11-24 12:10:10 -0800314 friend class GrDrawTarget;
tomhudson@google.com93813632011-10-27 20:21:16 +0000315};
316
317#endif