aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/render/jsl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/render/jsl.cpp')
-rw-r--r--src/libjin/render/jsl.cpp51
1 files changed, 26 insertions, 25 deletions
diff --git a/src/libjin/render/jsl.cpp b/src/libjin/render/jsl.cpp
index 3702e14..e883104 100644
--- a/src/libjin/render/jsl.cpp
+++ b/src/libjin/render/jsl.cpp
@@ -6,25 +6,23 @@ 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 Canvas sampler2D \n"
- "#define Color vec4 \n"
- "#define Texel texture2D \n"
- "#define extern uniform \n"
- "uniform Image _tex0_; \n"
- "%s \n"
- "void main(){ \n"
+ "#version 120 \n"
+ "#define number float \n"
+ "#define Image sampler2D \n"
+ "#define Canvas sampler2D \n"
+ "#define Color vec4 \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::currentTextureUnit = 0;
- shared GLint JSLProgram::maxTextureUnits = -1;
-
shared JSLProgram* JSLProgram::currentJSLProgram = nullptr;
JSLProgram::JSLProgram(const char* program)
+ : currentTextureUnit(0)
{
initialize(program);
}
@@ -42,9 +40,6 @@ namespace render
inline void JSLProgram::initialize(const char* program)
{
- if (maxTextureUnits == -1)
- glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
-
char* fs = (char*)alloca(strlen(program) + strlen(base_f));
sprintf(fs, base_f, program);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
@@ -55,12 +50,20 @@ namespace render
glAttachShader(pid, fragmentShader);
glLinkProgram(pid);
}
+
+ 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;
@@ -81,14 +84,13 @@ namespace render
{
checkJSL();
- GLint texture_unit = JSLProgram::getTextureUnit(variable);
-
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, image->getTexture());
-
+ glBindTexture(GL_TEXTURE_2D, image->getTexture());
glActiveTexture(GL_TEXTURE0);
}
@@ -96,14 +98,13 @@ namespace render
{
checkJSL();
- GLint texture_unit = getTextureUnit(variable);
-
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);
}