aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/builders/GrGLProgramBuilder.h
blob: ed4224d9b5f4e4d72d5dc19fc59aa38822275649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrGLProgramBuilder_DEFINED
#define GrGLProgramBuilder_DEFINED

#include "GrGLFragmentShaderBuilder.h"
#include "GrGLGeometryShaderBuilder.h"
#include "GrGLVertexShaderBuilder.h"
#include "../GrGLProgramDataManager.h"
#include "../GrGLUniformHandle.h"

class GrGLInstalledProcessors;

/*
 * This is the base class for a series of interfaces.  This base class *MUST* remain abstract with
 * NO data members because it is used in multiple interface inheritance.
 * Heirarchy:
 *                      GrGLUniformBuilder
 *                     /                  \
 *                GrGLFPBuilder       GrGLGPBuilder
 *                     \                  /
 *                     GrGLProgramBuilder(internal use only)
 */
class GrGLUniformBuilder {
public:
    enum ShaderVisibility {
        kVertex_Visibility   = 0x1,
        kGeometry_Visibility = 0x2,
        kFragment_Visibility = 0x4,
    };

    virtual ~GrGLUniformBuilder() {}

    typedef GrGLProgramDataManager::UniformHandle UniformHandle;

    /** Add a uniform variable to the current program, that has visibility in one or more shaders.
        visibility is a bitfield of ShaderVisibility values indicating from which shaders the
        uniform should be accessible. At least one bit must be set. Geometry shader uniforms are not
        supported at this time. The actual uniform name will be mangled. If outName is not NULL then
        it will refer to the final uniform name after return. Use the addUniformArray variant to add
        an array of uniforms. */
    virtual UniformHandle addUniform(uint32_t visibility,
                                     GrSLType type,
                                     const char* name,
                                     const char** outName = NULL) = 0;
    virtual UniformHandle addUniformArray(uint32_t visibility,
                                          GrSLType type,
                                          const char* name,
                                          int arrayCount,
                                          const char** outName = NULL) = 0;

    virtual const GrGLShaderVar& getUniformVariable(UniformHandle u) const = 0;

    /**
     * Shortcut for getUniformVariable(u).c_str()
     */
    virtual const char* getUniformCStr(UniformHandle u) const = 0;

    virtual const GrGLContextInfo& ctxInfo() const = 0;

    virtual GrGpuGL* gpu() const = 0;

    /*
     * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
     */
};

/* a specialization of the above for GPs.  Lets the user add uniforms, varyings, and VS / FS code */
class GrGLGPBuilder : public virtual GrGLUniformBuilder {
public:
    virtual void addVarying(GrSLType type,
                            const char* name,
                            const char** vsOutName = NULL,
                            const char** fsInName = NULL,
                            GrGLShaderVar::Precision fsPrecision=GrGLShaderVar::kDefault_Precision) = 0;

    // TODO rename getFragmentBuilder
    virtual GrGLGPFragmentBuilder* getFragmentShaderBuilder() = 0;
    virtual GrGLVertexBuilder* getVertexShaderBuilder() = 0;

    /*
     * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
     */
};

/* a specializations for FPs. Lets the user add uniforms and FS code */
class GrGLFPBuilder : public virtual GrGLUniformBuilder {
public:
    virtual GrGLFPFragmentBuilder* getFragmentShaderBuilder() = 0;

    /*
     * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
     */
};

