Skip to content

Commit 23a9b7c

Browse files
committed
Implement HLSL changes
1 parent e04afc1 commit 23a9b7c

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

src/nbl/asset/utils/CCompilerSet.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ core::smart_refctd_ptr<ICPUShader> CCompilerSet::preprocessShader(const ICPUShad
5757
return core::make_smart_refctd_ptr<ICPUShader>(resolvedCode.c_str(), stage, IShader::E_CONTENT_TYPE::ECT_GLSL, std::string(shader->getFilepathHint()));
5858
}
5959
break;
60+
case IShader::E_CONTENT_TYPE::ECT_SPIRV:
61+
return core::smart_refctd_ptr<ICPUShader>(const_cast<ICPUShader*>(shader));
6062
default:
6163
return nullptr;
6264
}

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,15 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
256256
auto newCode = preprocessShader(code, stage, hlslOptions.preprocessorOptions);
257257

258258
// Suffix is the shader model version
259+
// TODO: Figure out a way to get the shader model version automatically
260+
//
261+
// We can't get it from the DXC library itself, as the different versions and the parsing
262+
// use a weird lexer based system that resolves to a hash, and all of that is in a scoped variable
263+
// (lib/DXIL/DxilShaderModel.cpp:83)
264+
//
265+
// Another option is trying to fetch it from the commandline tool, either from parsing the help message
266+
// or from brute forcing every -T option until one isn't accepted
267+
//
259268
std::wstring targetProfile(L"XX_6_2");
260269

261270
// Set profile two letter prefix based on stage
@@ -289,29 +298,40 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
289298
return nullptr;
290299
};
291300

292-
LPCWSTR arguments[] = {
293-
// These will always be present
301+
std::vector<LPCWSTR> arguments = {
294302
L"-spirv",
295303
L"-HV", L"2021",
296304
L"-T", targetProfile.c_str(),
297-
298-
// These are debug only
299-
DXC_ARG_DEBUG,
300-
L"-Qembed_debug",
301-
L"-fspv-debug=vulkan-with-source",
302-
L"-fspv-debug=file"
303305
};
304306

305-
const uint32_t nonDebugArgs = 5;
306-
const uint32_t allArgs = nonDebugArgs + 4;
307+
// If a custom SPIR-V optimizer is specified, use that instead of DXC's spirv-opt.
308+
// This is how we can get more optimizer options.
309+
//
310+
// Optimization is also delegated to SPIRV-Tools. Right now there are no difference between
311+
// optimization levels greater than zero; they will all invoke the same optimization recipe.
312+
// https://github.com/Microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#optimization
313+
if (hlslOptions.spirvOptimizer)
314+
{
315+
arguments.push_back(L"-O0");
316+
}
317+
318+
// Debug only values
319+
if (hlslOptions.genDebugInfo)
320+
{
321+
arguments.insert(arguments.end(), {
322+
DXC_ARG_DEBUG,
323+
L"-Qembed_debug",
324+
L"-fspv-debug=vulkan-with-source",
325+
L"-fspv-debug=file"
326+
});
327+
}
307328

308-
// const CHLSLCompiler* compiler, nbl::asset::hlsl::impl::DXC* compilerTypes, std::string& source, LPCWSTR* args, uint32_t argCount, const CHLSLCompiler::SOptions& options
309329
auto compileResult = dxcCompile(
310330
this,
311331
m_dxcCompilerTypes,
312332
newCode,
313-
&arguments[0],
314-
hlslOptions.genDebugInfo ? allArgs : nonDebugArgs,
333+
&arguments[0],
334+
arguments.size(),
315335
hlslOptions
316336
);
317337

@@ -321,6 +341,11 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
321341
}
322342

323343
auto outSpirv = core::make_smart_refctd_ptr<ICPUBuffer>(compileResult.objectBlob->GetBufferSize());
344+
345+
// Optimizer step
346+
if (hlslOptions.spirvOptimizer)
347+
outSpirv = hlslOptions.spirvOptimizer->optimize(outSpirv.get(), hlslOptions.preprocessorOptions.logger);
348+
324349
memcpy(outSpirv->getPointer(), compileResult.objectBlob->GetBufferPointer(), compileResult.objectBlob->GetBufferSize());
325350

326351
return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move(outSpirv), stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, hlslOptions.preprocessorOptions.sourceIdentifier.data());

0 commit comments

Comments
 (0)