Files
wgpu/src/proc/typifier.rs
T
João Capucho 0aae8ae6f8 glsl-out: More glsl versions (#191)
* Added support for glsl core version up to 400

* Added 330 to the list supported versions

* Added links to all extensions and fixed a extension being wrongfully activated

* Cleanup and check for image formats

* Removed useless extension and location on layout

* Added support for external functions and fixed global names

* Typefier allow Scalars in distance and length

* Addressed comments

* Fixed extension appearing after body

* Fixed entry point not being generated
2020-09-18 17:01:05 -04:00

357 lines
14 KiB
Rust

use crate::arena::{Arena, Handle};
use thiserror::Error;
#[derive(Debug, PartialEq)]
enum Resolution {
Handle(Handle<crate::Type>),
Value(crate::TypeInner),
}
// Clone is only implemented for numeric variants of `TypeInner`.
impl Clone for Resolution {
fn clone(&self) -> Self {
match *self {
Resolution::Handle(handle) => Resolution::Handle(handle),
Resolution::Value(ref v) => Resolution::Value(match *v {
crate::TypeInner::Scalar { kind, width } => {
crate::TypeInner::Scalar { kind, width }
}
crate::TypeInner::Vector { size, kind, width } => {
crate::TypeInner::Vector { size, kind, width }
}
crate::TypeInner::Matrix {
rows,
columns,
kind,
width,
} => crate::TypeInner::Matrix {
rows,
columns,
kind,
width,
},
_ => panic!("Unepxected clone type: {:?}", v),
}),
}
}
}
#[derive(Debug)]
pub struct Typifier {
resolutions: Vec<Resolution>,
}
#[derive(Clone, Debug, Error)]
pub enum ResolveError {
#[error("Invalid index into array")]
InvalidAccessIndex,
#[error("Function {name} not defined")]
FunctionNotDefined { name: String },
#[error("Function without return type")]
FunctionReturnsVoid,
#[error("Type is not found in the given immutable arena")]
TypeNotFound,
}
pub struct ResolveContext<'a> {
pub constants: &'a Arena<crate::Constant>,
pub global_vars: &'a Arena<crate::GlobalVariable>,
pub local_vars: &'a Arena<crate::LocalVariable>,
pub functions: &'a Arena<crate::Function>,
pub parameter_types: &'a [Handle<crate::Type>],
}
impl Typifier {
pub fn new() -> Self {
Typifier {
resolutions: Vec::new(),
}
}
pub fn clear(&mut self) {
self.resolutions.clear()
}
pub fn get<'a>(
&'a self,
expr_handle: Handle<crate::Expression>,
types: &'a Arena<crate::Type>,
) -> &'a crate::TypeInner {
match self.resolutions[expr_handle.index()] {
Resolution::Handle(ty_handle) => &types[ty_handle].inner,
Resolution::Value(ref inner) => inner,
}
}
fn resolve_impl(
&self,
expr: &crate::Expression,
types: &Arena<crate::Type>,
ctx: &ResolveContext,
) -> Result<Resolution, ResolveError> {
Ok(match *expr {
crate::Expression::Access { base, .. } => match *self.get(base, types) {
crate::TypeInner::Array { base, .. } => Resolution::Handle(base),
ref other => panic!("Can't access into {:?}", other),
},
crate::Expression::AccessIndex { base, index } => match *self.get(base, types) {
crate::TypeInner::Vector { size, kind, width } => {
if index >= size as u32 {
return Err(ResolveError::InvalidAccessIndex);
}
Resolution::Value(crate::TypeInner::Scalar { kind, width })
}
crate::TypeInner::Matrix {
columns,
rows,
kind,
width,
} => {
if index >= columns as u32 {
return Err(ResolveError::InvalidAccessIndex);
}
Resolution::Value(crate::TypeInner::Vector {
size: rows,
kind,
width,
})
}
crate::TypeInner::Array { base, .. } => Resolution::Handle(base),
crate::TypeInner::Struct { ref members } => {
let member = members
.get(index as usize)
.ok_or(ResolveError::InvalidAccessIndex)?;
Resolution::Handle(member.ty)
}
ref other => panic!("Can't access into {:?}", other),
},
crate::Expression::Constant(h) => Resolution::Handle(ctx.constants[h].ty),
crate::Expression::Compose { ty, .. } => Resolution::Handle(ty),
crate::Expression::FunctionParameter(index) => {
Resolution::Handle(ctx.parameter_types[index as usize])
}
crate::Expression::GlobalVariable(h) => Resolution::Handle(ctx.global_vars[h].ty),
crate::Expression::LocalVariable(h) => Resolution::Handle(ctx.local_vars[h].ty),
crate::Expression::Load { .. } => unimplemented!(),
crate::Expression::ImageSample { image, .. }
| crate::Expression::ImageLoad { image, .. } => match *self.get(image, types) {
crate::TypeInner::Image { class, .. } => Resolution::Value(match class {
crate::ImageClass::Depth => crate::TypeInner::Scalar {
kind: crate::ScalarKind::Float,
width: 4,
},
crate::ImageClass::Sampled { kind, multi: _ } => crate::TypeInner::Vector {
kind,
width: 4,
size: crate::VectorSize::Quad,
},
crate::ImageClass::Storage(format) => crate::TypeInner::Vector {
kind: format.into(),
width: 4,
size: crate::VectorSize::Quad,
},
}),
_ => unreachable!(),
},
crate::Expression::Unary { expr, .. } => self.resolutions[expr.index()].clone(),
crate::Expression::Binary { op, left, right } => match op {
crate::BinaryOperator::Add
| crate::BinaryOperator::Subtract
| crate::BinaryOperator::Divide
| crate::BinaryOperator::Modulo => self.resolutions[left.index()].clone(),
crate::BinaryOperator::Multiply => {
let ty_left = self.get(left, types);
let ty_right = self.get(right, types);
if ty_left == ty_right {
self.resolutions[left.index()].clone()
} else if let crate::TypeInner::Scalar { .. } = *ty_right {
self.resolutions[left.index()].clone()
} else {
match *ty_left {
crate::TypeInner::Scalar { .. } => {
self.resolutions[right.index()].clone()
}
crate::TypeInner::Matrix {
columns,
rows: _,
kind,
width,
} => Resolution::Value(crate::TypeInner::Vector {
size: columns,
kind,
width,
}),
_ => panic!("Incompatible arguments {:?} x {:?}", ty_left, ty_right),
}
}
}
crate::BinaryOperator::Equal
| crate::BinaryOperator::NotEqual
| crate::BinaryOperator::Less
| crate::BinaryOperator::LessEqual
| crate::BinaryOperator::Greater
| crate::BinaryOperator::GreaterEqual
| crate::BinaryOperator::LogicalAnd
| crate::BinaryOperator::LogicalOr => self.resolutions[left.index()].clone(),
crate::BinaryOperator::And
| crate::BinaryOperator::ExclusiveOr
| crate::BinaryOperator::InclusiveOr
| crate::BinaryOperator::ShiftLeftLogical
| crate::BinaryOperator::ShiftRightLogical
| crate::BinaryOperator::ShiftRightArithmetic => {
self.resolutions[left.index()].clone()
}
},
crate::Expression::Intrinsic { .. } => unimplemented!(),
crate::Expression::Transpose(expr) => match *self.get(expr, types) {
crate::TypeInner::Matrix {
columns,
rows,
kind,
width,
} => Resolution::Value(crate::TypeInner::Matrix {
columns: rows,
rows: columns,
kind,
width,
}),
ref other => panic!("incompatible transpose of {:?}", other),
},
crate::Expression::DotProduct(left_expr, _) => match *self.get(left_expr, types) {
crate::TypeInner::Vector {
kind,
size: _,
width,
} => Resolution::Value(crate::TypeInner::Scalar { kind, width }),
ref other => panic!("incompatible dot of {:?}", other),
},
crate::Expression::CrossProduct(_, _) => unimplemented!(),
crate::Expression::As {
expr,
kind,
convert: _,
} => match *self.get(expr, types) {
crate::TypeInner::Scalar { kind: _, width } => {
Resolution::Value(crate::TypeInner::Scalar { kind, width })
}
crate::TypeInner::Vector {
kind: _,
size,
width,
} => Resolution::Value(crate::TypeInner::Vector { kind, size, width }),
ref other => panic!("incompatible as of {:?}", other),
},
crate::Expression::Derivative { .. } => unimplemented!(),
crate::Expression::Call {
origin: crate::FunctionOrigin::External(ref name),
ref arguments,
} => match name.as_str() {
"distance" | "length" => match *self.get(arguments[0], types) {
crate::TypeInner::Vector { kind, width, .. }
| crate::TypeInner::Scalar { kind, width } => {
Resolution::Value(crate::TypeInner::Scalar { kind, width })
}
ref other => panic!("Unexpected argument {:?} on {}", other, name),
},
"dot" => match *self.get(arguments[0], types) {
crate::TypeInner::Vector { kind, width, .. } => {
Resolution::Value(crate::TypeInner::Scalar { kind, width })
}
ref other => panic!("Unexpected argument {:?} on {}", other, name),
},
"atan2" | "cos" | "sin" | "normalize" | "max" | "reflect" | "pow" | "clamp"
| "fclamp" | "mix" => self.resolutions[arguments[0].index()].clone(),
_ => return Err(ResolveError::FunctionNotDefined { name: name.clone() }),
},
crate::Expression::Call {
origin: crate::FunctionOrigin::Local(handle),
arguments: _,
} => {
let ty = ctx.functions[handle]
.return_type
.ok_or(ResolveError::FunctionReturnsVoid)?;
Resolution::Handle(ty)
}
crate::Expression::ArrayLength(_) => Resolution::Value(crate::TypeInner::Scalar {
kind: crate::ScalarKind::Uint,
width: 4,
}),
})
}
pub fn grow(
&mut self,
expr_handle: Handle<crate::Expression>,
expressions: &Arena<crate::Expression>,
types: &mut Arena<crate::Type>,
ctx: &ResolveContext,
) -> Result<(), ResolveError> {
if self.resolutions.len() <= expr_handle.index() {
for (eh, expr) in expressions.iter().skip(self.resolutions.len()) {
let resolution = self.resolve_impl(expr, types, ctx)?;
log::debug!("Resolving {:?} = {:?} : {:?}", eh, expr, resolution);
let ty_handle = match resolution {
Resolution::Handle(h) => h,
Resolution::Value(inner) => types
.fetch_if_or_append(crate::Type { name: None, inner }, |a, b| {
a.inner == b.inner
}),
};
self.resolutions.push(Resolution::Handle(ty_handle));
}
}
Ok(())
}
pub fn resolve_all(
&mut self,
expressions: &Arena<crate::Expression>,
types: &Arena<crate::Type>,
ctx: &ResolveContext,
) -> Result<(), ResolveError> {
self.clear();
for (_, expr) in expressions.iter() {
let resolution = self.resolve_impl(expr, types, ctx)?;
self.resolutions.push(resolution);
}
Ok(())
}
}
pub fn check_constant_type(inner: &crate::ConstantInner, type_inner: &crate::TypeInner) -> bool {
match (inner, type_inner) {
(
crate::ConstantInner::Sint(_),
crate::TypeInner::Scalar {
kind: crate::ScalarKind::Sint,
width: _,
},
) => true,
(
crate::ConstantInner::Uint(_),
crate::TypeInner::Scalar {
kind: crate::ScalarKind::Uint,
width: _,
},
) => true,
(
crate::ConstantInner::Float(_),
crate::TypeInner::Scalar {
kind: crate::ScalarKind::Float,
width: _,
},
) => true,
(
crate::ConstantInner::Bool(_),
crate::TypeInner::Scalar {
kind: crate::ScalarKind::Bool,
width: _,
},
) => true,
(crate::ConstantInner::Composite(_inner), _) => true, // TODO recursively check composite types
(_, _) => false,
}
}