18
18
using namespace nbl ;
19
19
using namespace nbl ::asset;
20
20
21
+ #include < combaseapi.h>
22
+ #include < dxc/dxc/include/dxc/dxcapi.h>
21
23
22
24
CHLSLCompiler::CHLSLCompiler (core::smart_refctd_ptr<system::ISystem>&& system)
23
25
: IShaderCompiler(std::move(system))
24
26
{
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 () ));
27
29
assert (SUCCEEDED (res));
28
30
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 () ));
31
33
assert (SUCCEEDED (res));
32
34
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;
41
37
}
42
38
43
39
static tcpp::IInputStream* getInputStreamInclude (
@@ -78,7 +74,20 @@ static tcpp::IInputStream* getInputStreamInclude(
78
74
return new tcpp::StringInputStream (std::move (res_str));
79
75
}
80
76
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)
82
91
{
83
92
if (options.genDebugInfo )
84
93
{
@@ -93,29 +102,29 @@ CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& sourc
93
102
}
94
103
95
104
insertion << " \n " ;
96
- insertIntoStart (source, std::move (insertion));
105
+ compiler-> insertIntoStart (source, std::move (insertion));
97
106
}
98
107
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);
101
110
assert (SUCCEEDED (res));
102
111
103
112
DxcBuffer sourceBuffer;
104
113
sourceBuffer.Ptr = src->GetBufferPointer ();
105
114
sourceBuffer.Size = src->GetBufferSize ();
106
115
sourceBuffer.Encoding = 0 ;
107
116
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 () ));
110
119
// If the compilation failed, this should still be a successful result
111
120
assert (SUCCEEDED (res));
112
121
113
122
HRESULT compilationStatus = 0 ;
114
123
res = compileResult->GetStatus (&compilationStatus);
115
124
assert (SUCCEEDED (res));
116
125
117
- IDxcBlobEncoding* errorBuffer;
118
- res = compileResult->GetErrorBuffer (& errorBuffer);
126
+ ComPtr< IDxcBlobEncoding> errorBuffer;
127
+ res = compileResult->GetErrorBuffer (errorBuffer. GetAddressOf () );
119
128
assert (SUCCEEDED (res));
120
129
121
130
DxcCompilationResult result;
@@ -129,15 +138,16 @@ CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& sourc
129
138
return result;
130
139
}
131
140
132
- IDxcBlob* resultingBlob;
133
- res = compileResult->GetResult (& resultingBlob);
141
+ ComPtr< IDxcBlob> resultingBlob;
142
+ res = compileResult->GetResult (resultingBlob. GetAddressOf () );
134
143
assert (SUCCEEDED (res));
135
144
136
145
result.objectBlob = resultingBlob;
137
146
138
147
return result;
139
148
}
140
149
150
+
141
151
std::string CHLSLCompiler::preprocessShader (std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions) const
142
152
{
143
153
if (preprocessOptions.extraDefines .size ())
@@ -276,7 +286,7 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
276
286
const uint32_t nonDebugArgs = 5 ;
277
287
const uint32_t allArgs = nonDebugArgs + 2 ;
278
288
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);
280
290
281
291
if (!compileResult.objectBlob )
282
292
{
@@ -286,8 +296,6 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
286
296
auto outSpirv = core::make_smart_refctd_ptr<ICPUBuffer>(compileResult.objectBlob ->GetBufferSize ());
287
297
memcpy (outSpirv->getPointer (), compileResult.objectBlob ->GetBufferPointer (), compileResult.objectBlob ->GetBufferSize ());
288
298
289
- compileResult.release ();
290
-
291
299
return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move (outSpirv), stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, hlslOptions.preprocessorOptions .sourceIdentifier .data ());
292
300
}
293
301
0 commit comments