From 4af1f9d2fd9bd87914cb9bb951ec2c79f13df778 Mon Sep 17 00:00:00 2001 From: David Dubois Date: Thu, 18 Jul 2019 23:35:53 -0700 Subject: [PATCH] Add ID3D12Heap, and related structs and methods `D3D12_HEAP_TYPE, D3D12_CPU_PAGE_PROPERTY, D3D12_MEMORY_POOL, D3D12_HEAP_FLAGS, D3D12_HEAP_PROPERTIES, and D3D12_HEAP_DESC ID3D12Heap Wrap ID3D12Device::CreateHeap Made a tricky enum because DX12 doesn't follow Rust's rules bump version to 0.2.0 Use bitflags for heap::Flags Fix typo Another typo fix --- .gitignore | 2 ++ Cargo.toml | 2 +- src/device.rs | 26 +++++++++++++++ src/heap.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/heap.rs diff --git a/.gitignore b/.gitignore index 088ba6ba7d..cbed4d71a1 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk + +.idea/ diff --git a/Cargo.toml b/Cargo.toml index f1365b3121..c241090f0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "d3d12" -version = "0.1.0" +version = "0.2.1" authors = ["msiglreith "] description = "Low level D3D12 API wrapper" repository = "https://github.com/gfx-rs/d3d12-rs" diff --git a/src/device.rs b/src/device.rs index 376c304181..0dd175de42 100644 --- a/src/device.rs +++ b/src/device.rs @@ -12,6 +12,7 @@ use { Fence, GraphicsCommandList, NodeMask, PipelineState, QueryHeap, Resource, RootSignature, Shader, TextureAddressMode, }; +use heap::{Properties, Flags, Heap}; pub type Device = WeakPtr; @@ -33,6 +34,31 @@ impl Device { (device, hr) } + pub fn create_heap(&self, + size_in_bytes: u64, + properties: Properties, + alignment: u64, + flags: Flags) -> D3DResult { + let mut heap = Heap::null(); + + let desc = d3d12::D3D12_HEAP_DESC{ + SizeInBytes: size_in_bytes, + Properties: properties.0, + Alignment: alignment, + Flags: flags.bits(), + }; + + let hr = unsafe { + self.CreateHeap( + &desc, + &d3d12::ID3D12Heap::uuidof(), + heap.mut_void(), + ) + }; + + (heap, hr) + } + pub fn create_command_allocator(&self, list_type: CmdListType) -> D3DResult { let mut allocator = CommandAllocator::null(); let hr = unsafe { diff --git a/src/heap.rs b/src/heap.rs new file mode 100644 index 0000000000..82f3c5983c --- /dev/null +++ b/src/heap.rs @@ -0,0 +1,87 @@ +use com::WeakPtr; +use winapi::um::d3d12; + +pub type Heap = WeakPtr; + +#[repr(u32)] +#[derive(Clone, Copy)] +pub enum Type { + Default = d3d12::D3D12_HEAP_TYPE_DEFAULT, + Upload = d3d12::D3D12_HEAP_TYPE_UPLOAD, + Readback = d3d12::D3D12_HEAP_TYPE_READBACK, + Custom = d3d12::D3D12_HEAP_TYPE_CUSTOM, +} + +#[repr(u32)] +#[derive(Clone, Copy)] +pub enum CpuPageProperty { + Unknown = d3d12::D3D12_CPU_PAGE_PROPERTY_UNKNOWN, + NotAvailable = d3d12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE, + WriteCombine = d3d12::D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE, + WriteBack= d3d12::D3D12_CPU_PAGE_PROPERTY_WRITE_BACK, +} + +#[repr(u32)] +#[derive(Clone, Copy)] +pub enum MemoryPool { + Unknown = d3d12::D3D12_CPU_PAGE_PROPERTY_UNKNOWN, + L0 = d3d12::D3D12_MEMORY_POOL_L0, + L1 = d3d12::D3D12_MEMORY_POOL_L1, +} + +bitflags! { + pub struct Flags: u32 { + const NONE = d3d12::D3D12_HEAP_FLAG_NONE; + const SHARED = d3d12::D3D12_HEAP_FLAG_SHARED; + const DENY_BUFFERS = d3d12::D3D12_HEAP_FLAG_DENY_BUFFERS; + const ALLOW_DISPLAY = d3d12::D3D12_HEAP_FLAG_ALLOW_DISPLAY; + const SHARED_CROSS_ADAPTER = d3d12::D3D12_HEAP_FLAG_SHARED_CROSS_ADAPTER; + const DENT_RT_DS_TEXTURES = d3d12::D3D12_HEAP_FLAG_DENY_RT_DS_TEXTURES; + const DENY_NON_RT_DS_TEXTURES = d3d12::D3D12_HEAP_FLAG_DENY_NON_RT_DS_TEXTURES; + const HARDWARE_PROTECTED = d3d12::D3D12_HEAP_FLAG_HARDWARE_PROTECTED; + const ALLOW_WRITE_WATCH = d3d12::D3D12_HEAP_FLAG_ALLOW_WRITE_WATCH; + const ALLOW_ALL_BUFFERS_AND_TEXTURES = d3d12::D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES; + const ALLOW_ONLY_BUFFERS = d3d12::D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS; + const ALLOW_ONLY_NON_RT_DS_TEXTURES = d3d12::D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES; + const ALLOW_ONLY_RT_DS_TEXTURES = d3d12::D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES; + } +} + +#[repr(transparent)] +pub struct Properties(pub d3d12::D3D12_HEAP_PROPERTIES); +impl Properties { + pub fn new( + heap_type: Type, + cpu_page_property: CpuPageProperty, + memory_pool_preference: MemoryPool, + creation_node_mask: u32, + visible_node_mask: u32, + ) -> Self { + Properties(d3d12::D3D12_HEAP_PROPERTIES { + Type: heap_type as _, + CPUPageProperty: cpu_page_property as _, + MemoryPoolPreference: memory_pool_preference as _, + CreationNodeMask: creation_node_mask, + VisibleNodeMask: visible_node_mask, + }) + } +} + +#[repr(transparent)] +pub struct Desc(d3d12::D3D12_HEAP_DESC); +impl Desc{ + pub fn new( + size_in_bytes: u64, + properties: Properties, + alignment: u64, + flags: Flags, + ) -> Self { + Desc(d3d12::D3D12_HEAP_DESC{ + SizeInBytes: size_in_bytes, + Properties: properties.0, + Alignment: alignment, + Flags: flags.bits(), + }) + } +} + diff --git a/src/lib.rs b/src/lib.rs index 2ea9d576ae..1a9cfee163 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ pub mod debug; pub mod descriptor; pub mod device; pub mod dxgi; +pub mod heap; pub mod pso; pub mod query; pub mod queue; @@ -25,6 +26,7 @@ pub use self::command_list::{CommandSignature, GraphicsCommandList}; pub use self::debug::Debug; pub use self::descriptor::{CpuDescriptor, DescriptorHeap, GpuDescriptor, RootSignature}; pub use self::device::Device; +pub use self::heap::Properties; pub use self::pso::{CachedPSO, PipelineState, Shader}; pub use self::query::QueryHeap; pub use self::queue::CommandQueue;