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
|
#include "../modules.h"
#if JIN_MODULES_RENDER
#include "../utils/macros.h"
#include "Shader.h"
namespace jin
{
namespace graphics
{
#include "base.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_TEX_UNIT = 0;
/*static*/ JSLProgram* JSLProgram::currentJSLProgram = nullptr;
JSLProgram* JSLProgram::createJSLProgram(const char* program)
{
return new JSLProgram(program);
}
JSLProgram::JSLProgram(const char* program)
: currentTextureUnit(DEFAULT_TEX_UNIT)
{
char* fs = (char*)calloc(1, strlen(program) + SHADER_FORMAT_SIZE);
formatShader(fs, program);
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shader, 1, (const GLchar**)&fs, NULL);
glCompileShader(shader);
pid = glCreateProgram();
glAttachShader(pid, shader);
glLinkProgram(pid);
free(fs);
}
JSLProgram::~JSLProgram()
{
if (currentJSLProgram == this)
unuse();
}
static inline GLint getMaxTextureUnits()
{
GLint maxTextureUnits = 0;
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
return maxTextureUnits;
}
void JSLProgram::use()
{
glUseProgram(pid);
currentJSLProgram = this;
bindDefaultTexture();
}
/*static*/ void JSLProgram::unuse()
{
glUseProgram(0);
currentJSLProgram = nullptr;
}
void JSLProgram::bindDefaultTexture()
{
int loc = glGetUniformLocation(pid, default_tex);
glUniform1i(loc, DEFAULT_TEX_UNIT);
}
GLint JSLProgram::claimTextureUnit(const std::string& name)
{
std::map<std::string, GLint>::iterator unit = textureUnits.find(name);
if (unit != textureUnits.end())
return unit->second;
static GLint MAX_TEXTURE_UNITS = getMaxTextureUnits();
if (++currentTextureUnit >= MAX_TEXTURE_UNITS)
return 0;
textureUnits[name] = currentTextureUnit;
return currentTextureUnit;
}
#define checkJSL() \
if (currentJSLProgram != this) \
return
void JSLProgram::sendFloat(const char* variable, float number)
{
checkJSL();
int loc = glGetUniformLocation(pid, variable);
glUniform1f(loc, number);
}
/**
* https://www.douban.com/note/627332677/
* struct TextureUnit
* {
* GLuint targetTexture1D;
* GLuint targetTexture2D;
* GLuint targetTexture3D;
* GLuint targetTextureCube;
* ...
* };
*
* TextureUnit textureUnits[GL_MAX_TEXTURE_IMAGE_UNITS]
* GLuint currentTextureUnit = 0;
*/
void JSLProgram::sendTexture(const char* variable, const Texture* tex)
{
checkJSL();
GLint location = glGetUniformLocation(pid, variable);
if (location == -1)
return;
GLint unit = claimTextureUnit(variable);
if (unit == 0)
{
// TODO: 쳣
return;
}
glUniform1i(location, unit);
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, tex->getTexture());
glActiveTexture(GL_TEXTURE0);
}
void JSLProgram::sendCanvas(const char* variable, const Canvas* canvas)
{
checkJSL();
GLint location = glGetUniformLocation(pid, variable);
if (location == -1)
return;
GLint unit = claimTextureUnit(variable);
if (unit == 0)
{
// TODO: 쳣
return;
}
glUniform1i(location, unit);
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, canvas->getTexture());
glActiveTexture(GL_TEXTURE0);
}
void JSLProgram::sendVec2(const char* name, float x, float y)
{
checkJSL();
int loc = glGetUniformLocation(pid, name);
glUniform2f(loc, x, y);
}
void JSLProgram::sendVec3(const char* name, float x, float y, float z)
{
checkJSL();
int loc = glGetUniformLocation(pid, name);
glUniform3f(loc, x, y, z);
}
void JSLProgram::sendVec4(const char* name, float x, float y, float z, float w)
{
checkJSL();
int loc = glGetUniformLocation(pid, name);
glUniform4f(loc, x, y, z, w);
}
void JSLProgram::sendColor(const char* name, const Color* col)
{
checkJSL();
int loc = glGetUniformLocation(pid, name);
glUniform4f(loc,
col->r / 255.f,
col->g / 255.f,
col->b / 255.f,
col->a / 255.f
);
}
} // graphics
} // jin
#endif // JIN_MODULES_RENDER
|