Skip to content

Commit 2ba7ce7

Browse files
committed
createOverridenCopy add position parameter and a new function for GLSLCompiler
1 parent 0828baa commit 2ba7ce7

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

include/nbl/asset/utils/IGLSLCompiler.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ class NBL_API IGLSLCompiler final : public IShaderCompiler
8484
system::logger_opt_ptr logger = nullptr,
8585
const E_SPIRV_VERSION targetSpirvVersion = ESV_1_6) const;
8686

87+
/*
88+
If original code contains #version specifier,
89+
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
90+
beginning of the output buffer.
91+
*/
92+
template<typename... Args>
93+
static core::smart_refctd_ptr<ICPUShader> createOverridenCopy(const ICPUShader* original, const char* fmt, Args... args)
94+
{
95+
uint32_t position = 0u;
96+
if (original != nullptr)
97+
{
98+
auto origCodeBuffer = original->getContent();
99+
auto origCode = std::string_view(reinterpret_cast<const char*>(origCodeBuffer->getPointer()), origCodeBuffer->getSize());
100+
auto start = origCode.find("#version");
101+
auto end = origCode.find("\n", start);
102+
if (end != std::string_view::npos)
103+
position = end + 1u;
104+
}
105+
106+
return IShaderCompiler::createOverridenCopy(original, position, fmt, args...);
107+
}
108+
87109
static inline const char* getStorageImageFormatQualifier(const asset::E_FORMAT format)
88110
{
89111
switch (format)

include/nbl/asset/utils/IShaderCompiler.h

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,15 @@ class NBL_API IShaderCompiler : public core::IReferenceCounted
5555
Creates a formatted copy of the original
5656
5757
@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
5859
@param fmt A string with c-like format, which will be filled with data from ...args
5960
@param ...args Data to fill fmt with
6061
@returns shader containing fmt filled with ...args, placed before the original code.
6162
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.
6564
*/
6665
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)
6867
{
6968
assert(original == nullptr || (!original->isADummyObjectForCache() && original->isContentHighLevelLanguage()));
7069

@@ -95,30 +94,34 @@ class NBL_API IShaderCompiler : public core::IReferenceCounted
9594

9695
nbl::core::smart_refctd_ptr<ICPUBuffer> outBuffer = nbl::core::make_smart_refctd_ptr<ICPUBuffer>(outSize);
9796

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);
10198
auto outCode = reinterpret_cast<char*>(outBuffer->getPointer());
102-
if (original!=nullptr)
99+
100+
if (position < origLen)
103101
{
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;
110105

111-
std::copy_n(origCode.data(),versionDirectiveLength,outCode);
112-
outCode += versionDirectiveLength;
106+
// Copy formatted string
107+
outCode += sprintf(outCode,fmt,std::forward<Args>(args)...);
113108

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;
115113

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;
120116

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+
}
122125
}
123126

124127
virtual IShader::E_CONTENT_TYPE getCodeContentType() const = 0;

src/nbl/asset/interchange/CGLTFLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ using namespace nbl::asset;
5151

5252
auto unspecializedShader = core::make_smart_refctd_ptr<asset::ICPUShader>(std::move(glsl), stage, asset::ICPUShader::E_CONTENT_TYPE::ECT_GLSL, stage != ICPUShader::ESS_VERTEX ? "?IrrlichtBAW glTFLoader FragmentShader?" : "?IrrlichtBAW glTFLoader VertexShader?");
5353
if (extraDefine)
54-
unspecializedShader = IShaderCompiler::createOverridenCopy(unspecializedShader.get(),"%s",extraDefine);
54+
unspecializedShader = IGLSLCompiler::createOverridenCopy(unspecializedShader.get(),"%s",extraDefine);
5555

5656
ICPUSpecializedShader::SInfo specInfo({},nullptr,"main");
5757
auto shader = core::make_smart_refctd_ptr<asset::ICPUSpecializedShader>(std::move(unspecializedShader),std::move(specInfo));

0 commit comments

Comments
 (0)