blob: a048e93b6a5a764b066c8dddea1f27014cc18aea [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();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000028
reed@google.comac10a2d2010-12-22 21:39:39 +000029 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);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000040 virtual void setupGeometry(int* startVertex,
41 int* startIndex,
42 int vertexCount,
43 int indexCount);
44
reed@google.comac10a2d2010-12-22 21:39:39 +000045private:
46 void resetContextHelper();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000047
reed@google.comac10a2d2010-12-22 21:39:39 +000048 // sets the texture matrix uniform for currently bound program
bsalomon@google.com1c13c962011-02-14 16:51:21 +000049 void flushTexMatrix(GLint location,
reed@google.comac10a2d2010-12-22 21:39:39 +000050 GrGLTexture::Orientation orientation);
51 // sets the MVP matrix uniform for currently bound program
52 void flushMatrix(GLint location);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000053
reed@google.comac10a2d2010-12-22 21:39:39 +000054 void flushTwoPointRadial(GLint paramsLocation, const GrSamplerState&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000055
reed@google.comac10a2d2010-12-22 21:39:39 +000056 // reads shader from array and compiles it with GL, returns shader ID or 0 if failed
57 GLuint loadShader(GLenum type, const char* src);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000058
reed@google.comac10a2d2010-12-22 21:39:39 +000059 struct ProgramData;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000060 // creates a GL program with two shaders attached.
reed@google.comac10a2d2010-12-22 21:39:39 +000061 // 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 {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000077 // use vertex coordinates
reed@google.comac10a2d2010-12-22 21:39:39 +000078 kTextureVertCoords_Program = 0,
79 kTextureVertCoordsProj_Program,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000080
reed@google.comac10a2d2010-12-22 21:39:39 +000081 // use separate tex coords
82 kTextureTexCoords_Program,
83 kTextureTexCoordsProj_Program,
84
85 // constant color texture, no proj
86 // verts as a tex coords
bsalomon@google.com1c13c962011-02-14 16:51:21 +000087 kTextureVertCoordsNoColor_Program,
reed@google.comac10a2d2010-12-22 21:39:39 +000088
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,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000102 kSweepTextureTexCoords_Program,
103
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 // programs for two-point radial lookup
105 kTwoPointRadialTextureVertCoords_Program,
106 kTwoPointRadialTextureTexCoords_Program,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000107
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 // 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
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000116 // across our shaders. But the driver determines the uniform locations
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 // 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
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000119 // Uniforms are program-local so we can't rely on fHWState to hold the
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 // previous uniform state after a program change.
121 struct ProgramData {
122 // IDs
123 GLuint fVShaderID;
124 GLuint fFShaderID;
125 GLuint fProgramID;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000126
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 // shader uniform locations (-1 if shader doesn't use them)
128 GLint fMatrixLocation;
129 GLint fTexMatrixLocation;
130 GLint fColorLocation;
131 GLint fTwoPointParamsLocation;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000132
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 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 };
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000145
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 ProgramData fPrograms[kProgramCount];
147 Programs fHWProgram;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000148
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 GrGLTexture::Orientation fTextureOrientation;
150
151 typedef GrGpuGL INHERITED;
152};
153
154#endif