1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef GrXferProcessor_DEFINED
9 #define GrXferProcessor_DEFINED
10
11 #include "include/gpu/GrTypes.h"
12 #include "src/gpu/GrBlend.h"
13 #include "src/gpu/GrNonAtomicRef.h"
14 #include "src/gpu/GrProcessor.h"
15 #include "src/gpu/GrProcessorAnalysis.h"
16 #include "src/gpu/GrSurfaceProxyView.h"
17 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
18
19 class GrGLSLXPFragmentBuilder;
20 class GrGLSLProgramDataManager;
21 struct GrShaderCaps;
22
23 /**
24 * Barriers for blending. When a shader reads the dst directly, an Xfer barrier is sometimes
25 * required after a pixel has been written, before it can be safely read again.
26 */
27 enum GrXferBarrierType {
28 kNone_GrXferBarrierType = 0, //<! No barrier is required
29 kTexture_GrXferBarrierType, //<! Required when a shader reads and renders to the same texture.
30 kBlend_GrXferBarrierType, //<! Required by certain blend extensions.
31 };
32 /** Should be able to treat kNone as false in boolean expressions */
33 static_assert(SkToBool(kNone_GrXferBarrierType) == false);
34
35 // Flag version of the above enum.
36 enum class GrXferBarrierFlags {
37 kNone = 0,
38 kTexture = 1 << 0,
39 kBlend = 1 << 1,
40 };
41
GR_MAKE_BITFIELD_CLASS_OPS(GrXferBarrierFlags)42 GR_MAKE_BITFIELD_CLASS_OPS(GrXferBarrierFlags)
43
44 /**
45 * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst
46 * color, and for applying any coverage. It does this by emitting fragment shader code and
47 * controlling the fixed-function blend state. When dual-source blending is available, it may also
48 * write a seconday fragment shader output color. GrXferProcessor has two modes of operation:
49 *
50 * Dst read: When allowed by the backend API, or when supplied a texture of the destination, the
51 * GrXferProcessor may read the destination color. While operating in this mode, the subclass only
52 * provides shader code that blends the src and dst colors, and the base class applies coverage.
53 *
54 * No dst read: When not performing a dst read, the subclass is given full control of the fixed-
55 * function blend state and/or secondary output, and is responsible to apply coverage on its own.
56 *
57 * A GrXferProcessor is never installed directly into our draw state, but instead is created from a
58 * GrXPFactory once we have finalized the state of our draw.
59 */
60 class GrXferProcessor : public GrProcessor, public GrNonAtomicRef<GrXferProcessor> {
61 public:
62 /**
63 * Every GrXferProcessor must be capable of creating a subclass of ProgramImpl. The ProgramImpl
64 * emits the shader code combines determines the fragment shader output(s) from the color and
65 * coverage FP outputs, is attached to the generated backend API pipeline/program, and used to
66 * extract uniform data from GrXferProcessor instances.
67 */
68 class ProgramImpl;
69
70 /**
71 * Adds a key on the skgpu::KeyBuilder calls onAddToKey(...) to get the specific subclass's key.
72 */
73 void addToKey(const GrShaderCaps&,
74 skgpu::KeyBuilder*,
75 const GrSurfaceOrigin* originIfDstTexture,
76 bool usesInputAttachmentForDstRead) const;
77
78 /** Returns a new instance of the appropriate *GL* implementation class
79 for the given GrXferProcessor; caller is responsible for deleting
80 the object. */
81 virtual std::unique_ptr<ProgramImpl> makeProgramImpl() const = 0;
82
83 /**
84 * Returns the barrier type, if any, that this XP will require. Note that the possibility
85 * that a kTexture type barrier is required is handled by the GrPipeline and need not be
86 * considered by subclass overrides of this function.
87 */
88 virtual GrXferBarrierType xferBarrierType(const GrCaps& caps) const {
89 return kNone_GrXferBarrierType;
90 }
91
92 struct BlendInfo {
93 SkDEBUGCODE(SkString dump() const;)
94
95 GrBlendEquation fEquation = kAdd_GrBlendEquation;
96 GrBlendCoeff fSrcBlend = kOne_GrBlendCoeff;
97 GrBlendCoeff fDstBlend = kZero_GrBlendCoeff;
98 SkPMColor4f fBlendConstant = SK_PMColor4fTRANSPARENT;
99 bool fWriteColor = true;
100 };
101
102 inline BlendInfo getBlendInfo() const {
103 BlendInfo blendInfo;
104 if (!this->willReadDstColor()) {
105 this->onGetBlendInfo(&blendInfo);
106 }
107 return blendInfo;
108 }
109
110 bool willReadDstColor() const { return fWillReadDstColor; }
111
112 /**
113 * Returns whether or not this xferProcossor will set a secondary output to be used with dual
114 * source blending.
115 */
116 bool hasSecondaryOutput() const;
117
118 bool isLCD() const { return fIsLCD; }
119
120 /** Returns true if this and other processor conservatively draw identically. It can only return
121 true when the two processor are of the same subclass (i.e. they return the same object from
122 from getFactory()).
123
124 A return value of true from isEqual() should not be used to test whether the processor would
125 generate the same shader code. To test for identical code generation use addToKey.
126 */
127
128 bool isEqual(const GrXferProcessor& that) const {
129 if (this->classID() != that.classID()) {
130 return false;
131 }
132 if (this->fWillReadDstColor != that.fWillReadDstColor) {
133 return false;
134 }
135 if (fIsLCD != that.fIsLCD) {
136 return false;
137 }
138 return this->onIsEqual(that);
139 }
140
141 protected:
142 GrXferProcessor(ClassID classID);
143 GrXferProcessor(ClassID classID, bool willReadDstColor, GrProcessorAnalysisCoverage);
144
145 private:
146 /**
147 * Adds a key on the skgpu::KeyBuilder that reflects any variety in the code that may be emitted
148 * by the xfer processor subclass.
149 */
150 virtual void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const = 0;
151
152 /**
153 * If we are not performing a dst read, returns whether the subclass will set a secondary
154 * output. When using dst reads, the base class controls the secondary output and this method
155 * will not be called.
156 */
157 virtual bool onHasSecondaryOutput() const { return false; }
158
159 /**
160 * If we are not performing a dst read, retrieves the fixed-function blend state required by the
161 * subclass. When using dst reads, the base class controls the fixed-function blend state and
162 * this method will not be called. The BlendInfo struct comes initialized to "no blending".
163 */
164 virtual void onGetBlendInfo(BlendInfo*) const {}
165
166 virtual bool onIsEqual(const GrXferProcessor&) const = 0;
167
168 bool fWillReadDstColor;
169 bool fIsLCD;
170
171 using INHERITED = GrProcessor;
172 };
173
174 /**
175 * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is
176 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the
177 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the
178 * draw information to create a GrXferProcessor (XP) which can implement the desired blending for
179 * the draw.
180 *
181 * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it
182 * creates will have. For example, can it create an XP that supports RGB coverage or will the XP
183 * blend with the destination color.
184 *
185 * GrXPFactories are intended to be static immutable objects. We pass them around as raw pointers
186 * and expect the pointers to always be valid and for the factories to be reusable and thread safe.
187 * Equality is tested for using pointer comparison. GrXPFactory destructors must be no-ops.
188 */
189
190 // In order to construct GrXPFactory subclass instances as constexpr the subclass, and therefore
191 // GrXPFactory, must be a literal type. One requirement is having a trivial destructor. This is ok
192 // since these objects have no need for destructors. However, GCC and clang throw a warning when a
193 // class has virtual functions and a non-virtual destructor. We suppress that warning here and
194 // for the subclasses.
195 #if defined(__GNUC__)
196 #pragma GCC diagnostic push
197 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
198 #endif
199 #if defined(__clang__)
200 #pragma clang diagnostic push
201 #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
202 #endif
203 class GrXPFactory {
204 public:
205 enum class AnalysisProperties : unsigned {
206 kNone = 0x0,
207 /**
208 * The fragment shader will require the destination color.
209 */
210 kReadsDstInShader = 0x1,
211 /**
212 * The op may apply coverage as alpha and still blend correctly.
213 */
214 kCompatibleWithCoverageAsAlpha = 0x2,
215 /**
216 * The color input to the GrXferProcessor will be ignored.
217 */
218 kIgnoresInputColor = 0x4,
219 /**
220 * The destination color will be provided to the fragment processor using a texture. This is
221 * additional information about the implementation of kReadsDstInShader.
222 */
223 kRequiresDstTexture = 0x10,
224 /**
225 * If set, each pixel can only be touched once during a draw (e.g., because we have a dst
226 * texture or because we need an xfer barrier).
227 */
228 kRequiresNonOverlappingDraws = 0x20,
229 /**
230 * If set the draw will use fixed function non coherent advanced blends.
231 */
232 kUsesNonCoherentHWBlending = 0x40,
233 /**
234 * If set, the existing dst value has no effect on the final output.
235 */
236 kUnaffectedByDstValue = 0x80,
237 };
238 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AnalysisProperties);
239
240 static sk_sp<const GrXferProcessor> MakeXferProcessor(const GrXPFactory*,
241 const GrProcessorAnalysisColor&,
242 GrProcessorAnalysisCoverage,
243 const GrCaps& caps,
244 GrClampType);
245
246 static AnalysisProperties GetAnalysisProperties(const GrXPFactory*,
247 const GrProcessorAnalysisColor&,
248 const GrProcessorAnalysisCoverage&,
249 const GrCaps&,
250 GrClampType);
251
252 protected:
GrXPFactory()253 constexpr GrXPFactory() {}
254
255 private:
256 virtual sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&,
257 GrProcessorAnalysisCoverage,
258 const GrCaps&,
259 GrClampType) const = 0;
260
261 /**
262 * Subclass analysis implementation. This should not return kNeedsDstInTexture as that will be
263 * inferred by the base class based on kReadsDstInShader and the caps.
264 */
265 virtual AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&,
266 const GrProcessorAnalysisCoverage&,
267 const GrCaps&,
268 GrClampType) const = 0;
269 };
270 #if defined(__GNUC__)
271 #pragma GCC diagnostic pop
272 #endif
273 #if defined(__clang__)
274 #pragma clang diagnostic pop
275 #endif
276
GR_MAKE_BITFIELD_CLASS_OPS(GrXPFactory::AnalysisProperties)277 GR_MAKE_BITFIELD_CLASS_OPS(GrXPFactory::AnalysisProperties)
278
279 //////////////////////////////////////////////////////////////////////////////
280
281 class GrXferProcessor::ProgramImpl {
282 public:
283 virtual ~ProgramImpl() = default;
284
285 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
286
287 struct EmitArgs {
288 EmitArgs(GrGLSLXPFragmentBuilder* fragBuilder,
289 GrGLSLUniformHandler* uniformHandler,
290 const GrShaderCaps* caps,
291 const GrXferProcessor& xp,
292 const char* inputColor,
293 const char* inputCoverage,
294 const char* outputPrimary,
295 const char* outputSecondary,
296 const SamplerHandle dstTextureSamplerHandle,
297 GrSurfaceOrigin dstTextureOrigin,
298 const skgpu::Swizzle& writeSwizzle)
299 : fXPFragBuilder(fragBuilder)
300 , fUniformHandler(uniformHandler)
301 , fShaderCaps(caps)
302 , fXP(xp)
303 , fInputColor(inputColor ? inputColor : "half4(1.0)")
304 , fInputCoverage(inputCoverage)
305 , fOutputPrimary(outputPrimary)
306 , fOutputSecondary(outputSecondary)
307 , fDstTextureSamplerHandle(dstTextureSamplerHandle)
308 , fDstTextureOrigin(dstTextureOrigin)
309 , fWriteSwizzle(writeSwizzle) {}
310 GrGLSLXPFragmentBuilder* fXPFragBuilder;
311 GrGLSLUniformHandler* fUniformHandler;
312 const GrShaderCaps* fShaderCaps;
313 const GrXferProcessor& fXP;
314 const char* fInputColor;
315 const char* fInputCoverage;
316 const char* fOutputPrimary;
317 const char* fOutputSecondary;
318 const SamplerHandle fDstTextureSamplerHandle;
319 GrSurfaceOrigin fDstTextureOrigin;
320 skgpu::Swizzle fWriteSwizzle;
321 };
322 /**
323 * This is similar to emitCode() in the base class, except it takes a full shader builder.
324 * This allows the effect subclass to emit vertex code.
325 */
326 void emitCode(const EmitArgs&);
327
328 /** A ProgramImpl instance can be reused with any GrXferProcessor that produces the same key.
329 This function reads data from a GrXferProcessor and uploads any uniform variables required
330 by the shaders created in emitCode(). The GrXferProcessor parameter is guaranteed to be of
331 the same type that created this ProgramImpl and to have an identical processor key as the
332 one that created this ProgramImpl. This function calls onSetData on the subclass of
333 ProgramImpl.
334 */
335 void setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp);
336
337 protected:
338 ProgramImpl() = default;
339
340 static void DefaultCoverageModulation(GrGLSLXPFragmentBuilder* fragBuilder,
341 const char* srcCoverage,
342 const char* dstColor,
343 const char* outColor,
344 const char* outColorSecondary,
345 const GrXferProcessor& proc);
346
347 private:
348 /**
349 * Called by emitCode() when the XP will not be performing a dst read. This method is
350 * responsible for both blending and coverage. A subclass only needs to implement this method if
351 * it can construct a GrXferProcessor that will not read the dst color.
352 */
353 virtual void emitOutputsForBlendState(const EmitArgs&) {
354 SK_ABORT("emitOutputsForBlendState not implemented.");
355 }
356
357 /**
358 * Called by emitCode() when the XP will perform a dst read. This method only needs to supply
359 * the blending logic. The base class applies coverage. A subclass only needs to implement this
360 * method if it can construct a GrXferProcessor that reads the dst color.
361 */
362 virtual void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder*,
363 GrGLSLUniformHandler*,
364 const char* srcColor,
365 const char* srcCoverage,
366 const char* dstColor,
367 const char* outColor,
368 const char* outColorSecondary,
369 const GrXferProcessor&) {
370 SK_ABORT("emitBlendCodeForDstRead not implemented.");
371 }
372
373 virtual void emitWriteSwizzle(GrGLSLXPFragmentBuilder*,
374 const skgpu::Swizzle&,
375 const char* outColor,
376 const char* outColorSecondary) const;
377
378 virtual void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) {}
379 };
380
381 #endif
382