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, hal_api::HalApi,
id::{BindGroupLayoutId, BufferId, SamplerId, TextureViewId}, id::{BindGroupLayoutId, BufferId, SamplerId, TextureViewId},
init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction}, init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction},
resource::{ParentDevice, Resource, ResourceInfo, ResourceType}, resource::{MissingBufferUsageError, ParentDevice, Resource, ResourceInfo, ResourceType},
resource_log, resource_log,
snatch::{SnatchGuard, Snatchable}, snatch::{SnatchGuard, Snatchable},
track::{BindGroupStates, UsageConflict}, track::{BindGroupStates, UsageConflict},
validation::{MissingBufferUsageError, MissingTextureUsageError}, validation::MissingTextureUsageError,
Label, Label,
}; };

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -17,7 +17,6 @@ use crate::{
resource_log, resource_log,
snatch::{ExclusiveSnatchGuard, SnatchGuard, Snatchable}, snatch::{ExclusiveSnatchGuard, SnatchGuard, Snatchable},
track::{SharedTrackerIndexAllocator, TextureSelector, TrackerIndex}, track::{SharedTrackerIndexAllocator, TextureSelector, TrackerIndex},
validation::MissingBufferUsageError,
Label, SubmissionIndex, Label, SubmissionIndex,
}; };
@ -424,6 +423,14 @@ pub enum BufferAccessError {
MapAborted, 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>; pub type BufferAccessResult = Result<(), BufferAccessError>;
#[derive(Debug)] #[derive(Debug)]
@ -476,6 +483,22 @@ impl<A: HalApi> Buffer<A> {
self.raw.get(guard).is_none() 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 /// 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. /// of the locks that are held in this function.
pub(crate) fn map_async( pub(crate) fn map_async(
@ -510,8 +533,7 @@ impl<A: HalApi> Buffer<A> {
HostMap::Write => (wgt::BufferUsages::MAP_WRITE, hal::BufferUses::MAP_WRITE), 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())); return Err((op, e.into()));
} }

View File

@ -1,8 +1,4 @@
use crate::{ use crate::{device::bgl, FastHashMap, FastHashSet};
device::bgl,
id::{markers::Buffer, Id},
FastHashMap, FastHashSet,
};
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use std::{collections::hash_map::Entry, fmt}; use std::{collections::hash_map::Entry, fmt};
use thiserror::Error; use thiserror::Error;
@ -137,34 +133,6 @@ pub struct Interface {
entry_points: FastHashMap<(naga::ShaderStage, String), EntryPoint>, 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)] #[derive(Clone, Debug, Error)]
#[error("Texture usage is {actual:?} which does not contain required usage {expected:?}")] #[error("Texture usage is {actual:?} which does not contain required usage {expected:?}")]
pub struct MissingTextureUsageError { pub struct MissingTextureUsageError {