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