1313
1414namespace Pathfinder {
1515
16- bool replace (std::string &str, const std::string &from, const std::string &to) {
16+ bool replaceFirst (std::string &str, const std::string &from, const std::string &to) {
1717 size_t start_pos = str.find (from);
1818 if (start_pos == std::string::npos) {
1919 return false ;
@@ -22,6 +22,15 @@ bool replace(std::string &str, const std::string &from, const std::string &to) {
2222 return true ;
2323}
2424
25+ void replaceAll (std::string &str, const std::string &from, const std::string &to) {
26+ if (from.empty ()) return ;
27+ size_t start_pos = 0 ;
28+ while ((start_pos = str.find (from, start_pos)) != std::string::npos) {
29+ str.replace (start_pos, from.length (), to);
30+ start_pos += to.length ();
31+ }
32+ }
33+
2534ShaderModuleGl::ShaderModuleGl (const std::vector<char > &source_code,
2635 ShaderStage shader_stage,
2736 const std::string &label) {
@@ -48,9 +57,22 @@ ShaderModuleGl::ShaderModuleGl(const std::vector<char> &source_code,
4857
4958#ifdef PATHFINDER_MINIMUM_SHADER_VERSION_SUPPORT
5059 if (shader_stage == ShaderStage::Compute) {
51- replace (code_string, " #version 430" , " #version 310 es" );
60+ replaceFirst (code_string, " #version 430" , " #version 310 es" );
5261 } else {
53- replace (code_string, " #version 310 es" , " #version 300 es" );
62+ replaceFirst (code_string, " #version 310 es" , " #version 300 es" );
63+ }
64+
65+ if (shader_stage == ShaderStage::Vertex) {
66+ // 1. Find where "#version" is
67+ size_t ver_pos = code_string.find (" #version" );
68+ if (ver_pos != std::string::npos) {
69+ // 2. Find the following line break
70+ size_t nl_pos = code_string.find (' \n ' , ver_pos);
71+ if (nl_pos != std::string::npos) {
72+ // 3. Add a new line
73+ code_string.insert (nl_pos + 1 , " #define gl_VertexIndex gl_VertexID\n " );
74+ }
75+ }
5476 }
5577#endif
5678
0 commit comments