@@ -820,37 +820,34 @@ std::string GLShaderManager::BuildGPUShaderText( Str::StringRef mainShaderNa
820820 // so we have to reset the line counting.
821821 env += " #line 0\n " ;
822822
823- std::string shaderText = env + libs + GetShaderText ( filename );
824-
825- std::istringstream shaderTextStream ( shaderText );
826- std::string shaderMain;
823+ std::string mainShaderText = GetShaderText ( filename );
824+ std::string out = env + libs;
825+ std::istringstream shaderTextStream ( mainShaderText );
827826
828827 std::string line;
829- uint insertCount = 0 ;
828+ int insertCount = 0 ;
829+ int lineCount = 0 ;
830830
831831 while ( std::getline ( shaderTextStream, line, ' \n ' ) ) {
832+ ++lineCount;
832833 const std::string::size_type position = line.find ( " #insert" );
833834 if ( position == std::string::npos || line.find_first_not_of ( " \t " ) != position ) {
834- shaderMain += line + " \n " ;
835+ out += line + " \n " ;
835836 continue ;
836837 }
837838
838839 std::string shaderInsertPath = line.substr ( position + 8 , std::string::npos );
839- shaderMain += " /* Shader file: glsl/" + shaderInsertPath + " .glsl: */\n " ;
840840
841- // Put a line count marker here so we don't get the wrong line number due to previous #line 0
842- shaderMain += " #line -1\n " ;
843-
844841 // Inserted shader lines will start at 10000, 20000 etc. to easily tell them apart from the main shader code
845- shaderMain += " #line " + std::to_string ( ( insertCount + 1 ) * 10000 ) + " \n " ;
846- insertCount++;
847-
848- shaderMain += GetShaderText ( " glsl/" + shaderInsertPath + " .glsl" );
842+ // #insert recursion is not supported
843+ ++insertCount;
844+ out += " #line " + std::to_string ( insertCount * 10000 ) + " // " + shaderInsertPath + " .glsl\n " ;
849845
850- shaderMain += " #line -1\n " ;
846+ out += GetShaderText ( " glsl/" + shaderInsertPath + " .glsl" );
847+ out += " #line " + std::to_string ( lineCount ) + " \n " ;
851848 }
852849
853- return shaderMain ;
850+ return out ;
854851}
855852
856853static bool IsUnusedPermutation ( const char *compileMacros )
@@ -1342,36 +1339,13 @@ void GLShaderManager::PrintShaderSource( Str::StringRef programName, GLuint obje
13421339
13431340 int lineNumber = 0 ;
13441341 size_t pos = 0 ;
1345- int lineMarker = - 1 ;
1342+
13461343 while ( ( pos = src.find ( delim ) ) != std::string::npos ) {
13471344 std::string line = src.substr ( 0 , pos );
1348-
1349- const std::string::size_type position = line.find ( " #line" );
1350- if ( ( position != std::string::npos ) && ( line.find_first_not_of ( " \t " ) == position ) ) {
1351- const int newLineNumber = std::stoi ( line.substr ( 5 , std::string::npos ) );
1352-
1353- /* #line -1 is used as a line marker
1354- * The line number will continue from the line before the start marker, eg:
1355- * // line 10
1356- * #line -1
1357- * #line 10000
1358- * // .. a bunch of code here
1359- * #line -1
1360- * // This line will have line number 11
1361- * This does NOT support nesting
1362- */
1363- if ( newLineNumber == -1 ) {
1364- if ( lineMarker == -1 ) {
1365- lineMarker = lineNumber;
1366- } else {
1367- lineNumber = lineMarker;
1368- lineMarker = -1 ;
1369- }
1370- src.erase ( 0 , pos + delim.length () );
1371- continue ;
1372- } else {
1373- lineNumber = newLineNumber;
1374- }
1345+ if ( Str::IsPrefix ( " #line " , line ) )
1346+ {
1347+ size_t lineNumEnd = line.find ( ' ' , 6 );
1348+ Str::ParseInt ( lineNumber, line.substr ( 6 , lineNumEnd - 6 ) );
13751349 }
13761350
13771351 std::string number = std::to_string ( lineNumber );
0 commit comments