take resource lookup out of StatelessTracker.add_single

This commit is contained in:
teoxoy 2024-06-20 12:30:21 +02:00 committed by Teodor Tanasoaia
parent 2ec7254772
commit 7bd9195aa2
4 changed files with 89 additions and 72 deletions

View File

@ -410,13 +410,17 @@ impl RenderBundleEncoder {
} => {
let scope = PassErrorScope::SetBindGroup(bind_group_id);
let bind_group = state
let bind_group = bind_group_guard
.get(bind_group_id)
.map_err(|_| RenderCommandError::InvalidBindGroup(bind_group_id))
.map_pass_err(scope)?;
state
.trackers
.bind_groups
.write()
.add_single(&*bind_group_guard, bind_group_id)
.ok_or(RenderCommandError::InvalidBindGroup(bind_group_id))
.map_pass_err(scope)?;
.add_single(bind_group);
self.check_valid_to_use(bind_group.device.info.id())
.map_pass_err(scope)?;
@ -475,13 +479,17 @@ impl RenderBundleEncoder {
RenderCommand::SetPipeline(pipeline_id) => {
let scope = PassErrorScope::SetPipelineRender(pipeline_id);
let pipeline = state
let pipeline = pipeline_guard
.get(pipeline_id)
.map_err(|_| RenderCommandError::InvalidPipeline(pipeline_id))
.map_pass_err(scope)?;
state
.trackers
.render_pipelines
.write()
.add_single(&*pipeline_guard, pipeline_id)
.ok_or(RenderCommandError::InvalidPipeline(pipeline_id))
.map_pass_err(scope)?;
.add_single(pipeline);
self.check_valid_to_use(pipeline.device.info.id())
.map_pass_err(scope)?;

View File

@ -353,10 +353,11 @@ impl Global {
let raw_encoder = encoder.open()?;
let query_set_guard = hub.query_sets.read();
let query_set = tracker
.query_sets
.add_single(&*query_set_guard, query_set_id)
.ok_or(QueryError::InvalidQuerySet(query_set_id))?;
let query_set = query_set_guard
.get(query_set_id)
.map_err(|_| QueryError::InvalidQuerySet(query_set_id))?;
tracker.query_sets.add_single(query_set);
query_set.validate_and_write_timestamp(raw_encoder, query_index, None)?;
@ -397,11 +398,13 @@ impl Global {
if destination_offset % wgt::QUERY_RESOLVE_BUFFER_ALIGNMENT != 0 {
return Err(QueryError::Resolve(ResolveError::BufferOffsetAlignment));
}
let query_set_guard = hub.query_sets.read();
let query_set = tracker
.query_sets
.add_single(&*query_set_guard, query_set_id)
.ok_or(QueryError::InvalidQuerySet(query_set_id))?;
let query_set = query_set_guard
.get(query_set_id)
.map_err(|_| QueryError::InvalidQuerySet(query_set_id))?;
tracker.query_sets.add_single(query_set);
query_set.same_device_as(cmd_buf.as_ref())?;

View File

@ -7,7 +7,6 @@ use crate::{
api_log,
binding_model::BindError,
command::{
self,
bind::Binder,
end_occlusion_query, end_pipeline_statistics_query,
memory_init::{fixup_discarded_surfaces, SurfacesInDiscardState},
@ -903,10 +902,14 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> {
let mut depth_stencil = None;
if let Some(at) = depth_stencil_attachment {
let view: &TextureView<A> = trackers
.views
.add_single(view_guard, at.view)
.ok_or(RenderPassErrorInner::InvalidAttachment(at.view))?;
let view = view_guard
.get(at.view)
.map_err(|_| RenderPassErrorInner::InvalidAttachment(at.view))?;
trackers.views.add_single(view);
let view = view.as_ref();
check_multiview(view)?;
add_view(view, AttachmentErrorLocation::Depth)?;
@ -1036,10 +1039,13 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> {
colors.push(None);
continue;
};
let color_view: &TextureView<A> = trackers
.views
.add_single(view_guard, at.view)
.ok_or(RenderPassErrorInner::InvalidAttachment(at.view))?;
let color_view = view_guard
.get(at.view)
.map_err(|_| RenderPassErrorInner::InvalidAttachment(at.view))?;
trackers.views.add_single(color_view);
check_multiview(color_view)?;
add_view(
color_view,
@ -1070,10 +1076,11 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> {
let mut hal_resolve_target = None;
if let Some(resolve_target) = at.resolve_target {
let resolve_view: &TextureView<A> = trackers
.views
.add_single(view_guard, resolve_target)
.ok_or(RenderPassErrorInner::InvalidAttachment(resolve_target))?;
let resolve_view = view_guard
.get(resolve_target)
.map_err(|_| RenderPassErrorInner::InvalidAttachment(resolve_target))?;
trackers.views.add_single(resolve_view);
check_multiview(resolve_view)?;
@ -1177,10 +1184,11 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> {
};
let timestamp_writes = if let Some(tw) = timestamp_writes {
let query_set = trackers
.query_sets
.add_single(query_set_guard, tw.query_set)
.ok_or(RenderPassErrorInner::InvalidQuerySet(tw.query_set))?;
let query_set = query_set_guard
.get(tw.query_set)
.map_err(|_| RenderPassErrorInner::InvalidQuerySet(tw.query_set))?;
trackers.query_sets.add_single(query_set);
if let Some(index) = tw.beginning_of_pass_write_index {
pending_query_resets.use_query_set(tw.query_set, query_set, index);
@ -1199,10 +1207,11 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> {
};
let occlusion_query_set = if let Some(occlusion_query_set) = occlusion_query_set {
let query_set = trackers
.query_sets
.add_single(query_set_guard, occlusion_query_set)
.ok_or(RenderPassErrorInner::InvalidQuerySet(occlusion_query_set))?;
let query_set = query_set_guard
.get(occlusion_query_set)
.map_err(|_| RenderPassErrorInner::InvalidQuerySet(occlusion_query_set))?;
trackers.query_sets.add_single(query_set);
Some(query_set.raw.as_ref().unwrap())
} else {
@ -1470,12 +1479,13 @@ impl Global {
);
dynamic_offset_count += num_dynamic_offsets;
let bind_group = tracker
.bind_groups
.add_single(&*bind_group_guard, bind_group_id)
.ok_or(RenderCommandError::InvalidBindGroup(bind_group_id))
let bind_group = bind_group_guard
.get(bind_group_id)
.map_err(|_| RenderCommandError::InvalidBindGroup(bind_group_id))
.map_pass_err(scope)?;
tracker.bind_groups.add_single(bind_group);
bind_group
.same_device_as(cmd_buf.as_ref())
.map_pass_err(scope)?;
@ -1538,12 +1548,13 @@ impl Global {
let scope = PassErrorScope::SetPipelineRender(pipeline_id);
state.pipeline = Some(pipeline_id);
let pipeline: &pipeline::RenderPipeline<A> = tracker
.render_pipelines
.add_single(&*render_pipeline_guard, pipeline_id)
.ok_or(RenderCommandError::InvalidPipeline(pipeline_id))
let pipeline = render_pipeline_guard
.get(pipeline_id)
.map_err(|_| RenderCommandError::InvalidPipeline(pipeline_id))
.map_pass_err(scope)?;
tracker.render_pipelines.add_single(pipeline);
pipeline
.same_device_as(cmd_buf.as_ref())
.map_pass_err(scope)?;
@ -2231,12 +2242,13 @@ impl Global {
.require_features(wgt::Features::TIMESTAMP_QUERY_INSIDE_PASSES)
.map_pass_err(scope)?;
let query_set = tracker
.query_sets
.add_single(&*query_set_guard, query_set_id)
.ok_or(RenderCommandError::InvalidQuerySet(query_set_id))
let query_set = query_set_guard
.get(query_set_id)
.map_err(|_| RenderPassErrorInner::InvalidQuerySet(query_set_id))
.map_pass_err(scope)?;
tracker.query_sets.add_single(query_set);
query_set
.validate_and_write_timestamp(
raw,
@ -2253,12 +2265,13 @@ impl Global {
.ok_or(RenderPassErrorInner::MissingOcclusionQuerySet)
.map_pass_err(scope)?;
let query_set = tracker
.query_sets
.add_single(&*query_set_guard, query_set_id)
.ok_or(RenderCommandError::InvalidQuerySet(query_set_id))
let query_set = query_set_guard
.get(query_set_id)
.map_err(|_| RenderPassErrorInner::InvalidQuerySet(query_set_id))
.map_pass_err(scope)?;
tracker.query_sets.add_single(query_set);
validate_and_begin_occlusion_query(
query_set.clone(),
raw,
@ -2281,12 +2294,13 @@ impl Global {
api_log!("RenderPass::begin_pipeline_statistics_query {query_set_id:?} {query_index}");
let scope = PassErrorScope::BeginPipelineStatisticsQuery;
let query_set = tracker
.query_sets
.add_single(&*query_set_guard, query_set_id)
.ok_or(RenderCommandError::InvalidQuerySet(query_set_id))
let query_set = query_set_guard
.get(query_set_id)
.map_err(|_| RenderPassErrorInner::InvalidQuerySet(query_set_id))
.map_pass_err(scope)?;
tracker.query_sets.add_single(query_set);
validate_and_begin_pipeline_statistics_query(
query_set.clone(),
raw,
@ -2306,12 +2320,14 @@ impl Global {
RenderCommand::ExecuteBundle(bundle_id) => {
api_log!("RenderPass::execute_bundle {bundle_id:?}");
let scope = PassErrorScope::ExecuteBundle;
let bundle: &command::RenderBundle<A> = tracker
.bundles
.add_single(&*bundle_guard, bundle_id)
.ok_or(RenderCommandError::InvalidRenderBundle(bundle_id))
let bundle = bundle_guard
.get(bundle_id)
.map_err(|_| RenderCommandError::InvalidRenderBundle(bundle_id))
.map_pass_err(scope)?;
tracker.bundles.add_single(bundle);
bundle
.same_device_as(cmd_buf.as_ref())
.map_pass_err(scope)?;

View File

@ -7,11 +7,9 @@
use std::sync::Arc;
use crate::{
id::Id,
lock::{rank, Mutex},
resource::Resource,
resource_log,
storage::Storage,
track::ResourceMetadata,
};
@ -177,13 +175,7 @@ impl<T: Resource> StatelessTracker<T> {
///
/// If the ID is higher than the length of internal vectors,
/// the vectors will be extended. A call to set_size is not needed.
pub fn add_single<'a>(
&mut self,
storage: &'a Storage<T>,
id: Id<T::Marker>,
) -> Option<&'a Arc<T>> {
let resource = storage.get(id).ok()?;
pub fn add_single(&mut self, resource: &Arc<T>) {
let index = resource.as_info().tracker_index().as_usize();
self.allow_index(index);
@ -193,8 +185,6 @@ impl<T: Resource> StatelessTracker<T> {
unsafe {
self.metadata.insert(index, resource.clone());
}
Some(resource)
}
/// Adds the given resources from the given tracker.