@@ -55,16 +55,15 @@ class NBL_API IShaderCompiler : public core::IReferenceCounted
55
55
Creates a formatted copy of the original
56
56
57
57
@param original An original High Level shader (must contain high level language code and must not be a nullptr).
58
+ @param position if original != nullptr, is the position in the code to insert the formatted string to the original shader code
58
59
@param fmt A string with c-like format, which will be filled with data from ...args
59
60
@param ...args Data to fill fmt with
60
61
@returns shader containing fmt filled with ...args, placed before the original code.
61
62
62
- If original == nullptr, the output buffer will only contain the data from fmt. If original code contains #version specifier,
63
- then the filled fmt will be placed onto the next line after #version in the output buffer. If not, fmt will be placed into the
64
- beginning of the output buffer.
63
+ If original == nullptr, the output buffer will only contain the data from fmt.
65
64
*/
66
65
template <typename ... Args>
67
- static core::smart_refctd_ptr<ICPUShader> createOverridenCopy (const ICPUShader* original, const char * fmt, Args... args)
66
+ static core::smart_refctd_ptr<ICPUShader> createOverridenCopy (const ICPUShader* original, uint32_t position, const char * fmt, Args... args)
68
67
{
69
68
assert (original == nullptr || (!original->isADummyObjectForCache () && original->isContentHighLevelLanguage ()));
70
69
@@ -95,30 +94,34 @@ class NBL_API IShaderCompiler : public core::IReferenceCounted
95
94
96
95
nbl::core::smart_refctd_ptr<ICPUBuffer> outBuffer = nbl::core::make_smart_refctd_ptr<ICPUBuffer>(outSize);
97
96
98
- size_t versionDirectiveLength = 0 ;
99
-
100
- std::string_view origCode;
97
+ auto origCode = std::string_view (reinterpret_cast <const char *>(original->getContent ()->getPointer ()), origLen);
101
98
auto outCode = reinterpret_cast <char *>(outBuffer->getPointer ());
102
- if (original!=nullptr )
99
+
100
+ if (position < origLen)
103
101
{
104
- origCode = std::string_view (reinterpret_cast <const char *>(original->getContent ()->getPointer ()),origLen);
105
- auto start = origCode.find (" #version" );
106
- auto end = origCode.find (" \n " ,start);
107
- if (end!=std::string_view::npos)
108
- versionDirectiveLength = end+1u ;
109
- }
102
+ // Copy whatever comes before position
103
+ std::copy_n (origCode.data (), position, outCode);
104
+ outCode += position;
110
105
111
- std::copy_n (origCode. data (),versionDirectiveLength,outCode);
112
- outCode += versionDirectiveLength ;
106
+ // Copy formatted string
107
+ outCode += sprintf (outCode,fmt,std::forward<Args>(args)...) ;
113
108
114
- outCode += sprintf (outCode,fmt,std::forward<Args>(args)...);
109
+ // Copy the rest of the original code
110
+ auto epilogueLen = origLen - position;
111
+ std::copy_n (origCode.data () + position, epilogueLen, outCode);
112
+ outCode += epilogueLen;
115
113
116
- auto epilogueLen = origLen-versionDirectiveLength;
117
- std::copy_n (origCode.data ()+versionDirectiveLength,epilogueLen,outCode);
118
- outCode += epilogueLen;
119
- *outCode = 0 ; // terminating char
114
+ // terminating char
115
+ *outCode = 0 ;
120
116
121
- return nbl::core::make_smart_refctd_ptr<ICPUShader>(std::move (outBuffer), original->getStage (), original->getContentType (), std::string (original->getFilepathHint ()));
117
+ return nbl::core::make_smart_refctd_ptr<ICPUShader>(std::move (outBuffer), original->getStage (), original->getContentType (), std::string (original->getFilepathHint ()));
118
+ }
119
+ else
120
+ {
121
+ // Position isn't valid.
122
+ assert (false );
123
+ return nullptr ;
124
+ }
122
125
}
123
126
124
127
virtual IShader::E_CONTENT_TYPE getCodeContentType () const = 0;
0 commit comments