@@ -42,47 +42,56 @@ class IShader : public virtual core::IReferenceCounted
42
42
if (!_exts)
43
43
return ;
44
44
45
- auto findLineJustAfterVersionOrPragmaShaderStageDirective = [&_glsl]
46
- {
47
- size_t hashPos = _glsl.find_first_of (' #' );
48
- if (hashPos >= _glsl.length ())
49
- return _glsl.npos ;
50
- if (_glsl.compare (hashPos, 8 , " #version" ))
51
- return _glsl.npos ;
52
-
53
- size_t searchPos = hashPos + 8ull ;
54
-
55
- size_t hashPos2 = _glsl.find_first_of (' #' , hashPos+8ull );
56
- if (hashPos2<_glsl.length ())
57
- {
58
- char pragma_stage_str[] = " #pragma shader_stage" ;
59
- if (_glsl.compare (hashPos2, sizeof (pragma_stage_str)-1ull , pragma_stage_str) == 0 )
60
- searchPos = hashPos2 + sizeof (pragma_stage_str) - 1ull ;
61
- }
62
- size_t nlPos = _glsl.find_first_of (' \n ' , searchPos);
63
-
64
- return (nlPos >= _glsl.length ()) ? _glsl.npos : nlPos+1ull ;
65
- };
66
-
67
- const size_t pos = findLineJustAfterVersionOrPragmaShaderStageDirective ();
68
- if (pos == _glsl.npos )
69
- return ;
70
-
71
- const size_t ln = std::count (_glsl.begin (),_glsl.begin ()+pos, ' \n ' )+1 ;// +1 to count from 1
72
-
73
- std::string insertion = " \n " ;
74
- for (const std::string& ext : (*_exts))
75
- {
76
- std::string str = " #ifndef " + ext + " \n " ;
77
- str += " \t #define IRR_" + ext + " \n " ;
78
- str += " #endif //" + ext + " \n " ;
45
+ const std::string insertion = genGLSLExtensionDefines (_exts);
79
46
80
- insertion += str;
81
- }
82
- insertion += " #line " + std::to_string (ln) + " \n " ;
47
+ insertAfterVersionAndPragmaShaderStage (_glsl, insertion);
48
+ }
83
49
84
- _glsl.insert (pos, insertion);
50
+ protected:
51
+ static inline std::string genGLSLExtensionDefines (const core::refctd_dynamic_array<std::string>* _exts)
52
+ {
53
+ std::string insertion = " \n " ;
54
+ for (const std::string& ext : (*_exts))
55
+ {
56
+ std::string str = " #ifndef " + ext + " \n " ;
57
+ str += " \t #define NBL_" + ext + " \n " ;
58
+ str += " #endif //" + ext + " \n " ;
59
+
60
+ insertion += str;
85
61
}
62
+ }
63
+ static inline void insertAfterVersionAndPragmaShaderStage (std::string& _glsl, const std::string& _ins)
64
+ {
65
+ auto findLineJustAfterVersionOrPragmaShaderStageDirective = [&_glsl]
66
+ {
67
+ size_t hashPos = _glsl.find_first_of (' #' );
68
+ if (hashPos >= _glsl.length ())
69
+ return _glsl.npos ;
70
+ if (_glsl.compare (hashPos, 8 , " #version" ))
71
+ return _glsl.npos ;
72
+
73
+ size_t searchPos = hashPos + 8ull ;
74
+
75
+ size_t hashPos2 = _glsl.find_first_of (' #' , hashPos + 8ull );
76
+ if (hashPos2 < _glsl.length ())
77
+ {
78
+ char pragma_stage_str[] = " #pragma shader_stage" ;
79
+ if (_glsl.compare (hashPos2, sizeof (pragma_stage_str) - 1ull , pragma_stage_str) == 0 )
80
+ searchPos = hashPos2 + sizeof (pragma_stage_str) - 1ull ;
81
+ }
82
+ size_t nlPos = _glsl.find_first_of (' \n ' , searchPos);
83
+
84
+ return (nlPos >= _glsl.length ()) ? _glsl.npos : nlPos + 1ull ;
85
+ };
86
+
87
+ const size_t pos = findLineJustAfterVersionOrPragmaShaderStageDirective ();
88
+ if (pos == _glsl.npos )
89
+ return ;
90
+
91
+ const size_t ln = std::count (_glsl.begin (), _glsl.begin () + pos, ' \n ' ) + 1 ;// +1 to count from 1
92
+
93
+ _glsl.insert (pos, _ins + " #line " + std::to_string (ln) + " \n " );
94
+ }
86
95
};
87
96
88
97
}
0 commit comments