8
8
#include < regex>
9
9
#include < iterator>
10
10
11
+ #include < dxc/dxcapi.h>
12
+ #include < combaseapi.h>
11
13
12
14
using namespace nbl ;
13
15
using namespace nbl ::asset;
@@ -16,6 +18,56 @@ using namespace nbl::asset;
16
18
CHLSLCompiler::CHLSLCompiler (core::smart_refctd_ptr<system::ISystem>&& system)
17
19
: IShaderCompiler(std::move(system))
18
20
{
21
+ IDxcUtils* utils;
22
+ auto res = DxcCreateInstance (CLSID_DxcUtils, IID_PPV_ARGS (&utils));
23
+ assert (SUCCEEDED (res));
24
+
25
+ IDxcCompiler3* compiler;
26
+ res = DxcCreateInstance (CLSID_DxcCompiler, IID_PPV_ARGS (&compiler));
27
+ assert (SUCCEEDED (res));
28
+
29
+ m_dxcUtils = std::unique_ptr<IDxcUtils>(utils);
30
+ m_dxcCompiler = std::unique_ptr<IDxcCompiler3>(compiler);
31
+ }
32
+
33
+ CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile (asset::ICPUShader* source, LPCWSTR* args, uint32_t argCount, const SOptions& options)
34
+ {
35
+ DxcBuffer sourceBuffer;
36
+ sourceBuffer.Ptr = source->getContent ()->getPointer ();
37
+ sourceBuffer.Size = source->getContent ()->getSize ();
38
+ sourceBuffer.Encoding = 0 ;
39
+
40
+ IDxcResult* compileResult;
41
+ auto res = m_dxcCompiler->Compile (&sourceBuffer, args, argCount, nullptr , IID_PPV_ARGS (&compileResult));
42
+ // If the compilation failed, this should still be a successful result
43
+ assert (SUCCEEDED (res));
44
+
45
+ HRESULT compilationStatus = 0 ;
46
+ res = compileResult->GetStatus (&compilationStatus);
47
+ assert (SUCCEEDED (res));
48
+
49
+ IDxcBlobEncoding* errorBuffer;
50
+ res = compileResult->GetErrorBuffer (&errorBuffer);
51
+ assert (SUCCEEDED (res));
52
+
53
+ DxcCompilationResult result;
54
+ result.errorMessages = std::unique_ptr<IDxcBlobEncoding>(errorBuffer);
55
+ result.compileResult = std::unique_ptr<IDxcResult>(compileResult);
56
+ result.objectBlob = nullptr ;
57
+
58
+ if (!SUCCEEDED (compilationStatus))
59
+ {
60
+ options.logger .log (result.GetErrorMessagesString (), system::ILogger::ELL_ERROR);
61
+ return result;
62
+ }
63
+
64
+ IDxcBlob* resultingBlob;
65
+ res = compileResult->GetResult (&resultingBlob);
66
+ assert (SUCCEEDED (res));
67
+
68
+ result.objectBlob = std::unique_ptr<IDxcBlob>(resultingBlob);
69
+
70
+ return result;
19
71
}
20
72
21
73
core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV (const char * code, const IShaderCompiler::SCompilerOptions& options) const
@@ -28,9 +80,38 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
28
80
return nullptr ;
29
81
}
30
82
31
- core::smart_refctd_ptr<ICPUBuffer> spirv = nullptr ;
32
- // TODO: Use DXC
33
- return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move (spirv), options.stage , IShader::E_CONTENT_TYPE::ECT_SPIRV, options.preprocessorOptions .sourceIdentifier .data ());
83
+ core::smart_refctd_ptr<asset::ICPUShader> includesResolved;
84
+ if (hlslOptions.includeFinder != nullptr )
85
+ {
86
+ includesResolved = resolveIncludeDirectives (code, hlslOptions.stage , hlslOptions.sourceIdentifier .data (), hlslOptions.maxSelfInclusionCount , hlslOptions.logger );
87
+ if (includesResolved)
88
+ {
89
+ code = reinterpret_cast <const char *>(includesResolved->getContent ()->getPointer ());
90
+ }
91
+ }
92
+
93
+ LPCWSTR arguments[] = {
94
+ // These will always be present
95
+ L" -spirv" ,
96
+
97
+ // These are debug only
98
+ L" -DPARTI" ,
99
+ L" -DBOY"
100
+ };
101
+ const uint32_t allArgs = 3 ;
102
+ const uint32_t nonDebugArgs = 1 ;
103
+
104
+ DxcCompilationResult compileResult = dxcCompile (includesResolved.get (), &arguments[0 ], hlslOptions.genDebugInfo ? allArgs : nonDebugArgs, hlslOptions);
105
+
106
+ if (!compileResult.objectBlob )
107
+ {
108
+ return nullptr ;
109
+ }
110
+
111
+ auto outSpirv = core::make_smart_refctd_ptr<ICPUBuffer>(compileResult.objectBlob ->GetBufferSize ());
112
+ memcpy (outSpirv->getPointer (), compileResult.objectBlob ->GetBufferPointer (), compileResult.objectBlob ->GetBufferSize ());
113
+
114
+ return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move (outSpirv), hlslOptions.stage , IShader::E_CONTENT_TYPE::ECT_SPIRV, hlslOptions.sourceIdentifier .data ());
34
115
}
35
116
36
117
void CHLSLCompiler::insertExtraDefines (std::string& code, const core::SRange<const char * const >& defines) const
0 commit comments