Skip to content

Commit ea45004

Browse files
committed
extensions/amd/shader_info: Replace unreachable ShaderInfoResult with 3 specialized functions
Since the extension modularization in #894 this `enum ShaderInfoResult` is no longer reachable through the crate hierarchy, making it impossible for callers to match on its variants. Not that this type was ideal to begin with: the returned `enum` variant depended purely on the `info_type` parameter, leading to ugly `unreachable!()`-like unwraps in caller code when the variant should always be of the type that a caller requested. To solve both issues, create 3 instances of the `get_shader_info()` function for each of the 3 `vk::ShaderInfoTypeAMD`s that a caller can request.
1 parent 0c362c0 commit ea45004

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

ash/src/extensions/amd/shader_info.rs

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,63 @@ use alloc::vec::Vec;
66
use core::mem;
77

88
impl crate::amd::shader_info::Device {
9-
/// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkGetShaderInfoAMD.html>
9+
/// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkGetShaderInfoAMD.html> with [`vk::ShaderInfoTypeAMD::STATISTICS`]
1010
#[inline]
11-
pub unsafe fn get_shader_info(
11+
pub unsafe fn get_shader_info_statistics(
1212
&self,
1313
pipeline: vk::Pipeline,
1414
shader_stage: vk::ShaderStageFlags,
15-
info_type: vk::ShaderInfoTypeAMD,
16-
) -> VkResult<ShaderInfoResult> {
17-
let load_data = |count: &mut usize, data: *mut u8| {
15+
) -> VkResult<vk::ShaderStatisticsInfoAMD> {
16+
let mut info = mem::MaybeUninit::<vk::ShaderStatisticsInfoAMD>::uninit();
17+
let mut size = mem::size_of_val(&info);
18+
(self.fp.get_shader_info_amd)(
19+
self.handle,
20+
pipeline,
21+
shader_stage,
22+
vk::ShaderInfoTypeAMD::STATISTICS,
23+
&mut size,
24+
info.as_mut_ptr().cast(),
25+
)
26+
.result()?;
27+
assert_eq!(size, mem::size_of_val(&info));
28+
Ok(info.assume_init())
29+
}
30+
31+
/// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkGetShaderInfoAMD.html> with [`vk::ShaderInfoTypeAMD::BINARY`]
32+
#[inline]
33+
pub unsafe fn get_shader_info_binary(
34+
&self,
35+
pipeline: vk::Pipeline,
36+
shader_stage: vk::ShaderStageFlags,
37+
) -> VkResult<Vec<u8>> {
38+
read_into_uninitialized_vector(|count, data: *mut u8| {
1839
(self.fp.get_shader_info_amd)(
1940
self.handle,
2041
pipeline,
2142
shader_stage,
22-
info_type,
43+
vk::ShaderInfoTypeAMD::BINARY,
2344
count,
2445
data.cast(),
2546
)
26-
};
27-
28-
match info_type {
29-
vk::ShaderInfoTypeAMD::STATISTICS => {
30-
let mut statistics_info = mem::MaybeUninit::<vk::ShaderStatisticsInfoAMD>::uninit();
31-
load_data(
32-
&mut mem::size_of_val(&statistics_info),
33-
statistics_info.as_mut_ptr().cast(),
34-
)
35-
.result()?;
36-
Ok(ShaderInfoResult::StatisticsInfo(
37-
statistics_info.assume_init(),
38-
))
39-
}
40-
vk::ShaderInfoTypeAMD::BINARY => {
41-
read_into_uninitialized_vector(load_data).map(ShaderInfoResult::Binary)
42-
}
43-
vk::ShaderInfoTypeAMD::DISASSEMBLY => {
44-
read_into_uninitialized_vector(load_data).map(ShaderInfoResult::Disassembly)
45-
}
46-
#[cfg(feature = "debug")]
47-
x => unimplemented!("ShaderInfoTypeAMD {:?}", x),
48-
#[cfg(not(feature = "debug"))]
49-
x => unimplemented!("ShaderInfoTypeAMD {}", x.0),
50-
}
47+
})
5148
}
52-
}
5349

54-
#[derive(Clone)]
55-
#[cfg_attr(feature = "debug", derive(Debug))]
56-
pub enum ShaderInfoResult {
57-
StatisticsInfo(vk::ShaderStatisticsInfoAMD),
58-
Binary(Vec<u8>),
59-
Disassembly(Vec<u8>),
50+
/// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkGetShaderInfoAMD.html> with [`vk::ShaderInfoTypeAMD::DISASSEMBLY`]
51+
#[inline]
52+
pub unsafe fn get_shader_info_disassembly(
53+
&self,
54+
pipeline: vk::Pipeline,
55+
shader_stage: vk::ShaderStageFlags,
56+
) -> VkResult<Vec<u8>> {
57+
read_into_uninitialized_vector(|count, data: *mut u8| {
58+
(self.fp.get_shader_info_amd)(
59+
self.handle,
60+
pipeline,
61+
shader_stage,
62+
vk::ShaderInfoTypeAMD::DISASSEMBLY,
63+
count,
64+
data.cast(),
65+
)
66+
})
67+
}
6068
}

0 commit comments

Comments
 (0)