Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Rua committed May 31, 2022
1 parent de8e950 commit 315b1b9
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 270 deletions.
226 changes: 126 additions & 100 deletions vulkano/src/device/physical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
SurfaceApi, SurfaceCapabilities, SurfaceInfo,
},
sync::{ExternalSemaphoreInfo, ExternalSemaphoreProperties, PipelineStage},
DeviceSize, Error, OomError, Version, VulkanObject,
DeviceSize, Error, OomError, Success, Version, VulkanObject,
};
use std::{error, ffi::CStr, fmt, hash::Hash, mem::MaybeUninit, ptr, sync::Arc};

Expand All @@ -41,22 +41,27 @@ pub(crate) fn init_physical_devices(
let fns = instance.fns();
let instance_extensions = instance.enabled_extensions();

let handles: Vec<ash::vk::PhysicalDevice> = unsafe {
let mut num = 0;
check_errors((fns.v1_0.enumerate_physical_devices)(
instance.internal_object(),
&mut num,
ptr::null_mut(),
))?;
let handles = unsafe {
loop {
let mut count = 0;
check_errors((fns.v1_0.enumerate_physical_devices)(
instance.internal_object(),
&mut count,
ptr::null_mut(),
))?;

let mut handles = Vec::with_capacity(num as usize);
check_errors((fns.v1_0.enumerate_physical_devices)(
instance.internal_object(),
&mut num,
handles.as_mut_ptr(),
))?;
handles.set_len(num as usize);
handles
let mut handles = Vec::with_capacity(count as usize);
let result = check_errors((fns.v1_0.enumerate_physical_devices)(
instance.internal_object(),
&mut count,
handles.as_mut_ptr(),
))?;

if !matches!(result, Success::Incomplete) {
handles.set_len(count as usize);
break handles;
}
}
};

Ok(handles
Expand All @@ -70,24 +75,29 @@ pub(crate) fn init_physical_devices(
std::cmp::min(instance.max_api_version(), api_version)
};

let extension_properties: Vec<ash::vk::ExtensionProperties> = unsafe {
let mut num = 0;
check_errors((fns.v1_0.enumerate_device_extension_properties)(
handle,
ptr::null(),
&mut num,
ptr::null_mut(),
))?;

let mut properties = Vec::with_capacity(num as usize);
check_errors((fns.v1_0.enumerate_device_extension_properties)(
handle,
ptr::null(),
&mut num,
properties.as_mut_ptr(),
))?;
properties.set_len(num as usize);
properties
let extension_properties = unsafe {
loop {
let mut count = 0;
check_errors((fns.v1_0.enumerate_device_extension_properties)(
handle,
ptr::null(),
&mut count,
ptr::null_mut(),
))?;

let mut properties = Vec::with_capacity(count as usize);
let result = check_errors((fns.v1_0.enumerate_device_extension_properties)(
handle,
ptr::null(),
&mut count,
properties.as_mut_ptr(),
))?;

if !matches!(result, Success::Incomplete) {
properties.set_len(count as usize);
break properties;
}
}
};

let supported_extensions = DeviceExtensions::from(
Expand Down Expand Up @@ -1163,31 +1173,37 @@ impl<'a> PhysicalDevice<'a> {
surface_full_screen_exclusive_win32_info as *const _ as *const _;
}

let mut surface_format2s;

unsafe {
let fns = self.instance.fns();

let mut num = 0;
check_errors((fns
.khr_get_surface_capabilities2
.get_physical_device_surface_formats2_khr)(
self.internal_object(),
&surface_info2,
&mut num,
ptr::null_mut(),
))?;
let fns = self.instance.fns();

surface_format2s = vec![ash::vk::SurfaceFormat2KHR::default(); num as usize];
check_errors((fns
.khr_get_surface_capabilities2
.get_physical_device_surface_formats2_khr)(
self.internal_object(),
&surface_info2,
&mut num,
surface_format2s.as_mut_ptr(),
))?;
}
let surface_format2s = unsafe {
loop {
let mut count = 0;
check_errors((fns
.khr_get_surface_capabilities2
.get_physical_device_surface_formats2_khr)(
self.internal_object(),
&surface_info2,
&mut count,
ptr::null_mut(),
))?;

let mut surface_format2s =
vec![ash::vk::SurfaceFormat2KHR::default(); count as usize];
let result = check_errors((fns
.khr_get_surface_capabilities2
.get_physical_device_surface_formats2_khr)(
self.internal_object(),
&surface_info2,
&mut count,
surface_format2s.as_mut_ptr(),
))?;

if !matches!(result, Success::Incomplete) {
surface_format2s.set_len(count as usize);
break surface_format2s;
}
}
};

Ok(surface_format2s
.into_iter()
Expand All @@ -1201,28 +1217,33 @@ impl<'a> PhysicalDevice<'a> {
return Ok(Vec::new());
}

let mut surface_formats;

unsafe {
let fns = self.instance.fns();

let mut num = 0;
check_errors((fns.khr_surface.get_physical_device_surface_formats_khr)(
self.internal_object(),
surface.internal_object(),
&mut num,
ptr::null_mut(),
))?;
let fns = self.instance.fns();

surface_formats = Vec::with_capacity(num as usize);
check_errors((fns.khr_surface.get_physical_device_surface_formats_khr)(
self.internal_object(),
surface.internal_object(),
&mut num,
surface_formats.as_mut_ptr(),
))?;
surface_formats.set_len(num as usize);
}
let surface_formats = unsafe {
loop {
let mut count = 0;
check_errors((fns.khr_surface.get_physical_device_surface_formats_khr)(
self.internal_object(),
surface.internal_object(),
&mut count,
ptr::null_mut(),
))?;

let mut surface_formats = Vec::with_capacity(count as usize);
let result =
check_errors((fns.khr_surface.get_physical_device_surface_formats_khr)(
self.internal_object(),
surface.internal_object(),
&mut count,
surface_formats.as_mut_ptr(),
))?;

if !matches!(result, Success::Incomplete) {
surface_formats.set_len(count as usize);
break surface_formats;
}
}
};

Ok(surface_formats
.into_iter()
Expand All @@ -1248,30 +1269,35 @@ impl<'a> PhysicalDevice<'a> {
surface.instance().internal_object(),
);

let fns = self.instance.fns();

let modes = unsafe {
let fns = self.instance.fns();
loop {
let mut count = 0;
check_errors((fns
.khr_surface
.get_physical_device_surface_present_modes_khr)(
self.internal_object(),
surface.internal_object(),
&mut count,
ptr::null_mut(),
))?;

let mut num = 0;
check_errors((fns
.khr_surface
.get_physical_device_surface_present_modes_khr)(
self.internal_object(),
surface.internal_object(),
&mut num,
ptr::null_mut(),
))?;
let mut modes = Vec::with_capacity(count as usize);
let result = check_errors((fns
.khr_surface
.get_physical_device_surface_present_modes_khr)(
self.internal_object(),
surface.internal_object(),
&mut count,
modes.as_mut_ptr(),
))?;

let mut modes = Vec::with_capacity(num as usize);
check_errors((fns
.khr_surface
.get_physical_device_surface_present_modes_khr)(
self.internal_object(),
surface.internal_object(),
&mut num,
modes.as_mut_ptr(),
))?;
modes.set_len(num as usize);
modes
if !matches!(result, Success::Incomplete) {
modes.set_len(count as usize);
break modes;
}
}
};

debug_assert!(modes.len() > 0);
Expand Down
63 changes: 36 additions & 27 deletions vulkano/src/instance/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

use crate::check_errors;
pub use crate::extensions::{
ExtensionRestriction, ExtensionRestrictionError, OneOfRequirements, SupportedExtensionsError,
use super::{loader, LoadingError};
use crate::{check_errors, Success};
pub use crate::{
extensions::{
ExtensionRestriction, ExtensionRestrictionError, OneOfRequirements,
SupportedExtensionsError,
},
Version,
};
use std::{
ffi::{CStr, CString},
fmt::Formatter,
ptr,
};
use crate::instance::loader;
use crate::instance::loader::LoadingError;
use crate::Version;
use std::ffi::{CStr, CString};
use std::fmt::Formatter;
use std::ptr;

// Generated by build.rs
include!(concat!(env!("OUT_DIR"), "/instance_extensions.rs"));
Expand Down Expand Up @@ -59,27 +63,32 @@ impl InstanceExtensions {
{
let fns = ptrs.fns();

let properties: Vec<ash::vk::ExtensionProperties> = unsafe {
let mut num = 0;
check_errors((fns.v1_0.enumerate_instance_extension_properties)(
ptr::null(),
&mut num,
ptr::null_mut(),
))?;
let extension_properties = unsafe {
loop {
let mut count = 0;
check_errors((fns.v1_0.enumerate_instance_extension_properties)(
ptr::null(),
&mut count,
ptr::null_mut(),
))?;

let mut properties = Vec::with_capacity(count as usize);
let result = check_errors((fns.v1_0.enumerate_instance_extension_properties)(
ptr::null(),
&mut count,
properties.as_mut_ptr(),
))?;

let mut properties = Vec::with_capacity(num as usize);
check_errors((fns.v1_0.enumerate_instance_extension_properties)(
ptr::null(),
&mut num,
properties.as_mut_ptr(),
))?;
properties.set_len(num as usize);
properties
if !matches!(result, Success::Incomplete) {
properties.set_len(count as usize);
break properties;
}
}
};

Ok(Self::from(properties.iter().map(|property| unsafe {
CStr::from_ptr(property.extension_name.as_ptr())
})))
Ok(Self::from(extension_properties.iter().map(
|property| unsafe { CStr::from_ptr(property.extension_name.as_ptr()) },
)))
}
}

Expand Down
Loading

0 comments on commit 315b1b9

Please # to comment.