Expose all items in naga::back.

This helps out-of-tree backends.

Fixes https://github.com/gfx-rs/wgpu/issues/5398.
This commit is contained in:
Christian Legnitto 2024-03-15 15:23:18 -04:00 committed by Teodor Tanasoaia
parent 1ead28701d
commit 1fd47b54ab

View File

@ -16,14 +16,19 @@ pub mod spv;
#[cfg(feature = "wgsl-out")] #[cfg(feature = "wgsl-out")]
pub mod wgsl; pub mod wgsl;
const COMPONENTS: &[char] = &['x', 'y', 'z', 'w']; /// Names of vector components.
const INDENT: &str = " "; pub const COMPONENTS: &[char] = &['x', 'y', 'z', 'w'];
const BAKE_PREFIX: &str = "_e"; /// Indent for backends.
pub const INDENT: &str = " ";
/// Prefix used for baking.
pub const BAKE_PREFIX: &str = "_e";
type NeedBakeExpressions = crate::FastHashSet<crate::Handle<crate::Expression>>; /// Expressions that need baking.
pub type NeedBakeExpressions = crate::FastHashSet<crate::Handle<crate::Expression>>;
/// Indentation level.
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
struct Level(usize); pub struct Level(pub usize);
impl Level { impl Level {
const fn next(&self) -> Self { const fn next(&self) -> Self {
@ -52,7 +57,7 @@ impl std::fmt::Display for Level {
/// [`EntryPoint`]: crate::EntryPoint /// [`EntryPoint`]: crate::EntryPoint
/// [`Module`]: crate::Module /// [`Module`]: crate::Module
/// [`Module::entry_points`]: crate::Module::entry_points /// [`Module::entry_points`]: crate::Module::entry_points
enum FunctionType { pub enum FunctionType {
/// A regular function. /// A regular function.
Function(crate::Handle<crate::Function>), Function(crate::Handle<crate::Function>),
/// An [`EntryPoint`], and its index in [`Module::entry_points`]. /// An [`EntryPoint`], and its index in [`Module::entry_points`].
@ -63,7 +68,8 @@ enum FunctionType {
} }
impl FunctionType { impl FunctionType {
fn is_compute_entry_point(&self, module: &crate::Module) -> bool { /// Returns true if the function is an entry point for a compute shader.
pub fn is_compute_entry_point(&self, module: &crate::Module) -> bool {
match *self { match *self {
FunctionType::EntryPoint(index) => { FunctionType::EntryPoint(index) => {
module.entry_points[index as usize].stage == crate::ShaderStage::Compute module.entry_points[index as usize].stage == crate::ShaderStage::Compute
@ -74,19 +80,20 @@ impl FunctionType {
} }
/// Helper structure that stores data needed when writing the function /// Helper structure that stores data needed when writing the function
struct FunctionCtx<'a> { pub struct FunctionCtx<'a> {
/// The current function being written /// The current function being written
ty: FunctionType, pub ty: FunctionType,
/// Analysis about the function /// Analysis about the function
info: &'a crate::valid::FunctionInfo, pub info: &'a crate::valid::FunctionInfo,
/// The expression arena of the current function being written /// The expression arena of the current function being written
expressions: &'a crate::Arena<crate::Expression>, pub expressions: &'a crate::Arena<crate::Expression>,
/// Map of expressions that have associated variable names /// Map of expressions that have associated variable names
named_expressions: &'a crate::NamedExpressions, pub named_expressions: &'a crate::NamedExpressions,
} }
impl FunctionCtx<'_> { impl FunctionCtx<'_> {
fn resolve_type<'a>( /// Helper method that resolves a type of a given expression.
pub fn resolve_type<'a>(
&'a self, &'a self,
handle: crate::Handle<crate::Expression>, handle: crate::Handle<crate::Expression>,
types: &'a crate::UniqueArena<crate::Type>, types: &'a crate::UniqueArena<crate::Type>,
@ -95,7 +102,10 @@ impl FunctionCtx<'_> {
} }
/// Helper method that generates a [`NameKey`](crate::proc::NameKey) for a local in the current function /// Helper method that generates a [`NameKey`](crate::proc::NameKey) for a local in the current function
const fn name_key(&self, local: crate::Handle<crate::LocalVariable>) -> crate::proc::NameKey { pub const fn name_key(
&self,
local: crate::Handle<crate::LocalVariable>,
) -> crate::proc::NameKey {
match self.ty { match self.ty {
FunctionType::Function(handle) => crate::proc::NameKey::FunctionLocal(handle, local), FunctionType::Function(handle) => crate::proc::NameKey::FunctionLocal(handle, local),
FunctionType::EntryPoint(idx) => crate::proc::NameKey::EntryPointLocal(idx, local), FunctionType::EntryPoint(idx) => crate::proc::NameKey::EntryPointLocal(idx, local),
@ -106,7 +116,7 @@ impl FunctionCtx<'_> {
/// ///
/// # Panics /// # Panics
/// - If the function arguments are less or equal to `arg` /// - If the function arguments are less or equal to `arg`
const fn argument_key(&self, arg: u32) -> crate::proc::NameKey { pub const fn argument_key(&self, arg: u32) -> crate::proc::NameKey {
match self.ty { match self.ty {
FunctionType::Function(handle) => crate::proc::NameKey::FunctionArgument(handle, arg), FunctionType::Function(handle) => crate::proc::NameKey::FunctionArgument(handle, arg),
FunctionType::EntryPoint(ep_index) => { FunctionType::EntryPoint(ep_index) => {
@ -115,8 +125,8 @@ impl FunctionCtx<'_> {
} }
} }
// Returns true if the given expression points to a fixed-function pipeline input. /// Returns true if the given expression points to a fixed-function pipeline input.
fn is_fixed_function_input( pub fn is_fixed_function_input(
&self, &self,
mut expression: crate::Handle<crate::Expression>, mut expression: crate::Handle<crate::Expression>,
module: &crate::Module, module: &crate::Module,
@ -162,7 +172,7 @@ impl crate::Expression {
/// See the [module-level documentation][emit] for details. /// See the [module-level documentation][emit] for details.
/// ///
/// [emit]: index.html#expression-evaluation-time /// [emit]: index.html#expression-evaluation-time
const fn bake_ref_count(&self) -> usize { pub const fn bake_ref_count(&self) -> usize {
match *self { match *self {
// accesses are never cached, only loads are // accesses are never cached, only loads are
crate::Expression::Access { .. } | crate::Expression::AccessIndex { .. } => usize::MAX, crate::Expression::Access { .. } | crate::Expression::AccessIndex { .. } => usize::MAX,
@ -181,9 +191,7 @@ impl crate::Expression {
} }
/// Helper function that returns the string corresponding to the [`BinaryOperator`](crate::BinaryOperator) /// Helper function that returns the string corresponding to the [`BinaryOperator`](crate::BinaryOperator)
/// # Notes pub const fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
/// Used by `glsl-out`, `msl-out`, `wgsl-out`, `hlsl-out`.
const fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
use crate::BinaryOperator as Bo; use crate::BinaryOperator as Bo;
match op { match op {
Bo::Add => "+", Bo::Add => "+",
@ -208,8 +216,6 @@ const fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
} }
/// Helper function that returns the string corresponding to the [`VectorSize`](crate::VectorSize) /// Helper function that returns the string corresponding to the [`VectorSize`](crate::VectorSize)
/// # Notes
/// Used by `msl-out`, `wgsl-out`, `hlsl-out`.
const fn vector_size_str(size: crate::VectorSize) -> &'static str { const fn vector_size_str(size: crate::VectorSize) -> &'static str {
match size { match size {
crate::VectorSize::Bi => "2", crate::VectorSize::Bi => "2",
@ -219,7 +225,8 @@ const fn vector_size_str(size: crate::VectorSize) -> &'static str {
} }
impl crate::TypeInner { impl crate::TypeInner {
const fn is_handle(&self) -> bool { /// Returns true if this is a handle to a type rather than the type directly.
pub const fn is_handle(&self) -> bool {
match *self { match *self {
crate::TypeInner::Image { .. } | crate::TypeInner::Sampler { .. } => true, crate::TypeInner::Image { .. } | crate::TypeInner::Sampler { .. } => true,
_ => false, _ => false,
@ -266,8 +273,9 @@ bitflags::bitflags! {
} }
} }
/// The intersection test to use for ray queries.
#[repr(u32)] #[repr(u32)]
enum RayIntersectionType { pub enum RayIntersectionType {
Triangle = 1, Triangle = 1,
BoundingBox = 4, BoundingBox = 4,
} }