Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions crates/io/or-io/src/meta/binary/hash_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,14 @@ static INSTALLED_INDEX: OnceLock<HashIndex> = OnceLock::new();
static PUBLIC_INDEX: OnceLock<HashIndex> = OnceLock::new();
static CLEAN_INDEX: OnceLock<HashIndex> = OnceLock::new();
static TITLE_INDEX: OnceLock<HashIndex> = OnceLock::new();
static TITLE_PUBLIC_INDEX: OnceLock<HashIndex> = OnceLock::new();

/// Whether any default hash index has already been built for this process.
pub(crate) fn has_default_index() -> bool {
INSTALLED_INDEX.get().is_some() || PUBLIC_INDEX.get().is_some() || TITLE_INDEX.get().is_some()
INSTALLED_INDEX.get().is_some()
|| PUBLIC_INDEX.get().is_some()
|| TITLE_INDEX.get().is_some()
|| TITLE_PUBLIC_INDEX.get().is_some()
}

/// The names the default library and [`crate::meta::binary::public_names`] give.
Expand All @@ -199,11 +203,18 @@ pub fn default_hash_index() -> &'static HashIndex {
let installed = crate::meta::snapshot::installed();
let title = crate::meta::snapshot::names_library().filter(|_| TITLE_NAMES.with(Cell::get));
if let Some(title) = title {
return TITLE_INDEX.get_or_init(|| {
let mut index = HashIndex::from_library(title);
installed.into_iter().for_each(|l| index.add_library(l));
index.with_public_names().with_parser_names()
});
return match installed {
Some(library) => TITLE_INDEX.get_or_init(|| {
let mut index = HashIndex::from_library(title);
index.add_library(library);
index.with_public_names().with_parser_names()
}),
None => TITLE_PUBLIC_INDEX.get_or_init(|| {
HashIndex::from_library(title)
.with_public_names()
.with_parser_names()
}),
};
}
match installed {
Some(library) => INSTALLED_INDEX.get_or_init(|| {
Expand Down
6 changes: 6 additions & 0 deletions crates/io/or-io/src/meta/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ pub fn install_names_snapshot(path: &Path) -> Result<&'static SchemaLibrary> {
if let Some(held) = NAMES.get() {
return Ok(held);
}
if crate::meta::binary::hash_index::has_default_index() {
tracing::warn!(
path = %path.display(),
"a default hash index was already built before GTA VI's names snapshot was installed"
);
}
let library = load_snapshot(path)?;
Ok(NAMES.get_or_init(|| library))
}
Expand Down
81 changes: 67 additions & 14 deletions crates/runtime/or-renderer/src/game_shaders/kept_dx12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::cell::{Cell, RefCell};
use std::num::NonZeroU32;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::OnceLock;
use std::sync::{PoisonError, RwLock};

use naga::back::hlsl;
use naga::ShaderStage;
Expand Down Expand Up @@ -48,10 +48,66 @@ const MOST: hlsl::ShaderModel = hlsl::ShaderModel::V6_6;
/// The features a device is asked for where its adapter has them, to be handed a kept stage.
pub(super) const FEATURES: wgpu::Features = wgpu::Features::PASSTHROUGH_SHADERS;

static FOLDER: OnceLock<PathBuf> = OnceLock::new();
/// The file whose being there says that the device refused a stage compiled here, named by its
/// adapter, driver and compiler.
static MARK: OnceLock<PathBuf> = OnceLock::new();
struct FolderState {
folder: Option<PathBuf>,
/// The file whose being there says that the device refused a stage compiled here, named by its
/// adapter, driver and compiler.
mark: Option<PathBuf>,
}

struct FolderSlot(RwLock<FolderState>);

impl FolderSlot {
const fn new() -> Self {
Self(RwLock::new(FolderState {
folder: None,
mark: None,
}))
}

fn set(&self, folder: PathBuf) {
let mut state = self.0.write().unwrap_or_else(PoisonError::into_inner);
if state.folder.as_ref() == Some(&folder) {
return;
}
state.folder = Some(folder);
state.mark = None;
}

fn folder(&self) -> Option<PathBuf> {
self.0
.read()
.unwrap_or_else(PoisonError::into_inner)
.folder
.clone()
}

fn mark(&self) -> Option<PathBuf> {
self.0
.read()
.unwrap_or_else(PoisonError::into_inner)
.mark
.clone()
}

fn mark_for(&self, folder: &Path, adapter: &str, compiler: &str) -> PathBuf {
let named = hex(&digest(&[MIRRORED, adapter, compiler]));
let mark = folder.join(format!("refused-{named}"));
let cached = self
.0
.read()
.unwrap_or_else(PoisonError::into_inner)
.mark
.as_ref()
== Some(&mark);
if !cached {
self.0.write().unwrap_or_else(PoisonError::into_inner).mark = Some(mark.clone());
}
mark
}
}

static STATE: FolderSlot = FolderSlot::new();
static REFUSED: AtomicBool = AtomicBool::new(false);
static PARTS: AtomicU64 = AtomicU64::new(0);

Expand All @@ -62,9 +118,9 @@ thread_local! {
static OFF: Cell<bool> = const { Cell::new(false) };
}

/// Keep the compiled stages in `folder`; the first call of a process decides.
/// Keep the compiled stages in `folder`.
pub(super) fn keep_in(folder: PathBuf) {
let _ = FOLDER.set(folder);
STATE.set(folder);
}

/// Whether a device of `backend` with `features` is handed stages compiled here.
Expand Down Expand Up @@ -123,8 +179,8 @@ pub(super) fn without<T, E>(build: impl FnOnce() -> Result<T, E>) -> Result<T, E
OFF.with(|off| off.set(false));
if built.is_ok() {
REFUSED.store(true, Ordering::Relaxed);
if let Some(mark) = MARK.get() {
store(mark, b"");
if let Some(mark) = STATE.mark() {
store(&mark, b"");
}
tracing::warn!("the device refuses Direct3D 12 stages compiled here: passes are compiled by wgpu until its driver or the compiler changes");
}
Expand All @@ -144,16 +200,13 @@ fn dxil_of(
if !used_on(info.backend, device.features()) {
return None;
}
let folder = FOLDER.get()?;
let folder = STATE.folder()?;
let dxc = dxc::compiler()?;
let adapter = format!(
"{} {:x}:{:x} {} {}",
info.name, info.vendor, info.device, info.driver, info.driver_info
);
let mark = MARK.get_or_init(|| {
let named = hex(&digest(&[MIRRORED, adapter.as_str(), dxc.identity()]));
folder.join(format!("refused-{named}"))
});
let mark = STATE.mark_for(&folder, &adapter, dxc.identity());
if mark.exists() {
REFUSED.store(true, Ordering::Relaxed);
return None;
Expand Down
30 changes: 30 additions & 0 deletions crates/runtime/or-renderer/src/game_shaders/kept_dx12/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,33 @@ fn a_function_of_a_choice_between_bare_floats_compiles_for_direct3d_12() {
let compiled = dxc.compile(&written.source, &arguments);
assert!(compiled.is_ok(), "{compiled:?}\n{}", written.source);
}

#[test]
fn late_keep_in_updates_the_cache_folder_and_refusal_mark() {
let first = tempfile::tempdir().expect("first scratch folder");
let second = tempfile::tempdir().expect("second scratch folder");
let slot = FolderSlot::new();

// Reading before configuration leaves the slot open to a later `keep_in`.
assert_eq!(slot.folder(), None);
assert_eq!(slot.mark(), None);

slot.set(first.path().to_path_buf());
assert_eq!(slot.folder().as_deref(), Some(first.path()));
let first_mark = slot.mark_for(first.path(), "adapter 1.0", "dxc 1.8");
assert_eq!(first_mark.parent(), Some(first.path()));
assert_eq!(slot.mark().as_deref(), Some(first_mark.as_path()));

// Setting the same folder again keeps the cached mark intact.
slot.set(first.path().to_path_buf());
assert_eq!(slot.mark().as_deref(), Some(first_mark.as_path()));

// A later `keep_in` with a new folder takes effect and invalidates the previous mark.
slot.set(second.path().to_path_buf());
assert_eq!(slot.folder().as_deref(), Some(second.path()));
assert_eq!(slot.mark(), None);
let second_mark = slot.mark_for(second.path(), "adapter 1.0", "dxc 1.8");
assert_eq!(second_mark.parent(), Some(second.path()));
assert_ne!(second_mark, first_mark);
assert_eq!(slot.mark().as_deref(), Some(second_mark.as_path()));
}
Loading
Loading