|
|
@@ -1,35 +1,35 @@
|
|
|
-#include "shader_prog.h"
|
|
|
+#include "ShaderProg.h"
|
|
|
#include "renderer.h"
|
|
|
#include "ShaderParser.h"
|
|
|
-#include "texture.h"
|
|
|
+#include "Texture.h"
|
|
|
|
|
|
|
|
|
-#define SHADER_ERROR( x ) ERROR( "Shader (" << GetName() << "): " << x )
|
|
|
-#define SHADER_WARNING( x ) WARNING( "Shader (" << GetName() << "): " << x )
|
|
|
+#define SHADER_ERROR( x ) ERROR( "Shader (" << getName() << "): " << x )
|
|
|
+#define SHADER_WARNING( x ) WARNING( "Shader (" << getName() << "): " << x )
|
|
|
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// CreateAndCompileShader =
|
|
|
+// createAndCompileShader =
|
|
|
//=====================================================================================================================================
|
|
|
-uint shader_prog_t::CreateAndCompileShader( const char* source_code, const char* preproc, int type ) const
|
|
|
+uint ShaderProg::createAndCompileShader( const char* source_code, const char* preproc, int type ) const
|
|
|
{
|
|
|
- uint gl_id = 0;
|
|
|
+ uint glId = 0;
|
|
|
const char* source_strs[2] = {NULL, NULL};
|
|
|
|
|
|
// create the shader
|
|
|
- gl_id = glCreateShader( type );
|
|
|
+ glId = glCreateShader( type );
|
|
|
|
|
|
// attach the source
|
|
|
source_strs[1] = source_code;
|
|
|
source_strs[0] = preproc;
|
|
|
|
|
|
// compile
|
|
|
- glShaderSource( gl_id, 2, source_strs, NULL );
|
|
|
- glCompileShader( gl_id );
|
|
|
+ glShaderSource( glId, 2, source_strs, NULL );
|
|
|
+ glCompileShader( glId );
|
|
|
|
|
|
int success;
|
|
|
- glGetShaderiv( gl_id, GL_COMPILE_STATUS, &success );
|
|
|
+ glGetShaderiv( glId, GL_COMPILE_STATUS, &success );
|
|
|
|
|
|
if( !success )
|
|
|
{
|
|
|
@@ -38,9 +38,9 @@ uint shader_prog_t::CreateAndCompileShader( const char* source_code, const char*
|
|
|
int chars_written = 0;
|
|
|
char* info_log = NULL;
|
|
|
|
|
|
- glGetShaderiv( gl_id, GL_INFO_LOG_LENGTH, &info_len );
|
|
|
+ glGetShaderiv( glId, GL_INFO_LOG_LENGTH, &info_len );
|
|
|
info_log = (char*)malloc( (info_len+1)*sizeof(char) );
|
|
|
- glGetShaderInfoLog( gl_id, info_len, &chars_written, info_log );
|
|
|
+ glGetShaderInfoLog( glId, info_len, &chars_written, info_log );
|
|
|
|
|
|
const char* shader_type;
|
|
|
switch( type )
|
|
|
@@ -60,21 +60,21 @@ uint shader_prog_t::CreateAndCompileShader( const char* source_code, const char*
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- return gl_id;
|
|
|
+ return glId;
|
|
|
}
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// Link =
|
|
|
+// link =
|
|
|
//=====================================================================================================================================
|
|
|
-bool shader_prog_t::Link()
|
|
|
+bool ShaderProg::link()
|
|
|
{
|
|
|
// link
|
|
|
- glLinkProgram( gl_id );
|
|
|
+ glLinkProgram( glId );
|
|
|
|
|
|
// check if linked correctly
|
|
|
int success;
|
|
|
- glGetProgramiv( gl_id, GL_LINK_STATUS, &success );
|
|
|
+ glGetProgramiv( glId, GL_LINK_STATUS, &success );
|
|
|
|
|
|
if( !success )
|
|
|
{
|
|
|
@@ -82,10 +82,10 @@ bool shader_prog_t::Link()
|
|
|
int chars_written = 0;
|
|
|
char* info_log_txt = NULL;
|
|
|
|
|
|
- glGetProgramiv( gl_id, GL_INFO_LOG_LENGTH, &info_len );
|
|
|
+ glGetProgramiv( glId, GL_INFO_LOG_LENGTH, &info_len );
|
|
|
|
|
|
info_log_txt = (char*)malloc( (info_len+1)*sizeof(char) );
|
|
|
- glGetProgramInfoLog( gl_id, info_len, &chars_written, info_log_txt );
|
|
|
+ glGetProgramInfoLog( glId, info_len, &chars_written, info_log_txt );
|
|
|
SHADER_ERROR( "Link log follows:\n" << info_log_txt );
|
|
|
free( info_log_txt );
|
|
|
return false;
|
|
|
@@ -96,9 +96,9 @@ bool shader_prog_t::Link()
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// GetUniAndAttribLocs =
|
|
|
+// getUniAndAttribLocs =
|
|
|
//=====================================================================================================================================
|
|
|
-void shader_prog_t::GetUniAndAttribLocs()
|
|
|
+void ShaderProg::getUniAndAttribLocs()
|
|
|
{
|
|
|
int num;
|
|
|
char name_[256];
|
|
|
@@ -108,101 +108,101 @@ void shader_prog_t::GetUniAndAttribLocs()
|
|
|
|
|
|
|
|
|
// attrib locations
|
|
|
- glGetProgramiv( gl_id, GL_ACTIVE_ATTRIBUTES, &num );
|
|
|
+ glGetProgramiv( glId, GL_ACTIVE_ATTRIBUTES, &num );
|
|
|
for( int i=0; i<num; i++ ) // loop all attributes
|
|
|
{
|
|
|
- glGetActiveAttrib( gl_id, i, sizeof(name_)/sizeof(char), &length, &size, &type, name_ );
|
|
|
+ glGetActiveAttrib( glId, i, sizeof(name_)/sizeof(char), &length, &size, &type, name_ );
|
|
|
name_[ length ] = '\0';
|
|
|
|
|
|
// check if its FFP location
|
|
|
- int loc = glGetAttribLocation(gl_id, name_);
|
|
|
+ int loc = glGetAttribLocation(glId, name_);
|
|
|
if( loc == -1 )
|
|
|
{
|
|
|
//SHADER_WARNING( "You are using FFP vertex attributes (\"" << name_ << "\")" );
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- attrib_name_to_loc[ name_ ] = loc;
|
|
|
+ attribNameToLoc[ name_ ] = loc;
|
|
|
}
|
|
|
|
|
|
|
|
|
// uni locations
|
|
|
- glGetProgramiv( gl_id, GL_ACTIVE_UNIFORMS, &num );
|
|
|
+ glGetProgramiv( glId, GL_ACTIVE_UNIFORMS, &num );
|
|
|
for( int i=0; i<num; i++ ) // loop all uniforms
|
|
|
{
|
|
|
- glGetActiveUniform( gl_id, i, sizeof(name_)/sizeof(char), &length, &size, &type, name_ );
|
|
|
+ glGetActiveUniform( glId, i, sizeof(name_)/sizeof(char), &length, &size, &type, name_ );
|
|
|
name_[ length ] = '\0';
|
|
|
|
|
|
// check if its FFP location
|
|
|
- int loc = glGetUniformLocation(gl_id, name_);
|
|
|
+ int loc = glGetUniformLocation(glId, name_);
|
|
|
if( loc == -1 )
|
|
|
{
|
|
|
//SHADER_WARNING( "You are using FFP uniforms (\"" << name_ << "\")" );
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- uni_name_to_loc[ name_ ] = loc;
|
|
|
+ uniNameToLoc[ name_ ] = loc;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// FillTheCustomLocationsVectors =
|
|
|
+// fillTheCustomLocationsVectors =
|
|
|
//=====================================================================================================================================
|
|
|
-bool shader_prog_t::FillTheCustomLocationsVectors( const ShaderParser& pars )
|
|
|
+bool ShaderProg::fillTheCustomLocationsVectors( const ShaderParser& pars )
|
|
|
{
|
|
|
- Bind();
|
|
|
+ bind();
|
|
|
uint max = 0;
|
|
|
|
|
|
// uniforms
|
|
|
- for( uint i=0; i<pars.uniforms.size(); ++i )
|
|
|
+ for( uint i=0; i<pars.getOutput().getUniLocs().size(); ++i )
|
|
|
{
|
|
|
- if( pars.uniforms[i].custom_loc > max )
|
|
|
- max = pars.uniforms[i].custom_loc;
|
|
|
+ if( pars.getOutput().getUniLocs()[i].customLoc > max )
|
|
|
+ max = pars.getOutput().getUniLocs()[i].customLoc;
|
|
|
}
|
|
|
- custom_uni_loc_to_real_loc.assign( max + 1, -1 );
|
|
|
+ customUniLocToRealLoc.assign( max + 1, -1 );
|
|
|
|
|
|
- for( uint i=0; i<pars.uniforms.size(); ++i )
|
|
|
+ for( uint i=0; i<pars.getOutput().getUniLocs().size(); ++i )
|
|
|
{
|
|
|
- if( custom_uni_loc_to_real_loc[ pars.uniforms[i].custom_loc ] != -1 )
|
|
|
+ if( customUniLocToRealLoc[ pars.getOutput().getUniLocs()[i].customLoc ] != -1 )
|
|
|
{
|
|
|
- SHADER_ERROR( "The uniform \"" << pars.uniforms[i].name << "\" has the same value with another one" );
|
|
|
+ SHADER_ERROR( "The uniform \"" << pars.getOutput().getUniLocs()[i].name << "\" has the same value with another one" );
|
|
|
return false;
|
|
|
}
|
|
|
- int loc = GetUniformLocation( pars.uniforms[i].name.c_str() );
|
|
|
+ int loc = getUniLoc( pars.getOutput().getUniLocs()[i].name.c_str() );
|
|
|
if( loc == -1 )
|
|
|
{
|
|
|
SHADER_WARNING( "Check the previous error" );
|
|
|
continue;
|
|
|
}
|
|
|
- custom_uni_loc_to_real_loc[pars.uniforms[i].custom_loc] = loc;
|
|
|
+ customUniLocToRealLoc[pars.getOutput().getUniLocs()[i].customLoc] = loc;
|
|
|
}
|
|
|
|
|
|
|
|
|
// attributes
|
|
|
max = 0;
|
|
|
|
|
|
- for( uint i=0; i<pars.attributes.size(); ++i )
|
|
|
+ for( uint i=0; i<pars.getOutput().getAttribLocs().size(); ++i )
|
|
|
{
|
|
|
- if( pars.attributes[i].custom_loc > max )
|
|
|
- max = pars.attributes[i].custom_loc;
|
|
|
+ if( pars.getOutput().getAttribLocs()[i].customLoc > max )
|
|
|
+ max = pars.getOutput().getAttribLocs()[i].customLoc;
|
|
|
}
|
|
|
- custom_attrib_loc_to_real_loc.assign( max + 1, -1 );
|
|
|
+ customAttribLocToRealLoc.assign( max + 1, -1 );
|
|
|
|
|
|
- for( uint i=0; i<pars.attributes.size(); ++i )
|
|
|
+ for( uint i=0; i<pars.getOutput().getAttribLocs().size(); ++i )
|
|
|
{
|
|
|
- if( custom_attrib_loc_to_real_loc[ pars.attributes[i].custom_loc ] != -1 )
|
|
|
+ if( customAttribLocToRealLoc[ pars.getOutput().getAttribLocs()[i].customLoc ] != -1 )
|
|
|
{
|
|
|
- SHADER_ERROR( "The attribute \"" << pars.attributes[i].name << "\" has the same value with another one" );
|
|
|
+ SHADER_ERROR( "The attribute \"" << pars.getOutput().getAttribLocs()[i].name << "\" has the same value with another one" );
|
|
|
return false;
|
|
|
}
|
|
|
- int loc = GetAttributeLocation( pars.attributes[i].name.c_str() );
|
|
|
+ int loc = getAttribLoc( pars.getOutput().getAttribLocs()[i].name.c_str() );
|
|
|
if( loc == -1 )
|
|
|
{
|
|
|
SHADER_ERROR( "Check the previous error" );
|
|
|
return false;
|
|
|
}
|
|
|
- custom_attrib_loc_to_real_loc[pars.attributes[i].custom_loc] = loc;
|
|
|
+ customAttribLocToRealLoc[pars.getOutput().getAttribLocs()[i].customLoc] = loc;
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
@@ -210,21 +210,21 @@ bool shader_prog_t::FillTheCustomLocationsVectors( const ShaderParser& pars )
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// Load =
|
|
|
+// load =
|
|
|
//=====================================================================================================================================
|
|
|
-bool shader_prog_t::Load( const char* filename )
|
|
|
+bool ShaderProg::load( const char* filename )
|
|
|
{
|
|
|
- if( !CustomLoad( filename, "" ) ) return false;
|
|
|
+ if( !customload( filename, "" ) ) return false;
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// CustomLoad =
|
|
|
+// customload =
|
|
|
//=====================================================================================================================================
|
|
|
-bool shader_prog_t::CustomLoad( const char* filename, const char* extra_source )
|
|
|
+bool ShaderProg::customload( const char* filename, const char* extra_source )
|
|
|
{
|
|
|
- if( GetName().length() == 0 )
|
|
|
+ if( getName().length() == 0 )
|
|
|
{
|
|
|
name = util::CutPath( filename );
|
|
|
path = util::GetPath( filename );
|
|
|
@@ -232,39 +232,39 @@ bool shader_prog_t::CustomLoad( const char* filename, const char* extra_source )
|
|
|
|
|
|
ShaderParser pars;
|
|
|
|
|
|
- if( !pars.ParseFile( filename ) ) return false;
|
|
|
+ if( !pars.parseFile( filename ) ) return false;
|
|
|
|
|
|
// create, compile, attach and link
|
|
|
string preproc_source = r::GetStdShaderPreprocDefines() + extra_source;
|
|
|
- uint vert_gl_id = CreateAndCompileShader( pars.vert_shader_source.c_str(), preproc_source.c_str(), GL_VERTEX_SHADER );
|
|
|
- if( vert_gl_id == 0 ) return false;
|
|
|
+ uint vert_glId = createAndCompileShader( pars.getOutput().getVertShaderSource().c_str(), preproc_source.c_str(), GL_VERTEX_SHADER );
|
|
|
+ if( vert_glId == 0 ) return false;
|
|
|
|
|
|
- uint frag_gl_id = CreateAndCompileShader( pars.frag_shader_source.c_str(), preproc_source.c_str(), GL_FRAGMENT_SHADER );
|
|
|
- if( frag_gl_id == 0 ) return false;
|
|
|
+ uint frag_glId = createAndCompileShader( pars.getOutput().getFragShaderSource().c_str(), preproc_source.c_str(), GL_FRAGMENT_SHADER );
|
|
|
+ if( frag_glId == 0 ) return false;
|
|
|
|
|
|
- gl_id = glCreateProgram();
|
|
|
- glAttachShader( gl_id, vert_gl_id );
|
|
|
- glAttachShader( gl_id, frag_gl_id );
|
|
|
+ glId = glCreateProgram();
|
|
|
+ glAttachShader( glId, vert_glId );
|
|
|
+ glAttachShader( glId, frag_glId );
|
|
|
|
|
|
- if( !Link() ) return false;
|
|
|
+ if( !link() ) return false;
|
|
|
|
|
|
|
|
|
// init the rest
|
|
|
- GetUniAndAttribLocs();
|
|
|
- if( !FillTheCustomLocationsVectors( pars ) ) return false;
|
|
|
+ getUniAndAttribLocs();
|
|
|
+ if( !fillTheCustomLocationsVectors( pars ) ) return false;
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// GetUniformLocation =
|
|
|
+// getUniLoc =
|
|
|
//=====================================================================================================================================
|
|
|
-int shader_prog_t::GetUniformLocation( const char* name ) const
|
|
|
+int ShaderProg::getUniLoc( const char* name ) const
|
|
|
{
|
|
|
- DEBUG_ERR( gl_id == 0 ); // not initialized
|
|
|
- ntlit_t it = uni_name_to_loc.find( name );
|
|
|
- if( it == uni_name_to_loc.end() )
|
|
|
+ DEBUG_ERR( glId == 0 ); // not initialized
|
|
|
+ NameToLocIterator it = uniNameToLoc.find( name );
|
|
|
+ if( it == uniNameToLoc.end() )
|
|
|
{
|
|
|
SHADER_ERROR( "Cannot get uniform loc \"" << name << '\"' );
|
|
|
return -1;
|
|
|
@@ -274,13 +274,13 @@ int shader_prog_t::GetUniformLocation( const char* name ) const
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// GetAttributeLocation =
|
|
|
+// getAttribLoc =
|
|
|
//=====================================================================================================================================
|
|
|
-int shader_prog_t::GetAttributeLocation( const char* name ) const
|
|
|
+int ShaderProg::getAttribLoc( const char* name ) const
|
|
|
{
|
|
|
- DEBUG_ERR( gl_id == 0 ); // not initialized
|
|
|
- ntlit_t it = attrib_name_to_loc.find( name );
|
|
|
- if( it == attrib_name_to_loc.end() )
|
|
|
+ DEBUG_ERR( glId == 0 ); // not initialized
|
|
|
+ NameToLocIterator it = attribNameToLoc.find( name );
|
|
|
+ if( it == attribNameToLoc.end() )
|
|
|
{
|
|
|
SHADER_ERROR( "Cannot get attrib loc \"" << name << '\"' );
|
|
|
return -1;
|
|
|
@@ -290,13 +290,13 @@ int shader_prog_t::GetAttributeLocation( const char* name ) const
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// GetUniformLocationSilently =
|
|
|
+// getUniLocSilently =
|
|
|
//=====================================================================================================================================
|
|
|
-int shader_prog_t::GetUniformLocationSilently( const char* name ) const
|
|
|
+int ShaderProg::getUniLocSilently( const char* name ) const
|
|
|
{
|
|
|
- DEBUG_ERR( gl_id == 0 ); // not initialized
|
|
|
- ntlit_t it = uni_name_to_loc.find( name );
|
|
|
- if( it == uni_name_to_loc.end() )
|
|
|
+ DEBUG_ERR( glId == 0 ); // not initialized
|
|
|
+ NameToLocIterator it = uniNameToLoc.find( name );
|
|
|
+ if( it == uniNameToLoc.end() )
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -305,13 +305,13 @@ int shader_prog_t::GetUniformLocationSilently( const char* name ) const
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// GetAttributeLocationSilently =
|
|
|
+// getAttribLocSilently =
|
|
|
//=====================================================================================================================================
|
|
|
-int shader_prog_t::GetAttributeLocationSilently( const char* name ) const
|
|
|
+int ShaderProg::getAttribLocSilently( const char* name ) const
|
|
|
{
|
|
|
- DEBUG_ERR( gl_id == 0 ); // not initialized
|
|
|
- ntlit_t it = attrib_name_to_loc.find( name );
|
|
|
- if( it == attrib_name_to_loc.end() )
|
|
|
+ DEBUG_ERR( glId == 0 ); // not initialized
|
|
|
+ NameToLocIterator it = attribNameToLoc.find( name );
|
|
|
+ if( it == attribNameToLoc.end() )
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -320,41 +320,41 @@ int shader_prog_t::GetAttributeLocationSilently( const char* name ) const
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// GetUniformLocation =
|
|
|
+// getUniLoc =
|
|
|
//=====================================================================================================================================
|
|
|
-int shader_prog_t::GetUniformLocation( int id ) const
|
|
|
+int ShaderProg::GetUniLoc( int id ) const
|
|
|
{
|
|
|
- DEBUG_ERR( uint(id) >= custom_uni_loc_to_real_loc.size() );
|
|
|
- DEBUG_ERR( custom_uni_loc_to_real_loc[id] == -1 );
|
|
|
- return custom_uni_loc_to_real_loc[id];
|
|
|
+ DEBUG_ERR( uint(id) >= customUniLocToRealLoc.size() );
|
|
|
+ DEBUG_ERR( customUniLocToRealLoc[id] == -1 );
|
|
|
+ return customUniLocToRealLoc[id];
|
|
|
}
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// GetAttributeLocation =
|
|
|
+// getAttribLoc =
|
|
|
//=====================================================================================================================================
|
|
|
-int shader_prog_t::GetAttributeLocation( int id ) const
|
|
|
+int ShaderProg::getAttribLoc( int id ) const
|
|
|
{
|
|
|
- DEBUG_ERR( uint(id) >= custom_attrib_loc_to_real_loc.size() );
|
|
|
- DEBUG_ERR( custom_attrib_loc_to_real_loc[id] == -1 );
|
|
|
- return custom_attrib_loc_to_real_loc[id];
|
|
|
+ DEBUG_ERR( uint(id) >= customAttribLocToRealLoc.size() );
|
|
|
+ DEBUG_ERR( customAttribLocToRealLoc[id] == -1 );
|
|
|
+ return customAttribLocToRealLoc[id];
|
|
|
}
|
|
|
|
|
|
|
|
|
//=====================================================================================================================================
|
|
|
-// LocTexUnit =
|
|
|
+// locTexUnit =
|
|
|
//=====================================================================================================================================
|
|
|
-void shader_prog_t::LocTexUnit( int loc, const texture_t& tex, uint tex_unit ) const
|
|
|
+void ShaderProg::locTexUnit( int loc, const Texture& tex, uint tex_unit ) const
|
|
|
{
|
|
|
DEBUG_ERR( loc == -1 );
|
|
|
- DEBUG_ERR( GetCurrentProgram() != gl_id );
|
|
|
- tex.Bind( tex_unit );
|
|
|
+ DEBUG_ERR( getCurrentProgram() != glId );
|
|
|
+ tex.bind( tex_unit );
|
|
|
glUniform1i( loc, tex_unit );
|
|
|
}
|
|
|
|
|
|
-void shader_prog_t::LocTexUnit( const char* loc, const texture_t& tex, uint tex_unit ) const
|
|
|
+void ShaderProg::locTexUnit( const char* loc, const Texture& tex, uint tex_unit ) const
|
|
|
{
|
|
|
- DEBUG_ERR( GetCurrentProgram() != gl_id );
|
|
|
- tex.Bind( tex_unit );
|
|
|
- glUniform1i( GetUniformLocation(loc), tex_unit );
|
|
|
+ DEBUG_ERR( getCurrentProgram() != glId );
|
|
|
+ tex.bind( tex_unit );
|
|
|
+ glUniform1i( getUniLoc(loc), tex_unit );
|
|
|
}
|