From 1b773ad2c250e09c09c065eb3eec64bfebde09ca Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 18 May 2018 14:39:38 +0800 Subject: =?UTF-8?q?=E4=BF=AE=E6=94=B9userdata=E5=88=9B=E5=BB=BA=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/render/jsl.cpp | 54 ++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 24 deletions(-) (limited to 'src/libjin/render/jsl.cpp') diff --git a/src/libjin/render/jsl.cpp b/src/libjin/render/jsl.cpp index 56cbc31..49bb18f 100644 --- a/src/libjin/render/jsl.cpp +++ b/src/libjin/render/jsl.cpp @@ -18,15 +18,31 @@ namespace render "gl_FragColor = effect(gl_Color, _tex0_, gl_TexCoord[0].xy, gl_FragCoord.xy);\n" "}\0"; - shared GLint JSLProgram::current_texture_unit = 0; - shared GLint JSLProgram::max_texture_units = 0; + shared GLint JSLProgram::currentTextureUnit = 0; + shared GLint JSLProgram::maxTextureUnits = -1; - shared JSLProgram* JSLProgram::current_JSL_program = nullptr; + shared JSLProgram* JSLProgram::currentJSLProgram = nullptr; - void JSLProgram::init(const char* program) + JSLProgram::JSLProgram(const char* program) { - if(max_texture_units == 0) - glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units); + initialize(program); + } + + JSLProgram::~JSLProgram() + { + destroy(); + } + + void JSLProgram::destroy() + { + if (currentJSLProgram == this) + unuse(); + } + + 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); @@ -39,28 +55,18 @@ namespace render glLinkProgram(pid); } - shared GLint JSLProgram::getTextureUnit(const std::string& name) + GLint JSLProgram::getTextureUnit(const std::string& name) { - if (++current_texture_unit >= max_texture_units) + std::map::iterator texture_unit = texturePool.find(name); + if (texture_unit != texturePool.end()) + return texture_unit->second; + if (++currentTextureUnit >= maxTextureUnits) return 0; - return current_texture_unit; - } - - void JSLProgram::use() - { - glUseProgram(pid); - JSLProgram::current_JSL_program = this; - JSLProgram::current_texture_unit = 0; - } - - shared void JSLProgram::unuse() - { - glUseProgram(0); - JSLProgram::current_JSL_program = nullptr; - JSLProgram::current_texture_unit = 0; + texturePool[name] = currentTextureUnit; + return currentTextureUnit; } -#define checkJSL() if (current_JSL_program != this) return +#define checkJSL() if (currentJSLProgram != this) return void JSLProgram::sendFloat(const char* variable, float number) { -- cgit v1.1-26-g67d0