|
@@ -126,14 +126,17 @@ const SHADER_VISIBLE: u32 = 1;
|
|
|
/// Shader-visible heaps by address range, for mirroring descriptor writes:
|
|
/// Shader-visible heaps by address range, for mirroring descriptor writes:
|
|
|
/// start, end, heap.
|
|
/// start, end, heap.
|
|
|
#[cfg(windows)]
|
|
#[cfg(windows)]
|
|
|
-static SHADER_HEAPS: std::sync::Mutex<Vec<(u64, u64, usize)>> = std::sync::Mutex::new(Vec::new());
|
|
|
|
|
|
|
+///
|
|
|
|
|
+/// Held shared while a heap found here is used (by every thread that writes
|
|
|
|
|
+/// descriptors, and by [`sync_heaps`]), alone only where heaps come and go.
|
|
|
|
|
+static SHADER_HEAPS: std::sync::RwLock<Vec<(u64, u64, usize)>> = std::sync::RwLock::new(Vec::new());
|
|
|
|
|
|
|
|
/// Makes the host descriptor for what the title wrote at `handle`, if it
|
|
/// Makes the host descriptor for what the title wrote at `handle`, if it
|
|
|
/// is in a shader-visible heap. Keeps `SHADER_HEAPS` locked for the duration
|
|
/// is in a shader-visible heap. Keeps `SHADER_HEAPS` locked for the duration
|
|
|
/// so `Heap::drop` cannot free the heap concurrently.
|
|
/// so `Heap::drop` cannot free the heap concurrently.
|
|
|
#[cfg(windows)]
|
|
#[cfg(windows)]
|
|
|
fn mirror(handle: u64) {
|
|
fn mirror(handle: u64) {
|
|
|
- let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
|
|
+ let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
|
|
|
let Some(&(_, _, at)) = heaps
|
|
let Some(&(_, _, at)) = heaps
|
|
|
.iter()
|
|
.iter()
|
|
|
.find(|&&(start, end, _)| (start..end).contains(&handle))
|
|
.find(|&&(start, end, _)| (start..end).contains(&handle))
|
|
@@ -177,7 +180,7 @@ pub(crate) fn sync_heaps() {
|
|
|
let started = std::time::Instant::now();
|
|
let started = std::time::Instant::now();
|
|
|
let (mut pages, mut looked, mut mirrored) = (0u64, 0u64, 0u64);
|
|
let (mut pages, mut looked, mut mirrored) = (0u64, 0u64, 0u64);
|
|
|
let mut page_bytes: Vec<u8> = Vec::new();
|
|
let mut page_bytes: Vec<u8> = Vec::new();
|
|
|
- let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
|
|
+ let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
|
|
|
for &(start, end, at) in heaps.iter() {
|
|
for &(start, end, at) in heaps.iter() {
|
|
|
// SAFETY: registered heaps are live (removed when dropped).
|
|
// SAFETY: registered heaps are live (removed when dropped).
|
|
|
let heap = unsafe { &*(at as *const Heap) };
|
|
let heap = unsafe { &*(at as *const Heap) };
|
|
@@ -275,7 +278,7 @@ fn changed_slots(
|
|
|
/// `heap`, if that heap still exists.
|
|
/// `heap`, if that heap still exists.
|
|
|
#[cfg(windows)]
|
|
#[cfg(windows)]
|
|
|
pub(crate) fn release_dynamic(heap: usize, first: u32, n: u32) {
|
|
pub(crate) fn release_dynamic(heap: usize, first: u32, n: u32) {
|
|
|
- let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
|
|
+ let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
|
|
|
if heaps.iter().any(|&(_, _, h)| h == heap)
|
|
if heaps.iter().any(|&(_, _, h)| h == heap)
|
|
|
// SAFETY: registered heaps are live (removed when dropped).
|
|
// SAFETY: registered heaps are live (removed when dropped).
|
|
|
&& let Some(Some(host)) = unsafe { &*(heap as *const Heap) }.host.get()
|
|
&& let Some(Some(host)) = unsafe { &*(heap as *const Heap) }.host.get()
|
|
@@ -289,7 +292,7 @@ pub(crate) fn release_dynamic(heap: usize, first: u32, n: u32) {
|
|
|
/// bytes at `gpu`.
|
|
/// bytes at `gpu`.
|
|
|
#[cfg(windows)]
|
|
#[cfg(windows)]
|
|
|
pub(crate) fn constant_slot(heap: usize, index: u32, gpu: u64, size: u32) {
|
|
pub(crate) fn constant_slot(heap: usize, index: u32, gpu: u64, size: u32) {
|
|
|
- let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
|
|
+ let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
|
|
|
if heaps.iter().any(|&(_, _, h)| h == heap)
|
|
if heaps.iter().any(|&(_, _, h)| h == heap)
|
|
|
// SAFETY: registered heaps are live (removed when dropped).
|
|
// SAFETY: registered heaps are live (removed when dropped).
|
|
|
&& let Some(Some(host)) = unsafe { &*(heap as *const Heap) }.host.get()
|
|
&& let Some(Some(host)) = unsafe { &*(heap as *const Heap) }.host.get()
|
|
@@ -302,7 +305,7 @@ pub(crate) fn constant_slot(heap: usize, index: u32, gpu: u64, size: u32) {
|
|
|
impl Drop for Heap {
|
|
impl Drop for Heap {
|
|
|
fn drop(&mut self) {
|
|
fn drop(&mut self) {
|
|
|
let at = std::ptr::from_ref::<Heap>(self) as usize;
|
|
let at = std::ptr::from_ref::<Heap>(self) as usize;
|
|
|
- let mut heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
|
|
+ let mut heaps = SHADER_HEAPS.write().unwrap_or_else(|e| e.into_inner());
|
|
|
heaps.retain(|&(_, _, h)| h != at);
|
|
heaps.retain(|&(_, _, h)| h != at);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -819,7 +822,14 @@ pub(crate) fn make_waiting() {
|
|
|
// texture without a host copy must go on waiting).
|
|
// texture without a host copy must go on waiting).
|
|
|
// (Looked up first: the handle is read only while its heap is
|
|
// (Looked up first: the handle is read only while its heap is
|
|
|
// there.)
|
|
// there.)
|
|
|
- let Some((heap, index)) = shader_slot(handle) else {
|
|
|
|
|
|
|
+ // (The heap stays while the lock is held: `mirror`.)
|
|
|
|
|
+ let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
+ let Some(&(_, _, at)) = heaps.iter().find(|&&(start, end, _)| (start..end).contains(&handle)) else {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ };
|
|
|
|
|
+ // SAFETY: a registered heap, live while the lock is held.
|
|
|
|
|
+ let heap = unsafe { &*(at as *const Heap) };
|
|
|
|
|
+ let Some(index) = heap.index(handle) else {
|
|
|
continue;
|
|
continue;
|
|
|
};
|
|
};
|
|
|
let same = match view(handle) {
|
|
let same = match view(handle) {
|
|
@@ -1113,7 +1123,7 @@ pub(crate) extern "system" fn create_heap(
|
|
|
#[cfg(windows)]
|
|
#[cfg(windows)]
|
|
|
if visible {
|
|
if visible {
|
|
|
let at = std::ptr::from_ref::<Heap>(&heap) as usize;
|
|
let at = std::ptr::from_ref::<Heap>(&heap) as usize;
|
|
|
- let mut heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
|
|
+ let mut heaps = SHADER_HEAPS.write().unwrap_or_else(|e| e.into_inner());
|
|
|
heaps.push((start, start + size, at));
|
|
heaps.push((start, start + size, at));
|
|
|
}
|
|
}
|
|
|
#[cfg(not(windows))]
|
|
#[cfg(not(windows))]
|