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: |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame^] | 25 | bool isValid() const { return !fKey.empty(); } |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 26 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 27 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| 28 | const uint32_t* asKey() const { |
| 29 | return reinterpret_cast<const uint32_t*>(fKey.begin()); |
| 30 | } |
| 31 | |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 32 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. |
| 33 | uint32_t keyLength() const { |
| 34 | SkASSERT(0 == (fKey.count() % 4)); |
| 35 | return fKey.count(); |
| 36 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 37 | |
| 38 | GrProgramDesc& operator= (const GrProgramDesc& other) { |
bsalomon | ccb328d | 2014-12-11 13:31:06 -0800 | [diff] [blame] | 39 | uint32_t keyLength = other.keyLength(); |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 40 | fKey.reset(SkToInt(keyLength)); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 41 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
| 42 | return *this; |
| 43 | } |
| 44 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 45 | bool operator== (const GrProgramDesc& that) const { |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 46 | if (this->keyLength() != that.keyLength()) { |
| 47 | return false; |
| 48 | } |
| 49 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 50 | SkASSERT(SkIsAlign4(this->keyLength())); |
| 51 | int l = this->keyLength() >> 2; |
| 52 | const uint32_t* aKey = this->asKey(); |
| 53 | const uint32_t* bKey = that.asKey(); |
| 54 | for (int i = 0; i < l; ++i) { |
| 55 | if (aKey[i] != bKey[i]) { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | return true; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool operator!= (const GrProgramDesc& other) const { |
| 63 | return !(*this == other); |
| 64 | } |
| 65 | |
Robert Phillips | c15e890 | 2019-11-26 14:26:36 -0500 | [diff] [blame] | 66 | uint32_t initialKeyLength() const { return this->header().fInitialKeyLength; } |
| 67 | |
Robert Phillips | d5c1f34 | 2019-10-14 09:50:05 -0400 | [diff] [blame] | 68 | protected: |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame^] | 69 | friend class GrDawnCaps; |
| 70 | friend class GrGLCaps; |
| 71 | friend class GrMockCaps; |
| 72 | friend class GrMtlCaps; |
| 73 | friend class GrVkCaps; |
| 74 | |
| 75 | friend class GrGLGpu; // for ProgramCache to access BuildFromData |
| 76 | |
| 77 | // Creates an uninitialized key that must be populated by Build |
| 78 | GrProgramDesc() {} |
| 79 | |
| 80 | /** |
| 81 | * Builds a program descriptor. |
| 82 | * |
| 83 | * @param desc The built descriptor |
| 84 | * @param renderTarget The target of the draw |
| 85 | * @param programInfo Program information need to build the key |
| 86 | * @param caps the caps |
| 87 | **/ |
| 88 | static bool Build(GrProgramDesc*, const GrRenderTarget*, const GrProgramInfo&, const GrCaps&); |
| 89 | |
| 90 | // This is strictly an OpenGL call since the other backends have additional data in their |
| 91 | // keys |
| 92 | static bool BuildFromData(GrProgramDesc* desc, const void* keyData, size_t keyLength) { |
| 93 | if (!SkTFitsIn<int>(keyLength)) { |
| 94 | return false; |
| 95 | } |
| 96 | desc->fKey.reset(SkToInt(keyLength)); |
| 97 | memcpy(desc->fKey.begin(), keyData, keyLength); |
| 98 | return true; |
| 99 | } |
| 100 | |
Robert Phillips | 373bda6 | 2019-11-12 13:30:05 -0500 | [diff] [blame] | 101 | // TODO: this should be removed and converted to just data added to the key |
Robert Phillips | d5c1f34 | 2019-10-14 09:50:05 -0400 | [diff] [blame] | 102 | struct KeyHeader { |
| 103 | // Set to uniquely identify any swizzling of the shader's output color(s). |
Greg Daniel | f259b8b | 2019-02-14 09:03:43 -0500 | [diff] [blame] | 104 | uint16_t fOutputSwizzle; |
Chris Dalton | 535ba8d | 2018-02-20 09:51:59 -0700 | [diff] [blame] | 105 | uint8_t fColorFragmentProcessorCnt; // Can be packed into 4 bits if required. |
| 106 | uint8_t fCoverageFragmentProcessorCnt; |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 107 | // Set to uniquely identify the rt's origin, or 0 if the shader does not require this info. |
Robert Phillips | c15e890 | 2019-11-26 14:26:36 -0500 | [diff] [blame] | 108 | uint32_t fSurfaceOriginKey : 2; |
| 109 | uint32_t fProcessorFeatures : 1; |
| 110 | uint32_t fSnapVerticesToPixelCenters : 1; |
| 111 | uint32_t fHasPointSize : 1; |
| 112 | // This is the key size (in bytes) after core key construction. It doesn't include any |
| 113 | // portions added by the platform-specific backends. |
| 114 | uint32_t fInitialKeyLength : 27; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 115 | }; |
Robert Phillips | c15e890 | 2019-11-26 14:26:36 -0500 | [diff] [blame] | 116 | GR_STATIC_ASSERT(sizeof(KeyHeader) == 8); |
| 117 | |
| 118 | const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 119 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 120 | template<typename T, size_t OFFSET> T* atOffset() { |
| 121 | return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 122 | } |
| 123 | |
Robert Phillips | c15e890 | 2019-11-26 14:26:36 -0500 | [diff] [blame] | 124 | template<typename T, size_t OFFSET> const T* atOffset() const { |
| 125 | return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 126 | } |
| 127 | |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 128 | // The key, stored in fKey, is composed of two parts: |
| 129 | // 1. Header struct defined above. |
| 130 | // 2. A Backend specific payload which includes the per-processor keys. |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 131 | enum KeyOffsets { |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 132 | kHeaderOffset = 0, |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 133 | kHeaderSize = SkAlign4(sizeof(KeyHeader)), |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 134 | // This is the offset into the backenend specific part of the key, which includes |
| 135 | // per-processor keys. |
| 136 | kProcessorKeysOffset = kHeaderOffset + kHeaderSize, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | enum { |
| 140 | kMaxPreallocProcessors = 8, |
| 141 | kIntsPerProcessor = 4, // This is an overestimate of the average effect key size. |
| 142 | kPreAllocSize = kHeaderOffset + kHeaderSize + |
| 143 | kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor, |
| 144 | }; |
| 145 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 146 | SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 147 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 148 | private: |
| 149 | SkSTArray<kPreAllocSize, uint8_t, true> fKey; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | #endif |