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