#include "externals.hh" #include "programs.hh" /*= T_ShaderCode =============================================================*/ T_ShaderCode::T_ShaderCode( __rd__ const int nparts ) : code( nparts , nullptr ) { } T_ShaderCode::~T_ShaderCode( ) { for ( char* str : code ) { delete[] str; } } /*----------------------------------------------------------------------------*/ void T_ShaderCode::setPart( __rd__ const int index , __rd__ char const* const string ) { assert( code[ index ] == nullptr ); const int len( strlen( string ) + 1 ); char buffer[ 32 ]; const int extraLen( index == 0 ? 0 : snprintf( buffer , sizeof( buffer ) , "\n#line 0 %d\n" , index ) ); char* const output( new char[ extraLen + len ] ); if ( index != 0 ) { memcpy( output , buffer , extraLen ); } strcpy( output + extraLen , string ); code[ index ] = output; } void T_ShaderCode::setPart( __rd__ const int index , __rd__ void const* const data , __rd__ const int size ) { assert( code[ index ] == nullptr ); char buffer[ 32 ]; const int extraLen( index == 0 ? 0 : snprintf( buffer , sizeof( buffer ) , "\n#line 0 %d\n" , index ) ); char* const output( new char[ extraLen + size + 1 ] ); if ( index != 0 ) { memcpy( output , buffer , extraLen ); } memcpy( output + extraLen , data , size ); output[ extraLen + size ] = 0; code[ index ] = output; } bool T_ShaderCode::loadPart( __rd__ const int index , __rd__ std::string const& source , __rw__ std::vector< std::string >& errors ) { assert( code[ index ] == nullptr ); const std::string sPath( "shaders/" + source ); FILE * f = fopen( sPath.c_str( ) , "r" ); if ( !f ) { errors.push_back( std::string( "File not found: " + sPath ) ); return false; } char buffer[ 32 ]; const int extraLen( index == 0 ? 0 : snprintf( buffer , sizeof( buffer ) , "\n#line 0 %d\n" , index ) ); fseek( f , 0 , SEEK_END ); const size_t size( ftell( f ) ); fseek( f , 0 , SEEK_SET ); char* const output( new char[ extraLen + size + 1 ] ); if ( index != 0 ) { memcpy( output , buffer , extraLen ); } if ( fread( output + extraLen , 1 , size , f ) != size ) { fclose( f ); delete[] output; std::string error( "Could not read file: " ); error += source; errors.push_back( error ); return false; } output[ extraLen + size ] = 0; fclose( f ); code[ index ] = output; return true; } /*----------------------------------------------------------------------------*/ GLuint T_ShaderCode::createProgram( __rd__ GLenum type , __rw__ std::vector< std::string >& errors ) const { GLenum sid = glCreateShaderProgramv( type , code.size( ) , &code[ 0 ] ); if ( sid == 0 ) { errors.push_back( "Failed to create GL program" ); return sid; } int infoLogLength; glGetProgramiv( sid , GL_INFO_LOG_LENGTH , &infoLogLength ); if ( infoLogLength ) { char buffer[ infoLogLength + 1 ]; glGetProgramInfoLog( sid , infoLogLength , nullptr , buffer ); char* start( buffer ); char* found( strchr( buffer , '\n' ) ); while ( found ) { *found = 0; errors.push_back( start ); start = found + 1; found = strchr( start , '\n' ); } if ( start < &buffer[ infoLogLength - 1 ] ) { errors.push_back( start ); } } int lnk; glGetProgramiv( sid , GL_LINK_STATUS , &lnk ); if ( !lnk ) { glDeleteProgram( sid ); return 0; } return sid; } /*= T_ShaderProgram ==========================================================*/ T_ShaderProgram::T_ShaderProgram( __rd__ const GLenum programType , __rw__ T_FilesWatcher& watcher ) : files_( watcher , [this] { load( ); } ) , programType_( programType ) , program_( 0 ) { } T_ShaderProgram::~T_ShaderProgram( ) { if ( program_ != 0 ) { glDeleteProgram( program_ ); } } void T_ShaderProgram::addChunk( __rd__ std::string const& string ) { chunksOrFiles_.push_back( true ); parts_.push_back( string ); } void T_ShaderProgram::addFile( __rd__ std::string const& source ) { chunksOrFiles_.push_back( false ); parts_.push_back( source ); } void T_ShaderProgram::load( ) { const auto n( parts_.size( ) ); errors_.clear( ); files_.clear( ); if ( program_ != 0 ) { glDeleteProgram( program_ ); program_ = 0; } T_ShaderCode sc( n ); printf( "LOAD PROGRAM" ); for ( auto i = 0u ; i < n ; i ++ ) { if ( chunksOrFiles_[ i ] ) { printf( " %d:(chunk)" , i ); sc.setPart( i , parts_[ i ].c_str( ) ); } else { printf( " %d:%s" , i , parts_[ i ].c_str( ) ); if ( sc.loadPart( i , parts_[ i ].c_str( ) , errors_ ) ) { files_.watch( parts_[ i ] ); } } } printf( "\n" ); if ( errors_.size( ) == 0 ) { program_ = sc.createProgram( programType_ , errors_ ); if ( program_ != 0 ) { GLint x; glGetProgramInterfaceiv( program_ , GL_PROGRAM_OUTPUT , GL_ACTIVE_RESOURCES , &x ); printf( "-> LOADED (%d output(s)" , x ); for ( int i = 0 ; i < x ; i ++ ) { static const GLenum query[] = { GL_NAME_LENGTH , GL_LOCATION }; int output[ 2 ]; glGetProgramResourceiv( program_ , GL_PROGRAM_OUTPUT , i , 2 , query , 2 , nullptr , output ); char rName[ output[ 0 ] + 1 ]; glGetProgramResourceName( program_ , GL_PROGRAM_OUTPUT , i , sizeof( rName ) , nullptr , rName ); printf( "%s %s@%d" , i == 0 ? ":" : "" , rName , output[ 1 ] ); } printf( ")\n" ); } } if ( errors_.size( ) ) { printf( "\n--------------------------------------------------------------------------------\n\n" ); for ( auto const& str : errors_ ) { printf( "ERR: %s\n" , str.c_str( ) ); } } } bool T_ShaderProgram::activate( ) const { if ( program_ != 0 ) { glUseProgram( program_ ); } return program_ != 0; } namespace { std::unique_ptr< T_ShaderProgram > FsQuad_; } T_ShaderProgram const& T_ShaderProgram::FullscreenQuad( __rw__ T_FilesWatcher& watcher ) { if ( !FsQuad_ ) { FsQuad_ = std::make_unique< T_ShaderProgram >( GL_VERTEX_SHADER , watcher ); FsQuad_->addFile( "fsquad.glsl" ); FsQuad_->load( ); } return *FsQuad_; }