Skip to content

Commit 13bf14d

Browse files
change the API of Extra Defines, nuke the GLSL JIT macros
1 parent c02f4c4 commit 13bf14d

File tree

7 files changed

+15
-583
lines changed

7 files changed

+15
-583
lines changed

include/nbl/asset/utils/IShaderCompiler.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,12 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
115115
std::string_view sourceIdentifier = "";
116116
system::logger_opt_ptr logger = nullptr;
117117
const CIncludeFinder* includeFinder = nullptr;
118-
uint32_t maxSelfInclusionCount = 4u;
119-
core::SRange<const char* const> extraDefines = {nullptr, nullptr};
118+
struct SMacroDefinition
119+
{
120+
std::string_view identifier;
121+
std::string_view definition;
122+
};
123+
std::span<const SMacroDefinition> extraDefines = {};
120124
};
121125

122126
// https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#debugging
@@ -290,8 +294,6 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
290294

291295
virtual void insertIntoStart(std::string& code, std::ostringstream&& ins) const = 0;
292296

293-
void insertExtraDefines(std::string& code, const core::SRange<const char* const>& defines) const;
294-
295297
core::smart_refctd_ptr<system::ISystem> m_system;
296298
private:
297299
core::smart_refctd_ptr<CIncludeFinder> m_defaultIncludeFinder;

include/nbl/video/ILogicalDevice.h

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,6 @@ class NBL_API2 ILogicalDevice : public core::IReferenceCounted, public IDeviceMe
477477
// OpenGL: const egl::CEGL::Context*
478478
// Vulkan: const VkDevice*
479479
virtual const void* getNativeHandle() const = 0;
480-
481-
// these are the defines which shall be added to any IGPUShader which has its source as GLSL
482-
inline core::SRange<const char* const> getExtraShaderDefines() const
483-
{
484-
const char* const* begin = m_extraShaderDefines.data();
485-
return {begin,begin+m_extraShaderDefines.size()};
486-
}
487480

488481
protected:
489482
ILogicalDevice(core::smart_refctd_ptr<IAPIConnection>&& api, IPhysicalDevice* physicalDevice, const SCreationParams& params)
@@ -563,37 +556,6 @@ class NBL_API2 ILogicalDevice : public core::IReferenceCounted, public IDeviceMe
563556
) = 0;
564557
virtual core::smart_refctd_ptr<IGPUGraphicsPipeline> createGraphicsPipeline_impl(IGPUPipelineCache* pipelineCache, IGPUGraphicsPipeline::SCreationParams&& params) = 0;
565558
virtual bool createGraphicsPipelines_impl(IGPUPipelineCache* pipelineCache, core::SRange<const IGPUGraphicsPipeline::SCreationParams> params, core::smart_refctd_ptr<IGPUGraphicsPipeline>* output) = 0;
566-
567-
void addCommonShaderDefines(std::ostringstream& pool, const bool runningInRenderDoc);
568-
569-
template<typename... Args>
570-
inline void addShaderDefineToPool(std::ostringstream& pool, const char* define, Args&&... args)
571-
{
572-
const ptrdiff_t pos = pool.tellp();
573-
m_extraShaderDefines.push_back(reinterpret_cast<const char*>(pos));
574-
pool << define << " ";
575-
((pool << (std::is_same<uint8_t, Args>::value ? static_cast<uint32_t>(std::forward<Args>(args)) : std::forward<Args>(args))), ...);
576-
}
577-
inline void finalizeShaderDefinePool(std::ostringstream&& pool)
578-
{
579-
m_ShaderDefineStringPool.resize(static_cast<size_t>(pool.tellp())+m_extraShaderDefines.size());
580-
const auto data = ptrdiff_t(m_ShaderDefineStringPool.data());
581-
582-
const auto str = pool.str();
583-
size_t nullCharsWritten = 0u;
584-
for (auto i=0u; i<m_extraShaderDefines.size(); i++)
585-
{
586-
auto& dst = m_extraShaderDefines[i];
587-
const auto len = (i!=(m_extraShaderDefines.size()-1u) ? ptrdiff_t(m_extraShaderDefines[i+1]):str.length())-ptrdiff_t(dst);
588-
const char* src = str.data()+ptrdiff_t(dst);
589-
dst += data+(nullCharsWritten++);
590-
memcpy(const_cast<char*>(dst),src,len);
591-
const_cast<char*>(dst)[len] = 0;
592-
}
593-
}
594-
595-
core::vector<char> m_ShaderDefineStringPool;
596-
core::vector<const char*> m_extraShaderDefines;
597559

598560
core::smart_refctd_ptr<asset::CCompilerSet> m_compilerSet;
599561
core::smart_refctd_ptr<IAPIConnection> m_api;

src/nbl/asset/utils/CGLSLCompiler.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,12 @@ CGLSLCompiler::CGLSLCompiler(core::smart_refctd_ptr<system::ISystem>&& system)
133133

