Skip to content

Commit cd87254

Browse files
committed
Implement inserting of commandline args as a debug comment
1 parent ec5e143 commit cd87254

File tree

5 files changed

+47
-48
lines changed

5 files changed

+47
-48
lines changed

include/nbl/asset/utils/CGLSLCompiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class NBL_API2 CGLSLCompiler final : public IShaderCompiler
127127

128128
protected:
129129

130-
void insertExtraDefines(std::string& code, const core::SRange<const char* const>& defines) const override;
130+
virtual void insertIntoStart(std::string& code, std::ostringstream&& ins) const override;
131131

132132
static CGLSLCompiler::SOptions option_cast(const IShaderCompiler::SCompilerOptions& options)
133133
{

include/nbl/asset/utils/CHLSLCompiler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
4444

4545
protected:
4646

47-
void insertExtraDefines(std::string& str, const core::SRange<const char* const>& defines) const override;
47+
virtual void insertIntoStart(std::string& code, std::ostringstream&& ins) const override;
4848

4949
// TODO do we want to use ComPtr?
5050
std::unique_ptr<IDxcUtils> m_dxcUtils;
@@ -73,7 +73,7 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
7373
}
7474
};
7575

76-
DxcCompilationResult dxcCompile(asset::ICPUShader* source, LPCWSTR* args, uint32_t argCount, const SOptions& options) const;
76+
DxcCompilationResult dxcCompile(const std::string& source, LPCWSTR* args, uint32_t argCount, const SOptions& options) const;
7777
};
7878

7979
}

include/nbl/asset/utils/IShaderCompiler.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,20 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
263263

264264
protected:
265265

266-
virtual void insertExtraDefines(std::string& code, const core::SRange<const char* const>& defines) const = 0;
266+
virtual void insertIntoStart(std::string& code, std::ostringstream&& ins) const = 0;
267+
268+
void insertExtraDefines(std::string& code, const core::SRange<const char* const>& defines) const
269+
{
270+
if (defines.empty())
271+
return;
272+
273+
std::ostringstream insertion;
274+
for (auto def : defines)
275+
{
276+
insertion << "#define " << def << "\n";
277+
}
278+
insertIntoStart(code, std::move(insertion));
279+
}
267280

268281
private:
269282
core::smart_refctd_ptr<system::ISystem> m_system;

src/nbl/asset/utils/CGLSLCompiler.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ core::smart_refctd_ptr<ICPUShader> CGLSLCompiler::compileToSPIRV(const char* cod
6161
}
6262
}
6363

