Optimize viewer shaders and camera controls; sample GPU profiles asynchronously

Amp-Thread-ID: https://ampcode.com/threads/T-01a0b382-1e79-712b-abd1-d29201c9bb5e
Co-authored-by: Heaust Azure <heaust.azure@gmail.com>
This commit is contained in:
Amp
2026-09-18 11:52:09 +00:00
co-authored by heaust
parent bc957cf40d
commit eb8aa0e9dc
21 changed files with 762 additions and 174 deletions
+8 -12
View File
@@ -1,8 +1,8 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use std::num::NonZeroU64;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::Arc;
use futures_channel::oneshot;
use serde::de::DeserializeOwned;
use serde_json::Value;
@@ -31,7 +31,7 @@ pub struct GpuProfiler {
pub struct ProfileMap {
readback: Arc<wgpu::Buffer>,
state: Arc<AtomicU8>,
completion: oneshot::Receiver<Result<(), wgpu::BufferAsyncError>>,
labels: Vec<String>,
timestamp_period: f32,
}
@@ -417,16 +417,15 @@ impl GpuResources {
pub fn map_profile(&self, timestamp_period: f32) -> Option<ProfileMap> {
let profiler = self.profiler.as_ref()?;
let state = Arc::new(AtomicU8::new(0));
let callback = state.clone();
let (complete, completion) = oneshot::channel();
profiler
.readback
.map_async(wgpu::MapMode::Read, .., move |result| {
callback.store(if result.is_ok() { 1 } else { 2 }, Ordering::Release);
let _ = complete.send(result);
});
Some(ProfileMap {
readback: profiler.readback.clone(),
state,
completion,
labels: self
.passes
.iter()
@@ -438,11 +437,8 @@ impl GpuResources {
}
impl ProfileMap {
pub fn state(&self) -> u8 {
self.state.load(Ordering::Acquire)
}
pub fn read(self) -> Vec<(String, f64)> {
pub async fn read(self) -> Option<Vec<(String, f64)>> {
self.completion.await.ok()?.ok()?;
let bytes = self.readback.get_mapped_range(..);
let values = bytes
.chunks_exact(8)
@@ -463,7 +459,7 @@ impl ProfileMap {
.collect();
drop(bytes);
self.readback.unmap();
profile
Some(profile)
}
}