| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 GrGLProgramDesc_DEFINED |
| 9 | #define GrGLProgramDesc_DEFINED |
| 10 | |
| 11 | #include "GrGLEffect.h" |
| bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 12 | #include "GrDrawState.h" |
| commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 13 | #include "GrGpu.h" |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 14 | |
| 15 | class GrGpuGL; |
| 16 | |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 17 | #ifdef SK_DEBUG |
| 18 | // Optionally compile the experimental GS code. Set to SK_DEBUG so that debug build bots will |
| 19 | // execute the code. |
| 20 | #define GR_GL_EXPERIMENTAL_GS 1 |
| 21 | #else |
| 22 | #define GR_GL_EXPERIMENTAL_GS 0 |
| 23 | #endif |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 24 | |
| 25 | |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 26 | /** This class describes a program to generate. It also serves as a program cache key. Very little |
| 27 | of this is GL-specific. There is the generation of GrGLEffect::EffectKeys and the dst-read part |
| 28 | of the key set by GrGLShaderBuilder. If the interfaces that set those portions were abstracted |
| 29 | to be API-neutral then so could this class. */ |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 30 | class GrGLProgramDesc { |
| 31 | public: |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 32 | GrGLProgramDesc() {} |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 33 | GrGLProgramDesc(const GrGLProgramDesc& desc) { *this = desc; } |
| 34 | |
| skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 35 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 36 | const uint32_t* asKey() const { |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 37 | return reinterpret_cast<const uint32_t*>(fKey.begin()); |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 40 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two |
| 41 | // keys the size of either key can be used with memcmp() since the lengths themselves begin the |
| 42 | // keys and thus the memcmp will exit early if the keys are of different lengths. |
| 43 | uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); } |
| 44 | |
| 45 | // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache. |
| 46 | uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); } |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 47 | |
| 48 | // For unit testing. |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 49 | bool setRandom(SkRandom*, |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 50 | const GrGpuGL* gpu, |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 51 | const GrRenderTarget* dummyDstRenderTarget, |
| 52 | const GrTexture* dummyDstCopyTexture, |
| 53 | const GrEffectStage* stages[], |
| 54 | int numColorStages, |
| 55 | int numCoverageStages, |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 56 | int currAttribIndex); |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Builds a program descriptor from a GrDrawState. Whether the primitive type is points, the |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 60 | * output of GrDrawState::getBlendOpts, and the caps of the GrGpuGL are also inputs. It also |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 61 | * outputs the color and coverage stages referenced by the generated descriptor. This may |
| 62 | * not contain all stages from the draw state and coverage stages from the drawState may |
| 63 | * be treated as color stages in the output. |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 64 | */ |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 65 | static bool Build(const GrDrawState&, |
| commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 66 | GrGpu::DrawType drawType, |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 67 | GrDrawState::BlendOptFlags, |
| 68 | GrBlendCoeff srcCoeff, |
| 69 | GrBlendCoeff dstCoeff, |
| 70 | const GrGpuGL* gpu, |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 71 | const GrDeviceCoordTexture* dstCopy, |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 72 | SkTArray<const GrEffectStage*, true>* outColorStages, |
| 73 | SkTArray<const GrEffectStage*, true>* outCoverageStages, |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 74 | GrGLProgramDesc* outDesc); |
| 75 | |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 76 | int numColorEffects() const { |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 77 | return this->getHeader().fColorEffectCnt; |
| 78 | } |
| 79 | |
| 80 | int numCoverageEffects() const { |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 81 | return this->getHeader().fCoverageEffectCnt; |
| 82 | } |
| 83 | |
| 84 | int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); } |
| 85 | |
| 86 | GrGLProgramDesc& operator= (const GrGLProgramDesc& other); |
| 87 | |
| 88 | bool operator== (const GrGLProgramDesc& other) const { |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 89 | // The length is masked as a hint to the compiler that the address will be 4 byte aligned. |
| 90 | return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3); |
| 91 | } |
| 92 | |
| 93 | bool operator!= (const GrGLProgramDesc& other) const { |
| 94 | return !(*this == other); |
| 95 | } |
| 96 | |
| 97 | static bool Less(const GrGLProgramDesc& a, const GrGLProgramDesc& b) { |
| 98 | return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0; |
| 99 | } |
| 100 | |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 101 | private: |
| 102 | // Specifies where the initial color comes from before the stages are applied. |
| 103 | enum ColorInput { |
| 104 | kSolidWhite_ColorInput, |
| 105 | kTransBlack_ColorInput, |
| 106 | kAttribute_ColorInput, |
| 107 | kUniform_ColorInput, |
| 108 | |
| 109 | kColorInputCnt |
| 110 | }; |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 111 | |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 112 | enum CoverageOutput { |
| 113 | // modulate color and coverage, write result as the color output. |
| 114 | kModulate_CoverageOutput, |
| 115 | // Writes color*coverage as the primary color output and also writes coverage as the |
| 116 | // secondary output. Only set if dual source blending is supported. |
| 117 | kSecondaryCoverage_CoverageOutput, |
| 118 | // Writes color*coverage as the primary color output and also writes coverage * (1 - colorA) |
| 119 | // as the secondary output. Only set if dual source blending is supported. |
| 120 | kSecondaryCoverageISA_CoverageOutput, |
| 121 | // Writes color*coverage as the primary color output and also writes coverage * |
| 122 | // (1 - colorRGB) as the secondary output. Only set if dual source blending is supported. |
| 123 | kSecondaryCoverageISC_CoverageOutput, |
| 124 | // Combines the coverage, dst, and color as coverage * color + (1 - coverage) * dst. This |
| bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 125 | // can only be set if fDstReadKey is non-zero. |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 126 | kCombineWithDst_CoverageOutput, |
| 127 | |
| 128 | kCoverageOutputCnt |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 131 | static bool CoverageOutputUsesSecondaryOutput(CoverageOutput co) { |
| 132 | switch (co) { |
| 133 | case kSecondaryCoverage_CoverageOutput: // fallthru |
| 134 | case kSecondaryCoverageISA_CoverageOutput: |
| 135 | case kSecondaryCoverageISC_CoverageOutput: |
| 136 | return true; |
| 137 | default: |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 | |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 142 | struct KeyHeader { |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 143 | uint8_t fDstReadKey; // set by GrGLShaderBuilder if there |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 144 | // are effects that must read the dst. |
| 145 | // Otherwise, 0. |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 146 | uint8_t fFragPosKey; // set by GrGLShaderBuilder if there are |
| bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 147 | // effects that read the fragment position. |
| 148 | // Otherwise, 0. |
| commit-bot@chromium.org | 949eef0 | 2013-10-01 18:43:29 +0000 | [diff] [blame] | 149 | ColorInput fColorInput : 8; |
| 150 | ColorInput fCoverageInput : 8; |
| 151 | CoverageOutput fCoverageOutput : 8; |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 152 | |
| commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 153 | SkBool8 fHasVertexCode; |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 154 | SkBool8 fEmitsPointSize; |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 155 | |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 156 | // To enable experimental geometry shader code (not for use in |
| 157 | // production) |
| 158 | #if GR_GL_EXPERIMENTAL_GS |
| 159 | SkBool8 fExperimentalGS; |
| 160 | #endif |
| 161 | |
| 162 | int8_t fPositionAttributeIndex; |
| 163 | int8_t fLocalCoordAttributeIndex; |
| 164 | int8_t fColorAttributeIndex; |
| 165 | int8_t fCoverageAttributeIndex; |
| skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 166 | |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 167 | int8_t fColorEffectCnt; |
| 168 | int8_t fCoverageEffectCnt; |
| 169 | }; |
| 170 | |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 171 | // The key, stored in fKey, is composed of five parts: |
| 172 | // 1. uint32_t for total key length. |
| 173 | // 2. uint32_t for a checksum. |
| 174 | // 3. Header struct defined above. |
| bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame^] | 175 | // 4. An array of offsets to effect keys and their sizes (see 5). uint16_t for each |
| 176 | // offset and size. |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 177 | // 5. per-effect keys. Each effect's key is a variable length array of uint32_t. |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 178 | enum { |
| bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame^] | 179 | // Part 1. |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 180 | kLengthOffset = 0, |
| bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame^] | 181 | // Part 2. |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 182 | kChecksumOffset = kLengthOffset + sizeof(uint32_t), |
| bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame^] | 183 | // Part 3. |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 184 | kHeaderOffset = kChecksumOffset + sizeof(uint32_t), |
| 185 | kHeaderSize = SkAlign4(sizeof(KeyHeader)), |
| bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame^] | 186 | // Part 4. |
| 187 | // This is the offset in the overall key to the array of per-effect offset,length pairs. |
| 188 | kEffectKeyOffsetsAndLengthOffset = kHeaderOffset + kHeaderSize, |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | template<typename T, size_t OFFSET> T* atOffset() { |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 192 | return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | template<typename T, size_t OFFSET> const T* atOffset() const { |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 196 | return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 199 | typedef GrBackendEffectFactory::EffectKey EffectKey; |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 200 | |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 201 | KeyHeader* header() { return this->atOffset<KeyHeader, kHeaderOffset>(); } |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 202 | |
| bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame^] | 203 | // Shared code between setRandom() and Build(). |
| 204 | static bool GetEffectKeyAndUpdateStats(const GrEffectStage& stage, |
| 205 | const GrGLCaps& caps, |
| 206 | bool useExplicitLocalCoords, |
| 207 | GrEffectKeyBuilder* b, |
| 208 | uint16_t* effectKeySize, |
| 209 | bool* setTrueIfReadsDst, |
| 210 | bool* setTrueIfReadsPos, |
| 211 | bool* setTrueIfHasVertexCode); |
| 212 | |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 213 | void finalize(); |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 214 | |
| 215 | const KeyHeader& getHeader() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); } |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 216 | |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 217 | /** Used to provide effects' keys to their emitCode() function. */ |
| 218 | class EffectKeyProvider { |
| 219 | public: |
| 220 | enum EffectType { |
| 221 | kColor_EffectType, |
| 222 | kCoverage_EffectType, |
| 223 | }; |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 224 | |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 225 | EffectKeyProvider(const GrGLProgramDesc* desc, EffectType type) : fDesc(desc) { |
| 226 | // Coverage effect key offsets begin immediately after those of the color effects. |
| 227 | fBaseIndex = kColor_EffectType == type ? 0 : desc->numColorEffects(); |
| 228 | } |
| 229 | |
| 230 | EffectKey get(int index) const { |
| bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame^] | 231 | const uint16_t* offsets = reinterpret_cast<const uint16_t*>( |
| 232 | fDesc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset); |
| 233 | // We store two uint16_ts per effect, one for the offset to the effect's key and one for |
| 234 | // its length. Here we just need the offset. |
| 235 | uint16_t offset = offsets[2 * (fBaseIndex + index)]; |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 236 | return *reinterpret_cast<const EffectKey*>(fDesc->fKey.begin() + offset); |
| 237 | } |
| 238 | private: |
| 239 | const GrGLProgramDesc* fDesc; |
| 240 | int fBaseIndex; |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 243 | enum { |
| 244 | kMaxPreallocEffects = 8, |
| 245 | kIntsPerEffect = 4, // This is an overestimate of the average effect key size. |
| bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame^] | 246 | kPreAllocSize = kEffectKeyOffsetsAndLengthOffset + |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 247 | kMaxPreallocEffects * sizeof(uint32_t) * kIntsPerEffect, |
| 248 | }; |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 249 | |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 250 | SkSTArray<kPreAllocSize, uint8_t, true> fKey; |
| 251 | |
| 252 | // GrGLProgram and GrGLShaderBuilder read the private fields to generate code. TODO: Split out |
| 253 | // part of GrGLShaderBuilder that is used by effects so that this header doesn't need to be |
| 254 | // visible to GrGLEffects. Then make public accessors as necessary and remove friends. |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 255 | friend class GrGLProgram; |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 256 | friend class GrGLShaderBuilder; |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 257 | friend class GrGLFullShaderBuilder; |
| commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 258 | friend class GrGLFragmentOnlyShaderBuilder; |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | #endif |