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" |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 13 | #include "GrGLShaderBuilder.h" |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 14 | |
| 15 | class GrGpuGL; |
| 16 | |
| 17 | // optionally compile the experimental GS code. Set to GR_DEBUG so that debug build bots will |
| 18 | // execute the code. |
| 19 | #define GR_GL_EXPERIMENTAL_GS GR_DEBUG |
| 20 | |
| 21 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 22 | /** This class describes a program to generate. It also serves as a program cache key. Very little |
| 23 | of this is GL-specific. There is the generation of GrGLEffect::EffectKeys and the dst-read part |
| 24 | of the key set by GrGLShaderBuilder. If the interfaces that set those portions were abstracted |
| 25 | to be API-neutral then so could this class. */ |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 26 | class GrGLProgramDesc { |
| 27 | public: |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 28 | GrGLProgramDesc() : fInitialized(false) {} |
| 29 | GrGLProgramDesc(const GrGLProgramDesc& desc) { *this = desc; } |
| 30 | |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame^] | 31 | // 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] | 32 | const uint32_t* asKey() const { |
| 33 | GrAssert(fInitialized); |
| 34 | return reinterpret_cast<const uint32_t*>(fKey.get()); |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 35 | } |
| 36 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 37 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two |
| 38 | // keys the size of either key can be used with memcmp() since the lengths themselves begin the |
| 39 | // keys and thus the memcmp will exit early if the keys are of different lengths. |
| 40 | uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); } |
| 41 | |
| 42 | // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache. |
| 43 | uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); } |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 44 | |
| 45 | // For unit testing. |
| 46 | void setRandom(SkMWCRandom*, |
| 47 | const GrGpuGL* gpu, |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 48 | const GrRenderTarget* dummyDstRenderTarget, |
| 49 | const GrTexture* dummyDstCopyTexture, |
| 50 | const GrEffectStage* stages[], |
| 51 | int numColorStages, |
| 52 | int numCoverageStages, |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 53 | int currAttribIndex); |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * 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] | 57 | * output of GrDrawState::getBlendOpts, and the caps of the GrGpuGL are also inputs. It also |
| 58 | * writes a tightly packed array of GrEffectStage* from the drawState. |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 59 | */ |
| 60 | static void Build(const GrDrawState&, |
| 61 | bool isPoints, |
| 62 | GrDrawState::BlendOptFlags, |
| 63 | GrBlendCoeff srcCoeff, |
| 64 | GrBlendCoeff dstCoeff, |
| 65 | const GrGpuGL* gpu, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 66 | const GrDeviceCoordTexture* dstCopy, |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 67 | const GrEffectStage* outStages[GrDrawState::kNumStages], |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 68 | GrGLProgramDesc* outDesc); |
| 69 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 70 | int numColorEffects() const { |
| 71 | GrAssert(fInitialized); |
| 72 | return this->getHeader().fColorEffectCnt; |
| 73 | } |
| 74 | |
| 75 | int numCoverageEffects() const { |
| 76 | GrAssert(fInitialized); |
| 77 | return this->getHeader().fCoverageEffectCnt; |
| 78 | } |
| 79 | |
| 80 | int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); } |
| 81 | |
| 82 | GrGLProgramDesc& operator= (const GrGLProgramDesc& other); |
| 83 | |
| 84 | bool operator== (const GrGLProgramDesc& other) const { |
| 85 | GrAssert(fInitialized && other.fInitialized); |
| 86 | // The length is masked as a hint to the compiler that the address will be 4 byte aligned. |
| 87 | return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3); |
| 88 | } |
| 89 | |
| 90 | bool operator!= (const GrGLProgramDesc& other) const { |
| 91 | return !(*this == other); |
| 92 | } |
| 93 | |
| 94 | static bool Less(const GrGLProgramDesc& a, const GrGLProgramDesc& b) { |
| 95 | return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0; |
| 96 | } |
| 97 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 98 | private: |
| 99 | // Specifies where the initial color comes from before the stages are applied. |
| 100 | enum ColorInput { |
| 101 | kSolidWhite_ColorInput, |
| 102 | kTransBlack_ColorInput, |
| 103 | kAttribute_ColorInput, |
| 104 | kUniform_ColorInput, |
| 105 | |
| 106 | kColorInputCnt |
| 107 | }; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 108 | |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 109 | enum CoverageOutput { |
| 110 | // modulate color and coverage, write result as the color output. |
| 111 | kModulate_CoverageOutput, |
| 112 | // Writes color*coverage as the primary color output and also writes coverage as the |
| 113 | // secondary output. Only set if dual source blending is supported. |
| 114 | kSecondaryCoverage_CoverageOutput, |
| 115 | // Writes color*coverage as the primary color output and also writes coverage * (1 - colorA) |
| 116 | // as the secondary output. Only set if dual source blending is supported. |
| 117 | kSecondaryCoverageISA_CoverageOutput, |
| 118 | // Writes color*coverage as the primary color output and also writes coverage * |
| 119 | // (1 - colorRGB) as the secondary output. Only set if dual source blending is supported. |
| 120 | kSecondaryCoverageISC_CoverageOutput, |
| 121 | // 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] | 122 | // can only be set if fDstReadKey is non-zero. |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 123 | kCombineWithDst_CoverageOutput, |
| 124 | |
| 125 | kCoverageOutputCnt |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 128 | static bool CoverageOutputUsesSecondaryOutput(CoverageOutput co) { |
| 129 | switch (co) { |
| 130 | case kSecondaryCoverage_CoverageOutput: // fallthru |
| 131 | case kSecondaryCoverageISA_CoverageOutput: |
| 132 | case kSecondaryCoverageISC_CoverageOutput: |
| 133 | return true; |
| 134 | default: |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 139 | struct KeyHeader { |
| 140 | GrGLShaderBuilder::DstReadKey fDstReadKey; // set by GrGLShaderBuilder if there |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 141 | // are effects that must read the dst. |
| 142 | // Otherwise, 0. |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 143 | GrGLShaderBuilder::FragPosKey fFragPosKey; // set by GrGLShaderBuilder if there are |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 144 | // effects that read the fragment position. |
| 145 | // Otherwise, 0. |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 146 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 147 | // should the FS discard if the coverage is zero (to avoid stencil manipulation) |
| 148 | SkBool8 fDiscardIfZeroCoverage; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 149 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 150 | uint8_t fColorInput; // casts to enum ColorInput |
| 151 | uint8_t fCoverageInput; // casts to enum ColorInput |
| 152 | uint8_t fCoverageOutput; // casts to enum CoverageOutput |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 153 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 154 | SkBool8 fEmitsPointSize; |
| 155 | uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 156 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 157 | // To enable experimental geometry shader code (not for use in |
| 158 | // production) |
| 159 | #if GR_GL_EXPERIMENTAL_GS |
| 160 | SkBool8 fExperimentalGS; |
| 161 | #endif |
| 162 | |
| 163 | int8_t fPositionAttributeIndex; |
| 164 | int8_t fLocalCoordAttributeIndex; |
| 165 | int8_t fColorAttributeIndex; |
| 166 | int8_t fCoverageAttributeIndex; |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame^] | 167 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 168 | int8_t fColorEffectCnt; |
| 169 | int8_t fCoverageEffectCnt; |
| 170 | }; |
| 171 | |
| 172 | // The key is 1 uint32_t for the length, followed another for the checksum, the header, and then |
| 173 | // the effect keys. Everything is fixed length except the effect key array. |
| 174 | enum { |
| 175 | kLengthOffset = 0, |
| 176 | kChecksumOffset = kLengthOffset + sizeof(uint32_t), |
| 177 | kHeaderOffset = kChecksumOffset + sizeof(uint32_t), |
| 178 | kHeaderSize = SkAlign4(sizeof(KeyHeader)), |
| 179 | kEffectKeyOffset = kHeaderOffset + kHeaderSize, |
| 180 | }; |
| 181 | |
| 182 | template<typename T, size_t OFFSET> T* atOffset() { |
| 183 | return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.get()) + OFFSET); |
| 184 | } |
| 185 | |
| 186 | template<typename T, size_t OFFSET> const T* atOffset() const { |
| 187 | return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.get()) + OFFSET); |
| 188 | } |
| 189 | |
| 190 | typedef GrGLEffect::EffectKey EffectKey; |
| 191 | |
| 192 | uint32_t* checksum() { return this->atOffset<uint32_t, kChecksumOffset>(); } |
| 193 | KeyHeader* header() { return this->atOffset<KeyHeader, kHeaderOffset>(); } |
| 194 | EffectKey* effectKeys() { return this->atOffset<EffectKey, kEffectKeyOffset>(); } |
| 195 | |
| 196 | const KeyHeader& getHeader() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); } |
| 197 | const EffectKey* getEffectKeys() const { return this->atOffset<EffectKey, kEffectKeyOffset>(); } |
| 198 | |
| 199 | static size_t KeyLength(int effectCnt) { |
| 200 | GR_STATIC_ASSERT(!(sizeof(EffectKey) & 0x3)); |
| 201 | return kEffectKeyOffset + effectCnt * sizeof(EffectKey); |
| 202 | } |
| 203 | |
| 204 | enum { |
| 205 | kMaxPreallocEffects = 16, |
| 206 | kPreAllocSize = kEffectKeyOffset + kMaxPreallocEffects * sizeof(EffectKey), |
| 207 | }; |
| 208 | |
| 209 | SkAutoSMalloc<kPreAllocSize> fKey; |
| 210 | bool fInitialized; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 211 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 212 | // GrGLProgram and GrGLShaderBuilder read the private fields to generate code. TODO: Move all |
| 213 | // code generation to GrGLShaderBuilder (and maybe add getters rather than friending). |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 214 | friend class GrGLProgram; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 215 | friend class GrGLShaderBuilder; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | #endif |