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 | |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 11 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/private/GrTypesPriv.h" |
| 13 | #include "include/private/SkTArray.h" |
| 14 | #include "include/private/SkTo.h" |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 15 | |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 16 | #include <limits.h> |
| 17 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 18 | class GrCaps; |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 19 | class GrProgramInfo; |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 20 | class GrRenderTarget; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 21 | class GrShaderCaps; |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 22 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 23 | class GrProcessorKeyBuilder { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 24 | public: |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 25 | GrProcessorKeyBuilder() = default; |
| 26 | GrProcessorKeyBuilder(const GrProcessorKeyBuilder& other) = default; |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 27 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 28 | void reset() { *this = GrProcessorKeyBuilder{}; } |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 29 | |
| 30 | void addBits(uint32_t numBits, uint32_t val, const char* label) { |
| 31 | SkASSERT(numBits > 0 && numBits <= 32); |
| 32 | SkASSERT(numBits == 32 || (val < (1u << numBits))); |
| 33 | |
| 34 | SkDEBUGCODE(fDescription.appendf("%s: %u\n", label, val);) |
| 35 | |
| 36 | fCurValue |= (val << fBitsUsed); |
| 37 | fBitsUsed += numBits; |
| 38 | |
| 39 | if (fBitsUsed >= 32) { |
| 40 | // Overflow, start a new working value |
| 41 | fData.push_back(fCurValue); |
| 42 | uint32_t excess = fBitsUsed - 32; |
| 43 | fCurValue = excess ? (val >> (numBits - excess)) : 0; |
| 44 | fBitsUsed = excess; |
| 45 | } |
| 46 | |
| 47 | SkASSERT(fCurValue < (1u << fBitsUsed)); |
| 48 | } |
| 49 | |
| 50 | void addBytes(uint32_t numBytes, const void* data, const char* label) { |
| 51 | // TODO: Make this smarter/faster? |
| 52 | const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
| 53 | for (; numBytes --> 0; bytes++) { |
| 54 | this->addBits(8, *bytes, label); |
| 55 | } |
| 56 | } |
| 57 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 58 | void addBool(bool b, const char* label) { |
| 59 | this->addBits(1, b, label); |
| 60 | } |
| 61 | |
| 62 | void add32(uint32_t v, const char* label = "unknown") { |
| 63 | this->addBits(32, v, label); |
| 64 | } |
| 65 | |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 66 | template <typename StringFunc> |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 67 | void appendComment(StringFunc&& sf) { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 68 | #ifdef SK_DEBUG |
| 69 | fDescription.append(sf()); |
| 70 | fDescription.append("\n"); |
| 71 | #endif |
| 72 | } |
| 73 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 74 | // Introduces a word-boundary in the key. Must be called before using the key with any cache, |
| 75 | // but can also be called to create a break between generic data and backend-specific data. |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 76 | void flush() { |
| 77 | if (fBitsUsed) { |
| 78 | fData.push_back(fCurValue); |
| 79 | fCurValue = 0; |
| 80 | fBitsUsed = 0; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | bool empty() const { return fData.empty() && !fBitsUsed; } |
| 85 | |
| 86 | const uint32_t* data() const { |
| 87 | SkASSERT(fBitsUsed == 0); // flush() must be called when construction is complete |
| 88 | return fData.begin(); |
| 89 | } |
| 90 | |
| 91 | size_t size() const { |
| 92 | return (fData.count() + (fBitsUsed ? 1 : 0)) * sizeof(uint32_t); |
| 93 | } |
| 94 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 95 | GrProcessorKeyBuilder& operator=(const GrProcessorKeyBuilder& other) = default; |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 96 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 97 | bool operator==(const GrProcessorKeyBuilder& that) const { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 98 | return fBitsUsed == that.fBitsUsed && |
| 99 | fCurValue == that.fCurValue && |
| 100 | fData == that.fData; |
| 101 | } |
| 102 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 103 | bool operator!= (const GrProcessorKeyBuilder& other) const { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 104 | return !(*this == other); |
| 105 | } |
| 106 | |
| 107 | void setData(const void* data, size_t length) { |
| 108 | SkASSERT(SkIsAlign4(length)); |
| 109 | fData.reset(length / 4); |
| 110 | memcpy(fData.begin(), data, length); |
| 111 | } |
| 112 | |
| 113 | SkString description() const { |
| 114 | #ifdef SK_DEBUG |
| 115 | return fDescription; |
| 116 | #else |
| 117 | return SkString{}; |
| 118 | #endif |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | enum { |
| 123 | kHeaderSize = 1, // "header" in ::Build |
| 124 | kMaxPreallocProcessors = 8, |
| 125 | kIntsPerProcessor = 4, // This is an overestimate of the average effect key size. |
| 126 | kPreAllocSize = kHeaderSize + |
| 127 | kMaxPreallocProcessors * kIntsPerProcessor, |
| 128 | }; |
| 129 | |
| 130 | SkSTArray<kPreAllocSize, uint32_t, true> fData; |
| 131 | uint32_t fCurValue = 0; |
| 132 | uint32_t fBitsUsed = 0; // ... in current value |
| 133 | |
| 134 | SkDEBUGCODE(SkString fDescription;) |
| 135 | }; |
| 136 | |
Robert Phillips | 373bda6 | 2019-11-12 13:30:05 -0500 | [diff] [blame] | 137 | /** This class is used to generate a generic program cache key. The Dawn, Metal and Vulkan |
| 138 | * backends derive backend-specific versions which add additional information. |
| 139 | */ |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 140 | class GrProgramDesc { |
| 141 | public: |
Brian Osman | 9b510a3 | 2021-02-26 14:29:39 -0500 | [diff] [blame] | 142 | GrProgramDesc(const GrProgramDesc& other) = default; |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 143 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 144 | bool isValid() const { return !fKey.empty(); } |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 145 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 146 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| 147 | const uint32_t* asKey() const { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 148 | return fKey.data(); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 151 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. |
| 152 | uint32_t keyLength() const { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 153 | SkASSERT(0 == (fKey.size() % 4)); |
| 154 | return fKey.size(); |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 155 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 156 | |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 157 | SkString description() const { return fKey.description(); } |
| 158 | |
| 159 | GrProgramDesc& operator= (const GrProgramDesc& other) = default; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 160 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 161 | bool operator== (const GrProgramDesc& that) const { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 162 | return this->fKey == that.fKey; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | bool operator!= (const GrProgramDesc& other) const { |
| 166 | return !(*this == other); |
| 167 | } |
| 168 | |
Brian Osman | 9b510a3 | 2021-02-26 14:29:39 -0500 | [diff] [blame] | 169 | uint32_t initialKeyLength() const { return fInitialKeyLength; } |
Robert Phillips | c15e890 | 2019-11-26 14:26:36 -0500 | [diff] [blame] | 170 | |
Robert Phillips | d5c1f34 | 2019-10-14 09:50:05 -0400 | [diff] [blame] | 171 | protected: |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 172 | friend class GrDawnCaps; |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 173 | friend class GrD3DCaps; |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 174 | friend class GrGLCaps; |
| 175 | friend class GrMockCaps; |
| 176 | friend class GrMtlCaps; |
| 177 | friend class GrVkCaps; |
| 178 | |
| 179 | friend class GrGLGpu; // for ProgramCache to access BuildFromData |
| 180 | |
| 181 | // Creates an uninitialized key that must be populated by Build |
| 182 | GrProgramDesc() {} |
| 183 | |
| 184 | /** |
| 185 | * Builds a program descriptor. |
| 186 | * |
| 187 | * @param desc The built descriptor |
| 188 | * @param renderTarget The target of the draw |
| 189 | * @param programInfo Program information need to build the key |
| 190 | * @param caps the caps |
| 191 | **/ |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame] | 192 | static bool Build(GrProgramDesc*, GrRenderTarget*, const GrProgramInfo&, const GrCaps&); |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 193 | |
John Stiles | d3feb6f | 2020-07-23 18:18:12 -0400 | [diff] [blame] | 194 | // This is strictly an OpenGL call since the other backends have additional data in their keys. |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 195 | static bool BuildFromData(GrProgramDesc* desc, const void* keyData, size_t keyLength) { |
| 196 | if (!SkTFitsIn<int>(keyLength)) { |
| 197 | return false; |
| 198 | } |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 199 | desc->fKey.setData(keyData, keyLength); |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 203 | GrProcessorKeyBuilder* key() { return &fKey; } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 204 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 205 | private: |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 206 | GrProcessorKeyBuilder fKey; |
Brian Osman | 9b510a3 | 2021-02-26 14:29:39 -0500 | [diff] [blame] | 207 | uint32_t fInitialKeyLength = 0; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | #endif |