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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/GrTypesPriv.h" |
| 12 | #include "include/private/SkTArray.h" |
| 13 | #include "include/private/SkTo.h" |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 14 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 15 | class GrCaps; |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 16 | class GrProgramInfo; |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 17 | class GrRenderTarget; |
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 | |
Robert Phillips | 373bda6 | 2019-11-12 13:30:05 -0500 | [diff] [blame] | 20 | /** This class is used to generate a generic program cache key. The Dawn, Metal and Vulkan |
| 21 | * backends derive backend-specific versions which add additional information. |
| 22 | */ |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 23 | class GrProgramDesc { |
| 24 | public: |
Brian Osman | 9b510a3 | 2021-02-26 14:29:39 -0500 | [diff] [blame] | 25 | GrProgramDesc(const GrProgramDesc& other) = default; |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 26 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 27 | bool isValid() const { return !fKey.empty(); } |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 28 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 29 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| 30 | const uint32_t* asKey() const { |
Brian Osman | acf2650 | 2021-03-03 13:35:04 +0000 | [diff] [blame] | 31 | return reinterpret_cast<const uint32_t*>(fKey.begin()); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 34 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. |
| 35 | uint32_t keyLength() const { |
Brian Osman | acf2650 | 2021-03-03 13:35:04 +0000 | [diff] [blame] | 36 | SkASSERT(0 == (fKey.count() % 4)); |
| 37 | return fKey.count(); |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 38 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 39 | |
Brian Osman | acf2650 | 2021-03-03 13:35:04 +0000 | [diff] [blame] | 40 | GrProgramDesc& operator= (const GrProgramDesc& other) { |
| 41 | uint32_t keyLength = other.keyLength(); |
| 42 | fKey.reset(SkToInt(keyLength)); |
| 43 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
| 44 | fInitialKeyLength = other.fInitialKeyLength; |
| 45 | return *this; |
| 46 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 47 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 48 | bool operator== (const GrProgramDesc& that) const { |
Brian Osman | acf2650 | 2021-03-03 13:35:04 +0000 | [diff] [blame] | 49 | if (this->keyLength() != that.keyLength()) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | SkASSERT(SkIsAlign4(this->keyLength())); |
| 54 | int l = this->keyLength() >> 2; |
| 55 | const uint32_t* aKey = this->asKey(); |
| 56 | const uint32_t* bKey = that.asKey(); |
| 57 | for (int i = 0; i < l; ++i) { |
| 58 | if (aKey[i] != bKey[i]) { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | return true; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool operator!= (const GrProgramDesc& other) const { |
| 66 | return !(*this == other); |
| 67 | } |
| 68 | |
Brian Osman | 9b510a3 | 2021-02-26 14:29:39 -0500 | [diff] [blame] | 69 | uint32_t initialKeyLength() const { return fInitialKeyLength; } |
Robert Phillips | c15e890 | 2019-11-26 14:26:36 -0500 | [diff] [blame] | 70 | |
Robert Phillips | d5c1f34 | 2019-10-14 09:50:05 -0400 | [diff] [blame] | 71 | protected: |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 72 | friend class GrDawnCaps; |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 73 | friend class GrD3DCaps; |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 74 | friend class GrGLCaps; |
| 75 | friend class GrMockCaps; |
| 76 | friend class GrMtlCaps; |
| 77 | friend class GrVkCaps; |
| 78 | |
| 79 | friend class GrGLGpu; // for ProgramCache to access BuildFromData |
| 80 | |
| 81 | // Creates an uninitialized key that must be populated by Build |
| 82 | GrProgramDesc() {} |
| 83 | |
| 84 | /** |
| 85 | * Builds a program descriptor. |
| 86 | * |
| 87 | * @param desc The built descriptor |
| 88 | * @param renderTarget The target of the draw |
| 89 | * @param programInfo Program information need to build the key |
| 90 | * @param caps the caps |
| 91 | **/ |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame] | 92 | static bool Build(GrProgramDesc*, GrRenderTarget*, const GrProgramInfo&, const GrCaps&); |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 93 | |
John Stiles | d3feb6f | 2020-07-23 18:18:12 -0400 | [diff] [blame] | 94 | // 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] | 95 | static bool BuildFromData(GrProgramDesc* desc, const void* keyData, size_t keyLength) { |
| 96 | if (!SkTFitsIn<int>(keyLength)) { |
| 97 | return false; |
| 98 | } |
Brian Osman | acf2650 | 2021-03-03 13:35:04 +0000 | [diff] [blame] | 99 | desc->fKey.reset(SkToInt(keyLength)); |
| 100 | memcpy(desc->fKey.begin(), keyData, keyLength); |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 101 | return true; |
| 102 | } |
| 103 | |
Brian Osman | acf2650 | 2021-03-03 13:35:04 +0000 | [diff] [blame] | 104 | enum { |
| 105 | kHeaderSize = 4, // "header" in ::Build |
| 106 | kMaxPreallocProcessors = 8, |
| 107 | kIntsPerProcessor = 4, // This is an overestimate of the average effect key size. |
| 108 | kPreAllocSize = kHeaderSize + |
| 109 | kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor, |
| 110 | }; |
| 111 | |
| 112 | SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 113 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 114 | private: |
Brian Osman | acf2650 | 2021-03-03 13:35:04 +0000 | [diff] [blame] | 115 | SkSTArray<kPreAllocSize, uint8_t, true> fKey; |
Brian Osman | 9b510a3 | 2021-02-26 14:29:39 -0500 | [diff] [blame] | 116 | uint32_t fInitialKeyLength = 0; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | #endif |