mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-24 16:31:11 +01:00
[msl] collect function parameters
This commit is contained in:
1 parent
763014cb43
commit
b3c75ec1cd
2 files changed
+119
-69
No files matched your search
+119
-63
@@ -1,4 +1,4 @@
|
||||
use std::fmt::{Error as FmtError, Write};
|
||||
use std::fmt::{Display, Error as FmtError, Formatter, Write};
|
||||
|
||||
|
||||
pub struct Options {
|
||||
@@ -15,6 +15,94 @@ impl From<FmtError> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
trait Indexed {
|
||||
const CLASS: &'static str;
|
||||
fn id(&self) -> usize;
|
||||
}
|
||||
|
||||
impl Indexed for crate::Token<crate::StructDeclaration> {
|
||||
const CLASS: &'static str = "Struct";
|
||||
fn id(&self) -> usize { self.index() }
|
||||
}
|
||||
impl Indexed for crate::Token<crate::GlobalVariable> {
|
||||
const CLASS: &'static str = "global";
|
||||
fn id(&self) -> usize { self.index() }
|
||||
}
|
||||
impl Indexed for crate::Token<crate::Function> {
|
||||
const CLASS: &'static str = "function";
|
||||
fn id(&self) -> usize { self.index() }
|
||||
}
|
||||
|
||||
struct MemberIndex(usize);
|
||||
impl Indexed for MemberIndex {
|
||||
const CLASS: &'static str = "field";
|
||||
fn id(&self) -> usize { self.0 }
|
||||
}
|
||||
|
||||
struct ParameterIndex(usize);
|
||||
impl Indexed for ParameterIndex {
|
||||
const CLASS: &'static str = "param";
|
||||
fn id(&self) -> usize { self.0 }
|
||||
}
|
||||
|
||||
|
||||
enum Name<'a, I> {
|
||||
Custom(&'a str),
|
||||
Index(I),
|
||||
}
|
||||
|
||||
impl<I: Indexed> Display for Name<'_, I> {
|
||||
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), FmtError> {
|
||||
match *self {
|
||||
Name::Custom(name) => formatter.write_str(name),
|
||||
Name::Index(ref index) => write!(formatter, "{}{}", I::CLASS, index.id()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait AsName {
|
||||
fn or_index<I>(&self, index: I) -> Name<I>;
|
||||
}
|
||||
|
||||
impl AsName for Option<String> {
|
||||
fn or_index<I>(&self, index: I) -> Name<I> {
|
||||
match *self {
|
||||
Some(ref name) => Name::Custom(name),
|
||||
None => Name::Index(index),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TypedVar<'a, T>(&'a crate::Type, &'a T, &'a crate::Storage<crate::StructDeclaration>);
|
||||
|
||||
impl<T: Display> Display for TypedVar<'_, T> {
|
||||
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), FmtError> {
|
||||
match *self.0 {
|
||||
crate::Type::Void => {
|
||||
write!(formatter, "void {}", self.1)
|
||||
},
|
||||
crate::Type::Scalar { kind, .. } => {
|
||||
write!(formatter, "{} {}", scalar_kind_string(kind), self.1)
|
||||
},
|
||||
crate::Type::Vector { size, kind, .. } => {
|
||||
write!(formatter, "{}{} {}", scalar_kind_string(kind), vector_size_string(size), self.1)
|
||||
},
|
||||
crate::Type::Matrix { columns, rows, kind, .. } => {
|
||||
write!(formatter, "{}{}x{} {}", scalar_kind_string(kind), vector_size_string(columns), vector_size_string(rows), self.1)
|
||||
}
|
||||
crate::Type::Array { ref base, length } => {
|
||||
write!(formatter, "{}[{}]", TypedVar(base, self.1, self.2), length)
|
||||
}
|
||||
crate::Type::Pointer { ref base, .. } => {
|
||||
write!(formatter, "{}*{}", TypedVar(base, &"", self.2), self.1)
|
||||
}
|
||||
crate::Type::Struct(token) => {
|
||||
write!(formatter, "{} {}", self.2[token].name.or_index(token), self.1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Writer<W> {
|
||||
out: W,
|
||||
}
|
||||
@@ -36,59 +124,6 @@ fn vector_size_string(size: crate::VectorSize) -> &'static str {
|
||||
}
|
||||
|
||||
impl<W: Write> Writer<W> {
|
||||
fn put_type_pre(&mut self, ty: &crate::Type) -> Result<(), FmtError> {
|
||||
match *ty {
|
||||
crate::Type::Void => {
|
||||
write!(self.out, "void")
|
||||
},
|
||||
crate::Type::Scalar { kind, .. } => {
|
||||
write!(self.out, "{}", scalar_kind_string(kind))
|
||||
},
|
||||
crate::Type::Vector { size, kind, .. } => {
|
||||
write!(self.out, "{}{}", scalar_kind_string(kind), vector_size_string(size))
|
||||
},
|
||||
crate::Type::Matrix { columns, rows, kind, .. } => {
|
||||
write!(self.out, "{}{}x{}", scalar_kind_string(kind), vector_size_string(columns), vector_size_string(rows))
|
||||
}
|
||||
crate::Type::Array { ref base, .. } => {
|
||||
self.put_type_pre(base)
|
||||
}
|
||||
_ => panic!("Unknown type {:?}", ty),
|
||||
}
|
||||
}
|
||||
|
||||
fn put_type_post(&mut self, ty: &crate::Type) -> Result<(), FmtError> {
|
||||
match *ty {
|
||||
crate::Type::Array { length, .. } => {
|
||||
write!(self.out, "[{}]", length)
|
||||
}
|
||||
_ => Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn put_struct_name(
|
||||
&mut self,
|
||||
token: crate::Token<crate::StructDeclaration>,
|
||||
module: &crate::Module,
|
||||
) -> Result<(), FmtError> {
|
||||
match module.struct_declarations[token].name {
|
||||
Some(ref name) => write!(self.out, "{}", name),
|
||||
None => write!(self.out, "_Struct{}", token.index()),
|
||||
}
|
||||
}
|
||||
|
||||
fn put_struct_member_name(
|
||||
&mut self,
|
||||
token: crate::Token<crate::StructDeclaration>,
|
||||
index: usize,
|
||||
module: &crate::Module,
|
||||
) -> Result<(), FmtError> {
|
||||
match module.struct_declarations[token].members[index].name {
|
||||
Some(ref name) => write!(self.out, "{}", name),
|
||||
None => write!(self.out, "_field{}", index),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(&mut self, module: &crate::Module) -> Result<(), Error> {
|
||||
writeln!(self.out, "#include <metal_stdlib>")?;
|
||||
writeln!(self.out, "#include <simd/simd.h>")?;
|
||||
@@ -96,20 +131,41 @@ impl<W: Write> Writer<W> {
|
||||
|
||||
writeln!(self.out, "")?;
|
||||
for (token, decl) in module.struct_declarations.iter() {
|
||||
write!(self.out, "struct ")?;
|
||||
self.put_struct_name(token, module)?;
|
||||
writeln!(self.out, " {{")?;
|
||||
writeln!(self.out, "struct {} {{", decl.name.or_index(token))?;
|
||||
for (index, member) in decl.members.iter().enumerate() {
|
||||
write!(self.out, "\t")?;
|
||||
self.put_type_pre(&member.ty)?;
|
||||
write!(self.out, " ")?;
|
||||
self.put_struct_member_name(token, index, module)?;
|
||||
self.put_type_post(&member.ty)?;
|
||||
writeln!(self.out, ";")?;
|
||||
let name = member.name.or_index(MemberIndex(index));
|
||||
let tv = TypedVar(&member.ty, &name, &module.struct_declarations);
|
||||
writeln!(self.out, "\t{};", tv)?;
|
||||
}
|
||||
writeln!(self.out, "}};")?;
|
||||
}
|
||||
|
||||
let mut globals_used = Vec::new();
|
||||
writeln!(self.out, "")?;
|
||||
for (fun_token, fun) in module.functions.iter() {
|
||||
let fun_name = fun.name.or_index(fun_token);
|
||||
let fun_tv = TypedVar(&fun.return_type, &fun_name, &module.struct_declarations);
|
||||
writeln!(self.out, "{}(", fun_tv)?;
|
||||
for (index, ty) in fun.parameter_types.iter().enumerate() {
|
||||
let name = Name::Index(ParameterIndex(index));
|
||||
let tv = TypedVar(ty, &name, &module.struct_declarations);
|
||||
writeln!(self.out, "\t{},", tv)?;
|
||||
}
|
||||
for (_, expr) in fun.expressions.iter() {
|
||||
if let crate::Expression::GlobalVariable(token) = *expr {
|
||||
if !globals_used.contains(&token) {
|
||||
globals_used.push(token);
|
||||
let var = &module.global_variables[token];
|
||||
let name = var.name.or_index(token);
|
||||
let tv = TypedVar(&var.ty, &name, &module.struct_declarations);
|
||||
writeln!(self.out, "\t{},", tv)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
writeln!(self.out, ") {{")?;
|
||||
writeln!(self.out, "}}")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,12 +40,6 @@ pub enum ScalarKind {
|
||||
Float,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PointerDeclaration {
|
||||
pub base: Type,
|
||||
pub length: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct StructMember {
|
||||
pub name: Option<String>,
|
||||
|
||||
Reference in new issue
Block a user