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

Remove more internal uses of IDs #5 #5900

Merged
merged 22 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2823be2
don't clone resources for `ResourceMetadataProvider::Direct`
teoxoy Jul 2, 2024
d92e936
make `Device.create_buffer` return an `Arc<Buffer>`
teoxoy Jul 2, 2024
f2d5d3d
remove duplicate check, it's already present in `Device.create_buffer`
teoxoy Jul 2, 2024
3b34d9a
remove no-op, calling `schedule_resource_destruction` with `last_subm…
teoxoy Jul 2, 2024
b524fea
move buffer creation logic in a new `Device` method
teoxoy Jul 2, 2024
f8e85ba
add `Texture::new` fn
teoxoy Jul 2, 2024
4b356d2
remove duplicate check, it's already present in `Device.create_textur…
teoxoy Jul 2, 2024
af39eec
move code after calls to `FutureId.assign` inside `Device` fns
teoxoy Jul 2, 2024
713bb4c
change `FutureId.assign` to not return a clone of the resource passed…
teoxoy Jul 2, 2024
da0d6d7
move `Device.set_queue` call in `create_device_and_queue_from_hal`
teoxoy Jul 2, 2024
54c1a3b
remove `FutureId.assign_existing`
teoxoy Jul 2, 2024
00c164d
remove `StatelessTracker.add_single`
teoxoy Jul 2, 2024
93e8b1b
simplify `State.set_bind_group`
teoxoy Jul 2, 2024
c809a9b
use `Storage.get_owned` in cases where we used to later clone the res…
teoxoy Jul 2, 2024
2da9cbc
remove unused `std::ops::Index` impl for `Storage`
teoxoy Jul 2, 2024
d74d050
replace `Registry.read().get_owned()` with `Registry.get()`
teoxoy Jul 2, 2024
5c82c76
take guard to render bundles at the top of `resolve_render_command_ids`
teoxoy Jul 2, 2024
d136881
cleanup adapter usages within the `Device`
teoxoy Jul 2, 2024
b5b4a5b
unregister adapters like all other resources
teoxoy Jul 2, 2024
7bb9d54
use a `read` guard instead of `write`
teoxoy Jul 2, 2024
dfce681
use `Surface.get_capabilities` in all relevant places
teoxoy Jul 2, 2024
0e60d02
move command buffer resolving in `Global`'s methods
teoxoy Jul 3, 2024
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
75 changes: 39 additions & 36 deletions wgpu-core/src/command/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ index format changes.
#![allow(clippy::reversed_empty_ranges)]

