-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Passes device list to Slint UI (#1093)
Co-authored-by: tompro <tomas.prochazka@apertia.cz>
- Loading branch information
1 parent
f9c14d6
commit 7f7c542
Showing
9 changed files
with
194 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
use self::buffer::MetalBuffer; | ||
use super::{Screen, ScreenBuffer}; | ||
use crate::vmm::VmmScreen; | ||
use metal::{CAMetalLayer, Device, MetalLayer}; | ||
use objc::runtime::{Object, NO, YES}; | ||
use objc::{msg_send, sel, sel_impl}; | ||
use std::ptr::null_mut; | ||
use std::sync::Arc; | ||
use thiserror::Error; | ||
|
||
pub struct Metal { | ||
devices: Vec<metal::Device>, | ||
} | ||
|
||
impl super::GraphicsApi for Metal { | ||
type PhysicalDevice = metal::Device; | ||
|
||
type CreateError = MetalCreateError; | ||
|
||
fn new() -> Result<Self, Self::CreateError> { | ||
Ok(Self { | ||
devices: Device::all(), | ||
}) | ||
} | ||
|
||
fn physical_devices(&self) -> &[Self::PhysicalDevice] { | ||
&self.devices | ||
} | ||
} | ||
|
||
impl super::PhysicalDevice for metal::Device { | ||
fn name(&self) -> &str { | ||
self.name() | ||
} | ||
} | ||
|
||
/// Represents an error when [`Metal::new()`] fails. | ||
#[derive(Debug, Error)] | ||
pub enum MetalCreateError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
#[cfg_attr(target_os = "macos", path = "metal.rs")] | ||
#[cfg_attr(not(target_os = "macos"), path = "vulkan.rs")] | ||
mod api; | ||
|
||
#[cfg(not(target_os = "macos"))] | ||
pub type DefaultApi = self::api::Vulkan; | ||
|
||
#[cfg(target_os = "macos")] | ||
pub type DefaultApi = self::api::Metal; | ||
|
||
pub trait GraphicsApi: Sized + 'static { | ||
type PhysicalDevice: PhysicalDevice; | ||
|
||
type CreateError: core::error::Error; | ||
|
||
fn new() -> Result<Self, Self::CreateError>; | ||
|
||
fn physical_devices(&self) -> &[Self::PhysicalDevice]; | ||
} | ||
|
||
pub trait PhysicalDevice: Sized { | ||
fn name(&self) -> &str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
use ash::vk::{ApplicationInfo, InstanceCreateInfo}; | ||
use std::ffi::CStr; | ||
use thiserror::Error; | ||
|
||
pub struct Vulkan { | ||
entry: ash::Entry, | ||
instance: ash::Instance, | ||
devices: Vec<VulkanPhysicalDevice>, | ||
} | ||
|
||
impl super::GraphicsApi for Vulkan { | ||
type PhysicalDevice = VulkanPhysicalDevice; | ||
|
||
type CreateError = VulkanCreateError; | ||
|
||
fn new() -> Result<Self, Self::CreateError> { | ||
let entry = ash::Entry::linked(); | ||
|
||
let app_info = ApplicationInfo::default().application_name(c"Obliteration"); | ||
|
||
let create_info = InstanceCreateInfo::default().application_info(&app_info); | ||
|
||
let instance = unsafe { entry.create_instance(&create_info, None) } | ||
.map_err(VulkanCreateError::CreateInstanceFailed)?; | ||
|
||
let devices = unsafe { instance.enumerate_physical_devices() } | ||
.map_err(VulkanCreateError::EnumeratePhysicalDevicesFailed)? | ||
.into_iter() | ||
.map( | ||
|device| -> Result<VulkanPhysicalDevice, VulkanCreateError> { | ||
let properties = unsafe { instance.get_physical_device_properties(device) }; | ||
|
||
let name = CStr::from_bytes_until_nul(unsafe { | ||
std::slice::from_raw_parts( | ||
properties.device_name.as_ptr().cast(), | ||
properties.device_name.len(), | ||
) | ||
}) | ||
.map_err(|_| VulkanCreateError::DeviceNameInvalid)? | ||
.to_str() | ||
.map_err(VulkanCreateError::DeviceNameInvalidUtf8)? | ||
.to_owned(); | ||
|
||
Ok(VulkanPhysicalDevice { device, name }) | ||
}, | ||
) | ||
.collect::<Result<_, VulkanCreateError>>()?; | ||
|
||
Ok(Self { | ||
entry, | ||
instance, | ||
devices, | ||
}) | ||
} | ||
|
||
fn physical_devices(&self) -> &[Self::PhysicalDevice] { | ||
&self.devices | ||
} | ||
} | ||
|
||
impl Drop for Vulkan { | ||
fn drop(&mut self) { | ||
unsafe { self.instance.destroy_instance(None) }; | ||
} | ||
} | ||
|
||
pub struct VulkanPhysicalDevice { | ||
device: ash::vk::PhysicalDevice, | ||
name: String, | ||
} | ||
|
||
impl super::PhysicalDevice for VulkanPhysicalDevice { | ||
fn name(&self) -> &str { | ||
&self.name | ||
} | ||
} | ||
|
||
/// Represents an error when [`Vulkan::new()`] fails. | ||
#[derive(Debug, Error)] | ||
pub enum VulkanCreateError { | ||
#[error("couldn't create Vulkan instance")] | ||
CreateInstanceFailed(#[source] ash::vk::Result), | ||
|
||
#[error("couldn't enumerate physical devices")] | ||
EnumeratePhysicalDevicesFailed(#[source] ash::vk::Result), | ||
|
||
#[error("no null byte in device name")] | ||
DeviceNameInvalid, | ||
|
||
#[error("device name is not valid UTF-8")] | ||
DeviceNameInvalidUtf8(#[source] std::str::Utf8Error), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters