Skip to content

Commit

Permalink
Make it possible to filter labels out.
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed Oct 16, 2023
1 parent ff306d2 commit 67177f0
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 33 deletions.
12 changes: 11 additions & 1 deletion wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,18 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Some(&*query_set_guard),
);

let label = if self
.instance
.flags
.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
{
None
} else {
base.label
};

let hal_desc = hal::ComputePassDescriptor {
label: base.label,
label,
timestamp_writes,
};

Expand Down
6 changes: 1 addition & 5 deletions wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,11 +1150,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
multi_ref_count: crate::MultiRefCount::new(),
}
} else {
match device.create_bind_group_layout(
device_id,
desc.label.borrow_option(),
entry_map,
) {
match device.create_bind_group_layout(device_id, &desc.label, entry_map) {
Ok(layout) => layout,
Err(e) => break e,
}
Expand Down
50 changes: 34 additions & 16 deletions wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub struct Device<A: HalApi> {
pub(crate) limits: wgt::Limits,
pub(crate) features: wgt::Features,
pub(crate) downlevel: wgt::DownlevelCapabilities,
pub(crate) instance_flags: wgt::InstanceFlags,
// TODO: move this behind another mutex. This would allow several methods to
// switch to borrow Device immutably, such as `write_buffer`, `write_texture`,
// and `buffer_unmap`.
Expand Down Expand Up @@ -147,6 +148,7 @@ impl<A: HalApi> Device<A> {
downlevel: wgt::DownlevelCapabilities,
desc: &DeviceDescriptor,
trace_path: Option<&std::path::Path>,
instance_flags: wgt::InstanceFlags,
) -> Result<Self, CreateDeviceError> {
#[cfg(not(feature = "trace"))]
if let Some(_) = trace_path {
Expand All @@ -165,9 +167,15 @@ impl<A: HalApi> Device<A> {

// Create zeroed buffer used for texture clears.
let zero_buffer = unsafe {
let label = if instance_flags.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS) {
None
} else {
Some("(wgpu internal) zero init buffer")
};

open.device
.create_buffer(&hal::BufferDescriptor {
label: Some("(wgpu internal) zero init buffer"),
label,
size: ZERO_BUFFER_SIZE,
usage: hal::BufferUses::COPY_SRC | hal::BufferUses::COPY_DST,
memory_flags: hal::MemoryFlags::empty(),
Expand Down Expand Up @@ -227,6 +235,7 @@ impl<A: HalApi> Device<A> {
limits: desc.limits.clone(),
features: desc.features,
downlevel,
instance_flags,
pending_writes,
})
}
Expand Down Expand Up @@ -466,7 +475,7 @@ impl<A: HalApi> Device<A> {
memory_flags.set(hal::MemoryFlags::TRANSIENT, transient);

let hal_desc = hal::BufferDescriptor {
label: desc.label.borrow_option(),
label: desc.label.to_hal(self.instance_flags),
size: aligned_size,
usage,
memory_flags,
Expand Down Expand Up @@ -722,7 +731,7 @@ impl<A: HalApi> Device<A> {
};

let hal_desc = hal::TextureDescriptor {
label: desc.label.borrow_option(),
label: desc.label.to_hal(self.instance_flags),
size: desc.size,
mip_level_count: desc.mip_level_count,
sample_count: desc.sample_count,
Expand Down Expand Up @@ -753,11 +762,20 @@ impl<A: HalApi> Device<A> {
wgt::TextureDimension::D3 => unreachable!(),
};

let clear_label = if self
.instance_flags
.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
{
None
} else {
Some("(wgpu internal) clear texture view")
};

let mut clear_views = SmallVec::new();
for mip_level in 0..desc.mip_level_count {
for array_layer in 0..desc.size.depth_or_array_layers {
let desc = hal::TextureViewDescriptor {
label: Some("(wgpu internal) clear texture view"),
label: clear_label,
format: desc.format,
dimension,
usage,
Expand Down Expand Up @@ -1059,7 +1077,7 @@ impl<A: HalApi> Device<A> {
};

let hal_desc = hal::TextureViewDescriptor {
label: desc.label.borrow_option(),
label: desc.label.to_hal(self.instance_flags),
format,
dimension: resolved_dimension,
usage,
Expand Down Expand Up @@ -1177,7 +1195,7 @@ impl<A: HalApi> Device<A> {
//TODO: check for wgt::DownlevelFlags::COMPARISON_SAMPLERS

let hal_desc = hal::SamplerDescriptor {
label: desc.label.borrow_option(),
label: desc.label.to_hal(self.instance_flags),
address_modes: desc.address_modes,
mag_filter: desc.mag_filter,
min_filter: desc.min_filter,
Expand Down Expand Up @@ -1316,7 +1334,7 @@ impl<A: HalApi> Device<A> {
let hal_shader = hal::ShaderInput::Naga(hal::NagaShader { module, info });

let hal_desc = hal::ShaderModuleDescriptor {
label: desc.label.borrow_option(),
label: desc.label.to_hal(self.instance_flags),
runtime_checks: desc.shader_bound_checks.runtime_checks(),
};
let raw = match unsafe { self.raw.create_shader_module(&hal_desc, hal_shader) } {
Expand Down Expand Up @@ -1355,7 +1373,7 @@ impl<A: HalApi> Device<A> {
) -> Result<pipeline::ShaderModule<A>, pipeline::CreateShaderModuleError> {
self.require_features(wgt::Features::SPIRV_SHADER_PASSTHROUGH)?;
let hal_desc = hal::ShaderModuleDescriptor {
label: desc.label.borrow_option(),
label: desc.label.to_hal(self.instance_flags),
runtime_checks: desc.shader_bound_checks.runtime_checks(),
};
let hal_shader = hal::ShaderInput::SpirV(source);
Expand Down Expand Up @@ -1464,7 +1482,7 @@ impl<A: HalApi> Device<A> {
pub(super) fn create_bind_group_layout(
&self,
self_id: id::DeviceId,
label: Option<&str>,
label: &crate::Label,
entry_map: binding_model::BindEntryMap,
) -> Result<binding_model::BindGroupLayout<A>, binding_model::CreateBindGroupLayoutError> {
#[derive(PartialEq)]
Expand Down Expand Up @@ -1629,7 +1647,7 @@ impl<A: HalApi> Device<A> {
let mut hal_bindings = entry_map.values().cloned().collect::<Vec<_>>();
hal_bindings.sort_by_key(|b| b.binding);
let hal_desc = hal::BindGroupLayoutDescriptor {
label,
label: label.to_hal(self.instance_flags),
flags: bgl_flags,
entries: &hal_bindings,
};
Expand Down Expand Up @@ -1664,7 +1682,7 @@ impl<A: HalApi> Device<A> {
count_validator,
entries: entry_map,
#[cfg(debug_assertions)]
label: label.unwrap_or("").to_string(),
label: label.borrow_or_default().to_string(),
}),
})
}
Expand Down Expand Up @@ -2087,7 +2105,7 @@ impl<A: HalApi> Device<A> {
let layout_inner = layout.assume_deduplicated();

let hal_desc = hal::BindGroupDescriptor {
label: desc.label.borrow_option(),
label: desc.label.to_hal(self.instance_flags),
layout: &layout_inner.raw,
entries: &hal_entries,
buffers: &hal_buffers,
Expand Down Expand Up @@ -2371,7 +2389,7 @@ impl<A: HalApi> Device<A> {
})
.collect::<Vec<_>>();
let hal_desc = hal::PipelineLayoutDescriptor {
label: desc.label.borrow_option(),
label: desc.label.to_hal(self.instance_flags),
flags: hal::PipelineLayoutFlags::BASE_VERTEX_INSTANCE,
bind_group_layouts: &bgl_vec,
push_constant_ranges: desc.push_constant_ranges.as_ref(),
Expand Down Expand Up @@ -2437,7 +2455,7 @@ impl<A: HalApi> Device<A> {
*bgl_id = dedup_id;
}
None => {
let bgl = self.create_bind_group_layout(self_id, None, map)?;
let bgl = self.create_bind_group_layout(self_id, &None, map)?;
bgl_guard.force_replace(*bgl_id, bgl);
}
};
Expand Down Expand Up @@ -2542,7 +2560,7 @@ impl<A: HalApi> Device<A> {
Device::make_late_sized_buffer_groups(&shader_binding_sizes, layout, &*bgl_guard);

let pipeline_desc = hal::ComputePipelineDescriptor {
label: desc.label.borrow_option(),
label: desc.label.to_hal(self.instance_flags),
layout: &layout.raw,
stage: hal::ProgrammableStage {
entry_point: desc.stage.entry_point.as_ref(),
Expand Down Expand Up @@ -3072,7 +3090,7 @@ impl<A: HalApi> Device<A> {
Device::make_late_sized_buffer_groups(&shader_binding_sizes, layout, &*bgl_guard);

let pipeline_desc = hal::RenderPipelineDescriptor {
label: desc.label.borrow_option(),
label: desc.label.to_hal(self.instance_flags),
layout: &layout.raw,
vertex_buffers: &vertex_buffers,
vertex_stage,
Expand Down
31 changes: 21 additions & 10 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct Instance {
pub dx11: Option<HalInstance<hal::api::Dx11>>,
#[cfg(feature = "gles")]
pub gl: Option<HalInstance<hal::api::Gles>>,
pub flags: wgt::InstanceFlags,
}

impl Instance {
Expand Down Expand Up @@ -111,6 +112,7 @@ impl Instance {
dx11: init(hal::api::Dx11, &instance_desc),
#[cfg(feature = "gles")]
gl: init(hal::api::Gles, &instance_desc),
flags: instance_desc.flags,
}
}

Expand Down Expand Up @@ -295,6 +297,7 @@ impl<A: HalApi> Adapter<A> {
self_id: AdapterId,
open: hal::OpenDevice<A>,
desc: &DeviceDescriptor,
instance_flags: wgt::InstanceFlags,
trace_path: Option<&std::path::Path>,
) -> Result<Device<A>, RequestDeviceError> {
log::trace!("Adapter::create_device");
Expand All @@ -310,6 +313,7 @@ impl<A: HalApi> Adapter<A> {
caps.downlevel.clone(),
desc,
trace_path,
instance_flags,
)
.or(Err(RequestDeviceError::OutOfMemory))
}
Expand All @@ -318,6 +322,7 @@ impl<A: HalApi> Adapter<A> {
&self,
self_id: AdapterId,
desc: &DeviceDescriptor,
instance_flags: wgt::InstanceFlags,
trace_path: Option<&std::path::Path>,
) -> Result<Device<A>, RequestDeviceError> {
// Verify all features were exposed by the adapter
Expand Down Expand Up @@ -368,7 +373,7 @@ impl<A: HalApi> Adapter<A> {
},
)?;

self.create_device_from_hal(self_id, open, desc, trace_path)
self.create_device_from_hal(self_id, open, desc, instance_flags, trace_path)
}
}

Expand Down Expand Up @@ -1127,10 +1132,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Ok(adapter) => adapter,
Err(_) => break RequestDeviceError::InvalidAdapter,
};
let device = match adapter.create_device(adapter_id, desc, trace_path) {
Ok(device) => device,
Err(e) => break e,
};
let device =
match adapter.create_device(adapter_id, desc, self.instance.flags, trace_path) {
Ok(device) => device,
Err(e) => break e,
};
let id = fid.assign(device, &mut token);
return (id.0, None);
};
Expand Down Expand Up @@ -1163,11 +1169,16 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Ok(adapter) => adapter,
Err(_) => break RequestDeviceError::InvalidAdapter,
};
let device =
match adapter.create_device_from_hal(adapter_id, hal_device, desc, trace_path) {
Ok(device) => device,
Err(e) => break e,
};
let device = match adapter.create_device_from_hal(
adapter_id,
hal_device,
desc,
self.instance.flags,
trace_path,
) {
Ok(device) => device,
Err(e) => break e,
};
let id = fid.assign(device, &mut token);
return (id.0, None);
};
Expand Down
8 changes: 8 additions & 0 deletions wgpu-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,20 @@ pub type Label<'a> = Option<Cow<'a, str>>;

trait LabelHelpers<'a> {
fn borrow_option(&'a self) -> Option<&'a str>;
fn to_hal(&'a self, flags: wgt::InstanceFlags) -> Option<&'a str>;
fn borrow_or_default(&'a self) -> &'a str;
}
impl<'a> LabelHelpers<'a> for Label<'a> {
fn borrow_option(&'a self) -> Option<&'a str> {
self.as_ref().map(|cow| cow.as_ref())
}
fn to_hal(&'a self, flags: wgt::InstanceFlags) -> Option<&'a str> {
if flags.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS) {
return None;
}

self.as_ref().map(|cow| cow.as_ref())
}
fn borrow_or_default(&'a self) -> &'a str {
self.borrow_option().unwrap_or_default()
}
Expand Down
11 changes: 10 additions & 1 deletion wgpu-core/src/present.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,17 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
)))
} {
Ok(Some(ast)) => {
let label = if self
.instance
.flags
.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
{
None
} else {
Some("(wgpu internal) clear surface texture view")
};
let clear_view_desc = hal::TextureViewDescriptor {
label: Some("(wgpu internal) clear surface texture view"),
label,
format: config.format,
dimension: wgt::TextureViewDimension::D2,
usage: hal::TextureUses::COLOR_TARGET,
Expand Down
2 changes: 2 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,8 @@ bitflags::bitflags! {
const DEBUG = 1 << 0;
/// Enable validation, if possible.
const VALIDATION = 1 << 1;
/// Don't pass labels to wgpu-hal.
const DISCARD_HAL_LABELS = 1 << 2;
}
}

Expand Down

0 comments on commit 67177f0

Please sign in to comment.