Some reformatting
This commit is contained in:
parent
952aad1e02
commit
8eefe21aae
1 changed files with 114 additions and 109 deletions
223
shaders.cc
223
shaders.cc
|
@ -3,134 +3,139 @@
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
const std::regex PreprocDirective_( "^\\s*//!\\s*([a-z]+(\\s+([^\\s]+))*)\\s*$" );
|
|
||||||
|
|
||||||
const std::map< std::string , E_ShaderInput > InputTypes_( ([] {
|
const std::regex PreprocDirective_( "^\\s*//!\\s*([a-z]+(\\s+([^\\s]+))*)\\s*$" );
|
||||||
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;
|
|
||||||
})());
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
})());
|
||||||
|
|
||||||
struct T_InputReader_
|
/*============================================================================*/
|
||||||
{
|
|
||||||
using T_Tokens_ = std::vector< std::string >;
|
|
||||||
|
|
||||||
FILE* const file;
|
// Input reader state and functions, used when loading a source file
|
||||||
T_ShaderInput& input;
|
struct T_InputReader_
|
||||||
uint32_t line{ 0 };
|
{
|
||||||
|
using T_Tokens_ = std::vector< std::string >;
|
||||||
|
|
||||||
char* buffer{ nullptr };
|
FILE* const file;
|
||||||
ssize_t readCount;
|
T_ShaderInput& input;
|
||||||
|
uint32_t line{ 0 };
|
||||||
|
|
||||||
std::string accumulator{ };
|
char* buffer{ nullptr };
|
||||||
uint32_t accumLines{ 0 };
|
ssize_t readCount;
|
||||||
|
|
||||||
T_InputReader_( __rd__ FILE* const file ,
|
std::string accumulator{ };
|
||||||
__rw__ T_ShaderInput& input )
|
uint32_t accumLines{ 0 };
|
||||||
: file( file ) , input( input )
|
|
||||||
{ }
|
|
||||||
~T_InputReader_( );
|
|
||||||
|
|
||||||
void read( );
|
T_InputReader_( __rd__ FILE* const file ,
|
||||||
void nl( );
|
__rw__ T_ShaderInput& input )
|
||||||
void addAccumulated( );
|
: file( file ) , input( input )
|
||||||
void handleDirective(
|
{ }
|
||||||
__rd__ T_Tokens_ const& tokens );
|
~T_InputReader_( );
|
||||||
void error( __rd__ std::string const& err );
|
|
||||||
};
|
|
||||||
|
|
||||||
T_InputReader_::~T_InputReader_( )
|
void read( );
|
||||||
{
|
void nl( );
|
||||||
if ( buffer ) {
|
void addAccumulated( );
|
||||||
free( buffer );
|
void handleDirective(
|
||||||
}
|
__rd__ T_Tokens_ const& tokens );
|
||||||
fclose( file );
|
void error( __rd__ std::string const& err );
|
||||||
|
};
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
T_InputReader_::~T_InputReader_( )
|
||||||
|
{
|
||||||
|
if ( buffer ) {
|
||||||
|
free( buffer );
|
||||||
}
|
}
|
||||||
|
fclose( file );
|
||||||
|
}
|
||||||
|
|
||||||
void T_InputReader_::read( )
|
void T_InputReader_::read( )
|
||||||
{
|
{
|
||||||
size_t bsz( 0 );
|
size_t bsz( 0 );
|
||||||
while ( ( readCount = getline( &buffer , &bsz , file ) ) != -1 ) {
|
while ( ( readCount = getline( &buffer , &bsz , file ) ) != -1 ) {
|
||||||
line ++;
|
line ++;
|
||||||
std::cmatch match;
|
std::cmatch match;
|
||||||
if ( std::regex_match( buffer , match , PreprocDirective_ ) ) {
|
if ( std::regex_match( buffer , match , PreprocDirective_ ) ) {
|
||||||
const T_Tokens_ tokens( ([](std::string const& a) {
|
const T_Tokens_ tokens( ([](std::string const& a) {
|
||||||
using stri = std::istream_iterator< std::string >;
|
using stri = std::istream_iterator< std::string >;
|
||||||
std::istringstream iss( a );
|
std::istringstream iss( a );
|
||||||
return T_Tokens_{ stri( iss ) , stri( ) };
|
return T_Tokens_{ stri( iss ) , stri( ) };
|
||||||
})( match[ 1 ].str( ) ) );
|
})( match[ 1 ].str( ) ) );
|
||||||
assert( tokens.size( ) >= 1 );
|
assert( tokens.size( ) >= 1 );
|
||||||
handleDirective( tokens );
|
handleDirective( tokens );
|
||||||
} else {
|
} else {
|
||||||
accumulator += buffer;
|
accumulator += buffer;
|
||||||
accumLines ++;
|
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( );
|
addAccumulated( );
|
||||||
}
|
auto& ck( input.chunks );
|
||||||
|
ck.emplace_back( E_ShaderInputChunk::INCLUDE , tokens[ 1 ] , 1 );
|
||||||
|
|
||||||
void T_InputReader_::nl( )
|
} else if ( directive == "type" ) {
|
||||||
{
|
nl( );
|
||||||
accumLines ++;
|
if ( tokens.size( ) != 2 ) {
|
||||||
accumulator += '\n';
|
error( "invalid arguments" );
|
||||||
}
|
return;
|
||||||
|
|
||||||
void T_InputReader_::addAccumulated( )
|
|
||||||
{
|
|
||||||
if ( accumLines ) {
|
|
||||||
auto& ck( input.chunks );
|
|
||||||
ck.emplace_back( E_ShaderInputChunk::CODE ,
|
|
||||||
std::move( accumulator ) , accumLines );
|
|
||||||
accumulator = {};
|
|
||||||
accumLines = 0;
|
|
||||||
}
|
}
|
||||||
}
|
auto pos( InputTypes_.find( tokens[ 1 ] ) );
|
||||||
|
if ( pos == InputTypes_.end( ) ) {
|
||||||
void T_InputReader_::handleDirective(
|
error( "unknown type" );
|
||||||
__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 {
|
} else {
|
||||||
nl( );
|
input.type = pos->second;
|
||||||
error( "unknown directive" );
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void T_InputReader_::error(
|
} else {
|
||||||
__rd__ std::string const& err )
|
nl( );
|
||||||
{
|
error( "unknown directive" );
|
||||||
input.errors.push_back( T_ShaderInputError{
|
|
||||||
line , err } );
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void T_InputReader_::error(
|
||||||
|
__rd__ std::string const& err )
|
||||||
|
{
|
||||||
|
input.errors.push_back( T_ShaderInputError{
|
||||||
|
line , err } );
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue