GrGLProgramDesc.h revision 2db3ded335fdb6697623bece61cabc307a414770
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" 12#include "GrDrawState.h" 13#include "GrGLShaderBuilder.h" 14 15class 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 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. */ 26class GrGLProgramDesc { 27public: 28 GrGLProgramDesc() : fInitialized(false) {} 29 GrGLProgramDesc(const GrGLProgramDesc& desc) { *this = desc; } 30 31 // Returns this as a uint32_t array to be used as a key in the program cache. 32 const uint32_t* asKey() const { 33 GrAssert(fInitialized); 34 return reinterpret_cast<const uint32_t*>(fKey.get()); 35 } 36 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>(); } 44 45 // For unit testing. 46 void setRandom(SkMWCRandom*, 47 const GrGpuGL* gpu, 48 const GrRenderTarget* dummyDstRenderTarget, 49 const GrTexture* dummyDstCopyTexture, 50 const GrEffectStage* stages[], 51 int numColorStages, 52 int numCoverageStages, 53 int currAttribIndex); 54 55 /** 56 * Builds a program descriptor from a GrDrawState. Whether the primitive type is points, the 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. 59 */ 60 static void Build(const GrDrawState&, 61 bool isPoints, 62 GrDrawState::BlendOptFlags, 63 GrBlendCoeff srcCoeff, 64 GrBlendCoeff dstCoeff, 65 const GrGpuGL* gpu, 66 const GrDeviceCoordTexture* dstCopy, 67 const GrEffectStage* outStages[GrDrawState::kNumStages], 68 GrGLProgramDesc* outDesc); 69 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 98private: 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 }; 108 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 122 // can only be set if fDstReadKey is non-zero. 123 kCombineWithDst_CoverageOutput, 124 125 kCoverageOutputCnt 126 }; 127 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 139 struct KeyHeader { 140 GrGLShaderBuilder::DstReadKey fDstReadKey; // set by GrGLShaderBuilder if there 141 // are effects that must read the dst. 142 // Otherwise, 0. 143 GrGLShaderBuilder::FragPosKey fFragPosKey; // set by GrGLShaderBuilder if there are 144 // effects that read the fragment position. 145 // Otherwise, 0. 146 147 // should the FS discard if the coverage is zero (to avoid stencil manipulation) 148 SkBool8 fDiscardIfZeroCoverage; 149 150 uint8_t fColorInput; // casts to enum ColorInput 151 uint8_t fCoverageInput; // casts to enum ColorInput 152 uint8_t fCoverageOutput; // casts to enum CoverageOutput 153 154 SkBool8 fEmitsPointSize; 155 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode 156 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; 167 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; 211 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). 214 friend class GrGLProgram; 215 friend class GrGLShaderBuilder; 216}; 217 218#endif 219