Skip to content

Commit

Permalink
d3d12: Remove now-unneeded unsafe Send/Sync markers for `Allocati…
Browse files Browse the repository at this point in the history
…on` and `MemoryBlock` (#152)

Thanks to windows-rs implementing these unsafe markers for COM objects
because they are [declared `agile`] (and wrapping the raw pointers in
the first place, which are not marked `Send`/`Sync` by the Rust
language) we can now let the compiler auto-implement the marker traits.
This'll benefit safety (the traits require `unsafe` for a reason) in
case we add fields to these structs that aren't `Send`/`Sync` in the
future.

The same should be done for Vulkan at some point, by manually defining
`Send`+`Sync` wrapper types around affected fields.

Also clear out `MemoryType`'s `Debug` implementation which is already
implemented by windows-rs.

[declared `agile`]: microsoft/win32metadata#740
  • Loading branch information
MarijnS95 authored Jan 16, 2023
1 parent d859dab commit 94545e7
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) trait SubAllocatorBase: crate::visualizer::SubAllocatorVisualizer {}
#[cfg(not(feature = "visualizer"))]
pub(crate) trait SubAllocatorBase {}

pub(crate) trait SubAllocator: SubAllocatorBase + std::fmt::Debug {
pub(crate) trait SubAllocator: SubAllocatorBase + std::fmt::Debug + Sync + Send {
fn allocate(
&mut self,
size: u64,
Expand Down
37 changes: 1 addition & 36 deletions src/d3d12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,6 @@ pub struct CommittedAllocationStatistics {
pub total_size: u64,
}

unsafe impl Send for Allocation {}
unsafe impl Sync for Allocation {}

impl Allocation {
pub fn chunk_id(&self) -> Option<std::num::NonZeroU64> {
self.chunk_id
Expand Down Expand Up @@ -363,9 +360,7 @@ impl MemoryBlock {
}
}

unsafe impl Send for MemoryBlock {}
unsafe impl Sync for MemoryBlock {}

#[derive(Debug)]
struct MemoryType {
memory_blocks: Vec<Option<MemoryBlock>>,
committed_allocations: CommittedAllocationStatistics,
Expand All @@ -376,36 +371,6 @@ struct MemoryType {
active_general_blocks: usize,
}

struct HeapPropertiesDebug(D3D12_HEAP_PROPERTIES);

impl std::fmt::Debug for HeapPropertiesDebug {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("D3D12_HEAP_PROPERTIES")
.field("Type", &self.0.Type)
.field("CPUPageProperty", &self.0.CPUPageProperty)
.field("MemoryPoolPreference", &self.0.MemoryPoolPreference)
.field("CreationNodeMask", &self.0.CreationNodeMask)
.field("VisibleNodeMask", &self.0.VisibleNodeMask)
.finish()
}
}

impl std::fmt::Debug for MemoryType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MemoryType")
.field("memory_blocks", &self.memory_blocks)
.field("memory_location", &self.memory_location)
.field("heap_category", &self.heap_category)
.field(
"heap_properties",
&HeapPropertiesDebug(self.heap_properties),
)
.field("memory_type_index", &self.memory_type_index)
.field("active_general_blocks", &self.active_general_blocks)
.finish()
}
}

const DEFAULT_DEVICE_MEMBLOCK_SIZE: u64 = 256 * 1024 * 1024;
const DEFAULT_HOST_MEMBLOCK_SIZE: u64 = 64 * 1024 * 1024;
impl MemoryType {
Expand Down

0 comments on commit 94545e7

Please sign in to comment.