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/core/SkRefCnt.h"
12 #include "include/private/base/SkAssert.h"
13 #include "include/private/base/SkMacros.h"
14 #include "include/private/base/SkTo.h"
15 #include "src/gpu/Blend.h"
16 #include "src/gpu/Swizzle.h"
17 #include "src/gpu/ganesh/GrCaps.h"
18 #include "src/gpu/ganesh/GrNonAtomicRef.h"
19 #include "src/gpu/ganesh/GrProcessor.h"
20 #include "src/gpu/ganesh/GrProcessorAnalysis.h"
21 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h"
22
23 #include <memory>
24
25 class GrGLSLProgramDataManager;
26 class GrGLSLXPFragmentBuilder;
27 enum GrSurfaceOrigin : int;
28 enum class GrClampType;
29 enum class SkBlendMode;
30 namespace skgpu { class KeyBuilder; }
31 struct GrShaderCaps;
32
33 /**
34 * Barriers for blending. When a shader reads the dst directly, an Xfer barrier is sometimes
35 * required after a pixel has been written, before it can be safely read again.
36 */
37 enum GrXferBarrierType {
38 kNone_GrXferBarrierType = 0, //<! No barrier is required
39 kTexture_GrXferBarrierType, //<! Required when a shader reads and renders to the same texture.
40 kBlend_GrXferBarrierType, //<! Required by certain blend extensions.
41 };
42 /** Should be able to treat kNone as false in boolean expressions */
43 static_assert(SkToBool(kNone_GrXferBarrierType) == false);
44
45 // Flag version of the above enum.
46 enum class GrXferBarrierFlags {
47 kNone = 0,
48 kTexture = 1 << 0,
49 kBlend = 1 << 1,
50 };
51
SK_MAKE_BITFIELD_CLASS_OPS(GrXferBarrierFlags)52 SK_MAKE_BITFIELD_CLASS_OPS(GrXferBarrierFlags)
53
54 /**
55 * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst
56 * color, and for applying any coverage. It does this by emitting fragment shader code and
57 * controlling the fixed-function blend state. When dual-source blending is available, it may also
58 * write a secondary fragment shader output color. GrXferProcessor has two modes of operation:
59 *
60 * Dst read: When allowed by the backend API, or when supplied a texture of the destination, the
61 * GrXferProcessor may read the destination color. While operating in this mode, the subclass only
62 * provides shader code that blends the src and dst colors, and the base class applies coverage.
63 *
64 * No dst read: When not performing a dst read, the subclass is given full control of the fixed-
65 * function blend state and/or secondary output, and is responsible to apply coverage on its own.
66 *
67 * A GrXferProcessor is never installed directly into our draw state, but instead is created from a
68 * GrXPFactory once we have finalized the state of our draw.
69 */
70 class GrXferProcessor : public GrProcessor, public GrNonAtomicRef<GrXferProcessor> {
71 public:
72 /**
73 * Every GrXferProcessor must be capable of creating a subclass of ProgramImpl. The ProgramImpl
74 * emits the shader code combines determines the fragment shader output(s) from the color and
75 * coverage FP outputs, is attached to the generated backend API pipeline/program, and used to
76 * extract uniform data from GrXferProcessor instances.
77 */
78 class ProgramImpl;
79
80 /**
81 * Adds a key on the skgpu::KeyBuilder calls onAddToKey(...) to get the specific subclass's key.
82 */
83 void addToKey(const GrShaderCaps&,
84 skgpu::KeyBuilder*,
85 const GrSurfaceOrigin* originIfDstTexture,
86 bool usesInputAttachmentForDstRead) const;
87
88 /** Returns a new instance of the appropriate *GL* implementation class
89 for the given GrXferProcessor; caller is responsible for deleting
90 the object. */
91 virtual std::unique_ptr<ProgramImpl> makeProgramImpl() const = 0;
92
93 /**
94 * Returns the barrier type, if any, that this XP will require. Note that the possibility
95 * that a kTexture type barrier is required is handled by the GrPipeline and need not be
96 * considered by subclass overrides of this function.
97 */
98 virtual GrXferBarrierType xferBarrierType(const GrCaps& caps) const {
99 return kNone_GrXferBarrierType;
100 }
101
102 inline skgpu::BlendInfo getBlendInfo() const {
103 skgpu::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(skgpu::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 SK_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 static const GrXPFactory* FromBlendMode(SkBlendMode);
253
254 protected:
GrXPFactory()255 constexpr GrXPFactory() {}
256
257 private:
258 virtual sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&,
259 GrProcessorAnalysisCoverage,
260 const GrCaps&,
261 GrClampType) const = 0;
262
263 /**
264 * Subclass analysis implementation. This should not return kNeedsDstInTexture as that will be
265 * inferred by the base class based on kReadsDstInShader and the caps.
266 */
267 virtual AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&,
268 const GrProcessorAnalysisCoverage&,
269 const GrCaps&,
270 GrClampType) const = 0;
271 };
272 #if defined(__GNUC__)
273 #pragma GCC diagnostic pop
274 #endif
275 #if defined(__clang__)
276 #pragma clang diagnostic pop
277 #endif
278
SK_MAKE_BITFIELD_CLASS_OPS(GrXPFactory::AnalysisProperties)279 SK_MAKE_BITFIELD_CLASS_OPS(GrXPFactory::AnalysisProperties)
280
281 //////////////////////////////////////////////////////////////////////////////
282
283 class GrXferProcessor::ProgramImpl {
284 public:
285 virtual ~ProgramImpl() = default;
286
287 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
288
289 struct EmitArgs {
290 EmitArgs(GrGLSLXPFragmentBuilder* fragBuilder,
291 GrGLSLUniformHandler* uniformHandler,
292 const GrShaderCaps* caps,
293 const GrXferProcessor& xp,
294 const char* inputColor,
295 const char* inputCoverage,
296 const char* outputPrimary,
297 const char* outputSecondary,
298 const SamplerHandle dstTextureSamplerHandle,
299 GrSurfaceOrigin dstTextureOrigin,
300 const skgpu::Swizzle& writeSwizzle)
301 : fXPFragBuilder(fragBuilder)
302 , fUniformHandler(uniformHandler)
303 , fShaderCaps(caps)
304 , fXP(xp)
305 , fInputColor(inputColor ? inputColor : "half4(1.0)")
306 , fInputCoverage(inputCoverage)
307 , fOutputPrimary(outputPrimary)
308 , fOutputSecondary(outputSecondary)
309 , fDstTextureSamplerHandle(dstTextureSamplerHandle)
310 , fDstTextureOrigin(dstTextureOrigin)
311 , fWriteSwizzle(writeSwizzle) {}
312 GrGLSLXPFragmentBuilder* fXPFragBuilder;
313 GrGLSLUniformHandler* fUniformHandler;
314 const GrShaderCaps* fShaderCaps;
315 const GrXferProcessor& fXP;
316 const char* fInputColor;
317 const char* fInputCoverage;
318 const char* fOutputPrimary;
319 const char* fOutputSecondary;
320 const SamplerHandle fDstTextureSamplerHandle;
321 GrSurfaceOrigin fDstTextureOrigin;
322 skgpu::Swizzle fWriteSwizzle;
323 };
324 /**
325 * This is similar to emitCode() in the base class, except it takes a full shader builder.
326 * This allows the effect subclass to emit vertex code.
327 */
328 void emitCode(const EmitArgs&);
329
330 /** A ProgramImpl instance can be reused with any GrXferProcessor that produces the same key.
331 This function reads data from a GrXferProcessor and uploads any uniform variables required
332 by the shaders created in emitCode(). The GrXferProcessor parameter is guaranteed to be of
333 the same type that created this ProgramImpl and to have an identical processor key as the
334 one that created this ProgramImpl. This function calls onSetData on the subclass of
335 ProgramImpl.
336 */
337 void setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp);
338
339 protected:
340 ProgramImpl() = default;
341
342 static void DefaultCoverageModulation(GrGLSLXPFragmentBuilder* fragBuilder,
343 const char* srcCoverage,
344 const char* dstColor,
345 const char* outColor,
346 const char* outColorSecondary,
347 const GrXferProcessor& proc);
348
349 private:
350 /**
351 * Called by emitCode() when the XP will not be performing a dst read. This method is
352 * responsible for both blending and coverage. A subclass only needs to implement this method if
353 * it can construct a GrXferProcessor that will not read the dst color.
354 */
355 virtual void emitOutputsForBlendState(const EmitArgs&) {
356 SK_ABORT("emitOutputsForBlendState not implemented.");
357 }
358
359 /**
360 * Called by emitCode() when the XP will perform a dst read. This method only needs to supply
361 * the blending logic. The base class applies coverage. A subclass only needs to implement this
362 * method if it can construct a GrXferProcessor that reads the dst color.
363 */
364 virtual void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder*,
365 GrGLSLUniformHandler*,
366 const char* srcColor,
367 const char* srcCoverage,
368 const char* dstColor,
369 const char* outColor,
370 const char* outColorSecondary,
371 const GrXferProcessor&) {
372 SK_ABORT("emitBlendCodeForDstRead not implemented.");
373 }
374
375 virtual void emitWriteSwizzle(GrGLSLXPFragmentBuilder*,
376 const skgpu::Swizzle&,
377 const char* outColor,
378 const char* outColorSecondary) const;
379
380 virtual void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) {}
381 };
382
383 #endif
384