Skip to content

Commit 62b4be3

Browse files
Merge pull request #849 from Devsh-Graphics-Programming/full_inline_spirv_bda
DXC member decorate, Spir-V 1.4 backports and KHR_fragment_shader_barycentrics support
2 parents 947475f + 0a92d23 commit 62b4be3

File tree

9 files changed

+140
-37
lines changed

9 files changed

+140
-37
lines changed

3rdparty/dxc/dxc

Submodule dxc updated 69 files

include/nbl/asset/utils/CHLSLCompiler.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
146146
L"-Wno-c++1z-extensions",
147147
L"-Wno-c++14-extensions",
148148
L"-Wno-gnu-static-float-init",
149-
L"-fspv-target-env=vulkan1.3",
150149
L"-HV", L"202x"
151150
});
152151
};

include/nbl/builtin/hlsl/bda/__ref.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace hlsl
1313
namespace bda
1414
{
1515
template<typename T>
16-
using __spv_ptr_t = spirv::pointer_t<spv::StorageClassPhysicalStorageBuffer,T>;
16+
using __spv_ptr_t __NBL_CAPABILITY_PhysicalStorageBufferAddresses = spirv::pointer_t<spv::StorageClassPhysicalStorageBuffer,T>;
1717

1818
template<typename T>
1919
struct __ptr;

include/nbl/builtin/hlsl/spirv_intrinsics/core.hlsl

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,40 @@ namespace nbl
1717
namespace hlsl
1818
{
1919
#ifdef __HLSL_VERSION
20+
21+
22+
#if __SPIRV_MAJOR_VERSION__>1 || (__SPIRV_MAJOR_VERSION__==1 && __SPIRV_MINOR_VERSION__>=5)
23+
24+
#define __NBL_SPIRV_SUPERSET_1_5__
25+
26+
#define __NBL_CAPABILITY_VulkanMemoryModel [[vk::ext_capability(spv::CapabilityVulkanMemoryModel)]]
27+
#define __NBL_CAPABILITY_ShaderNonUniform [[vk::ext_capability(spv::CapabilityShaderNonUniform)]]
28+
#define __NBL_CAPABILITY_PhysicalStorageBufferAddresses [[vk::ext_capability(spv::CapabilityPhysicalStorageBufferAddresses)]]
29+
// TODO: some poor soul needs to study rest of https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_capability
30+
#define __NBL_CAPABILITY_ShaderLayer [[vk::ext_capability(spv::CapabilityShaderLayer)]]
31+
#define __NBL_CAPABILITY_ShaderViewportIndex [[vk::ext_capability(spv::CapabilityShaderViewportIndex)]]
32+
33+
#else
34+
35+
#define __NBL_CAPABILITY_VulkanMemoryModel [[vk::ext_capability(spv::CapabilityVulkanMemoryModel)]] [[vk::ext_extension("SPV_KHR_vulkan_memory_model")]]
36+
#define __NBL_CAPABILITY_ShaderNonUniform [[vk::ext_capability(spv::CapabilityShaderNonUniform)]] [[vk::ext_extension("SPV_EXT_descriptor_indexing")]]
37+
#define __NBL_CAPABILITY_PhysicalStorageBufferAddresses [[vk::ext_capability(spv::CapabilityPhysicalStorageBufferAddresses)]] [[vk::ext_extension("SPV_KHR_physical_storage_buffer")]]
38+
39+
#endif
40+
41+
42+
#if __SPIRV_MAJOR_VERSION__>1 || (__SPIRV_MAJOR_VERSION__==1 && __SPIRV_MINOR_VERSION__>=6)
43+
44+
#define __NBL_SPIRV_SUPERSET_1_6__
45+
46+
// 1.6 core caps
47+
48+
#else
49+
50+
// 1.6 core caps and their old extensions
51+
52+
#endif
53+
2054
namespace spirv
2155
{
2256
//! General
@@ -171,7 +205,7 @@ enable_if_t<is_spirv_type_v<Ptr_T>, T> atomicCompareExchange(Ptr_T ptr, uint32_t
171205

172206

173207
template<typename T, uint32_t alignment>
174-
[[vk::ext_capability(spv::CapabilityPhysicalStorageBufferAddresses)]]
208+
__NBL_CAPABILITY_PhysicalStorageBufferAddresses
175209
[[vk::ext_instruction(spv::OpLoad)]]
176210
T load(pointer_t<spv::StorageClassPhysicalStorageBuffer,T> pointer, [[vk::ext_literal]] uint32_t __aligned = /*Aligned*/0x00000002, [[vk::ext_literal]] uint32_t __alignment = alignment);
177211

@@ -180,7 +214,7 @@ template<typename T, typename P>
180214
enable_if_t<is_spirv_type_v<P>,T> load(P pointer);
181215

182216
template<typename T, uint32_t alignment>
183-
[[vk::ext_capability(spv::CapabilityPhysicalStorageBufferAddresses)]]
217+
__NBL_CAPABILITY_PhysicalStorageBufferAddresses
184218
[[vk::ext_instruction(spv::OpStore)]]
185219
void store(pointer_t<spv::StorageClassPhysicalStorageBuffer,T> pointer, T obj, [[vk::ext_literal]] uint32_t __aligned = /*Aligned*/0x00000002, [[vk::ext_literal]] uint32_t __alignment = alignment);
186220

@@ -205,12 +239,12 @@ template<typename T, typename U>
205239
enable_if_t<is_spirv_type_v<T> && is_spirv_type_v<U>, T> bitcast(U);
206240

207241
template<typename T>
208-
[[vk::ext_capability(spv::CapabilityPhysicalStorageBufferAddresses)]]
242+
__NBL_CAPABILITY_PhysicalStorageBufferAddresses
209243
[[vk::ext_instruction(spv::OpBitcast)]]
210244
uint64_t bitcast(pointer_t<spv::StorageClassPhysicalStorageBuffer,T>);
211245

212246
template<typename T>
213-
[[vk::ext_capability(spv::CapabilityPhysicalStorageBufferAddresses)]]
247+
__NBL_CAPABILITY_PhysicalStorageBufferAddresses
214248
[[vk::ext_instruction(spv::OpBitcast)]]
215249
pointer_t<spv::StorageClassPhysicalStorageBuffer,T> bitcast(uint64_t);
216250

@@ -269,8 +303,7 @@ enable_if_t<is_vector_v<BooleanVector>&& is_same_v<typename vector_traits<Boolea
269303
}
270304

271305
#endif
272-
}
273306
}
274-
307+
}
275308
#endif
276309
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (C) 2023 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_BUILTIN_HLSL_SPIRV_INTRINSICS_FRAGMENT_SHADER_BARYCENTRIC_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_SPIRV_INTRINSICS_FRAGMENT_SHADER_BARYCENTRIC_INCLUDED_
6+
7+
#include "spirv/unified1/spirv.hpp"
8+
9+
namespace nbl
10+
{
11+
namespace hlsl
12+
{
13+
namespace spirv
14+
{
15+
16+
[[vk::ext_capability(/*spv::CapabilityFragmentBarycentricKHR*/5284)]]
17+
[[vk::ext_extension("SPV_KHR_fragment_shader_barycentric")]]
18+
[[vk::ext_builtin_input(/*spv::BuiltInBaryCoordKHR*/5286)]]
19+
static const float32_t3 BaryCoordKHR;
20+
21+
[[vk::ext_capability(/*spv::CapabilityFragmentBarycentricKHR*/5284)]]
22+
[[vk::ext_extension("SPV_KHR_fragment_shader_barycentric")]]
23+
[[vk::ext_builtin_input(/*spv::BuiltInBaryCoordKHR*/5287)]]
24+
static const float32_t3 BaryCoordNoPerspKHR;
25+
26+
}
27+
}
28+
}
29+
30+
#endif

