aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/GrGLProgramEffects.h
blob: 6140cde88f7128c96f20a69acc84276a9695c47d (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
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrGLProgramEffects_DEFINED
#define GrGLProgramEffects_DEFINED

#include "GrBackendProcessorFactory.h"
#include "GrGLProgramDataManager.h"
#include "GrGpu.h"
#include "GrTexture.h"
#include "GrTextureAccess.h"

class GrProcessor;
class GrProcessorStage;
class GrGLVertexProgramEffectsBuilder;
class GrGLProgramBuilder;
class GrGLFullProgramBuilder;
class GrGLFragmentOnlyProgramBuilder;

/**
 * This class encapsulates an array of GrGLProcessors and their supporting data (coord transforms
 * and textures). It is built with GrGLProgramEffectsBuilder, then used to manage the necessary GL
 * state and shader uniforms.
 */
class GrGLProgramEffects : public SkRefCnt {
public:
    typedef GrGLProgramDataManager::UniformHandle UniformHandle;
    typedef GrGLProgramDataManager::VaryingHandle VaryingHandle;
    virtual ~GrGLProgramEffects();

    /**
     * Assigns a texture unit to each sampler. It starts on *texUnitIdx and writes the next
     * available unit to *texUnitIdx when it returns.
     */
    void initSamplers(const GrGLProgramDataManager&, int* texUnitIdx);

    /**
     * Calls setData() on each effect, and sets their transformation matrices and texture bindings.
     */
    virtual void setData(GrGpuGL*,
                         GrGpu::DrawType,
                         const GrGLProgramDataManager&,
                         const GrGeometryStage* effectStages) {
        SkFAIL("For geometry processor only");
    }

    virtual void setData(GrGpuGL*,
                         GrGpu::DrawType,
                         const GrGLProgramDataManager&,
                         const GrFragmentStage* effectStages[]) = 0;

protected:
    GrGLProgramEffects(int reserveCount)
        : fGLProcessors(reserveCount)
        , fSamplers(reserveCount) {
    }

    /**
     * Helper for setData(). Binds all the textures for an effect.
     */
    void bindTextures(GrGpuGL*, const GrProcessor&, int effectIdx);

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

    /*
     * Helpers for shader builders to build up program effects objects alongside shader code
     */
    void addEffect(GrGLProcessor* effect) { fGLProcessors.push_back(effect); }
    SkTArray<Sampler, true>& addSamplers() { return fSamplers.push_back(); }

    SkTArray<GrGLProcessor*>               fGLProcessors;
    SkTArray<SkSTArray<4, Sampler, true> > fSamplers;

private:
    friend class GrGLProgramBuilder;
    friend class GrGLFullProgramBuilder;
    friend class GrGLFragmentOnlyShaderBuilder;

    typedef SkRefCnt INHERITED;
};

////////////////////////////////////////////////////////////////////////////////

/**
 * This is a GrGLProgramEffects implementation that does coord transforms with the vertex shader.
 */
class GrGLVertexProgramEffects : public GrGLProgramEffects {
public:
    virtual void setData(GrGpuGL*,
                         GrGpu::DrawType,
                         const GrGLProgramDataManager&,
                         const GrGeometryStage* effectStages) SK_OVERRIDE;

    virtual void setData(GrGpuGL*,
                         GrGpu::DrawType,
                         const GrGLProgramDataManager&,
                         const GrFragmentStage* effectStages[]) SK_OVERRIDE;

private:
    GrGLVertexProgramEffects(int reserveCount, bool explicitLocalCoords)
        : INHERITED(reserveCount)
        , fTransforms(reserveCount)
        , fHasExplicitLocalCoords(explicitLocalCoords) {
    }

    struct Transform {
        Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); }
        UniformHandle fHandle;
        SkMatrix      fCurrentValue;
    };

    struct PathTransform {
        PathTransform() { fCurrentValue = SkMatrix::InvalidMatrix(); }
        VaryingHandle fHandle;
        SkMatrix fCurrentValue;
        GrSLType fType;
    };

    /*
     * These functions are used by the builders to build up program effects along side the shader
     * code itself
     */
    SkSTArray<2, Transform, true>& addTransforms() { return fTransforms.push_back(); }
    SkTArray<PathTransform, true>& addPathTransforms() { return fPathTransforms.push_back(); }

    /**
     * Helper for setData(). Sets all the transform matrices for an effect.
     */
    void setDataInternal(GrGpuGL* gpu,
                         GrGpu::DrawType drawType,
                         const GrGLProgramDataManager& programDataManager,
                         const GrProcessorStage& effectStage,
                         int index);
    void setTransformData(GrGpuGL* gpu, const GrGLProgramDataManager&, const GrProcessorStage&,
                          int effectIdx);
    void setPathTransformData(GrGpuGL* gpu, const GrGLProgramDataManager&,
                              const GrProcessorStage&, int effectIdx);


    SkTArray<SkSTArray<2, Transform, true> > fTransforms;
    SkTArray<SkTArray<PathTransform, true> > fPathTransforms;
    bool                                     fHasExplicitLocalCoords;

    friend class GrGLFullProgramBuilder;

    typedef GrGLProgramEffects INHERITED;
};

////////////////////////////////////////////////////////////////////////////////

/**
 * This is a GrGLProgramEffects implementation that does coord transforms with
 * the the  NV_path_rendering PathTexGen functionality.
 */
class GrGLPathTexGenProgramEffects : public GrGLProgramEffects {
public:
    virtual void setData(GrGpuGL*,
                         GrGpu::DrawType,
                         const GrGLProgramDataManager&,
                         const GrFragmentStage* effectStages[]) SK_OVERRIDE;

private:
    GrGLPathTexGenProgramEffects(int reserveCount)
        : INHERITED(reserveCount)
        , fTransforms(reserveCount) {
    }

    /**
     * Helper for setData(). Sets the PathTexGen state for each transform in an effect.
     */
    void setPathTexGenState(GrGpuGL*, const GrProcessorStage&, int effectIdx);

    struct Transforms {
        Transforms(int texCoordIndex)
            : fTexCoordIndex(texCoordIndex) {}
        int fTexCoordIndex;
    };

    /*
     * Helper for fragment only shader builder to build up the program effects alongside the shader
     */
    void addTransforms(int coordIndex) {
        fTransforms.push_back(Transforms(coordIndex));
    }

    SkTArray<Transforms> fTransforms;

    friend class GrGLFragmentOnlyProgramBuilder;

    typedef GrGLProgramEffects INHERITED;
};

#endif