Write textures in separate thread; fixes perf

This commit is contained in:
2024-11-11 19:27:23 -05:00
parent f22a74b036
commit d306f19297
5 changed files with 167 additions and 75 deletions
+18 -9
View File
@@ -11,7 +11,7 @@ use std::{
use anyhow::Result;
use crate::{audio::Audio, renderer::GameRenderer};
use crate::{audio::Audio, graphics::TextureSink};
use shrooms_vb_core::Sim;
pub use shrooms_vb_core::VBKey;
@@ -30,7 +30,10 @@ pub enum SimId {
Player2,
}
impl SimId {
pub fn to_index(self) -> usize {
pub const fn values() -> [Self; 2] {
[Self::Player1, Self::Player2]
}
pub const fn to_index(self) -> usize {
match self {
Self::Player1 => 0,
Self::Player2 => 1,
@@ -75,7 +78,7 @@ pub struct Emulator {
sims: Vec<Sim>,
audio: Audio,
commands: mpsc::Receiver<EmulatorCommand>,
renderers: HashMap<SimId, GameRenderer>,
renderers: HashMap<SimId, TextureSink>,
running: Arc<AtomicBool>,
has_game: Arc<AtomicBool>,
}
@@ -140,11 +143,17 @@ impl Emulator {
idle = false;
Sim::emulate_many(&mut self.sims);
}
for (sim_id, renderer) in self.renderers.iter_mut() {
if let Some(sim) = self.sims.get_mut(sim_id.to_index()) {
if sim.read_pixels(&mut eye_contents) {
idle = false;
renderer.render(&eye_contents);
for sim_id in SimId::values() {
let Some(renderer) = self.renderers.get_mut(&sim_id) else {
continue;
};
let Some(sim) = self.sims.get_mut(sim_id.to_index()) else {
continue;
};
if sim.read_pixels(&mut eye_contents) {
idle = false;
if renderer.queue_render(&eye_contents).is_err() {
self.renderers.remove(&sim_id);
}
}
}
@@ -226,7 +235,7 @@ impl Emulator {
#[derive(Debug)]
pub enum EmulatorCommand {
SetRenderer(SimId, GameRenderer),
SetRenderer(SimId, TextureSink),
LoadGame(SimId, PathBuf),
StartSecondSim(Option<PathBuf>),
StopSecondSim,