| 1 |
use alloc::vec::Vec; |
| 2 |
|
| 3 |
use bit_set::BitSet; |
| 4 |
|
| 5 |
use super::{ |
| 6 |
analyzer::{FunctionInfo, GlobalUse}, |
| 7 |
Capabilities, Disalignment, FunctionError, ModuleInfo, PushConstantError, |
| 8 |
}; |
| 9 |
use crate::arena::{Handle, UniqueArena}; |
| 10 |
use crate::span::{AddSpan as _, MapErrWithSpan as _, SpanProvider as _, WithSpan}; |
| 11 |
|
| 12 |
const MAX_WORKGROUP_SIZE: u32 = 0x4000; |
| 13 |
|
| 14 |
#[derive(Clone, Debug, thiserror::Error)] |
| 15 |
#[cfg_attr(test, derive(PartialEq))] |
| 16 |
pub enum GlobalVariableError { |
| 17 |
#[error("Usage isn't compatible with address space {0:?}")] |
| 18 |
InvalidUsage(crate::AddressSpace), |
| 19 |
#[error("Type isn't compatible with address space {0:?}")] |
| 20 |
InvalidType(crate::AddressSpace), |
| 21 |
#[error("Type flags {seen:?} do not meet the required {required:?}")] |
| 22 |
MissingTypeFlags { |
| 23 |
required: super::TypeFlags, |
| 24 |
seen: super::TypeFlags, |
| 25 |
}, |
| 26 |
#[error("Capability {0:?} is not supported")] |
| 27 |
UnsupportedCapability(Capabilities), |
| 28 |
#[error("Binding decoration is missing or not applicable")] |
| 29 |
InvalidBinding, |
| 30 |
#[error("Alignment requirements for address space {0:?} are not met by {1:?}")] |
| 31 |
Alignment( |
| 32 |
crate::AddressSpace, |
| 33 |
Handle<crate::Type>, |
| 34 |
#[source] Disalignment, |
| 35 |
), |
| 36 |
#[error("Initializer must be an override-expression")] |
| 37 |
InitializerExprType, |
| 38 |
#[error("Initializer doesn't match the variable type")] |
| 39 |
InitializerType, |
| 40 |
#[error("Initializer can't be used with address space {0:?}")] |
| 41 |
InitializerNotAllowed(crate::AddressSpace), |
| 42 |
#[error("Storage address space doesn't support write-only access")] |
| 43 |
StorageAddressSpaceWriteOnlyNotSupported, |
| 44 |
#[error("Type is not valid for use as a push constant")] |
| 45 |
InvalidPushConstantType(#[source] PushConstantError), |
| 46 |
} |
| 47 |
|
| 48 |
#[derive(Clone, Debug, thiserror::Error)] |
| 49 |
#[cfg_attr(test, derive(PartialEq))] |
| 50 |
pub enum VaryingError { |
| 51 |
#[error("The type {0:?} does not match the varying")] |
| 52 |
InvalidType(Handle<crate::Type>), |
| 53 |
#[error("The type {0:?} cannot be used for user-defined entry point inputs or outputs")] |
| 54 |
NotIOShareableType(Handle<crate::Type>), |
| 55 |
#[error("Interpolation is not valid")] |
| 56 |
InvalidInterpolation, |
| 57 |
#[error("Cannot combine {interpolation:?} interpolation with the {sampling:?} sample type")] |
| 58 |
InvalidInterpolationSamplingCombination { |
| 59 |
interpolation: crate::Interpolation, |
| 60 |
sampling: crate::Sampling, |
| 61 |
}, |
| 62 |
#[error("Interpolation must be specified on vertex shader outputs and fragment shader inputs")] |
| 63 |
MissingInterpolation, |
| 64 |
#[error("Built-in {0:?} is not available at this stage")] |
| 65 |
InvalidBuiltInStage(crate::BuiltIn), |
| 66 |
#[error("Built-in type for {0:?} is invalid")] |
| 67 |
InvalidBuiltInType(crate::BuiltIn), |
| 68 |
#[error("Entry point arguments and return values must all have bindings")] |
| 69 |
MissingBinding, |
| 70 |
#[error("Struct member {0} is missing a binding")] |
| 71 |
MemberMissingBinding(u32), |
| 72 |
#[error("Multiple bindings at location {location} are present")] |
| 73 |
BindingCollision { location: u32 }, |
| 74 |
#[error("Multiple bindings use the same `blend_src` {blend_src}")] |
| 75 |
BindingCollisionBlendSrc { blend_src: u32 }, |
| 76 |
#[error("Built-in {0:?} is present more than once")] |
| 77 |
DuplicateBuiltIn(crate::BuiltIn), |
| 78 |
#[error("Capability {0:?} is not supported")] |
| 79 |
UnsupportedCapability(Capabilities), |
| 80 |
#[error("The attribute {0:?} is only valid as an output for stage {1:?}")] |
| 81 |
InvalidInputAttributeInStage(&'static str, crate::ShaderStage), |
| 82 |
#[error("The attribute {0:?} is not valid for stage {1:?}")] |
| 83 |
InvalidAttributeInStage(&'static str, crate::ShaderStage), |
| 84 |
#[error("The `blend_src` attribute can only be used on location 0, only indices 0 and 1 are valid. Location was {location}, index was {blend_src}.")] |
| 85 |
InvalidBlendSrcIndex { location: u32, blend_src: u32 }, |
| 86 |
#[error("If `blend_src` is used, there must be exactly two outputs both with location 0, one with `blend_src(0)` and the other with `blend_src(1)`.")] |
| 87 |
IncompleteBlendSrcUsage, |
| 88 |
#[error("If `blend_src` is used, both outputs must have the same type. `blend_src(0)` has type {blend_src_0_type:?} and `blend_src(1)` has type {blend_src_1_type:?}.")] |
| 89 |
BlendSrcOutputTypeMismatch { |
| 90 |
blend_src_0_type: Handle<crate::Type>, |
| 91 |
blend_src_1_type: Handle<crate::Type>, |
| 92 |
}, |
| 93 |
#[error("Workgroup size is multi dimensional, `@builtin(subgroup_id)` and `@builtin(subgroup_invocation_id)` are not supported.")] |
| 94 |
InvalidMultiDimensionalSubgroupBuiltIn, |
| 95 |
} |
| 96 |
|
| 97 |
#[derive(Clone, Debug, thiserror::Error)] |
| 98 |
#[cfg_attr(test, derive(PartialEq))] |
| 99 |
pub enum EntryPointError { |
| 100 |
#[error("Multiple conflicting entry points")] |
| 101 |
Conflict, |
| 102 |
#[error("Vertex shaders must return a `@builtin(position)` output value")] |
| 103 |
MissingVertexOutputPosition, |
| 104 |
#[error("Early depth test is not applicable")] |
| 105 |
UnexpectedEarlyDepthTest, |
| 106 |
#[error("Workgroup size is not applicable")] |
| 107 |
UnexpectedWorkgroupSize, |
| 108 |
#[error("Workgroup size is out of range")] |
| 109 |
OutOfRangeWorkgroupSize, |
| 110 |
#[error("Uses operations forbidden at this stage")] |
| 111 |
ForbiddenStageOperations, |
| 112 |
#[error("Global variable {0:?} is used incorrectly as {1:?}")] |
| 113 |
InvalidGlobalUsage(Handle<crate::GlobalVariable>, GlobalUse), |
| 114 |
#[error("More than 1 push constant variable is used")] |
| 115 |
MoreThanOnePushConstantUsed, |
| 116 |
#[error("Bindings for {0:?} conflict with other resource")] |
| 117 |
BindingCollision(Handle<crate::GlobalVariable>), |
| 118 |
#[error("Argument {0} varying error")] |
| 119 |
Argument(u32, #[source] VaryingError), |
| 120 |
#[error(transparent)] |
| 121 |
Result(#[from] VaryingError), |
| 122 |
#[error("Location {location} interpolation of an integer has to be flat")] |
| 123 |
InvalidIntegerInterpolation { location: u32 }, |
| 124 |
#[error(transparent)] |
| 125 |
Function(#[from] FunctionError), |
| 126 |
} |
| 127 |
|
| 128 |
fn storage_usage(access: crate::StorageAccess) -> GlobalUse { |
| 129 |
let mut storage_usage = GlobalUse::QUERY; |
| 130 |
if access.contains(crate::StorageAccess::LOAD) { |
| 131 |
storage_usage |= GlobalUse::READ; |
| 132 |
} |
| 133 |
if access.contains(crate::StorageAccess::STORE) { |
| 134 |
storage_usage |= GlobalUse::WRITE; |
| 135 |
} |
| 136 |
if access.contains(crate::StorageAccess::ATOMIC) { |
| 137 |
storage_usage |= GlobalUse::ATOMIC; |
| 138 |
} |
| 139 |
storage_usage |
| 140 |
} |
| 141 |
|
| 142 |
struct VaryingContext<'a> { |
| 143 |
stage: crate::ShaderStage, |
| 144 |
output: bool, |
| 145 |
types: &'a UniqueArena<crate::Type>, |
| 146 |
type_info: &'a Vec<super::r#type::TypeInfo>, |
| 147 |
location_mask: &'a mut BitSet, |
| 148 |
blend_src_mask: &'a mut BitSet, |
| 149 |
built_ins: &'a mut crate::FastHashSet<crate::BuiltIn>, |
| 150 |
capabilities: Capabilities, |
| 151 |
flags: super::ValidationFlags, |
| 152 |
} |
| 153 |
|
| 154 |
impl VaryingContext<'_> { |
| 155 |
fn validate_impl( |
| 156 |
&mut self, |
| 157 |
ep: &crate::EntryPoint, |
| 158 |
ty: Handle<crate::Type>, |
| 159 |
binding: &crate::Binding, |
| 160 |
) -> Result<(), VaryingError> { |
| 161 |
use crate::{BuiltIn as Bi, ShaderStage as St, TypeInner as Ti, VectorSize as Vs}; |
| 162 |
|
| 163 |
let ty_inner = &self.types[ty].inner; |
| 164 |
match *binding { |
| 165 |
crate::Binding::BuiltIn(built_in) => { |
| 166 |
// Ignore the `invariant` field for the sake of duplicate checks, |
| 167 |
// but use the original in error messages. |
| 168 |
let canonical = if let crate::BuiltIn::Position { .. } = built_in { |
| 169 |
crate::BuiltIn::Position { invariant: false } |
| 170 |
} else { |
| 171 |
built_in |
| 172 |
}; |
| 173 |
|
| 174 |
if self.built_ins.contains(&canonical) { |
| 175 |
return Err(VaryingError::DuplicateBuiltIn(built_in)); |
| 176 |
} |
| 177 |
self.built_ins.insert(canonical); |
| 178 |
|
| 179 |
let required = match built_in { |
| 180 |
Bi::ClipDistance => Capabilities::CLIP_DISTANCE, |
| 181 |
Bi::CullDistance => Capabilities::CULL_DISTANCE, |
| 182 |
Bi::PrimitiveIndex => Capabilities::PRIMITIVE_INDEX, |
| 183 |
Bi::ViewIndex => Capabilities::MULTIVIEW, |
| 184 |
Bi::SampleIndex => Capabilities::MULTISAMPLED_SHADING, |
| 185 |
Bi::NumSubgroups |
| 186 |
| Bi::SubgroupId |
| 187 |
| Bi::SubgroupSize |
| 188 |
| Bi::SubgroupInvocationId => Capabilities::SUBGROUP, |
| 189 |
_ => Capabilities::empty(), |
| 190 |
}; |
| 191 |
if !self.capabilities.contains(required) { |
| 192 |
return Err(VaryingError::UnsupportedCapability(required)); |
| 193 |
} |
| 194 |
|
| 195 |
if matches!( |
| 196 |
built_in, |
| 197 |
crate::BuiltIn::SubgroupId | crate::BuiltIn::SubgroupInvocationId |
| 198 |
) && ep.workgroup_size[1..].iter().any(|&s| s > 1) |
| 199 |
{ |
| 200 |
return Err(VaryingError::InvalidMultiDimensionalSubgroupBuiltIn); |
| 201 |
} |
| 202 |
|
| 203 |
let (visible, type_good) = match built_in { |
| 204 |
Bi::BaseInstance |
| 205 |
| Bi::BaseVertex |
| 206 |
| Bi::InstanceIndex |
| 207 |
| Bi::VertexIndex |
| 208 |
| Bi::DrawID => ( |
| 209 |
self.stage == St::Vertex && !self.output, |
| 210 |
*ty_inner == Ti::Scalar(crate::Scalar::U32), |
| 211 |
), |
| 212 |
Bi::ClipDistance | Bi::CullDistance => ( |
| 213 |
self.stage == St::Vertex && self.output, |
| 214 |
match *ty_inner { |
| 215 |
Ti::Array { base, .. } => { |
| 216 |
self.types[base].inner == Ti::Scalar(crate::Scalar::F32) |
| 217 |
} |
| 218 |
_ => false, |
| 219 |
}, |
| 220 |
), |
| 221 |
Bi::PointSize => ( |
| 222 |
self.stage == St::Vertex && self.output, |
| 223 |
*ty_inner == Ti::Scalar(crate::Scalar::F32), |
| 224 |
), |
| 225 |
Bi::PointCoord => ( |
| 226 |
self.stage == St::Fragment && !self.output, |
| 227 |
*ty_inner |
| 228 |
== Ti::Vector { |
| 229 |
size: Vs::Bi, |
| 230 |
scalar: crate::Scalar::F32, |
| 231 |
}, |
| 232 |
), |
| 233 |
Bi::Position { .. } => ( |
| 234 |
match self.stage { |
| 235 |
St::Vertex => self.output, |
| 236 |
St::Fragment => !self.output, |
| 237 |
St::Compute => false, |
| 238 |
St::Task | St::Mesh => unreachable!(), |
| 239 |
}, |
| 240 |
*ty_inner |
| 241 |
== Ti::Vector { |
| 242 |
size: Vs::Quad, |
| 243 |
scalar: crate::Scalar::F32, |
| 244 |
}, |
| 245 |
), |
| 246 |
Bi::ViewIndex => ( |
| 247 |
match self.stage { |
| 248 |
St::Vertex | St::Fragment => !self.output, |
| 249 |
St::Compute => false, |
| 250 |
St::Task | St::Mesh => unreachable!(), |
| 251 |
}, |
| 252 |
*ty_inner == Ti::Scalar(crate::Scalar::I32), |
| 253 |
), |
| 254 |
Bi::FragDepth => ( |
| 255 |
self.stage == St::Fragment && self.output, |
| 256 |
*ty_inner == Ti::Scalar(crate::Scalar::F32), |
| 257 |
), |
| 258 |
Bi::FrontFacing => ( |
| 259 |
self.stage == St::Fragment && !self.output, |
| 260 |
*ty_inner == Ti::Scalar(crate::Scalar::BOOL), |
| 261 |
), |
| 262 |
Bi::PrimitiveIndex => ( |
| 263 |
self.stage == St::Fragment && !self.output, |
| 264 |
*ty_inner == Ti::Scalar(crate::Scalar::U32), |
| 265 |
), |
| 266 |
Bi::SampleIndex => ( |
| 267 |
self.stage == St::Fragment && !self.output, |
| 268 |
*ty_inner == Ti::Scalar(crate::Scalar::U32), |
| 269 |
), |
| 270 |
Bi::SampleMask => ( |
| 271 |
self.stage == St::Fragment, |
| 272 |
*ty_inner == Ti::Scalar(crate::Scalar::U32), |
| 273 |
), |
| 274 |
Bi::LocalInvocationIndex => ( |
| 275 |
self.stage == St::Compute && !self.output, |
| 276 |
*ty_inner == Ti::Scalar(crate::Scalar::U32), |
| 277 |
), |
| 278 |
Bi::GlobalInvocationId |
| 279 |
| Bi::LocalInvocationId |
| 280 |
| Bi::WorkGroupId |
| 281 |
| Bi::WorkGroupSize |
| 282 |
| Bi::NumWorkGroups => ( |
| 283 |
self.stage == St::Compute && !self.output, |
| 284 |
*ty_inner |
| 285 |
== Ti::Vector { |
| 286 |
size: Vs::Tri, |
| 287 |
scalar: crate::Scalar::U32, |
| 288 |
}, |
| 289 |
), |
| 290 |
Bi::NumSubgroups | Bi::SubgroupId => ( |
| 291 |
self.stage == St::Compute && !self.output, |
| 292 |
*ty_inner == Ti::Scalar(crate::Scalar::U32), |
| 293 |
), |
| 294 |
Bi::SubgroupSize | Bi::SubgroupInvocationId => ( |
| 295 |
match self.stage { |
| 296 |
St::Compute | St::Fragment => !self.output, |
| 297 |
St::Vertex => false, |
| 298 |
St::Task | St::Mesh => unreachable!(), |
| 299 |
}, |
| 300 |
*ty_inner == Ti::Scalar(crate::Scalar::U32), |
| 301 |
), |
| 302 |
}; |
| 303 |
|
| 304 |
if !visible { |
| 305 |
return Err(VaryingError::InvalidBuiltInStage(built_in)); |
| 306 |
} |
| 307 |
if !type_good { |
| 308 |
log::warn!("Wrong builtin type: {:?}", ty_inner); |
| 309 |
return Err(VaryingError::InvalidBuiltInType(built_in)); |
| 310 |
} |
| 311 |
} |
| 312 |
crate::Binding::Location { |
| 313 |
location, |
| 314 |
interpolation, |
| 315 |
sampling, |
| 316 |
blend_src, |
| 317 |
} => { |
| 318 |
// Only IO-shareable types may be stored in locations. |
| 319 |
if !self.type_info[ty.index()] |
| 320 |
.flags |
| 321 |
.contains(super::TypeFlags::IO_SHAREABLE) |
| 322 |
{ |
| 323 |
return Err(VaryingError::NotIOShareableType(ty)); |
| 324 |
} |
| 325 |
|
| 326 |
if let Some(blend_src) = blend_src { |
| 327 |
// `blend_src` is only valid if dual source blending was explicitly enabled, |
| 328 |
// see https://www.w3.org/TR/WGSL/#extension-dual_source_blending |
| 329 |
if !self |
| 330 |
.capabilities |
| 331 |
.contains(Capabilities::DUAL_SOURCE_BLENDING) |
| 332 |
{ |
| 333 |
return Err(VaryingError::UnsupportedCapability( |
| 334 |
Capabilities::DUAL_SOURCE_BLENDING, |
| 335 |
)); |
| 336 |
} |
| 337 |
if self.stage != crate::ShaderStage::Fragment { |
| 338 |
return Err(VaryingError::InvalidAttributeInStage( |
| 339 |
"blend_src", |
| 340 |
self.stage, |
| 341 |
)); |
| 342 |
} |
| 343 |
if !self.output { |
| 344 |
return Err(VaryingError::InvalidInputAttributeInStage( |
| 345 |
"blend_src", |
| 346 |
self.stage, |
| 347 |
)); |
| 348 |
} |
| 349 |
if (blend_src != 0 && blend_src != 1) || location != 0 { |
| 350 |
return Err(VaryingError::InvalidBlendSrcIndex { |
| 351 |
location, |
| 352 |
blend_src, |
| 353 |
}); |
| 354 |
} |
| 355 |
if !self.blend_src_mask.insert(blend_src as usize) { |
| 356 |
return Err(VaryingError::BindingCollisionBlendSrc { blend_src }); |
| 357 |
} |
| 358 |
} else if !self.location_mask.insert(location as usize) |
| 359 |
&& self.flags.contains(super::ValidationFlags::BINDINGS) |
| 360 |
{ |
| 361 |
return Err(VaryingError::BindingCollision { location }); |
| 362 |
} |
| 363 |
|
| 364 |
if let Some(interpolation) = interpolation { |
| 365 |
let invalid_sampling = match (interpolation, sampling) { |
| 366 |
(_, None) |
| 367 |
| ( |
| 368 |
crate::Interpolation::Perspective | crate::Interpolation::Linear, |
| 369 |
Some( |
| 370 |
crate::Sampling::Center |
| 371 |
| crate::Sampling::Centroid |
| 372 |
| crate::Sampling::Sample, |
| 373 |
), |
| 374 |
) |
| 375 |
| ( |
| 376 |
crate::Interpolation::Flat, |
| 377 |
Some(crate::Sampling::First | crate::Sampling::Either), |
| 378 |
) => None, |
| 379 |
(_, Some(invalid_sampling)) => Some(invalid_sampling), |
| 380 |
}; |
| 381 |
if let Some(sampling) = invalid_sampling { |
| 382 |
return Err(VaryingError::InvalidInterpolationSamplingCombination { |
| 383 |
interpolation, |
| 384 |
sampling, |
| 385 |
}); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
let needs_interpolation = match self.stage { |
| 390 |
crate::ShaderStage::Vertex => self.output, |
| 391 |
crate::ShaderStage::Fragment => !self.output, |
| 392 |
crate::ShaderStage::Compute => false, |
| 393 |
crate::ShaderStage::Task | crate::ShaderStage::Mesh => unreachable!(), |
| 394 |
}; |
| 395 |
|
| 396 |
// It doesn't make sense to specify a sampling when `interpolation` is `Flat`, but |
| 397 |
// SPIR-V and GLSL both explicitly tolerate such combinations of decorators / |
| 398 |
// qualifiers, so we won't complain about that here. |
| 399 |
let _ = sampling; |
| 400 |
|
| 401 |
let required = match sampling { |
| 402 |
Some(crate::Sampling::Sample) => Capabilities::MULTISAMPLED_SHADING, |
| 403 |
_ => Capabilities::empty(), |
| 404 |
}; |
| 405 |
if !self.capabilities.contains(required) { |
| 406 |
return Err(VaryingError::UnsupportedCapability(required)); |
| 407 |
} |
| 408 |
|
| 409 |
match ty_inner.scalar_kind() { |
| 410 |
Some(crate::ScalarKind::Float) => { |
| 411 |
if needs_interpolation && interpolation.is_none() { |
| 412 |
return Err(VaryingError::MissingInterpolation); |
| 413 |
} |
| 414 |
} |
| 415 |
Some(_) => { |
| 416 |
if needs_interpolation && interpolation != Some(crate::Interpolation::Flat) |
| 417 |
{ |
| 418 |
return Err(VaryingError::InvalidInterpolation); |
| 419 |
} |
| 420 |
} |
| 421 |
None => return Err(VaryingError::InvalidType(ty)), |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
Ok(()) |
| 427 |
} |
| 428 |
|
| 429 |
fn validate( |
| 430 |
&mut self, |
| 431 |
ep: &crate::EntryPoint, |
| 432 |
ty: Handle<crate::Type>, |
| 433 |
binding: Option<&crate::Binding>, |
| 434 |
) -> Result<(), WithSpan<VaryingError>> { |
| 435 |
let span_context = self.types.get_span_context(ty); |
| 436 |
match binding { |
| 437 |
Some(binding) => self |
| 438 |
.validate_impl(ep, ty, binding) |
| 439 |
.map_err(|e| e.with_span_context(span_context)), |
| 440 |
None => { |
| 441 |
match self.types[ty].inner { |
| 442 |
crate::TypeInner::Struct { ref members, .. } => { |
| 443 |
for (index, member) in members.iter().enumerate() { |
| 444 |
let span_context = self.types.get_span_context(ty); |
| 445 |
match member.binding { |
| 446 |
None => { |
| 447 |
if self.flags.contains(super::ValidationFlags::BINDINGS) { |
| 448 |
return Err(VaryingError::MemberMissingBinding( |
| 449 |
index as u32, |
| 450 |
) |
| 451 |
.with_span_context(span_context)); |
| 452 |
} |
| 453 |
} |
| 454 |
Some(ref binding) => self |
| 455 |
.validate_impl(ep, member.ty, binding) |
| 456 |
.map_err(|e| e.with_span_context(span_context))?, |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
if !self.blend_src_mask.is_empty() { |
| 461 |
let span_context = self.types.get_span_context(ty); |
| 462 |
|
| 463 |
// If there's any blend_src usage, it must apply to all members of which there must be exactly 2. |
| 464 |
if members.len() != 2 || self.blend_src_mask.len() != 2 { |
| 465 |
return Err(VaryingError::IncompleteBlendSrcUsage |
| 466 |
.with_span_context(span_context)); |
| 467 |
} |
| 468 |
// Also, all members must have the same type. |
| 469 |
if members[0].ty != members[1].ty { |
| 470 |
return Err(VaryingError::BlendSrcOutputTypeMismatch { |
| 471 |
blend_src_0_type: members[0].ty, |
| 472 |
blend_src_1_type: members[1].ty, |
| 473 |
} |
| 474 |
.with_span_context(span_context)); |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
_ => { |
| 479 |
if self.flags.contains(super::ValidationFlags::BINDINGS) { |
| 480 |
return Err(VaryingError::MissingBinding.with_span()); |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
Ok(()) |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
impl super::Validator { |
| 491 |
pub(super) fn validate_global_var( |
| 492 |
&self, |
| 493 |
var: &crate::GlobalVariable, |
| 494 |
gctx: crate::proc::GlobalCtx, |
| 495 |
mod_info: &ModuleInfo, |
| 496 |
global_expr_kind: &crate::proc::ExpressionKindTracker, |
| 497 |
) -> Result<(), GlobalVariableError> { |
| 498 |
use super::TypeFlags; |
| 499 |
|
| 500 |
log::debug!("var {:?}", var); |
| 501 |
let inner_ty = match gctx.types[var.ty].inner { |
| 502 |
// A binding array is (mostly) supposed to behave the same as a |
| 503 |
// series of individually bound resources, so we can (mostly) |
| 504 |
// validate a `binding_array<T>` as if it were just a plain `T`. |
| 505 |
crate::TypeInner::BindingArray { base, .. } => match var.space { |
| 506 |
crate::AddressSpace::Storage { .. } |
| 507 |
| crate::AddressSpace::Uniform |
| 508 |
| crate::AddressSpace::Handle => base, |
| 509 |
_ => return Err(GlobalVariableError::InvalidUsage(var.space)), |
| 510 |
}, |
| 511 |
_ => var.ty, |
| 512 |
}; |
| 513 |
let type_info = &self.types[inner_ty.index()]; |
| 514 |
|
| 515 |
let (required_type_flags, is_resource) = match var.space { |
| 516 |
crate::AddressSpace::Function => { |
| 517 |
return Err(GlobalVariableError::InvalidUsage(var.space)) |
| 518 |
} |
| 519 |
crate::AddressSpace::Storage { access } => { |
| 520 |
if let Err((ty_handle, disalignment)) = type_info.storage_layout { |
| 521 |
if self.flags.contains(super::ValidationFlags::STRUCT_LAYOUTS) { |
| 522 |
return Err(GlobalVariableError::Alignment( |
| 523 |
var.space, |
| 524 |
ty_handle, |
| 525 |
disalignment, |
| 526 |
)); |
| 527 |
} |
| 528 |
} |
| 529 |
if access == crate::StorageAccess::STORE { |
| 530 |
return Err(GlobalVariableError::StorageAddressSpaceWriteOnlyNotSupported); |
| 531 |
} |
| 532 |
( |
| 533 |
TypeFlags::DATA | TypeFlags::HOST_SHAREABLE | TypeFlags::CREATION_RESOLVED, |
| 534 |
true, |
| 535 |
) |
| 536 |
} |
| 537 |
crate::AddressSpace::Uniform => { |
| 538 |
if let Err((ty_handle, disalignment)) = type_info.uniform_layout { |
| 539 |
if self.flags.contains(super::ValidationFlags::STRUCT_LAYOUTS) { |
| 540 |
return Err(GlobalVariableError::Alignment( |
| 541 |
var.space, |
| 542 |
ty_handle, |
| 543 |
disalignment, |
| 544 |
)); |
| 545 |
} |
| 546 |
} |
| 547 |
( |
| 548 |
TypeFlags::DATA |
| 549 |
| TypeFlags::COPY |
| 550 |
| TypeFlags::SIZED |
| 551 |
| TypeFlags::HOST_SHAREABLE |
| 552 |
| TypeFlags::CREATION_RESOLVED, |
| 553 |
true, |
| 554 |
) |
| 555 |
} |
| 556 |
crate::AddressSpace::Handle => { |
| 557 |
match gctx.types[inner_ty].inner { |
| 558 |
crate::TypeInner::Image { class, .. } => match class { |
| 559 |
crate::ImageClass::Storage { |
| 560 |
format: |
| 561 |
crate::StorageFormat::R16Unorm |
| 562 |
| crate::StorageFormat::R16Snorm |
| 563 |
| crate::StorageFormat::Rg16Unorm |
| 564 |
| crate::StorageFormat::Rg16Snorm |
| 565 |
| crate::StorageFormat::Rgba16Unorm |
| 566 |
| crate::StorageFormat::Rgba16Snorm, |
| 567 |
.. |
| 568 |
} => { |
| 569 |
if !self |
| 570 |
.capabilities |
| 571 |
.contains(Capabilities::STORAGE_TEXTURE_16BIT_NORM_FORMATS) |
| 572 |
{ |
| 573 |
return Err(GlobalVariableError::UnsupportedCapability( |
| 574 |
Capabilities::STORAGE_TEXTURE_16BIT_NORM_FORMATS, |
| 575 |
)); |
| 576 |
} |
| 577 |
} |
| 578 |
_ => {} |
| 579 |
}, |
| 580 |
crate::TypeInner::Sampler { .. } |
| 581 |
| crate::TypeInner::AccelerationStructure { .. } |
| 582 |
| crate::TypeInner::RayQuery { .. } => {} |
| 583 |
_ => { |
| 584 |
return Err(GlobalVariableError::InvalidType(var.space)); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
(TypeFlags::empty(), true) |
| 589 |
} |
| 590 |
crate::AddressSpace::Private => ( |
| 591 |
TypeFlags::CONSTRUCTIBLE | TypeFlags::CREATION_RESOLVED, |
| 592 |
false, |
| 593 |
), |
| 594 |
crate::AddressSpace::WorkGroup => (TypeFlags::DATA | TypeFlags::SIZED, false), |
| 595 |
crate::AddressSpace::PushConstant => { |
| 596 |
if !self.capabilities.contains(Capabilities::PUSH_CONSTANT) { |
| 597 |
return Err(GlobalVariableError::UnsupportedCapability( |
| 598 |
Capabilities::PUSH_CONSTANT, |
| 599 |
)); |
| 600 |
} |
| 601 |
if let Err(ref err) = type_info.push_constant_compatibility { |
| 602 |
return Err(GlobalVariableError::InvalidPushConstantType(err.clone())); |
| 603 |
} |
| 604 |
( |
| 605 |
TypeFlags::DATA |
| 606 |
| TypeFlags::COPY |
| 607 |
| TypeFlags::HOST_SHAREABLE |
| 608 |
| TypeFlags::SIZED, |
| 609 |
false, |
| 610 |
) |
| 611 |
} |
| 612 |
}; |
| 613 |
|
| 614 |
if !type_info.flags.contains(required_type_flags) { |
| 615 |
return Err(GlobalVariableError::MissingTypeFlags { |
| 616 |
seen: type_info.flags, |
| 617 |
required: required_type_flags, |
| 618 |
}); |
| 619 |
} |
| 620 |
|
| 621 |
if is_resource != var.binding.is_some() { |
| 622 |
if self.flags.contains(super::ValidationFlags::BINDINGS) { |
| 623 |
return Err(GlobalVariableError::InvalidBinding); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
if let Some(init) = var.init { |
| 628 |
match var.space { |
| 629 |
crate::AddressSpace::Private | crate::AddressSpace::Function => {} |
| 630 |
_ => { |
| 631 |
return Err(GlobalVariableError::InitializerNotAllowed(var.space)); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
if !global_expr_kind.is_const_or_override(init) { |
| 636 |
return Err(GlobalVariableError::InitializerExprType); |
| 637 |
} |
| 638 |
|
| 639 |
if !gctx.compare_types( |
| 640 |
&crate::proc::TypeResolution::Handle(var.ty), |
| 641 |
&mod_info[init], |
| 642 |
) { |
| 643 |
return Err(GlobalVariableError::InitializerType); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
Ok(()) |
| 648 |
} |
| 649 |
|
| 650 |
pub(super) fn validate_entry_point( |
| 651 |
&mut self, |
| 652 |
ep: &crate::EntryPoint, |
| 653 |
module: &crate::Module, |
| 654 |
mod_info: &ModuleInfo, |
| 655 |
) -> Result<FunctionInfo, WithSpan<EntryPointError>> { |
| 656 |
if ep.early_depth_test.is_some() { |
| 657 |
let required = Capabilities::EARLY_DEPTH_TEST; |
| 658 |
if !self.capabilities.contains(required) { |
| 659 |
return Err( |
| 660 |
EntryPointError::Result(VaryingError::UnsupportedCapability(required)) |
| 661 |
.with_span(), |
| 662 |
); |
| 663 |
} |
| 664 |
|
| 665 |
if ep.stage != crate::ShaderStage::Fragment { |
| 666 |
return Err(EntryPointError::UnexpectedEarlyDepthTest.with_span()); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
if ep.stage == crate::ShaderStage::Compute { |
| 671 |
if ep |
| 672 |
.workgroup_size |
| 673 |
.iter() |
| 674 |
.any(|&s| s == 0 || s > MAX_WORKGROUP_SIZE) |
| 675 |
{ |
| 676 |
return Err(EntryPointError::OutOfRangeWorkgroupSize.with_span()); |
| 677 |
} |
| 678 |
} else if ep.workgroup_size != [0; 3] { |
| 679 |
return Err(EntryPointError::UnexpectedWorkgroupSize.with_span()); |
| 680 |
} |
| 681 |
|
| 682 |
let mut info = self |
| 683 |
.validate_function(&ep.function, module, mod_info, true) |
| 684 |
.map_err(WithSpan::into_other)?; |
| 685 |
|
| 686 |
{ |
| 687 |
use super::ShaderStages; |
| 688 |
|
| 689 |
let stage_bit = match ep.stage { |
| 690 |
crate::ShaderStage::Vertex => ShaderStages::VERTEX, |
| 691 |
crate::ShaderStage::Fragment => ShaderStages::FRAGMENT, |
| 692 |
crate::ShaderStage::Compute => ShaderStages::COMPUTE, |
| 693 |
crate::ShaderStage::Task | crate::ShaderStage::Mesh => unreachable!(), |
| 694 |
}; |
| 695 |
|
| 696 |
if !info.available_stages.contains(stage_bit) { |
| 697 |
return Err(EntryPointError::ForbiddenStageOperations.with_span()); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
self.location_mask.clear(); |
| 702 |
let mut argument_built_ins = crate::FastHashSet::default(); |
| 703 |
// TODO: add span info to function arguments |
| 704 |
for (index, fa) in ep.function.arguments.iter().enumerate() { |
| 705 |
let mut ctx = VaryingContext { |
| 706 |
stage: ep.stage, |
| 707 |
output: false, |
| 708 |
types: &module.types, |
| 709 |
type_info: &self.types, |
| 710 |
location_mask: &mut self.location_mask, |
| 711 |
blend_src_mask: &mut self.blend_src_mask, |
| 712 |
built_ins: &mut argument_built_ins, |
| 713 |
capabilities: self.capabilities, |
| 714 |
flags: self.flags, |
| 715 |
}; |
| 716 |
ctx.validate(ep, fa.ty, fa.binding.as_ref()) |
| 717 |
.map_err_inner(|e| EntryPointError::Argument(index as u32, e).with_span())?; |
| 718 |
} |
| 719 |
|
| 720 |
self.location_mask.clear(); |
| 721 |
if let Some(ref fr) = ep.function.result { |
| 722 |
let mut result_built_ins = crate::FastHashSet::default(); |
| 723 |
let mut ctx = VaryingContext { |
| 724 |
stage: ep.stage, |
| 725 |
output: true, |
| 726 |
types: &module.types, |
| 727 |
type_info: &self.types, |
| 728 |
location_mask: &mut self.location_mask, |
| 729 |
blend_src_mask: &mut self.blend_src_mask, |
| 730 |
built_ins: &mut result_built_ins, |
| 731 |
capabilities: self.capabilities, |
| 732 |
flags: self.flags, |
| 733 |
}; |
| 734 |
ctx.validate(ep, fr.ty, fr.binding.as_ref()) |
| 735 |
.map_err_inner(|e| EntryPointError::Result(e).with_span())?; |
| 736 |
if ep.stage == crate::ShaderStage::Vertex |
| 737 |
&& !result_built_ins.contains(&crate::BuiltIn::Position { invariant: false }) |
| 738 |
{ |
| 739 |
return Err(EntryPointError::MissingVertexOutputPosition.with_span()); |
| 740 |
} |
| 741 |
if !self.blend_src_mask.is_empty() { |
| 742 |
info.dual_source_blending = true; |
| 743 |
} |
| 744 |
} else if ep.stage == crate::ShaderStage::Vertex { |
| 745 |
return Err(EntryPointError::MissingVertexOutputPosition.with_span()); |
| 746 |
} |
| 747 |
|
| 748 |
{ |
| 749 |
let mut used_push_constants = module |
| 750 |
.global_variables |
| 751 |
.iter() |
| 752 |
.filter(|&(_, var)| var.space == crate::AddressSpace::PushConstant) |
| 753 |
.map(|(handle, _)| handle) |
| 754 |
.filter(|&handle| !info[handle].is_empty()); |
| 755 |
// Check if there is more than one push constant, and error if so. |
| 756 |
// Use a loop for when returning multiple errors is supported. |
| 757 |
if let Some(handle) = used_push_constants.nth(1) { |
| 758 |
return Err(EntryPointError::MoreThanOnePushConstantUsed |
| 759 |
.with_span_handle(handle, &module.global_variables)); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
self.ep_resource_bindings.clear(); |
| 764 |
for (var_handle, var) in module.global_variables.iter() { |
| 765 |
let usage = info[var_handle]; |
| 766 |
if usage.is_empty() { |
| 767 |
continue; |
| 768 |
} |
| 769 |
|
| 770 |
let allowed_usage = match var.space { |
| 771 |
crate::AddressSpace::Function => unreachable!(), |
| 772 |
crate::AddressSpace::Uniform => GlobalUse::READ | GlobalUse::QUERY, |
| 773 |
crate::AddressSpace::Storage { access } => storage_usage(access), |
| 774 |
crate::AddressSpace::Handle => match module.types[var.ty].inner { |
| 775 |
crate::TypeInner::BindingArray { base, .. } => match module.types[base].inner { |
| 776 |
crate::TypeInner::Image { |
| 777 |
class: crate::ImageClass::Storage { access, .. }, |
| 778 |
.. |
| 779 |
} => storage_usage(access), |
| 780 |
_ => GlobalUse::READ | GlobalUse::QUERY, |
| 781 |
}, |
| 782 |
crate::TypeInner::Image { |
| 783 |
class: crate::ImageClass::Storage { access, .. }, |
| 784 |
.. |
| 785 |
} => storage_usage(access), |
| 786 |
_ => GlobalUse::READ | GlobalUse::QUERY, |
| 787 |
}, |
| 788 |
crate::AddressSpace::Private | crate::AddressSpace::WorkGroup => { |
| 789 |
GlobalUse::READ | GlobalUse::WRITE | GlobalUse::QUERY |
| 790 |
} |
| 791 |
crate::AddressSpace::PushConstant => GlobalUse::READ, |
| 792 |
}; |
| 793 |
if !allowed_usage.contains(usage) { |
| 794 |
log::warn!("\tUsage error for: {:?}", var); |
| 795 |
log::warn!( |
| 796 |
"\tAllowed usage: {:?}, requested: {:?}", |
| 797 |
allowed_usage, |
| 798 |
usage |
| 799 |
); |
| 800 |
return Err(EntryPointError::InvalidGlobalUsage(var_handle, usage) |
| 801 |
.with_span_handle(var_handle, &module.global_variables)); |
| 802 |
} |
| 803 |
|
| 804 |
if let Some(ref bind) = var.binding { |
| 805 |
if !self.ep_resource_bindings.insert(*bind) { |
| 806 |
if self.flags.contains(super::ValidationFlags::BINDINGS) { |
| 807 |
return Err(EntryPointError::BindingCollision(var_handle) |
| 808 |
.with_span_handle(var_handle, &module.global_variables)); |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
Ok(info) |
| 815 |
} |
| 816 |
} |