Skip to content

Commit c2760e1

Browse files
On allocate_exportable_memory renamed a parameter as external_memory_type_flags for Hal and all the backends.
Renamed `allocate_exportable_memory` to `allocate_external_memory`. Changed the api: merged the functions for external memory allocation and external buffer creation. Changed the import memory function so that now it return both buffer and memory. Moved some general definitions to src/auxil/external-memory. Unified the platform specific export functions into one that is valid for all the platforms. Included the functionalities of `get_external_memory_mask` in `import_external_buffer` so that it can be removed from the API. Overall greatly reduced the number of support structures and reused them where possible.
1 parent 7a282fa commit c2760e1

File tree

27 files changed

+1085
-1196
lines changed

27 files changed

+1085
-1196
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
members = [
44
"src/auxil/auxil",
5+
"src/auxil/external-memory",
56
"src/auxil/range-alloc",
67
"src/auxil/renderdoc",
78
"src/backend/dx11",

src/auxil/external-memory/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "external-memory"
3+
version = "0.0.1"
4+
description = "Generic external memory structures used by gfx-rs backends"
5+
homepage = "https://github.com/gfx-rs/gfx"
6+
repository = "https://github.com/gfx-rs/gfx"
7+
keywords = ["external-memory"]
8+
license = "MIT OR Apache-2.0"
9+
authors = ["The Gfx-rs Developers"]
10+
categories = ["external-memory"]
11+
edition = "2018"
12+
13+
[lib]
14+
name = "external_memory"

src/auxil/external-memory/src/fd.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// Unix file descriptor
2+
#[derive(Debug)]
3+
pub struct Fd(i32);
4+
impl From<i32> for Fd {
5+
fn from(fd: i32) -> Self {
6+
Self(fd)
7+
}
8+
}
9+
impl std::os::unix::io::AsRawFd for Fd {
10+
fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
11+
self.0
12+
}
13+
}
14+
impl std::ops::Deref for Fd {
15+
type Target = i32;
16+
fn deref(&self) -> &Self::Target {
17+
&self.0
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// Windows handle
2+
#[derive(Debug)]
3+
pub struct Handle(*mut std::ffi::c_void);
4+
impl From<*mut std::ffi::c_void> for Handle {
5+
fn from(ptr: *mut std::ffi::c_void) -> Self {
6+
Self(ptr)
7+
}
8+
}
9+
impl std::os::windows::io::AsRawHandle for Handle {
10+
fn as_raw_handle(&self) -> std::os::windows::raw::HANDLE {
11+
self.0
12+
}
13+
}
14+
impl std::ops::Deref for Handle {
15+
type Target = *mut std::ffi::c_void;
16+
fn deref(&self) -> &Self::Target {
17+
&self.0
18+
}
19+
}
20+

src/auxil/external-memory/src/lib.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#[cfg(any(unix))]
2+
#[cfg_attr(feature = "unstable", doc(cfg(unix)))]
3+
mod fd;
4+
#[cfg(any(unix))]
5+
#[cfg_attr(feature = "unstable", doc(cfg(unix)))]
6+
pub use fd::*;
7+
8+
#[cfg(any(windows))]
9+
#[cfg_attr(feature = "unstable", doc(cfg(windows)))]
10+
mod handle;
11+
#[cfg(any(windows))]
12+
#[cfg_attr(feature = "unstable", doc(cfg(windows)))]
13+
pub use handle::*;
14+
15+
mod ptr;
16+
pub use ptr::*;
17+
18+
pub enum PlatformMemory {
19+
#[cfg(any(unix))]
20+
#[cfg_attr(feature = "unstable", doc(cfg(unix)))]
21+
Fd(Fd),
22+
#[cfg(any(windows))]
23+
#[cfg_attr(feature = "unstable", doc(cfg(windows)))]
24+
Handle(Handle),
25+
Ptr(Ptr),
26+
}
27+
28+
#[cfg(any(unix))]
29+
#[cfg_attr(feature = "unstable", doc(cfg(unix)))]
30+
impl From<Fd> for PlatformMemory {
31+
fn from(fd: Fd) -> Self {
32+
Self::Fd(fd)
33+
}
34+
}
35+
36+
#[cfg(any(windows))]
37+
#[cfg_attr(feature = "unstable", doc(cfg(windows)))]
38+
impl From<Handle> for PlatformMemory {
39+
fn from(handle: Handle) -> Self {
40+
Self::Handle(handle)
41+
}
42+
}
43+
44+
impl From<Ptr> for PlatformMemory {
45+
fn from(ptr: Ptr) -> Self {
46+
Self::Ptr(ptr)
47+
}
48+
}
49+
50+
pub enum PlatformMemoryType {
51+
#[cfg(any(unix))]
52+
#[cfg_attr(feature = "unstable", doc(cfg(unix)))]
53+
Fd,
54+
#[cfg(any(windows))]
55+
#[cfg_attr(feature = "unstable", doc(cfg(windows)))]
56+
Handle,
57+
Ptr,
58+
}

src/auxil/external-memory/src/ptr.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// Pointer to a host allocated memory
2+
#[derive(Debug)]
3+
pub struct Ptr(*mut std::ffi::c_void);
4+
impl Ptr {
5+
/// Get the inner ptr
6+
pub fn as_raw_ptr(&self) -> *mut std::ffi::c_void {
7+
self.0
8+
}
9+
}
10+
impl<T> From<*mut T> for Ptr {
11+
fn from(ptr: *mut T) -> Self {
12+
Self(unsafe { std::mem::transmute(ptr) })
13+
}
14+
}
15+
impl std::ops::Deref for Ptr {
16+
type Target = *mut std::ffi::c_void;
17+
fn deref(&self) -> &Self::Target {
18+
&self.0
19+
}
20+
}

src/backend/dx11/src/device.rs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,61 +2486,43 @@ impl device::Device<Backend> for Device {
24862486
unimplemented!()
24872487
}
24882488

2489-
unsafe fn create_external_buffer(
2489+
unsafe fn create_allocate_external_buffer(
24902490
&self,
2491-
external_memory_types: hal::external_memory::ExternalMemoryTypeFlags,
2492-
usage: hal::buffer::Usage,
2493-
sparse: hal::memory::SparseFlags,
2494-
size: u64,
2495-
) -> Result<Buffer, hal::external_memory::ExternalBufferCreateError> {
2491+
_external_memory_types: hal::external_memory::ExternalMemoryTypeFlags,
2492+
_usage: hal::buffer::Usage,
2493+
_sparse: hal::memory::SparseFlags,
2494+
_mem_types: Vec<hal::MemoryTypeId>,
2495+
_size: u64,
2496+
) -> Result<(Buffer, Memory), hal::external_memory::ExternalBufferCreateAllocateError> {
24962497
unimplemented!()
24972498
}
24982499

2499-
unsafe fn allocate_exportable_memory(
2500+
unsafe fn import_external_buffer(
25002501
&self,
2501-
external_memory_types: hal::external_memory::ExternalMemoryTypeFlags,
2502-
dedicated_allocation: Option<hal::external_memory::BufferOrImage<Backend>>,
2503-
mem_type: hal::MemoryTypeId,
2504-
size: u64,
2505-
) -> Result<Memory, hal::external_memory::ExternalMemoryAllocateError> {
2502+
_external_memory: hal::external_memory::ExternalMemory,
2503+
_usage: hal::buffer::Usage,
2504+
_sparse: hal::memory::SparseFlags,
2505+
_mem_types: Vec<hal::MemoryTypeId>,
2506+
_size: u64,
2507+
) -> Result<(Buffer, Memory), hal::external_memory::ExternalBufferImportError> {
25062508
unimplemented!()
25072509
}
25082510

2509-
unsafe fn import_external_memory(
2511+
unsafe fn export_memory(
25102512
&self,
2511-
external_memory: hal::external_memory::ExternalMemory,
2512-
dedicated_allocation: Option<hal::external_memory::BufferOrImage<Backend>>,
2513-
mem_type: hal::MemoryTypeId,
2514-
) -> Result<Memory, hal::external_memory::ExternalMemoryAllocateError> {
2515-
unimplemented!()
2516-
}
2517-
2518-
#[cfg(unix)]
2519-
unsafe fn export_memory_as_fd(
2520-
&self,
2521-
external_memory_type: hal::external_memory::ExternalMemoryFdType,
2522-
memory: &Memory,
2523-
) -> Result<std::os::unix::io::RawFd, hal::external_memory::ExternalMemoryExportError> {
2513+
_external_memory_type: hal::external_memory::ExternalMemoryType,
2514+
_memory: &Memory,
2515+
) -> Result<hal::external_memory::ExternalMemory, hal::external_memory::ExternalMemoryExportError> {
25242516
unimplemented!()
25252517
}
2526-
2527-
#[cfg(windows)]
2528-
unsafe fn export_memory_as_handle(
2529-
&self,
2530-
external_memory_type: hal::external_memory::ExternalMemoryHandleType,
2531-
memory: &Memory,
2532-
) -> Result<std::os::windows::raw::HANDLE, hal::external_memory::ExternalMemoryExportError>
2533-
{
2534-
unimplemented!()
2535-
}
2536-
2518+
/*
25372519
unsafe fn get_external_memory_mask(
25382520
&self,
2539-
external_memory: &hal::external_memory::ExternalMemory,
2521+
_external_memory: &hal::external_memory::ExternalMemory,
25402522
) -> Result<u32, hal::external_memory::ExternalMemoryError> {
25412523
unimplemented!()
25422524
}
2543-
2525+
*/
25442526
fn start_capture(&self) {
25452527
unsafe {
25462528
self.render_doc

src/backend/dx11/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,9 @@ impl adapter::PhysicalDevice<Backend> for PhysicalDevice {
979979

980980
fn query_external_buffer_properties(
981981
&self,
982-
usage: hal::buffer::Usage,
983-
sparse: hal::memory::SparseFlags,
984-
memory_type: hal::external_memory::ExternalMemoryType,
982+
_usage: hal::buffer::Usage,
983+
_sparse: hal::memory::SparseFlags,
984+
_memory_type: hal::external_memory::ExternalMemoryType,
985985
) -> Result<
986986
hal::external_memory::ExternalBufferProperties,
987987
hal::external_memory::ExternalMemoryQueryError,

src/backend/dx12/src/device.rs

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3801,61 +3801,44 @@ impl d::Device<B> for Device {
38013801
unimplemented!()
38023802
}
38033803

3804-
unsafe fn create_external_buffer(
3804+
unsafe fn create_allocate_external_buffer(
38053805
&self,
3806-
external_memory_types: hal::external_memory::ExternalMemoryTypeFlags,
3807-
usage: hal::buffer::Usage,
3808-
sparse: hal::memory::SparseFlags,
3809-
size: u64,
3810-
) -> Result<r::Buffer, hal::external_memory::ExternalBufferCreateError> {
3811-
unimplemented!()
3812-
}
3813-
3814-
unsafe fn allocate_exportable_memory(
3815-
&self,
3816-
external_memory_types: hal::external_memory::ExternalMemoryTypeFlags,
3817-
dedicated_allocation: Option<hal::external_memory::BufferOrImage<B>>,
3818-
mem_type: hal::MemoryTypeId,
3819-
size: u64,
3820-
) -> Result<r::Memory, hal::external_memory::ExternalMemoryAllocateError> {
3821-
unimplemented!()
3822-
}
3823-
3824-
unsafe fn import_external_memory(
3825-
&self,
3826-
external_memory: hal::external_memory::ExternalMemory,
3827-
dedicated_allocation: Option<hal::external_memory::BufferOrImage<B>>,
3828-
mem_type: hal::MemoryTypeId,
3829-
) -> Result<r::Memory, hal::external_memory::ExternalMemoryAllocateError> {
3806+
_external_memory_types: hal::external_memory::ExternalMemoryTypeFlags,
3807+
_usage: hal::buffer::Usage,
3808+
_sparse: hal::memory::SparseFlags,
3809+
_mem_types: Vec<hal::MemoryTypeId>,
3810+
_size: u64,
3811+
) -> Result<(r::Buffer, r::Memory), hal::external_memory::ExternalBufferCreateAllocateError>
3812+
{
38303813
unimplemented!()
38313814
}
38323815

3833-
#[cfg(unix)]
3834-
unsafe fn export_memory_as_fd(
3816+
unsafe fn import_external_buffer(
38353817
&self,
3836-
external_memory_type: hal::external_memory::ExternalMemoryFdType,
3837-
memory: &r::Memory,
3838-
) -> Result<std::os::unix::io::RawFd, hal::external_memory::ExternalMemoryExportError> {
3818+
_external_memory: hal::external_memory::ExternalMemory,
3819+
_usage: hal::buffer::Usage,
3820+
_sparse: hal::memory::SparseFlags,
3821+
_mem_types: Vec<hal::MemoryTypeId>,
3822+
_size: u64,
3823+
) -> Result<(r::Buffer, r::Memory), hal::external_memory::ExternalBufferImportError> {
38393824
unimplemented!()
38403825
}
38413826

3842-
#[cfg(windows)]
3843-
unsafe fn export_memory_as_handle(
3827+
unsafe fn export_memory(
38443828
&self,
3845-
external_memory_type: hal::external_memory::ExternalMemoryHandleType,
3846-
memory: &r::Memory,
3847-
) -> Result<std::os::windows::raw::HANDLE, hal::external_memory::ExternalMemoryExportError>
3848-
{
3829+
_external_memory_type: hal::external_memory::ExternalMemoryType,
3830+
_memory: &r::Memory,
3831+
) -> Result<hal::external_memory::ExternalMemory, hal::external_memory::ExternalMemoryExportError> {
38493832
unimplemented!()
38503833
}
3851-
3834+
/*
38523835
unsafe fn get_external_memory_mask(
38533836
&self,
3854-
external_memory: &hal::external_memory::ExternalMemory,
3837+
_external_memory: &hal::external_memory::ExternalMemory,
38553838
) -> Result<u32, hal::external_memory::ExternalMemoryError> {
38563839
unimplemented!()
38573840
}
3858-
3841+
*/
38593842
fn start_capture(&self) {
38603843
unsafe {
38613844
self.render_doc

src/backend/dx12/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,9 @@ impl adapter::PhysicalDevice<Backend> for PhysicalDevice {
444444

445445
fn query_external_buffer_properties(
446446
&self,
447-
usage: hal::buffer::Usage,
448-
sparse: hal::memory::SparseFlags,
449-
memory_type: hal::external_memory::ExternalMemoryType,
447+
_usage: hal::buffer::Usage,
448+
_sparse: hal::memory::SparseFlags,
449+
_memory_type: hal::external_memory::ExternalMemoryType,
450450
) -> Result<
451451
hal::external_memory::ExternalBufferProperties,
452452
hal::external_memory::ExternalMemoryQueryError,

0 commit comments

Comments
 (0)