GrGLProgramDesc.h revision b515881446c303a50d9b2dd38b9163b4e5c625a2
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 GrTexture* dummyDstTexture,
42                   const GrEffectStage* stages[GrDrawState::kNumStages],
43                   int currAttribIndex);
44
45    /**
46     * Builds a program descriptor from a GrDrawState. Whether the primitive type is points, the
47     * output of GrDrawState::getBlendOpts, and the caps of the GrGpuGL are also inputs.
48     */
49    static void Build(const GrDrawState&,
50                      bool isPoints,
51                      GrDrawState::BlendOptFlags,
52                      GrBlendCoeff srcCoeff,
53                      GrBlendCoeff dstCoeff,
54                      const GrGpuGL* gpu,
55                      const GrDeviceCoordTexture* dstCopy,
56                      GrGLProgramDesc* outDesc);
57
58private:
59    // Specifies where the initial color comes from before the stages are applied.
60    enum ColorInput {
61        kSolidWhite_ColorInput,
62        kTransBlack_ColorInput,
63        kAttribute_ColorInput,
64        kUniform_ColorInput,
65
66        kColorInputCnt
67    };
68
69    enum CoverageOutput {
70        // modulate color and coverage, write result as the color output.
71        kModulate_CoverageOutput,
72        // Writes color*coverage as the primary color output and also writes coverage as the
73        // secondary output. Only set if dual source blending is supported.
74        kSecondaryCoverage_CoverageOutput,
75        // Writes color*coverage as the primary color output and also writes coverage * (1 - colorA)
76        // as the secondary output. Only set if dual source blending is supported.
77        kSecondaryCoverageISA_CoverageOutput,
78        // Writes color*coverage as the primary color output and also writes coverage *
79        // (1 - colorRGB) as the secondary output. Only set if dual source blending is supported.
80        kSecondaryCoverageISC_CoverageOutput,
81        // Combines the coverage, dst, and color as coverage * color + (1 - coverage) * dst. This
82        // can only be set if fDstReadKey is non-zero.
83        kCombineWithDst_CoverageOutput,
84
85        kCoverageOutputCnt
86    };
87
88    static bool CoverageOutputUsesSecondaryOutput(CoverageOutput co) {
89        switch (co) {
90            case kSecondaryCoverage_CoverageOutput: //  fallthru
91            case kSecondaryCoverageISA_CoverageOutput:
92            case kSecondaryCoverageISC_CoverageOutput:
93                return true;
94            default:
95                return false;
96        }
97    }
98
99    /** Non-zero if this stage has an effect */
100    GrGLEffect::EffectKey       fEffectKeys[GrDrawState::kNumStages];
101
102    // To enable experimental geometry shader code (not for use in
103    // production)
104#if GR_GL_EXPERIMENTAL_GS
105    bool                     fExperimentalGS;
106#endif
107
108    GrGLShaderBuilder::DstReadKey fDstReadKey;          // set by GrGLShaderBuilder if there
109                                                        // are effects that must read the dst.
110                                                        // Otherwise, 0.
111    GrGLShaderBuilder::FragPosKey fFragPosKey;          // set by GrGLShaderBuilder if there are
112                                                        // effects that read the fragment position.
113                                                        // Otherwise, 0.
114
115    // should the FS discard if the coverage is zero (to avoid stencil manipulation)
116    SkBool8                     fDiscardIfZeroCoverage;
117
118    uint8_t                     fColorInput;            // casts to enum ColorInput
119    uint8_t                     fCoverageInput;         // casts to enum ColorInput
120    uint8_t                     fCoverageOutput;        // casts to enum CoverageOutput
121
122    int8_t                      fFirstCoverageStage;
123    SkBool8                     fEmitsPointSize;
124    uint8_t                     fColorFilterXfermode;   // casts to enum SkXfermode::Mode
125
126    int8_t                      fPositionAttributeIndex;
127    int8_t                      fLocalCoordAttributeIndex;
128    int8_t                      fColorAttributeIndex;
129    int8_t                      fCoverageAttributeIndex;
130
131    // GrGLProgram and GrGLShaderBuilder read the private fields to generate code. TODO: Move all
132    // code generation to GrGLShaderBuilder (and maybe add getters rather than friending).
133    friend class GrGLProgram;
134    friend class GrGLShaderBuilder;
135};
136
137#endif
138