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

Pass names, scopes, and timings #179

Merged
merged 9 commits into from
Sep 28, 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
2 changes: 1 addition & 1 deletion blade-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl GuiPainter {
copies.push((src, dst, extent));
}

if let mut transfer = command_encoder.transfer() {
if let mut transfer = command_encoder.transfer("update egui textures") {
for (src, dst, extent) in copies {
transfer.copy_buffer_to_texture(src, 4 * extent.width, dst, extent);
}
Expand Down
6 changes: 5 additions & 1 deletion blade-graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ raw-window-handle = "0.6"
[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
block = "0.1"
core-graphics-types = "0.1"
metal = "0.29"
#TODO: switch to crates once https://github.com/gfx-rs/metal-rs/pull/335 is published
#TODO: switch to upstream once these are merged:
# - https://github.com/gfx-rs/metal-rs/pull/336
# - https://github.com/gfx-rs/metal-rs/pull/337
metal = { git = "https://github.com/kvark/metal-rs", branch = "blade" }
objc = "0.2.5"
naga = { workspace = true, features = ["msl-out"] }

Expand Down
2 changes: 1 addition & 1 deletion blade-graphics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ All of these required extensions are supported in software by the driver on any

GLES is also supported at a basic level. It's enabled for `wasm32-unknown-unknown` target, and can also be force-enabled on native:
```bash
RUSTFLAGS="--cfg gles" CARGO_TARGET_DIR=./target-gl cargo test
RUSTFLAGS="--cfg gles" CARGO_TARGET_DIR=./target-gl cargo run --example bunnymark
```

This path can be activated on all platforms via Angle library.
Expand Down
165 changes: 108 additions & 57 deletions blade-graphics/src/gles/command.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{str, time::Duration};

const COLOR_ATTACHMENTS: &[u32] = &[
glow::COLOR_ATTACHMENT0,
glow::COLOR_ATTACHMENT1,
Expand Down Expand Up @@ -79,48 +81,111 @@
}

impl super::CommandEncoder {
fn begin_pass(&mut self, label: &str) {
if self.needs_scopes {
let start = self.string_data.len();
self.string_data.extend_from_slice(label.as_bytes());
self.commands.push(super::Command::PushScope {
name_range: start..self.string_data.len(),
});
}
if let Some(ref mut timing_datas) = self.timing_datas {
let td = timing_datas.first_mut().unwrap();
let id = td.pass_names.len();
self.commands.push(super::Command::QueryCounter {
query: td.queries[id],
});
td.pass_names.push(label.to_string());
}
}

fn pass<P>(&mut self, kind: super::PassKind) -> super::PassEncoder<P> {
super::PassEncoder {
commands: &mut self.commands,
plain_data: &mut self.plain_data,
kind,
invalidate_attachments: Vec::new(),
pipeline: Default::default(),
limits: &self.limits,
has_scope: self.needs_scopes,
}
}

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)]
if let Some(ref mut timing_datas) = self.timing_datas {
{
let td = timing_datas.first_mut().unwrap();
let id = td.pass_names.len();
self.commands.push(super::Command::QueryCounter {
query: td.queries[id],
});
}

timing_datas.rotate_left(1);
self.timings.clear();
let td = timing_datas.first_mut().unwrap();
if !td.pass_names.is_empty() {
let mut prev = 0;
unsafe {
gl.get_query_parameter_u64_with_offset(
td.queries[0],
glow::QUERY_RESULT,
&mut prev as *mut _ as usize,
);
}
for (pass_name, &query) in td.pass_names.drain(..).zip(td.queries[1..].iter()) {
let mut result: u64 = 0;
unsafe {
gl.get_query_parameter_u64_with_offset(
query,
glow::QUERY_RESULT,
&mut result as *mut _ as usize,
);
}
let time = Duration::from_nanos(result - prev);
self.timings.push((pass_name, 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) -> super::PassEncoder<()> {
super::PassEncoder {
commands: &mut self.commands,
plain_data: &mut self.plain_data,
kind: super::PassKind::Transfer,
invalidate_attachments: Vec::new(),
pipeline: Default::default(),
limits: &self.limits,
}
pub fn transfer(&mut self, label: &str) -> super::PassEncoder<()> {
self.begin_pass(label);
self.pass(super::PassKind::Transfer)
}

pub fn acceleration_structure(&mut self) -> super::PassEncoder<()> {
pub fn acceleration_structure(&mut self, _label: &str) -> super::PassEncoder<()> {
unimplemented!()
}

pub fn compute(&mut self) -> super::PassEncoder<super::ComputePipeline> {
super::PassEncoder {
commands: &mut self.commands,
plain_data: &mut self.plain_data,
kind: super::PassKind::Compute,
invalidate_attachments: Vec::new(),
pipeline: Default::default(),
limits: &self.limits,
}
pub fn compute(&mut self, label: &str) -> super::PassEncoder<super::ComputePipeline> {
self.begin_pass(label);
self.pass(super::PassKind::Compute)
}

pub fn render(
&mut self,
label: &str,
targets: crate::RenderTargetSet,
) -> super::PassEncoder<super::RenderPipeline> {
self.begin_pass(label);

let mut target_size = [0u16; 2];
let mut invalidate_attachments = Vec::new();
for (i, rt) in targets.colors.iter().enumerate() {
Expand Down Expand Up @@ -191,14 +256,13 @@
}
}

super::PassEncoder {
commands: &mut self.commands,
plain_data: &mut self.plain_data,
kind: super::PassKind::Render,
invalidate_attachments,
pipeline: Default::default(),
limits: &self.limits,
}
let mut pass = self.pass(super::PassKind::Render);
pass.invalidate_attachments = invalidate_attachments;
pass
}

pub fn timings(&self) -> &[(String, Duration)] {
&self.timings
}
}

Expand Down Expand Up @@ -266,6 +330,9 @@
self.commands.push(super::Command::ResetFramebuffer);
}
}
if self.has_scope {
self.commands.push(super::Command::PopScope);
}
}
}

Expand Down Expand Up @@ -338,33 +405,6 @@
}
}

#[hidden_trait::expose]
impl crate::traits::AccelerationStructureEncoder for super::PassEncoder<'_, ()> {
type AccelerationStructure = crate::AccelerationStructure;
type AccelerationStructureMesh = crate::AccelerationStructureMesh;
type BufferPiece = crate::BufferPiece;

fn build_bottom_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_meshes: &[crate::AccelerationStructureMesh],
_scratch_data: crate::BufferPiece,
) {
unimplemented!()
}

