Skip to content

Commit b9432c1

Browse files
committed
Isolate code into CPP
1 parent 2725358 commit b9432c1

File tree

2 files changed

+45
-55
lines changed

2 files changed

+45
-55
lines changed

include/nbl/asset/utils/CHLSLCompiler.h

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
#include "nbl/asset/utils/ISPIRVOptimizer.h"
99
#include "nbl/asset/utils/IShaderCompiler.h"
1010

11-
#include <combaseapi.h>
12-
#include <dxc/dxc/include/dxc/dxcapi.h>
11+
#include <wrl.h>
12+
13+
using Microsoft::WRL::ComPtr;
14+
15+
class IDxcUtils;
16+
class IDxcCompiler3;
1317

1418
namespace nbl::asset
1519
{
@@ -20,7 +24,6 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
2024
IShader::E_CONTENT_TYPE getCodeContentType() const override { return IShader::E_CONTENT_TYPE::ECT_HLSL; };
2125

2226
CHLSLCompiler(core::smart_refctd_ptr<system::ISystem>&& system);
23-
~CHLSLCompiler();
2427

2528
struct SOptions : IShaderCompiler::SCompilerOptions
2629
{
@@ -44,13 +47,15 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
4447

4548
std::string preprocessShader(std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions) const override;
4649

47-
protected:
48-
4950
void insertIntoStart(std::string& code, std::ostringstream&& ins) const override;
5051

52+
IDxcUtils* getDxcUtils() const { return m_dxcUtils.Get(); }
53+
IDxcCompiler3* getDxcCompiler() const { return m_dxcCompiler.Get(); }
54+
protected:
55+
5156
// TODO do we want to use ComPtr?
52-
std::unique_ptr<IDxcUtils> m_dxcUtils;
53-
std::unique_ptr<IDxcCompiler3> m_dxcCompiler;
57+
ComPtr<IDxcUtils> m_dxcUtils;
58+
ComPtr<IDxcCompiler3> m_dxcCompiler;
5459

5560
static CHLSLCompiler::SOptions option_cast(const IShaderCompiler::SCompilerOptions& options)
5661
{
@@ -61,29 +66,6 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
6166
ret.setCommonData(options);
6267
return ret;
6368
}
64-
65-
class DxcCompilationResult
66-
{
67-
public:
68-
IDxcBlobEncoding* errorMessages;
69-
IDxcBlob* objectBlob;
70-
IDxcResult* compileResult;
71-
72-
char* GetErrorMessagesString()
73-
{
74-
return reinterpret_cast<char*>(errorMessages->GetBufferPointer());
75-
}
76-
77-
// TODO figure out why this is crashing when done as part of the destructor
78-
void release()
79-
{
80-
errorMessages->Release();
81-
objectBlob->Release();
82-
compileResult->Release();
83-
}
84-
};
85-
86-
CHLSLCompiler::DxcCompilationResult dxcCompile(std::string& source, LPCWSTR* args, uint32_t argCount, const SOptions& options) const;
8769
};
8870

8971
}

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,22 @@
1818
using namespace nbl;
1919
using namespace nbl::asset;
2020

21+
#include <combaseapi.h>
22+
#include <dxc/dxc/include/dxc/dxcapi.h>
2123

2224
CHLSLCompiler::CHLSLCompiler(core::smart_refctd_ptr<system::ISystem>&& system)
2325
: IShaderCompiler(std::move(system))
2426
{
25-
IDxcUtils* utils;
26-
auto res = DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS(&utils));
27+
ComPtr<IDxcUtils> utils;
28+
auto res = DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS(utils.GetAddressOf()));
2729
assert(SUCCEEDED(res));
2830

29-
IDxcCompiler3* compiler;
30-
res = DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&compiler));
31+
ComPtr<IDxcCompiler3> compiler;
32+
res = DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(compiler.GetAddressOf()));
3133
assert(SUCCEEDED(res));
3234

33-
m_dxcUtils = std::unique_ptr<IDxcUtils>(utils);
34-
m_dxcCompiler = std::unique_ptr<IDxcCompiler3>(compiler);
35-
}
36-
37-
CHLSLCompiler::~CHLSLCompiler()
38-
{
39-
m_dxcUtils->Release();
40-
m_dxcCompiler->Release();
35+
m_dxcUtils = utils;
36+
m_dxcCompiler = compiler;
4137
}
4238

