-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tompro
committed
Nov 13, 2024
1 parent
c79d76d
commit fff77c4
Showing
11 changed files
with
173 additions
and
140 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 |
---|---|---|
|
@@ -7,6 +7,7 @@ members = [ | |
"kernel", | ||
"macros", | ||
"src/fs", | ||
"src/graphics", | ||
"src/llt", | ||
"src/obconf", | ||
"src/param", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "graphics" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
thiserror = "2.0.3" | ||
|
||
[target.'cfg(not(target_os = "macos"))'.dependencies] | ||
ash = { version = "0.38.0", features = ["linked", "std"], default-features = false } | ||
|
||
[target.'cfg(target_os = "macos")'.dependencies] | ||
applevisor-sys = "0.1.3" | ||
core-graphics-types = "0.1.3" | ||
metal = "0.29.0" | ||
objc = "0.2.7" |
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,23 @@ | ||
#[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 InitError: core::error::Error; | ||
|
||
fn init() -> Result<Self, Self::InitError>; | ||
|
||
fn enumerate_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,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 InitError = MetalInitError; | ||
|
||
fn init() -> Result<Self, Self::InitError> { | ||
Ok(Self { | ||
devices: Device::all(), | ||
}) | ||
} | ||
|
||
fn enumerate_physical_devices(&self) -> &[Self::PhysicalDevice] { | ||
&self.devices | ||
} | ||
} | ||
|
||
impl super::PhysicalDevice for metal::Device { | ||
fn name(&self) -> &str { | ||
self.name() | ||
} | ||
} | ||
|
||
/// Represents an error when [`Metal::init()`] fails. | ||
#[derive(Debug, Error)] | ||
pub enum MetalInitError {} |
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,87 @@ | ||
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 InitError = VulkanInitError; | ||
|
||
fn init() -> Result<Self, Self::InitError> { | ||
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(VulkanInitError::CreateInstanceFailed)?; | ||
|
||
let devices = unsafe { instance.enumerate_physical_devices() } | ||
.map_err(VulkanInitError::EnumeratePhysicalDevicesFailed)? | ||
.into_iter() | ||
.map(|device| -> Result<VulkanPhysicalDevice, VulkanInitError> { | ||
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(), 256) | ||
}) | ||
.map_err(|_| VulkanInitError::DeviceNameInvalid)? | ||
.to_str() | ||
.map_err(VulkanInitError::DeviceNameInvalidUtf8)? | ||
.to_owned(); | ||
|
||
Ok(VulkanPhysicalDevice { device, name }) | ||
}) | ||
.collect::<Result<_, VulkanInitError>>()?; | ||
|
||
Ok(Self { | ||
entry, | ||
instance, | ||
devices, | ||
}) | ||
} | ||
|
||
fn enumerate_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::init()`] fails. | ||
#[derive(Debug, Error)] | ||
pub enum VulkanInitError { | ||
#[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), | ||
} |
Oops, something went wrong.