44#include " nbl/asset/utils/CHLSLCompiler.h"
55#include " nbl/asset/utils/shadercUtils.h"
66
7+ #include < wrl.h>
8+ #include < combaseapi.h>
9+
710#include < dxc/dxcapi.h>
811
912#include < sstream>
@@ -18,6 +21,14 @@ using namespace nbl;
1821using namespace nbl ::asset;
1922using Microsoft::WRL::ComPtr;
2023
24+ namespace nbl ::asset::hlsl::impl
25+ {
26+ struct DXC {
27+ Microsoft::WRL::ComPtr<IDxcUtils> m_dxcUtils;
28+ Microsoft::WRL::ComPtr<IDxcCompiler3> m_dxcCompiler;
29+ };
30+ }
31+
2132CHLSLCompiler::CHLSLCompiler (core::smart_refctd_ptr<system::ISystem>&& system)
2233 : IShaderCompiler(std::move(system))
2334{
@@ -29,8 +40,10 @@ CHLSLCompiler::CHLSLCompiler(core::smart_refctd_ptr<system::ISystem>&& system)
2940 res = DxcCreateInstance (CLSID_DxcCompiler, IID_PPV_ARGS (compiler.GetAddressOf ()));
3041 assert (SUCCEEDED (res));
3142
32- m_dxcUtils = utils;
33- m_dxcCompiler = compiler;
43+ m_dxcCompilerTypes = std::unique_ptr<nbl::asset::hlsl::impl::DXC>(new nbl::asset::hlsl::impl::DXC{
44+ utils,
45+ compiler
46+ });
3447}
3548
3649static tcpp::IInputStream* getInputStreamInclude (
@@ -88,9 +101,8 @@ class DxcCompilationResult
88101 }
89102};
90103
91- DxcCompilationResult CHLSLCompiler::dxcCompile ( std::string& source, LPCWSTR* args, uint32_t argCount, const CHLSLCompiler::SOptions& options) const
104+ DxcCompilationResult dxcCompile ( const CHLSLCompiler* compiler, nbl::asset::hlsl::impl::DXC* dxc, std::string& source, LPCWSTR* args, uint32_t argCount, const CHLSLCompiler::SOptions& options)
92105{
93- auto compiler = this ;
94106 if (options.genDebugInfo )
95107 {
96108 std::ostringstream insertion;
@@ -108,7 +120,7 @@ DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& source, LPCWSTR* arg
108120 }
109121
110122 ComPtr<IDxcBlobEncoding> src;
111- auto res = compiler-> getDxcUtils () ->CreateBlob (reinterpret_cast <const void *>(source.data ()), source.size (), CP_UTF8, &src);
123+ auto res = dxc-> m_dxcUtils ->CreateBlob (reinterpret_cast <const void *>(source.data ()), source.size (), CP_UTF8, &src);
112124 assert (SUCCEEDED (res));
113125
114126 DxcBuffer sourceBuffer;
@@ -117,7 +129,7 @@ DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& source, LPCWSTR* arg
117129 sourceBuffer.Encoding = 0 ;
118130
119131 ComPtr<IDxcResult> compileResult;
120- res = compiler-> getDxcCompiler () ->Compile (&sourceBuffer, args, argCount, nullptr , IID_PPV_ARGS (compileResult.GetAddressOf ()));
132+ res = dxc-> m_dxcCompiler ->Compile (&sourceBuffer, args, argCount, nullptr , IID_PPV_ARGS (compileResult.GetAddressOf ()));
121133 // If the compilation failed, this should still be a successful result
122134 assert (SUCCEEDED (res));
123135
@@ -288,7 +300,15 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
288300 const uint32_t nonDebugArgs = 5 ;
289301 const uint32_t allArgs = nonDebugArgs + 4 ;
290302
291- auto compileResult = dxcCompile (newCode, &arguments[0 ], hlslOptions.genDebugInfo ? allArgs : nonDebugArgs, hlslOptions);
303+ // const CHLSLCompiler* compiler, nbl::asset::hlsl::impl::DXC* compilerTypes, std::string& source, LPCWSTR* args, uint32_t argCount, const CHLSLCompiler::SOptions& options
304+ auto compileResult = dxcCompile (
305+ this ,
306+ m_dxcCompilerTypes.get (),
307+ newCode,
308+ &arguments[0 ],
309+ hlslOptions.genDebugInfo ? allArgs : nonDebugArgs,
310+ hlslOptions
311+ );
292312
293313 if (!compileResult.objectBlob )
294314 {
0 commit comments