Skip to content

Commit

Permalink
const-expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Mar 30, 2023
1 parent 52043be commit c10de39
Show file tree
Hide file tree
Showing 166 changed files with 10,534 additions and 11,618 deletions.
12 changes: 7 additions & 5 deletions src/back/dot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@ fn write_function_expressions(
for (handle, expression) in fun.expressions.iter() {
use crate::Expression as E;
let (label, color_id) = match *expression {
E::Literal(_) => ("Literal".into(), 2),
E::Constant(_) => ("Constant".into(), 2),
E::New(_) => ("New".into(), 2),
E::Compose { ref components, .. } => {
payload = Some(Payload::Arguments(components));
("Compose".into(), 3)
}
E::Access { base, index } => {
edges.insert("base", base);
edges.insert("index", index);
Expand All @@ -406,7 +413,6 @@ fn write_function_expressions(
edges.insert("base", base);
(format!("AccessIndex[{index}]").into(), 1)
}
E::Constant(_) => ("Constant".into(), 2),
E::Splat { size, value } => {
edges.insert("value", value);
(format!("Splat{size:?}").into(), 3)
Expand All @@ -419,10 +425,6 @@ fn write_function_expressions(
edges.insert("vector", vector);
(format!("Swizzle{:?}", &pattern[..size as usize]).into(), 3)
}
E::Compose { ref components, .. } => {
payload = Some(Payload::Arguments(components));
("Compose".into(), 3)
}
E::FunctionArgument(index) => (format!("Argument[{index}]").into(), 1),
E::GlobalVariable(h) => {
payload = Some(Payload::Global(h));
Expand Down
242 changes: 119 additions & 123 deletions src/back/glsl/mod.rs

Large diffs are not rendered by default.

24 changes: 8 additions & 16 deletions src/back/hlsl/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ impl crate::TypeInner {
}
}

pub(super) fn size_hlsl(
&self,
types: &crate::UniqueArena<crate::Type>,
constants: &crate::Arena<crate::Constant>,
) -> u32 {
pub(super) fn size_hlsl(&self, gctx: crate::GlobalCtx) -> u32 {
match *self {
Self::Matrix {
columns,
Expand All @@ -57,27 +53,24 @@ impl crate::TypeInner {
}
Self::Array { base, size, stride } => {
let count = match size {
crate::ArraySize::Constant(handle) => {
constants[handle].to_array_length().unwrap_or(1)
}
crate::ArraySize::Constant(size) => size.get(),
// A dynamically-sized array has to have at least one element
crate::ArraySize::Dynamic => 1,
};
let last_el_size = types[base].inner.size_hlsl(types, constants);
let last_el_size = gctx.types[base].inner.size_hlsl(gctx.reborrow());
((count - 1) * stride) + last_el_size
}
_ => self.size(constants),
_ => self.size(gctx.reborrow()),
}
}

/// Used to generate the name of the wrapped type constructor
pub(super) fn hlsl_type_id<'a>(
base: crate::Handle<crate::Type>,
types: &crate::UniqueArena<crate::Type>,
constants: &crate::Arena<crate::Constant>,
gctx: crate::GlobalCtx,
names: &'a crate::FastHashMap<crate::proc::NameKey, String>,
) -> Result<Cow<'a, str>, Error> {
Ok(match types[base].inner {
Ok(match gctx.types[base].inner {
crate::TypeInner::Scalar { kind, width } => Cow::Borrowed(kind.to_hlsl_str(width)?),
crate::TypeInner::Vector { size, kind, width } => Cow::Owned(format!(
"{}{}",
Expand All @@ -99,9 +92,8 @@ impl crate::TypeInner {
size: crate::ArraySize::Constant(size),
..
} => Cow::Owned(format!(
"array{}_{}_",
constants[size].to_array_length().unwrap(),
Self::hlsl_type_id(base, types, constants, names)?
"array{size}_{}_",
Self::hlsl_type_id(base, gctx, names)?
)),
crate::TypeInner::Struct { .. } => {
Cow::Borrowed(&names[&crate::proc::NameKey::Type(base)])
Expand Down
111 changes: 38 additions & 73 deletions src/back/hlsl/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,15 @@ impl<'a, W: Write> super::Writer<'a, W> {
/// <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-rwbyteaddressbuffer-getdimensions>
pub(super) fn write_wrapped_array_length_function(
&mut self,
module: &crate::Module,
wal: WrappedArrayLength,
expr_handle: Handle<crate::Expression>,
func_ctx: &FunctionCtx,
) -> BackendResult {
use crate::back::INDENT;

const ARGUMENT_VARIABLE_NAME: &str = "buffer";
const RETURN_VARIABLE_NAME: &str = "ret";

// Write function return type and name
let ret_ty = func_ctx.info[expr_handle].ty.inner_with(&module.types);
self.write_value_type(module, ret_ty)?;
write!(self.out, " ")?;
write!(self.out, "uint ")?;
self.write_wrapped_array_length_function_name(wal)?;

// Write function parameters
Expand Down Expand Up @@ -347,12 +342,7 @@ impl<'a, W: Write> super::Writer<'a, W> {
module: &crate::Module,
constructor: WrappedConstructor,
) -> BackendResult {
let name = crate::TypeInner::hlsl_type_id(
constructor.ty,
&module.types,
&module.constants,
&self.names,
)?;
let name = crate::TypeInner::hlsl_type_id(constructor.ty, module.to_ctx(), &self.names)?;
write!(self.out, "Construct{name}")?;
Ok(())
}
Expand Down Expand Up @@ -411,8 +401,7 @@ impl<'a, W: Write> super::Writer<'a, W> {
size: crate::ArraySize::Constant(size),
..
} => {
let count = module.constants[size].to_array_length().unwrap();
for i in 0..count as usize {
for i in 0..size.get() as usize {
write_arg(i, base)?;
}
}
Expand Down Expand Up @@ -486,8 +475,7 @@ impl<'a, W: Write> super::Writer<'a, W> {
write!(self.out, " {RETURN_VARIABLE_NAME}")?;
self.write_array_size(module, base, crate::ArraySize::Constant(size))?;
write!(self.out, " = {{ ")?;
let count = module.constants[size].to_array_length().unwrap();
for i in 0..count {
for i in 0..size.get() {
if i != 0 {
write!(self.out, ", ")?;
}
Expand Down Expand Up @@ -793,14 +781,36 @@ impl<'a, W: Write> super::Writer<'a, W> {
Ok(())
}

/// Helper function that write wrapped function for `Expression::ImageQuery` and `Expression::ArrayLength`
///
/// <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-getdimensions>
/// Helper function that writes compose wrapped functions
pub(super) fn write_wrapped_compose_functions(
&mut self,
module: &crate::Module,
expressions: &crate::Arena<crate::Expression>,
) -> BackendResult {
for (handle, _) in expressions.iter() {
if let crate::Expression::Compose { ty, .. } = expressions[handle] {
match module.types[ty].inner {
crate::TypeInner::Struct { .. } | crate::TypeInner::Array { .. } => {
let constructor = WrappedConstructor { ty };
if self.wrapped.constructors.insert(constructor) {
self.write_wrapped_constructor_function(module, constructor)?;
}
}
_ => {}
};
}
}
Ok(())
}

/// Helper function that writes various wrapped functions
pub(super) fn write_wrapped_functions(
&mut self,
module: &crate::Module,
func_ctx: &FunctionCtx,
) -> BackendResult {
self.write_wrapped_compose_functions(module, func_ctx.expressions)?;

for (handle, _) in func_ctx.expressions.iter() {
match func_ctx.expressions[handle] {
crate::Expression::ArrayLength(expr) => {
Expand All @@ -823,9 +833,8 @@ impl<'a, W: Write> super::Writer<'a, W> {
writable: storage_access.contains(crate::StorageAccess::STORE),
};

if !self.wrapped.array_lengths.contains(&wal) {
self.write_wrapped_array_length_function(module, wal, handle, func_ctx)?;
self.wrapped.array_lengths.insert(wal);
if self.wrapped.array_lengths.insert(wal) {
self.write_wrapped_array_length_function(wal)?;
}
}
crate::Expression::ImageQuery { image, query } => {
Expand All @@ -843,9 +852,8 @@ impl<'a, W: Write> super::Writer<'a, W> {
_ => unreachable!("we only query images"),
};

if !self.wrapped.image_queries.contains(&wiq) {
if self.wrapped.image_queries.insert(wiq) {
self.write_wrapped_image_query_function(module, wiq, handle, func_ctx)?;
self.wrapped.image_queries.insert(wiq);
}
}
// Write `WrappedConstructor` for structs that are loaded from `AddressSpace::Storage`
Expand Down Expand Up @@ -874,19 +882,18 @@ impl<'a, W: Write> super::Writer<'a, W> {
}

let constructor = WrappedConstructor { ty };
if !writer.wrapped.constructors.contains(&constructor) {
if writer.wrapped.constructors.insert(constructor) {
writer
.write_wrapped_constructor_function(module, constructor)?;
writer.wrapped.constructors.insert(constructor);
}
}
crate::TypeInner::Array { base, .. } => {
write_wrapped_constructor(writer, base, module)?;

let constructor = WrappedConstructor { ty };
if !writer.wrapped.constructors.contains(&constructor) {
if writer.wrapped.constructors.insert(constructor) {
writer
.write_wrapped_constructor_function(module, constructor)?;
writer.wrapped.constructors.insert(constructor);
}
}
_ => {}
Expand All @@ -895,18 +902,6 @@ impl<'a, W: Write> super::Writer<'a, W> {
Ok(())
}
}
crate::Expression::Compose { ty, components: _ } => {
let constructor = match module.types[ty].inner {
crate::TypeInner::Struct { .. } | crate::TypeInner::Array { .. } => {
WrappedConstructor { ty }
}
_ => continue,
};
if !self.wrapped.constructors.contains(&constructor) {
self.write_wrapped_constructor_function(module, constructor)?;
self.wrapped.constructors.insert(constructor);
}
}
// We treat matrices of the form `matCx2` as a sequence of C `vec2`s
// (see top level module docs for details).
//
Expand All @@ -932,7 +927,7 @@ impl<'a, W: Write> super::Writer<'a, W> {
let ty = base_ty_handle.unwrap();
let access = WrappedStructMatrixAccess { ty, index };

if !self.wrapped.struct_matrix_access.contains(&access) {
if self.wrapped.struct_matrix_access.insert(access) {
self.write_wrapped_struct_matrix_get_function(module, access)?;
self.write_wrapped_struct_matrix_set_function(module, access)?;
self.write_wrapped_struct_matrix_set_vec_function(
Expand All @@ -941,7 +936,6 @@ impl<'a, W: Write> super::Writer<'a, W> {
self.write_wrapped_struct_matrix_set_scalar_function(
module, access,
)?;
self.wrapped.struct_matrix_access.insert(access);
}
}
_ => {}
Expand All @@ -955,33 +949,6 @@ impl<'a, W: Write> super::Writer<'a, W> {
Ok(())
}

pub(super) fn write_wrapped_constructor_function_for_constant(
&mut self,
module: &crate::Module,
constant: &crate::Constant,
) -> BackendResult {
if let crate::ConstantInner::Composite { ty, ref components } = constant.inner {
match module.types[ty].inner {
crate::TypeInner::Struct { .. } | crate::TypeInner::Array { .. } => {
let constructor = WrappedConstructor { ty };
if !self.wrapped.constructors.contains(&constructor) {
self.write_wrapped_constructor_function(module, constructor)?;
self.wrapped.constructors.insert(constructor);
}
}
_ => {}
}
for constant in components {
self.write_wrapped_constructor_function_for_constant(
module,
&module.constants[*constant],
)?;
}
}

Ok(())
}

pub(super) fn write_texture_coordinates(
&mut self,
kind: &str,
Expand Down Expand Up @@ -1092,9 +1059,8 @@ impl<'a, W: Write> super::Writer<'a, W> {
}) = super::writer::get_inner_matrix_data(module, global.ty)
{
let entry = WrappedMatCx2 { columns };
if !self.wrapped.mat_cx2s.contains(&entry) {
if self.wrapped.mat_cx2s.insert(entry) {
self.write_mat_cx2_typedef_and_functions(entry)?;
self.wrapped.mat_cx2s.insert(entry);
}
}
}
Expand All @@ -1111,9 +1077,8 @@ impl<'a, W: Write> super::Writer<'a, W> {
}) = super::writer::get_inner_matrix_data(module, member.ty)
{
let entry = WrappedMatCx2 { columns };
if !self.wrapped.mat_cx2s.contains(&entry) {
if self.wrapped.mat_cx2s.insert(entry) {
self.write_mat_cx2_typedef_and_functions(entry)?;
self.wrapped.mat_cx2s.insert(entry);
}
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/back/hlsl/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,15 @@ impl<W: fmt::Write> super::Writer<'_, W> {
}
crate::TypeInner::Array {
base,
size: crate::ArraySize::Constant(const_handle),
..
size: crate::ArraySize::Constant(size),
stride,
} => {
let constructor = super::help::WrappedConstructor {
ty: result_ty.handle().unwrap(),
};
self.write_wrapped_constructor_function_name(module, constructor)?;
write!(self.out, "(")?;
let count = module.constants[const_handle].to_array_length().unwrap();
let stride = module.types[base].inner.size(&module.constants);
let iter = (0..count).map(|i| (TypeResolution::Handle(base), stride * i));
let iter = (0..size.get()).map(|i| (TypeResolution::Handle(base), stride * i));
self.write_storage_load_sequence(module, var_handle, iter, func_ctx)?;
write!(self.out, ")")?;
}
Expand Down Expand Up @@ -301,23 +299,21 @@ impl<W: fmt::Write> super::Writer<'_, W> {
}
crate::TypeInner::Array {
base,
size: crate::ArraySize::Constant(const_handle),
..
size: crate::ArraySize::Constant(size),
stride,
} => {
// first, assign the value to a temporary
writeln!(self.out, "{level}{{")?;
write!(self.out, "{}", level.next())?;
self.write_value_type(module, &module.types[base].inner)?;
let depth = level.next().0;
write!(self.out, " {STORE_TEMP_NAME}{depth}")?;
self.write_array_size(module, base, crate::ArraySize::Constant(const_handle))?;
self.write_array_size(module, base, crate::ArraySize::Constant(size))?;
write!(self.out, " = ")?;
self.write_store_value(module, &value, func_ctx)?;
writeln!(self.out, ";")?;
// then iterate the stores
let count = module.constants[const_handle].to_array_length().unwrap();
let stride = module.types[base].inner.size(&module.constants);
for i in 0..count {
for i in 0..size.get() {
self.temp_access_chain.push(SubAccess::Offset(i * stride));
let sv = StoreValue::TempIndex {
depth,
Expand Down
Loading

0 comments on commit c10de39

Please sign in to comment.