134134
std::string CGLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions) const
135135
{
136-
if (preprocessOptions.extraDefines.size())
136+
if (!preprocessOptions.extraDefines.empty())
137137
{
138-
insertExtraDefines(code, preprocessOptions.extraDefines);
138+
std::ostringstream insertion;
139+
for (const auto& define : preprocessOptions.extraDefines)
140+
insertion << "#define " << define.identifier << " " << define.definition << "\n";
141+
insertIntoStart(code,std::move(insertion));
139142
}
140143
IShaderCompiler::disableAllDirectivesExceptIncludes(code);
141144
disableGlDirectives(code);
@@ -145,7 +148,7 @@ std::string CGLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADE
145148

146149
if (preprocessOptions.includeFinder != nullptr)
147150
{
148-
options.SetIncluder(std::make_unique<impl::Includer>(preprocessOptions.includeFinder, m_system.get(), preprocessOptions.maxSelfInclusionCount + 1u));//custom #include handler
151+
options.SetIncluder(std::make_unique<impl::Includer>(preprocessOptions.includeFinder, m_system.get(), /*maxSelfInclusionCount*/5));//custom #include handler
149152
}
150153
const shaderc_shader_kind scstage = stage == IShader::ESS_UNKNOWN ? shaderc_glsl_infer_from_source : ESStoShadercEnum(stage);
151154
auto res = comp.PreprocessGlsl(code, scstage, preprocessOptions.sourceIdentifier.data(), options);

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,8 @@ std::string CHLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADE
292292
// now define them as "NBL_GLSL_LIMIT_MAX_IMAGE_DIMENSION_1D=32768"
293293
// to match boost wave syntax
294294
// https://www.boost.org/doc/libs/1_82_0/libs/wave/doc/class_reference_context.html#:~:text=Maintain%20defined%20macros-,add_macro_definition,-bool%20add_macro_definition
295-
for (auto iter = preprocessOptions.extraDefines.begin(); iter!=preprocessOptions.extraDefines.end(); iter++)
296-
{
297-
std::string s = *iter;
298-
size_t firstParenthesis = s.find(')');
299-
if (firstParenthesis == -1) firstParenthesis = 0;
300-
size_t firstWhitespace = s.find(' ', firstParenthesis);
301-
if (firstWhitespace != -1)
302-
s[firstWhitespace] = '=';
303-
context.add_macro_definition(s);
304-
}
295+
for (const auto& define : preprocessOptions.extraDefines)
296+
context.add_macro_definition(define.identifier.data()+core::string("=")+define.definition.data());
305297

306298
// preprocess
307299
std::stringstream stream = std::stringstream();

src/nbl/asset/utils/IShaderCompiler.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,3 @@ std::optional<std::string> IShaderCompiler::CIncludeFinder::tryIncludeGenerators
289289

290290
return std::nullopt;
291291
}
292-
293-
void IShaderCompiler::insertExtraDefines(std::string& code, const core::SRange<const char* const>& defines) const
294-
{
295-
if (defines.empty())
296-
return;
297-
298-
std::ostringstream insertion;
299-
for (auto i = 0u; i < defines.size(); ++i)
300-
{
301-
insertion << "#define " << defines[i] << "\n";
302-
}
303-
insertIntoStart(code, std::move(insertion));
304-
}

src/nbl/video/CVulkanLogicalDevice.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ class CVulkanLogicalDevice final : public ILogicalDevice
6868
(*m_queues)[ix] = new CThreadSafeGPUQueueAdapter(this, new CVulkanQueue(this, rdoc, vkinst, q, famIx, flags, priority));
6969
}
7070
}
71-
72-
std::ostringstream pool;
73-
bool runningInRenderdoc = (rdoc != nullptr);
74-
addCommonShaderDefines(pool,runningInRenderdoc);
75-
finalizeShaderDefinePool(std::move(pool));
7671

7772
m_dummyDSLayout = createDescriptorSetLayout(nullptr, nullptr);
7873
}
@@ -703,7 +698,7 @@ class CVulkanLogicalDevice final : public ILogicalDevice
703698
commonCompileOptions.preprocessorOptions.logger = (m_physicalDevice->getDebugCallback()) ? m_physicalDevice->getDebugCallback()->getLogger() : nullptr;
704699
commonCompileOptions.preprocessorOptions.includeFinder = compiler->getDefaultIncludeFinder(); // to resolve includes before compilation
705700
commonCompileOptions.preprocessorOptions.sourceIdentifier = cpushader->getFilepathHint().c_str();
706-
commonCompileOptions.preprocessorOptions.extraDefines = getExtraShaderDefines();
701+
commonCompileOptions.preprocessorOptions.extraDefines = {};
707702

708703
commonCompileOptions.stage = shaderStage;
709704
commonCompileOptions.debugInfoFlags =

0 commit comments

Comments
 (0)