Skip to content

renderer: catch shader source processing exceptions and print raw source #1614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 76 additions & 63 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1729,90 +1729,103 @@ void GLShaderManager::PrintShaderSource( Str::StringRef programName, GLuint obje
std::string delim( "\n" );
std::string src( dump );

ri.Hunk_FreeTempMemory( dump );
try
{
int lineNumber = 0;
size_t pos = 0;

int lineNumber = 0;
size_t pos = 0;
int infoLogID = -1;
if ( infoLog.size() > 0 ) {
infoLogID = 0;
}

int infoLogID = -1;
if ( infoLog.size() > 0 ) {
infoLogID = 0;
}
while ( ( pos = src.find( delim ) ) != std::string::npos ) {
std::string line = src.substr( 0, pos );
if ( Str::IsPrefix( "#line ", line ) )
{
size_t lineNumEnd = line.find( ' ', 6 );
Str::ParseInt( lineNumber, line.substr( 6, lineNumEnd - 6 ) );
}

while ( ( pos = src.find( delim ) ) != std::string::npos ) {
std::string line = src.substr( 0, pos );
if ( Str::IsPrefix( "#line ", line ) )
{
size_t lineNumEnd = line.find( ' ', 6 );
Str::ParseInt( lineNumber, line.substr( 6, lineNumEnd - 6 ) );
}
std::string number = std::to_string( lineNumber );

std::string number = std::to_string( lineNumber );
static const int numberWidth = 4;
int p = numberWidth - number.length();
p = p < 0 ? 0 : p;
number.insert( number.begin(), p, ' ' );

static const int numberWidth = 4;
int p = numberWidth - number.length();
p = p < 0 ? 0 : p;
number.insert( number.begin(), p, ' ' );
buffer.append( number );
buffer.append( ": " );
buffer.append( line );
buffer.append( delim );

buffer.append( number );
buffer.append( ": " );
buffer.append( line );
buffer.append( delim );
while ( infoLogID != -1 && infoLog[infoLogID].line == lineNumber ) {
if ( ( int( line.length() ) > infoLog[infoLogID].character ) && ( infoLog[infoLogID].character != -1 ) ) {
buffer.append( numberWidth + 2, '-' );
const size_t position = line.find_first_not_of( "\t" );

while ( infoLogID != -1 && infoLog[infoLogID].line == lineNumber ) {
if ( ( int( line.length() ) > infoLog[infoLogID].character ) && ( infoLog[infoLogID].character != -1 ) ) {
buffer.append( numberWidth + 2, '-' );
const size_t position = line.find_first_not_of( "\t" );
if ( position != std::string::npos ) {
buffer.append( position, '\t' );
buffer.append( infoLog[infoLogID].character - position, '-' );
} else {
buffer.append( infoLog[infoLogID].character, '-' );
}
buffer.append( "^" );
buffer.append( line.length() - infoLog[infoLogID].character - 1, '-' );

if ( position != std::string::npos ) {
buffer.append( position, '\t' );
buffer.append( infoLog[infoLogID].character - position, '-' );
} else {
buffer.append( infoLog[infoLogID].character, '-' );
}
buffer.append( "^" );
buffer.append( line.length() - infoLog[infoLogID].character - 1, '-' );
} else if ( ( line.length() > 0 ) && ( infoLog[infoLogID].token.length() > 0 ) ) {
size_t position = line.find_first_not_of( "\t" );
size_t prevPosition = 0;

} else if ( ( line.length() > 0 ) && ( infoLog[infoLogID].token.length() > 0 ) ) {
size_t position = line.find_first_not_of( "\t" );
size_t prevPosition = 0;
buffer.append( numberWidth + 2, '-' );
if ( position != std::string::npos ) {
buffer.append( position, '\t' );
} else {
position = 0;
}

buffer.append( numberWidth + 2, '-' );
if ( position != std::string::npos ) {
buffer.append( position, '\t' );
while ( ( position = line.find( infoLog[infoLogID].token, position ) ) && ( position != std::string::npos ) ) {
buffer.append( position - prevPosition - 1, '-' );
buffer.append( "^" );
prevPosition = position;
position++;
}
buffer.append( line.length() - position - 1, '-' );
} else {
position = 0;
buffer.append( numberWidth + 2 + line.length(), '^' );
}

while ( ( position = line.find( infoLog[infoLogID].token, position ) ) && ( position != std::string::npos ) ) {
buffer.append( position - prevPosition - 1, '-' );
buffer.append( "^" );
prevPosition = position;
position++;
buffer.append( delim );
buffer.append( infoLog[infoLogID].error );
buffer.append( delim );
buffer.append( delim );

infoLogID++;

if ( infoLogID >= int( infoLog.size() ) ) {
infoLogID = -1;
}
buffer.append( line.length() - position - 1, '-' );
} else {
buffer.append( numberWidth + 2 + line.length(), '^' );
}

buffer.append( delim );
buffer.append( infoLog[infoLogID].error );
buffer.append( delim );
buffer.append( delim );

infoLogID++;
src.erase( 0, pos + delim.length() );

if ( infoLogID >= int( infoLog.size() ) ) {
infoLogID = -1;
}
lineNumber++;
}

src.erase( 0, pos + delim.length() );

lineNumber++;
Log::Warn( "Source for shader program %s:\n%s", programName, buffer );
}
catch (std::exception& err)
{
Log::Warn( "Exception occurred when processing the source for shader program %s (%s): %s", programName, typeid(err).name(), err.what() );
Log::Warn( "Raw source for shader program %s:\n%s", programName, dump );
}
catch (...)
{
Log::Warn( "Unknown exception occurred when processing the source for shader program %s.", programName );
Log::Warn( "Raw source for shader program %s:\n%s", programName, dump );
}

Log::Warn("Source for shader program %s:\n%s", programName, buffer.c_str());
ri.Hunk_FreeTempMemory( dump );
}

std::vector<GLShaderManager::InfoLogEntry> GLShaderManager::ParseInfoLog( const std::string& infoLog ) const {
Expand Down