- removed duplicated list of variables and unified under the new way of registering uniforms, simplifying required code

- removed commented out code on CPass as there's already almost equivalent code working

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
This commit is contained in:
Alexis Maiquez 2021-09-01 01:48:21 +02:00
parent 7613bf254a
commit eb29078cfe
6 changed files with 33 additions and 233 deletions

View File

@ -6,3 +6,8 @@ CShaderConstant::CShaderConstant (std::string type) :
m_type (std::move(type)) m_type (std::move(type))
{ {
} }
const std::string& CShaderConstant::getType () const
{
return this->m_type;
}

View File

@ -13,6 +13,7 @@ namespace WallpaperEngine::Core::Objects::Effects::Constants
template<class T> T* as () { assert (is <T> ()); return (T*) this; } template<class T> T* as () { assert (is <T> ()); return (T*) this; }
template<class T> bool is () { return this->m_type == T::Type; } template<class T> bool is () { return this->m_type == T::Type; }
const std::string& getType () const;
private: private:
std::string m_type; std::string m_type;

View File

@ -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 <CShaderVariableInteger> () == true)
{
GLint value = *static_cast <const int32_t*> ((*cur).second->getValue ());
#ifdef DEBUG
std::cout << value << "\n";
#endif /* DEBUG */
glUniform1i ((*cur).first, value);
}
else if ((*cur).second->is <CShaderVariableFloat> () == true)
{
GLfloat value = *static_cast <const float*> ((*cur).second->getValue ());
#ifdef DEBUG
std::cout << value << "\n";
#endif /* DEBUG */
glUniform1f ((*cur).first, value);
}
else if ((*cur).second->is <CShaderVariableVector2> () == true)
{
glm::vec2 value = *static_cast <const glm::vec2*> ((*cur).second-> as <CShaderVariableVector2> ()->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 <CShaderVariableVector3> () == true)
{
glm::vec3 value = *static_cast <const glm::vec3*> ((*cur).second-> as <CShaderVariableVector3> ()->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 <CShaderVariableVector4> () == true)
{
glm::vec4 value = *static_cast <const glm::vec4*> ((*cur).second-> as <CShaderVariableVector4> ()->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 // add uniforms
{ {
auto cur = this->m_uniforms.begin (); auto cur = this->m_uniforms.begin ();
@ -237,137 +177,6 @@ void CPass::render (GLuint drawTo, GLuint input)
glDisableVertexAttribArray (this->a_TexCoord); 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 <CShaderVariableInteger> () == true)
{
services->setVertexShaderConstant (
(*cur)->getName ().c_str (),
(irr::s32*) (*cur)->getValue (),
(*cur)->getSize ()
);
}
else if (
(*cur)->is <CShaderVariableFloat> () == true ||
(*cur)->is <CShaderVariableVector2> () == true ||
(*cur)->is <CShaderVariableVector3> () == true ||
(*cur)->is <CShaderVariableVector4> () == 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 <CShaderVariableInteger> () == true)
{
services->setPixelShaderConstant (
(*cur)->getName ().c_str (),
(irr::s32*) (*cur)->getValue (),
(*cur)->getSize ()
);
}
else if (
(*cur)->is <CShaderVariableFloat> () == true ||
(*cur)->is <CShaderVariableVector2> () == true ||
(*cur)->is <CShaderVariableVector3> () == true ||
(*cur)->is <CShaderVariableVector4> () == 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 <CShaderVariableFloatPointer> () == 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) GLuint CPass::compileShader (Render::Shaders::Compiler* shader, GLuint type)
{ {
// reserve shaders in OpenGL // reserve shaders in OpenGL
@ -560,51 +369,31 @@ void CPass::setupShaderVariables ()
CShaderVariable* vertexVar = this->m_vertShader->findParameter ((*cur).first); CShaderVariable* vertexVar = this->m_vertShader->findParameter ((*cur).first);
CShaderVariable* pixelVar = this->m_fragShader->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 <CShaderVariableFloat> () && (*cur).second->is <CShaderConstantFloat> ()) if (vertexVar->getType () != pixelVar->getType ())
{ throw std::runtime_error ("Pixel and vertex shader variable types do not match");
pixelVar->as <CShaderVariableFloat> ()->setValue (*(*cur).second->as <CShaderConstantFloat> ()->getValue ());
}
else if (pixelVar->is <CShaderVariableInteger> () && (*cur).second->is <CShaderConstantInteger> ())
{
pixelVar->as <CShaderVariableInteger> ()->setValue (*(*cur).second->as <CShaderConstantInteger> ()->getValue ());
}
else if (pixelVar->is <CShaderVariableVector3> () && (*cur).second->is <CShaderConstantVector3> ())
{
pixelVar->as <CShaderVariableVector3> ()->setValue (*(*cur).second->as <CShaderConstantVector3> ()->getValue ());
} }
// get the uniform first // get one instance of it
GLint uniform = glGetUniformLocation (this->m_programID, pixelVar->getName ().c_str ()); CShaderVariable* var = vertexVar == nullptr ? pixelVar : vertexVar;
if (uniform != -1) // ensure the shader's and the constant are of the same type
// register the variable if ((*cur).second->getType () != var->getType ())
this->m_variables.insert (std::make_pair (uniform, pixelVar)); throw std::runtime_error ("Constant and pixel/vertex variable are not of the same type");
}
if (vertexVar) // now determine the constant's type and register the correct uniform for it
{ if ((*cur).second->is <CShaderConstantFloat> ())
if (vertexVar->is <CShaderVariableFloat> () && (*cur).second->is <CShaderConstantFloat> ()) this->addUniform (var->getName (), (*cur).second->as <CShaderConstantFloat> ()->getValue ());
{ else if ((*cur).second->is <CShaderConstantInteger> ())
vertexVar->as <CShaderVariableFloat> ()->setValue (*(*cur).second->as <CShaderConstantFloat> ()->getValue ()); this->addUniform (var->getName (), (*cur).second->as <CShaderConstantInteger> ()->getValue ());
} else if ((*cur).second->is <CShaderConstantVector3> ())
else if (vertexVar->is <CShaderVariableInteger> () && (*cur).second->is <CShaderConstantInteger> ()) this->addUniform (var->getName (), (*cur).second->as <CShaderConstantVector3> ()->getValue ());
{
vertexVar->as <CShaderVariableInteger> ()->setValue (*(*cur).second->as <CShaderConstantInteger> ()->getValue ());
}
else if (vertexVar->is <CShaderVariableVector3> () && (*cur).second->is <CShaderConstantVector3> ())
{
vertexVar->as <CShaderVariableVector3> ()->setValue (*(*cur).second->as <CShaderConstantVector3> ()->getValue ());
}
// get the uniform first
GLint uniform = glGetUniformLocation (this->m_programID, vertexVar->getName ().c_str ());
if (uniform != -1)
// register the variable
this->m_variables.insert (std::make_pair (uniform, vertexVar));
}
} }
} }