include/nbl/builtin/hlsl/spirv_intrinsics/subgroup_arithmetic.hlsl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
// For all WaveMultiPrefix* ops, an example can be found here https://github.com/microsoft/DirectXShaderCompiler/blob/4e5440e1ee1f30d1164f90445611328293de08fa/tools/clang/test/HLSLFileCheck/hlsl/intrinsics/wave/prefix/sm_6_5_wave.hlsl
88
// However, we prefer to implement them with SPIRV intrinsics to avoid DXC changes in the compiler's emitted code
99

10+
11+
#include "nbl/builtin/hlsl/spirv_intrinsics/core.hlsl"
12+
1013
namespace nbl
1114
{
1215
namespace hlsl

include/nbl/builtin/hlsl/spirv_intrinsics/subgroup_vote.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#define _NBL_BUILTIN_HLSL_SPIRV_INTRINSICS_SUBGROUP_VOTE_INCLUDED_
66

77

8-
#include "nbl/builtin/hlsl/spirv_intrinsics/basic.hlsl"
8+
#include "nbl/builtin/hlsl/spirv_intrinsics/subgroup_basic.hlsl"
99

1010

1111
namespace nbl

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ CHLSLCompiler::~CHLSLCompiler()
9595
delete m_dxcCompilerTypes;
9696
}
9797

