Split vulkan resources
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 7m49s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 7m49s
This commit is contained in:
parent
b977f446d3
commit
f585ba78e7
9 changed files with 483 additions and 244 deletions
301
crates/engine_vulkan/src/utils/device.rs
Normal file
301
crates/engine_vulkan/src/utils/device.rs
Normal file
|
@ -0,0 +1,301 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use bevy_ecs::world::World;
|
||||
use engine_window::raw_handle::DisplayHandleWrapper;
|
||||
use vulkano::{
|
||||
command_buffer::allocator::StandardCommandBufferAllocator,
|
||||
descriptor_set::allocator::StandardDescriptorSetAllocator,
|
||||
device::{
|
||||
Device, DeviceCreateInfo, DeviceExtensions, Queue, QueueCreateInfo, QueueFlags,
|
||||
physical::{PhysicalDevice, PhysicalDeviceType},
|
||||
},
|
||||
memory::allocator::StandardMemoryAllocator,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
VulkanCommandBufferAllocator, VulkanComputeQueue, VulkanConfig, VulkanDescriptorSetAllocator,
|
||||
VulkanDevice, VulkanGraphicsQueue, VulkanInstance, VulkanMemoryAllocator, VulkanTransferQueue,
|
||||
};
|
||||
|
||||
pub fn create_and_insert_device(world: &mut World, config: &VulkanConfig) {
|
||||
let picked_device =
|
||||
pick_physical_device(world, &config).expect("Failed to pick physical device");
|
||||
|
||||
let device = picked_device.device;
|
||||
let physical_device = device.physical_device();
|
||||
|
||||
log::debug!("Vulkan device created");
|
||||
log::debug!(
|
||||
"\tPhysical device: {:?} ({:?})",
|
||||
physical_device.properties().device_name,
|
||||
physical_device.properties().device_type
|
||||
);
|
||||
log::debug!("\tDevice extensions: {:?}", device.enabled_extensions());
|
||||
log::debug!("\tDevice features: {:?}", device.enabled_features());
|
||||
|
||||
world.insert_resource(VulkanDevice(device.clone()));
|
||||
|
||||
log::debug!("\tDevice selected queues:");
|
||||
if config.with_graphics_queue {
|
||||
world.insert_resource(VulkanGraphicsQueue(
|
||||
picked_device
|
||||
.graphics_queue
|
||||
.expect("Failed to get graphics queue"),
|
||||
));
|
||||
log::debug!("\t\t- Graphics queue");
|
||||
}
|
||||
|
||||
if config.with_compute_queue {
|
||||
world.insert_resource(VulkanComputeQueue(
|
||||
picked_device
|
||||
.compute_queue
|
||||
.expect("Failed to get compute queue"),
|
||||
));
|
||||
log::debug!("\t\t- Compute queue");
|
||||
}
|
||||
|
||||
if config.with_transfer_queue {
|
||||
world.insert_resource(VulkanTransferQueue(
|
||||
picked_device
|
||||
.transfer_queue
|
||||
.expect("Failed to get transfer queue"),
|
||||
));
|
||||
log::debug!("\t\t- Transfer queue");
|
||||
}
|
||||
|
||||
world.insert_resource(VulkanMemoryAllocator(Arc::new(
|
||||
StandardMemoryAllocator::new_default(device.clone()),
|
||||
)));
|
||||
world.insert_resource(VulkanCommandBufferAllocator(Arc::new(
|
||||
StandardCommandBufferAllocator::new(device.clone(), Default::default()),
|
||||
)));
|
||||
world.insert_resource(VulkanDescriptorSetAllocator(Arc::new(
|
||||
StandardDescriptorSetAllocator::new(device.clone(), Default::default()),
|
||||
)));
|
||||
}
|
||||
|
||||
struct PickedDevice {
|
||||
pub device: Arc<Device>,
|
||||
pub graphics_queue: Option<Arc<Queue>>,
|
||||
pub compute_queue: Option<Arc<Queue>>,
|
||||
pub transfer_queue: Option<Arc<Queue>>,
|
||||
}
|
||||
|
||||
fn pick_physical_device(world: &World, config: &VulkanConfig) -> Option<PickedDevice> {
|
||||
let instance = world
|
||||
.get_resource::<VulkanInstance>()
|
||||
.expect("Failed to get VulkanInstance during vulkan plugin initialization");
|
||||
|
||||
instance
|
||||
.0
|
||||
.enumerate_physical_devices()
|
||||
.expect("Failed to enumerate physical devices")
|
||||
.filter_map(|p| check_physical_device(world, &p, config))
|
||||
.min_by_key(
|
||||
|p| match p.device.physical_device().properties().device_type {
|
||||
PhysicalDeviceType::DiscreteGpu => 0,
|
||||
PhysicalDeviceType::IntegratedGpu => 1,
|
||||
PhysicalDeviceType::VirtualGpu => 2,
|
||||
PhysicalDeviceType::Cpu => 3,
|
||||
PhysicalDeviceType::Other => 4,
|
||||
_ => 5,
|
||||
},
|
||||
)
|
||||
.take()
|
||||
}
|
||||
|
||||
fn check_device_extensions_support(
|
||||
physical_device: &Arc<PhysicalDevice>,
|
||||
config: &VulkanConfig,
|
||||
) -> Option<DeviceExtensions> {
|
||||
let device_extensions = DeviceExtensions {
|
||||
khr_swapchain: config.with_window_surface,
|
||||
..config.device_extensions
|
||||
};
|
||||
|
||||
if physical_device
|
||||
.supported_extensions()
|
||||
.contains(&device_extensions)
|
||||
{
|
||||
log::debug!(
|
||||
"\t\t[OK] Device supports required extensions {:?}",
|
||||
device_extensions
|
||||
);
|
||||
Some(device_extensions)
|
||||
} else {
|
||||
log::debug!(
|
||||
"\t\t[FAILED] Device does not support required extensions {:?}",
|
||||
device_extensions
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
struct PickedQueuesInfo {
|
||||
graphics_queue_family_index: Option<u32>,
|
||||
compute_queue_family_index: Option<u32>,
|
||||
transfer_queue_family_index: Option<u32>,
|
||||
}
|
||||
|
||||
fn check_queues_support(
|
||||
world: &World,
|
||||
physical_device: &Arc<PhysicalDevice>,
|
||||
config: &VulkanConfig,
|
||||
) -> Option<PickedQueuesInfo> {
|
||||
let mut graphics_queue_family_index: Option<u32> = None;
|
||||
let mut compute_queue_family_index: Option<u32> = None;
|
||||
let mut transfer_queue_family_index: Option<u32> = None;
|
||||
|
||||
for (i, queue_family_property) in physical_device.queue_family_properties().iter().enumerate() {
|
||||
if config.with_graphics_queue {
|
||||
let graphics_supported = queue_family_property
|
||||
.queue_flags
|
||||
.intersects(QueueFlags::GRAPHICS);
|
||||
|
||||
let presentation_valid = if config.with_window_surface {
|
||||
let display_handle = world
|
||||
.get_resource::<DisplayHandleWrapper>()
|
||||
.expect("DisplayHandleWrapper must be added before VulkanPlugin");
|
||||
|
||||
physical_device
|
||||
.presentation_support(i as u32, &display_handle.0)
|
||||
.expect("Failed to check presentation support")
|
||||
} else {
|
||||
true
|
||||
};
|
||||
|
||||
if graphics_supported && presentation_valid {
|
||||
graphics_queue_family_index = Some(i as u32);
|
||||
}
|
||||
}
|
||||
|
||||
if config.with_compute_queue {
|
||||
let compute_supported = queue_family_property
|
||||
.queue_flags
|
||||
.intersects(QueueFlags::COMPUTE);
|
||||
|
||||
if compute_supported {
|
||||
compute_queue_family_index = Some(i as u32);
|
||||
}
|
||||
}
|
||||
|
||||
if config.with_transfer_queue {
|
||||
let transfer_supported = queue_family_property
|
||||
.queue_flags
|
||||
.intersects(QueueFlags::TRANSFER);
|
||||
|
||||
if transfer_supported {
|
||||
transfer_queue_family_index = Some(i as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !config.with_graphics_queue {
|
||||
log::debug!("\t\t[SKIPPED] Graphics queue is not required");
|
||||
} else if graphics_queue_family_index.is_some() {
|
||||
log::debug!("\t\t[OK] Graphics queue is supported");
|
||||
} else {
|
||||
log::debug!("\t\t[FAILED] Graphics queue is not supported");
|
||||
return None;
|
||||
}
|
||||
|
||||
if !config.with_compute_queue {
|
||||
log::debug!("\t\t[SKIPPED] Compute queue is not required");
|
||||
} else if compute_queue_family_index.is_some() {
|
||||
log::debug!("\t\t[OK] Compute queue is supported");
|
||||
} else {
|
||||
log::debug!("\t\t[FAILED] Compute queue is not supported");
|
||||
return None;
|
||||
}
|
||||
|
||||
if !config.with_transfer_queue {
|
||||
log::debug!("\t\t[SKIPPED] Transfer queue is not required");
|
||||
} else if transfer_queue_family_index.is_some() {
|
||||
log::debug!("\t\t[OK] Transfer queue is supported");
|
||||
} else {
|
||||
log::debug!("\t\t[FAILED] Transfer queue is not supported");
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(PickedQueuesInfo {
|
||||
graphics_queue_family_index,
|
||||
compute_queue_family_index,
|
||||
transfer_queue_family_index,
|
||||
})
|
||||
}
|
||||
|
||||
fn check_physical_device(
|
||||
world: &World,
|
||||
physical_device: &Arc<PhysicalDevice>,
|
||||
config: &VulkanConfig,
|
||||
) -> Option<PickedDevice> {
|
||||
log::debug!("Checking physical device");
|
||||
log::debug!("\tProperties");
|
||||
log::debug!("\t\tName: {}", physical_device.properties().device_name);
|
||||
log::debug!("\t\tAPI version: {}", physical_device.api_version());
|
||||
log::debug!(
|
||||
"\t\tDevice type: {:?}",
|
||||
physical_device.properties().device_type
|
||||
);
|
||||
log::debug!("\tRequired supports checking report");
|
||||
|
||||
let device_extensions = check_device_extensions_support(physical_device, config)?;
|
||||
let picked_queues_info = check_queues_support(world, physical_device, config)?;
|
||||
|
||||
let mut queue_create_infos = vec![];
|
||||
|
||||
if config.with_graphics_queue {
|
||||
queue_create_infos.push(QueueCreateInfo {
|
||||
queue_family_index: picked_queues_info.graphics_queue_family_index.unwrap(),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
||||
if config.with_compute_queue {
|
||||
queue_create_infos.push(QueueCreateInfo {
|
||||
queue_family_index: picked_queues_info.compute_queue_family_index.unwrap(),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
||||
if config.with_transfer_queue {
|
||||
queue_create_infos.push(QueueCreateInfo {
|
||||
queue_family_index: picked_queues_info.transfer_queue_family_index.unwrap(),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
||||
let (device, mut queues) = Device::new(
|
||||
physical_device.clone(),
|
||||
DeviceCreateInfo {
|
||||
queue_create_infos,
|
||||
enabled_extensions: device_extensions,
|
||||
enabled_features: config.device_features,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.expect("Failed to create device");
|
||||
|
||||
let mut graphics_queue = None;
|
||||
let mut compute_queue = None;
|
||||
let mut transfer_queue = None;
|
||||
|
||||
if config.with_graphics_queue {
|
||||
graphics_queue = queues.next();
|
||||
}
|
||||
|
||||
if config.with_compute_queue {
|
||||
compute_queue = queues.next();
|
||||
}
|
||||
|
||||
if config.with_transfer_queue {
|
||||
transfer_queue = queues.next();
|
||||
}
|
||||
|
||||
Some(PickedDevice {
|
||||
device,
|
||||
graphics_queue,
|
||||
compute_queue,
|
||||
transfer_queue,
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue