From 7a904de2e1826f44547d6749b76e539573295f98 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 1 Sep 2016 18:32:12 -0300 Subject: [PATCH] In fact, lets just replace Shader:getUniformVariable with Shader:hasUniform. The latter returns true if the uniform with the specified name exists and contributes to the final output of the shader. --HG-- branch : minor --- src/modules/graphics/opengl/Shader.cpp | 16 ++--------- src/modules/graphics/opengl/Shader.h | 13 ++------- src/modules/graphics/opengl/wrap_Shader.cpp | 32 +++------------------ 3 files changed, 9 insertions(+), 52 deletions(-) diff --git a/src/modules/graphics/opengl/Shader.cpp b/src/modules/graphics/opengl/Shader.cpp index 176d1deeb..f10df4c74 100644 --- a/src/modules/graphics/opengl/Shader.cpp +++ b/src/modules/graphics/opengl/Shader.cpp @@ -621,21 +621,9 @@ int Shader::getTextureUnit(const std::string &name) return texunit; } -Shader::UniformType Shader::getUniformVariable(const std::string &name, int &components, int &count) +bool Shader::hasUniform(const std::string &name) const { - auto it = uniforms.find(name); - - if (it == uniforms.end()) - { - components = 0; - count = 0; - return UNIFORM_UNKNOWN; - } - - components = getUniformTypeSize(it->second.type); - count = (int) it->second.count; - - return it->second.baseType; + return uniforms.find(name) != uniforms.end(); } GLint Shader::getAttribLocation(const std::string &name) diff --git a/src/modules/graphics/opengl/Shader.h b/src/modules/graphics/opengl/Shader.h index dafe43753..77020c1a5 100644 --- a/src/modules/graphics/opengl/Shader.h +++ b/src/modules/graphics/opengl/Shader.h @@ -168,17 +168,10 @@ public: void sendTexture(const std::string &name, Texture *texture); /** - * Gets the type, number of components, and number of array elements of - * an active uniform variable in the shader. If a uniform variable with the - * specified name doesn't exist, returns UNIFORM_UNKNOWN and sets the - * 'components' and 'count' values to 0. - * - * @param name The name of the uniform variable in the source code. - * @param[out] components Number of components of the variable (2 for vec2.) - * @param[out] count Number of array elements, if the variable is an array. - * @return The base type of the uniform variable. + * Gets whether a uniform with the specified name exists and is actively + * used in the shader. **/ - UniformType getUniformVariable(const std::string &name, int &components, int &count); + bool hasUniform(const std::string &name) const; GLint getAttribLocation(const std::string &name); diff --git a/src/modules/graphics/opengl/wrap_Shader.cpp b/src/modules/graphics/opengl/wrap_Shader.cpp index ea2488b8a..be53d1c81 100644 --- a/src/modules/graphics/opengl/wrap_Shader.cpp +++ b/src/modules/graphics/opengl/wrap_Shader.cpp @@ -324,36 +324,12 @@ int w_Shader_send(lua_State *L) return luaL_argerror(L, 3, "number, boolean, table, image, or canvas expected"); } -int w_Shader_getUniformVariable(lua_State *L) +int w_Shader_hasUniform(lua_State *L) { Shader *shader = luax_checkshader(L, 1); const char *name = luaL_checkstring(L, 2); - - int components = 0; - int arrayelements = 0; - Shader::UniformType type = Shader::UNIFORM_UNKNOWN; - - type = shader->getUniformVariable(name, components, arrayelements); - - // Check if the variable exists (function will set components to 0 if not.) - if (components > 0) - { - const char *tname = nullptr; - if (!Shader::getConstant(type, tname)) - return luaL_error(L, "Unknown uniform variable type name."); - - lua_pushstring(L, tname); - lua_pushinteger(L, components); - lua_pushinteger(L, arrayelements); - } - else - { - lua_pushnil(L); - lua_pushnil(L); - lua_pushnil(L); - } - - return 3; + luax_pushboolean(L, shader->hasUniform(name)); + return 1; } static const luaL_Reg w_Shader_functions[] = @@ -366,7 +342,7 @@ static const luaL_Reg w_Shader_functions[] = { "sendMatrix", w_Shader_sendMatrix }, { "sendTexture", w_Shader_sendTexture }, { "send", w_Shader_send }, - { "getUniformVariable", w_Shader_getUniformVariable }, + { "hasUniform", w_Shader_hasUniform }, { 0, 0 } };