diff --git a/crates/metal2vulkan-runtime/src/d3d11.rs b/crates/metal2vulkan-runtime/src/d3d11.rs index 70813de16..7ff9227fc 100644 --- a/crates/metal2vulkan-runtime/src/d3d11.rs +++ b/crates/metal2vulkan-runtime/src/d3d11.rs @@ -15,7 +15,7 @@ use crate::hazard::HeapId; use crate::import::ImportHandle; use crate::memory; use crate::surface::{Surface, MAX_PLANES}; -use crate::texture::{Backing, SurfaceImage, Texture, TextureDescriptor}; +use crate::texture::{size_within, Backing, SurfaceImage, Texture, TextureDescriptor}; use crate::PixelFormat; use ash::vk; use metal2vulkan::metal::format as formats; @@ -153,7 +153,10 @@ impl Device { /// not import the handle (`vkGetPhysicalDeviceImageFormatProperties2`); [`Gap::SurfaceUsage`] /// for a usage the descriptor asks that the texture's bind flags do not give or the import /// does not offer (`ShaderWrite` on a texture made without `D3D11_BIND_UNORDERED_ACCESS`); - /// [`Limit::ImportType`] + /// [`Limit::TextureSize`] where a plane's extent is past the `maxExtent` the image format + /// query answers of its image with the Direct3D 11 handle type + /// (`VkPhysicalDeviceExternalImageFormatInfo`), which can be less than the query's without + /// it; [`Limit::ImportType`] /// where the handle's properties and the image's requirements share no memory type; /// [`Limit::MemoryAllocations`]; [`Error::Vulkan`] where the handle's properties, the image, /// its import or its binding fail; what the texture's index takes; [`Error::Quiesced`]. @@ -188,8 +191,9 @@ impl Device { } /// One plane's image usage and the formats it lists, which the plane's bind flags and - /// descriptor give ([`Device::import_d3d11_surface`]). - fn d3d11_plane_form( + /// descriptor give ([`Device::import_d3d11_surface`]), its extent within what the import + /// query answers of that image. + pub(crate) fn d3d11_plane_form( &self, core: &Arc, plane: &D3d11Plane<'_>, @@ -253,8 +257,9 @@ impl Device { held.contains(feature) }; let imports = |usage: vk::ImageUsageFlags, list: &[vk::Format]| { - (import_query(core, format, usage, list)) - .is_ok_and(|features| features.contains(vk::ExternalMemoryFeatureFlags::IMPORTABLE)) + (import_query(core, format, usage, list)).is_ok_and(|(features, _)| { + features.contains(vk::ExternalMemoryFeatureFlags::IMPORTABLE) + }) }; // The usage the texture's bind flags give, no other. let wanted = bound_usage(plane.bind_flags); @@ -288,6 +293,13 @@ impl Device { Some(siblings) if imports(usage, &siblings) => siblings, _ => pair, }; + // The plane's extent within what the query answers of its image with the handle, which + // can be less than the plain query's that `plane_plan` checked + // (`VUID-VkImageCreateInfo-extent-02252`: its limit is the query's with the image's + // external memory handle type). + let (_, made) = (import_query(core, format, usage, &list)) + .map_err(|_| Error::Limit(Limit::NoImport { handle }))?; + size_within([d.width, d.height, 1], 1, &made)?; Ok((usage, list)) } @@ -429,13 +441,15 @@ impl Device { /// What `vkGetPhysicalDeviceImageFormatProperties2` answers of an optimal 2D image of `format` /// and `usage` imported from a Direct3D 11 texture, mutable and listing `list` where it is not -/// empty: the handle's external memory features, or the query's error. +/// empty: the handle's external memory features and the image's properties with the handle +/// (its `maxExtent` the limit of such an image's extent, `VUID-VkImageCreateInfo-extent-02252`), +/// or the query's error. pub(crate) fn import_query( core: &Core, format: vk::Format, usage: vk::ImageUsageFlags, list: &[vk::Format], -) -> Result { +) -> Result<(vk::ExternalMemoryFeatureFlags, vk::ImageFormatProperties), vk::Result> { let (instance, physical) = (core.instance.vk(), core.physical); let mut external = vk::PhysicalDeviceExternalImageFormatInfo::default().handle_type(HANDLE); let mut listed = vk::ImageFormatListCreateInfo::default().view_formats(list); @@ -455,7 +469,9 @@ pub(crate) fn import_query( // SAFETY: `physical` is `instance`'s, and the handle's extension is enabled (the caller holds // the device's `VK_KHR_external_memory_win32`). unsafe { instance.get_physical_device_image_format_properties2(physical, &info, &mut out) }?; - Ok(external_out + let properties = out.image_format_properties; + let features = external_out .external_memory_properties - .external_memory_features) + .external_memory_features; + Ok((features, properties)) } diff --git a/crates/metal2vulkan-runtime/src/error.rs b/crates/metal2vulkan-runtime/src/error.rs index 395368f9b..a8e8a4014 100644 --- a/crates/metal2vulkan-runtime/src/error.rs +++ b/crates/metal2vulkan-runtime/src/error.rs @@ -2149,10 +2149,12 @@ pub enum Limit { }, /// A texture whose extent or array layers are past what the device makes of its format, /// type, usage and sample count. A cube's layers are six a cube. A texture buffer's width past - /// the device's texel buffer elements, its extent `[width, 1, 1]` in 1 layer. + /// the device's texel buffer elements, its extent `[width, 1, 1]` in 1 layer. A Direct3D 11 + /// plane's extent past what the device makes of its image imported from the plane's handle + /// (`Device::import_d3d11_surface`, on Windows). /// - /// Basis: device (the image format query's `maxExtent` and `maxArrayLayers`, and - /// `maxTexelBufferElements`). + /// Basis: device (the image format query's `maxExtent` and `maxArrayLayers`, an import's with + /// its handle type, and `maxTexelBufferElements`). TextureSize { /// The extent asked for. extent: [u32; 3], diff --git a/crates/metal2vulkan-runtime/src/tests/d3d11.rs b/crates/metal2vulkan-runtime/src/tests/d3d11.rs index 9c6ddbcb8..3f23130b5 100644 --- a/crates/metal2vulkan-runtime/src/tests/d3d11.rs +++ b/crates/metal2vulkan-runtime/src/tests/d3d11.rs @@ -1,6 +1,6 @@ //! The Direct3D 11 surface import, a Windows host's, under a device cfg. On every device of the -//! cfg's kind, its typed refusals: no plane, five planes, and a plane whose image format's texel -//! is not its descriptor's. Under `m2v_gpu`, on every device: a picture's two planes made as shared +//! cfg's kind, its typed refusals: no plane, five planes, a plane whose image format's texel is +//! not its descriptor's, and a plane wider than the import query's `maxExtent` for its image. Under `m2v_gpu`, on every device: a picture's two planes made as shared //! Direct3D 11 textures on the DXGI adapter of the device's LUID (luma `R8_UNORM` at 64x32, chroma //! `R16_UINT` at 32x16, which rt's texture views as `RG8Unorm`), imported by their NT handles //! with no submission. Direct3D 11 draws picture 1 (one triangle a plane, its pixel shader @@ -40,6 +40,7 @@ #[cfg(all(windows, any(m2v_gpu, m2v_cpu_icd)))] mod on_device { + use crate::d3d11::import_query; use crate::tests::command::on_device::each_device; use crate::{ D3d11Plane, Error, ImportHandle, Limit, PixelFormat, Refusal, TextureDescriptor, @@ -101,6 +102,38 @@ mod on_device { ), "{name}: a two-byte view of a one-byte texture: {error:?}" ); + // A plane one texel wider than the import query's `maxExtent` for its image with + // the handle: `Limit::TextureSize`, at that answer or the plain query's below it. + let core = device.core(); + let (usage, list) = + (device.d3d11_plane_form(core, &luma)).expect("the luma's form"); + let format = (device.plane_plan(&luma.descriptor)) + .expect("a plan") + .format; + let (_, made) = + (import_query(core, format, usage, &list)).expect("the import query"); + let most = made.max_extent.width; + let mut past = luma; + past.descriptor.width = most + 1; + let error = device.import_d3d11_surface(&[past]).err(); + println!( + "d3d11: {name}: the import query's maxExtent for the luma {:?}; a plane {} wide: \ + {error:?}", + made.max_extent, + most + 1 + ); + assert!( + matches!( + error, + Some(Error::Limit(Limit::TextureSize { + extent: [width, 32, 1], + layers: 1, + max_extent: [max, ..], + .. + })) if width == most + 1 && max <= most + ), + "{name}: a plane past the import's maxExtent: {error:?}" + ); } else { assert!( matches!( @@ -1223,7 +1256,7 @@ mod on_device { held.contains(feature) }; let imports = |usage: vk::ImageUsageFlags, list: &[vk::Format]| { - (import_query(core, format, usage, list)).is_ok_and(|features| { + (import_query(core, format, usage, list)).is_ok_and(|(features, _)| { features.contains(vk::ExternalMemoryFeatureFlags::IMPORTABLE) }) }; diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index de3a19b56..ad8fae0e5 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -2367,6 +2367,29 @@ pub(crate) fn kept_features(usage: vk::ImageUsageFlags) -> vk::FormatFeatureFlag kept } +/// Whether an image of `extent` in `layers` layers is within what an image format query answered +/// (`properties`), its `maxExtent` and `maxArrayLayers` (`VUID-VkImageCreateInfo-extent-02252`, +/// `...-arrayLayers-02256`). +/// +/// Err: [`Limit::TextureSize`] where it is past either. +pub(crate) fn size_within( + extent: [u32; 3], + layers: u32, + properties: &vk::ImageFormatProperties, +) -> Result<(), Error> { + let max = properties.max_extent; + let max_extent = [max.width, max.height, max.depth]; + if extent.iter().zip(max_extent).any(|(&e, m)| e > m) || layers > properties.max_array_layers { + return Err(Error::Limit(Limit::TextureSize { + extent, + layers, + max_extent, + max_layers: properties.max_array_layers, + })); + } + Ok(()) +} + /// The largest extent of a linear image of `plan`'s format and usage of `shape`'s type, where the /// device makes one; made for memory of the import `handle`, where the device makes one that such /// memory binds at any place: its external query (`VkPhysicalDeviceExternalImageFormatInfo`) @@ -2750,18 +2773,7 @@ fn planned( } } let properties = properties.ok_or_else(gap)?; - let max = properties.max_extent; - let max_extent = [max.width, max.height, max.depth]; - if shape.extent.iter().zip(max_extent).any(|(&e, m)| e > m) - || shape.layers > properties.max_array_layers - { - return Err(Error::Limit(Limit::TextureSize { - extent: shape.extent, - layers: shape.layers, - max_extent, - max_layers: properties.max_array_layers, - })); - } + size_within(shape.extent, shape.layers, &properties)?; if !d.sample_count.is_power_of_two() || !properties.sample_counts.contains(plan.samples) { return Err(Error::Limit(Limit::TextureSamples { sample_count: d.sample_count, diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index 79b00e2a0..66d2a3eb8 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -324,13 +324,13 @@ Columns: | `ThreadgroupThreads` | A dispatch whose threadgroup is within Metal's `maxTotalThreadsPerThreadgroup` for the kernel and past the state's on this device | at the device's limit | `device`: the device's compute work-group and shared-memory limits | 1: `crates/metal2vulkan-runtime/src/encoder.rs (size_checked)` | `-` | | `BytesLength` | `setBytes` data longer than one parameter ring chunk | every device (a public call's own check of its arguments) | `ours`: a ring chunk is 64 KiB | 2: `crates/metal2vulkan-runtime/src/encoder.rs (set_bytes)`, `crates/metal2vulkan-runtime/src/render_encoder.rs (set_bytes)` | `-` | | `IndexRange` | A grant of no indices, or one past its class's range (`Device::grant_indices`, [`IndexClass::range`]) | every device (a public call's own check of its arguments) | `ours`: the descriptor arrays are 65,536 sampled and 65,536 storage textures, one index of both a texture's, and 4,000 samplers of each use, and index 0 is nil | 1: `crates/metal2vulkan-runtime/src/arrays.rs (grant)` | `-` | -| `TextureSize` | A texture whose extent or array layers are past what the device makes of its format, type, usage and sample count | at the device's limit | `device`: the image format query's `maxExtent` and `maxArrayLayers`, and `maxTexelBufferElements` | 2: `crates/metal2vulkan-runtime/src/texture.rs (planned)`, `crates/metal2vulkan-runtime/src/texture.rs (texel_texture)` | `-` | +| `TextureSize` | A texture whose extent or array layers are past what the device makes of its format, type, usage and sample count | at the device's limit | `device`: the image format query's `maxExtent` and `maxArrayLayers`, an import's with its handle type, and `maxTexelBufferElements` | 2: `crates/metal2vulkan-runtime/src/texture.rs (size_within)`, `crates/metal2vulkan-runtime/src/texture.rs (texel_texture)` | `-` | | `DeviceSamplers` | A sampler state whose device samplers would take the device past its `maxSamplerAllocationCount`, less the sampler arrays' two defaults... | at the device's limit | `device`: the device's `maxSamplerAllocationCount` | 1: `crates/metal2vulkan-runtime/src/sampler.rs (take)` | `-` | | `LodClampVariants` | A sampler state bound with LOD clamps of its own past the variants the runtime holds at once | every device (a public call's own check of its arguments) | `ours`: the runtime's 4,000 variant elements | 1: `crates/metal2vulkan-runtime/src/arrays.rs (place_variant)` | `-` | | `LevelElements` | A dispatch or a draw that binds a texture of more than one level to a slot its function writes or reads at a level other than the constant 0 (MSL... | every device (a public call's own check of its arguments) | `ours`: the runtime's 65,536 level elements, 4,096 runs of 16 | 2: `crates/metal2vulkan-runtime/src/arrays.rs (place_levels)`, `crates/metal2vulkan-runtime/src/texture.rs (hold_levels)` | `-` | | `MemoryAllocations` | A `VkDeviceMemory` allocation past the device's `maxMemoryAllocationCount` live ones, refused before the driver is called | at the device's limit | `device`: the device's `maxMemoryAllocationCount` | 1: `crates/metal2vulkan-runtime/src/memory.rs (allocate)` | `-` | | `AllocationNotMapped` | A take that no free range fits made an allocation that the consumer's `BlockMappings::map` did not map | every device (a public call's own check of its arguments) | `ours`: the consumer's hook gave the allocation no place in its own address range | 1: `crates/metal2vulkan-runtime/src/memory.rs (own)` | `-` | -| `NoImport` | An import of a handle the device does not take (`Device::import_dma_buf` and `Device::import_dma_bufs` on Unix, `Device::import_host_memory`, a... | at the device's limit | `device`: the device's extensions, features, queue family and external buffer properties | 7: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_form)`, `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)` (+5) | `-` | +| `NoImport` | An import of a handle the device does not take (`Device::import_dma_buf` and `Device::import_dma_bufs` on Unix, `Device::import_host_memory`, a... | at the device's limit | `device`: the device's extensions, features, queue family and external buffer properties | 8: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_form, 2 sites)`, `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)` (+5) | `-` | | `ImportType` | An import whose handle's memory type query offers no type the import takes, so nothing is imported | at the device's limit | `device`: the handle's memory type query and the buffer's or image's memory requirements | 3: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)`, `crates/metal2vulkan-runtime/src/import.rs (dma_buf_type)` (+1) | `-` | | `ImportCoherence` | An import of a dma-buf (`Device::import_dma_buf` and `Device::import_dma_bufs`, on Unix) on a device whose Shared memory type, the first... | at the device's limit | `device`: the Shared memory type's `VK_MEMORY_PROPERTY_HOST_COHERENT_BIT` | 1: `crates/metal2vulkan-runtime/src/import.rs (dma_buf_type)` | `-` | | `ImportAlignment` | An import of no bytes, or one whose address or length is not a multiple of `alignment` | at the device's limit | `device`: `minImportedHostPointerAlignment` and the buffer's memory requirements | 4: `crates/metal2vulkan-runtime/src/import.rs (check_len)`, `crates/metal2vulkan-runtime/src/import.rs (check_size)` (+2) | `-` | @@ -616,13 +616,13 @@ Each refusal's sites, with the function that builds it. - `rt:Limit::ThreadgroupThreads`: `crates/metal2vulkan-runtime/src/encoder.rs (size_checked)` - `rt:Limit::BytesLength`: `crates/metal2vulkan-runtime/src/encoder.rs (set_bytes)`; `crates/metal2vulkan-runtime/src/render_encoder.rs (set_bytes)` - `rt:Limit::IndexRange`: `crates/metal2vulkan-runtime/src/arrays.rs (grant)` -- `rt:Limit::TextureSize`: `crates/metal2vulkan-runtime/src/texture.rs (planned)`; `crates/metal2vulkan-runtime/src/texture.rs (texel_texture)` +- `rt:Limit::TextureSize`: `crates/metal2vulkan-runtime/src/texture.rs (size_within)`; `crates/metal2vulkan-runtime/src/texture.rs (texel_texture)` - `rt:Limit::DeviceSamplers`: `crates/metal2vulkan-runtime/src/sampler.rs (take)` - `rt:Limit::LodClampVariants`: `crates/metal2vulkan-runtime/src/arrays.rs (place_variant)` - `rt:Limit::LevelElements`: `crates/metal2vulkan-runtime/src/arrays.rs (place_levels)`; `crates/metal2vulkan-runtime/src/texture.rs (hold_levels)` - `rt:Limit::MemoryAllocations`: `crates/metal2vulkan-runtime/src/memory.rs (allocate)` - `rt:Limit::AllocationNotMapped`: `crates/metal2vulkan-runtime/src/memory.rs (own)` -- `rt:Limit::NoImport`: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_form)`; `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)`; `crates/metal2vulkan-runtime/src/d3d11.rs (import_d3d11_surface)`; `crates/metal2vulkan-runtime/src/import.rs (import_dma_buf)`; `crates/metal2vulkan-runtime/src/import.rs (import_dma_bufs)`; `crates/metal2vulkan-runtime/src/import.rs (import_host_memory)`; `crates/metal2vulkan-runtime/src/import.rs (surface_import)` +- `rt:Limit::NoImport`: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_form, 2 sites)`; `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)`; `crates/metal2vulkan-runtime/src/d3d11.rs (import_d3d11_surface)`; `crates/metal2vulkan-runtime/src/import.rs (import_dma_buf)`; `crates/metal2vulkan-runtime/src/import.rs (import_dma_bufs)`; `crates/metal2vulkan-runtime/src/import.rs (import_host_memory)`; `crates/metal2vulkan-runtime/src/import.rs (surface_import)` - `rt:Limit::ImportType`: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_texture)`; `crates/metal2vulkan-runtime/src/import.rs (dma_buf_type)`; `crates/metal2vulkan-runtime/src/import.rs (import_host_memory)` - `rt:Limit::ImportCoherence`: `crates/metal2vulkan-runtime/src/import.rs (dma_buf_type)` - `rt:Limit::ImportAlignment`: `crates/metal2vulkan-runtime/src/import.rs (check_len)`; `crates/metal2vulkan-runtime/src/import.rs (check_size)`; `crates/metal2vulkan-runtime/src/import.rs (import_dma_bufs)`; `crates/metal2vulkan-runtime/src/import.rs (import_host_memory)`