Shader loader - recreate shader code

This commit is contained in:
Emmanuel BENOîT 2017-10-03 16:09:12 +02:00
parent 88cd5c97dc
commit 952aad1e02
3 changed files with 194 additions and 16 deletions

View file

@ -19,6 +19,7 @@
#include <memory>
#include <fstream>
#include <map>
#include <set>
#include <regex>
// ImGui

View file

@ -37,6 +37,7 @@ namespace {
~T_InputReader_( );
void read( );
void nl( );
void addAccumulated( );
void handleDirective(
__rd__ T_Tokens_ const& tokens );
@ -73,12 +74,18 @@ namespace {
addAccumulated( );
}
void T_InputReader_::nl( )
{
accumLines ++;
accumulator += '\n';
}
void T_InputReader_::addAccumulated( )
{
if ( accumLines ) {
auto& ck( input.chunks );
ck.emplace( ck.end( ) , E_ShaderInputChunk::CODE ,
accumulator , accumLines );
ck.emplace_back( E_ShaderInputChunk::CODE ,
std::move( accumulator ) , accumLines );
accumulator = {};
accumLines = 0;
}
@ -91,13 +98,16 @@ namespace {
if ( directive == "include" ) {
if ( tokens.size( ) != 2 ) {
nl( );
error( "invalid arguments" );
return;
}
addAccumulated( );
auto& ck( input.chunks );
ck.emplace( ck.end( ) , E_ShaderInputChunk::INCLUDE , tokens[ 1 ] , 1 );
ck.emplace_back( E_ShaderInputChunk::INCLUDE , tokens[ 1 ] , 1 );
} else if ( directive == "type" ) {
nl( );
if ( tokens.size( ) != 2 ) {
error( "invalid arguments" );
return;
@ -110,6 +120,7 @@ namespace {
}
} else {
nl( );
error( "unknown directive" );
}
}
@ -124,6 +135,8 @@ namespace {
} // namespace
/*= T_ShaderInput ============================================================*/
bool T_ShaderInput::load(
__rd__ std::string const& path )
{
@ -133,7 +146,6 @@ bool T_ShaderInput::load(
FILE* const file{ fopen( path.c_str( ) , "r" ) };
if ( !file ) {
errors.push_back( T_ShaderInputError{ 0 , "unable to open" } );
return false;
}
@ -144,20 +156,142 @@ bool T_ShaderInput::load(
}
#if 0
/*============================================================================*/
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 ) {
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 ) );
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" );
const std::string sPath( "shaders/" + source );
T_ShaderInput input;
const bool ok( input.load( sPath ) );
printf( "OK? %s\n" , ok ? "yes" : "no" );
printf( "type: %d\n" , int( input.type ) );
printf( "chunks: %zu\n" , input.chunks.size( ) );
printf( "errors: %zu\n" , input.errors.size( ) );
for ( auto const& error : input.errors ) {
printf( "\tl%d: %s\n" , error.line , error.error.c_str( ) );
T_ShaderCodeLoader loader;
T_Frankenshader code;
if ( loader.load( source , code ) ) {
printf( "SUCCESS! TYPE = %d\n" , int( code.type ) );
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

View file

@ -51,6 +51,49 @@ struct T_ShaderInput
bool load( __rd__ std::string const& path );
};
using P_ShaderInput = std::unique_ptr< T_ShaderInput >;
//void testLoadShaderFile( );
// Type of shader
enum class E_ShaderType {
VERTEX , FRAGMENT ,
};
// Errors in shader code - the errors it represents may come from either
// the input loader, the shader loader or the driver.
struct T_ShaderError
{
std::string source;
uint32_t line;
std::string error;
};
// Shader code after reconstitution
// XXX rename this
struct T_Frankenshader
{
E_ShaderType type;
std::vector< uint32_t > starts; // Position of chunk in source file
std::vector< uint32_t > counts; // Chunk lengths
std::vector< std::string > sources; // Chunk source files
std::string code;
std::vector< T_ShaderError > errors;
};
using P_Frankenshader = std::unique_ptr< T_Frankenshader >;
// XXX test for the loader
struct T_ShaderCodeLoader
{
bool load( __rd__ std::string const& name ,
__wr__ T_Frankenshader& code );
T_ShaderInput const* getInput( __rd__ std::string const& name );
void removeInput( __rd__ std::string const& name );
private:
std::map< std::string , P_ShaderInput > files;
};
void testLoadShaderFile( );