|
|
@@ -57,6 +57,10 @@ pub(crate) struct Host {
|
|
|
/// The adapter the device is on, for its memory budget
|
|
|
/// ([`Host::video_memory`]); `None` if the system does not name it.
|
|
|
adapter: Option<IDXGIAdapter3>,
|
|
|
+ /// 1-descriptor shader-visible fallback heaps bound when a command list
|
|
|
+ /// omits a view or sampler heap while using directly-indexed root signatures.
|
|
|
+ fallback_view: OnceLock<Option<ID3D12DescriptorHeap>>,
|
|
|
+ fallback_sampler: OnceLock<Option<ID3D12DescriptorHeap>>,
|
|
|
}
|
|
|
|
|
|
// SAFETY: D3D12 devices, queues, fences and descriptor heaps are
|
|
|
@@ -368,7 +372,11 @@ pub(crate) fn keep_freed() -> bool {
|
|
|
/// when free physical RAM is low. Never returns less than 2,000 ms.
|
|
|
/// With plenty of it five minutes ([`keep_freed`]).
|
|
|
pub(crate) fn ram_idle_ms(avail_phys: Option<u64>) -> u64 {
|
|
|
- ram_idle_for(avail_phys, keep_freed())
|
|
|
+ let effective = match (avail_phys, crate::residency::headroom()) {
|
|
|
+ (Some(a), Some(b)) => Some(a.min(b)),
|
|
|
+ (a, b) => a.or(b),
|
|
|
+ };
|
|
|
+ ram_idle_for(effective, keep_freed())
|
|
|
}
|
|
|
|
|
|
fn ram_idle_for(avail_phys: Option<u64>, keep: bool) -> u64 {
|
|
|
@@ -432,6 +440,10 @@ fn sweep() {
|
|
|
}
|
|
|
#[cfg(feature = "diagnostics")]
|
|
|
crate::book::check();
|
|
|
+ crate::residency::refresh();
|
|
|
+ if crate::residency::pressured() {
|
|
|
+ trim_spare_chunks();
|
|
|
+ }
|
|
|
crate::pool::room(host().and_then(|h| h.video_memory()));
|
|
|
crate::pipeline::trim_library();
|
|
|
crate::shadow::let_go_idle();
|
|
|
@@ -484,7 +496,10 @@ pub(crate) fn retire_resource(object: ID3D12Resource) {
|
|
|
/// recorded and not yet sent may name them. `VICTORY_BURY_LAG=0`: not.
|
|
|
fn bury_lag() -> u64 {
|
|
|
static LAG: OnceLock<u64> = OnceLock::new();
|
|
|
- *LAG.get_or_init(|| std::env::var("VICTORY_BURY_LAG").ok().and_then(|v| v.parse().ok()).unwrap_or(64))
|
|
|
+ *LAG.get_or_init(|| {
|
|
|
+ let default = if crate::residency::limited_host_ram() { 16 } else { 64 };
|
|
|
+ std::env::var("VICTORY_BURY_LAG").ok().and_then(|v| v.parse().ok()).unwrap_or(default)
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
/// Drops what waited for submissions up to `finished` ([`bury_lag`]). The
|
|
|
@@ -967,6 +982,8 @@ fn create() -> windows::core::Result<Host> {
|
|
|
next_rtv: AtomicU32::new(0),
|
|
|
presenter: Mutex::new(None),
|
|
|
adapter,
|
|
|
+ fallback_view: OnceLock::new(),
|
|
|
+ fallback_sampler: OnceLock::new(),
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -1915,7 +1932,12 @@ fn give_chunks(chunks: Vec<(Chunk, u64)>) {
|
|
|
let mut spare = SPARE.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
for (chunk, size) in chunks {
|
|
|
let back = if scratch_pool() {
|
|
|
- spare.give(chunk, size, if keep_freed() { SPARE_KEPT } else { SPARE_MOST })
|
|
|
+ let limit = if keep_freed() && !crate::residency::limited_host_ram() {
|
|
|
+ SPARE_KEPT
|
|
|
+ } else {
|
|
|
+ SPARE_MOST
|
|
|
+ };
|
|
|
+ spare.give(chunk, size, limit)
|
|
|
} else {
|
|
|
Some(chunk)
|
|
|
};
|
|
|
@@ -1936,6 +1958,25 @@ fn give_chunks(chunks: Vec<(Chunk, u64)>) {
|
|
|
drop(freed);
|
|
|
}
|
|
|
|
|
|
+/// Drops spare scratch chunks kept idle when host memory is pressured.
|
|
|
+fn trim_spare_chunks() {
|
|
|
+ let freed: Vec<(Chunk, u64)> = {
|
|
|
+ let mut spare = SPARE.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
+ if spare.kept.is_empty() {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ spare.bytes = 0;
|
|
|
+ std::mem::take(&mut spare.kept)
|
|
|
+ };
|
|
|
+ let count = freed.len() as u64;
|
|
|
+ let bytes: u64 = freed.iter().map(|(_, s)| *s).sum();
|
|
|
+ ALIVE[1].fetch_sub(count, Ordering::Relaxed);
|
|
|
+ ALIVE[2].fetch_sub(bytes, Ordering::Relaxed);
|
|
|
+ ALIVE[3].fetch_sub(count, Ordering::Relaxed);
|
|
|
+ ALIVE[4].fetch_sub(bytes, Ordering::Relaxed);
|
|
|
+ drop(freed);
|
|
|
+}
|
|
|
+
|
|
|
/// What the host keeps for copies in the card's own memory
|
|
|
/// ([`crate::shadow::cards`]): three lists of ours and the compare.
|
|
|
struct CardWork {
|
|
|
@@ -4258,6 +4299,7 @@ impl Host {
|
|
|
// pass: a wait between each copy and its pass had the
|
|
|
// card do them one after the other.
|
|
|
let mut sent = Vec::new();
|
|
|
+ let mut transitioned = Vec::new();
|
|
|
if let Some(own) = &own {
|
|
|
own.record(self, list, &dirty, |shadow| looks.as_ref().is_some_and(|l| l.hold(shadow)));
|
|
|
}
|
|
|
@@ -4276,11 +4318,15 @@ impl Host {
|
|
|
#[cfg(feature = "diagnostics")]
|
|
|
traffic::moved(traffic::UP, shadow.span(), ranges.iter().copied());
|
|
|
if own.is_none() && before.is_some() && !ranges.is_empty() {
|
|
|
- sent.push(transition(
|
|
|
- &shadow.buffer,
|
|
|
- D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
- D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
|
|
|
- ));
|
|
|
+ let raw = shadow.buffer.as_raw();
|
|
|
+ if !transitioned.contains(&raw) {
|
|
|
+ transitioned.push(raw);
|
|
|
+ sent.push(transition(
|
|
|
+ &shadow.buffer,
|
|
|
+ D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
+ D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
|
|
|
+ ));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
if !sent.is_empty() {
|
|
|
@@ -6042,6 +6088,71 @@ impl Host {
|
|
|
handle
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /// 1-descriptor shader-visible CBV/SRV/UAV fallback heap for directly-indexed root signatures.
|
|
|
+ pub(crate) fn fallback_view_heap(&self) -> Option<&ID3D12DescriptorHeap> {
|
|
|
+ self.fallback_view
|
|
|
+ .get_or_init(|| {
|
|
|
+ // SAFETY: plain 1-descriptor shader-visible heap + null SRV initialization.
|
|
|
+ unsafe {
|
|
|
+ let heap: ID3D12DescriptorHeap = self
|
|
|
+ .device
|
|
|
+ .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC {
|
|
|
+ Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
|
|
|
+ NumDescriptors: 1,
|
|
|
+ Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
|
|
|
+ NodeMask: 0,
|
|
|
+ })
|
|
|
+ .ok()?;
|
|
|
+ let desc = D3D12_SHADER_RESOURCE_VIEW_DESC {
|
|
|
+ Format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_UINT,
|
|
|
+ ViewDimension: D3D12_SRV_DIMENSION_BUFFER,
|
|
|
+ Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
|
|
|
+ Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 {
|
|
|
+ Buffer: D3D12_BUFFER_SRV::default(),
|
|
|
+ },
|
|
|
+ };
|
|
|
+ self.device.CreateShaderResourceView(
|
|
|
+ None,
|
|
|
+ Some(&desc),
|
|
|
+ heap.GetCPUDescriptorHandleForHeapStart(),
|
|
|
+ );
|
|
|
+ Some(heap)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .as_ref()
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 1-descriptor shader-visible Sampler fallback heap for directly-indexed root signatures.
|
|
|
+ pub(crate) fn fallback_sampler_heap(&self) -> Option<&ID3D12DescriptorHeap> {
|
|
|
+ self.fallback_sampler
|
|
|
+ .get_or_init(|| {
|
|
|
+ // SAFETY: plain 1-descriptor shader-visible sampler heap initialization.
|
|
|
+ unsafe {
|
|
|
+ let heap: ID3D12DescriptorHeap = self
|
|
|
+ .device
|
|
|
+ .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC {
|
|
|
+ Type: D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
|
|
|
+ NumDescriptors: 1,
|
|
|
+ Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
|
|
|
+ NodeMask: 0,
|
|
|
+ })
|
|
|
+ .ok()?;
|
|
|
+ let desc = D3D12_SAMPLER_DESC {
|
|
|
+ Filter: D3D12_FILTER_MIN_MAG_MIP_POINT,
|
|
|
+ AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
|
|
|
+ AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
|
|
|
+ AddressW: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
|
|
|
+ MaxLOD: f32::MAX,
|
|
|
+ ..Default::default()
|
|
|
+ };
|
|
|
+ self.device
|
|
|
+ .CreateSampler(&desc, heap.GetCPUDescriptorHandleForHeapStart());
|
|
|
+ Some(heap)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .as_ref()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
impl HostList {
|
|
|
@@ -8480,6 +8591,7 @@ struct Presenter {
|
|
|
ahead: Vec<(HostList, u64)>,
|
|
|
hwnd: HWND,
|
|
|
meter: Meter,
|
|
|
+ composer: Option<crate::compose::Composer>,
|
|
|
#[cfg(feature = "diagnostics")]
|
|
|
panel: panel::Panel,
|
|
|
}
|
|
|
@@ -8803,12 +8915,40 @@ impl Host {
|
|
|
/// Shows `texture`, which is in `state` (a host state) and stays in it,
|
|
|
/// as frame `frame` (saved if captures are on). `None` if nothing was
|
|
|
/// shown; else, in diagnostics builds, whether the frame was black.
|
|
|
+ #[allow(dead_code)]
|
|
|
pub(crate) fn present(&self, texture: &ID3D12Resource, state: u32, frame: u64) -> Option<bool> {
|
|
|
+ self.present_planes(texture, state, None, frame)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Shows `texture`, optionally alpha-compositing a second `overlay` plane
|
|
|
+ /// over it (Series S two-plane presentation), and returns them to their states.
|
|
|
+ pub(crate) fn present_planes(
|
|
|
+ &self,
|
|
|
+ texture: &ID3D12Resource,
|
|
|
+ state: u32,
|
|
|
+ overlay: Option<(&ID3D12Resource, u32)>,
|
|
|
+ frame: u64,
|
|
|
+ ) -> Option<bool> {
|
|
|
// SAFETY: a live resource.
|
|
|
let desc = unsafe { texture.GetDesc() };
|
|
|
- let Some(format) = swap_format(desc.Format) else {
|
|
|
- tracing::warn!(format = desc.Format.0, "cannot present this format");
|
|
|
- return None;
|
|
|
+ let planes = overlay.and_then(|(over, over_state)| {
|
|
|
+ // SAFETY: a live overlay resource.
|
|
|
+ let over_desc = unsafe { over.GetDesc() };
|
|
|
+ Some((
|
|
|
+ over,
|
|
|
+ D3D12_RESOURCE_STATES(over_state as i32),
|
|
|
+ crate::compose::view(&desc)?,
|
|
|
+ crate::compose::view(&over_desc)?,
|
|
|
+ ))
|
|
|
+ });
|
|
|
+ let format = if planes.is_some() {
|
|
|
+ DXGI_FORMAT_R8G8B8A8_UNORM
|
|
|
+ } else {
|
|
|
+ let Some(format) = swap_format(desc.Format) else {
|
|
|
+ tracing::warn!(format = desc.Format.0, "cannot present this format");
|
|
|
+ return None;
|
|
|
+ };
|
|
|
+ format
|
|
|
};
|
|
|
let (width, height) = (desc.Width as u32, desc.Height);
|
|
|
let mut presenter = self.presenter.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
@@ -8832,6 +8972,9 @@ impl Host {
|
|
|
};
|
|
|
match resized {
|
|
|
Ok(()) => {
|
|
|
+ if p.format != format {
|
|
|
+ p.composer = None;
|
|
|
+ }
|
|
|
(p.width, p.height, p.format) = (width, height, format);
|
|
|
tracing::info!(
|
|
|
width,
|
|
|
@@ -8849,7 +8992,7 @@ impl Host {
|
|
|
}
|
|
|
}
|
|
|
let state = D3D12_RESOURCE_STATES(state as i32);
|
|
|
- // SAFETY: our swap chain, list and a live source texture.
|
|
|
+ // SAFETY: our swap chain, list and live source texture(s).
|
|
|
unsafe {
|
|
|
let index = p.swap_chain.GetCurrentBackBufferIndex();
|
|
|
let Ok(back) = p.swap_chain.GetBuffer::<ID3D12Resource>(index) else {
|
|
|
@@ -8878,36 +9021,119 @@ impl Host {
|
|
|
None => &p.copy,
|
|
|
};
|
|
|
copy.reset();
|
|
|
+ copy.keep(texture);
|
|
|
let list = ©.list;
|
|
|
- list.ResourceBarrier(&[
|
|
|
- transition(texture, state, D3D12_RESOURCE_STATE_COPY_SOURCE),
|
|
|
- transition(
|
|
|
- &back,
|
|
|
- D3D12_RESOURCE_STATE_PRESENT,
|
|
|
- D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
- ),
|
|
|
- ]);
|
|
|
- list.CopyResource(&back, texture);
|
|
|
+ let composed = if let Some((over, over_state, base_fmt, over_fmt)) = planes {
|
|
|
+ copy.keep(over);
|
|
|
+ if p.composer.is_none() {
|
|
|
+ match crate::compose::Composer::new(self, format) {
|
|
|
+ Ok(c) => p.composer = Some(c),
|
|
|
+ Err(e) => tracing::warn!(error = %e, "two-plane composer not made; showing base plane only"),
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if let Some(composer) = p.composer.as_mut() {
|
|
|
+ let mut pre = Vec::with_capacity(3);
|
|
|
+ if state != D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE {
|
|
|
+ pre.push(transition(texture, state, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
|
|
|
+ }
|
|
|
+ if over_state != D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE {
|
|
|
+ pre.push(transition(over, over_state, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
|
|
|
+ }
|
|
|
+ pre.push(transition(&back, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));
|
|
|
+ list.ResourceBarrier(&pre);
|
|
|
+ let compose_slot = slot.map_or(0, |i| i + 1);
|
|
|
+ let _ = composer.record(
|
|
|
+ self,
|
|
|
+ compose_slot,
|
|
|
+ list,
|
|
|
+ [(texture, base_fmt), (over, over_fmt)],
|
|
|
+ &back,
|
|
|
+ (width, height),
|
|
|
+ );
|
|
|
+ let mut post = Vec::with_capacity(3);
|
|
|
+ if state != D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE {
|
|
|
+ post.push(transition(texture, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, state));
|
|
|
+ }
|
|
|
+ if over_state != D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE {
|
|
|
+ post.push(transition(over, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, over_state));
|
|
|
+ }
|
|
|
+ post.push(transition(
|
|
|
+ &back,
|
|
|
+ D3D12_RESOURCE_STATE_RENDER_TARGET,
|
|
|
+ D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
+ ));
|
|
|
+ list.ResourceBarrier(&post);
|
|
|
+ true
|
|
|
+ } else {
|
|
|
+ false
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ false
|
|
|
+ };
|
|
|
+ if !composed {
|
|
|
+ list.ResourceBarrier(&[
|
|
|
+ transition(texture, state, D3D12_RESOURCE_STATE_COPY_SOURCE),
|
|
|
+ transition(
|
|
|
+ &back,
|
|
|
+ D3D12_RESOURCE_STATE_PRESENT,
|
|
|
+ D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
+ ),
|
|
|
+ ]);
|
|
|
+ list.CopyResource(&back, texture);
|
|
|
+ }
|
|
|
#[cfg(feature = "diagnostics")]
|
|
|
let capture = crate::capture::wanted(frame)
|
|
|
- .then(|| crate::capture::read_back(&self.device, list, texture))
|
|
|
+ .then(|| {
|
|
|
+ if composed {
|
|
|
+ list.ResourceBarrier(&[transition(
|
|
|
+ &back,
|
|
|
+ D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
+ D3D12_RESOURCE_STATE_COPY_SOURCE,
|
|
|
+ )]);
|
|
|
+ let res = crate::capture::read_back(&self.device, list, &back);
|
|
|
+ list.ResourceBarrier(&[transition(
|
|
|
+ &back,
|
|
|
+ D3D12_RESOURCE_STATE_COPY_SOURCE,
|
|
|
+ D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
+ )]);
|
|
|
+ res
|
|
|
+ } else {
|
|
|
+ crate::capture::read_back(&self.device, list, texture)
|
|
|
+ }
|
|
|
+ })
|
|
|
.flatten();
|
|
|
#[cfg(feature = "diagnostics")]
|
|
|
- let probe = if slot.is_some() { None } else { crate::capture::probe(&self.device, list, texture) };
|
|
|
+ let probe = if slot.is_some() || composed {
|
|
|
+ None
|
|
|
+ } else {
|
|
|
+ crate::capture::probe(&self.device, list, texture)
|
|
|
+ };
|
|
|
#[cfg(feature = "diagnostics")]
|
|
|
- let watched = crate::popwatch::take(&self.device, list, texture);
|
|
|
+ let watched = if composed {
|
|
|
+ None
|
|
|
+ } else {
|
|
|
+ crate::popwatch::take(&self.device, list, texture)
|
|
|
+ };
|
|
|
#[cfg(feature = "diagnostics")]
|
|
|
p.panel.draw(self, copy, &back, (width, height), format);
|
|
|
#[cfg(not(feature = "diagnostics"))]
|
|
|
let _ = frame;
|
|
|
- list.ResourceBarrier(&[
|
|
|
- transition(texture, D3D12_RESOURCE_STATE_COPY_SOURCE, state),
|
|
|
- transition(
|
|
|
+ if composed {
|
|
|
+ list.ResourceBarrier(&[transition(
|
|
|
&back,
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
D3D12_RESOURCE_STATE_PRESENT,
|
|
|
- ),
|
|
|
- ]);
|
|
|
+ )]);
|
|
|
+ } else {
|
|
|
+ list.ResourceBarrier(&[
|
|
|
+ transition(texture, D3D12_RESOURCE_STATE_COPY_SOURCE, state),
|
|
|
+ transition(
|
|
|
+ &back,
|
|
|
+ D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
+ D3D12_RESOURCE_STATE_PRESENT,
|
|
|
+ ),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
copy.close();
|
|
|
let flown = match slot {
|
|
|
Some(i) => {
|
|
|
@@ -8989,6 +9215,7 @@ impl Host {
|
|
|
ahead: Vec::new(),
|
|
|
hwnd,
|
|
|
meter: Meter::new(),
|
|
|
+ composer: None,
|
|
|
#[cfg(feature = "diagnostics")]
|
|
|
panel: panel::Panel::new(),
|
|
|
})
|