Skip to content

Commit 1c9a5f2

Browse files
committed
renderer: catch shader source processing exceptions and print raw source
1 parent 13886d8 commit 1c9a5f2

File tree

1 file changed

+76
-63
lines changed

1 file changed

+76
-63
lines changed

src/engine/renderer/gl_shader.cpp

+76-63
Original file line numberDiff line numberDiff line change
@@ -1729,90 +1729,103 @@ void GLShaderManager::PrintShaderSource( Str::StringRef programName, GLuint obje
17291729
std::string delim( "\n" );
17301730
std::string src( dump );
17311731

1732-
ri.Hunk_FreeTempMemory( dump );
1732+
try
1733+
{
1734+
int lineNumber = 0;
1735+
size_t pos = 0;
17331736

1734-
int lineNumber = 0;
1735-
size_t pos = 0;
1737+
int infoLogID = -1;
1738+
if ( infoLog.size() > 0 ) {
1739+
infoLogID = 0;
1740+
}
17361741

1737-
int infoLogID = -1;
1738-
if ( infoLog.size() > 0 ) {
1739-
infoLogID = 0;
1740-
}
1742+
while ( ( pos = src.find( delim ) ) != std::string::npos ) {
1743+
std::string line = src.substr( 0, pos );
1744+
if ( Str::IsPrefix( "#line ", line ) )
1745+
{
1746+
size_t lineNumEnd = line.find( ' ', 6 );
1747+
Str::ParseInt( lineNumber, line.substr( 6, lineNumEnd - 6 ) );
1748+
}
17411749

1742-
while ( ( pos = src.find( delim ) ) != std::string::npos ) {
1743-
std::string line = src.substr( 0, pos );
1744-
if ( Str::IsPrefix( "#line ", line ) )
1745-
{
1746-
size_t lineNumEnd = line.find( ' ', 6 );
1747-
Str::ParseInt( lineNumber, line.substr( 6, lineNumEnd - 6 ) );
1748-
}
1750+
std::string number = std::to_string( lineNumber );
17491751

1750-
std::string number = std::to_string( lineNumber );
1752+
static const int numberWidth = 4;
1753+
int p = numberWidth - number.length();
1754+
p = p < 0 ? 0 : p;
1755+
number.insert( number.begin(), p, ' ' );
17511756

1752-
static const int numberWidth = 4;
1753-
int p = numberWidth - number.length();
1754-
p = p < 0 ? 0 : p;
1755-
number.insert( number.begin(), p, ' ' );
1757+
buffer.append( number );
1758+
buffer.append( ": " );
1759+
buffer.append( line );
1760+
buffer.append( delim );
17561761

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

1762-
while ( infoLogID != -1 && infoLog[infoLogID].line == lineNumber ) {
1763-
if ( ( int( line.length() ) > infoLog[infoLogID].character ) && ( infoLog[infoLogID].character != -1 ) ) {
1764-
buffer.append( numberWidth + 2, '-' );
1765-
const size_t position = line.find_first_not_of( "\t" );
1767+
if ( position != std::string::npos ) {
1768+
buffer.append( position, '\t' );
1769+
buffer.append( infoLog[infoLogID].character - position, '-' );
1770+
} else {
1771+
buffer.append( infoLog[infoLogID].character, '-' );
1772+
}
1773+
buffer.append( "^" );
1774+
buffer.append( line.length() - infoLog[infoLogID].character - 1, '-' );
17661775

1767-
if ( position != std::string::npos ) {
1768-
buffer.append( position, '\t' );
1769-
buffer.append( infoLog[infoLogID].character - position, '-' );
1770-
} else {
1771-
buffer.append( infoLog[infoLogID].character, '-' );
1772-
}
1773-
buffer.append( "^" );
1774-
buffer.append( line.length() - infoLog[infoLogID].character - 1, '-' );
1776+
} else if ( ( line.length() > 0 ) && ( infoLog[infoLogID].token.length() > 0 ) ) {
1777+
size_t position = line.find_first_not_of( "\t" );
1778+
size_t prevPosition = 0;
17751779

1776-
} else if ( ( line.length() > 0 ) && ( infoLog[infoLogID].token.length() > 0 ) ) {
1777-
size_t position = line.find_first_not_of( "\t" );
1778-
size_t prevPosition = 0;
1780+
buffer.append( numberWidth + 2, '-' );
1781+
if ( position != std::string::npos ) {
1782+
buffer.append( position, '\t' );
1783+
} else {
1784+
position = 0;
1785+
}
17791786

1780-
buffer.append( numberWidth + 2, '-' );
1781-
if ( position != std::string::npos ) {
1782-
buffer.append( position, '\t' );
1787+
while ( ( position = line.find( infoLog[infoLogID].token, position ) ) && ( position != std::string::npos ) ) {
1788+
buffer.append( position - prevPosition - 1, '-' );
1789+
buffer.append( "^" );
1790+
prevPosition = position;
1791+
position++;
1792+
}
1793+
buffer.append( line.length() - position - 1, '-' );
17831794
} else {
1784-
position = 0;
1795+
buffer.append( numberWidth + 2 + line.length(), '^' );
17851796
}
17861797

1787-
while ( ( position = line.find( infoLog[infoLogID].token, position ) ) && ( position != std::string::npos ) ) {
1788-
buffer.append( position - prevPosition - 1, '-' );
1789-
buffer.append( "^" );
1790-
prevPosition = position;
1791-
position++;
1798+
buffer.append( delim );
1799+
buffer.append( infoLog[infoLogID].error );
1800+
buffer.append( delim );
1801+
buffer.append( delim );
1802+
1803+
infoLogID++;
1804+
1805+
if ( infoLogID >= int( infoLog.size() ) ) {
1806+
infoLogID = -1;
17921807
}
1793-
buffer.append( line.length() - position - 1, '-' );
1794-
} else {
1795-
buffer.append( numberWidth + 2 + line.length(), '^' );
17961808
}
17971809

1798-
buffer.append( delim );
1799-
buffer.append( infoLog[infoLogID].error );
1800-
buffer.append( delim );
1801-
buffer.append( delim );
1802-
1803-
infoLogID++;
1810+
src.erase( 0, pos + delim.length() );
18041811

1805-
if ( infoLogID >= int( infoLog.size() ) ) {
1806-
infoLogID = -1;
1807-
}
1812+
lineNumber++;
18081813
}
18091814

1810-
src.erase( 0, pos + delim.length() );
1811-
1812-
lineNumber++;
1815+
Log::Warn("Source for shader program %s:\n%s", programName, buffer);
1816+
}
1817+
catch (std::exception& err)
1818+
{
1819+
Log::Warn("Exception occurred when processing the source for shader program %s (%s): %s", programName, typeid(err).name(), err.what() );
1820+
Log::Warn("Raw source for shader program %s:\n%s", programName, dump);
1821+
}
1822+
catch (...)
1823+
{
1824+
Log::Warn("Unknown exception occurred when processing the source for shader program %s.", programName);
1825+
Log::Warn("Raw source for shader program %s:\n%s", programName, dump);
18131826
}
18141827

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

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

0 commit comments

Comments
 (0)