Skip to content

Commit

Permalink
feat: optimize rendering for different platforms
Browse files Browse the repository at this point in the history
- add `wgpu-profiler` as a dependency in the cargo.toml file
- remove the `wgpu-profiler` dependency from the old location in the cargo.toml file
- introduce `wgpu_profiler` usage within `src/renderer.rs` for platforms other than macos
- initialize `gpuprofiler` object conditionally in `renderer` struct based on the platform
- refactor the `draw_mask` function for platform specific contexts
- adjust render pass creation logic for offscreen copy and layer rendering based on platform
- scope the `drawable` rendering based on platform conditions
  • Loading branch information
falcucci committed Sep 19, 2024
1 parent f0b7e55 commit e76adb1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ thread_local = "1.1.7"
# Used to make the error output for shader recompilation easier to read
termcolor = "1.4.1"
wgpu = { workspace = true, features = ["glsl"] }
wgpu-profiler = { version = "0.18.0", features = ["tracy"] }
winit = { workspace = true }

[target.'cfg(not(target_os = "macos"))'.dependencies]
wgpu-profiler = { version = "0.18.0", features = ["tracy"] }

[dev-dependencies]
image-compare = "0.4.1"
git2 = "0.18.3"
86 changes: 77 additions & 9 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use futures::executor::block_on;
use glam::*;
use glamour::{AsRaw, Rect};
use wgpu::*;

#[cfg(not(target_os = "macos"))]
use wgpu_profiler::{GpuProfiler, GpuProfilerSettings};

use crate::{
Expand All @@ -29,6 +31,8 @@ pub struct Renderer {
pub device: Device,
pub queue: Queue,
pub shaders: HashMap<String, ShaderModule>,

#[cfg(not(target_os = "macos"))]
pub profiler: GpuProfiler,

pub format: TextureFormat,
Expand Down Expand Up @@ -153,19 +157,33 @@ impl Renderer {
);
let shaders = shaders.await;

let profiler = GpuProfiler::new_with_tracy_client(
GpuProfilerSettings::default(),
#[cfg(not(target_os = "macos"))]
let mut profiler = GpuProfiler::new_with_tracy_client(
GpuProfilerSettings {
// enable_timer_queries: false,
..Default::default()
},
adapter.get_info().backend,
&device,
&queue,
)
.expect("Could not create profiler");

#[cfg(not(target_os = "macos"))]
profiler
.change_settings(GpuProfilerSettings {
enable_timer_queries: false,
..Default::default()
})
.unwrap();

Self {
adapter,
device,
queue,
shaders,

#[cfg(not(target_os = "macos"))]
profiler,

format,
Expand Down Expand Up @@ -226,8 +244,8 @@ impl Renderer {
| Features::VERTEX_WRITABLE_STORAGE
| Features::CLEAR_TEXTURE
| Features::DUAL_SOURCE_BLENDING
| Features::TIMESTAMP_QUERY
| Features::TIMESTAMP_QUERY_INSIDE_ENCODERS
// | Features::TIMESTAMP_QUERY
// | Features::TIMESTAMP_QUERY_INSIDE_ENCODERS
}

#[cfg(not(target_os = "macos"))]
Expand Down Expand Up @@ -356,13 +374,18 @@ impl Renderer {
&scene.resources,
);
}
self.profiler.resolve_queries(&mut encoder);
self.queue.submit(Some(encoder.finish()));

self.profiler.end_frame().unwrap();
#[cfg(not(target_os = "macos"))]
{
self.profiler.resolve_queries(&mut encoder);

self.profiler.end_frame().unwrap();

self.profiler
.process_finished_frame(self.queue.get_timestamp_period());
}

self.profiler
.process_finished_frame(self.queue.get_timestamp_period());
self.queue.submit(Some(encoder.finish()));
}

pub fn draw_mask(
Expand Down Expand Up @@ -508,11 +531,16 @@ impl Renderer {
constants: ShaderConstants,
resources: &Resources,
) {
#[cfg(not(target_os = "macos"))]
let mut content_scope = self
.profiler
.scope("content", context.encoder, &self.device);

#[cfg(target_os = "macos")]
let content_scope = context.encoder;

if *context.first {
#[cfg(not(target_os = "macos"))]
content_scope.scoped_render_pass(
"Clear Offscreen Texture",
&self.device,
Expand All @@ -531,12 +559,33 @@ impl Renderer {
timestamp_writes: None,
},
);

#[cfg(target_os = "macos")]
content_scope.begin_render_pass(&RenderPassDescriptor {
label: Some("Clear Offscreen Texture"),
color_attachments: &[Some(RenderPassColorAttachment {
view: &self.offscreen_texture.view,
resolve_target: None,
ops: Operations {
load: LoadOp::Clear(Color::WHITE),
store: StoreOp::Store,
},
})],
depth_stencil_attachment: None,
occlusion_query_set: None,
timestamp_writes: None,
});
} else {
'offscreen_copy: for batch in contents.primitives.iter() {
for drawable in self.drawables.iter() {
if drawable.has_work(batch) && drawable.requires_offscreen_copy() {
#[cfg(not(target_os = "macos"))]
let mut copy_scope =
content_scope.scope("Copy Frame to Offscreen", &self.device);

#[cfg(target_os = "macos")]
let copy_scope = &mut *content_scope;

copy_scope.copy_texture_to_texture(
ImageCopyTexture {
texture: context.frame,
Expand Down Expand Up @@ -581,6 +630,7 @@ impl Renderer {
}
};

#[cfg(not(target_os = "macos"))]
let mut render_pass = content_scope.scoped_render_pass(
"Layer Render Pass",
&self.device,
Expand All @@ -596,8 +646,26 @@ impl Renderer {
},
);

#[cfg(target_os = "macos")]
let render_pass = content_scope.begin_render_pass(&RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[Some(RenderPassColorAttachment {
view: context.frame_view,
resolve_target: None,
ops: attachment_op,
})],
depth_stencil_attachment: None,
..Default::default()
});

profiling::scope!("drawable", &drawable.name);

#[cfg(not(target_os = "macos"))]
let mut drawable_scope = render_pass.scope(&drawable.name, &self.device);

#[cfg(target_os = "macos")]
let mut drawable_scope = render_pass;

if !drawable.ready() {
continue;
}
Expand Down

0 comments on commit e76adb1

Please sign in to comment.