2017-10-03 13:53:50 +02:00
|
|
|
#include "externals.hh"
|
|
|
|
#include "shaders.hh"
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-10-03 16:10:48 +02:00
|
|
|
const std::regex PreprocDirective_( "^\\s*//!\\s*([a-z]+(\\s+([^\\s]+))*)\\s*$" );
|
|
|
|
|
|
|
|
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-03 16:10:48 +02:00
|
|
|
return t;
|
|
|
|
})());
|
|
|
|
|
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 };
|
|
|
|
|
|
|
|
T_InputReader_( __rd__ FILE* const file ,
|
|
|
|
__rw__ T_ShaderInput& input )
|
|
|
|
: file( file ) , input( input )
|
|
|
|
{ }
|
|
|
|
~T_InputReader_( );
|
|
|
|
|
|
|
|
void read( );
|
|
|
|
void nl( );
|
|
|
|
void addAccumulated( );
|
|
|
|
void handleDirective(
|
|
|
|
__rd__ T_Tokens_ const& tokens );
|
|
|
|
void error( __rd__ std::string const& err );
|
|
|
|
};
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
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( );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:09:12 +02:00
|
|
|
}
|
2017-10-03 16:10:48 +02:00
|
|
|
}
|
2017-10-03 16:09:12 +02:00
|
|
|
|
2017-10-03 16:10:48 +02:00
|
|
|
void T_InputReader_::handleDirective(
|
|
|
|
__rd__ T_Tokens_ const& tokens )
|
|
|
|
{
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
void T_InputReader_::error(
|
|
|
|
__rd__ std::string const& err )
|
|
|
|
{
|
|
|
|
input.errors.push_back( T_ShaderInputError{
|
|
|
|
line , err } );
|
|
|
|
}
|
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(
|
|
|
|
__rd__ std::string const& path )
|
|
|
|
{
|
|
|
|
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-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-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
|
|
|
|
|
|
|
T_ShaderCodeLoader& loader;
|
|
|
|
const std::string name;
|
|
|
|
T_Frankenshader& code;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
T_CodeBuilder_( __rw__ T_ShaderCodeLoader& loader ,
|
|
|
|
__rd__ std::string const& name ,
|
|
|
|
__rw__ T_Frankenshader& code )
|
|
|
|
: loader( loader ) , name( name ) , code( code ) ,
|
|
|
|
main( loader.getInput( name ) )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool buildCode( );
|
|
|
|
void appendChunk( __rd__ T_ShaderInputChunk const& chunk );
|
|
|
|
void include( __rd__ std::string const& name ,
|
|
|
|
__rd__ const uint32_t lines );
|
|
|
|
void next( );
|
|
|
|
};
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
bool T_CodeBuilder_::buildCode( )
|
|
|
|
{
|
|
|
|
code = T_Frankenshader{ };
|
|
|
|
code.files.emplace( name , main != nullptr );
|
|
|
|
if ( !main ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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(
|
|
|
|
__rd__ T_ShaderInputChunk const& chunk )
|
|
|
|
{
|
|
|
|
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(
|
|
|
|
__rd__ std::string const& nname ,
|
|
|
|
__rd__ const uint32_t lines )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderInput const* const isi( loader.getInput( nname ) );
|
|
|
|
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( ) ) {
|
|
|
|
for ( auto const& errs : isi->errors ) {
|
|
|
|
code.errors.push_back( T_ShaderError{
|
|
|
|
nname , errs.line , errs.error } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
libraries.insert( nname );
|
|
|
|
|
|
|
|
// Enter the new file
|
|
|
|
stack.push_back( T_StackEntry_{ cname , current , cpos } );
|
|
|
|
cname = nname;
|
|
|
|
current = isi;
|
|
|
|
cpos = UINT32_MAX;
|
|
|
|
pos[ cname ] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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( );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
/*============================================================================*/
|
|
|
|
|
|
|
|
bool T_ShaderCodeLoader::load(
|
|
|
|
__rd__ std::string const& name ,
|
|
|
|
__wr__ T_Frankenshader& code )
|
|
|
|
{
|
|
|
|
T_CodeBuilder_ cb( *this , name , code );
|
|
|
|
return cb.buildCode( );
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:09:12 +02:00
|
|
|
T_ShaderInput const* T_ShaderCodeLoader::getInput(
|
|
|
|
__rd__ std::string const& name )
|
|
|
|
{
|
|
|
|
auto pos( files.find( name ) );
|
|
|
|
if ( pos != files.end( ) ) {
|
|
|
|
return pos->second.get( );
|
|
|
|
}
|
|
|
|
T_ShaderInput ni;
|
|
|
|
if ( !ni.load( "shaders/" + name ) ) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
files.emplace( name , std::make_unique< T_ShaderInput >(
|
|
|
|
std::move( ni ) ) );
|
|
|
|
return files.find( name )->second.get( );
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderCodeLoader::removeInput(
|
|
|
|
__rd__ std::string const& name )
|
|
|
|
{
|
|
|
|
files.erase( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 1
|
2017-10-03 13:53:50 +02:00
|
|
|
void testLoadShaderFile( )
|
|
|
|
{
|
|
|
|
const std::string source( "test-loader.glsl" );
|
2017-10-03 16:09:12 +02:00
|
|
|
T_ShaderCodeLoader loader;
|
|
|
|
T_Frankenshader code;
|
|
|
|
if ( loader.load( source , code ) ) {
|
|
|
|
printf( "SUCCESS! TYPE = %d\n" , int( code.type ) );
|
2017-10-03 16:25:46 +02:00
|
|
|
printf( "FILES:\n" );
|
|
|
|
for ( auto const& e : code.files ) {
|
|
|
|
printf( " -> %s (%s)\n" , e.first.c_str( ) ,
|
|
|
|
e.second ? "found" : "missing" );
|
|
|
|
}
|
|
|
|
if ( code.errors.size( ) ) {
|
|
|
|
printf( "ERRORS:\n" );
|
|
|
|
for ( auto const& err : code.errors ) {
|
|
|
|
printf( "%s:%d: %s\n" , err.source.c_str( ) , err.line , err.error.c_str( ) );
|
|
|
|
}
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf( "fail :'(\n" );
|
2017-10-03 13:53:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|