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 | |
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 | class GrPipeline; |
| 20 | class GrPrimitiveProcessor; |
| 21 | |
| 22 | /** 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] | 23 | class GrProgramDesc { |
| 24 | public: |
| 25 | // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc() |
| 26 | GrProgramDesc() {} |
| 27 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Builds a program descriptor. Before the descriptor can be used, the client must call finalize |
| 30 | * on the returned GrProgramDesc. |
| 31 | * |
| 32 | * @param GrPrimitiveProcessor The geometry |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 33 | * @param hasPointSize Controls whether the shader will output a point size. |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 34 | * @param GrPipeline The optimized drawstate. The descriptor will represent a program |
| 35 | * which this optstate can use to draw with. The optstate contains |
| 36 | * general draw information, as well as the specific color, geometry, |
| 37 | * and coverage stages which will be used to generate the GL Program for |
| 38 | * this optstate. |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 39 | * @param GrGpu Ptr to the GrGpu object the program will be used with. |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 40 | * @param GrProgramDesc The built and finalized descriptor |
| 41 | **/ |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 42 | static bool Build(GrProgramDesc*, const GrRenderTarget*, const GrPrimitiveProcessor&, |
| 43 | bool hasPointSize, const GrPipeline&, GrGpu*); |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 44 | |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 45 | static bool BuildFromData(GrProgramDesc* desc, const void* keyData, size_t keyLength) { |
| 46 | if (!SkTFitsIn<int>(keyLength)) { |
| 47 | return false; |
| 48 | } |
| 49 | desc->fKey.reset(SkToInt(keyLength)); |
| 50 | memcpy(desc->fKey.begin(), keyData, keyLength); |
| 51 | return true; |
| 52 | } |
| 53 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 54 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| 55 | const uint32_t* asKey() const { |
| 56 | return reinterpret_cast<const uint32_t*>(fKey.begin()); |
| 57 | } |
| 58 | |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 59 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. |
| 60 | uint32_t keyLength() const { |
| 61 | SkASSERT(0 == (fKey.count() % 4)); |
| 62 | return fKey.count(); |
| 63 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 64 | |
| 65 | GrProgramDesc& operator= (const GrProgramDesc& other) { |
bsalomon | ccb328d | 2014-12-11 13:31:06 -0800 | [diff] [blame] | 66 | uint32_t keyLength = other.keyLength(); |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 67 | fKey.reset(SkToInt(keyLength)); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 68 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
| 69 | return *this; |
| 70 | } |
| 71 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 72 | bool operator== (const GrProgramDesc& that) const { |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 73 | if (this->keyLength() != that.keyLength()) { |
| 74 | return false; |
| 75 | } |
| 76 | |
bsalomon | 89d5988 | 2015-06-04 15:34:34 -0700 | [diff] [blame] | 77 | SkASSERT(SkIsAlign4(this->keyLength())); |
| 78 | int l = this->keyLength() >> 2; |
| 79 | const uint32_t* aKey = this->asKey(); |
| 80 | const uint32_t* bKey = that.asKey(); |
| 81 | for (int i = 0; i < l; ++i) { |
| 82 | if (aKey[i] != bKey[i]) { |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | return true; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | bool operator!= (const GrProgramDesc& other) const { |
| 90 | return !(*this == other); |
| 91 | } |
| 92 | |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 93 | void setSurfaceOriginKey(int key) { |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 94 | KeyHeader* header = this->atOffset<KeyHeader, kHeaderOffset>(); |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 95 | header->fSurfaceOriginKey = key; |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 96 | } |
| 97 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 98 | struct KeyHeader { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 99 | bool hasSurfaceOriginKey() const { |
| 100 | return SkToBool(fSurfaceOriginKey); |
| 101 | } |
| 102 | GrProcessor::CustomFeatures processorFeatures() const { |
| 103 | return (GrProcessor::CustomFeatures)fProcessorFeatures; |
| 104 | } |
| 105 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 106 | // Set to uniquely idenitify any swizzling of the shader's output color(s). |
Greg Daniel | f259b8b | 2019-02-14 09:03:43 -0500 | [diff] [blame] | 107 | uint16_t fOutputSwizzle; |
Chris Dalton | 535ba8d | 2018-02-20 09:51:59 -0700 | [diff] [blame] | 108 | uint8_t fColorFragmentProcessorCnt; // Can be packed into 4 bits if required. |
| 109 | uint8_t fCoverageFragmentProcessorCnt; |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 110 | // 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] | 111 | uint8_t fSurfaceOriginKey : 2; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 112 | uint8_t fProcessorFeatures : 1; |
Chris Dalton | 535ba8d | 2018-02-20 09:51:59 -0700 | [diff] [blame] | 113 | bool fSnapVerticesToPixelCenters : 1; |
| 114 | bool fHasPointSize : 1; |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 115 | uint8_t fPad : 3; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 116 | }; |
Greg Daniel | f259b8b | 2019-02-14 09:03:43 -0500 | [diff] [blame] | 117 | GR_STATIC_ASSERT(sizeof(KeyHeader) == 6); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 118 | |
| 119 | // This should really only be used internally, base classes should return their own headers |
| 120 | const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); } |
| 121 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 122 | protected: |
| 123 | template<typename T, size_t OFFSET> T* atOffset() { |
| 124 | return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 125 | } |
| 126 | |
| 127 | template<typename T, size_t OFFSET> const T* atOffset() const { |
| 128 | return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 129 | } |
| 130 | |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 131 | // The key, stored in fKey, is composed of two parts: |
| 132 | // 1. Header struct defined above. |
| 133 | // 2. A Backend specific payload which includes the per-processor keys. |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 134 | enum KeyOffsets { |
Greg Daniel | 2d2c09f | 2019-01-07 16:14:12 -0500 | [diff] [blame] | 135 | kHeaderOffset = 0, |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 136 | kHeaderSize = SkAlign4(sizeof(KeyHeader)), |
| 137 | // Part 4. |
| 138 | // This is the offset into the backenend specific part of the key, which includes |
| 139 | // per-processor keys. |
| 140 | kProcessorKeysOffset = kHeaderOffset + kHeaderSize, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | enum { |
| 144 | kMaxPreallocProcessors = 8, |
| 145 | kIntsPerProcessor = 4, // This is an overestimate of the average effect key size. |
| 146 | kPreAllocSize = kHeaderOffset + kHeaderSize + |
| 147 | kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor, |
| 148 | }; |
| 149 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 150 | SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; } |
| 151 | const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 152 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 153 | private: |
| 154 | SkSTArray<kPreAllocSize, uint8_t, true> fKey; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | #endif |