64-
static void insertAfterVersionAndPragmaShaderStage(std::string& code, std::ostringstream&& ins)
64+
void CGLSLCompiler::insertIntoStart(std::string& code, std::ostringstream&& ins) const
6565
{
6666
auto findLineJustAfterVersionOrPragmaShaderStageDirective = [&code]() -> size_t
6767
{
@@ -94,16 +94,3 @@ static void insertAfterVersionAndPragmaShaderStage(std::string& code, std::ostri
9494
ins << "#line " << std::to_string(ln) << "\n";
9595
code.insert(pos, ins.str());
9696
}
97-
98-
void CGLSLCompiler::insertExtraDefines(std::string& str, const core::SRange<const char* const>& defines) const
99-
{
100-
if (defines.empty())
101-
return;
102-
103-
std::ostringstream insertion;
104-
for (auto def : defines)
105-
{
106-
insertion << "#define " << def << "\n";
107-
}
108-
insertAfterVersionAndPragmaShaderStage(str, std::move(insertion));
109-
}

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ CHLSLCompiler::~CHLSLCompiler()
3636
m_dxcCompiler->Release();
3737
}
3838

39-
CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(asset::ICPUShader* source, LPCWSTR* args, uint32_t argCount, const SOptions& options) const
39+
CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(const std::string& source, LPCWSTR* args, uint32_t argCount, const SOptions& options) const
4040
{
4141
DxcBuffer sourceBuffer;
42-
sourceBuffer.Ptr = source->getContent()->getPointer();
43-
sourceBuffer.Size = source->getContent()->getSize();
42+
sourceBuffer.Ptr = source.data();
43+
sourceBuffer.Size = source.size();
4444
sourceBuffer.Encoding = 0;
4545

4646
IDxcResult* compileResult;
@@ -63,7 +63,7 @@ CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(asset::ICPUShader*
6363

6464
if (!SUCCEEDED(compilationStatus))
6565
{
66-
options.logger.log(result.GetErrorMessagesString(), system::ILogger::ELL_ERROR);
66+
options.preprocessorOptions.logger.log(result.GetErrorMessagesString(), system::ILogger::ELL_ERROR);
6767
return result;
6868
}
6969

@@ -82,33 +82,40 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
8282

8383
if (!code)
8484
{
85-
options.preprocessorOptions.logger.log("code is nullptr", system::ILogger::ELL_ERROR);
85+
hlslOptions.preprocessorOptions.logger.log("code is nullptr", system::ILogger::ELL_ERROR);
8686
return nullptr;
8787
}
8888

89-
core::smart_refctd_ptr<asset::ICPUShader> includesResolved;
90-
if (hlslOptions.includeFinder != nullptr)
91-
{
92-
includesResolved = resolveIncludeDirectives(code, hlslOptions.stage, hlslOptions.sourceIdentifier.data(), hlslOptions.maxSelfInclusionCount, hlslOptions.logger);
93-
if (includesResolved)
94-
{
95-
code = reinterpret_cast<const char*>(includesResolved->getContent()->getPointer());
96-
}
97-
}
89+
auto newCode = preprocessShader(code, hlslOptions.stage, hlslOptions.preprocessorOptions);
9890

9991
LPCWSTR arguments[] = {
10092
// These will always be present
10193
L"-spirv",
10294
L"-HLSL2021",
10395

10496
// These are debug only
105-
L"-DPARTI",
106-
L"-DBOY"
97+
L"-Qembed_debug"
10798
};
99+
if (hlslOptions.genDebugInfo)
100+
{
101+
std::ostringstream insertion;
102+
insertion << "// commandline compiler options : ";
103+
104+
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> conv;
105+
for (auto arg : arguments)
106+
{
107+
auto str = conv.to_bytes(arg);
108+
insertion << str.c_str() << " ";
109+
}
110+
111+
insertion << "\n";
112+
insertIntoStart(newCode, std::move(insertion));
113+
}
114+
108115
const uint32_t nonDebugArgs = 2;
109-
const uint32_t allArgs = nonDebugArgs + 2;
116+
const uint32_t allArgs = nonDebugArgs + 0;
110117

111-
DxcCompilationResult compileResult = dxcCompile(includesResolved.get(), &arguments[0], hlslOptions.genDebugInfo ? allArgs : nonDebugArgs, hlslOptions);
118+
DxcCompilationResult compileResult = dxcCompile(newCode, &arguments[0], hlslOptions.genDebugInfo ? allArgs : nonDebugArgs, hlslOptions);
112119

113120
if (!compileResult.objectBlob)
114121
{
@@ -118,18 +125,10 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
118125
auto outSpirv = core::make_smart_refctd_ptr<ICPUBuffer>(compileResult.objectBlob->GetBufferSize());
119126
memcpy(outSpirv->getPointer(), compileResult.objectBlob->GetBufferPointer(), compileResult.objectBlob->GetBufferSize());
120127

121-
return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move(outSpirv), hlslOptions.stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, hlslOptions.sourceIdentifier.data());
128+
return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move(outSpirv), hlslOptions.stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, hlslOptions.preprocessorOptions.sourceIdentifier.data());
122129
}
123130

124-
void CHLSLCompiler::insertExtraDefines(std::string& code, const core::SRange<const char* const>& defines) const
131+
void CHLSLCompiler::insertIntoStart(std::string& code, std::ostringstream&& ins) const
125132
{
126-
if (defines.empty())
127-
return;
128-
129-
std::ostringstream insertion;
130-
for (auto def : defines)
131-
{
132-
insertion << "#define " << def << "\n";
133-
}
134-
code.insert(0u, insertion.str());
135-
}
133+
code.insert(0u, ins.str());
134+
}

0 commit comments

Comments
 (0)