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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
#include "../../core/je_configuration.h"
#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader)
#include <iostream>
#include "../../filesystem/je_buffer.h"
#include "../../utils/je_macros.h"
#include "je_jsl_compiler.h"
#include "je_shader.h"
using namespace std;
using namespace JinEngine::Filesystem;
namespace JinEngine
{
namespace Graphics
{
namespace Shaders
{
//
// default_texture
// base_shader
// SHADER_FORMAT_SIZE
// formatShader
//
#include "built-in/je_default.shader.h"
//
// https://stackoverflow.com/questions/27941496/use-sampler-without-passing-through-value
// The default value of a sampler variable is 0. From the GLSL 3.30 spec,
// section "4.3.5 Uniforms":
//
// The link time initial value is either the value of the variable's
// initializer, if present, or 0 if no initializer is present.Sampler
// types cannot have initializers.
//
// Since a value of 0 means that it's sampling from texture unit 0, it will
// work without ever setting the value as long as you bind your textures to
// unit 0. This is well defined behavior.
//
// Since texture unit 0 is also the default until you call glActiveTexture()
// with a value other than GL_TEXTURE0, it's very common to always use unit
// 0 as long as shaders do not need more than one texture.Which means that
// often times, setting the sampler uniforms is redundant for simple
// applications.
//
// I would still prefer to always set the values.If nothing else, it makes
// it clear to anybody reading your code that you really mean to sample from
// texture unit 0, and did not just forget to set the value.
//
const int DEFAULT_TEXTURE_UNIT = 0;
/*static*/ Shader* Shader::CurrentShader = nullptr;
Shader* Shader::createShader(const string& program)
{
Shader* shader = nullptr;
try
{
shader = new Shader(program);
}
catch (...)
{
return nullptr;
}
return shader;
}
Shader::Shader(const string& program)
: mCurrentTextureUnit(DEFAULT_TEXTURE_UNIT)
{
if (!compile(program))
throw 0;
}
Shader::~Shader()
{
if (CurrentShader == this)
unuse();
// delete shader program
glDeleteShader(mPID);
}
bool Shader::compile(const string& program)
{
string vertex_shader, fragment_shader;
// Compile JSL shader source into GLSL shader source.
JSLCompiler* compiler = JSLCompiler::get();
if (!compiler->compile(program, &vertex_shader, &fragment_shader))
{
return false;
}
#define glsl(SHADER_MODE, SHADER, SRC) \
do{ \
const GLchar* src = SRC.c_str(); \
glShaderSource(SHADER, 1, &src, NULL); \
glCompileShader(SHADER); \
GLint success; \
glGetShaderiv(SHADER, GL_COMPILE_STATUS, &success); \
if (success == GL_FALSE) \
return false; \
}while(0)
// Compile vertex shader.
GLuint vid = glCreateShader(GL_VERTEX_SHADER);
glsl(GL_VERTEX_SHADER, vid, vertex_shader);
// Compile fragment shader.
GLuint fid = glCreateShader(GL_FRAGMENT_SHADER);
glsl(GL_FRAGMENT_SHADER, fid, fragment_shader);
#undef glsl
// Create OpenGL shader program.
mPID = glCreateProgram();
glAttachShader(mPID, vid);
glAttachShader(mPID, fid);
glLinkProgram(mPID);
GLint success;
glGetProgramiv(mPID, GL_LINK_STATUS, &success);
if (success == GL_FALSE)
return false;
}
static inline GLint getMaxTextureUnits()
{
GLint maxTextureUnits = 0;
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
return maxTextureUnits;
}
void Shader::use()
{
glUseProgram(mPID);
CurrentShader = this;
sendInt(SHADER_MAIN_TEXTURE, DEFAULT_TEXTURE_UNIT);
}
/*static*/ void Shader::unuse()
{
glUseProgram(0);
CurrentShader = nullptr;
}
GLint Shader::claimTextureUnit(const std::string& name)
{
std::map<std::string, GLint>::iterator unit = mTextureUnits.find(name);
if (unit != mTextureUnits.end())
return unit->second;
static GLint MAX_TEXTURE_UNITS = getMaxTextureUnits();
if (++mCurrentTextureUnit >= MAX_TEXTURE_UNITS)
return 0;
mTextureUnits[name] = mCurrentTextureUnit;
return mCurrentTextureUnit;
}
#define checkJSL() \
if (CurrentShader != this) \
return
void Shader::sendInt(const char* name, int value)
{
checkJSL();
int loc = glGetUniformLocation(mPID, name);
glUniform1i(loc, value);
}
void Shader::sendFloat(const char* variable, float number)
{
checkJSL();
int loc = glGetUniformLocation(mPID, variable);
glUniform1f(loc, number);
}
//
// https://www.douban.com/note/627332677/
// struct TextureUnit
// {
// GLuint targetTexture1D;
// GLuint targetTexture2D;
// GLuint targetTexture3D;
// GLuint targetTextureCube;
// ...
// };
//
// TextureUnit mTextureUnits[GL_MAX_TEXTURE_IMAGE_UNITS]
// GLuint mCurrentTextureUnit = 0;
//
void Shader::sendTexture(const char* variable, const Texture* tex)
{
checkJSL();
GLint location = glGetUniformLocation(mPID, variable);
if (location == -1)
return;
GLint unit = claimTextureUnit(variable);
if (unit == 0)
{
// TODO: 쳣
return;
}
gl.activeTexUnit(unit);
glUniform1i(location, unit);
gl.bindTexture(tex->getGLTexture());
gl.activeTexUnit(0);
}
void Shader::sendCanvas(const char* variable, const Canvas* canvas)
{
checkJSL();
GLint location = glGetUniformLocation(mPID, variable);
if (location == -1)
return;
GLint unit = claimTextureUnit(variable);
if (unit == 0)
{
// TODO: 쳣
return;
}
glUniform1i(location, unit);
glActiveTexture(GL_TEXTURE0 + unit);
gl.bindTexture(canvas->getGLTexture());
glActiveTexture(GL_TEXTURE0);
}
void Shader::sendVec2(const char* name, float x, float y)
{
checkJSL();
int loc = glGetUniformLocation(mPID, name);
glUniform2f(loc, x, y);
}
void Shader::sendVec3(const char* name, float x, float y, float z)
{
checkJSL();
int loc = glGetUniformLocation(mPID, name);
glUniform3f(loc, x, y, z);
}
void Shader::sendVec4(const char* name, float x, float y, float z, float w)
{
checkJSL();
int loc = glGetUniformLocation(mPID, name);
glUniform4f(loc, x, y, z, w);
}
void Shader::sendColor(const char* name, const Color* col)
{
checkJSL();
int loc = glGetUniformLocation(mPID, name);
glUniform4f(loc,
col->r / 255.f,
col->g / 255.f,
col->b / 255.f,
col->a / 255.f
);
}
void Shader::sendMatrix4(const char* name, const Math::Matrix* mat4)
{
int loc = glGetUniformLocation(mPID, name);
glUniformMatrix4fv(loc, 1, GL_FALSE, mat4->getElements());
}
void Shader::bindVertexPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers)
{
GLint loc = glGetAttribLocation(mPID, SHADER_VERTEX_COORDS);
glEnableVertexAttribArray(0);
glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers);
}
void Shader::bindUVPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers)
{
GLint loc = glGetAttribLocation(mPID, SHADER_TEXTURE_COORDS);
glEnableVertexAttribArray(1);
glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers);
}
} // namespace Shaders
} // namespace Graphics
} // namespace JinEngine
#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader)
|