michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef GrGLProgramDesc_DEFINED michael@0: #define GrGLProgramDesc_DEFINED michael@0: michael@0: #include "GrGLEffect.h" michael@0: #include "GrDrawState.h" michael@0: #include "GrGLShaderBuilder.h" michael@0: michael@0: class GrGpuGL; michael@0: michael@0: #ifdef SK_DEBUG michael@0: // Optionally compile the experimental GS code. Set to SK_DEBUG so that debug build bots will michael@0: // execute the code. michael@0: #define GR_GL_EXPERIMENTAL_GS 1 michael@0: #else michael@0: #define GR_GL_EXPERIMENTAL_GS 0 michael@0: #endif michael@0: michael@0: michael@0: /** This class describes a program to generate. It also serves as a program cache key. Very little michael@0: of this is GL-specific. There is the generation of GrGLEffect::EffectKeys and the dst-read part michael@0: of the key set by GrGLShaderBuilder. If the interfaces that set those portions were abstracted michael@0: to be API-neutral then so could this class. */ michael@0: class GrGLProgramDesc { michael@0: public: michael@0: GrGLProgramDesc() : fInitialized(false) {} michael@0: GrGLProgramDesc(const GrGLProgramDesc& desc) { *this = desc; } michael@0: michael@0: // Returns this as a uint32_t array to be used as a key in the program cache. michael@0: const uint32_t* asKey() const { michael@0: SkASSERT(fInitialized); michael@0: return reinterpret_cast(fKey.get()); michael@0: } michael@0: michael@0: // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two michael@0: // keys the size of either key can be used with memcmp() since the lengths themselves begin the michael@0: // keys and thus the memcmp will exit early if the keys are of different lengths. michael@0: uint32_t keyLength() const { return *this->atOffset(); } michael@0: michael@0: // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache. michael@0: uint32_t getChecksum() const { return *this->atOffset(); } michael@0: michael@0: // For unit testing. michael@0: void setRandom(SkRandom*, michael@0: const GrGpuGL* gpu, michael@0: const GrRenderTarget* dummyDstRenderTarget, michael@0: const GrTexture* dummyDstCopyTexture, michael@0: const GrEffectStage* stages[], michael@0: int numColorStages, michael@0: int numCoverageStages, michael@0: int currAttribIndex); michael@0: michael@0: /** michael@0: * Builds a program descriptor from a GrDrawState. Whether the primitive type is points, the michael@0: * output of GrDrawState::getBlendOpts, and the caps of the GrGpuGL are also inputs. It also michael@0: * outputs the color and coverage stages referenced by the generated descriptor. This may michael@0: * not contain all stages from the draw state and coverage stages from the drawState may michael@0: * be treated as color stages in the output. michael@0: */ michael@0: static void Build(const GrDrawState&, michael@0: bool isPoints, michael@0: GrDrawState::BlendOptFlags, michael@0: GrBlendCoeff srcCoeff, michael@0: GrBlendCoeff dstCoeff, michael@0: const GrGpuGL* gpu, michael@0: const GrDeviceCoordTexture* dstCopy, michael@0: SkTArray* outColorStages, michael@0: SkTArray* outCoverageStages, michael@0: GrGLProgramDesc* outDesc); michael@0: michael@0: int numColorEffects() const { michael@0: SkASSERT(fInitialized); michael@0: return this->getHeader().fColorEffectCnt; michael@0: } michael@0: michael@0: int numCoverageEffects() const { michael@0: SkASSERT(fInitialized); michael@0: return this->getHeader().fCoverageEffectCnt; michael@0: } michael@0: michael@0: int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); } michael@0: michael@0: GrGLProgramDesc& operator= (const GrGLProgramDesc& other); michael@0: michael@0: bool operator== (const GrGLProgramDesc& other) const { michael@0: SkASSERT(fInitialized && other.fInitialized); michael@0: // The length is masked as a hint to the compiler that the address will be 4 byte aligned. michael@0: return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3); michael@0: } michael@0: michael@0: bool operator!= (const GrGLProgramDesc& other) const { michael@0: return !(*this == other); michael@0: } michael@0: michael@0: static bool Less(const GrGLProgramDesc& a, const GrGLProgramDesc& b) { michael@0: return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0; michael@0: } michael@0: michael@0: private: michael@0: // Specifies where the initial color comes from before the stages are applied. michael@0: enum ColorInput { michael@0: kSolidWhite_ColorInput, michael@0: kTransBlack_ColorInput, michael@0: kAttribute_ColorInput, michael@0: kUniform_ColorInput, michael@0: michael@0: kColorInputCnt michael@0: }; michael@0: michael@0: enum CoverageOutput { michael@0: // modulate color and coverage, write result as the color output. michael@0: kModulate_CoverageOutput, michael@0: // Writes color*coverage as the primary color output and also writes coverage as the michael@0: // secondary output. Only set if dual source blending is supported. michael@0: kSecondaryCoverage_CoverageOutput, michael@0: // Writes color*coverage as the primary color output and also writes coverage * (1 - colorA) michael@0: // as the secondary output. Only set if dual source blending is supported. michael@0: kSecondaryCoverageISA_CoverageOutput, michael@0: // Writes color*coverage as the primary color output and also writes coverage * michael@0: // (1 - colorRGB) as the secondary output. Only set if dual source blending is supported. michael@0: kSecondaryCoverageISC_CoverageOutput, michael@0: // Combines the coverage, dst, and color as coverage * color + (1 - coverage) * dst. This michael@0: // can only be set if fDstReadKey is non-zero. michael@0: kCombineWithDst_CoverageOutput, michael@0: michael@0: kCoverageOutputCnt michael@0: }; michael@0: michael@0: static bool CoverageOutputUsesSecondaryOutput(CoverageOutput co) { michael@0: switch (co) { michael@0: case kSecondaryCoverage_CoverageOutput: // fallthru michael@0: case kSecondaryCoverageISA_CoverageOutput: michael@0: case kSecondaryCoverageISC_CoverageOutput: michael@0: return true; michael@0: default: michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: struct KeyHeader { michael@0: GrGLShaderBuilder::DstReadKey fDstReadKey; // set by GrGLShaderBuilder if there michael@0: // are effects that must read the dst. michael@0: // Otherwise, 0. michael@0: GrGLShaderBuilder::FragPosKey fFragPosKey; // set by GrGLShaderBuilder if there are michael@0: // effects that read the fragment position. michael@0: // Otherwise, 0. michael@0: michael@0: ColorInput fColorInput : 8; michael@0: ColorInput fCoverageInput : 8; michael@0: CoverageOutput fCoverageOutput : 8; michael@0: michael@0: SkBool8 fHasVertexCode; michael@0: SkBool8 fEmitsPointSize; michael@0: michael@0: // To enable experimental geometry shader code (not for use in michael@0: // production) michael@0: #if GR_GL_EXPERIMENTAL_GS michael@0: SkBool8 fExperimentalGS; michael@0: #endif michael@0: michael@0: int8_t fPositionAttributeIndex; michael@0: int8_t fLocalCoordAttributeIndex; michael@0: int8_t fColorAttributeIndex; michael@0: int8_t fCoverageAttributeIndex; michael@0: michael@0: int8_t fColorEffectCnt; michael@0: int8_t fCoverageEffectCnt; michael@0: }; michael@0: michael@0: // The key is 1 uint32_t for the length, followed another for the checksum, the header, and then michael@0: // the effect keys. Everything is fixed length except the effect key array. michael@0: enum { michael@0: kLengthOffset = 0, michael@0: kChecksumOffset = kLengthOffset + sizeof(uint32_t), michael@0: kHeaderOffset = kChecksumOffset + sizeof(uint32_t), michael@0: kHeaderSize = SkAlign4(sizeof(KeyHeader)), michael@0: kEffectKeyOffset = kHeaderOffset + kHeaderSize, michael@0: }; michael@0: michael@0: template T* atOffset() { michael@0: return reinterpret_cast(reinterpret_cast(fKey.get()) + OFFSET); michael@0: } michael@0: michael@0: template const T* atOffset() const { michael@0: return reinterpret_cast(reinterpret_cast(fKey.get()) + OFFSET); michael@0: } michael@0: michael@0: typedef GrGLEffect::EffectKey EffectKey; michael@0: michael@0: uint32_t* checksum() { return this->atOffset(); } michael@0: KeyHeader* header() { return this->atOffset(); } michael@0: EffectKey* effectKeys() { return this->atOffset(); } michael@0: michael@0: const KeyHeader& getHeader() const { return *this->atOffset(); } michael@0: const EffectKey* getEffectKeys() const { return this->atOffset(); } michael@0: michael@0: static size_t KeyLength(int effectCnt) { michael@0: GR_STATIC_ASSERT(!(sizeof(EffectKey) & 0x3)); michael@0: return kEffectKeyOffset + effectCnt * sizeof(EffectKey); michael@0: } michael@0: michael@0: enum { michael@0: kMaxPreallocEffects = 16, michael@0: kPreAllocSize = kEffectKeyOffset + kMaxPreallocEffects * sizeof(EffectKey), michael@0: }; michael@0: michael@0: SkAutoSMalloc fKey; michael@0: bool fInitialized; michael@0: michael@0: // GrGLProgram and GrGLShaderBuilder read the private fields to generate code. TODO: Move all michael@0: // code generation to GrGLShaderBuilder (and maybe add getters rather than friending). michael@0: friend class GrGLProgram; michael@0: friend class GrGLShaderBuilder; michael@0: friend class GrGLFullShaderBuilder; michael@0: friend class GrGLFragmentOnlyShaderBuilder; michael@0: }; michael@0: michael@0: #endif