2017-10-03 13:53:50 +02:00
|
|
|
#include "externals.hh"
|
|
|
|
#include "shaders.hh"
|
2017-10-04 11:20:27 +02:00
|
|
|
#include "globals.hh"
|
2017-10-03 13:53:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-10-03 16:10:48 +02:00
|
|
|
const std::regex PreprocDirective_( "^\\s*//!\\s*([a-z]+(\\s+([^\\s]+))*)\\s*$" );
|
2017-10-08 10:13:11 +02:00
|
|
|
const std::regex UniformName_( "^[A-Za-z][A-Za-z0-9_]*$" );
|
2017-10-04 17:29:48 +02:00
|
|
|
const std::regex GLSLErrorNv_( "^[0-9]*\\(([0-9]+).*$" );
|
2017-10-04 16:29:39 +02:00
|
|
|
|
2017-10-03 16:10:48 +02:00
|
|
|
const std::map< std::string , E_ShaderInput > InputTypes_( ([] {
|
|
|
|
std::map< std::string , E_ShaderInput > t;
|
|
|
|
t.emplace( "chunk" , E_ShaderInput::CHUNK );
|
|
|
|
t.emplace( "library" , E_ShaderInput::LIBRARY );
|
|
|
|
t.emplace( "lib" , E_ShaderInput::LIBRARY );
|
|
|
|
t.emplace( "vertex" , E_ShaderInput::VERTEX );
|
|
|
|
t.emplace( "fragment" , E_ShaderInput::FRAGMENT );
|
2017-10-04 10:06:45 +02:00
|
|
|
t.emplace( "compute" , E_ShaderInput::COMPUTE );
|
2017-10-04 11:20:27 +02:00
|
|
|
t.emplace( "geo" , E_ShaderInput::GEOMETRY );
|
|
|
|
t.emplace( "geometry" , E_ShaderInput::GEOMETRY );
|
2017-10-03 16:10:48 +02:00
|
|
|
return t;
|
|
|
|
})());
|
|
|
|
|
2017-10-08 10:13:11 +02:00
|
|
|
const std::unordered_map< std::string , E_UniformType > UniformTypes_( ([] {
|
|
|
|
std::unordered_map< std::string , E_UniformType > t;
|
|
|
|
t.emplace( "float" , E_UniformType::F1 );
|
|
|
|
t.emplace( "vec2" , E_UniformType::F2 );
|
|
|
|
t.emplace( "vec3" , E_UniformType::F3 );
|
|
|
|
t.emplace( "vec4" , E_UniformType::F4 );
|
|
|
|
t.emplace( "int" , E_UniformType::I1 );
|
|
|
|
t.emplace( "ivec2" , E_UniformType::I2 );
|
|
|
|
t.emplace( "ivec3" , E_UniformType::I3 );
|
|
|
|
t.emplace( "ivec4" , E_UniformType::I4 );
|
|
|
|
t.emplace( "sampler2D" , E_UniformType::SAMPLER2D );
|
|
|
|
return t;
|
|
|
|
})());
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
const GLenum ProgramTypes_[] = {
|
|
|
|
GL_VERTEX_SHADER ,
|
|
|
|
GL_FRAGMENT_SHADER ,
|
|
|
|
GL_GEOMETRY_SHADER ,
|
|
|
|
GL_COMPUTE_SHADER
|
|
|
|
};
|
|
|
|
|
|
|
|
const GLbitfield PipelineStages_[] = {
|
|
|
|
GL_VERTEX_SHADER_BIT ,
|
|
|
|
GL_FRAGMENT_SHADER_BIT ,
|
|
|
|
GL_GEOMETRY_SHADER_BIT ,
|
|
|
|
GL_COMPUTE_SHADER_BIT
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert( sizeof( PipelineStages_ ) / sizeof( GLbitfield ) == size_t( E_ShaderType::__COUNT__ ) ,
|
|
|
|
"missing pipeline stage constants" );
|
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
|
2017-10-03 16:10:48 +02:00
|
|
|
/*============================================================================*/
|
|
|
|
|
|
|
|
// Input reader state and functions, used when loading a source file
|
|
|
|
struct T_InputReader_
|
|
|
|
{
|
|
|
|
using T_Tokens_ = std::vector< std::string >;
|
|
|
|
|
|
|
|
FILE* const file;
|
|
|
|
T_ShaderInput& input;
|
|
|
|
uint32_t line{ 0 };
|
|
|
|
|
|
|
|
char* buffer{ nullptr };
|
|
|
|
ssize_t readCount;
|
|
|
|
|
|
|
|
std::string accumulator{ };
|
|
|
|
uint32_t accumLines{ 0 };
|
|
|
|
|
2017-11-04 12:00:58 +01:00
|
|
|
T_InputReader_( FILE* const file ,
|
|
|
|
T_ShaderInput& input )
|
2017-10-03 16:10:48 +02:00
|
|
|
: file( file ) , input( input )
|
|
|
|
{ }
|
|
|
|
~T_InputReader_( );
|
|
|
|
|
|
|
|
void read( );
|
2017-10-08 10:13:11 +02:00
|
|
|
|
|
|
|
void handleDirective(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Tokens_ const& tokens );
|
2017-10-08 10:13:11 +02:00
|
|
|
void parseInputDirective(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Tokens_ const& tokens );
|
2017-10-08 10:13:11 +02:00
|
|
|
|
|
|
|
void error(
|
2017-11-04 12:00:58 +01:00
|
|
|
std::string const& err );
|
2017-10-03 16:10:48 +02:00
|
|
|
void nl( );
|
|
|
|
void addAccumulated( );
|
|
|
|
};
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
T_InputReader_::~T_InputReader_( )
|
|
|
|
{
|
|
|
|
if ( buffer ) {
|
|
|
|
free( buffer );
|
|
|
|
}
|
|
|
|
fclose( file );
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_InputReader_::read( )
|
|
|
|
{
|
|
|
|
size_t bsz( 0 );
|
|
|
|
while ( ( readCount = getline( &buffer , &bsz , file ) ) != -1 ) {
|
|
|
|
line ++;
|
|
|
|
std::cmatch match;
|
|
|
|
if ( std::regex_match( buffer , match , PreprocDirective_ ) ) {
|
|
|
|
const T_Tokens_ tokens( ([](std::string const& a) {
|
|
|
|
using stri = std::istream_iterator< std::string >;
|
|
|
|
std::istringstream iss( a );
|
|
|
|
return T_Tokens_{ stri( iss ) , stri( ) };
|
|
|
|
})( match[ 1 ].str( ) ) );
|
|
|
|
assert( tokens.size( ) >= 1 );
|
|
|
|
handleDirective( tokens );
|
|
|
|
} else {
|
|
|
|
accumulator += buffer;
|
|
|
|
accumLines ++;
|
2017-10-03 13:53:50 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-03 16:10:48 +02:00
|
|
|
addAccumulated( );
|
|
|
|
}
|
|
|
|
|
2017-10-08 10:13:11 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-10-03 16:09:12 +02:00
|
|
|
|
2017-10-03 16:10:48 +02:00
|
|
|
void T_InputReader_::handleDirective(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Tokens_ const& tokens )
|
2017-10-03 16:10:48 +02:00
|
|
|
{
|
|
|
|
auto const& directive( tokens[ 0 ] );
|
2017-10-03 13:53:50 +02:00
|
|
|
|
2017-10-03 16:10:48 +02:00
|
|
|
if ( directive == "include" ) {
|
|
|
|
if ( tokens.size( ) != 2 ) {
|
2017-10-03 16:09:12 +02:00
|
|
|
nl( );
|
2017-10-03 16:10:48 +02:00
|
|
|
error( "invalid arguments" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
addAccumulated( );
|
|
|
|
auto& ck( input.chunks );
|
|
|
|
ck.emplace_back( E_ShaderInputChunk::INCLUDE , tokens[ 1 ] , 1 );
|
|
|
|
|
|
|
|
} else if ( directive == "type" ) {
|
|
|
|
nl( );
|
|
|
|
if ( tokens.size( ) != 2 ) {
|
|
|
|
error( "invalid arguments" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto pos( InputTypes_.find( tokens[ 1 ] ) );
|
|
|
|
if ( pos == InputTypes_.end( ) ) {
|
|
|
|
error( "unknown type" );
|
2017-10-03 13:53:50 +02:00
|
|
|
} else {
|
2017-10-03 16:10:48 +02:00
|
|
|
input.type = pos->second;
|
2017-10-03 13:53:50 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 10:13:11 +02:00
|
|
|
} else if ( directive == "input" ) {
|
|
|
|
nl( );
|
|
|
|
parseInputDirective( tokens );
|
|
|
|
|
|
|
|
} else if ( directive == "uniforms" ) {
|
|
|
|
nl( );
|
|
|
|
for ( auto const& c : input.chunks ) {
|
|
|
|
if ( c.type == E_ShaderInputChunk::UNIFORMS ) {
|
|
|
|
error( "duplicate uniform generation" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
input.chunks.emplace_back( E_ShaderInputChunk::UNIFORMS , "" , 1 );
|
|
|
|
|
2017-10-03 16:10:48 +02:00
|
|
|
} else {
|
|
|
|
nl( );
|
|
|
|
error( "unknown directive" );
|
2017-10-03 13:53:50 +02:00
|
|
|
}
|
2017-10-03 16:10:48 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 10:13:11 +02:00
|
|
|
void T_InputReader_::parseInputDirective(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Tokens_ const& tokens )
|
2017-10-08 10:13:11 +02:00
|
|
|
{
|
|
|
|
if ( tokens.size( ) != 4 ) {
|
|
|
|
error( "invalid arguments" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local/global
|
|
|
|
const bool global{ tokens[ 1 ] == "global" };
|
|
|
|
if ( !global && tokens[ 1 ] != "local" ) {
|
|
|
|
error( "second argument should be 'local' or 'global'" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name
|
|
|
|
std::string const& name{ tokens[ 2 ] };
|
|
|
|
if ( input.uniforms.find( name ) != input.uniforms.end( ) ) {
|
|
|
|
error( "duplicate uniform" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( !std::regex_match( name , UniformName_ ) ) {
|
|
|
|
error( "invalid uniform name" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type
|
|
|
|
auto tPos( UniformTypes_.find( tokens[ 3 ] ) );
|
|
|
|
if ( tPos == UniformTypes_.end( ) ) {
|
|
|
|
error( "unsupported uniform type" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
input.uniforms.emplace( tokens[ 2 ] , T_ShaderUniform{
|
|
|
|
tokens[ 2 ] , global , tPos->second } );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2017-10-03 16:10:48 +02:00
|
|
|
void T_InputReader_::error(
|
2017-11-04 12:00:58 +01:00
|
|
|
std::string const& err )
|
2017-10-03 16:10:48 +02:00
|
|
|
{
|
|
|
|
input.errors.push_back( T_ShaderInputError{
|
|
|
|
line , err } );
|
|
|
|
}
|
2017-10-03 13:53:50 +02:00
|
|
|
|
2017-10-08 10:13:11 +02:00
|
|
|
void T_InputReader_::nl( )
|
|
|
|
{
|
|
|
|
accumLines ++;
|
|
|
|
accumulator += '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_InputReader_::addAccumulated( )
|
|
|
|
{
|
|
|
|
if ( accumLines ) {
|
|
|
|
auto& ck( input.chunks );
|
|
|
|
ck.emplace_back( E_ShaderInputChunk::CODE ,
|
|
|
|
std::move( accumulator ) , accumLines );
|
|
|
|
accumulator = {};
|
|
|
|
accumLines = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 13:53:50 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2017-10-03 16:09:12 +02:00
|
|
|
/*= T_ShaderInput ============================================================*/
|
|
|
|
|
2017-10-03 13:53:50 +02:00
|
|
|
bool T_ShaderInput::load(
|
2017-11-04 12:00:58 +01:00
|
|
|
std::string const& path )
|
2017-10-03 13:53:50 +02:00
|
|
|
{
|
|
|
|
type = E_ShaderInput::CHUNK;
|
|
|
|
chunks.clear( );
|
|
|
|
errors.clear( );
|
|
|
|
|
|
|
|
FILE* const file{ fopen( path.c_str( ) , "r" ) };
|
|
|
|
if ( !file ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
T_InputReader_ reader( file , *this );
|
|
|
|
reader.read( );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
/*= T_ShaderPipeline =========================================================*/
|
|
|
|
|
|
|
|
T_ShaderPipeline::T_ShaderPipeline( )
|
|
|
|
: T_ShaderPipeline( -1 )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
T_ShaderPipeline::T_ShaderPipeline(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderPipeline const& other )
|
2017-10-04 16:29:39 +02:00
|
|
|
: T_ShaderPipeline( other.smIndex_ )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
T_ShaderPipeline::T_ShaderPipeline(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderPipeline&& other ) noexcept
|
2017-10-04 16:29:39 +02:00
|
|
|
: T_ShaderPipeline( )
|
|
|
|
{
|
|
|
|
std::swap( other.smIndex_ , smIndex_ );
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderPipeline::T_ShaderPipeline(
|
2017-11-04 12:00:58 +01:00
|
|
|
const int32_t index )
|
2017-10-04 16:29:39 +02:00
|
|
|
: smIndex_( index )
|
|
|
|
{
|
|
|
|
if ( smIndex_ >= 0 ) {
|
|
|
|
Globals::Shaders( ).pipelines_[ smIndex_ ].references ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderPipeline::~T_ShaderPipeline( )
|
|
|
|
{
|
|
|
|
if ( smIndex_ >= 0 ) {
|
|
|
|
Globals::Shaders( ).dereferencePipeline( smIndex_ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderPipeline& T_ShaderPipeline::operator=(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderPipeline const& other )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
if ( this != &other ) {
|
|
|
|
if ( smIndex_ >= 0 ) {
|
|
|
|
Globals::Shaders( ).dereferencePipeline( smIndex_ );
|
|
|
|
}
|
|
|
|
smIndex_ = other.smIndex_;
|
|
|
|
if ( smIndex_ >= 0 ) {
|
|
|
|
Globals::Shaders( ).pipelines_[ smIndex_ ].references ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderPipeline& T_ShaderPipeline::operator=(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderPipeline&& other ) noexcept
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
if ( this != &other ) {
|
|
|
|
if ( smIndex_ >= 0 ) {
|
|
|
|
Globals::Shaders( ).dereferencePipeline( smIndex_ );
|
|
|
|
}
|
|
|
|
smIndex_ = other.smIndex_;
|
|
|
|
other.smIndex_ = -1;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool T_ShaderPipeline::valid( ) const noexcept
|
|
|
|
{
|
|
|
|
return smIndex_ >= 0
|
|
|
|
&& Globals::Shaders( ).pipelines_[ smIndex_ ].id != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderPipeline::enable( ) const
|
|
|
|
{
|
|
|
|
if ( valid( ) ) {
|
|
|
|
glBindProgramPipeline( Globals::Shaders( ).pipelines_[ smIndex_ ].id );
|
|
|
|
}
|
2017-10-15 09:34:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GLuint T_ShaderPipeline::id( ) const
|
|
|
|
{
|
|
|
|
if ( smIndex_ >= 0 ) {
|
|
|
|
return Globals::Shaders( ).pipelines_[ smIndex_ ].id;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 17:29:48 +02:00
|
|
|
GLuint T_ShaderPipeline::program(
|
2017-11-04 12:00:58 +01:00
|
|
|
const E_ShaderType program ) const
|
2017-10-04 17:29:48 +02:00
|
|
|
{
|
|
|
|
if ( !valid( ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const& sm( Globals::Shaders( ) );
|
|
|
|
auto const& pl( sm.pipelines_[ smIndex_ ] );
|
|
|
|
for ( auto const& pn : pl.programs ) {
|
|
|
|
auto pos( sm.programIndex_.find( pn ) );
|
|
|
|
if ( pos == sm.programIndex_.end( ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto& p( sm.programs_[ pos->second ] );
|
|
|
|
if ( p.code.type == program ) {
|
|
|
|
return p.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
/*= T_CodeBuilder_ ===========================================================*/
|
2017-10-03 16:09:12 +02:00
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
namespace {
|
2017-10-03 16:09:12 +02:00
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
using F_GetInput_ = std::function< T_ShaderInput const*( std::string const& ) >;
|
|
|
|
using T_Code_ = T_ShaderManager::T_ShaderCode;
|
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
// Code builder, state and functions
|
|
|
|
struct T_CodeBuilder_
|
|
|
|
{
|
2017-10-03 16:09:12 +02:00
|
|
|
struct T_StackEntry_ {
|
|
|
|
std::string name;
|
|
|
|
T_ShaderInput const* input;
|
|
|
|
uint32_t pos;
|
|
|
|
};
|
2017-10-03 16:41:00 +02:00
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
F_GetInput_ loader;
|
2017-10-03 16:41:00 +02:00
|
|
|
const std::string name;
|
2017-10-04 16:29:39 +02:00
|
|
|
T_Code_& code;
|
2017-10-03 16:41:00 +02:00
|
|
|
|
|
|
|
T_ShaderInput const* main;
|
2017-10-03 16:09:12 +02:00
|
|
|
std::map< std::string , uint32_t > pos;
|
|
|
|
std::vector< T_StackEntry_ > stack;
|
|
|
|
std::set< std::string > libraries;
|
2017-10-03 16:41:00 +02:00
|
|
|
T_ShaderInput const* current;
|
2017-10-03 16:09:12 +02:00
|
|
|
uint32_t cpos{ 0 };
|
2017-10-03 16:41:00 +02:00
|
|
|
std::string cname;
|
|
|
|
|
2017-11-04 12:00:58 +01:00
|
|
|
T_CodeBuilder_( F_GetInput_ loader ,
|
|
|
|
std::string const& name ,
|
|
|
|
T_Code_& code )
|
2017-10-03 16:41:00 +02:00
|
|
|
: loader( loader ) , name( name ) , code( code ) ,
|
2017-10-04 16:29:39 +02:00
|
|
|
main( loader( name ) )
|
2017-10-03 16:41:00 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
bool buildCode( );
|
2017-11-04 12:00:58 +01:00
|
|
|
void appendChunk( T_ShaderInputChunk const& chunk );
|
|
|
|
void include( std::string const& name ,
|
|
|
|
const uint32_t lines );
|
2017-10-03 16:41:00 +02:00
|
|
|
void next( );
|
2017-10-08 09:45:47 +02:00
|
|
|
|
|
|
|
void addInputLoaderErrors(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderInput const* input ,
|
|
|
|
std::string const& name );
|
2017-10-03 16:41:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
bool T_CodeBuilder_::buildCode( )
|
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
code = T_Code_{ };
|
2017-10-03 16:41:00 +02:00
|
|
|
code.files.emplace( name , main != nullptr );
|
|
|
|
if ( !main ) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-04 17:29:48 +02:00
|
|
|
if ( main->type == E_ShaderInput::CHUNK
|
|
|
|
|| main->type == E_ShaderInput::LIBRARY ) {
|
|
|
|
code.errors.push_back( T_ShaderError{
|
|
|
|
name , 0 , "invalid type" } );
|
|
|
|
} else {
|
|
|
|
code.type = E_ShaderType( int( main->type ) - 2 );
|
|
|
|
}
|
2017-10-08 09:45:47 +02:00
|
|
|
addInputLoaderErrors( main , name );
|
2017-10-03 16:41:00 +02:00
|
|
|
cname = name;
|
|
|
|
current = main;
|
2017-10-03 16:09:12 +02:00
|
|
|
|
|
|
|
while ( cpos < current->chunks.size( ) ) {
|
|
|
|
auto& chunk( current->chunks[ cpos ] );
|
|
|
|
if ( chunk.type == E_ShaderInputChunk::CODE ) {
|
2017-10-03 16:41:00 +02:00
|
|
|
appendChunk( chunk );
|
2017-10-03 16:09:12 +02:00
|
|
|
} else {
|
2017-10-03 16:41:00 +02:00
|
|
|
include( chunk.text , chunk.lines );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
next( );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
void T_CodeBuilder_::appendChunk(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderInputChunk const& chunk )
|
2017-10-03 16:41:00 +02:00
|
|
|
{
|
|
|
|
code.sources.push_back( cname );
|
|
|
|
code.counts.push_back( chunk.lines );
|
|
|
|
code.starts.push_back( pos[ cname ] );
|
|
|
|
code.code += chunk.text;
|
|
|
|
pos[ cname ] += chunk.lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_CodeBuilder_::include(
|
2017-11-04 12:00:58 +01:00
|
|
|
std::string const& nname ,
|
|
|
|
const uint32_t lines )
|
2017-10-03 16:41:00 +02:00
|
|
|
{
|
|
|
|
const auto prevPos( pos[ cname ] );
|
|
|
|
pos[ cname ] += lines;
|
|
|
|
|
|
|
|
// Avoid recursion
|
|
|
|
if ( cname == nname || 0 != count_if( stack.begin( ) , stack.end( ) ,
|
|
|
|
[nname] ( T_StackEntry_ const& e ) {
|
|
|
|
return nname == e.name;
|
|
|
|
} ) ) {
|
|
|
|
code.errors.push_back( T_ShaderError{ cname , prevPos ,
|
|
|
|
"recursive inclusion of '" + nname + "'" } );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid including libraries more than once
|
|
|
|
if ( libraries.find( nname ) != libraries.end( ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
T_ShaderInput const* const isi( loader( nname ) );
|
2017-10-03 16:41:00 +02:00
|
|
|
code.files.emplace( nname , isi != nullptr );
|
|
|
|
|
|
|
|
// Check for problems
|
|
|
|
if ( !isi ) {
|
|
|
|
// Not found
|
|
|
|
code.errors.push_back( T_ShaderError{ cname , prevPos ,
|
|
|
|
"file not found" } );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( isi->type != E_ShaderInput::CHUNK && isi->type != E_ShaderInput::LIBRARY ) {
|
|
|
|
// Trying to load a top-level shader
|
|
|
|
code.errors.push_back( T_ShaderError{ cname , prevPos ,
|
|
|
|
"trying to include a top-level file" } );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add input loader errors
|
|
|
|
if ( libraries.find( nname ) == libraries.end( ) ) {
|
2017-10-08 09:45:47 +02:00
|
|
|
addInputLoaderErrors( isi , nname );
|
2017-10-03 16:41:00 +02:00
|
|
|
}
|
|
|
|
libraries.insert( nname );
|
|
|
|
|
|
|
|
// Enter the new file
|
|
|
|
stack.push_back( T_StackEntry_{ cname , current , cpos } );
|
|
|
|
cname = nname;
|
|
|
|
current = isi;
|
|
|
|
cpos = UINT32_MAX;
|
2017-10-04 17:29:48 +02:00
|
|
|
pos[ cname ] = 0;
|
2017-10-03 16:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_CodeBuilder_::next( )
|
|
|
|
{
|
|
|
|
cpos ++;
|
|
|
|
while ( cpos == current->chunks.size( ) && !stack.empty( ) ) {
|
|
|
|
T_StackEntry_ const& se( stack[ stack.size( ) - 1 ] );
|
|
|
|
pos.erase( cname );
|
|
|
|
cpos = se.pos + 1;
|
|
|
|
current = se.input;
|
|
|
|
cname = se.name;
|
|
|
|
stack.pop_back( );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 09:45:47 +02:00
|
|
|
void T_CodeBuilder_::addInputLoaderErrors(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderInput const* input ,
|
|
|
|
std::string const& name )
|
2017-10-08 09:45:47 +02:00
|
|
|
{
|
|
|
|
for ( auto const& errs : input->errors ) {
|
|
|
|
code.errors.push_back( T_ShaderError{
|
|
|
|
name , errs.line , errs.error } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/*============================================================================*/
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
T_ShaderPipeline T_ShaderManager::pipeline(
|
2017-11-04 12:00:58 +01:00
|
|
|
std::initializer_list< std::string > shaders )
|
2017-10-04 11:20:27 +02:00
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
if ( shaders.size( ) < 1 || shaders.size( ) > 5 ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string id( ([&shaders] () {
|
|
|
|
std::ostringstream oss;
|
|
|
|
std::copy( shaders.begin( ) , shaders.end( ) ,
|
|
|
|
std::ostream_iterator< std::string >( oss , "|" ) );
|
|
|
|
return oss.str( );
|
|
|
|
} )() );
|
|
|
|
auto pos( pipelineIndex_.find( id ) );
|
|
|
|
if ( pos != pipelineIndex_.end( ) ) {
|
|
|
|
return T_ShaderPipeline{ int32_t( pos->second ) };
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto index( newPipelineRecord( ) );
|
|
|
|
auto& p( pipelines_[ index ] );
|
|
|
|
pipelineIndex_.emplace( id , index );
|
|
|
|
p.idString = id;
|
|
|
|
p.id = 0;
|
|
|
|
p.references = 0;
|
|
|
|
p.programs = shaders;
|
|
|
|
for ( auto const& pName : shaders ) {
|
|
|
|
loadProgram( index , pName );
|
|
|
|
}
|
|
|
|
initPipeline( p );
|
|
|
|
|
|
|
|
return T_ShaderPipeline{ int32_t( index ) };
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 18:42:51 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
void T_ShaderManager::update( )
|
2017-10-03 16:41:00 +02:00
|
|
|
{
|
2017-10-04 17:29:48 +02:00
|
|
|
inputs_.clear( );
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
// Check for missing files
|
|
|
|
for ( auto it = missing_.begin( ) ; it != missing_.end( ) ; ++ it ) {
|
|
|
|
const bool exists( ([] ( std::string const& name ) -> bool {
|
|
|
|
struct stat buffer;
|
|
|
|
return ( stat( name.c_str( ) , &buffer ) == 0 );
|
|
|
|
})( "shaders/" + (*it).first ) );
|
|
|
|
if ( !exists ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
updates_.insert( (*it).second.begin( ) , (*it).second.end( ) );
|
|
|
|
missing_.erase( (*it).first );
|
|
|
|
}
|
2017-10-04 11:20:27 +02:00
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
// Reset programs that need to be updated
|
|
|
|
std::set< uint32_t > pipelines;
|
|
|
|
for ( auto const& name : updates_ ) {
|
|
|
|
auto p( programIndex_.find( name ) );
|
|
|
|
if ( p == programIndex_.end( ) ) {
|
|
|
|
updates_.erase( name );
|
|
|
|
} else {
|
|
|
|
auto& pr( programs_[ p->second ] );
|
|
|
|
pipelines.insert( pr.references.begin( ) , pr.references.end( ) );
|
|
|
|
resetProgram( pr );
|
|
|
|
}
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
2017-10-04 17:29:48 +02:00
|
|
|
if ( updates_.empty( ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-04 11:20:27 +02:00
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
// Reset pipelines affected by the programs above
|
|
|
|
for ( auto plid : pipelines ) {
|
|
|
|
auto& pipeline( pipelines_[ plid ] );
|
|
|
|
if ( pipeline.id != 0 ) {
|
|
|
|
glDeleteProgramPipelines( 1 , &pipeline.id );
|
|
|
|
pipeline.id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to load all updated programs
|
|
|
|
for ( auto const& name : updates_ ) {
|
|
|
|
auto& pr( programs_[ programIndex_[ name ] ] );
|
|
|
|
initProgram( pr );
|
2017-10-04 17:29:48 +02:00
|
|
|
for ( auto const& e : pr.code.errors ) {
|
|
|
|
printf( "%s:%d: %s\n" , e.source.c_str( ) , e.line ,
|
|
|
|
e.error.c_str( ) );
|
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to initialise all affected pipelines
|
|
|
|
for ( auto plid : pipelines ) {
|
|
|
|
initPipeline( pipelines_[ plid ] );
|
|
|
|
}
|
2017-10-04 17:29:48 +02:00
|
|
|
|
|
|
|
updates_.clear( );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t T_ShaderManager::newPipelineRecord( )
|
|
|
|
{
|
|
|
|
uint32_t i = 0;
|
|
|
|
while ( i < pipelines_.size( ) ) {
|
|
|
|
if ( pipelines_[ i ].references == 0 ) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
pipelines_.emplace_back( );
|
|
|
|
return i;
|
2017-10-03 16:41:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
uint32_t T_ShaderManager::newProgramRecord( )
|
|
|
|
{
|
|
|
|
uint32_t i = 0;
|
|
|
|
while ( i < programs_.size( ) ) {
|
|
|
|
if ( programs_[ i ].references.empty( ) ) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
programs_.emplace_back( );
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void T_ShaderManager::loadProgram(
|
2017-11-04 12:00:58 +01:00
|
|
|
const uint32_t pipeline ,
|
|
|
|
std::string const& name )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
if ( useExistingProgram( pipeline , name ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t index( newProgramRecord( ) );
|
|
|
|
auto& program( programs_[ index ] );
|
|
|
|
programIndex_.emplace( name , index );
|
|
|
|
program.name = name;
|
|
|
|
program.references.insert( pipeline );
|
|
|
|
program.id = 0;
|
|
|
|
initProgram( program );
|
2017-10-04 17:29:48 +02:00
|
|
|
for ( auto const& e : program.code.errors ) {
|
|
|
|
printf( "%s:%d: %s\n" , e.source.c_str( ) , e.line , e.error.c_str( ) );
|
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool T_ShaderManager::useExistingProgram(
|
2017-11-04 12:00:58 +01:00
|
|
|
const uint32_t pipeline ,
|
|
|
|
std::string const& name )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
auto pos( programIndex_.find( name ) );
|
|
|
|
if ( pos == programIndex_.end( ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto& refs( programs_[ pos->second ].references );
|
|
|
|
assert( refs.find( pipeline ) == refs.end( ) );
|
|
|
|
refs.insert( pipeline );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
T_ShaderInput const* T_ShaderManager::getInput(
|
2017-11-04 12:00:58 +01:00
|
|
|
std::string const& name )
|
2017-10-03 16:09:12 +02:00
|
|
|
{
|
2017-10-04 11:20:27 +02:00
|
|
|
auto pos( inputs_.find( name ) );
|
|
|
|
if ( pos != inputs_.end( ) ) {
|
2017-10-03 16:09:12 +02:00
|
|
|
return pos->second.get( );
|
|
|
|
}
|
|
|
|
T_ShaderInput ni;
|
|
|
|
if ( !ni.load( "shaders/" + name ) ) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-10-04 11:20:27 +02:00
|
|
|
inputs_.emplace( name , std::make_unique< T_ShaderInput >( std::move( ni ) ) );
|
|
|
|
return inputs_.find( name )->second.get( );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
void T_ShaderManager::dereferencePipeline(
|
2017-11-04 12:00:58 +01:00
|
|
|
const uint32_t index )
|
2017-10-03 16:09:12 +02:00
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
auto& pipeline( pipelines_[ index ] );
|
|
|
|
assert( pipeline.references > 0 );
|
|
|
|
pipeline.references --;
|
|
|
|
if ( pipeline.references > 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipelineIndex_.erase( pipeline.idString );
|
|
|
|
if ( pipeline.id != 0 ) {
|
|
|
|
glDeleteProgramPipelines( 1 , &pipeline.id );
|
|
|
|
pipeline.id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( auto const& pName : pipeline.programs ) {
|
|
|
|
auto pos( programIndex_.find( pName ) );
|
|
|
|
assert( pos != programIndex_.end( ) );
|
|
|
|
dereferenceProgram( pos->second , index );
|
|
|
|
}
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
void T_ShaderManager::dereferenceProgram(
|
2017-11-04 12:00:58 +01:00
|
|
|
const uint32_t index ,
|
|
|
|
const uint32_t pipeline )
|
2017-10-04 11:20:27 +02:00
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
auto& program( programs_[ index ] );
|
|
|
|
auto& refs( program.references );
|
|
|
|
assert( refs.find( pipeline ) != refs.end( ) );
|
|
|
|
refs.erase( pipeline );
|
|
|
|
if ( refs.size( ) != 0 ) {
|
|
|
|
return;
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
resetProgram( program );
|
|
|
|
programIndex_.erase( program.name );
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
void T_ShaderManager::initPipeline(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Pipeline_& pipeline ) const
|
2017-10-04 11:20:27 +02:00
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
assert( pipeline.id == 0 );
|
|
|
|
|
2017-10-04 17:29:48 +02:00
|
|
|
printf( "init pipeline %s\n" , pipeline.idString.c_str( ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
constexpr auto nst{ size_t( E_ShaderType::__COUNT__ ) };
|
|
|
|
int32_t programs[ nst ];
|
|
|
|
for ( auto i = 0u ; i < nst ; i ++ ) {
|
|
|
|
programs[ i ] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( auto const& pName : pipeline.programs ) {
|
|
|
|
const uint32_t pid( programIndex_.find( pName )->second );
|
|
|
|
auto const& program( programs_[ pid ] );
|
|
|
|
if ( programs[ int( program.code.type ) ] != -1
|
|
|
|
|| program.id == 0 ) {
|
2017-10-04 17:29:48 +02:00
|
|
|
printf( "... failed\n" );
|
2017-10-04 16:29:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
programs[ int( program.code.type ) ] = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint id( 0 );
|
2017-10-06 20:38:08 +02:00
|
|
|
GL_CHECK( {} );
|
2017-10-04 16:29:39 +02:00
|
|
|
glGenProgramPipelines( 1 , &id );
|
|
|
|
GL_CHECK( return );
|
|
|
|
for ( auto i = 0u ; i < nst ; i ++ ) {
|
|
|
|
const auto pid( programs[ i ] );
|
|
|
|
if ( pid == -1 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto& program( programs_[ pid ] );
|
|
|
|
const GLbitfield type( PipelineStages_[ int( program.code.type ) ] );
|
|
|
|
glUseProgramStages( id , type , program.id );
|
|
|
|
}
|
|
|
|
GL_CHECK({
|
|
|
|
glDeleteProgramPipelines( 1 , &id );
|
2017-10-04 17:29:48 +02:00
|
|
|
printf( "... failed\n" );
|
2017-10-04 16:29:39 +02:00
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
|
|
|
pipeline.id = id;
|
2017-10-04 17:29:48 +02:00
|
|
|
printf( "... success\n" );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
void T_ShaderManager::initProgram(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Program_& program )
|
2017-10-03 13:53:50 +02:00
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
assert( program.id == 0 );
|
|
|
|
|
|
|
|
// Build the code
|
|
|
|
auto name( program.name );
|
2017-10-04 17:29:48 +02:00
|
|
|
printf( "init program %s\n" , program.name.c_str( ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
auto& code( program.code );
|
|
|
|
T_CodeBuilder_ cb( [this]( std::string const& n ) { return getInput( n ); } ,
|
|
|
|
name , code );
|
|
|
|
const bool built(
|
|
|
|
T_CodeBuilder_{
|
|
|
|
[this]( std::string const& n ) { return getInput( n ); } ,
|
|
|
|
program.name , code
|
|
|
|
}.buildCode( ) );
|
|
|
|
|
|
|
|
// Initialise file watcher + missing files
|
|
|
|
program.watch = std::make_unique< T_WatchedFiles >( Globals::Watcher( ) ,
|
|
|
|
[this,name]() {
|
|
|
|
programUpdated( name );
|
|
|
|
} );
|
|
|
|
for ( auto entry : code.files ) {
|
|
|
|
if ( entry.second ) {
|
2017-11-04 09:17:31 +01:00
|
|
|
ebcl::T_StringBuilder sb( "shaders/" );
|
|
|
|
sb << entry.first.c_str( );
|
|
|
|
program.watch->watch( std::move( sb ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
} else {
|
|
|
|
missing_[ entry.first ].insert( name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !( built && code.errors.empty( ) ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to compile the program
|
|
|
|
char const* const list[] = {
|
|
|
|
program.code.code.c_str( )
|
|
|
|
};
|
|
|
|
const GLenum sid = glCreateShaderProgramv(
|
|
|
|
ProgramTypes_[ int( code.type ) ] ,
|
|
|
|
1 , &list[ 0 ] );
|
|
|
|
if ( sid == 0 ) {
|
|
|
|
code.errors.push_back( T_ShaderError{
|
|
|
|
name , 0 , "failed to create GL program" } );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and convert the log
|
|
|
|
int infoLogLength( 0 );
|
|
|
|
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;
|
|
|
|
parseGLSLError( code , start );
|
|
|
|
start = found + 1;
|
|
|
|
found = strchr( start , '\n' );
|
2017-10-03 16:25:46 +02:00
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
if ( start < &buffer[ infoLogLength - 1 ] ) {
|
|
|
|
parseGLSLError( code , start );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Did it link?
|
|
|
|
GLint lnk( 0 );
|
|
|
|
glGetProgramiv( sid , GL_LINK_STATUS , &lnk );
|
|
|
|
if ( lnk ) {
|
|
|
|
program.id = sid;
|
2017-10-04 17:29:48 +02:00
|
|
|
printf( "... success\n" );
|
2017-10-04 16:29:39 +02:00
|
|
|
} else {
|
|
|
|
glDeleteProgram( sid );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderManager::parseGLSLError(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderCode& code ,
|
|
|
|
char const* errorLine )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
assert( !code.sources.empty( ) );
|
|
|
|
|
|
|
|
std::cmatch m;
|
|
|
|
uint32_t rawLine;
|
|
|
|
if ( std::regex_match( errorLine , m , GLSLErrorNv_ ) ) {
|
2017-10-04 17:29:48 +02:00
|
|
|
rawLine = atoi( m[ 1 ].str( ).c_str( ) );
|
2017-10-03 16:09:12 +02:00
|
|
|
} else {
|
2017-10-04 16:29:39 +02:00
|
|
|
rawLine = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t check = 0 , pos = 0;
|
|
|
|
while ( pos < code.starts.size( ) && check < rawLine ) {
|
|
|
|
check += code.counts[ pos ];
|
|
|
|
pos ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
code.errors.push_back( T_ShaderError{
|
|
|
|
pos == 0 ? "*unknown*" : code.sources[ pos - 1 ] ,
|
2017-10-04 17:29:48 +02:00
|
|
|
pos == 0 ? 0 : ( rawLine + code.counts[ pos - 1 ] - check + code.starts[ pos - 1 ] ) ,
|
2017-10-04 16:29:39 +02:00
|
|
|
errorLine } );
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderManager::programUpdated(
|
2017-11-04 12:00:58 +01:00
|
|
|
std::string const& name )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
updates_.insert( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderManager::resetProgram(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Program_& program )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
if ( program.watch ) {
|
|
|
|
program.watch.reset( );
|
|
|
|
}
|
|
|
|
if ( program.id != 0 ) {
|
|
|
|
glDeleteProgram( program.id );
|
|
|
|
program.id = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( auto entry : program.code.files ) {
|
|
|
|
if ( entry.second || missing_.find( entry.first ) == missing_.end( ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto& set( missing_[ entry.first ] );
|
|
|
|
set.erase( program.name );
|
|
|
|
if ( set.empty( ) ) {
|
|
|
|
missing_.erase( entry.first );
|
|
|
|
}
|
2017-10-03 13:53:50 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-06 18:42:51 +02:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void T_ShaderManager::makeUI( )
|
|
|
|
{
|
|
|
|
if ( !uiEnabled_ ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
|
|
|
|
ImGui::SetNextWindowSize( ImVec2( dspSize.x , 150 ) ,
|
|
|
|
ImGuiSetCond_Once );
|
|
|
|
ImGui::SetNextWindowPos( ImVec2( 0 , dspSize.y - 150 ) ,
|
|
|
|
ImGuiSetCond_Once );
|
|
|
|
ImGui::Begin( "Shaders" );
|
|
|
|
|
|
|
|
const auto n( std::count_if( programs_.begin( ) , programs_.end( ) ,
|
|
|
|
[]( auto const& p ) { return !p.references.empty( ); } ) );
|
|
|
|
|
|
|
|
std::vector< size_t > indices;
|
|
|
|
const auto rn = programs_.size( );
|
|
|
|
for ( auto i = 0u ; i < rn ; i ++ ) {
|
|
|
|
if ( !programs_[ i ].references.empty( ) ) {
|
|
|
|
indices.push_back( i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort( indices.begin( ) , indices.end( ) ,
|
|
|
|
[this]( size_t a , size_t b ) {
|
|
|
|
auto const& pa( programs_[ a ] );
|
|
|
|
auto const& pb( programs_[ b ] );
|
|
|
|
if ( pa.code.errors.size( ) != pb.code.errors.size( ) ) {
|
|
|
|
return pa.code.errors.size( ) > pb.code.errors.size( );
|
|
|
|
}
|
|
|
|
if ( pa.references.size( ) != pb.references.size( ) ) {
|
|
|
|
return pa.references.size( ) > pb.references.size( );
|
|
|
|
}
|
|
|
|
return programs_[ a ].name < programs_[ b ].name;
|
|
|
|
} );
|
|
|
|
|
|
|
|
for ( auto i = 0u ; i < n ; i ++ ) {
|
|
|
|
auto const& program( programs_[ indices[ i ] ] );
|
|
|
|
const auto nErrors( program.code.errors.size( ) );
|
|
|
|
|
|
|
|
const bool open( nErrors
|
|
|
|
? ImGui::TreeNodeEx( &program , ImGuiTreeNodeFlags_OpenOnArrow
|
|
|
|
| ImGuiTreeNodeFlags_OpenOnDoubleClick , "%s" ,
|
|
|
|
program.name.c_str( ) )
|
|
|
|
: false );
|
|
|
|
|
|
|
|
if ( !nErrors ) {
|
|
|
|
ImGui::Text( "%s" , program.name.c_str( ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine( 400 );
|
|
|
|
ImGui::Text( "Usage: %zu" , program.references.size( ) );
|
|
|
|
ImGui::SameLine( 550 );
|
|
|
|
if ( program.code.errors.empty( ) ) {
|
|
|
|
ImGui::PushStyleColor( ImGuiCol_Text , ImVec4( .6 , 1 , .6 , 1 ) );
|
|
|
|
ImGui::Text( "No errors" );
|
|
|
|
} else {
|
|
|
|
ImGui::PushStyleColor( ImGuiCol_Text , ImVec4( 1 , .6 , .6 , 1 ) );
|
|
|
|
ImGui::Text( "%zu error%s" , nErrors , nErrors > 1 ? "s" : "" );
|
|
|
|
}
|
|
|
|
ImGui::PopStyleColor( );
|
|
|
|
|
|
|
|
if ( open ) {
|
|
|
|
for ( auto const& err : program.code.errors ) {
|
|
|
|
ImGui::NewLine( );
|
|
|
|
ImGui::SameLine( 50 );
|
|
|
|
ImGui::Text( "%s" , err.source.c_str( ) );
|
|
|
|
ImGui::SameLine( 250 );
|
|
|
|
ImGui::Text( "line %d" , err.line );
|
|
|
|
ImGui::SameLine( 370 );
|
|
|
|
ImGui::Text( "%s" , err.error.c_str( ) );
|
|
|
|
}
|
|
|
|
ImGui::TreePop( );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::End( );
|
|
|
|
}
|