Debian Codegraph @

src/gpu/ganesh/GrXferProcessor.h

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