| 1 |
use super::{compose::validate_compose, FunctionInfo, ModuleInfo, ShaderStages, TypeFlags}; |
| 2 |
use crate::arena::UniqueArena; |
| 3 |
use crate::{ |
| 4 |
arena::Handle, |
| 5 |
proc::OverloadSet as _, |
| 6 |
proc::{IndexableLengthError, ResolveError}, |
| 7 |
}; |
| 8 |
|
| 9 |
#[derive(Clone, Debug, thiserror::Error)] |
| 10 |
#[cfg_attr(test, derive(PartialEq))] |
| 11 |
pub enum ExpressionError { |
| 12 |
#[error("Used by a statement before it was introduced into the scope by any of the dominating blocks")] |
| 13 |
NotInScope, |
| 14 |
#[error("Base type {0:?} is not compatible with this expression")] |
| 15 |
InvalidBaseType(Handle<crate::Expression>), |
| 16 |
#[error("Accessing with index {0:?} can't be done")] |
| 17 |
InvalidIndexType(Handle<crate::Expression>), |
| 18 |
#[error("Accessing {0:?} via a negative index is invalid")] |
| 19 |
NegativeIndex(Handle<crate::Expression>), |
| 20 |
#[error("Accessing index {1} is out of {0:?} bounds")] |
| 21 |
IndexOutOfBounds(Handle<crate::Expression>, u32), |
| 22 |
#[error("Function argument {0:?} doesn't exist")] |
| 23 |
FunctionArgumentDoesntExist(u32), |
| 24 |
#[error("Loading of {0:?} can't be done")] |
| 25 |
InvalidPointerType(Handle<crate::Expression>), |
| 26 |
#[error("Array length of {0:?} can't be done")] |
| 27 |
InvalidArrayType(Handle<crate::Expression>), |
| 28 |
#[error("Get intersection of {0:?} can't be done")] |
| 29 |
InvalidRayQueryType(Handle<crate::Expression>), |
| 30 |
#[error("Splatting {0:?} can't be done")] |
| 31 |
InvalidSplatType(Handle<crate::Expression>), |
| 32 |
#[error("Swizzling {0:?} can't be done")] |
| 33 |
InvalidVectorType(Handle<crate::Expression>), |
| 34 |
#[error("Swizzle component {0:?} is outside of vector size {1:?}")] |
| 35 |
InvalidSwizzleComponent(crate::SwizzleComponent, crate::VectorSize), |
| 36 |
#[error(transparent)] |
| 37 |
Compose(#[from] super::ComposeError), |
| 38 |
#[error(transparent)] |
| 39 |
IndexableLength(#[from] IndexableLengthError), |
| 40 |
#[error("Operation {0:?} can't work with {1:?}")] |
| 41 |
InvalidUnaryOperandType(crate::UnaryOperator, Handle<crate::Expression>), |
| 42 |
#[error( |
| 43 |
"Operation {:?} can't work with {:?} (of type {:?}) and {:?} (of type {:?})", |
| 44 |
op, |
| 45 |
lhs_expr, |
| 46 |
lhs_type, |
| 47 |
rhs_expr, |
| 48 |
rhs_type |
| 49 |
)] |
| 50 |
InvalidBinaryOperandTypes { |
| 51 |
op: crate::BinaryOperator, |
| 52 |
lhs_expr: Handle<crate::Expression>, |
| 53 |
lhs_type: crate::TypeInner, |
| 54 |
rhs_expr: Handle<crate::Expression>, |
| 55 |
rhs_type: crate::TypeInner, |
| 56 |
}, |
| 57 |
#[error("Expected selection argument types to match, but reject value of type {reject:?} does not match accept value of value {accept:?}")] |
| 58 |
SelectValuesTypeMismatch { |
| 59 |
accept: crate::TypeInner, |
| 60 |
reject: crate::TypeInner, |
| 61 |
}, |
| 62 |
#[error("Expected selection condition to be a boolean value, got {actual:?}")] |
| 63 |
SelectConditionNotABool { actual: crate::TypeInner }, |
| 64 |
#[error("Relational argument {0:?} is not a boolean vector")] |
| 65 |
InvalidBooleanVector(Handle<crate::Expression>), |
| 66 |
#[error("Relational argument {0:?} is not a float")] |
| 67 |
InvalidFloatArgument(Handle<crate::Expression>), |
| 68 |
#[error("Type resolution failed")] |
| 69 |
Type(#[from] ResolveError), |
| 70 |
#[error("Not a global variable")] |
| 71 |
ExpectedGlobalVariable, |
| 72 |
#[error("Not a global variable or a function argument")] |
| 73 |
ExpectedGlobalOrArgument, |
| 74 |
#[error("Needs to be an binding array instead of {0:?}")] |
| 75 |
ExpectedBindingArrayType(Handle<crate::Type>), |
| 76 |
#[error("Needs to be an image instead of {0:?}")] |
| 77 |
ExpectedImageType(Handle<crate::Type>), |
| 78 |
#[error("Needs to be an image instead of {0:?}")] |
| 79 |
ExpectedSamplerType(Handle<crate::Type>), |
| 80 |
#[error("Unable to operate on image class {0:?}")] |
| 81 |
InvalidImageClass(crate::ImageClass), |
| 82 |
#[error("Image atomics are not supported for storage format {0:?}")] |
| 83 |
InvalidImageFormat(crate::StorageFormat), |
| 84 |
#[error("Image atomics require atomic storage access, {0:?} is insufficient")] |
| 85 |
InvalidImageStorageAccess(crate::StorageAccess), |
| 86 |
#[error("Derivatives can only be taken from scalar and vector floats")] |
| 87 |
InvalidDerivative, |
| 88 |
#[error("Image array index parameter is misplaced")] |
| 89 |
InvalidImageArrayIndex, |
| 90 |
#[error("Inappropriate sample or level-of-detail index for texel access")] |
| 91 |
InvalidImageOtherIndex, |
| 92 |
#[error("Image array index type of {0:?} is not an integer scalar")] |
| 93 |
InvalidImageArrayIndexType(Handle<crate::Expression>), |
| 94 |
#[error("Image sample or level-of-detail index's type of {0:?} is not an integer scalar")] |
| 95 |
InvalidImageOtherIndexType(Handle<crate::Expression>), |
| 96 |
#[error("Image coordinate type of {1:?} does not match dimension {0:?}")] |
| 97 |
InvalidImageCoordinateType(crate::ImageDimension, Handle<crate::Expression>), |
| 98 |
#[error("Comparison sampling mismatch: image has class {image:?}, but the sampler is comparison={sampler}, and the reference was provided={has_ref}")] |
| 99 |
ComparisonSamplingMismatch { |
| 100 |
image: crate::ImageClass, |
| 101 |
sampler: bool, |
| 102 |
has_ref: bool, |
| 103 |
}, |
| 104 |
#[error("Sample offset must be a const-expression")] |
| 105 |
InvalidSampleOffsetExprType, |
| 106 |
#[error("Sample offset constant {1:?} doesn't match the image dimension {0:?}")] |
| 107 |
InvalidSampleOffset(crate::ImageDimension, Handle<crate::Expression>), |
| 108 |
#[error("Depth reference {0:?} is not a scalar float")] |
| 109 |
InvalidDepthReference(Handle<crate::Expression>), |
| 110 |
#[error("Depth sample level can only be Auto or Zero")] |
| 111 |
InvalidDepthSampleLevel, |
| 112 |
#[error("Gather level can only be Zero")] |
| 113 |
InvalidGatherLevel, |
| 114 |
#[error("Gather component {0:?} doesn't exist in the image")] |
| 115 |
InvalidGatherComponent(crate::SwizzleComponent), |
| 116 |
#[error("Gather can't be done for image dimension {0:?}")] |
| 117 |
InvalidGatherDimension(crate::ImageDimension), |
| 118 |
#[error("Sample level (exact) type {0:?} has an invalid type")] |
| 119 |
InvalidSampleLevelExactType(Handle<crate::Expression>), |
| 120 |
#[error("Sample level (bias) type {0:?} is not a scalar float")] |
| 121 |
InvalidSampleLevelBiasType(Handle<crate::Expression>), |
| 122 |
#[error("Bias can't be done for image dimension {0:?}")] |
| 123 |
InvalidSampleLevelBiasDimension(crate::ImageDimension), |
| 124 |
#[error("Sample level (gradient) of {1:?} doesn't match the image dimension {0:?}")] |
| 125 |
InvalidSampleLevelGradientType(crate::ImageDimension, Handle<crate::Expression>), |
| 126 |
#[error("Unable to cast")] |
| 127 |
InvalidCastArgument, |
| 128 |
#[error("Invalid argument count for {0:?}")] |
| 129 |
WrongArgumentCount(crate::MathFunction), |
| 130 |
#[error("Argument [{1}] to {0:?} as expression {2:?} has an invalid type.")] |
| 131 |
InvalidArgumentType(crate::MathFunction, u32, Handle<crate::Expression>), |
| 132 |
#[error( |
| 133 |
"workgroupUniformLoad result type can't be {0:?}. It can only be a constructible type." |
| 134 |
)] |
| 135 |
InvalidWorkGroupUniformLoadResultType(Handle<crate::Type>), |
| 136 |
#[error("Shader requires capability {0:?}")] |
| 137 |
MissingCapabilities(super::Capabilities), |
| 138 |
#[error(transparent)] |
| 139 |
Literal(#[from] LiteralError), |
| 140 |
#[error("{0:?} is not supported for Width {2} {1:?} arguments yet, see https://github.com/gfx-rs/wgpu/issues/5276")] |
| 141 |
UnsupportedWidth(crate::MathFunction, crate::ScalarKind, crate::Bytes), |
| 142 |
} |
| 143 |
|
| 144 |
#[derive(Clone, Debug, thiserror::Error)] |
| 145 |
#[cfg_attr(test, derive(PartialEq))] |
| 146 |
pub enum ConstExpressionError { |
| 147 |
#[error("The expression is not a constant or override expression")] |
| 148 |
NonConstOrOverride, |
| 149 |
#[error("The expression is not a fully evaluated constant expression")] |
| 150 |
NonFullyEvaluatedConst, |
| 151 |
#[error(transparent)] |
| 152 |
Compose(#[from] super::ComposeError), |
| 153 |
#[error("Splatting {0:?} can't be done")] |
| 154 |
InvalidSplatType(Handle<crate::Expression>), |
| 155 |
#[error("Type resolution failed")] |
| 156 |
Type(#[from] ResolveError), |
| 157 |
#[error(transparent)] |
| 158 |
Literal(#[from] LiteralError), |
| 159 |
#[error(transparent)] |
| 160 |
Width(#[from] super::r#type::WidthError), |
| 161 |
} |
| 162 |
|
| 163 |
#[derive(Clone, Debug, thiserror::Error)] |
| 164 |
#[cfg_attr(test, derive(PartialEq))] |
| 165 |
pub enum LiteralError { |
| 166 |
#[error("Float literal is NaN")] |
| 167 |
NaN, |
| 168 |
#[error("Float literal is infinite")] |
| 169 |
Infinity, |
| 170 |
#[error(transparent)] |
| 171 |
Width(#[from] super::r#type::WidthError), |
| 172 |
} |
| 173 |
|
| 174 |
struct ExpressionTypeResolver<'a> { |
| 175 |
root: Handle<crate::Expression>, |
| 176 |
types: &'a UniqueArena<crate::Type>, |
| 177 |
info: &'a FunctionInfo, |
| 178 |
} |
| 179 |
|
| 180 |
impl core::ops::Index<Handle<crate::Expression>> for ExpressionTypeResolver<'_> { |
| 181 |
type Output = crate::TypeInner; |
| 182 |
|
| 183 |
#[allow(clippy::panic)] |
| 184 |
fn index(&self, handle: Handle<crate::Expression>) -> &Self::Output { |
| 185 |
if handle < self.root { |
| 186 |
self.info[handle].ty.inner_with(self.types) |
| 187 |
} else { |
| 188 |
// `Validator::validate_module_handles` should have caught this. |
| 189 |
panic!( |
| 190 |
"Depends on {:?}, which has not been processed yet", |
| 191 |
self.root |
| 192 |
) |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
impl super::Validator { |
| 198 |
pub(super) fn validate_const_expression( |
| 199 |
&self, |
| 200 |
handle: Handle<crate::Expression>, |
| 201 |
gctx: crate::proc::GlobalCtx, |
| 202 |
mod_info: &ModuleInfo, |
| 203 |
global_expr_kind: &crate::proc::ExpressionKindTracker, |
| 204 |
) -> Result<(), ConstExpressionError> { |
| 205 |
use crate::Expression as E; |
| 206 |
|
| 207 |
if !global_expr_kind.is_const_or_override(handle) { |
| 208 |
return Err(ConstExpressionError::NonConstOrOverride); |
| 209 |
} |
| 210 |
|
| 211 |
match gctx.global_expressions[handle] { |
| 212 |
E::Literal(literal) => { |
| 213 |
self.validate_literal(literal)?; |
| 214 |
} |
| 215 |
E::Constant(_) | E::ZeroValue(_) => {} |
| 216 |
E::Compose { ref components, ty } => { |
| 217 |
validate_compose( |
| 218 |
ty, |
| 219 |
gctx, |
| 220 |
components.iter().map(|&handle| mod_info[handle].clone()), |
| 221 |
)?; |
| 222 |
} |
| 223 |
E::Splat { value, .. } => match *mod_info[value].inner_with(gctx.types) { |
| 224 |
crate::TypeInner::Scalar { .. } => {} |
| 225 |
_ => return Err(ConstExpressionError::InvalidSplatType(value)), |
| 226 |
}, |
| 227 |
_ if global_expr_kind.is_const(handle) || self.overrides_resolved => { |
| 228 |
return Err(ConstExpressionError::NonFullyEvaluatedConst) |
| 229 |
} |
| 230 |
// the constant evaluator will report errors about override-expressions |
| 231 |
_ => {} |
| 232 |
} |
| 233 |
|
| 234 |
Ok(()) |
| 235 |
} |
| 236 |
|
| 237 |
#[allow(clippy::too_many_arguments)] |
| 238 |
pub(super) fn validate_expression( |
| 239 |
&self, |
| 240 |
root: Handle<crate::Expression>, |
| 241 |
expression: &crate::Expression, |
| 242 |
function: &crate::Function, |
| 243 |
module: &crate::Module, |
| 244 |
info: &FunctionInfo, |
| 245 |
mod_info: &ModuleInfo, |
| 246 |
expr_kind: &crate::proc::ExpressionKindTracker, |
| 247 |
) -> Result<ShaderStages, ExpressionError> { |
| 248 |
use crate::{Expression as E, Scalar as Sc, ScalarKind as Sk, TypeInner as Ti}; |
| 249 |
|
| 250 |
let resolver = ExpressionTypeResolver { |
| 251 |
root, |
| 252 |
types: &module.types, |
| 253 |
info, |
| 254 |
}; |
| 255 |
|
| 256 |
let stages = match *expression { |
| 257 |
E::Access { base, index } => { |
| 258 |
let base_type = &resolver[base]; |
| 259 |
match *base_type { |
| 260 |
Ti::Matrix { .. } |
| 261 |
| Ti::Vector { .. } |
| 262 |
| Ti::Array { .. } |
| 263 |
| Ti::Pointer { .. } |
| 264 |
| Ti::ValuePointer { size: Some(_), .. } |
| 265 |
| Ti::BindingArray { .. } => {} |
| 266 |
ref other => { |
| 267 |
log::error!("Indexing of {:?}", other); |
| 268 |
return Err(ExpressionError::InvalidBaseType(base)); |
| 269 |
} |
| 270 |
}; |
| 271 |
match resolver[index] { |
| 272 |
//TODO: only allow one of these |
| 273 |
Ti::Scalar(Sc { |
| 274 |
kind: Sk::Sint | Sk::Uint, |
| 275 |
.. |
| 276 |
}) => {} |
| 277 |
ref other => { |
| 278 |
log::error!("Indexing by {:?}", other); |
| 279 |
return Err(ExpressionError::InvalidIndexType(index)); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// If index is const we can do check for non-negative index |
| 284 |
match module |
| 285 |
.to_ctx() |
| 286 |
.eval_expr_to_u32_from(index, &function.expressions) |
| 287 |
{ |
| 288 |
Ok(value) => { |
| 289 |
let length = if self.overrides_resolved { |
| 290 |
base_type.indexable_length_resolved(module) |
| 291 |
} else { |
| 292 |
base_type.indexable_length_pending(module) |
| 293 |
}?; |
| 294 |
// If we know both the length and the index, we can do the |
| 295 |
// bounds check now. |
| 296 |
if let crate::proc::IndexableLength::Known(known_length) = length { |
| 297 |
if value >= known_length { |
| 298 |
return Err(ExpressionError::IndexOutOfBounds(base, value)); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
Err(crate::proc::U32EvalError::Negative) => { |
| 303 |
return Err(ExpressionError::NegativeIndex(base)) |
| 304 |
} |
| 305 |
Err(crate::proc::U32EvalError::NonConst) => {} |
| 306 |
} |
| 307 |
|
| 308 |
ShaderStages::all() |
| 309 |
} |
| 310 |
E::AccessIndex { base, index } => { |
| 311 |
fn resolve_index_limit( |
| 312 |
module: &crate::Module, |
| 313 |
top: Handle<crate::Expression>, |
| 314 |
ty: &crate::TypeInner, |
| 315 |
top_level: bool, |
| 316 |
) -> Result<u32, ExpressionError> { |
| 317 |
let limit = match *ty { |
| 318 |
Ti::Vector { size, .. } |
| 319 |
| Ti::ValuePointer { |
| 320 |
size: Some(size), .. |
| 321 |
} => size as u32, |
| 322 |
Ti::Matrix { columns, .. } => columns as u32, |
| 323 |
Ti::Array { |
| 324 |
size: crate::ArraySize::Constant(len), |
| 325 |
.. |
| 326 |
} => len.get(), |
| 327 |
Ti::Array { .. } | Ti::BindingArray { .. } => u32::MAX, // can't statically know, but need run-time checks |
| 328 |
Ti::Pointer { base, .. } if top_level => { |
| 329 |
resolve_index_limit(module, top, &module.types[base].inner, false)? |
| 330 |
} |
| 331 |
Ti::Struct { ref members, .. } => members.len() as u32, |
| 332 |
ref other => { |
| 333 |
log::error!("Indexing of {:?}", other); |
| 334 |
return Err(ExpressionError::InvalidBaseType(top)); |
| 335 |
} |
| 336 |
}; |
| 337 |
Ok(limit) |
| 338 |
} |
| 339 |
|
| 340 |
let limit = resolve_index_limit(module, base, &resolver[base], true)?; |
| 341 |
if index >= limit { |
| 342 |
return Err(ExpressionError::IndexOutOfBounds(base, limit)); |
| 343 |
} |
| 344 |
ShaderStages::all() |
| 345 |
} |
| 346 |
E::Splat { size: _, value } => match resolver[value] { |
| 347 |
Ti::Scalar { .. } => ShaderStages::all(), |
| 348 |
ref other => { |
| 349 |
log::error!("Splat scalar type {:?}", other); |
| 350 |
return Err(ExpressionError::InvalidSplatType(value)); |
| 351 |
} |
| 352 |
}, |
| 353 |
E::Swizzle { |
| 354 |
size, |
| 355 |
vector, |
| 356 |
pattern, |
| 357 |
} => { |
| 358 |
let vec_size = match resolver[vector] { |
| 359 |
Ti::Vector { size: vec_size, .. } => vec_size, |
| 360 |
ref other => { |
| 361 |
log::error!("Swizzle vector type {:?}", other); |
| 362 |
return Err(ExpressionError::InvalidVectorType(vector)); |
| 363 |
} |
| 364 |
}; |
| 365 |
for &sc in pattern[..size as usize].iter() { |
| 366 |
if sc as u8 >= vec_size as u8 { |
| 367 |
return Err(ExpressionError::InvalidSwizzleComponent(sc, vec_size)); |
| 368 |
} |
| 369 |
} |
| 370 |
ShaderStages::all() |
| 371 |
} |
| 372 |
E::Literal(literal) => { |
| 373 |
self.validate_literal(literal)?; |
| 374 |
ShaderStages::all() |
| 375 |
} |
| 376 |
E::Constant(_) | E::Override(_) | E::ZeroValue(_) => ShaderStages::all(), |
| 377 |
E::Compose { ref components, ty } => { |
| 378 |
validate_compose( |
| 379 |
ty, |
| 380 |
module.to_ctx(), |
| 381 |
components.iter().map(|&handle| info[handle].ty.clone()), |
| 382 |
)?; |
| 383 |
ShaderStages::all() |
| 384 |
} |
| 385 |
E::FunctionArgument(index) => { |
| 386 |
if index >= function.arguments.len() as u32 { |
| 387 |
return Err(ExpressionError::FunctionArgumentDoesntExist(index)); |
| 388 |
} |
| 389 |
ShaderStages::all() |
| 390 |
} |
| 391 |
E::GlobalVariable(_handle) => ShaderStages::all(), |
| 392 |
E::LocalVariable(_handle) => ShaderStages::all(), |
| 393 |
E::Load { pointer } => { |
| 394 |
match resolver[pointer] { |
| 395 |
Ti::Pointer { base, .. } |
| 396 |
if self.types[base.index()] |
| 397 |
.flags |
| 398 |
.contains(TypeFlags::SIZED | TypeFlags::DATA) => {} |
| 399 |
Ti::ValuePointer { .. } => {} |
| 400 |
ref other => { |
| 401 |
log::error!("Loading {:?}", other); |
| 402 |
return Err(ExpressionError::InvalidPointerType(pointer)); |
| 403 |
} |
| 404 |
} |
| 405 |
ShaderStages::all() |
| 406 |
} |
| 407 |
E::ImageSample { |
| 408 |
image, |
| 409 |
sampler, |
| 410 |
gather, |
| 411 |
coordinate, |
| 412 |
array_index, |
| 413 |
offset, |
| 414 |
level, |
| 415 |
depth_ref, |
| 416 |
} => { |
| 417 |
// check the validity of expressions |
| 418 |
let image_ty = Self::global_var_ty(module, function, image)?; |
| 419 |
let sampler_ty = Self::global_var_ty(module, function, sampler)?; |
| 420 |
|
| 421 |
let comparison = match module.types[sampler_ty].inner { |
| 422 |
Ti::Sampler { comparison } => comparison, |
| 423 |
_ => return Err(ExpressionError::ExpectedSamplerType(sampler_ty)), |
| 424 |
}; |
| 425 |
|
| 426 |
let (class, dim) = match module.types[image_ty].inner { |
| 427 |
Ti::Image { |
| 428 |
class, |
| 429 |
arrayed, |
| 430 |
dim, |
| 431 |
} => { |
| 432 |
// check the array property |
| 433 |
if arrayed != array_index.is_some() { |
| 434 |
return Err(ExpressionError::InvalidImageArrayIndex); |
| 435 |
} |
| 436 |
if let Some(expr) = array_index { |
| 437 |
match resolver[expr] { |
| 438 |
Ti::Scalar(Sc { |
| 439 |
kind: Sk::Sint | Sk::Uint, |
| 440 |
.. |
| 441 |
}) => {} |
| 442 |
_ => return Err(ExpressionError::InvalidImageArrayIndexType(expr)), |
| 443 |
} |
| 444 |
} |
| 445 |
(class, dim) |
| 446 |
} |
| 447 |
_ => return Err(ExpressionError::ExpectedImageType(image_ty)), |
| 448 |
}; |
| 449 |
|
| 450 |
// check sampling and comparison properties |
| 451 |
let image_depth = match class { |
| 452 |
crate::ImageClass::Sampled { |
| 453 |
kind: crate::ScalarKind::Float, |
| 454 |
multi: false, |
| 455 |
} => false, |
| 456 |
crate::ImageClass::Sampled { |
| 457 |
kind: crate::ScalarKind::Uint | crate::ScalarKind::Sint, |
| 458 |
multi: false, |
| 459 |
} if gather.is_some() => false, |
| 460 |
crate::ImageClass::Depth { multi: false } => true, |
| 461 |
_ => return Err(ExpressionError::InvalidImageClass(class)), |
| 462 |
}; |
| 463 |
if comparison != depth_ref.is_some() || (comparison && !image_depth) { |
| 464 |
return Err(ExpressionError::ComparisonSamplingMismatch { |
| 465 |
image: class, |
| 466 |
sampler: comparison, |
| 467 |
has_ref: depth_ref.is_some(), |
| 468 |
}); |
| 469 |
} |
| 470 |
|
| 471 |
// check texture coordinates type |
| 472 |
let num_components = match dim { |
| 473 |
crate::ImageDimension::D1 => 1, |
| 474 |
crate::ImageDimension::D2 => 2, |
| 475 |
crate::ImageDimension::D3 | crate::ImageDimension::Cube => 3, |
| 476 |
}; |
| 477 |
match resolver[coordinate] { |
| 478 |
Ti::Scalar(Sc { |
| 479 |
kind: Sk::Float, .. |
| 480 |
}) if num_components == 1 => {} |
| 481 |
Ti::Vector { |
| 482 |
size, |
| 483 |
scalar: |
| 484 |
Sc { |
| 485 |
kind: Sk::Float, .. |
| 486 |
}, |
| 487 |
} if size as u32 == num_components => {} |
| 488 |
_ => return Err(ExpressionError::InvalidImageCoordinateType(dim, coordinate)), |
| 489 |
} |
| 490 |
|
| 491 |
// check constant offset |
| 492 |
if let Some(const_expr) = offset { |
| 493 |
if !expr_kind.is_const(const_expr) { |
| 494 |
return Err(ExpressionError::InvalidSampleOffsetExprType); |
| 495 |
} |
| 496 |
|
| 497 |
match resolver[const_expr] { |
| 498 |
Ti::Scalar(Sc { kind: Sk::Sint, .. }) if num_components == 1 => {} |
| 499 |
Ti::Vector { |
| 500 |
size, |
| 501 |
scalar: Sc { kind: Sk::Sint, .. }, |
| 502 |
} if size as u32 == num_components => {} |
| 503 |
_ => { |
| 504 |
return Err(ExpressionError::InvalidSampleOffset(dim, const_expr)); |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
// check depth reference type |
| 510 |
if let Some(expr) = depth_ref { |
| 511 |
match resolver[expr] { |
| 512 |
Ti::Scalar(Sc { |
| 513 |
kind: Sk::Float, .. |
| 514 |
}) => {} |
| 515 |
_ => return Err(ExpressionError::InvalidDepthReference(expr)), |
| 516 |
} |
| 517 |
match level { |
| 518 |
crate::SampleLevel::Auto | crate::SampleLevel::Zero => {} |
| 519 |
_ => return Err(ExpressionError::InvalidDepthSampleLevel), |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
if let Some(component) = gather { |
| 524 |
match dim { |
| 525 |
crate::ImageDimension::D2 | crate::ImageDimension::Cube => {} |
| 526 |
crate::ImageDimension::D1 | crate::ImageDimension::D3 => { |
| 527 |
return Err(ExpressionError::InvalidGatherDimension(dim)) |
| 528 |
} |
| 529 |
}; |
| 530 |
let max_component = match class { |
| 531 |
crate::ImageClass::Depth { .. } => crate::SwizzleComponent::X, |
| 532 |
_ => crate::SwizzleComponent::W, |
| 533 |
}; |
| 534 |
if component > max_component { |
| 535 |
return Err(ExpressionError::InvalidGatherComponent(component)); |
| 536 |
} |
| 537 |
match level { |
| 538 |
crate::SampleLevel::Zero => {} |
| 539 |
_ => return Err(ExpressionError::InvalidGatherLevel), |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
// check level properties |
| 544 |
match level { |
| 545 |
crate::SampleLevel::Auto => ShaderStages::FRAGMENT, |
| 546 |
crate::SampleLevel::Zero => ShaderStages::all(), |
| 547 |
crate::SampleLevel::Exact(expr) => { |
| 548 |
match class { |
| 549 |
crate::ImageClass::Depth { .. } => match resolver[expr] { |
| 550 |
Ti::Scalar(Sc { |
| 551 |
kind: Sk::Sint | Sk::Uint, |
| 552 |
.. |
| 553 |
}) => {} |
| 554 |
_ => { |
| 555 |
return Err(ExpressionError::InvalidSampleLevelExactType(expr)) |
| 556 |
} |
| 557 |
}, |
| 558 |
_ => match resolver[expr] { |
| 559 |
Ti::Scalar(Sc { |
| 560 |
kind: Sk::Float, .. |
| 561 |
}) => {} |
| 562 |
_ => { |
| 563 |
return Err(ExpressionError::InvalidSampleLevelExactType(expr)) |
| 564 |
} |
| 565 |
}, |
| 566 |
} |
| 567 |
ShaderStages::all() |
| 568 |
} |
| 569 |
crate::SampleLevel::Bias(expr) => { |
| 570 |
match resolver[expr] { |
| 571 |
Ti::Scalar(Sc { |
| 572 |
kind: Sk::Float, .. |
| 573 |
}) => {} |
| 574 |
_ => return Err(ExpressionError::InvalidSampleLevelBiasType(expr)), |
| 575 |
} |
| 576 |
match class { |
| 577 |
crate::ImageClass::Sampled { |
| 578 |
kind: Sk::Float, |
| 579 |
multi: false, |
| 580 |
} => { |
| 581 |
if dim == crate::ImageDimension::D1 { |
| 582 |
return Err(ExpressionError::InvalidSampleLevelBiasDimension( |
| 583 |
dim, |
| 584 |
)); |
| 585 |
} |
| 586 |
} |
| 587 |
_ => return Err(ExpressionError::InvalidImageClass(class)), |
| 588 |
} |
| 589 |
ShaderStages::FRAGMENT |
| 590 |
} |
| 591 |
crate::SampleLevel::Gradient { x, y } => { |
| 592 |
match resolver[x] { |
| 593 |
Ti::Scalar(Sc { |
| 594 |
kind: Sk::Float, .. |
| 595 |
}) if num_components == 1 => {} |
| 596 |
Ti::Vector { |
| 597 |
size, |
| 598 |
scalar: |
| 599 |
Sc { |
| 600 |
kind: Sk::Float, .. |
| 601 |
}, |
| 602 |
} if size as u32 == num_components => {} |
| 603 |
_ => { |
| 604 |
return Err(ExpressionError::InvalidSampleLevelGradientType(dim, x)) |
| 605 |
} |
| 606 |
} |
| 607 |
match resolver[y] { |
| 608 |
Ti::Scalar(Sc { |
| 609 |
kind: Sk::Float, .. |
| 610 |
}) if num_components == 1 => {} |
| 611 |
Ti::Vector { |
| 612 |
size, |
| 613 |
scalar: |
| 614 |
Sc { |
| 615 |
kind: Sk::Float, .. |
| 616 |
}, |
| 617 |
} if size as u32 == num_components => {} |
| 618 |
_ => { |
| 619 |
return Err(ExpressionError::InvalidSampleLevelGradientType(dim, y)) |
| 620 |
} |
| 621 |
} |
| 622 |
ShaderStages::all() |
| 623 |
} |
| 624 |
} |
| 625 |
} |
| 626 |
E::ImageLoad { |
| 627 |
image, |
| 628 |
coordinate, |
| 629 |
array_index, |
| 630 |
sample, |
| 631 |
level, |
| 632 |
} => { |
| 633 |
let ty = Self::global_var_ty(module, function, image)?; |
| 634 |
let Ti::Image { |
| 635 |
class, |
| 636 |
arrayed, |
| 637 |
dim, |
| 638 |
} = module.types[ty].inner |
| 639 |
else { |
| 640 |
return Err(ExpressionError::ExpectedImageType(ty)); |
| 641 |
}; |
| 642 |
|
| 643 |
match resolver[coordinate].image_storage_coordinates() { |
| 644 |
Some(coord_dim) if coord_dim == dim => {} |
| 645 |
_ => return Err(ExpressionError::InvalidImageCoordinateType(dim, coordinate)), |
| 646 |
}; |
| 647 |
if arrayed != array_index.is_some() { |
| 648 |
return Err(ExpressionError::InvalidImageArrayIndex); |
| 649 |
} |
| 650 |
if let Some(expr) = array_index { |
| 651 |
if !matches!(resolver[expr], Ti::Scalar(Sc::I32 | Sc::U32)) { |
| 652 |
return Err(ExpressionError::InvalidImageArrayIndexType(expr)); |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
match (sample, class.is_multisampled()) { |
| 657 |
(None, false) => {} |
| 658 |
(Some(sample), true) => { |
| 659 |
if !matches!(resolver[sample], Ti::Scalar(Sc::I32 | Sc::U32)) { |
| 660 |
return Err(ExpressionError::InvalidImageOtherIndexType(sample)); |
| 661 |
} |
| 662 |
} |
| 663 |
_ => { |
| 664 |
return Err(ExpressionError::InvalidImageOtherIndex); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
match (level, class.is_mipmapped()) { |
| 669 |
(None, false) => {} |
| 670 |
(Some(level), true) => match resolver[level] { |
| 671 |
Ti::Scalar(Sc { |
| 672 |
kind: Sk::Sint | Sk::Uint, |
| 673 |
width: _, |
| 674 |
}) => {} |
| 675 |
_ => return Err(ExpressionError::InvalidImageArrayIndexType(level)), |
| 676 |
}, |
| 677 |
_ => { |
| 678 |
return Err(ExpressionError::InvalidImageOtherIndex); |
| 679 |
} |
| 680 |
} |
| 681 |
ShaderStages::all() |
| 682 |
} |
| 683 |
E::ImageQuery { image, query } => { |
| 684 |
let ty = Self::global_var_ty(module, function, image)?; |
| 685 |
match module.types[ty].inner { |
| 686 |
Ti::Image { class, arrayed, .. } => { |
| 687 |
let good = match query { |
| 688 |
crate::ImageQuery::NumLayers => arrayed, |
| 689 |
crate::ImageQuery::Size { level: None } => true, |
| 690 |
crate::ImageQuery::Size { level: Some(level) } => { |
| 691 |
match resolver[level] { |
| 692 |
Ti::Scalar(Sc::I32 | Sc::U32) => {} |
| 693 |
_ => { |
| 694 |
return Err(ExpressionError::InvalidImageOtherIndexType( |
| 695 |
level, |
| 696 |
)) |
| 697 |
} |
| 698 |
} |
| 699 |
class.is_mipmapped() |
| 700 |
} |
| 701 |
crate::ImageQuery::NumLevels => class.is_mipmapped(), |
| 702 |
crate::ImageQuery::NumSamples => class.is_multisampled(), |
| 703 |
}; |
| 704 |
if !good { |
| 705 |
return Err(ExpressionError::InvalidImageClass(class)); |
| 706 |
} |
| 707 |
} |
| 708 |
_ => return Err(ExpressionError::ExpectedImageType(ty)), |
| 709 |
} |
| 710 |
ShaderStages::all() |
| 711 |
} |
| 712 |
E::Unary { op, expr } => { |
| 713 |
use crate::UnaryOperator as Uo; |
| 714 |
let inner = &resolver[expr]; |
| 715 |
match (op, inner.scalar_kind()) { |
| 716 |
(Uo::Negate, Some(Sk::Float | Sk::Sint)) |
| 717 |
| (Uo::LogicalNot, Some(Sk::Bool)) |
| 718 |
| (Uo::BitwiseNot, Some(Sk::Sint | Sk::Uint)) => {} |
| 719 |
other => { |
| 720 |
log::error!("Op {:?} kind {:?}", op, other); |
| 721 |
return Err(ExpressionError::InvalidUnaryOperandType(op, expr)); |
| 722 |
} |
| 723 |
} |
| 724 |
ShaderStages::all() |
| 725 |
} |
| 726 |
E::Binary { op, left, right } => { |
| 727 |
use crate::BinaryOperator as Bo; |
| 728 |
let left_inner = &resolver[left]; |
| 729 |
let right_inner = &resolver[right]; |
| 730 |
let good = match op { |
| 731 |
Bo::Add | Bo::Subtract => match *left_inner { |
| 732 |
Ti::Scalar(scalar) | Ti::Vector { scalar, .. } => match scalar.kind { |
| 733 |
Sk::Uint | Sk::Sint | Sk::Float => left_inner == right_inner, |
| 734 |
Sk::Bool | Sk::AbstractInt | Sk::AbstractFloat => false, |
| 735 |
}, |
| 736 |
Ti::Matrix { .. } => left_inner == right_inner, |
| 737 |
_ => false, |
| 738 |
}, |
| 739 |
Bo::Divide | Bo::Modulo => match *left_inner { |
| 740 |
Ti::Scalar(scalar) | Ti::Vector { scalar, .. } => match scalar.kind { |
| 741 |
Sk::Uint | Sk::Sint | Sk::Float => left_inner == right_inner, |
| 742 |
Sk::Bool | Sk::AbstractInt | Sk::AbstractFloat => false, |
| 743 |
}, |
| 744 |
_ => false, |
| 745 |
}, |
| 746 |
Bo::Multiply => { |
| 747 |
let kind_allowed = match left_inner.scalar_kind() { |
| 748 |
Some(Sk::Uint | Sk::Sint | Sk::Float) => true, |
| 749 |
Some(Sk::Bool | Sk::AbstractInt | Sk::AbstractFloat) | None => false, |
| 750 |
}; |
| 751 |
let types_match = match (left_inner, right_inner) { |
| 752 |
// Straight scalar and mixed scalar/vector. |
| 753 |
(&Ti::Scalar(scalar1), &Ti::Scalar(scalar2)) |
| 754 |
| ( |
| 755 |
&Ti::Vector { |
| 756 |
scalar: scalar1, .. |
| 757 |
}, |
| 758 |
&Ti::Scalar(scalar2), |
| 759 |
) |
| 760 |
| ( |
| 761 |
&Ti::Scalar(scalar1), |
| 762 |
&Ti::Vector { |
| 763 |
scalar: scalar2, .. |
| 764 |
}, |
| 765 |
) => scalar1 == scalar2, |
| 766 |
// Scalar/matrix. |
| 767 |
( |
| 768 |
&Ti::Scalar(Sc { |
| 769 |
kind: Sk::Float, .. |
| 770 |
}), |
| 771 |
&Ti::Matrix { .. }, |
| 772 |
) |
| 773 |
| ( |
| 774 |
&Ti::Matrix { .. }, |
| 775 |
&Ti::Scalar(Sc { |
| 776 |
kind: Sk::Float, .. |
| 777 |
}), |
| 778 |
) => true, |
| 779 |
// Vector/vector. |
| 780 |
( |
| 781 |
&Ti::Vector { |
| 782 |
size: size1, |
| 783 |
scalar: scalar1, |
| 784 |
}, |
| 785 |
&Ti::Vector { |
| 786 |
size: size2, |
| 787 |
scalar: scalar2, |
| 788 |
}, |
| 789 |
) => scalar1 == scalar2 && size1 == size2, |
| 790 |
// Matrix * vector. |
| 791 |
( |
| 792 |
&Ti::Matrix { columns, .. }, |
| 793 |
&Ti::Vector { |
| 794 |
size, |
| 795 |
scalar: |
| 796 |
Sc { |
| 797 |
kind: Sk::Float, .. |
| 798 |
}, |
| 799 |
}, |
| 800 |
) => columns == size, |
| 801 |
// Vector * matrix. |
| 802 |
( |
| 803 |
&Ti::Vector { |
| 804 |
size, |
| 805 |
scalar: |
| 806 |
Sc { |
| 807 |
kind: Sk::Float, .. |
| 808 |
}, |
| 809 |
}, |
| 810 |
&Ti::Matrix { rows, .. }, |
| 811 |
) => size == rows, |
| 812 |
(&Ti::Matrix { columns, .. }, &Ti::Matrix { rows, .. }) => { |
| 813 |
columns == rows |
| 814 |
} |
| 815 |
_ => false, |
| 816 |
}; |
| 817 |
let left_width = left_inner.scalar_width().unwrap_or(0); |
| 818 |
let right_width = right_inner.scalar_width().unwrap_or(0); |
| 819 |
kind_allowed && types_match && left_width == right_width |
| 820 |
} |
| 821 |
Bo::Equal | Bo::NotEqual => left_inner.is_sized() && left_inner == right_inner, |
| 822 |
Bo::Less | Bo::LessEqual | Bo::Greater | Bo::GreaterEqual => { |
| 823 |
match *left_inner { |
| 824 |
Ti::Scalar(scalar) | Ti::Vector { scalar, .. } => match scalar.kind { |
| 825 |
Sk::Uint | Sk::Sint | Sk::Float => left_inner == right_inner, |
| 826 |
Sk::Bool | Sk::AbstractInt | Sk::AbstractFloat => false, |
| 827 |
}, |
| 828 |
ref other => { |
| 829 |
log::error!("Op {:?} left type {:?}", op, other); |
| 830 |
false |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
Bo::LogicalAnd | Bo::LogicalOr => match *left_inner { |
| 835 |
Ti::Scalar(Sc { kind: Sk::Bool, .. }) |
| 836 |
| Ti::Vector { |
| 837 |
scalar: Sc { kind: Sk::Bool, .. }, |
| 838 |
.. |
| 839 |
} => left_inner == right_inner, |
| 840 |
ref other => { |
| 841 |
log::error!("Op {:?} left type {:?}", op, other); |
| 842 |
false |
| 843 |
} |
| 844 |
}, |
| 845 |
Bo::And | Bo::InclusiveOr => match *left_inner { |
| 846 |
Ti::Scalar(scalar) | Ti::Vector { scalar, .. } => match scalar.kind { |
| 847 |
Sk::Bool | Sk::Sint | Sk::Uint => left_inner == right_inner, |
| 848 |
Sk::Float | Sk::AbstractInt | Sk::AbstractFloat => false, |
| 849 |
}, |
| 850 |
ref other => { |
| 851 |
log::error!("Op {:?} left type {:?}", op, other); |
| 852 |
false |
| 853 |
} |
| 854 |
}, |
| 855 |
Bo::ExclusiveOr => match *left_inner { |
| 856 |
Ti::Scalar(scalar) | Ti::Vector { scalar, .. } => match scalar.kind { |
| 857 |
Sk::Sint | Sk::Uint => left_inner == right_inner, |
| 858 |
Sk::Bool | Sk::Float | Sk::AbstractInt | Sk::AbstractFloat => false, |
| 859 |
}, |
| 860 |
ref other => { |
| 861 |
log::error!("Op {:?} left type {:?}", op, other); |
| 862 |
false |
| 863 |
} |
| 864 |
}, |
| 865 |
Bo::ShiftLeft | Bo::ShiftRight => { |
| 866 |
let (base_size, base_scalar) = match *left_inner { |
| 867 |
Ti::Scalar(scalar) => (Ok(None), scalar), |
| 868 |
Ti::Vector { size, scalar } => (Ok(Some(size)), scalar), |
| 869 |
ref other => { |
| 870 |
log::error!("Op {:?} base type {:?}", op, other); |
| 871 |
(Err(()), Sc::BOOL) |
| 872 |
} |
| 873 |
}; |
| 874 |
let shift_size = match *right_inner { |
| 875 |
Ti::Scalar(Sc { kind: Sk::Uint, .. }) => Ok(None), |
| 876 |
Ti::Vector { |
| 877 |
size, |
| 878 |
scalar: Sc { kind: Sk::Uint, .. }, |
| 879 |
} => Ok(Some(size)), |
| 880 |
ref other => { |
| 881 |
log::error!("Op {:?} shift type {:?}", op, other); |
| 882 |
Err(()) |
| 883 |
} |
| 884 |
}; |
| 885 |
match base_scalar.kind { |
| 886 |
Sk::Sint | Sk::Uint => base_size.is_ok() && base_size == shift_size, |
| 887 |
Sk::Float | Sk::AbstractInt | Sk::AbstractFloat | Sk::Bool => false, |
| 888 |
} |
| 889 |
} |
| 890 |
}; |
| 891 |
if !good { |
| 892 |
log::error!( |
| 893 |
"Left: {:?} of type {:?}", |
| 894 |
function.expressions[left], |
| 895 |
left_inner |
| 896 |
); |
| 897 |
log::error!( |
| 898 |
"Right: {:?} of type {:?}", |
| 899 |
function.expressions[right], |
| 900 |
right_inner |
| 901 |
); |
| 902 |
return Err(ExpressionError::InvalidBinaryOperandTypes { |
| 903 |
op, |
| 904 |
lhs_expr: left, |
| 905 |
lhs_type: left_inner.clone(), |
| 906 |
rhs_expr: right, |
| 907 |
rhs_type: right_inner.clone(), |
| 908 |
}); |
| 909 |
} |
| 910 |
ShaderStages::all() |
| 911 |
} |
| 912 |
E::Select { |
| 913 |
condition, |
| 914 |
accept, |
| 915 |
reject, |
| 916 |
} => { |
| 917 |
let accept_inner = &resolver[accept]; |
| 918 |
let reject_inner = &resolver[reject]; |
| 919 |
let condition_ty = &resolver[condition]; |
| 920 |
let condition_good = match *condition_ty { |
| 921 |
Ti::Scalar(Sc { |
| 922 |
kind: Sk::Bool, |
| 923 |
width: _, |
| 924 |
}) => { |
| 925 |
// When `condition` is a single boolean, `accept` and |
| 926 |
// `reject` can be vectors or scalars. |
| 927 |
match *accept_inner { |
| 928 |
Ti::Scalar { .. } | Ti::Vector { .. } => true, |
| 929 |
_ => false, |
| 930 |
} |
| 931 |
} |
| 932 |
Ti::Vector { |
| 933 |
size, |
| 934 |
scalar: |
| 935 |
Sc { |
| 936 |
kind: Sk::Bool, |
| 937 |
width: _, |
| 938 |
}, |
| 939 |
} => match *accept_inner { |
| 940 |
Ti::Vector { |
| 941 |
size: other_size, .. |
| 942 |
} => size == other_size, |
| 943 |
_ => false, |
| 944 |
}, |
| 945 |
_ => false, |
| 946 |
}; |
| 947 |
if accept_inner != reject_inner { |
| 948 |
return Err(ExpressionError::SelectValuesTypeMismatch { |
| 949 |
accept: accept_inner.clone(), |
| 950 |
reject: reject_inner.clone(), |
| 951 |
}); |
| 952 |
} |
| 953 |
if !condition_good { |
| 954 |
return Err(ExpressionError::SelectConditionNotABool { |
| 955 |
actual: condition_ty.clone(), |
| 956 |
}); |
| 957 |
} |
| 958 |
ShaderStages::all() |
| 959 |
} |
| 960 |
E::Derivative { expr, .. } => { |
| 961 |
match resolver[expr] { |
| 962 |
Ti::Scalar(Sc { |
| 963 |
kind: Sk::Float, .. |
| 964 |
}) |
| 965 |
| Ti::Vector { |
| 966 |
scalar: |
| 967 |
Sc { |
| 968 |
kind: Sk::Float, .. |
| 969 |
}, |
| 970 |
.. |
| 971 |
} => {} |
| 972 |
_ => return Err(ExpressionError::InvalidDerivative), |
| 973 |
} |
| 974 |
ShaderStages::FRAGMENT |
| 975 |
} |
| 976 |
E::Relational { fun, argument } => { |
| 977 |
use crate::RelationalFunction as Rf; |
| 978 |
let argument_inner = &resolver[argument]; |
| 979 |
match fun { |
| 980 |
Rf::All | Rf::Any => match *argument_inner { |
| 981 |
Ti::Vector { |
| 982 |
scalar: Sc { kind: Sk::Bool, .. }, |
| 983 |
.. |
| 984 |
} => {} |
| 985 |
ref other => { |
| 986 |
log::error!("All/Any of type {:?}", other); |
| 987 |
return Err(ExpressionError::InvalidBooleanVector(argument)); |
| 988 |
} |
| 989 |
}, |
| 990 |
Rf::IsNan | Rf::IsInf => match *argument_inner { |
| 991 |
Ti::Scalar(scalar) | Ti::Vector { scalar, .. } |
| 992 |
if scalar.kind == Sk::Float => {} |
| 993 |
ref other => { |
| 994 |
log::error!("Float test of type {:?}", other); |
| 995 |
return Err(ExpressionError::InvalidFloatArgument(argument)); |
| 996 |
} |
| 997 |
}, |
| 998 |
} |
| 999 |
ShaderStages::all() |
| 1000 |
} |
| 1001 |
E::Math { |
| 1002 |
fun, |
| 1003 |
arg, |
| 1004 |
arg1, |
| 1005 |
arg2, |
| 1006 |
arg3, |
| 1007 |
} => { |
| 1008 |
let actuals: &[_] = match (arg1, arg2, arg3) { |
| 1009 |
(None, None, None) => &[arg], |
| 1010 |
(Some(arg1), None, None) => &[arg, arg1], |
| 1011 |
(Some(arg1), Some(arg2), None) => &[arg, arg1, arg2], |
| 1012 |
(Some(arg1), Some(arg2), Some(arg3)) => &[arg, arg1, arg2, arg3], |
| 1013 |
_ => return Err(ExpressionError::WrongArgumentCount(fun)), |
| 1014 |
}; |
| 1015 |
|
| 1016 |
let resolve = |arg| &resolver[arg]; |
| 1017 |
let actual_types: &[_] = match *actuals { |
| 1018 |
[arg0] => &[resolve(arg0)], |
| 1019 |
[arg0, arg1] => &[resolve(arg0), resolve(arg1)], |
| 1020 |
[arg0, arg1, arg2] => &[resolve(arg0), resolve(arg1), resolve(arg2)], |
| 1021 |
[arg0, arg1, arg2, arg3] => { |
| 1022 |
&[resolve(arg0), resolve(arg1), resolve(arg2), resolve(arg3)] |
| 1023 |
} |
| 1024 |
_ => unreachable!(), |
| 1025 |
}; |
| 1026 |
|
| 1027 |
// Start with the set of all overloads available for `fun`. |
| 1028 |
let mut overloads = fun.overloads(); |
| 1029 |
log::debug!( |
| 1030 |
"initial overloads for {:?}: {:#?}", |
| 1031 |
fun, |
| 1032 |
overloads.for_debug(&module.types) |
| 1033 |
); |
| 1034 |
|
| 1035 |
// If any argument is not a constant expression, then no |
| 1036 |
// overloads that accept abstract values should be considered. |
| 1037 |
// `OverloadSet::concrete_only` is supposed to help impose this |
| 1038 |
// restriction. However, no `MathFunction` accepts a mix of |
| 1039 |
// abstract and concrete arguments, so we don't need to worry |
| 1040 |
// about that here. |
| 1041 |
|
| 1042 |
for (i, (&expr, &ty)) in actuals.iter().zip(actual_types).enumerate() { |
| 1043 |
// Remove overloads that cannot accept an `i`'th |
| 1044 |
// argument arguments of type `ty`. |
| 1045 |
overloads = overloads.arg(i, ty, &module.types); |
| 1046 |
log::debug!( |
| 1047 |
"overloads after arg {i}: {:#?}", |
| 1048 |
overloads.for_debug(&module.types) |
| 1049 |
); |
| 1050 |
|
| 1051 |
if overloads.is_empty() { |
| 1052 |
log::debug!("all overloads eliminated"); |
| 1053 |
return Err(ExpressionError::InvalidArgumentType(fun, i as u32, expr)); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
if actuals.len() < overloads.min_arguments() { |
| 1058 |
return Err(ExpressionError::WrongArgumentCount(fun)); |
| 1059 |
} |
| 1060 |
|
| 1061 |
ShaderStages::all() |
| 1062 |
} |
| 1063 |
E::As { |
| 1064 |
expr, |
| 1065 |
kind, |
| 1066 |
convert, |
| 1067 |
} => { |
| 1068 |
let mut base_scalar = match resolver[expr] { |
| 1069 |
crate::TypeInner::Scalar(scalar) | crate::TypeInner::Vector { scalar, .. } => { |
| 1070 |
scalar |
| 1071 |
} |
| 1072 |
crate::TypeInner::Matrix { scalar, .. } => scalar, |
| 1073 |
_ => return Err(ExpressionError::InvalidCastArgument), |
| 1074 |
}; |
| 1075 |
base_scalar.kind = kind; |
| 1076 |
if let Some(width) = convert { |
| 1077 |
base_scalar.width = width; |
| 1078 |
} |
| 1079 |
if self.check_width(base_scalar).is_err() { |
| 1080 |
return Err(ExpressionError::InvalidCastArgument); |
| 1081 |
} |
| 1082 |
ShaderStages::all() |
| 1083 |
} |
| 1084 |
E::CallResult(function) => mod_info.functions[function.index()].available_stages, |
| 1085 |
E::AtomicResult { .. } => { |
| 1086 |
// These expressions are validated when we check the `Atomic` statement |
| 1087 |
// that refers to them, because we have all the information we need at |
| 1088 |
// that point. The checks driven by `Validator::needs_visit` ensure |
| 1089 |
// that this expression is indeed visited by one `Atomic` statement. |
| 1090 |
ShaderStages::all() |
| 1091 |
} |
| 1092 |
E::WorkGroupUniformLoadResult { ty } => { |
| 1093 |
if self.types[ty.index()] |
| 1094 |
.flags |
| 1095 |
// Sized | Constructible is exactly the types currently supported by |
| 1096 |
// WorkGroupUniformLoad |
| 1097 |
.contains(TypeFlags::SIZED | TypeFlags::CONSTRUCTIBLE) |
| 1098 |
{ |
| 1099 |
ShaderStages::COMPUTE |
| 1100 |
} else { |
| 1101 |
return Err(ExpressionError::InvalidWorkGroupUniformLoadResultType(ty)); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
E::ArrayLength(expr) => match resolver[expr] { |
| 1105 |
Ti::Pointer { base, .. } => { |
| 1106 |
let base_ty = &resolver.types[base]; |
| 1107 |
if let Ti::Array { |
| 1108 |
size: crate::ArraySize::Dynamic, |
| 1109 |
.. |
| 1110 |
} = base_ty.inner |
| 1111 |
{ |
| 1112 |
ShaderStages::all() |
| 1113 |
} else { |
| 1114 |
return Err(ExpressionError::InvalidArrayType(expr)); |
| 1115 |
} |
| 1116 |
} |
| 1117 |
ref other => { |
| 1118 |
log::error!("Array length of {:?}", other); |
| 1119 |
return Err(ExpressionError::InvalidArrayType(expr)); |
| 1120 |
} |
| 1121 |
}, |
| 1122 |
E::RayQueryProceedResult => ShaderStages::all(), |
| 1123 |
E::RayQueryGetIntersection { |
| 1124 |
query, |
| 1125 |
committed: _, |
| 1126 |
} => match resolver[query] { |
| 1127 |
Ti::Pointer { |
| 1128 |
base, |
| 1129 |
space: crate::AddressSpace::Function, |
| 1130 |
} => match resolver.types[base].inner { |
| 1131 |
Ti::RayQuery { .. } => ShaderStages::all(), |
| 1132 |
ref other => { |
| 1133 |
log::error!("Intersection result of a pointer to {:?}", other); |
| 1134 |
return Err(ExpressionError::InvalidRayQueryType(query)); |
| 1135 |
} |
| 1136 |
}, |
| 1137 |
ref other => { |
| 1138 |
log::error!("Intersection result of {:?}", other); |
| 1139 |
return Err(ExpressionError::InvalidRayQueryType(query)); |
| 1140 |
} |
| 1141 |
}, |
| 1142 |
E::RayQueryVertexPositions { |
| 1143 |
query, |
| 1144 |
committed: _, |
| 1145 |
} => match resolver[query] { |
| 1146 |
Ti::Pointer { |
| 1147 |
base, |
| 1148 |
space: crate::AddressSpace::Function, |
| 1149 |
} => match resolver.types[base].inner { |
| 1150 |
Ti::RayQuery { |
| 1151 |
vertex_return: true, |
| 1152 |
} => ShaderStages::all(), |
| 1153 |
ref other => { |
| 1154 |
log::error!("Intersection result of a pointer to {:?}", other); |
| 1155 |
return Err(ExpressionError::InvalidRayQueryType(query)); |
| 1156 |
} |
| 1157 |
}, |
| 1158 |
ref other => { |
| 1159 |
log::error!("Intersection result of {:?}", other); |
| 1160 |
return Err(ExpressionError::InvalidRayQueryType(query)); |
| 1161 |
} |
| 1162 |
}, |
| 1163 |
E::SubgroupBallotResult | E::SubgroupOperationResult { .. } => self.subgroup_stages, |
| 1164 |
}; |
| 1165 |
Ok(stages) |
| 1166 |
} |
| 1167 |
|
| 1168 |
fn global_var_ty( |
| 1169 |
module: &crate::Module, |
| 1170 |
function: &crate::Function, |
| 1171 |
expr: Handle<crate::Expression>, |
| 1172 |
) -> Result<Handle<crate::Type>, ExpressionError> { |
| 1173 |
use crate::Expression as Ex; |
| 1174 |
|
| 1175 |
match function.expressions[expr] { |
| 1176 |
Ex::GlobalVariable(var_handle) => Ok(module.global_variables[var_handle].ty), |
| 1177 |
Ex::FunctionArgument(i) => Ok(function.arguments[i as usize].ty), |
| 1178 |
Ex::Access { base, .. } | Ex::AccessIndex { base, .. } => { |
| 1179 |
match function.expressions[base] { |
| 1180 |
Ex::GlobalVariable(var_handle) => { |
| 1181 |
let array_ty = module.global_variables[var_handle].ty; |
| 1182 |
|
| 1183 |
match module.types[array_ty].inner { |
| 1184 |
crate::TypeInner::BindingArray { base, .. } => Ok(base), |
| 1185 |
_ => Err(ExpressionError::ExpectedBindingArrayType(array_ty)), |
| 1186 |
} |
| 1187 |
} |
| 1188 |
_ => Err(ExpressionError::ExpectedGlobalVariable), |
| 1189 |
} |
| 1190 |
} |
| 1191 |
_ => Err(ExpressionError::ExpectedGlobalVariable), |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
pub fn validate_literal(&self, literal: crate::Literal) -> Result<(), LiteralError> { |
| 1196 |
let _ = self.check_width(literal.scalar())?; |
| 1197 |
check_literal_value(literal)?; |
| 1198 |
|
| 1199 |
Ok(()) |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
pub fn check_literal_value(literal: crate::Literal) -> Result<(), LiteralError> { |
| 1204 |
let is_nan = match literal { |
| 1205 |
crate::Literal::F64(v) => v.is_nan(), |
| 1206 |
crate::Literal::F32(v) => v.is_nan(), |
| 1207 |
_ => false, |
| 1208 |
}; |
| 1209 |
if is_nan { |
| 1210 |
return Err(LiteralError::NaN); |
| 1211 |
} |
| 1212 |
|
| 1213 |
let is_infinite = match literal { |
| 1214 |
crate::Literal::F64(v) => v.is_infinite(), |
| 1215 |
crate::Literal::F32(v) => v.is_infinite(), |
| 1216 |
_ => false, |
| 1217 |
}; |
| 1218 |
if is_infinite { |
| 1219 |
return Err(LiteralError::Infinity); |
| 1220 |
} |
| 1221 |
|
| 1222 |
Ok(()) |
| 1223 |
} |
| 1224 |
|
| 1225 |
#[cfg(test)] |
| 1226 |
/// Validate a module containing the given expression, expecting an error. |
| 1227 |
fn validate_with_expression( |
| 1228 |
expr: crate::Expression, |
| 1229 |
caps: super::Capabilities, |
| 1230 |
) -> Result<ModuleInfo, crate::span::WithSpan<super::ValidationError>> { |
| 1231 |
use crate::span::Span; |
| 1232 |
|
| 1233 |
let mut function = crate::Function::default(); |
| 1234 |
function.expressions.append(expr, Span::default()); |
| 1235 |
function.body.push( |
| 1236 |
crate::Statement::Emit(function.expressions.range_from(0)), |
| 1237 |
Span::default(), |
| 1238 |
); |
| 1239 |
|
| 1240 |
let mut module = crate::Module::default(); |
| 1241 |
module.functions.append(function, Span::default()); |
| 1242 |
|
| 1243 |
let mut validator = super::Validator::new(super::ValidationFlags::EXPRESSIONS, caps); |
| 1244 |
|
| 1245 |
validator.validate(&module) |
| 1246 |
} |
| 1247 |
|
| 1248 |
#[cfg(test)] |
| 1249 |
/// Validate a module containing the given constant expression, expecting an error. |
| 1250 |
fn validate_with_const_expression( |
| 1251 |
expr: crate::Expression, |
| 1252 |
caps: super::Capabilities, |
| 1253 |
) -> Result<ModuleInfo, crate::span::WithSpan<super::ValidationError>> { |
| 1254 |
use crate::span::Span; |
| 1255 |
|
| 1256 |
let mut module = crate::Module::default(); |
| 1257 |
module.global_expressions.append(expr, Span::default()); |
| 1258 |
|
| 1259 |
let mut validator = super::Validator::new(super::ValidationFlags::CONSTANTS, caps); |
| 1260 |
|
| 1261 |
validator.validate(&module) |
| 1262 |
} |
| 1263 |
|
| 1264 |
/// Using F64 in a function's expression arena is forbidden. |
| 1265 |
#[test] |
| 1266 |
fn f64_runtime_literals() { |
| 1267 |
let result = validate_with_expression( |
| 1268 |
crate::Expression::Literal(crate::Literal::F64(0.57721_56649)), |
| 1269 |
super::Capabilities::default(), |
| 1270 |
); |
| 1271 |
let error = result.unwrap_err().into_inner(); |
| 1272 |
assert!(matches!( |
| 1273 |
error, |
| 1274 |
crate::valid::ValidationError::Function { |
| 1275 |
source: super::FunctionError::Expression { |
| 1276 |
source: ExpressionError::Literal(LiteralError::Width( |
| 1277 |
super::r#type::WidthError::MissingCapability { |
| 1278 |
name: "f64", |
| 1279 |
flag: "FLOAT64", |
| 1280 |
} |
| 1281 |
),), |
| 1282 |
.. |
| 1283 |
}, |
| 1284 |
.. |
| 1285 |
} |
| 1286 |
)); |
| 1287 |
|
| 1288 |
let result = validate_with_expression( |
| 1289 |
crate::Expression::Literal(crate::Literal::F64(0.57721_56649)), |
| 1290 |
super::Capabilities::default() | super::Capabilities::FLOAT64, |
| 1291 |
); |
| 1292 |
assert!(result.is_ok()); |
| 1293 |
} |
| 1294 |
|
| 1295 |
/// Using F64 in a module's constant expression arena is forbidden. |
| 1296 |
#[test] |
| 1297 |
fn f64_const_literals() { |
| 1298 |
let result = validate_with_const_expression( |
| 1299 |
crate::Expression::Literal(crate::Literal::F64(0.57721_56649)), |
| 1300 |
super::Capabilities::default(), |
| 1301 |
); |
| 1302 |
let error = result.unwrap_err().into_inner(); |
| 1303 |
assert!(matches!( |
| 1304 |
error, |
| 1305 |
crate::valid::ValidationError::ConstExpression { |
| 1306 |
source: ConstExpressionError::Literal(LiteralError::Width( |
| 1307 |
super::r#type::WidthError::MissingCapability { |
| 1308 |
name: "f64", |
| 1309 |
flag: "FLOAT64", |
| 1310 |
} |
| 1311 |
)), |
| 1312 |
.. |
| 1313 |
} |
| 1314 |
)); |
| 1315 |
|
| 1316 |
let result = validate_with_const_expression( |
| 1317 |
crate::Expression::Literal(crate::Literal::F64(0.57721_56649)), |
| 1318 |
super::Capabilities::default() | super::Capabilities::FLOAT64, |
| 1319 |
); |
| 1320 |
assert!(result.is_ok()); |
| 1321 |
} |