joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 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 GrProgramDesc_DEFINED |
| 9 | #define GrProgramDesc_DEFINED |
| 10 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 11 | #include "GrColor.h" |
| 12 | #include "GrTypesPriv.h" |
| 13 | #include "SkChecksum.h" |
| 14 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 15 | /** This class describes a program to generate. It also serves as a program cache key. Very little |
| 16 | of this is GL-specific. The GL-specific parts could be factored out into a subclass. */ |
| 17 | class GrProgramDesc { |
| 18 | public: |
| 19 | // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc() |
| 20 | GrProgramDesc() {} |
| 21 | |
| 22 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| 23 | const uint32_t* asKey() const { |
| 24 | return reinterpret_cast<const uint32_t*>(fKey.begin()); |
| 25 | } |
| 26 | |
| 27 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two |
| 28 | // keys the size of either key can be used with memcmp() since the lengths themselves begin the |
| 29 | // keys and thus the memcmp will exit early if the keys are of different lengths. |
| 30 | uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); } |
| 31 | |
| 32 | // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache. |
| 33 | uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); } |
| 34 | |
| 35 | GrProgramDesc& operator= (const GrProgramDesc& other) { |
bsalomon | ccb328d | 2014-12-11 13:31:06 -0800 | [diff] [blame] | 36 | uint32_t keyLength = other.keyLength(); |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 37 | fKey.reset(SkToInt(keyLength)); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 38 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
| 39 | return *this; |
| 40 | } |
| 41 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 42 | bool operator== (const GrProgramDesc& that) const { |
| 43 | SkASSERT(SkIsAlign4(this->keyLength())); |
| 44 | int l = this->keyLength() >> 2; |
| 45 | const uint32_t* aKey = this->asKey(); |
| 46 | const uint32_t* bKey = that.asKey(); |
| 47 | for (int i = 0; i < l; ++i) { |
| 48 | if (aKey[i] != bKey[i]) { |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | return true; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | bool operator!= (const GrProgramDesc& other) const { |
| 56 | return !(*this == other); |
| 57 | } |
| 58 | |
| 59 | static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) { |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 60 | SkASSERT(SkIsAlign4(a.keyLength())); |
| 61 | int l = a.keyLength() >> 2; |
| 62 | const uint32_t* aKey = a.asKey(); |
| 63 | const uint32_t* bKey = b.asKey(); |
| 64 | for (int i = 0; i < l; ++i) { |
| 65 | if (aKey[i] != bKey[i]) { |
| 66 | return aKey[i] < bKey[i] ? true : false; |
| 67 | } |
| 68 | } |
| 69 | return false; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 70 | } |
| 71 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 72 | struct KeyHeader { |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 73 | // Set to uniquely identify the rt's origin, or 0 if the shader does not require this info. |
| 74 | uint8_t fSurfaceOriginKey; |
| 75 | // Set to uniquely identify the sample pattern, or 0 if the shader doesn't use sample |
| 76 | // locations. |
| 77 | uint8_t fSamplePatternKey; |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 78 | // Set to uniquely idenitify any swizzling of the shader's output color(s). |
| 79 | uint8_t fOutputSwizzle; |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 80 | uint8_t fSnapVerticesToPixelCenters; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 81 | int8_t fColorEffectCnt; |
| 82 | int8_t fCoverageEffectCnt; |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 83 | uint8_t fIgnoresCoverage; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 86 | int numColorEffects() const { |
| 87 | return this->header().fColorEffectCnt; |
| 88 | } |
| 89 | |
| 90 | int numCoverageEffects() const { |
| 91 | return this->header().fCoverageEffectCnt; |
| 92 | } |
| 93 | |
| 94 | int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); } |
| 95 | |
| 96 | // This should really only be used internally, base classes should return their own headers |
| 97 | const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); } |
| 98 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 99 | protected: |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 100 | template<typename T, size_t OFFSET> T* atOffset() { |
| 101 | return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 102 | } |
| 103 | |
| 104 | template<typename T, size_t OFFSET> const T* atOffset() const { |
| 105 | return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 106 | } |
| 107 | |
| 108 | void finalize() { |
| 109 | int keyLength = fKey.count(); |
| 110 | SkASSERT(0 == (keyLength % 4)); |
| 111 | *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength); |
| 112 | |
| 113 | uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>(); |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 114 | *checksum = 0; // We'll hash through these bytes, so make sure they're initialized. |
| 115 | *checksum = SkChecksum::Murmur3(fKey.begin(), keyLength); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // The key, stored in fKey, is composed of four parts: |
| 119 | // 1. uint32_t for total key length. |
| 120 | // 2. uint32_t for a checksum. |
| 121 | // 3. Header struct defined above. Also room for extensions to the header |
| 122 | // 4. A Backend specific payload. Room is preallocated for this |
| 123 | enum KeyOffsets { |
| 124 | // Part 1. |
| 125 | kLengthOffset = 0, |
| 126 | // Part 2. |
| 127 | kChecksumOffset = kLengthOffset + sizeof(uint32_t), |
| 128 | // Part 3. |
| 129 | kHeaderOffset = kChecksumOffset + sizeof(uint32_t), |
| 130 | kHeaderSize = SkAlign4(2 * sizeof(KeyHeader)), |
| 131 | }; |
| 132 | |
| 133 | enum { |
| 134 | kMaxPreallocProcessors = 8, |
| 135 | kIntsPerProcessor = 4, // This is an overestimate of the average effect key size. |
| 136 | kPreAllocSize = kHeaderOffset + kHeaderSize + |
| 137 | kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor, |
| 138 | }; |
| 139 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 140 | SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; } |
| 141 | const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 142 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 143 | private: |
| 144 | SkSTArray<kPreAllocSize, uint8_t, true> fKey; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | #endif |