98+
static bool fixup_spirv_target_ver(std::vector<std::wstring>& arguments, system::logger_opt_ptr& logger)
99+
{
100+
constexpr std::wstring_view Prefix = L"-fspv-target-env=";
101+
const std::set<std::wstring_view> AllowedSuffices = {L"vulkan1.1spirv1.4",L"vulkan1.2",L"vulkan1.3"};
102+
103+
for (auto targetEnvArgumentPos=arguments.begin(); targetEnvArgumentPos!=arguments.end(); targetEnvArgumentPos++)
104+
if (targetEnvArgumentPos->find(Prefix)==0)
105+
{
106+
const auto suffix = targetEnvArgumentPos->substr(Prefix.length());
107+
const auto found = AllowedSuffices.find(suffix);
108+
if (found!=AllowedSuffices.end())
109+
return true;
110+
logger.log("Compile flag error: Required compile flag not found -fspv-target-env=. Force enabling -fspv-target-env= found but with unsupported value `%s`.", system::ILogger::ELL_ERROR, "TODO: write wchar to char convert usage");
111+
return false;
112+
}
113+
114+
logger.log("Compile flag error: Required compile flag not found -fspv-target-env=. Force enabling -fspv-target-env=vulkan1.3, as it is required by Nabla.", system::ILogger::ELL_WARNING);
115+
arguments.push_back(L"-fspv-target-env=vulkan1.3");
116+
return true;
117+
}
98118

