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" |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 13 | #include "SkOpts.h" |
| 14 | #include "SkTArray.h" |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 15 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 16 | class GrGLSLCaps; |
| 17 | class GrPipeline; |
| 18 | class GrPrimitiveProcessor; |
| 19 | |
| 20 | /** This class describes a program to generate. It also serves as a program cache key */ |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 21 | class GrProgramDesc { |
| 22 | public: |
| 23 | // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc() |
| 24 | GrProgramDesc() {} |
| 25 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Builds a program descriptor. Before the descriptor can be used, the client must call finalize |
| 28 | * on the returned GrProgramDesc. |
| 29 | * |
| 30 | * @param GrPrimitiveProcessor The geometry |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame^] | 31 | * @param hasPointSize Controls whether the shader will output a point size. |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 32 | * @param GrPipeline The optimized drawstate. The descriptor will represent a program |
| 33 | * which this optstate can use to draw with. The optstate contains |
| 34 | * general draw information, as well as the specific color, geometry, |
| 35 | * and coverage stages which will be used to generate the GL Program for |
| 36 | * this optstate. |
| 37 | * @param GrGLSLCaps Capabilities of the GLSL backend. |
| 38 | * @param GrProgramDesc The built and finalized descriptor |
| 39 | **/ |
| 40 | static bool Build(GrProgramDesc*, |
| 41 | const GrPrimitiveProcessor&, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame^] | 42 | bool hasPointSize, |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 43 | const GrPipeline&, |
| 44 | const GrGLSLCaps&); |
| 45 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 46 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| 47 | const uint32_t* asKey() const { |
| 48 | return reinterpret_cast<const uint32_t*>(fKey.begin()); |
| 49 | } |
| 50 | |
| 51 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two |
| 52 | // keys the size of either key can be used with memcmp() since the lengths themselves begin the |
| 53 | // keys and thus the memcmp will exit early if the keys are of different lengths. |
| 54 | uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); } |
| 55 | |
| 56 | // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache. |
| 57 | uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); } |
| 58 | |
| 59 | GrProgramDesc& operator= (const GrProgramDesc& other) { |
bsalomon | ccb328d | 2014-12-11 13:31:06 -0800 | [diff] [blame] | 60 | uint32_t keyLength = other.keyLength(); |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 61 | fKey.reset(SkToInt(keyLength)); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 62 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
| 63 | return *this; |
| 64 | } |
| 65 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 66 | bool operator== (const GrProgramDesc& that) const { |
| 67 | SkASSERT(SkIsAlign4(this->keyLength())); |
| 68 | int l = this->keyLength() >> 2; |
| 69 | const uint32_t* aKey = this->asKey(); |
| 70 | const uint32_t* bKey = that.asKey(); |
| 71 | for (int i = 0; i < l; ++i) { |
| 72 | if (aKey[i] != bKey[i]) { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | return true; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | bool operator!= (const GrProgramDesc& other) const { |
| 80 | return !(*this == other); |
| 81 | } |
| 82 | |
| 83 | static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) { |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 84 | SkASSERT(SkIsAlign4(a.keyLength())); |
| 85 | int l = a.keyLength() >> 2; |
| 86 | const uint32_t* aKey = a.asKey(); |
| 87 | const uint32_t* bKey = b.asKey(); |
| 88 | for (int i = 0; i < l; ++i) { |
| 89 | if (aKey[i] != bKey[i]) { |
| 90 | return aKey[i] < bKey[i] ? true : false; |
| 91 | } |
| 92 | } |
| 93 | return false; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 94 | } |
| 95 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 96 | struct KeyHeader { |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 97 | // Set to uniquely identify the sample pattern, or 0 if the shader doesn't use sample |
| 98 | // locations. |
| 99 | uint8_t fSamplePatternKey; |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 100 | // Set to uniquely idenitify any swizzling of the shader's output color(s). |
| 101 | uint8_t fOutputSwizzle; |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame^] | 102 | uint8_t fColorFragmentProcessorCnt : 4; |
| 103 | uint8_t fCoverageFragmentProcessorCnt : 4; |
| 104 | // Set to uniquely identify the rt's origin, or 0 if the shader does not require this info. |
| 105 | uint8_t fSurfaceOriginKey : 2; |
| 106 | uint8_t fIgnoresCoverage : 1; |
| 107 | uint8_t fSnapVerticesToPixelCenters : 1; |
| 108 | uint8_t fHasPointSize : 1; |
| 109 | uint8_t fPad : 3; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 110 | }; |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame^] | 111 | GR_STATIC_ASSERT(sizeof(KeyHeader) == 4); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 112 | |
| 113 | // This should really only be used internally, base classes should return their own headers |
| 114 | const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); } |
| 115 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 116 | void finalize() { |
| 117 | int keyLength = fKey.count(); |
| 118 | SkASSERT(0 == (keyLength % 4)); |
| 119 | *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength); |
| 120 | |
| 121 | uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>(); |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 122 | *checksum = 0; // We'll hash through these bytes, so make sure they're initialized. |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 123 | *checksum = SkOpts::hash(fKey.begin(), keyLength); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 124 | } |
| 125 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 126 | protected: |
| 127 | template<typename T, size_t OFFSET> T* atOffset() { |
| 128 | return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 129 | } |
| 130 | |
| 131 | template<typename T, size_t OFFSET> const T* atOffset() const { |
| 132 | return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 133 | } |
| 134 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 135 | // The key, stored in fKey, is composed of four parts: |
| 136 | // 1. uint32_t for total key length. |
| 137 | // 2. uint32_t for a checksum. |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 138 | // 3. Header struct defined above. |
| 139 | // 4. A Backend specific payload which includes the per-processor keys. |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 140 | enum KeyOffsets { |
| 141 | // Part 1. |
| 142 | kLengthOffset = 0, |
| 143 | // Part 2. |
| 144 | kChecksumOffset = kLengthOffset + sizeof(uint32_t), |
| 145 | // Part 3. |
| 146 | kHeaderOffset = kChecksumOffset + sizeof(uint32_t), |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 147 | kHeaderSize = SkAlign4(sizeof(KeyHeader)), |
| 148 | // Part 4. |
| 149 | // This is the offset into the backenend specific part of the key, which includes |
| 150 | // per-processor keys. |
| 151 | kProcessorKeysOffset = kHeaderOffset + kHeaderSize, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | enum { |
| 155 | kMaxPreallocProcessors = 8, |
| 156 | kIntsPerProcessor = 4, // This is an overestimate of the average effect key size. |
| 157 | kPreAllocSize = kHeaderOffset + kHeaderSize + |
| 158 | kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor, |
| 159 | }; |
| 160 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 161 | SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; } |
| 162 | const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 163 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 164 | private: |
| 165 | SkSTArray<kPreAllocSize, uint8_t, true> fKey; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | #endif |