| 1 |
use alloc::{ |
| 2 |
borrow::Cow, |
| 3 |
boxed::Box, |
| 4 |
string::{String, ToString as _}, |
| 5 |
sync::{Arc, Weak}, |
| 6 |
vec::Vec, |
| 7 |
}; |
| 8 |
use core::{ |
| 9 |
fmt, |
| 10 |
mem::{self, ManuallyDrop}, |
| 11 |
num::NonZeroU32, |
| 12 |
sync::atomic::{AtomicBool, Ordering}, |
| 13 |
}; |
| 14 |
|
| 15 |
use arrayvec::ArrayVec; |
| 16 |
use bitflags::Flags; |
| 17 |
use smallvec::SmallVec; |
| 18 |
use wgt::{ |
| 19 |
math::align_to, DeviceLostReason, TextureFormat, TextureSampleType, TextureSelector, |
| 20 |
TextureViewDimension, |
| 21 |
}; |
| 22 |
|
| 23 |
#[cfg(feature = "trace")] |
| 24 |
use crate::device::trace; |
| 25 |
use crate::{ |
| 26 |
binding_model::{self, BindGroup, BindGroupLayout, BindGroupLayoutEntryError}, |
| 27 |
command, conv, |
| 28 |
device::{ |
| 29 |
bgl, create_validator, life::WaitIdleError, map_buffer, AttachmentData, |
| 30 |
DeviceLostInvocation, HostMap, MissingDownlevelFlags, MissingFeatures, RenderPassContext, |
| 31 |
CLEANUP_WAIT_MS, |
| 32 |
}, |
| 33 |
hal_label, |
| 34 |
init_tracker::{ |
| 35 |
BufferInitTracker, BufferInitTrackerAction, MemoryInitKind, TextureInitRange, |
| 36 |
TextureInitTrackerAction, |
| 37 |
}, |
| 38 |
instance::{Adapter, RequestDeviceError}, |
| 39 |
lock::{rank, Mutex, RwLock}, |
| 40 |
pipeline, |
| 41 |
pool::ResourcePool, |
| 42 |
resource::{ |
| 43 |
self, AccelerationStructure, Buffer, Fallible, Labeled, ParentDevice, QuerySet, Sampler, |
| 44 |
StagingBuffer, Texture, TextureView, TextureViewNotRenderableReason, Tlas, TrackingData, |
| 45 |
}, |
| 46 |
resource_log, |
| 47 |
snatch::{SnatchGuard, SnatchLock, Snatchable}, |
| 48 |
timestamp_normalization::TIMESTAMP_NORMALIZATION_BUFFER_USES, |
| 49 |
track::{BindGroupStates, DeviceTracker, TrackerIndexAllocators, UsageScope, UsageScopePool}, |
| 50 |
validation::{self, validate_color_attachment_bytes_per_sample}, |
| 51 |
weak_vec::WeakVec, |
| 52 |
FastHashMap, LabelHelpers, OnceCellOrLock, |
| 53 |
}; |
| 54 |
|
| 55 |
use super::{ |
| 56 |
queue::Queue, DeviceDescriptor, DeviceError, DeviceLostClosure, UserClosures, |
| 57 |
ENTRYPOINT_FAILURE_ERROR, ZERO_BUFFER_SIZE, |
| 58 |
}; |
| 59 |
|
| 60 |
#[cfg(supports_64bit_atomics)] |
| 61 |
use core::sync::atomic::AtomicU64; |
| 62 |
#[cfg(not(supports_64bit_atomics))] |
| 63 |
use portable_atomic::AtomicU64; |
| 64 |
|
| 65 |
pub(crate) struct CommandIndices { |
| 66 |
/// The index of the last command submission that was attempted. |
| 67 |
/// |
| 68 |
/// Note that `fence` may never be signalled with this value, if the command |
| 69 |
/// submission failed. If you need to wait for everything running on a |
| 70 |
/// `Queue` to complete, wait for [`last_successful_submission_index`]. |
| 71 |
/// |
| 72 |
/// [`last_successful_submission_index`]: Device::last_successful_submission_index |
| 73 |
pub(crate) active_submission_index: hal::FenceValue, |
| 74 |
pub(crate) next_acceleration_structure_build_command_index: u64, |
| 75 |
} |
| 76 |
|
| 77 |
/// Structure describing a logical device. Some members are internally mutable, |
| 78 |
/// stored behind mutexes. |
| 79 |
pub struct Device { |
| 80 |
raw: Box<dyn hal::DynDevice>, |
| 81 |
pub(crate) adapter: Arc<Adapter>, |
| 82 |
pub(crate) queue: OnceCellOrLock<Weak<Queue>>, |
| 83 |
pub(crate) zero_buffer: ManuallyDrop<Box<dyn hal::DynBuffer>>, |
| 84 |
/// The `label` from the descriptor used to create the resource. |
| 85 |
label: String, |
| 86 |
|
| 87 |
pub(crate) command_allocator: command::CommandAllocator, |
| 88 |
|
| 89 |
pub(crate) command_indices: RwLock<CommandIndices>, |
| 90 |
|
| 91 |
/// The index of the last successful submission to this device's |
| 92 |
/// [`hal::Queue`]. |
| 93 |
/// |
| 94 |
/// Unlike [`active_submission_index`], which is incremented each time |
| 95 |
/// submission is attempted, this is updated only when submission succeeds, |
| 96 |
/// so waiting for this value won't hang waiting for work that was never |
| 97 |
/// submitted. |
| 98 |
/// |
| 99 |
/// [`active_submission_index`]: CommandIndices::active_submission_index |
| 100 |
pub(crate) last_successful_submission_index: hal::AtomicFenceValue, |
| 101 |
|
| 102 |
// NOTE: if both are needed, the `snatchable_lock` must be consistently acquired before the |
| 103 |
// `fence` lock to avoid deadlocks. |
| 104 |
pub(crate) fence: RwLock<ManuallyDrop<Box<dyn hal::DynFence>>>, |
| 105 |
pub(crate) snatchable_lock: SnatchLock, |
| 106 |
|
| 107 |
/// Is this device valid? Valid is closely associated with "lose the device", |
| 108 |
/// which can be triggered by various methods, including at the end of device |
| 109 |
/// destroy, and by any GPU errors that cause us to no longer trust the state |
| 110 |
/// of the device. Ideally we would like to fold valid into the storage of |
| 111 |
/// the device itself (for example as an Error enum), but unfortunately we |
| 112 |
/// need to continue to be able to retrieve the device in poll_devices to |
| 113 |
/// determine if it can be dropped. If our internal accesses of devices were |
| 114 |
/// done through ref-counted references and external accesses checked for |
| 115 |
/// Error enums, we wouldn't need this. For now, we need it. All the call |
| 116 |
/// sites where we check it are areas that should be revisited if we start |
| 117 |
/// using ref-counted references for internal access. |
| 118 |
pub(crate) valid: AtomicBool, |
| 119 |
|
| 120 |
/// Closure to be called on "lose the device". This is invoked directly by |
| 121 |
/// device.lose or by the UserCallbacks returned from maintain when the device |
| 122 |
/// has been destroyed and its queues are empty. |
| 123 |
pub(crate) device_lost_closure: Mutex<Option<DeviceLostClosure>>, |
| 124 |
|
| 125 |
/// Stores the state of buffers and textures. |
| 126 |
pub(crate) trackers: Mutex<DeviceTracker>, |
| 127 |
pub(crate) tracker_indices: TrackerIndexAllocators, |
| 128 |
/// Pool of bind group layouts, allowing deduplication. |
| 129 |
pub(crate) bgl_pool: ResourcePool<bgl::EntryMap, BindGroupLayout>, |
| 130 |
pub(crate) alignments: hal::Alignments, |
| 131 |
pub(crate) limits: wgt::Limits, |
| 132 |
pub(crate) features: wgt::Features, |
| 133 |
pub(crate) downlevel: wgt::DownlevelCapabilities, |
| 134 |
pub(crate) instance_flags: wgt::InstanceFlags, |
| 135 |
pub(crate) deferred_destroy: Mutex<Vec<DeferredDestroy>>, |
| 136 |
pub(crate) usage_scopes: UsageScopePool, |
| 137 |
pub(crate) indirect_validation: Option<crate::indirect_validation::IndirectValidation>, |
| 138 |
// Optional so that we can late-initialize this after the queue is created. |
| 139 |
pub(crate) timestamp_normalizer: |
| 140 |
OnceCellOrLock<crate::timestamp_normalization::TimestampNormalizer>, |
| 141 |
// needs to be dropped last |
| 142 |
#[cfg(feature = "trace")] |
| 143 |
pub(crate) trace: Mutex<Option<trace::Trace>>, |
| 144 |
} |
| 145 |
|
| 146 |
pub(crate) enum DeferredDestroy { |
| 147 |
TextureViews(WeakVec<TextureView>), |
| 148 |
BindGroups(WeakVec<BindGroup>), |
| 149 |
} |
| 150 |
|
| 151 |
impl fmt::Debug for Device { |
| 152 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 153 |
f.debug_struct("Device") |
| 154 |
.field("label", &self.label()) |
| 155 |
.field("limits", &self.limits) |
| 156 |
.field("features", &self.features) |
| 157 |
.field("downlevel", &self.downlevel) |
| 158 |
.finish() |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
impl Drop for Device { |
| 163 |
fn drop(&mut self) { |
| 164 |
resource_log!("Drop {}", self.error_ident()); |
| 165 |
|
| 166 |
// SAFETY: We are in the Drop impl and we don't use self.zero_buffer anymore after this point. |
| 167 |
let zero_buffer = unsafe { ManuallyDrop::take(&mut self.zero_buffer) }; |
| 168 |
// SAFETY: We are in the Drop impl and we don't use self.fence anymore after this point. |
| 169 |
let fence = unsafe { ManuallyDrop::take(&mut self.fence.write()) }; |
| 170 |
if let Some(indirect_validation) = self.indirect_validation.take() { |
| 171 |
indirect_validation.dispose(self.raw.as_ref()); |
| 172 |
} |
| 173 |
if let Some(timestamp_normalizer) = self.timestamp_normalizer.take() { |
| 174 |
timestamp_normalizer.dispose(self.raw.as_ref()); |
| 175 |
} |
| 176 |
unsafe { |
| 177 |
self.raw.destroy_buffer(zero_buffer); |
| 178 |
self.raw.destroy_fence(fence); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
impl Device { |
| 184 |
pub(crate) fn raw(&self) -> &dyn hal::DynDevice { |
| 185 |
self.raw.as_ref() |
| 186 |
} |
| 187 |
pub(crate) fn require_features(&self, feature: wgt::Features) -> Result<(), MissingFeatures> { |
| 188 |
if self.features.contains(feature) { |
| 189 |
Ok(()) |
| 190 |
} else { |
| 191 |
Err(MissingFeatures(feature)) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
pub(crate) fn require_downlevel_flags( |
| 196 |
&self, |
| 197 |
flags: wgt::DownlevelFlags, |
| 198 |
) -> Result<(), MissingDownlevelFlags> { |
| 199 |
if self.downlevel.flags.contains(flags) { |
| 200 |
Ok(()) |
| 201 |
} else { |
| 202 |
Err(MissingDownlevelFlags(flags)) |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
impl Device { |
| 208 |
pub(crate) fn new( |
| 209 |
raw_device: Box<dyn hal::DynDevice>, |
| 210 |
adapter: &Arc<Adapter>, |
| 211 |
desc: &DeviceDescriptor, |
| 212 |
instance_flags: wgt::InstanceFlags, |
| 213 |
) -> Result<Self, DeviceError> { |
| 214 |
#[cfg(not(feature = "trace"))] |
| 215 |
match &desc.trace { |
| 216 |
wgt::Trace::Off => {} |
| 217 |
_ => { |
| 218 |
log::error!("wgpu-core feature 'trace' is not enabled"); |
| 219 |
} |
| 220 |
}; |
| 221 |
#[cfg(feature = "trace")] |
| 222 |
let trace_dir_name: Option<&std::path::PathBuf> = match &desc.trace { |
| 223 |
wgt::Trace::Off => None, |
| 224 |
wgt::Trace::Directory(d) => Some(d), |
| 225 |
// The enum is non_exhaustive, so we must have a fallback arm (that should be |
| 226 |
// unreachable in practice). |
| 227 |
t => { |
| 228 |
log::error!("unimplemented wgpu_types::Trace variant {t:?}"); |
| 229 |
None |
| 230 |
} |
| 231 |
}; |
| 232 |
|
| 233 |
let fence = unsafe { raw_device.create_fence() }.map_err(DeviceError::from_hal)?; |
| 234 |
|
| 235 |
let command_allocator = command::CommandAllocator::new(); |
| 236 |
|
| 237 |
let rt_uses = if desc |
| 238 |
.required_features |
| 239 |
.contains(wgt::Features::EXPERIMENTAL_RAY_TRACING_ACCELERATION_STRUCTURE) |
| 240 |
{ |
| 241 |
wgt::BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT |
| 242 |
} else { |
| 243 |
wgt::BufferUses::empty() |
| 244 |
}; |
| 245 |
|
| 246 |
// Create zeroed buffer used for texture clears (and raytracing if required). |
| 247 |
let zero_buffer = unsafe { |
| 248 |
raw_device.create_buffer(&hal::BufferDescriptor { |
| 249 |
label: hal_label(Some("(wgpu internal) zero init buffer"), instance_flags), |
| 250 |
size: ZERO_BUFFER_SIZE, |
| 251 |
usage: wgt::BufferUses::COPY_SRC | wgt::BufferUses::COPY_DST | rt_uses, |
| 252 |
memory_flags: hal::MemoryFlags::empty(), |
| 253 |
}) |
| 254 |
} |
| 255 |
.map_err(DeviceError::from_hal)?; |
| 256 |
|
| 257 |
let alignments = adapter.raw.capabilities.alignments.clone(); |
| 258 |
let downlevel = adapter.raw.capabilities.downlevel.clone(); |
| 259 |
|
| 260 |
let enable_indirect_validation = instance_flags |
| 261 |
.contains(wgt::InstanceFlags::VALIDATION_INDIRECT_CALL) |
| 262 |
&& downlevel |
| 263 |
.flags |
| 264 |
.contains(wgt::DownlevelFlags::INDIRECT_EXECUTION); |
| 265 |
|
| 266 |
let indirect_validation = if enable_indirect_validation { |
| 267 |
Some(crate::indirect_validation::IndirectValidation::new( |
| 268 |
raw_device.as_ref(), |
| 269 |
&desc.required_limits, |
| 270 |
&desc.required_features, |
| 271 |
adapter.backend(), |
| 272 |
)?) |
| 273 |
} else { |
| 274 |
None |
| 275 |
}; |
| 276 |
|
| 277 |
Ok(Self { |
| 278 |
raw: raw_device, |
| 279 |
adapter: adapter.clone(), |
| 280 |
queue: OnceCellOrLock::new(), |
| 281 |
zero_buffer: ManuallyDrop::new(zero_buffer), |
| 282 |
label: desc.label.to_string(), |
| 283 |
command_allocator, |
| 284 |
command_indices: RwLock::new( |
| 285 |
rank::DEVICE_COMMAND_INDICES, |
| 286 |
CommandIndices { |
| 287 |
active_submission_index: 0, |
| 288 |
// By starting at one, we can put the result in a NonZeroU64. |
| 289 |
next_acceleration_structure_build_command_index: 1, |
| 290 |
}, |
| 291 |
), |
| 292 |
last_successful_submission_index: AtomicU64::new(0), |
| 293 |
fence: RwLock::new(rank::DEVICE_FENCE, ManuallyDrop::new(fence)), |
| 294 |
snatchable_lock: unsafe { SnatchLock::new(rank::DEVICE_SNATCHABLE_LOCK) }, |
| 295 |
valid: AtomicBool::new(true), |
| 296 |
device_lost_closure: Mutex::new(rank::DEVICE_LOST_CLOSURE, None), |
| 297 |
trackers: Mutex::new(rank::DEVICE_TRACKERS, DeviceTracker::new()), |
| 298 |
tracker_indices: TrackerIndexAllocators::new(), |
| 299 |
bgl_pool: ResourcePool::new(), |
| 300 |
#[cfg(feature = "trace")] |
| 301 |
trace: Mutex::new( |
| 302 |
rank::DEVICE_TRACE, |
| 303 |
trace_dir_name.and_then(|path| match trace::Trace::new(path.clone()) { |
| 304 |
Ok(mut trace) => { |
| 305 |
trace.add(trace::Action::Init { |
| 306 |
desc: wgt::DeviceDescriptor { |
| 307 |
trace: wgt::Trace::Off, |
| 308 |
..desc.clone() |
| 309 |
}, |
| 310 |
backend: adapter.backend(), |
| 311 |
}); |
| 312 |
Some(trace) |
| 313 |
} |
| 314 |
Err(e) => { |
| 315 |
log::error!("Unable to start a trace in '{path:?}': {e}"); |
| 316 |
None |
| 317 |
} |
| 318 |
}), |
| 319 |
), |
| 320 |
alignments, |
| 321 |
limits: desc.required_limits.clone(), |
| 322 |
features: desc.required_features, |
| 323 |
downlevel, |
| 324 |
instance_flags, |
| 325 |
deferred_destroy: Mutex::new(rank::DEVICE_DEFERRED_DESTROY, Vec::new()), |
| 326 |
usage_scopes: Mutex::new(rank::DEVICE_USAGE_SCOPES, Default::default()), |
| 327 |
timestamp_normalizer: OnceCellOrLock::new(), |
| 328 |
indirect_validation, |
| 329 |
}) |
| 330 |
} |
| 331 |
|
| 332 |
pub fn late_init_resources_with_queue(&self) -> Result<(), RequestDeviceError> { |
| 333 |
let queue = self.get_queue().unwrap(); |
| 334 |
|
| 335 |
let timestamp_normalizer = crate::timestamp_normalization::TimestampNormalizer::new( |
| 336 |
self, |
| 337 |
queue.get_timestamp_period(), |
| 338 |
)?; |
| 339 |
|
| 340 |
self.timestamp_normalizer |
| 341 |
.set(timestamp_normalizer) |
| 342 |
.unwrap_or_else(|_| panic!("Called late_init_resources_with_queue twice")); |
| 343 |
|
| 344 |
Ok(()) |
| 345 |
} |
| 346 |
|
| 347 |
/// Returns the backend this device is using. |
| 348 |
pub fn backend(&self) -> wgt::Backend { |
| 349 |
self.adapter.backend() |
| 350 |
} |
| 351 |
|
| 352 |
pub fn is_valid(&self) -> bool { |
| 353 |
self.valid.load(Ordering::Acquire) |
| 354 |
} |
| 355 |
|
| 356 |
pub fn check_is_valid(&self) -> Result<(), DeviceError> { |
| 357 |
if self.is_valid() { |
| 358 |
Ok(()) |
| 359 |
} else { |
| 360 |
Err(DeviceError::Invalid(self.error_ident())) |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/// Checks that we are operating within the memory budget reported by the native APIs. |
| 365 |
/// |
| 366 |
/// If we are not, the device gets invalidated. |
| 367 |
/// |
| 368 |
/// The budget might fluctuate over the lifetime of the application, so it should be checked |
| 369 |
/// somewhat frequently. |
| 370 |
pub fn lose_if_oom(&self) { |
| 371 |
let _ = self |
| 372 |
.raw() |
| 373 |
.check_if_oom() |
| 374 |
.map_err(|e| self.handle_hal_error(e)); |
| 375 |
} |
| 376 |
|
| 377 |
pub fn handle_hal_error(&self, error: hal::DeviceError) -> DeviceError { |
| 378 |
match error { |
| 379 |
hal::DeviceError::OutOfMemory |
| 380 |
| hal::DeviceError::Lost |
| 381 |
| hal::DeviceError::ResourceCreationFailed |
| 382 |
| hal::DeviceError::Unexpected => { |
| 383 |
self.lose(&error.to_string()); |
| 384 |
} |
| 385 |
} |
| 386 |
DeviceError::from_hal(error) |
| 387 |
} |
| 388 |
|
| 389 |
pub fn handle_hal_error_with_nonfatal_oom(&self, error: hal::DeviceError) -> DeviceError { |
| 390 |
match error { |
| 391 |
hal::DeviceError::OutOfMemory => DeviceError::from_hal(error), |
| 392 |
error => self.handle_hal_error(error), |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
/// Run some destroy operations that were deferred. |
| 397 |
/// |
| 398 |
/// Destroying the resources requires taking a write lock on the device's snatch lock, |
| 399 |
/// so a good reason for deferring resource destruction is when we don't know for sure |
| 400 |
/// how risky it is to take the lock (typically, it shouldn't be taken from the drop |
| 401 |
/// implementation of a reference-counted structure). |
| 402 |
/// The snatch lock must not be held while this function is called. |
| 403 |
pub(crate) fn deferred_resource_destruction(&self) { |
| 404 |
let deferred_destroy = mem::take(&mut *self.deferred_destroy.lock()); |
| 405 |
for item in deferred_destroy { |
| 406 |
match item { |
| 407 |
DeferredDestroy::TextureViews(views) => { |
| 408 |
for view in views { |
| 409 |
let Some(view) = view.upgrade() else { |
| 410 |
continue; |
| 411 |
}; |
| 412 |
let Some(raw_view) = view.raw.snatch(&mut self.snatchable_lock.write()) |
| 413 |
else { |
| 414 |
continue; |
| 415 |
}; |
| 416 |
|
| 417 |
resource_log!("Destroy raw {}", view.error_ident()); |
| 418 |
|
| 419 |
unsafe { |
| 420 |
self.raw().destroy_texture_view(raw_view); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
DeferredDestroy::BindGroups(bind_groups) => { |
| 425 |
for bind_group in bind_groups { |
| 426 |
let Some(bind_group) = bind_group.upgrade() else { |
| 427 |
continue; |
| 428 |
}; |
| 429 |
let Some(raw_bind_group) = |
| 430 |
bind_group.raw.snatch(&mut self.snatchable_lock.write()) |
| 431 |
else { |
| 432 |
continue; |
| 433 |
}; |
| 434 |
|
| 435 |
resource_log!("Destroy raw {}", bind_group.error_ident()); |
| 436 |
|
| 437 |
unsafe { |
| 438 |
self.raw().destroy_bind_group(raw_bind_group); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
pub fn get_queue(&self) -> Option<Arc<Queue>> { |
| 447 |
self.queue.get().as_ref()?.upgrade() |
| 448 |
} |
| 449 |
|
| 450 |
pub fn set_queue(&self, queue: &Arc<Queue>) { |
| 451 |
assert!(self.queue.set(Arc::downgrade(queue)).is_ok()); |
| 452 |
} |
| 453 |
|
| 454 |
/// Check the current status of the GPU and process any submissions that have |
| 455 |
/// finished. |
| 456 |
/// |
| 457 |
/// The `poll_type` argument tells if this function should wait for a particular |
| 458 |
/// submission index to complete, or if it should just poll the current status. |
| 459 |
/// |
| 460 |
/// This will process _all_ completed submissions, even if the caller only asked |
| 461 |
/// us to poll to a given submission index. |
| 462 |
/// |
| 463 |
/// Return a pair `(closures, result)`, where: |
| 464 |
/// |
| 465 |
/// - `closures` is a list of callbacks that need to be invoked informing the user |
| 466 |
/// about various things occurring. These happen and should be handled even if |
| 467 |
/// this function returns an error, hence they are outside of the result. |
| 468 |
/// |
| 469 |
/// - `results` is a boolean indicating the result of the wait operation, including |
| 470 |
/// if there was a timeout or a validation error. |
| 471 |
pub(crate) fn maintain<'this>( |
| 472 |
&'this self, |
| 473 |
fence: crate::lock::RwLockReadGuard<ManuallyDrop<Box<dyn hal::DynFence>>>, |
| 474 |
poll_type: wgt::PollType<crate::SubmissionIndex>, |
| 475 |
snatch_guard: SnatchGuard, |
| 476 |
) -> (UserClosures, Result<wgt::PollStatus, WaitIdleError>) { |
| 477 |
profiling::scope!("Device::maintain"); |
| 478 |
|
| 479 |
let mut user_closures = UserClosures::default(); |
| 480 |
|
| 481 |
// If a wait was requested, determine which submission index to wait for. |
| 482 |
let wait_submission_index = match poll_type { |
| 483 |
wgt::PollType::WaitForSubmissionIndex(submission_index) => { |
| 484 |
let last_successful_submission_index = self |
| 485 |
.last_successful_submission_index |
| 486 |
.load(Ordering::Acquire); |
| 487 |
|
| 488 |
if submission_index > last_successful_submission_index { |
| 489 |
let result = Err(WaitIdleError::WrongSubmissionIndex( |
| 490 |
submission_index, |
| 491 |
last_successful_submission_index, |
| 492 |
)); |
| 493 |
|
| 494 |
return (user_closures, result); |
| 495 |
} |
| 496 |
|
| 497 |
Some(submission_index) |
| 498 |
} |
| 499 |
wgt::PollType::Wait => Some( |
| 500 |
self.last_successful_submission_index |
| 501 |
.load(Ordering::Acquire), |
| 502 |
), |
| 503 |
wgt::PollType::Poll => None, |
| 504 |
}; |
| 505 |
|
| 506 |
// Wait for the submission index if requested. |
| 507 |
if let Some(target_submission_index) = wait_submission_index { |
| 508 |
log::trace!("Device::maintain: waiting for submission index {target_submission_index}"); |
| 509 |
|
| 510 |
let wait_result = unsafe { |
| 511 |
self.raw() |
| 512 |
.wait(fence.as_ref(), target_submission_index, CLEANUP_WAIT_MS) |
| 513 |
}; |
| 514 |
|
| 515 |
// This error match is only about `DeviceErrors`. At this stage we do not care if |
| 516 |
// the wait succeeded or not, and the `Ok(bool)`` variant is ignored. |
| 517 |
if let Err(e) = wait_result { |
| 518 |
let hal_error: WaitIdleError = self.handle_hal_error(e).into(); |
| 519 |
return (user_closures, Err(hal_error)); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
// Get the currently finished submission index. This may be higher than the requested |
| 524 |
// wait, or it may be less than the requested wait if the wait failed. |
| 525 |
let fence_value_result = unsafe { self.raw().get_fence_value(fence.as_ref()) }; |
| 526 |
let current_finished_submission = match fence_value_result { |
| 527 |
Ok(fence_value) => fence_value, |
| 528 |
Err(e) => { |
| 529 |
let hal_error: WaitIdleError = self.handle_hal_error(e).into(); |
| 530 |
return (user_closures, Err(hal_error)); |
| 531 |
} |
| 532 |
}; |
| 533 |
|
| 534 |
// Maintain all finished submissions on the queue, updating the relevant user closures and collecting if the queue is empty. |
| 535 |
// |
| 536 |
// We don't use the result of the wait here, as we want to progress forward as far as possible |
| 537 |
// and the wait could have been for submissions that finished long ago. |
| 538 |
let mut queue_empty = false; |
| 539 |
if let Some(queue) = self.get_queue() { |
| 540 |
let queue_result = queue.maintain(current_finished_submission, &snatch_guard); |
| 541 |
( |
| 542 |
user_closures.submissions, |
| 543 |
user_closures.mappings, |
| 544 |
queue_empty, |
| 545 |
) = queue_result |
| 546 |
}; |
| 547 |
|
| 548 |
// Based on the queue empty status, and the current finished submission index, determine the result of the poll. |
| 549 |
let result = if queue_empty { |
| 550 |
if let Some(wait_submission_index) = wait_submission_index { |
| 551 |
// Assert to ensure that if we received a queue empty status, the fence shows the correct value. |
| 552 |
// This is defensive, as this should never be hit. |
| 553 |
assert!( |
| 554 |
current_finished_submission >= wait_submission_index, |
| 555 |
"If the queue is empty, the current submission index ({}) should be at least the wait submission index ({})", |
| 556 |
current_finished_submission, |
| 557 |
wait_submission_index |
| 558 |
); |
| 559 |
} |
| 560 |
|
| 561 |
Ok(wgt::PollStatus::QueueEmpty) |
| 562 |
} else if let Some(wait_submission_index) = wait_submission_index { |
| 563 |
// This is theoretically possible to succeed more than checking on the poll result |
| 564 |
// as submissions could have finished in the time between the timeout resolving, |
| 565 |
// the thread getting scheduled again, and us checking the fence value. |
| 566 |
if current_finished_submission >= wait_submission_index { |
| 567 |
Ok(wgt::PollStatus::WaitSucceeded) |
| 568 |
} else { |
| 569 |
Err(WaitIdleError::Timeout) |
| 570 |
} |
| 571 |
} else { |
| 572 |
Ok(wgt::PollStatus::Poll) |
| 573 |
}; |
| 574 |
|
| 575 |
// Detect if we have been destroyed and now need to lose the device. |
| 576 |
// |
| 577 |
// If we are invalid (set at start of destroy) and our queue is empty, |
| 578 |
// and we have a DeviceLostClosure, return the closure to be called by |
| 579 |
// our caller. This will complete the steps for both destroy and for |
| 580 |
// "lose the device". |
| 581 |
let mut should_release_gpu_resource = false; |
| 582 |
if !self.is_valid() && queue_empty { |
| 583 |
// We can release gpu resources associated with this device (but not |
| 584 |
// while holding the life_tracker lock). |
| 585 |
should_release_gpu_resource = true; |
| 586 |
|
| 587 |
// If we have a DeviceLostClosure, build an invocation with the |
| 588 |
// reason DeviceLostReason::Destroyed and no message. |
| 589 |
if let Some(device_lost_closure) = self.device_lost_closure.lock().take() { |
| 590 |
user_closures |
| 591 |
.device_lost_invocations |
| 592 |
.push(DeviceLostInvocation { |
| 593 |
closure: device_lost_closure, |
| 594 |
reason: DeviceLostReason::Destroyed, |
| 595 |
message: String::new(), |
| 596 |
}); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
// Don't hold the locks while calling release_gpu_resources. |
| 601 |
drop(fence); |
| 602 |
drop(snatch_guard); |
| 603 |
|
| 604 |
if should_release_gpu_resource { |
| 605 |
self.release_gpu_resources(); |
| 606 |
} |
| 607 |
|
| 608 |
(user_closures, result) |
| 609 |
} |
| 610 |
|
| 611 |
pub(crate) fn create_buffer( |
| 612 |
self: &Arc<Self>, |
| 613 |
desc: &resource::BufferDescriptor, |
| 614 |
) -> Result<Arc<Buffer>, resource::CreateBufferError> { |
| 615 |
self.check_is_valid()?; |
| 616 |
|
| 617 |
if desc.size > self.limits.max_buffer_size { |
| 618 |
return Err(resource::CreateBufferError::MaxBufferSize { |
| 619 |
requested: desc.size, |
| 620 |
maximum: self.limits.max_buffer_size, |
| 621 |
}); |
| 622 |
} |
| 623 |
|
| 624 |
if desc.usage.contains(wgt::BufferUsages::INDEX) |
| 625 |
&& desc.usage.contains( |
| 626 |
wgt::BufferUsages::VERTEX |
| 627 |
| wgt::BufferUsages::UNIFORM |
| 628 |
| wgt::BufferUsages::INDIRECT |
| 629 |
| wgt::BufferUsages::STORAGE, |
| 630 |
) |
| 631 |
{ |
| 632 |
self.require_downlevel_flags(wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER)?; |
| 633 |
} |
| 634 |
|
| 635 |
if desc.usage.is_empty() || desc.usage.contains_unknown_bits() { |
| 636 |
return Err(resource::CreateBufferError::InvalidUsage(desc.usage)); |
| 637 |
} |
| 638 |
|
| 639 |
if !self |
| 640 |
.features |
| 641 |
.contains(wgt::Features::MAPPABLE_PRIMARY_BUFFERS) |
| 642 |
{ |
| 643 |
use wgt::BufferUsages as Bu; |
| 644 |
let write_mismatch = desc.usage.contains(Bu::MAP_WRITE) |
| 645 |
&& !(Bu::MAP_WRITE | Bu::COPY_SRC).contains(desc.usage); |
| 646 |
let read_mismatch = desc.usage.contains(Bu::MAP_READ) |
| 647 |
&& !(Bu::MAP_READ | Bu::COPY_DST).contains(desc.usage); |
| 648 |
if write_mismatch || read_mismatch { |
| 649 |
return Err(resource::CreateBufferError::UsageMismatch(desc.usage)); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
let mut usage = conv::map_buffer_usage(desc.usage); |
| 654 |
|
| 655 |
if desc.usage.contains(wgt::BufferUsages::INDIRECT) { |
| 656 |
self.require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION)?; |
| 657 |
// We are going to be reading from it, internally; |
| 658 |
// when validating the content of the buffer |
| 659 |
usage |= wgt::BufferUses::STORAGE_READ_ONLY | wgt::BufferUses::STORAGE_READ_WRITE; |
| 660 |
} |
| 661 |
|
| 662 |
if desc.usage.contains(wgt::BufferUsages::QUERY_RESOLVE) { |
| 663 |
usage |= TIMESTAMP_NORMALIZATION_BUFFER_USES; |
| 664 |
} |
| 665 |
|
| 666 |
if desc.mapped_at_creation { |
| 667 |
if desc.size % wgt::COPY_BUFFER_ALIGNMENT != 0 { |
| 668 |
return Err(resource::CreateBufferError::UnalignedSize); |
| 669 |
} |
| 670 |
if !desc.usage.contains(wgt::BufferUsages::MAP_WRITE) { |
| 671 |
// we are going to be copying into it, internally |
| 672 |
usage |= wgt::BufferUses::COPY_DST; |
| 673 |
} |
| 674 |
} else { |
| 675 |
// We are required to zero out (initialize) all memory. This is done |
| 676 |
// on demand using clear_buffer which requires write transfer usage! |
| 677 |
usage |= wgt::BufferUses::COPY_DST; |
| 678 |
} |
| 679 |
|
| 680 |
let actual_size = if desc.size == 0 { |
| 681 |
wgt::COPY_BUFFER_ALIGNMENT |
| 682 |
} else if desc.usage.contains(wgt::BufferUsages::VERTEX) { |
| 683 |
// Bumping the size by 1 so that we can bind an empty range at the |
| 684 |
// end of the buffer. |
| 685 |
desc.size + 1 |
| 686 |
} else { |
| 687 |
desc.size |
| 688 |
}; |
| 689 |
let clear_remainder = actual_size % wgt::COPY_BUFFER_ALIGNMENT; |
| 690 |
let aligned_size = if clear_remainder != 0 { |
| 691 |
actual_size + wgt::COPY_BUFFER_ALIGNMENT - clear_remainder |
| 692 |
} else { |
| 693 |
actual_size |
| 694 |
}; |
| 695 |
|
| 696 |
let hal_desc = hal::BufferDescriptor { |
| 697 |
label: desc.label.to_hal(self.instance_flags), |
| 698 |
size: aligned_size, |
| 699 |
usage, |
| 700 |
memory_flags: hal::MemoryFlags::empty(), |
| 701 |
}; |
| 702 |
let buffer = unsafe { self.raw().create_buffer(&hal_desc) } |
| 703 |
.map_err(|e| self.handle_hal_error_with_nonfatal_oom(e))?; |
| 704 |
|
| 705 |
let timestamp_normalization_bind_group = Snatchable::new( |
| 706 |
self.timestamp_normalizer |
| 707 |
.get() |
| 708 |
.unwrap() |
| 709 |
.create_normalization_bind_group( |
| 710 |
self, |
| 711 |
&*buffer, |
| 712 |
desc.label.as_deref(), |
| 713 |
desc.size, |
| 714 |
desc.usage, |
| 715 |
)?, |
| 716 |
); |
| 717 |
|
| 718 |
let indirect_validation_bind_groups = |
| 719 |
self.create_indirect_validation_bind_groups(buffer.as_ref(), desc.size, desc.usage)?; |
| 720 |
|
| 721 |
let buffer = Buffer { |
| 722 |
raw: Snatchable::new(buffer), |
| 723 |
device: self.clone(), |
| 724 |
usage: desc.usage, |
| 725 |
size: desc.size, |
| 726 |
initialization_status: RwLock::new( |
| 727 |
rank::BUFFER_INITIALIZATION_STATUS, |
| 728 |
BufferInitTracker::new(aligned_size), |
| 729 |
), |
| 730 |
map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle), |
| 731 |
label: desc.label.to_string(), |
| 732 |
tracking_data: TrackingData::new(self.tracker_indices.buffers.clone()), |
| 733 |
bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, WeakVec::new()), |
| 734 |
timestamp_normalization_bind_group, |
| 735 |
indirect_validation_bind_groups, |
| 736 |
}; |
| 737 |
|
| 738 |
let buffer = Arc::new(buffer); |
| 739 |
|
| 740 |
let buffer_use = if !desc.mapped_at_creation { |
| 741 |
wgt::BufferUses::empty() |
| 742 |
} else if desc.usage.contains(wgt::BufferUsages::MAP_WRITE) { |
| 743 |
// buffer is mappable, so we are just doing that at start |
| 744 |
let map_size = buffer.size; |
| 745 |
let mapping = if map_size == 0 { |
| 746 |
hal::BufferMapping { |
| 747 |
ptr: core::ptr::NonNull::dangling(), |
| 748 |
is_coherent: true, |
| 749 |
} |
| 750 |
} else { |
| 751 |
let snatch_guard: SnatchGuard = self.snatchable_lock.read(); |
| 752 |
map_buffer(&buffer, 0, map_size, HostMap::Write, &snatch_guard)? |
| 753 |
}; |
| 754 |
*buffer.map_state.lock() = resource::BufferMapState::Active { |
| 755 |
mapping, |
| 756 |
range: 0..map_size, |
| 757 |
host: HostMap::Write, |
| 758 |
}; |
| 759 |
wgt::BufferUses::MAP_WRITE |
| 760 |
} else { |
| 761 |
let mut staging_buffer = |
| 762 |
StagingBuffer::new(self, wgt::BufferSize::new(aligned_size).unwrap())?; |
| 763 |
|
| 764 |
// Zero initialize memory and then mark the buffer as initialized |
| 765 |
// (it's guaranteed that this is the case by the time the buffer is usable) |
| 766 |
staging_buffer.write_zeros(); |
| 767 |
buffer.initialization_status.write().drain(0..aligned_size); |
| 768 |
|
| 769 |
*buffer.map_state.lock() = resource::BufferMapState::Init { staging_buffer }; |
| 770 |
wgt::BufferUses::COPY_DST |
| 771 |
}; |
| 772 |
|
| 773 |
self.trackers |
| 774 |
.lock() |
| 775 |
.buffers |
| 776 |
.insert_single(&buffer, buffer_use); |
| 777 |
|
| 778 |
Ok(buffer) |
| 779 |
} |
| 780 |
|
| 781 |
pub(crate) fn create_texture_from_hal( |
| 782 |
self: &Arc<Self>, |
| 783 |
hal_texture: Box<dyn hal::DynTexture>, |
| 784 |
desc: &resource::TextureDescriptor, |
| 785 |
) -> Result<Arc<Texture>, resource::CreateTextureError> { |
| 786 |
let format_features = self |
| 787 |
.describe_format_features(desc.format) |
| 788 |
.map_err(|error| resource::CreateTextureError::MissingFeatures(desc.format, error))?; |
| 789 |
|
| 790 |
unsafe { self.raw().add_raw_texture(&*hal_texture) }; |
| 791 |
|
| 792 |
let texture = Texture::new( |
| 793 |
self, |
| 794 |
resource::TextureInner::Native { raw: hal_texture }, |
| 795 |
conv::map_texture_usage(desc.usage, desc.format.into(), format_features.flags), |
| 796 |
desc, |
| 797 |
format_features, |
| 798 |
resource::TextureClearMode::None, |
| 799 |
false, |
| 800 |
); |
| 801 |
|
| 802 |
let texture = Arc::new(texture); |
| 803 |
|
| 804 |
self.trackers |
| 805 |
.lock() |
| 806 |
.textures |
| 807 |
.insert_single(&texture, wgt::TextureUses::UNINITIALIZED); |
| 808 |
|
| 809 |
Ok(texture) |
| 810 |
} |
| 811 |
|
| 812 |
pub(crate) fn create_buffer_from_hal( |
| 813 |
self: &Arc<Self>, |
| 814 |
hal_buffer: Box<dyn hal::DynBuffer>, |
| 815 |
desc: &resource::BufferDescriptor, |
| 816 |
) -> (Fallible<Buffer>, Option<resource::CreateBufferError>) { |
| 817 |
let timestamp_normalization_bind_group = match self |
| 818 |
.timestamp_normalizer |
| 819 |
.get() |
| 820 |
.unwrap() |
| 821 |
.create_normalization_bind_group( |
| 822 |
self, |
| 823 |
&*hal_buffer, |
| 824 |
desc.label.as_deref(), |
| 825 |
desc.size, |
| 826 |
desc.usage, |
| 827 |
) { |
| 828 |
Ok(bg) => Snatchable::new(bg), |
| 829 |
Err(e) => { |
| 830 |
return ( |
| 831 |
Fallible::Invalid(Arc::new(desc.label.to_string())), |
| 832 |
Some(e.into()), |
| 833 |
) |
| 834 |
} |
| 835 |
}; |
| 836 |
|
| 837 |
let indirect_validation_bind_groups = match self.create_indirect_validation_bind_groups( |
| 838 |
hal_buffer.as_ref(), |
| 839 |
desc.size, |
| 840 |
desc.usage, |
| 841 |
) { |
| 842 |
Ok(ok) => ok, |
| 843 |
Err(e) => return (Fallible::Invalid(Arc::new(desc.label.to_string())), Some(e)), |
| 844 |
}; |
| 845 |
|
| 846 |
unsafe { self.raw().add_raw_buffer(&*hal_buffer) }; |
| 847 |
|
| 848 |
let buffer = Buffer { |
| 849 |
raw: Snatchable::new(hal_buffer), |
| 850 |
device: self.clone(), |
| 851 |
usage: desc.usage, |
| 852 |
size: desc.size, |
| 853 |
initialization_status: RwLock::new( |
| 854 |
rank::BUFFER_INITIALIZATION_STATUS, |
| 855 |
BufferInitTracker::new(0), |
| 856 |
), |
| 857 |
map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle), |
| 858 |
label: desc.label.to_string(), |
| 859 |
tracking_data: TrackingData::new(self.tracker_indices.buffers.clone()), |
| 860 |
bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, WeakVec::new()), |
| 861 |
timestamp_normalization_bind_group, |
| 862 |
indirect_validation_bind_groups, |
| 863 |
}; |
| 864 |
|
| 865 |
let buffer = Arc::new(buffer); |
| 866 |
|
| 867 |
self.trackers |
| 868 |
.lock() |
| 869 |
.buffers |
| 870 |
.insert_single(&buffer, wgt::BufferUses::empty()); |
| 871 |
|
| 872 |
(Fallible::Valid(buffer), None) |
| 873 |
} |
| 874 |
|
| 875 |
fn create_indirect_validation_bind_groups( |
| 876 |
&self, |
| 877 |
raw_buffer: &dyn hal::DynBuffer, |
| 878 |
buffer_size: u64, |
| 879 |
usage: wgt::BufferUsages, |
| 880 |
) -> Result<Snatchable<crate::indirect_validation::BindGroups>, resource::CreateBufferError> |
| 881 |
{ |
| 882 |
if !usage.contains(wgt::BufferUsages::INDIRECT) { |
| 883 |
return Ok(Snatchable::empty()); |
| 884 |
} |
| 885 |
|
| 886 |
let Some(ref indirect_validation) = self.indirect_validation else { |
| 887 |
return Ok(Snatchable::empty()); |
| 888 |
}; |
| 889 |
|
| 890 |
let bind_groups = crate::indirect_validation::BindGroups::new( |
| 891 |
indirect_validation, |
| 892 |
self, |
| 893 |
buffer_size, |
| 894 |
raw_buffer, |
| 895 |
) |
| 896 |
.map_err(resource::CreateBufferError::IndirectValidationBindGroup)?; |
| 897 |
|
| 898 |
if let Some(bind_groups) = bind_groups { |
| 899 |
Ok(Snatchable::new(bind_groups)) |
| 900 |
} else { |
| 901 |
Ok(Snatchable::empty()) |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
pub(crate) fn create_texture( |
| 906 |
self: &Arc<Self>, |
| 907 |
desc: &resource::TextureDescriptor, |
| 908 |
) -> Result<Arc<Texture>, resource::CreateTextureError> { |
| 909 |
use resource::{CreateTextureError, TextureDimensionError}; |
| 910 |
|
| 911 |
self.check_is_valid()?; |
| 912 |
|
| 913 |
if desc.usage.is_empty() || desc.usage.contains_unknown_bits() { |
| 914 |
return Err(CreateTextureError::InvalidUsage(desc.usage)); |
| 915 |
} |
| 916 |
|
| 917 |
conv::check_texture_dimension_size( |
| 918 |
desc.dimension, |
| 919 |
desc.size, |
| 920 |
desc.sample_count, |
| 921 |
&self.limits, |
| 922 |
)?; |
| 923 |
|
| 924 |
if desc.dimension != wgt::TextureDimension::D2 { |
| 925 |
// Depth textures can only be 2D |
| 926 |
if desc.format.is_depth_stencil_format() { |
| 927 |
return Err(CreateTextureError::InvalidDepthDimension( |
| 928 |
desc.dimension, |
| 929 |
desc.format, |
| 930 |
)); |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
if desc.dimension != wgt::TextureDimension::D2 |
| 935 |
&& desc.dimension != wgt::TextureDimension::D3 |
| 936 |
{ |
| 937 |
// Compressed textures can only be 2D or 3D |
| 938 |
if desc.format.is_compressed() { |
| 939 |
return Err(CreateTextureError::InvalidCompressedDimension( |
| 940 |
desc.dimension, |
| 941 |
desc.format, |
| 942 |
)); |
| 943 |
} |
| 944 |
|
| 945 |
// Renderable textures can only be 2D or 3D |
| 946 |
if desc.usage.contains(wgt::TextureUsages::RENDER_ATTACHMENT) { |
| 947 |
return Err(CreateTextureError::InvalidDimensionUsages( |
| 948 |
wgt::TextureUsages::RENDER_ATTACHMENT, |
| 949 |
desc.dimension, |
| 950 |
)); |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
if desc.format.is_compressed() { |
| 955 |
let (block_width, block_height) = desc.format.block_dimensions(); |
| 956 |
|
| 957 |
if desc.size.width % block_width != 0 { |
| 958 |
return Err(CreateTextureError::InvalidDimension( |
| 959 |
TextureDimensionError::NotMultipleOfBlockWidth { |
| 960 |
width: desc.size.width, |
| 961 |
block_width, |
| 962 |
format: desc.format, |
| 963 |
}, |
| 964 |
)); |
| 965 |
} |
| 966 |
|
| 967 |
if desc.size.height % block_height != 0 { |
| 968 |
return Err(CreateTextureError::InvalidDimension( |
| 969 |
TextureDimensionError::NotMultipleOfBlockHeight { |
| 970 |
height: desc.size.height, |
| 971 |
block_height, |
| 972 |
format: desc.format, |
| 973 |
}, |
| 974 |
)); |
| 975 |
} |
| 976 |
|
| 977 |
if desc.dimension == wgt::TextureDimension::D3 { |
| 978 |
// Only BCn formats with Sliced 3D feature can be used for 3D textures |
| 979 |
if desc.format.is_bcn() { |
| 980 |
self.require_features(wgt::Features::TEXTURE_COMPRESSION_BC_SLICED_3D) |
| 981 |
.map_err(|error| CreateTextureError::MissingFeatures(desc.format, error))?; |
| 982 |
} else if desc.format.is_astc() { |
| 983 |
self.require_features(wgt::Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D) |
| 984 |
.map_err(|error| CreateTextureError::MissingFeatures(desc.format, error))?; |
| 985 |
} else { |
| 986 |
return Err(CreateTextureError::InvalidCompressedDimension( |
| 987 |
desc.dimension, |
| 988 |
desc.format, |
| 989 |
)); |
| 990 |
} |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
{ |
| 995 |
let (width_multiple, height_multiple) = desc.format.size_multiple_requirement(); |
| 996 |
|
| 997 |
if desc.size.width % width_multiple != 0 { |
| 998 |
return Err(CreateTextureError::InvalidDimension( |
| 999 |
TextureDimensionError::WidthNotMultipleOf { |
| 1000 |
width: desc.size.width, |
| 1001 |
multiple: width_multiple, |
| 1002 |
format: desc.format, |
| 1003 |
}, |
| 1004 |
)); |
| 1005 |
} |
| 1006 |
|
| 1007 |
if desc.size.height % height_multiple != 0 { |
| 1008 |
return Err(CreateTextureError::InvalidDimension( |
| 1009 |
TextureDimensionError::HeightNotMultipleOf { |
| 1010 |
height: desc.size.height, |
| 1011 |
multiple: height_multiple, |
| 1012 |
format: desc.format, |
| 1013 |
}, |
| 1014 |
)); |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
let format_features = self |
| 1019 |
.describe_format_features(desc.format) |
| 1020 |
.map_err(|error| CreateTextureError::MissingFeatures(desc.format, error))?; |
| 1021 |
|
| 1022 |
if desc.sample_count > 1 { |
| 1023 |
if desc.mip_level_count != 1 { |
| 1024 |
return Err(CreateTextureError::InvalidMipLevelCount { |
| 1025 |
requested: desc.mip_level_count, |
| 1026 |
maximum: 1, |
| 1027 |
}); |
| 1028 |
} |
| 1029 |
|
| 1030 |
if desc.size.depth_or_array_layers != 1 { |
| 1031 |
return Err(CreateTextureError::InvalidDimension( |
| 1032 |
TextureDimensionError::MultisampledDepthOrArrayLayer( |
| 1033 |
desc.size.depth_or_array_layers, |
| 1034 |
), |
| 1035 |
)); |
| 1036 |
} |
| 1037 |
|
| 1038 |
if desc.usage.contains(wgt::TextureUsages::STORAGE_BINDING) { |
| 1039 |
return Err(CreateTextureError::InvalidMultisampledStorageBinding); |
| 1040 |
} |
| 1041 |
|
| 1042 |
if !desc.usage.contains(wgt::TextureUsages::RENDER_ATTACHMENT) { |
| 1043 |
return Err(CreateTextureError::MultisampledNotRenderAttachment); |
| 1044 |
} |
| 1045 |
|
| 1046 |
if !format_features.flags.intersects( |
| 1047 |
wgt::TextureFormatFeatureFlags::MULTISAMPLE_X4 |
| 1048 |
| wgt::TextureFormatFeatureFlags::MULTISAMPLE_X2 |
| 1049 |
| wgt::TextureFormatFeatureFlags::MULTISAMPLE_X8 |
| 1050 |
| wgt::TextureFormatFeatureFlags::MULTISAMPLE_X16, |
| 1051 |
) { |
| 1052 |
return Err(CreateTextureError::InvalidMultisampledFormat(desc.format)); |
| 1053 |
} |
| 1054 |
|
| 1055 |
if !format_features |
| 1056 |
.flags |
| 1057 |
.sample_count_supported(desc.sample_count) |
| 1058 |
{ |
| 1059 |
return Err(CreateTextureError::InvalidSampleCount( |
| 1060 |
desc.sample_count, |
| 1061 |
desc.format, |
| 1062 |
desc.format |
| 1063 |
.guaranteed_format_features(self.features) |
| 1064 |
.flags |
| 1065 |
.supported_sample_counts(), |
| 1066 |
self.adapter |
| 1067 |
.get_texture_format_features(desc.format) |
| 1068 |
.flags |
| 1069 |
.supported_sample_counts(), |
| 1070 |
)); |
| 1071 |
}; |
| 1072 |
} |
| 1073 |
|
| 1074 |
let mips = desc.mip_level_count; |
| 1075 |
let max_levels_allowed = desc.size.max_mips(desc.dimension).min(hal::MAX_MIP_LEVELS); |
| 1076 |
if mips == 0 || mips > max_levels_allowed { |
| 1077 |
return Err(CreateTextureError::InvalidMipLevelCount { |
| 1078 |
requested: mips, |
| 1079 |
maximum: max_levels_allowed, |
| 1080 |
}); |
| 1081 |
} |
| 1082 |
|
| 1083 |
let missing_allowed_usages = desc.usage - format_features.allowed_usages; |
| 1084 |
if !missing_allowed_usages.is_empty() { |
| 1085 |
// detect downlevel incompatibilities |
| 1086 |
let wgpu_allowed_usages = desc |
| 1087 |
.format |
| 1088 |
.guaranteed_format_features(self.features) |
| 1089 |
.allowed_usages; |
| 1090 |
let wgpu_missing_usages = desc.usage - wgpu_allowed_usages; |
| 1091 |
return Err(CreateTextureError::InvalidFormatUsages( |
| 1092 |
missing_allowed_usages, |
| 1093 |
desc.format, |
| 1094 |
wgpu_missing_usages.is_empty(), |
| 1095 |
)); |
| 1096 |
} |
| 1097 |
|
| 1098 |
let mut hal_view_formats = Vec::new(); |
| 1099 |
for format in desc.view_formats.iter() { |
| 1100 |
if desc.format == *format { |
| 1101 |
continue; |
| 1102 |
} |
| 1103 |
if desc.format.remove_srgb_suffix() != format.remove_srgb_suffix() { |
| 1104 |
return Err(CreateTextureError::InvalidViewFormat(*format, desc.format)); |
| 1105 |
} |
| 1106 |
hal_view_formats.push(*format); |
| 1107 |
} |
| 1108 |
if !hal_view_formats.is_empty() { |
| 1109 |
self.require_downlevel_flags(wgt::DownlevelFlags::VIEW_FORMATS)?; |
| 1110 |
} |
| 1111 |
|
| 1112 |
let hal_usage = conv::map_texture_usage_for_texture(desc, &format_features); |
| 1113 |
|
| 1114 |
let hal_desc = hal::TextureDescriptor { |
| 1115 |
label: desc.label.to_hal(self.instance_flags), |
| 1116 |
size: desc.size, |
| 1117 |
mip_level_count: desc.mip_level_count, |
| 1118 |
sample_count: desc.sample_count, |
| 1119 |
dimension: desc.dimension, |
| 1120 |
format: desc.format, |
| 1121 |
usage: hal_usage, |
| 1122 |
memory_flags: hal::MemoryFlags::empty(), |
| 1123 |
view_formats: hal_view_formats, |
| 1124 |
}; |
| 1125 |
|
| 1126 |
let raw_texture = unsafe { self.raw().create_texture(&hal_desc) } |
| 1127 |
.map_err(|e| self.handle_hal_error_with_nonfatal_oom(e))?; |
| 1128 |
|
| 1129 |
let clear_mode = if hal_usage |
| 1130 |
.intersects(wgt::TextureUses::DEPTH_STENCIL_WRITE | wgt::TextureUses::COLOR_TARGET) |
| 1131 |
&& desc.dimension == wgt::TextureDimension::D2 |
| 1132 |
{ |
| 1133 |
let (is_color, usage) = if desc.format.is_depth_stencil_format() { |
| 1134 |
(false, wgt::TextureUses::DEPTH_STENCIL_WRITE) |
| 1135 |
} else { |
| 1136 |
(true, wgt::TextureUses::COLOR_TARGET) |
| 1137 |
}; |
| 1138 |
|
| 1139 |
let clear_label = hal_label( |
| 1140 |
Some("(wgpu internal) clear texture view"), |
| 1141 |
self.instance_flags, |
| 1142 |
); |
| 1143 |
|
| 1144 |
let mut clear_views = SmallVec::new(); |
| 1145 |
for mip_level in 0..desc.mip_level_count { |
| 1146 |
for array_layer in 0..desc.size.depth_or_array_layers { |
| 1147 |
macro_rules! push_clear_view { |
| 1148 |
($format:expr, $aspect:expr) => { |
| 1149 |
let desc = hal::TextureViewDescriptor { |
| 1150 |
label: clear_label, |
| 1151 |
format: $format, |
| 1152 |
dimension: TextureViewDimension::D2, |
| 1153 |
usage, |
| 1154 |
range: wgt::ImageSubresourceRange { |
| 1155 |
aspect: $aspect, |
| 1156 |
base_mip_level: mip_level, |
| 1157 |
mip_level_count: Some(1), |
| 1158 |
base_array_layer: array_layer, |
| 1159 |
array_layer_count: Some(1), |
| 1160 |
}, |
| 1161 |
}; |
| 1162 |
clear_views.push(ManuallyDrop::new( |
| 1163 |
unsafe { |
| 1164 |
self.raw().create_texture_view(raw_texture.as_ref(), &desc) |
| 1165 |
} |
| 1166 |
.map_err(|e| self.handle_hal_error(e))?, |
| 1167 |
)); |
| 1168 |
}; |
| 1169 |
} |
| 1170 |
|
| 1171 |
if let Some(planes) = desc.format.planes() { |
| 1172 |
for plane in 0..planes { |
| 1173 |
let aspect = wgt::TextureAspect::from_plane(plane).unwrap(); |
| 1174 |
let format = desc.format.aspect_specific_format(aspect).unwrap(); |
| 1175 |
push_clear_view!(format, aspect); |
| 1176 |
} |
| 1177 |
} else { |
| 1178 |
push_clear_view!(desc.format, wgt::TextureAspect::All); |
| 1179 |
} |
| 1180 |
} |
| 1181 |
} |
| 1182 |
resource::TextureClearMode::RenderPass { |
| 1183 |
clear_views, |
| 1184 |
is_color, |
| 1185 |
} |
| 1186 |
} else { |
| 1187 |
resource::TextureClearMode::BufferCopy |
| 1188 |
}; |
| 1189 |
|
| 1190 |
let texture = Texture::new( |
| 1191 |
self, |
| 1192 |
resource::TextureInner::Native { raw: raw_texture }, |
| 1193 |
hal_usage, |
| 1194 |
desc, |
| 1195 |
format_features, |
| 1196 |
clear_mode, |
| 1197 |
true, |
| 1198 |
); |
| 1199 |
|
| 1200 |
let texture = Arc::new(texture); |
| 1201 |
|
| 1202 |
self.trackers |
| 1203 |
.lock() |
| 1204 |
.textures |
| 1205 |
.insert_single(&texture, wgt::TextureUses::UNINITIALIZED); |
| 1206 |
|
| 1207 |
Ok(texture) |
| 1208 |
} |
| 1209 |
|
| 1210 |
pub(crate) fn create_texture_view( |
| 1211 |
self: &Arc<Self>, |
| 1212 |
texture: &Arc<Texture>, |
| 1213 |
desc: &resource::TextureViewDescriptor, |
| 1214 |
) -> Result<Arc<TextureView>, resource::CreateTextureViewError> { |
| 1215 |
self.check_is_valid()?; |
| 1216 |
|
| 1217 |
let snatch_guard = texture.device.snatchable_lock.read(); |
| 1218 |
|
| 1219 |
let texture_raw = texture.try_raw(&snatch_guard)?; |
| 1220 |
|
| 1221 |
// resolve TextureViewDescriptor defaults |
| 1222 |
// https://gpuweb.github.io/gpuweb/#abstract-opdef-resolving-gputextureviewdescriptor-defaults |
| 1223 |
let resolved_format = desc.format.unwrap_or_else(|| { |
| 1224 |
texture |
| 1225 |
.desc |
| 1226 |
.format |
| 1227 |
.aspect_specific_format(desc.range.aspect) |
| 1228 |
.unwrap_or(texture.desc.format) |
| 1229 |
}); |
| 1230 |
|
| 1231 |
let resolved_dimension = desc |
| 1232 |
.dimension |
| 1233 |
.unwrap_or_else(|| match texture.desc.dimension { |
| 1234 |
wgt::TextureDimension::D1 => TextureViewDimension::D1, |
| 1235 |
wgt::TextureDimension::D2 => { |
| 1236 |
if texture.desc.array_layer_count() == 1 { |
| 1237 |
TextureViewDimension::D2 |
| 1238 |
} else { |
| 1239 |
TextureViewDimension::D2Array |
| 1240 |
} |
| 1241 |
} |
| 1242 |
wgt::TextureDimension::D3 => TextureViewDimension::D3, |
| 1243 |
}); |
| 1244 |
|
| 1245 |
let resolved_mip_level_count = desc.range.mip_level_count.unwrap_or_else(|| { |
| 1246 |
texture |
| 1247 |
.desc |
| 1248 |
.mip_level_count |
| 1249 |
.saturating_sub(desc.range.base_mip_level) |
| 1250 |
}); |
| 1251 |
|
| 1252 |
let resolved_array_layer_count = |
| 1253 |
desc.range |
| 1254 |
.array_layer_count |
| 1255 |
.unwrap_or_else(|| match resolved_dimension { |
| 1256 |
TextureViewDimension::D1 |
| 1257 |
| TextureViewDimension::D2 |
| 1258 |
| TextureViewDimension::D3 => 1, |
| 1259 |
TextureViewDimension::Cube => 6, |
| 1260 |
TextureViewDimension::D2Array | TextureViewDimension::CubeArray => texture |
| 1261 |
.desc |
| 1262 |
.array_layer_count() |
| 1263 |
.saturating_sub(desc.range.base_array_layer), |
| 1264 |
}); |
| 1265 |
|
| 1266 |
let resolved_usage = { |
| 1267 |
let usage = desc.usage.unwrap_or(wgt::TextureUsages::empty()); |
| 1268 |
if usage.is_empty() { |
| 1269 |
texture.desc.usage |
| 1270 |
} else if texture.desc.usage.contains(usage) { |
| 1271 |
usage |
| 1272 |
} else { |
| 1273 |
return Err(resource::CreateTextureViewError::InvalidTextureViewUsage { |
| 1274 |
view: usage, |
| 1275 |
texture: texture.desc.usage, |
| 1276 |
}); |
| 1277 |
} |
| 1278 |
}; |
| 1279 |
|
| 1280 |
let format_features = self.describe_format_features(resolved_format)?; |
| 1281 |
let allowed_format_usages = format_features.allowed_usages; |
| 1282 |
if resolved_usage.contains(wgt::TextureUsages::RENDER_ATTACHMENT) |
| 1283 |
&& !allowed_format_usages.contains(wgt::TextureUsages::RENDER_ATTACHMENT) |
| 1284 |
{ |
| 1285 |
return Err( |
| 1286 |
resource::CreateTextureViewError::TextureViewFormatNotRenderable(resolved_format), |
| 1287 |
); |
| 1288 |
} |
| 1289 |
|
| 1290 |
if resolved_usage.contains(wgt::TextureUsages::STORAGE_BINDING) |
| 1291 |
&& !allowed_format_usages.contains(wgt::TextureUsages::STORAGE_BINDING) |
| 1292 |
{ |
| 1293 |
return Err( |
| 1294 |
resource::CreateTextureViewError::TextureViewFormatNotStorage(resolved_format), |
| 1295 |
); |
| 1296 |
} |
| 1297 |
|
| 1298 |
// validate TextureViewDescriptor |
| 1299 |
|
| 1300 |
let aspects = hal::FormatAspects::new(texture.desc.format, desc.range.aspect); |
| 1301 |
if aspects.is_empty() { |
| 1302 |
return Err(resource::CreateTextureViewError::InvalidAspect { |
| 1303 |
texture_format: texture.desc.format, |
| 1304 |
requested_aspect: desc.range.aspect, |
| 1305 |
}); |
| 1306 |
} |
| 1307 |
|
| 1308 |
let format_is_good = if desc.range.aspect == wgt::TextureAspect::All { |
| 1309 |
resolved_format == texture.desc.format |
| 1310 |
|| texture.desc.view_formats.contains(&resolved_format) |
| 1311 |
} else { |
| 1312 |
Some(resolved_format) |
| 1313 |
== texture |
| 1314 |
.desc |
| 1315 |
.format |
| 1316 |
.aspect_specific_format(desc.range.aspect) |
| 1317 |
}; |
| 1318 |
if !format_is_good { |
| 1319 |
return Err(resource::CreateTextureViewError::FormatReinterpretation { |
| 1320 |
texture: texture.desc.format, |
| 1321 |
view: resolved_format, |
| 1322 |
}); |
| 1323 |
} |
| 1324 |
|
| 1325 |
// check if multisampled texture is seen as anything but 2D |
| 1326 |
if texture.desc.sample_count > 1 && resolved_dimension != TextureViewDimension::D2 { |
| 1327 |
return Err( |
| 1328 |
resource::CreateTextureViewError::InvalidMultisampledTextureViewDimension( |
| 1329 |
resolved_dimension, |
| 1330 |
), |
| 1331 |
); |
| 1332 |
} |
| 1333 |
|
| 1334 |
// check if the dimension is compatible with the texture |
| 1335 |
if texture.desc.dimension != resolved_dimension.compatible_texture_dimension() { |
| 1336 |
return Err( |
| 1337 |
resource::CreateTextureViewError::InvalidTextureViewDimension { |
| 1338 |
view: resolved_dimension, |
| 1339 |
texture: texture.desc.dimension, |
| 1340 |
}, |
| 1341 |
); |
| 1342 |
} |
| 1343 |
|
| 1344 |
match resolved_dimension { |
| 1345 |
TextureViewDimension::D1 | TextureViewDimension::D2 | TextureViewDimension::D3 => { |
| 1346 |
if resolved_array_layer_count != 1 { |
| 1347 |
return Err(resource::CreateTextureViewError::InvalidArrayLayerCount { |
| 1348 |
requested: resolved_array_layer_count, |
| 1349 |
dim: resolved_dimension, |
| 1350 |
}); |
| 1351 |
} |
| 1352 |
} |
| 1353 |
TextureViewDimension::Cube => { |
| 1354 |
if resolved_array_layer_count != 6 { |
| 1355 |
return Err( |
| 1356 |
resource::CreateTextureViewError::InvalidCubemapTextureDepth { |
| 1357 |
depth: resolved_array_layer_count, |
| 1358 |
}, |
| 1359 |
); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
TextureViewDimension::CubeArray => { |
| 1363 |
if resolved_array_layer_count % 6 != 0 { |
| 1364 |
return Err( |
| 1365 |
resource::CreateTextureViewError::InvalidCubemapArrayTextureDepth { |
| 1366 |
depth: resolved_array_layer_count, |
| 1367 |
}, |
| 1368 |
); |
| 1369 |
} |
| 1370 |
} |
| 1371 |
_ => {} |
| 1372 |
} |
| 1373 |
|
| 1374 |
match resolved_dimension { |
| 1375 |
TextureViewDimension::Cube | TextureViewDimension::CubeArray => { |
| 1376 |
if texture.desc.size.width != texture.desc.size.height { |
| 1377 |
return Err(resource::CreateTextureViewError::InvalidCubeTextureViewSize); |
| 1378 |
} |
| 1379 |
} |
| 1380 |
_ => {} |
| 1381 |
} |
| 1382 |
|
| 1383 |
if resolved_mip_level_count == 0 { |
| 1384 |
return Err(resource::CreateTextureViewError::ZeroMipLevelCount); |
| 1385 |
} |
| 1386 |
|
| 1387 |
let mip_level_end = desc |
| 1388 |
.range |
| 1389 |
.base_mip_level |
| 1390 |
.saturating_add(resolved_mip_level_count); |
| 1391 |
|
| 1392 |
let level_end = texture.desc.mip_level_count; |
| 1393 |
if mip_level_end > level_end { |
| 1394 |
return Err(resource::CreateTextureViewError::TooManyMipLevels { |
| 1395 |
requested: mip_level_end, |
| 1396 |
total: level_end, |
| 1397 |
}); |
| 1398 |
} |
| 1399 |
|
| 1400 |
if resolved_array_layer_count == 0 { |
| 1401 |
return Err(resource::CreateTextureViewError::ZeroArrayLayerCount); |
| 1402 |
} |
| 1403 |
|
| 1404 |
let array_layer_end = desc |
| 1405 |
.range |
| 1406 |
.base_array_layer |
| 1407 |
.saturating_add(resolved_array_layer_count); |
| 1408 |
|
| 1409 |
let layer_end = texture.desc.array_layer_count(); |
| 1410 |
if array_layer_end > layer_end { |
| 1411 |
return Err(resource::CreateTextureViewError::TooManyArrayLayers { |
| 1412 |
requested: array_layer_end, |
| 1413 |
total: layer_end, |
| 1414 |
}); |
| 1415 |
}; |
| 1416 |
|
| 1417 |
// https://gpuweb.github.io/gpuweb/#abstract-opdef-renderable-texture-view |
| 1418 |
let render_extent = 'error: { |
| 1419 |
if !resolved_usage.contains(wgt::TextureUsages::RENDER_ATTACHMENT) { |
| 1420 |
break 'error Err(TextureViewNotRenderableReason::Usage(resolved_usage)); |
| 1421 |
} |
| 1422 |
|
| 1423 |
let allowed_view_dimensions = [ |
| 1424 |
TextureViewDimension::D2, |
| 1425 |
TextureViewDimension::D2Array, |
| 1426 |
TextureViewDimension::D3, |
| 1427 |
]; |
| 1428 |
if !allowed_view_dimensions.contains(&resolved_dimension) { |
| 1429 |
break 'error Err(TextureViewNotRenderableReason::Dimension( |
| 1430 |
resolved_dimension, |
| 1431 |
)); |
| 1432 |
} |
| 1433 |
|
| 1434 |
if resolved_mip_level_count != 1 { |
| 1435 |
break 'error Err(TextureViewNotRenderableReason::MipLevelCount( |
| 1436 |
resolved_mip_level_count, |
| 1437 |
)); |
| 1438 |
} |
| 1439 |
|
| 1440 |
if resolved_array_layer_count != 1 |
| 1441 |
&& !(self.features.contains(wgt::Features::MULTIVIEW)) |
| 1442 |
{ |
| 1443 |
break 'error Err(TextureViewNotRenderableReason::ArrayLayerCount( |
| 1444 |
resolved_array_layer_count, |
| 1445 |
)); |
| 1446 |
} |
| 1447 |
|
| 1448 |
if aspects != hal::FormatAspects::from(texture.desc.format) { |
| 1449 |
break 'error Err(TextureViewNotRenderableReason::Aspects(aspects)); |
| 1450 |
} |
| 1451 |
|
| 1452 |
Ok(texture |
| 1453 |
.desc |
| 1454 |
.compute_render_extent(desc.range.base_mip_level)) |
| 1455 |
}; |
| 1456 |
|
| 1457 |
// filter the usages based on the other criteria |
| 1458 |
let usage = { |
| 1459 |
let resolved_hal_usage = conv::map_texture_usage( |
| 1460 |
resolved_usage, |
| 1461 |
resolved_format.into(), |
| 1462 |
format_features.flags, |
| 1463 |
); |
| 1464 |
let mask_copy = !(wgt::TextureUses::COPY_SRC | wgt::TextureUses::COPY_DST); |
| 1465 |
let mask_dimension = match resolved_dimension { |
| 1466 |
TextureViewDimension::Cube | TextureViewDimension::CubeArray => { |
| 1467 |
wgt::TextureUses::RESOURCE |
| 1468 |
} |
| 1469 |
TextureViewDimension::D3 => { |
| 1470 |
wgt::TextureUses::RESOURCE |
| 1471 |
| wgt::TextureUses::STORAGE_READ_ONLY |
| 1472 |
| wgt::TextureUses::STORAGE_WRITE_ONLY |
| 1473 |
| wgt::TextureUses::STORAGE_READ_WRITE |
| 1474 |
} |
| 1475 |
_ => wgt::TextureUses::all(), |
| 1476 |
}; |
| 1477 |
let mask_mip_level = if resolved_mip_level_count == 1 { |
| 1478 |
wgt::TextureUses::all() |
| 1479 |
} else { |
| 1480 |
wgt::TextureUses::RESOURCE |
| 1481 |
}; |
| 1482 |
resolved_hal_usage & mask_copy & mask_dimension & mask_mip_level |
| 1483 |
}; |
| 1484 |
|
| 1485 |
// use the combined depth-stencil format for the view |
| 1486 |
let format = if resolved_format.is_depth_stencil_component(texture.desc.format) { |
| 1487 |
texture.desc.format |
| 1488 |
} else { |
| 1489 |
resolved_format |
| 1490 |
}; |
| 1491 |
|
| 1492 |
let resolved_range = wgt::ImageSubresourceRange { |
| 1493 |
aspect: desc.range.aspect, |
| 1494 |
base_mip_level: desc.range.base_mip_level, |
| 1495 |
mip_level_count: Some(resolved_mip_level_count), |
| 1496 |
base_array_layer: desc.range.base_array_layer, |
| 1497 |
array_layer_count: Some(resolved_array_layer_count), |
| 1498 |
}; |
| 1499 |
|
| 1500 |
let hal_desc = hal::TextureViewDescriptor { |
| 1501 |
label: desc.label.to_hal(self.instance_flags), |
| 1502 |
format, |
| 1503 |
dimension: resolved_dimension, |
| 1504 |
usage, |
| 1505 |
range: resolved_range, |
| 1506 |
}; |
| 1507 |
|
| 1508 |
let raw = unsafe { self.raw().create_texture_view(texture_raw, &hal_desc) } |
| 1509 |
.map_err(|e| self.handle_hal_error(e))?; |
| 1510 |
|
| 1511 |
let selector = TextureSelector { |
| 1512 |
mips: desc.range.base_mip_level..mip_level_end, |
| 1513 |
layers: desc.range.base_array_layer..array_layer_end, |
| 1514 |
}; |
| 1515 |
|
| 1516 |
let view = TextureView { |
| 1517 |
raw: Snatchable::new(raw), |
| 1518 |
parent: texture.clone(), |
| 1519 |
device: self.clone(), |
| 1520 |
desc: resource::HalTextureViewDescriptor { |
| 1521 |
texture_format: texture.desc.format, |
| 1522 |
format: resolved_format, |
| 1523 |
dimension: resolved_dimension, |
| 1524 |
usage: resolved_usage, |
| 1525 |
range: resolved_range, |
| 1526 |
}, |
| 1527 |
format_features: texture.format_features, |
| 1528 |
render_extent, |
| 1529 |
samples: texture.desc.sample_count, |
| 1530 |
selector, |
| 1531 |
label: desc.label.to_string(), |
| 1532 |
tracking_data: TrackingData::new(self.tracker_indices.texture_views.clone()), |
| 1533 |
}; |
| 1534 |
|
| 1535 |
let view = Arc::new(view); |
| 1536 |
|
| 1537 |
{ |
| 1538 |
let mut views = texture.views.lock(); |
| 1539 |
views.push(Arc::downgrade(&view)); |
| 1540 |
} |
| 1541 |
|
| 1542 |
Ok(view) |
| 1543 |
} |
| 1544 |
|
| 1545 |
pub(crate) fn create_sampler( |
| 1546 |
self: &Arc<Self>, |
| 1547 |
desc: &resource::SamplerDescriptor, |
| 1548 |
) -> Result<Arc<Sampler>, resource::CreateSamplerError> { |
| 1549 |
self.check_is_valid()?; |
| 1550 |
|
| 1551 |
if desc |
| 1552 |
.address_modes |
| 1553 |
.iter() |
| 1554 |
.any(|am| am == &wgt::AddressMode::ClampToBorder) |
| 1555 |
{ |
| 1556 |
self.require_features(wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER)?; |
| 1557 |
} |
| 1558 |
|
| 1559 |
if desc.border_color == Some(wgt::SamplerBorderColor::Zero) { |
| 1560 |
self.require_features(wgt::Features::ADDRESS_MODE_CLAMP_TO_ZERO)?; |
| 1561 |
} |
| 1562 |
|
| 1563 |
if desc.lod_min_clamp < 0.0 { |
| 1564 |
return Err(resource::CreateSamplerError::InvalidLodMinClamp( |
| 1565 |
desc.lod_min_clamp, |
| 1566 |
)); |
| 1567 |
} |
| 1568 |
if desc.lod_max_clamp < desc.lod_min_clamp { |
| 1569 |
return Err(resource::CreateSamplerError::InvalidLodMaxClamp { |
| 1570 |
lod_min_clamp: desc.lod_min_clamp, |
| 1571 |
lod_max_clamp: desc.lod_max_clamp, |
| 1572 |
}); |
| 1573 |
} |
| 1574 |
|
| 1575 |
if desc.anisotropy_clamp < 1 { |
| 1576 |
return Err(resource::CreateSamplerError::InvalidAnisotropy( |
| 1577 |
desc.anisotropy_clamp, |
| 1578 |
)); |
| 1579 |
} |
| 1580 |
|
| 1581 |
if desc.anisotropy_clamp != 1 { |
| 1582 |
if !matches!(desc.min_filter, wgt::FilterMode::Linear) { |
| 1583 |
return Err( |
| 1584 |
resource::CreateSamplerError::InvalidFilterModeWithAnisotropy { |
| 1585 |
filter_type: resource::SamplerFilterErrorType::MinFilter, |
| 1586 |
filter_mode: desc.min_filter, |
| 1587 |
anisotropic_clamp: desc.anisotropy_clamp, |
| 1588 |
}, |
| 1589 |
); |
| 1590 |
} |
| 1591 |
if !matches!(desc.mag_filter, wgt::FilterMode::Linear) { |
| 1592 |
return Err( |
| 1593 |
resource::CreateSamplerError::InvalidFilterModeWithAnisotropy { |
| 1594 |
filter_type: resource::SamplerFilterErrorType::MagFilter, |
| 1595 |
filter_mode: desc.mag_filter, |
| 1596 |
anisotropic_clamp: desc.anisotropy_clamp, |
| 1597 |
}, |
| 1598 |
); |
| 1599 |
} |
| 1600 |
if !matches!(desc.mipmap_filter, wgt::FilterMode::Linear) { |
| 1601 |
return Err( |
| 1602 |
resource::CreateSamplerError::InvalidFilterModeWithAnisotropy { |
| 1603 |
filter_type: resource::SamplerFilterErrorType::MipmapFilter, |
| 1604 |
filter_mode: desc.mipmap_filter, |
| 1605 |
anisotropic_clamp: desc.anisotropy_clamp, |
| 1606 |
}, |
| 1607 |
); |
| 1608 |
} |
| 1609 |
} |
| 1610 |
|
| 1611 |
let anisotropy_clamp = if self |
| 1612 |
.downlevel |
| 1613 |
.flags |
| 1614 |
.contains(wgt::DownlevelFlags::ANISOTROPIC_FILTERING) |
| 1615 |
{ |
| 1616 |
// Clamp anisotropy clamp to [1, 16] per the wgpu-hal interface |
| 1617 |
desc.anisotropy_clamp.min(16) |
| 1618 |
} else { |
| 1619 |
// If it isn't supported, set this unconditionally to 1 |
| 1620 |
1 |
| 1621 |
}; |
| 1622 |
|
| 1623 |
//TODO: check for wgt::DownlevelFlags::COMPARISON_SAMPLERS |
| 1624 |
|
| 1625 |
let hal_desc = hal::SamplerDescriptor { |
| 1626 |
label: desc.label.to_hal(self.instance_flags), |
| 1627 |
address_modes: desc.address_modes, |
| 1628 |
mag_filter: desc.mag_filter, |
| 1629 |
min_filter: desc.min_filter, |
| 1630 |
mipmap_filter: desc.mipmap_filter, |
| 1631 |
lod_clamp: desc.lod_min_clamp..desc.lod_max_clamp, |
| 1632 |
compare: desc.compare, |
| 1633 |
anisotropy_clamp, |
| 1634 |
border_color: desc.border_color, |
| 1635 |
}; |
| 1636 |
|
| 1637 |
let raw = unsafe { self.raw().create_sampler(&hal_desc) } |
| 1638 |
.map_err(|e| self.handle_hal_error_with_nonfatal_oom(e))?; |
| 1639 |
|
| 1640 |
let sampler = Sampler { |
| 1641 |
raw: ManuallyDrop::new(raw), |
| 1642 |
device: self.clone(), |
| 1643 |
label: desc.label.to_string(), |
| 1644 |
tracking_data: TrackingData::new(self.tracker_indices.samplers.clone()), |
| 1645 |
comparison: desc.compare.is_some(), |
| 1646 |
filtering: desc.min_filter == wgt::FilterMode::Linear |
| 1647 |
|| desc.mag_filter == wgt::FilterMode::Linear |
| 1648 |
|| desc.mipmap_filter == wgt::FilterMode::Linear, |
| 1649 |
}; |
| 1650 |
|
| 1651 |
let sampler = Arc::new(sampler); |
| 1652 |
|
| 1653 |
Ok(sampler) |
| 1654 |
} |
| 1655 |
|
| 1656 |
pub(crate) fn create_shader_module<'a>( |
| 1657 |
self: &Arc<Self>, |
| 1658 |
desc: &pipeline::ShaderModuleDescriptor<'a>, |
| 1659 |
source: pipeline::ShaderModuleSource<'a>, |
| 1660 |
) -> Result<Arc<pipeline::ShaderModule>, pipeline::CreateShaderModuleError> { |
| 1661 |
self.check_is_valid()?; |
| 1662 |
|
| 1663 |
let (module, source) = match source { |
| 1664 |
#[cfg(feature = "wgsl")] |
| 1665 |
pipeline::ShaderModuleSource::Wgsl(code) => { |
| 1666 |
profiling::scope!("naga::front::wgsl::parse_str"); |
| 1667 |
let module = naga::front::wgsl::parse_str(&code).map_err(|inner| { |
| 1668 |
pipeline::CreateShaderModuleError::Parsing(naga::error::ShaderError { |
| 1669 |
source: code.to_string(), |
| 1670 |
label: desc.label.as_ref().map(|l| l.to_string()), |
| 1671 |
inner: Box::new(inner), |
| 1672 |
}) |
| 1673 |
})?; |
| 1674 |
(Cow::Owned(module), code.into_owned()) |
| 1675 |
} |
| 1676 |
#[cfg(feature = "spirv")] |
| 1677 |
pipeline::ShaderModuleSource::SpirV(spv, options) => { |
| 1678 |
let parser = naga::front::spv::Frontend::new(spv.iter().cloned(), &options); |
| 1679 |
profiling::scope!("naga::front::spv::Frontend"); |
| 1680 |
let module = parser.parse().map_err(|inner| { |
| 1681 |
pipeline::CreateShaderModuleError::ParsingSpirV(naga::error::ShaderError { |
| 1682 |
source: String::new(), |
| 1683 |
label: desc.label.as_ref().map(|l| l.to_string()), |
| 1684 |
inner: Box::new(inner), |
| 1685 |
}) |
| 1686 |
})?; |
| 1687 |
(Cow::Owned(module), String::new()) |
| 1688 |
} |
| 1689 |
#[cfg(feature = "glsl")] |
| 1690 |
pipeline::ShaderModuleSource::Glsl(code, options) => { |
| 1691 |
let mut parser = naga::front::glsl::Frontend::default(); |
| 1692 |
profiling::scope!("naga::front::glsl::Frontend.parse"); |
| 1693 |
let module = parser.parse(&options, &code).map_err(|inner| { |
| 1694 |
pipeline::CreateShaderModuleError::ParsingGlsl(naga::error::ShaderError { |
| 1695 |
source: code.to_string(), |
| 1696 |
label: desc.label.as_ref().map(|l| l.to_string()), |
| 1697 |
inner: Box::new(inner), |
| 1698 |
}) |
| 1699 |
})?; |
| 1700 |
(Cow::Owned(module), code.into_owned()) |
| 1701 |
} |
| 1702 |
pipeline::ShaderModuleSource::Naga(module) => (module, String::new()), |
| 1703 |
pipeline::ShaderModuleSource::Dummy(_) => panic!("found `ShaderModuleSource::Dummy`"), |
| 1704 |
}; |
| 1705 |
for (_, var) in module.global_variables.iter() { |
| 1706 |
match var.binding { |
| 1707 |
Some(br) if br.group >= self.limits.max_bind_groups => { |
| 1708 |
return Err(pipeline::CreateShaderModuleError::InvalidGroupIndex { |
| 1709 |
bind: br, |
| 1710 |
group: br.group, |
| 1711 |
limit: self.limits.max_bind_groups, |
| 1712 |
}); |
| 1713 |
} |
| 1714 |
_ => continue, |
| 1715 |
}; |
| 1716 |
} |
| 1717 |
|
| 1718 |
profiling::scope!("naga::validate"); |
| 1719 |
let debug_source = |
| 1720 |
if self.instance_flags.contains(wgt::InstanceFlags::DEBUG) && !source.is_empty() { |
| 1721 |
Some(hal::DebugSource { |
| 1722 |
file_name: Cow::Owned( |
| 1723 |
desc.label |
| 1724 |
.as_ref() |
| 1725 |
.map_or("shader".to_string(), |l| l.to_string()), |
| 1726 |
), |
| 1727 |
source_code: Cow::Owned(source.clone()), |
| 1728 |
}) |
| 1729 |
} else { |
| 1730 |
None |
| 1731 |
}; |
| 1732 |
|
| 1733 |
let info = create_validator( |
| 1734 |
self.features, |
| 1735 |
self.downlevel.flags, |
| 1736 |
naga::valid::ValidationFlags::all(), |
| 1737 |
) |
| 1738 |
.validate(&module) |
| 1739 |
.map_err(|inner| { |
| 1740 |
pipeline::CreateShaderModuleError::Validation(naga::error::ShaderError { |
| 1741 |
source, |
| 1742 |
label: desc.label.as_ref().map(|l| l.to_string()), |
| 1743 |
inner: Box::new(inner), |
| 1744 |
}) |
| 1745 |
})?; |
| 1746 |
|
| 1747 |
let interface = validation::Interface::new(&module, &info, self.limits.clone()); |
| 1748 |
let hal_shader = hal::ShaderInput::Naga(hal::NagaShader { |
| 1749 |
module, |
| 1750 |
info, |
| 1751 |
debug_source, |
| 1752 |
}); |
| 1753 |
let hal_desc = hal::ShaderModuleDescriptor { |
| 1754 |
label: desc.label.to_hal(self.instance_flags), |
| 1755 |
runtime_checks: desc.runtime_checks, |
| 1756 |
}; |
| 1757 |
let raw = match unsafe { self.raw().create_shader_module(&hal_desc, hal_shader) } { |
| 1758 |
Ok(raw) => raw, |
| 1759 |
Err(error) => { |
| 1760 |
return Err(match error { |
| 1761 |
hal::ShaderError::Device(error) => { |
| 1762 |
pipeline::CreateShaderModuleError::Device(self.handle_hal_error(error)) |
| 1763 |
} |
| 1764 |
hal::ShaderError::Compilation(ref msg) => { |
| 1765 |
log::error!("Shader error: {}", msg); |
| 1766 |
pipeline::CreateShaderModuleError::Generation |
| 1767 |
} |
| 1768 |
}) |
| 1769 |
} |
| 1770 |
}; |
| 1771 |
|
| 1772 |
let module = pipeline::ShaderModule { |
| 1773 |
raw: ManuallyDrop::new(raw), |
| 1774 |
device: self.clone(), |
| 1775 |
interface: Some(interface), |
| 1776 |
label: desc.label.to_string(), |
| 1777 |
}; |
| 1778 |
|
| 1779 |
let module = Arc::new(module); |
| 1780 |
|
| 1781 |
Ok(module) |
| 1782 |
} |
| 1783 |
|
| 1784 |
#[allow(unused_unsafe)] |
| 1785 |
pub(crate) unsafe fn create_shader_module_passthrough<'a>( |
| 1786 |
self: &Arc<Self>, |
| 1787 |
descriptor: &pipeline::ShaderModuleDescriptorPassthrough<'a>, |
| 1788 |
) -> Result<Arc<pipeline::ShaderModule>, pipeline::CreateShaderModuleError> { |
| 1789 |
self.check_is_valid()?; |
| 1790 |
let hal_shader = match descriptor { |
| 1791 |
pipeline::ShaderModuleDescriptorPassthrough::SpirV(inner) => { |
| 1792 |
self.require_features(wgt::Features::SPIRV_SHADER_PASSTHROUGH)?; |
| 1793 |
hal::ShaderInput::SpirV(&inner.source) |
| 1794 |
} |
| 1795 |
pipeline::ShaderModuleDescriptorPassthrough::Msl(inner) => { |
| 1796 |
self.require_features(wgt::Features::MSL_SHADER_PASSTHROUGH)?; |
| 1797 |
hal::ShaderInput::Msl { |
| 1798 |
shader: inner.source.to_string(), |
| 1799 |
entry_point: inner.entry_point.to_string(), |
| 1800 |
num_workgroups: inner.num_workgroups, |
| 1801 |
} |
| 1802 |
} |
| 1803 |
}; |
| 1804 |
|
| 1805 |
let hal_desc = hal::ShaderModuleDescriptor { |
| 1806 |
label: descriptor.label().to_hal(self.instance_flags), |
| 1807 |
runtime_checks: wgt::ShaderRuntimeChecks::unchecked(), |
| 1808 |
}; |
| 1809 |
|
| 1810 |
let raw = match unsafe { self.raw().create_shader_module(&hal_desc, hal_shader) } { |
| 1811 |
Ok(raw) => raw, |
| 1812 |
Err(error) => { |
| 1813 |
return Err(match error { |
| 1814 |
hal::ShaderError::Device(error) => { |
| 1815 |
pipeline::CreateShaderModuleError::Device(self.handle_hal_error(error)) |
| 1816 |
} |
| 1817 |
hal::ShaderError::Compilation(ref msg) => { |
| 1818 |
log::error!("Shader error: {}", msg); |
| 1819 |
pipeline::CreateShaderModuleError::Generation |
| 1820 |
} |
| 1821 |
}) |
| 1822 |
} |
| 1823 |
}; |
| 1824 |
|
| 1825 |
let module = pipeline::ShaderModule { |
| 1826 |
raw: ManuallyDrop::new(raw), |
| 1827 |
device: self.clone(), |
| 1828 |
interface: None, |
| 1829 |
label: descriptor.label().to_string(), |
| 1830 |
}; |
| 1831 |
|
| 1832 |
Ok(Arc::new(module)) |
| 1833 |
} |
| 1834 |
|
| 1835 |
pub(crate) fn create_command_encoder( |
| 1836 |
self: &Arc<Self>, |
| 1837 |
label: &crate::Label, |
| 1838 |
) -> Result<Arc<command::CommandBuffer>, DeviceError> { |
| 1839 |
self.check_is_valid()?; |
| 1840 |
|
| 1841 |
let queue = self.get_queue().unwrap(); |
| 1842 |
|
| 1843 |
let encoder = self |
| 1844 |
.command_allocator |
| 1845 |
.acquire_encoder(self.raw(), queue.raw()) |
| 1846 |
.map_err(|e| self.handle_hal_error(e))?; |
| 1847 |
|
| 1848 |
let command_buffer = command::CommandBuffer::new(encoder, self, label); |
| 1849 |
|
| 1850 |
let command_buffer = Arc::new(command_buffer); |
| 1851 |
|
| 1852 |
Ok(command_buffer) |
| 1853 |
} |
| 1854 |
|
| 1855 |
/// Generate information about late-validated buffer bindings for pipelines. |
| 1856 |
//TODO: should this be combined with `get_introspection_bind_group_layouts` in some way? |
| 1857 |
fn make_late_sized_buffer_groups( |
| 1858 |
shader_binding_sizes: &FastHashMap<naga::ResourceBinding, wgt::BufferSize>, |
| 1859 |
layout: &binding_model::PipelineLayout, |
| 1860 |
) -> ArrayVec<pipeline::LateSizedBufferGroup, { hal::MAX_BIND_GROUPS }> { |
| 1861 |
// Given the shader-required binding sizes and the pipeline layout, |
| 1862 |
// return the filtered list of them in the layout order, |
| 1863 |
// removing those with given `min_binding_size`. |
| 1864 |
layout |
| 1865 |
.bind_group_layouts |
| 1866 |
.iter() |
| 1867 |
.enumerate() |
| 1868 |
.map(|(group_index, bgl)| pipeline::LateSizedBufferGroup { |
| 1869 |
shader_sizes: bgl |
| 1870 |
.entries |
| 1871 |
.values() |
| 1872 |
.filter_map(|entry| match entry.ty { |
| 1873 |
wgt::BindingType::Buffer { |
| 1874 |
min_binding_size: None, |
| 1875 |
.. |
| 1876 |
} => { |
| 1877 |
let rb = naga::ResourceBinding { |
| 1878 |
group: group_index as u32, |
| 1879 |
binding: entry.binding, |
| 1880 |
}; |
| 1881 |
let shader_size = |
| 1882 |
shader_binding_sizes.get(&rb).map_or(0, |nz| nz.get()); |
| 1883 |
Some(shader_size) |
| 1884 |
} |
| 1885 |
_ => None, |
| 1886 |
}) |
| 1887 |
.collect(), |
| 1888 |
}) |
| 1889 |
.collect() |
| 1890 |
} |
| 1891 |
|
| 1892 |
pub(crate) fn create_bind_group_layout( |
| 1893 |
self: &Arc<Self>, |
| 1894 |
label: &crate::Label, |
| 1895 |
entry_map: bgl::EntryMap, |
| 1896 |
origin: bgl::Origin, |
| 1897 |
) -> Result<Arc<BindGroupLayout>, binding_model::CreateBindGroupLayoutError> { |
| 1898 |
#[derive(PartialEq)] |
| 1899 |
enum WritableStorage { |
| 1900 |
Yes, |
| 1901 |
No, |
| 1902 |
} |
| 1903 |
|
| 1904 |
for entry in entry_map.values() { |
| 1905 |
use wgt::BindingType as Bt; |
| 1906 |
|
| 1907 |
let mut required_features = wgt::Features::empty(); |
| 1908 |
let mut required_downlevel_flags = wgt::DownlevelFlags::empty(); |
| 1909 |
let (array_feature, writable_storage) = match entry.ty { |
| 1910 |
Bt::Buffer { |
| 1911 |
ty: wgt::BufferBindingType::Uniform, |
| 1912 |
has_dynamic_offset: false, |
| 1913 |
min_binding_size: _, |
| 1914 |
} => ( |
| 1915 |
Some(wgt::Features::BUFFER_BINDING_ARRAY), |
| 1916 |
WritableStorage::No, |
| 1917 |
), |
| 1918 |
Bt::Buffer { |
| 1919 |
ty: wgt::BufferBindingType::Uniform, |
| 1920 |
has_dynamic_offset: true, |
| 1921 |
min_binding_size: _, |
| 1922 |
} => ( |
| 1923 |
Some(wgt::Features::BUFFER_BINDING_ARRAY), |
| 1924 |
WritableStorage::No, |
| 1925 |
), |
| 1926 |
Bt::Buffer { |
| 1927 |
ty: wgt::BufferBindingType::Storage { read_only }, |
| 1928 |
.. |
| 1929 |
} => ( |
| 1930 |
Some( |
| 1931 |
wgt::Features::BUFFER_BINDING_ARRAY |
| 1932 |
| wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY, |
| 1933 |
), |
| 1934 |
match read_only { |
| 1935 |
true => WritableStorage::No, |
| 1936 |
false => WritableStorage::Yes, |
| 1937 |
}, |
| 1938 |
), |
| 1939 |
Bt::Sampler { .. } => ( |
| 1940 |
Some(wgt::Features::TEXTURE_BINDING_ARRAY), |
| 1941 |
WritableStorage::No, |
| 1942 |
), |
| 1943 |
Bt::Texture { |
| 1944 |
multisampled: true, |
| 1945 |
sample_type: TextureSampleType::Float { filterable: true }, |
| 1946 |
.. |
| 1947 |
} => { |
| 1948 |
return Err(binding_model::CreateBindGroupLayoutError::Entry { |
| 1949 |
binding: entry.binding, |
| 1950 |
error: |
| 1951 |
BindGroupLayoutEntryError::SampleTypeFloatFilterableBindingMultisampled, |
| 1952 |
}); |
| 1953 |
} |
| 1954 |
Bt::Texture { |
| 1955 |
multisampled, |
| 1956 |
view_dimension, |
| 1957 |
.. |
| 1958 |
} => { |
| 1959 |
if multisampled && view_dimension != TextureViewDimension::D2 { |
| 1960 |
return Err(binding_model::CreateBindGroupLayoutError::Entry { |
| 1961 |
binding: entry.binding, |
| 1962 |
error: BindGroupLayoutEntryError::Non2DMultisampled(view_dimension), |
| 1963 |
}); |
| 1964 |
} |
| 1965 |
|
| 1966 |
( |
| 1967 |
Some(wgt::Features::TEXTURE_BINDING_ARRAY), |
| 1968 |
WritableStorage::No, |
| 1969 |
) |
| 1970 |
} |
| 1971 |
Bt::StorageTexture { |
| 1972 |
access, |
| 1973 |
view_dimension, |
| 1974 |
format: _, |
| 1975 |
} => { |
| 1976 |
match view_dimension { |
| 1977 |
TextureViewDimension::Cube | TextureViewDimension::CubeArray => { |
| 1978 |
return Err(binding_model::CreateBindGroupLayoutError::Entry { |
| 1979 |
binding: entry.binding, |
| 1980 |
error: BindGroupLayoutEntryError::StorageTextureCube, |
| 1981 |
}) |
| 1982 |
} |
| 1983 |
_ => (), |
| 1984 |
} |
| 1985 |
match access { |
| 1986 |
wgt::StorageTextureAccess::Atomic |
| 1987 |
if !self.features.contains(wgt::Features::TEXTURE_ATOMIC) => |
| 1988 |
{ |
| 1989 |
return Err(binding_model::CreateBindGroupLayoutError::Entry { |
| 1990 |
binding: entry.binding, |
| 1991 |
error: BindGroupLayoutEntryError::StorageTextureAtomic, |
| 1992 |
}); |
| 1993 |
} |
| 1994 |
_ => (), |
| 1995 |
} |
| 1996 |
( |
| 1997 |
Some( |
| 1998 |
wgt::Features::TEXTURE_BINDING_ARRAY |
| 1999 |
| wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY, |
| 2000 |
), |
| 2001 |
match access { |
| 2002 |
wgt::StorageTextureAccess::WriteOnly => WritableStorage::Yes, |
| 2003 |
wgt::StorageTextureAccess::ReadOnly => WritableStorage::No, |
| 2004 |
wgt::StorageTextureAccess::ReadWrite => WritableStorage::Yes, |
| 2005 |
wgt::StorageTextureAccess::Atomic => { |
| 2006 |
required_features |= wgt::Features::TEXTURE_ATOMIC; |
| 2007 |
WritableStorage::Yes |
| 2008 |
} |
| 2009 |
}, |
| 2010 |
) |
| 2011 |
} |
| 2012 |
Bt::AccelerationStructure { .. } => (None, WritableStorage::No), |
| 2013 |
}; |
| 2014 |
|
| 2015 |
// Validate the count parameter |
| 2016 |
if entry.count.is_some() { |
| 2017 |
required_features |= array_feature |
| 2018 |
.ok_or(BindGroupLayoutEntryError::ArrayUnsupported) |
| 2019 |
.map_err(|error| binding_model::CreateBindGroupLayoutError::Entry { |
| 2020 |
binding: entry.binding, |
| 2021 |
error, |
| 2022 |
})?; |
| 2023 |
} |
| 2024 |
|
| 2025 |
if entry.visibility.contains_unknown_bits() { |
| 2026 |
return Err( |
| 2027 |
binding_model::CreateBindGroupLayoutError::InvalidVisibility(entry.visibility), |
| 2028 |
); |
| 2029 |
} |
| 2030 |
|
| 2031 |
if entry.visibility.contains(wgt::ShaderStages::VERTEX) { |
| 2032 |
if writable_storage == WritableStorage::Yes { |
| 2033 |
required_features |= wgt::Features::VERTEX_WRITABLE_STORAGE; |
| 2034 |
} |
| 2035 |
if let Bt::Buffer { |
| 2036 |
ty: wgt::BufferBindingType::Storage { .. }, |
| 2037 |
.. |
| 2038 |
} = entry.ty |
| 2039 |
{ |
| 2040 |
required_downlevel_flags |= wgt::DownlevelFlags::VERTEX_STORAGE; |
| 2041 |
} |
| 2042 |
} |
| 2043 |
if writable_storage == WritableStorage::Yes |
| 2044 |
&& entry.visibility.contains(wgt::ShaderStages::FRAGMENT) |
| 2045 |
{ |
| 2046 |
required_downlevel_flags |= wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE; |
| 2047 |
} |
| 2048 |
|
| 2049 |
self.require_features(required_features) |
| 2050 |
.map_err(BindGroupLayoutEntryError::MissingFeatures) |
| 2051 |
.map_err(|error| binding_model::CreateBindGroupLayoutError::Entry { |
| 2052 |
binding: entry.binding, |
| 2053 |
error, |
| 2054 |
})?; |
| 2055 |
self.require_downlevel_flags(required_downlevel_flags) |
| 2056 |
.map_err(BindGroupLayoutEntryError::MissingDownlevelFlags) |
| 2057 |
.map_err(|error| binding_model::CreateBindGroupLayoutError::Entry { |
| 2058 |
binding: entry.binding, |
| 2059 |
error, |
| 2060 |
})?; |
| 2061 |
} |
| 2062 |
|
| 2063 |
let bgl_flags = conv::bind_group_layout_flags(self.features); |
| 2064 |
|
| 2065 |
let hal_bindings = entry_map.values().copied().collect::<Vec<_>>(); |
| 2066 |
let hal_desc = hal::BindGroupLayoutDescriptor { |
| 2067 |
label: label.to_hal(self.instance_flags), |
| 2068 |
flags: bgl_flags, |
| 2069 |
entries: &hal_bindings, |
| 2070 |
}; |
| 2071 |
|
| 2072 |
let mut count_validator = binding_model::BindingTypeMaxCountValidator::default(); |
| 2073 |
for entry in entry_map.values() { |
| 2074 |
count_validator.add_binding(entry); |
| 2075 |
} |
| 2076 |
// If a single bind group layout violates limits, the pipeline layout is |
| 2077 |
// definitely going to violate limits too, lets catch it now. |
| 2078 |
count_validator |
| 2079 |
.validate(&self.limits) |
| 2080 |
.map_err(binding_model::CreateBindGroupLayoutError::TooManyBindings)?; |
| 2081 |
|
| 2082 |
// Validate that binding arrays don't conflict with dynamic offsets. |
| 2083 |
count_validator.validate_binding_arrays()?; |
| 2084 |
|
| 2085 |
let raw = unsafe { self.raw().create_bind_group_layout(&hal_desc) } |
| 2086 |
.map_err(|e| self.handle_hal_error(e))?; |
| 2087 |
|
| 2088 |
let bgl = BindGroupLayout { |
| 2089 |
raw: ManuallyDrop::new(raw), |
| 2090 |
device: self.clone(), |
| 2091 |
entries: entry_map, |
| 2092 |
origin, |
| 2093 |
exclusive_pipeline: OnceCellOrLock::new(), |
| 2094 |
binding_count_validator: count_validator, |
| 2095 |
label: label.to_string(), |
| 2096 |
}; |
| 2097 |
|
| 2098 |
let bgl = Arc::new(bgl); |
| 2099 |
|
| 2100 |
Ok(bgl) |
| 2101 |
} |
| 2102 |
|
| 2103 |
fn create_buffer_binding<'a>( |
| 2104 |
&self, |
| 2105 |
bb: &'a binding_model::ResolvedBufferBinding, |
| 2106 |
binding: u32, |
| 2107 |
decl: &wgt::BindGroupLayoutEntry, |
| 2108 |
used_buffer_ranges: &mut Vec<BufferInitTrackerAction>, |
| 2109 |
dynamic_binding_info: &mut Vec<binding_model::BindGroupDynamicBindingData>, |
| 2110 |
late_buffer_binding_sizes: &mut FastHashMap<u32, wgt::BufferSize>, |
| 2111 |
used: &mut BindGroupStates, |
| 2112 |
snatch_guard: &'a SnatchGuard<'a>, |
| 2113 |
) -> Result<hal::BufferBinding<'a, dyn hal::DynBuffer>, binding_model::CreateBindGroupError> |
| 2114 |
{ |
| 2115 |
use crate::binding_model::CreateBindGroupError as Error; |
| 2116 |
|
| 2117 |
let (binding_ty, dynamic, min_size) = match decl.ty { |
| 2118 |
wgt::BindingType::Buffer { |
| 2119 |
ty, |
| 2120 |
has_dynamic_offset, |
| 2121 |
min_binding_size, |
| 2122 |
} => (ty, has_dynamic_offset, min_binding_size), |
| 2123 |
_ => { |
| 2124 |
return Err(Error::WrongBindingType { |
| 2125 |
binding, |
| 2126 |
actual: decl.ty, |
| 2127 |
expected: "UniformBuffer, StorageBuffer or ReadonlyStorageBuffer", |
| 2128 |
}) |
| 2129 |
} |
| 2130 |
}; |
| 2131 |
|
| 2132 |
let (pub_usage, internal_use, range_limit) = match binding_ty { |
| 2133 |
wgt::BufferBindingType::Uniform => ( |
| 2134 |
wgt::BufferUsages::UNIFORM, |
| 2135 |
wgt::BufferUses::UNIFORM, |
| 2136 |
self.limits.max_uniform_buffer_binding_size, |
| 2137 |
), |
| 2138 |
wgt::BufferBindingType::Storage { read_only } => ( |
| 2139 |
wgt::BufferUsages::STORAGE, |
| 2140 |
if read_only { |
| 2141 |
wgt::BufferUses::STORAGE_READ_ONLY |
| 2142 |
} else { |
| 2143 |
wgt::BufferUses::STORAGE_READ_WRITE |
| 2144 |
}, |
| 2145 |
self.limits.max_storage_buffer_binding_size, |
| 2146 |
), |
| 2147 |
}; |
| 2148 |
|
| 2149 |
let (align, align_limit_name) = |
| 2150 |
binding_model::buffer_binding_type_alignment(&self.limits, binding_ty); |
| 2151 |
if bb.offset % align as u64 != 0 { |
| 2152 |
return Err(Error::UnalignedBufferOffset( |
| 2153 |
bb.offset, |
| 2154 |
align_limit_name, |
| 2155 |
align, |
| 2156 |
)); |
| 2157 |
} |
| 2158 |
|
| 2159 |
let buffer = &bb.buffer; |
| 2160 |
|
| 2161 |
used.buffers.insert_single(buffer.clone(), internal_use); |
| 2162 |
|
| 2163 |
buffer.same_device(self)?; |
| 2164 |
|
| 2165 |
buffer.check_usage(pub_usage)?; |
| 2166 |
let raw_buffer = buffer.try_raw(snatch_guard)?; |
| 2167 |
|
| 2168 |
let (bind_size, bind_end) = match bb.size { |
| 2169 |
Some(size) => { |
| 2170 |
let end = bb.offset + size.get(); |
| 2171 |
if end > buffer.size { |
| 2172 |
return Err(Error::BindingRangeTooLarge { |
| 2173 |
buffer: buffer.error_ident(), |
| 2174 |
range: bb.offset..end, |
| 2175 |
size: buffer.size, |
| 2176 |
}); |
| 2177 |
} |
| 2178 |
(size.get(), end) |
| 2179 |
} |
| 2180 |
None => { |
| 2181 |
if buffer.size < bb.offset { |
| 2182 |
return Err(Error::BindingRangeTooLarge { |
| 2183 |
buffer: buffer.error_ident(), |
| 2184 |
range: bb.offset..bb.offset, |
| 2185 |
size: buffer.size, |
| 2186 |
}); |
| 2187 |
} |
| 2188 |
(buffer.size - bb.offset, buffer.size) |
| 2189 |
} |
| 2190 |
}; |
| 2191 |
|
| 2192 |
if bind_size > range_limit as u64 { |
| 2193 |
return Err(Error::BufferRangeTooLarge { |
| 2194 |
binding, |
| 2195 |
given: bind_size as u32, |
| 2196 |
limit: range_limit, |
| 2197 |
}); |
| 2198 |
} |
| 2199 |
|
| 2200 |
// Record binding info for validating dynamic offsets |
| 2201 |
if dynamic { |
| 2202 |
dynamic_binding_info.push(binding_model::BindGroupDynamicBindingData { |
| 2203 |
binding_idx: binding, |
| 2204 |
buffer_size: buffer.size, |
| 2205 |
binding_range: bb.offset..bind_end, |
| 2206 |
maximum_dynamic_offset: buffer.size - bind_end, |
| 2207 |
binding_type: binding_ty, |
| 2208 |
}); |
| 2209 |
} |
| 2210 |
|
| 2211 |
if let Some(non_zero) = min_size { |
| 2212 |
let min_size = non_zero.get(); |
| 2213 |
if min_size > bind_size { |
| 2214 |
return Err(Error::BindingSizeTooSmall { |
| 2215 |
buffer: buffer.error_ident(), |
| 2216 |
actual: bind_size, |
| 2217 |
min: min_size, |
| 2218 |
}); |
| 2219 |
} |
| 2220 |
} else { |
| 2221 |
let late_size = wgt::BufferSize::new(bind_size) |
| 2222 |
.ok_or_else(|| Error::BindingZeroSize(buffer.error_ident()))?; |
| 2223 |
late_buffer_binding_sizes.insert(binding, late_size); |
| 2224 |
} |
| 2225 |
|
| 2226 |
// This was checked against the device's alignment requirements above, |
| 2227 |
// which should always be a multiple of `COPY_BUFFER_ALIGNMENT`. |
| 2228 |
assert_eq!(bb.offset % wgt::COPY_BUFFER_ALIGNMENT, 0); |
| 2229 |
|
| 2230 |
// `wgpu_hal` only restricts shader access to bound buffer regions with |
| 2231 |
// a certain resolution. For the sake of lazy initialization, round up |
| 2232 |
// the size of the bound range to reflect how much of the buffer is |
| 2233 |
// actually going to be visible to the shader. |
| 2234 |
let bounds_check_alignment = |
| 2235 |
binding_model::buffer_binding_type_bounds_check_alignment(&self.alignments, binding_ty); |
| 2236 |
let visible_size = align_to(bind_size, bounds_check_alignment); |
| 2237 |
|
| 2238 |
used_buffer_ranges.extend(buffer.initialization_status.read().create_action( |
| 2239 |
buffer, |
| 2240 |
bb.offset..bb.offset + visible_size, |
| 2241 |
MemoryInitKind::NeedsInitializedMemory, |
| 2242 |
)); |
| 2243 |
|
| 2244 |
Ok(hal::BufferBinding { |
| 2245 |
buffer: raw_buffer, |
| 2246 |
offset: bb.offset, |
| 2247 |
size: bb.size, |
| 2248 |
}) |
| 2249 |
} |
| 2250 |
|
| 2251 |
fn create_sampler_binding<'a>( |
| 2252 |
&self, |
| 2253 |
used: &mut BindGroupStates, |
| 2254 |
binding: u32, |
| 2255 |
decl: &wgt::BindGroupLayoutEntry, |
| 2256 |
sampler: &'a Arc<Sampler>, |
| 2257 |
) -> Result<&'a dyn hal::DynSampler, binding_model::CreateBindGroupError> { |
| 2258 |
use crate::binding_model::CreateBindGroupError as Error; |
| 2259 |
|
| 2260 |
used.samplers.insert_single(sampler.clone()); |
| 2261 |
|
| 2262 |
sampler.same_device(self)?; |
| 2263 |
|
| 2264 |
match decl.ty { |
| 2265 |
wgt::BindingType::Sampler(ty) => { |
| 2266 |
let (allowed_filtering, allowed_comparison) = match ty { |
| 2267 |
wgt::SamplerBindingType::Filtering => (None, false), |
| 2268 |
wgt::SamplerBindingType::NonFiltering => (Some(false), false), |
| 2269 |
wgt::SamplerBindingType::Comparison => (None, true), |
| 2270 |
}; |
| 2271 |
if let Some(allowed_filtering) = allowed_filtering { |
| 2272 |
if allowed_filtering != sampler.filtering { |
| 2273 |
return Err(Error::WrongSamplerFiltering { |
| 2274 |
binding, |
| 2275 |
layout_flt: allowed_filtering, |
| 2276 |
sampler_flt: sampler.filtering, |
| 2277 |
}); |
| 2278 |
} |
| 2279 |
} |
| 2280 |
if allowed_comparison != sampler.comparison { |
| 2281 |
return Err(Error::WrongSamplerComparison { |
| 2282 |
binding, |
| 2283 |
layout_cmp: allowed_comparison, |
| 2284 |
sampler_cmp: sampler.comparison, |
| 2285 |
}); |
| 2286 |
} |
| 2287 |
} |
| 2288 |
_ => { |
| 2289 |
return Err(Error::WrongBindingType { |
| 2290 |
binding, |
| 2291 |
actual: decl.ty, |
| 2292 |
expected: "Sampler", |
| 2293 |
}) |
| 2294 |
} |
| 2295 |
} |
| 2296 |
|
| 2297 |
Ok(sampler.raw()) |
| 2298 |
} |
| 2299 |
|
| 2300 |
fn create_texture_binding<'a>( |
| 2301 |
&self, |
| 2302 |
binding: u32, |
| 2303 |
decl: &wgt::BindGroupLayoutEntry, |
| 2304 |
view: &'a Arc<TextureView>, |
| 2305 |
used: &mut BindGroupStates, |
| 2306 |
used_texture_ranges: &mut Vec<TextureInitTrackerAction>, |
| 2307 |
snatch_guard: &'a SnatchGuard<'a>, |
| 2308 |
) -> Result<hal::TextureBinding<'a, dyn hal::DynTextureView>, binding_model::CreateBindGroupError> |
| 2309 |
{ |
| 2310 |
view.same_device(self)?; |
| 2311 |
|
| 2312 |
let internal_use = self.texture_use_parameters( |
| 2313 |
binding, |
| 2314 |
decl, |
| 2315 |
view, |
| 2316 |
"SampledTexture, ReadonlyStorageTexture or WriteonlyStorageTexture", |
| 2317 |
)?; |
| 2318 |
|
| 2319 |
used.views.insert_single(view.clone(), internal_use); |
| 2320 |
|
| 2321 |
let texture = &view.parent; |
| 2322 |
|
| 2323 |
used_texture_ranges.push(TextureInitTrackerAction { |
| 2324 |
texture: texture.clone(), |
| 2325 |
range: TextureInitRange { |
| 2326 |
mip_range: view.desc.range.mip_range(texture.desc.mip_level_count), |
| 2327 |
layer_range: view |
| 2328 |
.desc |
| 2329 |
.range |
| 2330 |
.layer_range(texture.desc.array_layer_count()), |
| 2331 |
}, |
| 2332 |
kind: MemoryInitKind::NeedsInitializedMemory, |
| 2333 |
}); |
| 2334 |
|
| 2335 |
Ok(hal::TextureBinding { |
| 2336 |
view: view.try_raw(snatch_guard)?, |
| 2337 |
usage: internal_use, |
| 2338 |
}) |
| 2339 |
} |
| 2340 |
|
| 2341 |
fn create_tlas_binding<'a>( |
| 2342 |
self: &Arc<Self>, |
| 2343 |
used: &mut BindGroupStates, |
| 2344 |
binding: u32, |
| 2345 |
decl: &wgt::BindGroupLayoutEntry, |
| 2346 |
tlas: &'a Arc<Tlas>, |
| 2347 |
snatch_guard: &'a SnatchGuard<'a>, |
| 2348 |
) -> Result<&'a dyn hal::DynAccelerationStructure, binding_model::CreateBindGroupError> { |
| 2349 |
use crate::binding_model::CreateBindGroupError as Error; |
| 2350 |
|
| 2351 |
used.acceleration_structures.insert_single(tlas.clone()); |
| 2352 |
|
| 2353 |
tlas.same_device(self)?; |
| 2354 |
|
| 2355 |
match decl.ty { |
| 2356 |
wgt::BindingType::AccelerationStructure { vertex_return } => { |
| 2357 |
if vertex_return |
| 2358 |
&& !tlas.flags.contains( |
| 2359 |
wgpu_types::AccelerationStructureFlags::ALLOW_RAY_HIT_VERTEX_RETURN, |
| 2360 |
) |
| 2361 |
{ |
| 2362 |
return Err(Error::MissingTLASVertexReturn { binding }); |
| 2363 |
} |
| 2364 |
} |
| 2365 |
_ => { |
| 2366 |
return Err(Error::WrongBindingType { |
| 2367 |
binding, |
| 2368 |
actual: decl.ty, |
| 2369 |
expected: "Tlas", |
| 2370 |
}); |
| 2371 |
} |
| 2372 |
} |
| 2373 |
|
| 2374 |
Ok(tlas.try_raw(snatch_guard)?) |
| 2375 |
} |
| 2376 |
|
| 2377 |
// This function expects the provided bind group layout to be resolved |
| 2378 |
// (not passing a duplicate) beforehand. |
| 2379 |
pub(crate) fn create_bind_group( |
| 2380 |
self: &Arc<Self>, |
| 2381 |
desc: binding_model::ResolvedBindGroupDescriptor, |
| 2382 |
) -> Result<Arc<BindGroup>, binding_model::CreateBindGroupError> { |
| 2383 |
use crate::binding_model::{CreateBindGroupError as Error, ResolvedBindingResource as Br}; |
| 2384 |
|
| 2385 |
let layout = desc.layout; |
| 2386 |
|
| 2387 |
self.check_is_valid()?; |
| 2388 |
layout.same_device(self)?; |
| 2389 |
|
| 2390 |
{ |
| 2391 |
// Check that the number of entries in the descriptor matches |
| 2392 |
// the number of entries in the layout. |
| 2393 |
let actual = desc.entries.len(); |
| 2394 |
let expected = layout.entries.len(); |
| 2395 |
if actual != expected { |
| 2396 |
return Err(Error::BindingsNumMismatch { expected, actual }); |
| 2397 |
} |
| 2398 |
} |
| 2399 |
|
| 2400 |
// TODO: arrayvec/smallvec, or re-use allocations |
| 2401 |
// Record binding info for dynamic offset validation |
| 2402 |
let mut dynamic_binding_info = Vec::new(); |
| 2403 |
// Map of binding -> shader reflected size |
| 2404 |
//Note: we can't collect into a vector right away because |
| 2405 |
// it needs to be in BGL iteration order, not BG entry order. |
| 2406 |
let mut late_buffer_binding_sizes = FastHashMap::default(); |
| 2407 |
// fill out the descriptors |
| 2408 |
let mut used = BindGroupStates::new(); |
| 2409 |
|
| 2410 |
let mut used_buffer_ranges = Vec::new(); |
| 2411 |
let mut used_texture_ranges = Vec::new(); |
| 2412 |
let mut hal_entries = Vec::with_capacity(desc.entries.len()); |
| 2413 |
let mut hal_buffers = Vec::new(); |
| 2414 |
let mut hal_samplers = Vec::new(); |
| 2415 |
let mut hal_textures = Vec::new(); |
| 2416 |
let mut hal_tlas_s = Vec::new(); |
| 2417 |
let snatch_guard = self.snatchable_lock.read(); |
| 2418 |
for entry in desc.entries.iter() { |
| 2419 |
let binding = entry.binding; |
| 2420 |
// Find the corresponding declaration in the layout |
| 2421 |
let decl = layout |
| 2422 |
.entries |
| 2423 |
.get(binding) |
| 2424 |
.ok_or(Error::MissingBindingDeclaration(binding))?; |
| 2425 |
let (res_index, count) = match entry.resource { |
| 2426 |
Br::Buffer(ref bb) => { |
| 2427 |
let bb = self.create_buffer_binding( |
| 2428 |
bb, |
| 2429 |
binding, |
| 2430 |
decl, |
| 2431 |
&mut used_buffer_ranges, |
| 2432 |
&mut dynamic_binding_info, |
| 2433 |
&mut late_buffer_binding_sizes, |
| 2434 |
&mut used, |
| 2435 |
&snatch_guard, |
| 2436 |
)?; |
| 2437 |
|
| 2438 |
let res_index = hal_buffers.len(); |
| 2439 |
hal_buffers.push(bb); |
| 2440 |
(res_index, 1) |
| 2441 |
} |
| 2442 |
Br::BufferArray(ref bindings_array) => { |
| 2443 |
let num_bindings = bindings_array.len(); |
| 2444 |
Self::check_array_binding(self.features, decl.count, num_bindings)?; |
| 2445 |
|
| 2446 |
let res_index = hal_buffers.len(); |
| 2447 |
for bb in bindings_array.iter() { |
| 2448 |
let bb = self.create_buffer_binding( |
| 2449 |
bb, |
| 2450 |
binding, |
| 2451 |
decl, |
| 2452 |
&mut used_buffer_ranges, |
| 2453 |
&mut dynamic_binding_info, |
| 2454 |
&mut late_buffer_binding_sizes, |
| 2455 |
&mut used, |
| 2456 |
&snatch_guard, |
| 2457 |
)?; |
| 2458 |
hal_buffers.push(bb); |
| 2459 |
} |
| 2460 |
(res_index, num_bindings) |
| 2461 |
} |
| 2462 |
Br::Sampler(ref sampler) => { |
| 2463 |
let sampler = self.create_sampler_binding(&mut used, binding, decl, sampler)?; |
| 2464 |
|
| 2465 |
let res_index = hal_samplers.len(); |
| 2466 |
hal_samplers.push(sampler); |
| 2467 |
(res_index, 1) |
| 2468 |
} |
| 2469 |
Br::SamplerArray(ref samplers) => { |
| 2470 |
let num_bindings = samplers.len(); |
| 2471 |
Self::check_array_binding(self.features, decl.count, num_bindings)?; |
| 2472 |
|
| 2473 |
let res_index = hal_samplers.len(); |
| 2474 |
for sampler in samplers.iter() { |
| 2475 |
let sampler = |
| 2476 |
self.create_sampler_binding(&mut used, binding, decl, sampler)?; |
| 2477 |
|
| 2478 |
hal_samplers.push(sampler); |
| 2479 |
} |
| 2480 |
|
| 2481 |
(res_index, num_bindings) |
| 2482 |
} |
| 2483 |
Br::TextureView(ref view) => { |
| 2484 |
let tb = self.create_texture_binding( |
| 2485 |
binding, |
| 2486 |
decl, |
| 2487 |
view, |
| 2488 |
&mut used, |
| 2489 |
&mut used_texture_ranges, |
| 2490 |
&snatch_guard, |
| 2491 |
)?; |
| 2492 |
let res_index = hal_textures.len(); |
| 2493 |
hal_textures.push(tb); |
| 2494 |
(res_index, 1) |
| 2495 |
} |
| 2496 |
Br::TextureViewArray(ref views) => { |
| 2497 |
let num_bindings = views.len(); |
| 2498 |
Self::check_array_binding(self.features, decl.count, num_bindings)?; |
| 2499 |
|
| 2500 |
let res_index = hal_textures.len(); |
| 2501 |
for view in views.iter() { |
| 2502 |
let tb = self.create_texture_binding( |
| 2503 |
binding, |
| 2504 |
decl, |
| 2505 |
view, |
| 2506 |
&mut used, |
| 2507 |
&mut used_texture_ranges, |
| 2508 |
&snatch_guard, |
| 2509 |
)?; |
| 2510 |
|
| 2511 |
hal_textures.push(tb); |
| 2512 |
} |
| 2513 |
|
| 2514 |
(res_index, num_bindings) |
| 2515 |
} |
| 2516 |
Br::AccelerationStructure(ref tlas) => { |
| 2517 |
let tlas = |
| 2518 |
self.create_tlas_binding(&mut used, binding, decl, tlas, &snatch_guard)?; |
| 2519 |
let res_index = hal_tlas_s.len(); |
| 2520 |
hal_tlas_s.push(tlas); |
| 2521 |
(res_index, 1) |
| 2522 |
} |
| 2523 |
}; |
| 2524 |
|
| 2525 |
hal_entries.push(hal::BindGroupEntry { |
| 2526 |
binding, |
| 2527 |
resource_index: res_index as u32, |
| 2528 |
count: count as u32, |
| 2529 |
}); |
| 2530 |
} |
| 2531 |
|
| 2532 |
used.optimize(); |
| 2533 |
|
| 2534 |
hal_entries.sort_by_key(|entry| entry.binding); |
| 2535 |
for (a, b) in hal_entries.iter().zip(hal_entries.iter().skip(1)) { |
| 2536 |
if a.binding == b.binding { |
| 2537 |
return Err(Error::DuplicateBinding(a.binding)); |
| 2538 |
} |
| 2539 |
} |
| 2540 |
let hal_desc = hal::BindGroupDescriptor { |
| 2541 |
label: desc.label.to_hal(self.instance_flags), |
| 2542 |
layout: layout.raw(), |
| 2543 |
entries: &hal_entries, |
| 2544 |
buffers: &hal_buffers, |
| 2545 |
samplers: &hal_samplers, |
| 2546 |
textures: &hal_textures, |
| 2547 |
acceleration_structures: &hal_tlas_s, |
| 2548 |
}; |
| 2549 |
let raw = unsafe { self.raw().create_bind_group(&hal_desc) } |
| 2550 |
.map_err(|e| self.handle_hal_error(e))?; |
| 2551 |
|
| 2552 |
// collect in the order of BGL iteration |
| 2553 |
let late_buffer_binding_sizes = layout |
| 2554 |
.entries |
| 2555 |
.indices() |
| 2556 |
.flat_map(|binding| late_buffer_binding_sizes.get(&binding).cloned()) |
| 2557 |
.collect(); |
| 2558 |
|
| 2559 |
let bind_group = BindGroup { |
| 2560 |
raw: Snatchable::new(raw), |
| 2561 |
device: self.clone(), |
| 2562 |
layout, |
| 2563 |
label: desc.label.to_string(), |
| 2564 |
tracking_data: TrackingData::new(self.tracker_indices.bind_groups.clone()), |
| 2565 |
used, |
| 2566 |
used_buffer_ranges, |
| 2567 |
used_texture_ranges, |
| 2568 |
dynamic_binding_info, |
| 2569 |
late_buffer_binding_sizes, |
| 2570 |
}; |
| 2571 |
|
| 2572 |
let bind_group = Arc::new(bind_group); |
| 2573 |
|
| 2574 |
let weak_ref = Arc::downgrade(&bind_group); |
| 2575 |
for range in &bind_group.used_texture_ranges { |
| 2576 |
let mut bind_groups = range.texture.bind_groups.lock(); |
| 2577 |
bind_groups.push(weak_ref.clone()); |
| 2578 |
} |
| 2579 |
for range in &bind_group.used_buffer_ranges { |
| 2580 |
let mut bind_groups = range.buffer.bind_groups.lock(); |
| 2581 |
bind_groups.push(weak_ref.clone()); |
| 2582 |
} |
| 2583 |
|
| 2584 |
Ok(bind_group) |
| 2585 |
} |
| 2586 |
|
| 2587 |
fn check_array_binding( |
| 2588 |
features: wgt::Features, |
| 2589 |
count: Option<NonZeroU32>, |
| 2590 |
num_bindings: usize, |
| 2591 |
) -> Result<(), binding_model::CreateBindGroupError> { |
| 2592 |
use super::binding_model::CreateBindGroupError as Error; |
| 2593 |
|
| 2594 |
if let Some(count) = count { |
| 2595 |
let count = count.get() as usize; |
| 2596 |
if count < num_bindings { |
| 2597 |
return Err(Error::BindingArrayPartialLengthMismatch { |
| 2598 |
actual: num_bindings, |
| 2599 |
expected: count, |
| 2600 |
}); |
| 2601 |
} |
| 2602 |
if count != num_bindings |
| 2603 |
&& !features.contains(wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY) |
| 2604 |
{ |
| 2605 |
return Err(Error::BindingArrayLengthMismatch { |
| 2606 |
actual: num_bindings, |
| 2607 |
expected: count, |
| 2608 |
}); |
| 2609 |
} |
| 2610 |
if num_bindings == 0 { |
| 2611 |
return Err(Error::BindingArrayZeroLength); |
| 2612 |
} |
| 2613 |
} else { |
| 2614 |
return Err(Error::SingleBindingExpected); |
| 2615 |
}; |
| 2616 |
|
| 2617 |
Ok(()) |
| 2618 |
} |
| 2619 |
|
| 2620 |
fn texture_use_parameters( |
| 2621 |
&self, |
| 2622 |
binding: u32, |
| 2623 |
decl: &wgt::BindGroupLayoutEntry, |
| 2624 |
view: &TextureView, |
| 2625 |
expected: &'static str, |
| 2626 |
) -> Result<wgt::TextureUses, binding_model::CreateBindGroupError> { |
| 2627 |
use crate::binding_model::CreateBindGroupError as Error; |
| 2628 |
if view |
| 2629 |
.desc |
| 2630 |
.aspects() |
| 2631 |
.contains(hal::FormatAspects::DEPTH | hal::FormatAspects::STENCIL) |
| 2632 |
{ |
| 2633 |
return Err(Error::DepthStencilAspect); |
| 2634 |
} |
| 2635 |
match decl.ty { |
| 2636 |
wgt::BindingType::Texture { |
| 2637 |
sample_type, |
| 2638 |
view_dimension, |
| 2639 |
multisampled, |
| 2640 |
} => { |
| 2641 |
use wgt::TextureSampleType as Tst; |
| 2642 |
if multisampled != (view.samples != 1) { |
| 2643 |
return Err(Error::InvalidTextureMultisample { |
| 2644 |
binding, |
| 2645 |
layout_multisampled: multisampled, |
| 2646 |
view_samples: view.samples, |
| 2647 |
}); |
| 2648 |
} |
| 2649 |
let compat_sample_type = view |
| 2650 |
.desc |
| 2651 |
.format |
| 2652 |
.sample_type(Some(view.desc.range.aspect), Some(self.features)) |
| 2653 |
.unwrap(); |
| 2654 |
match (sample_type, compat_sample_type) { |
| 2655 |
(Tst::Uint, Tst::Uint) | |
| 2656 |
(Tst::Sint, Tst::Sint) | |
| 2657 |
(Tst::Depth, Tst::Depth) | |
| 2658 |
// if we expect non-filterable, accept anything float |
| 2659 |
(Tst::Float { filterable: false }, Tst::Float { .. }) | |
| 2660 |
// if we expect filterable, require it |
| 2661 |
(Tst::Float { filterable: true }, Tst::Float { filterable: true }) | |
| 2662 |
// if we expect non-filterable, also accept depth |
| 2663 |
(Tst::Float { filterable: false }, Tst::Depth) => {} |
| 2664 |
// if we expect filterable, also accept Float that is defined as |
| 2665 |
// unfilterable if filterable feature is explicitly enabled (only hit |
| 2666 |
// if wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES is |
| 2667 |
// enabled) |
| 2668 |
(Tst::Float { filterable: true }, Tst::Float { .. }) if view.format_features.flags.contains(wgt::TextureFormatFeatureFlags::FILTERABLE) => {} |
| 2669 |
_ => { |
| 2670 |
return Err(Error::InvalidTextureSampleType { |
| 2671 |
binding, |
| 2672 |
layout_sample_type: sample_type, |
| 2673 |
view_format: view.desc.format, |
| 2674 |
view_sample_type: compat_sample_type, |
| 2675 |
}) |
| 2676 |
} |
| 2677 |
} |
| 2678 |
if view_dimension != view.desc.dimension { |
| 2679 |
return Err(Error::InvalidTextureDimension { |
| 2680 |
binding, |
| 2681 |
layout_dimension: view_dimension, |
| 2682 |
view_dimension: view.desc.dimension, |
| 2683 |
}); |
| 2684 |
} |
| 2685 |
view.check_usage(wgt::TextureUsages::TEXTURE_BINDING)?; |
| 2686 |
Ok(wgt::TextureUses::RESOURCE) |
| 2687 |
} |
| 2688 |
wgt::BindingType::StorageTexture { |
| 2689 |
access, |
| 2690 |
format, |
| 2691 |
view_dimension, |
| 2692 |
} => { |
| 2693 |
if format != view.desc.format { |
| 2694 |
return Err(Error::InvalidStorageTextureFormat { |
| 2695 |
binding, |
| 2696 |
layout_format: format, |
| 2697 |
view_format: view.desc.format, |
| 2698 |
}); |
| 2699 |
} |
| 2700 |
if view_dimension != view.desc.dimension { |
| 2701 |
return Err(Error::InvalidTextureDimension { |
| 2702 |
binding, |
| 2703 |
layout_dimension: view_dimension, |
| 2704 |
view_dimension: view.desc.dimension, |
| 2705 |
}); |
| 2706 |
} |
| 2707 |
|
| 2708 |
let mip_level_count = view.selector.mips.end - view.selector.mips.start; |
| 2709 |
if mip_level_count != 1 { |
| 2710 |
return Err(Error::InvalidStorageTextureMipLevelCount { |
| 2711 |
binding, |
| 2712 |
mip_level_count, |
| 2713 |
}); |
| 2714 |
} |
| 2715 |
|
| 2716 |
let internal_use = match access { |
| 2717 |
wgt::StorageTextureAccess::WriteOnly => { |
| 2718 |
if !view |
| 2719 |
.format_features |
| 2720 |
.flags |
| 2721 |
.contains(wgt::TextureFormatFeatureFlags::STORAGE_WRITE_ONLY) |
| 2722 |
{ |
| 2723 |
return Err(Error::StorageWriteNotSupported(view.desc.format)); |
| 2724 |
} |
| 2725 |
wgt::TextureUses::STORAGE_WRITE_ONLY |
| 2726 |
} |
| 2727 |
wgt::StorageTextureAccess::ReadOnly => { |
| 2728 |
if !view |
| 2729 |
.format_features |
| 2730 |
.flags |
| 2731 |
.contains(wgt::TextureFormatFeatureFlags::STORAGE_READ_ONLY) |
| 2732 |
{ |
| 2733 |
return Err(Error::StorageReadNotSupported(view.desc.format)); |
| 2734 |
} |
| 2735 |
wgt::TextureUses::STORAGE_READ_ONLY |
| 2736 |
} |
| 2737 |
wgt::StorageTextureAccess::ReadWrite => { |
| 2738 |
if !view |
| 2739 |
.format_features |
| 2740 |
.flags |
| 2741 |
.contains(wgt::TextureFormatFeatureFlags::STORAGE_READ_WRITE) |
| 2742 |
{ |
| 2743 |
return Err(Error::StorageReadWriteNotSupported(view.desc.format)); |
| 2744 |
} |
| 2745 |
|
| 2746 |
wgt::TextureUses::STORAGE_READ_WRITE |
| 2747 |
} |
| 2748 |
wgt::StorageTextureAccess::Atomic => { |
| 2749 |
if !view |
| 2750 |
.format_features |
| 2751 |
.flags |
| 2752 |
.contains(wgt::TextureFormatFeatureFlags::STORAGE_ATOMIC) |
| 2753 |
{ |
| 2754 |
return Err(Error::StorageAtomicNotSupported(view.desc.format)); |
| 2755 |
} |
| 2756 |
|
| 2757 |
wgt::TextureUses::STORAGE_ATOMIC |
| 2758 |
} |
| 2759 |
}; |
| 2760 |
view.check_usage(wgt::TextureUsages::STORAGE_BINDING)?; |
| 2761 |
Ok(internal_use) |
| 2762 |
} |
| 2763 |
_ => Err(Error::WrongBindingType { |
| 2764 |
binding, |
| 2765 |
actual: decl.ty, |
| 2766 |
expected, |
| 2767 |
}), |
| 2768 |
} |
| 2769 |
} |
| 2770 |
|
| 2771 |
pub(crate) fn create_pipeline_layout( |
| 2772 |
self: &Arc<Self>, |
| 2773 |
desc: &binding_model::ResolvedPipelineLayoutDescriptor, |
| 2774 |
) -> Result<Arc<binding_model::PipelineLayout>, binding_model::CreatePipelineLayoutError> { |
| 2775 |
use crate::binding_model::CreatePipelineLayoutError as Error; |
| 2776 |
|
| 2777 |
self.check_is_valid()?; |
| 2778 |
|
| 2779 |
let bind_group_layouts_count = desc.bind_group_layouts.len(); |
| 2780 |
let device_max_bind_groups = self.limits.max_bind_groups as usize; |
| 2781 |
if bind_group_layouts_count > device_max_bind_groups { |
| 2782 |
return Err(Error::TooManyGroups { |
| 2783 |
actual: bind_group_layouts_count, |
| 2784 |
max: device_max_bind_groups, |
| 2785 |
}); |
| 2786 |
} |
| 2787 |
|
| 2788 |
if !desc.push_constant_ranges.is_empty() { |
| 2789 |
self.require_features(wgt::Features::PUSH_CONSTANTS)?; |
| 2790 |
} |
| 2791 |
|
| 2792 |
let mut used_stages = wgt::ShaderStages::empty(); |
| 2793 |
for (index, pc) in desc.push_constant_ranges.iter().enumerate() { |
| 2794 |
if pc.stages.intersects(used_stages) { |
| 2795 |
return Err(Error::MoreThanOnePushConstantRangePerStage { |
| 2796 |
index, |
| 2797 |
provided: pc.stages, |
| 2798 |
intersected: pc.stages & used_stages, |
| 2799 |
}); |
| 2800 |
} |
| 2801 |
used_stages |= pc.stages; |
| 2802 |
|
| 2803 |
let device_max_pc_size = self.limits.max_push_constant_size; |
| 2804 |
if device_max_pc_size < pc.range.end { |
| 2805 |
return Err(Error::PushConstantRangeTooLarge { |
| 2806 |
index, |
| 2807 |
range: pc.range.clone(), |
| 2808 |
max: device_max_pc_size, |
| 2809 |
}); |
| 2810 |
} |
| 2811 |
|
| 2812 |
if pc.range.start % wgt::PUSH_CONSTANT_ALIGNMENT != 0 { |
| 2813 |
return Err(Error::MisalignedPushConstantRange { |
| 2814 |
index, |
| 2815 |
bound: pc.range.start, |
| 2816 |
}); |
| 2817 |
} |
| 2818 |
if pc.range.end % wgt::PUSH_CONSTANT_ALIGNMENT != 0 { |
| 2819 |
return Err(Error::MisalignedPushConstantRange { |
| 2820 |
index, |
| 2821 |
bound: pc.range.end, |
| 2822 |
}); |
| 2823 |
} |
| 2824 |
} |
| 2825 |
|
| 2826 |
let mut count_validator = binding_model::BindingTypeMaxCountValidator::default(); |
| 2827 |
|
| 2828 |
for bgl in desc.bind_group_layouts.iter() { |
| 2829 |
bgl.same_device(self)?; |
| 2830 |
count_validator.merge(&bgl.binding_count_validator); |
| 2831 |
} |
| 2832 |
|
| 2833 |
count_validator |
| 2834 |
.validate(&self.limits) |
| 2835 |
.map_err(Error::TooManyBindings)?; |
| 2836 |
|
| 2837 |
let bind_group_layouts = desc |
| 2838 |
.bind_group_layouts |
| 2839 |
.iter() |
| 2840 |
.cloned() |
| 2841 |
.collect::<ArrayVec<_, { hal::MAX_BIND_GROUPS }>>(); |
| 2842 |
|
| 2843 |
let raw_bind_group_layouts = desc |
| 2844 |
.bind_group_layouts |
| 2845 |
.iter() |
| 2846 |
.map(|bgl| bgl.raw()) |
| 2847 |
.collect::<ArrayVec<_, { hal::MAX_BIND_GROUPS }>>(); |
| 2848 |
|
| 2849 |
let additional_flags = if self.indirect_validation.is_some() { |
| 2850 |
hal::PipelineLayoutFlags::INDIRECT_BUILTIN_UPDATE |
| 2851 |
} else { |
| 2852 |
hal::PipelineLayoutFlags::empty() |
| 2853 |
}; |
| 2854 |
|
| 2855 |
let hal_desc = hal::PipelineLayoutDescriptor { |
| 2856 |
label: desc.label.to_hal(self.instance_flags), |
| 2857 |
flags: hal::PipelineLayoutFlags::FIRST_VERTEX_INSTANCE |
| 2858 |
| hal::PipelineLayoutFlags::NUM_WORK_GROUPS |
| 2859 |
| additional_flags, |
| 2860 |
bind_group_layouts: &raw_bind_group_layouts, |
| 2861 |
push_constant_ranges: desc.push_constant_ranges.as_ref(), |
| 2862 |
}; |
| 2863 |
|
| 2864 |
let raw = unsafe { self.raw().create_pipeline_layout(&hal_desc) } |
| 2865 |
.map_err(|e| self.handle_hal_error(e))?; |
| 2866 |
|
| 2867 |
drop(raw_bind_group_layouts); |
| 2868 |
|
| 2869 |
let layout = binding_model::PipelineLayout { |
| 2870 |
raw: ManuallyDrop::new(raw), |
| 2871 |
device: self.clone(), |
| 2872 |
label: desc.label.to_string(), |
| 2873 |
bind_group_layouts, |
| 2874 |
push_constant_ranges: desc.push_constant_ranges.iter().cloned().collect(), |
| 2875 |
}; |
| 2876 |
|
| 2877 |
let layout = Arc::new(layout); |
| 2878 |
|
| 2879 |
Ok(layout) |
| 2880 |
} |
| 2881 |
|
| 2882 |
pub(crate) fn derive_pipeline_layout( |
| 2883 |
self: &Arc<Self>, |
| 2884 |
mut derived_group_layouts: Box<ArrayVec<bgl::EntryMap, { hal::MAX_BIND_GROUPS }>>, |
| 2885 |
) -> Result<Arc<binding_model::PipelineLayout>, pipeline::ImplicitLayoutError> { |
| 2886 |
while derived_group_layouts |
| 2887 |
.last() |
| 2888 |
.is_some_and(|map| map.is_empty()) |
| 2889 |
{ |
| 2890 |
derived_group_layouts.pop(); |
| 2891 |
} |
| 2892 |
|
| 2893 |
let mut unique_bind_group_layouts = FastHashMap::default(); |
| 2894 |
|
| 2895 |
let bind_group_layouts = derived_group_layouts |
| 2896 |
.into_iter() |
| 2897 |
.map(|mut bgl_entry_map| { |
| 2898 |
bgl_entry_map.sort(); |
| 2899 |
match unique_bind_group_layouts.entry(bgl_entry_map) { |
| 2900 |
hashbrown::hash_map::Entry::Occupied(v) => Ok(Arc::clone(v.get())), |
| 2901 |
hashbrown::hash_map::Entry::Vacant(e) => { |
| 2902 |
match self.create_bind_group_layout( |
| 2903 |
&None, |
| 2904 |
e.key().clone(), |
| 2905 |
bgl::Origin::Derived, |
| 2906 |
) { |
| 2907 |
Ok(bgl) => { |
| 2908 |
e.insert(bgl.clone()); |
| 2909 |
Ok(bgl) |
| 2910 |
} |
| 2911 |
Err(e) => Err(e), |
| 2912 |
} |
| 2913 |
} |
| 2914 |
} |
| 2915 |
}) |
| 2916 |
.collect::<Result<Vec<_>, _>>()?; |
| 2917 |
|
| 2918 |
let layout_desc = binding_model::ResolvedPipelineLayoutDescriptor { |
| 2919 |
label: None, |
| 2920 |
bind_group_layouts: Cow::Owned(bind_group_layouts), |
| 2921 |
push_constant_ranges: Cow::Borrowed(&[]), //TODO? |
| 2922 |
}; |
| 2923 |
|
| 2924 |
let layout = self.create_pipeline_layout(&layout_desc)?; |
| 2925 |
Ok(layout) |
| 2926 |
} |
| 2927 |
|
| 2928 |
pub(crate) fn create_compute_pipeline( |
| 2929 |
self: &Arc<Self>, |
| 2930 |
desc: pipeline::ResolvedComputePipelineDescriptor, |
| 2931 |
) -> Result<Arc<pipeline::ComputePipeline>, pipeline::CreateComputePipelineError> { |
| 2932 |
self.check_is_valid()?; |
| 2933 |
|
| 2934 |
self.require_downlevel_flags(wgt::DownlevelFlags::COMPUTE_SHADERS)?; |
| 2935 |
|
| 2936 |
let shader_module = desc.stage.module; |
| 2937 |
|
| 2938 |
shader_module.same_device(self)?; |
| 2939 |
|
| 2940 |
let is_auto_layout = desc.layout.is_none(); |
| 2941 |
|
| 2942 |
// Get the pipeline layout from the desc if it is provided. |
| 2943 |
let pipeline_layout = match desc.layout { |
| 2944 |
Some(pipeline_layout) => { |
| 2945 |
pipeline_layout.same_device(self)?; |
| 2946 |
Some(pipeline_layout) |
| 2947 |
} |
| 2948 |
None => None, |
| 2949 |
}; |
| 2950 |
|
| 2951 |
let mut binding_layout_source = match pipeline_layout { |
| 2952 |
Some(ref pipeline_layout) => { |
| 2953 |
validation::BindingLayoutSource::Provided(pipeline_layout.get_binding_maps()) |
| 2954 |
} |
| 2955 |
None => validation::BindingLayoutSource::new_derived(&self.limits), |
| 2956 |
}; |
| 2957 |
let mut shader_binding_sizes = FastHashMap::default(); |
| 2958 |
let io = validation::StageIo::default(); |
| 2959 |
|
| 2960 |
let final_entry_point_name; |
| 2961 |
|
| 2962 |
{ |
| 2963 |
let stage = wgt::ShaderStages::COMPUTE; |
| 2964 |
|
| 2965 |
final_entry_point_name = shader_module.finalize_entry_point_name( |
| 2966 |
stage, |
| 2967 |
desc.stage.entry_point.as_ref().map(|ep| ep.as_ref()), |
| 2968 |
)?; |
| 2969 |
|
| 2970 |
if let Some(ref interface) = shader_module.interface { |
| 2971 |
let _ = interface.check_stage( |
| 2972 |
&mut binding_layout_source, |
| 2973 |
&mut shader_binding_sizes, |
| 2974 |
&final_entry_point_name, |
| 2975 |
stage, |
| 2976 |
io, |
| 2977 |
None, |
| 2978 |
)?; |
| 2979 |
} |
| 2980 |
} |
| 2981 |
|
| 2982 |
let pipeline_layout = match binding_layout_source { |
| 2983 |
validation::BindingLayoutSource::Provided(_) => { |
| 2984 |
drop(binding_layout_source); |
| 2985 |
pipeline_layout.unwrap() |
| 2986 |
} |
| 2987 |
validation::BindingLayoutSource::Derived(entries) => { |
| 2988 |
self.derive_pipeline_layout(entries)? |
| 2989 |
} |
| 2990 |
}; |
| 2991 |
|
| 2992 |
let late_sized_buffer_groups = |
| 2993 |
Device::make_late_sized_buffer_groups(&shader_binding_sizes, &pipeline_layout); |
| 2994 |
|
| 2995 |
let cache = match desc.cache { |
| 2996 |
Some(cache) => { |
| 2997 |
cache.same_device(self)?; |
| 2998 |
Some(cache) |
| 2999 |
} |
| 3000 |
None => None, |
| 3001 |
}; |
| 3002 |
|
| 3003 |
let pipeline_desc = hal::ComputePipelineDescriptor { |
| 3004 |
label: desc.label.to_hal(self.instance_flags), |
| 3005 |
layout: pipeline_layout.raw(), |
| 3006 |
stage: hal::ProgrammableStage { |
| 3007 |
module: shader_module.raw(), |
| 3008 |
entry_point: final_entry_point_name.as_ref(), |
| 3009 |
constants: &desc.stage.constants, |
| 3010 |
zero_initialize_workgroup_memory: desc.stage.zero_initialize_workgroup_memory, |
| 3011 |
}, |
| 3012 |
cache: cache.as_ref().map(|it| it.raw()), |
| 3013 |
}; |
| 3014 |
|
| 3015 |
let raw = |
| 3016 |
unsafe { self.raw().create_compute_pipeline(&pipeline_desc) }.map_err( |
| 3017 |
|err| match err { |
| 3018 |
hal::PipelineError::Device(error) => { |
| 3019 |
pipeline::CreateComputePipelineError::Device(self.handle_hal_error(error)) |
| 3020 |
} |
| 3021 |
hal::PipelineError::Linkage(_stages, msg) => { |
| 3022 |
pipeline::CreateComputePipelineError::Internal(msg) |
| 3023 |
} |
| 3024 |
hal::PipelineError::EntryPoint(_stage) => { |
| 3025 |
pipeline::CreateComputePipelineError::Internal( |
| 3026 |
ENTRYPOINT_FAILURE_ERROR.to_string(), |
| 3027 |
) |
| 3028 |
} |
| 3029 |
hal::PipelineError::PipelineConstants(_stages, msg) => { |
| 3030 |
pipeline::CreateComputePipelineError::PipelineConstants(msg) |
| 3031 |
} |
| 3032 |
}, |
| 3033 |
)?; |
| 3034 |
|
| 3035 |
let pipeline = pipeline::ComputePipeline { |
| 3036 |
raw: ManuallyDrop::new(raw), |
| 3037 |
layout: pipeline_layout, |
| 3038 |
device: self.clone(), |
| 3039 |
_shader_module: shader_module, |
| 3040 |
late_sized_buffer_groups, |
| 3041 |
label: desc.label.to_string(), |
| 3042 |
tracking_data: TrackingData::new(self.tracker_indices.compute_pipelines.clone()), |
| 3043 |
}; |
| 3044 |
|
| 3045 |
let pipeline = Arc::new(pipeline); |
| 3046 |
|
| 3047 |
if is_auto_layout { |
| 3048 |
for bgl in pipeline.layout.bind_group_layouts.iter() { |
| 3049 |
// `bind_group_layouts` might contain duplicate entries, so we need to ignore the result. |
| 3050 |
let _ = bgl |
| 3051 |
.exclusive_pipeline |
| 3052 |
.set(binding_model::ExclusivePipeline::Compute(Arc::downgrade( |
| 3053 |
&pipeline, |
| 3054 |
))); |
| 3055 |
} |
| 3056 |
} |
| 3057 |
|
| 3058 |
Ok(pipeline) |
| 3059 |
} |
| 3060 |
|
| 3061 |
pub(crate) fn create_render_pipeline( |
| 3062 |
self: &Arc<Self>, |
| 3063 |
desc: pipeline::ResolvedRenderPipelineDescriptor, |
| 3064 |
) -> Result<Arc<pipeline::RenderPipeline>, pipeline::CreateRenderPipelineError> { |
| 3065 |
use wgt::TextureFormatFeatureFlags as Tfff; |
| 3066 |
|
| 3067 |
self.check_is_valid()?; |
| 3068 |
|
| 3069 |
let mut shader_binding_sizes = FastHashMap::default(); |
| 3070 |
|
| 3071 |
let num_attachments = desc.fragment.as_ref().map(|f| f.targets.len()).unwrap_or(0); |
| 3072 |
let max_attachments = self.limits.max_color_attachments as usize; |
| 3073 |
if num_attachments > max_attachments { |
| 3074 |
return Err(pipeline::CreateRenderPipelineError::ColorAttachment( |
| 3075 |
command::ColorAttachmentError::TooMany { |
| 3076 |
given: num_attachments, |
| 3077 |
limit: max_attachments, |
| 3078 |
}, |
| 3079 |
)); |
| 3080 |
} |
| 3081 |
|
| 3082 |
let color_targets = desc |
| 3083 |
.fragment |
| 3084 |
.as_ref() |
| 3085 |
.map_or(&[][..], |fragment| &fragment.targets); |
| 3086 |
let depth_stencil_state = desc.depth_stencil.as_ref(); |
| 3087 |
|
| 3088 |
{ |
| 3089 |
let cts: ArrayVec<_, { hal::MAX_COLOR_ATTACHMENTS }> = |
| 3090 |
color_targets.iter().filter_map(|x| x.as_ref()).collect(); |
| 3091 |
if !cts.is_empty() && { |
| 3092 |
let first = &cts[0]; |
| 3093 |
cts[1..] |
| 3094 |
.iter() |
| 3095 |
.any(|ct| ct.write_mask != first.write_mask || ct.blend != first.blend) |
| 3096 |
} { |
| 3097 |
self.require_downlevel_flags(wgt::DownlevelFlags::INDEPENDENT_BLEND)?; |
| 3098 |
} |
| 3099 |
} |
| 3100 |
|
| 3101 |
let mut io = validation::StageIo::default(); |
| 3102 |
let mut validated_stages = wgt::ShaderStages::empty(); |
| 3103 |
|
| 3104 |
let mut vertex_steps = Vec::with_capacity(desc.vertex.buffers.len()); |
| 3105 |
let mut vertex_buffers = Vec::with_capacity(desc.vertex.buffers.len()); |
| 3106 |
let mut total_attributes = 0; |
| 3107 |
let mut shader_expects_dual_source_blending = false; |
| 3108 |
let mut pipeline_expects_dual_source_blending = false; |
| 3109 |
for (i, vb_state) in desc.vertex.buffers.iter().enumerate() { |
| 3110 |
// https://gpuweb.github.io/gpuweb/#abstract-opdef-validating-gpuvertexbufferlayout |
| 3111 |
|
| 3112 |
if vb_state.array_stride > self.limits.max_vertex_buffer_array_stride as u64 { |
| 3113 |
return Err(pipeline::CreateRenderPipelineError::VertexStrideTooLarge { |
| 3114 |
index: i as u32, |
| 3115 |
given: vb_state.array_stride as u32, |
| 3116 |
limit: self.limits.max_vertex_buffer_array_stride, |
| 3117 |
}); |
| 3118 |
} |
| 3119 |
if vb_state.array_stride % wgt::VERTEX_STRIDE_ALIGNMENT != 0 { |
| 3120 |
return Err(pipeline::CreateRenderPipelineError::UnalignedVertexStride { |
| 3121 |
index: i as u32, |
| 3122 |
stride: vb_state.array_stride, |
| 3123 |
}); |
| 3124 |
} |
| 3125 |
|
| 3126 |
let max_stride = if vb_state.array_stride == 0 { |
| 3127 |
self.limits.max_vertex_buffer_array_stride as u64 |
| 3128 |
} else { |
| 3129 |
vb_state.array_stride |
| 3130 |
}; |
| 3131 |
let mut last_stride = 0; |
| 3132 |
for attribute in vb_state.attributes.iter() { |
| 3133 |
let attribute_stride = attribute.offset + attribute.format.size(); |
| 3134 |
if attribute_stride > max_stride { |
| 3135 |
return Err( |
| 3136 |
pipeline::CreateRenderPipelineError::VertexAttributeStrideTooLarge { |
| 3137 |
location: attribute.shader_location, |
| 3138 |
given: attribute_stride as u32, |
| 3139 |
limit: max_stride as u32, |
| 3140 |
}, |
| 3141 |
); |
| 3142 |
} |
| 3143 |
|
| 3144 |
let required_offset_alignment = attribute.format.size().min(4); |
| 3145 |
if attribute.offset % required_offset_alignment != 0 { |
| 3146 |
return Err( |
| 3147 |
pipeline::CreateRenderPipelineError::InvalidVertexAttributeOffset { |
| 3148 |
location: attribute.shader_location, |
| 3149 |
offset: attribute.offset, |
| 3150 |
}, |
| 3151 |
); |
| 3152 |
} |
| 3153 |
|
| 3154 |
if attribute.shader_location >= self.limits.max_vertex_attributes { |
| 3155 |
return Err( |
| 3156 |
pipeline::CreateRenderPipelineError::TooManyVertexAttributes { |
| 3157 |
given: attribute.shader_location, |
| 3158 |
limit: self.limits.max_vertex_attributes, |
| 3159 |
}, |
| 3160 |
); |
| 3161 |
} |
| 3162 |
|
| 3163 |
last_stride = last_stride.max(attribute_stride); |
| 3164 |
} |
| 3165 |
vertex_steps.push(pipeline::VertexStep { |
| 3166 |
stride: vb_state.array_stride, |
| 3167 |
last_stride, |
| 3168 |
mode: vb_state.step_mode, |
| 3169 |
}); |
| 3170 |
if vb_state.attributes.is_empty() { |
| 3171 |
continue; |
| 3172 |
} |
| 3173 |
vertex_buffers.push(hal::VertexBufferLayout { |
| 3174 |
array_stride: vb_state.array_stride, |
| 3175 |
step_mode: vb_state.step_mode, |
| 3176 |
attributes: vb_state.attributes.as_ref(), |
| 3177 |
}); |
| 3178 |
|
| 3179 |
for attribute in vb_state.attributes.iter() { |
| 3180 |
if attribute.offset >= 0x10000000 { |
| 3181 |
return Err( |
| 3182 |
pipeline::CreateRenderPipelineError::InvalidVertexAttributeOffset { |
| 3183 |
location: attribute.shader_location, |
| 3184 |
offset: attribute.offset, |
| 3185 |
}, |
| 3186 |
); |
| 3187 |
} |
| 3188 |
|
| 3189 |
if let wgt::VertexFormat::Float64 |
| 3190 |
| wgt::VertexFormat::Float64x2 |
| 3191 |
| wgt::VertexFormat::Float64x3 |
| 3192 |
| wgt::VertexFormat::Float64x4 = attribute.format |
| 3193 |
{ |
| 3194 |
self.require_features(wgt::Features::VERTEX_ATTRIBUTE_64BIT)?; |
| 3195 |
} |
| 3196 |
|
| 3197 |
let previous = io.insert( |
| 3198 |
attribute.shader_location, |
| 3199 |
validation::InterfaceVar::vertex_attribute(attribute.format), |
| 3200 |
); |
| 3201 |
|
| 3202 |
if previous.is_some() { |
| 3203 |
return Err(pipeline::CreateRenderPipelineError::ShaderLocationClash( |
| 3204 |
attribute.shader_location, |
| 3205 |
)); |
| 3206 |
} |
| 3207 |
} |
| 3208 |
total_attributes += vb_state.attributes.len(); |
| 3209 |
} |
| 3210 |
|
| 3211 |
if vertex_buffers.len() > self.limits.max_vertex_buffers as usize { |
| 3212 |
return Err(pipeline::CreateRenderPipelineError::TooManyVertexBuffers { |
| 3213 |
given: vertex_buffers.len() as u32, |
| 3214 |
limit: self.limits.max_vertex_buffers, |
| 3215 |
}); |
| 3216 |
} |
| 3217 |
if total_attributes > self.limits.max_vertex_attributes as usize { |
| 3218 |
return Err( |
| 3219 |
pipeline::CreateRenderPipelineError::TooManyVertexAttributes { |
| 3220 |
given: total_attributes as u32, |
| 3221 |
limit: self.limits.max_vertex_attributes, |
| 3222 |
}, |
| 3223 |
); |
| 3224 |
} |
| 3225 |
|
| 3226 |
if desc.primitive.strip_index_format.is_some() && !desc.primitive.topology.is_strip() { |
| 3227 |
return Err( |
| 3228 |
pipeline::CreateRenderPipelineError::StripIndexFormatForNonStripTopology { |
| 3229 |
strip_index_format: desc.primitive.strip_index_format, |
| 3230 |
topology: desc.primitive.topology, |
| 3231 |
}, |
| 3232 |
); |
| 3233 |
} |
| 3234 |
|
| 3235 |
if desc.primitive.unclipped_depth { |
| 3236 |
self.require_features(wgt::Features::DEPTH_CLIP_CONTROL)?; |
| 3237 |
} |
| 3238 |
|
| 3239 |
if desc.primitive.polygon_mode == wgt::PolygonMode::Line { |
| 3240 |
self.require_features(wgt::Features::POLYGON_MODE_LINE)?; |
| 3241 |
} |
| 3242 |
if desc.primitive.polygon_mode == wgt::PolygonMode::Point { |
| 3243 |
self.require_features(wgt::Features::POLYGON_MODE_POINT)?; |
| 3244 |
} |
| 3245 |
|
| 3246 |
if desc.primitive.conservative { |
| 3247 |
self.require_features(wgt::Features::CONSERVATIVE_RASTERIZATION)?; |
| 3248 |
} |
| 3249 |
|
| 3250 |
if desc.primitive.conservative && desc.primitive.polygon_mode != wgt::PolygonMode::Fill { |
| 3251 |
return Err( |
| 3252 |
pipeline::CreateRenderPipelineError::ConservativeRasterizationNonFillPolygonMode, |
| 3253 |
); |
| 3254 |
} |
| 3255 |
|
| 3256 |
let mut target_specified = false; |
| 3257 |
|
| 3258 |
for (i, cs) in color_targets.iter().enumerate() { |
| 3259 |
if let Some(cs) = cs.as_ref() { |
| 3260 |
target_specified = true; |
| 3261 |
let error = 'error: { |
| 3262 |
if cs.write_mask.contains_unknown_bits() { |
| 3263 |
break 'error Some(pipeline::ColorStateError::InvalidWriteMask( |
| 3264 |
cs.write_mask, |
| 3265 |
)); |
| 3266 |
} |
| 3267 |
|
| 3268 |
let format_features = self.describe_format_features(cs.format)?; |
| 3269 |
if !format_features |
| 3270 |
.allowed_usages |
| 3271 |
.contains(wgt::TextureUsages::RENDER_ATTACHMENT) |
| 3272 |
{ |
| 3273 |
break 'error Some(pipeline::ColorStateError::FormatNotRenderable( |
| 3274 |
cs.format, |
| 3275 |
)); |
| 3276 |
} |
| 3277 |
let blendable = format_features.flags.contains(Tfff::BLENDABLE); |
| 3278 |
let filterable = format_features.flags.contains(Tfff::FILTERABLE); |
| 3279 |
let adapter_specific = self |
| 3280 |
.features |
| 3281 |
.contains(wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES); |
| 3282 |
// according to WebGPU specifications the texture needs to be |
| 3283 |
// [`TextureFormatFeatureFlags::FILTERABLE`] if blending is set - use |
| 3284 |
// [`Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES`] to elude |
| 3285 |
// this limitation |
| 3286 |
if cs.blend.is_some() && (!blendable || (!filterable && !adapter_specific)) { |
| 3287 |
break 'error Some(pipeline::ColorStateError::FormatNotBlendable( |
| 3288 |
cs.format, |
| 3289 |
)); |
| 3290 |
} |
| 3291 |
if !hal::FormatAspects::from(cs.format).contains(hal::FormatAspects::COLOR) { |
| 3292 |
break 'error Some(pipeline::ColorStateError::FormatNotColor(cs.format)); |
| 3293 |
} |
| 3294 |
|
| 3295 |
if desc.multisample.count > 1 |
| 3296 |
&& !format_features |
| 3297 |
.flags |
| 3298 |
.sample_count_supported(desc.multisample.count) |
| 3299 |
{ |
| 3300 |
break 'error Some(pipeline::ColorStateError::InvalidSampleCount( |
| 3301 |
desc.multisample.count, |
| 3302 |
cs.format, |
| 3303 |
cs.format |
| 3304 |
.guaranteed_format_features(self.features) |
| 3305 |
.flags |
| 3306 |
.supported_sample_counts(), |
| 3307 |
self.adapter |
| 3308 |
.get_texture_format_features(cs.format) |
| 3309 |
.flags |
| 3310 |
.supported_sample_counts(), |
| 3311 |
)); |
| 3312 |
} |
| 3313 |
|
| 3314 |
if let Some(blend_mode) = cs.blend { |
| 3315 |
for factor in [ |
| 3316 |
blend_mode.color.src_factor, |
| 3317 |
blend_mode.color.dst_factor, |
| 3318 |
blend_mode.alpha.src_factor, |
| 3319 |
blend_mode.alpha.dst_factor, |
| 3320 |
] { |
| 3321 |
if factor.ref_second_blend_source() { |
| 3322 |
self.require_features(wgt::Features::DUAL_SOURCE_BLENDING)?; |
| 3323 |
if i == 0 { |
| 3324 |
pipeline_expects_dual_source_blending = true; |
| 3325 |
break; |
| 3326 |
} else { |
| 3327 |
return Err(pipeline::CreateRenderPipelineError |
| 3328 |
::BlendFactorOnUnsupportedTarget { factor, target: i as u32 }); |
| 3329 |
} |
| 3330 |
} |
| 3331 |
} |
| 3332 |
} |
| 3333 |
|
| 3334 |
break 'error None; |
| 3335 |
}; |
| 3336 |
if let Some(e) = error { |
| 3337 |
return Err(pipeline::CreateRenderPipelineError::ColorState(i as u8, e)); |
| 3338 |
} |
| 3339 |
} |
| 3340 |
} |
| 3341 |
|
| 3342 |
let limit = self.limits.max_color_attachment_bytes_per_sample; |
| 3343 |
let formats = color_targets |
| 3344 |
.iter() |
| 3345 |
.map(|cs| cs.as_ref().map(|cs| cs.format)); |
| 3346 |
if let Err(total) = validate_color_attachment_bytes_per_sample(formats, limit) { |
| 3347 |
return Err(pipeline::CreateRenderPipelineError::ColorAttachment( |
| 3348 |
command::ColorAttachmentError::TooManyBytesPerSample { total, limit }, |
| 3349 |
)); |
| 3350 |
} |
| 3351 |
|
| 3352 |
if let Some(ds) = depth_stencil_state { |
| 3353 |
target_specified = true; |
| 3354 |
let error = 'error: { |
| 3355 |
let format_features = self.describe_format_features(ds.format)?; |
| 3356 |
if !format_features |
| 3357 |
.allowed_usages |
| 3358 |
.contains(wgt::TextureUsages::RENDER_ATTACHMENT) |
| 3359 |
{ |
| 3360 |
break 'error Some(pipeline::DepthStencilStateError::FormatNotRenderable( |
| 3361 |
ds.format, |
| 3362 |
)); |
| 3363 |
} |
| 3364 |
|
| 3365 |
let aspect = hal::FormatAspects::from(ds.format); |
| 3366 |
if ds.is_depth_enabled() && !aspect.contains(hal::FormatAspects::DEPTH) { |
| 3367 |
break 'error Some(pipeline::DepthStencilStateError::FormatNotDepth(ds.format)); |
| 3368 |
} |
| 3369 |
if ds.stencil.is_enabled() && !aspect.contains(hal::FormatAspects::STENCIL) { |
| 3370 |
break 'error Some(pipeline::DepthStencilStateError::FormatNotStencil( |
| 3371 |
ds.format, |
| 3372 |
)); |
| 3373 |
} |
| 3374 |
if desc.multisample.count > 1 |
| 3375 |
&& !format_features |
| 3376 |
.flags |
| 3377 |
.sample_count_supported(desc.multisample.count) |
| 3378 |
{ |
| 3379 |
break 'error Some(pipeline::DepthStencilStateError::InvalidSampleCount( |
| 3380 |
desc.multisample.count, |
| 3381 |
ds.format, |
| 3382 |
ds.format |
| 3383 |
.guaranteed_format_features(self.features) |
| 3384 |
.flags |
| 3385 |
.supported_sample_counts(), |
| 3386 |
self.adapter |
| 3387 |
.get_texture_format_features(ds.format) |
| 3388 |
.flags |
| 3389 |
.supported_sample_counts(), |
| 3390 |
)); |
| 3391 |
} |
| 3392 |
|
| 3393 |
break 'error None; |
| 3394 |
}; |
| 3395 |
if let Some(e) = error { |
| 3396 |
return Err(pipeline::CreateRenderPipelineError::DepthStencilState(e)); |
| 3397 |
} |
| 3398 |
|
| 3399 |
if ds.bias.clamp != 0.0 { |
| 3400 |
self.require_downlevel_flags(wgt::DownlevelFlags::DEPTH_BIAS_CLAMP)?; |
| 3401 |
} |
| 3402 |
} |
| 3403 |
|
| 3404 |
if !target_specified { |
| 3405 |
return Err(pipeline::CreateRenderPipelineError::NoTargetSpecified); |
| 3406 |
} |
| 3407 |
|
| 3408 |
let is_auto_layout = desc.layout.is_none(); |
| 3409 |
|
| 3410 |
// Get the pipeline layout from the desc if it is provided. |
| 3411 |
let pipeline_layout = match desc.layout { |
| 3412 |
Some(pipeline_layout) => { |
| 3413 |
pipeline_layout.same_device(self)?; |
| 3414 |
Some(pipeline_layout) |
| 3415 |
} |
| 3416 |
None => None, |
| 3417 |
}; |
| 3418 |
|
| 3419 |
let mut binding_layout_source = match pipeline_layout { |
| 3420 |
Some(ref pipeline_layout) => { |
| 3421 |
validation::BindingLayoutSource::Provided(pipeline_layout.get_binding_maps()) |
| 3422 |
} |
| 3423 |
None => validation::BindingLayoutSource::new_derived(&self.limits), |
| 3424 |
}; |
| 3425 |
|
| 3426 |
let samples = { |
| 3427 |
let sc = desc.multisample.count; |
| 3428 |
if sc == 0 || sc > 32 || !sc.is_power_of_two() { |
| 3429 |
return Err(pipeline::CreateRenderPipelineError::InvalidSampleCount(sc)); |
| 3430 |
} |
| 3431 |
sc |
| 3432 |
}; |
| 3433 |
|
| 3434 |
let vertex_entry_point_name; |
| 3435 |
let vertex_stage = { |
| 3436 |
let stage_desc = &desc.vertex.stage; |
| 3437 |
let stage = wgt::ShaderStages::VERTEX; |
| 3438 |
|
| 3439 |
let vertex_shader_module = &stage_desc.module; |
| 3440 |
vertex_shader_module.same_device(self)?; |
| 3441 |
|
| 3442 |
let stage_err = |error| pipeline::CreateRenderPipelineError::Stage { stage, error }; |
| 3443 |
|
| 3444 |
vertex_entry_point_name = vertex_shader_module |
| 3445 |
.finalize_entry_point_name( |
| 3446 |
stage, |
| 3447 |
stage_desc.entry_point.as_ref().map(|ep| ep.as_ref()), |
| 3448 |
) |
| 3449 |
.map_err(stage_err)?; |
| 3450 |
|
| 3451 |
if let Some(ref interface) = vertex_shader_module.interface { |
| 3452 |
io = interface |
| 3453 |
.check_stage( |
| 3454 |
&mut binding_layout_source, |
| 3455 |
&mut shader_binding_sizes, |
| 3456 |
&vertex_entry_point_name, |
| 3457 |
stage, |
| 3458 |
io, |
| 3459 |
desc.depth_stencil.as_ref().map(|d| d.depth_compare), |
| 3460 |
) |
| 3461 |
.map_err(stage_err)?; |
| 3462 |
validated_stages |= stage; |
| 3463 |
} |
| 3464 |
|
| 3465 |
hal::ProgrammableStage { |
| 3466 |
module: vertex_shader_module.raw(), |
| 3467 |
entry_point: &vertex_entry_point_name, |
| 3468 |
constants: &stage_desc.constants, |
| 3469 |
zero_initialize_workgroup_memory: stage_desc.zero_initialize_workgroup_memory, |
| 3470 |
} |
| 3471 |
}; |
| 3472 |
|
| 3473 |
let fragment_entry_point_name; |
| 3474 |
let fragment_stage = match desc.fragment { |
| 3475 |
Some(ref fragment_state) => { |
| 3476 |
let stage = wgt::ShaderStages::FRAGMENT; |
| 3477 |
|
| 3478 |
let shader_module = &fragment_state.stage.module; |
| 3479 |
shader_module.same_device(self)?; |
| 3480 |
|
| 3481 |
let stage_err = |error| pipeline::CreateRenderPipelineError::Stage { stage, error }; |
| 3482 |
|
| 3483 |
fragment_entry_point_name = shader_module |
| 3484 |
.finalize_entry_point_name( |
| 3485 |
stage, |
| 3486 |
fragment_state |
| 3487 |
.stage |
| 3488 |
.entry_point |
| 3489 |
.as_ref() |
| 3490 |
.map(|ep| ep.as_ref()), |
| 3491 |
) |
| 3492 |
.map_err(stage_err)?; |
| 3493 |
|
| 3494 |
if validated_stages == wgt::ShaderStages::VERTEX { |
| 3495 |
if let Some(ref interface) = shader_module.interface { |
| 3496 |
io = interface |
| 3497 |
.check_stage( |
| 3498 |
&mut binding_layout_source, |
| 3499 |
&mut shader_binding_sizes, |
| 3500 |
&fragment_entry_point_name, |
| 3501 |
stage, |
| 3502 |
io, |
| 3503 |
desc.depth_stencil.as_ref().map(|d| d.depth_compare), |
| 3504 |
) |
| 3505 |
.map_err(stage_err)?; |
| 3506 |
validated_stages |= stage; |
| 3507 |
} |
| 3508 |
} |
| 3509 |
|
| 3510 |
if let Some(ref interface) = shader_module.interface { |
| 3511 |
shader_expects_dual_source_blending = interface |
| 3512 |
.fragment_uses_dual_source_blending(&fragment_entry_point_name) |
| 3513 |
.map_err(|error| pipeline::CreateRenderPipelineError::Stage { |
| 3514 |
stage, |
| 3515 |
error, |
| 3516 |
})?; |
| 3517 |
} |
| 3518 |
|
| 3519 |
Some(hal::ProgrammableStage { |
| 3520 |
module: shader_module.raw(), |
| 3521 |
entry_point: &fragment_entry_point_name, |
| 3522 |
constants: &fragment_state.stage.constants, |
| 3523 |
zero_initialize_workgroup_memory: fragment_state |
| 3524 |
.stage |
| 3525 |
.zero_initialize_workgroup_memory, |
| 3526 |
}) |
| 3527 |
} |
| 3528 |
None => None, |
| 3529 |
}; |
| 3530 |
|
| 3531 |
if !pipeline_expects_dual_source_blending && shader_expects_dual_source_blending { |
| 3532 |
return Err( |
| 3533 |
pipeline::CreateRenderPipelineError::ShaderExpectsPipelineToUseDualSourceBlending, |
| 3534 |
); |
| 3535 |
} |
| 3536 |
if pipeline_expects_dual_source_blending && !shader_expects_dual_source_blending { |
| 3537 |
return Err( |
| 3538 |
pipeline::CreateRenderPipelineError::PipelineExpectsShaderToUseDualSourceBlending, |
| 3539 |
); |
| 3540 |
} |
| 3541 |
|
| 3542 |
if validated_stages.contains(wgt::ShaderStages::FRAGMENT) { |
| 3543 |
for (i, output) in io.iter() { |
| 3544 |
match color_targets.get(*i as usize) { |
| 3545 |
Some(Some(state)) => { |
| 3546 |
validation::check_texture_format(state.format, &output.ty).map_err( |
| 3547 |
|pipeline| { |
| 3548 |
pipeline::CreateRenderPipelineError::ColorState( |
| 3549 |
*i as u8, |
| 3550 |
pipeline::ColorStateError::IncompatibleFormat { |
| 3551 |
pipeline, |
| 3552 |
shader: output.ty, |
| 3553 |
}, |
| 3554 |
) |
| 3555 |
}, |
| 3556 |
)?; |
| 3557 |
} |
| 3558 |
_ => { |
| 3559 |
log::warn!( |
| 3560 |
"The fragment stage {:?} output @location({}) values are ignored", |
| 3561 |
fragment_stage |
| 3562 |
.as_ref() |
| 3563 |
.map_or("", |stage| stage.entry_point), |
| 3564 |
i |
| 3565 |
); |
| 3566 |
} |
| 3567 |
} |
| 3568 |
} |
| 3569 |
} |
| 3570 |
let last_stage = match desc.fragment { |
| 3571 |
Some(_) => wgt::ShaderStages::FRAGMENT, |
| 3572 |
None => wgt::ShaderStages::VERTEX, |
| 3573 |
}; |
| 3574 |
if is_auto_layout && !validated_stages.contains(last_stage) { |
| 3575 |
return Err(pipeline::ImplicitLayoutError::ReflectionError(last_stage).into()); |
| 3576 |
} |
| 3577 |
|
| 3578 |
let pipeline_layout = match binding_layout_source { |
| 3579 |
validation::BindingLayoutSource::Provided(_) => { |
| 3580 |
drop(binding_layout_source); |
| 3581 |
pipeline_layout.unwrap() |
| 3582 |
} |
| 3583 |
validation::BindingLayoutSource::Derived(entries) => { |
| 3584 |
self.derive_pipeline_layout(entries)? |
| 3585 |
} |
| 3586 |
}; |
| 3587 |
|
| 3588 |
// Multiview is only supported if the feature is enabled |
| 3589 |
if desc.multiview.is_some() { |
| 3590 |
self.require_features(wgt::Features::MULTIVIEW)?; |
| 3591 |
} |
| 3592 |
|
| 3593 |
if !self |
| 3594 |
.downlevel |
| 3595 |
.flags |
| 3596 |
.contains(wgt::DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED) |
| 3597 |
{ |
| 3598 |
for (binding, size) in shader_binding_sizes.iter() { |
| 3599 |
if size.get() % 16 != 0 { |
| 3600 |
return Err(pipeline::CreateRenderPipelineError::UnalignedShader { |
| 3601 |
binding: binding.binding, |
| 3602 |
group: binding.group, |
| 3603 |
size: size.get(), |
| 3604 |
}); |
| 3605 |
} |
| 3606 |
} |
| 3607 |
} |
| 3608 |
|
| 3609 |
let late_sized_buffer_groups = |
| 3610 |
Device::make_late_sized_buffer_groups(&shader_binding_sizes, &pipeline_layout); |
| 3611 |
|
| 3612 |
let cache = match desc.cache { |
| 3613 |
Some(cache) => { |
| 3614 |
cache.same_device(self)?; |
| 3615 |
Some(cache) |
| 3616 |
} |
| 3617 |
None => None, |
| 3618 |
}; |
| 3619 |
|
| 3620 |
let pipeline_desc = hal::RenderPipelineDescriptor { |
| 3621 |
label: desc.label.to_hal(self.instance_flags), |
| 3622 |
layout: pipeline_layout.raw(), |
| 3623 |
vertex_buffers: &vertex_buffers, |
| 3624 |
vertex_stage, |
| 3625 |
primitive: desc.primitive, |
| 3626 |
depth_stencil: desc.depth_stencil.clone(), |
| 3627 |
multisample: desc.multisample, |
| 3628 |
fragment_stage, |
| 3629 |
color_targets, |
| 3630 |
multiview: desc.multiview, |
| 3631 |
cache: cache.as_ref().map(|it| it.raw()), |
| 3632 |
}; |
| 3633 |
let raw = |
| 3634 |
unsafe { self.raw().create_render_pipeline(&pipeline_desc) }.map_err( |
| 3635 |
|err| match err { |
| 3636 |
hal::PipelineError::Device(error) => { |
| 3637 |
pipeline::CreateRenderPipelineError::Device(self.handle_hal_error(error)) |
| 3638 |
} |
| 3639 |
hal::PipelineError::Linkage(stage, msg) => { |
| 3640 |
pipeline::CreateRenderPipelineError::Internal { stage, error: msg } |
| 3641 |
} |
| 3642 |
hal::PipelineError::EntryPoint(stage) => { |
| 3643 |
pipeline::CreateRenderPipelineError::Internal { |
| 3644 |
stage: hal::auxil::map_naga_stage(stage), |
| 3645 |
error: ENTRYPOINT_FAILURE_ERROR.to_string(), |
| 3646 |
} |
| 3647 |
} |
| 3648 |
hal::PipelineError::PipelineConstants(stage, error) => { |
| 3649 |
pipeline::CreateRenderPipelineError::PipelineConstants { stage, error } |
| 3650 |
} |
| 3651 |
}, |
| 3652 |
)?; |
| 3653 |
|
| 3654 |
let pass_context = RenderPassContext { |
| 3655 |
attachments: AttachmentData { |
| 3656 |
colors: color_targets |
| 3657 |
.iter() |
| 3658 |
.map(|state| state.as_ref().map(|s| s.format)) |
| 3659 |
.collect(), |
| 3660 |
resolves: ArrayVec::new(), |
| 3661 |
depth_stencil: depth_stencil_state.as_ref().map(|state| state.format), |
| 3662 |
}, |
| 3663 |
sample_count: samples, |
| 3664 |
multiview: desc.multiview, |
| 3665 |
}; |
| 3666 |
|
| 3667 |
let mut flags = pipeline::PipelineFlags::empty(); |
| 3668 |
for state in color_targets.iter().filter_map(|s| s.as_ref()) { |
| 3669 |
if let Some(ref bs) = state.blend { |
| 3670 |
if bs.color.uses_constant() | bs.alpha.uses_constant() { |
| 3671 |
flags |= pipeline::PipelineFlags::BLEND_CONSTANT; |
| 3672 |
} |
| 3673 |
} |
| 3674 |
} |
| 3675 |
if let Some(ds) = depth_stencil_state.as_ref() { |
| 3676 |
if ds.stencil.is_enabled() && ds.stencil.needs_ref_value() { |
| 3677 |
flags |= pipeline::PipelineFlags::STENCIL_REFERENCE; |
| 3678 |
} |
| 3679 |
if !ds.is_depth_read_only() { |
| 3680 |
flags |= pipeline::PipelineFlags::WRITES_DEPTH; |
| 3681 |
} |
| 3682 |
if !ds.is_stencil_read_only(desc.primitive.cull_mode) { |
| 3683 |
flags |= pipeline::PipelineFlags::WRITES_STENCIL; |
| 3684 |
} |
| 3685 |
} |
| 3686 |
|
| 3687 |
let shader_modules = { |
| 3688 |
let mut shader_modules = ArrayVec::new(); |
| 3689 |
shader_modules.push(desc.vertex.stage.module); |
| 3690 |
shader_modules.extend(desc.fragment.map(|f| f.stage.module)); |
| 3691 |
shader_modules |
| 3692 |
}; |
| 3693 |
|
| 3694 |
let pipeline = pipeline::RenderPipeline { |
| 3695 |
raw: ManuallyDrop::new(raw), |
| 3696 |
layout: pipeline_layout, |
| 3697 |
device: self.clone(), |
| 3698 |
pass_context, |
| 3699 |
_shader_modules: shader_modules, |
| 3700 |
flags, |
| 3701 |
strip_index_format: desc.primitive.strip_index_format, |
| 3702 |
vertex_steps, |
| 3703 |
late_sized_buffer_groups, |
| 3704 |
label: desc.label.to_string(), |
| 3705 |
tracking_data: TrackingData::new(self.tracker_indices.render_pipelines.clone()), |
| 3706 |
}; |
| 3707 |
|
| 3708 |
let pipeline = Arc::new(pipeline); |
| 3709 |
|
| 3710 |
if is_auto_layout { |
| 3711 |
for bgl in pipeline.layout.bind_group_layouts.iter() { |
| 3712 |
// `bind_group_layouts` might contain duplicate entries, so we need to ignore the result. |
| 3713 |
let _ = bgl |
| 3714 |
.exclusive_pipeline |
| 3715 |
.set(binding_model::ExclusivePipeline::Render(Arc::downgrade( |
| 3716 |
&pipeline, |
| 3717 |
))); |
| 3718 |
} |
| 3719 |
} |
| 3720 |
|
| 3721 |
Ok(pipeline) |
| 3722 |
} |
| 3723 |
|
| 3724 |
/// # Safety |
| 3725 |
/// The `data` field on `desc` must have previously been returned from [`crate::global::Global::pipeline_cache_get_data`] |
| 3726 |
pub unsafe fn create_pipeline_cache( |
| 3727 |
self: &Arc<Self>, |
| 3728 |
desc: &pipeline::PipelineCacheDescriptor, |
| 3729 |
) -> Result<Arc<pipeline::PipelineCache>, pipeline::CreatePipelineCacheError> { |
| 3730 |
use crate::pipeline_cache; |
| 3731 |
|
| 3732 |
self.check_is_valid()?; |
| 3733 |
|
| 3734 |
self.require_features(wgt::Features::PIPELINE_CACHE)?; |
| 3735 |
let data = if let Some((data, validation_key)) = desc |
| 3736 |
.data |
| 3737 |
.as_ref() |
| 3738 |
.zip(self.raw().pipeline_cache_validation_key()) |
| 3739 |
{ |
| 3740 |
let data = pipeline_cache::validate_pipeline_cache( |
| 3741 |
data, |
| 3742 |
&self.adapter.raw.info, |
| 3743 |
validation_key, |
| 3744 |
); |
| 3745 |
match data { |
| 3746 |
Ok(data) => Some(data), |
| 3747 |
Err(e) if e.was_avoidable() || !desc.fallback => return Err(e.into()), |
| 3748 |
// If the error was unavoidable and we are asked to fallback, do so |
| 3749 |
Err(_) => None, |
| 3750 |
} |
| 3751 |
} else { |
| 3752 |
None |
| 3753 |
}; |
| 3754 |
let cache_desc = hal::PipelineCacheDescriptor { |
| 3755 |
data, |
| 3756 |
label: desc.label.to_hal(self.instance_flags), |
| 3757 |
}; |
| 3758 |
let raw = match unsafe { self.raw().create_pipeline_cache(&cache_desc) } { |
| 3759 |
Ok(raw) => raw, |
| 3760 |
Err(e) => match e { |
| 3761 |
hal::PipelineCacheError::Device(e) => return Err(self.handle_hal_error(e).into()), |
| 3762 |
}, |
| 3763 |
}; |
| 3764 |
let cache = pipeline::PipelineCache { |
| 3765 |
device: self.clone(), |
| 3766 |
label: desc.label.to_string(), |
| 3767 |
// This would be none in the error condition, which we don't implement yet |
| 3768 |
raw: ManuallyDrop::new(raw), |
| 3769 |
}; |
| 3770 |
|
| 3771 |
let cache = Arc::new(cache); |
| 3772 |
|
| 3773 |
Ok(cache) |
| 3774 |
} |
| 3775 |
|
| 3776 |
fn get_texture_format_features(&self, format: TextureFormat) -> wgt::TextureFormatFeatures { |
| 3777 |
// Variant of adapter.get_texture_format_features that takes device features into account |
| 3778 |
use wgt::TextureFormatFeatureFlags as tfsc; |
| 3779 |
let mut format_features = self.adapter.get_texture_format_features(format); |
| 3780 |
if (format == TextureFormat::R32Float |
| 3781 |
|| format == TextureFormat::Rg32Float |
| 3782 |
|| format == TextureFormat::Rgba32Float) |
| 3783 |
&& !self.features.contains(wgt::Features::FLOAT32_FILTERABLE) |
| 3784 |
{ |
| 3785 |
format_features.flags.set(tfsc::FILTERABLE, false); |
| 3786 |
} |
| 3787 |
format_features |
| 3788 |
} |
| 3789 |
|
| 3790 |
fn describe_format_features( |
| 3791 |
&self, |
| 3792 |
format: TextureFormat, |
| 3793 |
) -> Result<wgt::TextureFormatFeatures, MissingFeatures> { |
| 3794 |
self.require_features(format.required_features())?; |
| 3795 |
|
| 3796 |
let using_device_features = self |
| 3797 |
.features |
| 3798 |
.contains(wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES); |
| 3799 |
// If we're running downlevel, we need to manually ask the backend what |
| 3800 |
// we can use as we can't trust WebGPU. |
| 3801 |
let downlevel = !self |
| 3802 |
.downlevel |
| 3803 |
.flags |
| 3804 |
.contains(wgt::DownlevelFlags::WEBGPU_TEXTURE_FORMAT_SUPPORT); |
| 3805 |
|
| 3806 |
if using_device_features || downlevel { |
| 3807 |
Ok(self.get_texture_format_features(format)) |
| 3808 |
} else { |
| 3809 |
Ok(format.guaranteed_format_features(self.features)) |
| 3810 |
} |
| 3811 |
} |
| 3812 |
|
| 3813 |
#[cfg(feature = "replay")] |
| 3814 |
pub(crate) fn wait_for_submit( |
| 3815 |
&self, |
| 3816 |
submission_index: crate::SubmissionIndex, |
| 3817 |
) -> Result<(), DeviceError> { |
| 3818 |
let fence = self.fence.read(); |
| 3819 |
let last_done_index = unsafe { self.raw().get_fence_value(fence.as_ref()) } |
| 3820 |
.map_err(|e| self.handle_hal_error(e))?; |
| 3821 |
if last_done_index < submission_index { |
| 3822 |
unsafe { self.raw().wait(fence.as_ref(), submission_index, !0) } |
| 3823 |
.map_err(|e| self.handle_hal_error(e))?; |
| 3824 |
drop(fence); |
| 3825 |
if let Some(queue) = self.get_queue() { |
| 3826 |
let closures = queue.lock_life().triage_submissions(submission_index); |
| 3827 |
assert!( |
| 3828 |
closures.is_empty(), |
| 3829 |
"wait_for_submit is not expected to work with closures" |
| 3830 |
); |
| 3831 |
} |
| 3832 |
} |
| 3833 |
Ok(()) |
| 3834 |
} |
| 3835 |
|
| 3836 |
pub(crate) fn create_query_set( |
| 3837 |
self: &Arc<Self>, |
| 3838 |
desc: &resource::QuerySetDescriptor, |
| 3839 |
) -> Result<Arc<QuerySet>, resource::CreateQuerySetError> { |
| 3840 |
use resource::CreateQuerySetError as Error; |
| 3841 |
|
| 3842 |
self.check_is_valid()?; |
| 3843 |
|
| 3844 |
match desc.ty { |
| 3845 |
wgt::QueryType::Occlusion => {} |
| 3846 |
wgt::QueryType::Timestamp => { |
| 3847 |
self.require_features(wgt::Features::TIMESTAMP_QUERY)?; |
| 3848 |
} |
| 3849 |
wgt::QueryType::PipelineStatistics(..) => { |
| 3850 |
self.require_features(wgt::Features::PIPELINE_STATISTICS_QUERY)?; |
| 3851 |
} |
| 3852 |
} |
| 3853 |
|
| 3854 |
if desc.count == 0 { |
| 3855 |
return Err(Error::ZeroCount); |
| 3856 |
} |
| 3857 |
|
| 3858 |
if desc.count > wgt::QUERY_SET_MAX_QUERIES { |
| 3859 |
return Err(Error::TooManyQueries { |
| 3860 |
count: desc.count, |
| 3861 |
maximum: wgt::QUERY_SET_MAX_QUERIES, |
| 3862 |
}); |
| 3863 |
} |
| 3864 |
|
| 3865 |
let hal_desc = desc.map_label(|label| label.to_hal(self.instance_flags)); |
| 3866 |
|
| 3867 |
let raw = unsafe { self.raw().create_query_set(&hal_desc) } |
| 3868 |
.map_err(|e| self.handle_hal_error_with_nonfatal_oom(e))?; |
| 3869 |
|
| 3870 |
let query_set = QuerySet { |
| 3871 |
raw: ManuallyDrop::new(raw), |
| 3872 |
device: self.clone(), |
| 3873 |
label: desc.label.to_string(), |
| 3874 |
tracking_data: TrackingData::new(self.tracker_indices.query_sets.clone()), |
| 3875 |
desc: desc.map_label(|_| ()), |
| 3876 |
}; |
| 3877 |
|
| 3878 |
let query_set = Arc::new(query_set); |
| 3879 |
|
| 3880 |
Ok(query_set) |
| 3881 |
} |
| 3882 |
|
| 3883 |
fn lose(&self, message: &str) { |
| 3884 |
// Follow the steps at https://gpuweb.github.io/gpuweb/#lose-the-device. |
| 3885 |
|
| 3886 |
// Mark the device explicitly as invalid. This is checked in various |
| 3887 |
// places to prevent new work from being submitted. |
| 3888 |
self.valid.store(false, Ordering::Release); |
| 3889 |
|
| 3890 |
// 1) Resolve the GPUDevice device.lost promise. |
| 3891 |
if let Some(device_lost_closure) = self.device_lost_closure.lock().take() { |
| 3892 |
device_lost_closure(DeviceLostReason::Unknown, message.to_string()); |
| 3893 |
} |
| 3894 |
|
| 3895 |
// 2) Complete any outstanding mapAsync() steps. |
| 3896 |
// 3) Complete any outstanding onSubmittedWorkDone() steps. |
| 3897 |
|
| 3898 |
// These parts are passively accomplished by setting valid to false, |
| 3899 |
// since that will prevent any new work from being added to the queues. |
| 3900 |
// Future calls to poll_devices will continue to check the work queues |
| 3901 |
// until they are cleared, and then drop the device. |
| 3902 |
} |
| 3903 |
|
| 3904 |
fn release_gpu_resources(&self) { |
| 3905 |
// This is called when the device is lost, which makes every associated |
| 3906 |
// resource invalid and unusable. This is an opportunity to release all of |
| 3907 |
// the underlying gpu resources, even though the objects remain visible to |
| 3908 |
// the user agent. We purge this memory naturally when resources have been |
| 3909 |
// moved into the appropriate buckets, so this function just needs to |
| 3910 |
// initiate movement into those buckets, and it can do that by calling |
| 3911 |
// "destroy" on all the resources we know about. |
| 3912 |
|
| 3913 |
// During these iterations, we discard all errors. We don't care! |
| 3914 |
let trackers = self.trackers.lock(); |
| 3915 |
for buffer in trackers.buffers.used_resources() { |
| 3916 |
if let Some(buffer) = Weak::upgrade(buffer) { |
| 3917 |
let _ = buffer.destroy(); |
| 3918 |
} |
| 3919 |
} |
| 3920 |
for texture in trackers.textures.used_resources() { |
| 3921 |
if let Some(texture) = Weak::upgrade(texture) { |
| 3922 |
let _ = texture.destroy(); |
| 3923 |
} |
| 3924 |
} |
| 3925 |
} |
| 3926 |
|
| 3927 |
pub(crate) fn new_usage_scope(&self) -> UsageScope<'_> { |
| 3928 |
UsageScope::new_pooled(&self.usage_scopes, &self.tracker_indices) |
| 3929 |
} |
| 3930 |
|
| 3931 |
pub fn get_hal_counters(&self) -> wgt::HalCounters { |
| 3932 |
self.raw().get_internal_counters() |
| 3933 |
} |
| 3934 |
|
| 3935 |
pub fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> { |
| 3936 |
self.raw().generate_allocator_report() |
| 3937 |
} |
| 3938 |
} |
| 3939 |
|
| 3940 |
crate::impl_resource_type!(Device); |
| 3941 |
crate::impl_labeled!(Device); |
| 3942 |
crate::impl_storage_item!(Device); |