From 0392cb783d431e9b2dab0210da314fc91af5e84a Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Tue, 8 Oct 2024 13:07:02 -0700 Subject: [PATCH] [naga spv-out] Rename `make_local` to `LocalType::from_inner`. Change the free function `back::spv::make_local` into an associated function `LocalType::from_inner`. --- naga/src/back/spv/block.rs | 8 +-- naga/src/back/spv/mod.rs | 102 +++++++++++++++++++----------------- naga/src/back/spv/writer.rs | 14 ++--- 3 files changed, 66 insertions(+), 58 deletions(-) diff --git a/naga/src/back/spv/block.rs b/naga/src/back/spv/block.rs index 496ae4fad..4f203ada8 100644 --- a/naga/src/back/spv/block.rs +++ b/naga/src/back/spv/block.rs @@ -3,8 +3,8 @@ Implementations for `BlockContext` methods. */ use super::{ - helpers, index::BoundsCheckResult, make_local, selection::Selection, Block, BlockContext, - Dimension, Error, Instruction, LocalType, LookupType, ResultMember, Writer, WriterFlags, + helpers, index::BoundsCheckResult, selection::Selection, Block, BlockContext, Dimension, Error, + Instruction, LocalType, LookupType, ResultMember, Writer, WriterFlags, }; use crate::{arena::Handle, proc::TypeResolution, Statement}; use spirv::Word; @@ -1809,7 +1809,9 @@ impl<'w> BlockContext<'w> { Some(ty) => ty, None => LookupType::Handle(ty_handle), }, - TypeResolution::Value(ref inner) => LookupType::Local(make_local(inner).unwrap()), + TypeResolution::Value(ref inner) => { + LookupType::Local(LocalType::from_inner(inner).unwrap()) + } }; let result_type_id = self.get_type_id(result_lookup_ty); diff --git a/naga/src/back/spv/mod.rs b/naga/src/back/spv/mod.rs index e6397017c..93e9a466c 100644 --- a/naga/src/back/spv/mod.rs +++ b/naga/src/back/spv/mod.rs @@ -246,9 +246,9 @@ impl LocalImageType { /// never synthesizes new struct types, so `LocalType` has nothing for that. /// /// Each `LocalType` variant should be handled identically to its analogous -/// `TypeInner` variant. You can use the [`make_local`] function to help with -/// this, by converting everything possible to a `LocalType` before inspecting -/// it. +/// `TypeInner` variant. You can use the [`LocalType::from_inner`] function to +/// help with this, by converting everything possible to a `LocalType` before +/// inspecting it. /// /// ## `LocalType` equality and SPIR-V `OpType` uniqueness /// @@ -357,52 +357,56 @@ struct LookupFunctionType { return_type_id: Word, } -fn make_local(inner: &crate::TypeInner) -> Option { - Some(match *inner { - crate::TypeInner::Scalar(scalar) | crate::TypeInner::Atomic(scalar) => LocalType::Value { - vector_size: None, - scalar, - pointer_space: None, - }, - crate::TypeInner::Vector { size, scalar } => LocalType::Value { - vector_size: Some(size), - scalar, - pointer_space: None, - }, - crate::TypeInner::Matrix { - columns, - rows, - scalar, - } => LocalType::Matrix { - columns, - rows, - width: scalar.width, - }, - crate::TypeInner::Pointer { base, space } => LocalType::Pointer { - base, - class: helpers::map_storage_class(space), - }, - crate::TypeInner::ValuePointer { - size, - scalar, - space, - } => LocalType::Value { - vector_size: size, - scalar, - pointer_space: Some(helpers::map_storage_class(space)), - }, - crate::TypeInner::Image { - dim, - arrayed, - class, - } => LocalType::Image(LocalImageType::from_inner(dim, arrayed, class)), - crate::TypeInner::Sampler { comparison: _ } => LocalType::Sampler, - crate::TypeInner::AccelerationStructure => LocalType::AccelerationStructure, - crate::TypeInner::RayQuery => LocalType::RayQuery, - crate::TypeInner::Array { .. } - | crate::TypeInner::Struct { .. } - | crate::TypeInner::BindingArray { .. } => return None, - }) +impl LocalType { + fn from_inner(inner: &crate::TypeInner) -> Option { + Some(match *inner { + crate::TypeInner::Scalar(scalar) | crate::TypeInner::Atomic(scalar) => { + LocalType::Value { + vector_size: None, + scalar, + pointer_space: None, + } + } + crate::TypeInner::Vector { size, scalar } => LocalType::Value { + vector_size: Some(size), + scalar, + pointer_space: None, + }, + crate::TypeInner::Matrix { + columns, + rows, + scalar, + } => LocalType::Matrix { + columns, + rows, + width: scalar.width, + }, + crate::TypeInner::Pointer { base, space } => LocalType::Pointer { + base, + class: helpers::map_storage_class(space), + }, + crate::TypeInner::ValuePointer { + size, + scalar, + space, + } => LocalType::Value { + vector_size: size, + scalar, + pointer_space: Some(helpers::map_storage_class(space)), + }, + crate::TypeInner::Image { + dim, + arrayed, + class, + } => LocalType::Image(LocalImageType::from_inner(dim, arrayed, class)), + crate::TypeInner::Sampler { comparison: _ } => LocalType::Sampler, + crate::TypeInner::AccelerationStructure => LocalType::AccelerationStructure, + crate::TypeInner::RayQuery => LocalType::RayQuery, + crate::TypeInner::Array { .. } + | crate::TypeInner::Struct { .. } + | crate::TypeInner::BindingArray { .. } => return None, + }) + } } #[derive(Debug)] diff --git a/naga/src/back/spv/writer.rs b/naga/src/back/spv/writer.rs index cd02c2219..27f2cbfdb 100644 --- a/naga/src/back/spv/writer.rs +++ b/naga/src/back/spv/writer.rs @@ -1,10 +1,10 @@ use super::{ block::DebugInfoInner, helpers::{contains_builtin, global_needs_wrapper, map_storage_class}, - make_local, Block, BlockContext, CachedConstant, CachedExpressions, DebugInfo, - EntryPointContext, Error, Function, FunctionArgument, GlobalVariable, IdGenerator, Instruction, - LocalType, LocalVariable, LogicalLayout, LookupFunctionType, LookupType, Options, - PhysicalLayout, PipelineOptions, ResultMember, Writer, WriterFlags, BITS_PER_BYTE, + Block, BlockContext, CachedConstant, CachedExpressions, DebugInfo, EntryPointContext, Error, + Function, FunctionArgument, GlobalVariable, IdGenerator, Instruction, LocalType, LocalVariable, + LogicalLayout, LookupFunctionType, LookupType, Options, PhysicalLayout, PipelineOptions, + ResultMember, Writer, WriterFlags, BITS_PER_BYTE, }; use crate::{ arena::{Handle, HandleVec, UniqueArena}, @@ -254,7 +254,9 @@ impl Writer { pub(super) fn get_expression_lookup_type(&mut self, tr: &TypeResolution) -> LookupType { match *tr { TypeResolution::Handle(ty_handle) => LookupType::Handle(ty_handle), - TypeResolution::Value(ref inner) => LookupType::Local(make_local(inner).unwrap()), + TypeResolution::Value(ref inner) => { + LookupType::Local(LocalType::from_inner(inner).unwrap()) + } } } @@ -1025,7 +1027,7 @@ impl Writer { // because some types which map to the same LocalType have different // capability requirements. See https://github.com/gfx-rs/wgpu/issues/5569 self.request_type_capabilities(&ty.inner)?; - let id = if let Some(local) = make_local(&ty.inner) { + let id = if let Some(local) = LocalType::from_inner(&ty.inner) { // This type can be represented as a `LocalType`, so check if we've // already written an instruction for it. If not, do so now, with // `write_type_declaration_local`.