312 lines
7.4 KiB
C++
312 lines
7.4 KiB
C++
#include "externals.hh"
|
|
#include "shaders.hh"
|
|
|
|
|
|
namespace {
|
|
|
|
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 );
|
|
return t;
|
|
})());
|
|
|
|
/*============================================================================*/
|
|
|
|
// 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 ++;
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
void T_InputReader_::handleDirective(
|
|
__rd__ T_Tokens_ const& tokens )
|
|
{
|
|
auto const& directive( tokens[ 0 ] );
|
|
|
|
if ( directive == "include" ) {
|
|
if ( tokens.size( ) != 2 ) {
|
|
nl( );
|
|
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" );
|
|
} else {
|
|
input.type = pos->second;
|
|
}
|
|
|
|
} else {
|
|
nl( );
|
|
error( "unknown directive" );
|
|
}
|
|
}
|
|
|
|
void T_InputReader_::error(
|
|
__rd__ std::string const& err )
|
|
{
|
|
input.errors.push_back( T_ShaderInputError{
|
|
line , err } );
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
/*= T_ShaderInput ============================================================*/
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
/*============================================================================*/
|
|
|
|
bool T_ShaderCodeLoader::load(
|
|
__rd__ std::string const& name ,
|
|
__wr__ T_Frankenshader& code )
|
|
{
|
|
code = T_Frankenshader{ };
|
|
|
|
T_ShaderInput const* const main( getInput( name ) );
|
|
if ( !main ) {
|
|
code.files.emplace( name , false );
|
|
return false;
|
|
}
|
|
if ( main->type == E_ShaderInput::LIBRARY
|
|
|| main->type == E_ShaderInput::CHUNK ) {
|
|
code.errors.push_back( T_ShaderError{ name , 0 ,
|
|
"incorrect file type" } );
|
|
} else {
|
|
code.type = E_ShaderType( int( main->type ) - 2 );
|
|
}
|
|
|
|
struct T_StackEntry_ {
|
|
std::string name;
|
|
T_ShaderInput const* input;
|
|
uint32_t pos;
|
|
};
|
|
std::map< std::string , uint32_t > pos;
|
|
std::vector< T_StackEntry_ > stack;
|
|
std::set< std::string > libraries;
|
|
T_ShaderInput const* current( main );
|
|
uint32_t cpos{ 0 };
|
|
std::string cname( name );
|
|
pos[ cname ] = 1;
|
|
|
|
while ( cpos < current->chunks.size( ) ) {
|
|
auto& chunk( current->chunks[ cpos ] );
|
|
if ( chunk.type == E_ShaderInputChunk::CODE ) {
|
|
// Chunk of code; just append it.
|
|
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;
|
|
} else {
|
|
// Include. Make sure we're not recursing.
|
|
const auto prevPos( pos[ cname ] );
|
|
pos[ cname ] += chunk.lines;
|
|
auto& nname( chunk.text );
|
|
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 + "'" } );
|
|
|
|
} else if ( libraries.find( nname ) == libraries.end( ) ) {
|
|
// Load it unless it's a library that's already included
|
|
T_ShaderInput const* const isi( getInput( nname ) );
|
|
code.files.emplace( nname , isi != nullptr );
|
|
if ( isi && ( isi->type == E_ShaderInput::CHUNK
|
|
|| isi->type == E_ShaderInput::LIBRARY ) ) {
|
|
// Ok, let's do this
|
|
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 );
|
|
stack.push_back( T_StackEntry_{
|
|
cname , current , cpos } );
|
|
cname = nname;
|
|
current = isi;
|
|
cpos = UINT32_MAX;
|
|
pos[ cname ] = 1;
|
|
|
|
} else if ( isi ) {
|
|
// Trying to load a top-level shader
|
|
code.errors.push_back( T_ShaderError{ cname , prevPos ,
|
|
"trying to include a top-level file" } );
|
|
} else {
|
|
// Not found
|
|
code.errors.push_back( T_ShaderError{ cname , prevPos ,
|
|
"file not found" } );
|
|
}
|
|
}
|
|
}
|
|
|
|
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( );
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
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
|
|
void testLoadShaderFile( )
|
|
{
|
|
const std::string source( "test-loader.glsl" );
|
|
T_ShaderCodeLoader loader;
|
|
T_Frankenshader code;
|
|
if ( loader.load( source , code ) ) {
|
|
printf( "SUCCESS! TYPE = %d\n" , int( code.type ) );
|
|
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( ) );
|
|
}
|
|
}
|
|
} else {
|
|
printf( "fail :'(\n" );
|
|
}
|
|
}
|
|
#endif
|