/*
 * Please note - no diamond problems because of virtual inheritance.  Also, both base classes
 * are pure virtual with no data members.  This is the base class for program building.
 * Subclasses are nearly identical but each has their own way of emitting transforms.  State for
 * each of the elements of the shader pipeline, ie vertex, fragment, geometry, etc, lives in those
 * respective builders
*/
class GrGLProgramBuilder : public GrGLGPBuilder,
                           public GrGLFPBuilder {
public:
    /** Generates a shader program.
     *
     * The program implements what is specified in the stages given as input.
     * After successful generation, the builder result objects are available
     * to be used.
     * @return true if generation was successful.
     */
    static GrGLProgram* CreateProgram(const GrOptDrawState&,
                                      const GrGLProgramDesc&,
                                      GrGpu::DrawType,
                                      const GrGeometryStage* inGeometryProcessor,
                                      const GrFragmentStage* inColorStages[],
                                      const GrFragmentStage* inCoverageStages[],
                                      GrGpuGL* gpu);

    virtual UniformHandle addUniform(uint32_t visibility,
                                     GrSLType type,
                                     const char* name,
                                     const char** outName = NULL) SK_OVERRIDE {
        return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName);
    }
    virtual UniformHandle addUniformArray(uint32_t visibility,
                                          GrSLType type,
                                          const char* name,
                                          int arrayCount,
                                          const char** outName = NULL) SK_OVERRIDE;

    virtual const GrGLShaderVar& getUniformVariable(UniformHandle u) const SK_OVERRIDE {
        return fUniforms[u.toShaderBuilderIndex()].fVariable;
    }

    virtual const char* getUniformCStr(UniformHandle u) const SK_OVERRIDE {
        return this->getUniformVariable(u).c_str();
    }

    virtual const GrGLContextInfo& ctxInfo() const SK_OVERRIDE;

    virtual GrGpuGL* gpu() const SK_OVERRIDE { return fGpu; }

    virtual GrGLFragmentShaderBuilder* getFragmentShaderBuilder() SK_OVERRIDE { return &fFS; }
    virtual GrGLVertexBuilder* getVertexShaderBuilder() SK_OVERRIDE { return &fVS; }

    virtual void addVarying(GrSLType type,
                            const char* name,
                            const char** vsOutName = NULL,
                            const char** fsInName = NULL,
                            GrGLShaderVar::Precision fsPrecision=GrGLShaderVar::kDefault_Precision);

    // Handles for program uniforms (other than per-effect uniforms)
    struct BuiltinUniformHandles {
        UniformHandle       fViewMatrixUni;
        UniformHandle       fRTAdjustmentUni;
        UniformHandle       fColorUni;
        UniformHandle       fCoverageUni;

        // We use the render target height to provide a y-down frag coord when specifying
        // origin_upper_left is not supported.
        UniformHandle       fRTHeightUni;

        // Uniforms for computing texture coords to do the dst-copy lookup
        UniformHandle       fDstCopyTopLeftUni;
        UniformHandle       fDstCopyScaleUni;
        UniformHandle       fDstCopySamplerUni;
    };

protected:
    static GrGLProgramBuilder* CreateProgramBuilder(const GrGLProgramDesc&,
                                                    const GrOptDrawState&,
                                                    GrGpu::DrawType,
                                                    bool hasGeometryProcessor,
                                                    GrGpuGL*);

    GrGLProgramBuilder(GrGpuGL*, const GrOptDrawState&, const GrGLProgramDesc&);

    const GrOptDrawState& optState() const { return fOptState; }
    const GrGLProgramDesc& desc() const { return fDesc; }
    const GrGLProgramDesc::KeyHeader& header() const { return fDesc.getHeader(); }

    // Generates a name for a variable. The generated string will be name prefixed by the prefix
    // char (unless the prefix is '\0'). It also mangles the name to be stage-specific if we're
    // generating stage code.
    void nameVariable(SkString* out, char prefix, const char* name);
    void setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* inputColor, GrGLSLExpr4* inputCoverage);
    void createAndEmitProcessors(const GrGeometryStage* geometryProcessor,
                                 const GrFragmentStage* colorStages[],
                                 const GrFragmentStage* coverageStages[],
                                 GrGLSLExpr4* inputColor,
                                 GrGLSLExpr4* inputCoverage);
    template <class ProcessorStage>
    void createAndEmitProcessors(const ProcessorStage*[],
                                 int effectCnt,
                                 const GrGLProgramDesc::EffectKeyProvider&,
                                 GrGLSLExpr4* fsInOutColor,
                                 GrGLInstalledProcessors*);
    void verify(const GrGeometryProcessor&);
    void verify(const GrFragmentProcessor&);
    void emitSamplers(const GrProcessor&,
                      GrGLProcessor::TextureSamplerArray* outSamplers,
                      GrGLInstalledProcessors*);

    // each specific program builder has a distinct transform and must override this function
    virtual void emitTransforms(const GrProcessorStage&,
                                GrGLProcessor::TransformedCoordsArray* outCoords,
                                GrGLInstalledProcessors*);
    GrGLProgram* finalize();
    void bindUniformLocations(GrGLuint programID);
    bool checkLinkStatus(GrGLuint programID);
    void resolveUniformLocations(GrGLuint programID);

    void cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs);
    void cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs);

    // Subclasses create different programs
    virtual GrGLProgram* createProgram(GrGLuint programID);

    void appendUniformDecls(ShaderVisibility, SkString*) const;

    // reset is called by program creator between each processor's emit code.  It increments the
    // stage offset for variable name mangling, and also ensures verfication variables in the
    // fragment shader are cleared.
    void reset() {
        this->enterStage();
        this->addStage();
        fFS.reset();
    }
    void addStage() { fStageIndex++; }

    // This simple class exits the stage and then restores the stage when it goes out of scope
    class AutoStageRestore {
    public:
        AutoStageRestore(GrGLProgramBuilder* pb)
            : fPB(pb), fOutOfStage(pb->fOutOfStage) { pb->exitStage(); }
        ~AutoStageRestore() { fPB->fOutOfStage = fOutOfStage; }
    private:
        GrGLProgramBuilder* fPB;
        bool fOutOfStage;
    };
    class AutoStageAdvance {
    public:
        AutoStageAdvance(GrGLProgramBuilder* pb) : fPB(pb) { fPB->reset(); }
        ~AutoStageAdvance() { fPB->exitStage(); }
    private:
        GrGLProgramBuilder* fPB;
    };
    void exitStage() { fOutOfStage = true; }
    void enterStage() { fOutOfStage = false; }
    int stageIndex() const { return fStageIndex; }

    typedef GrGLProgramDesc::EffectKeyProvider EffectKeyProvider;
    typedef GrGLProgramDataManager::UniformInfo UniformInfo;
    typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray;

    // number of each input/output type in a single allocation block, used by many builders
    static const int kVarsPerBlock;

    BuiltinUniformHandles fUniformHandles;
    GrGLVertexBuilder fVS;
    GrGLGeometryBuilder fGS;
    GrGLFragmentShaderBuilder fFS;
    bool fOutOfStage;
    int fStageIndex;

    SkAutoTUnref<GrGLInstalledProcessors> fGeometryProcessor;
    SkAutoTUnref<GrGLInstalledProcessors> fColorEffects;
    SkAutoTUnref<GrGLInstalledProcessors> fCoverageEffects;

    const GrOptDrawState& fOptState;
    const GrGLProgramDesc& fDesc;
    GrGpuGL* fGpu;
    UniformInfoArray fUniforms;

    friend class GrGLShaderBuilder;
    friend class GrGLVertexBuilder;
    friend class GrGLFragmentShaderBuilder;
    friend class GrGLGeometryBuilder;
};

