aboutsummaryrefslogtreecommitdiffhomepage
path: root/gpu/src/GrGpuGLShaders.h
blob: e5a5665868b58387dd7c0862d5480b609d8378b1 (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
/*
    Copyright 2010 Google Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */


#ifndef GrGpuGLShaders_DEFINED
#define GrGpuGLShaders_DEFINED

#include "GrGpuGL.h"

// Programmable OpenGL or OpenGL ES 2.0
class GrGpuGLShaders : public GrGpuGL {
public:
             GrGpuGLShaders();
    virtual ~GrGpuGLShaders();
    
    virtual void resetContext();

    // type of colors used by a program
    enum ColorType {
        kNone_ColorType,
        kAttrib_ColorType,
        kUniform_ColorType,
    };
protected:
    // overrides from GrGpu
    virtual bool flushGraphicsState(PrimitiveType type);
    virtual void setupGeometry(uint32_t startVertex,
                               uint32_t startIndex,
                               uint32_t vertexCount,
                               uint32_t indexCount);
    
private:
    void resetContextHelper();
    
    // sets the texture matrix uniform for currently bound program
    void flushTexMatrix(GLint location, 
                        GrGLTexture::Orientation orientation);
    // sets the MVP matrix uniform for currently bound program
    void flushMatrix(GLint location);
    
    void flushTwoPointRadial(GLint paramsLocation, const GrSamplerState&);
    
    // reads shader from array and compiles it with GL, returns shader ID or 0 if failed
    GLuint loadShader(GLenum type, const char* src);
    
    struct ProgramData;
    // creates a GL program with two shaders attached. 
    // Gets the relevant uniform locations.
    // Sets the texture sampler if present to texture 0
    // Binds the program
    // returns true if succeeded.
    bool createProgram(GLuint vshader,
                       GLuint fshader,
                       bool hasTexMatrix,
                       bool hasTexCoords,
                       ColorType colorType,
                       bool twoPointRadial,
                       ProgramData* program);

    // called at flush time to setup the appropriate program
    void flushProgram(PrimitiveType type);

    enum Programs {
        // use vertex coordinates         
        kTextureVertCoords_Program = 0,
        kTextureVertCoordsProj_Program,
        
        // use separate tex coords
        kTextureTexCoords_Program,
        kTextureTexCoordsProj_Program,

        // constant color texture, no proj
        // verts as a tex coords
        kTextureVertCoordsNoColor_Program,        

        // constant color texture, no proj
        // separate tex coords
        kTextureTexCoordsNoColor_Program,

        // special program for text glyphs
        kText_Program,

        // programs for radial texture lookup
        kRadialTextureVertCoords_Program,
        kRadialTextureTexCoords_Program,

        // programs for sweep texture lookup
        kSweepTextureVertCoords_Program,
        kSweepTextureTexCoords_Program, 
        
        // programs for two-point radial lookup
        kTwoPointRadialTextureVertCoords_Program,
        kTwoPointRadialTextureTexCoords_Program,
        
        // color only drawing
        kNoTexture_Program,

        kProgramCount
    };

    // Records per-program information
    // we can specify the attribute locations so that they are constant
    // across our shaders. But the driver determines the uniform locations 
    // at link time. We don't need to remember the sampler uniform location
    // because we will bind a texture slot to it and never change it
    // Uniforms are program-local so we can't rely on fHWState to hold the 
    // previous uniform state after a program change.
    struct ProgramData {
        // IDs
        GLuint    fVShaderID;
        GLuint    fFShaderID;
        GLuint    fProgramID;
        
        // shader uniform locations (-1 if shader doesn't use them)
        GLint     fMatrixLocation;
        GLint     fTexMatrixLocation;
        GLint     fColorLocation;
        GLint     fTwoPointParamsLocation;
        
        ColorType fColorType;

        // these reflect the current values of uniforms
        // (GL uniform values travel with program)
        GrMatrix                    fViewMatrix;
        GrMatrix                    fTextureMatrices[kNumStages];
        GrColor                     fColor;
        GrGLTexture::Orientation    fTextureOrientation;
        GrScalar                    fRadial2CenterX1;
        GrScalar                    fRadial2Radius0;
        bool                        fRadial2PosRoot;
    };
    
    ProgramData fPrograms[kProgramCount];
    Programs    fHWProgram;
    
    GrGLTexture::Orientation  fTextureOrientation;

    typedef GrGpuGL INHERITED;
};

#endif