| 1 |
//! Example showcasing [`gpu-allocator`] with types and functions from the [`windows`] crate. |
| 2 |
use gpu_allocator::{ |
| 3 |
d3d12::{ |
| 4 |
AllocationCreateDesc, Allocator, AllocatorCreateDesc, ID3D12DeviceVersion, ResourceCategory, |
| 5 |
}, |
| 6 |
MemoryLocation, |
| 7 |
}; |
| 8 |
use log::*; |
| 9 |
use windows::{ |
| 10 |
core::{Interface, Result}, |
| 11 |
Win32::{ |
| 12 |
Foundation::E_NOINTERFACE, |
| 13 |
Graphics::{ |
| 14 |
Direct3D::{D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_12_0}, |
| 15 |
Direct3D12::{ |
| 16 |
D3D12CreateDevice, ID3D12Device, ID3D12Resource, |
| 17 |
D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT, D3D12_RESOURCE_DESC, |
| 18 |
D3D12_RESOURCE_DIMENSION_BUFFER, D3D12_RESOURCE_FLAG_NONE, |
| 19 |
D3D12_RESOURCE_STATE_COMMON, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, |
| 20 |
}, |
| 21 |
Dxgi::{ |
| 22 |
Common::{DXGI_FORMAT_UNKNOWN, DXGI_SAMPLE_DESC}, |
| 23 |
CreateDXGIFactory2, IDXGIAdapter4, IDXGIFactory6, DXGI_ADAPTER_FLAG3_SOFTWARE, |
| 24 |
DXGI_ERROR_NOT_FOUND, |
| 25 |
}, |
| 26 |
}, |
| 27 |
}, |
| 28 |
}; |
| 29 |
|
| 30 |
fn create_d3d12_device(dxgi_factory: &IDXGIFactory6) -> Option<ID3D12Device> { |
| 31 |
for idx in 0.. { |
| 32 |
// TODO: Might as well return Result<> from this function |
| 33 |
let adapter1 = match unsafe { dxgi_factory.EnumAdapters1(idx) } { |
| 34 |
Ok(a) => a, |
| 35 |
Err(e) if e.code() == DXGI_ERROR_NOT_FOUND => break, |
| 36 |
Err(e) => panic!("{:?}", e), |
| 37 |
}; |
| 38 |
let adapter4: IDXGIAdapter4 = adapter1.cast().unwrap(); |
| 39 |
|
| 40 |
let desc = unsafe { adapter4.GetDesc3() }.unwrap(); |
| 41 |
// Skip software adapters |
| 42 |
// Vote for https://github.com/microsoft/windows-rs/issues/793! |
| 43 |
if (desc.Flags & DXGI_ADAPTER_FLAG3_SOFTWARE) == DXGI_ADAPTER_FLAG3_SOFTWARE { |
| 44 |
continue; |
| 45 |
} |
| 46 |
|
| 47 |
let feature_levels = [ |
| 48 |
(D3D_FEATURE_LEVEL_11_0, "D3D_FEATURE_LEVEL_11_0"), |
| 49 |
(D3D_FEATURE_LEVEL_11_1, "D3D_FEATURE_LEVEL_11_1"), |
| 50 |
(D3D_FEATURE_LEVEL_12_0, "D3D_FEATURE_LEVEL_12_0"), |
| 51 |
]; |
| 52 |
|
| 53 |
let device = |
| 54 |
feature_levels |
| 55 |
.iter() |
| 56 |
.rev() |
| 57 |
.find_map(|&(feature_level, feature_level_name)| { |
| 58 |
let mut device = None; |
| 59 |
match unsafe { D3D12CreateDevice(&adapter4, feature_level, &mut device) } { |
| 60 |
Ok(()) => { |
| 61 |
info!("Using D3D12 feature level: {}", feature_level_name); |
| 62 |
Some(device.unwrap()) |
| 63 |
} |
| 64 |
Err(e) if e.code() == E_NOINTERFACE => { |
| 65 |
error!("ID3D12Device interface not supported"); |
| 66 |
None |
| 67 |
} |
| 68 |
Err(e) => { |
| 69 |
info!( |
| 70 |
"D3D12 feature level {} not supported: {}", |
| 71 |
feature_level_name, e |
| 72 |
); |
| 73 |
None |
| 74 |
} |
| 75 |
} |
| 76 |
}); |
| 77 |
if device.is_some() { |
| 78 |
return device; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
None |
| 83 |
} |
| 84 |
|
| 85 |
fn main() -> Result<()> { |
| 86 |
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init(); |
| 87 |
|
| 88 |
let dxgi_factory = unsafe { |
| 89 |
CreateDXGIFactory2(windows::Win32::Graphics::Dxgi::DXGI_CREATE_FACTORY_FLAGS::default()) |
| 90 |
}?; |
| 91 |
|
| 92 |
let device = create_d3d12_device(&dxgi_factory).expect("Failed to create D3D12 device."); |
| 93 |
|
| 94 |
// Setting up the allocator |
| 95 |
let mut allocator = Allocator::new(&AllocatorCreateDesc { |
| 96 |
device: ID3D12DeviceVersion::Device(device.clone()), |
| 97 |
debug_settings: Default::default(), |
| 98 |
allocation_sizes: Default::default(), |
| 99 |
}) |
| 100 |
.unwrap(); |
| 101 |
|
| 102 |
// Test allocating Gpu Only memory |
| 103 |
{ |
| 104 |
let test_buffer_desc = D3D12_RESOURCE_DESC { |
| 105 |
Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, |
| 106 |
Alignment: D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT as u64, |
| 107 |
Width: 512, |
| 108 |
Height: 1, |
| 109 |
DepthOrArraySize: 1, |
| 110 |
MipLevels: 1, |
| 111 |
Format: DXGI_FORMAT_UNKNOWN, |
| 112 |
SampleDesc: DXGI_SAMPLE_DESC { |
| 113 |
Count: 1, |
| 114 |
Quality: 0, |
| 115 |
}, |
| 116 |
Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, |
| 117 |
Flags: D3D12_RESOURCE_FLAG_NONE, |
| 118 |
}; |
| 119 |
|
| 120 |
let allocation_desc = AllocationCreateDesc::from_d3d12_resource_desc( |
| 121 |
allocator.device(), |
| 122 |
&test_buffer_desc, |
| 123 |
"Test allocation (Gpu only)", |
| 124 |
MemoryLocation::GpuOnly, |
| 125 |
); |
| 126 |
let allocation = allocator.allocate(&allocation_desc).unwrap(); |
| 127 |
|
| 128 |
let mut resource: Option<ID3D12Resource> = None; |
| 129 |
unsafe { |
| 130 |
device.CreatePlacedResource( |
| 131 |
allocation.heap(), |
| 132 |
allocation.offset(), |
| 133 |
&test_buffer_desc, |
| 134 |
D3D12_RESOURCE_STATE_COMMON, |
| 135 |
None, |
| 136 |
&mut resource, |
| 137 |
) |
| 138 |
}?; |
| 139 |
|
| 140 |
drop(resource); |
| 141 |
|
| 142 |
allocator.free(allocation).unwrap(); |
| 143 |
info!("Allocation and deallocation of GpuOnly memory was successful."); |
| 144 |
} |
| 145 |
|
| 146 |
// Test allocating Cpu to Gpu memory |
| 147 |
{ |
| 148 |
let test_buffer_desc = D3D12_RESOURCE_DESC { |
| 149 |
Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, |
| 150 |
Alignment: D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT as u64, |
| 151 |
Width: 512, |
| 152 |
Height: 1, |
| 153 |
DepthOrArraySize: 1, |
| 154 |
MipLevels: 1, |
| 155 |
Format: DXGI_FORMAT_UNKNOWN, |
| 156 |
SampleDesc: DXGI_SAMPLE_DESC { |
| 157 |
Count: 1, |
| 158 |
Quality: 0, |
| 159 |
}, |
| 160 |
Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, |
| 161 |
Flags: D3D12_RESOURCE_FLAG_NONE, |
| 162 |
}; |
| 163 |
|
| 164 |
let alloc_info = unsafe { device.GetResourceAllocationInfo(0, &[test_buffer_desc]) }; |
| 165 |
|
| 166 |
let allocation = allocator |
| 167 |
.allocate(&AllocationCreateDesc { |
| 168 |
name: "Test allocation (Cpu To Gpu)", |
| 169 |
location: MemoryLocation::CpuToGpu, |
| 170 |
size: alloc_info.SizeInBytes, |
| 171 |
alignment: alloc_info.Alignment, |
| 172 |
resource_category: ResourceCategory::Buffer, |
| 173 |
}) |
| 174 |
.unwrap(); |
| 175 |
|
| 176 |
let mut resource: Option<ID3D12Resource> = None; |
| 177 |
unsafe { |
| 178 |
device.CreatePlacedResource( |
| 179 |
allocation.heap(), |
| 180 |
allocation.offset(), |
| 181 |
&test_buffer_desc, |
| 182 |
D3D12_RESOURCE_STATE_COMMON, |
| 183 |
None, |
| 184 |
&mut resource, |
| 185 |
) |
| 186 |
}?; |
| 187 |
|
| 188 |
drop(resource); |
| 189 |
|
| 190 |
allocator.free(allocation).unwrap(); |
| 191 |
info!("Allocation and deallocation of CpuToGpu memory was successful."); |
| 192 |
} |
| 193 |
|
| 194 |
// Test allocating Gpu to Cpu memory |
| 195 |
{ |
| 196 |
let test_buffer_desc = D3D12_RESOURCE_DESC { |
| 197 |
Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, |
| 198 |
Alignment: D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT as u64, |
| 199 |
Width: 512, |
| 200 |
Height: 1, |
| 201 |
DepthOrArraySize: 1, |
| 202 |
MipLevels: 1, |
| 203 |
Format: DXGI_FORMAT_UNKNOWN, |
| 204 |
SampleDesc: DXGI_SAMPLE_DESC { |
| 205 |
Count: 1, |
| 206 |
Quality: 0, |
| 207 |
}, |
| 208 |
Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, |
| 209 |
Flags: D3D12_RESOURCE_FLAG_NONE, |
| 210 |
}; |
| 211 |
|
| 212 |
let alloc_info = unsafe { device.GetResourceAllocationInfo(0, &[test_buffer_desc]) }; |
| 213 |
|
| 214 |
let allocation = allocator |
| 215 |
.allocate(&AllocationCreateDesc { |
| 216 |
name: "Test allocation (Gpu to Cpu)", |
| 217 |
location: MemoryLocation::GpuToCpu, |
| 218 |
size: alloc_info.SizeInBytes, |
| 219 |
alignment: alloc_info.Alignment, |
| 220 |
resource_category: ResourceCategory::Buffer, |
| 221 |
}) |
| 222 |
.unwrap(); |
| 223 |
|
| 224 |
let mut resource: Option<ID3D12Resource> = None; |
| 225 |
unsafe { |
| 226 |
device.CreatePlacedResource( |
| 227 |
allocation.heap(), |
| 228 |
allocation.offset(), |
| 229 |
&test_buffer_desc, |
| 230 |
D3D12_RESOURCE_STATE_COMMON, |
| 231 |
None, |
| 232 |
&mut resource, |
| 233 |
) |
| 234 |
}?; |
| 235 |
|
| 236 |
drop(resource); |
| 237 |
|
| 238 |
allocator.free(allocation).unwrap(); |
| 239 |
info!("Allocation and deallocation of CpuToGpu memory was successful."); |
| 240 |
} |
| 241 |
|
| 242 |
Ok(()) |
| 243 |
} |