joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 1 | /* |
| 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 GrPrimitiveProcessor_DEFINED |
| 9 | #define GrPrimitiveProcessor_DEFINED |
| 10 | |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrNonAtomicRef.h" |
| 13 | #include "src/gpu/GrProcessor.h" |
| 14 | #include "src/gpu/GrShaderVar.h" |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrSwizzle.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * The GrPrimitiveProcessor represents some kind of geometric primitive. This includes the shape |
| 19 | * of the primitive and the inherent color of the primitive. The GrPrimitiveProcessor is |
| 20 | * responsible for providing a color and coverage input into the Ganesh rendering pipeline. Through |
| 21 | * optimization, Ganesh may decide a different color, no color, and / or no coverage are required |
| 22 | * from the GrPrimitiveProcessor, so the GrPrimitiveProcessor must be able to support this |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 23 | * functionality. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 24 | * |
| 25 | * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the |
Brian Salomon | cb30bb2 | 2017-02-12 09:28:54 -0500 | [diff] [blame] | 26 | * GrPrimitiveProcessor. These loops run on the CPU and to determine known properties of the final |
| 27 | * color and coverage inputs to the GrXferProcessor in order to perform optimizations that preserve |
| 28 | * correctness. The GrDrawOp seeds these loops with initial color and coverage, in its |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 29 | * getProcessorAnalysisInputs implementation. These seed values are processed by the |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 30 | * subsequent |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 31 | * stages of the rendering pipeline and the output is then fed back into the GrDrawOp in |
| 32 | * the applyPipelineOptimizations call, where the op can use the information to inform decisions |
| 33 | * about GrPrimitiveProcessor creation. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 34 | */ |
| 35 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 36 | class GrGLSLPrimitiveProcessor; |
Chris Dalton | 54c9093 | 2020-06-24 10:51:52 -0600 | [diff] [blame] | 37 | class GrGLSLUniformHandler; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 38 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 39 | /** |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 40 | * GrPrimitiveProcessor defines an interface which all subclasses must implement. All |
| 41 | * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage |
| 42 | * pipelines, and they must provide some notion of equality |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 43 | * |
| 44 | * TODO: This class does not really need to be ref counted. Instances should be allocated using |
| 45 | * GrOpFlushState's arena and destroyed when the arena is torn down. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 46 | */ |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 47 | class GrPrimitiveProcessor : public GrProcessor, public GrNonAtomicRef<GrPrimitiveProcessor> { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 48 | public: |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 49 | class TextureSampler; |
| 50 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 51 | /** Describes a vertex or instance attribute. */ |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 52 | class Attribute { |
| 53 | public: |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 54 | constexpr Attribute() = default; |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 55 | constexpr Attribute(const char* name, |
| 56 | GrVertexAttribType cpuType, |
| 57 | GrSLType gpuType) |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 58 | : fName(name), fCPUType(cpuType), fGPUType(gpuType) { |
| 59 | SkASSERT(name && gpuType != kVoid_GrSLType); |
| 60 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 61 | constexpr Attribute(const Attribute&) = default; |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 62 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 63 | Attribute& operator=(const Attribute&) = default; |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 64 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 65 | constexpr bool isInitialized() const { return fGPUType != kVoid_GrSLType; } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 66 | |
| 67 | constexpr const char* name() const { return fName; } |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 68 | constexpr GrVertexAttribType cpuType() const { return fCPUType; } |
| 69 | constexpr GrSLType gpuType() const { return fGPUType; } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 70 | |
| 71 | inline constexpr size_t size() const; |
| 72 | constexpr size_t sizeAlign4() const { return SkAlign4(this->size()); } |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 73 | |
| 74 | GrShaderVar asShaderVar() const { |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 75 | return {fName, fGPUType, GrShaderVar::TypeModifier::In}; |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private: |
| 79 | const char* fName = nullptr; |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 80 | GrVertexAttribType fCPUType = kFloat_GrVertexAttribType; |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 81 | GrSLType fGPUType = kVoid_GrSLType; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 84 | class Iter { |
| 85 | public: |
| 86 | Iter() : fCurr(nullptr), fRemaining(0) {} |
| 87 | Iter(const Iter& iter) : fCurr(iter.fCurr), fRemaining(iter.fRemaining) {} |
| 88 | Iter& operator= (const Iter& iter) { |
| 89 | fCurr = iter.fCurr; |
| 90 | fRemaining = iter.fRemaining; |
| 91 | return *this; |
| 92 | } |
| 93 | Iter(const Attribute* attrs, int count) : fCurr(attrs), fRemaining(count) { |
| 94 | this->skipUninitialized(); |
| 95 | } |
| 96 | |
| 97 | bool operator!=(const Iter& that) const { return fCurr != that.fCurr; } |
| 98 | const Attribute& operator*() const { return *fCurr; } |
| 99 | void operator++() { |
| 100 | if (fRemaining) { |
| 101 | fRemaining--; |
| 102 | fCurr++; |
| 103 | this->skipUninitialized(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | void skipUninitialized() { |
| 109 | if (!fRemaining) { |
| 110 | fCurr = nullptr; |
| 111 | } else { |
| 112 | while (!fCurr->isInitialized()) { |
| 113 | ++fCurr; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | const Attribute* fCurr; |
| 119 | int fRemaining; |
| 120 | }; |
| 121 | |
| 122 | class AttributeSet { |
| 123 | public: |
| 124 | Iter begin() const { return Iter(fAttributes, fCount); } |
| 125 | Iter end() const { return Iter(); } |
| 126 | |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 127 | int count() const { return fCount; } |
| 128 | size_t stride() const { return fStride; } |
| 129 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 130 | private: |
| 131 | friend class GrPrimitiveProcessor; |
| 132 | |
| 133 | void init(const Attribute* attrs, int count) { |
| 134 | fAttributes = attrs; |
Brian Osman | d3e7130 | 2018-12-06 11:17:35 -0500 | [diff] [blame] | 135 | fRawCount = count; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 136 | fCount = 0; |
| 137 | fStride = 0; |
| 138 | for (int i = 0; i < count; ++i) { |
| 139 | if (attrs[i].isInitialized()) { |
| 140 | fCount++; |
| 141 | fStride += attrs[i].sizeAlign4(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | const Attribute* fAttributes = nullptr; |
Brian Osman | d3e7130 | 2018-12-06 11:17:35 -0500 | [diff] [blame] | 147 | int fRawCount = 0; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 148 | int fCount = 0; |
| 149 | size_t fStride = 0; |
| 150 | }; |
| 151 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 152 | GrPrimitiveProcessor(ClassID); |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 153 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 154 | int numTextureSamplers() const { return fTextureSamplerCnt; } |
| 155 | const TextureSampler& textureSampler(int index) const; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 156 | int numVertexAttributes() const { return fVertexAttributes.fCount; } |
| 157 | const AttributeSet& vertexAttributes() const { return fVertexAttributes; } |
| 158 | int numInstanceAttributes() const { return fInstanceAttributes.fCount; } |
| 159 | const AttributeSet& instanceAttributes() const { return fInstanceAttributes; } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 160 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 161 | bool hasVertexAttributes() const { return SkToBool(fVertexAttributes.fCount); } |
| 162 | bool hasInstanceAttributes() const { return SkToBool(fInstanceAttributes.fCount); } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 163 | |
| 164 | /** |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 165 | * A common practice is to populate the the vertex/instance's memory using an implicit array of |
| 166 | * structs. In this case, it is best to assert that: |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 167 | * stride == sizeof(struct) |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 168 | */ |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 169 | size_t vertexStride() const { return fVertexAttributes.fStride; } |
| 170 | size_t instanceStride() const { return fInstanceAttributes.fStride; } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 171 | |
Chris Dalton | 5a2f962 | 2019-12-27 14:56:38 -0700 | [diff] [blame] | 172 | bool willUseTessellationShaders() const { |
| 173 | return fShaders & (kTessControl_GrShaderFlag | kTessEvaluation_GrShaderFlag); |
| 174 | } |
| 175 | |
| 176 | bool willUseGeoShader() const { |
| 177 | return fShaders & kGeometry_GrShaderFlag; |
| 178 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 179 | |
| 180 | /** |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 181 | * Computes a key for the transforms owned by an FP based on the shader code that will be |
| 182 | * emitted by the primitive processor to implement them. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 183 | */ |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 184 | static uint32_t ComputeCoordTransformsKey(const GrFragmentProcessor& fp); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 185 | |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 186 | static constexpr int kCoordTransformKeyBits = 4; |
| 187 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 188 | /** |
| 189 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry |
| 190 | * processor's GL backend implementation. |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 191 | * |
| 192 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 193 | */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 194 | virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 195 | |
| 196 | |
Brian Osman | d3e7130 | 2018-12-06 11:17:35 -0500 | [diff] [blame] | 197 | void getAttributeKey(GrProcessorKeyBuilder* b) const { |
| 198 | // Ensure that our CPU and GPU type fields fit together in a 32-bit value, and we never |
| 199 | // collide with the "uninitialized" value. |
| 200 | static_assert(kGrVertexAttribTypeCount < (1 << 8), ""); |
| 201 | static_assert(kGrSLTypeCount < (1 << 8), ""); |
| 202 | |
| 203 | auto add_attributes = [=](const Attribute* attrs, int attrCount) { |
| 204 | for (int i = 0; i < attrCount; ++i) { |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 205 | const Attribute& attr = attrs[i]; |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame^] | 206 | b->appendComment(attr.isInitialized() ? attr.name() : "unusedAttr"); |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 207 | b->addBits(8, attr.isInitialized() ? attr.cpuType() : 0xff, "attrType"); |
| 208 | b->addBits(8, attr.isInitialized() ? attr.gpuType() : 0xff, "attrGpuType"); |
Brian Osman | d3e7130 | 2018-12-06 11:17:35 -0500 | [diff] [blame] | 209 | } |
| 210 | }; |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 211 | b->add32(fVertexAttributes.fRawCount, "numVertexAttributes"); |
Brian Osman | d3e7130 | 2018-12-06 11:17:35 -0500 | [diff] [blame] | 212 | add_attributes(fVertexAttributes.fAttributes, fVertexAttributes.fRawCount); |
Brian Osman | c8d836f | 2021-03-04 16:24:35 -0500 | [diff] [blame] | 213 | b->add32(fInstanceAttributes.fRawCount, "numInstanceAttributes"); |
Brian Osman | d3e7130 | 2018-12-06 11:17:35 -0500 | [diff] [blame] | 214 | add_attributes(fInstanceAttributes.fAttributes, fInstanceAttributes.fRawCount); |
| 215 | } |
| 216 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 217 | /** Returns a new instance of the appropriate *GL* implementation class |
| 218 | for the given GrProcessor; caller is responsible for deleting |
| 219 | the object. */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 220 | virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 221 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 222 | virtual bool isPathRendering() const { return false; } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 223 | |
Chris Dalton | 5a2f962 | 2019-12-27 14:56:38 -0700 | [diff] [blame] | 224 | // We use these methods as a temporary back door to inject OpenGL tessellation code. Once |
| 225 | // tessellation is supported by SkSL we can remove these. |
Chris Dalton | 54c9093 | 2020-06-24 10:51:52 -0600 | [diff] [blame] | 226 | virtual SkString getTessControlShaderGLSL(const GrGLSLPrimitiveProcessor*, |
| 227 | const char* versionAndExtensionDecls, |
| 228 | const GrGLSLUniformHandler&, |
Chris Dalton | 5a2f962 | 2019-12-27 14:56:38 -0700 | [diff] [blame] | 229 | const GrShaderCaps&) const { |
| 230 | SK_ABORT("Not implemented."); |
| 231 | } |
Chris Dalton | 54c9093 | 2020-06-24 10:51:52 -0600 | [diff] [blame] | 232 | virtual SkString getTessEvaluationShaderGLSL(const GrGLSLPrimitiveProcessor*, |
| 233 | const char* versionAndExtensionDecls, |
| 234 | const GrGLSLUniformHandler&, |
Chris Dalton | 5a2f962 | 2019-12-27 14:56:38 -0700 | [diff] [blame] | 235 | const GrShaderCaps&) const { |
| 236 | SK_ABORT("Not implemented."); |
| 237 | } |
| 238 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 239 | protected: |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 240 | void setVertexAttributes(const Attribute* attrs, int attrCount) { |
| 241 | fVertexAttributes.init(attrs, attrCount); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 242 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 243 | void setInstanceAttributes(const Attribute* attrs, int attrCount) { |
| 244 | SkASSERT(attrCount >= 0); |
| 245 | fInstanceAttributes.init(attrs, attrCount); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 246 | } |
Chris Dalton | 5a2f962 | 2019-12-27 14:56:38 -0700 | [diff] [blame] | 247 | void setWillUseTessellationShaders() { |
| 248 | fShaders |= kTessControl_GrShaderFlag | kTessEvaluation_GrShaderFlag; |
| 249 | } |
| 250 | void setWillUseGeoShader() { fShaders |= kGeometry_GrShaderFlag; } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 251 | void setTextureSamplerCnt(int cnt) { |
| 252 | SkASSERT(cnt >= 0); |
| 253 | fTextureSamplerCnt = cnt; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Helper for implementing onTextureSampler(). E.g.: |
| 258 | * return IthTexureSampler(i, fMyFirstSampler, fMySecondSampler, fMyThirdSampler); |
| 259 | */ |
| 260 | template <typename... Args> |
| 261 | static const TextureSampler& IthTextureSampler(int i, const TextureSampler& samp0, |
| 262 | const Args&... samps) { |
| 263 | return (0 == i) ? samp0 : IthTextureSampler(i - 1, samps...); |
| 264 | } |
| 265 | inline static const TextureSampler& IthTextureSampler(int i); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 266 | |
| 267 | private: |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 268 | virtual const TextureSampler& onTextureSampler(int) const { return IthTextureSampler(0); } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 269 | |
Chris Dalton | 5a2f962 | 2019-12-27 14:56:38 -0700 | [diff] [blame] | 270 | GrShaderFlags fShaders = kVertex_GrShaderFlag | kFragment_GrShaderFlag; |
| 271 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 272 | AttributeSet fVertexAttributes; |
| 273 | AttributeSet fInstanceAttributes; |
| 274 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 275 | int fTextureSamplerCnt = 0; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 276 | using INHERITED = GrProcessor; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 279 | ////////////////////////////////////////////////////////////////////////////// |
| 280 | |
| 281 | /** |
Robert Phillips | 7d7aaf4 | 2019-10-14 11:34:16 -0400 | [diff] [blame] | 282 | * Used to capture the properties of the GrTextureProxies required/expected by a primitiveProcessor |
| 283 | * along with an associated GrSamplerState. The actual proxies used are stored in either the |
| 284 | * fixed or dynamic state arrays. TextureSamplers don't perform any coord manipulation to account |
| 285 | * for texture origin. |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 286 | */ |
| 287 | class GrPrimitiveProcessor::TextureSampler { |
| 288 | public: |
| 289 | TextureSampler() = default; |
| 290 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 291 | TextureSampler(GrSamplerState, const GrBackendFormat&, const GrSwizzle&); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 292 | |
| 293 | TextureSampler(const TextureSampler&) = delete; |
| 294 | TextureSampler& operator=(const TextureSampler&) = delete; |
| 295 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 296 | void reset(GrSamplerState, const GrBackendFormat&, const GrSwizzle&); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 297 | |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 298 | const GrBackendFormat& backendFormat() const { return fBackendFormat; } |
| 299 | GrTextureType textureType() const { return fBackendFormat.textureType(); } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 300 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 301 | GrSamplerState samplerState() const { return fSamplerState; } |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 302 | const GrSwizzle& swizzle() const { return fSwizzle; } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 303 | |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 304 | bool isInitialized() const { return fIsInitialized; } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 305 | |
| 306 | private: |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 307 | GrSamplerState fSamplerState; |
| 308 | GrBackendFormat fBackendFormat; |
| 309 | GrSwizzle fSwizzle; |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 310 | bool fIsInitialized = false; |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::IthTextureSampler(int i) { |
| 314 | SK_ABORT("Illegal texture sampler index"); |
| 315 | static const TextureSampler kBogus; |
| 316 | return kBogus; |
| 317 | } |
| 318 | |
| 319 | ////////////////////////////////////////////////////////////////////////////// |
| 320 | |
| 321 | /** |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 322 | * Returns the size of the attrib type in bytes. |
| 323 | * This was moved from include/private/GrTypesPriv.h in service of Skia dependents that build |
| 324 | * with C++11. |
| 325 | */ |
| 326 | static constexpr inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) { |
| 327 | switch (type) { |
| 328 | case kFloat_GrVertexAttribType: |
| 329 | return sizeof(float); |
| 330 | case kFloat2_GrVertexAttribType: |
| 331 | return 2 * sizeof(float); |
| 332 | case kFloat3_GrVertexAttribType: |
| 333 | return 3 * sizeof(float); |
| 334 | case kFloat4_GrVertexAttribType: |
| 335 | return 4 * sizeof(float); |
| 336 | case kHalf_GrVertexAttribType: |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 337 | return sizeof(uint16_t); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 338 | case kHalf2_GrVertexAttribType: |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 339 | return 2 * sizeof(uint16_t); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 340 | case kHalf4_GrVertexAttribType: |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 341 | return 4 * sizeof(uint16_t); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 342 | case kInt2_GrVertexAttribType: |
| 343 | return 2 * sizeof(int32_t); |
| 344 | case kInt3_GrVertexAttribType: |
| 345 | return 3 * sizeof(int32_t); |
| 346 | case kInt4_GrVertexAttribType: |
| 347 | return 4 * sizeof(int32_t); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 348 | case kByte_GrVertexAttribType: |
| 349 | return 1 * sizeof(char); |
| 350 | case kByte2_GrVertexAttribType: |
| 351 | return 2 * sizeof(char); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 352 | case kByte4_GrVertexAttribType: |
| 353 | return 4 * sizeof(char); |
| 354 | case kUByte_GrVertexAttribType: |
| 355 | return 1 * sizeof(char); |
| 356 | case kUByte2_GrVertexAttribType: |
| 357 | return 2 * sizeof(char); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 358 | case kUByte4_GrVertexAttribType: |
| 359 | return 4 * sizeof(char); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 360 | case kUByte_norm_GrVertexAttribType: |
| 361 | return 1 * sizeof(char); |
| 362 | case kUByte4_norm_GrVertexAttribType: |
| 363 | return 4 * sizeof(char); |
| 364 | case kShort2_GrVertexAttribType: |
| 365 | return 2 * sizeof(int16_t); |
Brian Osman | a5c578f | 2018-09-19 14:19:02 -0400 | [diff] [blame] | 366 | case kShort4_GrVertexAttribType: |
| 367 | return 4 * sizeof(int16_t); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 368 | case kUShort2_GrVertexAttribType: // fall through |
| 369 | case kUShort2_norm_GrVertexAttribType: |
| 370 | return 2 * sizeof(uint16_t); |
| 371 | case kInt_GrVertexAttribType: |
| 372 | return sizeof(int32_t); |
| 373 | case kUint_GrVertexAttribType: |
| 374 | return sizeof(uint32_t); |
Robert Phillips | fe18de5 | 2019-06-06 17:21:50 -0400 | [diff] [blame] | 375 | case kUShort_norm_GrVertexAttribType: |
| 376 | return sizeof(uint16_t); |
Robert Phillips | 66a4603 | 2019-06-18 08:00:42 -0400 | [diff] [blame] | 377 | case kUShort4_norm_GrVertexAttribType: |
| 378 | return 4 * sizeof(uint16_t); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 379 | } |
| 380 | // GCC fails because SK_ABORT evaluates to non constexpr. clang and cl.exe think this is |
| 381 | // unreachable and don't complain. |
| 382 | #if defined(__clang__) || !defined(__GNUC__) |
| 383 | SK_ABORT("Unsupported type conversion"); |
| 384 | #endif |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | constexpr size_t GrPrimitiveProcessor::Attribute::size() const { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 389 | return GrVertexAttribTypeSize(fCPUType); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 390 | } |
| 391 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 392 | #endif |