GrGLProgramDesc.h revision f6de475e5cbd143f348ff7738919e397b7fe7f57
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        SkASSERT(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     * outputs the color and coverage stages referenced by the generated descriptor. This may
59     * not contain all stages from the draw state and coverage stages from the drawState may
60     * be treated as color stages in the output.
61     */
62    static void Build(const GrDrawState&,
63                      bool isPoints,
64                      GrDrawState::BlendOptFlags,
65                      GrBlendCoeff srcCoeff,
66                      GrBlendCoeff dstCoeff,
67                      const GrGpuGL* gpu,
68                      const GrDeviceCoordTexture* dstCopy,
69                      SkTArray<const GrEffectStage*, true>* outColorStages,
70                      SkTArray<const GrEffectStage*, true>* outCoverageStages,
71                      GrGLProgramDesc* outDesc);
72
73    int numColorEffects() const {
74        SkASSERT(fInitialized);
75        return this->getHeader().fColorEffectCnt;
76    }
77
78    int numCoverageEffects() const {
79        SkASSERT(fInitialized);
80        return this->getHeader().fCoverageEffectCnt;
81    }
82
83    int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); }
84
85    GrGLProgramDesc& operator= (const GrGLProgramDesc& other);
86
87    bool operator== (const GrGLProgramDesc& other) const {
88        SkASSERT(fInitialized && other.fInitialized);
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
101private:
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    };
111
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
125        // can only be set if fDstReadKey is non-zero.
126        kCombineWithDst_CoverageOutput,
127
128        kCoverageOutputCnt
129    };
130
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
142    struct KeyHeader {
143        GrGLShaderBuilder::DstReadKey fDstReadKey;      // set by GrGLShaderBuilder if there
144                                                        // are effects that must read the dst.
145                                                        // Otherwise, 0.
146        GrGLShaderBuilder::FragPosKey fFragPosKey;      // set by GrGLShaderBuilder if there are
147                                                        // effects that read the fragment position.
148                                                        // Otherwise, 0.
149
150        // should the FS discard if the coverage is zero (to avoid stencil manipulation)
151        SkBool8                     fDiscardIfZeroCoverage;
152
153        uint8_t                     fColorInput;            // casts to enum ColorInput
154        uint8_t                     fCoverageInput;         // casts to enum ColorInput
155        uint8_t                     fCoverageOutput;        // casts to enum CoverageOutput
156
157        SkBool8                     fEmitsPointSize;
158        uint8_t                     fColorFilterXfermode;   // casts to enum SkXfermode::Mode
159
160        // To enable experimental geometry shader code (not for use in
161        // production)
162#if GR_GL_EXPERIMENTAL_GS
163        SkBool8                     fExperimentalGS;
164#endif
165
166        int8_t                      fPositionAttributeIndex;
167        int8_t                      fLocalCoordAttributeIndex;
168        int8_t                      fColorAttributeIndex;
169        int8_t                      fCoverageAttributeIndex;
170
171        int8_t                      fColorEffectCnt;
172        int8_t                      fCoverageEffectCnt;
173    };
174
175    // The key is 1 uint32_t for the length, followed another for the checksum, the header, and then
176    // the effect keys. Everything is fixed length except the effect key array.
177    enum {
178        kLengthOffset = 0,
179        kChecksumOffset = kLengthOffset + sizeof(uint32_t),
180        kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
181        kHeaderSize = SkAlign4(sizeof(KeyHeader)),
182        kEffectKeyOffset = kHeaderOffset + kHeaderSize,
183    };
184
185    template<typename T, size_t OFFSET> T* atOffset() {
186        return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.get()) + OFFSET);
187    }
188
189    template<typename T, size_t OFFSET> const T* atOffset() const {
190        return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.get()) + OFFSET);
191    }
192
193    typedef GrGLEffect::EffectKey EffectKey;
194
195    uint32_t* checksum() { return this->atOffset<uint32_t, kChecksumOffset>(); }
196    KeyHeader* header() { return this->atOffset<KeyHeader, kHeaderOffset>(); }
197    EffectKey* effectKeys() { return this->atOffset<EffectKey, kEffectKeyOffset>(); }
198
199    const KeyHeader& getHeader() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
200    const EffectKey* getEffectKeys() const { return this->atOffset<EffectKey, kEffectKeyOffset>(); }
201
202    static size_t KeyLength(int effectCnt) {
203        GR_STATIC_ASSERT(!(sizeof(EffectKey) & 0x3));
204        return kEffectKeyOffset + effectCnt * sizeof(EffectKey);
205    }
206
207    enum {
208        kMaxPreallocEffects = 16,
209        kPreAllocSize = kEffectKeyOffset +  kMaxPreallocEffects * sizeof(EffectKey),
210    };
211
212    SkAutoSMalloc<kPreAllocSize> fKey;
213    bool fInitialized;
214
215    // GrGLProgram and GrGLShaderBuilder read the private fields to generate code. TODO: Move all
216    // code generation to GrGLShaderBuilder (and maybe add getters rather than friending).
217    friend class GrGLProgram;
218    friend class GrGLShaderBuilder;
219};
220
221#endif
222