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

Improved timings #185

Merged
merged 2 commits into from
Oct 5, 2024
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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ winit = { workspace = true }
blade-render = { version = "0.3", path = "blade-render" }

[dev-dependencies]
egui_plot = "0.28"
blade-macros = { version = "0.3", path = "blade-macros" }
bytemuck = { workspace = true }
choir = { workspace = true }
Expand Down
36 changes: 21 additions & 15 deletions blade-graphics/src/gles/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,6 @@
}
}

pub fn start(&mut self) {
self.commands.clear();
self.plain_data.clear();
self.string_data.clear();
self.has_present = false;
}

pub(super) fn finish(&mut self, gl: &glow::Context) {
use glow::HasContext as _;
#[allow(trivial_casts)]
Expand Down Expand Up @@ -152,19 +145,13 @@
);
}
let time = Duration::from_nanos(result - prev);
self.timings.push((pass_name, time));
*self.timings.entry(pass_name).or_default() += time;
prev = result
}
}
}
}

pub fn init_texture(&mut self, _texture: super::Texture) {}

pub fn present(&mut self, _frame: super::Frame) {
self.has_present = true;
}

pub fn transfer(&mut self, label: &str) -> super::PassEncoder<()> {
self.begin_pass(label);
self.pass(super::PassKind::Transfer)
Expand Down Expand Up @@ -260,8 +247,27 @@
pass.invalidate_attachments = invalidate_attachments;
pass
}
}

