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
This commit is contained in:
David Dubois
2019-07-18 23:35:53 -07:00
parent 5324fa6e1c
commit 4af1f9d2fd
5 changed files with 118 additions and 1 deletions

2
.gitignore vendored
View File

@@ -8,3 +8,5 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
.idea/

View File

@@ -1,6 +1,6 @@
[package]
name = "d3d12"
version = "0.1.0"
version = "0.2.1"
authors = ["msiglreith <m.siglreith@gmail.com>"]
description = "Low level D3D12 API wrapper"
repository = "https://github.com/gfx-rs/d3d12-rs"

View File

@@ -12,6 +12,7 @@ use {
Fence, GraphicsCommandList, NodeMask, PipelineState, QueryHeap, Resource, RootSignature,
Shader, TextureAddressMode,
};
use heap::{Properties, Flags, Heap};
pub type Device = WeakPtr<d3d12::ID3D12Device>;
@@ -33,6 +34,31 @@ impl Device {
(device, hr)
}
pub fn create_heap(&self,
size_in_bytes: u64,
properties: Properties,
alignment: u64,
flags: Flags) -> D3DResult<Heap> {
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<CommandAllocator> {
let mut allocator = CommandAllocator::null();
let hr = unsafe {

87
src/heap.rs Normal file
View File

@@ -0,0 +1,87 @@
use com::WeakPtr;
use winapi::um::d3d12;
pub type Heap = WeakPtr<d3d12::ID3D12Heap>;
#[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(),
})
}
}

View File

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