|
| 1 | +use objc2::{rc::Retained, runtime::ProtocolObject}; |
| 2 | +use objc2_foundation::NSRange; |
| 3 | +use objc2_metal::{ |
| 4 | + MTLBlitCommandEncoder, MTLBuffer, MTLCommandBuffer, MTLCommandEncoder, MTLCommandQueue, |
| 5 | + MTLDevice, |
| 6 | +}; |
| 7 | +use wgpu_test::{gpu_test, FailureCase, GpuTestConfiguration, GpuTestInitializer, TestParameters}; |
| 8 | + |
| 9 | +pub fn all_tests(tests: &mut Vec<GpuTestInitializer>) { |
| 10 | + tests.push(METAL_EXTERNAL_BUFFER_WRITE); |
| 11 | +} |
| 12 | + |
| 13 | +#[gpu_test] |
| 14 | +static METAL_EXTERNAL_BUFFER_WRITE: GpuTestConfiguration = GpuTestConfiguration::new() |
| 15 | + .parameters(TestParameters::default().skip(FailureCase::backend(!wgpu::Backends::METAL))) |
| 16 | + .run_sync(|ctx| { |
| 17 | + const SIZE: u64 = 64; |
| 18 | + const WRITTEN: core::ops::Range<usize> = 16..48; |
| 19 | + |
| 20 | + let buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { |
| 21 | + label: Some("external Metal destination"), |
| 22 | + size: SIZE, |
| 23 | + usage: wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST, |
| 24 | + mapped_at_creation: false, |
| 25 | + }); |
| 26 | + let readback = ctx.device.create_buffer(&wgpu::BufferDescriptor { |
| 27 | + label: Some("external Metal readback"), |
| 28 | + size: SIZE, |
| 29 | + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, |
| 30 | + mapped_at_creation: false, |
| 31 | + }); |
| 32 | + |
| 33 | + let device_ptr = unsafe { |
| 34 | + ctx.device |
| 35 | + .as_hal::<wgpu_hal::api::Metal>() |
| 36 | + .expect("Metal device") |
| 37 | + .retained_raw_handle() |
| 38 | + }; |
| 39 | + let queue_ptr = unsafe { |
| 40 | + ctx.queue |
| 41 | + .as_hal::<wgpu_hal::api::Metal>() |
| 42 | + .expect("Metal queue") |
| 43 | + .retained_raw_handle() |
| 44 | + }; |
| 45 | + let buffer_ptr = unsafe { |
| 46 | + buffer |
| 47 | + .as_hal::<wgpu_hal::api::Metal>() |
| 48 | + .expect("Metal buffer") |
| 49 | + .retained_raw_handle() |
| 50 | + }; |
| 51 | + |
| 52 | + let device = unsafe { |
| 53 | + Retained::<ProtocolObject<dyn MTLDevice>>::from_raw(device_ptr.cast().as_ptr()) |
| 54 | + .expect("retained Metal device") |
| 55 | + }; |
| 56 | + let queue = unsafe { |
| 57 | + Retained::<ProtocolObject<dyn MTLCommandQueue>>::from_raw(queue_ptr.cast().as_ptr()) |
| 58 | + .expect("retained Metal queue") |
| 59 | + }; |
| 60 | + let raw_buffer = unsafe { |
| 61 | + Retained::<ProtocolObject<dyn MTLBuffer>>::from_raw(buffer_ptr.cast().as_ptr()) |
| 62 | + .expect("retained Metal buffer") |
| 63 | + }; |
| 64 | + assert_eq!(queue.device(), device); |
| 65 | + |
| 66 | + let command_buffer = queue.commandBuffer().expect("external command buffer"); |
| 67 | + let encoder = command_buffer.blitCommandEncoder().expect("blit encoder"); |
| 68 | + encoder.fillBuffer_range_value( |
| 69 | + &raw_buffer, |
| 70 | + NSRange::new(WRITTEN.start, WRITTEN.len()), |
| 71 | + 0xA5, |
| 72 | + ); |
| 73 | + encoder.endEncoding(); |
| 74 | + command_buffer.commit(); |
| 75 | + command_buffer.waitUntilCompleted(); |
| 76 | + |
| 77 | + unsafe { |
| 78 | + buffer |
| 79 | + .mark_external_write_initialized(WRITTEN.start as u64..WRITTEN.end as u64) |
| 80 | + .expect("register completed external write"); |
| 81 | + } |
| 82 | + |
| 83 | + let mut command_encoder = ctx |
| 84 | + .device |
| 85 | + .create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); |
| 86 | + command_encoder.copy_buffer_to_buffer(&buffer, 0, &readback, 0, SIZE); |
| 87 | + ctx.queue.submit([command_encoder.finish()]); |
| 88 | + readback |
| 89 | + .slice(..) |
| 90 | + .map_async(wgpu::MapMode::Read, Result::unwrap); |
| 91 | + ctx.device |
| 92 | + .poll(wgpu::PollType::wait_indefinitely()) |
| 93 | + .unwrap(); |
| 94 | + |
| 95 | + let mapped = readback.slice(..).get_mapped_range(); |
| 96 | + assert!(mapped[..WRITTEN.start].iter().all(|byte| *byte == 0)); |
| 97 | + assert!(mapped[WRITTEN.clone()].iter().all(|byte| *byte == 0xA5)); |
| 98 | + assert!(mapped[WRITTEN.end..].iter().all(|byte| *byte == 0)); |
| 99 | + drop(mapped); |
| 100 | + readback.unmap(); |
| 101 | + }); |
0 commit comments