Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Mar 29, 2023
1 parent f29942a commit ed30382
Show file tree
Hide file tree
Showing 29 changed files with 374 additions and 495 deletions.
14 changes: 6 additions & 8 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2161,19 +2161,17 @@ impl<'a, W: Write> Writer<'a, W> {
match expressions[expr] {
Expression::Literal(literal) => {
match literal {
crate::Literal::I32(value) => write!(self.out, "{}", value)?,
// Floats are written using `Debug` instead of `Display` because it always appends the
// decimal part even it's zero which is needed for a valid glsl float constant
crate::Literal::F64(value) => write!(self.out, "{:?}LF", value)?,
crate::Literal::F32(value) => write!(self.out, "{:?}", value)?,
// Unsigned integers need a `u` at the end
//
// While `core` doesn't necessarily need it, it's allowed and since `es` needs it we
// always write it as the extra branch wouldn't have any benefit in readability
crate::Literal::U32(value) => write!(self.out, "{}u", value)?,
// Floats are written using `Debug` instead of `Display` because it always appends the
// decimal part even it's zero which is needed for a valid glsl float constant
crate::Literal::F32(value) => write!(self.out, "{:?}", value)?,
crate::Literal::I32(value) => write!(self.out, "{}", value)?,
crate::Literal::Bool(value) => write!(self.out, "{}", value)?,
crate::Literal::F64(_) => {
return Err(Error::Custom("f64 literal not supported".to_string()));
}
}
}
Expression::Constant(handle) => {
Expand Down Expand Up @@ -2230,7 +2228,7 @@ impl<'a, W: Write> Writer<'a, W> {
| Expression::Constant(_)
| Expression::New(_)
| Expression::Compose { .. } => {
self.write_possibly_const_expr(expr, &ctx.expressions, |writer, expr| {
self.write_possibly_const_expr(expr, ctx.expressions, |writer, expr| {
writer.write_expr(expr, ctx)
})?;
}
Expand Down
26 changes: 11 additions & 15 deletions src/back/hlsl/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ 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,
) -> BackendResult {
use crate::back::INDENT;
Expand Down Expand Up @@ -789,19 +788,16 @@ impl<'a, W: Write> super::Writer<'a, W> {
expressions: &crate::Arena<crate::Expression>,
) -> BackendResult {
for (handle, _) in expressions.iter() {
match expressions[handle] {
crate::Expression::Compose { ty, .. } => {
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)?;
}
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(())
Expand All @@ -813,7 +809,7 @@ impl<'a, W: Write> super::Writer<'a, W> {
module: &crate::Module,
func_ctx: &FunctionCtx,
) -> BackendResult {
self.write_wrapped_compose_functions(module, &func_ctx.expressions)?;
self.write_wrapped_compose_functions(module, func_ctx.expressions)?;

for (handle, _) in func_ctx.expressions.iter() {
match func_ctx.expressions[handle] {
Expand All @@ -838,7 +834,7 @@ impl<'a, W: Write> super::Writer<'a, W> {
};

if self.wrapped.array_lengths.insert(wal) {
self.write_wrapped_array_length_function(module, wal)?;
self.write_wrapped_array_length_function(wal)?;
}
}
crate::Expression::ImageQuery { image, query } => {
Expand Down
21 changes: 4 additions & 17 deletions src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1965,26 +1965,13 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {

match expressions[expr] {
Expression::Literal(literal) => match literal {
crate::Literal::I32(value) => write!(self.out, "{}", value)?,
crate::Literal::U32(value) => write!(self.out, "{}u", value)?,
// Floats are written using `Debug` instead of `Display` because it always appends the
// decimal part even it's zero
crate::Literal::F64(value) => write!(self.out, "{value:?}L")?,
crate::Literal::F32(value) => write!(self.out, "{value:?}")?,
// crate::Literal::F32(value) => {
// if value.is_infinite() {
// let sign = if value.is_sign_negative() { "-" } else { "" };
// write!(self.out, "{}1.#INF", sign)?;
// } else if value.is_nan() {
// write!(self.out, "(0.0/0.0)")?;
// } else {
// let suffix = if value.fract() == 0.0 { ".0" } else { "" };
// write!(self.out, "{}{}", value, suffix)?;
// }
// }
crate::Literal::U32(value) => write!(self.out, "{}u", value)?,
crate::Literal::I32(value) => write!(self.out, "{}", value)?,
crate::Literal::Bool(value) => write!(self.out, "{}", value)?,
crate::Literal::F64(_) => {
return Err(Error::Unimplemented("f64 literal".to_string()));
}
},
Expression::Constant(handle) => {
let constant = &module.constants[handle];
Expand Down Expand Up @@ -2077,7 +2064,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
self.write_possibly_const_expression(
module,
expr,
&func_ctx.expressions,
func_ctx.expressions,
|writer, expr| writer.write_expr(module, expr, func_ctx),
)?;
}
Expand Down
6 changes: 4 additions & 2 deletions src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,9 @@ impl<W: Write> Writer<W> {
{
match expressions[expr_handle] {
crate::Expression::Literal(literal) => match literal {
crate::Literal::F64(value) => todo!(),
crate::Literal::F64(_) => {
return Err(Error::CapabilityNotSupported(valid::Capabilities::FLOAT64))
}
crate::Literal::F32(value) => {
if value.is_infinite() {
let sign = if value.is_sign_negative() { "-" } else { "" };
Expand Down Expand Up @@ -3222,7 +3224,7 @@ impl<W: Write> Writer<W> {

/// Writes all named constants
fn write_global_constants(&mut self, module: &crate::Module) -> BackendResult {
let mut constants = module.constants.iter().filter(|&(_, c)| c.name.is_some());
let constants = module.constants.iter().filter(|&(_, c)| c.name.is_some());

for (handle, constant) in constants {
let ty_name = TypeContext {
Expand Down
4 changes: 2 additions & 2 deletions src/back/spv/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<'w> BlockContext<'w> {
if let Ok(known_index) = self
.ir_module
.to_ctx()
.to_array_length(index, Some(&self.ir_function.expressions))
.eval_expr_to_u32(index, Some(&self.ir_function.expressions))
{
// Both the index and length are known at compile time.
//
Expand Down Expand Up @@ -241,7 +241,7 @@ impl<'w> BlockContext<'w> {
if let Ok(known_index) = self
.ir_module
.to_ctx()
.to_array_length(index, Some(&self.ir_function.expressions))
.eval_expr_to_u32(index, Some(&self.ir_function.expressions))
{
// Both the index and length are known at compile time.
//
Expand Down
4 changes: 2 additions & 2 deletions src/back/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ enum LocalType {
Sampler,
PointerToBindingArray {
base: Handle<crate::Type>,
size: u64,
size: u32,
},
BindingArray {
base: Handle<crate::Type>,
size: u64,
size: u32,
},
AccelerationStructure,
RayQuery,
Expand Down
6 changes: 2 additions & 4 deletions src/back/spv/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,9 +936,7 @@ impl Writer {
}
LocalType::BindingArray { base, size } => {
let inner_ty = self.get_type_id(LookupType::Handle(base));
// TODO: add Literal::U64
let scalar_id =
self.get_constant_scalar(crate::Literal::U32(size.try_into().unwrap()));
let scalar_id = self.get_constant_scalar(crate::Literal::U32(size));
Instruction::type_array(id, inner_ty, scalar_id)
}
LocalType::PointerToBindingArray { base, size } => {
Expand Down Expand Up @@ -1635,7 +1633,7 @@ impl Writer {
substitute_inner_type_lookup =
Some(LookupType::Local(LocalType::PointerToBindingArray {
base,
size: remapped_binding_array_size as u64,
size: remapped_binding_array_size,
}))
}
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,15 +1089,15 @@ impl<W: Write> Writer<W> {
match expressions[expr] {
Expression::Literal(literal) => {
match literal {
crate::Literal::I32(value) => write!(self.out, "{}", value)?,
crate::Literal::U32(value) => write!(self.out, "{}u", value)?,
// Floats are written using `Debug` instead of `Display` because it always appends the
// decimal part even it's zero
crate::Literal::F32(value) => write!(self.out, "{:?}", value)?,
crate::Literal::Bool(value) => write!(self.out, "{}", value)?,
crate::Literal::F64(_) => {
return Err(Error::Unimplemented("f64 literal".to_string()));
return Err(Error::Custom("unsupported f64 literal".to_string()));
}
crate::Literal::F32(value) => write!(self.out, "{:?}", value)?,
crate::Literal::U32(value) => write!(self.out, "{}u", value)?,
crate::Literal::I32(value) => write!(self.out, "{}", value)?,
crate::Literal::Bool(value) => write!(self.out, "{}", value)?,
}
}
Expression::Constant(handle) => {
Expand Down Expand Up @@ -1168,7 +1168,7 @@ impl<W: Write> Writer<W> {
self.write_possibly_const_expression(
module,
expr,
&func_ctx.expressions,
func_ctx.expressions,
|writer, expr| writer.write_expr(module, expr, func_ctx),
)?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/front/glsl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ impl Context {
index: frontend
.module
.to_ctx()
.to_array_length(const_expr, None)
.eval_expr_to_u32(const_expr, None)
.ok()?,
},
meta,
Expand Down
7 changes: 3 additions & 4 deletions src/front/glsl/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
error::{Error, ErrorKind},
Span,
};
use crate::{proc::Alignment, Arena, Constant, Handle, Type, TypeInner, UniqueArena};
use crate::{proc::Alignment, Handle, Type, TypeInner, UniqueArena};

/// Struct with information needed for defining a struct member.
///
Expand All @@ -43,7 +43,6 @@ pub fn calculate_offset(
meta: Span,
layout: StructLayout,
types: &mut UniqueArena<Type>,
constants: &Arena<Constant>,
errors: &mut Vec<Error>,
) -> TypeAlignSpan {
// When using the std430 storage layout, shader storage blocks will be laid out in buffer storage
Expand All @@ -68,7 +67,7 @@ pub fn calculate_offset(
// to rules (1), (2), and (3), and rounded up to the base alignment of a vec4.
// TODO: Matrices array
TypeInner::Array { base, size, .. } => {
let info = calculate_offset(base, meta, layout, types, constants, errors);
let info = calculate_offset(base, meta, layout, types, errors);

let name = types[ty].name.clone();

Expand Down Expand Up @@ -133,7 +132,7 @@ pub fn calculate_offset(
let name = types[ty].name.clone();

for member in members.iter_mut() {
let info = calculate_offset(member.ty, meta, layout, types, constants, errors);
let info = calculate_offset(member.ty, meta, layout, types, errors);

let member_alignment = info.align;
span = member_alignment.round_up(span);
Expand Down
8 changes: 4 additions & 4 deletions src/front/glsl/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
variables::{GlobalOrConstant, VarDeclaration},
Frontend, Result,
};
use crate::{arena::Handle, proc::ArrayLengthError, Block, Expression, Span, Type};
use crate::{arena::Handle, proc::U32EvalError, Block, Expression, Span, Type};
use pp_rs::token::{PreprocessorError, Token as PPToken, TokenValue as PPTokenValue};
use std::iter::Peekable;

Expand Down Expand Up @@ -193,15 +193,15 @@ impl<'source> ParsingContext<'source> {
fn parse_uint_constant(&mut self, frontend: &mut Frontend) -> Result<(u32, Span)> {
let (const_expr, meta) = self.parse_constant_expression(frontend)?;

let res = frontend.module.to_ctx().to_array_length(const_expr, None);
let res = frontend.module.to_ctx().eval_expr_to_u32(const_expr, None);

let int = match res {
Ok(value) => Ok(value),
Err(ArrayLengthError::NotPositive) => Err(Error {
Err(U32EvalError::Negative) => Err(Error {
kind: ErrorKind::SemanticError("int constant overflows".into()),
meta,
}),
Err(ArrayLengthError::Invalid) => Err(Error {
Err(U32EvalError::Invalid) => Err(Error {
kind: ErrorKind::SemanticError("Expected a uint constant".into()),
meta,
}),
Expand Down
1 change: 0 additions & 1 deletion src/front/glsl/parser/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,6 @@ impl<'source> ParsingContext<'source> {
meta,
layout,
&mut frontend.module.types,
&frontend.module.constants,
&mut frontend.errors,
);

Expand Down
3 changes: 1 addition & 2 deletions src/front/glsl/parser/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ impl<'source> ParsingContext<'source> {

let size = u32::try_from(args.len())
.ok()
.map(NonZeroU32::new)
.flatten()
.and_then(NonZeroU32::new)
.ok_or(Error {
kind: ErrorKind::SemanticError(
"There must be at least one argument".into(),
Expand Down
4 changes: 1 addition & 3 deletions src/front/glsl/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use super::{
constants::{ConstantSolver, ExprType},
context::Context,
Error, ErrorKind, Frontend, Result, Span,
constants::ConstantSolver, context::Context, Error, ErrorKind, Frontend, Result, Span,
};
use crate::{
proc::ResolveContext, Bytes, Expression, Handle, ImageClass, ImageDimension, ScalarKind, Type,
Expand Down
11 changes: 2 additions & 9 deletions src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,6 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
selections: &[spirv::Word],
type_arena: &UniqueArena<crate::Type>,
expressions: &mut Arena<crate::Expression>,
constants: &Arena<crate::Constant>,
span: crate::Span,
) -> Result<Handle<crate::Expression>, Error> {
let selection = match selections.first() {
Expand Down Expand Up @@ -1229,7 +1228,6 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
&selections[1..],
type_arena,
expressions,
constants,
span,
)?;

Expand Down Expand Up @@ -1488,7 +1486,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
let index_maybe = match *index_expr_data {
crate::Expression::Constant(const_handle) => Some(
ctx.global_ctx()
.to_array_length(
.eval_expr_to_u32(
ctx.const_arena[const_handle].init.unwrap(),
None,
)
Expand Down Expand Up @@ -1755,7 +1753,6 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
let root_type_lookup = self.lookup_type.lookup(root_lexp.type_id)?;
let index_lexp = self.lookup_expression.lookup(index_id)?;
let index_handle = get_expr_handle!(index_id, index_lexp);
let index_type = self.lookup_type.lookup(index_lexp.type_id)?.handle;

let num_components = match ctx.type_arena[root_type_lookup.handle].inner {
crate::TypeInner::Vector { size, .. } => size as u32,
Expand Down Expand Up @@ -1887,7 +1884,6 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
&selections,
ctx.type_arena,
ctx.expressions,
ctx.const_arena,
span,
)?;

Expand Down Expand Up @@ -4358,8 +4354,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
let length_const = self.lookup_constant.lookup(length_id)?;

let size = resolve_constant(module.to_ctx(), length_const.handle)
.map(|size| NonZeroU32::new(size))
.flatten()
.and_then(NonZeroU32::new)
.ok_or(Error::InvalidArraySize(length_const.handle))?;

let decor = self.future_decor.remove(&id).unwrap_or_default();
Expand Down Expand Up @@ -5077,7 +5072,6 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
match null::generate_default_built_in(
Some(built_in),
ty,
&module.types,
&mut module.const_expressions,
span,
) {
Expand Down Expand Up @@ -5108,7 +5102,6 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
let handle = null::generate_default_built_in(
built_in,
member_ty,
&module.types,
&mut module.const_expressions,
span,
)?;
Expand Down
Loading

0 comments on commit ed30382

Please sign in to comment.