Skip to content

Commit

Permalink
Vulkan allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
i509VCB committed Jun 26, 2022
1 parent fad6f20 commit fa43594
Show file tree
Hide file tree
Showing 5 changed files with 899 additions and 2 deletions.
37 changes: 35 additions & 2 deletions examples/vulkan.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use std::sync::Mutex;

use drm_fourcc::{DrmFourcc, DrmModifier};
use slog::{o, Drain};
use smithay::backend::vulkan::{Instance, PhysicalDevice};
use smithay::backend::{
allocator::{
dmabuf::AsDmabuf,
vulkan::{ImageUsageFlags, VulkanAllocator},
Allocator, Buffer,
},
vulkan::{version::Version, Instance, PhysicalDevice},
};

fn main() {
let logger = slog::Logger::root(Mutex::new(slog_term::term_full().fuse()).fuse(), o!());

println!("Version: {}", Instance::max_instance_version().unwrap());
println!(
"Available instance extensions: {:?}",
Instance::enumerate_extensions().unwrap().collect::<Vec<_>>()
Expand All @@ -24,4 +31,30 @@ fn main() {
phy.driver()
);
}

let physical_device = PhysicalDevice::enumerate(&instance)
.unwrap()
.next()
.expect("No physical devices");

// We don't actually use any buffers created by the allocator in this example.
let mut allocator = VulkanAllocator::new(&physical_device, ImageUsageFlags::empty()).unwrap();

let image = allocator
.create_buffer(100, 200, DrmFourcc::Argb8888, &[DrmModifier::Linear])
.expect("create");

assert_eq!(image.width(), 100);
assert_eq!(image.height(), 200);

let image_dmabuf = image.export().expect("Export dmabuf");

drop(image);

let _image2 = allocator
.create_buffer(200, 200, DrmFourcc::Argb8888, &[DrmModifier::Linear])
.expect("create");

drop(allocator);
drop(image_dmabuf);
}
2 changes: 2 additions & 0 deletions src/backend/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub mod dumb;
pub mod format;
#[cfg(feature = "backend_gbm")]
pub mod gbm;
#[cfg(feature = "backend_vulkan")]
pub mod vulkan;

mod swapchain;
use std::{
Expand Down
73 changes: 73 additions & 0 deletions src/backend/allocator/vulkan/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! Format conversions between Vulkan and DRM formats.
/// Macro to generate format conversions between Vulkan and FourCC format codes.
///
/// Any entry in this table may have attributes associated with a conversion. This is needed for `PACK` Vulkan
/// formats which may only have an alternative given a specific host endian.
///
/// See the module documentation for usage details.
macro_rules! vk_format_table {
(
$(
// This meta specifier is used for format conversions for PACK formats.
$(#[$conv_meta:meta])*
$fourcc: ident => $vk: ident
),* $(,)?
) => {
/// Converts a FourCC format code to a Vulkan format code.
///
/// This will return [`None`] if the format is not known.
///
/// These format conversions will return all known FourCC and Vulkan format conversions. However a
/// Vulkan implementation may not support some Vulkan format. One notable example of this are the
/// formats introduced in `VK_EXT_4444_formats`. The corresponding FourCC codes will return the
/// formats from `VK_EXT_4444_formats`, but the caller is responsible for testing that a Vulkan device
/// supports these formats.
pub const fn get_vk_format(fourcc: $crate::backend::allocator::Fourcc) -> Option<ash::vk::Format> {
// FIXME: Use reexport for ash::vk::Format
match fourcc {
$(
$(#[$conv_meta])*
$crate::backend::allocator::Fourcc::$fourcc => Some(ash::vk::Format::$vk),
)*

_ => None,
}
}

/// Returns all the known format conversions.
///
/// The list contains FourCC format codes that may be converted using [`get_vk_format`].
pub const fn known_formats() -> &'static [$crate::backend::allocator::Fourcc] {
&[
$(
$crate::backend::allocator::Fourcc::$fourcc
),*
]
}
};
}

// FIXME: SRGB format is not always correct.
//
// Vulkan classifies formats by both channel sizes and colorspace. FourCC format codes do not classify formats
// based on colorspace.
//
// To implement this correctly, it is likely that parsing vulkan.xml and classifying families of colorspaces
// would be needed since there are a lot of formats.
//
// Many of these conversions come from wsi_common_wayland.c in Mesa
vk_format_table! {
Argb8888 => B8G8R8A8_SRGB,
Xrgb8888 => B8G8R8A8_SRGB,

Abgr8888 => R8G8B8A8_SRGB,
Xbgr8888 => R8G8B8A8_SRGB,

// PACK32 formats are equivalent to u32 instead of [u8; 4] and thus depend their layout depends the host
// endian.
#[cfg(target_endian = "little")]
Rgba8888 => A8B8G8R8_SRGB_PACK32,
#[cfg(target_endian = "little")]
Rgbx8888 => A8B8G8R8_SRGB_PACK32
}
Loading

0 comments on commit fa43594

Please sign in to comment.