| 1 |
use alloc::vec::Vec; |
| 2 |
use core::{mem, ops::Range}; |
| 3 |
|
| 4 |
use windows::Win32::{ |
| 5 |
Foundation, |
| 6 |
Graphics::{Direct3D12, Dxgi}, |
| 7 |
}; |
| 8 |
use windows_core::Interface; |
| 9 |
|
| 10 |
use super::conv; |
| 11 |
use crate::{ |
| 12 |
auxil::{ |
| 13 |
self, |
| 14 |
dxgi::{name::ObjectExt, result::HResult as _}, |
| 15 |
}, |
| 16 |
dx12::borrow_interface_temporarily, |
| 17 |
AccelerationStructureEntries, |
| 18 |
}; |
| 19 |
|
| 20 |
fn make_box(origin: &wgt::Origin3d, size: &crate::CopyExtent) -> Direct3D12::D3D12_BOX { |
| 21 |
Direct3D12::D3D12_BOX { |
| 22 |
left: origin.x, |
| 23 |
top: origin.y, |
| 24 |
right: origin.x + size.width, |
| 25 |
bottom: origin.y + size.height, |
| 26 |
front: origin.z, |
| 27 |
back: origin.z + size.depth, |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl crate::BufferTextureCopy { |
| 32 |
fn to_subresource_footprint( |
| 33 |
&self, |
| 34 |
format: wgt::TextureFormat, |
| 35 |
) -> Direct3D12::D3D12_PLACED_SUBRESOURCE_FOOTPRINT { |
| 36 |
let (block_width, _) = format.block_dimensions(); |
| 37 |
Direct3D12::D3D12_PLACED_SUBRESOURCE_FOOTPRINT { |
| 38 |
Offset: self.buffer_layout.offset, |
| 39 |
Footprint: Direct3D12::D3D12_SUBRESOURCE_FOOTPRINT { |
| 40 |
Format: auxil::dxgi::conv::map_texture_format_for_copy( |
| 41 |
format, |
| 42 |
self.texture_base.aspect, |
| 43 |
) |
| 44 |
.unwrap(), |
| 45 |
Width: self.size.width, |
| 46 |
Height: self.size.height, |
| 47 |
Depth: self.size.depth, |
| 48 |
RowPitch: { |
| 49 |
let actual = self.buffer_layout.bytes_per_row.unwrap_or_else(|| { |
| 50 |
// this may happen for single-line updates |
| 51 |
let block_size = format |
| 52 |
.block_copy_size(Some(self.texture_base.aspect.map())) |
| 53 |
.unwrap(); |
| 54 |
(self.size.width / block_width) * block_size |
| 55 |
}); |
| 56 |
wgt::math::align_to(actual, Direct3D12::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) |
| 57 |
}, |
| 58 |
}, |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl super::Temp { |
| 64 |
fn prepare_marker(&mut self, marker: &str) -> (&[u16], u32) { |
| 65 |
self.marker.clear(); |
| 66 |
self.marker.extend(marker.encode_utf16()); |
| 67 |
self.marker.push(0); |
| 68 |
(&self.marker, self.marker.len() as u32 * 2) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl Drop for super::CommandEncoder { |
| 73 |
fn drop(&mut self) { |
| 74 |
use crate::CommandEncoder; |
| 75 |
unsafe { self.discard_encoding() } |
| 76 |
|
| 77 |
let mut rtv_pool = self.rtv_pool.lock(); |
| 78 |
for handle in self.temp_rtv_handles.drain(..) { |
| 79 |
rtv_pool.free_handle(handle); |
| 80 |
} |
| 81 |
drop(rtv_pool); |
| 82 |
|
| 83 |
self.counters.command_encoders.sub(1); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
impl super::CommandEncoder { |
| 88 |
unsafe fn begin_pass(&mut self, kind: super::PassKind, label: crate::Label) { |
| 89 |
let list = self.list.as_ref().unwrap(); |
| 90 |
self.pass.kind = kind; |
| 91 |
if let Some(label) = label { |
| 92 |
let (wide_label, size) = self.temp.prepare_marker(label); |
| 93 |
unsafe { list.BeginEvent(0, Some(wide_label.as_ptr().cast()), size) }; |
| 94 |
self.pass.has_label = true; |
| 95 |
} |
| 96 |
self.pass.dirty_root_elements = 0; |
| 97 |
self.pass.dirty_vertex_buffers = 0; |
| 98 |
unsafe { |
| 99 |
list.SetDescriptorHeaps(&[ |
| 100 |
Some(self.shared.heap_views.raw.clone()), |
| 101 |
Some(self.shared.sampler_heap.heap().clone()), |
| 102 |
]) |
| 103 |
}; |
| 104 |
} |
| 105 |
|
| 106 |
unsafe fn end_pass(&mut self) { |
| 107 |
let list = self.list.as_ref().unwrap(); |
| 108 |
unsafe { list.SetDescriptorHeaps(&[]) }; |
| 109 |
if self.pass.has_label { |
| 110 |
unsafe { list.EndEvent() }; |
| 111 |
} |
| 112 |
self.pass.clear(); |
| 113 |
} |
| 114 |
|
| 115 |
unsafe fn prepare_vertex_buffers(&mut self) { |
| 116 |
while self.pass.dirty_vertex_buffers != 0 { |
| 117 |
let list = self.list.as_ref().unwrap(); |
| 118 |
let index = self.pass.dirty_vertex_buffers.trailing_zeros(); |
| 119 |
self.pass.dirty_vertex_buffers ^= 1 << index; |
| 120 |
unsafe { |
| 121 |
list.IASetVertexBuffers( |
| 122 |
index, |
| 123 |
Some(&self.pass.vertex_buffers[index as usize..][..1]), |
| 124 |
); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
unsafe fn prepare_draw(&mut self, first_vertex: i32, first_instance: u32) { |
| 130 |
unsafe { |
| 131 |
self.prepare_vertex_buffers(); |
| 132 |
} |
| 133 |
if let Some(root_index) = self |
| 134 |
.pass |
| 135 |
.layout |
| 136 |
.special_constants |
| 137 |
.as_ref() |
| 138 |
.map(|sc| sc.root_index) |
| 139 |
{ |
| 140 |
let needs_update = match self.pass.root_elements[root_index as usize] { |
| 141 |
super::RootElement::SpecialConstantBuffer { |
| 142 |
first_vertex: other_vertex, |
| 143 |
first_instance: other_instance, |
| 144 |
other: _, |
| 145 |
} => first_vertex != other_vertex || first_instance != other_instance, |
| 146 |
_ => true, |
| 147 |
}; |
| 148 |
if needs_update { |
| 149 |
self.pass.dirty_root_elements |= 1 << root_index; |
| 150 |
self.pass.root_elements[root_index as usize] = |
| 151 |
super::RootElement::SpecialConstantBuffer { |
| 152 |
first_vertex, |
| 153 |
first_instance, |
| 154 |
other: 0, |
| 155 |
}; |
| 156 |
} |
| 157 |
} |
| 158 |
self.update_root_elements(); |
| 159 |
} |
| 160 |
|
| 161 |
fn prepare_dispatch(&mut self, count: [u32; 3]) { |
| 162 |
if let Some(root_index) = self |
| 163 |
.pass |
| 164 |
.layout |
| 165 |
.special_constants |
| 166 |
.as_ref() |
| 167 |
.map(|sc| sc.root_index) |
| 168 |
{ |
| 169 |
let needs_update = match self.pass.root_elements[root_index as usize] { |
| 170 |
super::RootElement::SpecialConstantBuffer { |
| 171 |
first_vertex, |
| 172 |
first_instance, |
| 173 |
other, |
| 174 |
} => [first_vertex as u32, first_instance, other] != count, |
| 175 |
_ => true, |
| 176 |
}; |
| 177 |
if needs_update { |
| 178 |
self.pass.dirty_root_elements |= 1 << root_index; |
| 179 |
self.pass.root_elements[root_index as usize] = |
| 180 |
super::RootElement::SpecialConstantBuffer { |
| 181 |
first_vertex: count[0] as i32, |
| 182 |
first_instance: count[1], |
| 183 |
other: count[2], |
| 184 |
}; |
| 185 |
} |
| 186 |
} |
| 187 |
self.update_root_elements(); |
| 188 |
} |
| 189 |
|
| 190 |
// Note: we have to call this lazily before draw calls. Otherwise, D3D complains |
| 191 |
// about the root parameters being incompatible with root signature. |
| 192 |
fn update_root_elements(&mut self) { |
| 193 |
use super::PassKind as Pk; |
| 194 |
|
| 195 |
while self.pass.dirty_root_elements != 0 { |
| 196 |
let list = self.list.as_ref().unwrap(); |
| 197 |
let index = self.pass.dirty_root_elements.trailing_zeros(); |
| 198 |
self.pass.dirty_root_elements ^= 1 << index; |
| 199 |
|
| 200 |
match self.pass.root_elements[index as usize] { |
| 201 |
super::RootElement::Empty => log::error!("Root index {} is not bound", index), |
| 202 |
super::RootElement::Constant => { |
| 203 |
let info = self.pass.layout.root_constant_info.as_ref().unwrap(); |
| 204 |
|
| 205 |
for offset in info.range.clone() { |
| 206 |
let val = self.pass.constant_data[offset as usize]; |
| 207 |
match self.pass.kind { |
| 208 |
Pk::Render => unsafe { |
| 209 |
list.SetGraphicsRoot32BitConstant(index, val, offset) |
| 210 |
}, |
| 211 |
Pk::Compute => unsafe { |
| 212 |
list.SetComputeRoot32BitConstant(index, val, offset) |
| 213 |
}, |
| 214 |
Pk::Transfer => (), |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
super::RootElement::SpecialConstantBuffer { |
| 219 |
first_vertex, |
| 220 |
first_instance, |
| 221 |
other, |
| 222 |
} => match self.pass.kind { |
| 223 |
Pk::Render => { |
| 224 |
unsafe { list.SetGraphicsRoot32BitConstant(index, first_vertex as u32, 0) }; |
| 225 |
unsafe { list.SetGraphicsRoot32BitConstant(index, first_instance, 1) }; |
| 226 |
} |
| 227 |
Pk::Compute => { |
| 228 |
unsafe { list.SetComputeRoot32BitConstant(index, first_vertex as u32, 0) }; |
| 229 |
unsafe { list.SetComputeRoot32BitConstant(index, first_instance, 1) }; |
| 230 |
unsafe { list.SetComputeRoot32BitConstant(index, other, 2) }; |
| 231 |
} |
| 232 |
Pk::Transfer => (), |
| 233 |
}, |
| 234 |
super::RootElement::Table(descriptor) => match self.pass.kind { |
| 235 |
Pk::Render => unsafe { list.SetGraphicsRootDescriptorTable(index, descriptor) }, |
| 236 |
Pk::Compute => unsafe { list.SetComputeRootDescriptorTable(index, descriptor) }, |
| 237 |
Pk::Transfer => (), |
| 238 |
}, |
| 239 |
super::RootElement::DynamicUniformBuffer { address } => { |
| 240 |
let address = address.ptr; |
| 241 |
match self.pass.kind { |
| 242 |
Pk::Render => unsafe { |
| 243 |
list.SetGraphicsRootConstantBufferView(index, address) |
| 244 |
}, |
| 245 |
Pk::Compute => unsafe { |
| 246 |
list.SetComputeRootConstantBufferView(index, address) |
| 247 |
}, |
| 248 |
Pk::Transfer => (), |
| 249 |
} |
| 250 |
} |
| 251 |
super::RootElement::DynamicOffsetsBuffer { start, end } => { |
| 252 |
let values = &self.pass.dynamic_storage_buffer_offsets[start..end]; |
| 253 |
|
| 254 |
for (offset, &value) in values.iter().enumerate() { |
| 255 |
match self.pass.kind { |
| 256 |
Pk::Render => unsafe { |
| 257 |
list.SetGraphicsRoot32BitConstant(index, value, offset as u32) |
| 258 |
}, |
| 259 |
Pk::Compute => unsafe { |
| 260 |
list.SetComputeRoot32BitConstant(index, value, offset as u32) |
| 261 |
}, |
| 262 |
Pk::Transfer => (), |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
super::RootElement::SamplerHeap => match self.pass.kind { |
| 267 |
Pk::Render => unsafe { |
| 268 |
list.SetGraphicsRootDescriptorTable( |
| 269 |
index, |
| 270 |
self.shared.sampler_heap.gpu_descriptor_table(), |
| 271 |
) |
| 272 |
}, |
| 273 |
Pk::Compute => unsafe { |
| 274 |
list.SetComputeRootDescriptorTable( |
| 275 |
index, |
| 276 |
self.shared.sampler_heap.gpu_descriptor_table(), |
| 277 |
) |
| 278 |
}, |
| 279 |
Pk::Transfer => (), |
| 280 |
}, |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
fn reset_signature(&mut self, layout: &super::PipelineLayoutShared) { |
| 286 |
if let Some(root_index) = layout.special_constants.as_ref().map(|sc| sc.root_index) { |
| 287 |
self.pass.root_elements[root_index as usize] = |
| 288 |
super::RootElement::SpecialConstantBuffer { |
| 289 |
first_vertex: 0, |
| 290 |
first_instance: 0, |
| 291 |
other: 0, |
| 292 |
}; |
| 293 |
} |
| 294 |
if let Some(root_index) = layout.sampler_heap_root_index { |
| 295 |
self.pass.root_elements[root_index as usize] = super::RootElement::SamplerHeap; |
| 296 |
} |
| 297 |
self.pass.layout = layout.clone(); |
| 298 |
self.pass.dirty_root_elements = (1 << layout.total_root_elements) - 1; |
| 299 |
} |
| 300 |
|
| 301 |
fn write_pass_end_timestamp_if_requested(&mut self) { |
| 302 |
if let Some((query_set_raw, index)) = self.end_of_pass_timer_query.take() { |
| 303 |
use crate::CommandEncoder as _; |
| 304 |
unsafe { |
| 305 |
self.write_timestamp( |
| 306 |
&crate::dx12::QuerySet { |
| 307 |
raw: query_set_raw, |
| 308 |
raw_ty: Direct3D12::D3D12_QUERY_TYPE_TIMESTAMP, |
| 309 |
}, |
| 310 |
index, |
| 311 |
); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
impl crate::CommandEncoder for super::CommandEncoder { |
| 318 |
type A = super::Api; |
| 319 |
|
| 320 |
unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> { |
| 321 |
let list = loop { |
| 322 |
if let Some(list) = self.free_lists.pop() { |
| 323 |
// TODO: Is an error expected here and should we print it? |
| 324 |
let reset_result = unsafe { list.Reset(&self.allocator, None) }; |
| 325 |
if reset_result.is_ok() { |
| 326 |
break Some(list); |
| 327 |
} |
| 328 |
} else { |
| 329 |
break None; |
| 330 |
} |
| 331 |
}; |
| 332 |
|
| 333 |
let list = if let Some(list) = list { |
| 334 |
list |
| 335 |
} else { |
| 336 |
unsafe { |
| 337 |
self.device.CreateCommandList( |
| 338 |
0, |
| 339 |
Direct3D12::D3D12_COMMAND_LIST_TYPE_DIRECT, |
| 340 |
&self.allocator, |
| 341 |
None, |
| 342 |
) |
| 343 |
} |
| 344 |
.into_device_result("Create command list")? |
| 345 |
}; |
| 346 |
|
| 347 |
if let Some(label) = label { |
| 348 |
list.set_name(label)?; |
| 349 |
} |
| 350 |
|
| 351 |
self.list = Some(list); |
| 352 |
self.temp.clear(); |
| 353 |
self.pass.clear(); |
| 354 |
Ok(()) |
| 355 |
} |
| 356 |
unsafe fn discard_encoding(&mut self) { |
| 357 |
if let Some(list) = self.list.take() { |
| 358 |
if unsafe { list.Close() }.is_ok() { |
| 359 |
self.free_lists.push(list); |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> { |
| 364 |
let raw = self.list.take().unwrap(); |
| 365 |
unsafe { raw.Close() }.into_device_result("GraphicsCommandList::close")?; |
| 366 |
Ok(super::CommandBuffer { raw }) |
| 367 |
} |
| 368 |
unsafe fn reset_all<I: Iterator<Item = super::CommandBuffer>>(&mut self, command_buffers: I) { |
| 369 |
for cmd_buf in command_buffers { |
| 370 |
self.free_lists.push(cmd_buf.raw); |
| 371 |
} |
| 372 |
if let Err(e) = unsafe { self.allocator.Reset() } { |
| 373 |
log::error!("ID3D12CommandAllocator::Reset() failed with {e}"); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
unsafe fn transition_buffers<'a, T>(&mut self, barriers: T) |
| 378 |
where |
| 379 |
T: Iterator<Item = crate::BufferBarrier<'a, super::Buffer>>, |
| 380 |
{ |
| 381 |
self.temp.barriers.clear(); |
| 382 |
|
| 383 |
for barrier in barriers { |
| 384 |
let s0 = conv::map_buffer_usage_to_state(barrier.usage.from); |
| 385 |
let s1 = conv::map_buffer_usage_to_state(barrier.usage.to); |
| 386 |
if s0 != s1 { |
| 387 |
let raw = Direct3D12::D3D12_RESOURCE_BARRIER { |
| 388 |
Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, |
| 389 |
Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, |
| 390 |
Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { |
| 391 |
Transition: mem::ManuallyDrop::new( |
| 392 |
Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER { |
| 393 |
pResource: unsafe { |
| 394 |
borrow_interface_temporarily(&barrier.buffer.resource) |
| 395 |
}, |
| 396 |
Subresource: Direct3D12::D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, |
| 397 |
StateBefore: s0, |
| 398 |
StateAfter: s1, |
| 399 |
}, |
| 400 |
), |
| 401 |
}, |
| 402 |
}; |
| 403 |
self.temp.barriers.push(raw); |
| 404 |
} else if barrier.usage.from == wgt::BufferUses::STORAGE_READ_WRITE |
| 405 |
|| barrier.usage.from == wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY |
| 406 |
{ |
| 407 |
let raw = Direct3D12::D3D12_RESOURCE_BARRIER { |
| 408 |
Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV, |
| 409 |
Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, |
| 410 |
Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { |
| 411 |
UAV: mem::ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { |
| 412 |
pResource: unsafe { |
| 413 |
borrow_interface_temporarily(&barrier.buffer.resource) |
| 414 |
}, |
| 415 |
}), |
| 416 |
}, |
| 417 |
}; |
| 418 |
self.temp.barriers.push(raw); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
if !self.temp.barriers.is_empty() { |
| 423 |
unsafe { |
| 424 |
self.list |
| 425 |
.as_ref() |
| 426 |
.unwrap() |
| 427 |
.ResourceBarrier(&self.temp.barriers) |
| 428 |
}; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
unsafe fn transition_textures<'a, T>(&mut self, barriers: T) |
| 433 |
where |
| 434 |
T: Iterator<Item = crate::TextureBarrier<'a, super::Texture>>, |
| 435 |
{ |
| 436 |
self.temp.barriers.clear(); |
| 437 |
|
| 438 |
for barrier in barriers { |
| 439 |
let s0 = conv::map_texture_usage_to_state(barrier.usage.from); |
| 440 |
let s1 = conv::map_texture_usage_to_state(barrier.usage.to); |
| 441 |
if s0 != s1 { |
| 442 |
let mut raw = Direct3D12::D3D12_RESOURCE_BARRIER { |
| 443 |
Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, |
| 444 |
Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, |
| 445 |
Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { |
| 446 |
Transition: mem::ManuallyDrop::new( |
| 447 |
Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER { |
| 448 |
pResource: unsafe { |
| 449 |
borrow_interface_temporarily(&barrier.texture.resource) |
| 450 |
}, |
| 451 |
Subresource: Direct3D12::D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, |
| 452 |
StateBefore: s0, |
| 453 |
StateAfter: s1, |
| 454 |
}, |
| 455 |
), |
| 456 |
}, |
| 457 |
}; |
| 458 |
|
| 459 |
let tex_mip_level_count = barrier.texture.mip_level_count; |
| 460 |
let tex_array_layer_count = barrier.texture.array_layer_count(); |
| 461 |
|
| 462 |
if barrier.range.is_full_resource( |
| 463 |
barrier.texture.format, |
| 464 |
tex_mip_level_count, |
| 465 |
tex_array_layer_count, |
| 466 |
) { |
| 467 |
// Only one barrier if it affects the whole image. |
| 468 |
self.temp.barriers.push(raw); |
| 469 |
} else { |
| 470 |
// Selected texture aspect is relevant if the texture format has both depth _and_ stencil aspects. |
| 471 |
let planes = if barrier.texture.format.is_combined_depth_stencil_format() { |
| 472 |
match barrier.range.aspect { |
| 473 |
wgt::TextureAspect::All => 0..2, |
| 474 |
wgt::TextureAspect::DepthOnly => 0..1, |
| 475 |
wgt::TextureAspect::StencilOnly => 1..2, |
| 476 |
_ => unreachable!(), |
| 477 |
} |
| 478 |
} else if let Some(planes) = barrier.texture.format.planes() { |
| 479 |
match barrier.range.aspect { |
| 480 |
wgt::TextureAspect::All => 0..planes, |
| 481 |
wgt::TextureAspect::Plane0 => 0..1, |
| 482 |
wgt::TextureAspect::Plane1 => 1..2, |
| 483 |
wgt::TextureAspect::Plane2 => 2..3, |
| 484 |
_ => unreachable!(), |
| 485 |
} |
| 486 |
} else { |
| 487 |
match barrier.texture.format { |
| 488 |
wgt::TextureFormat::Stencil8 => 1..2, |
| 489 |
wgt::TextureFormat::Depth24Plus => 0..2, // TODO: investigate why tests fail if we set this to 0..1 |
| 490 |
_ => 0..1, |
| 491 |
} |
| 492 |
}; |
| 493 |
|
| 494 |
for mip_level in barrier.range.mip_range(tex_mip_level_count) { |
| 495 |
for array_layer in barrier.range.layer_range(tex_array_layer_count) { |
| 496 |
for plane in planes.clone() { |
| 497 |
unsafe { &mut *raw.Anonymous.Transition }.Subresource = barrier |
| 498 |
.texture |
| 499 |
.calc_subresource(mip_level, array_layer, plane); |
| 500 |
self.temp.barriers.push(raw.clone()); |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
} else if barrier.usage.from == wgt::TextureUses::STORAGE_READ_WRITE { |
| 506 |
let raw = Direct3D12::D3D12_RESOURCE_BARRIER { |
| 507 |
Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV, |
| 508 |
Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, |
| 509 |
Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { |
| 510 |
UAV: mem::ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { |
| 511 |
pResource: unsafe { |
| 512 |
borrow_interface_temporarily(&barrier.texture.resource) |
| 513 |
}, |
| 514 |
}), |
| 515 |
}, |
| 516 |
}; |
| 517 |
self.temp.barriers.push(raw); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
if !self.temp.barriers.is_empty() { |
| 522 |
unsafe { |
| 523 |
self.list |
| 524 |
.as_ref() |
| 525 |
.unwrap() |
| 526 |
.ResourceBarrier(&self.temp.barriers) |
| 527 |
}; |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
unsafe fn clear_buffer(&mut self, buffer: &super::Buffer, range: crate::MemoryRange) { |
| 532 |
let list = self.list.as_ref().unwrap(); |
| 533 |
let mut offset = range.start; |
| 534 |
while offset < range.end { |
| 535 |
let size = super::ZERO_BUFFER_SIZE.min(range.end - offset); |
| 536 |
unsafe { |
| 537 |
list.CopyBufferRegion(&buffer.resource, offset, &self.shared.zero_buffer, 0, size) |
| 538 |
}; |
| 539 |
offset += size; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
unsafe fn copy_buffer_to_buffer<T>( |
| 544 |
&mut self, |
| 545 |
src: &super::Buffer, |
| 546 |
dst: &super::Buffer, |
| 547 |
regions: T, |
| 548 |
) where |
| 549 |
T: Iterator<Item = crate::BufferCopy>, |
| 550 |
{ |
| 551 |
let list = self.list.as_ref().unwrap(); |
| 552 |
for r in regions { |
| 553 |
unsafe { |
| 554 |
list.CopyBufferRegion( |
| 555 |
&dst.resource, |
| 556 |
r.dst_offset, |
| 557 |
&src.resource, |
| 558 |
r.src_offset, |
| 559 |
r.size.get(), |
| 560 |
) |
| 561 |
}; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
unsafe fn copy_texture_to_texture<T>( |
| 566 |
&mut self, |
| 567 |
src: &super::Texture, |
| 568 |
_src_usage: wgt::TextureUses, |
| 569 |
dst: &super::Texture, |
| 570 |
regions: T, |
| 571 |
) where |
| 572 |
T: Iterator<Item = crate::TextureCopy>, |
| 573 |
{ |
| 574 |
let list = self.list.as_ref().unwrap(); |
| 575 |
|
| 576 |
for r in regions { |
| 577 |
let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { |
| 578 |
pResource: unsafe { borrow_interface_temporarily(&src.resource) }, |
| 579 |
Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, |
| 580 |
Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { |
| 581 |
SubresourceIndex: src.calc_subresource_for_copy(&r.src_base), |
| 582 |
}, |
| 583 |
}; |
| 584 |
let dst_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { |
| 585 |
pResource: unsafe { borrow_interface_temporarily(&dst.resource) }, |
| 586 |
Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, |
| 587 |
Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { |
| 588 |
SubresourceIndex: dst.calc_subresource_for_copy(&r.dst_base), |
| 589 |
}, |
| 590 |
}; |
| 591 |
|
| 592 |
let src_box = make_box(&r.src_base.origin, &r.size); |
| 593 |
|
| 594 |
unsafe { |
| 595 |
list.CopyTextureRegion( |
| 596 |
&dst_location, |
| 597 |
r.dst_base.origin.x, |
| 598 |
r.dst_base.origin.y, |
| 599 |
r.dst_base.origin.z, |
| 600 |
&src_location, |
| 601 |
Some(&src_box), |
| 602 |
) |
| 603 |
}; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
unsafe fn copy_buffer_to_texture<T>( |
| 608 |
&mut self, |
| 609 |
src: &super::Buffer, |
| 610 |
dst: &super::Texture, |
| 611 |
regions: T, |
| 612 |
) where |
| 613 |
T: Iterator<Item = crate::BufferTextureCopy>, |
| 614 |
{ |
| 615 |
let list = self.list.as_ref().unwrap(); |
| 616 |
for r in regions { |
| 617 |
let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { |
| 618 |
pResource: unsafe { borrow_interface_temporarily(&src.resource) }, |
| 619 |
Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, |
| 620 |
Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { |
| 621 |
PlacedFootprint: r.to_subresource_footprint(dst.format), |
| 622 |
}, |
| 623 |
}; |
| 624 |
let dst_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { |
| 625 |
pResource: unsafe { borrow_interface_temporarily(&dst.resource) }, |
| 626 |
Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, |
| 627 |
Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { |
| 628 |
SubresourceIndex: dst.calc_subresource_for_copy(&r.texture_base), |
| 629 |
}, |
| 630 |
}; |
| 631 |
|
| 632 |
let src_box = make_box(&wgt::Origin3d::ZERO, &r.size); |
| 633 |
unsafe { |
| 634 |
list.CopyTextureRegion( |
| 635 |
&dst_location, |
| 636 |
r.texture_base.origin.x, |
| 637 |
r.texture_base.origin.y, |
| 638 |
r.texture_base.origin.z, |
| 639 |
&src_location, |
| 640 |
Some(&src_box), |
| 641 |
) |
| 642 |
}; |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
unsafe fn copy_texture_to_buffer<T>( |
| 647 |
&mut self, |
| 648 |
src: &super::Texture, |
| 649 |
_src_usage: wgt::TextureUses, |
| 650 |
dst: &super::Buffer, |
| 651 |
regions: T, |
| 652 |
) where |
| 653 |
T: Iterator<Item = crate::BufferTextureCopy>, |
| 654 |
{ |
| 655 |
let list = self.list.as_ref().unwrap(); |
| 656 |
for r in regions { |
| 657 |
let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { |
| 658 |
pResource: unsafe { borrow_interface_temporarily(&src.resource) }, |
| 659 |
Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, |
| 660 |
Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { |
| 661 |
SubresourceIndex: src.calc_subresource_for_copy(&r.texture_base), |
| 662 |
}, |
| 663 |
}; |
| 664 |
let dst_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { |
| 665 |
pResource: unsafe { borrow_interface_temporarily(&dst.resource) }, |
| 666 |
Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, |
| 667 |
Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { |
| 668 |
PlacedFootprint: r.to_subresource_footprint(src.format), |
| 669 |
}, |
| 670 |
}; |
| 671 |
|
| 672 |
let src_box = make_box(&r.texture_base.origin, &r.size); |
| 673 |
unsafe { |
| 674 |
list.CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, Some(&src_box)) |
| 675 |
}; |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) { |
| 680 |
unsafe { |
| 681 |
self.list |
| 682 |
.as_ref() |
| 683 |
.unwrap() |
| 684 |
.BeginQuery(&set.raw, set.raw_ty, index) |
| 685 |
}; |
| 686 |
} |
| 687 |
unsafe fn end_query(&mut self, set: &super::QuerySet, index: u32) { |
| 688 |
unsafe { |
| 689 |
self.list |
| 690 |
.as_ref() |
| 691 |
.unwrap() |
| 692 |
.EndQuery(&set.raw, set.raw_ty, index) |
| 693 |
}; |
| 694 |
} |
| 695 |
unsafe fn write_timestamp(&mut self, set: &super::QuerySet, index: u32) { |
| 696 |
unsafe { |
| 697 |
self.list.as_ref().unwrap().EndQuery( |
| 698 |
&set.raw, |
| 699 |
Direct3D12::D3D12_QUERY_TYPE_TIMESTAMP, |
| 700 |
index, |
| 701 |
) |
| 702 |
}; |
| 703 |
} |
| 704 |
unsafe fn read_acceleration_structure_compact_size( |
| 705 |
&mut self, |
| 706 |
acceleration_structure: &super::AccelerationStructure, |
| 707 |
buf: &super::Buffer, |
| 708 |
) { |
| 709 |
let list = self |
| 710 |
.list |
| 711 |
.as_ref() |
| 712 |
.unwrap() |
| 713 |
.cast::<Direct3D12::ID3D12GraphicsCommandList4>() |
| 714 |
.unwrap(); |
| 715 |
unsafe { |
| 716 |
list.EmitRaytracingAccelerationStructurePostbuildInfo( |
| 717 |
&Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_DESC { |
| 718 |
DestBuffer: buf.resource.GetGPUVirtualAddress(), |
| 719 |
InfoType: Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE, |
| 720 |
}, |
| 721 |
&[ |
| 722 |
acceleration_structure.resource.GetGPUVirtualAddress() |
| 723 |
], |
| 724 |
) |
| 725 |
} |
| 726 |
} |
| 727 |
unsafe fn reset_queries(&mut self, _set: &super::QuerySet, _range: Range<u32>) { |
| 728 |
// nothing to do here |
| 729 |
} |
| 730 |
unsafe fn copy_query_results( |
| 731 |
&mut self, |
| 732 |
set: &super::QuerySet, |
| 733 |
range: Range<u32>, |
| 734 |
buffer: &super::Buffer, |
| 735 |
offset: wgt::BufferAddress, |
| 736 |
_stride: wgt::BufferSize, |
| 737 |
) { |
| 738 |
unsafe { |
| 739 |
self.list.as_ref().unwrap().ResolveQueryData( |
| 740 |
&set.raw, |
| 741 |
set.raw_ty, |
| 742 |
range.start, |
| 743 |
range.end - range.start, |
| 744 |
&buffer.resource, |
| 745 |
offset, |
| 746 |
) |
| 747 |
}; |
| 748 |
} |
| 749 |
|
| 750 |
// render |
| 751 |
|
| 752 |
unsafe fn begin_render_pass( |
| 753 |
&mut self, |
| 754 |
desc: &crate::RenderPassDescriptor<super::QuerySet, super::TextureView>, |
| 755 |
) -> Result<(), crate::DeviceError> { |
| 756 |
unsafe { self.begin_pass(super::PassKind::Render, desc.label) }; |
| 757 |
|
| 758 |
// Start timestamp if any (before all other commands but after debug marker) |
| 759 |
if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() { |
| 760 |
if let Some(index) = timestamp_writes.beginning_of_pass_write_index { |
| 761 |
unsafe { |
| 762 |
self.write_timestamp(timestamp_writes.query_set, index); |
| 763 |
} |
| 764 |
} |
| 765 |
self.end_of_pass_timer_query = timestamp_writes |
| 766 |
.end_of_pass_write_index |
| 767 |
.map(|index| (timestamp_writes.query_set.raw.clone(), index)); |
| 768 |
} |
| 769 |
|
| 770 |
let mut color_views = |
| 771 |
[Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE { ptr: 0 }; crate::MAX_COLOR_ATTACHMENTS]; |
| 772 |
let mut rtv_pool = self.rtv_pool.lock(); |
| 773 |
for (rtv, cat) in color_views.iter_mut().zip(desc.color_attachments.iter()) { |
| 774 |
if let Some(cat) = cat.as_ref() { |
| 775 |
if cat.target.view.dimension == wgt::TextureViewDimension::D3 { |
| 776 |
let desc = Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC { |
| 777 |
Format: cat.target.view.raw_format, |
| 778 |
ViewDimension: Direct3D12::D3D12_RTV_DIMENSION_TEXTURE3D, |
| 779 |
Anonymous: Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC_0 { |
| 780 |
Texture3D: Direct3D12::D3D12_TEX3D_RTV { |
| 781 |
MipSlice: cat.target.view.mip_slice, |
| 782 |
FirstWSlice: cat.depth_slice.unwrap(), |
| 783 |
WSize: 1, |
| 784 |
}, |
| 785 |
}, |
| 786 |
}; |
| 787 |
let handle = rtv_pool.alloc_handle()?; |
| 788 |
unsafe { |
| 789 |
self.device.CreateRenderTargetView( |
| 790 |
&cat.target.view.texture, |
| 791 |
Some(&desc), |
| 792 |
handle.raw, |
| 793 |
) |
| 794 |
}; |
| 795 |
*rtv = handle.raw; |
| 796 |
self.temp_rtv_handles.push(handle); |
| 797 |
} else { |
| 798 |
*rtv = cat.target.view.handle_rtv.unwrap().raw; |
| 799 |
} |
| 800 |
} else { |
| 801 |
*rtv = self.null_rtv_handle.raw; |
| 802 |
} |
| 803 |
} |
| 804 |
drop(rtv_pool); |
| 805 |
|
| 806 |
let ds_view = desc.depth_stencil_attachment.as_ref().map(|ds| { |
| 807 |
if ds.target.usage == wgt::TextureUses::DEPTH_STENCIL_WRITE { |
| 808 |
ds.target.view.handle_dsv_rw.as_ref().unwrap().raw |
| 809 |
} else { |
| 810 |
ds.target.view.handle_dsv_ro.as_ref().unwrap().raw |
| 811 |
} |
| 812 |
}); |
| 813 |
|
| 814 |
let list = self.list.as_ref().unwrap(); |
| 815 |
#[allow(trivial_casts)] // No other clean way to write the coercion inside .map() below? |
| 816 |
unsafe { |
| 817 |
list.OMSetRenderTargets( |
| 818 |
desc.color_attachments.len() as u32, |
| 819 |
Some(color_views.as_ptr()), |
| 820 |
false, |
| 821 |
ds_view.as_ref().map(core::ptr::from_ref), |
| 822 |
) |
| 823 |
}; |
| 824 |
|
| 825 |
self.pass.resolves.clear(); |
| 826 |
for (rtv, cat) in color_views.iter().zip(desc.color_attachments.iter()) { |
| 827 |
if let Some(cat) = cat.as_ref() { |
| 828 |
if !cat.ops.contains(crate::AttachmentOps::LOAD) { |
| 829 |
let value = [ |
| 830 |
cat.clear_value.r as f32, |
| 831 |
cat.clear_value.g as f32, |
| 832 |
cat.clear_value.b as f32, |
| 833 |
cat.clear_value.a as f32, |
| 834 |
]; |
| 835 |
unsafe { list.ClearRenderTargetView(*rtv, &value, None) }; |
| 836 |
} |
| 837 |
if let Some(ref target) = cat.resolve_target { |
| 838 |
self.pass.resolves.push(super::PassResolve { |
| 839 |
src: ( |
| 840 |
cat.target.view.texture.clone(), |
| 841 |
cat.target.view.subresource_index, |
| 842 |
), |
| 843 |
dst: (target.view.texture.clone(), target.view.subresource_index), |
| 844 |
format: target.view.raw_format, |
| 845 |
}); |
| 846 |
} |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
if let Some(ref ds) = desc.depth_stencil_attachment { |
| 851 |
let mut flags = Direct3D12::D3D12_CLEAR_FLAGS::default(); |
| 852 |
let aspects = ds.target.view.aspects; |
| 853 |
if !ds.depth_ops.contains(crate::AttachmentOps::LOAD) |
| 854 |
&& aspects.contains(crate::FormatAspects::DEPTH) |
| 855 |
{ |
| 856 |
flags |= Direct3D12::D3D12_CLEAR_FLAG_DEPTH; |
| 857 |
} |
| 858 |
if !ds.stencil_ops.contains(crate::AttachmentOps::LOAD) |
| 859 |
&& aspects.contains(crate::FormatAspects::STENCIL) |
| 860 |
{ |
| 861 |
flags |= Direct3D12::D3D12_CLEAR_FLAG_STENCIL; |
| 862 |
} |
| 863 |
|
| 864 |
if let Some(ds_view) = ds_view { |
| 865 |
if flags != Direct3D12::D3D12_CLEAR_FLAGS::default() { |
| 866 |
unsafe { |
| 867 |
// list.ClearDepthStencilView( |
| 868 |
// ds_view, |
| 869 |
// flags, |
| 870 |
// ds.clear_value.0, |
| 871 |
// ds.clear_value.1 as u8, |
| 872 |
// None, |
| 873 |
// ) |
| 874 |
// TODO: Replace with the above in the next breaking windows-rs release, |
| 875 |
// when https://github.com/microsoft/win32metadata/pull/1971 is in. |
| 876 |
(Interface::vtable(list).ClearDepthStencilView)( |
| 877 |
Interface::as_raw(list), |
| 878 |
ds_view, |
| 879 |
flags, |
| 880 |
ds.clear_value.0, |
| 881 |
ds.clear_value.1 as u8, |
| 882 |
0, |
| 883 |
core::ptr::null(), |
| 884 |
) |
| 885 |
} |
| 886 |
} |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
let raw_vp = Direct3D12::D3D12_VIEWPORT { |
| 891 |
TopLeftX: 0.0, |
| 892 |
TopLeftY: 0.0, |
| 893 |
Width: desc.extent.width as f32, |
| 894 |
Height: desc.extent.height as f32, |
| 895 |
MinDepth: 0.0, |
| 896 |
MaxDepth: 1.0, |
| 897 |
}; |
| 898 |
let raw_rect = Foundation::RECT { |
| 899 |
left: 0, |
| 900 |
top: 0, |
| 901 |
right: desc.extent.width as i32, |
| 902 |
bottom: desc.extent.height as i32, |
| 903 |
}; |
| 904 |
unsafe { list.RSSetViewports(core::slice::from_ref(&raw_vp)) }; |
| 905 |
unsafe { list.RSSetScissorRects(core::slice::from_ref(&raw_rect)) }; |
| 906 |
|
| 907 |
Ok(()) |
| 908 |
} |
| 909 |
|
| 910 |
unsafe fn end_render_pass(&mut self) { |
| 911 |
if !self.pass.resolves.is_empty() { |
| 912 |
let list = self.list.as_ref().unwrap(); |
| 913 |
self.temp.barriers.clear(); |
| 914 |
|
| 915 |
// All the targets are expected to be in `COLOR_TARGET` state, |
| 916 |
// but D3D12 has special source/destination states for the resolves. |
| 917 |
for resolve in self.pass.resolves.iter() { |
| 918 |
let barrier = Direct3D12::D3D12_RESOURCE_BARRIER { |
| 919 |
Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, |
| 920 |
Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, |
| 921 |
Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { |
| 922 |
// Note: this assumes `D3D12_RESOURCE_STATE_RENDER_TARGET`. |
| 923 |
// If it's not the case, we can include the `TextureUses` in `PassResove`. |
| 924 |
Transition: mem::ManuallyDrop::new( |
| 925 |
Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER { |
| 926 |
pResource: unsafe { borrow_interface_temporarily(&resolve.src.0) }, |
| 927 |
Subresource: resolve.src.1, |
| 928 |
StateBefore: Direct3D12::D3D12_RESOURCE_STATE_RENDER_TARGET, |
| 929 |
StateAfter: Direct3D12::D3D12_RESOURCE_STATE_RESOLVE_SOURCE, |
| 930 |
}, |
| 931 |
), |
| 932 |
}, |
| 933 |
}; |
| 934 |
self.temp.barriers.push(barrier); |
| 935 |
let barrier = Direct3D12::D3D12_RESOURCE_BARRIER { |
| 936 |
Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, |
| 937 |
Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, |
| 938 |
Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { |
| 939 |
// Note: this assumes `D3D12_RESOURCE_STATE_RENDER_TARGET`. |
| 940 |
// If it's not the case, we can include the `TextureUses` in `PassResolve`. |
| 941 |
Transition: mem::ManuallyDrop::new( |
| 942 |
Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER { |
| 943 |
pResource: unsafe { borrow_interface_temporarily(&resolve.dst.0) }, |
| 944 |
Subresource: resolve.dst.1, |
| 945 |
StateBefore: Direct3D12::D3D12_RESOURCE_STATE_RENDER_TARGET, |
| 946 |
StateAfter: Direct3D12::D3D12_RESOURCE_STATE_RESOLVE_DEST, |
| 947 |
}, |
| 948 |
), |
| 949 |
}, |
| 950 |
}; |
| 951 |
self.temp.barriers.push(barrier); |
| 952 |
} |
| 953 |
|
| 954 |
if !self.temp.barriers.is_empty() { |
| 955 |
profiling::scope!("ID3D12GraphicsCommandList::ResourceBarrier"); |
| 956 |
unsafe { list.ResourceBarrier(&self.temp.barriers) }; |
| 957 |
} |
| 958 |
|
| 959 |
for resolve in self.pass.resolves.iter() { |
| 960 |
profiling::scope!("ID3D12GraphicsCommandList::ResolveSubresource"); |
| 961 |
unsafe { |
| 962 |
list.ResolveSubresource( |
| 963 |
&resolve.dst.0, |
| 964 |
resolve.dst.1, |
| 965 |
&resolve.src.0, |
| 966 |
resolve.src.1, |
| 967 |
resolve.format, |
| 968 |
) |
| 969 |
}; |
| 970 |
} |
| 971 |
|
| 972 |
// Flip all the barriers to reverse, back into `COLOR_TARGET`. |
| 973 |
for barrier in self.temp.barriers.iter_mut() { |
| 974 |
let transition = unsafe { &mut *barrier.Anonymous.Transition }; |
| 975 |
mem::swap(&mut transition.StateBefore, &mut transition.StateAfter); |
| 976 |
} |
| 977 |
if !self.temp.barriers.is_empty() { |
| 978 |
profiling::scope!("ID3D12GraphicsCommandList::ResourceBarrier"); |
| 979 |
unsafe { list.ResourceBarrier(&self.temp.barriers) }; |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
self.write_pass_end_timestamp_if_requested(); |
| 984 |
|
| 985 |
unsafe { self.end_pass() }; |
| 986 |
} |
| 987 |
|
| 988 |
unsafe fn set_bind_group( |
| 989 |
&mut self, |
| 990 |
layout: &super::PipelineLayout, |
| 991 |
index: u32, |
| 992 |
group: &super::BindGroup, |
| 993 |
dynamic_offsets: &[wgt::DynamicOffset], |
| 994 |
) { |
| 995 |
let info = &layout.bind_group_infos[index as usize]; |
| 996 |
let mut root_index = info.base_root_index as usize; |
| 997 |
|
| 998 |
// Bind CBV/SRC/UAV descriptor tables |
| 999 |
if info.tables.contains(super::TableTypes::SRV_CBV_UAV) { |
| 1000 |
self.pass.root_elements[root_index] = |
| 1001 |
super::RootElement::Table(group.handle_views.unwrap().gpu); |
| 1002 |
root_index += 1; |
| 1003 |
} |
| 1004 |
|
| 1005 |
let mut offsets_index = 0; |
| 1006 |
if let Some(dynamic_storage_buffer_offsets) = info.dynamic_storage_buffer_offsets.as_ref() { |
| 1007 |
let root_index = dynamic_storage_buffer_offsets.root_index; |
| 1008 |
let range = &dynamic_storage_buffer_offsets.range; |
| 1009 |
|
| 1010 |
if range.end > self.pass.dynamic_storage_buffer_offsets.len() { |
| 1011 |
self.pass |
| 1012 |
.dynamic_storage_buffer_offsets |
| 1013 |
.resize(range.end, 0); |
| 1014 |
} |
| 1015 |
|
| 1016 |
offsets_index += range.start; |
| 1017 |
|
| 1018 |
self.pass.root_elements[root_index as usize] = |
| 1019 |
super::RootElement::DynamicOffsetsBuffer { |
| 1020 |
start: range.start, |
| 1021 |
end: range.end, |
| 1022 |
}; |
| 1023 |
|
| 1024 |
if self.pass.layout.signature == layout.shared.signature { |
| 1025 |
self.pass.dirty_root_elements |= 1 << root_index; |
| 1026 |
} else { |
| 1027 |
// D3D12 requires full reset on signature change |
| 1028 |
// but we don't reset it here since it will be reset below |
| 1029 |
}; |
| 1030 |
} |
| 1031 |
|
| 1032 |
// Bind root descriptors for dynamic uniform buffers |
| 1033 |
// or set root constants for offsets of dynamic storage buffers |
| 1034 |
for (&dynamic_buffer, &offset) in group.dynamic_buffers.iter().zip(dynamic_offsets) { |
| 1035 |
match dynamic_buffer { |
| 1036 |
super::DynamicBuffer::Uniform(gpu_base) => { |
| 1037 |
self.pass.root_elements[root_index] = |
| 1038 |
super::RootElement::DynamicUniformBuffer { |
| 1039 |
address: Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE { |
| 1040 |
ptr: gpu_base.ptr + offset as u64, |
| 1041 |
}, |
| 1042 |
}; |
| 1043 |
root_index += 1; |
| 1044 |
} |
| 1045 |
super::DynamicBuffer::Storage => { |
| 1046 |
self.pass.dynamic_storage_buffer_offsets[offsets_index] = offset; |
| 1047 |
offsets_index += 1; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
if self.pass.layout.signature == layout.shared.signature { |
| 1053 |
self.pass.dirty_root_elements |= (1 << root_index) - (1 << info.base_root_index); |
| 1054 |
} else { |
| 1055 |
// D3D12 requires full reset on signature change |
| 1056 |
self.reset_signature(&layout.shared); |
| 1057 |
}; |
| 1058 |
} |
| 1059 |
unsafe fn set_push_constants( |
| 1060 |
&mut self, |
| 1061 |
layout: &super::PipelineLayout, |
| 1062 |
_stages: wgt::ShaderStages, |
| 1063 |
offset_bytes: u32, |
| 1064 |
data: &[u32], |
| 1065 |
) { |
| 1066 |
let offset_words = offset_bytes as usize / 4; |
| 1067 |
|
| 1068 |
let info = layout.shared.root_constant_info.as_ref().unwrap(); |
| 1069 |
|
| 1070 |
self.pass.root_elements[info.root_index as usize] = super::RootElement::Constant; |
| 1071 |
|
| 1072 |
self.pass.constant_data[offset_words..(offset_words + data.len())].copy_from_slice(data); |
| 1073 |
|
| 1074 |
if self.pass.layout.signature == layout.shared.signature { |
| 1075 |
self.pass.dirty_root_elements |= 1 << info.root_index; |
| 1076 |
} else { |
| 1077 |
// D3D12 requires full reset on signature change |
| 1078 |
self.reset_signature(&layout.shared); |
| 1079 |
}; |
| 1080 |
} |
| 1081 |
|
| 1082 |
unsafe fn insert_debug_marker(&mut self, label: &str) { |
| 1083 |
let (wide_label, size) = self.temp.prepare_marker(label); |
| 1084 |
unsafe { |
| 1085 |
self.list |
| 1086 |
.as_ref() |
| 1087 |
.unwrap() |
| 1088 |
.SetMarker(0, Some(wide_label.as_ptr().cast()), size) |
| 1089 |
}; |
| 1090 |
} |
| 1091 |
unsafe fn begin_debug_marker(&mut self, group_label: &str) { |
| 1092 |
let (wide_label, size) = self.temp.prepare_marker(group_label); |
| 1093 |
unsafe { |
| 1094 |
self.list |
| 1095 |
.as_ref() |
| 1096 |
.unwrap() |
| 1097 |
.BeginEvent(0, Some(wide_label.as_ptr().cast()), size) |
| 1098 |
}; |
| 1099 |
} |
| 1100 |
unsafe fn end_debug_marker(&mut self) { |
| 1101 |
unsafe { self.list.as_ref().unwrap().EndEvent() } |
| 1102 |
} |
| 1103 |
|
| 1104 |
unsafe fn set_render_pipeline(&mut self, pipeline: &super::RenderPipeline) { |
| 1105 |
let list = self.list.clone().unwrap(); |
| 1106 |
|
| 1107 |
if self.pass.layout.signature != pipeline.layout.signature { |
| 1108 |
// D3D12 requires full reset on signature change |
| 1109 |
unsafe { list.SetGraphicsRootSignature(pipeline.layout.signature.as_ref()) }; |
| 1110 |
self.reset_signature(&pipeline.layout); |
| 1111 |
}; |
| 1112 |
|
| 1113 |
unsafe { list.SetPipelineState(&pipeline.raw) }; |
| 1114 |
unsafe { list.IASetPrimitiveTopology(pipeline.topology) }; |
| 1115 |
|
| 1116 |
for (index, (vb, &stride)) in self |
| 1117 |
.pass |
| 1118 |
.vertex_buffers |
| 1119 |
.iter_mut() |
| 1120 |
.zip(pipeline.vertex_strides.iter()) |
| 1121 |
.enumerate() |
| 1122 |
{ |
| 1123 |
if let Some(stride) = stride { |
| 1124 |
if vb.StrideInBytes != stride.get() { |
| 1125 |
vb.StrideInBytes = stride.get(); |
| 1126 |
self.pass.dirty_vertex_buffers |= 1 << index; |
| 1127 |
} |
| 1128 |
} |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
unsafe fn set_index_buffer<'a>( |
| 1133 |
&mut self, |
| 1134 |
binding: crate::BufferBinding<'a, super::Buffer>, |
| 1135 |
format: wgt::IndexFormat, |
| 1136 |
) { |
| 1137 |
let ibv = Direct3D12::D3D12_INDEX_BUFFER_VIEW { |
| 1138 |
BufferLocation: binding.resolve_address(), |
| 1139 |
SizeInBytes: binding.resolve_size() as u32, |
| 1140 |
Format: auxil::dxgi::conv::map_index_format(format), |
| 1141 |
}; |
| 1142 |
|
| 1143 |
unsafe { self.list.as_ref().unwrap().IASetIndexBuffer(Some(&ibv)) } |
| 1144 |
} |
| 1145 |
unsafe fn set_vertex_buffer<'a>( |
| 1146 |
&mut self, |
| 1147 |
index: u32, |
| 1148 |
binding: crate::BufferBinding<'a, super::Buffer>, |
| 1149 |
) { |
| 1150 |
let vb = &mut self.pass.vertex_buffers[index as usize]; |
| 1151 |
vb.BufferLocation = binding.resolve_address(); |
| 1152 |
vb.SizeInBytes = binding.resolve_size() as u32; |
| 1153 |
self.pass.dirty_vertex_buffers |= 1 << index; |
| 1154 |
} |
| 1155 |
|
| 1156 |
unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) { |
| 1157 |
let raw_vp = Direct3D12::D3D12_VIEWPORT { |
| 1158 |
TopLeftX: rect.x, |
| 1159 |
TopLeftY: rect.y, |
| 1160 |
Width: rect.w, |
| 1161 |
Height: rect.h, |
| 1162 |
MinDepth: depth_range.start, |
| 1163 |
MaxDepth: depth_range.end, |
| 1164 |
}; |
| 1165 |
unsafe { |
| 1166 |
self.list |
| 1167 |
.as_ref() |
| 1168 |
.unwrap() |
| 1169 |
.RSSetViewports(core::slice::from_ref(&raw_vp)) |
| 1170 |
} |
| 1171 |
} |
| 1172 |
unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) { |
| 1173 |
let raw_rect = Foundation::RECT { |
| 1174 |
left: rect.x as i32, |
| 1175 |
top: rect.y as i32, |
| 1176 |
right: (rect.x + rect.w) as i32, |
| 1177 |
bottom: (rect.y + rect.h) as i32, |
| 1178 |
}; |
| 1179 |
unsafe { |
| 1180 |
self.list |
| 1181 |
.as_ref() |
| 1182 |
.unwrap() |
| 1183 |
.RSSetScissorRects(core::slice::from_ref(&raw_rect)) |
| 1184 |
} |
| 1185 |
} |
| 1186 |
unsafe fn set_stencil_reference(&mut self, value: u32) { |
| 1187 |
unsafe { self.list.as_ref().unwrap().OMSetStencilRef(value) } |
| 1188 |
} |
| 1189 |
unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) { |
| 1190 |
unsafe { self.list.as_ref().unwrap().OMSetBlendFactor(Some(color)) } |
| 1191 |
} |
| 1192 |
|
| 1193 |
unsafe fn draw( |
| 1194 |
&mut self, |
| 1195 |
first_vertex: u32, |
| 1196 |
vertex_count: u32, |
| 1197 |
first_instance: u32, |
| 1198 |
instance_count: u32, |
| 1199 |
) { |
| 1200 |
unsafe { self.prepare_draw(first_vertex as i32, first_instance) }; |
| 1201 |
unsafe { |
| 1202 |
self.list.as_ref().unwrap().DrawInstanced( |
| 1203 |
vertex_count, |
| 1204 |
instance_count, |
| 1205 |
first_vertex, |
| 1206 |
first_instance, |
| 1207 |
) |
| 1208 |
} |
| 1209 |
} |
| 1210 |
unsafe fn draw_indexed( |
| 1211 |
&mut self, |
| 1212 |
first_index: u32, |
| 1213 |
index_count: u32, |
| 1214 |
base_vertex: i32, |
| 1215 |
first_instance: u32, |
| 1216 |
instance_count: u32, |
| 1217 |
) { |
| 1218 |
unsafe { self.prepare_draw(base_vertex, first_instance) }; |
| 1219 |
unsafe { |
| 1220 |
self.list.as_ref().unwrap().DrawIndexedInstanced( |
| 1221 |
index_count, |
| 1222 |
instance_count, |
| 1223 |
first_index, |
| 1224 |
base_vertex, |
| 1225 |
first_instance, |
| 1226 |
) |
| 1227 |
} |
| 1228 |
} |
| 1229 |
unsafe fn draw_mesh_tasks( |
| 1230 |
&mut self, |
| 1231 |
_group_count_x: u32, |
| 1232 |
_group_count_y: u32, |
| 1233 |
_group_count_z: u32, |
| 1234 |
) { |
| 1235 |
unreachable!() |
| 1236 |
} |
| 1237 |
unsafe fn draw_indirect( |
| 1238 |
&mut self, |
| 1239 |
buffer: &super::Buffer, |
| 1240 |
offset: wgt::BufferAddress, |
| 1241 |
draw_count: u32, |
| 1242 |
) { |
| 1243 |
if self |
| 1244 |
.pass |
| 1245 |
.layout |
| 1246 |
.special_constants |
| 1247 |
.as_ref() |
| 1248 |
.and_then(|sc| sc.indirect_cmd_signatures.as_ref()) |
| 1249 |
.is_some() |
| 1250 |
{ |
| 1251 |
unsafe { self.prepare_vertex_buffers() }; |
| 1252 |
self.update_root_elements(); |
| 1253 |
} else { |
| 1254 |
unsafe { self.prepare_draw(0, 0) }; |
| 1255 |
} |
| 1256 |
|
| 1257 |
let cmd_signature = &self |
| 1258 |
.pass |
| 1259 |
.layout |
| 1260 |
.special_constants |
| 1261 |
.as_ref() |
| 1262 |
.and_then(|sc| sc.indirect_cmd_signatures.as_ref()) |
| 1263 |
.unwrap_or_else(|| &self.shared.cmd_signatures) |
| 1264 |
.draw; |
| 1265 |
unsafe { |
| 1266 |
self.list.as_ref().unwrap().ExecuteIndirect( |
| 1267 |
cmd_signature, |
| 1268 |
draw_count, |
| 1269 |
&buffer.resource, |
| 1270 |
offset, |
| 1271 |
None, |
| 1272 |
0, |
| 1273 |
) |
| 1274 |
} |
| 1275 |
} |
| 1276 |
unsafe fn draw_indexed_indirect( |
| 1277 |
&mut self, |
| 1278 |
buffer: &super::Buffer, |
| 1279 |
offset: wgt::BufferAddress, |
| 1280 |
draw_count: u32, |
| 1281 |
) { |
| 1282 |
if self |
| 1283 |
.pass |
| 1284 |
.layout |
| 1285 |
.special_constants |
| 1286 |
.as_ref() |
| 1287 |
.and_then(|sc| sc.indirect_cmd_signatures.as_ref()) |
| 1288 |
.is_some() |
| 1289 |
{ |
| 1290 |
unsafe { self.prepare_vertex_buffers() }; |
| 1291 |
self.update_root_elements(); |
| 1292 |
} else { |
| 1293 |
unsafe { self.prepare_draw(0, 0) }; |
| 1294 |
} |
| 1295 |
|
| 1296 |
let cmd_signature = &self |
| 1297 |
.pass |
| 1298 |
.layout |
| 1299 |
.special_constants |
| 1300 |
.as_ref() |
| 1301 |
.and_then(|sc| sc.indirect_cmd_signatures.as_ref()) |
| 1302 |
.unwrap_or_else(|| &self.shared.cmd_signatures) |
| 1303 |
.draw_indexed; |
| 1304 |
unsafe { |
| 1305 |
self.list.as_ref().unwrap().ExecuteIndirect( |
| 1306 |
cmd_signature, |
| 1307 |
draw_count, |
| 1308 |
&buffer.resource, |
| 1309 |
offset, |
| 1310 |
None, |
| 1311 |
0, |
| 1312 |
) |
| 1313 |
} |
| 1314 |
} |
| 1315 |
unsafe fn draw_mesh_tasks_indirect( |
| 1316 |
&mut self, |
| 1317 |
_buffer: &<Self::A as crate::Api>::Buffer, |
| 1318 |
_offset: wgt::BufferAddress, |
| 1319 |
_draw_count: u32, |
| 1320 |
) { |
| 1321 |
unreachable!() |
| 1322 |
} |
| 1323 |
unsafe fn draw_indirect_count( |
| 1324 |
&mut self, |
| 1325 |
buffer: &super::Buffer, |
| 1326 |
offset: wgt::BufferAddress, |
| 1327 |
count_buffer: &super::Buffer, |
| 1328 |
count_offset: wgt::BufferAddress, |
| 1329 |
max_count: u32, |
| 1330 |
) { |
| 1331 |
unsafe { self.prepare_draw(0, 0) }; |
| 1332 |
unsafe { |
| 1333 |
self.list.as_ref().unwrap().ExecuteIndirect( |
| 1334 |
&self.shared.cmd_signatures.draw, |
| 1335 |
max_count, |
| 1336 |
&buffer.resource, |
| 1337 |
offset, |
| 1338 |
&count_buffer.resource, |
| 1339 |
count_offset, |
| 1340 |
) |
| 1341 |
} |
| 1342 |
} |
| 1343 |
unsafe fn draw_indexed_indirect_count( |
| 1344 |
&mut self, |
| 1345 |
buffer: &super::Buffer, |
| 1346 |
offset: wgt::BufferAddress, |
| 1347 |
count_buffer: &super::Buffer, |
| 1348 |
count_offset: wgt::BufferAddress, |
| 1349 |
max_count: u32, |
| 1350 |
) { |
| 1351 |
unsafe { self.prepare_draw(0, 0) }; |
| 1352 |
unsafe { |
| 1353 |
self.list.as_ref().unwrap().ExecuteIndirect( |
| 1354 |
&self.shared.cmd_signatures.draw_indexed, |
| 1355 |
max_count, |
| 1356 |
&buffer.resource, |
| 1357 |
offset, |
| 1358 |
&count_buffer.resource, |
| 1359 |
count_offset, |
| 1360 |
) |
| 1361 |
} |
| 1362 |
} |
| 1363 |
unsafe fn draw_mesh_tasks_indirect_count( |
| 1364 |
&mut self, |
| 1365 |
_buffer: &<Self::A as crate::Api>::Buffer, |
| 1366 |
_offset: wgt::BufferAddress, |
| 1367 |
_count_buffer: &<Self::A as crate::Api>::Buffer, |
| 1368 |
_count_offset: wgt::BufferAddress, |
| 1369 |
_max_count: u32, |
| 1370 |
) { |
| 1371 |
unreachable!() |
| 1372 |
} |
| 1373 |
|
| 1374 |
// compute |
| 1375 |
|
| 1376 |
unsafe fn begin_compute_pass<'a>( |
| 1377 |
&mut self, |
| 1378 |
desc: &crate::ComputePassDescriptor<'a, super::QuerySet>, |
| 1379 |
) { |
| 1380 |
unsafe { self.begin_pass(super::PassKind::Compute, desc.label) }; |
| 1381 |
|
| 1382 |
if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() { |
| 1383 |
if let Some(index) = timestamp_writes.beginning_of_pass_write_index { |
| 1384 |
unsafe { |
| 1385 |
self.write_timestamp(timestamp_writes.query_set, index); |
| 1386 |
} |
| 1387 |
} |
| 1388 |
self.end_of_pass_timer_query = timestamp_writes |
| 1389 |
.end_of_pass_write_index |
| 1390 |
.map(|index| (timestamp_writes.query_set.raw.clone(), index)); |
| 1391 |
} |
| 1392 |
} |
| 1393 |
unsafe fn end_compute_pass(&mut self) { |
| 1394 |
self.write_pass_end_timestamp_if_requested(); |
| 1395 |
unsafe { self.end_pass() }; |
| 1396 |
} |
| 1397 |
|
| 1398 |
unsafe fn set_compute_pipeline(&mut self, pipeline: &super::ComputePipeline) { |
| 1399 |
let list = self.list.clone().unwrap(); |
| 1400 |
|
| 1401 |
if self.pass.layout.signature != pipeline.layout.signature { |
| 1402 |
// D3D12 requires full reset on signature change |
| 1403 |
unsafe { list.SetComputeRootSignature(pipeline.layout.signature.as_ref()) }; |
| 1404 |
self.reset_signature(&pipeline.layout); |
| 1405 |
}; |
| 1406 |
|
| 1407 |
unsafe { list.SetPipelineState(&pipeline.raw) } |
| 1408 |
} |
| 1409 |
|
| 1410 |
unsafe fn dispatch(&mut self, count @ [x, y, z]: [u32; 3]) { |
| 1411 |
self.prepare_dispatch(count); |
| 1412 |
unsafe { self.list.as_ref().unwrap().Dispatch(x, y, z) } |
| 1413 |
} |
| 1414 |
|
| 1415 |
unsafe fn dispatch_indirect(&mut self, buffer: &super::Buffer, offset: wgt::BufferAddress) { |
| 1416 |
if self |
| 1417 |
.pass |
| 1418 |
.layout |
| 1419 |
.special_constants |
| 1420 |
.as_ref() |
| 1421 |
.and_then(|sc| sc.indirect_cmd_signatures.as_ref()) |
| 1422 |
.is_some() |
| 1423 |
{ |
| 1424 |
self.update_root_elements(); |
| 1425 |
} else { |
| 1426 |
self.prepare_dispatch([0; 3]); |
| 1427 |
} |
| 1428 |
|
| 1429 |
let cmd_signature = &self |
| 1430 |
.pass |
| 1431 |
.layout |
| 1432 |
.special_constants |
| 1433 |
.as_ref() |
| 1434 |
.and_then(|sc| sc.indirect_cmd_signatures.as_ref()) |
| 1435 |
.unwrap_or_else(|| &self.shared.cmd_signatures) |
| 1436 |
.dispatch; |
| 1437 |
unsafe { |
| 1438 |
self.list.as_ref().unwrap().ExecuteIndirect( |
| 1439 |
cmd_signature, |
| 1440 |
1, |
| 1441 |
&buffer.resource, |
| 1442 |
offset, |
| 1443 |
None, |
| 1444 |
0, |
| 1445 |
) |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
unsafe fn build_acceleration_structures<'a, T>( |
| 1450 |
&mut self, |
| 1451 |
_descriptor_count: u32, |
| 1452 |
descriptors: T, |
| 1453 |
) where |
| 1454 |
super::Api: 'a, |
| 1455 |
T: IntoIterator< |
| 1456 |
Item = crate::BuildAccelerationStructureDescriptor< |
| 1457 |
'a, |
| 1458 |
super::Buffer, |
| 1459 |
super::AccelerationStructure, |
| 1460 |
>, |
| 1461 |
>, |
| 1462 |
{ |
| 1463 |
// Implement using `BuildRaytracingAccelerationStructure`: |
| 1464 |
// https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#buildraytracingaccelerationstructure |
| 1465 |
let list = self |
| 1466 |
.list |
| 1467 |
.as_ref() |
| 1468 |
.unwrap() |
| 1469 |
.cast::<Direct3D12::ID3D12GraphicsCommandList4>() |
| 1470 |
.unwrap(); |
| 1471 |
for descriptor in descriptors { |
| 1472 |
// TODO: This is the same as getting build sizes apart from requiring buffers, should this be de-duped? |
| 1473 |
let mut geometry_desc; |
| 1474 |
let ty; |
| 1475 |
let inputs0; |
| 1476 |
let num_desc; |
| 1477 |
match descriptor.entries { |
| 1478 |
AccelerationStructureEntries::Instances(instances) => { |
| 1479 |
let desc_address = unsafe { |
| 1480 |
instances |
| 1481 |
.buffer |
| 1482 |
.expect("needs buffer to build") |
| 1483 |
.resource |
| 1484 |
.GetGPUVirtualAddress() |
| 1485 |
} + instances.offset as u64; |
| 1486 |
ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; |
| 1487 |
inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { |
| 1488 |
InstanceDescs: desc_address, |
| 1489 |
}; |
| 1490 |
num_desc = instances.count; |
| 1491 |
} |
| 1492 |
AccelerationStructureEntries::Triangles(triangles) => { |
| 1493 |
geometry_desc = Vec::with_capacity(triangles.len()); |
| 1494 |
for triangle in triangles { |
| 1495 |
let transform_address = |
| 1496 |
triangle.transform.as_ref().map_or(0, |transform| unsafe { |
| 1497 |
transform.buffer.resource.GetGPUVirtualAddress() |
| 1498 |
+ transform.offset as u64 |
| 1499 |
}); |
| 1500 |
let index_format = triangle |
| 1501 |
.indices |
| 1502 |
.as_ref() |
| 1503 |
.map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { |
| 1504 |
auxil::dxgi::conv::map_index_format(indices.format) |
| 1505 |
}); |
| 1506 |
let vertex_format = |
| 1507 |
auxil::dxgi::conv::map_vertex_format(triangle.vertex_format); |
| 1508 |
let index_count = |
| 1509 |
triangle.indices.as_ref().map_or(0, |indices| indices.count); |
| 1510 |
let index_address = triangle.indices.as_ref().map_or(0, |indices| unsafe { |
| 1511 |
indices |
| 1512 |
.buffer |
| 1513 |
.expect("needs buffer to build") |
| 1514 |
.resource |
| 1515 |
.GetGPUVirtualAddress() |
| 1516 |
+ indices.offset as u64 |
| 1517 |
}); |
| 1518 |
let vertex_address = unsafe { |
| 1519 |
triangle |
| 1520 |
.vertex_buffer |
| 1521 |
.expect("needs buffer to build") |
| 1522 |
.resource |
| 1523 |
.GetGPUVirtualAddress() |
| 1524 |
+ (triangle.first_vertex as u64 * triangle.vertex_stride) |
| 1525 |
}; |
| 1526 |
|
| 1527 |
let triangle_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { |
| 1528 |
Transform3x4: transform_address, |
| 1529 |
IndexFormat: index_format, |
| 1530 |
VertexFormat: vertex_format, |
| 1531 |
IndexCount: index_count, |
| 1532 |
VertexCount: triangle.vertex_count, |
| 1533 |
IndexBuffer: index_address, |
| 1534 |
VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { |
| 1535 |
StartAddress: vertex_address, |
| 1536 |
StrideInBytes: triangle.vertex_stride, |
| 1537 |
}, |
| 1538 |
}; |
| 1539 |
|
| 1540 |
geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { |
| 1541 |
Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, |
| 1542 |
Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags), |
| 1543 |
Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { |
| 1544 |
Triangles: triangle_desc, |
| 1545 |
}, |
| 1546 |
}) |
| 1547 |
} |
| 1548 |
ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; |
| 1549 |
inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { |
| 1550 |
pGeometryDescs: geometry_desc.as_ptr(), |
| 1551 |
}; |
| 1552 |
num_desc = geometry_desc.len() as u32; |
| 1553 |
} |
| 1554 |
AccelerationStructureEntries::AABBs(aabbs) => { |
| 1555 |
geometry_desc = Vec::with_capacity(aabbs.len()); |
| 1556 |
for aabb in aabbs { |
| 1557 |
let aabb_address = unsafe { |
| 1558 |
aabb.buffer |
| 1559 |
.expect("needs buffer to build") |
| 1560 |
.resource |
| 1561 |
.GetGPUVirtualAddress() |
| 1562 |
+ (aabb.offset as u64 * aabb.stride) |
| 1563 |
}; |
| 1564 |
|
| 1565 |
let aabb_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { |
| 1566 |
AABBCount: aabb.count as u64, |
| 1567 |
AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { |
| 1568 |
StartAddress: aabb_address, |
| 1569 |
StrideInBytes: aabb.stride, |
| 1570 |
}, |
| 1571 |
}; |
| 1572 |
|
| 1573 |
geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { |
| 1574 |
Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS, |
| 1575 |
Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), |
| 1576 |
Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { |
| 1577 |
AABBs: aabb_desc, |
| 1578 |
}, |
| 1579 |
}) |
| 1580 |
} |
| 1581 |
ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; |
| 1582 |
inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { |
| 1583 |
pGeometryDescs: geometry_desc.as_ptr(), |
| 1584 |
}; |
| 1585 |
num_desc = geometry_desc.len() as u32; |
| 1586 |
} |
| 1587 |
}; |
| 1588 |
let acceleration_structure_inputs = |
| 1589 |
Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS { |
| 1590 |
Type: ty, |
| 1591 |
Flags: conv::map_acceleration_structure_build_flags( |
| 1592 |
descriptor.flags, |
| 1593 |
Some(descriptor.mode), |
| 1594 |
), |
| 1595 |
NumDescs: num_desc, |
| 1596 |
DescsLayout: Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, |
| 1597 |
Anonymous: inputs0, |
| 1598 |
}; |
| 1599 |
|
| 1600 |
let dst_acceleration_structure_address = unsafe { |
| 1601 |
descriptor |
| 1602 |
.destination_acceleration_structure |
| 1603 |
.resource |
| 1604 |
.GetGPUVirtualAddress() |
| 1605 |
}; |
| 1606 |
let src_acceleration_structure_address = descriptor |
| 1607 |
.source_acceleration_structure |
| 1608 |
.as_ref() |
| 1609 |
.map_or(0, |source| unsafe { |
| 1610 |
source.resource.GetGPUVirtualAddress() |
| 1611 |
}); |
| 1612 |
let scratch_address = unsafe { |
| 1613 |
descriptor.scratch_buffer.resource.GetGPUVirtualAddress() |
| 1614 |
+ descriptor.scratch_buffer_offset |
| 1615 |
}; |
| 1616 |
|
| 1617 |
let desc = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC { |
| 1618 |
DestAccelerationStructureData: dst_acceleration_structure_address, |
| 1619 |
Inputs: acceleration_structure_inputs, |
| 1620 |
SourceAccelerationStructureData: src_acceleration_structure_address, |
| 1621 |
ScratchAccelerationStructureData: scratch_address, |
| 1622 |
}; |
| 1623 |
unsafe { list.BuildRaytracingAccelerationStructure(&desc, None) }; |
| 1624 |
} |
| 1625 |
} |
| 1626 |
|
| 1627 |
unsafe fn place_acceleration_structure_barrier( |
| 1628 |
&mut self, |
| 1629 |
_barriers: crate::AccelerationStructureBarrier, |
| 1630 |
) { |
| 1631 |
// TODO: This is not very optimal, we should be using [enhanced barriers](https://microsoft.github.io/DirectX-Specs/d3d/D3D12EnhancedBarriers.html) if possible |
| 1632 |
let list = self |
| 1633 |
.list |
| 1634 |
.as_ref() |
| 1635 |
.unwrap() |
| 1636 |
.cast::<Direct3D12::ID3D12GraphicsCommandList4>() |
| 1637 |
.unwrap(); |
| 1638 |
unsafe { |
| 1639 |
list.ResourceBarrier(&[Direct3D12::D3D12_RESOURCE_BARRIER { |
| 1640 |
Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV, |
| 1641 |
Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, |
| 1642 |
Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { |
| 1643 |
UAV: mem::ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { |
| 1644 |
pResource: Default::default(), |
| 1645 |
}), |
| 1646 |
}, |
| 1647 |
}]) |
| 1648 |
} |
| 1649 |
} |
| 1650 |
|
| 1651 |
unsafe fn copy_acceleration_structure_to_acceleration_structure( |
| 1652 |
&mut self, |
| 1653 |
src: &super::AccelerationStructure, |
| 1654 |
dst: &super::AccelerationStructure, |
| 1655 |
copy: wgt::AccelerationStructureCopy, |
| 1656 |
) { |
| 1657 |
let list = self |
| 1658 |
.list |
| 1659 |
.as_ref() |
| 1660 |
.unwrap() |
| 1661 |
.cast::<Direct3D12::ID3D12GraphicsCommandList4>() |
| 1662 |
.unwrap(); |
| 1663 |
unsafe { |
| 1664 |
list.CopyRaytracingAccelerationStructure( |
| 1665 |
dst.resource.GetGPUVirtualAddress(), |
| 1666 |
src.resource.GetGPUVirtualAddress(), |
| 1667 |
conv::map_acceleration_structure_copy_mode(copy), |
| 1668 |
) |
| 1669 |
} |
| 1670 |
} |
| 1671 |
} |