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 | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 25 | GrProcessorKeyBuilder(SkTArray<uint32_t, true>* data) : fData(data) {} |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 26 | |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 27 | virtual ~GrProcessorKeyBuilder() { |
| 28 | // Ensure that flush was called before we went out of scope |
| 29 | SkASSERT(fBitsUsed == 0); |
| 30 | } |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 31 | |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 32 | virtual void addBits(uint32_t numBits, uint32_t val, const char* label) { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 33 | SkASSERT(numBits > 0 && numBits <= 32); |
| 34 | SkASSERT(numBits == 32 || (val < (1u << numBits))); |
| 35 | |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 36 | fCurValue |= (val << fBitsUsed); |
| 37 | fBitsUsed += numBits; |
| 38 | |
| 39 | if (fBitsUsed >= 32) { |
| 40 | // Overflow, start a new working value |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 41 | fData->push_back(fCurValue); |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 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) { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 51 | const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
| 52 | for (; numBytes --> 0; bytes++) { |
| 53 | this->addBits(8, *bytes, label); |
| 54 | } |
| 55 | } |
| 56 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 57 | void addBool(bool b, const char* label) { |
| 58 | this->addBits(1, b, label); |
| 59 | } |
| 60 | |
| 61 | void add32(uint32_t v, const char* label = "unknown") { |
| 62 | this->addBits(32, v, label); |
| 63 | } |
| 64 | |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 65 | virtual void appendComment(const char* comment) {} |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 66 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 67 | // Introduces a word-boundary in the key. Must be called before using the key with any cache, |
| 68 | // 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] | 69 | void flush() { |
| 70 | if (fBitsUsed) { |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 71 | fData->push_back(fCurValue); |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 72 | fCurValue = 0; |
| 73 | fBitsUsed = 0; |
| 74 | } |
| 75 | } |
| 76 | |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 77 | private: |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 78 | SkTArray<uint32_t, true>* fData; |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 79 | uint32_t fCurValue = 0; |
| 80 | uint32_t fBitsUsed = 0; // ... in current value |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 81 | }; |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 82 | |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 83 | class GrProcessorStringKeyBuilder : public GrProcessorKeyBuilder { |
| 84 | public: |
| 85 | GrProcessorStringKeyBuilder(SkTArray<uint32_t, true>* data) : INHERITED(data) {} |
| 86 | |
| 87 | void addBits(uint32_t numBits, uint32_t val, const char* label) override { |
| 88 | INHERITED::addBits(numBits, val, label); |
| 89 | fDescription.appendf("%s: %u\n", label, val); |
| 90 | } |
| 91 | |
| 92 | void appendComment(const char* comment) override { |
| 93 | fDescription.appendf("%s\n", comment); |
| 94 | } |
| 95 | |
| 96 | SkString description() const { return fDescription; } |
| 97 | |
| 98 | private: |
| 99 | using INHERITED = GrProcessorKeyBuilder; |
| 100 | SkString fDescription; |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 101 | }; |
| 102 | |
Robert Phillips | 373bda6 | 2019-11-12 13:30:05 -0500 | [diff] [blame] | 103 | /** This class is used to generate a generic program cache key. The Dawn, Metal and Vulkan |
| 104 | * backends derive backend-specific versions which add additional information. |
| 105 | */ |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 106 | class GrProgramDesc { |
| 107 | public: |
Brian Osman | 9b510a3 | 2021-02-26 14:29:39 -0500 | [diff] [blame] | 108 | GrProgramDesc(const GrProgramDesc& other) = default; |
Kevin Lubick | c3aa2f0 | 2021-05-27 09:49:18 -0400 | [diff] [blame^] | 109 | GrProgramDesc& operator=(const GrProgramDesc &other) = default; |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 110 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 111 | bool isValid() const { return !fKey.empty(); } |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 112 | void reset() { *this = GrProgramDesc{}; } |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 113 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 114 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| 115 | const uint32_t* asKey() const { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 116 | return fKey.data(); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 119 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. |
| 120 | uint32_t keyLength() const { |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 121 | return fKey.size() * sizeof(uint32_t); |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 122 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 123 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 124 | bool operator== (const GrProgramDesc& that) const { |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 125 | return this->fKey == that.fKey; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | bool operator!= (const GrProgramDesc& other) const { |
| 129 | return !(*this == other); |
| 130 | } |
| 131 | |
Brian Osman | 9b510a3 | 2021-02-26 14:29:39 -0500 | [diff] [blame] | 132 | uint32_t initialKeyLength() const { return fInitialKeyLength; } |
Robert Phillips | c15e890 | 2019-11-26 14:26:36 -0500 | [diff] [blame] | 133 | |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 134 | // TODO(skia:11372): Incorporate this into caps interface (part of makeDesc, or a parallel |
| 135 | // function), so other backends can include their information in the description. |
Robert Phillips | ced5e83 | 2021-03-23 09:36:53 -0400 | [diff] [blame] | 136 | static SkString Describe(const GrProgramInfo&, const GrCaps&); |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 137 | |
Robert Phillips | d5c1f34 | 2019-10-14 09:50:05 -0400 | [diff] [blame] | 138 | protected: |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 139 | friend class GrDawnCaps; |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 140 | friend class GrD3DCaps; |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 141 | friend class GrGLCaps; |
| 142 | friend class GrMockCaps; |
| 143 | friend class GrMtlCaps; |
| 144 | friend class GrVkCaps; |
| 145 | |
| 146 | friend class GrGLGpu; // for ProgramCache to access BuildFromData |
Jim Van Verth | 7d195ff | 2021-03-09 14:59:58 -0500 | [diff] [blame] | 147 | friend class GrMtlResourceProvider; // for PipelineStateCache to access BuildFromData |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 148 | |
| 149 | // Creates an uninitialized key that must be populated by Build |
| 150 | GrProgramDesc() {} |
| 151 | |
| 152 | /** |
| 153 | * Builds a program descriptor. |
| 154 | * |
| 155 | * @param desc The built descriptor |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 156 | * @param programInfo Program information need to build the key |
| 157 | * @param caps the caps |
| 158 | **/ |
Robert Phillips | ced5e83 | 2021-03-23 09:36:53 -0400 | [diff] [blame] | 159 | static void Build(GrProgramDesc*, const GrProgramInfo&, const GrCaps&); |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 160 | |
John Stiles | d3feb6f | 2020-07-23 18:18:12 -0400 | [diff] [blame] | 161 | // 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] | 162 | static bool BuildFromData(GrProgramDesc* desc, const void* keyData, size_t keyLength) { |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 163 | if (!SkTFitsIn<int>(keyLength) || !SkIsAlign4(keyLength)) { |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 164 | return false; |
| 165 | } |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 166 | desc->fKey.reset(keyLength / 4); |
| 167 | memcpy(desc->fKey.begin(), keyData, keyLength); |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 171 | enum { |
| 172 | kHeaderSize = 1, // "header" in ::Build |
| 173 | kMaxPreallocProcessors = 8, |
| 174 | kIntsPerProcessor = 4, // This is an overestimate of the average effect key size. |
| 175 | kPreAllocSize = kHeaderSize + |
| 176 | kMaxPreallocProcessors * kIntsPerProcessor, |
| 177 | }; |
| 178 | |
| 179 | using KeyType = SkSTArray<kPreAllocSize, uint32_t, true>; |
| 180 | |
| 181 | KeyType* key() { return &fKey; } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 182 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 183 | private: |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 184 | SkSTArray<kPreAllocSize, uint32_t, true> fKey; |
Brian Osman | 9b510a3 | 2021-02-26 14:29:39 -0500 | [diff] [blame] | 185 | uint32_t fInitialKeyLength = 0; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | #endif |