From afa0046e9e7202ed01e61ba748886c39a7ee4db9 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 2 Dec 2026 18:53:55 -0800 Subject: [PATCH] rt test: a kernel writes an exported surface plane through a ShaderWrite R8Uint view A guard, not red. For each row the device exports (the VA-API dma-buf row, the NVIDIA opaque-handle row and the copy-form row), on the 1080p picture, a surface whose luma has ShaderRead, ShaderWrite, RenderTarget and PixelFormatView usage; an R8Uint view of the luma that fpv_rgbx_write_uint writes from a buffer of values; then the surface's buffer, copied whole to a Shared buffer, holds each written texel at the luma's offset and pitch as the export states them. In place, a device whose linear image takes no storage usage may answer Gap::SurfaceUsage at the make, or Gap::TextureView of SurfacePixelFormat at the view, each printed. This is the consumer's form for the exported planes a guest writes (row 5): the plane's own image, viewed, with no storage shape and no copy. The row 5 format queries (5080 m3-max-81308, 2080 81309, Iris Xe 81310, 780M 81314, 3090 81318, RX 550 81322) found storage offered on the plane's linear and DRM_FORMAT_MOD_LINEAR images on every card, and the layout unchanged by it; surface.rs already adds storage where the query offers it. --- .../metal2vulkan-runtime/src/tests/surface.rs | 177 ++++++++++++++++++ 1 file changed, 177 insertions(+) diff --git a/crates/metal2vulkan-runtime/src/tests/surface.rs b/crates/metal2vulkan-runtime/src/tests/surface.rs index fc4e6e1bc..5dcda353a 100644 --- a/crates/metal2vulkan-runtime/src/tests/surface.rs +++ b/crates/metal2vulkan-runtime/src/tests/surface.rs @@ -28,6 +28,9 @@ //! surface, and whether the device's buffers import a dma-buf are printed. A luma plane of //! `PixelFormatView` usage takes a swizzled `R8Uint` view, through which rt's writes read back //! exactly through the plane's own texture. +//! A luma plane of `ShaderWrite` and `PixelFormatView` usage takes an `R8Uint` view that a +//! kernel writes, and the surface's buffer holds each texel it wrote at the plane's offset and +//! pitch. //! //! The second device stands in for the other API, as in the export tests: a device of another //! instance cannot run under the validation layer. @@ -2293,6 +2296,180 @@ mod on_device { }); } + /// A surface plane written by a kernel through a `ShaderWrite` `R8Uint` view: for each row the + /// device exports, on the 1080p picture, a surface whose luma's texture has `ShaderRead`, + /// `ShaderWrite`, `RenderTarget` and `PixelFormatView` usage, reached through its views alone, + /// then an `R8Uint` view of the luma, which `fpv_rgbx_write_uint` (the owned fixture + /// `fpv_rgbx_textures`) writes, texel by texel, from a buffer of values. The luma's bytes read + /// back exactly through the surface's buffer, at the offset and pitch the export states. Only + /// in place may a device answer otherwise: `Gap::SurfaceUsage` at the make, where its linear + /// image takes no storage usage, or `Gap::TextureView` of `SurfacePixelFormat` at the view, + /// where its image does not list `R8Uint`. Printed: the form and the answer. + #[test] + fn every_device_writes_a_surface_plane_through_a_shader_write_uint_view() { + let ll = fixtures_dir().join("engine/fpv_rgbx_textures.ll"); + let library = match llvm_assemble(&ll) { + Ok(bitcode) => Library::from_air(&bitcode).expect("the library"), + Err(AssembleError::Missing(why)) => { + eprintln!("SKIPPED: the plane's storage write: {why}"); + return; + } + Err(error) => panic!("{}: {error:?}", ll.display()), + }; + each_device("surface_storage", |name, _, device| { + let queue = device.new_command_queue(1).expect("a queue"); + let function = library.function("fpv_rgbx_write_uint").expect("the kernel"); + let descriptor = ComputePipelineDescriptor::new(StageSource::Function( + FunctionDescriptor::new(function), + )); + let write = (device + .new_compute_pipeline_state(&descriptor, crate::tests::no_constexpr_samplers)) + .unwrap_or_else(|error| panic!("{name}: the kernel: {error}")); + let (picture, planes) = &PICTURES[0]; + let luma = planes[0]; + let texels = u64::from(luma.width) * u64::from(luma.height); + // Texel `i`'s value, below 256 so that `R8Uint` holds it exactly. + let value = |i: u64| (i * 37 + 11) as u8; + let (_values_block, values) = shared(&device, texels * 16); + fill(&values, |bytes| { + for (i, texel) in bytes.chunks_exact_mut(16).enumerate() { + texel.fill(0); + texel[0] = value(i as u64); + } + }); + let (_width_block, width) = shared(&device, 4); + fill(&width, |bytes| { + bytes.copy_from_slice(&luma.width.to_le_bytes()) + }); + let first = IndexClass::Texture.range().start; + let _grant = (device.grant_indices(IndexClass::Texture, first, 1)).expect("a grant"); + for row in &ROWS { + let handle = row.handle; + let foreign = device + .extensions() + .iter() + .any(|n| n.as_c_str() == ash::ext::queue_family_foreign::NAME); + let label = format!("{name}: {} {handle:?} {picture}", row.name); + if !device.core().exports.offers(handle) || (handle.foreign() && !foreign) { + println!("surface-storage: {label}: not exported"); + continue; + } + let specs = [0, 1].map(|i| { + let p = planes[i]; + let mut descriptor = + TextureDescriptor::new(TextureType::Type2D, p.format, p.width, p.height); + descriptor.usage = match i { + 0 => { + TextureUsage::SHADER_READ + | TextureUsage::SHADER_WRITE + | TextureUsage::RENDER_TARGET + | TextureUsage::PIXEL_FORMAT_VIEW + } + _ => TextureUsage::RENDER_TARGET | TextureUsage::SHADER_READ, + }; + PlaneSpec { + descriptor, + index: None, + rows_below: row.rows_below(planes)[i], + } + }); + let spec = SurfaceSpec { + handle, + pitch_multiple: row.pitch_multiple, + start_multiple: row.start_multiple, + planes: &specs, + }; + let (surface, export) = match device.new_exported_surface(&spec) { + Ok(made) => made, + Err(Error::Unsupported(Gap::SurfaceUsage { + pixel_format, + missing, + })) => { + println!( + "surface-storage: {label}: the surface refused: SurfaceUsage of \ + {pixel_format:?}, missing {missing:#x}" + ); + continue; + } + Err(error) => panic!("{label}: {error}"), + }; + let form = export.form; + let view = surface.planes()[0].new_texture_view( + PixelFormat::R8Uint, + TextureType::Type2D, + 0..1, + 0..1, + Some(first), + ); + let view = match view { + Ok(view) => view, + Err(Error::Unsupported(Gap::TextureView { + kind: TextureViewKind::SurfacePixelFormat, + })) if matches!(form, SurfaceForm::InPlace) => { + println!( + "surface-storage: {label}: form {form:?}: the R8Uint view of the \ + R8Unorm plane refused: SurfacePixelFormat" + ); + continue; + } + Err(error) => panic!("{label}: form {form:?}: the R8Uint view: {error}"), + }; + let command_buffer = queue.command_buffer().expect("a command buffer"); + let mut encoder = (command_buffer.compute_command_encoder(DispatchType::Serial)) + .expect("an encoder"); + let done = |result: Result<(), crate::Error>| { + result.unwrap_or_else(|error| panic!("{label}: {error}")) + }; + done(encoder.set_compute_pipeline_state(&write)); + done(encoder.set_texture(Some(&view), 0)); + done(encoder.set_buffer(Some(&values), 0, 0)); + done(encoder.set_buffer(Some(&width), 0, 1)); + done( + encoder + .dispatch_threadgroups([luma.width / 16, luma.height / 8, 1], [16, 8, 1]), + ); + encoder.end_encoding(); + command_buffer.commit().expect("a commit"); + finished(&label, &command_buffer); + assert_eq!(command_buffer.error(), None, "{label}"); + let buffer = (surface.buffer()) + .unwrap_or_else(|error| panic!("{label}: the buffer: {error}")); + let total = buffer.length(); + let (_whole_block, whole) = shared(&device, total); + let command_buffer = queue.command_buffer().expect("a command buffer"); + let mut encoder = command_buffer.blit_command_encoder().expect("an encoder"); + (encoder.copy_from_buffer_to_buffer(&buffer, 0, &whole, 0, total)) + .unwrap_or_else(|error| panic!("{label}: the buffer's read: {error}")); + encoder.end_encoding(); + command_buffer.commit().expect("a commit"); + finished(&label, &command_buffer); + assert_eq!(command_buffer.error(), None, "{label}"); + let laid = export.planes[0]; + let bytes = bytes_of(&whole); + let wrong = (0..luma.height).find_map(|y| { + let at = (laid.offset + u64::from(y) * laid.row_pitch) as usize; + (0..luma.width).find_map(|x| { + let i = u64::from(y) * u64::from(luma.width) + u64::from(x); + let read = bytes[at + x as usize]; + (read != value(i)).then_some((x, y, read, value(i))) + }) + }); + assert_eq!( + wrong, None, + "{label}: form {form:?}: the first texel the kernel wrote through the view \ + that the surface's buffer does not hold (x, y, read, written)" + ); + println!( + "surface-storage: {label}: form {form:?}: a kernel wrote the R8Unorm luma's \ + {texels} texels through its ShaderWrite R8Uint view; the surface's buffer \ + holds each exactly at offset {} pitch {}", + laid.offset, laid.row_pitch + ); + drop((view, buffer, surface)); + } + }); + } + /// The handles and multiples the busy-queue timing makes a surface of, where the device /// exports the handle: the product's VA-API row and its NVIDIA row. #[cfg(unix)]