Skip to content

Commit 7962ce0

Browse files
committed
Support VK_KHR_get_surface_capabilities2
1 parent 1b19af0 commit 7962ce0

File tree

3 files changed

+183
-20
lines changed

3 files changed

+183
-20
lines changed

libportability-gfx/src/impls.rs

Lines changed: 114 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ use hal::{
1414
use std::{
1515
borrow::Cow,
1616
ffi::{CStr, CString},
17-
os::raw::c_int,
17+
os::raw::{c_int, c_void},
1818
mem, ptr, str,
1919
};
2020
#[cfg(feature = "gfx-backend-metal")]
2121
use std::env;
22-
#[cfg(feature = "renderdoc")]
23-
use std::os::raw::c_void;
2422

2523
use super::*;
2624

@@ -64,6 +62,16 @@ pub extern "C" fn gfxCreateInstance(
6462
#[cfg(feature = "env_logger")]
6563
{
6664
let _ = env_logger::try_init();
65+
let backend = if cfg!(feature = "gfx-backend-vulkan") {
66+
"Vulkan"
67+
} else if cfg!(feature = "gfx-backend-dx12") {
68+
"DX12"
69+
} else if cfg!(feature = "gfx-backend-metal") {
70+
"Metal"
71+
} else {
72+
"Other"
73+
};
74+
println!("gfx-portability backend: {}", backend);
6775
}
6876

6977
#[allow(unused_mut)]
@@ -275,7 +283,7 @@ pub extern "C" fn gfxGetPhysicalDeviceFeatures2KHR(
275283
other => {
276284
warn!("Unrecognized {:?}, skipping", other);
277285
unsafe {
278-
(ptr as *const VkPhysicalDeviceFeatures2KHR)
286+
(ptr as *const VkBaseStruct)
279287
.as_ref()
280288
.unwrap()
281289
}
@@ -385,7 +393,7 @@ pub extern "C" fn gfxGetPhysicalDeviceImageFormatProperties2KHR(
385393
other => {
386394
warn!("Unrecognized {:?}, skipping", other);
387395
unsafe {
388-
(ptr as *const VkPhysicalDeviceImageFormatInfo2KHR)
396+
(ptr as *const VkBaseStruct)
389397
.as_ref()
390398
.unwrap()
391399
}
@@ -470,7 +478,7 @@ pub extern "C" fn gfxGetPhysicalDeviceProperties2KHR(
470478
other => {
471479
warn!("Unrecognized {:?}, skipping", other);
472480
unsafe {
473-
(ptr as *const VkPhysicalDeviceProperties2KHR).as_ref().unwrap()
481+
(ptr as *const VkBaseStruct).as_ref().unwrap()
474482
}.pNext
475483
}
476484
} as *const VkStructureType;
@@ -544,7 +552,9 @@ pub extern "C" fn gfxGetInstanceProcAddr(
544552

545553
vkGetPhysicalDeviceSurfaceSupportKHR, PFN_vkGetPhysicalDeviceSurfaceSupportKHR => gfxGetPhysicalDeviceSurfaceSupportKHR,
546554
vkGetPhysicalDeviceSurfaceCapabilitiesKHR, PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR => gfxGetPhysicalDeviceSurfaceCapabilitiesKHR,
555+
vkGetPhysicalDeviceSurfaceCapabilities2KHR, PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR => gfxGetPhysicalDeviceSurfaceCapabilities2KHR,
547556
vkGetPhysicalDeviceSurfaceFormatsKHR, PFN_vkGetPhysicalDeviceSurfaceFormatsKHR => gfxGetPhysicalDeviceSurfaceFormatsKHR,
557+
vkGetPhysicalDeviceSurfaceFormats2KHR, PFN_vkGetPhysicalDeviceSurfaceFormats2KHR => gfxGetPhysicalDeviceSurfaceFormats2KHR,
548558
vkGetPhysicalDeviceSurfacePresentModesKHR, PFN_vkGetPhysicalDeviceSurfacePresentModesKHR => gfxGetPhysicalDeviceSurfacePresentModesKHR,
549559
vkGetPhysicalDeviceWin32PresentationSupportKHR, PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR => gfxGetPhysicalDeviceWin32PresentationSupportKHR,
550560

@@ -961,6 +971,7 @@ lazy_static! {
961971
#[cfg(target_os="macos")]
962972
VK_MVK_MACOS_SURFACE_EXTENSION_NAME,
963973
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
974+
VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
964975
]
965976
};
966977

@@ -1272,7 +1283,7 @@ pub extern "C" fn gfxMapMemory(
12721283
offset: VkDeviceSize,
12731284
size: VkDeviceSize,
12741285
_flags: VkMemoryMapFlags,
1275-
ppData: *mut *mut ::std::os::raw::c_void,
1286+
ppData: *mut *mut c_void,
12761287
) -> VkResult {
12771288
let range = hal::memory::Segment {
12781289
offset,
@@ -1422,7 +1433,7 @@ pub extern "C" fn gfxGetImageMemoryRequirements2KHR(
14221433
other => {
14231434
warn!("Unrecognized {:?}, skipping", other);
14241435
unsafe {
1425-
(ptr as *const VkMemoryRequirements2KHR).as_ref().unwrap()
1436+
(ptr as *const VkBaseStruct).as_ref().unwrap()
14261437
}.pNext
14271438
}
14281439
} as *const VkStructureType;
@@ -1691,7 +1702,7 @@ pub extern "C" fn gfxGetQueryPoolResults(
16911702
firstQuery: u32,
16921703
queryCount: u32,
16931704
dataSize: usize,
1694-
pData: *mut ::std::os::raw::c_void,
1705+
pData: *mut c_void,
16951706
stride: VkDeviceSize,
16961707
flags: VkQueryResultFlags,
16971708
) -> VkResult {
@@ -1988,7 +1999,7 @@ pub extern "C" fn gfxGetPipelineCacheData(
19881999
_gpu: VkDevice,
19892000
_pipelineCache: VkPipelineCache,
19902001
pDataSize: *mut usize,
1991-
_pData: *mut ::std::os::raw::c_void,
2002+
_pData: *mut c_void,
19922003
) -> VkResult {
19932004
//TODO: save
19942005
unsafe {
@@ -2982,11 +2993,22 @@ impl<'a> Iterator for DescriptorIter<'a> {
29822993
pso::DescriptorType::Image {
29832994
ty: pso::ImageDescriptorType::Sampled { with_sampler: true },
29842995
} => self.image_infos.next().map(|image| {
2985-
pso::Descriptor::CombinedImageSampler(
2986-
image.imageView.to_native().unwrap(),
2987-
conv::map_image_layout(image.imageLayout),
2988-
&*image.sampler,
2989-
)
2996+
// It is valid for the sampler to be NULL in case the descriptor is
2997+
// actually associated with an immutable sampler.
2998+
// It's still bad to try to derefence it, even theough the implementation
2999+
// will not try to use the value. (TODO: make this nicer)
3000+
if image.sampler != Handle::null() {
3001+
pso::Descriptor::CombinedImageSampler(
3002+
image.imageView.to_native().unwrap(),
3003+
conv::map_image_layout(image.imageLayout),
3004+
&*image.sampler,
3005+
)
3006+
} else {
3007+
pso::Descriptor::Image(
3008+
image.imageView.to_native().unwrap(),
3009+
conv::map_image_layout(image.imageLayout),
3010+
)
3011+
}
29903012
}),
29913013

29923014
pso::DescriptorType::InputAttachment | pso::DescriptorType::Image { .. } => {
@@ -3805,8 +3827,20 @@ pub extern "C" fn gfxCmdBlitImage(
38053827
pRegions: *const VkImageBlit,
38063828
filter: VkFilter,
38073829
) {
3808-
let src = srcImage.to_native().unwrap();
3809-
let dst = dstImage.to_native().unwrap();
3830+
let src = match srcImage.to_native() {
3831+
Ok(img) => img,
3832+
Err(_) => {
3833+
warn!("Unable to copy from a swapchain image!");
3834+
return;
3835+
}
3836+
};
3837+
let dst = match dstImage.to_native() {
3838+
Ok(img) => img,
3839+
Err(_) => {
3840+
warn!("Unable to copy into a swapchain image!");
3841+
return;
3842+
}
3843+
};
38103844

38113845
let regions = unsafe { slice::from_raw_parts(pRegions, regionCount as _) }
38123846
.iter()
@@ -3896,7 +3930,7 @@ pub extern "C" fn gfxCmdUpdateBuffer(
38963930
dstBuffer: VkBuffer,
38973931
dstOffset: VkDeviceSize,
38983932
dataSize: VkDeviceSize,
3899-
pData: *const ::std::os::raw::c_void,
3933+
pData: *const c_void,
39003934
) {
39013935
unsafe {
39023936
commandBuffer.update_buffer(
@@ -4277,7 +4311,7 @@ pub extern "C" fn gfxCmdPushConstants(
42774311
stageFlags: VkShaderStageFlags,
42784312
offset: u32,
42794313
size: u32,
4280-
pValues: *const ::std::os::raw::c_void,
4314+
pValues: *const c_void,
42814315
) {
42824316
assert_eq!(size % 4, 0);
42834317
unsafe {
@@ -4415,6 +4449,35 @@ pub extern "C" fn gfxGetPhysicalDeviceSurfaceCapabilitiesKHR(
44154449
VkResult::VK_SUCCESS
44164450
}
44174451

4452+
#[inline]
4453+
pub extern "C" fn gfxGetPhysicalDeviceSurfaceCapabilities2KHR(
4454+
adapter: VkPhysicalDevice,
4455+
pSurfaceInfo: *const VkPhysicalDeviceSurfaceInfo2KHR,
4456+
pSurfaceCapabilities: *mut VkSurfaceCapabilities2KHR,
4457+
) -> VkResult {
4458+
let surface = unsafe { (*pSurfaceInfo).surface };
4459+
let mut ptr = pSurfaceCapabilities as *const VkStructureType;
4460+
while !ptr.is_null() {
4461+
ptr = match unsafe { *ptr } {
4462+
VkStructureType::VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR => {
4463+
let data = unsafe { (ptr as *mut VkSurfaceCapabilities2KHR).as_mut().unwrap() };
4464+
gfxGetPhysicalDeviceSurfaceCapabilitiesKHR(adapter, surface, &mut data.surfaceCapabilities);
4465+
data.pNext
4466+
}
4467+
other => {
4468+
warn!("Unrecognized {:?}, skipping", other);
4469+
unsafe {
4470+
(ptr as *const VkBaseStruct)
4471+
.as_ref()
4472+
.unwrap()
4473+
}
4474+
.pNext
4475+
}
4476+
} as *const VkStructureType;
4477+
}
4478+
VkResult::VK_SUCCESS
4479+
}
4480+
44184481
#[inline]
44194482
pub extern "C" fn gfxGetPhysicalDeviceSurfaceFormatsKHR(
44204483
adapter: VkPhysicalDevice,
@@ -4447,6 +4510,38 @@ pub extern "C" fn gfxGetPhysicalDeviceSurfaceFormatsKHR(
44474510
VkResult::VK_SUCCESS
44484511
}
44494512

4513+
#[inline]
4514+
pub extern "C" fn gfxGetPhysicalDeviceSurfaceFormats2KHR(
4515+
adapter: VkPhysicalDevice,
4516+
pSurfaceInfo: *const VkPhysicalDeviceSurfaceInfo2KHR,
4517+
pSurfaceFormatCount: *mut u32,
4518+
pSurfaceFormats: *mut VkSurfaceFormat2KHR,
4519+
) -> VkResult {
4520+
let formats = unsafe { (*pSurfaceInfo).surface }
4521+
.supported_formats(&adapter.physical_device)
4522+
.map(|formats| formats.into_iter().map(conv::format_from_hal).collect())
4523+
.unwrap_or(vec![VkFormat::VK_FORMAT_UNDEFINED]);
4524+
4525+
if pSurfaceFormats.is_null() {
4526+
// Return only the number of formats
4527+
unsafe { *pSurfaceFormatCount = formats.len() as u32 };
4528+
} else {
4529+
let output =
4530+
unsafe { slice::from_raw_parts_mut(pSurfaceFormats, *pSurfaceFormatCount as usize) };
4531+
if output.len() > formats.len() {
4532+
unsafe { *pSurfaceFormatCount = formats.len() as u32 };
4533+
}
4534+
for (out, format) in output.iter_mut().zip(formats) {
4535+
out.surfaceFormat = VkSurfaceFormatKHR {
4536+
format,
4537+
colorSpace: VkColorSpaceKHR::VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, //TODO
4538+
};
4539+
}
4540+
}
4541+
4542+
VkResult::VK_SUCCESS
4543+
}
4544+
44504545
#[inline]
44514546
pub extern "C" fn gfxGetPhysicalDeviceSurfacePresentModesKHR(
44524547
adapter: VkPhysicalDevice,

libportability-gfx/src/lib.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ pub const VK_KHR_get_physical_device_properties2: ::std::os::raw::c_uint = 1;
404404
pub const VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_SPEC_VERSION: ::std::os::raw::c_uint = 1;
405405
pub const VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME: &'static [u8; 39usize] =
406406
b"VK_KHR_get_physical_device_properties2\x00";
407+
pub const VK_KHR_get_surface_capabilities2: ::std::os::raw::c_uint = 1;
408+
pub const VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION: ::std::os::raw::c_uint = 1;
409+
pub const VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME: &'static [u8; 33usize] =
410+
b"VK_KHR_get_surface_capabilities2\x00";
407411
pub const VK_KHR_shader_draw_parameters: ::std::os::raw::c_uint = 1;
408412
pub const VK_KHR_SHADER_DRAW_PARAMETERS_SPEC_VERSION: ::std::os::raw::c_uint = 1;
409413
pub const VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME: &'static [u8; 30usize] =
@@ -866,7 +870,7 @@ pub enum VkStructureType {
866870
VK_STRUCTURE_TYPE_DEVICE_GENERATED_COMMANDS_LIMITS_NVX = 1000086004,
867871
VK_STRUCTURE_TYPE_DEVICE_GENERATED_COMMANDS_FEATURES_NVX = 1000086005,
868872
VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_W_SCALING_STATE_CREATE_INFO_NV = 1000087000,
869-
VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES2_EXT = 1000090000,
873+
VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR = 1000090000,
870874
VK_STRUCTURE_TYPE_DISPLAY_POWER_INFO_EXT = 1000091000,
871875
VK_STRUCTURE_TYPE_DEVICE_EVENT_INFO_EXT = 1000091001,
872876
VK_STRUCTURE_TYPE_DISPLAY_EVENT_INFO_EXT = 1000091002,
@@ -1973,6 +1977,12 @@ pub type PFN_vkInternalFreeNotification = ::std::option::Option<
19731977
>;
19741978
pub type PFN_vkVoidFunction = ::std::option::Option<unsafe extern "C" fn()>;
19751979
#[repr(C)]
1980+
#[derive(Debug)]
1981+
pub struct VkBaseStruct {
1982+
pub sType: VkStructureType,
1983+
pub pNext: *const ::std::os::raw::c_void,
1984+
}
1985+
#[repr(C)]
19761986
#[derive(Debug, Copy)]
19771987
pub struct VkApplicationInfo {
19781988
pub sType: VkStructureType,
@@ -4745,6 +4755,18 @@ impl Clone for VkSurfaceCapabilitiesKHR {
47454755
}
47464756
#[repr(C)]
47474757
#[derive(Debug, Copy)]
4758+
pub struct VkSurfaceCapabilities2KHR {
4759+
pub sType: VkStructureType,
4760+
pub pNext: *const ::std::os::raw::c_void,
4761+
pub surfaceCapabilities: VkSurfaceCapabilitiesKHR,
4762+
}
4763+
impl Clone for VkSurfaceCapabilities2KHR {
4764+
fn clone(&self) -> Self {
4765+
*self
4766+
}
4767+
}
4768+
#[repr(C)]
4769+
#[derive(Debug, Copy)]
47484770
pub struct VkSurfaceFormatKHR {
47494771
pub format: VkFormat,
47504772
pub colorSpace: VkColorSpaceKHR,
@@ -4754,6 +4776,30 @@ impl Clone for VkSurfaceFormatKHR {
47544776
*self
47554777
}
47564778
}
4779+
#[repr(C)]
4780+
#[derive(Debug, Copy)]
4781+
pub struct VkSurfaceFormat2KHR {
4782+
pub sType: VkStructureType,
4783+
pub pNext: *const ::std::os::raw::c_void,
4784+
pub surfaceFormat: VkSurfaceFormatKHR,
4785+
}
4786+
impl Clone for VkSurfaceFormat2KHR {
4787+
fn clone(&self) -> Self {
4788+
*self
4789+
}
4790+
}
4791+
#[repr(C)]
4792+
#[derive(Debug, Copy)]
4793+
pub struct VkPhysicalDeviceSurfaceInfo2KHR {
4794+
pub sType: VkStructureType,
4795+
pub pNext: *const ::std::os::raw::c_void,
4796+
pub surface: VkSurfaceKHR,
4797+
}
4798+
impl Clone for VkPhysicalDeviceSurfaceInfo2KHR {
4799+
fn clone(&self) -> Self {
4800+
*self
4801+
}
4802+
}
47574803
pub type PFN_vkDestroySurfaceKHR = ::std::option::Option<
47584804
unsafe extern "C" fn(
47594805
instance: VkInstance,
@@ -4776,6 +4822,13 @@ pub type PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR = ::std::option::Option<
47764822
pSurfaceCapabilities: *mut VkSurfaceCapabilitiesKHR,
47774823
) -> VkResult,
47784824
>;
4825+
pub type PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR = ::std::option::Option<
4826+
unsafe extern "C" fn(
4827+
physicalDevice: VkPhysicalDevice,
4828+
pSurfaceInfo: *const VkPhysicalDeviceSurfaceInfo2KHR,
4829+
pSurfaceCapabilities: *mut VkSurfaceCapabilities2KHR,
4830+
) -> VkResult,
4831+
>;
47794832
pub type PFN_vkGetPhysicalDeviceSurfaceFormatsKHR = ::std::option::Option<
47804833
unsafe extern "C" fn(
47814834
physicalDevice: VkPhysicalDevice,
@@ -4784,6 +4837,14 @@ pub type PFN_vkGetPhysicalDeviceSurfaceFormatsKHR = ::std::option::Option<
47844837
pSurfaceFormats: *mut VkSurfaceFormatKHR,
47854838
) -> VkResult,
47864839
>;
4840+
pub type PFN_vkGetPhysicalDeviceSurfaceFormats2KHR = ::std::option::Option<
4841+
unsafe extern "C" fn(
4842+
physicalDevice: VkPhysicalDevice,
4843+
pSurfaceInfo: *const VkPhysicalDeviceSurfaceInfo2KHR,
4844+
pSurfaceFormatCount: *mut u32,
4845+
pSurfaceFormats: *mut VkSurfaceFormat2KHR,
4846+
) -> VkResult,
4847+
>;
47874848
pub type PFN_vkGetPhysicalDeviceSurfacePresentModesKHR = ::std::option::Option<
47884849
unsafe extern "C" fn(
47894850
physicalDevice: VkPhysicalDevice,

libportability/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,3 +1500,10 @@ pub extern "C" fn vkEnumerateInstanceExtensionProperties(
15001500
) -> VkResult {
15011501
gfxEnumerateInstanceExtensionProperties(pLayerName, pPropertyCount, pProperties)
15021502
}
1503+
1504+
//TODO: remove this once Dota2 stops asking for it
1505+
#[no_mangle]
1506+
pub extern "C" fn vkGetPhysicalDeviceMetalFeaturesMVK(
1507+
_adapter: VkPhysicalDevice,
1508+
_metal_features: *mut ::std::os::raw::c_void,
1509+
) {}

0 commit comments

Comments
 (0)