View File

@ -71,7 +71,6 @@ namespace WallpaperEngine::Render::Objects::Effects
CMaterial* m_material; CMaterial* m_material;
Core::Objects::Images::Materials::CPass* m_pass; Core::Objects::Images::Materials::CPass* m_pass;
std::vector<CTexture*> m_textures; std::vector<CTexture*> m_textures;
std::map<GLint,CShaderVariable*> m_variables;
std::map<GLint,CShaderVariable*> m_attribs; std::map<GLint,CShaderVariable*> m_attribs;
std::vector<UniformEntry*> m_uniforms; std::vector<UniformEntry*> m_uniforms;
glm::mat4 m_modelViewProjectionMatrix; glm::mat4 m_modelViewProjectionMatrix;

View File

@ -35,6 +35,11 @@ const std::string& CShaderVariable::getName () const
return this->m_name; return this->m_name;
} }
const std::string& CShaderVariable::getType () const
{
return this->m_type;
}
void CShaderVariable::setIdentifierName (std::string identifierName) void CShaderVariable::setIdentifierName (std::string identifierName)
{ {
this->m_identifierName = std::move(identifierName); this->m_identifierName = std::move(identifierName);

View File

@ -16,6 +16,7 @@ namespace WallpaperEngine::Render::Shaders::Variables
const std::string& getIdentifierName () const; const std::string& getIdentifierName () const;
const std::string& getName () const; const std::string& getName () const;
const std::string& getType () const;
void setIdentifierName (std::string identifierName); void setIdentifierName (std::string identifierName);
void setName (std::string name); void setName (std::string name);