diff --git a/src/WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.cpp b/src/WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.cpp index 995fc68..df33ec8 100644 --- a/src/WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.cpp +++ b/src/WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.cpp @@ -5,4 +5,9 @@ using namespace WallpaperEngine::Core::Objects::Effects::Constants; CShaderConstant::CShaderConstant (std::string type) : m_type (std::move(type)) { -} \ No newline at end of file +} + +const std::string& CShaderConstant::getType () const +{ + return this->m_type; +} diff --git a/src/WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.h b/src/WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.h index 9d2688a..00e6bc8 100644 --- a/src/WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.h +++ b/src/WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.h @@ -13,6 +13,7 @@ namespace WallpaperEngine::Core::Objects::Effects::Constants template T* as () { assert (is ()); return (T*) this; } template bool is () { return this->m_type == T::Type; } + const std::string& getType () const; private: std::string m_type; diff --git a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp index a762c29..1bfb684 100644 --- a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp +++ b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp @@ -90,66 +90,6 @@ void CPass::render (GLuint drawTo, GLuint input) } } - // now bind variables needed - { - auto cur = this->m_variables.begin (); - auto end = this->m_variables.end (); - - for (; cur != end; cur ++) - { -#ifdef DEBUG - std::cout << "(" << (*cur).second->getIdentifierName() << ") - " << (*cur).second->getName () << " = "; -#endif /* DEBUG */ - - if ((*cur).second->is () == true) - { - GLint value = *static_cast ((*cur).second->getValue ()); -#ifdef DEBUG - std::cout << value << "\n"; -#endif /* DEBUG */ - glUniform1i ((*cur).first, value); - } - else if ((*cur).second->is () == true) - { - GLfloat value = *static_cast ((*cur).second->getValue ()); -#ifdef DEBUG - std::cout << value << "\n"; -#endif /* DEBUG */ - glUniform1f ((*cur).first, value); - } - else if ((*cur).second->is () == true) - { - glm::vec2 value = *static_cast ((*cur).second-> as ()->getValue ()); -#ifdef DEBUG - std::cout << value.x << " " << value.y << "\n"; -#endif /* DEBUG */ - glUniform2fv ((*cur).first, 1, glm::value_ptr (value)); - } - else if ((*cur).second->is () == true) - { - glm::vec3 value = *static_cast ((*cur).second-> as ()->getValue ()); -#ifdef DEBUG - std::cout << value.x << " " << value.y << " " << value.z << "\n"; -#endif /* DEBUG */ - glUniform3fv ((*cur).first, 1, glm::value_ptr (value)); - } - else if ((*cur).second->is () == true) - { - glm::vec4 value = *static_cast ((*cur).second-> as ()->getValue ()); -#ifdef DEBUG - std::cout << value.x << " " << value.y << " " << value.z << " " << value.w << "\n"; -#endif /* DEBUG */ - glUniform4fv ((*cur).first, 1, glm::value_ptr (value)); - } - else - { -#ifdef DEBUG - std::cout << "null\n"; -#endif /* DEBUG */ - } - } - } - // add uniforms { auto cur = this->m_uniforms.begin (); @@ -237,137 +177,6 @@ void CPass::render (GLuint drawTo, GLuint input) glDisableVertexAttribArray (this->a_TexCoord); } -/* -void CPass::OnSetConstants (irr::video::IMaterialRendererServices *services, int32_t userData) -{ - // TODO: REWRITE - const Core::Objects::CImage* image = this->m_material->getImage ()->getImage (); - - irr::video::IVideoDriver* driver = services->getVideoDriver (); - - irr::core::matrix4 worldViewProj; - worldViewProj = driver->getTransform (irr::video::ETS_PROJECTION); - worldViewProj *= driver->getTransform (irr::video::ETS_VIEW); - worldViewProj *= driver->getTransform (irr::video::ETS_WORLD); - - auto cur = this->m_vertShader->getParameters ().begin (); - auto end = this->m_vertShader->getParameters ().end (); - - for (; cur != end; cur ++) - { - if ((*cur)->is () == true) - { - services->setVertexShaderConstant ( - (*cur)->getName ().c_str (), - (irr::s32*) (*cur)->getValue (), - (*cur)->getSize () - ); - } - else if ( - (*cur)->is () == true || - (*cur)->is () == true || - (*cur)->is () == true || - (*cur)->is () == true) - { - services->setVertexShaderConstant ( - (*cur)->getName ().c_str (), - (float*) (*cur)->getValue (), - (*cur)->getSize () - ); - } - } - - cur = this->m_fragShader->getParameters ().begin (); - end = this->m_fragShader->getParameters ().end (); - - for (; cur != end; cur ++) - { - if ((*cur)->is () == true) - { - services->setPixelShaderConstant ( - (*cur)->getName ().c_str (), - (irr::s32*) (*cur)->getValue (), - (*cur)->getSize () - ); - } - else if ( - (*cur)->is () == true || - (*cur)->is () == true || - (*cur)->is () == true || - (*cur)->is () == true) - { - services->setPixelShaderConstant ( - (*cur)->getName ().c_str (), - (float*) (*cur)->getValue (), - (*cur)->getSize () - ); - } - } - - cur = this->m_context->getShaderVariables ().begin (); - end = this->m_context->getShaderVariables ().end (); - - for (; cur != end; cur ++) - { - if ((*cur)->is () == true) - { - services->setPixelShaderConstant ( - (*cur)->getName ().c_str (), - (float*) (*cur)->getValue (), - (*cur)->getSize () - ); - services->setVertexShaderConstant ( - (*cur)->getName ().c_str (), - (float*) (*cur)->getValue (), - (*cur)->getSize () - ); - } - } - - services->setVertexShaderConstant ("g_ModelViewProjectionMatrix", worldViewProj.pointer(), 16); - - auto textureCur = this->m_textures.begin (); - auto textureEnd = this->m_textures.end (); - - char resolution [22]; - char sampler [12]; - char rotation [20]; - char translation [23]; - - float textureRotation [4] = { image->getAngles ().X, image->getAngles ().Y, image->getAngles ().Z, image->getAngles ().Z }; - - for (int index = 0; textureCur != textureEnd; textureCur ++, index ++) - { - // TODO: CHECK THESE VALUES, DOCUMENTATION SAYS THAT FIRST TWO ELEMENTS SHOULD BE WIDTH AND HEIGHT - // TODO: BUT IN REALITY THEY DO NOT SEEM TO BE THAT, NOT HAVING SUPPORT FOR ATTRIBUTES DOESN'T HELP EITHER - float textureResolution [4] = { - 1.0, -1.0, 1.0, 1.0 - }; - float textureTranslation [2] = { - 0, 0 - }; - - sprintf (resolution, "g_Texture%dResolution", index); - sprintf (sampler, "g_Texture%d", index); - sprintf (rotation, "g_Texture%dRotation", index); - sprintf (translation, "g_Texture%dTranslation", index); - - services->setVertexShaderConstant (resolution, textureResolution, 4); - services->setPixelShaderConstant (resolution, textureResolution, 4); - services->setVertexShaderConstant (sampler, &index, 1); - services->setPixelShaderConstant (sampler, &index, 1); - services->setVertexShaderConstant (rotation, textureRotation, 4); - services->setPixelShaderConstant (rotation, textureRotation, 4); - services->setVertexShaderConstant (translation, textureTranslation, 2); - services->setPixelShaderConstant (translation, textureTranslation, 2); - } - - // set variables for time - services->setVertexShaderConstant ("g_Time", &g_Time, 1); - services->setPixelShaderConstant ("g_Time", &g_Time, 1); -} -*/ - GLuint CPass::compileShader (Render::Shaders::Compiler* shader, GLuint type) { // reserve shaders in OpenGL @@ -560,51 +369,31 @@ void CPass::setupShaderVariables () CShaderVariable* vertexVar = this->m_vertShader->findParameter ((*cur).first); CShaderVariable* pixelVar = this->m_fragShader->findParameter ((*cur).first); - if (pixelVar) + // variable not found, can be ignored + if (vertexVar == nullptr && pixelVar == nullptr) + continue; + + // if both can be found, ensure they're the correct type + if (vertexVar != nullptr && pixelVar != nullptr) { - if (pixelVar->is () && (*cur).second->is ()) - { - pixelVar->as ()->setValue (*(*cur).second->as ()->getValue ()); - } - else if (pixelVar->is () && (*cur).second->is ()) - { - pixelVar->as ()->setValue (*(*cur).second->as ()->getValue ()); - } - else if (pixelVar->is () && (*cur).second->is ()) - { - pixelVar->as ()->setValue (*(*cur).second->as ()->getValue ()); - } - - // get the uniform first - GLint uniform = glGetUniformLocation (this->m_programID, pixelVar->getName ().c_str ()); - - if (uniform != -1) - // register the variable - this->m_variables.insert (std::make_pair (uniform, pixelVar)); + if (vertexVar->getType () != pixelVar->getType ()) + throw std::runtime_error ("Pixel and vertex shader variable types do not match"); } - if (vertexVar) - { - if (vertexVar->is () && (*cur).second->is ()) - { - vertexVar->as ()->setValue (*(*cur).second->as ()->getValue ()); - } - else if (vertexVar->is () && (*cur).second->is ()) - { - vertexVar->as ()->setValue (*(*cur).second->as ()->getValue ()); - } - else if (vertexVar->is () && (*cur).second->is ()) - { - vertexVar->as ()->setValue (*(*cur).second->as ()->getValue ()); - } + // get one instance of it + CShaderVariable* var = vertexVar == nullptr ? pixelVar : vertexVar; - // get the uniform first - GLint uniform = glGetUniformLocation (this->m_programID, vertexVar->getName ().c_str ()); + // ensure the shader's and the constant are of the same type + if ((*cur).second->getType () != var->getType ()) + throw std::runtime_error ("Constant and pixel/vertex variable are not of the same type"); - if (uniform != -1) - // register the variable - this->m_variables.insert (std::make_pair (uniform, vertexVar)); - } + // now determine the constant's type and register the correct uniform for it + if ((*cur).second->is ()) + this->addUniform (var->getName (), (*cur).second->as ()->getValue ()); + else if ((*cur).second->is ()) + this->addUniform (var->getName (), (*cur).second->as ()->getValue ()); + else if ((*cur).second->is ()) + this->addUniform (var->getName (), (*cur).second->as ()->getValue ()); } } diff --git a/src/WallpaperEngine/Render/Objects/Effects/CPass.h b/src/WallpaperEngine/Render/Objects/Effects/CPass.h index b6d2bf8..f0b67a6 100644 --- a/src/WallpaperEngine/Render/Objects/Effects/CPass.h +++ b/src/WallpaperEngine/Render/Objects/Effects/CPass.h @@ -71,7 +71,6 @@ namespace WallpaperEngine::Render::Objects::Effects CMaterial* m_material; Core::Objects::Images::Materials::CPass* m_pass; std::vector m_textures; - std::map m_variables; std::map m_attribs; std::vector m_uniforms; glm::mat4 m_modelViewProjectionMatrix; diff --git a/src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.cpp b/src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.cpp index a6782af..e79539c 100644 --- a/src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.cpp +++ b/src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.cpp @@ -35,6 +35,11 @@ const std::string& CShaderVariable::getName () const return this->m_name; } +const std::string& CShaderVariable::getType () const +{ + return this->m_type; +} + void CShaderVariable::setIdentifierName (std::string identifierName) { this->m_identifierName = std::move(identifierName); diff --git a/src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.h b/src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.h index a100936..89ce20f 100644 --- a/src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.h +++ b/src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.h @@ -16,6 +16,7 @@ namespace WallpaperEngine::Render::Shaders::Variables const std::string& getIdentifierName () const; const std::string& getName () const; + const std::string& getType () const; void setIdentifierName (std::string identifierName); void setName (std::string name);