mirror of
https://github.com/storytold/wgpu.git
synced 2026-12-01 10:34:57 +01:00
[wgsl-in] eagerly evaluate const-expressions
[wgsl-in] support const-expressions in attributes allow `Splat` as an evaluated const-expression type
This commit is contained in:
1 parent
c5d884fca8
commit
a730236b68
44 files changed
+2917
-2770
No files matched your search
+28
-19
@@ -1709,7 +1709,7 @@ impl<'a, W: Write> Writer<'a, W> {
|
||||
arg: Handle<crate::Expression>,
|
||||
arg1: Handle<crate::Expression>,
|
||||
size: usize,
|
||||
ctx: &back::FunctionCtx<'_>,
|
||||
ctx: &back::FunctionCtx,
|
||||
) -> BackendResult {
|
||||
// Write parantheses around the dot product expression to prevent operators
|
||||
// with different precedences from applying earlier.
|
||||
@@ -2254,9 +2254,12 @@ impl<'a, W: Write> Writer<'a, W> {
|
||||
/// [`Expression`]: crate::Expression
|
||||
/// [`Module`]: crate::Module
|
||||
fn write_const_expr(&mut self, expr: Handle<crate::Expression>) -> BackendResult {
|
||||
self.write_possibly_const_expr(expr, &self.module.const_expressions, |writer, expr| {
|
||||
writer.write_const_expr(expr)
|
||||
})
|
||||
self.write_possibly_const_expr(
|
||||
expr,
|
||||
&self.module.const_expressions,
|
||||
|expr| &self.info[expr],
|
||||
|writer, expr| writer.write_const_expr(expr),
|
||||
)
|
||||
}
|
||||
|
||||
/// Write [`Expression`] variants that can occur in both runtime and const expressions.
|
||||
@@ -2277,13 +2280,15 @@ impl<'a, W: Write> Writer<'a, W> {
|
||||
/// Adds no newlines or leading/trailing whitespace
|
||||
///
|
||||
/// [`Expression`]: crate::Expression
|
||||
fn write_possibly_const_expr<E>(
|
||||
&mut self,
|
||||
fn write_possibly_const_expr<'w, I, E>(
|
||||
&'w mut self,
|
||||
expr: Handle<crate::Expression>,
|
||||
expressions: &crate::Arena<crate::Expression>,
|
||||
info: I,
|
||||
write_expression: E,
|
||||
) -> BackendResult
|
||||
where
|
||||
I: Fn(Handle<crate::Expression>) -> &'w proc::TypeResolution,
|
||||
E: Fn(&mut Self, Handle<crate::Expression>) -> BackendResult,
|
||||
{
|
||||
use crate::Expression;
|
||||
@@ -2331,6 +2336,14 @@ impl<'a, W: Write> Writer<'a, W> {
|
||||
}
|
||||
write!(self.out, ")")?
|
||||
}
|
||||
// `Splat` needs to actually write down a vector, it's not always inferred in GLSL.
|
||||
Expression::Splat { size: _, value } => {
|
||||
let resolved = info(expr).inner_with(&self.module.types);
|
||||
self.write_value_type(resolved)?;
|
||||
write!(self.out, "(")?;
|
||||
write_expression(self, value)?;
|
||||
write!(self.out, ")")?
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
@@ -2344,7 +2357,7 @@ impl<'a, W: Write> Writer<'a, W> {
|
||||
fn write_expr(
|
||||
&mut self,
|
||||
expr: Handle<crate::Expression>,
|
||||
ctx: &back::FunctionCtx<'_>,
|
||||
ctx: &back::FunctionCtx,
|
||||
) -> BackendResult {
|
||||
use crate::Expression;
|
||||
|
||||
@@ -2357,10 +2370,14 @@ impl<'a, W: Write> Writer<'a, W> {
|
||||
Expression::Literal(_)
|
||||
| Expression::Constant(_)
|
||||
| Expression::ZeroValue(_)
|
||||
| Expression::Compose { .. } => {
|
||||
self.write_possibly_const_expr(expr, ctx.expressions, |writer, expr| {
|
||||
writer.write_expr(expr, ctx)
|
||||
})?;
|
||||
| Expression::Compose { .. }
|
||||
| Expression::Splat { .. } => {
|
||||
self.write_possibly_const_expr(
|
||||
expr,
|
||||
ctx.expressions,
|
||||
|expr| &ctx.info[expr].ty,
|
||||
|writer, expr| writer.write_expr(expr, ctx),
|
||||
)?;
|
||||
}
|
||||
// `Access` is applied to arrays, vectors and matrices and is written as indexing
|
||||
Expression::Access { base, index } => {
|
||||
@@ -2407,14 +2424,6 @@ impl<'a, W: Write> Writer<'a, W> {
|
||||
ref other => return Err(Error::Custom(format!("Cannot index {other:?}"))),
|
||||
}
|
||||
}
|
||||
// `Splat` needs to actually write down a vector, it's not always inferred in GLSL.
|
||||
Expression::Splat { size: _, value } => {
|
||||
let resolved = ctx.info[expr].ty.inner_with(&self.module.types);
|
||||
self.write_value_type(resolved)?;
|
||||
write!(self.out, "(")?;
|
||||
self.write_expr(value, ctx)?;
|
||||
write!(self.out, ")")?
|
||||
}
|
||||
// `Swizzle` adds a few letters behind the dot.
|
||||
Expression::Swizzle {
|
||||
size,
|
||||
|
||||
+17
-14
@@ -2078,6 +2078,19 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
}
|
||||
write!(self.out, ")")?;
|
||||
}
|
||||
Expression::Splat { size, value } => {
|
||||
// hlsl is not supported one value constructor
|
||||
// if we write, for example, int4(0), dxc returns error:
|
||||
// error: too few elements in vector initialization (expected 4 elements, have 1)
|
||||
let number_of_components = match size {
|
||||
crate::VectorSize::Bi => "xx",
|
||||
crate::VectorSize::Tri => "xxx",
|
||||
crate::VectorSize::Quad => "xxxx",
|
||||
};
|
||||
write!(self.out, "(")?;
|
||||
write_expression(self, value)?;
|
||||
write!(self.out, ").{number_of_components}")?
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
@@ -2135,7 +2148,8 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
Expression::Literal(_)
|
||||
| Expression::Constant(_)
|
||||
| Expression::ZeroValue(_)
|
||||
| Expression::Compose { .. } => {
|
||||
| Expression::Compose { .. }
|
||||
| Expression::Splat { .. } => {
|
||||
self.write_possibly_const_expression(
|
||||
module,
|
||||
expr,
|
||||
@@ -2423,7 +2437,9 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
|
||||
if let Some(offset) = offset {
|
||||
write!(self.out, ", ")?;
|
||||
write!(self.out, "int2(")?; // work around https://github.com/microsoft/DirectXShaderCompiler/issues/5082#issuecomment-1540147807
|
||||
self.write_const_expression(module, offset)?;
|
||||
write!(self.out, ")")?;
|
||||
}
|
||||
|
||||
write!(self.out, ")")?;
|
||||
@@ -3154,19 +3170,6 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
self.write_expr(module, argument, func_ctx)?;
|
||||
write!(self.out, ")")?
|
||||
}
|
||||
Expression::Splat { size, value } => {
|
||||
// hlsl is not supported one value constructor
|
||||
// if we write, for example, int4(0), dxc returns error:
|
||||
// error: too few elements in vector initialization (expected 4 elements, have 1)
|
||||
let number_of_components = match size {
|
||||
crate::VectorSize::Bi => "xx",
|
||||
crate::VectorSize::Tri => "xxx",
|
||||
crate::VectorSize::Quad => "xxxx",
|
||||
};
|
||||
write!(self.out, "(")?;
|
||||
self.write_expr(module, value, func_ctx)?;
|
||||
write!(self.out, ").{number_of_components}")?
|
||||
}
|
||||
Expression::Select {
|
||||
condition,
|
||||
accept,
|
||||
|
||||
+56
-34
@@ -501,6 +501,7 @@ struct ExpressionContext<'a> {
|
||||
origin: FunctionOrigin,
|
||||
info: &'a valid::FunctionInfo,
|
||||
module: &'a crate::Module,
|
||||
mod_info: &'a valid::ModuleInfo,
|
||||
pipeline_options: &'a PipelineOptions,
|
||||
policies: index::BoundsCheckPolicies,
|
||||
|
||||
@@ -571,7 +572,6 @@ impl<'a> ExpressionContext<'a> {
|
||||
|
||||
struct StatementContext<'a> {
|
||||
expression: ExpressionContext<'a>,
|
||||
mod_info: &'a valid::ModuleInfo,
|
||||
result_struct: Option<&'a str>,
|
||||
}
|
||||
|
||||
@@ -604,25 +604,26 @@ impl<W: Write> Writer<W> {
|
||||
parameters: impl Iterator<Item = Handle<crate::Expression>>,
|
||||
context: &ExpressionContext,
|
||||
) -> BackendResult {
|
||||
self.put_call_parameters_impl(parameters, |writer, expr| {
|
||||
self.put_call_parameters_impl(parameters, context, |writer, context, expr| {
|
||||
writer.put_expression(expr, context, true)
|
||||
})
|
||||
}
|
||||
|
||||
fn put_call_parameters_impl<E>(
|
||||
fn put_call_parameters_impl<C, E>(
|
||||
&mut self,
|
||||
parameters: impl Iterator<Item = Handle<crate::Expression>>,
|
||||
ctx: &C,
|
||||
put_expression: E,
|
||||
) -> BackendResult
|
||||
where
|
||||
E: Fn(&mut Self, Handle<crate::Expression>) -> BackendResult,
|
||||
E: Fn(&mut Self, &C, Handle<crate::Expression>) -> BackendResult,
|
||||
{
|
||||
write!(self.out, "(")?;
|
||||
for (i, handle) in parameters.enumerate() {
|
||||
if i != 0 {
|
||||
write!(self.out, ", ")?;
|
||||
}
|
||||
put_expression(self, handle)?;
|
||||
put_expression(self, ctx, handle)?;
|
||||
}
|
||||
write!(self.out, ")")?;
|
||||
Ok(())
|
||||
@@ -1213,24 +1214,33 @@ impl<W: Write> Writer<W> {
|
||||
&mut self,
|
||||
expr_handle: Handle<crate::Expression>,
|
||||
module: &crate::Module,
|
||||
mod_info: &valid::ModuleInfo,
|
||||
) -> BackendResult {
|
||||
self.put_possibly_const_expression(
|
||||
expr_handle,
|
||||
&module.const_expressions,
|
||||
module,
|
||||
|writer, expr| writer.put_const_expression(expr, module),
|
||||
mod_info,
|
||||
&(module, mod_info),
|
||||
|&(_, mod_info), expr| &mod_info[expr],
|
||||
|writer, &(module, _), expr| writer.put_const_expression(expr, module, mod_info),
|
||||
)
|
||||
}
|
||||
|
||||
fn put_possibly_const_expression<E>(
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn put_possibly_const_expression<C, I, E>(
|
||||
&mut self,
|
||||
expr_handle: Handle<crate::Expression>,
|
||||
expressions: &crate::Arena<crate::Expression>,
|
||||
module: &crate::Module,
|
||||
mod_info: &valid::ModuleInfo,
|
||||
ctx: &C,
|
||||
get_expr_ty: I,
|
||||
put_expression: E,
|
||||
) -> BackendResult
|
||||
where
|
||||
E: Fn(&mut Self, Handle<crate::Expression>) -> BackendResult,
|
||||
I: Fn(&C, Handle<crate::Expression>) -> &TypeResolution,
|
||||
E: Fn(&mut Self, &C, Handle<crate::Expression>) -> BackendResult,
|
||||
{
|
||||
match expressions[expr_handle] {
|
||||
crate::Expression::Literal(literal) => match literal {
|
||||
@@ -1263,7 +1273,7 @@ impl<W: Write> Writer<W> {
|
||||
if constant.name.is_some() {
|
||||
write!(self.out, "{}", self.names[&NameKey::Constant(handle)])?;
|
||||
} else {
|
||||
self.put_const_expression(constant.init, module)?;
|
||||
self.put_const_expression(constant.init, module, mod_info)?;
|
||||
}
|
||||
}
|
||||
crate::Expression::ZeroValue(ty) => {
|
||||
@@ -1291,7 +1301,11 @@ impl<W: Write> Writer<W> {
|
||||
crate::TypeInner::Scalar { .. }
|
||||
| crate::TypeInner::Vector { .. }
|
||||
| crate::TypeInner::Matrix { .. } => {
|
||||
self.put_call_parameters_impl(components.iter().copied(), put_expression)?;
|
||||
self.put_call_parameters_impl(
|
||||
components.iter().copied(),
|
||||
ctx,
|
||||
put_expression,
|
||||
)?;
|
||||
}
|
||||
crate::TypeInner::Array { .. } | crate::TypeInner::Struct { .. } => {
|
||||
write!(self.out, " {{")?;
|
||||
@@ -1303,13 +1317,23 @@ impl<W: Write> Writer<W> {
|
||||
if self.struct_member_pads.contains(&(ty, index as u32)) {
|
||||
write!(self.out, "{{}}, ")?;
|
||||
}
|
||||
put_expression(self, component)?;
|
||||
put_expression(self, ctx, component)?;
|
||||
}
|
||||
write!(self.out, "}}")?;
|
||||
}
|
||||
_ => return Err(Error::UnsupportedCompose(ty)),
|
||||
}
|
||||
}
|
||||
crate::Expression::Splat { size, value } => {
|
||||
let scalar_kind = match *get_expr_ty(ctx, value).inner_with(&module.types) {
|
||||
crate::TypeInner::Scalar { kind, .. } => kind,
|
||||
_ => return Err(Error::Validation),
|
||||
};
|
||||
put_numeric_type(&mut self.out, scalar_kind, &[size])?;
|
||||
write!(self.out, "(")?;
|
||||
put_expression(self, ctx, value)?;
|
||||
write!(self.out, ")")?;
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
@@ -1350,12 +1374,16 @@ impl<W: Write> Writer<W> {
|
||||
crate::Expression::Literal(_)
|
||||
| crate::Expression::Constant(_)
|
||||
| crate::Expression::ZeroValue(_)
|
||||
| crate::Expression::Compose { .. } => {
|
||||
| crate::Expression::Compose { .. }
|
||||
| crate::Expression::Splat { .. } => {
|
||||
self.put_possibly_const_expression(
|
||||
expr_handle,
|
||||
&context.function.expressions,
|
||||
context.module,
|
||||
|writer, expr| writer.put_expression(expr, context, true),
|
||||
context.mod_info,
|
||||
context,
|
||||
|context, expr: Handle<crate::Expression>| &context.info[expr].ty,
|
||||
|writer, context, expr| writer.put_expression(expr, context, true),
|
||||
)?;
|
||||
}
|
||||
crate::Expression::Access { base, .. }
|
||||
@@ -1385,16 +1413,6 @@ impl<W: Write> Writer<W> {
|
||||
self.put_access_chain(expr_handle, policy, context)?;
|
||||
}
|
||||
}
|
||||
crate::Expression::Splat { size, value } => {
|
||||
let scalar_kind = match *context.resolve_type(value) {
|
||||
crate::TypeInner::Scalar { kind, .. } => kind,
|
||||
_ => return Err(Error::Validation),
|
||||
};
|
||||
put_numeric_type(&mut self.out, scalar_kind, &[size])?;
|
||||
write!(self.out, "(")?;
|
||||
self.put_expression(value, context, true)?;
|
||||
write!(self.out, ")")?;
|
||||
}
|
||||
crate::Expression::Swizzle {
|
||||
size,
|
||||
vector,
|
||||
@@ -1469,7 +1487,7 @@ impl<W: Write> Writer<W> {
|
||||
|
||||
if let Some(offset) = offset {
|
||||
write!(self.out, ", ")?;
|
||||
self.put_const_expression(offset, context.module)?;
|
||||
self.put_const_expression(offset, context.module, context.mod_info)?;
|
||||
}
|
||||
|
||||
match gather {
|
||||
@@ -2792,7 +2810,7 @@ impl<W: Write> Writer<W> {
|
||||
}
|
||||
// follow-up with any global resources used
|
||||
let mut separate = !arguments.is_empty();
|
||||
let fun_info = &context.mod_info[function];
|
||||
let fun_info = &context.expression.mod_info[function];
|
||||
let mut supports_array_length = false;
|
||||
for (handle, var) in context.expression.module.global_variables.iter() {
|
||||
if fun_info[handle].is_empty() {
|
||||
@@ -3131,7 +3149,7 @@ impl<W: Write> Writer<W> {
|
||||
};
|
||||
|
||||
self.write_type_defs(module)?;
|
||||
self.write_global_constants(module)?;
|
||||
self.write_global_constants(module, info)?;
|
||||
self.write_functions(module, info, options, pipeline_options)
|
||||
}
|
||||
|
||||
@@ -3338,7 +3356,11 @@ impl<W: Write> Writer<W> {
|
||||
}
|
||||
|
||||
/// Writes all named constants
|
||||
fn write_global_constants(&mut self, module: &crate::Module) -> BackendResult {
|
||||
fn write_global_constants(
|
||||
&mut self,
|
||||
module: &crate::Module,
|
||||
mod_info: &valid::ModuleInfo,
|
||||
) -> BackendResult {
|
||||
let constants = module.constants.iter().filter(|&(_, c)| c.name.is_some());
|
||||
|
||||
for (handle, constant) in constants {
|
||||
@@ -3352,7 +3374,7 @@ impl<W: Write> Writer<W> {
|
||||
};
|
||||
let name = &self.names[&NameKey::Constant(handle)];
|
||||
write!(self.out, "constant {ty_name} {name} = ")?;
|
||||
self.put_const_expression(constant.init, module)?;
|
||||
self.put_const_expression(constant.init, module, mod_info)?;
|
||||
writeln!(self.out, ";")?;
|
||||
}
|
||||
|
||||
@@ -3549,7 +3571,7 @@ impl<W: Write> Writer<W> {
|
||||
match local.init {
|
||||
Some(value) => {
|
||||
write!(self.out, " = ")?;
|
||||
self.put_const_expression(value, module)?;
|
||||
self.put_const_expression(value, module, mod_info)?;
|
||||
}
|
||||
None => {
|
||||
write!(self.out, " = {{}}")?;
|
||||
@@ -3569,9 +3591,9 @@ impl<W: Write> Writer<W> {
|
||||
policies: options.bounds_check_policies,
|
||||
guarded_indices,
|
||||
module,
|
||||
mod_info,
|
||||
pipeline_options,
|
||||
},
|
||||
mod_info,
|
||||
result_struct: None,
|
||||
};
|
||||
self.named_expressions.clear();
|
||||
@@ -3958,7 +3980,7 @@ impl<W: Write> Writer<W> {
|
||||
}
|
||||
if let Some(value) = var.init {
|
||||
write!(self.out, " = ")?;
|
||||
self.put_const_expression(value, module)?;
|
||||
self.put_const_expression(value, module, mod_info)?;
|
||||
}
|
||||
writeln!(self.out)?;
|
||||
}
|
||||
@@ -4014,7 +4036,7 @@ impl<W: Write> Writer<W> {
|
||||
match var.init {
|
||||
Some(value) => {
|
||||
write!(self.out, " = ")?;
|
||||
self.put_const_expression(value, module)?;
|
||||
self.put_const_expression(value, module, mod_info)?;
|
||||
writeln!(self.out, ";")?;
|
||||
}
|
||||
None => {
|
||||
@@ -4106,7 +4128,7 @@ impl<W: Write> Writer<W> {
|
||||
match local.init {
|
||||
Some(value) => {
|
||||
write!(self.out, " = ")?;
|
||||
self.put_const_expression(value, module)?;
|
||||
self.put_const_expression(value, module, mod_info)?;
|
||||
}
|
||||
None => {
|
||||
write!(self.out, " = {{}}")?;
|
||||
@@ -4126,9 +4148,9 @@ impl<W: Write> Writer<W> {
|
||||
policies: options.bounds_check_policies,
|
||||
guarded_indices,
|
||||
module,
|
||||
mod_info,
|
||||
pipeline_options,
|
||||
},
|
||||
mod_info,
|
||||
result_struct: Some(&stage_out_name),
|
||||
};
|
||||
self.named_expressions.clear();
|
||||
|
||||
+17
-4
@@ -198,11 +198,15 @@ impl Writer {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn get_expression_type_id(&mut self, tr: &TypeResolution) -> Word {
|
||||
let lookup_ty = match *tr {
|
||||
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()),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn get_expression_type_id(&mut self, tr: &TypeResolution) -> Word {
|
||||
let lookup_ty = self.get_expression_lookup_type(tr);
|
||||
self.get_type_id(lookup_ty)
|
||||
}
|
||||
|
||||
@@ -1242,6 +1246,7 @@ impl Writer {
|
||||
&mut self,
|
||||
handle: Handle<crate::Expression>,
|
||||
ir_module: &crate::Module,
|
||||
mod_info: &ModuleInfo,
|
||||
) -> Result<Word, Error> {
|
||||
let id = match ir_module.const_expressions[handle] {
|
||||
crate::Expression::Literal(literal) => self.get_constant_scalar(literal),
|
||||
@@ -1260,6 +1265,14 @@ impl Writer {
|
||||
.collect();
|
||||
self.get_constant_composite(LookupType::Handle(ty), component_ids.as_slice())
|
||||
}
|
||||
crate::Expression::Splat { size, value } => {
|
||||
let value_id = self.constant_ids[value.index()];
|
||||
let component_ids = &[value_id; 4][..size as usize];
|
||||
|
||||
let ty = self.get_expression_lookup_type(&mod_info[handle]);
|
||||
|
||||
self.get_constant_composite(ty, component_ids)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
@@ -1878,7 +1891,7 @@ impl Writer {
|
||||
self.constant_ids
|
||||
.resize(ir_module.const_expressions.len(), 0);
|
||||
for (handle, _) in ir_module.const_expressions.iter() {
|
||||
self.write_constant_expr(handle, ir_module)?;
|
||||
self.write_constant_expr(handle, ir_module, mod_info)?;
|
||||
}
|
||||
debug_assert!(self.constant_ids.iter().all(|&id| id != 0));
|
||||
|
||||
|
||||
Reference in new issue
Block a user