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

Add buffer debug labels #528

Merged
merged 3 commits into from
Mar 27, 2020
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
8 changes: 6 additions & 2 deletions examples/compute/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ int main(

WGPUBufferId buffer = wgpu_device_create_buffer_mapped(device,
&(WGPUBufferDescriptor){
.label = "buffer",
.size = size,
.usage = WGPUBufferUsage_STORAGE | WGPUBufferUsage_MAP_READ},
&staging_memory);
Expand All @@ -80,6 +81,7 @@ int main(
WGPUBindGroupLayoutId bind_group_layout =
wgpu_device_create_bind_group_layout(device,
&(WGPUBindGroupLayoutDescriptor){
.label = "bind group layout",
.entries = &(WGPUBindGroupLayoutEntry){
.binding = 0,
.visibility = WGPUShaderStage_COMPUTE,
Expand All @@ -94,7 +96,9 @@ int main(
.offset = 0}}};

WGPUBindGroupId bind_group = wgpu_device_create_bind_group(device,
&(WGPUBindGroupDescriptor){.layout = bind_group_layout,
&(WGPUBindGroupDescriptor){
.label = "bind group",
.layout = bind_group_layout,
.entries = &(WGPUBindGroupEntry){
.binding = 0,
.resource = resource},
Expand Down Expand Up @@ -124,7 +128,7 @@ int main(

WGPUCommandEncoderId encoder = wgpu_device_create_command_encoder(
device, &(WGPUCommandEncoderDescriptor){
.todo = 0
.label = "command encoder",
});

WGPUComputePassId command_pass =
Expand Down
4 changes: 3 additions & 1 deletion examples/triangle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ int main() {
WGPUBindGroupLayoutId bind_group_layout =
wgpu_device_create_bind_group_layout(device,
&(WGPUBindGroupLayoutDescriptor){
.label = "bind group layout",
.entries = NULL,
.entries_length = 0,
});
WGPUBindGroupId bind_group =
wgpu_device_create_bind_group(device,
&(WGPUBindGroupDescriptor){
.label = "bind group",
.layout = bind_group_layout,
.entries = NULL,
.entries_length = 0,
Expand Down Expand Up @@ -237,7 +239,7 @@ int main() {
}

WGPUCommandEncoderId cmd_encoder = wgpu_device_create_command_encoder(
device, &(WGPUCommandEncoderDescriptor){.todo = 0});
device, &(WGPUCommandEncoderDescriptor){.label = "command encoder"});

WGPURenderPassColorAttachmentDescriptor
color_attachments[ATTACHMENTS_LENGTH] = {
Expand Down
6 changes: 5 additions & 1 deletion ffi/wgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ typedef struct {
} WGPUBindGroupEntry;

typedef struct {
const char *label;
WGPUBindGroupLayoutId layout;
const WGPUBindGroupEntry *entries;
uintptr_t entries_length;
Expand All @@ -493,6 +494,7 @@ typedef struct {
} WGPUBindGroupLayoutEntry;

typedef struct {
const char *label;
const WGPUBindGroupLayoutEntry *entries;
uintptr_t entries_length;
} WGPUBindGroupLayoutDescriptor;
Expand All @@ -511,12 +513,13 @@ typedef uint32_t WGPUBufferUsage;
#define WGPUBufferUsage_NONE 0

typedef struct {
const char *label;
WGPUBufferAddress size;
WGPUBufferUsage usage;
} WGPUBufferDescriptor;

typedef struct {
uint32_t todo;
const char *label;
} WGPUCommandEncoderDescriptor;

typedef uint64_t WGPUId_PipelineLayout_Dummy;
Expand Down Expand Up @@ -671,6 +674,7 @@ typedef struct {
} WGPUSwapChainDescriptor;

typedef struct {
const char *label;
WGPUExtent3d size;
uint32_t array_layer_count;
uint32_t mip_level_count;
Expand Down
2 changes: 2 additions & 0 deletions wgpu-core/src/binding_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub struct BindGroupLayoutEntry {
#[repr(C)]
#[derive(Debug)]
pub struct BindGroupLayoutDescriptor {
pub label: *const std::os::raw::c_char,
pub entries: *const BindGroupLayoutEntry,
pub entries_length: usize,
}
Expand Down Expand Up @@ -115,6 +116,7 @@ pub struct BindGroupEntry {
#[repr(C)]
#[derive(Debug)]
pub struct BindGroupDescriptor {
pub label: *const std::os::raw::c_char,
pub layout: BindGroupLayoutId,
pub entries: *const BindGroupEntry,
pub entries_length: usize,
Expand Down
49 changes: 38 additions & 11 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ impl<B: GfxBackend> Device<B> {
};

let mut buffer = unsafe { self.raw.create_buffer(desc.size, usage).unwrap() };
if !desc.label.is_null() {
unsafe {
let label = ffi::CStr::from_ptr(desc.label).to_string_lossy();
self.raw.set_buffer_name(&mut buffer, &label)
};
}
let requirements = unsafe { self.raw.get_buffer_requirements(&buffer) };
let memory = self
.mem_allocator
Expand Down Expand Up @@ -395,16 +401,20 @@ impl<B: GfxBackend> Device<B> {
// TODO: 2D arrays, cubemap arrays

let mut image = unsafe {
self.raw.create_image(
let mut image = self.raw.create_image(
kind,
desc.mip_level_count as hal::image::Level,
format,
hal::image::Tiling::Optimal,
usage,
view_capabilities,
)
}
.unwrap();
).unwrap();
if !desc.label.is_null() {
let label = ffi::CStr::from_ptr(desc.label).to_string_lossy();
self.raw.set_image_name(&mut image, &label);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, that seems suboptimal. So we are getting *const char, then converting to &str to pass to gfx-rs, and then it probably converts it back to *const char at some point later on... Hmm.
At the same time, this is object creation, so it's not a big concern. Just that it would affect the API.
One way to address this (I'm not sure that we should, but still) is to have something like this:

enum Stringy<'a> {
  Rust(&'a str),
  C(&'a CStr),
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep - that's the issue I mentioned in the EDIT above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think gfx-hal should just take CStr and be done with it. It's purpose is to be low level.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

problem is that not all the backends agree on the strings they want. D3D, GL, and Vulkan do agree, but Metal wants NSString anyway, so it doesn't want CStr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can convert a CStr to a &str by finding the length and creating the slice. You can't go in the other direction without allocating or having an intermediate buffer.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a copy and convert step, just like one would do to convert &str to NSString, or &str to CStr. In that sense, CStr doesn't have an advantage.

Isn't there a copy with &str to NSString anyway?

Wont &str -> &CStr -> &str -> [&CStr or NSString] always be more copies than
&str -> [&CStr or NSString].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anybody proposed &str -> &CStr -> &str -> [&CStr or NSString]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&str -> &CStr -> &str -> [&CStr or NSString]

This is what would currently happen with wgpu-rs. The label comes in as a &str from the rust descriptor. Then converted to a *const c_char to use with wgpu-native descriptor. Then converted back to &str to pass to gfx-hal. Then gfx-hal converts it back to CStr to use with native API (or NSString in the mac case).

Apologies, I'm using CStr and *const c_char interchangeably here to indicate a copy to null-terminated buffer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yeah. This isn't intended. As outlined in gfx-rs/wgpu-rs#156 , the main critical paths will be via wgpu-core (which works with &str) or websys, which needs its own strings anyway. Going via wgpu-native C API is kinda silly, only needed for compatibility with C apps and Dawn.

}
image
};
let requirements = unsafe { self.raw.get_image_requirements(&image) };

let memory = self
Expand Down Expand Up @@ -904,10 +914,15 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let (device_guard, mut token) = hub.devices.read(&mut token);
let device = &device_guard[device_id];
let raw = unsafe {
device
let mut raw_layout = device
.raw
.create_descriptor_set_layout(&raw_bindings, &[])
.unwrap()
.unwrap();
if !desc.label.is_null() {
let label = ffi::CStr::from_ptr(desc.label).to_string_lossy();
device.raw.set_descriptor_set_layout_name(&mut raw_layout, &label);
}
raw_layout
};

let layout = binding_model::BindGroupLayout {
Expand Down Expand Up @@ -1006,7 +1021,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
unsafe { slice::from_raw_parts(desc.entries, desc.entries_length as usize) };
assert_eq!(entries.len(), bind_group_layout.entries.len());

let desc_set = unsafe {
let mut desc_set = unsafe {
let mut desc_sets = ArrayVec::<[_; 1]>::new();
device
.desc_allocator
Expand All @@ -1022,6 +1037,13 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
desc_sets.pop().unwrap()
};

if !desc.label.is_null() {
unsafe {
let label = ffi::CStr::from_ptr(desc.label).to_string_lossy();
device.raw.set_descriptor_set_name(desc_set.raw_mut(), &label);
}
}

// fill out the descriptors
let mut used = TrackerSet::new(B::VARIANT);
{
Expand Down Expand Up @@ -1243,7 +1265,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
pub fn device_create_command_encoder<B: GfxBackend>(
&self,
device_id: id::DeviceId,
_desc: &wgt::CommandEncoderDescriptor,
desc: &wgt::CommandEncoderDescriptor,
id_in: Input<G, id::CommandEncoderId>,
) -> id::CommandEncoderId {
let hub = B::hub(self);
Expand All @@ -1261,17 +1283,22 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.lock_life(&mut token)
.lowest_active_submission();

let mut comb = device
let mut command_buffer = device
.com_allocator
.allocate(dev_stored, &device.raw, device.features, lowest_active_index);
unsafe {
comb.raw.last_mut().unwrap().begin_primary(
let raw_command_buffer = command_buffer.raw.last_mut().unwrap();
if !desc.label.is_null() {
let label = ffi::CStr::from_ptr(desc.label).to_string_lossy();
device.raw.set_command_buffer_name(raw_command_buffer, &label);
}
raw_command_buffer.begin_primary(
hal::command::CommandBufferFlags::ONE_TIME_SUBMIT,
);
}

hub.command_buffers
.register_identity(id_in, comb, &mut token)
.register_identity(id_in, command_buffer, &mut token)
}

pub fn command_encoder_destroy<B: GfxBackend>(
Expand Down
18 changes: 14 additions & 4 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::{io, slice};
use std::{io, slice, ptr};
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
#[cfg(feature = "peek-poke")]
Expand Down Expand Up @@ -536,19 +536,28 @@ bitflags::bitflags! {
}

#[repr(C)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct BufferDescriptor {
pub label: *const std::os::raw::c_char,
pub size: BufferAddress,
pub usage: BufferUsage,
}

#[repr(C)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct CommandEncoderDescriptor {
// MSVC doesn't allow zero-sized structs
// We can remove this when we actually have a field
pub todo: u32,
// pub todo: u32,
pub label: *const std::os::raw::c_char,
}

impl Default for CommandEncoderDescriptor {
fn default() -> CommandEncoderDescriptor {
CommandEncoderDescriptor {
label: ptr::null(),
}
}
}

pub type DynamicOffset = u32;
Expand Down Expand Up @@ -734,6 +743,7 @@ pub struct Extent3d {
#[repr(C)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TextureDescriptor {
pub label: *const std::os::raw::c_char,
pub size: Extent3d,
pub array_layer_count: u32,
pub mip_level_count: u32,
Expand Down