Skip to content

Commit ced786f

Browse files
committed
[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 ac120e9 commit ced786f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

wgpu-hal/src/vulkan/adapter.rs

+28
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub struct PhysicalDeviceFeatures {
4040
/// Basic Vulkan 1.0 features.
4141
core: vk::PhysicalDeviceFeatures,
4242

43+
/// Vulkan 1.2 features.
44+
vulkan12: Option<vk::PhysicalDeviceVulkan12Features<'static>>,
45+
4346
/// Features provided by `VK_EXT_descriptor_indexing`, promoted to Vulkan 1.2.
4447
pub(super) descriptor_indexing:
4548
Option<vk::PhysicalDeviceDescriptorIndexingFeaturesEXT<'static>>,
@@ -136,6 +139,9 @@ impl PhysicalDeviceFeatures {
136139
mut info: vk::DeviceCreateInfo<'a>,
137140
) -> vk::DeviceCreateInfo<'a> {
138141
info = info.enabled_features(&self.core);
142+
if let Some(ref mut feature) = self.vulkan12 {
143+
info = info.push_next(feature);
144+
}
139145
if let Some(ref mut feature) = self.descriptor_indexing {
140146
info = info.push_next(feature);
141147
}
@@ -316,6 +322,14 @@ impl PhysicalDeviceFeatures {
316322
.geometry_shader(requested_features.contains(wgt::Features::SHADER_PRIMITIVE_INDEX))
317323
.depth_clamp(requested_features.contains(wgt::Features::DEPTH_CLIP_CONTROL))
318324
.dual_src_blend(requested_features.contains(wgt::Features::DUAL_SOURCE_BLENDING)),
325+
vulkan12: if device_api_version >= vk::API_VERSION_1_2 {
326+
Some(
327+
vk::PhysicalDeviceVulkan12Features::default()
328+
.shader_int8(private_caps.shader_int8),
329+
)
330+
} else {
331+
None
332+
},
319333
descriptor_indexing: if requested_features.intersects(INDEXING_FEATURES) {
320334
Some(
321335
vk::PhysicalDeviceDescriptorIndexingFeaturesEXT::default()
@@ -1390,6 +1404,13 @@ impl super::InstanceShared {
13901404
let core = vk::PhysicalDeviceFeatures::default();
13911405
let mut features2 = vk::PhysicalDeviceFeatures2KHR::default().features(core);
13921406

1407+
if capabilities.device_api_version >= vk::API_VERSION_1_2 {
1408+
let next = features
1409+
.vulkan12
1410+
.insert(vk::PhysicalDeviceVulkan12Features::default());
1411+
features2 = features2.push_next(next);
1412+
}
1413+
13931414
// `VK_KHR_multiview` is promoted to 1.1
13941415
if capabilities.device_api_version >= vk::API_VERSION_1_1
13951416
|| capabilities.supports_extension(khr::multiview::NAME)
@@ -1714,6 +1735,9 @@ impl super::Instance {
17141735
shader_integer_dot_product: phd_features
17151736
.shader_integer_dot_product
17161737
.is_some_and(|ext| ext.shader_integer_dot_product != 0),
1738+
shader_int8: phd_features
1739+
.vulkan12
1740+
.is_some_and(|features| features.shader_int8 != 0),
17171741
};
17181742
let capabilities = crate::Capabilities {
17191743
limits: phd_capabilities.to_wgpu_limits(),
@@ -2015,6 +2039,10 @@ impl super::Adapter {
20152039
spv::Capability::DotProductKHR,
20162040
]);
20172041
}
2042+
if self.private_caps.shader_int8 {
2043+
// See <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPhysicalDeviceVulkan12Features.html#features-shaderInt8>.
2044+
capabilities.extend(&[spv::Capability::Int8]);
2045+
}
20182046
spv::Options {
20192047
lang_version: match self.phd_capabilities.device_api_version {
20202048
// Use maximum supported SPIR-V version according to

wgpu-hal/src/vulkan/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,19 @@ 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 (possible in Vulkan 1.2+).
541+
///
542+
/// Allows shaders to declare the "Int8" capability. Note, however, that this
543+
/// feature alone allows the use of 8-bit integers "only in the `Private`,
544+
/// `Workgroup` (for non-Block variables), and `Function` storage classes"
545+
/// ([see spec]). To use 8-bit integers in the interface storage classes (e.g.,
546+
/// `StorageBuffer`), you also need to enable the corresponding feature in
547+
/// `VkPhysicalDevice8BitStorageFeatures` and declare the corresponding SPIR-V
548+
/// capability (e.g., `StorageBuffer8BitAccess`).
549+
///
550+
/// [see spec]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPhysicalDeviceVulkan12Features.html#features-shaderInt8
551+
shader_int8: bool,
539552
}
540553

541554
bitflags::bitflags!(

0 commit comments

Comments
 (0)