4339
static tcpp::IInputStream* getInputStreamInclude(
@@ -78,7 +74,20 @@ static tcpp::IInputStream* getInputStreamInclude(
7874
return new tcpp::StringInputStream(std::move(res_str));
7975
}
8076

81-
CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& source, LPCWSTR* args, uint32_t argCount, const SOptions& options) const
77+
class DxcCompilationResult
78+
{
79+
public:
80+
ComPtr<IDxcBlobEncoding> errorMessages;
81+
ComPtr<IDxcBlob> objectBlob;
82+
ComPtr<IDxcResult> compileResult;
83+
84+
char* GetErrorMessagesString()
85+
{
86+
return reinterpret_cast<char*>(errorMessages->GetBufferPointer());
87+
}
88+
};
89+
90+
DxcCompilationResult dxcCompile(const CHLSLCompiler* compiler, std::string& source, LPCWSTR* args, uint32_t argCount, const CHLSLCompiler::SOptions& options)
8291
{
8392
if (options.genDebugInfo)
8493
{
@@ -93,29 +102,29 @@ CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& sourc
93102
}
94103

95104
insertion << "\n";
96-
insertIntoStart(source, std::move(insertion));
105+
compiler->insertIntoStart(source, std::move(insertion));
97106
}
98107

99-
IDxcBlobEncoding* src;
100-
auto res = m_dxcUtils->CreateBlob(reinterpret_cast<const void*>(source.data()), source.size(), CP_UTF8, &src);
108+
ComPtr<IDxcBlobEncoding> src;
109+
auto res = compiler->getDxcUtils()->CreateBlob(reinterpret_cast<const void*>(source.data()), source.size(), CP_UTF8, &src);
101110
assert(SUCCEEDED(res));
102111

103112
DxcBuffer sourceBuffer;
104113
sourceBuffer.Ptr = src->GetBufferPointer();
105114
sourceBuffer.Size = src->GetBufferSize();
106115
sourceBuffer.Encoding = 0;
107116

108-
IDxcResult* compileResult;
109-
res = m_dxcCompiler->Compile(&sourceBuffer, args, argCount, nullptr, IID_PPV_ARGS(&compileResult));
117+
ComPtr<IDxcResult> compileResult;
118+
res = compiler->getDxcCompiler()->Compile(&sourceBuffer, args, argCount, nullptr, IID_PPV_ARGS(compileResult.GetAddressOf()));
110119
// If the compilation failed, this should still be a successful result
111120
assert(SUCCEEDED(res));
112121

113122
HRESULT compilationStatus = 0;
114123
res = compileResult->GetStatus(&compilationStatus);
115124
assert(SUCCEEDED(res));
116125

117-
IDxcBlobEncoding* errorBuffer;
118-
res = compileResult->GetErrorBuffer(&errorBuffer);
126+
ComPtr<IDxcBlobEncoding> errorBuffer;
127+
res = compileResult->GetErrorBuffer(errorBuffer.GetAddressOf());
119128
assert(SUCCEEDED(res));
120129

121130
DxcCompilationResult result;
@@ -129,15 +138,16 @@ CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& sourc
129138
return result;
130139
}
131140

132-
IDxcBlob* resultingBlob;
133-
res = compileResult->GetResult(&resultingBlob);
141+
ComPtr<IDxcBlob> resultingBlob;
142+
res = compileResult->GetResult(resultingBlob.GetAddressOf());
134143
assert(SUCCEEDED(res));
135144

136145
result.objectBlob = resultingBlob;
137146

138147
return result;
139148
}
140149

150+
141151
std::string CHLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions) const
142152
{
143153
if (preprocessOptions.extraDefines.size())
@@ -276,7 +286,7 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
276286
const uint32_t nonDebugArgs = 5;
277287
const uint32_t allArgs = nonDebugArgs + 2;
278288

279-
auto compileResult = dxcCompile(newCode, &arguments[0], hlslOptions.genDebugInfo ? allArgs : nonDebugArgs, hlslOptions);
289+
auto compileResult = dxcCompile(this, newCode, &arguments[0], hlslOptions.genDebugInfo ? allArgs : nonDebugArgs, hlslOptions);
280290

281291
if (!compileResult.objectBlob)
282292
{
@@ -286,8 +296,6 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
286296
auto outSpirv = core::make_smart_refctd_ptr<ICPUBuffer>(compileResult.objectBlob->GetBufferSize());
287297
memcpy(outSpirv->getPointer(), compileResult.objectBlob->GetBufferPointer(), compileResult.objectBlob->GetBufferSize());
288298

289-
compileResult.release();
290-
291299
return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move(outSpirv), stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, hlslOptions.preprocessorOptions.sourceIdentifier.data());
292300
}
293301

0 commit comments

Comments
 (0)