From 7bd9195aa20d5b5b89d4bb5371859172496ce9b3 Mon Sep 17 00:00:00 2001
From: teoxoy <28601907+teoxoy@users.noreply.github.com>
Date: Thu, 20 Jun 2024 12:30:21 +0200
Subject: [PATCH] take resource lookup out of `StatelessTracker.add_single`
---
wgpu-core/src/command/bundle.rs | 24 ++++---
wgpu-core/src/command/query.rs | 19 +++---
wgpu-core/src/command/render.rs | 106 ++++++++++++++++++-------------
wgpu-core/src/track/stateless.rs | 12 +---
4 files changed, 89 insertions(+), 72 deletions(-)
diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs
index 9e4e52fe8..b4fb64c36 100644
--- a/wgpu-core/src/command/bundle.rs
+++ b/wgpu-core/src/command/bundle.rs
@@ -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)?;
diff --git a/wgpu-core/src/command/query.rs b/wgpu-core/src/command/query.rs
index cd543585a..2378f966b 100644
--- a/wgpu-core/src/command/query.rs
+++ b/wgpu-core/src/command/query.rs
@@ -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())?;
diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs
index 1ee58ed99..155d27597 100644
--- a/wgpu-core/src/command/render.rs
+++ b/wgpu-core/src/command/render.rs
@@ -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 = 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 = 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 = 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 = 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 = 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)?;
diff --git a/wgpu-core/src/track/stateless.rs b/wgpu-core/src/track/stateless.rs
index fd4bec911..734f51c01 100644
--- a/wgpu-core/src/track/stateless.rs
+++ b/wgpu-core/src/track/stateless.rs
@@ -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 StatelessTracker {
///
/// 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,
- id: Id,
- ) -> Option<&'a Arc> {
- let resource = storage.get(id).ok()?;
-
+ pub fn add_single(&mut self, resource: &Arc) {
let index = resource.as_info().tracker_index().as_usize();
self.allow_index(index);
@@ -193,8 +185,6 @@ impl StatelessTracker {
unsafe {
self.metadata.insert(index, resource.clone());
}
-
- Some(resource)
}
/// Adds the given resources from the given tracker.