#[hidden_trait::expose]
impl crate::traits::CommandEncoder for super::CommandEncoder {
type Texture = super::Texture;
type Frame = super::Frame;

fn start(&mut self) {
self.commands.clear();
self.plain_data.clear();
self.string_data.clear();
self.has_present = false;
}

fn init_texture(&mut self, _texture: super::Texture) {}

fn present(&mut self, _frame: super::Frame) {
self.has_present = true;
}

pub fn timings(&self) -> &[(String, Duration)] {
fn timings(&self) -> &crate::Timings {
&self.timings
}
}
Expand Down Expand Up @@ -704,7 +710,7 @@
} => {
let format_desc = super::describe_texture_format(dst.format);
let block_info = dst.format.block_info();
let row_texels =

Check warning on line 713 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unused variable: `row_texels`

Check warning on line 713 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `row_texels`
bytes_per_row / block_info.size as u32 * block_info.dimensions.0 as u32;
gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1);
gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(src.raw));
Expand Down Expand Up @@ -778,10 +784,10 @@
gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, None);
}
Self::CopyTextureToBuffer {
ref src,

Check warning on line 787 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unused variable: `src`

Check warning on line 787 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `src`
ref dst,

Check warning on line 788 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unused variable: `dst`

Check warning on line 788 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `dst`
bytes_per_row,

Check warning on line 789 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unused variable: `bytes_per_row`

Check warning on line 789 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `bytes_per_row`
ref size,

Check warning on line 790 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unused variable: `size`

Check warning on line 790 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `size`
} => unimplemented!(),
Self::ResetFramebuffer => {
for &attachment in COLOR_ATTACHMENTS.iter() {
Expand Down Expand Up @@ -928,10 +934,10 @@
gl.scissor(rect.x, rect.y, rect.w as i32, rect.h as i32);
}
Self::SetStencilFunc {
face,

Check warning on line 937 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unused variable: `face`

Check warning on line 937 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `face`
function,

Check warning on line 938 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unused variable: `function`

Check warning on line 938 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `function`
reference,

Check warning on line 939 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unused variable: `reference`

Check warning on line 939 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `reference`
read_mask,

Check warning on line 940 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

unused variable: `read_mask`

Check warning on line 940 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `read_mask`
} => unimplemented!(),
Self::SetStencilOps {
face,
Expand Down
7 changes: 4 additions & 3 deletions blade-graphics/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod pipeline;
mod platform;
mod resource;

use std::{marker::PhantomData, mem, ops::Range, time::Duration};
use std::{marker::PhantomData, mem, ops::Range};

type BindTarget = u32;
const DEBUG_ID: u32 = 0;
Expand Down Expand Up @@ -135,6 +135,7 @@ pub struct RenderPipeline {
topology: crate::PrimitiveTopology,
}

#[derive(Debug)]
pub struct Frame {
texture: Texture,
}
Expand Down Expand Up @@ -370,7 +371,7 @@ pub struct CommandEncoder {
has_present: bool,
limits: Limits,
timing_datas: Option<Box<[TimingData]>>,
timings: Vec<(String, Duration)>,
timings: crate::Timings,
}

enum PassKind {
Expand Down Expand Up @@ -477,7 +478,7 @@ impl crate::traits::CommandDevice for Context {
has_present: false,
limits: self.limits.clone(),
timing_datas,
timings: Vec::new(),
timings: Default::default(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,3 +1110,5 @@ pub struct ScissorRect {
pub w: u32,
pub h: u32,
}

pub type Timings = std::collections::HashMap<String, std::time::Duration>;
74 changes: 40 additions & 34 deletions blade-graphics/src/metal/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,39 +116,6 @@ impl super::CommandEncoder {
self.raw.take().unwrap()
}

pub fn start(&mut self) {
if let Some(ref mut td_array) = self.timing_datas {
self.timings.clear();
td_array.rotate_left(1);
let td = td_array.first_mut().unwrap();
if !td.pass_names.is_empty() {
let counters = td
.sample_buffer
.resolve_counter_range(metal::NSRange::new(0, td.pass_names.len() as u64 * 2));
for (name, chunk) in td.pass_names.drain(..).zip(counters.chunks(2)) {
let duration = Duration::from_nanos(chunk[1] - chunk[0]);
self.timings.push((name, duration));
}
}
}

let queue = self.queue.lock().unwrap();
self.raw = Some(objc::rc::autoreleasepool(|| {
let cmd_buf = queue.new_command_buffer_with_unretained_references();
if !self.name.is_empty() {
cmd_buf.set_label(&self.name);
}
cmd_buf.to_owned()
}));
self.has_open_debug_group = false;
}

pub fn init_texture(&mut self, _texture: super::Texture) {}

pub fn present(&mut self, frame: super::Frame) {
self.raw.as_mut().unwrap().present_drawable(&frame.drawable);
}

pub fn transfer(&mut self, label: &str) -> super::TransferCommandEncoder {
self.begin_pass(label);
let raw = objc::rc::autoreleasepool(|| {
Expand Down Expand Up @@ -313,8 +280,47 @@ impl super::CommandEncoder {
phantom: PhantomData,
}
}
}

#[hidden_trait::expose]
impl crate::traits::CommandEncoder for super::CommandEncoder {
type Texture = super::Texture;
type Frame = super::Frame;

fn start(&mut self) {
if let Some(ref mut td_array) = self.timing_datas {
self.timings.clear();
td_array.rotate_left(1);
let td = td_array.first_mut().unwrap();
if !td.pass_names.is_empty() {
let counters = td
.sample_buffer
.resolve_counter_range(metal::NSRange::new(0, td.pass_names.len() as u64 * 2));
for (name, chunk) in td.pass_names.drain(..).zip(counters.chunks(2)) {
let duration = Duration::from_nanos(chunk[1] - chunk[0]);
*self.timings.entry(name).or_default() += duration;
}
}
}

let queue = self.queue.lock().unwrap();
self.raw = Some(objc::rc::autoreleasepool(|| {
let cmd_buf = queue.new_command_buffer_with_unretained_references();
if !self.name.is_empty() {
cmd_buf.set_label(&self.name);
}
cmd_buf.to_owned()
}));
self.has_open_debug_group = false;
}

fn init_texture(&mut self, _texture: super::Texture) {}

fn present(&mut self, frame: super::Frame) {
self.raw.as_mut().unwrap().present_drawable(&frame.drawable);
}

pub fn timings(&self) -> &[(String, Duration)] {
fn timings(&self) -> &crate::Timings {
&self.timings
}
}
Expand Down
5 changes: 3 additions & 2 deletions blade-graphics/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct Surface {
unsafe impl Send for Surface {}
unsafe impl Sync for Surface {}

#[derive(Debug)]
pub struct Frame {
drawable: metal::MetalDrawable,
texture: metal::Texture,
Expand Down Expand Up @@ -199,7 +200,7 @@ pub struct CommandEncoder {
enable_dispatch_type: bool,
has_open_debug_group: bool,
timing_datas: Option<Box<[TimingData]>>,
timings: Vec<(String, time::Duration)>,
timings: crate::Timings,
}

#[derive(Debug)]
Expand Down Expand Up @@ -568,7 +569,7 @@ impl crate::traits::CommandDevice for Context {
enable_dispatch_type: self.info.enable_dispatch_type,
has_open_debug_group: false,
timing_datas,
timings: Vec::new(),
timings: Default::default(),
}
}

Expand Down
9 changes: 9 additions & 0 deletions blade-graphics/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ pub trait CommandDevice {
fn wait_for(&self, sp: &Self::SyncPoint, timeout_ms: u32) -> bool;
}

pub trait CommandEncoder {
type Texture: Send + Sync + Clone + Copy + Debug;
type Frame: Send + Sync + Debug;
fn start(&mut self);
fn init_texture(&mut self, texture: Self::Texture);
fn present(&mut self, frame: Self::Frame);
fn timings(&self) -> &super::Timings;
}

pub trait TransferEncoder {
type BufferPiece: Send + Sync + Clone + Copy + Debug;
type TexturePiece: Send + Sync + Clone + Copy + Debug;
Expand Down
Loading
Loading