detach shaders automatically after linking

This commit is contained in:
Jupeyy 2017-09-14 19:34:14 +02:00
parent 4fec86a354
commit b6e4b4b4a6
3 changed files with 32 additions and 40 deletions

View file

@ -609,10 +609,6 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
m_pPrimitiveProgram->AddShader(&PrimitiveFragmentShader);
m_pPrimitiveProgram->LinkProgram();
//detach shader, they are not needed anymore after linking
m_pPrimitiveProgram->DetachShader(&PrimitiveVertexShader);
m_pPrimitiveProgram->DetachShader(&PrimitiveFragmentShader);
m_pPrimitiveProgram->UseProgram();
m_pPrimitiveProgram->m_LocPos = m_pPrimitiveProgram->GetUniformLoc("Pos");
@ -630,10 +626,6 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
m_pTileProgram->AddShader(&FragmentShader);
m_pTileProgram->LinkProgram();
//detach shader, they are not needed anymore after linking
m_pTileProgram->DetachShader(&VertexShader);
m_pTileProgram->DetachShader(&FragmentShader);
m_pTileProgram->UseProgram();
m_pTileProgram->m_LocPos = m_pTileProgram->GetUniformLoc("Pos");
@ -653,10 +645,6 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
m_pTileProgramTextured->AddShader(&FragmentShader);
m_pTileProgramTextured->LinkProgram();
//detach shader, they are not needed anymore after linking
m_pTileProgramTextured->DetachShader(&VertexShader);
m_pTileProgramTextured->DetachShader(&FragmentShader);
m_pTileProgramTextured->UseProgram();
m_pTileProgramTextured->m_LocPos = m_pTileProgramTextured->GetUniformLoc("Pos");
@ -676,10 +664,6 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
m_pBorderTileProgram->AddShader(&FragmentShader);
m_pBorderTileProgram->LinkProgram();
//detach shader, they are not needed anymore after linking
m_pBorderTileProgram->DetachShader(&VertexShader);
m_pBorderTileProgram->DetachShader(&FragmentShader);
m_pBorderTileProgram->UseProgram();
m_pBorderTileProgram->m_LocPos = m_pBorderTileProgram->GetUniformLoc("Pos");
@ -702,10 +686,6 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
m_pBorderTileProgramTextured->AddShader(&FragmentShader);
m_pBorderTileProgramTextured->LinkProgram();
//detach shader, they are not needed anymore after linking
m_pBorderTileProgramTextured->DetachShader(&VertexShader);
m_pBorderTileProgramTextured->DetachShader(&FragmentShader);
m_pBorderTileProgramTextured->UseProgram();
m_pBorderTileProgramTextured->m_LocPos = m_pBorderTileProgramTextured->GetUniformLoc("Pos");
@ -728,10 +708,6 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
m_pBorderTileLineProgram->AddShader(&FragmentShader);
m_pBorderTileLineProgram->LinkProgram();
//detach shader, they are not needed anymore after linking
m_pBorderTileLineProgram->DetachShader(&VertexShader);
m_pBorderTileLineProgram->DetachShader(&FragmentShader);
m_pBorderTileLineProgram->UseProgram();
m_pBorderTileLineProgram->m_LocPos = m_pBorderTileLineProgram->GetUniformLoc("Pos");
@ -752,10 +728,6 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
m_pBorderTileLineProgramTextured->AddShader(&FragmentShader);
m_pBorderTileLineProgramTextured->LinkProgram();
//detach shader, they are not needed anymore after linking
m_pBorderTileLineProgramTextured->DetachShader(&VertexShader);
m_pBorderTileLineProgramTextured->DetachShader(&FragmentShader);
m_pBorderTileLineProgramTextured->UseProgram();
m_pBorderTileLineProgramTextured->m_LocPos = m_pBorderTileLineProgramTextured->GetUniformLoc("Pos");
@ -806,15 +778,6 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
void CCommandProcessorFragment_OpenGL3_3::Cmd_Shutdown(const SCommand_Shutdown *pCommand)
{
//clean up everything
m_pPrimitiveProgram->DeleteProgram();
//m_QuadProgram->DeleteProgram();
m_pTileProgram->DeleteProgram();
m_pTileProgramTextured->DeleteProgram();
m_pBorderTileProgram->DeleteProgram();
m_pBorderTileProgramTextured->DeleteProgram();
m_pBorderTileLineProgram->DeleteProgram();
m_pBorderTileLineProgramTextured->DeleteProgram();
delete m_pPrimitiveProgram;
//delete m_QuadProgram;
delete m_pTileProgram;

View file

@ -28,10 +28,15 @@ void CGLSLProgram::DetachShader(CGLSL* pShader)
{
if (pShader->IsLoaded())
{
glDetachShader(m_ProgramID, pShader->GetShaderID());
DetachShaderByID(pShader->GetShaderID());
}
}
void CGLSLProgram::DetachShaderByID(GLuint ShaderID)
{
glDetachShader(m_ProgramID, ShaderID);
}
void CGLSLProgram::LinkProgram()
{
glLinkProgram(m_ProgramID);
@ -47,6 +52,28 @@ void CGLSLProgram::LinkProgram()
str_format(sFinalMessage, 1536, "Error! Shader program wasn't linked! The linker returned:\n\n%s", sInfoLog);
dbg_msg("GLSL Program", sFinalMessage);
}
//detach all shaders attached to this program
DetachAllShaders();
}
void CGLSLProgram::DetachAllShaders(){
GLuint aShaders[100];
GLsizei ReturnedCount = 0;
while(1)
{
glGetAttachedShaders(m_ProgramID, 100, &ReturnedCount, aShaders);
if(ReturnedCount > 0)
{
for(GLsizei i = 0; i < ReturnedCount; ++i)
{
DetachShaderByID(aShaders[i]);
}
}
if(ReturnedCount < 100) break;
}
}
void CGLSLProgram::SetUniformVec4(int Loc, int Count, const float* Value)

View file

@ -16,6 +16,8 @@ public:
GLuint GetProgramID();
void DetachShader(CGLSL* pShader);
void DetachShaderByID(GLuint ShaderID);
void DetachAllShaders();
//Support various types
void SetUniformVec2(int Loc, int Count, const float* Value);
@ -29,7 +31,7 @@ public:
int GetUniformLoc(const char* Name);
CGLSLProgram();
~CGLSLProgram();
virtual ~CGLSLProgram();
protected:
GLuint m_ProgramID;