/**
 * This class encapsulates an array of GrGLProcessors and their supporting data (coord transforms
 * and textures). It is built by GrGLProgramBuilder, then used to manage the necessary GL
 * state and shader uniforms in GLPrograms.  Its just Plain old data, and as such is entirely public
 *
 * TODO We really don't need this class to have an array of processors.  It makes sense for it
 * to just have one, also break out the transforms
 */
class GrGLInstalledProcessors : public SkRefCnt {
public:
    GrGLInstalledProcessors(int reserveCount, bool hasExplicitLocalCoords = false)
        : fGLProcessors(reserveCount)
        , fSamplers(reserveCount)
        , fTransforms(reserveCount)
        , fHasExplicitLocalCoords(hasExplicitLocalCoords) {
    }

    virtual ~GrGLInstalledProcessors();

    typedef GrGLProgramDataManager::UniformHandle UniformHandle;

    struct Sampler {
        SkDEBUGCODE(Sampler() : fTextureUnit(-1) {})
        UniformHandle  fUniform;
        int            fTextureUnit;
    };

    class ShaderVarHandle {
    public:
        bool isValid() const { return fHandle > -1; }
        ShaderVarHandle() : fHandle(-1) {}
        ShaderVarHandle(int value) : fHandle(value) { SkASSERT(this->isValid()); }
        int handle() const { SkASSERT(this->isValid()); return fHandle; }
        UniformHandle convertToUniformHandle() {
            SkASSERT(this->isValid());
            return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex(fHandle);
        }

    private:
        int fHandle;
    };

    struct Transform {
        Transform() : fType(kVoid_GrSLType) { fCurrentValue = SkMatrix::InvalidMatrix(); }
        ShaderVarHandle fHandle;
        SkMatrix       fCurrentValue;
        GrSLType       fType;
    };

    void addEffect(GrGLProcessor* effect) { fGLProcessors.push_back(effect); }
    SkTArray<Sampler, true>& addSamplers() { return fSamplers.push_back(); }
    SkTArray<Transform, true>& addTransforms() { return fTransforms.push_back(); }

    SkTArray<GrGLProcessor*>                 fGLProcessors;
    SkTArray<SkSTArray<4, Sampler, true> >   fSamplers;
    SkTArray<SkSTArray<2, Transform, true> > fTransforms;
    bool                                     fHasExplicitLocalCoords;

    friend class GrGLShaderBuilder;
    friend class GrGLVertexShaderBuilder;
    friend class GrGLFragmentShaderBuilder;
    friend class GrGLGeometryShaderBuilder;
};

#endif