use crate::{
binding_model::{BindError, BindGroup, BindGroupLayout, PipelineLayout},
binding_model::{BindError, BindGroup, PipelineLayout},
command::{
BasePass, BindGroupStateChange, ColorAttachmentError, DrawError, MapPassErr,
PassErrorScope, RenderCommandError, StateChange,
Expand Down Expand Up @@ -347,7 +347,7 @@ impl RenderBundleEncoder {
desc: &RenderBundleDescriptor,
device: &Arc<Device<A>>,
hub: &Hub<A>,
) -> Result<RenderBundle<A>, RenderBundleError> {
) -> Result<Arc<RenderBundle<A>>, RenderBundleError> {
let scope = PassErrorScope::Bundle;

device.check_is_valid().map_pass_err(scope)?;
Expand Down Expand Up @@ -562,7 +562,7 @@ impl RenderBundleEncoder {
.instance_flags
.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS);

Ok(RenderBundle {
let render_bundle = RenderBundle {
base: BasePass {
label: desc.label.as_ref().map(|cow| cow.to_string()),
commands,
Expand All @@ -572,15 +572,25 @@ impl RenderBundleEncoder {
},
is_depth_read_only: self.is_depth_read_only,
is_stencil_read_only: self.is_stencil_read_only,
device,
device: device.clone(),
used: trackers,
buffer_memory_init_actions,
texture_memory_init_actions,
context: self.context,
label: desc.label.to_string(),
tracking_data: TrackingData::new(tracker_indices),
discard_hal_labels,
})
};

let render_bundle = Arc::new(render_bundle);

device
.trackers
.lock()
.bundles
.insert_single(render_bundle.clone());

Ok(render_bundle)
}

pub fn set_index_buffer(
Expand Down Expand Up @@ -608,11 +618,9 @@ fn set_bind_group<A: HalApi>(
bind_group_id: id::Id<id::markers::BindGroup>,
) -> Result<(), RenderBundleErrorInner> {
let bind_group = bind_group_guard
.get(bind_group_id)
.get_owned(bind_group_id)
.map_err(|_| RenderCommandError::InvalidBindGroupId(bind_group_id))?;

state.trackers.bind_groups.write().add_single(bind_group);

bind_group.same_device(&state.device)?;

let max_bind_groups = state.device.limits.max_bind_groups;
Expand All @@ -638,13 +646,9 @@ fn set_bind_group<A: HalApi>(
.texture_memory_init_actions
.extend_from_slice(&bind_group.used_texture_ranges);

state.set_bind_group(
index,
bind_group_guard.get(bind_group_id).as_ref().unwrap(),
&bind_group.layout,
offsets_range,
);
state.set_bind_group(index, &bind_group, offsets_range);
unsafe { state.trackers.merge_bind_group(&bind_group.used)? };
state.trackers.bind_groups.write().insert_single(bind_group);
// Note: stateless trackers are not merged: the lifetime reference
// is held to the bind group itself.
Ok(())
Expand All @@ -659,11 +663,9 @@ fn set_pipeline<A: HalApi>(
pipeline_id: id::Id<id::markers::RenderPipeline>,
) -> Result<(), RenderBundleErrorInner> {
let pipeline = pipeline_guard
.get(pipeline_id)
.get_owned(pipeline_id)
.map_err(|_| RenderCommandError::InvalidPipelineId(pipeline_id))?;

state.trackers.render_pipelines.write().add_single(pipeline);

pipeline.same_device(&state.device)?;

context
Expand All @@ -677,7 +679,7 @@ fn set_pipeline<A: HalApi>(
return Err(RenderCommandError::IncompatibleStencilAccess(pipeline.error_ident()).into());
}

let pipeline_state = PipelineState::new(pipeline);
let pipeline_state = PipelineState::new(&pipeline);

state
.commands
Expand All @@ -690,6 +692,12 @@ fn set_pipeline<A: HalApi>(

state.invalidate_bind_groups(&pipeline_state, &pipeline.layout);
state.pipeline = Some(pipeline_state);

state
.trackers
.render_pipelines
.write()
.insert_single(pipeline);
Ok(())
}

Expand All @@ -702,14 +710,14 @@ fn set_index_buffer<A: HalApi>(
size: Option<std::num::NonZeroU64>,
) -> Result<(), RenderBundleErrorInner> {
let buffer = buffer_guard
.get(buffer_id)
.get_owned(buffer_id)
.map_err(|_| RenderCommandError::InvalidBufferId(buffer_id))?;

state
.trackers
.buffers
.write()
.merge_single(buffer, hal::BufferUses::INDEX)?;
.merge_single(&buffer, hal::BufferUses::INDEX)?;

buffer.same_device(&state.device)?;
buffer.check_usage(wgt::BufferUsages::INDEX)?;
Expand All @@ -721,11 +729,11 @@ fn set_index_buffer<A: HalApi>(
state
.buffer_memory_init_actions
.extend(buffer.initialization_status.read().create_action(
buffer,
&buffer,
offset..end,
MemoryInitKind::NeedsInitializedMemory,
));
state.set_index_buffer(buffer.clone(), index_format, offset..end);
state.set_index_buffer(buffer, index_format, offset..end);
Ok(())
}

Expand All @@ -747,14 +755,14 @@ fn set_vertex_buffer<A: HalApi>(
}

let buffer = buffer_guard
.get(buffer_id)
.get_owned(buffer_id)
.map_err(|_| RenderCommandError::InvalidBufferId(buffer_id))?;

state
.trackers
.buffers
.write()
.merge_single(buffer, hal::BufferUses::VERTEX)?;
.merge_single(&buffer, hal::BufferUses::VERTEX)?;

buffer.same_device(&state.device)?;
buffer.check_usage(wgt::BufferUsages::VERTEX)?;
Expand All @@ -766,11 +774,11 @@ fn set_vertex_buffer<A: HalApi>(
state
.buffer_memory_init_actions
.extend(buffer.initialization_status.read().create_action(
buffer,
&buffer,
offset..end,
MemoryInitKind::NeedsInitializedMemory,
));
state.vertex[slot as usize] = Some(VertexState::new(buffer.clone(), offset..end));
state.vertex[slot as usize] = Some(VertexState::new(buffer, offset..end));
Ok(())
}

Expand Down Expand Up @@ -889,22 +897,22 @@ fn multi_draw_indirect<A: HalApi>(
let used_bind_groups = pipeline.used_bind_groups;

let buffer = buffer_guard
.get(buffer_id)
.get_owned(buffer_id)
.map_err(|_| RenderCommandError::InvalidBufferId(buffer_id))?;

state
.trackers
.buffers
.write()
.merge_single(buffer, hal::BufferUses::INDIRECT)?;
.merge_single(&buffer, hal::BufferUses::INDIRECT)?;

buffer.same_device(&state.device)?;
buffer.check_usage(wgt::BufferUsages::INDIRECT)?;

state
.buffer_memory_init_actions
.extend(buffer.initialization_status.read().create_action(
buffer,
&buffer,
offset..(offset + mem::size_of::<wgt::DrawIndirectArgs>() as u64),
MemoryInitKind::NeedsInitializedMemory,
));
Expand All @@ -920,7 +928,7 @@ fn multi_draw_indirect<A: HalApi>(
state.flush_vertices();
state.flush_binds(used_bind_groups, dynamic_offsets);
state.commands.push(ArcRenderCommand::MultiDrawIndirect {
buffer: buffer.clone(),
buffer,
offset,
count: None,
indexed,
Expand Down Expand Up @@ -1275,9 +1283,6 @@ struct BindState<A: HalApi> {
/// The id of the bind group set at this index.
bind_group: Arc<BindGroup<A>>,

/// The layout of `group`.
layout: Arc<BindGroupLayout<A>>,

/// The range of dynamic offsets for this bind group, in the original
/// command stream's `BassPass::dynamic_offsets` array.
dynamic_offsets: Range<usize>,
Expand Down Expand Up @@ -1403,7 +1408,6 @@ impl<A: HalApi> State<A> {
&mut self,
slot: u32,
bind_group: &Arc<BindGroup<A>>,
layout: &Arc<BindGroupLayout<A>>,
dynamic_offsets: Range<usize>,
) {
// If this call wouldn't actually change this index's state, we can
Expand All @@ -1420,7 +1424,6 @@ impl<A: HalApi> State<A> {
// Record the index's new state.
self.bind[slot as usize] = Some(BindState {
bind_group: bind_group.clone(),
layout: layout.clone(),
dynamic_offsets,
is_dirty: true,
});
Expand Down Expand Up @@ -1462,7 +1465,7 @@ impl<A: HalApi> State<A> {
} else {
let first_changed = self.bind.iter().zip(&layout.bind_group_layouts).position(
|(entry, layout)| match *entry {
Some(ref contents) => !contents.layout.is_equal(layout),
Some(ref contents) => !contents.bind_group.layout.is_equal(layout),
None => false,
},
);
Expand Down
24 changes: 20 additions & 4 deletions wgpu-core/src/command/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{ops::Range, sync::Arc};
use crate::device::trace::Command as TraceCommand;
use crate::{
api_log,
command::CommandBuffer,
command::CommandEncoderError,
device::DeviceError,
get_lowest_common_denom,
global::Global,
Expand Down Expand Up @@ -76,7 +76,7 @@ whereas subesource range specified start {subresource_base_array_layer} and coun
#[error(transparent)]
Device(#[from] DeviceError),
#[error(transparent)]
CommandEncoderError(#[from] super::CommandEncoderError),
CommandEncoderError(#[from] CommandEncoderError),
}

impl Global {
Expand All @@ -92,7 +92,15 @@ impl Global {

let hub = A::hub(self);

let cmd_buf = CommandBuffer::get_encoder(hub, command_encoder_id)?;
let cmd_buf = match hub
.command_buffers
.get(command_encoder_id.into_command_buffer_id())
{
Ok(cmd_buf) => cmd_buf,
Err(_) => return Err(CommandEncoderError::Invalid.into()),
};
cmd_buf.check_recording()?;

let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();

Expand Down Expand Up @@ -176,7 +184,15 @@ impl Global {

let hub = A::hub(self);

let cmd_buf = CommandBuffer::get_encoder(hub, command_encoder_id)?;
let cmd_buf = match hub
.command_buffers
.get(command_encoder_id.into_command_buffer_id())
{
Ok(cmd_buf) => cmd_buf,
Err(_) => return Err(CommandEncoderError::Invalid.into()),
};
cmd_buf.check_recording()?;

let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();

Expand Down
63 changes: 36 additions & 27 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,35 +301,40 @@ impl Global {
timestamp_writes: None, // Handle only once we resolved the encoder.
};

match CommandBuffer::lock_encoder(hub, encoder_id) {
Ok(cmd_buf) => {
arc_desc.timestamp_writes = if let Some(tw) = desc.timestamp_writes {
let Ok(query_set) = hub.query_sets.get(tw.query_set) else {
return (
ComputePass::new(None, arc_desc),
Some(CommandEncoderError::InvalidTimestampWritesQuerySetId(
tw.query_set,
)),
);
};
let make_err = |e, arc_desc| (ComputePass::new(None, arc_desc), Some(e));

if let Err(e) = query_set.same_device_as(cmd_buf.as_ref()) {
return (ComputePass::new(None, arc_desc), Some(e.into()));
}
let cmd_buf = match hub.command_buffers.get(encoder_id.into_command_buffer_id()) {
Ok(cmd_buf) => cmd_buf,
Err(_) => return make_err(CommandEncoderError::Invalid, arc_desc),
};

Some(ArcPassTimestampWrites {
query_set,
beginning_of_pass_write_index: tw.beginning_of_pass_write_index,
end_of_pass_write_index: tw.end_of_pass_write_index,
})
} else {
None
};

(ComputePass::new(Some(cmd_buf), arc_desc), None)
match cmd_buf.lock_encoder() {
Ok(_) => {}
Err(e) => return make_err(e, arc_desc),
};

arc_desc.timestamp_writes = if let Some(tw) = desc.timestamp_writes {
let Ok(query_set) = hub.query_sets.get(tw.query_set) else {
return make_err(
CommandEncoderError::InvalidTimestampWritesQuerySetId(tw.query_set),
arc_desc,
);
};

if let Err(e) = query_set.same_device_as(cmd_buf.as_ref()) {
return make_err(e.into(), arc_desc);
}
Err(err) => (ComputePass::new(None, arc_desc), Some(err)),
}

Some(ArcPassTimestampWrites {
query_set,
beginning_of_pass_write_index: tw.beginning_of_pass_write_index,
end_of_pass_write_index: tw.end_of_pass_write_index,
})
} else {
None
};

(ComputePass::new(Some(cmd_buf), arc_desc), None)
}

/// Creates a type erased compute pass.
Expand Down Expand Up @@ -378,7 +383,11 @@ impl Global {
let hub = A::hub(self);
let scope = PassErrorScope::Pass;

let cmd_buf = CommandBuffer::get_encoder(hub, encoder_id).map_pass_err(scope)?;
let cmd_buf = match hub.command_buffers.get(encoder_id.into_command_buffer_id()) {
Ok(cmd_buf) => cmd_buf,
Err(_) => return Err(CommandEncoderError::Invalid).map_pass_err(scope),
};
cmd_buf.check_recording().map_pass_err(scope)?;

#[cfg(feature = "trace")]
{
Expand Down
Loading
Loading