Better store vertex and pixel programs separately so we can detach them if needed.

--HG--
branch : gsoc2013-dfighter
hg/feature/gsoc2013-dfighter
dfighter1985 12 years ago
parent f5358f712c
commit 41e84066fb

@ -27,6 +27,9 @@ namespace NL3D
public: public:
CGLSLPixelProgram(); CGLSLPixelProgram();
~CGLSLPixelProgram(); ~CGLSLPixelProgram();
bool isVertexProgram() const{ return false; }
bool isPixelProgram() const{ return true; }
}; };
} }

@ -39,14 +39,17 @@ namespace NL3D
programId = 0; programId = 0;
} }
bool CGLSLProgram::attachShader( CGLSLShaderBase *shader ) bool CGLSLProgram::attachVertexProgram( CGLSLShaderBase *shader )
{ {
if( !shader->isVertexProgram() )
return false;
if( !shader->isCompiled() ) if( !shader->isCompiled() )
return false; return false;
std::vector< CGLSLShaderBase* >::const_iterator itr = std::vector< CGLSLShaderBase* >::const_iterator itr =
std::find( attachedShaders.begin(), attachedShaders.end(), shader ); std::find( vertexPrograms.begin(), vertexPrograms.end(), shader );
if( itr != attachedShaders.end() ) if( itr != vertexPrograms.end() )
return false; return false;
nglAttachShader( programId, shader->getShaderId() ); nglAttachShader( programId, shader->getShaderId() );
@ -55,14 +58,81 @@ namespace NL3D
if( error != 0 ) if( error != 0 )
return false; return false;
attachedShaders.push_back( shader ); vertexPrograms.push_back( shader );
return true;
}
bool CGLSLProgram::attachPixelProgram( CGLSLShaderBase *shader )
{
if( !shader->isPixelProgram() )
return false;
if( !shader->isCompiled() )
return false;
std::vector< CGLSLShaderBase* >::const_iterator itr =
std::find( pixelPrograms.begin(), pixelPrograms.end(), shader );
if( itr != pixelPrograms.end() )
return false;
nglAttachShader( programId, shader->getShaderId() );
GLenum error = glGetError();
if( error != GL_NO_ERROR )
return false;
pixelPrograms.push_back( shader );
return true;
}
bool CGLSLProgram::detachVertexProgram( CGLSLShaderBase *shader )
{
if( !shader->isVertexProgram() )
return false;
std::vector< CGLSLShaderBase* >::iterator itr =
std::find( vertexPrograms.begin(), vertexPrograms.end(), shader );
if( itr == vertexPrograms.end() )
return false;
nglDetachShader( programId, shader->getShaderId() );
GLenum error = glGetError();
if( error != GL_NO_ERROR )
return false;
vertexPrograms.erase( itr );
return true;
}
bool CGLSLProgram::detachPixelProgram( CGLSLShaderBase *shader )
{
if( !shader->isPixelProgram() )
return false;
std::vector< CGLSLShaderBase* >::iterator itr =
std::find( pixelPrograms.begin(), pixelPrograms.end(), shader );
if( itr == pixelPrograms.end() )
return false;
nglDetachShader( programId, shader->getShaderId() );
GLenum error = glGetError();
if( error != GL_NO_ERROR )
return false;
pixelPrograms.erase( itr );
return true; return true;
} }
bool CGLSLProgram::link( std::string &log ) bool CGLSLProgram::link( std::string &log )
{ {
if( attachedShaders.empty() ) if( vertexPrograms.empty() || pixelPrograms.empty() )
return false; return false;
nglLinkProgram( programId ); nglLinkProgram( programId );
@ -85,8 +155,17 @@ namespace NL3D
void CGLSLProgram::deleteShaders() void CGLSLProgram::deleteShaders()
{ {
std::vector< CGLSLShaderBase* >::iterator itr = attachedShaders.begin(); std::vector< CGLSLShaderBase* >::iterator itr;
while( itr != attachedShaders.end() )
itr = vertexPrograms.begin();
while( itr != vertexPrograms.end() )
{
delete *itr;
++itr;
}
itr = pixelPrograms.begin();
while( itr != pixelPrograms.end() )
{ {
delete *itr; delete *itr;
++itr; ++itr;

@ -32,7 +32,12 @@ namespace NL3D
CGLSLProgram(); CGLSLProgram();
~CGLSLProgram(); ~CGLSLProgram();
bool attachShader( CGLSLShaderBase *shader ); bool attachVertexProgram( CGLSLShaderBase *shader );
bool attachPixelProgram( CGLSLShaderBase *shader );
bool detachVertexProgram( CGLSLShaderBase *shader );
bool detachPixelProgram( CGLSLShaderBase *shader );
bool link( std::string &log ); bool link( std::string &log );
unsigned int getProgramId() const{ return programId; } unsigned int getProgramId() const{ return programId; }
@ -44,7 +49,8 @@ namespace NL3D
unsigned int programId; unsigned int programId;
bool linked; bool linked;
std::vector< CGLSLShaderBase* > attachedShaders; std::vector< CGLSLShaderBase* > vertexPrograms;
std::vector< CGLSLShaderBase* > pixelPrograms;
}; };
} }

@ -34,6 +34,9 @@ namespace NL3D
void shaderSource( const char *source ); void shaderSource( const char *source );
bool compile( std::string &log ); bool compile( std::string &log );
virtual bool isVertexProgram() const = 0;
virtual bool isPixelProgram() const = 0;
unsigned int getShaderId() const{ return shaderId; } unsigned int getShaderId() const{ return shaderId; }
bool isCompiled() const{ return compiled; } bool isCompiled() const{ return compiled; }

@ -28,6 +28,10 @@ namespace NL3D
public: public:
CGLSLVertexProgram(); CGLSLVertexProgram();
~CGLSLVertexProgram(); ~CGLSLVertexProgram();
bool isVertexProgram() const{ return true; }
bool isPixelProgram() const{ return false; }
}; };
} }

