| 1 |
use alloc::{ |
| 2 |
string::{String, ToString}, |
| 3 |
vec, |
| 4 |
vec::Vec, |
| 5 |
}; |
| 6 |
|
| 7 |
use hashbrown::hash_map::Entry; |
| 8 |
use spirv::Word; |
| 9 |
|
| 10 |
use super::{ |
| 11 |
block::DebugInfoInner, |
| 12 |
helpers::{contains_builtin, global_needs_wrapper, map_storage_class}, |
| 13 |
Block, BlockContext, CachedConstant, CachedExpressions, DebugInfo, EntryPointContext, Error, |
| 14 |
Function, FunctionArgument, GlobalVariable, IdGenerator, Instruction, LocalImageType, |
| 15 |
LocalType, LocalVariable, LogicalLayout, LookupFunctionType, LookupType, NumericType, Options, |
| 16 |
PhysicalLayout, PipelineOptions, ResultMember, Writer, WriterFlags, BITS_PER_BYTE, |
| 17 |
}; |
| 18 |
use crate::{ |
| 19 |
arena::{Handle, HandleVec, UniqueArena}, |
| 20 |
back::spv::{BindingInfo, WrappedFunction}, |
| 21 |
proc::{Alignment, TypeResolution}, |
| 22 |
valid::{FunctionInfo, ModuleInfo}, |
| 23 |
}; |
| 24 |
|
| 25 |
struct FunctionInterface<'a> { |
| 26 |
varying_ids: &'a mut Vec<Word>, |
| 27 |
stage: crate::ShaderStage, |
| 28 |
} |
| 29 |
|
| 30 |
impl Function { |
| 31 |
pub(super) fn to_words(&self, sink: &mut impl Extend<Word>) { |
| 32 |
self.signature.as_ref().unwrap().to_words(sink); |
| 33 |
for argument in self.parameters.iter() { |
| 34 |
argument.instruction.to_words(sink); |
| 35 |
} |
| 36 |
for (index, block) in self.blocks.iter().enumerate() { |
| 37 |
Instruction::label(block.label_id).to_words(sink); |
| 38 |
if index == 0 { |
| 39 |
for local_var in self.variables.values() { |
| 40 |
local_var.instruction.to_words(sink); |
| 41 |
} |
| 42 |
for local_var in self.force_loop_bounding_vars.iter() { |
| 43 |
local_var.instruction.to_words(sink); |
| 44 |
} |
| 45 |
for internal_var in self.spilled_composites.values() { |
| 46 |
internal_var.instruction.to_words(sink); |
| 47 |
} |
| 48 |
} |
| 49 |
for instruction in block.body.iter() { |
| 50 |
instruction.to_words(sink); |
| 51 |
} |
| 52 |
} |
| 53 |
Instruction::function_end().to_words(sink); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
impl Writer { |
| 58 |
pub fn new(options: &Options) -> Result<Self, Error> { |
| 59 |
let (major, minor) = options.lang_version; |
| 60 |
if major != 1 { |
| 61 |
return Err(Error::UnsupportedVersion(major, minor)); |
| 62 |
} |
| 63 |
|
| 64 |
let mut capabilities_used = crate::FastIndexSet::default(); |
| 65 |
capabilities_used.insert(spirv::Capability::Shader); |
| 66 |
|
| 67 |
let mut id_gen = IdGenerator::default(); |
| 68 |
let gl450_ext_inst_id = id_gen.next(); |
| 69 |
let void_type = id_gen.next(); |
| 70 |
|
| 71 |
Ok(Writer { |
| 72 |
physical_layout: PhysicalLayout::new(major, minor), |
| 73 |
logical_layout: LogicalLayout::default(), |
| 74 |
id_gen, |
| 75 |
capabilities_available: options.capabilities.clone(), |
| 76 |
capabilities_used, |
| 77 |
extensions_used: crate::FastIndexSet::default(), |
| 78 |
debugs: vec![], |
| 79 |
annotations: vec![], |
| 80 |
flags: options.flags, |
| 81 |
bounds_check_policies: options.bounds_check_policies, |
| 82 |
zero_initialize_workgroup_memory: options.zero_initialize_workgroup_memory, |
| 83 |
force_loop_bounding: options.force_loop_bounding, |
| 84 |
void_type, |
| 85 |
lookup_type: crate::FastHashMap::default(), |
| 86 |
lookup_function: crate::FastHashMap::default(), |
| 87 |
lookup_function_type: crate::FastHashMap::default(), |
| 88 |
wrapped_functions: crate::FastHashMap::default(), |
| 89 |
constant_ids: HandleVec::new(), |
| 90 |
cached_constants: crate::FastHashMap::default(), |
| 91 |
global_variables: HandleVec::new(), |
| 92 |
binding_map: options.binding_map.clone(), |
| 93 |
saved_cached: CachedExpressions::default(), |
| 94 |
gl450_ext_inst_id, |
| 95 |
temp_list: Vec::new(), |
| 96 |
ray_get_committed_intersection_function: None, |
| 97 |
ray_get_candidate_intersection_function: None, |
| 98 |
}) |
| 99 |
} |
| 100 |
|
| 101 |
/// Returns `(major, minor)` of the SPIR-V language version. |
| 102 |
pub const fn lang_version(&self) -> (u8, u8) { |
| 103 |
self.physical_layout.lang_version() |
| 104 |
} |
| 105 |
|
| 106 |
/// Reset `Writer` to its initial state, retaining any allocations. |
| 107 |
/// |
| 108 |
/// Why not just implement `Recyclable` for `Writer`? By design, |
| 109 |
/// `Recyclable::recycle` requires ownership of the value, not just |
| 110 |
/// `&mut`; see the trait documentation. But we need to use this method |
| 111 |
/// from functions like `Writer::write`, which only have `&mut Writer`. |
| 112 |
/// Workarounds include unsafe code (`core::ptr::read`, then `write`, ugh) |
| 113 |
/// or something like a `Default` impl that returns an oddly-initialized |
| 114 |
/// `Writer`, which is worse. |
| 115 |
fn reset(&mut self) { |
| 116 |
use super::recyclable::Recyclable; |
| 117 |
use core::mem::take; |
| 118 |
|
| 119 |
let mut id_gen = IdGenerator::default(); |
| 120 |
let gl450_ext_inst_id = id_gen.next(); |
| 121 |
let void_type = id_gen.next(); |
| 122 |
|
| 123 |
// Every field of the old writer that is not determined by the `Options` |
| 124 |
// passed to `Writer::new` should be reset somehow. |
| 125 |
let fresh = Writer { |
| 126 |
// Copied from the old Writer: |
| 127 |
flags: self.flags, |
| 128 |
bounds_check_policies: self.bounds_check_policies, |
| 129 |
zero_initialize_workgroup_memory: self.zero_initialize_workgroup_memory, |
| 130 |
force_loop_bounding: self.force_loop_bounding, |
| 131 |
capabilities_available: take(&mut self.capabilities_available), |
| 132 |
binding_map: take(&mut self.binding_map), |
| 133 |
|
| 134 |
// Initialized afresh: |
| 135 |
id_gen, |
| 136 |
void_type, |
| 137 |
gl450_ext_inst_id, |
| 138 |
|
| 139 |
// Recycled: |
| 140 |
capabilities_used: take(&mut self.capabilities_used).recycle(), |
| 141 |
extensions_used: take(&mut self.extensions_used).recycle(), |
| 142 |
physical_layout: self.physical_layout.clone().recycle(), |
| 143 |
logical_layout: take(&mut self.logical_layout).recycle(), |
| 144 |
debugs: take(&mut self.debugs).recycle(), |
| 145 |
annotations: take(&mut self.annotations).recycle(), |
| 146 |
lookup_type: take(&mut self.lookup_type).recycle(), |
| 147 |
lookup_function: take(&mut self.lookup_function).recycle(), |
| 148 |
lookup_function_type: take(&mut self.lookup_function_type).recycle(), |
| 149 |
wrapped_functions: take(&mut self.wrapped_functions).recycle(), |
| 150 |
constant_ids: take(&mut self.constant_ids).recycle(), |
| 151 |
cached_constants: take(&mut self.cached_constants).recycle(), |
| 152 |
global_variables: take(&mut self.global_variables).recycle(), |
| 153 |
saved_cached: take(&mut self.saved_cached).recycle(), |
| 154 |
temp_list: take(&mut self.temp_list).recycle(), |
| 155 |
ray_get_candidate_intersection_function: None, |
| 156 |
ray_get_committed_intersection_function: None, |
| 157 |
}; |
| 158 |
|
| 159 |
*self = fresh; |
| 160 |
|
| 161 |
self.capabilities_used.insert(spirv::Capability::Shader); |
| 162 |
} |
| 163 |
|
| 164 |
/// Indicate that the code requires any one of the listed capabilities. |
| 165 |
/// |
| 166 |
/// If nothing in `capabilities` appears in the available capabilities |
| 167 |
/// specified in the [`Options`] from which this `Writer` was created, |
| 168 |
/// return an error. The `what` string is used in the error message to |
| 169 |
/// explain what provoked the requirement. (If no available capabilities were |
| 170 |
/// given, assume everything is available.) |
| 171 |
/// |
| 172 |
/// The first acceptable capability will be added to this `Writer`'s |
| 173 |
/// [`capabilities_used`] table, and an `OpCapability` emitted for it in the |
| 174 |
/// result. For this reason, more specific capabilities should be listed |
| 175 |
/// before more general. |
| 176 |
/// |
| 177 |
/// [`capabilities_used`]: Writer::capabilities_used |
| 178 |
pub(super) fn require_any( |
| 179 |
&mut self, |
| 180 |
what: &'static str, |
| 181 |
capabilities: &[spirv::Capability], |
| 182 |
) -> Result<(), Error> { |
| 183 |
match *capabilities { |
| 184 |
[] => Ok(()), |
| 185 |
[first, ..] => { |
| 186 |
// Find the first acceptable capability, or return an error if |
| 187 |
// there is none. |
| 188 |
let selected = match self.capabilities_available { |
| 189 |
None => first, |
| 190 |
Some(ref available) => { |
| 191 |
match capabilities |
| 192 |
.iter() |
| 193 |
// need explicit type for hashbrown::HashSet::contains fn call to keep rustc happy |
| 194 |
.find(|cap| available.contains::<spirv::Capability>(cap)) |
| 195 |
{ |
| 196 |
Some(&cap) => cap, |
| 197 |
None => { |
| 198 |
return Err(Error::MissingCapabilities(what, capabilities.to_vec())) |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
}; |
| 203 |
self.capabilities_used.insert(selected); |
| 204 |
Ok(()) |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/// Indicate that the code requires all of the listed capabilities. |
| 210 |
/// |
| 211 |
/// If all entries of `capabilities` appear in the available capabilities |
| 212 |
/// specified in the [`Options`] from which this `Writer` was created |
| 213 |
/// (including the case where [`Options::capabilities`] is `None`), add |
| 214 |
/// them all to this `Writer`'s [`capabilities_used`] table, and return |
| 215 |
/// `Ok(())`. If at least one of the listed capabilities is not available, |
| 216 |
/// do not add anything to the `capabilities_used` table, and return the |
| 217 |
/// first unavailable requested capability, wrapped in `Err()`. |
| 218 |
/// |
| 219 |
/// This method is does not return an [`enum@Error`] in case of failure |
| 220 |
/// because it may be used in cases where the caller can recover (e.g., |
| 221 |
/// with a polyfill) if the requested capabilities are not available. In |
| 222 |
/// this case, it would be unnecessary work to find *all* the unavailable |
| 223 |
/// requested capabilities, and to allocate a `Vec` for them, just so we |
| 224 |
/// could return an [`Error::MissingCapabilities`]). |
| 225 |
/// |
| 226 |
/// [`capabilities_used`]: Writer::capabilities_used |
| 227 |
pub(super) fn require_all( |
| 228 |
&mut self, |
| 229 |
capabilities: &[spirv::Capability], |
| 230 |
) -> Result<(), spirv::Capability> { |
| 231 |
if let Some(ref available) = self.capabilities_available { |
| 232 |
for requested in capabilities { |
| 233 |
if !available.contains(requested) { |
| 234 |
return Err(*requested); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
for requested in capabilities { |
| 240 |
self.capabilities_used.insert(*requested); |
| 241 |
} |
| 242 |
|
| 243 |
Ok(()) |
| 244 |
} |
| 245 |
|
| 246 |
/// Indicate that the code uses the given extension. |
| 247 |
pub(super) fn use_extension(&mut self, extension: &'static str) { |
| 248 |
self.extensions_used.insert(extension); |
| 249 |
} |
| 250 |
|
| 251 |
pub(super) fn get_type_id(&mut self, lookup_ty: LookupType) -> Word { |
| 252 |
match self.lookup_type.entry(lookup_ty) { |
| 253 |
Entry::Occupied(e) => *e.get(), |
| 254 |
Entry::Vacant(e) => { |
| 255 |
let local = match lookup_ty { |
| 256 |
LookupType::Handle(_handle) => unreachable!("Handles are populated at start"), |
| 257 |
LookupType::Local(local) => local, |
| 258 |
}; |
| 259 |
|
| 260 |
let id = self.id_gen.next(); |
| 261 |
e.insert(id); |
| 262 |
self.write_type_declaration_local(id, local); |
| 263 |
id |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
pub(super) fn get_handle_type_id(&mut self, handle: Handle<crate::Type>) -> Word { |
| 269 |
self.get_type_id(LookupType::Handle(handle)) |
| 270 |
} |
| 271 |
|
| 272 |
pub(super) fn get_expression_lookup_type(&mut self, tr: &TypeResolution) -> LookupType { |
| 273 |
match *tr { |
| 274 |
TypeResolution::Handle(ty_handle) => LookupType::Handle(ty_handle), |
| 275 |
TypeResolution::Value(ref inner) => { |
| 276 |
let inner_local_type = self.localtype_from_inner(inner).unwrap(); |
| 277 |
LookupType::Local(inner_local_type) |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
pub(super) fn get_expression_type_id(&mut self, tr: &TypeResolution) -> Word { |
| 283 |
let lookup_ty = self.get_expression_lookup_type(tr); |
| 284 |
self.get_type_id(lookup_ty) |
| 285 |
} |
| 286 |
|
| 287 |
pub(super) fn get_localtype_id(&mut self, local: LocalType) -> Word { |
| 288 |
self.get_type_id(LookupType::Local(local)) |
| 289 |
} |
| 290 |
|
| 291 |
pub(super) fn get_pointer_type_id(&mut self, base: Word, class: spirv::StorageClass) -> Word { |
| 292 |
self.get_type_id(LookupType::Local(LocalType::Pointer { base, class })) |
| 293 |
} |
| 294 |
|
| 295 |
pub(super) fn get_handle_pointer_type_id( |
| 296 |
&mut self, |
| 297 |
base: Handle<crate::Type>, |
| 298 |
class: spirv::StorageClass, |
| 299 |
) -> Word { |
| 300 |
let base_id = self.get_handle_type_id(base); |
| 301 |
self.get_pointer_type_id(base_id, class) |
| 302 |
} |
| 303 |
|
| 304 |
pub(super) fn get_ray_query_pointer_id(&mut self, module: &crate::Module) -> Word { |
| 305 |
let rq_ty = module |
| 306 |
.types |
| 307 |
.get(&crate::Type { |
| 308 |
name: None, |
| 309 |
inner: crate::TypeInner::RayQuery { |
| 310 |
vertex_return: false, |
| 311 |
}, |
| 312 |
}) |
| 313 |
.or_else(|| { |
| 314 |
module.types.get(&crate::Type { |
| 315 |
name: None, |
| 316 |
inner: crate::TypeInner::RayQuery { |
| 317 |
vertex_return: true, |
| 318 |
}, |
| 319 |
}) |
| 320 |
}) |
| 321 |
.expect("ray_query type should have been populated by the variable passed into this!"); |
| 322 |
self.get_handle_pointer_type_id(rq_ty, spirv::StorageClass::Function) |
| 323 |
} |
| 324 |
|
| 325 |
/// Return a SPIR-V type for a pointer to `resolution`. |
| 326 |
/// |
| 327 |
/// The given `resolution` must be one that we can represent |
| 328 |
/// either as a `LocalType::Pointer` or `LocalType::LocalPointer`. |
| 329 |
pub(super) fn get_resolution_pointer_id( |
| 330 |
&mut self, |
| 331 |
resolution: &TypeResolution, |
| 332 |
class: spirv::StorageClass, |
| 333 |
) -> Word { |
| 334 |
let resolution_type_id = self.get_expression_type_id(resolution); |
| 335 |
self.get_pointer_type_id(resolution_type_id, class) |
| 336 |
} |
| 337 |
|
| 338 |
pub(super) fn get_numeric_type_id(&mut self, numeric: NumericType) -> Word { |
| 339 |
self.get_type_id(LocalType::Numeric(numeric).into()) |
| 340 |
} |
| 341 |
|
| 342 |
pub(super) fn get_u32_type_id(&mut self) -> Word { |
| 343 |
self.get_numeric_type_id(NumericType::Scalar(crate::Scalar::U32)) |
| 344 |
} |
| 345 |
|
| 346 |
pub(super) fn get_f32_type_id(&mut self) -> Word { |
| 347 |
self.get_numeric_type_id(NumericType::Scalar(crate::Scalar::F32)) |
| 348 |
} |
| 349 |
|
| 350 |
pub(super) fn get_vec2u_type_id(&mut self) -> Word { |
| 351 |
self.get_numeric_type_id(NumericType::Vector { |
| 352 |
size: crate::VectorSize::Bi, |
| 353 |
scalar: crate::Scalar::U32, |
| 354 |
}) |
| 355 |
} |
| 356 |
|
| 357 |
pub(super) fn get_vec3u_type_id(&mut self) -> Word { |
| 358 |
self.get_numeric_type_id(NumericType::Vector { |
| 359 |
size: crate::VectorSize::Tri, |
| 360 |
scalar: crate::Scalar::U32, |
| 361 |
}) |
| 362 |
} |
| 363 |
|
| 364 |
pub(super) fn get_f32_pointer_type_id(&mut self, class: spirv::StorageClass) -> Word { |
| 365 |
let f32_id = self.get_f32_type_id(); |
| 366 |
self.get_pointer_type_id(f32_id, class) |
| 367 |
} |
| 368 |
|
| 369 |
pub(super) fn get_vec2u_pointer_type_id(&mut self, class: spirv::StorageClass) -> Word { |
| 370 |
let vec2u_id = self.get_numeric_type_id(NumericType::Vector { |
| 371 |
size: crate::VectorSize::Bi, |
| 372 |
scalar: crate::Scalar::U32, |
| 373 |
}); |
| 374 |
self.get_pointer_type_id(vec2u_id, class) |
| 375 |
} |
| 376 |
|
| 377 |
pub(super) fn get_vec3u_pointer_type_id(&mut self, class: spirv::StorageClass) -> Word { |
| 378 |
let vec3u_id = self.get_numeric_type_id(NumericType::Vector { |
| 379 |
size: crate::VectorSize::Tri, |
| 380 |
scalar: crate::Scalar::U32, |
| 381 |
}); |
| 382 |
self.get_pointer_type_id(vec3u_id, class) |
| 383 |
} |
| 384 |
|
| 385 |
pub(super) fn get_bool_type_id(&mut self) -> Word { |
| 386 |
self.get_numeric_type_id(NumericType::Scalar(crate::Scalar::BOOL)) |
| 387 |
} |
| 388 |
|
| 389 |
pub(super) fn get_vec2_bool_type_id(&mut self) -> Word { |
| 390 |
self.get_numeric_type_id(NumericType::Vector { |
| 391 |
size: crate::VectorSize::Bi, |
| 392 |
scalar: crate::Scalar::BOOL, |
| 393 |
}) |
| 394 |
} |
| 395 |
|
| 396 |
pub(super) fn get_vec3_bool_type_id(&mut self) -> Word { |
| 397 |
self.get_numeric_type_id(NumericType::Vector { |
| 398 |
size: crate::VectorSize::Tri, |
| 399 |
scalar: crate::Scalar::BOOL, |
| 400 |
}) |
| 401 |
} |
| 402 |
|
| 403 |
pub(super) fn decorate(&mut self, id: Word, decoration: spirv::Decoration, operands: &[Word]) { |
| 404 |
self.annotations |
| 405 |
.push(Instruction::decorate(id, decoration, operands)); |
| 406 |
} |
| 407 |
|
| 408 |
/// Return `inner` as a `LocalType`, if that's possible. |
| 409 |
/// |
| 410 |
/// If `inner` can be represented as a `LocalType`, return |
| 411 |
/// `Some(local_type)`. |
| 412 |
/// |
| 413 |
/// Otherwise, return `None`. In this case, the type must always be looked |
| 414 |
/// up using a `LookupType::Handle`. |
| 415 |
fn localtype_from_inner(&mut self, inner: &crate::TypeInner) -> Option<LocalType> { |
| 416 |
Some(match *inner { |
| 417 |
crate::TypeInner::Scalar(_) |
| 418 |
| crate::TypeInner::Atomic(_) |
| 419 |
| crate::TypeInner::Vector { .. } |
| 420 |
| crate::TypeInner::Matrix { .. } => { |
| 421 |
// We expect `NumericType::from_inner` to handle all |
| 422 |
// these cases, so unwrap. |
| 423 |
LocalType::Numeric(NumericType::from_inner(inner).unwrap()) |
| 424 |
} |
| 425 |
crate::TypeInner::Pointer { base, space } => { |
| 426 |
let base_type_id = self.get_handle_type_id(base); |
| 427 |
LocalType::Pointer { |
| 428 |
base: base_type_id, |
| 429 |
class: map_storage_class(space), |
| 430 |
} |
| 431 |
} |
| 432 |
crate::TypeInner::ValuePointer { |
| 433 |
size, |
| 434 |
scalar, |
| 435 |
space, |
| 436 |
} => { |
| 437 |
let base_numeric_type = match size { |
| 438 |
Some(size) => NumericType::Vector { size, scalar }, |
| 439 |
None => NumericType::Scalar(scalar), |
| 440 |
}; |
| 441 |
LocalType::Pointer { |
| 442 |
base: self.get_numeric_type_id(base_numeric_type), |
| 443 |
class: map_storage_class(space), |
| 444 |
} |
| 445 |
} |
| 446 |
crate::TypeInner::Image { |
| 447 |
dim, |
| 448 |
arrayed, |
| 449 |
class, |
| 450 |
} => LocalType::Image(LocalImageType::from_inner(dim, arrayed, class)), |
| 451 |
crate::TypeInner::Sampler { comparison: _ } => LocalType::Sampler, |
| 452 |
crate::TypeInner::AccelerationStructure { .. } => LocalType::AccelerationStructure, |
| 453 |
crate::TypeInner::RayQuery { .. } => LocalType::RayQuery, |
| 454 |
crate::TypeInner::Array { .. } |
| 455 |
| crate::TypeInner::Struct { .. } |
| 456 |
| crate::TypeInner::BindingArray { .. } => return None, |
| 457 |
}) |
| 458 |
} |
| 459 |
|
| 460 |
/// Emits code for any wrapper functions required by the expressions in ir_function. |
| 461 |
/// The IDs of any emitted functions will be stored in [`Self::wrapped_functions`]. |
| 462 |
fn write_wrapped_functions( |
| 463 |
&mut self, |
| 464 |
ir_function: &crate::Function, |
| 465 |
info: &FunctionInfo, |
| 466 |
ir_module: &crate::Module, |
| 467 |
) -> Result<(), Error> { |
| 468 |
log::trace!("Generating wrapped functions for {:?}", ir_function.name); |
| 469 |
|
| 470 |
for (expr_handle, expr) in ir_function.expressions.iter() { |
| 471 |
match *expr { |
| 472 |
crate::Expression::Binary { op, left, right } => { |
| 473 |
let expr_ty_inner = info[expr_handle].ty.inner_with(&ir_module.types); |
| 474 |
if let Some(expr_ty) = NumericType::from_inner(expr_ty_inner) { |
| 475 |
match (op, expr_ty.scalar().kind) { |
| 476 |
// Division and modulo are undefined behaviour when the |
| 477 |
// dividend is the minimum representable value and the divisor |
| 478 |
// is negative one, or when the divisor is zero. These wrapped |
| 479 |
// functions override the divisor to one in these cases, |
| 480 |
// matching the WGSL spec. |
| 481 |
( |
| 482 |
crate::BinaryOperator::Divide | crate::BinaryOperator::Modulo, |
| 483 |
crate::ScalarKind::Sint | crate::ScalarKind::Uint, |
| 484 |
) => { |
| 485 |
self.write_wrapped_binary_op( |
| 486 |
op, |
| 487 |
expr_ty, |
| 488 |
&info[left].ty, |
| 489 |
&info[right].ty, |
| 490 |
)?; |
| 491 |
} |
| 492 |
_ => {} |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
_ => {} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
Ok(()) |
| 501 |
} |
| 502 |
|
| 503 |
/// Write a SPIR-V function that performs the operator `op` with Naga IR semantics. |
| 504 |
/// |
| 505 |
/// Define a function that performs an integer division or modulo operation, |
| 506 |
/// except that using a divisor of zero or causing signed overflow with a |
| 507 |
/// divisor of -1 returns the numerator unchanged, rather than exhibiting |
| 508 |
/// undefined behavior. |
| 509 |
/// |
| 510 |
/// Store the generated function's id in the [`wrapped_functions`] table. |
| 511 |
/// |
| 512 |
/// The operator `op` must be either [`Divide`] or [`Modulo`]. |
| 513 |
/// |
| 514 |
/// # Panics |
| 515 |
/// |
| 516 |
/// The `return_type`, `left_type` or `right_type` arguments must all be |
| 517 |
/// integer scalars or vectors. If not, this function panics. |
| 518 |
/// |
| 519 |
/// [`wrapped_functions`]: Writer::wrapped_functions |
| 520 |
/// [`Divide`]: crate::BinaryOperator::Divide |
| 521 |
/// [`Modulo`]: crate::BinaryOperator::Modulo |
| 522 |
fn write_wrapped_binary_op( |
| 523 |
&mut self, |
| 524 |
op: crate::BinaryOperator, |
| 525 |
return_type: NumericType, |
| 526 |
left_type: &TypeResolution, |
| 527 |
right_type: &TypeResolution, |
| 528 |
) -> Result<(), Error> { |
| 529 |
let return_type_id = self.get_localtype_id(LocalType::Numeric(return_type)); |
| 530 |
let left_type_id = self.get_expression_type_id(left_type); |
| 531 |
let right_type_id = self.get_expression_type_id(right_type); |
| 532 |
|
| 533 |
// Check if we've already emitted this function. |
| 534 |
let wrapped = WrappedFunction::BinaryOp { |
| 535 |
op, |
| 536 |
left_type_id, |
| 537 |
right_type_id, |
| 538 |
}; |
| 539 |
let function_id = match self.wrapped_functions.entry(wrapped) { |
| 540 |
Entry::Occupied(_) => return Ok(()), |
| 541 |
Entry::Vacant(e) => *e.insert(self.id_gen.next()), |
| 542 |
}; |
| 543 |
|
| 544 |
let scalar = return_type.scalar(); |
| 545 |
|
| 546 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 547 |
let function_name = match op { |
| 548 |
crate::BinaryOperator::Divide => "naga_div", |
| 549 |
crate::BinaryOperator::Modulo => "naga_mod", |
| 550 |
_ => unreachable!(), |
| 551 |
}; |
| 552 |
self.debugs |
| 553 |
.push(Instruction::name(function_id, function_name)); |
| 554 |
} |
| 555 |
let mut function = Function::default(); |
| 556 |
|
| 557 |
let function_type_id = self.get_function_type(LookupFunctionType { |
| 558 |
parameter_type_ids: vec![left_type_id, right_type_id], |
| 559 |
return_type_id, |
| 560 |
}); |
| 561 |
function.signature = Some(Instruction::function( |
| 562 |
return_type_id, |
| 563 |
function_id, |
| 564 |
spirv::FunctionControl::empty(), |
| 565 |
function_type_id, |
| 566 |
)); |
| 567 |
|
| 568 |
let lhs_id = self.id_gen.next(); |
| 569 |
let rhs_id = self.id_gen.next(); |
| 570 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 571 |
self.debugs.push(Instruction::name(lhs_id, "lhs")); |
| 572 |
self.debugs.push(Instruction::name(rhs_id, "rhs")); |
| 573 |
} |
| 574 |
let left_par = Instruction::function_parameter(left_type_id, lhs_id); |
| 575 |
let right_par = Instruction::function_parameter(right_type_id, rhs_id); |
| 576 |
for instruction in [left_par, right_par] { |
| 577 |
function.parameters.push(FunctionArgument { |
| 578 |
instruction, |
| 579 |
handle_id: 0, |
| 580 |
}); |
| 581 |
} |
| 582 |
|
| 583 |
let label_id = self.id_gen.next(); |
| 584 |
let mut block = Block::new(label_id); |
| 585 |
|
| 586 |
let bool_type = return_type.with_scalar(crate::Scalar::BOOL); |
| 587 |
let bool_type_id = self.get_numeric_type_id(bool_type); |
| 588 |
|
| 589 |
let maybe_splat_const = |writer: &mut Self, const_id| match return_type { |
| 590 |
NumericType::Scalar(_) => const_id, |
| 591 |
NumericType::Vector { size, .. } => { |
| 592 |
let constituent_ids = [const_id; crate::VectorSize::MAX]; |
| 593 |
writer.get_constant_composite( |
| 594 |
LookupType::Local(LocalType::Numeric(return_type)), |
| 595 |
&constituent_ids[..size as usize], |
| 596 |
) |
| 597 |
} |
| 598 |
NumericType::Matrix { .. } => unreachable!(), |
| 599 |
}; |
| 600 |
|
| 601 |
let const_zero_id = self.get_constant_scalar_with(0, scalar)?; |
| 602 |
let composite_zero_id = maybe_splat_const(self, const_zero_id); |
| 603 |
let rhs_eq_zero_id = self.id_gen.next(); |
| 604 |
block.body.push(Instruction::binary( |
| 605 |
spirv::Op::IEqual, |
| 606 |
bool_type_id, |
| 607 |
rhs_eq_zero_id, |
| 608 |
rhs_id, |
| 609 |
composite_zero_id, |
| 610 |
)); |
| 611 |
let divisor_selector_id = match scalar.kind { |
| 612 |
crate::ScalarKind::Sint => { |
| 613 |
let (const_min_id, const_neg_one_id) = match scalar.width { |
| 614 |
4 => Ok(( |
| 615 |
self.get_constant_scalar(crate::Literal::I32(i32::MIN)), |
| 616 |
self.get_constant_scalar(crate::Literal::I32(-1i32)), |
| 617 |
)), |
| 618 |
8 => Ok(( |
| 619 |
self.get_constant_scalar(crate::Literal::I64(i64::MIN)), |
| 620 |
self.get_constant_scalar(crate::Literal::I64(-1i64)), |
| 621 |
)), |
| 622 |
_ => Err(Error::Validation("Unexpected scalar width")), |
| 623 |
}?; |
| 624 |
let composite_min_id = maybe_splat_const(self, const_min_id); |
| 625 |
let composite_neg_one_id = maybe_splat_const(self, const_neg_one_id); |
| 626 |
|
| 627 |
let lhs_eq_int_min_id = self.id_gen.next(); |
| 628 |
block.body.push(Instruction::binary( |
| 629 |
spirv::Op::IEqual, |
| 630 |
bool_type_id, |
| 631 |
lhs_eq_int_min_id, |
| 632 |
lhs_id, |
| 633 |
composite_min_id, |
| 634 |
)); |
| 635 |
let rhs_eq_neg_one_id = self.id_gen.next(); |
| 636 |
block.body.push(Instruction::binary( |
| 637 |
spirv::Op::IEqual, |
| 638 |
bool_type_id, |
| 639 |
rhs_eq_neg_one_id, |
| 640 |
rhs_id, |
| 641 |
composite_neg_one_id, |
| 642 |
)); |
| 643 |
let lhs_eq_int_min_and_rhs_eq_neg_one_id = self.id_gen.next(); |
| 644 |
block.body.push(Instruction::binary( |
| 645 |
spirv::Op::LogicalAnd, |
| 646 |
bool_type_id, |
| 647 |
lhs_eq_int_min_and_rhs_eq_neg_one_id, |
| 648 |
lhs_eq_int_min_id, |
| 649 |
rhs_eq_neg_one_id, |
| 650 |
)); |
| 651 |
let rhs_eq_zero_or_lhs_eq_int_min_and_rhs_eq_neg_one_id = self.id_gen.next(); |
| 652 |
block.body.push(Instruction::binary( |
| 653 |
spirv::Op::LogicalOr, |
| 654 |
bool_type_id, |
| 655 |
rhs_eq_zero_or_lhs_eq_int_min_and_rhs_eq_neg_one_id, |
| 656 |
rhs_eq_zero_id, |
| 657 |
lhs_eq_int_min_and_rhs_eq_neg_one_id, |
| 658 |
)); |
| 659 |
rhs_eq_zero_or_lhs_eq_int_min_and_rhs_eq_neg_one_id |
| 660 |
} |
| 661 |
crate::ScalarKind::Uint => rhs_eq_zero_id, |
| 662 |
_ => unreachable!(), |
| 663 |
}; |
| 664 |
|
| 665 |
let const_one_id = self.get_constant_scalar_with(1, scalar)?; |
| 666 |
let composite_one_id = maybe_splat_const(self, const_one_id); |
| 667 |
let divisor_id = self.id_gen.next(); |
| 668 |
block.body.push(Instruction::select( |
| 669 |
right_type_id, |
| 670 |
divisor_id, |
| 671 |
divisor_selector_id, |
| 672 |
composite_one_id, |
| 673 |
rhs_id, |
| 674 |
)); |
| 675 |
let op = match (op, scalar.kind) { |
| 676 |
(crate::BinaryOperator::Divide, crate::ScalarKind::Sint) => spirv::Op::SDiv, |
| 677 |
(crate::BinaryOperator::Divide, crate::ScalarKind::Uint) => spirv::Op::UDiv, |
| 678 |
(crate::BinaryOperator::Modulo, crate::ScalarKind::Sint) => spirv::Op::SRem, |
| 679 |
(crate::BinaryOperator::Modulo, crate::ScalarKind::Uint) => spirv::Op::UMod, |
| 680 |
_ => unreachable!(), |
| 681 |
}; |
| 682 |
let return_id = self.id_gen.next(); |
| 683 |
block.body.push(Instruction::binary( |
| 684 |
op, |
| 685 |
return_type_id, |
| 686 |
return_id, |
| 687 |
lhs_id, |
| 688 |
divisor_id, |
| 689 |
)); |
| 690 |
|
| 691 |
function.consume(block, Instruction::return_value(return_id)); |
| 692 |
function.to_words(&mut self.logical_layout.function_definitions); |
| 693 |
Ok(()) |
| 694 |
} |
| 695 |
|
| 696 |
fn write_function( |
| 697 |
&mut self, |
| 698 |
ir_function: &crate::Function, |
| 699 |
info: &FunctionInfo, |
| 700 |
ir_module: &crate::Module, |
| 701 |
mut interface: Option<FunctionInterface>, |
| 702 |
debug_info: &Option<DebugInfoInner>, |
| 703 |
) -> Result<Word, Error> { |
| 704 |
self.write_wrapped_functions(ir_function, info, ir_module)?; |
| 705 |
|
| 706 |
log::trace!("Generating code for {:?}", ir_function.name); |
| 707 |
let mut function = Function::default(); |
| 708 |
|
| 709 |
let prelude_id = self.id_gen.next(); |
| 710 |
let mut prelude = Block::new(prelude_id); |
| 711 |
let mut ep_context = EntryPointContext { |
| 712 |
argument_ids: Vec::new(), |
| 713 |
results: Vec::new(), |
| 714 |
}; |
| 715 |
|
| 716 |
let mut local_invocation_id = None; |
| 717 |
|
| 718 |
let mut parameter_type_ids = Vec::with_capacity(ir_function.arguments.len()); |
| 719 |
for argument in ir_function.arguments.iter() { |
| 720 |
let class = spirv::StorageClass::Input; |
| 721 |
let handle_ty = ir_module.types[argument.ty].inner.is_handle(); |
| 722 |
let argument_type_id = if handle_ty { |
| 723 |
self.get_handle_pointer_type_id(argument.ty, spirv::StorageClass::UniformConstant) |
| 724 |
} else { |
| 725 |
self.get_handle_type_id(argument.ty) |
| 726 |
}; |
| 727 |
|
| 728 |
if let Some(ref mut iface) = interface { |
| 729 |
let id = if let Some(ref binding) = argument.binding { |
| 730 |
let name = argument.name.as_deref(); |
| 731 |
|
| 732 |
let varying_id = self.write_varying( |
| 733 |
ir_module, |
| 734 |
iface.stage, |
| 735 |
class, |
| 736 |
name, |
| 737 |
argument.ty, |
| 738 |
binding, |
| 739 |
)?; |
| 740 |
iface.varying_ids.push(varying_id); |
| 741 |
let id = self.id_gen.next(); |
| 742 |
prelude |
| 743 |
.body |
| 744 |
.push(Instruction::load(argument_type_id, id, varying_id, None)); |
| 745 |
|
| 746 |
if binding == &crate::Binding::BuiltIn(crate::BuiltIn::LocalInvocationId) { |
| 747 |
local_invocation_id = Some(id); |
| 748 |
} |
| 749 |
|
| 750 |
id |
| 751 |
} else if let crate::TypeInner::Struct { ref members, .. } = |
| 752 |
ir_module.types[argument.ty].inner |
| 753 |
{ |
| 754 |
let struct_id = self.id_gen.next(); |
| 755 |
let mut constituent_ids = Vec::with_capacity(members.len()); |
| 756 |
for member in members { |
| 757 |
let type_id = self.get_handle_type_id(member.ty); |
| 758 |
let name = member.name.as_deref(); |
| 759 |
let binding = member.binding.as_ref().unwrap(); |
| 760 |
let varying_id = self.write_varying( |
| 761 |
ir_module, |
| 762 |
iface.stage, |
| 763 |
class, |
| 764 |
name, |
| 765 |
member.ty, |
| 766 |
binding, |
| 767 |
)?; |
| 768 |
iface.varying_ids.push(varying_id); |
| 769 |
let id = self.id_gen.next(); |
| 770 |
prelude |
| 771 |
.body |
| 772 |
.push(Instruction::load(type_id, id, varying_id, None)); |
| 773 |
constituent_ids.push(id); |
| 774 |
|
| 775 |
if binding == &crate::Binding::BuiltIn(crate::BuiltIn::GlobalInvocationId) { |
| 776 |
local_invocation_id = Some(id); |
| 777 |
} |
| 778 |
} |
| 779 |
prelude.body.push(Instruction::composite_construct( |
| 780 |
argument_type_id, |
| 781 |
struct_id, |
| 782 |
&constituent_ids, |
| 783 |
)); |
| 784 |
struct_id |
| 785 |
} else { |
| 786 |
unreachable!("Missing argument binding on an entry point"); |
| 787 |
}; |
| 788 |
ep_context.argument_ids.push(id); |
| 789 |
} else { |
| 790 |
let argument_id = self.id_gen.next(); |
| 791 |
let instruction = Instruction::function_parameter(argument_type_id, argument_id); |
| 792 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 793 |
if let Some(ref name) = argument.name { |
| 794 |
self.debugs.push(Instruction::name(argument_id, name)); |
| 795 |
} |
| 796 |
} |
| 797 |
function.parameters.push(FunctionArgument { |
| 798 |
instruction, |
| 799 |
handle_id: if handle_ty { |
| 800 |
let id = self.id_gen.next(); |
| 801 |
prelude.body.push(Instruction::load( |
| 802 |
self.get_handle_type_id(argument.ty), |
| 803 |
id, |
| 804 |
argument_id, |
| 805 |
None, |
| 806 |
)); |
| 807 |
id |
| 808 |
} else { |
| 809 |
0 |
| 810 |
}, |
| 811 |
}); |
| 812 |
parameter_type_ids.push(argument_type_id); |
| 813 |
}; |
| 814 |
} |
| 815 |
|
| 816 |
let return_type_id = match ir_function.result { |
| 817 |
Some(ref result) => { |
| 818 |
if let Some(ref mut iface) = interface { |
| 819 |
let mut has_point_size = false; |
| 820 |
let class = spirv::StorageClass::Output; |
| 821 |
if let Some(ref binding) = result.binding { |
| 822 |
has_point_size |= |
| 823 |
*binding == crate::Binding::BuiltIn(crate::BuiltIn::PointSize); |
| 824 |
let type_id = self.get_handle_type_id(result.ty); |
| 825 |
let varying_id = self.write_varying( |
| 826 |
ir_module, |
| 827 |
iface.stage, |
| 828 |
class, |
| 829 |
None, |
| 830 |
result.ty, |
| 831 |
binding, |
| 832 |
)?; |
| 833 |
iface.varying_ids.push(varying_id); |
| 834 |
ep_context.results.push(ResultMember { |
| 835 |
id: varying_id, |
| 836 |
type_id, |
| 837 |
built_in: binding.to_built_in(), |
| 838 |
}); |
| 839 |
} else if let crate::TypeInner::Struct { ref members, .. } = |
| 840 |
ir_module.types[result.ty].inner |
| 841 |
{ |
| 842 |
for member in members { |
| 843 |
let type_id = self.get_handle_type_id(member.ty); |
| 844 |
let name = member.name.as_deref(); |
| 845 |
let binding = member.binding.as_ref().unwrap(); |
| 846 |
has_point_size |= |
| 847 |
*binding == crate::Binding::BuiltIn(crate::BuiltIn::PointSize); |
| 848 |
let varying_id = self.write_varying( |
| 849 |
ir_module, |
| 850 |
iface.stage, |
| 851 |
class, |
| 852 |
name, |
| 853 |
member.ty, |
| 854 |
binding, |
| 855 |
)?; |
| 856 |
iface.varying_ids.push(varying_id); |
| 857 |
ep_context.results.push(ResultMember { |
| 858 |
id: varying_id, |
| 859 |
type_id, |
| 860 |
built_in: binding.to_built_in(), |
| 861 |
}); |
| 862 |
} |
| 863 |
} else { |
| 864 |
unreachable!("Missing result binding on an entry point"); |
| 865 |
} |
| 866 |
|
| 867 |
if self.flags.contains(WriterFlags::FORCE_POINT_SIZE) |
| 868 |
&& iface.stage == crate::ShaderStage::Vertex |
| 869 |
&& !has_point_size |
| 870 |
{ |
| 871 |
// add point size artificially |
| 872 |
let varying_id = self.id_gen.next(); |
| 873 |
let pointer_type_id = self.get_f32_pointer_type_id(class); |
| 874 |
Instruction::variable(pointer_type_id, varying_id, class, None) |
| 875 |
.to_words(&mut self.logical_layout.declarations); |
| 876 |
self.decorate( |
| 877 |
varying_id, |
| 878 |
spirv::Decoration::BuiltIn, |
| 879 |
&[spirv::BuiltIn::PointSize as u32], |
| 880 |
); |
| 881 |
iface.varying_ids.push(varying_id); |
| 882 |
|
| 883 |
let default_value_id = self.get_constant_scalar(crate::Literal::F32(1.0)); |
| 884 |
prelude |
| 885 |
.body |
| 886 |
.push(Instruction::store(varying_id, default_value_id, None)); |
| 887 |
} |
| 888 |
self.void_type |
| 889 |
} else { |
| 890 |
self.get_handle_type_id(result.ty) |
| 891 |
} |
| 892 |
} |
| 893 |
None => self.void_type, |
| 894 |
}; |
| 895 |
|
| 896 |
let lookup_function_type = LookupFunctionType { |
| 897 |
parameter_type_ids, |
| 898 |
return_type_id, |
| 899 |
}; |
| 900 |
|
| 901 |
let function_id = self.id_gen.next(); |
| 902 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 903 |
if let Some(ref name) = ir_function.name { |
| 904 |
self.debugs.push(Instruction::name(function_id, name)); |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
let function_type = self.get_function_type(lookup_function_type); |
| 909 |
function.signature = Some(Instruction::function( |
| 910 |
return_type_id, |
| 911 |
function_id, |
| 912 |
spirv::FunctionControl::empty(), |
| 913 |
function_type, |
| 914 |
)); |
| 915 |
|
| 916 |
if interface.is_some() { |
| 917 |
function.entry_point_context = Some(ep_context); |
| 918 |
} |
| 919 |
|
| 920 |
// fill up the `GlobalVariable::access_id` |
| 921 |
for gv in self.global_variables.iter_mut() { |
| 922 |
gv.reset_for_function(); |
| 923 |
} |
| 924 |
for (handle, var) in ir_module.global_variables.iter() { |
| 925 |
if info[handle].is_empty() { |
| 926 |
continue; |
| 927 |
} |
| 928 |
|
| 929 |
let mut gv = self.global_variables[handle].clone(); |
| 930 |
if let Some(ref mut iface) = interface { |
| 931 |
// Have to include global variables in the interface |
| 932 |
if self.physical_layout.version >= 0x10400 { |
| 933 |
iface.varying_ids.push(gv.var_id); |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
// Handle globals are pre-emitted and should be loaded automatically. |
| 938 |
// |
| 939 |
// Any that are binding arrays we skip as we cannot load the array, we must load the result after indexing. |
| 940 |
match ir_module.types[var.ty].inner { |
| 941 |
crate::TypeInner::BindingArray { .. } => { |
| 942 |
gv.access_id = gv.var_id; |
| 943 |
} |
| 944 |
_ => { |
| 945 |
if var.space == crate::AddressSpace::Handle { |
| 946 |
let var_type_id = self.get_handle_type_id(var.ty); |
| 947 |
let id = self.id_gen.next(); |
| 948 |
prelude |
| 949 |
.body |
| 950 |
.push(Instruction::load(var_type_id, id, gv.var_id, None)); |
| 951 |
gv.access_id = gv.var_id; |
| 952 |
gv.handle_id = id; |
| 953 |
} else if global_needs_wrapper(ir_module, var) { |
| 954 |
let class = map_storage_class(var.space); |
| 955 |
let pointer_type_id = self.get_handle_pointer_type_id(var.ty, class); |
| 956 |
let index_id = self.get_index_constant(0); |
| 957 |
let id = self.id_gen.next(); |
| 958 |
prelude.body.push(Instruction::access_chain( |
| 959 |
pointer_type_id, |
| 960 |
id, |
| 961 |
gv.var_id, |
| 962 |
&[index_id], |
| 963 |
)); |
| 964 |
gv.access_id = id; |
| 965 |
} else { |
| 966 |
// by default, the variable ID is accessed as is |
| 967 |
gv.access_id = gv.var_id; |
| 968 |
}; |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
// work around borrow checking in the presence of `self.xxx()` calls |
| 973 |
self.global_variables[handle] = gv; |
| 974 |
} |
| 975 |
|
| 976 |
// Create a `BlockContext` for generating SPIR-V for the function's |
| 977 |
// body. |
| 978 |
let mut context = BlockContext { |
| 979 |
ir_module, |
| 980 |
ir_function, |
| 981 |
fun_info: info, |
| 982 |
function: &mut function, |
| 983 |
// Re-use the cached expression table from prior functions. |
| 984 |
cached: core::mem::take(&mut self.saved_cached), |
| 985 |
|
| 986 |
// Steal the Writer's temp list for a bit. |
| 987 |
temp_list: core::mem::take(&mut self.temp_list), |
| 988 |
force_loop_bounding: self.force_loop_bounding, |
| 989 |
writer: self, |
| 990 |
expression_constness: super::ExpressionConstnessTracker::from_arena( |
| 991 |
&ir_function.expressions, |
| 992 |
), |
| 993 |
}; |
| 994 |
|
| 995 |
// fill up the pre-emitted and const expressions |
| 996 |
context.cached.reset(ir_function.expressions.len()); |
| 997 |
for (handle, expr) in ir_function.expressions.iter() { |
| 998 |
if (expr.needs_pre_emit() && !matches!(*expr, crate::Expression::LocalVariable(_))) |
| 999 |
|| context.expression_constness.is_const(handle) |
| 1000 |
{ |
| 1001 |
context.cache_expression_value(handle, &mut prelude)?; |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
for (handle, variable) in ir_function.local_variables.iter() { |
| 1006 |
let id = context.gen_id(); |
| 1007 |
|
| 1008 |
if context.writer.flags.contains(WriterFlags::DEBUG) { |
| 1009 |
if let Some(ref name) = variable.name { |
| 1010 |
context.writer.debugs.push(Instruction::name(id, name)); |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
let init_word = variable.init.map(|constant| context.cached[constant]); |
| 1015 |
let pointer_type_id = context |
| 1016 |
.writer |
| 1017 |
.get_handle_pointer_type_id(variable.ty, spirv::StorageClass::Function); |
| 1018 |
let instruction = Instruction::variable( |
| 1019 |
pointer_type_id, |
| 1020 |
id, |
| 1021 |
spirv::StorageClass::Function, |
| 1022 |
init_word.or_else(|| match ir_module.types[variable.ty].inner { |
| 1023 |
crate::TypeInner::RayQuery { .. } => None, |
| 1024 |
_ => { |
| 1025 |
let type_id = context.get_handle_type_id(variable.ty); |
| 1026 |
Some(context.writer.write_constant_null(type_id)) |
| 1027 |
} |
| 1028 |
}), |
| 1029 |
); |
| 1030 |
context |
| 1031 |
.function |
| 1032 |
.variables |
| 1033 |
.insert(handle, LocalVariable { id, instruction }); |
| 1034 |
} |
| 1035 |
|
| 1036 |
for (handle, expr) in ir_function.expressions.iter() { |
| 1037 |
match *expr { |
| 1038 |
crate::Expression::LocalVariable(_) => { |
| 1039 |
// Cache the `OpVariable` instruction we generated above as |
| 1040 |
// the value of this expression. |
| 1041 |
context.cache_expression_value(handle, &mut prelude)?; |
| 1042 |
} |
| 1043 |
crate::Expression::Access { base, .. } |
| 1044 |
| crate::Expression::AccessIndex { base, .. } => { |
| 1045 |
// Count references to `base` by `Access` and `AccessIndex` |
| 1046 |
// instructions. See `access_uses` for details. |
| 1047 |
*context.function.access_uses.entry(base).or_insert(0) += 1; |
| 1048 |
} |
| 1049 |
_ => {} |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
let next_id = context.gen_id(); |
| 1054 |
|
| 1055 |
context |
| 1056 |
.function |
| 1057 |
.consume(prelude, Instruction::branch(next_id)); |
| 1058 |
|
| 1059 |
let workgroup_vars_init_exit_block_id = |
| 1060 |
match (context.writer.zero_initialize_workgroup_memory, interface) { |
| 1061 |
( |
| 1062 |
super::ZeroInitializeWorkgroupMemoryMode::Polyfill, |
| 1063 |
Some( |
| 1064 |
ref mut interface @ FunctionInterface { |
| 1065 |
stage: crate::ShaderStage::Compute, |
| 1066 |
.. |
| 1067 |
}, |
| 1068 |
), |
| 1069 |
) => context.writer.generate_workgroup_vars_init_block( |
| 1070 |
next_id, |
| 1071 |
ir_module, |
| 1072 |
info, |
| 1073 |
local_invocation_id, |
| 1074 |
interface, |
| 1075 |
context.function, |
| 1076 |
), |
| 1077 |
_ => None, |
| 1078 |
}; |
| 1079 |
|
| 1080 |
let main_id = if let Some(exit_id) = workgroup_vars_init_exit_block_id { |
| 1081 |
exit_id |
| 1082 |
} else { |
| 1083 |
next_id |
| 1084 |
}; |
| 1085 |
|
| 1086 |
context.write_function_body(main_id, debug_info.as_ref())?; |
| 1087 |
|
| 1088 |
// Consume the `BlockContext`, ending its borrows and letting the |
| 1089 |
// `Writer` steal back its cached expression table and temp_list. |
| 1090 |
let BlockContext { |
| 1091 |
cached, temp_list, .. |
| 1092 |
} = context; |
| 1093 |
self.saved_cached = cached; |
| 1094 |
self.temp_list = temp_list; |
| 1095 |
|
| 1096 |
function.to_words(&mut self.logical_layout.function_definitions); |
| 1097 |
|
| 1098 |
Ok(function_id) |
| 1099 |
} |
| 1100 |
|
| 1101 |
fn write_execution_mode( |
| 1102 |
&mut self, |
| 1103 |
function_id: Word, |
| 1104 |
mode: spirv::ExecutionMode, |
| 1105 |
) -> Result<(), Error> { |
| 1106 |
//self.check(mode.required_capabilities())?; |
| 1107 |
Instruction::execution_mode(function_id, mode, &[]) |
| 1108 |
.to_words(&mut self.logical_layout.execution_modes); |
| 1109 |
Ok(()) |
| 1110 |
} |
| 1111 |
|
| 1112 |
// TODO Move to instructions module |
| 1113 |
fn write_entry_point( |
| 1114 |
&mut self, |
| 1115 |
entry_point: &crate::EntryPoint, |
| 1116 |
info: &FunctionInfo, |
| 1117 |
ir_module: &crate::Module, |
| 1118 |
debug_info: &Option<DebugInfoInner>, |
| 1119 |
) -> Result<Instruction, Error> { |
| 1120 |
let mut interface_ids = Vec::new(); |
| 1121 |
let function_id = self.write_function( |
| 1122 |
&entry_point.function, |
| 1123 |
info, |
| 1124 |
ir_module, |
| 1125 |
Some(FunctionInterface { |
| 1126 |
varying_ids: &mut interface_ids, |
| 1127 |
stage: entry_point.stage, |
| 1128 |
}), |
| 1129 |
debug_info, |
| 1130 |
)?; |
| 1131 |
|
| 1132 |
let exec_model = match entry_point.stage { |
| 1133 |
crate::ShaderStage::Vertex => spirv::ExecutionModel::Vertex, |
| 1134 |
crate::ShaderStage::Fragment => { |
| 1135 |
self.write_execution_mode(function_id, spirv::ExecutionMode::OriginUpperLeft)?; |
| 1136 |
if let Some(ref result) = entry_point.function.result { |
| 1137 |
if contains_builtin( |
| 1138 |
result.binding.as_ref(), |
| 1139 |
result.ty, |
| 1140 |
&ir_module.types, |
| 1141 |
crate::BuiltIn::FragDepth, |
| 1142 |
) { |
| 1143 |
self.write_execution_mode( |
| 1144 |
function_id, |
| 1145 |
spirv::ExecutionMode::DepthReplacing, |
| 1146 |
)?; |
| 1147 |
} |
| 1148 |
} |
| 1149 |
spirv::ExecutionModel::Fragment |
| 1150 |
} |
| 1151 |
crate::ShaderStage::Compute => { |
| 1152 |
let execution_mode = spirv::ExecutionMode::LocalSize; |
| 1153 |
//self.check(execution_mode.required_capabilities())?; |
| 1154 |
Instruction::execution_mode( |
| 1155 |
function_id, |
| 1156 |
execution_mode, |
| 1157 |
&entry_point.workgroup_size, |
| 1158 |
) |
| 1159 |
.to_words(&mut self.logical_layout.execution_modes); |
| 1160 |
spirv::ExecutionModel::GLCompute |
| 1161 |
} |
| 1162 |
crate::ShaderStage::Task | crate::ShaderStage::Mesh => unreachable!(), |
| 1163 |
}; |
| 1164 |
//self.check(exec_model.required_capabilities())?; |
| 1165 |
|
| 1166 |
Ok(Instruction::entry_point( |
| 1167 |
exec_model, |
| 1168 |
function_id, |
| 1169 |
&entry_point.name, |
| 1170 |
interface_ids.as_slice(), |
| 1171 |
)) |
| 1172 |
} |
| 1173 |
|
| 1174 |
fn make_scalar(&mut self, id: Word, scalar: crate::Scalar) -> Instruction { |
| 1175 |
use crate::ScalarKind as Sk; |
| 1176 |
|
| 1177 |
let bits = (scalar.width * BITS_PER_BYTE) as u32; |
| 1178 |
match scalar.kind { |
| 1179 |
Sk::Sint | Sk::Uint => { |
| 1180 |
let signedness = if scalar.kind == Sk::Sint { |
| 1181 |
super::instructions::Signedness::Signed |
| 1182 |
} else { |
| 1183 |
super::instructions::Signedness::Unsigned |
| 1184 |
}; |
| 1185 |
let cap = match bits { |
| 1186 |
8 => Some(spirv::Capability::Int8), |
| 1187 |
16 => Some(spirv::Capability::Int16), |
| 1188 |
64 => Some(spirv::Capability::Int64), |
| 1189 |
_ => None, |
| 1190 |
}; |
| 1191 |
if let Some(cap) = cap { |
| 1192 |
self.capabilities_used.insert(cap); |
| 1193 |
} |
| 1194 |
Instruction::type_int(id, bits, signedness) |
| 1195 |
} |
| 1196 |
Sk::Float => { |
| 1197 |
if bits == 64 { |
| 1198 |
self.capabilities_used.insert(spirv::Capability::Float64); |
| 1199 |
} |
| 1200 |
if bits == 16 { |
| 1201 |
self.capabilities_used.insert(spirv::Capability::Float16); |
| 1202 |
self.capabilities_used |
| 1203 |
.insert(spirv::Capability::StorageBuffer16BitAccess); |
| 1204 |
self.capabilities_used |
| 1205 |
.insert(spirv::Capability::UniformAndStorageBuffer16BitAccess); |
| 1206 |
self.capabilities_used |
| 1207 |
.insert(spirv::Capability::StorageInputOutput16); |
| 1208 |
} |
| 1209 |
Instruction::type_float(id, bits) |
| 1210 |
} |
| 1211 |
Sk::Bool => Instruction::type_bool(id), |
| 1212 |
Sk::AbstractInt | Sk::AbstractFloat => { |
| 1213 |
unreachable!("abstract types should never reach the backend"); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
fn request_type_capabilities(&mut self, inner: &crate::TypeInner) -> Result<(), Error> { |
| 1219 |
match *inner { |
| 1220 |
crate::TypeInner::Image { |
| 1221 |
dim, |
| 1222 |
arrayed, |
| 1223 |
class, |
| 1224 |
} => { |
| 1225 |
let sampled = match class { |
| 1226 |
crate::ImageClass::Sampled { .. } => true, |
| 1227 |
crate::ImageClass::Depth { .. } => true, |
| 1228 |
crate::ImageClass::Storage { format, .. } => { |
| 1229 |
self.request_image_format_capabilities(format.into())?; |
| 1230 |
false |
| 1231 |
} |
| 1232 |
}; |
| 1233 |
|
| 1234 |
match dim { |
| 1235 |
crate::ImageDimension::D1 => { |
| 1236 |
if sampled { |
| 1237 |
self.require_any("sampled 1D images", &[spirv::Capability::Sampled1D])?; |
| 1238 |
} else { |
| 1239 |
self.require_any("1D storage images", &[spirv::Capability::Image1D])?; |
| 1240 |
} |
| 1241 |
} |
| 1242 |
crate::ImageDimension::Cube if arrayed => { |
| 1243 |
if sampled { |
| 1244 |
self.require_any( |
| 1245 |
"sampled cube array images", |
| 1246 |
&[spirv::Capability::SampledCubeArray], |
| 1247 |
)?; |
| 1248 |
} else { |
| 1249 |
self.require_any( |
| 1250 |
"cube array storage images", |
| 1251 |
&[spirv::Capability::ImageCubeArray], |
| 1252 |
)?; |
| 1253 |
} |
| 1254 |
} |
| 1255 |
_ => {} |
| 1256 |
} |
| 1257 |
} |
| 1258 |
crate::TypeInner::AccelerationStructure { .. } => { |
| 1259 |
self.require_any("Acceleration Structure", &[spirv::Capability::RayQueryKHR])?; |
| 1260 |
} |
| 1261 |
crate::TypeInner::RayQuery { .. } => { |
| 1262 |
self.require_any("Ray Query", &[spirv::Capability::RayQueryKHR])?; |
| 1263 |
} |
| 1264 |
crate::TypeInner::Atomic(crate::Scalar { width: 8, kind: _ }) => { |
| 1265 |
self.require_any("64 bit integer atomics", &[spirv::Capability::Int64Atomics])?; |
| 1266 |
} |
| 1267 |
crate::TypeInner::Atomic(crate::Scalar { |
| 1268 |
width: 4, |
| 1269 |
kind: crate::ScalarKind::Float, |
| 1270 |
}) => { |
| 1271 |
self.require_any( |
| 1272 |
"32 bit floating-point atomics", |
| 1273 |
&[spirv::Capability::AtomicFloat32AddEXT], |
| 1274 |
)?; |
| 1275 |
self.use_extension("SPV_EXT_shader_atomic_float_add"); |
| 1276 |
} |
| 1277 |
// 16 bit floating-point support requires Float16 capability |
| 1278 |
crate::TypeInner::Matrix { |
| 1279 |
scalar: crate::Scalar::F16, |
| 1280 |
.. |
| 1281 |
} |
| 1282 |
| crate::TypeInner::Vector { |
| 1283 |
scalar: crate::Scalar::F16, |
| 1284 |
.. |
| 1285 |
} |
| 1286 |
| crate::TypeInner::Scalar(crate::Scalar::F16) => { |
| 1287 |
self.require_any("16 bit floating-point", &[spirv::Capability::Float16])?; |
| 1288 |
self.use_extension("SPV_KHR_16bit_storage"); |
| 1289 |
} |
| 1290 |
_ => {} |
| 1291 |
} |
| 1292 |
Ok(()) |
| 1293 |
} |
| 1294 |
|
| 1295 |
fn write_numeric_type_declaration_local(&mut self, id: Word, numeric: NumericType) { |
| 1296 |
let instruction = match numeric { |
| 1297 |
NumericType::Scalar(scalar) => self.make_scalar(id, scalar), |
| 1298 |
NumericType::Vector { size, scalar } => { |
| 1299 |
let scalar_id = self.get_numeric_type_id(NumericType::Scalar(scalar)); |
| 1300 |
Instruction::type_vector(id, scalar_id, size) |
| 1301 |
} |
| 1302 |
NumericType::Matrix { |
| 1303 |
columns, |
| 1304 |
rows, |
| 1305 |
scalar, |
| 1306 |
} => { |
| 1307 |
let column_id = |
| 1308 |
self.get_numeric_type_id(NumericType::Vector { size: rows, scalar }); |
| 1309 |
Instruction::type_matrix(id, column_id, columns) |
| 1310 |
} |
| 1311 |
}; |
| 1312 |
|
| 1313 |
instruction.to_words(&mut self.logical_layout.declarations); |
| 1314 |
} |
| 1315 |
|
| 1316 |
fn write_type_declaration_local(&mut self, id: Word, local_ty: LocalType) { |
| 1317 |
let instruction = match local_ty { |
| 1318 |
LocalType::Numeric(numeric) => { |
| 1319 |
self.write_numeric_type_declaration_local(id, numeric); |
| 1320 |
return; |
| 1321 |
} |
| 1322 |
LocalType::Pointer { base, class } => Instruction::type_pointer(id, class, base), |
| 1323 |
LocalType::Image(image) => { |
| 1324 |
let local_type = LocalType::Numeric(NumericType::Scalar(image.sampled_type)); |
| 1325 |
let type_id = self.get_localtype_id(local_type); |
| 1326 |
Instruction::type_image(id, type_id, image.dim, image.flags, image.image_format) |
| 1327 |
} |
| 1328 |
LocalType::Sampler => Instruction::type_sampler(id), |
| 1329 |
LocalType::SampledImage { image_type_id } => { |
| 1330 |
Instruction::type_sampled_image(id, image_type_id) |
| 1331 |
} |
| 1332 |
LocalType::BindingArray { base, size } => { |
| 1333 |
let inner_ty = self.get_handle_type_id(base); |
| 1334 |
let scalar_id = self.get_constant_scalar(crate::Literal::U32(size)); |
| 1335 |
Instruction::type_array(id, inner_ty, scalar_id) |
| 1336 |
} |
| 1337 |
LocalType::AccelerationStructure => Instruction::type_acceleration_structure(id), |
| 1338 |
LocalType::RayQuery => Instruction::type_ray_query(id), |
| 1339 |
}; |
| 1340 |
|
| 1341 |
instruction.to_words(&mut self.logical_layout.declarations); |
| 1342 |
} |
| 1343 |
|
| 1344 |
fn write_type_declaration_arena( |
| 1345 |
&mut self, |
| 1346 |
module: &crate::Module, |
| 1347 |
handle: Handle<crate::Type>, |
| 1348 |
) -> Result<Word, Error> { |
| 1349 |
let ty = &module.types[handle]; |
| 1350 |
// If it's a type that needs SPIR-V capabilities, request them now. |
| 1351 |
// This needs to happen regardless of the LocalType lookup succeeding, |
| 1352 |
// because some types which map to the same LocalType have different |
| 1353 |
// capability requirements. See https://github.com/gfx-rs/wgpu/issues/5569 |
| 1354 |
self.request_type_capabilities(&ty.inner)?; |
| 1355 |
let id = if let Some(local) = self.localtype_from_inner(&ty.inner) { |
| 1356 |
// This type can be represented as a `LocalType`, so check if we've |
| 1357 |
// already written an instruction for it. If not, do so now, with |
| 1358 |
// `write_type_declaration_local`. |
| 1359 |
match self.lookup_type.entry(LookupType::Local(local)) { |
| 1360 |
// We already have an id for this `LocalType`. |
| 1361 |
Entry::Occupied(e) => *e.get(), |
| 1362 |
|
| 1363 |
// It's a type we haven't seen before. |
| 1364 |
Entry::Vacant(e) => { |
| 1365 |
let id = self.id_gen.next(); |
| 1366 |
e.insert(id); |
| 1367 |
|
| 1368 |
self.write_type_declaration_local(id, local); |
| 1369 |
|
| 1370 |
id |
| 1371 |
} |
| 1372 |
} |
| 1373 |
} else { |
| 1374 |
use spirv::Decoration; |
| 1375 |
|
| 1376 |
let id = self.id_gen.next(); |
| 1377 |
let instruction = match ty.inner { |
| 1378 |
crate::TypeInner::Array { base, size, stride } => { |
| 1379 |
self.decorate(id, Decoration::ArrayStride, &[stride]); |
| 1380 |
|
| 1381 |
let type_id = self.get_handle_type_id(base); |
| 1382 |
match size.resolve(module.to_ctx())? { |
| 1383 |
crate::proc::IndexableLength::Known(length) => { |
| 1384 |
let length_id = self.get_index_constant(length); |
| 1385 |
Instruction::type_array(id, type_id, length_id) |
| 1386 |
} |
| 1387 |
crate::proc::IndexableLength::Dynamic => { |
| 1388 |
Instruction::type_runtime_array(id, type_id) |
| 1389 |
} |
| 1390 |
} |
| 1391 |
} |
| 1392 |
crate::TypeInner::BindingArray { base, size } => { |
| 1393 |
let type_id = self.get_handle_type_id(base); |
| 1394 |
match size.resolve(module.to_ctx())? { |
| 1395 |
crate::proc::IndexableLength::Known(length) => { |
| 1396 |
let length_id = self.get_index_constant(length); |
| 1397 |
Instruction::type_array(id, type_id, length_id) |
| 1398 |
} |
| 1399 |
crate::proc::IndexableLength::Dynamic => { |
| 1400 |
Instruction::type_runtime_array(id, type_id) |
| 1401 |
} |
| 1402 |
} |
| 1403 |
} |
| 1404 |
crate::TypeInner::Struct { |
| 1405 |
ref members, |
| 1406 |
span: _, |
| 1407 |
} => { |
| 1408 |
let mut has_runtime_array = false; |
| 1409 |
let mut member_ids = Vec::with_capacity(members.len()); |
| 1410 |
for (index, member) in members.iter().enumerate() { |
| 1411 |
let member_ty = &module.types[member.ty]; |
| 1412 |
match member_ty.inner { |
| 1413 |
crate::TypeInner::Array { |
| 1414 |
base: _, |
| 1415 |
size: crate::ArraySize::Dynamic, |
| 1416 |
stride: _, |
| 1417 |
} => { |
| 1418 |
has_runtime_array = true; |
| 1419 |
} |
| 1420 |
_ => (), |
| 1421 |
} |
| 1422 |
self.decorate_struct_member(id, index, member, &module.types)?; |
| 1423 |
let member_id = self.get_handle_type_id(member.ty); |
| 1424 |
member_ids.push(member_id); |
| 1425 |
} |
| 1426 |
if has_runtime_array { |
| 1427 |
self.decorate(id, Decoration::Block, &[]); |
| 1428 |
} |
| 1429 |
Instruction::type_struct(id, member_ids.as_slice()) |
| 1430 |
} |
| 1431 |
|
| 1432 |
// These all have TypeLocal representations, so they should have been |
| 1433 |
// handled by `write_type_declaration_local` above. |
| 1434 |
crate::TypeInner::Scalar(_) |
| 1435 |
| crate::TypeInner::Atomic(_) |
| 1436 |
| crate::TypeInner::Vector { .. } |
| 1437 |
| crate::TypeInner::Matrix { .. } |
| 1438 |
| crate::TypeInner::Pointer { .. } |
| 1439 |
| crate::TypeInner::ValuePointer { .. } |
| 1440 |
| crate::TypeInner::Image { .. } |
| 1441 |
| crate::TypeInner::Sampler { .. } |
| 1442 |
| crate::TypeInner::AccelerationStructure { .. } |
| 1443 |
| crate::TypeInner::RayQuery { .. } => unreachable!(), |
| 1444 |
}; |
| 1445 |
|
| 1446 |
instruction.to_words(&mut self.logical_layout.declarations); |
| 1447 |
id |
| 1448 |
}; |
| 1449 |
|
| 1450 |
// Add this handle as a new alias for that type. |
| 1451 |
self.lookup_type.insert(LookupType::Handle(handle), id); |
| 1452 |
|
| 1453 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 1454 |
if let Some(ref name) = ty.name { |
| 1455 |
self.debugs.push(Instruction::name(id, name)); |
| 1456 |
} |
| 1457 |
} |
| 1458 |
|
| 1459 |
Ok(id) |
| 1460 |
} |
| 1461 |
|
| 1462 |
fn request_image_format_capabilities( |
| 1463 |
&mut self, |
| 1464 |
format: spirv::ImageFormat, |
| 1465 |
) -> Result<(), Error> { |
| 1466 |
use spirv::ImageFormat as If; |
| 1467 |
match format { |
| 1468 |
If::Rg32f |
| 1469 |
| If::Rg16f |
| 1470 |
| If::R11fG11fB10f |
| 1471 |
| If::R16f |
| 1472 |
| If::Rgba16 |
| 1473 |
| If::Rgb10A2 |
| 1474 |
| If::Rg16 |
| 1475 |
| If::Rg8 |
| 1476 |
| If::R16 |
| 1477 |
| If::R8 |
| 1478 |
| If::Rgba16Snorm |
| 1479 |
| If::Rg16Snorm |
| 1480 |
| If::Rg8Snorm |
| 1481 |
| If::R16Snorm |
| 1482 |
| If::R8Snorm |
| 1483 |
| If::Rg32i |
| 1484 |
| If::Rg16i |
| 1485 |
| If::Rg8i |
| 1486 |
| If::R16i |
| 1487 |
| If::R8i |
| 1488 |
| If::Rgb10a2ui |
| 1489 |
| If::Rg32ui |
| 1490 |
| If::Rg16ui |
| 1491 |
| If::Rg8ui |
| 1492 |
| If::R16ui |
| 1493 |
| If::R8ui => self.require_any( |
| 1494 |
"storage image format", |
| 1495 |
&[spirv::Capability::StorageImageExtendedFormats], |
| 1496 |
), |
| 1497 |
If::R64ui | If::R64i => { |
| 1498 |
self.use_extension("SPV_EXT_shader_image_int64"); |
| 1499 |
self.require_any( |
| 1500 |
"64-bit integer storage image format", |
| 1501 |
&[spirv::Capability::Int64ImageEXT], |
| 1502 |
) |
| 1503 |
} |
| 1504 |
If::Unknown |
| 1505 |
| If::Rgba32f |
| 1506 |
| If::Rgba16f |
| 1507 |
| If::R32f |
| 1508 |
| If::Rgba8 |
| 1509 |
| If::Rgba8Snorm |
| 1510 |
| If::Rgba32i |
| 1511 |
| If::Rgba16i |
| 1512 |
| If::Rgba8i |
| 1513 |
| If::R32i |
| 1514 |
| If::Rgba32ui |
| 1515 |
| If::Rgba16ui |
| 1516 |
| If::Rgba8ui |
| 1517 |
| If::R32ui => Ok(()), |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
pub(super) fn get_index_constant(&mut self, index: Word) -> Word { |
| 1522 |
self.get_constant_scalar(crate::Literal::U32(index)) |
| 1523 |
} |
| 1524 |
|
| 1525 |
pub(super) fn get_constant_scalar_with( |
| 1526 |
&mut self, |
| 1527 |
value: u8, |
| 1528 |
scalar: crate::Scalar, |
| 1529 |
) -> Result<Word, Error> { |
| 1530 |
Ok( |
| 1531 |
self.get_constant_scalar(crate::Literal::new(value, scalar).ok_or( |
| 1532 |
Error::Validation("Unexpected kind and/or width for Literal"), |
| 1533 |
)?), |
| 1534 |
) |
| 1535 |
} |
| 1536 |
|
| 1537 |
pub(super) fn get_constant_scalar(&mut self, value: crate::Literal) -> Word { |
| 1538 |
let scalar = CachedConstant::Literal(value.into()); |
| 1539 |
if let Some(&id) = self.cached_constants.get(&scalar) { |
| 1540 |
return id; |
| 1541 |
} |
| 1542 |
let id = self.id_gen.next(); |
| 1543 |
self.write_constant_scalar(id, &value, None); |
| 1544 |
self.cached_constants.insert(scalar, id); |
| 1545 |
id |
| 1546 |
} |
| 1547 |
|
| 1548 |
fn write_constant_scalar( |
| 1549 |
&mut self, |
| 1550 |
id: Word, |
| 1551 |
value: &crate::Literal, |
| 1552 |
debug_name: Option<&String>, |
| 1553 |
) { |
| 1554 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 1555 |
if let Some(name) = debug_name { |
| 1556 |
self.debugs.push(Instruction::name(id, name)); |
| 1557 |
} |
| 1558 |
} |
| 1559 |
let type_id = self.get_numeric_type_id(NumericType::Scalar(value.scalar())); |
| 1560 |
let instruction = match *value { |
| 1561 |
crate::Literal::F64(value) => { |
| 1562 |
let bits = value.to_bits(); |
| 1563 |
Instruction::constant_64bit(type_id, id, bits as u32, (bits >> 32) as u32) |
| 1564 |
} |
| 1565 |
crate::Literal::F32(value) => Instruction::constant_32bit(type_id, id, value.to_bits()), |
| 1566 |
crate::Literal::F16(value) => { |
| 1567 |
let low = value.to_bits(); |
| 1568 |
Instruction::constant_16bit(type_id, id, low as u32) |
| 1569 |
} |
| 1570 |
crate::Literal::U32(value) => Instruction::constant_32bit(type_id, id, value), |
| 1571 |
crate::Literal::I32(value) => Instruction::constant_32bit(type_id, id, value as u32), |
| 1572 |
crate::Literal::U64(value) => { |
| 1573 |
Instruction::constant_64bit(type_id, id, value as u32, (value >> 32) as u32) |
| 1574 |
} |
| 1575 |
crate::Literal::I64(value) => { |
| 1576 |
Instruction::constant_64bit(type_id, id, value as u32, (value >> 32) as u32) |
| 1577 |
} |
| 1578 |
crate::Literal::Bool(true) => Instruction::constant_true(type_id, id), |
| 1579 |
crate::Literal::Bool(false) => Instruction::constant_false(type_id, id), |
| 1580 |
crate::Literal::AbstractInt(_) | crate::Literal::AbstractFloat(_) => { |
| 1581 |
unreachable!("Abstract types should not appear in IR presented to backends"); |
| 1582 |
} |
| 1583 |
}; |
| 1584 |
|
| 1585 |
instruction.to_words(&mut self.logical_layout.declarations); |
| 1586 |
} |
| 1587 |
|
| 1588 |
pub(super) fn get_constant_composite( |
| 1589 |
&mut self, |
| 1590 |
ty: LookupType, |
| 1591 |
constituent_ids: &[Word], |
| 1592 |
) -> Word { |
| 1593 |
let composite = CachedConstant::Composite { |
| 1594 |
ty, |
| 1595 |
constituent_ids: constituent_ids.to_vec(), |
| 1596 |
}; |
| 1597 |
if let Some(&id) = self.cached_constants.get(&composite) { |
| 1598 |
return id; |
| 1599 |
} |
| 1600 |
let id = self.id_gen.next(); |
| 1601 |
self.write_constant_composite(id, ty, constituent_ids, None); |
| 1602 |
self.cached_constants.insert(composite, id); |
| 1603 |
id |
| 1604 |
} |
| 1605 |
|
| 1606 |
fn write_constant_composite( |
| 1607 |
&mut self, |
| 1608 |
id: Word, |
| 1609 |
ty: LookupType, |
| 1610 |
constituent_ids: &[Word], |
| 1611 |
debug_name: Option<&String>, |
| 1612 |
) { |
| 1613 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 1614 |
if let Some(name) = debug_name { |
| 1615 |
self.debugs.push(Instruction::name(id, name)); |
| 1616 |
} |
| 1617 |
} |
| 1618 |
let type_id = self.get_type_id(ty); |
| 1619 |
Instruction::constant_composite(type_id, id, constituent_ids) |
| 1620 |
.to_words(&mut self.logical_layout.declarations); |
| 1621 |
} |
| 1622 |
|
| 1623 |
pub(super) fn get_constant_null(&mut self, type_id: Word) -> Word { |
| 1624 |
let null = CachedConstant::ZeroValue(type_id); |
| 1625 |
if let Some(&id) = self.cached_constants.get(&null) { |
| 1626 |
return id; |
| 1627 |
} |
| 1628 |
let id = self.write_constant_null(type_id); |
| 1629 |
self.cached_constants.insert(null, id); |
| 1630 |
id |
| 1631 |
} |
| 1632 |
|
| 1633 |
pub(super) fn write_constant_null(&mut self, type_id: Word) -> Word { |
| 1634 |
let null_id = self.id_gen.next(); |
| 1635 |
Instruction::constant_null(type_id, null_id) |
| 1636 |
.to_words(&mut self.logical_layout.declarations); |
| 1637 |
null_id |
| 1638 |
} |
| 1639 |
|
| 1640 |
fn write_constant_expr( |
| 1641 |
&mut self, |
| 1642 |
handle: Handle<crate::Expression>, |
| 1643 |
ir_module: &crate::Module, |
| 1644 |
mod_info: &ModuleInfo, |
| 1645 |
) -> Result<Word, Error> { |
| 1646 |
let id = match ir_module.global_expressions[handle] { |
| 1647 |
crate::Expression::Literal(literal) => self.get_constant_scalar(literal), |
| 1648 |
crate::Expression::Constant(constant) => { |
| 1649 |
let constant = &ir_module.constants[constant]; |
| 1650 |
self.constant_ids[constant.init] |
| 1651 |
} |
| 1652 |
crate::Expression::ZeroValue(ty) => { |
| 1653 |
let type_id = self.get_handle_type_id(ty); |
| 1654 |
self.get_constant_null(type_id) |
| 1655 |
} |
| 1656 |
crate::Expression::Compose { ty, ref components } => { |
| 1657 |
let component_ids: Vec<_> = crate::proc::flatten_compose( |
| 1658 |
ty, |
| 1659 |
components, |
| 1660 |
&ir_module.global_expressions, |
| 1661 |
&ir_module.types, |
| 1662 |
) |
| 1663 |
.map(|component| self.constant_ids[component]) |
| 1664 |
.collect(); |
| 1665 |
self.get_constant_composite(LookupType::Handle(ty), component_ids.as_slice()) |
| 1666 |
} |
| 1667 |
crate::Expression::Splat { size, value } => { |
| 1668 |
let value_id = self.constant_ids[value]; |
| 1669 |
let component_ids = &[value_id; 4][..size as usize]; |
| 1670 |
|
| 1671 |
let ty = self.get_expression_lookup_type(&mod_info[handle]); |
| 1672 |
|
| 1673 |
self.get_constant_composite(ty, component_ids) |
| 1674 |
} |
| 1675 |
_ => { |
| 1676 |
return Err(Error::Override); |
| 1677 |
} |
| 1678 |
}; |
| 1679 |
|
| 1680 |
self.constant_ids[handle] = id; |
| 1681 |
|
| 1682 |
Ok(id) |
| 1683 |
} |
| 1684 |
|
| 1685 |
pub(super) fn write_barrier(&mut self, flags: crate::Barrier, block: &mut Block) { |
| 1686 |
let memory_scope = if flags.contains(crate::Barrier::STORAGE) { |
| 1687 |
spirv::Scope::Device |
| 1688 |
} else { |
| 1689 |
spirv::Scope::Workgroup |
| 1690 |
}; |
| 1691 |
let mut semantics = spirv::MemorySemantics::ACQUIRE_RELEASE; |
| 1692 |
semantics.set( |
| 1693 |
spirv::MemorySemantics::UNIFORM_MEMORY, |
| 1694 |
flags.contains(crate::Barrier::STORAGE), |
| 1695 |
); |
| 1696 |
semantics.set( |
| 1697 |
spirv::MemorySemantics::WORKGROUP_MEMORY, |
| 1698 |
flags.contains(crate::Barrier::WORK_GROUP), |
| 1699 |
); |
| 1700 |
semantics.set( |
| 1701 |
spirv::MemorySemantics::IMAGE_MEMORY, |
| 1702 |
flags.contains(crate::Barrier::TEXTURE), |
| 1703 |
); |
| 1704 |
let exec_scope_id = if flags.contains(crate::Barrier::SUB_GROUP) { |
| 1705 |
self.get_index_constant(spirv::Scope::Subgroup as u32) |
| 1706 |
} else { |
| 1707 |
self.get_index_constant(spirv::Scope::Workgroup as u32) |
| 1708 |
}; |
| 1709 |
let mem_scope_id = self.get_index_constant(memory_scope as u32); |
| 1710 |
let semantics_id = self.get_index_constant(semantics.bits()); |
| 1711 |
block.body.push(Instruction::control_barrier( |
| 1712 |
exec_scope_id, |
| 1713 |
mem_scope_id, |
| 1714 |
semantics_id, |
| 1715 |
)); |
| 1716 |
} |
| 1717 |
|
| 1718 |
fn generate_workgroup_vars_init_block( |
| 1719 |
&mut self, |
| 1720 |
entry_id: Word, |
| 1721 |
ir_module: &crate::Module, |
| 1722 |
info: &FunctionInfo, |
| 1723 |
local_invocation_id: Option<Word>, |
| 1724 |
interface: &mut FunctionInterface, |
| 1725 |
function: &mut Function, |
| 1726 |
) -> Option<Word> { |
| 1727 |
let body = ir_module |
| 1728 |
.global_variables |
| 1729 |
.iter() |
| 1730 |
.filter(|&(handle, var)| { |
| 1731 |
!info[handle].is_empty() && var.space == crate::AddressSpace::WorkGroup |
| 1732 |
}) |
| 1733 |
.map(|(handle, var)| { |
| 1734 |
// It's safe to use `var_id` here, not `access_id`, because only |
| 1735 |
// variables in the `Uniform` and `StorageBuffer` address spaces |
| 1736 |
// get wrapped, and we're initializing `WorkGroup` variables. |
| 1737 |
let var_id = self.global_variables[handle].var_id; |
| 1738 |
let var_type_id = self.get_handle_type_id(var.ty); |
| 1739 |
let init_word = self.get_constant_null(var_type_id); |
| 1740 |
Instruction::store(var_id, init_word, None) |
| 1741 |
}) |
| 1742 |
.collect::<Vec<_>>(); |
| 1743 |
|
| 1744 |
if body.is_empty() { |
| 1745 |
return None; |
| 1746 |
} |
| 1747 |
|
| 1748 |
let uint3_type_id = self.get_vec3u_type_id(); |
| 1749 |
|
| 1750 |
let mut pre_if_block = Block::new(entry_id); |
| 1751 |
|
| 1752 |
let local_invocation_id = if let Some(local_invocation_id) = local_invocation_id { |
| 1753 |
local_invocation_id |
| 1754 |
} else { |
| 1755 |
let varying_id = self.id_gen.next(); |
| 1756 |
let class = spirv::StorageClass::Input; |
| 1757 |
let pointer_type_id = self.get_vec3u_pointer_type_id(class); |
| 1758 |
|
| 1759 |
Instruction::variable(pointer_type_id, varying_id, class, None) |
| 1760 |
.to_words(&mut self.logical_layout.declarations); |
| 1761 |
|
| 1762 |
self.decorate( |
| 1763 |
varying_id, |
| 1764 |
spirv::Decoration::BuiltIn, |
| 1765 |
&[spirv::BuiltIn::LocalInvocationId as u32], |
| 1766 |
); |
| 1767 |
|
| 1768 |
interface.varying_ids.push(varying_id); |
| 1769 |
let id = self.id_gen.next(); |
| 1770 |
pre_if_block |
| 1771 |
.body |
| 1772 |
.push(Instruction::load(uint3_type_id, id, varying_id, None)); |
| 1773 |
|
| 1774 |
id |
| 1775 |
}; |
| 1776 |
|
| 1777 |
let zero_id = self.get_constant_null(uint3_type_id); |
| 1778 |
let bool3_type_id = self.get_vec3_bool_type_id(); |
| 1779 |
|
| 1780 |
let eq_id = self.id_gen.next(); |
| 1781 |
pre_if_block.body.push(Instruction::binary( |
| 1782 |
spirv::Op::IEqual, |
| 1783 |
bool3_type_id, |
| 1784 |
eq_id, |
| 1785 |
local_invocation_id, |
| 1786 |
zero_id, |
| 1787 |
)); |
| 1788 |
|
| 1789 |
let condition_id = self.id_gen.next(); |
| 1790 |
let bool_type_id = self.get_bool_type_id(); |
| 1791 |
pre_if_block.body.push(Instruction::relational( |
| 1792 |
spirv::Op::All, |
| 1793 |
bool_type_id, |
| 1794 |
condition_id, |
| 1795 |
eq_id, |
| 1796 |
)); |
| 1797 |
|
| 1798 |
let merge_id = self.id_gen.next(); |
| 1799 |
pre_if_block.body.push(Instruction::selection_merge( |
| 1800 |
merge_id, |
| 1801 |
spirv::SelectionControl::NONE, |
| 1802 |
)); |
| 1803 |
|
| 1804 |
let accept_id = self.id_gen.next(); |
| 1805 |
function.consume( |
| 1806 |
pre_if_block, |
| 1807 |
Instruction::branch_conditional(condition_id, accept_id, merge_id), |
| 1808 |
); |
| 1809 |
|
| 1810 |
let accept_block = Block { |
| 1811 |
label_id: accept_id, |
| 1812 |
body, |
| 1813 |
}; |
| 1814 |
function.consume(accept_block, Instruction::branch(merge_id)); |
| 1815 |
|
| 1816 |
let mut post_if_block = Block::new(merge_id); |
| 1817 |
|
| 1818 |
self.write_barrier(crate::Barrier::WORK_GROUP, &mut post_if_block); |
| 1819 |
|
| 1820 |
let next_id = self.id_gen.next(); |
| 1821 |
function.consume(post_if_block, Instruction::branch(next_id)); |
| 1822 |
Some(next_id) |
| 1823 |
} |
| 1824 |
|
| 1825 |
/// Generate an `OpVariable` for one value in an [`EntryPoint`]'s IO interface. |
| 1826 |
/// |
| 1827 |
/// The [`Binding`]s of the arguments and result of an [`EntryPoint`]'s |
| 1828 |
/// [`Function`] describe a SPIR-V shader interface. In SPIR-V, the |
| 1829 |
/// interface is represented by global variables in the `Input` and `Output` |
| 1830 |
/// storage classes, with decorations indicating which builtin or location |
| 1831 |
/// each variable corresponds to. |
| 1832 |
/// |
| 1833 |
/// This function emits a single global `OpVariable` for a single value from |
| 1834 |
/// the interface, and adds appropriate decorations to indicate which |
| 1835 |
/// builtin or location it represents, how it should be interpolated, and so |
| 1836 |
/// on. The `class` argument gives the variable's SPIR-V storage class, |
| 1837 |
/// which should be either [`Input`] or [`Output`]. |
| 1838 |
/// |
| 1839 |
/// [`Binding`]: crate::Binding |
| 1840 |
/// [`Function`]: crate::Function |
| 1841 |
/// [`EntryPoint`]: crate::EntryPoint |
| 1842 |
/// [`Input`]: spirv::StorageClass::Input |
| 1843 |
/// [`Output`]: spirv::StorageClass::Output |
| 1844 |
fn write_varying( |
| 1845 |
&mut self, |
| 1846 |
ir_module: &crate::Module, |
| 1847 |
stage: crate::ShaderStage, |
| 1848 |
class: spirv::StorageClass, |
| 1849 |
debug_name: Option<&str>, |
| 1850 |
ty: Handle<crate::Type>, |
| 1851 |
binding: &crate::Binding, |
| 1852 |
) -> Result<Word, Error> { |
| 1853 |
let id = self.id_gen.next(); |
| 1854 |
let pointer_type_id = self.get_handle_pointer_type_id(ty, class); |
| 1855 |
Instruction::variable(pointer_type_id, id, class, None) |
| 1856 |
.to_words(&mut self.logical_layout.declarations); |
| 1857 |
|
| 1858 |
if self |
| 1859 |
.flags |
| 1860 |
.contains(WriterFlags::DEBUG | WriterFlags::LABEL_VARYINGS) |
| 1861 |
{ |
| 1862 |
if let Some(name) = debug_name { |
| 1863 |
self.debugs.push(Instruction::name(id, name)); |
| 1864 |
} |
| 1865 |
} |
| 1866 |
|
| 1867 |
use spirv::{BuiltIn, Decoration}; |
| 1868 |
|
| 1869 |
match *binding { |
| 1870 |
crate::Binding::Location { |
| 1871 |
location, |
| 1872 |
interpolation, |
| 1873 |
sampling, |
| 1874 |
blend_src, |
| 1875 |
} => { |
| 1876 |
self.decorate(id, Decoration::Location, &[location]); |
| 1877 |
|
| 1878 |
let no_decorations = |
| 1879 |
// VUID-StandaloneSpirv-Flat-06202 |
| 1880 |
// > The Flat, NoPerspective, Sample, and Centroid decorations |
| 1881 |
// > must not be used on variables with the Input storage class in a vertex shader |
| 1882 |
(class == spirv::StorageClass::Input && stage == crate::ShaderStage::Vertex) || |
| 1883 |
// VUID-StandaloneSpirv-Flat-06201 |
| 1884 |
// > The Flat, NoPerspective, Sample, and Centroid decorations |
| 1885 |
// > must not be used on variables with the Output storage class in a fragment shader |
| 1886 |
(class == spirv::StorageClass::Output && stage == crate::ShaderStage::Fragment); |
| 1887 |
|
| 1888 |
if !no_decorations { |
| 1889 |
match interpolation { |
| 1890 |
// Perspective-correct interpolation is the default in SPIR-V. |
| 1891 |
None | Some(crate::Interpolation::Perspective) => (), |
| 1892 |
Some(crate::Interpolation::Flat) => { |
| 1893 |
self.decorate(id, Decoration::Flat, &[]); |
| 1894 |
} |
| 1895 |
Some(crate::Interpolation::Linear) => { |
| 1896 |
self.decorate(id, Decoration::NoPerspective, &[]); |
| 1897 |
} |
| 1898 |
} |
| 1899 |
match sampling { |
| 1900 |
// Center sampling is the default in SPIR-V. |
| 1901 |
None |
| 1902 |
| Some( |
| 1903 |
crate::Sampling::Center |
| 1904 |
| crate::Sampling::First |
| 1905 |
| crate::Sampling::Either, |
| 1906 |
) => (), |
| 1907 |
Some(crate::Sampling::Centroid) => { |
| 1908 |
self.decorate(id, Decoration::Centroid, &[]); |
| 1909 |
} |
| 1910 |
Some(crate::Sampling::Sample) => { |
| 1911 |
self.require_any( |
| 1912 |
"per-sample interpolation", |
| 1913 |
&[spirv::Capability::SampleRateShading], |
| 1914 |
)?; |
| 1915 |
self.decorate(id, Decoration::Sample, &[]); |
| 1916 |
} |
| 1917 |
} |
| 1918 |
} |
| 1919 |
if let Some(blend_src) = blend_src { |
| 1920 |
self.decorate(id, Decoration::Index, &[blend_src]); |
| 1921 |
} |
| 1922 |
} |
| 1923 |
crate::Binding::BuiltIn(built_in) => { |
| 1924 |
use crate::BuiltIn as Bi; |
| 1925 |
let built_in = match built_in { |
| 1926 |
Bi::Position { invariant } => { |
| 1927 |
if invariant { |
| 1928 |
self.decorate(id, Decoration::Invariant, &[]); |
| 1929 |
} |
| 1930 |
|
| 1931 |
if class == spirv::StorageClass::Output { |
| 1932 |
BuiltIn::Position |
| 1933 |
} else { |
| 1934 |
BuiltIn::FragCoord |
| 1935 |
} |
| 1936 |
} |
| 1937 |
Bi::ViewIndex => { |
| 1938 |
self.require_any("`view_index` built-in", &[spirv::Capability::MultiView])?; |
| 1939 |
BuiltIn::ViewIndex |
| 1940 |
} |
| 1941 |
// vertex |
| 1942 |
Bi::BaseInstance => BuiltIn::BaseInstance, |
| 1943 |
Bi::BaseVertex => BuiltIn::BaseVertex, |
| 1944 |
Bi::ClipDistance => { |
| 1945 |
self.require_any( |
| 1946 |
"`clip_distance` built-in", |
| 1947 |
&[spirv::Capability::ClipDistance], |
| 1948 |
)?; |
| 1949 |
BuiltIn::ClipDistance |
| 1950 |
} |
| 1951 |
Bi::CullDistance => { |
| 1952 |
self.require_any( |
| 1953 |
"`cull_distance` built-in", |
| 1954 |
&[spirv::Capability::CullDistance], |
| 1955 |
)?; |
| 1956 |
BuiltIn::CullDistance |
| 1957 |
} |
| 1958 |
Bi::InstanceIndex => BuiltIn::InstanceIndex, |
| 1959 |
Bi::PointSize => BuiltIn::PointSize, |
| 1960 |
Bi::VertexIndex => BuiltIn::VertexIndex, |
| 1961 |
Bi::DrawID => BuiltIn::DrawIndex, |
| 1962 |
// fragment |
| 1963 |
Bi::FragDepth => BuiltIn::FragDepth, |
| 1964 |
Bi::PointCoord => BuiltIn::PointCoord, |
| 1965 |
Bi::FrontFacing => BuiltIn::FrontFacing, |
| 1966 |
Bi::PrimitiveIndex => { |
| 1967 |
self.require_any( |
| 1968 |
"`primitive_index` built-in", |
| 1969 |
&[spirv::Capability::Geometry], |
| 1970 |
)?; |
| 1971 |
BuiltIn::PrimitiveId |
| 1972 |
} |
| 1973 |
Bi::SampleIndex => { |
| 1974 |
self.require_any( |
| 1975 |
"`sample_index` built-in", |
| 1976 |
&[spirv::Capability::SampleRateShading], |
| 1977 |
)?; |
| 1978 |
|
| 1979 |
BuiltIn::SampleId |
| 1980 |
} |
| 1981 |
Bi::SampleMask => BuiltIn::SampleMask, |
| 1982 |
// compute |
| 1983 |
Bi::GlobalInvocationId => BuiltIn::GlobalInvocationId, |
| 1984 |
Bi::LocalInvocationId => BuiltIn::LocalInvocationId, |
| 1985 |
Bi::LocalInvocationIndex => BuiltIn::LocalInvocationIndex, |
| 1986 |
Bi::WorkGroupId => BuiltIn::WorkgroupId, |
| 1987 |
Bi::WorkGroupSize => BuiltIn::WorkgroupSize, |
| 1988 |
Bi::NumWorkGroups => BuiltIn::NumWorkgroups, |
| 1989 |
// Subgroup |
| 1990 |
Bi::NumSubgroups => { |
| 1991 |
self.require_any( |
| 1992 |
"`num_subgroups` built-in", |
| 1993 |
&[spirv::Capability::GroupNonUniform], |
| 1994 |
)?; |
| 1995 |
BuiltIn::NumSubgroups |
| 1996 |
} |
| 1997 |
Bi::SubgroupId => { |
| 1998 |
self.require_any( |
| 1999 |
"`subgroup_id` built-in", |
| 2000 |
&[spirv::Capability::GroupNonUniform], |
| 2001 |
)?; |
| 2002 |
BuiltIn::SubgroupId |
| 2003 |
} |
| 2004 |
Bi::SubgroupSize => { |
| 2005 |
self.require_any( |
| 2006 |
"`subgroup_size` built-in", |
| 2007 |
&[ |
| 2008 |
spirv::Capability::GroupNonUniform, |
| 2009 |
spirv::Capability::SubgroupBallotKHR, |
| 2010 |
], |
| 2011 |
)?; |
| 2012 |
BuiltIn::SubgroupSize |
| 2013 |
} |
| 2014 |
Bi::SubgroupInvocationId => { |
| 2015 |
self.require_any( |
| 2016 |
"`subgroup_invocation_id` built-in", |
| 2017 |
&[ |
| 2018 |
spirv::Capability::GroupNonUniform, |
| 2019 |
spirv::Capability::SubgroupBallotKHR, |
| 2020 |
], |
| 2021 |
)?; |
| 2022 |
BuiltIn::SubgroupLocalInvocationId |
| 2023 |
} |
| 2024 |
}; |
| 2025 |
|
| 2026 |
self.decorate(id, Decoration::BuiltIn, &[built_in as u32]); |
| 2027 |
|
| 2028 |
use crate::ScalarKind as Sk; |
| 2029 |
|
| 2030 |
// Per the Vulkan spec, `VUID-StandaloneSpirv-Flat-04744`: |
| 2031 |
// |
| 2032 |
// > Any variable with integer or double-precision floating- |
| 2033 |
// > point type and with Input storage class in a fragment |
| 2034 |
// > shader, must be decorated Flat |
| 2035 |
if class == spirv::StorageClass::Input && stage == crate::ShaderStage::Fragment { |
| 2036 |
let is_flat = match ir_module.types[ty].inner { |
| 2037 |
crate::TypeInner::Scalar(scalar) |
| 2038 |
| crate::TypeInner::Vector { scalar, .. } => match scalar.kind { |
| 2039 |
Sk::Uint | Sk::Sint | Sk::Bool => true, |
| 2040 |
Sk::Float => false, |
| 2041 |
Sk::AbstractInt | Sk::AbstractFloat => { |
| 2042 |
return Err(Error::Validation( |
| 2043 |
"Abstract types should not appear in IR presented to backends", |
| 2044 |
)) |
| 2045 |
} |
| 2046 |
}, |
| 2047 |
_ => false, |
| 2048 |
}; |
| 2049 |
|
| 2050 |
if is_flat { |
| 2051 |
self.decorate(id, Decoration::Flat, &[]); |
| 2052 |
} |
| 2053 |
} |
| 2054 |
} |
| 2055 |
} |
| 2056 |
|
| 2057 |
Ok(id) |
| 2058 |
} |
| 2059 |
|
| 2060 |
fn write_global_variable( |
| 2061 |
&mut self, |
| 2062 |
ir_module: &crate::Module, |
| 2063 |
global_variable: &crate::GlobalVariable, |
| 2064 |
) -> Result<Word, Error> { |
| 2065 |
use spirv::Decoration; |
| 2066 |
|
| 2067 |
let id = self.id_gen.next(); |
| 2068 |
let class = map_storage_class(global_variable.space); |
| 2069 |
|
| 2070 |
//self.check(class.required_capabilities())?; |
| 2071 |
|
| 2072 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 2073 |
if let Some(ref name) = global_variable.name { |
| 2074 |
self.debugs.push(Instruction::name(id, name)); |
| 2075 |
} |
| 2076 |
} |
| 2077 |
|
| 2078 |
let storage_access = match global_variable.space { |
| 2079 |
crate::AddressSpace::Storage { access } => Some(access), |
| 2080 |
_ => match ir_module.types[global_variable.ty].inner { |
| 2081 |
crate::TypeInner::Image { |
| 2082 |
class: crate::ImageClass::Storage { access, .. }, |
| 2083 |
.. |
| 2084 |
} => Some(access), |
| 2085 |
_ => None, |
| 2086 |
}, |
| 2087 |
}; |
| 2088 |
if let Some(storage_access) = storage_access { |
| 2089 |
if !storage_access.contains(crate::StorageAccess::LOAD) { |
| 2090 |
self.decorate(id, Decoration::NonReadable, &[]); |
| 2091 |
} |
| 2092 |
if !storage_access.contains(crate::StorageAccess::STORE) { |
| 2093 |
self.decorate(id, Decoration::NonWritable, &[]); |
| 2094 |
} |
| 2095 |
} |
| 2096 |
|
| 2097 |
// Note: we should be able to substitute `binding_array<Foo, 0>`, |
| 2098 |
// but there is still code that tries to register the pre-substituted type, |
| 2099 |
// and it is failing on 0. |
| 2100 |
let mut substitute_inner_type_lookup = None; |
| 2101 |
if let Some(ref res_binding) = global_variable.binding { |
| 2102 |
self.decorate(id, Decoration::DescriptorSet, &[res_binding.group]); |
| 2103 |
self.decorate(id, Decoration::Binding, &[res_binding.binding]); |
| 2104 |
|
| 2105 |
if let Some(&BindingInfo { |
| 2106 |
binding_array_size: Some(remapped_binding_array_size), |
| 2107 |
}) = self.binding_map.get(res_binding) |
| 2108 |
{ |
| 2109 |
if let crate::TypeInner::BindingArray { base, .. } = |
| 2110 |
ir_module.types[global_variable.ty].inner |
| 2111 |
{ |
| 2112 |
let binding_array_type_id = |
| 2113 |
self.get_type_id(LookupType::Local(LocalType::BindingArray { |
| 2114 |
base, |
| 2115 |
size: remapped_binding_array_size, |
| 2116 |
})); |
| 2117 |
substitute_inner_type_lookup = Some(LookupType::Local(LocalType::Pointer { |
| 2118 |
base: binding_array_type_id, |
| 2119 |
class, |
| 2120 |
})); |
| 2121 |
} |
| 2122 |
} |
| 2123 |
}; |
| 2124 |
|
| 2125 |
let init_word = global_variable |
| 2126 |
.init |
| 2127 |
.map(|constant| self.constant_ids[constant]); |
| 2128 |
let inner_type_id = self.get_type_id( |
| 2129 |
substitute_inner_type_lookup.unwrap_or(LookupType::Handle(global_variable.ty)), |
| 2130 |
); |
| 2131 |
|
| 2132 |
// generate the wrapping structure if needed |
| 2133 |
let pointer_type_id = if global_needs_wrapper(ir_module, global_variable) { |
| 2134 |
let wrapper_type_id = self.id_gen.next(); |
| 2135 |
|
| 2136 |
self.decorate(wrapper_type_id, Decoration::Block, &[]); |
| 2137 |
let member = crate::StructMember { |
| 2138 |
name: None, |
| 2139 |
ty: global_variable.ty, |
| 2140 |
binding: None, |
| 2141 |
offset: 0, |
| 2142 |
}; |
| 2143 |
self.decorate_struct_member(wrapper_type_id, 0, &member, &ir_module.types)?; |
| 2144 |
|
| 2145 |
Instruction::type_struct(wrapper_type_id, &[inner_type_id]) |
| 2146 |
.to_words(&mut self.logical_layout.declarations); |
| 2147 |
|
| 2148 |
let pointer_type_id = self.id_gen.next(); |
| 2149 |
Instruction::type_pointer(pointer_type_id, class, wrapper_type_id) |
| 2150 |
.to_words(&mut self.logical_layout.declarations); |
| 2151 |
|
| 2152 |
pointer_type_id |
| 2153 |
} else { |
| 2154 |
// This is a global variable in the Storage address space. The only |
| 2155 |
// way it could have `global_needs_wrapper() == false` is if it has |
| 2156 |
// a runtime-sized or binding array. |
| 2157 |
// Runtime-sized arrays were decorated when iterating through struct content. |
| 2158 |
// Now binding arrays require Block decorating. |
| 2159 |
if let crate::AddressSpace::Storage { .. } = global_variable.space { |
| 2160 |
match ir_module.types[global_variable.ty].inner { |
| 2161 |
crate::TypeInner::BindingArray { base, .. } => { |
| 2162 |
let ty = &ir_module.types[base]; |
| 2163 |
let mut should_decorate = true; |
| 2164 |
// Check if the type has a runtime array. |
| 2165 |
// A normal runtime array gets validated out, |
| 2166 |
// so only structs can be with runtime arrays |
| 2167 |
if let crate::TypeInner::Struct { ref members, .. } = ty.inner { |
| 2168 |
// only the last member in a struct can be dynamically sized |
| 2169 |
if let Some(last_member) = members.last() { |
| 2170 |
if let &crate::TypeInner::Array { |
| 2171 |
size: crate::ArraySize::Dynamic, |
| 2172 |
.. |
| 2173 |
} = &ir_module.types[last_member.ty].inner |
| 2174 |
{ |
| 2175 |
should_decorate = false; |
| 2176 |
} |
| 2177 |
} |
| 2178 |
} |
| 2179 |
if should_decorate { |
| 2180 |
let decorated_id = self.get_handle_type_id(base); |
| 2181 |
self.decorate(decorated_id, Decoration::Block, &[]); |
| 2182 |
} |
| 2183 |
} |
| 2184 |
_ => (), |
| 2185 |
}; |
| 2186 |
} |
| 2187 |
if substitute_inner_type_lookup.is_some() { |
| 2188 |
inner_type_id |
| 2189 |
} else { |
| 2190 |
self.get_handle_pointer_type_id(global_variable.ty, class) |
| 2191 |
} |
| 2192 |
}; |
| 2193 |
|
| 2194 |
let init_word = match (global_variable.space, self.zero_initialize_workgroup_memory) { |
| 2195 |
(crate::AddressSpace::Private, _) |
| 2196 |
| (crate::AddressSpace::WorkGroup, super::ZeroInitializeWorkgroupMemoryMode::Native) => { |
| 2197 |
init_word.or_else(|| Some(self.get_constant_null(inner_type_id))) |
| 2198 |
} |
| 2199 |
_ => init_word, |
| 2200 |
}; |
| 2201 |
|
| 2202 |
Instruction::variable(pointer_type_id, id, class, init_word) |
| 2203 |
.to_words(&mut self.logical_layout.declarations); |
| 2204 |
Ok(id) |
| 2205 |
} |
| 2206 |
|
| 2207 |
/// Write the necessary decorations for a struct member. |
| 2208 |
/// |
| 2209 |
/// Emit decorations for the `index`'th member of the struct type |
| 2210 |
/// designated by `struct_id`, described by `member`. |
| 2211 |
fn decorate_struct_member( |
| 2212 |
&mut self, |
| 2213 |
struct_id: Word, |
| 2214 |
index: usize, |
| 2215 |
member: &crate::StructMember, |
| 2216 |
arena: &UniqueArena<crate::Type>, |
| 2217 |
) -> Result<(), Error> { |
| 2218 |
use spirv::Decoration; |
| 2219 |
|
| 2220 |
self.annotations.push(Instruction::member_decorate( |
| 2221 |
struct_id, |
| 2222 |
index as u32, |
| 2223 |
Decoration::Offset, |
| 2224 |
&[member.offset], |
| 2225 |
)); |
| 2226 |
|
| 2227 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 2228 |
if let Some(ref name) = member.name { |
| 2229 |
self.debugs |
| 2230 |
.push(Instruction::member_name(struct_id, index as u32, name)); |
| 2231 |
} |
| 2232 |
} |
| 2233 |
|
| 2234 |
// Matrices and (potentially nested) arrays of matrices both require decorations, |
| 2235 |
// so "see through" any arrays to determine if they're needed. |
| 2236 |
let mut member_array_subty_inner = &arena[member.ty].inner; |
| 2237 |
while let crate::TypeInner::Array { base, .. } = *member_array_subty_inner { |
| 2238 |
member_array_subty_inner = &arena[base].inner; |
| 2239 |
} |
| 2240 |
|
| 2241 |
if let crate::TypeInner::Matrix { |
| 2242 |
columns: _, |
| 2243 |
rows, |
| 2244 |
scalar, |
| 2245 |
} = *member_array_subty_inner |
| 2246 |
{ |
| 2247 |
let byte_stride = Alignment::from(rows) * scalar.width as u32; |
| 2248 |
self.annotations.push(Instruction::member_decorate( |
| 2249 |
struct_id, |
| 2250 |
index as u32, |
| 2251 |
Decoration::ColMajor, |
| 2252 |
&[], |
| 2253 |
)); |
| 2254 |
self.annotations.push(Instruction::member_decorate( |
| 2255 |
struct_id, |
| 2256 |
index as u32, |
| 2257 |
Decoration::MatrixStride, |
| 2258 |
&[byte_stride], |
| 2259 |
)); |
| 2260 |
} |
| 2261 |
|
| 2262 |
Ok(()) |
| 2263 |
} |
| 2264 |
|
| 2265 |
pub(super) fn get_function_type(&mut self, lookup_function_type: LookupFunctionType) -> Word { |
| 2266 |
match self |
| 2267 |
.lookup_function_type |
| 2268 |
.entry(lookup_function_type.clone()) |
| 2269 |
{ |
| 2270 |
Entry::Occupied(e) => *e.get(), |
| 2271 |
Entry::Vacant(_) => { |
| 2272 |
let id = self.id_gen.next(); |
| 2273 |
let instruction = Instruction::type_function( |
| 2274 |
id, |
| 2275 |
lookup_function_type.return_type_id, |
| 2276 |
&lookup_function_type.parameter_type_ids, |
| 2277 |
); |
| 2278 |
instruction.to_words(&mut self.logical_layout.declarations); |
| 2279 |
self.lookup_function_type.insert(lookup_function_type, id); |
| 2280 |
id |
| 2281 |
} |
| 2282 |
} |
| 2283 |
} |
| 2284 |
|
| 2285 |
fn write_physical_layout(&mut self) { |
| 2286 |
self.physical_layout.bound = self.id_gen.0 + 1; |
| 2287 |
} |
| 2288 |
|
| 2289 |
fn write_logical_layout( |
| 2290 |
&mut self, |
| 2291 |
ir_module: &crate::Module, |
| 2292 |
mod_info: &ModuleInfo, |
| 2293 |
ep_index: Option<usize>, |
| 2294 |
debug_info: &Option<DebugInfo>, |
| 2295 |
) -> Result<(), Error> { |
| 2296 |
fn has_view_index_check( |
| 2297 |
ir_module: &crate::Module, |
| 2298 |
binding: Option<&crate::Binding>, |
| 2299 |
ty: Handle<crate::Type>, |
| 2300 |
) -> bool { |
| 2301 |
match ir_module.types[ty].inner { |
| 2302 |
crate::TypeInner::Struct { ref members, .. } => members.iter().any(|member| { |
| 2303 |
has_view_index_check(ir_module, member.binding.as_ref(), member.ty) |
| 2304 |
}), |
| 2305 |
_ => binding == Some(&crate::Binding::BuiltIn(crate::BuiltIn::ViewIndex)), |
| 2306 |
} |
| 2307 |
} |
| 2308 |
|
| 2309 |
let has_storage_buffers = |
| 2310 |
ir_module |
| 2311 |
.global_variables |
| 2312 |
.iter() |
| 2313 |
.any(|(_, var)| match var.space { |
| 2314 |
crate::AddressSpace::Storage { .. } => true, |
| 2315 |
_ => false, |
| 2316 |
}); |
| 2317 |
let has_view_index = ir_module |
| 2318 |
.entry_points |
| 2319 |
.iter() |
| 2320 |
.flat_map(|entry| entry.function.arguments.iter()) |
| 2321 |
.any(|arg| has_view_index_check(ir_module, arg.binding.as_ref(), arg.ty)); |
| 2322 |
let mut has_ray_query = ir_module.special_types.ray_desc.is_some() |
| 2323 |
| ir_module.special_types.ray_intersection.is_some(); |
| 2324 |
let has_vertex_return = ir_module.special_types.ray_vertex_return.is_some(); |
| 2325 |
|
| 2326 |
for (_, &crate::Type { ref inner, .. }) in ir_module.types.iter() { |
| 2327 |
// spirv does not know whether these have vertex return - that is done by us |
| 2328 |
if let &crate::TypeInner::AccelerationStructure { .. } |
| 2329 |
| &crate::TypeInner::RayQuery { .. } = inner |
| 2330 |
{ |
| 2331 |
has_ray_query = true |
| 2332 |
} |
| 2333 |
} |
| 2334 |
|
| 2335 |
if self.physical_layout.version < 0x10300 && has_storage_buffers { |
| 2336 |
// enable the storage buffer class on < SPV-1.3 |
| 2337 |
Instruction::extension("SPV_KHR_storage_buffer_storage_class") |
| 2338 |
.to_words(&mut self.logical_layout.extensions); |
| 2339 |
} |
| 2340 |
if has_view_index { |
| 2341 |
Instruction::extension("SPV_KHR_multiview") |
| 2342 |
.to_words(&mut self.logical_layout.extensions) |
| 2343 |
} |
| 2344 |
if has_ray_query { |
| 2345 |
Instruction::extension("SPV_KHR_ray_query") |
| 2346 |
.to_words(&mut self.logical_layout.extensions) |
| 2347 |
} |
| 2348 |
if has_vertex_return { |
| 2349 |
Instruction::extension("SPV_KHR_ray_tracing_position_fetch") |
| 2350 |
.to_words(&mut self.logical_layout.extensions); |
| 2351 |
} |
| 2352 |
Instruction::type_void(self.void_type).to_words(&mut self.logical_layout.declarations); |
| 2353 |
Instruction::ext_inst_import(self.gl450_ext_inst_id, "GLSL.std.450") |
| 2354 |
.to_words(&mut self.logical_layout.ext_inst_imports); |
| 2355 |
|
| 2356 |
let mut debug_info_inner = None; |
| 2357 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 2358 |
if let Some(debug_info) = debug_info.as_ref() { |
| 2359 |
let source_file_id = self.id_gen.next(); |
| 2360 |
self.debugs.push(Instruction::string( |
| 2361 |
&debug_info.file_name.display().to_string(), |
| 2362 |
source_file_id, |
| 2363 |
)); |
| 2364 |
|
| 2365 |
debug_info_inner = Some(DebugInfoInner { |
| 2366 |
source_code: debug_info.source_code, |
| 2367 |
source_file_id, |
| 2368 |
}); |
| 2369 |
self.debugs.append(&mut Instruction::source_auto_continued( |
| 2370 |
debug_info.language, |
| 2371 |
0, |
| 2372 |
&debug_info_inner, |
| 2373 |
)); |
| 2374 |
} |
| 2375 |
} |
| 2376 |
|
| 2377 |
// write all types |
| 2378 |
for (handle, _) in ir_module.types.iter() { |
| 2379 |
self.write_type_declaration_arena(ir_module, handle)?; |
| 2380 |
} |
| 2381 |
|
| 2382 |
// write all const-expressions as constants |
| 2383 |
self.constant_ids |
| 2384 |
.resize(ir_module.global_expressions.len(), 0); |
| 2385 |
for (handle, _) in ir_module.global_expressions.iter() { |
| 2386 |
self.write_constant_expr(handle, ir_module, mod_info)?; |
| 2387 |
} |
| 2388 |
debug_assert!(self.constant_ids.iter().all(|&id| id != 0)); |
| 2389 |
|
| 2390 |
// write the name of constants on their respective const-expression initializer |
| 2391 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 2392 |
for (_, constant) in ir_module.constants.iter() { |
| 2393 |
if let Some(ref name) = constant.name { |
| 2394 |
let id = self.constant_ids[constant.init]; |
| 2395 |
self.debugs.push(Instruction::name(id, name)); |
| 2396 |
} |
| 2397 |
} |
| 2398 |
} |
| 2399 |
|
| 2400 |
// write all global variables |
| 2401 |
for (handle, var) in ir_module.global_variables.iter() { |
| 2402 |
// If a single entry point was specified, only write `OpVariable` instructions |
| 2403 |
// for the globals it actually uses. Emit dummies for the others, |
| 2404 |
// to preserve the indices in `global_variables`. |
| 2405 |
let gvar = match ep_index { |
| 2406 |
Some(index) if mod_info.get_entry_point(index)[handle].is_empty() => { |
| 2407 |
GlobalVariable::dummy() |
| 2408 |
} |
| 2409 |
_ => { |
| 2410 |
let id = self.write_global_variable(ir_module, var)?; |
| 2411 |
GlobalVariable::new(id) |
| 2412 |
} |
| 2413 |
}; |
| 2414 |
self.global_variables.insert(handle, gvar); |
| 2415 |
} |
| 2416 |
|
| 2417 |
// write all functions |
| 2418 |
for (handle, ir_function) in ir_module.functions.iter() { |
| 2419 |
let info = &mod_info[handle]; |
| 2420 |
if let Some(index) = ep_index { |
| 2421 |
let ep_info = mod_info.get_entry_point(index); |
| 2422 |
// If this function uses globals that we omitted from the SPIR-V |
| 2423 |
// because the entry point and its callees didn't use them, |
| 2424 |
// then we must skip it. |
| 2425 |
if !ep_info.dominates_global_use(info) { |
| 2426 |
log::info!("Skip function {:?}", ir_function.name); |
| 2427 |
continue; |
| 2428 |
} |
| 2429 |
|
| 2430 |
// Skip functions that that are not compatible with this entry point's stage. |
| 2431 |
// |
| 2432 |
// When validation is enabled, it rejects modules whose entry points try to call |
| 2433 |
// incompatible functions, so if we got this far, then any functions incompatible |
| 2434 |
// with our selected entry point must not be used. |
| 2435 |
// |
| 2436 |
// When validation is disabled, `fun_info.available_stages` is always just |
| 2437 |
// `ShaderStages::all()`, so this will write all functions in the module, and |
| 2438 |
// the downstream GLSL compiler will catch any problems. |
| 2439 |
if !info.available_stages.contains(ep_info.available_stages) { |
| 2440 |
continue; |
| 2441 |
} |
| 2442 |
} |
| 2443 |
let id = self.write_function(ir_function, info, ir_module, None, &debug_info_inner)?; |
| 2444 |
self.lookup_function.insert(handle, id); |
| 2445 |
} |
| 2446 |
|
| 2447 |
// write all or one entry points |
| 2448 |
for (index, ir_ep) in ir_module.entry_points.iter().enumerate() { |
| 2449 |
if ep_index.is_some() && ep_index != Some(index) { |
| 2450 |
continue; |
| 2451 |
} |
| 2452 |
let info = mod_info.get_entry_point(index); |
| 2453 |
let ep_instruction = |
| 2454 |
self.write_entry_point(ir_ep, info, ir_module, &debug_info_inner)?; |
| 2455 |
ep_instruction.to_words(&mut self.logical_layout.entry_points); |
| 2456 |
} |
| 2457 |
|
| 2458 |
for capability in self.capabilities_used.iter() { |
| 2459 |
Instruction::capability(*capability).to_words(&mut self.logical_layout.capabilities); |
| 2460 |
} |
| 2461 |
for extension in self.extensions_used.iter() { |
| 2462 |
Instruction::extension(extension).to_words(&mut self.logical_layout.extensions); |
| 2463 |
} |
| 2464 |
if ir_module.entry_points.is_empty() { |
| 2465 |
// SPIR-V doesn't like modules without entry points |
| 2466 |
Instruction::capability(spirv::Capability::Linkage) |
| 2467 |
.to_words(&mut self.logical_layout.capabilities); |
| 2468 |
} |
| 2469 |
|
| 2470 |
let addressing_model = spirv::AddressingModel::Logical; |
| 2471 |
let memory_model = spirv::MemoryModel::GLSL450; |
| 2472 |
//self.check(addressing_model.required_capabilities())?; |
| 2473 |
//self.check(memory_model.required_capabilities())?; |
| 2474 |
|
| 2475 |
Instruction::memory_model(addressing_model, memory_model) |
| 2476 |
.to_words(&mut self.logical_layout.memory_model); |
| 2477 |
|
| 2478 |
if self.flags.contains(WriterFlags::DEBUG) { |
| 2479 |
for debug in self.debugs.iter() { |
| 2480 |
debug.to_words(&mut self.logical_layout.debugs); |
| 2481 |
} |
| 2482 |
} |
| 2483 |
|
| 2484 |
for annotation in self.annotations.iter() { |
| 2485 |
annotation.to_words(&mut self.logical_layout.annotations); |
| 2486 |
} |
| 2487 |
|
| 2488 |
Ok(()) |
| 2489 |
} |
| 2490 |
|
| 2491 |
pub fn write( |
| 2492 |
&mut self, |
| 2493 |
ir_module: &crate::Module, |
| 2494 |
info: &ModuleInfo, |
| 2495 |
pipeline_options: Option<&PipelineOptions>, |
| 2496 |
debug_info: &Option<DebugInfo>, |
| 2497 |
words: &mut Vec<Word>, |
| 2498 |
) -> Result<(), Error> { |
| 2499 |
self.reset(); |
| 2500 |
|
| 2501 |
// Try to find the entry point and corresponding index |
| 2502 |
let ep_index = match pipeline_options { |
| 2503 |
Some(po) => { |
| 2504 |
let index = ir_module |
| 2505 |
.entry_points |
| 2506 |
.iter() |
| 2507 |
.position(|ep| po.shader_stage == ep.stage && po.entry_point == ep.name) |
| 2508 |
.ok_or(Error::EntryPointNotFound)?; |
| 2509 |
Some(index) |
| 2510 |
} |
| 2511 |
None => None, |
| 2512 |
}; |
| 2513 |
|
| 2514 |
self.write_logical_layout(ir_module, info, ep_index, debug_info)?; |
| 2515 |
self.write_physical_layout(); |
| 2516 |
|
| 2517 |
self.physical_layout.in_words(words); |
| 2518 |
self.logical_layout.in_words(words); |
| 2519 |
Ok(()) |
| 2520 |
} |
| 2521 |
|
| 2522 |
/// Return the set of capabilities the last module written used. |
| 2523 |
pub const fn get_capabilities_used(&self) -> &crate::FastIndexSet<spirv::Capability> { |
| 2524 |
&self.capabilities_used |
| 2525 |
} |
| 2526 |
|
| 2527 |
pub fn decorate_non_uniform_binding_array_access(&mut self, id: Word) -> Result<(), Error> { |
| 2528 |
self.require_any("NonUniformEXT", &[spirv::Capability::ShaderNonUniform])?; |
| 2529 |
self.use_extension("SPV_EXT_descriptor_indexing"); |
| 2530 |
self.decorate(id, spirv::Decoration::NonUniform, &[]); |
| 2531 |
Ok(()) |
| 2532 |
} |
| 2533 |
} |
| 2534 |
|
| 2535 |
#[test] |
| 2536 |
fn test_write_physical_layout() { |
| 2537 |
let mut writer = Writer::new(&Options::default()).unwrap(); |
| 2538 |
assert_eq!(writer.physical_layout.bound, 0); |
| 2539 |
writer.write_physical_layout(); |
| 2540 |
assert_eq!(writer.physical_layout.bound, 3); |
| 2541 |
} |