| 1 |
use alloc::sync::Arc; |
| 2 |
use gpu_allocator::{d3d12::AllocationCreateDesc, MemoryLocation}; |
| 3 |
use parking_lot::Mutex; |
| 4 |
use windows::Win32::Graphics::{Direct3D12, Dxgi}; |
| 5 |
|
| 6 |
use crate::{ |
| 7 |
auxil::dxgi::{name::ObjectExt, result::HResult as _}, |
| 8 |
dx12::conv, |
| 9 |
}; |
| 10 |
|
| 11 |
#[derive(Debug)] |
| 12 |
pub(crate) enum AllocationType { |
| 13 |
Buffer, |
| 14 |
Texture, |
| 15 |
AccelerationStructure, |
| 16 |
} |
| 17 |
|
| 18 |
#[derive(Debug)] |
| 19 |
enum AllocationInner { |
| 20 |
/// This resource is suballocated from a heap. |
| 21 |
Placed { |
| 22 |
inner: gpu_allocator::d3d12::Allocation, |
| 23 |
}, |
| 24 |
/// This resource is a committed resource and does not belong to a |
| 25 |
/// suballocated heap. We store an approximate size, so we can manage our counters |
| 26 |
/// correctly. |
| 27 |
/// |
| 28 |
/// This is only used for Intel Xe drivers, which have a bug that |
| 29 |
/// prevents suballocation from working correctly. |
| 30 |
Committed { size: u64 }, |
| 31 |
} |
| 32 |
|
| 33 |
#[derive(Debug)] |
| 34 |
pub(crate) struct Allocation { |
| 35 |
inner: AllocationInner, |
| 36 |
ty: AllocationType, |
| 37 |
} |
| 38 |
|
| 39 |
impl Allocation { |
| 40 |
pub fn placed(inner: gpu_allocator::d3d12::Allocation, ty: AllocationType) -> Self { |
| 41 |
Self { |
| 42 |
inner: AllocationInner::Placed { inner }, |
| 43 |
ty, |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
pub fn none(ty: AllocationType, size: u64) -> Self { |
| 48 |
Self { |
| 49 |
inner: AllocationInner::Committed { size }, |
| 50 |
ty, |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
pub fn size(&self) -> u64 { |
| 55 |
match self.inner { |
| 56 |
AllocationInner::Placed { ref inner } => inner.size(), |
| 57 |
AllocationInner::Committed { size } => size, |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
#[derive(Clone)] |
| 63 |
pub(crate) struct Allocator { |
| 64 |
inner: Arc<Mutex<gpu_allocator::d3d12::Allocator>>, |
| 65 |
device_memblock_size: u64, |
| 66 |
host_memblock_size: u64, |
| 67 |
pub memory_budget_thresholds: wgt::MemoryBudgetThresholds, |
| 68 |
} |
| 69 |
|
| 70 |
impl Allocator { |
| 71 |
pub(crate) fn new( |
| 72 |
raw: &Direct3D12::ID3D12Device, |
| 73 |
memory_hints: &wgt::MemoryHints, |
| 74 |
memory_budget_thresholds: wgt::MemoryBudgetThresholds, |
| 75 |
) -> Result<Self, crate::DeviceError> { |
| 76 |
// TODO: the allocator's configuration should take hardware capability into |
| 77 |
// account. |
| 78 |
const MB: u64 = 1024 * 1024; |
| 79 |
let (device_memblock_size, host_memblock_size) = match memory_hints { |
| 80 |
wgt::MemoryHints::Performance => (256 * MB, 64 * MB), |
| 81 |
wgt::MemoryHints::MemoryUsage => (8 * MB, 4 * MB), |
| 82 |
wgt::MemoryHints::Manual { |
| 83 |
suballocated_device_memory_block_size, |
| 84 |
} => { |
| 85 |
// TODO: Would it be useful to expose the host size in memory hints |
| 86 |
// instead of always using half of the device size? |
| 87 |
let device_size = suballocated_device_memory_block_size.start; |
| 88 |
let host_size = device_size / 2; |
| 89 |
(device_size, host_size) |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
// gpu_allocator clamps the sizes between 4MiB and 256MiB, but we clamp them ourselves since we use |
| 94 |
// the sizes when detecting high memory pressure and there is no way to query the values otherwise. |
| 95 |
|
| 96 |
let device_memblock_size = device_memblock_size.clamp(4 * MB, 256 * MB); |
| 97 |
let host_memblock_size = host_memblock_size.clamp(4 * MB, 256 * MB); |
| 98 |
|
| 99 |
let allocation_sizes = |
| 100 |
gpu_allocator::AllocationSizes::new(device_memblock_size, host_memblock_size); |
| 101 |
|
| 102 |
let allocator_desc = gpu_allocator::d3d12::AllocatorCreateDesc { |
| 103 |
device: gpu_allocator::d3d12::ID3D12DeviceVersion::Device(raw.clone()), |
| 104 |
debug_settings: Default::default(), |
| 105 |
allocation_sizes, |
| 106 |
}; |
| 107 |
|
| 108 |
let allocator = gpu_allocator::d3d12::Allocator::new(&allocator_desc).inspect_err(|e| { |
| 109 |
log::error!("Failed to create d3d12 allocator, error: {}", e); |
| 110 |
})?; |
| 111 |
|
| 112 |
Ok(Self { |
| 113 |
inner: Arc::new(Mutex::new(allocator)), |
| 114 |
device_memblock_size, |
| 115 |
host_memblock_size, |
| 116 |
memory_budget_thresholds, |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
pub(crate) fn generate_report(&self) -> wgt::AllocatorReport { |
| 121 |
let mut upstream = self.inner.lock().generate_report(); |
| 122 |
|
| 123 |
let allocations = upstream |
| 124 |
.allocations |
| 125 |
.iter_mut() |
| 126 |
.map(|alloc| wgt::AllocationReport { |
| 127 |
name: core::mem::take(&mut alloc.name), |
| 128 |
offset: alloc.offset, |
| 129 |
size: alloc.size, |
| 130 |
}) |
| 131 |
.collect(); |
| 132 |
|
| 133 |
let blocks = upstream |
| 134 |
.blocks |
| 135 |
.iter() |
| 136 |
.map(|block| wgt::MemoryBlockReport { |
| 137 |
size: block.size, |
| 138 |
allocations: block.allocations.clone(), |
| 139 |
}) |
| 140 |
.collect(); |
| 141 |
|
| 142 |
wgt::AllocatorReport { |
| 143 |
allocations, |
| 144 |
blocks, |
| 145 |
total_allocated_bytes: upstream.total_allocated_bytes, |
| 146 |
total_reserved_bytes: upstream.total_reserved_bytes, |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/// To allow us to construct buffers from both a `Device` and `CommandEncoder` |
| 152 |
/// without needing each function to take a million arguments, we create a |
| 153 |
/// borrowed context struct that contains the relevant members. |
| 154 |
pub(crate) struct DeviceAllocationContext<'a> { |
| 155 |
pub(crate) raw: &'a Direct3D12::ID3D12Device, |
| 156 |
pub(crate) shared: &'a super::DeviceShared, |
| 157 |
pub(crate) mem_allocator: &'a Allocator, |
| 158 |
pub(crate) counters: &'a wgt::HalCounters, |
| 159 |
} |
| 160 |
|
| 161 |
impl<'a> From<&'a super::Device> for DeviceAllocationContext<'a> { |
| 162 |
fn from(device: &'a super::Device) -> Self { |
| 163 |
Self { |
| 164 |
raw: &device.raw, |
| 165 |
shared: &device.shared, |
| 166 |
mem_allocator: &device.mem_allocator, |
| 167 |
counters: &device.counters, |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
impl<'a> From<&'a super::CommandEncoder> for DeviceAllocationContext<'a> { |
| 173 |
fn from(encoder: &'a super::CommandEncoder) -> Self { |
| 174 |
Self { |
| 175 |
raw: &encoder.device, |
| 176 |
shared: &encoder.shared, |
| 177 |
mem_allocator: &encoder.mem_allocator, |
| 178 |
counters: &encoder.counters, |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
impl<'a> DeviceAllocationContext<'a> { |
| 184 |
/////////////////////// |
| 185 |
// Resource Creation // |
| 186 |
/////////////////////// |
| 187 |
|
| 188 |
pub(crate) fn create_buffer( |
| 189 |
&self, |
| 190 |
desc: &crate::BufferDescriptor, |
| 191 |
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> { |
| 192 |
let is_cpu_read = desc.usage.contains(wgt::BufferUses::MAP_READ); |
| 193 |
let is_cpu_write = desc.usage.contains(wgt::BufferUses::MAP_WRITE); |
| 194 |
|
| 195 |
let location = match (is_cpu_read, is_cpu_write) { |
| 196 |
(true, true) => MemoryLocation::CpuToGpu, |
| 197 |
(true, false) => MemoryLocation::GpuToCpu, |
| 198 |
(false, true) => MemoryLocation::CpuToGpu, |
| 199 |
(false, false) => MemoryLocation::GpuOnly, |
| 200 |
}; |
| 201 |
|
| 202 |
let raw_desc = conv::map_buffer_descriptor(desc); |
| 203 |
let allocation_info = |
| 204 |
self.error_if_would_oom_on_resource_allocation(&raw_desc, location)?; |
| 205 |
|
| 206 |
let (resource, allocation) = if self.shared.private_caps.suballocation_supported { |
| 207 |
self.create_placed_buffer(desc, raw_desc, allocation_info, location)? |
| 208 |
} else { |
| 209 |
self.create_committed_buffer(raw_desc, location)? |
| 210 |
}; |
| 211 |
|
| 212 |
if let Some(label) = desc.label { |
| 213 |
resource.set_name(label)?; |
| 214 |
} |
| 215 |
|
| 216 |
self.counters.buffer_memory.add(allocation.size() as isize); |
| 217 |
|
| 218 |
Ok((resource, allocation)) |
| 219 |
} |
| 220 |
|
| 221 |
pub(crate) fn create_texture( |
| 222 |
&self, |
| 223 |
desc: &crate::TextureDescriptor, |
| 224 |
raw_desc: Direct3D12::D3D12_RESOURCE_DESC, |
| 225 |
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> { |
| 226 |
let location = MemoryLocation::GpuOnly; |
| 227 |
let allocation_info = |
| 228 |
self.error_if_would_oom_on_resource_allocation(&raw_desc, location)?; |
| 229 |
|
| 230 |
let (resource, allocation) = if self.shared.private_caps.suballocation_supported { |
| 231 |
self.create_placed_texture(desc, raw_desc, allocation_info, location)? |
| 232 |
} else { |
| 233 |
self.create_committed_texture(desc, raw_desc)? |
| 234 |
}; |
| 235 |
|
| 236 |
if let Some(label) = desc.label { |
| 237 |
resource.set_name(label)?; |
| 238 |
} |
| 239 |
|
| 240 |
self.counters.texture_memory.add(allocation.size() as isize); |
| 241 |
|
| 242 |
Ok((resource, allocation)) |
| 243 |
} |
| 244 |
|
| 245 |
pub(crate) fn create_acceleration_structure( |
| 246 |
&self, |
| 247 |
desc: &crate::AccelerationStructureDescriptor, |
| 248 |
raw_desc: Direct3D12::D3D12_RESOURCE_DESC, |
| 249 |
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> { |
| 250 |
let location = MemoryLocation::GpuOnly; |
| 251 |
let allocation_info = |
| 252 |
self.error_if_would_oom_on_resource_allocation(&raw_desc, location)?; |
| 253 |
|
| 254 |
let (resource, allocation) = if self.shared.private_caps.suballocation_supported { |
| 255 |
self.create_placed_acceleration_structure(desc, raw_desc, allocation_info, location)? |
| 256 |
} else { |
| 257 |
self.create_committed_acceleration_structure(desc, raw_desc)? |
| 258 |
}; |
| 259 |
|
| 260 |
if let Some(label) = desc.label { |
| 261 |
resource.set_name(label)?; |
| 262 |
} |
| 263 |
|
| 264 |
self.counters |
| 265 |
.acceleration_structure_memory |
| 266 |
.add(allocation.size() as isize); |
| 267 |
|
| 268 |
Ok((resource, allocation)) |
| 269 |
} |
| 270 |
|
| 271 |
////////////////////////// |
| 272 |
// Resource Destruction // |
| 273 |
////////////////////////// |
| 274 |
|
| 275 |
pub(crate) fn free_resource( |
| 276 |
&self, |
| 277 |
resource: Direct3D12::ID3D12Resource, |
| 278 |
allocation: Allocation, |
| 279 |
) { |
| 280 |
// Make sure the resource is released before we free the allocation. |
| 281 |
drop(resource); |
| 282 |
|
| 283 |
let counter = match allocation.ty { |
| 284 |
AllocationType::Buffer => &self.counters.buffer_memory, |
| 285 |
AllocationType::Texture => &self.counters.texture_memory, |
| 286 |
AllocationType::AccelerationStructure => &self.counters.acceleration_structure_memory, |
| 287 |
}; |
| 288 |
counter.sub(allocation.size() as isize); |
| 289 |
|
| 290 |
if let AllocationInner::Placed { inner } = allocation.inner { |
| 291 |
match self.mem_allocator.inner.lock().free(inner) { |
| 292 |
Ok(_) => (), |
| 293 |
// TODO: Don't panic here |
| 294 |
Err(e) => panic!("Failed to destroy dx12 {:?}, {e}", allocation.ty), |
| 295 |
}; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/////////////////////////////// |
| 300 |
// Placed Resource Creation /// |
| 301 |
/////////////////////////////// |
| 302 |
|
| 303 |
fn create_placed_buffer( |
| 304 |
&self, |
| 305 |
desc: &crate::BufferDescriptor<'_>, |
| 306 |
raw_desc: Direct3D12::D3D12_RESOURCE_DESC, |
| 307 |
allocation_info: Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO, |
| 308 |
location: MemoryLocation, |
| 309 |
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> { |
| 310 |
let name = desc.label.unwrap_or("Unlabeled buffer"); |
| 311 |
|
| 312 |
let mut allocator = self.mem_allocator.inner.lock(); |
| 313 |
|
| 314 |
let allocation_desc = AllocationCreateDesc { |
| 315 |
name, |
| 316 |
location, |
| 317 |
size: allocation_info.SizeInBytes, |
| 318 |
alignment: allocation_info.Alignment, |
| 319 |
resource_category: gpu_allocator::d3d12::ResourceCategory::from(&raw_desc), |
| 320 |
}; |
| 321 |
|
| 322 |
let allocation = allocator.allocate(&allocation_desc)?; |
| 323 |
let mut resource = None; |
| 324 |
unsafe { |
| 325 |
self.raw.CreatePlacedResource( |
| 326 |
allocation.heap(), |
| 327 |
allocation.offset(), |
| 328 |
&raw_desc, |
| 329 |
Direct3D12::D3D12_RESOURCE_STATE_COMMON, |
| 330 |
None, |
| 331 |
&mut resource, |
| 332 |
) |
| 333 |
} |
| 334 |
.into_device_result("Placed buffer creation")?; |
| 335 |
|
| 336 |
let resource = resource.ok_or(crate::DeviceError::Unexpected)?; |
| 337 |
let wrapped_allocation = Allocation::placed(allocation, AllocationType::Buffer); |
| 338 |
|
| 339 |
Ok((resource, wrapped_allocation)) |
| 340 |
} |
| 341 |
|
| 342 |
fn create_placed_texture( |
| 343 |
&self, |
| 344 |
desc: &crate::TextureDescriptor<'_>, |
| 345 |
raw_desc: Direct3D12::D3D12_RESOURCE_DESC, |
| 346 |
allocation_info: Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO, |
| 347 |
location: MemoryLocation, |
| 348 |
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> { |
| 349 |
let name = desc.label.unwrap_or("Unlabeled texture"); |
| 350 |
|
| 351 |
let mut allocator = self.mem_allocator.inner.lock(); |
| 352 |
|
| 353 |
let allocation_desc = AllocationCreateDesc { |
| 354 |
name, |
| 355 |
location, |
| 356 |
size: allocation_info.SizeInBytes, |
| 357 |
alignment: allocation_info.Alignment, |
| 358 |
resource_category: gpu_allocator::d3d12::ResourceCategory::from(&raw_desc), |
| 359 |
}; |
| 360 |
|
| 361 |
let allocation = allocator.allocate(&allocation_desc)?; |
| 362 |
let mut resource = None; |
| 363 |
unsafe { |
| 364 |
self.raw.CreatePlacedResource( |
| 365 |
allocation.heap(), |
| 366 |
allocation.offset(), |
| 367 |
&raw_desc, |
| 368 |
Direct3D12::D3D12_RESOURCE_STATE_COMMON, |
| 369 |
None, // clear value |
| 370 |
&mut resource, |
| 371 |
) |
| 372 |
} |
| 373 |
.into_device_result("Placed texture creation")?; |
| 374 |
|
| 375 |
let resource = resource.ok_or(crate::DeviceError::Unexpected)?; |
| 376 |
let wrapped_allocation = Allocation::placed(allocation, AllocationType::Texture); |
| 377 |
|
| 378 |
Ok((resource, wrapped_allocation)) |
| 379 |
} |
| 380 |
|
| 381 |
fn create_placed_acceleration_structure( |
| 382 |
&self, |
| 383 |
desc: &crate::AccelerationStructureDescriptor<'_>, |
| 384 |
raw_desc: Direct3D12::D3D12_RESOURCE_DESC, |
| 385 |
allocation_info: Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO, |
| 386 |
location: MemoryLocation, |
| 387 |
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> { |
| 388 |
let name = desc.label.unwrap_or("Unlabeled acceleration structure"); |
| 389 |
|
| 390 |
let mut allocator = self.mem_allocator.inner.lock(); |
| 391 |
|
| 392 |
let allocation_desc = AllocationCreateDesc { |
| 393 |
name, |
| 394 |
location, |
| 395 |
size: allocation_info.SizeInBytes, |
| 396 |
alignment: allocation_info.Alignment, |
| 397 |
resource_category: gpu_allocator::d3d12::ResourceCategory::from(&raw_desc), |
| 398 |
}; |
| 399 |
|
| 400 |
let allocation = allocator.allocate(&allocation_desc)?; |
| 401 |
let mut resource = None; |
| 402 |
unsafe { |
| 403 |
self.raw.CreatePlacedResource( |
| 404 |
allocation.heap(), |
| 405 |
allocation.offset(), |
| 406 |
&raw_desc, |
| 407 |
Direct3D12::D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE, |
| 408 |
None, |
| 409 |
&mut resource, |
| 410 |
) |
| 411 |
} |
| 412 |
.into_device_result("Placed acceleration structure creation")?; |
| 413 |
|
| 414 |
let resource = resource.ok_or(crate::DeviceError::Unexpected)?; |
| 415 |
let wrapped_allocation = |
| 416 |
Allocation::placed(allocation, AllocationType::AccelerationStructure); |
| 417 |
|
| 418 |
Ok((resource, wrapped_allocation)) |
| 419 |
} |
| 420 |
|
| 421 |
///////////////////////////////// |
| 422 |
// Committed Resource Creation // |
| 423 |
///////////////////////////////// |
| 424 |
|
| 425 |
fn create_committed_buffer( |
| 426 |
&self, |
| 427 |
raw_desc: Direct3D12::D3D12_RESOURCE_DESC, |
| 428 |
location: MemoryLocation, |
| 429 |
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> { |
| 430 |
let is_uma = matches!( |
| 431 |
self.shared.private_caps.memory_architecture, |
| 432 |
crate::dx12::MemoryArchitecture::Unified { .. } |
| 433 |
); |
| 434 |
|
| 435 |
let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES { |
| 436 |
Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM, |
| 437 |
CPUPageProperty: match location { |
| 438 |
MemoryLocation::GpuOnly => Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE, |
| 439 |
MemoryLocation::CpuToGpu => Direct3D12::D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE, |
| 440 |
MemoryLocation::GpuToCpu => Direct3D12::D3D12_CPU_PAGE_PROPERTY_WRITE_BACK, |
| 441 |
_ => unreachable!(), |
| 442 |
}, |
| 443 |
MemoryPoolPreference: match (is_uma, location) { |
| 444 |
// On dedicated GPUs, we only use L1 for GPU-only allocations. |
| 445 |
(false, MemoryLocation::GpuOnly) => Direct3D12::D3D12_MEMORY_POOL_L1, |
| 446 |
(_, _) => Direct3D12::D3D12_MEMORY_POOL_L0, |
| 447 |
}, |
| 448 |
CreationNodeMask: 0, |
| 449 |
VisibleNodeMask: 0, |
| 450 |
}; |
| 451 |
|
| 452 |
let mut resource = None; |
| 453 |
|
| 454 |
unsafe { |
| 455 |
self.raw.CreateCommittedResource( |
| 456 |
&heap_properties, |
| 457 |
if self.shared.private_caps.heap_create_not_zeroed { |
| 458 |
Direct3D12::D3D12_HEAP_FLAG_CREATE_NOT_ZEROED |
| 459 |
} else { |
| 460 |
Direct3D12::D3D12_HEAP_FLAG_NONE |
| 461 |
}, |
| 462 |
&raw_desc, |
| 463 |
Direct3D12::D3D12_RESOURCE_STATE_COMMON, |
| 464 |
None, |
| 465 |
&mut resource, |
| 466 |
) |
| 467 |
} |
| 468 |
.into_device_result("Committed buffer creation")?; |
| 469 |
|
| 470 |
let resource = resource.ok_or(crate::DeviceError::Unexpected)?; |
| 471 |
let wrapped_allocation = Allocation::none(AllocationType::Buffer, raw_desc.Width); |
| 472 |
|
| 473 |
Ok((resource, wrapped_allocation)) |
| 474 |
} |
| 475 |
|
| 476 |
fn create_committed_texture( |
| 477 |
&self, |
| 478 |
desc: &crate::TextureDescriptor, |
| 479 |
raw_desc: Direct3D12::D3D12_RESOURCE_DESC, |
| 480 |
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> { |
| 481 |
let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES { |
| 482 |
Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM, |
| 483 |
CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE, |
| 484 |
MemoryPoolPreference: match self.shared.private_caps.memory_architecture { |
| 485 |
crate::dx12::MemoryArchitecture::NonUnified => Direct3D12::D3D12_MEMORY_POOL_L1, |
| 486 |
crate::dx12::MemoryArchitecture::Unified { .. } => Direct3D12::D3D12_MEMORY_POOL_L0, |
| 487 |
}, |
| 488 |
CreationNodeMask: 0, |
| 489 |
VisibleNodeMask: 0, |
| 490 |
}; |
| 491 |
|
| 492 |
let mut resource = None; |
| 493 |
|
| 494 |
unsafe { |
| 495 |
self.raw.CreateCommittedResource( |
| 496 |
&heap_properties, |
| 497 |
if self.shared.private_caps.heap_create_not_zeroed { |
| 498 |
Direct3D12::D3D12_HEAP_FLAG_CREATE_NOT_ZEROED |
| 499 |
} else { |
| 500 |
Direct3D12::D3D12_HEAP_FLAG_NONE |
| 501 |
}, |
| 502 |
&raw_desc, |
| 503 |
Direct3D12::D3D12_RESOURCE_STATE_COMMON, |
| 504 |
None, // clear value |
| 505 |
&mut resource, |
| 506 |
) |
| 507 |
} |
| 508 |
.into_device_result("Committed texture creation")?; |
| 509 |
|
| 510 |
let resource = resource.ok_or(crate::DeviceError::Unexpected)?; |
| 511 |
let wrapped_allocation = Allocation::none( |
| 512 |
AllocationType::Texture, |
| 513 |
desc.format.theoretical_memory_footprint(desc.size), |
| 514 |
); |
| 515 |
|
| 516 |
Ok((resource, wrapped_allocation)) |
| 517 |
} |
| 518 |
|
| 519 |
fn create_committed_acceleration_structure( |
| 520 |
&self, |
| 521 |
desc: &crate::AccelerationStructureDescriptor, |
| 522 |
raw_desc: Direct3D12::D3D12_RESOURCE_DESC, |
| 523 |
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> { |
| 524 |
let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES { |
| 525 |
Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM, |
| 526 |
CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE, |
| 527 |
MemoryPoolPreference: match self.shared.private_caps.memory_architecture { |
| 528 |
crate::dx12::MemoryArchitecture::NonUnified => Direct3D12::D3D12_MEMORY_POOL_L1, |
| 529 |
crate::dx12::MemoryArchitecture::Unified { .. } => Direct3D12::D3D12_MEMORY_POOL_L0, |
| 530 |
}, |
| 531 |
CreationNodeMask: 0, |
| 532 |
VisibleNodeMask: 0, |
| 533 |
}; |
| 534 |
|
| 535 |
let mut resource = None; |
| 536 |
|
| 537 |
unsafe { |
| 538 |
self.raw.CreateCommittedResource( |
| 539 |
&heap_properties, |
| 540 |
if self.shared.private_caps.heap_create_not_zeroed { |
| 541 |
Direct3D12::D3D12_HEAP_FLAG_CREATE_NOT_ZEROED |
| 542 |
} else { |
| 543 |
Direct3D12::D3D12_HEAP_FLAG_NONE |
| 544 |
}, |
| 545 |
&raw_desc, |
| 546 |
Direct3D12::D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE, |
| 547 |
None, |
| 548 |
&mut resource, |
| 549 |
) |
| 550 |
} |
| 551 |
.into_device_result("Committed acceleration structure creation")?; |
| 552 |
|
| 553 |
let resource = resource.ok_or(crate::DeviceError::Unexpected)?; |
| 554 |
let wrapped_allocation = Allocation::none(AllocationType::AccelerationStructure, desc.size); |
| 555 |
|
| 556 |
Ok((resource, wrapped_allocation)) |
| 557 |
} |
| 558 |
|
| 559 |
fn error_if_would_oom_on_resource_allocation( |
| 560 |
&self, |
| 561 |
desc: &Direct3D12::D3D12_RESOURCE_DESC, |
| 562 |
location: MemoryLocation, |
| 563 |
) -> Result<Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO, crate::DeviceError> { |
| 564 |
let allocation_info = unsafe { |
| 565 |
self.raw |
| 566 |
.GetResourceAllocationInfo(0, core::slice::from_ref(desc)) |
| 567 |
}; |
| 568 |
|
| 569 |
let Some(threshold) = self |
| 570 |
.mem_allocator |
| 571 |
.memory_budget_thresholds |
| 572 |
.for_resource_creation |
| 573 |
else { |
| 574 |
return Ok(allocation_info); |
| 575 |
}; |
| 576 |
|
| 577 |
let memory_segment_group = match location { |
| 578 |
MemoryLocation::Unknown => unreachable!(), |
| 579 |
MemoryLocation::GpuOnly => Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL, |
| 580 |
MemoryLocation::CpuToGpu | MemoryLocation::GpuToCpu => { |
| 581 |
match self.shared.private_caps.memory_architecture { |
| 582 |
super::MemoryArchitecture::Unified { .. } => { |
| 583 |
Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL |
| 584 |
} |
| 585 |
super::MemoryArchitecture::NonUnified => { |
| 586 |
Dxgi::DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
}; |
| 591 |
|
| 592 |
let info = self |
| 593 |
.shared |
| 594 |
.adapter |
| 595 |
.query_video_memory_info(memory_segment_group)?; |
| 596 |
|
| 597 |
let memblock_size = match location { |
| 598 |
MemoryLocation::Unknown => unreachable!(), |
| 599 |
MemoryLocation::GpuOnly => self.mem_allocator.device_memblock_size, |
| 600 |
MemoryLocation::CpuToGpu | MemoryLocation::GpuToCpu => { |
| 601 |
self.mem_allocator.host_memblock_size |
| 602 |
} |
| 603 |
}; |
| 604 |
|
| 605 |
if info.CurrentUsage + allocation_info.SizeInBytes.max(memblock_size) |
| 606 |
>= info.Budget / 100 * threshold as u64 |
| 607 |
{ |
| 608 |
return Err(crate::DeviceError::OutOfMemory); |
| 609 |
} |
| 610 |
|
| 611 |
Ok(allocation_info) |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
impl From<gpu_allocator::AllocationError> for crate::DeviceError { |
| 616 |
fn from(result: gpu_allocator::AllocationError) -> Self { |
| 617 |
match result { |
| 618 |
gpu_allocator::AllocationError::OutOfMemory => Self::OutOfMemory, |
| 619 |
gpu_allocator::AllocationError::FailedToMap(e) => { |
| 620 |
log::error!("DX12 gpu-allocator: Failed to map: {}", e); |
| 621 |
Self::Lost |
| 622 |
} |
| 623 |
gpu_allocator::AllocationError::NoCompatibleMemoryTypeFound => { |
| 624 |
log::error!("DX12 gpu-allocator: No Compatible Memory Type Found"); |
| 625 |
Self::Lost |
| 626 |
} |
| 627 |
gpu_allocator::AllocationError::InvalidAllocationCreateDesc => { |
| 628 |
log::error!("DX12 gpu-allocator: Invalid Allocation Creation Description"); |
| 629 |
Self::Lost |
| 630 |
} |
| 631 |
gpu_allocator::AllocationError::InvalidAllocatorCreateDesc(e) => { |
| 632 |
log::error!( |
| 633 |
"DX12 gpu-allocator: Invalid Allocator Creation Description: {}", |
| 634 |
e |
| 635 |
); |
| 636 |
Self::Lost |
| 637 |
} |
| 638 |
|
| 639 |
gpu_allocator::AllocationError::Internal(e) => { |
| 640 |
log::error!("DX12 gpu-allocator: Internal Error: {}", e); |
| 641 |
Self::Lost |
| 642 |
} |
| 643 |
gpu_allocator::AllocationError::BarrierLayoutNeedsDevice10 |
| 644 |
| gpu_allocator::AllocationError::CastableFormatsRequiresEnhancedBarriers |
| 645 |
| gpu_allocator::AllocationError::CastableFormatsRequiresAtLeastDevice12 => { |
| 646 |
unreachable!() |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
} |