diff --git a/d3d12/crates/leonida-gpu/src/replay/record.rs b/d3d12/crates/leonida-gpu/src/replay/record.rs index e1c56f96a4..5d5e34deb3 100644 --- a/d3d12/crates/leonida-gpu/src/replay/record.rs +++ b/d3d12/crates/leonida-gpu/src/replay/record.rs @@ -1076,6 +1076,7 @@ fn list_table(t: &mut [usize; hook::SLOTS]) { ID3D12GraphicsCommandList_Vtbl.SetPipelineState = set_pipeline ); set!(t, ID3D12GraphicsCommandList_Vtbl.ResourceBarrier = barrier); + set!(t, ID3D12GraphicsCommandList7_Vtbl.Barrier = barrier7); set!( t, ID3D12GraphicsCommandList_Vtbl.SetDescriptorHeaps = set_heaps @@ -1208,7 +1209,6 @@ fn list_rest(t: &mut [usize; hook::SLOTS]) -> &'static [(usize, &'static str)] { ID3D12GraphicsCommandList4_Vtbl.DispatchRays, ID3D12GraphicsCommandList5_Vtbl.RSSetShadingRate, ID3D12GraphicsCommandList5_Vtbl.RSSetShadingRateImage, - ID3D12GraphicsCommandList7_Vtbl.Barrier, ID3D12GraphicsCommandList8_Vtbl.OMSetFrontAndBackStencilRef, ID3D12GraphicsCommandList9_Vtbl.RSSetDepthBias, ID3D12GraphicsCommandList9_Vtbl.IASetIndexBufferStripCutValue, @@ -2317,6 +2317,196 @@ unsafe extern "system" fn barrier(this: Ptr, n: u32, all: *const D3D12_RESOURCE_ } } +unsafe extern "system" fn barrier7(this: Ptr, n: u32, all: *const D3D12_BARRIER_GROUP) { + // SAFETY: as above; the call's groups are live for the call. + unsafe { + own!(this, ID3D12GraphicsCommandList7_Vtbl.Barrier)(this, n, all); + note_change(this, |r| { + let groups = if n == 0 { + &[][..] + } else { + std::slice::from_raw_parts(all, n as usize) + }; + Cmd::Barriers { + all: enhanced(groups, r), + } + }); + } +} + +/// The legacy state a texture barrier's layout stands for: the wire's +/// transitions carry states, not the enhanced layouts. The queue-specific +/// forms stand for the same state as their common form. +fn layout_state(layout: D3D12_BARRIER_LAYOUT) -> i32 { + // A queue-specific layout stands for the same state as its common form. + let common = match layout.0 { + 18 => 0, + 19 => 1, + 20 => 3, + 21 => 6, + 22 => 7, + 23 => 8, + 24 => 0, + 25 => 1, + 26 => 3, + 27 => 6, + 28 => 7, + 29 => 8, + other => other, + }; + // The layouts' values; the states are `D3D12_RESOURCE_STATES`. + match common { + -1 | 0 => 0x0, // UNDEFINED, COMMON + 1 => 0x1 | 0x2 | 0x40 | 0x80 | 0x200 | 0x800, // GENERIC_READ + 2 => 0x4, // RENDER_TARGET + 3 => 0x8, // UNORDERED_ACCESS + 4 => 0x10, // DEPTH_STENCIL_WRITE + 5 => 0x20, // DEPTH_STENCIL_READ + 6 => 0x40 | 0x80, // SHADER_RESOURCE + 7 => 0x800, // COPY_SOURCE + 8 => 0x400, // COPY_DEST + 9 => 0x2000, // RESOLVE_SOURCE + 10 => 0x1000, // RESOLVE_DEST + 11 => 0x1000000, // SHADING_RATE_SOURCE + 12 => 0x10000, // VIDEO_DECODE_READ + 13 => 0x20000, // VIDEO_DECODE_WRITE + 14 => 0x40000, // VIDEO_PROCESS_READ + 15 => 0x80000, // VIDEO_PROCESS_WRITE + 16 => 0x200000, // VIDEO_ENCODE_READ + 17 => 0x800000, // VIDEO_ENCODE_WRITE + _ => 0x0, // PRESENT, or unknown + } +} + +/// A texture barrier's wait on the wire: a transition between the states its +/// layouts stand for, a UAV barrier when both stand for the same UAV state, +/// nothing when both stand for any other state (a real device refuses a +/// transition to the state it is in: `E_INVALIDARG`). +fn texture_barrier( + res: u32, + range: &D3D12_BARRIER_SUBRESOURCE_RANGE, + before: i32, + after: i32, + desc: Option<&wire::Desc>, +) -> Option { + const UNORDERED_ACCESS: i32 = 0x8; + if before == after { + return (before == UNORDERED_ACCESS).then_some(Barrier::Uav { flags: 0, res }); + } + Some(Barrier::Transition { + flags: 0, + res, + sub: subresource(range, desc), + before, + after, + }) +} + +/// The subresource a texture barrier's range names, [`ALL`] when it names +/// more than one (or the resource is unknown). +fn subresource(range: &D3D12_BARRIER_SUBRESOURCE_RANGE, desc: Option<&wire::Desc>) -> u32 { + if range.NumMipLevels == 0 { + // The index is the subresource itself (`0xffffffff`: every one). + return range.IndexOrFirstMipLevel; + } + let Some(desc) = desc else { return ALL }; + if range.NumMipLevels != 1 || range.NumArraySlices != 1 || range.NumPlanes != 1 { + return ALL; + } + let mips = u32::from(desc.mips.max(1)); + let array = if desc.dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D.0 { + 1 + } else { + u32::from(desc.depth).max(1) + }; + range.IndexOrFirstMipLevel + + range.FirstArraySlice * mips + + range.FirstPlane * mips * array +} + +/// The barriers one enhanced `Barrier` call stands for on the wire: the +/// legacy barriers the runtime itself issues for the same wait. A global one +/// is the alias + UAV pair `play::wait_for_all` falls back to, a texture one +/// a transition between the states its layouts stand for (a UAV barrier or +/// nothing when they are the same), a buffer one a UAV barrier when the +/// access after is UAV, else the same alias + UAV pair (a legacy barrier +/// cannot name another buffer access). +/// The enhanced masks order the same accesses with less; these are the same +/// waits, never fewer. +/// shortcut: a texture subresource range that names more than one +/// subresource is recorded as the whole resource (a legacy transition +/// carries one subresource); upgrade if a title keeps two subresources of +/// one texture in different layouts at once. +unsafe fn enhanced(groups: &[D3D12_BARRIER_GROUP], r: &Recorder) -> Vec { + const GLOBAL: i32 = D3D12_BARRIER_TYPE_GLOBAL.0; + const TEXTURE: i32 = D3D12_BARRIER_TYPE_TEXTURE.0; + const BUFFER: i32 = D3D12_BARRIER_TYPE_BUFFER.0; + let mut all = Vec::new(); + for group in groups { + let n = group.NumBarriers as usize; + if n == 0 { + continue; + } + match group.Type.0 { + GLOBAL => { + // SAFETY: the type says which pointer of the union holds, and + // the call's arrays are live. + let barriers = unsafe { std::slice::from_raw_parts(group.Anonymous.pGlobalBarriers, n) }; + for _ in barriers { + all.push(Barrier::Alias { + flags: 0, + before: NONE, + after: NONE, + }); + all.push(Barrier::Uav { + flags: 0, + res: NONE, + }); + } + } + TEXTURE => { + // SAFETY: as above; a texture barrier's resource is live. + let barriers = unsafe { std::slice::from_raw_parts(group.Anonymous.pTextureBarriers, n) }; + for b in barriers { + let res = r.resource(unsafe { std::mem::transmute_copy::<_, Ptr>(&b.pResource) }); + let desc = read(&r.resources).get(&res).map(|res| res.desc.clone()); + if let Some(barrier) = texture_barrier( + res, + &b.Subresources, + layout_state(b.LayoutBefore), + layout_state(b.LayoutAfter), + desc.as_ref(), + ) { + all.push(barrier); + } + } + } + BUFFER => { + // SAFETY: as above. + let barriers = unsafe { std::slice::from_raw_parts(group.Anonymous.pBufferBarriers, n) }; + for b in barriers { + let res = r.resource(unsafe { std::mem::transmute_copy::<_, Ptr>(&b.pResource) }); + if b.AccessAfter.0 == D3D12_BARRIER_ACCESS_UNORDERED_ACCESS.0 { + all.push(Barrier::Uav { flags: 0, res }); + } else { + all.push(Barrier::Alias { + flags: 0, + before: NONE, + after: NONE, + }); + all.push(Barrier::Uav { + flags: 0, + res: NONE, + }); + } + } + } + _ => {} + } + } + all +} + unsafe extern "system" fn set_heaps(this: Ptr, n: u32, all: *const Ptr) { // SAFETY: as above. unsafe { @@ -2779,4 +2969,260 @@ mod tests { assert_eq!(h.all(), [Slot::EMPTY, s]); assert_eq!(h.get(9), Slot::EMPTY); } + + #[test] + fn enhanced_layouts_record_as_the_states_they_stand_for() { + // The states a recorded transition carries (D3D12_RESOURCE_STATES). + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_UNDEFINED), 0); + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_COMMON), 0); + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_RENDER_TARGET), 0x4); + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS), 0x8); + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE), 0x10); + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_READ), 0x20); + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_SHADER_RESOURCE), 0xC0); + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_COPY_SOURCE), 0x800); + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_COPY_DEST), 0x400); + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_RESOLVE_SOURCE), 0x2000); + assert_eq!(layout_state(D3D12_BARRIER_LAYOUT_RESOLVE_DEST), 0x1000); + assert_eq!( + layout_state(D3D12_BARRIER_LAYOUT_GENERIC_READ), + 0x1 | 0x2 | 0x40 | 0x80 | 0x200 | 0x800 + ); + // A queue-specific form stands for the same state as its common form. + for (queue, common) in [ + (D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_COMMON, D3D12_BARRIER_LAYOUT_COMMON), + (D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_GENERIC_READ, D3D12_BARRIER_LAYOUT_GENERIC_READ), + (D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_UNORDERED_ACCESS, D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS), + (D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_SHADER_RESOURCE, D3D12_BARRIER_LAYOUT_SHADER_RESOURCE), + (D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_COPY_SOURCE, D3D12_BARRIER_LAYOUT_COPY_SOURCE), + (D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_COPY_DEST, D3D12_BARRIER_LAYOUT_COPY_DEST), + (D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_COMMON, D3D12_BARRIER_LAYOUT_COMMON), + (D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_GENERIC_READ, D3D12_BARRIER_LAYOUT_GENERIC_READ), + (D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_UNORDERED_ACCESS, D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS), + (D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_SHADER_RESOURCE, D3D12_BARRIER_LAYOUT_SHADER_RESOURCE), + (D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_COPY_SOURCE, D3D12_BARRIER_LAYOUT_COPY_SOURCE), + (D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_COPY_DEST, D3D12_BARRIER_LAYOUT_COPY_DEST), + ] { + assert_eq!(layout_state(queue), layout_state(common)); + } + } + + #[test] + fn same_state_texture_barriers_are_a_wait_or_nothing() { + let range = D3D12_BARRIER_SUBRESOURCE_RANGE { + NumMipLevels: 1, + NumArraySlices: 1, + NumPlanes: 1, + ..Default::default() + }; + let desc = gpu::from_desc(&D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + DepthOrArraySize: 2, + MipLevels: 2, + Format: DXGI_FORMAT(28), + ..Default::default() + }); + let uav = Barrier::Uav { flags: 0, res: 7 }; + assert_eq!(texture_barrier(7, &range, 0x8, 0x8, Some(&desc)), Some(uav.clone())); + assert_eq!(texture_barrier(7, &range, 0x8, 0x8, None), Some(uav)); + assert_eq!(texture_barrier(7, &range, 0, 0, Some(&desc)), None); + assert_eq!(texture_barrier(7, &range, 0xC0, 0xC0, Some(&desc)), None); + assert_eq!( + texture_barrier(7, &range, 0x8, 0xC0, Some(&desc)), + Some(Barrier::Transition { + flags: 0, + res: 7, + sub: 0, + before: 0x8, + after: 0xC0, + }) + ); + } + + #[test] + fn one_subresource_ranges_flatten_to_that_subresource() { + let desc = gpu::from_desc(&D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + DepthOrArraySize: 2, + MipLevels: 2, + Format: DXGI_FORMAT(28), + ..Default::default() + }); + let one = D3D12_BARRIER_SUBRESOURCE_RANGE { + IndexOrFirstMipLevel: 1, + NumMipLevels: 1, + FirstArraySlice: 1, + NumArraySlices: 1, + NumPlanes: 1, + ..Default::default() + }; + assert_eq!(subresource(&one, Some(&desc)), 3, "mip 1 of slice 1"); + let plane = D3D12_BARRIER_SUBRESOURCE_RANGE { + FirstArraySlice: 1, + FirstPlane: 1, + NumMipLevels: 1, + NumArraySlices: 1, + NumPlanes: 1, + ..Default::default() + }; + assert_eq!(subresource(&plane, Some(&desc)), 6, "plane 1 of slice 1"); + let many = D3D12_BARRIER_SUBRESOURCE_RANGE { + NumMipLevels: 2, + NumArraySlices: 1, + NumPlanes: 1, + ..Default::default() + }; + assert_eq!(subresource(&many, Some(&desc)), ALL, "two mips"); + let index = D3D12_BARRIER_SUBRESOURCE_RANGE { + IndexOrFirstMipLevel: 3, + ..Default::default() + }; + assert_eq!(subresource(&index, Some(&desc)), 3); + assert_eq!(subresource(&index, None), 3); + let every = D3D12_BARRIER_SUBRESOURCE_RANGE { + IndexOrFirstMipLevel: ALL, + ..Default::default() + }; + assert_eq!(subresource(&every, Some(&desc)), ALL); + let three_d = gpu::from_desc(&D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE3D, + DepthOrArraySize: 6, + MipLevels: 4, + Format: DXGI_FORMAT(28), + ..Default::default() + }); + let plane_of_3d = D3D12_BARRIER_SUBRESOURCE_RANGE { + IndexOrFirstMipLevel: 1, + FirstPlane: 1, + NumMipLevels: 1, + NumArraySlices: 1, + NumPlanes: 1, + ..Default::default() + }; + assert_eq!(subresource(&plane_of_3d, Some(&three_d)), 5, "3D has one array slice"); + } + + #[test] + fn enhanced_records_its_texture_and_buffer_barriers() { + let dir = tempfile::tempdir().expect("a temp dir"); + let r = Recorder::new(Settings { + folder: dir.path().to_path_buf(), + at: At::Trigger, + frames: 1, + always: false, + }) + .expect("a recorder"); + let ptr = 0x1000usize; + let id = r.id(); + r.ids.set(ptr, id); + let desc = gpu::from_desc(&D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + DepthOrArraySize: 2, + MipLevels: 2, + Format: DXGI_FORMAT(28), + ..Default::default() + }); + r.resources.write().unwrap().insert( + id, + Arc::new(Res { + id, + ptr, + place: Place::BackBuffer {}, + desc: desc.clone(), + made_in: 0, + clear: None, + subs: gpu::subresources(&desc), + seen: gpu::Seen::No, + late: false, + }), + ); + // SAFETY: `enhanced` only takes the address of this resource, which is + // registered above; it is never called through. + let fake = || ManuallyDrop::new(Some(unsafe { ID3D12Resource::from_raw(ptr as Ptr) })); + let mut texture = D3D12_TEXTURE_BARRIER { + LayoutBefore: D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS, + LayoutAfter: D3D12_BARRIER_LAYOUT_SHADER_RESOURCE, + pResource: fake(), + Subresources: D3D12_BARRIER_SUBRESOURCE_RANGE { + IndexOrFirstMipLevel: 1, + NumMipLevels: 1, + FirstArraySlice: 1, + NumArraySlices: 1, + NumPlanes: 1, + ..Default::default() + }, + ..Default::default() + }; + let texture_group = D3D12_BARRIER_GROUP { + Type: D3D12_BARRIER_TYPE_TEXTURE, + NumBarriers: 1, + Anonymous: D3D12_BARRIER_GROUP_0 { + pTextureBarriers: &texture, + }, + }; + // SAFETY: the group points at the barrier and resource above. + assert_eq!( + unsafe { enhanced(&[texture_group], &r) }, + vec![Barrier::Transition { + flags: 0, + res: id, + sub: 3, + before: 0x8, + after: 0xC0, + }] + ); + texture.LayoutBefore = D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_UNORDERED_ACCESS; + texture.LayoutAfter = D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS; + let same_state = D3D12_BARRIER_GROUP { + Anonymous: D3D12_BARRIER_GROUP_0 { + pTextureBarriers: &texture, + }, + ..texture_group + }; + // SAFETY: as above. + assert_eq!( + unsafe { enhanced(&[same_state], &r) }, + vec![Barrier::Uav { flags: 0, res: id }] + ); + let mut buffer = D3D12_BUFFER_BARRIER { + AccessBefore: D3D12_BARRIER_ACCESS_COPY_DEST, + AccessAfter: D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, + pResource: fake(), + ..Default::default() + }; + let buffer_group = D3D12_BARRIER_GROUP { + Type: D3D12_BARRIER_TYPE_BUFFER, + NumBarriers: 1, + Anonymous: D3D12_BARRIER_GROUP_0 { + pBufferBarriers: &buffer, + }, + }; + // SAFETY: as above. + assert_eq!( + unsafe { enhanced(&[buffer_group], &r) }, + vec![Barrier::Uav { flags: 0, res: id }] + ); + buffer.AccessAfter = D3D12_BARRIER_ACCESS_COPY_SOURCE; + let copy = D3D12_BARRIER_GROUP { + Anonymous: D3D12_BARRIER_GROUP_0 { + pBufferBarriers: &buffer, + }, + ..buffer_group + }; + // SAFETY: as above. + assert_eq!( + unsafe { enhanced(&[copy], &r) }, + vec![ + Barrier::Alias { + flags: 0, + before: NONE, + after: NONE, + }, + Barrier::Uav { + flags: 0, + res: NONE, + }, + ] + ); + } }