Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce GlobalCtx #2335

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 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::proc::GlobalCtx) -> u32 {
match *self {
Self::Matrix {
columns,
Expand All @@ -58,26 +54,25 @@ impl crate::TypeInner {
Self::Array { base, size, stride } => {
let count = match size {
crate::ArraySize::Constant(handle) => {
constants[handle].to_array_length().unwrap_or(1)
gctx.constants[handle].to_array_length().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);
((count - 1) * stride) + last_el_size
}
_ => self.size(constants),
_ => self.size(gctx),
}
}

/// 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::proc::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 +95,8 @@ impl crate::TypeInner {
..
} => Cow::Owned(format!(
"array{}_{}_",
constants[size].to_array_length().unwrap(),
Self::hlsl_type_id(base, types, constants, names)?
gctx.constants[size].to_array_length().unwrap(),
Self::hlsl_type_id(base, gctx, names)?
)),
crate::TypeInner::Struct { .. } => {
Cow::Borrowed(&names[&crate::proc::NameKey::Type(base)])
Expand Down
7 changes: 1 addition & 6 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
4 changes: 2 additions & 2 deletions src/back/hlsl/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl<W: fmt::Write> super::Writer<'_, W> {
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 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 @@ -381,7 +381,7 @@ impl<W: fmt::Write> super::Writer<'_, W> {
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 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
2 changes: 1 addition & 1 deletion src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,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
40 changes: 20 additions & 20 deletions src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const CLAMPED_LOD_LOAD_PREFIX: &str = "clamped_lod_e";

struct TypeContext<'a> {
handle: Handle<crate::Type>,
module: &'a crate::Module,
gctx: proc::GlobalCtx<'a>,
names: &'a FastHashMap<NameKey, String>,
access: crate::StorageAccess,
binding: Option<&'a super::ResolvedBinding>,
Expand All @@ -86,7 +86,7 @@ struct TypeContext<'a> {

impl<'a> Display for TypeContext<'a> {
fn fmt(&self, out: &mut Formatter<'_>) -> Result<(), FmtError> {
let ty = &self.module.types[self.handle];
let ty = &self.gctx.types[self.handle];
if ty.needs_alias() && !self.first_time {
let name = &self.names[&NameKey::Type(self.handle)];
return write!(out, "{name}");
Expand Down Expand Up @@ -223,7 +223,7 @@ impl<'a> Display for TypeContext<'a> {
} else if let crate::ArraySize::Constant(size) = size {
let constant_ctx = ConstantContext {
handle: size,
arena: &self.module.constants,
arena: self.gctx.constants,
names: self.names,
first_time: false,
};
Expand Down Expand Up @@ -271,7 +271,7 @@ impl<'a> TypedGlobalVariable<'a> {
};
let ty_name = TypeContext {
handle: var.ty,
module: self.module,
gctx: self.module.to_ctx(),
names: self.names,
access: storage_access,
binding: self.binding,
Expand Down Expand Up @@ -399,7 +399,7 @@ fn should_pack_struct_member(
}

let ty_inner = &module.types[member.ty].inner;
let last_offset = member.offset + ty_inner.size(&module.constants);
let last_offset = member.offset + ty_inner.size(module.to_ctx());
let next_offset = match members.get(index + 1) {
Some(next) => next.offset,
None => span,
Expand Down Expand Up @@ -1153,7 +1153,7 @@ impl<W: Write> Writer<W> {
crate::TypeInner::Array { base, stride, .. } => (
context.module.types[base]
.inner
.size(&context.module.constants),
.size(context.module.to_ctx()),
stride,
),
_ => return Err(Error::Validation),
Expand Down Expand Up @@ -1336,7 +1336,7 @@ impl<W: Write> Writer<W> {
crate::Expression::ZeroValue(ty) => {
let ty_name = TypeContext {
handle: ty,
module: context.module,
gctx: context.module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -2459,7 +2459,7 @@ impl<W: Write> Writer<W> {
TypeResolution::Handle(ty_handle) => {
let ty_name = TypeContext {
handle: ty_handle,
module: context.module,
gctx: context.module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -3155,7 +3155,7 @@ impl<W: Write> Writer<W> {
} => {
let base_name = TypeContext {
handle: base,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -3200,7 +3200,7 @@ impl<W: Write> Writer<W> {
writeln!(self.out, "{}char _pad{}[{}];", back::INDENT, index, pad)?;
}
let ty_inner = &module.types[member.ty].inner;
last_offset = member.offset + ty_inner.size(&module.constants);
last_offset = member.offset + ty_inner.size(module.to_ctx());

let member_name = &self.names[&NameKey::StructMember(handle, index as u32)];

Expand All @@ -3219,7 +3219,7 @@ impl<W: Write> Writer<W> {
None => {
let base_name = TypeContext {
handle: member.ty,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -3250,7 +3250,7 @@ impl<W: Write> Writer<W> {
_ => {
let ty_name = TypeContext {
handle,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -3310,7 +3310,7 @@ impl<W: Write> Writer<W> {
let name = &self.names[&NameKey::Constant(handle)];
let ty_name = TypeContext {
handle: ty,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -3450,7 +3450,7 @@ impl<W: Write> Writer<W> {
Some(ref result) => {
let ty_name = TypeContext {
handle: result.ty,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand All @@ -3468,7 +3468,7 @@ impl<W: Write> Writer<W> {
let name = &self.names[&NameKey::FunctionArgument(fun_handle, index as u32)];
let param_type_name = TypeContext {
handle: arg.ty,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -3517,7 +3517,7 @@ impl<W: Write> Writer<W> {
for (local_handle, local) in fun.local_variables.iter() {
let ty_name = TypeContext {
handle: local.ty,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -3714,7 +3714,7 @@ impl<W: Write> Writer<W> {
let name = &self.names[name_key];
let ty_name = TypeContext {
handle: ty,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -3758,7 +3758,7 @@ impl<W: Write> Writer<W> {
for (name, ty, binding) in result_members {
let ty_name = TypeContext {
handle: ty,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -3853,7 +3853,7 @@ impl<W: Write> Writer<W> {

let ty_name = TypeContext {
handle: ty,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down Expand Up @@ -4090,7 +4090,7 @@ impl<W: Write> Writer<W> {
let name = &self.names[&NameKey::EntryPointLocal(ep_index as _, local_handle)];
let ty_name = TypeContext {
handle: local.ty,
module,
gctx: module.to_ctx(),
names: &self.names,
access: crate::StorageAccess::empty(),
binding: None,
Expand Down
2 changes: 1 addition & 1 deletion src/front/glsl/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ impl Frontend {
offset: span,
});

span += self.module.types[ty].inner.size(&self.module.constants);
span += self.module.types[ty].inner.size(self.module.to_ctx());

let len = expressions.len();
let load = expressions.append(Expression::Load { pointer }, Default::default());
Expand Down
5 changes: 1 addition & 4 deletions src/front/glsl/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ impl<'source> ParsingContext<'source> {
ArraySize::Constant(constant)
};

frontend
.layouter
.update(&frontend.module.types, &frontend.module.constants)
.unwrap();
frontend.layouter.update(frontend.module.to_ctx()).unwrap();
let stride = frontend.layouter[*ty].to_stride();
*ty = frontend.module.types.insert(
Type {
Expand Down
7 changes: 7 additions & 0 deletions src/front/spv/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
}

impl<'function> BlockContext<'function> {
pub(super) fn gctx(&self) -> crate::proc::GlobalCtx {
crate::proc::GlobalCtx {
types: self.type_arena,
constants: self.const_arena,
}
}

/// Consumes the `BlockContext` producing a Ir [`Block`](crate::Block)
fn lower(mut self) -> crate::Block {
fn lower_impl(
Expand Down
14 changes: 4 additions & 10 deletions src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2089,7 +2089,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
let result_ty = self.lookup_type.lookup(result_type_id)?;
let inner = &ctx.type_arena[result_ty.handle].inner;
let kind = inner.scalar_kind().unwrap();
let size = inner.size(ctx.const_arena) as u8;
let size = inner.size(ctx.gctx()) as u8;

let left_cast = ctx.expressions.append(
crate::Expression::As {
Expand Down Expand Up @@ -4387,9 +4387,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
let decor = self.future_decor.remove(&id).unwrap_or_default();
let base = self.lookup_type.lookup(type_id)?.handle;

self.layouter
.update(&module.types, &module.constants)
.unwrap();
self.layouter.update(module.to_ctx()).unwrap();

// HACK if the underlying type is an image or a sampler, let's assume
// that we're dealing with a binding-array
Expand Down Expand Up @@ -4470,9 +4468,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
let decor = self.future_decor.remove(&id).unwrap_or_default();
let base = self.lookup_type.lookup(type_id)?.handle;

self.layouter
.update(&module.types, &module.constants)
.unwrap();
self.layouter.update(module.to_ctx()).unwrap();

// HACK same case as in `parse_type_array()`
let inner = if let crate::TypeInner::Image { .. } | crate::TypeInner::Sampler { .. } =
Expand Down Expand Up @@ -4523,9 +4519,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
.as_ref()
.map_or(false, |decor| decor.storage_buffer);

self.layouter
.update(&module.types, &module.constants)
.unwrap();
self.layouter.update(module.to_ctx()).unwrap();

let mut members = Vec::<crate::StructMember>::with_capacity(inst.wc as usize - 2);
let mut member_lookups = Vec::with_capacity(members.capacity());
Expand Down
8 changes: 2 additions & 6 deletions src/front/wgsl/lower/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
ctx.module.constants.fetch_or_append(size, Span::UNDEFINED),
),
stride: {
self.layouter
.update(&ctx.module.types, &ctx.module.constants)
.unwrap();
self.layouter.update(ctx.module.to_ctx()).unwrap();
self.layouter[base].to_stride()
},
};
Expand Down Expand Up @@ -645,9 +643,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
ast::ArraySize::Dynamic => crate::ArraySize::Dynamic,
};

self.layouter
.update(&ctx.module.types, &ctx.module.constants)
.unwrap();
self.layouter.update(ctx.module.to_ctx()).unwrap();
let ty = ctx.ensure_type_exists(crate::TypeInner::Array {
base,
size,
Expand Down
Loading