99119
static void try_upgrade_hlsl_version(std::vector<std::wstring>& arguments, system::logger_opt_ptr& logger)
100120
{
@@ -418,35 +438,49 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV_impl(const std:
418438
std::wstring targetProfile(SHADER_MODEL_PROFILE);
419439

420440
std::vector<std::wstring> arguments = {};
421-
if (dxc_compile_flags.size()) { // #pragma dxc_compile_flags takes priority
441+
if (!dxc_compile_flags.empty()) // #pragma dxc_compile_flags takes priority
422442
populate_arguments_with_type_conversion(arguments, dxc_compile_flags, logger);
423-
}
424-
else if (hlslOptions.dxcOptions.size()) { // second in order of priority is command line arguments
425-
populate_arguments_with_type_conversion(arguments, hlslOptions.dxcOptions, logger);
426-
}
427-
else { // lastly default arguments
428-
const auto required = CHLSLCompiler::getRequiredArguments();
429-
arguments = {};
430-
for (size_t i = 0; i < required.size(); i++)
431-
arguments.push_back(required[i]);
432-
arguments.push_back(L"-HV");
433-
arguments.push_back(L"202x");
434-
// TODO: add this to `CHLSLCompiler::SOptions` and handle it properly in `dxc_compile_flags.empty()`
435-
if (stage != asset::IShader::E_SHADER_STAGE::ESS_ALL_OR_LIBRARY) {
436-
arguments.push_back(L"-E");
437-
arguments.push_back(L"main");
438-
}
439-
// If a custom SPIR-V optimizer is specified, use that instead of DXC's spirv-opt.
440-
// This is how we can get more optimizer options.
441-
//
442-
// Optimization is also delegated to SPIRV-Tools. Right now there are no difference between
443-
// optimization levels greater than zero; they will all invoke the same optimization recipe.
444-
// https://github.com/Microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#optimization
445-
if (hlslOptions.spirvOptimizer)
446-
arguments.push_back(L"-O0");
447-
}
448-
if (dxc_compile_flags.empty())
443+
else
449444
{
445+
if (hlslOptions.dxcOptions.size()) // second in order of priority is command line arguments
446+
populate_arguments_with_type_conversion(arguments, hlslOptions.dxcOptions, logger);
447+
else // lastly default arguments from polymorphic options
448+
{
449+
const auto required = CHLSLCompiler::getRequiredArguments();
450+
arguments = {};
451+
for (size_t i = 0; i < required.size(); i++)
452+
arguments.push_back(required[i]);
453+
//
454+
switch (options.targetSpirvVersion)
455+
{
456+
case hlsl::SpirvVersion::ESV_1_4:
457+
arguments.push_back(L"-fspv-target-env=vulkan1.1spirv1.4");
458+
case hlsl::SpirvVersion::ESV_1_5:
459+
arguments.push_back(L"-fspv-target-env=vulkan1.2");
460+
break;
461+
case hlsl::SpirvVersion::ESV_1_6:
462+
arguments.push_back(L"-fspv-target-env=vulkan1.3");
463+
break;
464+
default:
465+
logger.log("Invalid `IShaderCompiler::SCompilerOptions::targetSpirvVersion`", system::ILogger::ELL_ERROR);
466+
return nullptr;
467+
break;
468+
}
469+
// TODO: add entry point to `CHLSLCompiler::SOptions` and handle it properly in `dxc_compile_flags.empty()`
470+
if (stage != asset::IShader::E_SHADER_STAGE::ESS_ALL_OR_LIBRARY) {
471+
arguments.push_back(L"-E");
472+
arguments.push_back(L"main");
473+
}
474+
// If a custom SPIR-V optimizer is specified, use that instead of DXC's spirv-opt.
475+
// This is how we can get more optimizer options.
476+
//
477+
// Optimization is also delegated to SPIRV-Tools. Right now there are no difference between
478+
// optimization levels greater than zero; they will all invoke the same optimization recipe.
479+
// https://github.com/Microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#optimization
480+
if (hlslOptions.spirvOptimizer)
481+
arguments.push_back(L"-O0");
482+
}
483+
//
450484
auto set = std::unordered_set<std::wstring>();
451485
for (int i = 0; i < arguments.size(); i++)
452486
set.insert(arguments[i]);
@@ -469,6 +503,9 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV_impl(const std:
469503
add_if_missing(L"-fspv-debug=vulkan-with-source");
470504
}
471505

506+
if (!fixup_spirv_target_ver(arguments, logger))
507+
return nullptr;
508+
472509
try_upgrade_shader_stage(arguments, stage, logger);
473510
try_upgrade_hlsl_version(arguments, logger);
474511

src/nbl/builtin/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/matrix_utils/matrix_traits.hl
230230
#spirv intrinsics
231231
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/spirv_intrinsics/core.hlsl")
232232
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/spirv_intrinsics/fragment_shader_pixel_interlock.hlsl")
233+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/spirv_intrinsics/fragment_shader_barycentric.hlsl")
233234
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/spirv_intrinsics/raytracing.hlsl")
234235
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/spirv_intrinsics/subgroup_arithmetic.hlsl")
235236
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/spirv_intrinsics/subgroup_ballot.hlsl")

0 commit comments

Comments
 (0)