@ -35,60 +35,60 @@ namespace NL3D
} }
const char *vs = const char *vs =
"#version 330\n" "#version 330\n"
"in vec4 vertex;\n" "in vec4 vertex;\n"
"void main( void )\n" "void main( void )\n"
"{\n" "{\n"
"gl_Position = vertex;\n" "gl_Position = vertex;\n"
"}\n"; "}\n";
const char *fs = const char *fs =
"#version 330\n" "#version 330\n"
"out vec4 color;\n" "out vec4 color;\n"
"void main( void )\n" "void main( void )\n"
"{\n" "{\n"
"color = vec4( 1.0, 0.0, 0.0, 1.0 );\n" "color = vec4( 1.0, 0.0, 0.0, 1.0 );\n"
"}\n"; "}\n";
bool CDriverGL3::renderRawTriangles2( CMaterial &mat, uint32 startIndex, uint32 numTris ) bool CDriverGL3::renderRawTriangles2( CMaterial &mat, uint32 startIndex, uint32 numTris )
{ {
std::string log; std::string log;
CGLSLProgram program; CGLSLProgram program;
CGLSLVertexProgram *vp = new CGLSLVertexProgram(); CGLSLVertexProgram *vp = new CGLSLVertexProgram();
CGLSLPixelProgram *pp = new CGLSLPixelProgram(); CGLSLPixelProgram *pp = new CGLSLPixelProgram();
vp->shaderSource( vs ); vp->shaderSource( vs );
if( !vp->compile( log ) ) if( !vp->compile( log ) )
{ {
nlinfo( "%s", log.c_str() ); nlinfo( "%s", log.c_str() );
return false; return false;
} }
pp->shaderSource( fs ); pp->shaderSource( fs );
if( !pp->compile( log ) ) if( !pp->compile( log ) )
{ {
nlinfo( "%s", log.c_str() ); nlinfo( "%s", log.c_str() );
return false; return false;
} }
if( !program.attachShader( vp ) ) if( !program.attachVertexProgram( vp ) )
return false; return false;
if( !program.attachShader( pp ) ) if( !program.attachPixelProgram( pp ) )
return false; return false;
if( !program.link( log ) ) if( !program.link( log ) )
{ {
nlinfo( "%s", log.c_str() ); nlinfo( "%s", log.c_str() );
return false; return false;
} }
if( !activeGLSLProgram( &program ) ) if( !activeGLSLProgram( &program ) )
return false; return false;
glDrawArrays( GL_TRIANGLES, startIndex * 3, numTris * 3 ); glDrawArrays( GL_TRIANGLES, startIndex * 3, numTris * 3 );
activeGLSLProgram( NULL ); activeGLSLProgram( NULL );

Loading…
Cancel
Save