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" |
| 14 | #include "src/core/SkOpts.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 17 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 18 | class GrProgramInfo; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 19 | class GrShaderCaps; |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 20 | |
| 21 | /** 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] | 22 | class GrProgramDesc { |
| 23 | public: |
| 24 | // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc() |
| 25 | GrProgramDesc() {} |
| 26 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 27 | /** |
Robert Phillips | d5c1f34 | 2019-10-14 09:50:05 -0400 | [diff] [blame] | 28 | * Builds a program descriptor. Before the descriptor can be used, the client must call finalize |
| 29 | * on the filled in GrProgramDesc. |
| 30 | * |
| 31 | * @param desc The built and finalized descriptor |
| 32 | * @param renderTarget The target of the draw |
| 33 | * @param programInfo Program information need to build the key |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 34 | * @param caps the caps |
Robert Phillips | d5c1f34 | 2019-10-14 09:50:05 -0400 | [diff] [blame] | 35 | **/ |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 36 | static bool Build(GrProgramDesc*, const GrRenderTarget*, const GrProgramInfo&, const GrCaps&); |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 37 | |
Robert Phillips | d5c1f34 | 2019-10-14 09:50:05 -0400 | [diff] [blame] | 38 | // This is strictly an OpenGL call since the other backends have additional data in their |
| 39 | // keys |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 40 | static bool BuildFromData(GrProgramDesc* desc, const void* keyData, size_t keyLength) { |
| 41 | if (!SkTFitsIn<int>(keyLength)) { |
| 42 | return false; |
| 43 | } |
| 44 | desc->fKey.reset(SkToInt(keyLength)); |
| 45 | memcpy(desc->fKey.begin(), keyData, keyLength); |
| 46 | return true; |
| 47 | } |
| 48 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 49 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| 50 | const uint32_t* asKey() const { |
| 51 | return reinterpret_cast<const uint32_t*>(fKey.begin()); |
| 52 | } |
| 53 | |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 54 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. |
| 55 | uint32_t keyLength() const { |
| 56 | SkASSERT(0 == (fKey.count() % 4)); |
| 57 | return fKey.count(); |
| 58 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 59 | |
| 60 | GrProgramDesc& operator= (const GrProgramDesc& other) { |
bsalomon | ccb328d | 2014-12-11 13:31:06 -0800 | [diff] [blame] | 61 | uint32_t keyLength = other.keyLength(); |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 62 | fKey.reset(SkToInt(keyLength)); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 63 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
| 64 | return *this; |
| 65 | } |
| 66 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 67 | bool operator== (const GrProgramDesc& that) const { |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 68 | if (this->keyLength() != that.keyLength()) { |
| 69 | return false; |
| 70 | } |
| 71 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 72 | SkASSERT(SkIsAlign4(this->keyLength())); |
| 73 | int l = this->keyLength() >> 2; |
| 74 | const uint32_t* aKey = this->asKey(); |
| 75 | const uint32_t* bKey = that.asKey(); |
| 76 | for (int i = 0; i < l; ++i) { |
| 77 | if (aKey[i] != bKey[i]) { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | return true; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | bool operator!= (const GrProgramDesc& other) const { |
| 85 | return !(*this == other); |
| 86 | } |
| 87 | |
Robert Phillips | d5c1f34 | 2019-10-14 09:50:05 -0400 | [diff] [blame] | 88 | // TODO: remove this use of the header |
| 89 | bool hasPointSize() const { return this->header().fHasPointSize; } |
Robert Phillips | 4face83 | 2019-10-10 11:58:11 -0400 | [diff] [blame] | 90 | |
Robert Phillips | d5c1f34 | 2019-10-14 09:50:05 -0400 | [diff] [blame] | 91 | protected: |
| 92 | struct KeyHeader { |
| 93 | // 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] | 94 | uint16_t fOutputSwizzle; |
Chris Dalton | 535ba8d | 2018-02-20 09:51:59 -0700 | [diff] [blame] | 95 | uint8_t fColorFragmentProcessorCnt; // Can be packed into 4 bits if required. |
| 96 | uint8_t fCoverageFragmentProcessorCnt; |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 97 | // 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] | 98 | uint8_t fSurfaceOriginKey : 2; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 99 | uint8_t fProcessorFeatures : 1; |
Chris Dalton | 535ba8d | 2018-02-20 09:51:59 -0700 | [diff] [blame] | 100 | bool fSnapVerticesToPixelCenters : 1; |
| 101 | bool fHasPointSize : 1; |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 102 | uint8_t fPad : 3; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 103 | }; |
Greg Daniel | f259b8b | 2019-02-14 09:03:43 -0500 | [diff] [blame] | 104 | GR_STATIC_ASSERT(sizeof(KeyHeader) == 6); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 105 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 106 | const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); } |
| 107 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 108 | template<typename T, size_t OFFSET> T* atOffset() { |
| 109 | return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 110 | } |
| 111 | |
| 112 | template<typename T, size_t OFFSET> const T* atOffset() const { |
| 113 | return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 114 | } |
| 115 | |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 116 | // The key, stored in fKey, is composed of two parts: |
| 117 | // 1. Header struct defined above. |
| 118 | // 2. A Backend specific payload which includes the per-processor keys. |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 119 | enum KeyOffsets { |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 120 | kHeaderOffset = 0, |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 121 | kHeaderSize = SkAlign4(sizeof(KeyHeader)), |
| 122 | // Part 4. |
| 123 | // This is the offset into the backenend specific part of the key, which includes |
| 124 | // per-processor keys. |
| 125 | kProcessorKeysOffset = kHeaderOffset + kHeaderSize, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | enum { |
| 129 | kMaxPreallocProcessors = 8, |
| 130 | kIntsPerProcessor = 4, // This is an overestimate of the average effect key size. |
| 131 | kPreAllocSize = kHeaderOffset + kHeaderSize + |
| 132 | kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor, |
| 133 | }; |
| 134 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 135 | SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; } |
| 136 | const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 137 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 138 | private: |
| 139 | SkSTArray<kPreAllocSize, uint8_t, true> fKey; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | #endif |