Skip to content

Commit

Permalink
WIP2
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Mar 13, 2023
1 parent d17cef2 commit c3ce0dd
Show file tree
Hide file tree
Showing 30 changed files with 789 additions and 1,241 deletions.
23 changes: 8 additions & 15 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(handle) => gctx.to_array_length(handle).unwrap_or(1),
// 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 @@ -100,8 +93,8 @@ impl crate::TypeInner {
..
} => Cow::Owned(format!(
"array{}_{}_",
constants[size].to_array_length().unwrap(),
Self::hlsl_type_id(base, types, constants, names)?
gctx.to_array_length(size).unwrap(),
Self::hlsl_type_id(base, gctx, names)?
)),
crate::TypeInner::Struct { .. } => {
Cow::Borrowed(&names[&crate::proc::NameKey::Type(base)])
Expand Down
11 changes: 3 additions & 8 deletions src/back/hlsl/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,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,7 +406,7 @@ impl<'a, W: Write> super::Writer<'a, W> {
size: crate::ArraySize::Constant(size),
..
} => {
let count = module.constants[size].to_array_length().unwrap();
let count = module.to_ctx().to_array_length(size).unwrap();
for i in 0..count as usize {
write_arg(i, base)?;
}
Expand Down Expand Up @@ -486,7 +481,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();
let count = module.to_ctx().to_array_length(size).unwrap();
for i in 0..count {
if i != 0 {
write!(self.out, ", ")?;
Expand Down
8 changes: 4 additions & 4 deletions src/back/hlsl/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ impl<W: fmt::Write> super::Writer<'_, W> {
..
} => {
write!(self.out, "{{")?;
let count = module.constants[const_handle].to_array_length().unwrap();
let stride = module.types[base].inner.size(&module.constants);
let count = module.to_ctx().to_array_length(const_handle).unwrap();
let stride = module.types[base].inner.size(module.to_ctx());
let iter = (0..count).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 @@ -311,8 +311,8 @@ impl<W: fmt::Write> super::Writer<'_, W> {
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);
let count = module.to_ctx().to_array_length(const_handle).unwrap();
let stride = module.types[base].inner.size(module.to_ctx());
for i in 0..count {
self.temp_access_chain.push(SubAccess::Offset(i * stride));
let sv = StoreValue::TempIndex {
Expand Down
4 changes: 2 additions & 2 deletions src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
// Panics if `ArraySize::Constant` has a constant that isn't an sint or uint
match size {
crate::ArraySize::Constant(const_handle) => {
let size = module.constants[const_handle].to_array_length().unwrap();
let size = module.to_ctx().to_array_length(const_handle).unwrap();
write!(self.out, "{size}")?;
}
crate::ArraySize::Dynamic => {}
Expand Down Expand Up @@ -870,7 +870,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
}
}
let ty_inner = &module.types[member.ty].inner;
last_offset = member.offset + ty_inner.size_hlsl(&module.types, &module.constants);
last_offset = member.offset + ty_inner.size_hlsl(module.to_ctx());

// The indentation is only for readability
write!(self.out, "{}", back::INDENT)?;
Expand Down
Loading

0 comments on commit c3ce0dd

Please sign in to comment.