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 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 15 | class GrGLGpu; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 16 | |
| 17 | /** This class describes a program to generate. It also serves as a program cache key. Very little |
| 18 | of this is GL-specific. The GL-specific parts could be factored out into a subclass. */ |
| 19 | class GrProgramDesc { |
| 20 | public: |
| 21 | // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc() |
| 22 | GrProgramDesc() {} |
| 23 | |
| 24 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| 25 | const uint32_t* asKey() const { |
| 26 | return reinterpret_cast<const uint32_t*>(fKey.begin()); |
| 27 | } |
| 28 | |
| 29 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two |
| 30 | // keys the size of either key can be used with memcmp() since the lengths themselves begin the |
| 31 | // keys and thus the memcmp will exit early if the keys are of different lengths. |
| 32 | uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); } |
| 33 | |
| 34 | // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache. |
| 35 | uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); } |
| 36 | |
| 37 | GrProgramDesc& operator= (const GrProgramDesc& other) { |
bsalomon | ccb328d | 2014-12-11 13:31:06 -0800 | [diff] [blame] | 38 | uint32_t keyLength = other.keyLength(); |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 39 | fKey.reset(SkToInt(keyLength)); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 40 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
| 41 | return *this; |
| 42 | } |
| 43 | |
| 44 | bool operator== (const GrProgramDesc& other) const { |
| 45 | // The length is masked as a hint to the compiler that the address will be 4 byte aligned. |
| 46 | return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3); |
| 47 | } |
| 48 | |
| 49 | bool operator!= (const GrProgramDesc& other) const { |
| 50 | return !(*this == other); |
| 51 | } |
| 52 | |
| 53 | static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) { |
| 54 | return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0; |
| 55 | } |
| 56 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 57 | struct KeyHeader { |
| 58 | uint8_t fDstReadKey; // set by GrGLShaderBuilder if there |
| 59 | // are effects that must read the dst. |
| 60 | // Otherwise, 0. |
| 61 | uint8_t fFragPosKey; // set by GrGLShaderBuilder if there are |
| 62 | // effects that read the fragment position. |
| 63 | // Otherwise, 0. |
| 64 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 65 | int8_t fColorEffectCnt; |
| 66 | int8_t fCoverageEffectCnt; |
| 67 | }; |
| 68 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 69 | int numColorEffects() const { |
| 70 | return this->header().fColorEffectCnt; |
| 71 | } |
| 72 | |
| 73 | int numCoverageEffects() const { |
| 74 | return this->header().fCoverageEffectCnt; |
| 75 | } |
| 76 | |
| 77 | int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); } |
| 78 | |
| 79 | // This should really only be used internally, base classes should return their own headers |
| 80 | const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); } |
| 81 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 82 | // A struct to communicate descriptor information to the program descriptor builder |
| 83 | struct DescInfo { |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 84 | bool operator==(const DescInfo& that) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 85 | return fReadsDst == that.fReadsDst && |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 86 | fReadsFragPosition == that.fReadsFragPosition && |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 87 | fRequiresLocalCoordAttrib == that.fRequiresLocalCoordAttrib; |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 88 | } |
| 89 | bool operator!=(const DescInfo& that) const { return !(*this == that); }; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 90 | |
| 91 | // These flags give aggregated info on the processor stages that are used when building |
| 92 | // programs. |
| 93 | bool fReadsDst; |
| 94 | bool fReadsFragPosition; |
| 95 | bool fRequiresLocalCoordAttrib; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | private: |
| 99 | template<typename T, size_t OFFSET> T* atOffset() { |
| 100 | return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 101 | } |
| 102 | |
| 103 | template<typename T, size_t OFFSET> const T* atOffset() const { |
| 104 | return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 105 | } |
| 106 | |
| 107 | void finalize() { |
| 108 | int keyLength = fKey.count(); |
| 109 | SkASSERT(0 == (keyLength % 4)); |
| 110 | *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength); |
| 111 | |
| 112 | uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>(); |
| 113 | *checksum = 0; |
| 114 | *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength); |
| 115 | } |
| 116 | |
| 117 | // The key, stored in fKey, is composed of four parts: |
| 118 | // 1. uint32_t for total key length. |
| 119 | // 2. uint32_t for a checksum. |
| 120 | // 3. Header struct defined above. Also room for extensions to the header |
| 121 | // 4. A Backend specific payload. Room is preallocated for this |
| 122 | enum KeyOffsets { |
| 123 | // Part 1. |
| 124 | kLengthOffset = 0, |
| 125 | // Part 2. |
| 126 | kChecksumOffset = kLengthOffset + sizeof(uint32_t), |
| 127 | // Part 3. |
| 128 | kHeaderOffset = kChecksumOffset + sizeof(uint32_t), |
| 129 | kHeaderSize = SkAlign4(2 * sizeof(KeyHeader)), |
| 130 | }; |
| 131 | |
| 132 | enum { |
| 133 | kMaxPreallocProcessors = 8, |
| 134 | kIntsPerProcessor = 4, // This is an overestimate of the average effect key size. |
| 135 | kPreAllocSize = kHeaderOffset + kHeaderSize + |
| 136 | kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor, |
| 137 | }; |
| 138 | |
| 139 | SkSTArray<kPreAllocSize, uint8_t, true> fKey; |
| 140 | |
| 141 | friend class GrGLProgramDescBuilder; |
| 142 | }; |
| 143 | |
| 144 | #endif |