Skip to content

Commit

Permalink
Improve the code
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaopengli89 committed May 16, 2023
1 parent de12cfc commit eba791f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 78 deletions.
9 changes: 5 additions & 4 deletions wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ impl super::Device {
library: &Arc<d3d12::D3D12Lib>,
dx12_shader_compiler: wgt::Dx12Compiler,
) -> Result<Self, crate::DeviceError> {
let mut mem_allocator = None;
if private_caps.suballocation_supported {
mem_allocator = super::suballocation::create_allocator_wrapper(&raw)?;
}
let mem_allocator = if private_caps.suballocation_supported {
super::suballocation::create_allocator_wrapper(&raw)?
} else {
None
};

let dxc_container = match dx12_shader_compiler {
wgt::Dx12Compiler::Dxc {
Expand Down
88 changes: 14 additions & 74 deletions wgpu-hal/src/dx12/suballocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ pub(crate) use allocation::{
free_buffer_allocation, free_texture_allocation, AllocationWrapper, GpuAllocatorWrapper,
};

#[cfg(not(feature = "windows_rs"))]
use committed as allocation;
#[cfg(feature = "windows_rs")]
use placed as allocation;

// This exists to work around https://github.com/gfx-rs/wgpu/issues/3207
// Currently this will work the older, slower way if the windows_rs feature is disabled,
// and will use the fast path of suballocating buffers and textures using gpu_allocator if
// the windows_rs feature is enabled.

// This is the fast path using gpu_allocator to suballocate buffers and textures.
#[cfg(feature = "windows_rs")]
mod allocation {
mod placed {
use d3d12::WeakPtr;
use parking_lot::Mutex;
use std::ptr;
Expand All @@ -28,9 +33,6 @@ mod allocation {
MemoryLocation,
};

// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_heap_flags
const D3D12_HEAP_FLAG_CREATE_NOT_ZEROED: d3d12_ty::D3D12_HEAP_FLAGS = 0x1000;

#[derive(Debug)]
pub(crate) struct GpuAllocatorWrapper {
pub(crate) allocator: gpu_allocator::d3d12::Allocator,
Expand Down Expand Up @@ -69,44 +71,8 @@ mod allocation {

// It's a workaround for Intel Xe drivers.
if !device.private_caps.suballocation_supported {
let heap_properties = d3d12_ty::D3D12_HEAP_PROPERTIES {
Type: d3d12_ty::D3D12_HEAP_TYPE_CUSTOM,
CPUPageProperty: if is_cpu_read {
d3d12_ty::D3D12_CPU_PAGE_PROPERTY_WRITE_BACK
} else if is_cpu_write {
d3d12_ty::D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE
} else {
d3d12_ty::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE
},
MemoryPoolPreference: match device.private_caps.memory_architecture {
crate::dx12::MemoryArchitecture::NonUnified
if !is_cpu_read && !is_cpu_write =>
{
d3d12_ty::D3D12_MEMORY_POOL_L1
}
_ => d3d12_ty::D3D12_MEMORY_POOL_L0,
},
CreationNodeMask: 0,
VisibleNodeMask: 0,
};

let hr = unsafe {
device.raw.CreateCommittedResource(
&heap_properties,
if device.private_caps.heap_create_not_zeroed {
D3D12_HEAP_FLAG_CREATE_NOT_ZEROED
} else {
d3d12_ty::D3D12_HEAP_FLAG_NONE
},
&raw_desc,
d3d12_ty::D3D12_RESOURCE_STATE_COMMON,
ptr::null(),
&d3d12_ty::ID3D12Resource::uuidof(),
resource.mut_void(),
)
};

return Ok((hr, None));
return super::committed::create_buffer_resource(device, desc, raw_desc, resource)
.map(|(hr, _)| (hr, None));
}

let location = match (is_cpu_read, is_cpu_write) {
Expand Down Expand Up @@ -159,36 +125,8 @@ mod allocation {
) -> Result<(HRESULT, Option<AllocationWrapper>), crate::DeviceError> {
// It's a workaround for Intel Xe drivers.
if !device.private_caps.suballocation_supported {
let heap_properties = d3d12_ty::D3D12_HEAP_PROPERTIES {
Type: d3d12_ty::D3D12_HEAP_TYPE_CUSTOM,
CPUPageProperty: d3d12_ty::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE,
MemoryPoolPreference: match device.private_caps.memory_architecture {
crate::dx12::MemoryArchitecture::NonUnified => d3d12_ty::D3D12_MEMORY_POOL_L1,
crate::dx12::MemoryArchitecture::Unified { .. } => {
d3d12_ty::D3D12_MEMORY_POOL_L0
}
},
CreationNodeMask: 0,
VisibleNodeMask: 0,
};

let hr = unsafe {
device.raw.CreateCommittedResource(
&heap_properties,
if device.private_caps.heap_create_not_zeroed {
D3D12_HEAP_FLAG_CREATE_NOT_ZEROED
} else {
d3d12_ty::D3D12_HEAP_FLAG_NONE
},
&raw_desc,
d3d12_ty::D3D12_RESOURCE_STATE_COMMON,
ptr::null(), // clear value
&d3d12_ty::ID3D12Resource::uuidof(),
resource.mut_void(),
)
};

return Ok((hr, None));
return super::committed::create_texture_resource(device, desc, raw_desc, resource)
.map(|(hr, _)| (hr, None));
}

let location = MemoryLocation::GpuOnly;
Expand Down Expand Up @@ -282,8 +220,7 @@ mod allocation {

// This is the older, slower path where it doesn't suballocate buffers.
// Tracking issue for when it can be removed: https://github.com/gfx-rs/wgpu/issues/3207
#[cfg(not(feature = "windows_rs"))]
mod allocation {
mod committed {
use d3d12::WeakPtr;
use parking_lot::Mutex;
use std::ptr;
Expand All @@ -306,6 +243,7 @@ mod allocation {
#[derive(Debug)]
pub(crate) struct AllocationWrapper {}

#[allow(unused)]
pub(crate) fn create_allocator_wrapper(
_raw: &d3d12::Device,
) -> Result<Option<Mutex<GpuAllocatorWrapper>>, crate::DeviceError> {
Expand Down Expand Up @@ -395,13 +333,15 @@ mod allocation {
Ok((hr, None))
}

#[allow(unused)]
pub(crate) fn free_buffer_allocation(
_allocation: AllocationWrapper,
_allocator: &Mutex<GpuAllocatorWrapper>,
) {
// No-op when not using gpu-allocator
}

#[allow(unused)]
pub(crate) fn free_texture_allocation(
_allocation: AllocationWrapper,
_allocator: &Mutex<GpuAllocatorWrapper>,
Expand Down

0 comments on commit eba791f

Please sign in to comment.