Skip to content

Commit

Permalink
Add no_std support
Browse files Browse the repository at this point in the history
  • Loading branch information
i509VCB committed Nov 24, 2022
1 parent 69c56ea commit 170ae59
Show file tree
Hide file tree
Showing 85 changed files with 6,359 additions and 7,117 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ jobs:
command: check
args: -p ash -p ash-window --all-features

check_no_std_msrv:
name: Check MSRV (1.64 and no_std)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: dtolnay/rust-toolchain@1.64.0
- uses: actions-rs/cargo@v1
with:
command: check
args: -p=ash --no-default-features --features=debug,linked

generated:
name: Generated
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - ReleaseDate

### Added

- Added new default `std` feature. Disabling this feature makes ash no_std capable and raises the MSRV to 1.64 (#664)

### Changed

- Replaced builders with lifetimes/setters directly on Vulkan structs (#602)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ The default `loaded` cargo feature will dynamically load the default Vulkan libr

If, on the other hand, your application cannot handle Vulkan being missing at runtime, you can instead enable the `linked` feature, which will link your binary with the Vulkan loader directly and expose the infallible `Entry::linked`.

### Use in `no_std` environments

Ash can be used in `no_std` environments (with `alloc`) by disabling the default `std` feature. This [raises the MSRV to 1.64](https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html#c-compatible-ffi-types-in-core-and-alloc).

## Example
You can find the examples [here](https://github.com/MaikKlein/ash/tree/master/examples).
All examples currently require: the LunarG Validation layers and a Vulkan library that is visible in your `PATH`. An easy way to get started is to use the [LunarG Vulkan SDK](https://lunarg.com/vulkan-sdk/)
Expand Down
4 changes: 2 additions & 2 deletions ash-window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ workspace = ".."
rust-version = "1.59.0"

[dependencies]
ash = { path = "../ash", version = "0.37", default-features = false }
ash = { path = "../ash", version = "0.37", features = ["std"], default-features = false }
raw-window-handle = "0.5"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
Expand All @@ -27,4 +27,4 @@ ash = { path = "../ash", version = "0.37", default-features = false, features =

[[example]]
name = "winit"
required-features = ["ash/linked"]
required-features = ["ash/linked", "ash/std"]
6 changes: 4 additions & 2 deletions ash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ rust-version = "1.59.0"
libloading = { version = "0.7", optional = true }

[features]
default = ["loaded", "debug"]
default = ["loaded", "debug", "std"]
# Whether ash depends on the standard library. Disabling this raises the MSRV to 1.64.
std = []
# Link the Vulkan loader at compile time.
linked = []
# Support searching for the Vulkan loader manually at runtime.
loaded = ["libloading"]
loaded = ["std", "libloading"]
# Whether Vulkan structs should implement Debug.
debug = []

Expand Down
19 changes: 12 additions & 7 deletions ash/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
use crate::prelude::*;
use crate::vk;
use crate::RawPtr;
use std::mem;
use std::os::raw::c_void;
use std::ptr;
use core::mem;
use core::ptr;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(not(feature = "std"))]
use core::ffi::{c_void, CStr};
#[cfg(feature = "std")]
use std::ffi::{c_void, CStr};

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VkDevice.html>
#[derive(Clone)]
Expand All @@ -19,9 +25,8 @@ pub struct Device {

impl Device {
pub unsafe fn load(instance_fn: &vk::InstanceFnV1_0, device: vk::Device) -> Self {
let load_fn = |name: &std::ffi::CStr| {
mem::transmute((instance_fn.get_device_proc_addr)(device, name.as_ptr()))
};
let load_fn =
|name: &CStr| mem::transmute((instance_fn.get_device_proc_addr)(device, name.as_ptr()));

Self {
handle: device,
Expand Down Expand Up @@ -494,7 +499,7 @@ impl Device {
self.handle,
create_info,
&mut count,
std::ptr::null_mut(),
ptr::null_mut(),
);
count as usize
}
Expand Down
39 changes: 22 additions & 17 deletions ash/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ use crate::instance::Instance;
use crate::prelude::*;
use crate::vk;
use crate::RawPtr;
use core::mem;
use core::ptr;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(not(feature = "std"))]
use core::ffi::{c_char, c_void, CStr};
#[cfg(feature = "std")]
use std::ffi::CStr;
#[cfg(feature = "std")]
use std::os::raw::{c_char, c_void};

#[cfg(feature = "loaded")]
use libloading::Library;
#[cfg(feature = "loaded")]
use std::ffi::OsStr;
use std::mem;
use std::os::raw::c_char;
use std::os::raw::c_void;
use std::ptr;
#[cfg(feature = "loaded")]
use std::sync::Arc;

#[cfg(feature = "loaded")]
use libloading::Library;

/// Holds the Vulkan functions independent of a particular instance
#[derive(Clone)]
pub struct Entry {
Expand Down Expand Up @@ -88,7 +94,7 @@ impl Entry {
///
/// ```no_run
/// use ash::{vk, Entry};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # fn main() -> Result<(), vk::Result> {
/// let entry = Entry::linked();
/// let app_info = vk::ApplicationInfo {
/// api_version: vk::make_api_version(0, 1, 0, 0),
Expand Down Expand Up @@ -143,7 +149,7 @@ impl Entry {
/// `static_fn` must contain valid function pointers that comply with the semantics specified by
/// Vulkan 1.0, which must remain valid for at least the lifetime of the returned [`Entry`].
pub unsafe fn from_static_fn(static_fn: vk::StaticFn) -> Self {
let load_fn = |name: &std::ffi::CStr| {
let load_fn = |name: &CStr| {
mem::transmute((static_fn.get_instance_proc_addr)(
vk::Instance::null(),
name.as_ptr(),
Expand Down Expand Up @@ -178,7 +184,7 @@ impl Entry {
/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkEnumerateInstanceVersion.html>
/// ```no_run
/// # use ash::{Entry, vk};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # fn main() -> Result<(), vk::Result> {
/// let entry = Entry::linked();
/// match entry.try_enumerate_instance_version()? {
/// // Vulkan 1.1+
Expand All @@ -197,9 +203,7 @@ impl Entry {
unsafe {
let mut api_version = 0;
let enumerate_instance_version: Option<vk::PFN_vkEnumerateInstanceVersion> = {
let name = ::std::ffi::CStr::from_bytes_with_nul_unchecked(
b"vkEnumerateInstanceVersion\0",
);
let name = CStr::from_bytes_with_nul_unchecked(b"vkEnumerateInstanceVersion\0");
mem::transmute((self.static_fn.get_instance_proc_addr)(
vk::Instance::null(),
name.as_ptr(),
Expand Down Expand Up @@ -326,7 +330,7 @@ impl Default for Entry {
impl vk::StaticFn {
pub fn load_checked<F>(mut _f: F) -> Result<Self, MissingEntryPoint>
where
F: FnMut(&::std::ffi::CStr) -> *const c_void,
F: FnMut(&CStr) -> *const c_void,
{
// TODO: Make this a &'static CStr once CStr::from_bytes_with_nul_unchecked is const
static ENTRY_POINT: &[u8] = b"vkGetInstanceProcAddr\0";
Expand All @@ -338,7 +342,7 @@ impl vk::StaticFn {
if val.is_null() {
return Err(MissingEntryPoint);
} else {
::std::mem::transmute(val)
::core::mem::transmute(val)
}
},
})
Expand All @@ -347,11 +351,12 @@ impl vk::StaticFn {

#[derive(Clone, Debug)]
pub struct MissingEntryPoint;
impl std::fmt::Display for MissingEntryPoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
impl core::fmt::Display for MissingEntryPoint {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Cannot load `vkGetInstanceProcAddr` symbol from library")
}
}
#[cfg(feature = "std")]
impl std::error::Error for MissingEntryPoint {}

#[cfg(feature = "linked")]
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/acquire_drm_display.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude::*;
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Entry, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_EXT_acquire_drm_display.html>
#[derive(Clone)]
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/buffer_device_address.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

#[derive(Clone)]
pub struct BufferDeviceAddress {
Expand Down
6 changes: 4 additions & 2 deletions ash/src/extensions/ext/calibrated_timestamps.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::prelude::*;
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Entry, Instance};
use std::ffi::CStr;
use std::mem;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::mem;

#[derive(Clone)]
pub struct CalibratedTimestamps {
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/debug_marker.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude::*;
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

#[derive(Clone)]
pub struct DebugMarker {
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/debug_report.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::prelude::*;
use crate::prelude_internal::CStr;
use crate::vk;
use crate::RawPtr;
use crate::{Entry, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

#[derive(Clone)]
pub struct DebugReport {
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/debug_utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude::*;
use crate::prelude_internal::CStr;
use crate::{vk, RawPtr};
use crate::{Entry, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

#[derive(Clone)]
pub struct DebugUtils {
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/descriptor_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude::*;
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_EXT_descriptor_buffer.html>
#[derive(Clone)]
Expand Down
6 changes: 3 additions & 3 deletions ash/src/extensions/ext/extended_dynamic_state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use std::ptr;
use core::mem;
use core::ptr;

#[derive(Clone)]
pub struct ExtendedDynamicState {
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/extended_dynamic_state2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_EXT_extended_dynamic_state2.html>
#[derive(Clone)]
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/extended_dynamic_state3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

/// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_EXT_extended_dynamic_state3.html>
#[derive(Clone)]
Expand Down
6 changes: 4 additions & 2 deletions ash/src/extensions/ext/full_screen_exclusive.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::prelude::*;
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::mem;

#[derive(Clone)]
pub struct FullScreenExclusive {
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/headless_surface.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::prelude::*;
use crate::prelude_internal::CStr;
use crate::vk;
use crate::RawPtr;
use crate::{Entry, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_EXT_headless_surface.html>
#[derive(Clone)]
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/image_compression_control.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_EXT_image_compression_control.html>
#[derive(Clone)]
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/image_drm_format_modifier.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude::*;
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_EXT_image_drm_format_modifier.html>
#[derive(Clone)]
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/mesh_shader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude_internal::CStr;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

/// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_EXT_mesh_shader.html>
#[derive(Clone)]
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/ext/metal_surface.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::prelude::*;
use crate::prelude_internal::CStr;
use crate::vk;
use crate::RawPtr;
use crate::{Entry, Instance};
use std::ffi::CStr;
use std::mem;
use core::mem;

#[derive(Clone)]
pub struct MetalSurface {
Expand Down
Loading

0 comments on commit 170ae59

Please sign in to comment.