Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

desktop: Track wgpu stats in Tracy #12099

Merged
merged 3 commits into from
Jul 19, 2023
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
9 changes: 3 additions & 6 deletions desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::custom_event::RuffleEvent;
use crate::gui::{GuiController, MENU_HEIGHT};
use crate::player::{PlayerController, PlayerOptions};
use crate::util::{
get_screen_size, parse_url, pick_file, winit_key_to_char, winit_to_ruffle_key_code,
winit_to_ruffle_text_control,
get_screen_size, parse_url, pick_file, plot_stats_in_tracy, winit_key_to_char,
winit_to_ruffle_key_code, winit_to_ruffle_text_control,
};
use anyhow::{Context, Error};
use ruffle_core::{PlayerEvent, StageDisplayState};
Expand Down Expand Up @@ -136,10 +136,7 @@ impl App {
} else {
self.gui.borrow_mut().render(None);
}
#[cfg(feature = "tracy")]
tracing_tracy::client::Client::running()
.expect("tracy client must be running")
.frame_mark();
plot_stats_in_tracy(&self.gui.borrow().descriptors().wgpu_instance);
}
}

Expand Down
4 changes: 2 additions & 2 deletions desktop/src/gui/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl GuiController {
let surface = unsafe { instance.create_surface(window.as_ref()) }?;
let (adapter, device, queue) = futures::executor::block_on(request_adapter_and_device(
backend,
instance,
&instance,
Some(&surface),
opt.power.into(),
opt.trace_path(),
Expand All @@ -83,7 +83,7 @@ impl GuiController {
view_formats: Default::default(),
},
);
let descriptors = Descriptors::new(adapter, device, queue);
let descriptors = Descriptors::new(instance, adapter, device, queue);
let egui_ctx = Context::default();
if let Some(Theme::Light) = window.theme() {
egui_ctx.set_visuals(egui::Visuals::light());
Expand Down
39 changes: 39 additions & 0 deletions desktop/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,42 @@ pub fn pick_file(in_ui: bool, path: Option<PathBuf>) -> Option<PathBuf> {
pub fn pick_file(_in_ui: bool, path: Option<PathBuf>) -> Option<PathBuf> {
actually_pick_file(path)
}

#[cfg(not(feature = "tracy"))]
pub fn plot_stats_in_tracy(_instance: &wgpu::Instance) {}

#[cfg(feature = "tracy")]
pub fn plot_stats_in_tracy(instance: &wgpu::Instance) {
use tracing_tracy::client::*;
const BIND_GROUPS: PlotName = plot_name!("Bind Groups");
const BUFFERS: PlotName = plot_name!("Buffers");
const TEXTURES: PlotName = plot_name!("Textures");
const TEXTURE_VIEWS: PlotName = plot_name!("Texture Views");

let tracy = Client::running().expect("tracy client must be running");
let report = instance.generate_report();

#[allow(unused_mut)]
let mut backend = None;
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
{
backend = backend.or(report.vulkan).or(report.gl);
}
#[cfg(windows)]
{
backend = backend.or(report.dx12).or(report.dx11);
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
{
backend = backend.or(report.metal);
}

if let Some(stats) = backend {
tracy.plot(BIND_GROUPS, stats.bind_groups.num_occupied as f64);
tracy.plot(BUFFERS, stats.buffers.num_occupied as f64);
tracy.plot(TEXTURES, stats.textures.num_occupied as f64);
tracy.plot(TEXTURE_VIEWS, stats.texture_views.num_occupied as f64);
}

tracy.frame_mark();
}
4 changes: 2 additions & 2 deletions exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,14 @@ fn main() -> Result<()> {
});
let (adapter, device, queue) = futures::executor::block_on(request_adapter_and_device(
opt.graphics.into(),
instance,
&instance,
None,
opt.power.into(),
trace_path(&opt),
))
.map_err(|e| anyhow!(e.to_string()))?;

let descriptors = Arc::new(Descriptors::new(adapter, device, queue));
let descriptors = Arc::new(Descriptors::new(instance, adapter, device, queue));

if opt.swf.is_file() {
capture_single_swf(descriptors, &opt)?;
Expand Down
14 changes: 7 additions & 7 deletions render/wgpu/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ impl WgpuRenderBackend<SwapChainTarget> {
let surface = instance.create_surface_from_canvas(canvas)?;
let (adapter, device, queue) = request_adapter_and_device(
wgpu::Backends::BROWSER_WEBGPU | wgpu::Backends::GL,
instance,
&instance,
Some(&surface),
wgpu::PowerPreference::HighPerformance,
None,
)
.await?;
let descriptors = Descriptors::new(adapter, device, queue);
let descriptors = Descriptors::new(instance, adapter, device, queue);
let target =
SwapChainTarget::new(surface, &descriptors.adapter, (1, 1), &descriptors.device);
Self::new(Arc::new(descriptors), target)
Expand Down Expand Up @@ -102,12 +102,12 @@ impl WgpuRenderBackend<SwapChainTarget> {
let surface = unsafe { instance.create_surface(window) }?;
let (adapter, device, queue) = futures::executor::block_on(request_adapter_and_device(
backend,
instance,
&instance,
Some(&surface),
power_preference,
trace_path,
))?;
let descriptors = Descriptors::new(adapter, device, queue);
let descriptors = Descriptors::new(instance, adapter, device, queue);
let target = SwapChainTarget::new(surface, &descriptors.adapter, size, &descriptors.device);
Self::new(Arc::new(descriptors), target)
}
Expand All @@ -133,12 +133,12 @@ impl WgpuRenderBackend<crate::target::TextureTarget> {
});
let (adapter, device, queue) = futures::executor::block_on(request_adapter_and_device(
backend,
instance,
&instance,
None,
power_preference,
trace_path,
))?;
let descriptors = Descriptors::new(adapter, device, queue);
let descriptors = Descriptors::new(instance, adapter, device, queue);
let target = crate::target::TextureTarget::new(&descriptors.device, size)?;
Self::new(Arc::new(descriptors), target)
}
Expand Down Expand Up @@ -1031,7 +1031,7 @@ impl<T: RenderTarget + 'static> RenderBackend for WgpuRenderBackend<T> {

pub async fn request_adapter_and_device(
backend: wgpu::Backends,
instance: wgpu::Instance,
instance: &wgpu::Instance,
surface: Option<&wgpu::Surface>,
power_preference: wgpu::PowerPreference,
trace_path: Option<&Path>,
Expand Down
9 changes: 8 additions & 1 deletion render/wgpu/src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::mem;
use std::sync::{Arc, Mutex};

pub struct Descriptors {
pub wgpu_instance: wgpu::Instance,
pub adapter: wgpu::Adapter,
pub device: wgpu::Device,
pub limits: wgpu::Limits,
Expand All @@ -34,7 +35,12 @@ impl Debug for Descriptors {
}

impl Descriptors {
pub fn new(adapter: wgpu::Adapter, device: wgpu::Device, queue: wgpu::Queue) -> Self {
pub fn new(
instance: wgpu::Instance,
adapter: wgpu::Adapter,
device: wgpu::Device,
queue: wgpu::Queue,
) -> Self {
let limits = device.limits();
let bind_layouts = BindLayouts::new(&device);
let bitmap_samplers = BitmapSamplers::new(&device);
Expand All @@ -57,6 +63,7 @@ impl Descriptors {
let filters = Filters::new(&device);

Self {
wgpu_instance: instance,
adapter,
device,
limits,
Expand Down
10 changes: 6 additions & 4 deletions tests/tests/util/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ use std::sync::{Arc, OnceLock};
but for `cargo nextest run` it's a big cost per test if it's not going to use it.
*/

fn create_wgpu_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
fn create_wgpu_device() -> Option<(wgpu::Instance, wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
let instance = wgpu::Instance::new(Default::default());
futures::executor::block_on(request_adapter_and_device(
wgpu::Backends::all(),
wgpu::Instance::new(Default::default()),
&instance,
None,
Default::default(),
None,
))
.ok()
.map(|(adapter, device, queue)| (instance, adapter, device, queue))
}

fn build_wgpu_descriptors() -> Option<Arc<Descriptors>> {
if let Some((adapter, device, queue)) = create_wgpu_device() {
Some(Arc::new(Descriptors::new(adapter, device, queue)))
if let Some((instance, adapter, device, queue)) = create_wgpu_device() {
Some(Arc::new(Descriptors::new(instance, adapter, device, queue)))
} else {
None
}
Expand Down