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
|
/***************************************************************************
*
* Copyright 2010,2011 BMW Car IT GmbH
* Copyright (C) 2012 DENSO CORPORATION and Robert Bosch Car Multimedia Gmbh
* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
*
*
* 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 _SHADERPROGRAM_H_
#define _SHADERPROGRAM_H_
#include <string>
using std::string;
#include "ShaderProgramList.h"
class ShaderProgram;
typedef ShaderProgram* (*PfnShaderProgramCreator)(const string&, const string&);
#define MAX_MULTI_SURFACE 2
/**
* Represents an OpenGL shader program.
*
* There is a global list of allocated shader programs. Each instance is reference
* counted, so if program is no longer in use by any shader instance, it will
* be deleted automatically.
*/
class ShaderProgram
{
public:
/**
* Stores uniform values for common surface properties,
* like position, size, opacity, etc...
*/
struct CommonUniforms
{
float x;
float y;
float width;
float height;
float* matrix;
float chromaKey[3];
bool chromaKeyEnabled;
// Per-texture uniforms
float opacity[MAX_MULTI_SURFACE];
float texRange[MAX_MULTI_SURFACE][2];
float texOffset[MAX_MULTI_SURFACE][2];
int texUnit[MAX_MULTI_SURFACE];
};
/**
* Return a shader program from the gobal list. If no matching instance is found, a new
* one will be created and added to the list.
*
* @param vertFileName File name of vertex shader.
* @param fragFileName File name of fragment shader.
* @return new Program instance, NULL if shader could not be loaded, compiled or linked.
*/
static ShaderProgram* obtain(const string& vertFileName, const string& fragFileName);
/**
* Destructor
*/
virtual ~ShaderProgram();
/**
* Start using the shader program for rendering.
*/
virtual void use(void) const = 0;
/**
* Load uniform values for common surface properties, like position,
* size, opacity, etc...
*
* @param uniforms Uniform values
* @param texCount texture count
*/
void loadCommonUniforms(const CommonUniforms& uniforms, const int texCount) const;
/**
* @return The name of the vertex shader
*/
const string& getVertexName(void) const
{
return m_vertFileName;
}
/**
* @return The name of the fragment shader
*/
const string& getFragmentName(void) const
{
return m_fragFileName;
}
/**
* Increment usage counter for this object.
*/
void ref(void);
/**
* Decrement usage counter for this object.
* It will be delete if usage counter reaches 0.
*/
void unref(void);
virtual int getUniformLocation(const char* name) = 0;
virtual void uniform1iv(int location, int count, const int* v) const = 0;
virtual void uniform1fv(int location, int count, const float* v) const = 0;
virtual void uniform2fv(int location, int count, const float* v) const = 0;
virtual void uniform3fv(int location, int count, const float* v) const = 0;
virtual void uniform4fv(int location, int count, const float* v) const = 0;
virtual void uniformMatrix2fv(int location, int count, bool transpose, const float* v) const = 0;
virtual void uniformMatrix3fv(int location, int count, bool transpose, const float* v) const = 0;
virtual void uniformMatrix4fv(int location, int count, bool transpose, const float* v) const = 0;
protected:
/**
* Protected constructor.
* New instances of this class are supposed to be created by the shader program factory.
*
* @param vertFileName File name of vertex shader.
* @param fragFileName File name of fragment shader.
*/
ShaderProgram(const string& vertFileName, const string& fragFileName);
/**
* Update the list of uniform locations
*/
void updateCommonUniformLocations(void);
private:
/// usage counter
int m_referenceCounter;
/// name of the vertex shader
string m_vertFileName;
/// name of the fragment shader
string m_fragFileName;
// uniform locations
int m_xLoc;
int m_yLoc;
int m_widthLoc;
int m_heightLoc;
int m_matrixLoc;
// per-texture uniforms
int m_opacityLoc;
int m_texRangeLoc;
int m_texOffsetLoc;
int m_texUnitLoc;
int m_chromaKeyLoc;
/// global list of programs
static ShaderProgramList m_programList;
};
#endif /* _SHADERPROGRAM_H_ */
|