Moved shader code builder to separate internal struct
This commit is contained in:
parent
7a7ff88ac6
commit
8be3c508a3
1 changed files with 135 additions and 78 deletions
149
shaders.cc
149
shaders.cc
|
@ -16,6 +16,7 @@ const std::map< std::string , E_ShaderInput > InputTypes_( ([] {
|
||||||
return t;
|
return t;
|
||||||
})());
|
})());
|
||||||
|
|
||||||
|
|
||||||
/*============================================================================*/
|
/*============================================================================*/
|
||||||
|
|
||||||
// Input reader state and functions, used when loading a source file
|
// Input reader state and functions, used when loading a source file
|
||||||
|
@ -163,66 +164,119 @@ bool T_ShaderInput::load(
|
||||||
|
|
||||||
/*============================================================================*/
|
/*============================================================================*/
|
||||||
|
|
||||||
bool T_ShaderCodeLoader::load(
|
namespace {
|
||||||
__rd__ std::string const& name ,
|
|
||||||
__wr__ T_Frankenshader& code )
|
// Code builder, state and functions
|
||||||
|
struct T_CodeBuilder_
|
||||||
{
|
{
|
||||||
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_ {
|
struct T_StackEntry_ {
|
||||||
std::string name;
|
std::string name;
|
||||||
T_ShaderInput const* input;
|
T_ShaderInput const* input;
|
||||||
uint32_t pos;
|
uint32_t pos;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
T_ShaderCodeLoader& loader;
|
||||||
|
const std::string name;
|
||||||
|
T_Frankenshader& code;
|
||||||
|
|
||||||
|
T_ShaderInput const* main;
|
||||||
std::map< std::string , uint32_t > pos;
|
std::map< std::string , uint32_t > pos;
|
||||||
std::vector< T_StackEntry_ > stack;
|
std::vector< T_StackEntry_ > stack;
|
||||||
std::set< std::string > libraries;
|
std::set< std::string > libraries;
|
||||||
T_ShaderInput const* current( main );
|
T_ShaderInput const* current;
|
||||||
uint32_t cpos{ 0 };
|
uint32_t cpos{ 0 };
|
||||||
std::string cname( name );
|
std::string cname;
|
||||||
pos[ cname ] = 1;
|
|
||||||
|
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;
|
||||||
|
|
||||||
while ( cpos < current->chunks.size( ) ) {
|
while ( cpos < current->chunks.size( ) ) {
|
||||||
auto& chunk( current->chunks[ cpos ] );
|
auto& chunk( current->chunks[ cpos ] );
|
||||||
if ( chunk.type == E_ShaderInputChunk::CODE ) {
|
if ( chunk.type == E_ShaderInputChunk::CODE ) {
|
||||||
// Chunk of code; just append it.
|
appendChunk( chunk );
|
||||||
|
} else {
|
||||||
|
include( chunk.text , chunk.lines );
|
||||||
|
}
|
||||||
|
|
||||||
|
next( );
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void T_CodeBuilder_::appendChunk(
|
||||||
|
__rd__ T_ShaderInputChunk const& chunk )
|
||||||
|
{
|
||||||
code.sources.push_back( cname );
|
code.sources.push_back( cname );
|
||||||
code.counts.push_back( chunk.lines );
|
code.counts.push_back( chunk.lines );
|
||||||
code.starts.push_back( pos[ cname ] );
|
code.starts.push_back( pos[ cname ] );
|
||||||
code.code += chunk.text;
|
code.code += chunk.text;
|
||||||
pos[ cname ] += chunk.lines;
|
pos[ cname ] += chunk.lines;
|
||||||
} else {
|
}
|
||||||
// Include. Make sure we're not recursing.
|
|
||||||
|
void T_CodeBuilder_::include(
|
||||||
|
__rd__ std::string const& nname ,
|
||||||
|
__rd__ const uint32_t lines )
|
||||||
|
{
|
||||||
const auto prevPos( pos[ cname ] );
|
const auto prevPos( pos[ cname ] );
|
||||||
pos[ cname ] += chunk.lines;
|
pos[ cname ] += lines;
|
||||||
auto& nname( chunk.text );
|
|
||||||
|
// Avoid recursion
|
||||||
if ( cname == nname || 0 != count_if( stack.begin( ) , stack.end( ) ,
|
if ( cname == nname || 0 != count_if( stack.begin( ) , stack.end( ) ,
|
||||||
[nname] ( T_StackEntry_ const& e ) {
|
[nname] ( T_StackEntry_ const& e ) {
|
||||||
return nname == e.name;
|
return nname == e.name;
|
||||||
} ) ) {
|
} ) ) {
|
||||||
code.errors.push_back( T_ShaderError{ cname , prevPos ,
|
code.errors.push_back( T_ShaderError{ cname , prevPos ,
|
||||||
"recursive inclusion of '" + nname + "'" } );
|
"recursive inclusion of '" + nname + "'" } );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
} else if ( libraries.find( nname ) == libraries.end( ) ) {
|
// Avoid including libraries more than once
|
||||||
// Load it unless it's a library that's already included
|
if ( libraries.find( nname ) != libraries.end( ) ) {
|
||||||
T_ShaderInput const* const isi( getInput( nname ) );
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
T_ShaderInput const* const isi( loader.getInput( nname ) );
|
||||||
code.files.emplace( nname , isi != nullptr );
|
code.files.emplace( nname , isi != nullptr );
|
||||||
if ( isi && ( isi->type == E_ShaderInput::CHUNK
|
|
||||||
|| isi->type == E_ShaderInput::LIBRARY ) ) {
|
// Check for problems
|
||||||
// Ok, let's do this
|
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( ) ) {
|
if ( libraries.find( nname ) == libraries.end( ) ) {
|
||||||
for ( auto const& errs : isi->errors ) {
|
for ( auto const& errs : isi->errors ) {
|
||||||
code.errors.push_back( T_ShaderError{
|
code.errors.push_back( T_ShaderError{
|
||||||
|
@ -230,25 +284,17 @@ bool T_ShaderCodeLoader::load(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
libraries.insert( nname );
|
libraries.insert( nname );
|
||||||
stack.push_back( T_StackEntry_{
|
|
||||||
cname , current , cpos } );
|
// Enter the new file
|
||||||
|
stack.push_back( T_StackEntry_{ cname , current , cpos } );
|
||||||
cname = nname;
|
cname = nname;
|
||||||
current = isi;
|
current = isi;
|
||||||
cpos = UINT32_MAX;
|
cpos = UINT32_MAX;
|
||||||
pos[ cname ] = 1;
|
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" } );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void T_CodeBuilder_::next( )
|
||||||
|
{
|
||||||
cpos ++;
|
cpos ++;
|
||||||
while ( cpos == current->chunks.size( ) && !stack.empty( ) ) {
|
while ( cpos == current->chunks.size( ) && !stack.empty( ) ) {
|
||||||
T_StackEntry_ const& se( stack[ stack.size( ) - 1 ] );
|
T_StackEntry_ const& se( stack[ stack.size( ) - 1 ] );
|
||||||
|
@ -260,7 +306,18 @@ bool T_ShaderCodeLoader::load(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
|
/*============================================================================*/
|
||||||
|
|
||||||
|
bool T_ShaderCodeLoader::load(
|
||||||
|
__rd__ std::string const& name ,
|
||||||
|
__wr__ T_Frankenshader& code )
|
||||||
|
{
|
||||||
|
T_CodeBuilder_ cb( *this , name , code );
|
||||||
|
return cb.buildCode( );
|
||||||
}
|
}
|
||||||
|
|
||||||
T_ShaderInput const* T_ShaderCodeLoader::getInput(
|
T_ShaderInput const* T_ShaderCodeLoader::getInput(
|
||||||
|
|
Loading…
Reference in a new issue