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
|
#include "../modules.h"
#if JIN_MODULES_RENDER
#include "../utils/macros.h"
#include "jsl.h"
namespace jin
{
namespace graphics
{
static const char* base_f = R"base_f(
#define number float
#define Texture sampler2D
#define Canvas sampler2D
#define Color vec4
#define Texel texture2D
#define extern uniform
uniform Texture _tex0_;
%s
void main()
{
gl_FragColor = effect(gl_Color, _tex0_, gl_TexCoord[0].xy, gl_FragCoord.xy);
}
)base_f";
/*static*/ JSLProgram* JSLProgram::currentJSLProgram = nullptr;
JSLProgram* JSLProgram::createJSLProgram(const char* program)
{
return new JSLProgram(program);
}
JSLProgram::JSLProgram(const char* program)
: currentTextureUnit(0)
{
char* fs = (char*)alloca(strlen(program) + strlen(base_f));
sprintf(fs, base_f, program);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, (const GLchar**)&fs, NULL);
glCompileShader(fragmentShader);
pid = glCreateProgram();
glAttachShader(pid, fragmentShader);
glLinkProgram(pid);
}
JSLProgram::~JSLProgram()
{
if (currentJSLProgram == this)
unuse();
}
static inline GLint getMaxTextureUnits()
{
GLint maxTextureUnits = 0;
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
return maxTextureUnits;
}
GLint JSLProgram::getTextureUnit(const std::string& name)
{
std::map<std::string, GLint>::iterator texture_unit = texturePool.find(name);
if (texture_unit != texturePool.end())
return texture_unit->second;
static GLint maxTextureUnits = getMaxTextureUnits();
if (++currentTextureUnit >= maxTextureUnits)
return 0;
texturePool[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);
}
void JSLProgram::sendTexture(const char* variable, const Texture* tex)
{
checkJSL();
GLint location = glGetUniformLocation(pid, variable);
if (location == -1)
return;
GLint texture_unit = getTextureUnit(variable);
glUniform1i(location, texture_unit);
glActiveTexture(GL_TEXTURE0 + texture_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 texture_unit = getTextureUnit(variable);
glUniform1i(location, texture_unit);
glActiveTexture(GL_TEXTURE0 + texture_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->rgba.r / 255.f,
col->rgba.g / 255.f,
col->rgba.b / 255.f,
col->rgba.a / 255.f
);
}
} // graphics
} // jin
#endif // JIN_MODULES_RENDER
|