fn build_top_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_bottom_level: &[super::AccelerationStructure],
_instance_count: u32,
_instance_data: crate::BufferPiece,
_scratch_data: crate::BufferPiece,
) {
unimplemented!()
}
}

#[hidden_trait::expose]
impl crate::traits::PipelineEncoder for super::PipelineEncoder<'_> {
fn bind<D: crate::ShaderData>(&mut self, group: u32, data: &D) {
Expand Down Expand Up @@ -393,6 +433,7 @@
}

fn bind_vertex(&mut self, index: u32, vertex_buf: crate::BufferPiece) {
assert_eq!(index, 0);
self.commands.push(super::Command::BindVertex {
buffer: vertex_buf.buffer.raw,
});
Expand Down Expand Up @@ -605,9 +646,9 @@
gl.dispatch_compute_indirect(indirect_buf.offset as i32);
}
Self::FillBuffer {
ref dst,
size,
value,
dst: ref _dst,
size: _size,
value: _value,
} => unimplemented!(),
Self::CopyBufferToBuffer {
ref src,
Expand Down Expand Up @@ -663,7 +704,7 @@
} => {
let format_desc = super::describe_texture_format(dst.format);
let block_info = dst.format.block_info();
let row_texels =

Check warning on line 707 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 707 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 @@ -737,10 +778,10 @@
gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, None);
}
Self::CopyTextureToBuffer {
ref src,

Check warning on line 781 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 781 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 782 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 782 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 783 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 783 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 784 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 784 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 @@ -887,10 +928,10 @@
gl.scissor(rect.x, rect.y, rect.w as i32, rect.h as i32);
}
Self::SetStencilFunc {
face,

Check warning on line 931 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 931 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 932 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 932 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 933 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 933 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 934 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 934 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 Expand Up @@ -981,6 +1022,16 @@
gl.bind_sampler(slot, None);
}
}
Self::QueryCounter { query } => {
gl.query_counter(query, glow::TIMESTAMP);
}
Self::PushScope { ref name_range } => {
let name = str::from_utf8(&ec.string_data[name_range.clone()]).unwrap();
gl.push_debug_group(glow::DEBUG_SOURCE_APPLICATION, super::DEBUG_ID, name);
}
Self::PopScope => {
gl.pop_debug_group();
}
}
}
}
Expand Down
Loading
Loading