GrGLProgramDesc.h revision 26e18b593ab65e4d92dfbce92579d8bc180d4c2c
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() {
29        // since we use this as part of a key we can't have any uninitialized padding
30        memset(this, 0, sizeof(GrGLProgramDesc));
31    }
32
33    // Returns this as a uint32_t array to be used as a key in the program cache
34    const uint32_t* asKey() const {
35        return reinterpret_cast<const uint32_t*>(this);
36    }
37
38    // For unit testing.
39    void setRandom(SkMWCRandom*,
40                   const GrGpuGL* gpu,
41                   const GrEffectStage stages[GrDrawState::kNumStages]);
42
43    /**
44     * Builds a program descriptor from a GrDrawState. Whether the primitive type is points, the
45     * output of GrDrawState::getBlendOpts, and the caps of the GrGpuGL are also inputs.
46     */
47    static void Build(const GrDrawState&,
48                      bool isPoints,
49                      GrDrawState::BlendOptFlags,
50                      GrBlendCoeff srcCoeff,
51                      GrBlendCoeff dstCoeff,
52                      const GrGpuGL* gpu,
53                      const GrDeviceCoordTexture* dstCopy,
54                      GrGLProgramDesc* outDesc);
55
56private:
57    // Specifies where the initial color comes from before the stages are applied.
58    enum ColorInput {
59        kSolidWhite_ColorInput,
60        kTransBlack_ColorInput,
61        kAttribute_ColorInput,
62        kUniform_ColorInput,
63
64        kColorInputCnt
65    };
66    // Dual-src blending makes use of a secondary output color that can be
67    // used as a per-pixel blend coefficient. This controls whether a
68    // secondary source is output and what value it holds.
69    enum DualSrcOutput {
70        kNone_DualSrcOutput,
71        kCoverage_DualSrcOutput,
72        kCoverageISA_DualSrcOutput,
73        kCoverageISC_DualSrcOutput,
74
75        kDualSrcOutputCnt
76    };
77
78    // should the FS discard if the coverage is zero (to avoid stencil manipulation)
79    bool                        fDiscardIfZeroCoverage;
80
81    // stripped of bits that don't affect program generation
82    GrAttribBindings            fAttribBindings;
83
84    /** Non-zero if this stage has an effect */
85    GrGLEffect::EffectKey       fEffectKeys[GrDrawState::kNumStages];
86
87    // To enable experimental geometry shader code (not for use in
88    // production)
89#if GR_GL_EXPERIMENTAL_GS
90    bool                        fExperimentalGS;
91#endif
92    GrGLShaderBuilder::DstReadKey fDstRead;             // set by GrGLShaderBuilder if there
93                                                        // are effects that must read the dst.
94                                                        // Otherwise, 0.
95
96    uint8_t                     fColorInput;            // casts to enum ColorInput
97    uint8_t                     fCoverageInput;         // casts to enum ColorInput
98    uint8_t                     fDualSrcOutput;         // casts to enum DualSrcOutput
99    int8_t                      fFirstCoverageStage;
100    SkBool8                     fEmitsPointSize;
101    uint8_t                     fColorFilterXfermode;   // casts to enum SkXfermode::Mode
102
103    int8_t                      fPositionAttributeIndex;
104    int8_t                      fColorAttributeIndex;
105    int8_t                      fCoverageAttributeIndex;
106    int8_t                      fLocalCoordsAttributeIndex;
107
108    // GrGLProgram and GrGLShaderBuilder read the private fields to generate code. TODO: Move all
109    // code generation to GrGLShaderBuilder (and maybe add getters rather than friending).
110    friend class GrGLProgram;
111    friend class GrGLShaderBuilder;
112};
113
114#endif
115