feat(runtime): implement shared transactional memcard with async saves
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#![allow(clippy::collapsible_if)]
|
||||
mod resumable;
|
||||
use crate::memory_banks::{GlyphBankPoolInstaller, SceneBankPoolInstaller, SoundBankPoolInstaller};
|
||||
use prometeu_hal::AssetBridge;
|
||||
use prometeu_hal::asset::{
|
||||
@@ -340,6 +341,8 @@ pub struct AssetManager {
|
||||
|
||||
// Commits that are ready to be applied at the next frame boundary.
|
||||
pending_commits: Mutex<Vec<(HandleId, u64)>>,
|
||||
preload_handles: Mutex<Vec<HandleId>>,
|
||||
preload_failed: std::sync::atomic::AtomicBool,
|
||||
}
|
||||
|
||||
struct LoadHandleInfo {
|
||||
@@ -374,6 +377,7 @@ struct AssetLoadQueueState {
|
||||
pending: VecDeque<AssetLoadJob>,
|
||||
active: Option<AssetLoadJob>,
|
||||
shutdown: bool,
|
||||
scheduled: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -470,6 +474,7 @@ impl AssetLoadQueue {
|
||||
struct AssetLoadWorker {
|
||||
queue: Arc<AssetLoadQueue>,
|
||||
join_handle: Option<thread::JoinHandle<()>>,
|
||||
shared: Option<(Arc<dyn prometeu_hal::background_work::BackgroundWork>, resumable::Resources)>,
|
||||
}
|
||||
|
||||
impl AssetLoadWorker {
|
||||
@@ -500,11 +505,15 @@ impl AssetLoadWorker {
|
||||
}
|
||||
});
|
||||
|
||||
Self { queue, join_handle: Some(join_handle) }
|
||||
Self { queue, join_handle: Some(join_handle), shared: None }
|
||||
}
|
||||
|
||||
fn submit_counting_superseded(&self, job: AssetLoadJob) -> usize {
|
||||
self.queue.submit(job)
|
||||
let removed = self.queue.submit(job);
|
||||
if let Some((lane, resources)) = &self.shared {
|
||||
resumable::schedule(self.queue.clone(), lane.clone(), resources.clone());
|
||||
}
|
||||
removed
|
||||
}
|
||||
|
||||
fn clear_pending(&self) {
|
||||
@@ -544,6 +553,25 @@ enum AssetOpMode {
|
||||
}
|
||||
|
||||
impl AssetBridge for AssetManager {
|
||||
fn bind_background_work(
|
||||
&mut self,
|
||||
lane: Arc<dyn prometeu_hal::background_work::BackgroundWork>,
|
||||
) -> bool {
|
||||
if self.load_worker.shared.as_ref().is_some_and(|(bound, _)| Arc::ptr_eq(bound, &lane)) {
|
||||
return true;
|
||||
}
|
||||
// Bind only before loading: replacing an active worker could block the main lane.
|
||||
if self.load_worker.pending_count() != 0 || self.load_worker.active_job().is_some() {
|
||||
return false;
|
||||
}
|
||||
let resources = resumable::Resources::new(self);
|
||||
self.load_worker = AssetLoadWorker {
|
||||
queue: Arc::new(AssetLoadQueue::default()),
|
||||
join_handle: None,
|
||||
shared: Some((lane, resources)),
|
||||
};
|
||||
true
|
||||
}
|
||||
fn initialize_for_cartridge(
|
||||
&self,
|
||||
assets: Vec<AssetEntry>,
|
||||
@@ -552,6 +580,12 @@ impl AssetBridge for AssetManager {
|
||||
) {
|
||||
self.initialize_for_cartridge(assets, preload, assets_data)
|
||||
}
|
||||
fn preloads_ready(&self) -> Result<bool, AssetLoadError> {
|
||||
if self.preload_failed.load(std::sync::atomic::Ordering::Acquire) {
|
||||
return Err(AssetLoadError::BackendError);
|
||||
}
|
||||
Ok(self.preload_handles.lock().unwrap().is_empty())
|
||||
}
|
||||
fn load(&self, asset_id: AssetId, slot_index: usize) -> Result<HandleId, AssetLoadError> {
|
||||
self.load(asset_id, slot_index)
|
||||
}
|
||||
@@ -715,6 +749,8 @@ impl AssetManager {
|
||||
pipeline_telemetry,
|
||||
load_worker,
|
||||
pending_commits: Mutex::new(Vec::new()),
|
||||
preload_handles: Mutex::new(Vec::new()),
|
||||
preload_failed: std::sync::atomic::AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -734,7 +770,16 @@ impl AssetManager {
|
||||
}
|
||||
*self.assets_data.write().unwrap() = assets_data;
|
||||
|
||||
// Perform Preload for assets in the preload list
|
||||
if self.load_worker.shared.is_some() {
|
||||
for item in preload {
|
||||
match self.load(item.asset_id, item.slot) {
|
||||
Ok(handle) => self.preload_handles.lock().unwrap().push(handle),
|
||||
Err(_) => self.preload_failed.store(true, std::sync::atomic::Ordering::SeqCst),
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Standalone bridges retain their synchronous preload compatibility path.
|
||||
for item in preload {
|
||||
let entry_opt = {
|
||||
let assets = self.assets.read().unwrap();
|
||||
@@ -1709,6 +1754,19 @@ impl AssetManager {
|
||||
}
|
||||
|
||||
pub fn apply_commits(&self) {
|
||||
self.preload_handles.lock().unwrap().retain(|&handle| match self.status(handle) {
|
||||
LoadStatus::READY => {
|
||||
self.commit(handle);
|
||||
false
|
||||
}
|
||||
LoadStatus::QUEUED | LoadStatus::LOADING | LoadStatus::PENDING | LoadStatus::ACTIVE => {
|
||||
true
|
||||
}
|
||||
_ => {
|
||||
self.preload_failed.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
false
|
||||
}
|
||||
});
|
||||
let mut pending = self.pending_commits.lock().unwrap();
|
||||
let mut handles = self.handles.write().unwrap();
|
||||
|
||||
@@ -1846,16 +1904,20 @@ impl AssetManager {
|
||||
}
|
||||
|
||||
pub fn shutdown(&self) {
|
||||
// Exclude worker publication while invalidating and clearing the cartridge.
|
||||
let mut generations = self.target_generations.write().unwrap();
|
||||
for generation in generations.values_mut() {
|
||||
*generation += 1;
|
||||
}
|
||||
self.gfx_policy.clear();
|
||||
self.sound_policy.clear();
|
||||
self.scene_policy.clear();
|
||||
self.handles.write().unwrap().clear();
|
||||
self.target_handles.write().unwrap().clear();
|
||||
for generation in self.target_generations.write().unwrap().values_mut() {
|
||||
*generation += 1;
|
||||
}
|
||||
self.load_worker.clear_pending();
|
||||
self.pending_commits.lock().unwrap().clear();
|
||||
self.preload_handles.lock().unwrap().clear();
|
||||
self.preload_failed.store(false, std::sync::atomic::Ordering::SeqCst);
|
||||
self.gfx_slots.write().unwrap().fill(None);
|
||||
self.glyph_slot_index.clear();
|
||||
self.sound_slots.write().unwrap().fill(None);
|
||||
@@ -1936,7 +1998,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_scene() -> SceneBank {
|
||||
pub(super) fn test_scene() -> SceneBank {
|
||||
let make_layer = |glyph_asset_id: AssetId,
|
||||
active: bool,
|
||||
parallax_x: f32,
|
||||
@@ -1999,7 +2061,7 @@ mod tests {
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn encode_scene_payload(scene: &SceneBank) -> Vec<u8> {
|
||||
pub(super) fn encode_scene_payload(scene: &SceneBank) -> Vec<u8> {
|
||||
let mut data = Vec::new();
|
||||
data.extend_from_slice(&SCENE_PAYLOAD_MAGIC_V1);
|
||||
data.extend_from_slice(&SCENE_PAYLOAD_VERSION_V1.to_le_bytes());
|
||||
@@ -2039,7 +2101,11 @@ mod tests {
|
||||
data
|
||||
}
|
||||
|
||||
fn test_scene_asset_entry(asset_name: &str, data: &[u8], scene: &SceneBank) -> AssetEntry {
|
||||
pub(super) fn test_scene_asset_entry(
|
||||
asset_name: &str,
|
||||
data: &[u8],
|
||||
scene: &SceneBank,
|
||||
) -> AssetEntry {
|
||||
AssetEntry {
|
||||
asset_id: 2,
|
||||
asset_name: asset_name.to_string(),
|
||||
|
||||
Reference in New Issue
Block a user