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
|
#include "../utils/macros.h"
#include "jsl.h"
namespace jin
{
namespace render
{
//vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
static const char* base_f = " "
"#version 120 \n"
"#define number float \n"
"#define Image sampler2D \n"
"#define Texel texture2D \n"
"#define extern uniform \n"
"uniform Image _tex0_; \n"
"%s \n"
"void main(){ \n"
"gl_FragColor = effect(gl_Color, _tex0_, gl_TexCoord[0].xy, gl_FragCoord.xy);\n"
"}\0";
shared GLint JSLProgram::_current_texture_unit;
shared GLint JSLProgram::_max_texture_units;
void JSLProgram::init(const char* program)
{
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &_max_texture_units);
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);
}
shared GLint JSLProgram::getTextureUnit(const std::string& name)
{
if (++_current_texture_unit >= _max_texture_units)
return 0;
return _current_texture_unit;
}
void JSLProgram::use()
{
glUseProgram(pid);
_current_texture_unit = 0;
}
shared void JSLProgram::unuse()
{
glUseProgram(0);
}
void JSLProgram::sendFloat(const char* variable, float number)
{
int loc = glGetUniformLocation(pid, variable);
glUniform1f(loc, number);
}
void JSLProgram::sendImage(const char* variable, const Image* image)
{
GLint texture_unit = getTextureUnit(variable);
GLint location = glGetUniformLocation(pid, variable);
glUniform1i(location, texture_unit);
glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(GL_TEXTURE_2D, image->getTexture()); // guarantee it gets bound
// reset texture unit
glActiveTexture(GL_TEXTURE0);
}
}
}
|