From 0152fd0803446612ee7de627e3adfe3a3a649df1 Mon Sep 17 00:00:00 2001 From: VitalyR Date: Mon, 24 Jul 2023 19:11:31 +0800 Subject: [PATCH] update naga to 0.13.0 --- Cargo.toml | 4 +- src/derive.rs | 101 ++++++++++++++++++++++++++++++++++++++++-------- src/redirect.rs | 1 + 3 files changed, 87 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5741a96..f406263 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ prune = [] override_any = [] [dependencies] -naga = { version="0.12", features = ["wgsl-in", "wgsl-out", "glsl-in", "glsl-out", "clone", "span"] } +naga = { version="0.13", features = ["wgsl-in", "wgsl-out", "glsl-in", "glsl-out", "clone", "span"] } tracing = "0.1" regex = "1.5" regex-syntax = "0.6" @@ -29,6 +29,6 @@ once_cell = "1.17.0" indexmap = "1.9.3" [dev-dependencies] -wgpu = { version = "0.16", features=["naga"] } +wgpu = { version = "0.17", features=["naga"] } futures-lite = "1" tracing-subscriber = { version = "0.3", features = ["std", "fmt"] } diff --git a/src/derive.rs b/src/derive.rs index 12a1dae..7b5682a 100644 --- a/src/derive.rs +++ b/src/derive.rs @@ -1,7 +1,7 @@ use indexmap::IndexMap; use naga::{ - Arena, ArraySize, Block, Constant, ConstantInner, EntryPoint, Expression, Function, - FunctionArgument, FunctionResult, GlobalVariable, Handle, ImageQuery, LocalVariable, Module, + Arena, ArraySize, Block, Constant, EntryPoint, Expression, Function, FunctionArgument, + FunctionResult, GlobalVariable, Handle, ImageQuery, LocalVariable, Module, Override, SampleLevel, Span, Statement, StructMember, SwitchCase, Type, TypeInner, UniqueArena, }; use std::collections::HashMap; @@ -13,11 +13,13 @@ pub struct DerivedModule<'a> { type_map: HashMap, Handle>, const_map: HashMap, Handle>, + const_expression_map: HashMap, Handle>, global_map: HashMap, Handle>, function_map: HashMap>, types: UniqueArena, constants: Arena, + const_expressions: Arena, globals: Arena, functions: Arena, } @@ -104,7 +106,7 @@ impl<'a> DerivedModule<'a> { } TypeInner::Array { base, size, stride } => { let size = match size { - ArraySize::Constant(c) => ArraySize::Constant(self.import_const(c)), + c @ ArraySize::Constant(_) => *c, ArraySize::Dynamic => ArraySize::Dynamic, }; TypeInner::Array { @@ -115,7 +117,7 @@ impl<'a> DerivedModule<'a> { } TypeInner::BindingArray { base, size } => { let size = match size { - ArraySize::Constant(c) => ArraySize::Constant(self.import_const(c)), + c @ ArraySize::Constant(_) => *c, ArraySize::Dynamic => ArraySize::Dynamic, }; TypeInner::BindingArray { @@ -145,16 +147,14 @@ impl<'a> DerivedModule<'a> { let new_const = Constant { name: c.name.clone(), - specialization: c.specialization, - inner: match &c.inner { - ConstantInner::Scalar { .. } => c.inner.clone(), - ConstantInner::Composite { ty, components } => { - let components = components.iter().map(|c| self.import_const(c)).collect(); - ConstantInner::Composite { - ty: self.import_type(ty), - components, - } - } + r#override: c.r#override.clone(), + ty: self.import_type(&c.ty), + //TODO: infinite recursion? + init: if c.r#override == Override::None { + self.import_const_expression(&c.init) + } else { + // r#override doesn't do anything now + unreachable!() }, }; @@ -183,7 +183,7 @@ impl<'a> DerivedModule<'a> { space: gv.space, binding: gv.binding.clone(), ty: self.import_type(&gv.ty), - init: gv.init.map(|c| self.import_const(&c)), + init: gv.init.map(|c| self.import_const_expression(&c)), }; let span = self @@ -199,6 +199,63 @@ impl<'a> DerivedModule<'a> { new_h }) } + // remap a const expression from source context into our derived context + pub fn import_const_expression( + &mut self, + const_expression_handle: &Handle, + ) -> Handle { + self.const_expression_map + .get(const_expression_handle) + .copied() + .unwrap_or_else(|| { + let old_c = self + .shader + .as_ref() + .unwrap() + .const_expressions + .try_get(*const_expression_handle) + .unwrap(); + + let new_const = match old_c { + Expression::Constant(h_c) => { + let c = self + .shader + .as_ref() + .unwrap() + .constants + .try_get(*h_c) + .unwrap(); + + Constant { + name: c.name.clone(), + r#override: c.r#override.clone(), + ty: self.import_type(&c.ty), + init: self.import_const_expression(&c.init), + } + } + // TODO + // What should I do? + _ => todo!(), + }; + + let span = self + .shader + .as_ref() + .unwrap() + .const_expressions + .get_span(*const_expression_handle); + let new_constant_handle = self + .constants + .fetch_or_append(new_const, self.map_span(span)); + let new_const_expression = naga::Expression::Constant(new_constant_handle); + let new_const_expression_handle = self + .const_expressions + .append(new_const_expression, self.map_span(span)); + self.const_expression_map + .insert(*const_expression_handle, new_const_expression_handle); + new_const_expression_handle + }) + } // remap a block fn import_block( @@ -325,6 +382,12 @@ impl<'a> DerivedModule<'a> { value: map_expr!(value), result: map_expr!(result), }, + Statement::WorkGroupUniformLoad { pointer, result } => { + Statement::WorkGroupUniformLoad { + pointer: map_expr!(pointer), + result: map_expr!(result), + } + } Statement::Return { value } => Statement::Return { value: map_expr_opt!(value), }, @@ -406,6 +469,8 @@ impl<'a> DerivedModule<'a> { let mut is_external = false; let expr = old_expressions.try_get(h_expr).unwrap(); let expr = match expr { + Expression::Literal(_) => expr.clone(), + Expression::ZeroValue(zv) => Expression::ZeroValue(self.import_type(zv)), Expression::CallResult(f) => Expression::CallResult(self.map_function_handle(f)), Expression::Constant(c) => { is_external = true; @@ -434,7 +499,7 @@ impl<'a> DerivedModule<'a> { gather: *gather, coordinate: map_expr!(coordinate), array_index: map_expr_opt!(array_index), - offset: offset.map(|c| self.import_const(&c)), + offset: offset.map(|c| self.import_const_expression(&c)), level: match level { SampleLevel::Auto | SampleLevel::Zero => *level, SampleLevel::Exact(expr) => SampleLevel::Exact(map_expr!(expr)), @@ -549,6 +614,7 @@ impl<'a> DerivedModule<'a> { } Expression::AtomicResult { .. } => expr.clone(), + Expression::WorkGroupUniformLoadResult { .. } => expr.clone(), Expression::RayQueryProceedResult => expr.clone(), Expression::RayQueryGetIntersection { query, committed } => { Expression::RayQueryGetIntersection { @@ -591,7 +657,7 @@ impl<'a> DerivedModule<'a> { let new_local = LocalVariable { name: l.name.clone(), ty: self.import_type(&l.ty), - init: l.init.map(|c| self.import_const(&c)), + init: l.init.map(|c| self.import_const_expression(&c)), }; let span = func.local_variables.get_span(h_l); let new_h = local_variables.append(new_local, self.map_span(span)); @@ -688,6 +754,7 @@ impl<'a> From> for naga::Module { types: derived.types, constants: derived.constants, global_variables: derived.globals, + const_expressions: derived.const_expressions, functions: derived.functions, special_types: Default::default(), entry_points: Default::default(), diff --git a/src/redirect.rs b/src/redirect.rs index 32e6c5f..eca25bd 100644 --- a/src/redirect.rs +++ b/src/redirect.rs @@ -62,6 +62,7 @@ impl Redirector { | Statement::Break | Statement::Continue | Statement::Return { .. } + | Statement::WorkGroupUniformLoad { .. } | Statement::Kill | Statement::Barrier(_) | Statement::Store { .. }