make check_buffer_usage a buffer method

This commit is contained in:
teoxoy 2024-06-19 16:51:03 +02:00 committed by Teodor Tanasoaia
parent 068da49a4c
commit 87382d7133
9 changed files with 58 additions and 78 deletions

View File

@ -8,11 +8,11 @@ use crate::{
hal_api::HalApi,
id::{BindGroupLayoutId, BufferId, SamplerId, TextureViewId},
init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction},
resource::{ParentDevice, Resource, ResourceInfo, ResourceType},
resource::{MissingBufferUsageError, ParentDevice, Resource, ResourceInfo, ResourceType},
resource_log,
snatch::{SnatchGuard, Snatchable},
track::{BindGroupStates, UsageConflict},
validation::{MissingBufferUsageError, MissingTextureUsageError},
validation::MissingTextureUsageError,
Label,
};

View File

@ -101,7 +101,6 @@ use crate::{
resource_log,
snatch::SnatchGuard,
track::RenderBundleScope,
validation::check_buffer_usage,
Label, LabelHelpers,
};
use arrayvec::ArrayVec;
@ -525,8 +524,7 @@ impl RenderBundleEncoder {
.map_pass_err(scope)?;
self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::INDEX)
.map_pass_err(scope)?;
buffer.check_usage(wgt::BufferUsages::INDEX).map_pass_err(scope)?;
let end = match size {
Some(s) => offset + s.get(),
@ -564,8 +562,7 @@ impl RenderBundleEncoder {
.map_pass_err(scope)?;
self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::VERTEX)
.map_pass_err(scope)?;
buffer.check_usage(wgt::BufferUsages::VERTEX).map_pass_err(scope)?;
let end = match size {
Some(s) => offset + s.get(),
@ -691,8 +688,7 @@ impl RenderBundleEncoder {
.map_pass_err(scope)?;
self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::INDIRECT)
.map_pass_err(scope)?;
buffer.check_usage(wgt::BufferUsages::INDIRECT).map_pass_err(scope)?;
buffer_memory_init_actions.extend(buffer.initialization_status.read().create_action(
buffer,
@ -730,8 +726,7 @@ impl RenderBundleEncoder {
.map_pass_err(scope)?;
self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::INDIRECT)
.map_pass_err(scope)?;
buffer.check_usage(wgt::BufferUsages::INDIRECT).map_pass_err(scope)?;
buffer_memory_init_actions.extend(buffer.initialization_status.read().create_action(
buffer,

View File

@ -15,10 +15,9 @@ use crate::{
hal_api::HalApi,
hal_label, id,
init_tracker::MemoryInitKind,
resource::{self, ParentDevice, Resource},
resource::{self, MissingBufferUsageError, ParentDevice, Resource},
snatch::SnatchGuard,
track::{Tracker, TrackerIndex, UsageConflict, UsageScope},
validation::{check_buffer_usage, MissingBufferUsageError},
Label,
};
@ -793,7 +792,8 @@ impl Global {
.buffers
.insert_merge_single(buffer.clone(), hal::BufferUses::INDIRECT)
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::INDIRECT)
buffer
.check_usage(wgt::BufferUsages::INDIRECT)
.map_pass_err(scope)?;
let end_offset = offset + mem::size_of::<wgt::DispatchIndirectArgs>() as u64;

View File

@ -7,9 +7,9 @@ use crate::{
hal_api::HalApi,
id,
pipeline::RenderPipeline,
resource::{Buffer, QuerySet},
resource::{Buffer, MissingBufferUsageError, QuerySet},
track::UsageConflict,
validation::{MissingBufferUsageError, MissingTextureUsageError},
validation::MissingTextureUsageError,
};
use wgt::{BufferAddress, BufferSize, Color, VertexStepMode};

View File

@ -25,12 +25,13 @@ use crate::{
hal_label, id,
init_tracker::{MemoryInitKind, TextureInitRange, TextureInitTrackerAction},
pipeline::{self, PipelineFlags},
resource::{ParentDevice, QuerySet, Texture, TextureView, TextureViewNotRenderableReason},
resource::{
MissingBufferUsageError, ParentDevice, QuerySet, Texture, TextureView,
TextureViewNotRenderableReason,
},
storage::Storage,
track::{TextureSelector, Tracker, UsageConflict, UsageScope},
validation::{
check_buffer_usage, check_texture_usage, MissingBufferUsageError, MissingTextureUsageError,
},
validation::{check_texture_usage, MissingTextureUsageError},
Label,
};
@ -1675,7 +1676,8 @@ impl Global {
.same_device_as(cmd_buf.as_ref())
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, buffer.usage, BufferUsages::INDEX)
buffer
.check_usage(BufferUsages::INDEX)
.map_pass_err(scope)?;
let buf_raw = buffer
.raw
@ -1737,7 +1739,8 @@ impl Global {
.map_pass_err(scope);
}
check_buffer_usage(buffer_id, buffer.usage, BufferUsages::VERTEX)
buffer
.check_usage(BufferUsages::VERTEX)
.map_pass_err(scope)?;
let buf_raw = buffer
.raw
@ -2034,12 +2037,9 @@ impl Global {
.buffers
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
.map_pass_err(scope)?;
check_buffer_usage(
buffer_id,
indirect_buffer.usage,
BufferUsages::INDIRECT,
)
.map_pass_err(scope)?;
indirect_buffer
.check_usage(BufferUsages::INDIRECT)
.map_pass_err(scope)?;
let indirect_raw = indirect_buffer
.raw
.get(&snatch_guard)
@ -2110,12 +2110,9 @@ impl Global {
.buffers
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
.map_pass_err(scope)?;
check_buffer_usage(
buffer_id,
indirect_buffer.usage,
BufferUsages::INDIRECT,
)
.map_pass_err(scope)?;
indirect_buffer
.check_usage(BufferUsages::INDIRECT)
.map_pass_err(scope)?;
let indirect_raw = indirect_buffer
.raw
.get(&snatch_guard)
@ -2131,7 +2128,8 @@ impl Global {
hal::BufferUses::INDIRECT,
)
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, count_buffer.usage, BufferUsages::INDIRECT)
count_buffer
.check_usage(BufferUsages::INDIRECT)
.map_pass_err(scope)?;
let count_raw = count_buffer
.raw

View File

@ -16,7 +16,6 @@ use crate::{
resource::{
self, BufferAccessError, BufferAccessResult, BufferMapOperation, CreateBufferError,
},
validation::check_buffer_usage,
Label, LabelHelpers as _,
};
@ -389,7 +388,7 @@ impl Global {
.buffers
.get(buffer_id)
.map_err(|_| BufferAccessError::Invalid)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::MAP_WRITE)?;
buffer.check_usage(wgt::BufferUsages::MAP_WRITE)?;
//assert!(buffer isn't used by the GPU);
#[cfg(feature = "trace")]
@ -450,7 +449,7 @@ impl Global {
.buffers
.get(buffer_id)
.map_err(|_| BufferAccessError::Invalid)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::MAP_READ)?;
buffer.check_usage(wgt::BufferUsages::MAP_READ)?;
//assert!(buffer isn't used by the GPU);
let raw_buf = buffer

View File

@ -34,9 +34,7 @@ use crate::{
BindGroupStates, TextureSelector, Tracker, TrackerIndexAllocators, UsageScope,
UsageScopePool,
},
validation::{
self, check_buffer_usage, check_texture_usage, validate_color_attachment_bytes_per_sample,
},
validation::{self, check_texture_usage, validate_color_attachment_bytes_per_sample},
FastHashMap, LabelHelpers as _, SubmissionIndex,
};
@ -1932,7 +1930,7 @@ impl<A: HalApi> Device<A> {
buffer.same_device(self)?;
check_buffer_usage(bb.buffer_id, buffer.usage, pub_usage)?;
buffer.check_usage(pub_usage)?;
let raw_buffer = buffer
.raw
.get(snatch_guard)

View File

@ -17,7 +17,6 @@ use crate::{
resource_log,
snatch::{ExclusiveSnatchGuard, SnatchGuard, Snatchable},
track::{SharedTrackerIndexAllocator, TextureSelector, TrackerIndex},
validation::MissingBufferUsageError,
Label, SubmissionIndex,
};
@ -424,6 +423,14 @@ pub enum BufferAccessError {
MapAborted,
}
#[derive(Clone, Debug, Error)]
#[error("Usage flags {actual:?} of {res} do not contain required usage flags {expected:?}")]
pub struct MissingBufferUsageError {
pub(crate) res: ResourceErrorIdent,
pub(crate) actual: wgt::BufferUsages,
pub(crate) expected: wgt::BufferUsages,
}
pub type BufferAccessResult = Result<(), BufferAccessError>;
#[derive(Debug)]
@ -476,6 +483,22 @@ impl<A: HalApi> Buffer<A> {
self.raw.get(guard).is_none()
}
/// Checks that the given buffer usage contains the required buffer usage,
/// returns an error otherwise.
pub(crate) fn check_usage(
self: &Arc<Self>,
expected: wgt::BufferUsages,
) -> Result<(), MissingBufferUsageError> {
self.usage
.contains(expected)
.then_some(())
.ok_or_else(|| MissingBufferUsageError {
res: self.error_ident(),
actual: self.usage,
expected,
})
}
/// Returns the mapping callback in case of error so that the callback can be fired outside
/// of the locks that are held in this function.
pub(crate) fn map_async(
@ -510,8 +533,7 @@ impl<A: HalApi> Buffer<A> {
HostMap::Write => (wgt::BufferUsages::MAP_WRITE, hal::BufferUses::MAP_WRITE),
};
if let Err(e) = crate::validation::check_buffer_usage(self.info.id(), self.usage, pub_usage)
{
if let Err(e) = self.check_usage(pub_usage) {
return Err((op, e.into()));
}

View File

@ -1,8 +1,4 @@
use crate::{
device::bgl,
id::{markers::Buffer, Id},
FastHashMap, FastHashSet,
};
use crate::{device::bgl, FastHashMap, FastHashSet};
use arrayvec::ArrayVec;
use std::{collections::hash_map::Entry, fmt};
use thiserror::Error;
@ -137,34 +133,6 @@ pub struct Interface {
entry_points: FastHashMap<(naga::ShaderStage, String), EntryPoint>,
}
#[derive(Clone, Debug, Error)]
#[error(
"Usage flags {actual:?} for buffer {id:?} do not contain required usage flags {expected:?}"
)]
pub struct MissingBufferUsageError {
pub(crate) id: Id<Buffer>,
pub(crate) actual: wgt::BufferUsages,
pub(crate) expected: wgt::BufferUsages,
}
/// Checks that the given buffer usage contains the required buffer usage,
/// returns an error otherwise.
pub fn check_buffer_usage(
id: Id<Buffer>,
actual: wgt::BufferUsages,
expected: wgt::BufferUsages,
) -> Result<(), MissingBufferUsageError> {
if !actual.contains(expected) {
Err(MissingBufferUsageError {
id,
actual,
expected,
})
} else {
Ok(())
}
}
#[derive(Clone, Debug, Error)]
#[error("Texture usage is {actual:?} which does not contain required usage {expected:?}")]
pub struct MissingTextureUsageError {