From 862f19524faa30b50c4d81f8ff32c08d2ceedaa9 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Wed, 19 Jun 2024 16:59:30 +0200 Subject: [PATCH] make `check_texture_usage` a texture method --- wgpu-core/src/binding_model.rs | 6 ++++-- wgpu-core/src/command/draw.rs | 3 +-- wgpu-core/src/command/render.rs | 7 +++---- wgpu-core/src/device/resource.rs | 4 ++-- wgpu-core/src/resource.rs | 27 +++++++++++++++++++++++++++ wgpu-core/src/validation.rs | 20 -------------------- 6 files changed, 37 insertions(+), 30 deletions(-) diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index d8e262a5b..dd0fe723d 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -8,11 +8,13 @@ use crate::{ hal_api::HalApi, id::{BindGroupLayoutId, BufferId, SamplerId, TextureViewId}, init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction}, - resource::{MissingBufferUsageError, ParentDevice, Resource, ResourceInfo, ResourceType}, + resource::{ + MissingBufferUsageError, MissingTextureUsageError, ParentDevice, Resource, ResourceInfo, + ResourceType, + }, resource_log, snatch::{SnatchGuard, Snatchable}, track::{BindGroupStates, UsageConflict}, - validation::MissingTextureUsageError, Label, }; diff --git a/wgpu-core/src/command/draw.rs b/wgpu-core/src/command/draw.rs index fb36108f2..af1cad642 100644 --- a/wgpu-core/src/command/draw.rs +++ b/wgpu-core/src/command/draw.rs @@ -7,9 +7,8 @@ use crate::{ hal_api::HalApi, id, pipeline::RenderPipeline, - resource::{Buffer, MissingBufferUsageError, QuerySet}, + resource::{Buffer, MissingBufferUsageError, MissingTextureUsageError, QuerySet}, track::UsageConflict, - validation::MissingTextureUsageError, }; use wgt::{BufferAddress, BufferSize, Color, VertexStepMode}; diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 890fee527..f061ab966 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -26,12 +26,11 @@ use crate::{ init_tracker::{MemoryInitKind, TextureInitRange, TextureInitTrackerAction}, pipeline::{self, PipelineFlags}, resource::{ - MissingBufferUsageError, ParentDevice, QuerySet, Texture, TextureView, - TextureViewNotRenderableReason, + MissingBufferUsageError, MissingTextureUsageError, ParentDevice, QuerySet, Texture, + TextureView, TextureViewNotRenderableReason, }, storage::Storage, track::{TextureSelector, Tracker, UsageConflict, UsageScope}, - validation::{check_texture_usage, MissingTextureUsageError}, Label, }; @@ -1248,7 +1247,7 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> { for ra in self.render_attachments { let texture = &ra.texture; - check_texture_usage(texture.desc.usage, TextureUsages::RENDER_ATTACHMENT)?; + texture.check_usage(TextureUsages::RENDER_ATTACHMENT)?; // the tracker set of the pass is always in "extend" mode unsafe { diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 211929933..c00964a24 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -34,7 +34,7 @@ use crate::{ BindGroupStates, TextureSelector, Tracker, TrackerIndexAllocators, UsageScope, UsageScopePool, }, - validation::{self, check_texture_usage, validate_color_attachment_bytes_per_sample}, + validation::{self, validate_color_attachment_bytes_per_sample}, FastHashMap, LabelHelpers as _, SubmissionIndex, }; @@ -2059,7 +2059,7 @@ impl Device { texture.same_device_as(view)?; - check_texture_usage(texture.desc.usage, pub_usage)?; + texture.check_usage(pub_usage)?; used_texture_ranges.push(TextureInitTrackerAction { texture: texture.clone(), diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index c6f04af28..f5f4ee4a6 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -431,6 +431,14 @@ pub struct MissingBufferUsageError { pub(crate) expected: wgt::BufferUsages, } +#[derive(Clone, Debug, Error)] +#[error("Usage flags {actual:?} of {res} do not contain required usage flags {expected:?}")] +pub struct MissingTextureUsageError { + pub(crate) res: ResourceErrorIdent, + pub(crate) actual: wgt::TextureUsages, + pub(crate) expected: wgt::TextureUsages, +} + pub type BufferAccessResult = Result<(), BufferAccessError>; #[derive(Debug)] @@ -967,6 +975,25 @@ pub struct Texture { pub(crate) bind_groups: Mutex>>>, } +impl Texture { + /// Checks that the given texture usage contains the required texture usage, + /// returns an error otherwise. + pub(crate) fn check_usage( + &self, + expected: wgt::TextureUsages, + ) -> Result<(), MissingTextureUsageError> { + self.desc + .usage + .contains(expected) + .then_some(()) + .ok_or_else(|| MissingTextureUsageError { + res: self.error_ident(), + actual: self.desc.usage, + expected, + }) + } +} + impl Drop for Texture { fn drop(&mut self) { resource_log!("Destroy raw Texture {:?}", self.info.label()); diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 6490cdac2..bb02f279a 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -133,26 +133,6 @@ pub struct Interface { entry_points: FastHashMap<(naga::ShaderStage, String), EntryPoint>, } -#[derive(Clone, Debug, Error)] -#[error("Texture usage is {actual:?} which does not contain required usage {expected:?}")] -pub struct MissingTextureUsageError { - pub(crate) actual: wgt::TextureUsages, - pub(crate) expected: wgt::TextureUsages, -} - -/// Checks that the given texture usage contains the required texture usage, -/// returns an error otherwise. -pub fn check_texture_usage( - actual: wgt::TextureUsages, - expected: wgt::TextureUsages, -) -> Result<(), MissingTextureUsageError> { - if !actual.contains(expected) { - Err(MissingTextureUsageError { actual, expected }) - } else { - Ok(()) - } -} - #[derive(Clone, Debug, Error)] #[non_exhaustive] pub enum BindingError {