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