| 1 |
use alloc::{ |
| 2 |
format, |
| 3 |
string::{String, ToString}, |
| 4 |
vec, |
| 5 |
vec::Vec, |
| 6 |
}; |
| 7 |
use core::iter; |
| 8 |
|
| 9 |
use super::{ |
| 10 |
ast::*, |
| 11 |
builtins::{inject_builtin, sampled_to_depth}, |
| 12 |
context::{Context, ExprPos, StmtContext}, |
| 13 |
error::{Error, ErrorKind}, |
| 14 |
types::scalar_components, |
| 15 |
Frontend, Result, |
| 16 |
}; |
| 17 |
use crate::{ |
| 18 |
front::glsl::types::type_power, proc::ensure_block_returns, AddressSpace, Block, EntryPoint, |
| 19 |
Expression, Function, FunctionArgument, FunctionResult, Handle, Literal, LocalVariable, Scalar, |
| 20 |
ScalarKind, Span, Statement, StructMember, Type, TypeInner, |
| 21 |
}; |
| 22 |
|
| 23 |
/// Struct detailing a store operation that must happen after a function call |
| 24 |
struct ProxyWrite { |
| 25 |
/// The store target |
| 26 |
target: Handle<Expression>, |
| 27 |
/// A pointer to read the value of the store |
| 28 |
value: Handle<Expression>, |
| 29 |
/// An optional conversion to be applied |
| 30 |
convert: Option<Scalar>, |
| 31 |
} |
| 32 |
|
| 33 |
impl Frontend { |
| 34 |
pub(crate) fn function_or_constructor_call( |
| 35 |
&mut self, |
| 36 |
ctx: &mut Context, |
| 37 |
stmt: &StmtContext, |
| 38 |
fc: FunctionCallKind, |
| 39 |
raw_args: &[Handle<HirExpr>], |
| 40 |
meta: Span, |
| 41 |
) -> Result<Option<Handle<Expression>>> { |
| 42 |
let args: Vec<_> = raw_args |
| 43 |
.iter() |
| 44 |
.map(|e| ctx.lower_expect_inner(stmt, self, *e, ExprPos::Rhs)) |
| 45 |
.collect::<Result<_>>()?; |
| 46 |
|
| 47 |
match fc { |
| 48 |
FunctionCallKind::TypeConstructor(ty) => { |
| 49 |
if args.len() == 1 { |
| 50 |
self.constructor_single(ctx, ty, args[0], meta).map(Some) |
| 51 |
} else { |
| 52 |
self.constructor_many(ctx, ty, args, meta).map(Some) |
| 53 |
} |
| 54 |
} |
| 55 |
FunctionCallKind::Function(name) => { |
| 56 |
self.function_call(ctx, stmt, name, args, raw_args, meta) |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
fn constructor_single( |
| 62 |
&mut self, |
| 63 |
ctx: &mut Context, |
| 64 |
ty: Handle<Type>, |
| 65 |
(mut value, expr_meta): (Handle<Expression>, Span), |
| 66 |
meta: Span, |
| 67 |
) -> Result<Handle<Expression>> { |
| 68 |
let expr_type = ctx.resolve_type(value, expr_meta)?; |
| 69 |
|
| 70 |
let vector_size = match *expr_type { |
| 71 |
TypeInner::Vector { size, .. } => Some(size), |
| 72 |
_ => None, |
| 73 |
}; |
| 74 |
|
| 75 |
let expr_is_bool = expr_type.scalar_kind() == Some(ScalarKind::Bool); |
| 76 |
|
| 77 |
// Special case: if casting from a bool, we need to use Select and not As. |
| 78 |
match ctx.module.types[ty].inner.scalar() { |
| 79 |
Some(result_scalar) if expr_is_bool && result_scalar.kind != ScalarKind::Bool => { |
| 80 |
let result_scalar = Scalar { |
| 81 |
width: 4, |
| 82 |
..result_scalar |
| 83 |
}; |
| 84 |
let l0 = Literal::zero(result_scalar).unwrap(); |
| 85 |
let l1 = Literal::one(result_scalar).unwrap(); |
| 86 |
let mut reject = ctx.add_expression(Expression::Literal(l0), expr_meta)?; |
| 87 |
let mut accept = ctx.add_expression(Expression::Literal(l1), expr_meta)?; |
| 88 |
|
| 89 |
ctx.implicit_splat(&mut reject, meta, vector_size)?; |
| 90 |
ctx.implicit_splat(&mut accept, meta, vector_size)?; |
| 91 |
|
| 92 |
let h = ctx.add_expression( |
| 93 |
Expression::Select { |
| 94 |
accept, |
| 95 |
reject, |
| 96 |
condition: value, |
| 97 |
}, |
| 98 |
expr_meta, |
| 99 |
)?; |
| 100 |
|
| 101 |
return Ok(h); |
| 102 |
} |
| 103 |
_ => {} |
| 104 |
} |
| 105 |
|
| 106 |
Ok(match ctx.module.types[ty].inner { |
| 107 |
TypeInner::Vector { size, scalar } if vector_size.is_none() => { |
| 108 |
ctx.forced_conversion(&mut value, expr_meta, scalar)?; |
| 109 |
|
| 110 |
if let TypeInner::Scalar { .. } = *ctx.resolve_type(value, expr_meta)? { |
| 111 |
ctx.add_expression(Expression::Splat { size, value }, meta)? |
| 112 |
} else { |
| 113 |
self.vector_constructor(ctx, ty, size, scalar, &[(value, expr_meta)], meta)? |
| 114 |
} |
| 115 |
} |
| 116 |
TypeInner::Scalar(scalar) => { |
| 117 |
let mut expr = value; |
| 118 |
if let TypeInner::Vector { .. } | TypeInner::Matrix { .. } = |
| 119 |
*ctx.resolve_type(value, expr_meta)? |
| 120 |
{ |
| 121 |
expr = ctx.add_expression( |
| 122 |
Expression::AccessIndex { |
| 123 |
base: expr, |
| 124 |
index: 0, |
| 125 |
}, |
| 126 |
meta, |
| 127 |
)?; |
| 128 |
} |
| 129 |
|
| 130 |
if let TypeInner::Matrix { .. } = *ctx.resolve_type(value, expr_meta)? { |
| 131 |
expr = ctx.add_expression( |
| 132 |
Expression::AccessIndex { |
| 133 |
base: expr, |
| 134 |
index: 0, |
| 135 |
}, |
| 136 |
meta, |
| 137 |
)?; |
| 138 |
} |
| 139 |
|
| 140 |
ctx.add_expression( |
| 141 |
Expression::As { |
| 142 |
kind: scalar.kind, |
| 143 |
expr, |
| 144 |
convert: Some(scalar.width), |
| 145 |
}, |
| 146 |
meta, |
| 147 |
)? |
| 148 |
} |
| 149 |
TypeInner::Vector { size, scalar } => { |
| 150 |
if vector_size != Some(size) { |
| 151 |
value = ctx.vector_resize(size, value, expr_meta)?; |
| 152 |
} |
| 153 |
|
| 154 |
ctx.add_expression( |
| 155 |
Expression::As { |
| 156 |
kind: scalar.kind, |
| 157 |
expr: value, |
| 158 |
convert: Some(scalar.width), |
| 159 |
}, |
| 160 |
meta, |
| 161 |
)? |
| 162 |
} |
| 163 |
TypeInner::Matrix { |
| 164 |
columns, |
| 165 |
rows, |
| 166 |
scalar, |
| 167 |
} => self.matrix_one_arg(ctx, ty, columns, rows, scalar, (value, expr_meta), meta)?, |
| 168 |
TypeInner::Struct { ref members, .. } => { |
| 169 |
let scalar_components = members |
| 170 |
.first() |
| 171 |
.and_then(|member| scalar_components(&ctx.module.types[member.ty].inner)); |
| 172 |
if let Some(scalar) = scalar_components { |
| 173 |
ctx.implicit_conversion(&mut value, expr_meta, scalar)?; |
| 174 |
} |
| 175 |
|
| 176 |
ctx.add_expression( |
| 177 |
Expression::Compose { |
| 178 |
ty, |
| 179 |
components: vec![value], |
| 180 |
}, |
| 181 |
meta, |
| 182 |
)? |
| 183 |
} |
| 184 |
|
| 185 |
TypeInner::Array { base, .. } => { |
| 186 |
let scalar_components = scalar_components(&ctx.module.types[base].inner); |
| 187 |
if let Some(scalar) = scalar_components { |
| 188 |
ctx.implicit_conversion(&mut value, expr_meta, scalar)?; |
| 189 |
} |
| 190 |
|
| 191 |
ctx.add_expression( |
| 192 |
Expression::Compose { |
| 193 |
ty, |
| 194 |
components: vec![value], |
| 195 |
}, |
| 196 |
meta, |
| 197 |
)? |
| 198 |
} |
| 199 |
_ => { |
| 200 |
self.errors.push(Error { |
| 201 |
kind: ErrorKind::SemanticError("Bad type constructor".into()), |
| 202 |
meta, |
| 203 |
}); |
| 204 |
|
| 205 |
value |
| 206 |
} |
| 207 |
}) |
| 208 |
} |
| 209 |
|
| 210 |
#[allow(clippy::too_many_arguments)] |
| 211 |
fn matrix_one_arg( |
| 212 |
&mut self, |
| 213 |
ctx: &mut Context, |
| 214 |
ty: Handle<Type>, |
| 215 |
columns: crate::VectorSize, |
| 216 |
rows: crate::VectorSize, |
| 217 |
element_scalar: Scalar, |
| 218 |
(mut value, expr_meta): (Handle<Expression>, Span), |
| 219 |
meta: Span, |
| 220 |
) -> Result<Handle<Expression>> { |
| 221 |
let mut components = Vec::with_capacity(columns as usize); |
| 222 |
// TODO: casts |
| 223 |
// `Expression::As` doesn't support matrix width |
| 224 |
// casts so we need to do some extra work for casts |
| 225 |
|
| 226 |
ctx.forced_conversion(&mut value, expr_meta, element_scalar)?; |
| 227 |
match *ctx.resolve_type(value, expr_meta)? { |
| 228 |
TypeInner::Scalar(_) => { |
| 229 |
// If a matrix is constructed with a single scalar value, then that |
| 230 |
// value is used to initialize all the values along the diagonal of |
| 231 |
// the matrix; the rest are given zeros. |
| 232 |
let vector_ty = ctx.module.types.insert( |
| 233 |
Type { |
| 234 |
name: None, |
| 235 |
inner: TypeInner::Vector { |
| 236 |
size: rows, |
| 237 |
scalar: element_scalar, |
| 238 |
}, |
| 239 |
}, |
| 240 |
meta, |
| 241 |
); |
| 242 |
|
| 243 |
let zero_literal = Literal::zero(element_scalar).unwrap(); |
| 244 |
let zero = ctx.add_expression(Expression::Literal(zero_literal), meta)?; |
| 245 |
|
| 246 |
for i in 0..columns as u32 { |
| 247 |
components.push( |
| 248 |
ctx.add_expression( |
| 249 |
Expression::Compose { |
| 250 |
ty: vector_ty, |
| 251 |
components: (0..rows as u32) |
| 252 |
.map(|r| match r == i { |
| 253 |
true => value, |
| 254 |
false => zero, |
| 255 |
}) |
| 256 |
.collect(), |
| 257 |
}, |
| 258 |
meta, |
| 259 |
)?, |
| 260 |
) |
| 261 |
} |
| 262 |
} |
| 263 |
TypeInner::Matrix { |
| 264 |
rows: ori_rows, |
| 265 |
columns: ori_cols, |
| 266 |
.. |
| 267 |
} => { |
| 268 |
// If a matrix is constructed from a matrix, then each component |
| 269 |
// (column i, row j) in the result that has a corresponding component |
| 270 |
// (column i, row j) in the argument will be initialized from there. All |
| 271 |
// other components will be initialized to the identity matrix. |
| 272 |
|
| 273 |
let zero_literal = Literal::zero(element_scalar).unwrap(); |
| 274 |
let one_literal = Literal::one(element_scalar).unwrap(); |
| 275 |
|
| 276 |
let zero = ctx.add_expression(Expression::Literal(zero_literal), meta)?; |
| 277 |
let one = ctx.add_expression(Expression::Literal(one_literal), meta)?; |
| 278 |
|
| 279 |
let vector_ty = ctx.module.types.insert( |
| 280 |
Type { |
| 281 |
name: None, |
| 282 |
inner: TypeInner::Vector { |
| 283 |
size: rows, |
| 284 |
scalar: element_scalar, |
| 285 |
}, |
| 286 |
}, |
| 287 |
meta, |
| 288 |
); |
| 289 |
|
| 290 |
for i in 0..columns as u32 { |
| 291 |
if i < ori_cols as u32 { |
| 292 |
use core::cmp::Ordering; |
| 293 |
|
| 294 |
let vector = ctx.add_expression( |
| 295 |
Expression::AccessIndex { |
| 296 |
base: value, |
| 297 |
index: i, |
| 298 |
}, |
| 299 |
meta, |
| 300 |
)?; |
| 301 |
|
| 302 |
components.push(match ori_rows.cmp(&rows) { |
| 303 |
Ordering::Less => { |
| 304 |
let components = (0..rows as u32) |
| 305 |
.map(|r| { |
| 306 |
if r < ori_rows as u32 { |
| 307 |
ctx.add_expression( |
| 308 |
Expression::AccessIndex { |
| 309 |
base: vector, |
| 310 |
index: r, |
| 311 |
}, |
| 312 |
meta, |
| 313 |
) |
| 314 |
} else if r == i { |
| 315 |
Ok(one) |
| 316 |
} else { |
| 317 |
Ok(zero) |
| 318 |
} |
| 319 |
}) |
| 320 |
.collect::<Result<_>>()?; |
| 321 |
|
| 322 |
ctx.add_expression( |
| 323 |
Expression::Compose { |
| 324 |
ty: vector_ty, |
| 325 |
components, |
| 326 |
}, |
| 327 |
meta, |
| 328 |
)? |
| 329 |
} |
| 330 |
Ordering::Equal => vector, |
| 331 |
Ordering::Greater => ctx.vector_resize(rows, vector, meta)?, |
| 332 |
}) |
| 333 |
} else { |
| 334 |
let compose_expr = Expression::Compose { |
| 335 |
ty: vector_ty, |
| 336 |
components: (0..rows as u32) |
| 337 |
.map(|r| match r == i { |
| 338 |
true => one, |
| 339 |
false => zero, |
| 340 |
}) |
| 341 |
.collect(), |
| 342 |
}; |
| 343 |
|
| 344 |
let vec = ctx.add_expression(compose_expr, meta)?; |
| 345 |
|
| 346 |
components.push(vec) |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
_ => { |
| 351 |
components = iter::repeat_n(value, columns as usize).collect(); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
ctx.add_expression(Expression::Compose { ty, components }, meta) |
| 356 |
} |
| 357 |
|
| 358 |
#[allow(clippy::too_many_arguments)] |
| 359 |
fn vector_constructor( |
| 360 |
&mut self, |
| 361 |
ctx: &mut Context, |
| 362 |
ty: Handle<Type>, |
| 363 |
size: crate::VectorSize, |
| 364 |
scalar: Scalar, |
| 365 |
args: &[(Handle<Expression>, Span)], |
| 366 |
meta: Span, |
| 367 |
) -> Result<Handle<Expression>> { |
| 368 |
let mut components = Vec::with_capacity(size as usize); |
| 369 |
|
| 370 |
for (mut arg, expr_meta) in args.iter().copied() { |
| 371 |
ctx.forced_conversion(&mut arg, expr_meta, scalar)?; |
| 372 |
|
| 373 |
if components.len() >= size as usize { |
| 374 |
break; |
| 375 |
} |
| 376 |
|
| 377 |
match *ctx.resolve_type(arg, expr_meta)? { |
| 378 |
TypeInner::Scalar { .. } => components.push(arg), |
| 379 |
TypeInner::Matrix { rows, columns, .. } => { |
| 380 |
components.reserve(rows as usize * columns as usize); |
| 381 |
for c in 0..(columns as u32) { |
| 382 |
let base = ctx.add_expression( |
| 383 |
Expression::AccessIndex { |
| 384 |
base: arg, |
| 385 |
index: c, |
| 386 |
}, |
| 387 |
expr_meta, |
| 388 |
)?; |
| 389 |
for r in 0..(rows as u32) { |
| 390 |
components.push(ctx.add_expression( |
| 391 |
Expression::AccessIndex { base, index: r }, |
| 392 |
expr_meta, |
| 393 |
)?) |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
TypeInner::Vector { size: ori_size, .. } => { |
| 398 |
components.reserve(ori_size as usize); |
| 399 |
for index in 0..(ori_size as u32) { |
| 400 |
components.push(ctx.add_expression( |
| 401 |
Expression::AccessIndex { base: arg, index }, |
| 402 |
expr_meta, |
| 403 |
)?) |
| 404 |
} |
| 405 |
} |
| 406 |
_ => components.push(arg), |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
components.truncate(size as usize); |
| 411 |
|
| 412 |
ctx.add_expression(Expression::Compose { ty, components }, meta) |
| 413 |
} |
| 414 |
|
| 415 |
fn constructor_many( |
| 416 |
&mut self, |
| 417 |
ctx: &mut Context, |
| 418 |
ty: Handle<Type>, |
| 419 |
args: Vec<(Handle<Expression>, Span)>, |
| 420 |
meta: Span, |
| 421 |
) -> Result<Handle<Expression>> { |
| 422 |
let mut components = Vec::with_capacity(args.len()); |
| 423 |
|
| 424 |
let struct_member_data = match ctx.module.types[ty].inner { |
| 425 |
TypeInner::Matrix { |
| 426 |
columns, |
| 427 |
rows, |
| 428 |
scalar: element_scalar, |
| 429 |
} => { |
| 430 |
let mut flattened = Vec::with_capacity(columns as usize * rows as usize); |
| 431 |
|
| 432 |
for (mut arg, meta) in args.iter().copied() { |
| 433 |
ctx.forced_conversion(&mut arg, meta, element_scalar)?; |
| 434 |
|
| 435 |
match *ctx.resolve_type(arg, meta)? { |
| 436 |
TypeInner::Vector { size, .. } => { |
| 437 |
for i in 0..(size as u32) { |
| 438 |
flattened.push(ctx.add_expression( |
| 439 |
Expression::AccessIndex { |
| 440 |
base: arg, |
| 441 |
index: i, |
| 442 |
}, |
| 443 |
meta, |
| 444 |
)?) |
| 445 |
} |
| 446 |
} |
| 447 |
_ => flattened.push(arg), |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
let ty = ctx.module.types.insert( |
| 452 |
Type { |
| 453 |
name: None, |
| 454 |
inner: TypeInner::Vector { |
| 455 |
size: rows, |
| 456 |
scalar: element_scalar, |
| 457 |
}, |
| 458 |
}, |
| 459 |
meta, |
| 460 |
); |
| 461 |
|
| 462 |
for chunk in flattened.chunks(rows as usize) { |
| 463 |
components.push(ctx.add_expression( |
| 464 |
Expression::Compose { |
| 465 |
ty, |
| 466 |
components: Vec::from(chunk), |
| 467 |
}, |
| 468 |
meta, |
| 469 |
)?) |
| 470 |
} |
| 471 |
None |
| 472 |
} |
| 473 |
TypeInner::Vector { size, scalar } => { |
| 474 |
return self.vector_constructor(ctx, ty, size, scalar, &args, meta) |
| 475 |
} |
| 476 |
TypeInner::Array { base, .. } => { |
| 477 |
for (mut arg, meta) in args.iter().copied() { |
| 478 |
let scalar_components = scalar_components(&ctx.module.types[base].inner); |
| 479 |
if let Some(scalar) = scalar_components { |
| 480 |
ctx.implicit_conversion(&mut arg, meta, scalar)?; |
| 481 |
} |
| 482 |
|
| 483 |
components.push(arg) |
| 484 |
} |
| 485 |
None |
| 486 |
} |
| 487 |
TypeInner::Struct { ref members, .. } => Some( |
| 488 |
members |
| 489 |
.iter() |
| 490 |
.map(|member| scalar_components(&ctx.module.types[member.ty].inner)) |
| 491 |
.collect::<Vec<_>>(), |
| 492 |
), |
| 493 |
_ => { |
| 494 |
return Err(Error { |
| 495 |
kind: ErrorKind::SemanticError("Constructor: Too many arguments".into()), |
| 496 |
meta, |
| 497 |
}) |
| 498 |
} |
| 499 |
}; |
| 500 |
|
| 501 |
if let Some(struct_member_data) = struct_member_data { |
| 502 |
for ((mut arg, meta), scalar_components) in |
| 503 |
args.iter().copied().zip(struct_member_data.iter().copied()) |
| 504 |
{ |
| 505 |
if let Some(scalar) = scalar_components { |
| 506 |
ctx.implicit_conversion(&mut arg, meta, scalar)?; |
| 507 |
} |
| 508 |
|
| 509 |
components.push(arg) |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
ctx.add_expression(Expression::Compose { ty, components }, meta) |
| 514 |
} |
| 515 |
|
| 516 |
#[allow(clippy::too_many_arguments)] |
| 517 |
fn function_call( |
| 518 |
&mut self, |
| 519 |
ctx: &mut Context, |
| 520 |
stmt: &StmtContext, |
| 521 |
name: String, |
| 522 |
args: Vec<(Handle<Expression>, Span)>, |
| 523 |
raw_args: &[Handle<HirExpr>], |
| 524 |
meta: Span, |
| 525 |
) -> Result<Option<Handle<Expression>>> { |
| 526 |
// Grow the typifier to be able to index it later without needing |
| 527 |
// to hold the context mutably |
| 528 |
for &(expr, span) in args.iter() { |
| 529 |
ctx.typifier_grow(expr, span)?; |
| 530 |
} |
| 531 |
|
| 532 |
// Check if the passed arguments require any special variations |
| 533 |
let mut variations = |
| 534 |
builtin_required_variations(args.iter().map(|&(expr, _)| ctx.get_type(expr))); |
| 535 |
|
| 536 |
// Initiate the declaration if it wasn't previously initialized and inject builtins |
| 537 |
let declaration = self.lookup_function.entry(name.clone()).or_insert_with(|| { |
| 538 |
variations |= BuiltinVariations::STANDARD; |
| 539 |
Default::default() |
| 540 |
}); |
| 541 |
inject_builtin(declaration, ctx.module, &name, variations); |
| 542 |
|
| 543 |
// Borrow again but without mutability, at this point a declaration is guaranteed |
| 544 |
let declaration = self.lookup_function.get(&name).unwrap(); |
| 545 |
|
| 546 |
// Possibly contains the overload to be used in the call |
| 547 |
let mut maybe_overload = None; |
| 548 |
// The conversions needed for the best analyzed overload, this is initialized all to |
| 549 |
// `NONE` to make sure that conversions always pass the first time without ambiguity |
| 550 |
let mut old_conversions = vec![Conversion::None; args.len()]; |
| 551 |
// Tracks whether the comparison between overloads lead to an ambiguity |
| 552 |
let mut ambiguous = false; |
| 553 |
|
| 554 |
// Iterate over all the available overloads to select either an exact match or a |
| 555 |
// overload which has suitable implicit conversions |
| 556 |
'outer: for (overload_idx, overload) in declaration.overloads.iter().enumerate() { |
| 557 |
// If the overload and the function call don't have the same number of arguments |
| 558 |
// continue to the next overload |
| 559 |
if args.len() != overload.parameters.len() { |
| 560 |
continue; |
| 561 |
} |
| 562 |
|
| 563 |
log::trace!("Testing overload {}", overload_idx); |
| 564 |
|
| 565 |
// Stores whether the current overload matches exactly the function call |
| 566 |
let mut exact = true; |
| 567 |
// State of the selection |
| 568 |
// If None we still don't know what is the best overload |
| 569 |
// If Some(true) the new overload is better |
| 570 |
// If Some(false) the old overload is better |
| 571 |
let mut superior = None; |
| 572 |
// Store the conversions for the current overload so that later they can replace the |
| 573 |
// conversions used for querying the best overload |
| 574 |
let mut new_conversions = vec![Conversion::None; args.len()]; |
| 575 |
|
| 576 |
// Loop through the overload parameters and check if the current overload is better |
| 577 |
// compared to the previous best overload. |
| 578 |
for (i, overload_parameter) in overload.parameters.iter().enumerate() { |
| 579 |
let call_argument = &args[i]; |
| 580 |
let parameter_info = &overload.parameters_info[i]; |
| 581 |
|
| 582 |
// If the image is used in the overload as a depth texture convert it |
| 583 |
// before comparing, otherwise exact matches wouldn't be reported |
| 584 |
if parameter_info.depth { |
| 585 |
sampled_to_depth(ctx, call_argument.0, call_argument.1, &mut self.errors); |
| 586 |
ctx.invalidate_expression(call_argument.0, call_argument.1)? |
| 587 |
} |
| 588 |
|
| 589 |
ctx.typifier_grow(call_argument.0, call_argument.1)?; |
| 590 |
|
| 591 |
let overload_param_ty = &ctx.module.types[*overload_parameter].inner; |
| 592 |
let call_arg_ty = ctx.get_type(call_argument.0); |
| 593 |
|
| 594 |
log::trace!( |
| 595 |
"Testing parameter {}\n\tOverload = {:?}\n\tCall = {:?}", |
| 596 |
i, |
| 597 |
overload_param_ty, |
| 598 |
call_arg_ty |
| 599 |
); |
| 600 |
|
| 601 |
// Storage images cannot be directly compared since while the access is part of the |
| 602 |
// type in naga's IR, in glsl they are a qualifier and don't enter in the match as |
| 603 |
// long as the access needed is satisfied. |
| 604 |
if let ( |
| 605 |
&TypeInner::Image { |
| 606 |
class: |
| 607 |
crate::ImageClass::Storage { |
| 608 |
format: overload_format, |
| 609 |
access: overload_access, |
| 610 |
}, |
| 611 |
dim: overload_dim, |
| 612 |
arrayed: overload_arrayed, |
| 613 |
}, |
| 614 |
&TypeInner::Image { |
| 615 |
class: |
| 616 |
crate::ImageClass::Storage { |
| 617 |
format: call_format, |
| 618 |
access: call_access, |
| 619 |
}, |
| 620 |
dim: call_dim, |
| 621 |
arrayed: call_arrayed, |
| 622 |
}, |
| 623 |
) = (overload_param_ty, call_arg_ty) |
| 624 |
{ |
| 625 |
// Images size must match otherwise the overload isn't what we want |
| 626 |
let good_size = call_dim == overload_dim && call_arrayed == overload_arrayed; |
| 627 |
// Glsl requires the formats to strictly match unless you are builtin |
| 628 |
// function overload and have not been replaced, in which case we only |
| 629 |
// check that the format scalar kind matches |
| 630 |
let good_format = overload_format == call_format |
| 631 |
|| (overload.internal |
| 632 |
&& Scalar::from(overload_format) == Scalar::from(call_format)); |
| 633 |
if !(good_size && good_format) { |
| 634 |
continue 'outer; |
| 635 |
} |
| 636 |
|
| 637 |
// While storage access mismatch is an error it isn't one that causes |
| 638 |
// the overload matching to fail so we defer the error and consider |
| 639 |
// that the images match exactly |
| 640 |
if !call_access.contains(overload_access) { |
| 641 |
self.errors.push(Error { |
| 642 |
kind: ErrorKind::SemanticError( |
| 643 |
format!( |
| 644 |
"'{}': image needs {:?} access but only {:?} was provided", |
| 645 |
name, overload_access, call_access |
| 646 |
) |
| 647 |
.into(), |
| 648 |
), |
| 649 |
meta, |
| 650 |
}); |
| 651 |
} |
| 652 |
|
| 653 |
// The images satisfy the conditions to be considered as an exact match |
| 654 |
new_conversions[i] = Conversion::Exact; |
| 655 |
continue; |
| 656 |
} else if overload_param_ty == call_arg_ty { |
| 657 |
// If the types match there's no need to check for conversions so continue |
| 658 |
new_conversions[i] = Conversion::Exact; |
| 659 |
continue; |
| 660 |
} |
| 661 |
|
| 662 |
// Glsl defines that inout follows both the conversions for input parameters and |
| 663 |
// output parameters, this means that the type must have a conversion from both the |
| 664 |
// call argument to the function parameter and the function parameter to the call |
| 665 |
// argument, the only way this is possible is for the conversion to be an identity |
| 666 |
// (i.e. call argument = function parameter) |
| 667 |
if let ParameterQualifier::InOut = parameter_info.qualifier { |
| 668 |
continue 'outer; |
| 669 |
} |
| 670 |
|
| 671 |
// The function call argument and the function definition |
| 672 |
// parameter are not equal at this point, so we need to try |
| 673 |
// implicit conversions. |
| 674 |
// |
| 675 |
// Now there are two cases, the argument is defined as a normal |
| 676 |
// parameter (`in` or `const`), in this case an implicit |
| 677 |
// conversion is made from the calling argument to the |
| 678 |
// definition argument. If the parameter is `out` the |
| 679 |
// opposite needs to be done, so the implicit conversion is made |
| 680 |
// from the definition argument to the calling argument. |
| 681 |
let maybe_conversion = if parameter_info.qualifier.is_lhs() { |
| 682 |
conversion(call_arg_ty, overload_param_ty) |
| 683 |
} else { |
| 684 |
conversion(overload_param_ty, call_arg_ty) |
| 685 |
}; |
| 686 |
|
| 687 |
let conversion = match maybe_conversion { |
| 688 |
Some(info) => info, |
| 689 |
None => continue 'outer, |
| 690 |
}; |
| 691 |
|
| 692 |
// At this point a conversion will be needed so the overload no longer |
| 693 |
// exactly matches the call arguments |
| 694 |
exact = false; |
| 695 |
|
| 696 |
// Compare the conversions needed for this overload parameter to that of the |
| 697 |
// last overload analyzed respective parameter, the value is: |
| 698 |
// - `true` when the new overload argument has a better conversion |
| 699 |
// - `false` when the old overload argument has a better conversion |
| 700 |
let best_arg = match (conversion, old_conversions[i]) { |
| 701 |
// An exact match is always better, we don't need to check this for the |
| 702 |
// current overload since it was checked earlier |
| 703 |
(_, Conversion::Exact) => false, |
| 704 |
// No overload was yet analyzed so this one is the best yet |
| 705 |
(_, Conversion::None) => true, |
| 706 |
// A conversion from a float to a double is the best possible conversion |
| 707 |
(Conversion::FloatToDouble, _) => true, |
| 708 |
(_, Conversion::FloatToDouble) => false, |
| 709 |
// A conversion from a float to an integer is preferred than one |
| 710 |
// from double to an integer |
| 711 |
(Conversion::IntToFloat, Conversion::IntToDouble) => true, |
| 712 |
(Conversion::IntToDouble, Conversion::IntToFloat) => false, |
| 713 |
// This case handles things like no conversion and exact which were already |
| 714 |
// treated and other cases which no conversion is better than the other |
| 715 |
_ => continue, |
| 716 |
}; |
| 717 |
|
| 718 |
// Check if the best parameter corresponds to the current selected overload |
| 719 |
// to pass to the next comparison, if this isn't true mark it as ambiguous |
| 720 |
match best_arg { |
| 721 |
true => match superior { |
| 722 |
Some(false) => ambiguous = true, |
| 723 |
_ => { |
| 724 |
superior = Some(true); |
| 725 |
new_conversions[i] = conversion |
| 726 |
} |
| 727 |
}, |
| 728 |
false => match superior { |
| 729 |
Some(true) => ambiguous = true, |
| 730 |
_ => superior = Some(false), |
| 731 |
}, |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
// The overload matches exactly the function call so there's no ambiguity (since |
| 736 |
// repeated overload aren't allowed) and the current overload is selected, no |
| 737 |
// further querying is needed. |
| 738 |
if exact { |
| 739 |
maybe_overload = Some(overload); |
| 740 |
ambiguous = false; |
| 741 |
break; |
| 742 |
} |
| 743 |
|
| 744 |
match superior { |
| 745 |
// New overload is better keep it |
| 746 |
Some(true) => { |
| 747 |
maybe_overload = Some(overload); |
| 748 |
// Replace the conversions |
| 749 |
old_conversions = new_conversions; |
| 750 |
} |
| 751 |
// Old overload is better do nothing |
| 752 |
Some(false) => {} |
| 753 |
// No overload was better than the other this can be caused |
| 754 |
// when all conversions are ambiguous in which the overloads themselves are |
| 755 |
// ambiguous. |
| 756 |
None => { |
| 757 |
ambiguous = true; |
| 758 |
// Assign the new overload, this helps ensures that in this case of |
| 759 |
// ambiguity the parsing won't end immediately and allow for further |
| 760 |
// collection of errors. |
| 761 |
maybe_overload = Some(overload); |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
if ambiguous { |
| 767 |
self.errors.push(Error { |
| 768 |
kind: ErrorKind::SemanticError( |
| 769 |
format!("Ambiguous best function for '{name}'").into(), |
| 770 |
), |
| 771 |
meta, |
| 772 |
}) |
| 773 |
} |
| 774 |
|
| 775 |
let overload = maybe_overload.ok_or_else(|| Error { |
| 776 |
kind: ErrorKind::SemanticError(format!("Unknown function '{name}'").into()), |
| 777 |
meta, |
| 778 |
})?; |
| 779 |
|
| 780 |
let parameters_info = overload.parameters_info.clone(); |
| 781 |
let parameters = overload.parameters.clone(); |
| 782 |
let is_void = overload.void; |
| 783 |
let kind = overload.kind; |
| 784 |
|
| 785 |
let mut arguments = Vec::with_capacity(args.len()); |
| 786 |
let mut proxy_writes = Vec::new(); |
| 787 |
|
| 788 |
// Iterate through the function call arguments applying transformations as needed |
| 789 |
for (((parameter_info, call_argument), expr), parameter) in parameters_info |
| 790 |
.iter() |
| 791 |
.zip(&args) |
| 792 |
.zip(raw_args) |
| 793 |
.zip(¶meters) |
| 794 |
{ |
| 795 |
if parameter_info.qualifier.is_lhs() { |
| 796 |
// Reprocess argument in LHS position |
| 797 |
let (handle, meta) = ctx.lower_expect_inner(stmt, self, *expr, ExprPos::Lhs)?; |
| 798 |
|
| 799 |
self.process_lhs_argument( |
| 800 |
ctx, |
| 801 |
meta, |
| 802 |
*parameter, |
| 803 |
parameter_info, |
| 804 |
handle, |
| 805 |
call_argument, |
| 806 |
&mut proxy_writes, |
| 807 |
&mut arguments, |
| 808 |
)?; |
| 809 |
|
| 810 |
continue; |
| 811 |
} |
| 812 |
|
| 813 |
let (mut handle, meta) = *call_argument; |
| 814 |
|
| 815 |
let scalar_comps = scalar_components(&ctx.module.types[*parameter].inner); |
| 816 |
|
| 817 |
// Apply implicit conversions as needed |
| 818 |
if let Some(scalar) = scalar_comps { |
| 819 |
ctx.implicit_conversion(&mut handle, meta, scalar)?; |
| 820 |
} |
| 821 |
|
| 822 |
arguments.push(handle) |
| 823 |
} |
| 824 |
|
| 825 |
match kind { |
| 826 |
FunctionKind::Call(function) => { |
| 827 |
ctx.emit_end(); |
| 828 |
|
| 829 |
let result = if !is_void { |
| 830 |
Some(ctx.add_expression(Expression::CallResult(function), meta)?) |
| 831 |
} else { |
| 832 |
None |
| 833 |
}; |
| 834 |
|
| 835 |
ctx.body.push( |
| 836 |
Statement::Call { |
| 837 |
function, |
| 838 |
arguments, |
| 839 |
result, |
| 840 |
}, |
| 841 |
meta, |
| 842 |
); |
| 843 |
|
| 844 |
ctx.emit_start(); |
| 845 |
|
| 846 |
// Write back all the variables that were scheduled to their original place |
| 847 |
for proxy_write in proxy_writes { |
| 848 |
let mut value = ctx.add_expression( |
| 849 |
Expression::Load { |
| 850 |
pointer: proxy_write.value, |
| 851 |
}, |
| 852 |
meta, |
| 853 |
)?; |
| 854 |
|
| 855 |
if let Some(scalar) = proxy_write.convert { |
| 856 |
ctx.conversion(&mut value, meta, scalar)?; |
| 857 |
} |
| 858 |
|
| 859 |
ctx.emit_restart(); |
| 860 |
|
| 861 |
ctx.body.push( |
| 862 |
Statement::Store { |
| 863 |
pointer: proxy_write.target, |
| 864 |
value, |
| 865 |
}, |
| 866 |
meta, |
| 867 |
); |
| 868 |
} |
| 869 |
|
| 870 |
Ok(result) |
| 871 |
} |
| 872 |
FunctionKind::Macro(builtin) => builtin.call(self, ctx, arguments.as_mut_slice(), meta), |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
/// Processes a function call argument that appears in place of an output |
| 877 |
/// parameter. |
| 878 |
#[allow(clippy::too_many_arguments)] |
| 879 |
fn process_lhs_argument( |
| 880 |
&mut self, |
| 881 |
ctx: &mut Context, |
| 882 |
meta: Span, |
| 883 |
parameter_ty: Handle<Type>, |
| 884 |
parameter_info: &ParameterInfo, |
| 885 |
original: Handle<Expression>, |
| 886 |
call_argument: &(Handle<Expression>, Span), |
| 887 |
proxy_writes: &mut Vec<ProxyWrite>, |
| 888 |
arguments: &mut Vec<Handle<Expression>>, |
| 889 |
) -> Result<()> { |
| 890 |
let original_ty = ctx.resolve_type(original, meta)?; |
| 891 |
let original_pointer_space = original_ty.pointer_space(); |
| 892 |
|
| 893 |
// The type of a possible spill variable needed for a proxy write |
| 894 |
let mut maybe_ty = match *original_ty { |
| 895 |
// If the argument is to be passed as a pointer but the type of the |
| 896 |
// expression returns a vector it must mean that it was for example |
| 897 |
// swizzled and it must be spilled into a local before calling |
| 898 |
TypeInner::Vector { size, scalar } => Some(ctx.module.types.insert( |
| 899 |
Type { |
| 900 |
name: None, |
| 901 |
inner: TypeInner::Vector { size, scalar }, |
| 902 |
}, |
| 903 |
Span::default(), |
| 904 |
)), |
| 905 |
// If the argument is a pointer whose address space isn't `Function`, an |
| 906 |
// indirection through a local variable is needed to align the address |
| 907 |
// spaces of the call argument and the overload parameter. |
| 908 |
TypeInner::Pointer { base, space } if space != AddressSpace::Function => Some(base), |
| 909 |
TypeInner::ValuePointer { |
| 910 |
size, |
| 911 |
scalar, |
| 912 |
space, |
| 913 |
} if space != AddressSpace::Function => { |
| 914 |
let inner = match size { |
| 915 |
Some(size) => TypeInner::Vector { size, scalar }, |
| 916 |
None => TypeInner::Scalar(scalar), |
| 917 |
}; |
| 918 |
|
| 919 |
Some( |
| 920 |
ctx.module |
| 921 |
.types |
| 922 |
.insert(Type { name: None, inner }, Span::default()), |
| 923 |
) |
| 924 |
} |
| 925 |
_ => None, |
| 926 |
}; |
| 927 |
|
| 928 |
// Since the original expression might be a pointer and we want a value |
| 929 |
// for the proxy writes, we might need to load the pointer. |
| 930 |
let value = if original_pointer_space.is_some() { |
| 931 |
ctx.add_expression(Expression::Load { pointer: original }, Span::default())? |
| 932 |
} else { |
| 933 |
original |
| 934 |
}; |
| 935 |
|
| 936 |
ctx.typifier_grow(call_argument.0, call_argument.1)?; |
| 937 |
|
| 938 |
let overload_param_ty = &ctx.module.types[parameter_ty].inner; |
| 939 |
let call_arg_ty = ctx.get_type(call_argument.0); |
| 940 |
let needs_conversion = call_arg_ty != overload_param_ty; |
| 941 |
|
| 942 |
let arg_scalar_comps = scalar_components(call_arg_ty); |
| 943 |
|
| 944 |
// Since output parameters also allow implicit conversions from the |
| 945 |
// parameter to the argument, we need to spill the conversion to a |
| 946 |
// variable and create a proxy write for the original variable. |
| 947 |
if needs_conversion { |
| 948 |
maybe_ty = Some(parameter_ty); |
| 949 |
} |
| 950 |
|
| 951 |
if let Some(ty) = maybe_ty { |
| 952 |
// Create the spill variable |
| 953 |
let spill_var = ctx.locals.append( |
| 954 |
LocalVariable { |
| 955 |
name: None, |
| 956 |
ty, |
| 957 |
init: None, |
| 958 |
}, |
| 959 |
Span::default(), |
| 960 |
); |
| 961 |
let spill_expr = |
| 962 |
ctx.add_expression(Expression::LocalVariable(spill_var), Span::default())?; |
| 963 |
|
| 964 |
// If the argument is also copied in we must store the value of the |
| 965 |
// original variable to the spill variable. |
| 966 |
if let ParameterQualifier::InOut = parameter_info.qualifier { |
| 967 |
ctx.body.push( |
| 968 |
Statement::Store { |
| 969 |
pointer: spill_expr, |
| 970 |
value, |
| 971 |
}, |
| 972 |
Span::default(), |
| 973 |
); |
| 974 |
} |
| 975 |
|
| 976 |
// Add the spill variable as an argument to the function call |
| 977 |
arguments.push(spill_expr); |
| 978 |
|
| 979 |
let convert = if needs_conversion { |
| 980 |
arg_scalar_comps |
| 981 |
} else { |
| 982 |
None |
| 983 |
}; |
| 984 |
|
| 985 |
// Register the temporary local to be written back to it's original |
| 986 |
// place after the function call |
| 987 |
if let Expression::Swizzle { |
| 988 |
size, |
| 989 |
mut vector, |
| 990 |
pattern, |
| 991 |
} = ctx.expressions[original] |
| 992 |
{ |
| 993 |
if let Expression::Load { pointer } = ctx.expressions[vector] { |
| 994 |
vector = pointer; |
| 995 |
} |
| 996 |
|
| 997 |
for (i, component) in pattern.iter().take(size as usize).enumerate() { |
| 998 |
let original = ctx.add_expression( |
| 999 |
Expression::AccessIndex { |
| 1000 |
base: vector, |
| 1001 |
index: *component as u32, |
| 1002 |
}, |
| 1003 |
Span::default(), |
| 1004 |
)?; |
| 1005 |
|
| 1006 |
let spill_component = ctx.add_expression( |
| 1007 |
Expression::AccessIndex { |
| 1008 |
base: spill_expr, |
| 1009 |
index: i as u32, |
| 1010 |
}, |
| 1011 |
Span::default(), |
| 1012 |
)?; |
| 1013 |
|
| 1014 |
proxy_writes.push(ProxyWrite { |
| 1015 |
target: original, |
| 1016 |
value: spill_component, |
| 1017 |
convert, |
| 1018 |
}); |
| 1019 |
} |
| 1020 |
} else { |
| 1021 |
proxy_writes.push(ProxyWrite { |
| 1022 |
target: original, |
| 1023 |
value: spill_expr, |
| 1024 |
convert, |
| 1025 |
}); |
| 1026 |
} |
| 1027 |
} else { |
| 1028 |
arguments.push(original); |
| 1029 |
} |
| 1030 |
|
| 1031 |
Ok(()) |
| 1032 |
} |
| 1033 |
|
| 1034 |
pub(crate) fn add_function( |
| 1035 |
&mut self, |
| 1036 |
mut ctx: Context, |
| 1037 |
name: String, |
| 1038 |
result: Option<FunctionResult>, |
| 1039 |
meta: Span, |
| 1040 |
) { |
| 1041 |
ensure_block_returns(&mut ctx.body); |
| 1042 |
|
| 1043 |
let void = result.is_none(); |
| 1044 |
|
| 1045 |
// Check if the passed arguments require any special variations |
| 1046 |
let mut variations = builtin_required_variations( |
| 1047 |
ctx.parameters |
| 1048 |
.iter() |
| 1049 |
.map(|&arg| &ctx.module.types[arg].inner), |
| 1050 |
); |
| 1051 |
|
| 1052 |
// Initiate the declaration if it wasn't previously initialized and inject builtins |
| 1053 |
let declaration = self.lookup_function.entry(name.clone()).or_insert_with(|| { |
| 1054 |
variations |= BuiltinVariations::STANDARD; |
| 1055 |
Default::default() |
| 1056 |
}); |
| 1057 |
inject_builtin(declaration, ctx.module, &name, variations); |
| 1058 |
|
| 1059 |
let Context { |
| 1060 |
expressions, |
| 1061 |
locals, |
| 1062 |
arguments, |
| 1063 |
parameters, |
| 1064 |
parameters_info, |
| 1065 |
body, |
| 1066 |
module, |
| 1067 |
.. |
| 1068 |
} = ctx; |
| 1069 |
|
| 1070 |
let function = Function { |
| 1071 |
name: Some(name), |
| 1072 |
arguments, |
| 1073 |
result, |
| 1074 |
local_variables: locals, |
| 1075 |
expressions, |
| 1076 |
named_expressions: crate::NamedExpressions::default(), |
| 1077 |
body, |
| 1078 |
diagnostic_filter_leaf: None, |
| 1079 |
}; |
| 1080 |
|
| 1081 |
'outer: for decl in declaration.overloads.iter_mut() { |
| 1082 |
if parameters.len() != decl.parameters.len() { |
| 1083 |
continue; |
| 1084 |
} |
| 1085 |
|
| 1086 |
for (new_parameter, old_parameter) in parameters.iter().zip(decl.parameters.iter()) { |
| 1087 |
let new_inner = &module.types[*new_parameter].inner; |
| 1088 |
let old_inner = &module.types[*old_parameter].inner; |
| 1089 |
|
| 1090 |
if new_inner != old_inner { |
| 1091 |
continue 'outer; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
if decl.defined { |
| 1096 |
return self.errors.push(Error { |
| 1097 |
kind: ErrorKind::SemanticError("Function already defined".into()), |
| 1098 |
meta, |
| 1099 |
}); |
| 1100 |
} |
| 1101 |
|
| 1102 |
decl.defined = true; |
| 1103 |
decl.parameters_info = parameters_info; |
| 1104 |
match decl.kind { |
| 1105 |
FunctionKind::Call(handle) => *module.functions.get_mut(handle) = function, |
| 1106 |
FunctionKind::Macro(_) => { |
| 1107 |
let handle = module.functions.append(function, meta); |
| 1108 |
decl.kind = FunctionKind::Call(handle) |
| 1109 |
} |
| 1110 |
} |
| 1111 |
return; |
| 1112 |
} |
| 1113 |
|
| 1114 |
let handle = module.functions.append(function, meta); |
| 1115 |
declaration.overloads.push(Overload { |
| 1116 |
parameters, |
| 1117 |
parameters_info, |
| 1118 |
kind: FunctionKind::Call(handle), |
| 1119 |
defined: true, |
| 1120 |
internal: false, |
| 1121 |
void, |
| 1122 |
}); |
| 1123 |
} |
| 1124 |
|
| 1125 |
pub(crate) fn add_prototype( |
| 1126 |
&mut self, |
| 1127 |
ctx: Context, |
| 1128 |
name: String, |
| 1129 |
result: Option<FunctionResult>, |
| 1130 |
meta: Span, |
| 1131 |
) { |
| 1132 |
let void = result.is_none(); |
| 1133 |
|
| 1134 |
// Check if the passed arguments require any special variations |
| 1135 |
let mut variations = builtin_required_variations( |
| 1136 |
ctx.parameters |
| 1137 |
.iter() |
| 1138 |
.map(|&arg| &ctx.module.types[arg].inner), |
| 1139 |
); |
| 1140 |
|
| 1141 |
// Initiate the declaration if it wasn't previously initialized and inject builtins |
| 1142 |
let declaration = self.lookup_function.entry(name.clone()).or_insert_with(|| { |
| 1143 |
variations |= BuiltinVariations::STANDARD; |
| 1144 |
Default::default() |
| 1145 |
}); |
| 1146 |
inject_builtin(declaration, ctx.module, &name, variations); |
| 1147 |
|
| 1148 |
let Context { |
| 1149 |
arguments, |
| 1150 |
parameters, |
| 1151 |
parameters_info, |
| 1152 |
module, |
| 1153 |
.. |
| 1154 |
} = ctx; |
| 1155 |
|
| 1156 |
let function = Function { |
| 1157 |
name: Some(name), |
| 1158 |
arguments, |
| 1159 |
result, |
| 1160 |
..Default::default() |
| 1161 |
}; |
| 1162 |
|
| 1163 |
'outer: for decl in declaration.overloads.iter() { |
| 1164 |
if parameters.len() != decl.parameters.len() { |
| 1165 |
continue; |
| 1166 |
} |
| 1167 |
|
| 1168 |
for (new_parameter, old_parameter) in parameters.iter().zip(decl.parameters.iter()) { |
| 1169 |
let new_inner = &module.types[*new_parameter].inner; |
| 1170 |
let old_inner = &module.types[*old_parameter].inner; |
| 1171 |
|
| 1172 |
if new_inner != old_inner { |
| 1173 |
continue 'outer; |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
return self.errors.push(Error { |
| 1178 |
kind: ErrorKind::SemanticError("Prototype already defined".into()), |
| 1179 |
meta, |
| 1180 |
}); |
| 1181 |
} |
| 1182 |
|
| 1183 |
let handle = module.functions.append(function, meta); |
| 1184 |
declaration.overloads.push(Overload { |
| 1185 |
parameters, |
| 1186 |
parameters_info, |
| 1187 |
kind: FunctionKind::Call(handle), |
| 1188 |
defined: false, |
| 1189 |
internal: false, |
| 1190 |
void, |
| 1191 |
}); |
| 1192 |
} |
| 1193 |
|
| 1194 |
/// Create a Naga [`EntryPoint`] that calls the GLSL `main` function. |
| 1195 |
/// |
| 1196 |
/// We compile the GLSL `main` function as an ordinary Naga [`Function`]. |
| 1197 |
/// This function synthesizes a Naga [`EntryPoint`] to call that. |
| 1198 |
/// |
| 1199 |
/// Each GLSL input and output variable (including builtins) becomes a Naga |
| 1200 |
/// [`GlobalVariable`]s in the [`Private`] address space, which `main` can |
| 1201 |
/// access in the usual way. |
| 1202 |
/// |
| 1203 |
/// The `EntryPoint` we synthesize here has an argument for each GLSL input |
| 1204 |
/// variable, and returns a struct with a member for each GLSL output |
| 1205 |
/// variable. The entry point contains code to: |
| 1206 |
/// |
| 1207 |
/// - copy its arguments into the Naga globals representing the GLSL input |
| 1208 |
/// variables, |
| 1209 |
/// |
| 1210 |
/// - call the Naga `Function` representing the GLSL `main` function, and then |
| 1211 |
/// |
| 1212 |
/// - build its return value from whatever values the GLSL `main` left in |
| 1213 |
/// the Naga globals representing GLSL `output` variables. |
| 1214 |
/// |
| 1215 |
/// Upon entry, [`ctx.body`] should contain code, accumulated by prior calls |
| 1216 |
/// to [`ParsingContext::parse_external_declaration`][pxd], to initialize |
| 1217 |
/// private global variables as needed. This code gets spliced into the |
| 1218 |
/// entry point before the call to `main`. |
| 1219 |
/// |
| 1220 |
/// [`GlobalVariable`]: crate::GlobalVariable |
| 1221 |
/// [`Private`]: crate::AddressSpace::Private |
| 1222 |
/// [`ctx.body`]: Context::body |
| 1223 |
/// [pxd]: super::ParsingContext::parse_external_declaration |
| 1224 |
pub(crate) fn add_entry_point( |
| 1225 |
&mut self, |
| 1226 |
function: Handle<Function>, |
| 1227 |
mut ctx: Context, |
| 1228 |
) -> Result<()> { |
| 1229 |
let mut arguments = Vec::new(); |
| 1230 |
|
| 1231 |
let body = Block::with_capacity( |
| 1232 |
// global init body |
| 1233 |
ctx.body.len() + |
| 1234 |
// prologue and epilogue |
| 1235 |
self.entry_args.len() * 2 |
| 1236 |
// Call, Emit for composing struct and return |
| 1237 |
+ 3, |
| 1238 |
); |
| 1239 |
|
| 1240 |
let global_init_body = core::mem::replace(&mut ctx.body, body); |
| 1241 |
|
| 1242 |
for arg in self.entry_args.iter() { |
| 1243 |
if arg.storage != StorageQualifier::Input { |
| 1244 |
continue; |
| 1245 |
} |
| 1246 |
|
| 1247 |
let pointer = ctx |
| 1248 |
.expressions |
| 1249 |
.append(Expression::GlobalVariable(arg.handle), Default::default()); |
| 1250 |
ctx.local_expression_kind_tracker |
| 1251 |
.insert(pointer, crate::proc::ExpressionKind::Runtime); |
| 1252 |
|
| 1253 |
let ty = ctx.module.global_variables[arg.handle].ty; |
| 1254 |
|
| 1255 |
ctx.arg_type_walker( |
| 1256 |
arg.name.clone(), |
| 1257 |
arg.binding.clone(), |
| 1258 |
pointer, |
| 1259 |
ty, |
| 1260 |
&mut |ctx, name, pointer, ty, binding| { |
| 1261 |
let idx = arguments.len() as u32; |
| 1262 |
|
| 1263 |
arguments.push(FunctionArgument { |
| 1264 |
name, |
| 1265 |
ty, |
| 1266 |
binding: Some(binding), |
| 1267 |
}); |
| 1268 |
|
| 1269 |
let value = ctx |
| 1270 |
.expressions |
| 1271 |
.append(Expression::FunctionArgument(idx), Default::default()); |
| 1272 |
ctx.local_expression_kind_tracker |
| 1273 |
.insert(value, crate::proc::ExpressionKind::Runtime); |
| 1274 |
ctx.body |
| 1275 |
.push(Statement::Store { pointer, value }, Default::default()); |
| 1276 |
}, |
| 1277 |
)? |
| 1278 |
} |
| 1279 |
|
| 1280 |
ctx.body.extend_block(global_init_body); |
| 1281 |
|
| 1282 |
ctx.body.push( |
| 1283 |
Statement::Call { |
| 1284 |
function, |
| 1285 |
arguments: Vec::new(), |
| 1286 |
result: None, |
| 1287 |
}, |
| 1288 |
Default::default(), |
| 1289 |
); |
| 1290 |
|
| 1291 |
let mut span = 0; |
| 1292 |
let mut members = Vec::new(); |
| 1293 |
let mut components = Vec::new(); |
| 1294 |
|
| 1295 |
for arg in self.entry_args.iter() { |
| 1296 |
if arg.storage != StorageQualifier::Output { |
| 1297 |
continue; |
| 1298 |
} |
| 1299 |
|
| 1300 |
let pointer = ctx |
| 1301 |
.expressions |
| 1302 |
.append(Expression::GlobalVariable(arg.handle), Default::default()); |
| 1303 |
ctx.local_expression_kind_tracker |
| 1304 |
.insert(pointer, crate::proc::ExpressionKind::Runtime); |
| 1305 |
|
| 1306 |
let ty = ctx.module.global_variables[arg.handle].ty; |
| 1307 |
|
| 1308 |
ctx.arg_type_walker( |
| 1309 |
arg.name.clone(), |
| 1310 |
arg.binding.clone(), |
| 1311 |
pointer, |
| 1312 |
ty, |
| 1313 |
&mut |ctx, name, pointer, ty, binding| { |
| 1314 |
members.push(StructMember { |
| 1315 |
name, |
| 1316 |
ty, |
| 1317 |
binding: Some(binding), |
| 1318 |
offset: span, |
| 1319 |
}); |
| 1320 |
|
| 1321 |
span += ctx.module.types[ty].inner.size(ctx.module.to_ctx()); |
| 1322 |
|
| 1323 |
let len = ctx.expressions.len(); |
| 1324 |
let load = ctx |
| 1325 |
.expressions |
| 1326 |
.append(Expression::Load { pointer }, Default::default()); |
| 1327 |
ctx.local_expression_kind_tracker |
| 1328 |
.insert(load, crate::proc::ExpressionKind::Runtime); |
| 1329 |
ctx.body.push( |
| 1330 |
Statement::Emit(ctx.expressions.range_from(len)), |
| 1331 |
Default::default(), |
| 1332 |
); |
| 1333 |
components.push(load) |
| 1334 |
}, |
| 1335 |
)? |
| 1336 |
} |
| 1337 |
|
| 1338 |
let (ty, value) = if !components.is_empty() { |
| 1339 |
let ty = ctx.module.types.insert( |
| 1340 |
Type { |
| 1341 |
name: None, |
| 1342 |
inner: TypeInner::Struct { members, span }, |
| 1343 |
}, |
| 1344 |
Default::default(), |
| 1345 |
); |
| 1346 |
|
| 1347 |
let len = ctx.expressions.len(); |
| 1348 |
let res = ctx |
| 1349 |
.expressions |
| 1350 |
.append(Expression::Compose { ty, components }, Default::default()); |
| 1351 |
ctx.local_expression_kind_tracker |
| 1352 |
.insert(res, crate::proc::ExpressionKind::Runtime); |
| 1353 |
ctx.body.push( |
| 1354 |
Statement::Emit(ctx.expressions.range_from(len)), |
| 1355 |
Default::default(), |
| 1356 |
); |
| 1357 |
|
| 1358 |
(Some(ty), Some(res)) |
| 1359 |
} else { |
| 1360 |
(None, None) |
| 1361 |
}; |
| 1362 |
|
| 1363 |
ctx.body |
| 1364 |
.push(Statement::Return { value }, Default::default()); |
| 1365 |
|
| 1366 |
let Context { |
| 1367 |
body, expressions, .. |
| 1368 |
} = ctx; |
| 1369 |
|
| 1370 |
ctx.module.entry_points.push(EntryPoint { |
| 1371 |
name: "main".to_string(), |
| 1372 |
stage: self.meta.stage, |
| 1373 |
early_depth_test: Some(crate::EarlyDepthTest { conservative: None }) |
| 1374 |
.filter(|_| self.meta.early_fragment_tests), |
| 1375 |
workgroup_size: self.meta.workgroup_size, |
| 1376 |
workgroup_size_overrides: None, |
| 1377 |
function: Function { |
| 1378 |
arguments, |
| 1379 |
expressions, |
| 1380 |
body, |
| 1381 |
result: ty.map(|ty| FunctionResult { ty, binding: None }), |
| 1382 |
..Default::default() |
| 1383 |
}, |
| 1384 |
}); |
| 1385 |
|
| 1386 |
Ok(()) |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
impl Context<'_> { |
| 1391 |
/// Helper function for building the input/output interface of the entry point |
| 1392 |
/// |
| 1393 |
/// Calls `f` with the data of the entry point argument, flattening composite types |
| 1394 |
/// recursively |
| 1395 |
/// |
| 1396 |
/// The passed arguments to the callback are: |
| 1397 |
/// - The ctx |
| 1398 |
/// - The name |
| 1399 |
/// - The pointer expression to the global storage |
| 1400 |
/// - The handle to the type of the entry point argument |
| 1401 |
/// - The binding of the entry point argument |
| 1402 |
fn arg_type_walker( |
| 1403 |
&mut self, |
| 1404 |
name: Option<String>, |
| 1405 |
binding: crate::Binding, |
| 1406 |
pointer: Handle<Expression>, |
| 1407 |
ty: Handle<Type>, |
| 1408 |
f: &mut impl FnMut( |
| 1409 |
&mut Context, |
| 1410 |
Option<String>, |
| 1411 |
Handle<Expression>, |
| 1412 |
Handle<Type>, |
| 1413 |
crate::Binding, |
| 1414 |
), |
| 1415 |
) -> Result<()> { |
| 1416 |
match self.module.types[ty].inner { |
| 1417 |
// TODO: Better error reporting |
| 1418 |
// right now we just don't walk the array if the size isn't known at |
| 1419 |
// compile time and let validation catch it |
| 1420 |
TypeInner::Array { |
| 1421 |
base, |
| 1422 |
size: crate::ArraySize::Constant(size), |
| 1423 |
.. |
| 1424 |
} => { |
| 1425 |
let mut location = match binding { |
| 1426 |
crate::Binding::Location { location, .. } => location, |
| 1427 |
crate::Binding::BuiltIn(_) => return Ok(()), |
| 1428 |
}; |
| 1429 |
|
| 1430 |
let interpolation = |
| 1431 |
self.module.types[base] |
| 1432 |
.inner |
| 1433 |
.scalar_kind() |
| 1434 |
.map(|kind| match kind { |
| 1435 |
ScalarKind::Float => crate::Interpolation::Perspective, |
| 1436 |
_ => crate::Interpolation::Flat, |
| 1437 |
}); |
| 1438 |
|
| 1439 |
for index in 0..size.get() { |
| 1440 |
let member_pointer = self.add_expression( |
| 1441 |
Expression::AccessIndex { |
| 1442 |
base: pointer, |
| 1443 |
index, |
| 1444 |
}, |
| 1445 |
Span::default(), |
| 1446 |
)?; |
| 1447 |
|
| 1448 |
let binding = crate::Binding::Location { |
| 1449 |
location, |
| 1450 |
interpolation, |
| 1451 |
sampling: None, |
| 1452 |
blend_src: None, |
| 1453 |
}; |
| 1454 |
location += 1; |
| 1455 |
|
| 1456 |
self.arg_type_walker(name.clone(), binding, member_pointer, base, f)? |
| 1457 |
} |
| 1458 |
} |
| 1459 |
TypeInner::Struct { ref members, .. } => { |
| 1460 |
let mut location = match binding { |
| 1461 |
crate::Binding::Location { location, .. } => location, |
| 1462 |
crate::Binding::BuiltIn(_) => return Ok(()), |
| 1463 |
}; |
| 1464 |
|
| 1465 |
for (i, member) in members.clone().into_iter().enumerate() { |
| 1466 |
let member_pointer = self.add_expression( |
| 1467 |
Expression::AccessIndex { |
| 1468 |
base: pointer, |
| 1469 |
index: i as u32, |
| 1470 |
}, |
| 1471 |
Span::default(), |
| 1472 |
)?; |
| 1473 |
|
| 1474 |
let binding = match member.binding { |
| 1475 |
Some(binding) => binding, |
| 1476 |
None => { |
| 1477 |
let interpolation = self.module.types[member.ty] |
| 1478 |
.inner |
| 1479 |
.scalar_kind() |
| 1480 |
.map(|kind| match kind { |
| 1481 |
ScalarKind::Float => crate::Interpolation::Perspective, |
| 1482 |
_ => crate::Interpolation::Flat, |
| 1483 |
}); |
| 1484 |
let binding = crate::Binding::Location { |
| 1485 |
location, |
| 1486 |
interpolation, |
| 1487 |
sampling: None, |
| 1488 |
blend_src: None, |
| 1489 |
}; |
| 1490 |
location += 1; |
| 1491 |
binding |
| 1492 |
} |
| 1493 |
}; |
| 1494 |
|
| 1495 |
self.arg_type_walker(member.name, binding, member_pointer, member.ty, f)? |
| 1496 |
} |
| 1497 |
} |
| 1498 |
_ => f(self, name, pointer, ty, binding), |
| 1499 |
} |
| 1500 |
|
| 1501 |
Ok(()) |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
/// Helper enum containing the type of conversion need for a call |
| 1506 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
| 1507 |
enum Conversion { |
| 1508 |
/// No conversion needed |
| 1509 |
Exact, |
| 1510 |
/// Float to double conversion needed |
| 1511 |
FloatToDouble, |
| 1512 |
/// Int or uint to float conversion needed |
| 1513 |
IntToFloat, |
| 1514 |
/// Int or uint to double conversion needed |
| 1515 |
IntToDouble, |
| 1516 |
/// Other type of conversion needed |
| 1517 |
Other, |
| 1518 |
/// No conversion was yet registered |
| 1519 |
None, |
| 1520 |
} |
| 1521 |
|
| 1522 |
/// Helper function, returns the type of conversion from `source` to `target`, if a |
| 1523 |
/// conversion is not possible returns None. |
| 1524 |
fn conversion(target: &TypeInner, source: &TypeInner) -> Option<Conversion> { |
| 1525 |
use ScalarKind::*; |
| 1526 |
|
| 1527 |
// Gather the `ScalarKind` and scalar width from both the target and the source |
| 1528 |
let (target_scalar, source_scalar) = match (target, source) { |
| 1529 |
// Conversions between scalars are allowed |
| 1530 |
(&TypeInner::Scalar(tgt_scalar), &TypeInner::Scalar(src_scalar)) => { |
| 1531 |
(tgt_scalar, src_scalar) |
| 1532 |
} |
| 1533 |
// Conversions between vectors of the same size are allowed |
| 1534 |
( |
| 1535 |
&TypeInner::Vector { |
| 1536 |
size: tgt_size, |
| 1537 |
scalar: tgt_scalar, |
| 1538 |
}, |
| 1539 |
&TypeInner::Vector { |
| 1540 |
size: src_size, |
| 1541 |
scalar: src_scalar, |
| 1542 |
}, |
| 1543 |
) if tgt_size == src_size => (tgt_scalar, src_scalar), |
| 1544 |
// Conversions between matrices of the same size are allowed |
| 1545 |
( |
| 1546 |
&TypeInner::Matrix { |
| 1547 |
rows: tgt_rows, |
| 1548 |
columns: tgt_cols, |
| 1549 |
scalar: tgt_scalar, |
| 1550 |
}, |
| 1551 |
&TypeInner::Matrix { |
| 1552 |
rows: src_rows, |
| 1553 |
columns: src_cols, |
| 1554 |
scalar: src_scalar, |
| 1555 |
}, |
| 1556 |
) if tgt_cols == src_cols && tgt_rows == src_rows => (tgt_scalar, src_scalar), |
| 1557 |
_ => return None, |
| 1558 |
}; |
| 1559 |
|
| 1560 |
// Check if source can be converted into target, if this is the case then the type |
| 1561 |
// power of target must be higher than that of source |
| 1562 |
let target_power = type_power(target_scalar); |
| 1563 |
let source_power = type_power(source_scalar); |
| 1564 |
if target_power < source_power { |
| 1565 |
return None; |
| 1566 |
} |
| 1567 |
|
| 1568 |
Some(match (target_scalar, source_scalar) { |
| 1569 |
// A conversion from a float to a double is special |
| 1570 |
(Scalar::F64, Scalar::F32) => Conversion::FloatToDouble, |
| 1571 |
// A conversion from an integer to a float is special |
| 1572 |
( |
| 1573 |
Scalar::F32, |
| 1574 |
Scalar { |
| 1575 |
kind: Sint | Uint, |
| 1576 |
width: _, |
| 1577 |
}, |
| 1578 |
) => Conversion::IntToFloat, |
| 1579 |
// A conversion from an integer to a double is special |
| 1580 |
( |
| 1581 |
Scalar::F64, |
| 1582 |
Scalar { |
| 1583 |
kind: Sint | Uint, |
| 1584 |
width: _, |
| 1585 |
}, |
| 1586 |
) => Conversion::IntToDouble, |
| 1587 |
_ => Conversion::Other, |
| 1588 |
}) |
| 1589 |
} |
| 1590 |
|
| 1591 |
/// Helper method returning all the non standard builtin variations needed |
| 1592 |
/// to process the function call with the passed arguments |
| 1593 |
fn builtin_required_variations<'a>(args: impl Iterator<Item = &'a TypeInner>) -> BuiltinVariations { |
| 1594 |
let mut variations = BuiltinVariations::empty(); |
| 1595 |
|
| 1596 |
for ty in args { |
| 1597 |
match *ty { |
| 1598 |
TypeInner::ValuePointer { scalar, .. } |
| 1599 |
| TypeInner::Scalar(scalar) |
| 1600 |
| TypeInner::Vector { scalar, .. } |
| 1601 |
| TypeInner::Matrix { scalar, .. } => { |
| 1602 |
if scalar == Scalar::F64 { |
| 1603 |
variations |= BuiltinVariations::DOUBLE |
| 1604 |
} |
| 1605 |
} |
| 1606 |
TypeInner::Image { |
| 1607 |
dim, |
| 1608 |
arrayed, |
| 1609 |
class, |
| 1610 |
} => { |
| 1611 |
if dim == crate::ImageDimension::Cube && arrayed { |
| 1612 |
variations |= BuiltinVariations::CUBE_TEXTURES_ARRAY |
| 1613 |
} |
| 1614 |
|
| 1615 |
if dim == crate::ImageDimension::D2 && arrayed && class.is_multisampled() { |
| 1616 |
variations |= BuiltinVariations::D2_MULTI_TEXTURES_ARRAY |
| 1617 |
} |
| 1618 |
} |
| 1619 |
_ => {} |
| 1620 |
} |
| 1621 |
} |
| 1622 |
|
| 1623 |
variations |
| 1624 |
} |