blob: e5a5665868b58387dd7c0862d5480b609d8378b1 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#ifndef GrGpuGLShaders_DEFINED
19#define GrGpuGLShaders_DEFINED
20
21#include "GrGpuGL.h"
22
23// Programmable OpenGL or OpenGL ES 2.0
24class GrGpuGLShaders : public GrGpuGL {
25public:
26 GrGpuGLShaders();
27 virtual ~GrGpuGLShaders();
28
29 virtual void resetContext();
30
31 // type of colors used by a program
32 enum ColorType {
33 kNone_ColorType,
34 kAttrib_ColorType,
35 kUniform_ColorType,
36 };
37protected:
38 // overrides from GrGpu
39 virtual bool flushGraphicsState(PrimitiveType type);
40 virtual void setupGeometry(uint32_t startVertex,
41 uint32_t startIndex,
42 uint32_t vertexCount,
43 uint32_t indexCount);
44
45private:
46 void resetContextHelper();
47
48 // sets the texture matrix uniform for currently bound program
49 void flushTexMatrix(GLint location,
50 GrGLTexture::Orientation orientation);
51 // sets the MVP matrix uniform for currently bound program
52 void flushMatrix(GLint location);
53
54 void flushTwoPointRadial(GLint paramsLocation, const GrSamplerState&);
55
56 // reads shader from array and compiles it with GL, returns shader ID or 0 if failed
57 GLuint loadShader(GLenum type, const char* src);
58
59 struct ProgramData;
60 // creates a GL program with two shaders attached.
61 // Gets the relevant uniform locations.
62 // Sets the texture sampler if present to texture 0
63 // Binds the program
64 // returns true if succeeded.
65 bool createProgram(GLuint vshader,
66 GLuint fshader,
67 bool hasTexMatrix,
68 bool hasTexCoords,
69 ColorType colorType,
70 bool twoPointRadial,
71 ProgramData* program);
72
73 // called at flush time to setup the appropriate program
74 void flushProgram(PrimitiveType type);
75
76 enum Programs {
77 // use vertex coordinates
78 kTextureVertCoords_Program = 0,
79 kTextureVertCoordsProj_Program,
80
81 // use separate tex coords
82 kTextureTexCoords_Program,
83 kTextureTexCoordsProj_Program,
84
85 // constant color texture, no proj
86 // verts as a tex coords
87 kTextureVertCoordsNoColor_Program,
88
89 // constant color texture, no proj
90 // separate tex coords
91 kTextureTexCoordsNoColor_Program,
92
93 // special program for text glyphs
94 kText_Program,
95
96 // programs for radial texture lookup
97 kRadialTextureVertCoords_Program,
98 kRadialTextureTexCoords_Program,
99
100 // programs for sweep texture lookup
101 kSweepTextureVertCoords_Program,
102 kSweepTextureTexCoords_Program,
103
104 // programs for two-point radial lookup
105 kTwoPointRadialTextureVertCoords_Program,
106 kTwoPointRadialTextureTexCoords_Program,
107
108 // color only drawing
109 kNoTexture_Program,
110
111 kProgramCount
112 };
113
114 // Records per-program information
115 // we can specify the attribute locations so that they are constant
116 // across our shaders. But the driver determines the uniform locations
117 // at link time. We don't need to remember the sampler uniform location
118 // because we will bind a texture slot to it and never change it
119 // Uniforms are program-local so we can't rely on fHWState to hold the
120 // previous uniform state after a program change.
121 struct ProgramData {
122 // IDs
123 GLuint fVShaderID;
124 GLuint fFShaderID;
125 GLuint fProgramID;
126
127 // shader uniform locations (-1 if shader doesn't use them)
128 GLint fMatrixLocation;
129 GLint fTexMatrixLocation;
130 GLint fColorLocation;
131 GLint fTwoPointParamsLocation;
132
133 ColorType fColorType;
134
135 // these reflect the current values of uniforms
136 // (GL uniform values travel with program)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000137 GrMatrix fViewMatrix;
138 GrMatrix fTextureMatrices[kNumStages];
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 GrColor fColor;
140 GrGLTexture::Orientation fTextureOrientation;
141 GrScalar fRadial2CenterX1;
142 GrScalar fRadial2Radius0;
143 bool fRadial2PosRoot;
144 };
145
146 ProgramData fPrograms[kProgramCount];
147 Programs fHWProgram;
148
149 GrGLTexture::Orientation fTextureOrientation;
150
151 typedef GrGpuGL INHERITED;
152};
153
154#endif