Skip to content

Commit

Permalink
Add #insert directive for glsl shaders
Browse files Browse the repository at this point in the history
Adds the ability to insert text from a specified glsl shader file into an arbitrary place in another shader.
  • Loading branch information
VReaperV committed Jun 16, 2024
1 parent 40ed394 commit bba9fbb
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,9 +770,38 @@ std::string GLShaderManager::BuildGPUShaderText( Str::StringRef mainShaderNa
// so we have to reset the line counting.
env += "#line 0\n";

std::string shaderText = env + libs + GetShaderText(filename);
std::string shaderText = env + libs + GetShaderText( filename );

return shaderText;
std::istringstream shaderTextStream( shaderText );
std::string shaderMain;

std::string line;

while ( std::getline( shaderTextStream, line, '\n' ) ) {
const std::string::size_type position = line.find( "#insert" );
if ( position == std::string::npos ) {
shaderMain += line + "\n";
continue;
}

const std::string::iterator beginIt = std::find_if( line.begin(), line.end(),
[]( unsigned char character ) {
return !std::isspace( character );
} );
if ( beginIt - line.begin() != int( position ) ) { // Signed/unsigned CI bullshit
shaderMain += line + "\n";
continue;
}

std::string shaderInsertPath = line.substr( position + 8, std::string::npos );
if ( shaderType == GL_VERTEX_SHADER ) {
shaderMain += GetShaderText( "glsl/" + shaderInsertPath + ".glsl" );
} else {
shaderMain += GetShaderText( "glsl/" + shaderInsertPath + ".glsl" );
}
}

return shaderMain;
}

static bool IsUnusedPermutation( const char *compileMacros )
Expand Down

0 comments on commit bba9fbb

Please sign in to comment.