Skip to content
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

Add #insert directive for glsl shaders #1124

Merged
merged 1 commit into from
Jun 16, 2024
Merged
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
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