| 1 |
/*! |
| 2 |
Generating accesses to [`ByteAddressBuffer`] contents. |
| 3 |
|
| 4 |
Naga IR globals in the [`Storage`] address space are rendered as |
| 5 |
[`ByteAddressBuffer`]s or [`RWByteAddressBuffer`]s in HLSL. These |
| 6 |
buffers don't have HLSL types (structs, arrays, etc.); instead, they |
| 7 |
are just raw blocks of bytes, with methods to load and store values of |
| 8 |
specific types at particular byte offsets. This means that Naga must |
| 9 |
translate chains of [`Access`] and [`AccessIndex`] expressions into |
| 10 |
HLSL expressions that compute byte offsets into the buffer. |
| 11 |
|
| 12 |
To generate code for a [`Storage`] access: |
| 13 |
|
| 14 |
- Call [`Writer::fill_access_chain`] on the expression referring to |
| 15 |
the value. This populates [`Writer::temp_access_chain`] with the |
| 16 |
appropriate byte offset calculations, as a vector of [`SubAccess`] |
| 17 |
values. |
| 18 |
|
| 19 |
- Call [`Writer::write_storage_address`] to emit an HLSL expression |
| 20 |
for a given slice of [`SubAccess`] values. |
| 21 |
|
| 22 |
Naga IR expressions can operate on composite values of any type, but |
| 23 |
[`ByteAddressBuffer`] and [`RWByteAddressBuffer`] have only a fixed |
| 24 |
set of `Load` and `Store` methods, to access one through four |
| 25 |
consecutive 32-bit values. To synthesize a Naga access, you can |
| 26 |
initialize [`temp_access_chain`] to refer to the composite, and then |
| 27 |
temporarily push and pop additional steps on |
| 28 |
[`Writer::temp_access_chain`] to generate accesses to the individual |
| 29 |
elements/members. |
| 30 |
|
| 31 |
The [`temp_access_chain`] field is a member of [`Writer`] solely to |
| 32 |
allow re-use of the `Vec`'s dynamic allocation. Its value is no longer |
| 33 |
needed once HLSL for the access has been generated. |
| 34 |
|
| 35 |
Note about DXC and Load/Store functions: |
| 36 |
|
| 37 |
DXC's HLSL has a generic [`Load` and `Store`] function for [`ByteAddressBuffer`] and |
| 38 |
[`RWByteAddressBuffer`]. This is not available in FXC's HLSL, so we use |
| 39 |
it only for types that are only available in DXC. Notably 64 and 16 bit types. |
| 40 |
|
| 41 |
FXC's HLSL has functions Load, Load2, Load3, and Load4 and Store, Store2, Store3, Store4. |
| 42 |
This loads/stores a vector of length 1, 2, 3, or 4. We use that for 32bit types, bitcasting to the |
| 43 |
correct type if necessary. |
| 44 |
|
| 45 |
[`Storage`]: crate::AddressSpace::Storage |
| 46 |
[`ByteAddressBuffer`]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-byteaddressbuffer |
| 47 |
[`RWByteAddressBuffer`]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-rwbyteaddressbuffer |
| 48 |
[`Access`]: crate::Expression::Access |
| 49 |
[`AccessIndex`]: crate::Expression::AccessIndex |
| 50 |
[`Writer::fill_access_chain`]: super::Writer::fill_access_chain |
| 51 |
[`Writer::write_storage_address`]: super::Writer::write_storage_address |
| 52 |
[`Writer::temp_access_chain`]: super::Writer::temp_access_chain |
| 53 |
[`temp_access_chain`]: super::Writer::temp_access_chain |
| 54 |
[`Writer`]: super::Writer |
| 55 |
[`Load` and `Store`]: https://github.com/microsoft/DirectXShaderCompiler/wiki/ByteAddressBuffer-Load-Store-Additions |
| 56 |
*/ |
| 57 |
|
| 58 |
use alloc::format; |
| 59 |
use core::{fmt, mem}; |
| 60 |
|
| 61 |
use super::{super::FunctionCtx, BackendResult, Error}; |
| 62 |
use crate::{ |
| 63 |
proc::{Alignment, NameKey, TypeResolution}, |
| 64 |
Handle, |
| 65 |
}; |
| 66 |
|
| 67 |
const STORE_TEMP_NAME: &str = "_value"; |
| 68 |
|
| 69 |
/// One step in accessing a [`Storage`] global's component or element. |
| 70 |
/// |
| 71 |
/// [`Writer::temp_access_chain`] holds a series of these structures, |
| 72 |
/// describing how to compute the byte offset of a particular element |
| 73 |
/// or member of some global variable in the [`Storage`] address |
| 74 |
/// space. |
| 75 |
/// |
| 76 |
/// [`Writer::temp_access_chain`]: super::Writer::temp_access_chain |
| 77 |
/// [`Storage`]: crate::AddressSpace::Storage |
| 78 |
#[derive(Debug)] |
| 79 |
pub(super) enum SubAccess { |
| 80 |
BufferOffset { |
| 81 |
group: u32, |
| 82 |
offset: u32, |
| 83 |
}, |
| 84 |
|
| 85 |
/// Add the given byte offset. This is used for struct members, or |
| 86 |
/// known components of a vector or matrix. In all those cases, |
| 87 |
/// the byte offset is a compile-time constant. |
| 88 |
Offset(u32), |
| 89 |
|
| 90 |
/// Scale `value` by `stride`, and add that to the current byte |
| 91 |
/// offset. This is used to compute the offset of an array element |
| 92 |
/// whose index is computed at runtime. |
| 93 |
Index { |
| 94 |
value: Handle<crate::Expression>, |
| 95 |
stride: u32, |
| 96 |
}, |
| 97 |
} |
| 98 |
|
| 99 |
pub(super) enum StoreValue { |
| 100 |
Expression(Handle<crate::Expression>), |
| 101 |
TempIndex { |
| 102 |
depth: usize, |
| 103 |
index: u32, |
| 104 |
ty: TypeResolution, |
| 105 |
}, |
| 106 |
TempAccess { |
| 107 |
depth: usize, |
| 108 |
base: Handle<crate::Type>, |
| 109 |
member_index: u32, |
| 110 |
}, |
| 111 |
} |
| 112 |
|
| 113 |
impl<W: fmt::Write> super::Writer<'_, W> { |
| 114 |
pub(super) fn write_storage_address( |
| 115 |
&mut self, |
| 116 |
module: &crate::Module, |
| 117 |
chain: &[SubAccess], |
| 118 |
func_ctx: &FunctionCtx, |
| 119 |
) -> BackendResult { |
| 120 |
if chain.is_empty() { |
| 121 |
write!(self.out, "0")?; |
| 122 |
} |
| 123 |
for (i, access) in chain.iter().enumerate() { |
| 124 |
if i != 0 { |
| 125 |
write!(self.out, "+")?; |
| 126 |
} |
| 127 |
match *access { |
| 128 |
SubAccess::BufferOffset { group, offset } => { |
| 129 |
write!(self.out, "__dynamic_buffer_offsets{group}._{offset}")?; |
| 130 |
} |
| 131 |
SubAccess::Offset(offset) => { |
| 132 |
write!(self.out, "{offset}")?; |
| 133 |
} |
| 134 |
SubAccess::Index { value, stride } => { |
| 135 |
self.write_expr(module, value, func_ctx)?; |
| 136 |
write!(self.out, "*{stride}")?; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
Ok(()) |
| 141 |
} |
| 142 |
|
| 143 |
fn write_storage_load_sequence<I: Iterator<Item = (TypeResolution, u32)>>( |
| 144 |
&mut self, |
| 145 |
module: &crate::Module, |
| 146 |
var_handle: Handle<crate::GlobalVariable>, |
| 147 |
sequence: I, |
| 148 |
func_ctx: &FunctionCtx, |
| 149 |
) -> BackendResult { |
| 150 |
for (i, (ty_resolution, offset)) in sequence.enumerate() { |
| 151 |
// add the index temporarily |
| 152 |
self.temp_access_chain.push(SubAccess::Offset(offset)); |
| 153 |
if i != 0 { |
| 154 |
write!(self.out, ", ")?; |
| 155 |
}; |
| 156 |
self.write_storage_load(module, var_handle, ty_resolution, func_ctx)?; |
| 157 |
self.temp_access_chain.pop(); |
| 158 |
} |
| 159 |
Ok(()) |
| 160 |
} |
| 161 |
|
| 162 |
/// Emit code to access a [`Storage`] global's component. |
| 163 |
/// |
| 164 |
/// Emit HLSL to access the component of `var_handle`, a global |
| 165 |
/// variable in the [`Storage`] address space, whose type is |
| 166 |
/// `result_ty` and whose location within the global is given by |
| 167 |
/// [`self.temp_access_chain`]. See the [`storage`] module's |
| 168 |
/// documentation for background. |
| 169 |
/// |
| 170 |
/// [`Storage`]: crate::AddressSpace::Storage |
| 171 |
/// [`self.temp_access_chain`]: super::Writer::temp_access_chain |
| 172 |
pub(super) fn write_storage_load( |
| 173 |
&mut self, |
| 174 |
module: &crate::Module, |
| 175 |
var_handle: Handle<crate::GlobalVariable>, |
| 176 |
result_ty: TypeResolution, |
| 177 |
func_ctx: &FunctionCtx, |
| 178 |
) -> BackendResult { |
| 179 |
match *result_ty.inner_with(&module.types) { |
| 180 |
crate::TypeInner::Scalar(scalar) => { |
| 181 |
// working around the borrow checker in `self.write_expr` |
| 182 |
let chain = mem::take(&mut self.temp_access_chain); |
| 183 |
let var_name = &self.names[&NameKey::GlobalVariable(var_handle)]; |
| 184 |
// See note about DXC and Load/Store in the module's documentation. |
| 185 |
if scalar.width == 4 { |
| 186 |
let cast = scalar.kind.to_hlsl_cast(); |
| 187 |
write!(self.out, "{cast}({var_name}.Load(")?; |
| 188 |
} else { |
| 189 |
let ty = scalar.to_hlsl_str()?; |
| 190 |
write!(self.out, "{var_name}.Load<{ty}>(")?; |
| 191 |
}; |
| 192 |
self.write_storage_address(module, &chain, func_ctx)?; |
| 193 |
write!(self.out, ")")?; |
| 194 |
if scalar.width == 4 { |
| 195 |
write!(self.out, ")")?; |
| 196 |
} |
| 197 |
self.temp_access_chain = chain; |
| 198 |
} |
| 199 |
crate::TypeInner::Vector { size, scalar } => { |
| 200 |
// working around the borrow checker in `self.write_expr` |
| 201 |
let chain = mem::take(&mut self.temp_access_chain); |
| 202 |
let var_name = &self.names[&NameKey::GlobalVariable(var_handle)]; |
| 203 |
let size = size as u8; |
| 204 |
// See note about DXC and Load/Store in the module's documentation. |
| 205 |
if scalar.width == 4 { |
| 206 |
let cast = scalar.kind.to_hlsl_cast(); |
| 207 |
write!(self.out, "{cast}({var_name}.Load{size}(")?; |
| 208 |
} else { |
| 209 |
let ty = scalar.to_hlsl_str()?; |
| 210 |
write!(self.out, "{var_name}.Load<{ty}{size}>(")?; |
| 211 |
}; |
| 212 |
self.write_storage_address(module, &chain, func_ctx)?; |
| 213 |
write!(self.out, ")")?; |
| 214 |
if scalar.width == 4 { |
| 215 |
write!(self.out, ")")?; |
| 216 |
} |
| 217 |
self.temp_access_chain = chain; |
| 218 |
} |
| 219 |
crate::TypeInner::Matrix { |
| 220 |
columns, |
| 221 |
rows, |
| 222 |
scalar, |
| 223 |
} => { |
| 224 |
write!( |
| 225 |
self.out, |
| 226 |
"{}{}x{}(", |
| 227 |
scalar.to_hlsl_str()?, |
| 228 |
columns as u8, |
| 229 |
rows as u8, |
| 230 |
)?; |
| 231 |
|
| 232 |
// Note: Matrices containing vec3s, due to padding, act like they contain vec4s. |
| 233 |
let row_stride = Alignment::from(rows) * scalar.width as u32; |
| 234 |
let iter = (0..columns as u32).map(|i| { |
| 235 |
let ty_inner = crate::TypeInner::Vector { size: rows, scalar }; |
| 236 |
(TypeResolution::Value(ty_inner), i * row_stride) |
| 237 |
}); |
| 238 |
self.write_storage_load_sequence(module, var_handle, iter, func_ctx)?; |
| 239 |
write!(self.out, ")")?; |
| 240 |
} |
| 241 |
crate::TypeInner::Array { |
| 242 |
base, |
| 243 |
size: crate::ArraySize::Constant(size), |
| 244 |
stride, |
| 245 |
} => { |
| 246 |
let constructor = super::help::WrappedConstructor { |
| 247 |
ty: result_ty.handle().unwrap(), |
| 248 |
}; |
| 249 |
self.write_wrapped_constructor_function_name(module, constructor)?; |
| 250 |
write!(self.out, "(")?; |
| 251 |
let iter = (0..size.get()).map(|i| (TypeResolution::Handle(base), stride * i)); |
| 252 |
self.write_storage_load_sequence(module, var_handle, iter, func_ctx)?; |
| 253 |
write!(self.out, ")")?; |
| 254 |
} |
| 255 |
crate::TypeInner::Struct { ref members, .. } => { |
| 256 |
let constructor = super::help::WrappedConstructor { |
| 257 |
ty: result_ty.handle().unwrap(), |
| 258 |
}; |
| 259 |
self.write_wrapped_constructor_function_name(module, constructor)?; |
| 260 |
write!(self.out, "(")?; |
| 261 |
let iter = members |
| 262 |
.iter() |
| 263 |
.map(|m| (TypeResolution::Handle(m.ty), m.offset)); |
| 264 |
self.write_storage_load_sequence(module, var_handle, iter, func_ctx)?; |
| 265 |
write!(self.out, ")")?; |
| 266 |
} |
| 267 |
_ => unreachable!(), |
| 268 |
} |
| 269 |
Ok(()) |
| 270 |
} |
| 271 |
|
| 272 |
fn write_store_value( |
| 273 |
&mut self, |
| 274 |
module: &crate::Module, |
| 275 |
value: &StoreValue, |
| 276 |
func_ctx: &FunctionCtx, |
| 277 |
) -> BackendResult { |
| 278 |
match *value { |
| 279 |
StoreValue::Expression(expr) => self.write_expr(module, expr, func_ctx)?, |
| 280 |
StoreValue::TempIndex { |
| 281 |
depth, |
| 282 |
index, |
| 283 |
ty: _, |
| 284 |
} => write!(self.out, "{STORE_TEMP_NAME}{depth}[{index}]")?, |
| 285 |
StoreValue::TempAccess { |
| 286 |
depth, |
| 287 |
base, |
| 288 |
member_index, |
| 289 |
} => { |
| 290 |
let name = &self.names[&NameKey::StructMember(base, member_index)]; |
| 291 |
write!(self.out, "{STORE_TEMP_NAME}{depth}.{name}")? |
| 292 |
} |
| 293 |
} |
| 294 |
Ok(()) |
| 295 |
} |
| 296 |
|
| 297 |
/// Helper function to write down the Store operation on a `ByteAddressBuffer`. |
| 298 |
pub(super) fn write_storage_store( |
| 299 |
&mut self, |
| 300 |
module: &crate::Module, |
| 301 |
var_handle: Handle<crate::GlobalVariable>, |
| 302 |
value: StoreValue, |
| 303 |
func_ctx: &FunctionCtx, |
| 304 |
level: crate::back::Level, |
| 305 |
) -> BackendResult { |
| 306 |
let temp_resolution; |
| 307 |
let ty_resolution = match value { |
| 308 |
StoreValue::Expression(expr) => &func_ctx.info[expr].ty, |
| 309 |
StoreValue::TempIndex { |
| 310 |
depth: _, |
| 311 |
index: _, |
| 312 |
ref ty, |
| 313 |
} => ty, |
| 314 |
StoreValue::TempAccess { |
| 315 |
depth: _, |
| 316 |
base, |
| 317 |
member_index, |
| 318 |
} => { |
| 319 |
let ty_handle = match module.types[base].inner { |
| 320 |
crate::TypeInner::Struct { ref members, .. } => { |
| 321 |
members[member_index as usize].ty |
| 322 |
} |
| 323 |
_ => unreachable!(), |
| 324 |
}; |
| 325 |
temp_resolution = TypeResolution::Handle(ty_handle); |
| 326 |
&temp_resolution |
| 327 |
} |
| 328 |
}; |
| 329 |
match *ty_resolution.inner_with(&module.types) { |
| 330 |
crate::TypeInner::Scalar(scalar) => { |
| 331 |
// working around the borrow checker in `self.write_expr` |
| 332 |
let chain = mem::take(&mut self.temp_access_chain); |
| 333 |
let var_name = &self.names[&NameKey::GlobalVariable(var_handle)]; |
| 334 |
// See note about DXC and Load/Store in the module's documentation. |
| 335 |
if scalar.width == 4 { |
| 336 |
write!(self.out, "{level}{var_name}.Store(")?; |
| 337 |
self.write_storage_address(module, &chain, func_ctx)?; |
| 338 |
write!(self.out, ", asuint(")?; |
| 339 |
self.write_store_value(module, &value, func_ctx)?; |
| 340 |
writeln!(self.out, "));")?; |
| 341 |
} else { |
| 342 |
write!(self.out, "{level}{var_name}.Store(")?; |
| 343 |
self.write_storage_address(module, &chain, func_ctx)?; |
| 344 |
write!(self.out, ", ")?; |
| 345 |
self.write_store_value(module, &value, func_ctx)?; |
| 346 |
writeln!(self.out, ");")?; |
| 347 |
} |
| 348 |
self.temp_access_chain = chain; |
| 349 |
} |
| 350 |
crate::TypeInner::Vector { size, scalar } => { |
| 351 |
// working around the borrow checker in `self.write_expr` |
| 352 |
let chain = mem::take(&mut self.temp_access_chain); |
| 353 |
let var_name = &self.names[&NameKey::GlobalVariable(var_handle)]; |
| 354 |
// See note about DXC and Load/Store in the module's documentation. |
| 355 |
if scalar.width == 4 { |
| 356 |
write!(self.out, "{}{}.Store{}(", level, var_name, size as u8)?; |
| 357 |
self.write_storage_address(module, &chain, func_ctx)?; |
| 358 |
write!(self.out, ", asuint(")?; |
| 359 |
self.write_store_value(module, &value, func_ctx)?; |
| 360 |
writeln!(self.out, "));")?; |
| 361 |
} else { |
| 362 |
write!(self.out, "{level}{var_name}.Store(")?; |
| 363 |
self.write_storage_address(module, &chain, func_ctx)?; |
| 364 |
write!(self.out, ", ")?; |
| 365 |
self.write_store_value(module, &value, func_ctx)?; |
| 366 |
writeln!(self.out, ");")?; |
| 367 |
} |
| 368 |
self.temp_access_chain = chain; |
| 369 |
} |
| 370 |
crate::TypeInner::Matrix { |
| 371 |
columns, |
| 372 |
rows, |
| 373 |
scalar, |
| 374 |
} => { |
| 375 |
// first, assign the value to a temporary |
| 376 |
writeln!(self.out, "{level}{{")?; |
| 377 |
let depth = level.0 + 1; |
| 378 |
write!( |
| 379 |
self.out, |
| 380 |
"{}{}{}x{} {}{} = ", |
| 381 |
level.next(), |
| 382 |
scalar.to_hlsl_str()?, |
| 383 |
columns as u8, |
| 384 |
rows as u8, |
| 385 |
STORE_TEMP_NAME, |
| 386 |
depth, |
| 387 |
)?; |
| 388 |
self.write_store_value(module, &value, func_ctx)?; |
| 389 |
writeln!(self.out, ";")?; |
| 390 |
|
| 391 |
// Note: Matrices containing vec3s, due to padding, act like they contain vec4s. |
| 392 |
let row_stride = Alignment::from(rows) * scalar.width as u32; |
| 393 |
|
| 394 |
// then iterate the stores |
| 395 |
for i in 0..columns as u32 { |
| 396 |
self.temp_access_chain |
| 397 |
.push(SubAccess::Offset(i * row_stride)); |
| 398 |
let ty_inner = crate::TypeInner::Vector { size: rows, scalar }; |
| 399 |
let sv = StoreValue::TempIndex { |
| 400 |
depth, |
| 401 |
index: i, |
| 402 |
ty: TypeResolution::Value(ty_inner), |
| 403 |
}; |
| 404 |
self.write_storage_store(module, var_handle, sv, func_ctx, level.next())?; |
| 405 |
self.temp_access_chain.pop(); |
| 406 |
} |
| 407 |
// done |
| 408 |
writeln!(self.out, "{level}}}")?; |
| 409 |
} |
| 410 |
crate::TypeInner::Array { |
| 411 |
base, |
| 412 |
size: crate::ArraySize::Constant(size), |
| 413 |
stride, |
| 414 |
} => { |
| 415 |
// first, assign the value to a temporary |
| 416 |
writeln!(self.out, "{level}{{")?; |
| 417 |
write!(self.out, "{}", level.next())?; |
| 418 |
self.write_value_type(module, &module.types[base].inner)?; |
| 419 |
let depth = level.next().0; |
| 420 |
write!(self.out, " {STORE_TEMP_NAME}{depth}")?; |
| 421 |
self.write_array_size(module, base, crate::ArraySize::Constant(size))?; |
| 422 |
write!(self.out, " = ")?; |
| 423 |
self.write_store_value(module, &value, func_ctx)?; |
| 424 |
writeln!(self.out, ";")?; |
| 425 |
// then iterate the stores |
| 426 |
for i in 0..size.get() { |
| 427 |
self.temp_access_chain.push(SubAccess::Offset(i * stride)); |
| 428 |
let sv = StoreValue::TempIndex { |
| 429 |
depth, |
| 430 |
index: i, |
| 431 |
ty: TypeResolution::Handle(base), |
| 432 |
}; |
| 433 |
self.write_storage_store(module, var_handle, sv, func_ctx, level.next())?; |
| 434 |
self.temp_access_chain.pop(); |
| 435 |
} |
| 436 |
// done |
| 437 |
writeln!(self.out, "{level}}}")?; |
| 438 |
} |
| 439 |
crate::TypeInner::Struct { ref members, .. } => { |
| 440 |
// first, assign the value to a temporary |
| 441 |
writeln!(self.out, "{level}{{")?; |
| 442 |
let depth = level.next().0; |
| 443 |
let struct_ty = ty_resolution.handle().unwrap(); |
| 444 |
let struct_name = &self.names[&NameKey::Type(struct_ty)]; |
| 445 |
write!( |
| 446 |
self.out, |
| 447 |
"{}{} {}{} = ", |
| 448 |
level.next(), |
| 449 |
struct_name, |
| 450 |
STORE_TEMP_NAME, |
| 451 |
depth |
| 452 |
)?; |
| 453 |
self.write_store_value(module, &value, func_ctx)?; |
| 454 |
writeln!(self.out, ";")?; |
| 455 |
// then iterate the stores |
| 456 |
for (i, member) in members.iter().enumerate() { |
| 457 |
self.temp_access_chain |
| 458 |
.push(SubAccess::Offset(member.offset)); |
| 459 |
let sv = StoreValue::TempAccess { |
| 460 |
depth, |
| 461 |
base: struct_ty, |
| 462 |
member_index: i as u32, |
| 463 |
}; |
| 464 |
self.write_storage_store(module, var_handle, sv, func_ctx, level.next())?; |
| 465 |
self.temp_access_chain.pop(); |
| 466 |
} |
| 467 |
// done |
| 468 |
writeln!(self.out, "{level}}}")?; |
| 469 |
} |
| 470 |
_ => unreachable!(), |
| 471 |
} |
| 472 |
Ok(()) |
| 473 |
} |
| 474 |
|
| 475 |
/// Set [`temp_access_chain`] to compute the byte offset of `cur_expr`. |
| 476 |
/// |
| 477 |
/// The `cur_expr` expression must be a reference to a global |
| 478 |
/// variable in the [`Storage`] address space, or a chain of |
| 479 |
/// [`Access`] and [`AccessIndex`] expressions referring to some |
| 480 |
/// component of such a global. |
| 481 |
/// |
| 482 |
/// [`temp_access_chain`]: super::Writer::temp_access_chain |
| 483 |
/// [`Storage`]: crate::AddressSpace::Storage |
| 484 |
/// [`Access`]: crate::Expression::Access |
| 485 |
/// [`AccessIndex`]: crate::Expression::AccessIndex |
| 486 |
pub(super) fn fill_access_chain( |
| 487 |
&mut self, |
| 488 |
module: &crate::Module, |
| 489 |
mut cur_expr: Handle<crate::Expression>, |
| 490 |
func_ctx: &FunctionCtx, |
| 491 |
) -> Result<Handle<crate::GlobalVariable>, Error> { |
| 492 |
enum AccessIndex { |
| 493 |
Expression(Handle<crate::Expression>), |
| 494 |
Constant(u32), |
| 495 |
} |
| 496 |
enum Parent<'a> { |
| 497 |
Array { stride: u32 }, |
| 498 |
Struct(&'a [crate::StructMember]), |
| 499 |
} |
| 500 |
self.temp_access_chain.clear(); |
| 501 |
|
| 502 |
loop { |
| 503 |
let (next_expr, access_index) = match func_ctx.expressions[cur_expr] { |
| 504 |
crate::Expression::GlobalVariable(handle) => { |
| 505 |
if let Some(ref binding) = module.global_variables[handle].binding { |
| 506 |
// this was already resolved earlier when we started evaluating an entry point. |
| 507 |
let bt = self.options.resolve_resource_binding(binding).unwrap(); |
| 508 |
if let Some(dynamic_storage_buffer_offsets_index) = |
| 509 |
bt.dynamic_storage_buffer_offsets_index |
| 510 |
{ |
| 511 |
self.temp_access_chain.push(SubAccess::BufferOffset { |
| 512 |
group: binding.group, |
| 513 |
offset: dynamic_storage_buffer_offsets_index, |
| 514 |
}); |
| 515 |
} |
| 516 |
} |
| 517 |
return Ok(handle); |
| 518 |
} |
| 519 |
crate::Expression::Access { base, index } => (base, AccessIndex::Expression(index)), |
| 520 |
crate::Expression::AccessIndex { base, index } => { |
| 521 |
(base, AccessIndex::Constant(index)) |
| 522 |
} |
| 523 |
ref other => { |
| 524 |
return Err(Error::Unimplemented(format!("Pointer access of {other:?}"))) |
| 525 |
} |
| 526 |
}; |
| 527 |
|
| 528 |
let parent = match *func_ctx.resolve_type(next_expr, &module.types) { |
| 529 |
crate::TypeInner::Pointer { base, .. } => match module.types[base].inner { |
| 530 |
crate::TypeInner::Struct { ref members, .. } => Parent::Struct(members), |
| 531 |
crate::TypeInner::Array { stride, .. } => Parent::Array { stride }, |
| 532 |
crate::TypeInner::Vector { scalar, .. } => Parent::Array { |
| 533 |
stride: scalar.width as u32, |
| 534 |
}, |
| 535 |
crate::TypeInner::Matrix { rows, scalar, .. } => Parent::Array { |
| 536 |
// The stride between matrices is the count of rows as this is how |
| 537 |
// long each column is. |
| 538 |
stride: Alignment::from(rows) * scalar.width as u32, |
| 539 |
}, |
| 540 |
_ => unreachable!(), |
| 541 |
}, |
| 542 |
crate::TypeInner::ValuePointer { scalar, .. } => Parent::Array { |
| 543 |
stride: scalar.width as u32, |
| 544 |
}, |
| 545 |
_ => unreachable!(), |
| 546 |
}; |
| 547 |
|
| 548 |
let sub = match (parent, access_index) { |
| 549 |
(Parent::Array { stride }, AccessIndex::Expression(value)) => { |
| 550 |
SubAccess::Index { value, stride } |
| 551 |
} |
| 552 |
(Parent::Array { stride }, AccessIndex::Constant(index)) => { |
| 553 |
SubAccess::Offset(stride * index) |
| 554 |
} |
| 555 |
(Parent::Struct(members), AccessIndex::Constant(index)) => { |
| 556 |
SubAccess::Offset(members[index as usize].offset) |
| 557 |
} |
| 558 |
(Parent::Struct(_), AccessIndex::Expression(_)) => unreachable!(), |
| 559 |
}; |
| 560 |
|
| 561 |
self.temp_access_chain.push(sub); |
| 562 |
cur_expr = next_expr; |
| 563 |
} |
| 564 |
} |
| 565 |
} |