diff --git a/src/command_list.rs b/src/command_list.rs index 7db8d120b7..d756ced45a 100644 --- a/src/command_list.rs +++ b/src/command_list.rs @@ -56,6 +56,34 @@ impl IndirectArgument { // TODO: missing variants } +#[repr(transparent)] +pub struct ResourceBarrier(d3d12::D3D12_RESOURCE_BARRIER); + +impl ResourceBarrier { + pub fn transition( + resource: Resource, + subresource: u32, + state_before: d3d12::D3D12_RESOURCE_STATES, + state_after: d3d12::D3D12_RESOURCE_STATES, + flags: d3d12::D3D12_RESOURCE_BARRIER_FLAGS, + ) -> Self { + let mut barrier = d3d12::D3D12_RESOURCE_BARRIER { + Type: d3d12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: flags, + ..unsafe { mem::zeroed() } + }; + unsafe { + *barrier.u.Transition_mut() = d3d12::D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: resource.as_mut_ptr(), + Subresource: subresource, + StateBefore: state_before, + StateAfter: state_after, + }; + } + ResourceBarrier(barrier) + } +} + pub type CommandSignature = WeakPtr; pub type CommandList = WeakPtr; pub type GraphicsCommandList = WeakPtr; @@ -291,4 +319,10 @@ impl GraphicsCommandList { self.SetGraphicsRootUnorderedAccessView(root_index, buffer_location); } } + + pub fn resource_barrier(&self, barriers: &[ResourceBarrier]) { + unsafe { + self.ResourceBarrier(barriers.len() as _, barriers.as_ptr() as _) // matches representation + } + } } diff --git a/src/device.rs b/src/device.rs index 79f1d6a938..fd3fa5f595 100644 --- a/src/device.rs +++ b/src/device.rs @@ -3,6 +3,7 @@ use com::WeakPtr; use command_list::{CmdListType, CommandSignature, IndirectArgument}; use descriptor::{CpuDescriptor, DescriptorHeapFlags, DescriptorHeapType, RenderTargetViewDesc}; +use heap::{Heap, HeapFlags, HeapProperties}; use std::ops::Range; use winapi::um::d3d12; use winapi::Interface; @@ -12,7 +13,6 @@ use { Fence, GraphicsCommandList, NodeMask, PipelineState, QueryHeap, Resource, RootSignature, Shader, TextureAddressMode, }; -use heap::{Properties, Flags, Heap}; pub type Device = WeakPtr; @@ -34,27 +34,23 @@ impl Device { (device, hr) } - pub fn create_heap(&self, - size_in_bytes: u64, - properties: Properties, - alignment: u64, - flags: Flags) -> D3DResult { + pub fn create_heap( + &self, + size_in_bytes: u64, + properties: HeapProperties, + alignment: u64, + flags: HeapFlags, + ) -> D3DResult { let mut heap = Heap::null(); - let desc = d3d12::D3D12_HEAP_DESC{ + 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(), - ) - }; + let hr = unsafe { self.CreateHeap(&desc, &d3d12::ID3D12Heap::uuidof(), heap.mut_void()) }; (heap, hr) } diff --git a/src/heap.rs b/src/heap.rs index 82f3c5983c..de97dbf506 100644 --- a/src/heap.rs +++ b/src/heap.rs @@ -5,7 +5,7 @@ pub type Heap = WeakPtr; #[repr(u32)] #[derive(Clone, Copy)] -pub enum Type { +pub enum HeapType { Default = d3d12::D3D12_HEAP_TYPE_DEFAULT, Upload = d3d12::D3D12_HEAP_TYPE_UPLOAD, Readback = d3d12::D3D12_HEAP_TYPE_READBACK, @@ -18,7 +18,7 @@ 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, + WriteBack = d3d12::D3D12_CPU_PAGE_PROPERTY_WRITE_BACK, } #[repr(u32)] @@ -30,7 +30,7 @@ pub enum MemoryPool { } bitflags! { - pub struct Flags: u32 { + pub struct HeapFlags: u32 { const NONE = d3d12::D3D12_HEAP_FLAG_NONE; const SHARED = d3d12::D3D12_HEAP_FLAG_SHARED; const DENY_BUFFERS = d3d12::D3D12_HEAP_FLAG_DENY_BUFFERS; @@ -48,16 +48,16 @@ bitflags! { } #[repr(transparent)] -pub struct Properties(pub d3d12::D3D12_HEAP_PROPERTIES); -impl Properties { +pub struct HeapProperties(pub d3d12::D3D12_HEAP_PROPERTIES); +impl HeapProperties { pub fn new( - heap_type: Type, + heap_type: HeapType, cpu_page_property: CpuPageProperty, memory_pool_preference: MemoryPool, creation_node_mask: u32, visible_node_mask: u32, ) -> Self { - Properties(d3d12::D3D12_HEAP_PROPERTIES { + HeapProperties(d3d12::D3D12_HEAP_PROPERTIES { Type: heap_type as _, CPUPageProperty: cpu_page_property as _, MemoryPoolPreference: memory_pool_preference as _, @@ -68,15 +68,15 @@ impl Properties { } #[repr(transparent)] -pub struct Desc(d3d12::D3D12_HEAP_DESC); -impl Desc{ +pub struct HeapDesc(d3d12::D3D12_HEAP_DESC); +impl HeapDesc { pub fn new( size_in_bytes: u64, - properties: Properties, + properties: HeapProperties, alignment: u64, - flags: Flags, + flags: HeapFlags, ) -> Self { - Desc(d3d12::D3D12_HEAP_DESC{ + HeapDesc(d3d12::D3D12_HEAP_DESC { SizeInBytes: size_in_bytes, Properties: properties.0, Alignment: alignment, @@ -84,4 +84,3 @@ impl Desc{ }) } } -