Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Dec 22, 2023
1 parent c97b351 commit e9e6444
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 240 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions wgpu-core/src/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ use thiserror::Error;

pub(crate) use buffer::{BufferBindGroupState, BufferTracker, BufferUsageScope};
use metadata::{ResourceMetadata, ResourceMetadataProvider};
pub(crate) use stateless::{StatelessBindGroupSate, StatelessTracker};
pub(crate) use stateless::{StatelessBindGroupState, StatelessTracker};
pub(crate) use texture::{
TextureBindGroupState, TextureSelector, TextureTracker, TextureUsageScope,
};
Expand Down Expand Up @@ -325,17 +325,17 @@ impl<T: ResourceUses> fmt::Display for InvalidUse<T> {
pub(crate) struct BindGroupStates<A: HalApi> {
pub buffers: BufferBindGroupState<A>,
pub textures: TextureBindGroupState<A>,
pub views: StatelessBindGroupSate<id::TextureViewId, resource::TextureView<A>>,
pub samplers: StatelessBindGroupSate<id::SamplerId, resource::Sampler<A>>,
pub views: StatelessBindGroupState<id::TextureViewId, resource::TextureView<A>>,
pub samplers: StatelessBindGroupState<id::SamplerId, resource::Sampler<A>>,
}

impl<A: HalApi> BindGroupStates<A> {
pub fn new() -> Self {
Self {
buffers: BufferBindGroupState::new(),
textures: TextureBindGroupState::new(),
views: StatelessBindGroupSate::new(),
samplers: StatelessBindGroupSate::new(),
views: StatelessBindGroupState::new(),
samplers: StatelessBindGroupState::new(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/track/stateless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ use super::ResourceTracker;

/// Stores all the resources that a bind group stores.
#[derive(Debug)]
pub(crate) struct StatelessBindGroupSate<Id: TypedId, T: Resource<Id>> {
pub(crate) struct StatelessBindGroupState<Id: TypedId, T: Resource<Id>> {
resources: Mutex<Vec<(Id, Arc<T>)>>,
}

impl<Id: TypedId, T: Resource<Id>> StatelessBindGroupSate<Id, T> {
impl<Id: TypedId, T: Resource<Id>> StatelessBindGroupState<Id, T> {
pub fn new() -> Self {
Self {
resources: Mutex::new(Vec::new()),
Expand Down
12 changes: 12 additions & 0 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dx12 = [
"naga/hlsl-out",
"d3d12",
"bit-set",
"gpu-allocator",
"libloading",
"range-alloc",
"winapi/std",
Expand All @@ -56,6 +57,7 @@ dx12 = [
"winapi/d3d12shader",
"winapi/d3d12sdklayers",
"winapi/dxgi1_6",
"winapi/errhandlingapi",
]
# TODO: This is a separate feature until Mozilla okays windows-rs, see https://github.com/gfx-rs/wgpu/issues/3207 for the tracking issue.
windows_rs = ["gpu-allocator"]
Expand Down Expand Up @@ -130,6 +132,16 @@ winapi = { version = "0.3", features = [
d3d12 = { path = "../d3d12/", version = "0.7.0", optional = true, features = [
"libloading",
] }
windows = { version = "0.48.0", features = [
"Win32_Foundation",
"Win32_Graphics_Direct3D",
"Win32_Graphics_Direct3D12",
"Win32_Graphics_Direct3D_Fxc",
"Win32_Graphics_Dxgi",
"Win32_Graphics_Dxgi_Common",
"Win32_Security",
"Win32_System_Threading",
] }

[target.'cfg(any(target_os="macos", target_os="ios"))'.dependencies]
# backend: Metal
Expand Down
16 changes: 2 additions & 14 deletions wgpu-hal/src/dx12/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ use crate::{
};
use parking_lot::Mutex;
use std::{mem, ptr, sync::Arc, thread};
use winapi::{
shared::{
dxgi, dxgi1_2, dxgiformat::DXGI_FORMAT_B8G8R8A8_UNORM, minwindef::DWORD, windef, winerror,
},
um::{d3d12 as d3d12_ty, d3d12sdklayers, winuser},
};

impl Drop for super::Adapter {
fn drop(&mut self) {
Expand All @@ -29,15 +23,9 @@ impl Drop for super::Adapter {

impl super::Adapter {
pub unsafe fn report_live_objects(&self) {
if let Ok(debug_device) = unsafe {
self.raw
.cast::<d3d12sdklayers::ID3D12DebugDevice>()
.into_result()
} {
if let Ok(debug_device) = unsafe { self.raw.as_debug_device() } {
unsafe {
debug_device.ReportLiveDeviceObjects(
d3d12sdklayers::D3D12_RLDO_SUMMARY | d3d12sdklayers::D3D12_RLDO_IGNORE_INTERNAL,
)
debug_device.report_live_device_objects(RldoFlags::SUMMARY | RldoFlags::INTERNAL)
};
}
}
Expand Down
1 change: 0 additions & 1 deletion wgpu-hal/src/dx12/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::auxil::{self, dxgi::result::HResult as _};

use super::conv;
use std::{mem, ops::Range, ptr};
use winapi::um::d3d12 as d3d12_ty;

fn make_box(origin: &wgt::Origin3d, size: &crate::CopyExtent) -> d3d12_ty::D3D12_BOX {
d3d12_ty::D3D12_BOX {
Expand Down
4 changes: 0 additions & 4 deletions wgpu-hal/src/dx12/conv.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::iter;
use winapi::{
shared::minwindef::BOOL,
um::{d3d12 as d3d12_ty, d3dcommon},
};

pub fn map_buffer_usage_to_resource_flags(
usage: crate::BufferUses,
Expand Down
5 changes: 0 additions & 5 deletions wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ use crate::{
use super::{conv, descriptor, view};
use parking_lot::Mutex;
use std::{ffi, mem, num::NonZeroU32, ptr, sync::Arc};
use winapi::{
shared::{dxgiformat, dxgitype, minwindef::BOOL, winerror},
um::{d3d12 as d3d12_ty, synchapi, winbase},
Interface,
};

// this has to match Naga's HLSL backend, and also needs to be null-terminated
const NAGA_LOCATION_SEMANTIC: &[u8] = b"LOC\0";
Expand Down
1 change: 0 additions & 1 deletion wgpu-hal/src/dx12/instance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use parking_lot::RwLock;
use winapi::shared::{dxgi1_5, minwindef};

use super::SurfaceTarget;
use crate::auxil::{self, dxgi::result::HResult as _};
Expand Down
5 changes: 0 additions & 5 deletions wgpu-hal/src/dx12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ use crate::auxil::{self, dxgi::result::HResult as _};
use arrayvec::ArrayVec;
use parking_lot::{Mutex, RwLock};
use std::{ffi, fmt, mem, num::NonZeroU32, sync::Arc};
use winapi::{
shared::{dxgi, dxgi1_4, dxgitype, windef, winerror},
um::{d3d12 as d3d12_ty, dcomp, synchapi, winbase, winnt},
Interface as _,
};

#[derive(Clone, Debug)]
pub struct Api;
Expand Down
1 change: 0 additions & 1 deletion wgpu-hal/src/dx12/shader_compilation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ptr;

pub(super) use dxc::{compile_dxc, get_dxc_container, DxcContainer};
use winapi::um::d3dcompiler;

use crate::auxil::dxgi::result::HResult;

Expand Down
157 changes: 3 additions & 154 deletions wgpu-hal/src/dx12/suballocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,13 @@ 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")]
// TODO: inline module
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 placed {
use d3d12::ComPtr;
use parking_lot::Mutex;
use std::ptr;
use wgt::assertions::StrictAssertUnwrapExt;
use winapi::{
um::{
d3d12::{self as d3d12_ty, ID3D12Resource},
winnt::HRESULT,
},
Interface,
};

use gpu_allocator::{
d3d12::{AllocationCreateDesc, ToWinapi, ToWindows},
Expand All @@ -44,12 +27,10 @@ mod placed {
}

pub(crate) fn create_allocator_wrapper(
raw: &d3d12::Device,
device: &d3d12::Device,
) -> Result<Option<Mutex<GpuAllocatorWrapper>>, crate::DeviceError> {
let device = raw.as_ptr();

match gpu_allocator::d3d12::Allocator::new(&gpu_allocator::d3d12::AllocatorCreateDesc {
device: gpu_allocator::d3d12::ID3D12DeviceVersion::Device(device.as_windows().clone()),
device: gpu_allocator::d3d12::ID3D12DeviceVersion::Device(device.clone()),
debug_settings: Default::default(),
allocation_sizes: gpu_allocator::AllocationSizes::default(),
}) {
Expand Down Expand Up @@ -219,135 +200,3 @@ mod placed {
}
}
}

// 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
mod committed {
use d3d12::ComPtr;
use parking_lot::Mutex;
use std::ptr;
use winapi::{
um::{
d3d12::{self as d3d12_ty, ID3D12Resource},
winnt::HRESULT,
},
Interface,
};

// 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;

// Allocator isn't needed when not suballocating with gpu_allocator
#[derive(Debug)]
pub(crate) struct GpuAllocatorWrapper {}

// Allocations aren't needed when not suballocating with gpu_allocator
#[derive(Debug)]
pub(crate) struct AllocationWrapper {}

#[allow(unused)]
pub(crate) fn create_allocator_wrapper(
_raw: &d3d12::Device,
) -> Result<Option<Mutex<GpuAllocatorWrapper>>, crate::DeviceError> {
Ok(None)
}

pub(crate) fn create_buffer_resource(
device: &crate::dx12::Device,
desc: &crate::BufferDescriptor,
raw_desc: d3d12_ty::D3D12_RESOURCE_DESC,
resource: &mut ComPtr<ID3D12Resource>,
) -> Result<(HRESULT, Option<AllocationWrapper>), crate::DeviceError> {
let is_cpu_read = desc.usage.contains(crate::BufferUses::MAP_READ);
let is_cpu_write = desc.usage.contains(crate::BufferUses::MAP_WRITE);

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(),
)
};

Ok((hr, None))
}

pub(crate) fn create_texture_resource(
device: &crate::dx12::Device,
_desc: &crate::TextureDescriptor,
raw_desc: d3d12_ty::D3D12_RESOURCE_DESC,
resource: &mut ComPtr<ID3D12Resource>,
) -> Result<(HRESULT, Option<AllocationWrapper>), crate::DeviceError> {
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(),
)
};

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>,
) {
// No-op when not using gpu-allocator
}
}
Loading

0 comments on commit e9e6444

Please sign in to comment.