blob: b0131bc5968727952e3ec7ffc381bf5ee77ccaea [file] [log] [blame]
joshualittb0a8a372014-09-23 09:50:21 -07001/*
2 * Copyright 2013 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 GrGeometryProcessor_DEFINED
9#define GrGeometryProcessor_DEFINED
10
Robert Phillips787fd9d2021-03-22 14:48:09 -040011#include "src/gpu/GrColor.h"
Brian Salomon48959462021-08-11 13:01:06 -040012#include "src/gpu/GrFragmentProcessor.h"
Robert Phillips787fd9d2021-03-22 14:48:09 -040013#include "src/gpu/GrProcessor.h"
Brian Salomon48959462021-08-11 13:01:06 -040014#include "src/gpu/GrShaderCaps.h"
Robert Phillips787fd9d2021-03-22 14:48:09 -040015#include "src/gpu/GrShaderVar.h"
16#include "src/gpu/GrSwizzle.h"
Brian Salomon48959462021-08-11 13:01:06 -040017#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
18#include "src/gpu/glsl/GrGLSLUniformHandler.h"
19#include "src/gpu/glsl/GrGLSLVarying.h"
Robert Phillips787fd9d2021-03-22 14:48:09 -040020
Brian Salomon48959462021-08-11 13:01:06 -040021#include <unordered_map>
22
23class GrGLSLFPFragmentBuilder;
24class GrGLSLGeometryBuilder;
25class GrGLSLVaryingHandler;
Robert Phillips787fd9d2021-03-22 14:48:09 -040026class GrGLSLUniformHandler;
Brian Salomon48959462021-08-11 13:01:06 -040027class GrGLSLVertexBuilder;
Robert Phillips787fd9d2021-03-22 14:48:09 -040028
joshualitt9b989322014-12-15 14:16:27 -080029/**
Robert Phillips787fd9d2021-03-22 14:48:09 -040030 * The GrGeometryProcessor represents some kind of geometric primitive. This includes the shape
31 * of the primitive and the inherent color of the primitive. The GrGeometryProcessor is
32 * responsible for providing a color and coverage input into the Ganesh rendering pipeline. Through
33 * optimization, Ganesh may decide a different color, no color, and / or no coverage are required
34 * from the GrGeometryProcessor, so the GrGeometryProcessor must be able to support this
35 * functionality.
36 *
37 * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
38 * GrGeometryProcessor. These loops run on the CPU and to determine known properties of the final
39 * color and coverage inputs to the GrXferProcessor in order to perform optimizations that preserve
40 * correctness. The GrDrawOp seeds these loops with initial color and coverage, in its
41 * getProcessorAnalysisInputs implementation. These seed values are processed by the
42 * subsequent stages of the rendering pipeline and the output is then fed back into the GrDrawOp
43 * in the applyPipelineOptimizations call, where the op can use the information to inform
44 * decisions about GrGeometryProcessor creation.
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050045 *
46 * Note that all derived classes should hide their constructors and provide a Make factory
Robert Phillips787fd9d2021-03-22 14:48:09 -040047 * function that takes an arena (except for Tesselation-specific classes). This is because
48 * geometry processors can be created in either the record-time or flush-time arenas which
49 * define their lifetimes (i.e., a DDLs life time in the first case and a single flush in
50 * the second case).
joshualitt9b989322014-12-15 14:16:27 -080051 */
Brian Salomon8c8379b2021-08-11 16:19:06 -040052class GrGeometryProcessor : public GrProcessor {
joshualitt9b989322014-12-15 14:16:27 -080053public:
Brian Salomonf95940b2021-08-09 15:56:24 -040054 /**
Brian Salomond89d4452021-08-09 16:42:36 -040055 * Every GrGeometryProcessor must be capable of creating a subclass of ProgramImpl. The
56 * ProgramImpl emits the shader code that implements the GrGeometryProcessor, is attached to the
57 * generated backend API pipeline/program and used to extract uniform data from
58 * GrGeometryProcessor instances.
Brian Salomonf95940b2021-08-09 15:56:24 -040059 */
60 class ProgramImpl;
61
Robert Phillips787fd9d2021-03-22 14:48:09 -040062 class TextureSampler;
63
64 /** Describes a vertex or instance attribute. */
65 class Attribute {
66 public:
67 constexpr Attribute() = default;
68 constexpr Attribute(const char* name,
69 GrVertexAttribType cpuType,
70 GrSLType gpuType)
71 : fName(name), fCPUType(cpuType), fGPUType(gpuType) {
72 SkASSERT(name && gpuType != kVoid_GrSLType);
73 }
74 constexpr Attribute(const Attribute&) = default;
75
76 Attribute& operator=(const Attribute&) = default;
77
78 constexpr bool isInitialized() const { return fGPUType != kVoid_GrSLType; }
79
80 constexpr const char* name() const { return fName; }
81 constexpr GrVertexAttribType cpuType() const { return fCPUType; }
82 constexpr GrSLType gpuType() const { return fGPUType; }
83
84 inline constexpr size_t size() const;
85 constexpr size_t sizeAlign4() const { return SkAlign4(this->size()); }
86
87 GrShaderVar asShaderVar() const {
88 return {fName, fGPUType, GrShaderVar::TypeModifier::In};
89 }
90
91 private:
92 const char* fName = nullptr;
93 GrVertexAttribType fCPUType = kFloat_GrVertexAttribType;
94 GrSLType fGPUType = kVoid_GrSLType;
95 };
96
97 class Iter {
98 public:
99 Iter() : fCurr(nullptr), fRemaining(0) {}
100 Iter(const Iter& iter) : fCurr(iter.fCurr), fRemaining(iter.fRemaining) {}
101 Iter& operator= (const Iter& iter) {
102 fCurr = iter.fCurr;
103 fRemaining = iter.fRemaining;
104 return *this;
105 }
106 Iter(const Attribute* attrs, int count) : fCurr(attrs), fRemaining(count) {
107 this->skipUninitialized();
108 }
109
110 bool operator!=(const Iter& that) const { return fCurr != that.fCurr; }
111 const Attribute& operator*() const { return *fCurr; }
112 void operator++() {
113 if (fRemaining) {
114 fRemaining--;
115 fCurr++;
116 this->skipUninitialized();
117 }
118 }
119
120 private:
121 void skipUninitialized() {
122 if (!fRemaining) {
123 fCurr = nullptr;
124 } else {
125 while (!fCurr->isInitialized()) {
126 ++fCurr;
127 }
128 }
129 }
130
131 const Attribute* fCurr;
132 int fRemaining;
133 };
134
135 class AttributeSet {
136 public:
137 Iter begin() const { return Iter(fAttributes, fCount); }
138 Iter end() const { return Iter(); }
139
140 int count() const { return fCount; }
141 size_t stride() const { return fStride; }
142
143 private:
144 friend class GrGeometryProcessor;
145
146 void init(const Attribute* attrs, int count) {
147 fAttributes = attrs;
148 fRawCount = count;
149 fCount = 0;
150 fStride = 0;
151 for (int i = 0; i < count; ++i) {
152 if (attrs[i].isInitialized()) {
153 fCount++;
154 fStride += attrs[i].sizeAlign4();
155 }
156 }
157 }
158
159 const Attribute* fAttributes = nullptr;
160 int fRawCount = 0;
161 int fCount = 0;
162 size_t fStride = 0;
163 };
164
165 GrGeometryProcessor(ClassID);
166
167 int numTextureSamplers() const { return fTextureSamplerCnt; }
168 const TextureSampler& textureSampler(int index) const;
169 int numVertexAttributes() const { return fVertexAttributes.fCount; }
170 const AttributeSet& vertexAttributes() const { return fVertexAttributes; }
171 int numInstanceAttributes() const { return fInstanceAttributes.fCount; }
172 const AttributeSet& instanceAttributes() const { return fInstanceAttributes; }
173
174 bool hasVertexAttributes() const { return SkToBool(fVertexAttributes.fCount); }
175 bool hasInstanceAttributes() const { return SkToBool(fInstanceAttributes.fCount); }
176
177 /**
178 * A common practice is to populate the the vertex/instance's memory using an implicit array of
179 * structs. In this case, it is best to assert that:
180 * stride == sizeof(struct)
181 */
182 size_t vertexStride() const { return fVertexAttributes.fStride; }
183 size_t instanceStride() const { return fInstanceAttributes.fStride; }
184
185 bool willUseTessellationShaders() const {
186 return fShaders & (kTessControl_GrShaderFlag | kTessEvaluation_GrShaderFlag);
187 }
188
189 bool willUseGeoShader() const {
190 return fShaders & kGeometry_GrShaderFlag;
191 }
192
193 /**
194 * Computes a key for the transforms owned by an FP based on the shader code that will be
195 * emitted by the primitive processor to implement them.
196 */
197 static uint32_t ComputeCoordTransformsKey(const GrFragmentProcessor& fp);
198
199 static constexpr int kCoordTransformKeyBits = 4;
200
201 /**
Brian Salomon13b28732021-08-06 15:33:58 -0400202 * Adds a key on the GrProcessorKeyBuilder that reflects any variety in the code that the
203 * geometry processor subclass can emit.
Robert Phillips787fd9d2021-03-22 14:48:09 -0400204 */
Brian Salomon13b28732021-08-06 15:33:58 -0400205 virtual void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
Robert Phillips787fd9d2021-03-22 14:48:09 -0400206
207 void getAttributeKey(GrProcessorKeyBuilder* b) const {
208 // Ensure that our CPU and GPU type fields fit together in a 32-bit value, and we never
209 // collide with the "uninitialized" value.
210 static_assert(kGrVertexAttribTypeCount < (1 << 8), "");
211 static_assert(kGrSLTypeCount < (1 << 8), "");
212
213 auto add_attributes = [=](const Attribute* attrs, int attrCount) {
214 for (int i = 0; i < attrCount; ++i) {
215 const Attribute& attr = attrs[i];
216 b->appendComment(attr.isInitialized() ? attr.name() : "unusedAttr");
217 b->addBits(8, attr.isInitialized() ? attr.cpuType() : 0xff, "attrType");
218 b->addBits(8, attr.isInitialized() ? attr.gpuType() : 0xff, "attrGpuType");
219 }
220 };
221 b->add32(fVertexAttributes.fRawCount, "numVertexAttributes");
222 add_attributes(fVertexAttributes.fAttributes, fVertexAttributes.fRawCount);
223 b->add32(fInstanceAttributes.fRawCount, "numInstanceAttributes");
224 add_attributes(fInstanceAttributes.fAttributes, fInstanceAttributes.fRawCount);
225 }
226
Brian Salomonf95940b2021-08-09 15:56:24 -0400227 /**
228 * Returns a new instance of the appropriate implementation class for the given
229 * GrGeometryProcessor.
230 */
231 virtual std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const = 0;
Robert Phillips787fd9d2021-03-22 14:48:09 -0400232
joshualittb0a8a372014-09-23 09:50:21 -0700233protected:
Brian Osman2715bf52019-12-06 14:38:47 -0500234 // GPs that need to use either float or ubyte colors can just call this to get a correctly
Brian Osmanc906d252018-12-04 11:17:46 -0500235 // configured Attribute struct
236 static Attribute MakeColorAttribute(const char* name, bool wideColor) {
237 return { name,
Brian Osman2715bf52019-12-06 14:38:47 -0500238 wideColor ? kFloat4_GrVertexAttribType : kUByte4_norm_GrVertexAttribType,
Brian Osmanc906d252018-12-04 11:17:46 -0500239 kHalf4_GrSLType };
240 }
241
Robert Phillips787fd9d2021-03-22 14:48:09 -0400242 void setVertexAttributes(const Attribute* attrs, int attrCount) {
243 fVertexAttributes.init(attrs, attrCount);
244 }
245 void setInstanceAttributes(const Attribute* attrs, int attrCount) {
246 SkASSERT(attrCount >= 0);
247 fInstanceAttributes.init(attrs, attrCount);
248 }
249 void setWillUseTessellationShaders() {
250 fShaders |= kTessControl_GrShaderFlag | kTessEvaluation_GrShaderFlag;
251 }
252 void setWillUseGeoShader() { fShaders |= kGeometry_GrShaderFlag; }
253 void setTextureSamplerCnt(int cnt) {
254 SkASSERT(cnt >= 0);
255 fTextureSamplerCnt = cnt;
256 }
257
258 /**
259 * Helper for implementing onTextureSampler(). E.g.:
260 * return IthTexureSampler(i, fMyFirstSampler, fMySecondSampler, fMyThirdSampler);
261 */
262 template <typename... Args>
263 static const TextureSampler& IthTextureSampler(int i, const TextureSampler& samp0,
264 const Args&... samps) {
265 return (0 == i) ? samp0 : IthTextureSampler(i - 1, samps...);
266 }
267 inline static const TextureSampler& IthTextureSampler(int i);
268
joshualittb0a8a372014-09-23 09:50:21 -0700269private:
Robert Phillips787fd9d2021-03-22 14:48:09 -0400270 virtual const TextureSampler& onTextureSampler(int) const { return IthTextureSampler(0); }
271
272 GrShaderFlags fShaders = kVertex_GrShaderFlag | kFragment_GrShaderFlag;
273
274 AttributeSet fVertexAttributes;
275 AttributeSet fInstanceAttributes;
276
277 int fTextureSamplerCnt = 0;
278 using INHERITED = GrProcessor;
joshualittb0a8a372014-09-23 09:50:21 -0700279};
joshualitt56995b52014-12-11 15:44:02 -0800280
Robert Phillips787fd9d2021-03-22 14:48:09 -0400281//////////////////////////////////////////////////////////////////////////////
282
Brian Salomon48959462021-08-11 13:01:06 -0400283class GrGeometryProcessor::ProgramImpl {
284public:
285 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
286 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
287 /**
288 * Struct of optional varying that replaces the input coords and bool indicating whether the FP
289 * should take a coord param as an argument. The latter may be false if the coords are simply
290 * unused or if the GP has lifted their computation to a varying emitted by the VS.
291 */
292 struct FPCoords {GrShaderVar coordsVarying; bool hasCoordsParam;};
293 using FPCoordsMap = std::unordered_map<const GrFragmentProcessor*, FPCoords>;
294
295 virtual ~ProgramImpl() = default;
296
297 struct EmitArgs {
298 EmitArgs(GrGLSLVertexBuilder* vertBuilder,
299 GrGLSLGeometryBuilder* geomBuilder,
300 GrGLSLFPFragmentBuilder* fragBuilder,
301 GrGLSLVaryingHandler* varyingHandler,
302 GrGLSLUniformHandler* uniformHandler,
303 const GrShaderCaps* caps,
304 const GrGeometryProcessor& geomProc,
305 const char* outputColor,
306 const char* outputCoverage,
307 const SamplerHandle* texSamplers)
308 : fVertBuilder(vertBuilder)
309 , fGeomBuilder(geomBuilder)
310 , fFragBuilder(fragBuilder)
311 , fVaryingHandler(varyingHandler)
312 , fUniformHandler(uniformHandler)
313 , fShaderCaps(caps)
314 , fGeomProc(geomProc)
315 , fOutputColor(outputColor)
316 , fOutputCoverage(outputCoverage)
317 , fTexSamplers(texSamplers) {}
318 GrGLSLVertexBuilder* fVertBuilder;
319 GrGLSLGeometryBuilder* fGeomBuilder;
320 GrGLSLFPFragmentBuilder* fFragBuilder;
321 GrGLSLVaryingHandler* fVaryingHandler;
322 GrGLSLUniformHandler* fUniformHandler;
323 const GrShaderCaps* fShaderCaps;
324 const GrGeometryProcessor& fGeomProc;
325 const char* fOutputColor;
326 const char* fOutputCoverage;
327 const SamplerHandle* fTexSamplers;
328 };
329
330 /**
331 * Emits the code from this geometry processor into the shaders. For any FP in the pipeline that
332 * has its input coords implemented by the GP as a varying, the varying will be accessible in
333 * the returned map and should be used when the FP code is emitted.
334 **/
335 FPCoordsMap emitCode(EmitArgs&, const GrPipeline& pipeline);
336
337 /**
338 * Called after all effect emitCode() functions, to give the processor a chance to write out
339 * additional transformation code now that all uniforms have been emitted.
340 * It generates the final code for assigning transformed coordinates to the varyings recorded
341 * in the call to collectTransforms(). This must happen after FP code emission so that it has
342 * access to any uniforms the FPs registered for uniform sample matrix invocations.
343 */
344 void emitTransformCode(GrGLSLVertexBuilder* vb, GrGLSLUniformHandler* uniformHandler);
345
346 /**
347 * A ProgramImpl instance can be reused with any GrGeometryProcessor that produces the same key.
348 * This function reads data from a GrGeometryProcessor and updates any uniform variables
349 * required by the shaders created in emitCode(). The GrGeometryProcessor parameter is
350 * guaranteed to be of the same type and to have an identical processor key as the
351 * GrGeometryProcessor that created this ProgramImpl.
352 */
353 virtual void setData(const GrGLSLProgramDataManager&,
354 const GrShaderCaps&,
355 const GrGeometryProcessor&) = 0;
356
357 // We use these methods as a temporary back door to inject OpenGL tessellation code. Once
358 // tessellation is supported by SkSL we can remove these.
359 virtual SkString getTessControlShaderGLSL(const GrGeometryProcessor&,
360 const char* versionAndExtensionDecls,
361 const GrGLSLUniformHandler&,
362 const GrShaderCaps&) const {
363 SK_ABORT("Not implemented.");
364 }
365 virtual SkString getTessEvaluationShaderGLSL(const GrGeometryProcessor&,
366 const char* versionAndExtensionDecls,
367 const GrGLSLUniformHandler&,
368 const GrShaderCaps&) const {
369 SK_ABORT("Not implemented.");
370 }
371
372 // GPs that use writeOutputPosition and/or writeLocalCoord must incorporate the matrix type
373 // into their key, and should use this function or one of the other related helpers.
374 static uint32_t ComputeMatrixKey(const GrShaderCaps& caps, const SkMatrix& mat) {
375 if (!caps.reducedShaderMode()) {
376 if (mat.isIdentity()) {
377 return 0b00;
378 }
379 if (mat.isScaleTranslate()) {
380 return 0b01;
381 }
382 }
383 if (!mat.hasPerspective()) {
384 return 0b10;
385 }
386 return 0b11;
387 }
388
389 static uint32_t ComputeMatrixKeys(const GrShaderCaps& shaderCaps,
390 const SkMatrix& viewMatrix,
391 const SkMatrix& localMatrix) {
392 return (ComputeMatrixKey(shaderCaps, viewMatrix) << kMatrixKeyBits) |
393 ComputeMatrixKey(shaderCaps, localMatrix);
394 }
395
396 static uint32_t AddMatrixKeys(const GrShaderCaps& shaderCaps,
397 uint32_t flags,
398 const SkMatrix& viewMatrix,
399 const SkMatrix& localMatrix) {
400 // Shifting to make room for the matrix keys shouldn't lose bits
401 SkASSERT(((flags << (2 * kMatrixKeyBits)) >> (2 * kMatrixKeyBits)) == flags);
402 return (flags << (2 * kMatrixKeyBits)) |
403 ComputeMatrixKeys(shaderCaps, viewMatrix, localMatrix);
404 }
405 static constexpr int kMatrixKeyBits = 2;
406
407protected:
408 void setupUniformColor(GrGLSLFPFragmentBuilder* fragBuilder,
409 GrGLSLUniformHandler* uniformHandler,
410 const char* outputName,
411 UniformHandle* colorUniform);
412
413 // A helper for setting the matrix on a uniform handle initialized through
414 // writeOutputPosition or writeLocalCoord. Automatically handles elided uniforms,
415 // scale+translate matrices, and state tracking (if provided state pointer is non-null).
416 static void SetTransform(const GrGLSLProgramDataManager&,
417 const GrShaderCaps&,
418 const UniformHandle& uniform,
419 const SkMatrix& matrix,
420 SkMatrix* state = nullptr);
421
422 struct GrGPArgs {
423 // Used to specify the output variable used by the GP to store its device position. It can
424 // either be a float2 or a float3 (in order to handle perspective). The subclass sets this
425 // in its onEmitCode().
426 GrShaderVar fPositionVar;
427 // Used to specify the variable storing the draw's local coordinates. It can be either a
428 // float2, float3, or void. It can only be void when no FP needs local coordinates. This
429 // variable can be an attribute or local variable, but should not itself be a varying.
430 // ProgramImpl automatically determines if this must be passed to a FS.
431 GrShaderVar fLocalCoordVar;
432 };
433
434 // Helpers for adding code to write the transformed vertex position. The first simple version
435 // just writes a variable named by 'posName' into the position output variable with the
436 // assumption that the position is 2D. The second version transforms the input position by a
437 // view matrix and the output variable is 2D or 3D depending on whether the view matrix is
438 // perspective. Both versions declare the output position variable and will set
439 // GrGPArgs::fPositionVar.
440 static void WriteOutputPosition(GrGLSLVertexBuilder*, GrGPArgs*, const char* posName);
441 static void WriteOutputPosition(GrGLSLVertexBuilder*,
442 GrGLSLUniformHandler*,
443 const GrShaderCaps&,
444 GrGPArgs*,
445 const char* posName,
446 const SkMatrix& viewMatrix,
447 UniformHandle* viewMatrixUniform);
448
449 // Helper to transform an existing variable by a given local matrix (e.g. the inverse view
450 // matrix). It will declare the transformed local coord variable and will set
451 // GrGPArgs::fLocalCoordVar.
452 static void WriteLocalCoord(GrGLSLVertexBuilder*,
453 GrGLSLUniformHandler*,
454 const GrShaderCaps&,
455 GrGPArgs*,
456 GrShaderVar localVar,
457 const SkMatrix& localMatrix,
458 UniformHandle* localMatrixUniform);
459
460private:
461 virtual void onEmitCode(EmitArgs&, GrGPArgs*) = 0;
462
463 // Iterates over the FPs beginning with the passed iter to register additional varyings and
464 // uniforms to support VS-promoted local coord evaluation for the FPs.
465 //
466 // This must happen before FP code emission so that the FPs can find the appropriate varying
467 // handles they use in place of explicit coord sampling; it is automatically called after
468 // onEmitCode() returns using the value stored in GpArgs::fLocalCoordVar and
469 // GpArgs::fPositionVar.
470 FPCoordsMap collectTransforms(GrGLSLVertexBuilder* vb,
471 GrGLSLVaryingHandler* varyingHandler,
472 GrGLSLUniformHandler* uniformHandler,
473 const GrShaderVar& localCoordsVar,
474 const GrShaderVar& positionVar,
475 const GrPipeline& pipeline);
476 struct TransformInfo {
477 // The varying that conveys the coordinates to one or more FPs in the FS.
478 GrGLSLVarying varying;
479 // The coordinate to be transformed. varying is computed from this.
480 GrShaderVar inputCoords;
481 // Used to sort so that ancestor FP varyings are initialized before descendant FP varyings.
482 int traversalOrder;
483 };
484 // Populated by collectTransforms() for use in emitTransformCode(). When we lift the computation
485 // of a FP's input coord to a varying we propagate that varying up the FP tree to the highest
486 // node that shares the same coordinates. This allows multiple FPs in a subtree to share a
487 // varying.
488 std::unordered_map<const GrFragmentProcessor*, TransformInfo> fTransformVaryingsMap;
489};
490
491///////////////////////////////////////////////////////////////////////////
492
Robert Phillips787fd9d2021-03-22 14:48:09 -0400493/**
494 * Used to capture the properties of the GrTextureProxies required/expected by a primitiveProcessor
495 * along with an associated GrSamplerState. The actual proxies used are stored in either the
496 * fixed or dynamic state arrays. TextureSamplers don't perform any coord manipulation to account
497 * for texture origin.
498 */
499class GrGeometryProcessor::TextureSampler {
500public:
501 TextureSampler() = default;
502
503 TextureSampler(GrSamplerState, const GrBackendFormat&, const GrSwizzle&);
504
505 TextureSampler(const TextureSampler&) = delete;
506 TextureSampler& operator=(const TextureSampler&) = delete;
507
508 void reset(GrSamplerState, const GrBackendFormat&, const GrSwizzle&);
509
510 const GrBackendFormat& backendFormat() const { return fBackendFormat; }
511 GrTextureType textureType() const { return fBackendFormat.textureType(); }
512
513 GrSamplerState samplerState() const { return fSamplerState; }
514 const GrSwizzle& swizzle() const { return fSwizzle; }
515
516 bool isInitialized() const { return fIsInitialized; }
517
518private:
519 GrSamplerState fSamplerState;
520 GrBackendFormat fBackendFormat;
521 GrSwizzle fSwizzle;
522 bool fIsInitialized = false;
523};
524
525const GrGeometryProcessor::TextureSampler& GrGeometryProcessor::IthTextureSampler(int i) {
526 SK_ABORT("Illegal texture sampler index");
527 static const TextureSampler kBogus;
528 return kBogus;
529}
530
531//////////////////////////////////////////////////////////////////////////////
532
533/**
534 * Returns the size of the attrib type in bytes.
535 * This was moved from include/private/GrTypesPriv.h in service of Skia dependents that build
536 * with C++11.
537 */
538static constexpr inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
539 switch (type) {
540 case kFloat_GrVertexAttribType:
541 return sizeof(float);
542 case kFloat2_GrVertexAttribType:
543 return 2 * sizeof(float);
544 case kFloat3_GrVertexAttribType:
545 return 3 * sizeof(float);
546 case kFloat4_GrVertexAttribType:
547 return 4 * sizeof(float);
548 case kHalf_GrVertexAttribType:
549 return sizeof(uint16_t);
550 case kHalf2_GrVertexAttribType:
551 return 2 * sizeof(uint16_t);
552 case kHalf4_GrVertexAttribType:
553 return 4 * sizeof(uint16_t);
554 case kInt2_GrVertexAttribType:
555 return 2 * sizeof(int32_t);
556 case kInt3_GrVertexAttribType:
557 return 3 * sizeof(int32_t);
558 case kInt4_GrVertexAttribType:
559 return 4 * sizeof(int32_t);
560 case kByte_GrVertexAttribType:
561 return 1 * sizeof(char);
562 case kByte2_GrVertexAttribType:
563 return 2 * sizeof(char);
564 case kByte4_GrVertexAttribType:
565 return 4 * sizeof(char);
566 case kUByte_GrVertexAttribType:
567 return 1 * sizeof(char);
568 case kUByte2_GrVertexAttribType:
569 return 2 * sizeof(char);
570 case kUByte4_GrVertexAttribType:
571 return 4 * sizeof(char);
572 case kUByte_norm_GrVertexAttribType:
573 return 1 * sizeof(char);
574 case kUByte4_norm_GrVertexAttribType:
575 return 4 * sizeof(char);
576 case kShort2_GrVertexAttribType:
577 return 2 * sizeof(int16_t);
578 case kShort4_GrVertexAttribType:
579 return 4 * sizeof(int16_t);
580 case kUShort2_GrVertexAttribType: // fall through
581 case kUShort2_norm_GrVertexAttribType:
582 return 2 * sizeof(uint16_t);
583 case kInt_GrVertexAttribType:
584 return sizeof(int32_t);
585 case kUint_GrVertexAttribType:
586 return sizeof(uint32_t);
587 case kUShort_norm_GrVertexAttribType:
588 return sizeof(uint16_t);
589 case kUShort4_norm_GrVertexAttribType:
590 return 4 * sizeof(uint16_t);
591 }
592 // GCC fails because SK_ABORT evaluates to non constexpr. clang and cl.exe think this is
593 // unreachable and don't complain.
594#if defined(__clang__) || !defined(__GNUC__)
595 SK_ABORT("Unsupported type conversion");
596#endif
597 return 0;
598}
599
600constexpr size_t GrGeometryProcessor::Attribute::size() const {
601 return GrVertexAttribTypeSize(fCPUType);
602}
603
joshualittb0a8a372014-09-23 09:50:21 -0700604#endif