Skip to content

Commit ff29165

Browse files
robamlerteoxoy
authored andcommitted
[wgpu-hal] Add PrivateCapabilities::shader_int8 on Vulkan
This allows declaring the SPIR-V capability "Int8", which allows us to generate faster code for `[un]pack4x{I, U}8[Clamp]`.
1 parent e636696 commit ff29165

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

wgpu-hal/src/vulkan/adapter.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,13 @@ impl PhysicalDeviceFeatures {
386386
} else {
387387
None
388388
},
389-
shader_float16_int8: if requested_features.contains(wgt::Features::SHADER_F16) {
390-
Some(vk::PhysicalDeviceShaderFloat16Int8Features::default().shader_float16(true))
391-
} else {
392-
None
389+
shader_float16_int8: match requested_features.contains(wgt::Features::SHADER_F16) {
390+
shader_float16 if shader_float16 || private_caps.shader_int8 => Some(
391+
vk::PhysicalDeviceShaderFloat16Int8Features::default()
392+
.shader_float16(shader_float16)
393+
.shader_int8(private_caps.shader_int8),
394+
),
395+
_ => None,
393396
},
394397
_16bit_storage: if requested_features.contains(wgt::Features::SHADER_F16) {
395398
Some(
@@ -981,6 +984,15 @@ impl PhysicalDeviceProperties {
981984
if requested_features.contains(wgt::Features::TEXTURE_FORMAT_NV12) {
982985
extensions.push(khr::sampler_ycbcr_conversion::NAME);
983986
}
987+
988+
// Require `VK_KHR_16bit_storage` if the feature `SHADER_F16` was requested
989+
if requested_features.contains(wgt::Features::SHADER_F16) {
990+
// - Feature `SHADER_F16` also requires `VK_KHR_shader_float16_int8`, but we always
991+
// require that anyway (if it is available) below.
992+
// - `VK_KHR_16bit_storage` requires `VK_KHR_storage_buffer_storage_class`, however
993+
// we require that one already.
994+
extensions.push(khr::_16bit_storage::NAME);
995+
}
984996
}
985997

986998
if self.device_api_version < vk::API_VERSION_1_2 {
@@ -1004,13 +1016,13 @@ impl PhysicalDeviceProperties {
10041016
extensions.push(ext::descriptor_indexing::NAME);
10051017
}
10061018

1007-
// Require `VK_KHR_shader_float16_int8` and `VK_KHR_16bit_storage` if the associated feature was requested
1008-
if requested_features.contains(wgt::Features::SHADER_F16) {
1019+
// Always require `VK_KHR_shader_float16_int8` if available as it enables
1020+
// Int8 optimizations. Also require it even if it's not available but
1021+
// requested so that we get a corresponding error message.
1022+
if requested_features.contains(wgt::Features::SHADER_F16)
1023+
|| self.supports_extension(khr::shader_float16_int8::NAME)
1024+
{
10091025
extensions.push(khr::shader_float16_int8::NAME);
1010-
// `VK_KHR_16bit_storage` requires `VK_KHR_storage_buffer_storage_class`, however we require that one already
1011-
if self.device_api_version < vk::API_VERSION_1_1 {
1012-
extensions.push(khr::_16bit_storage::NAME);
1013-
}
10141026
}
10151027

10161028
if requested_features.intersects(wgt::Features::EXPERIMENTAL_MESH_SHADER) {
@@ -1479,12 +1491,17 @@ impl super::InstanceShared {
14791491
.insert(vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT::default());
14801492
features2 = features2.push_next(next);
14811493
}
1482-
if capabilities.supports_extension(khr::shader_float16_int8::NAME) {
1494+
1495+
// `VK_KHR_shader_float16_int8` is promoted to 1.2
1496+
if capabilities.device_api_version >= vk::API_VERSION_1_2
1497+
|| capabilities.supports_extension(khr::shader_float16_int8::NAME)
1498+
{
14831499
let next = features
14841500
.shader_float16_int8
14851501
.insert(vk::PhysicalDeviceShaderFloat16Int8FeaturesKHR::default());
14861502
features2 = features2.push_next(next);
14871503
}
1504+
14881505
if capabilities.supports_extension(khr::_16bit_storage::NAME) {
14891506
let next = features
14901507
._16bit_storage
@@ -1728,6 +1745,9 @@ impl super::Instance {
17281745
shader_integer_dot_product: phd_features
17291746
.shader_integer_dot_product
17301747
.is_some_and(|ext| ext.shader_integer_dot_product != 0),
1748+
shader_int8: phd_features
1749+
.shader_float16_int8
1750+
.is_some_and(|features| features.shader_int8 != 0),
17311751
};
17321752
let capabilities = crate::Capabilities {
17331753
limits: phd_capabilities.to_wgpu_limits(),
@@ -2029,6 +2049,10 @@ impl super::Adapter {
20292049
spv::Capability::DotProductKHR,
20302050
]);
20312051
}
2052+
if self.private_caps.shader_int8 {
2053+
// See <https://registry.khronos.org/vulkan/specs/latest/man/html/VkPhysicalDeviceShaderFloat16Int8Features.html#extension-features-shaderInt8>.
2054+
capabilities.extend(&[spv::Capability::Int8]);
2055+
}
20322056
spv::Options {
20332057
lang_version: match self.phd_capabilities.device_api_version {
20342058
// Use maximum supported SPIR-V version according to

wgpu-hal/src/vulkan/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,21 @@ struct PrivateCapabilities {
536536
///
537537
/// [`VK_KHR_shader_integer_dot_product`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_shader_integer_dot_product.html
538538
shader_integer_dot_product: bool,
539+
540+
/// True if this adapter supports 8-bit integers provided by the
541+
/// [`VK_KHR_shader_float16_int8`] extension (promoted to Vulkan 1.2).
542+
///
543+
/// Allows shaders to declare the "Int8" capability. Note, however, that this
544+
/// feature alone allows the use of 8-bit integers "only in the `Private`,
545+
/// `Workgroup` (for non-Block variables), and `Function` storage classes"
546+
/// ([see spec]). To use 8-bit integers in the interface storage classes (e.g.,
547+
/// `StorageBuffer`), you also need to enable the corresponding feature in
548+
/// `VkPhysicalDevice8BitStorageFeatures` and declare the corresponding SPIR-V
549+
/// capability (e.g., `StorageBuffer8BitAccess`).
550+
///
551+
/// [`VK_KHR_shader_float16_int8`]: https://registry.khronos.org/vulkan/specs/latest/man/html/VK_KHR_shader_float16_int8.html
552+
/// [see spec]: https://registry.khronos.org/vulkan/specs/latest/man/html/VkPhysicalDeviceShaderFloat16Int8Features.html#extension-features-shaderInt8
553+
shader_int8: bool,
539554
}
540555

541556
bitflags::bitflags!(

0 commit comments

Comments
 (0)