|
| 1 | +//! 显式硬件验收:普通 CI 只编译,实际运行需 Vulkan GPU 和产品原始 ABI 文件。 |
| 2 | +use super::{NativePostprocess, PostprocessDevice}; |
| 3 | +use gpui::{ |
| 4 | + BackgroundShaderCancellation, Bounds, DevicePixels, ScaledPixels, StreamImageBudget, |
| 5 | + StreamImageBudgets, WgslPostprocessDescriptor, WgslPostprocessPass, point, size, |
| 6 | +}; |
| 7 | +use std::sync::{Arc, atomic::AtomicBool}; |
| 8 | + |
| 9 | +const SOURCE: &str = r#" |
| 10 | +struct Params { factor: vec4<f32> } |
| 11 | +@group(0) @binding(0) var<uniform> frame: Params; |
| 12 | +@group(0) @binding(1) var surface: texture_2d<f32>; |
| 13 | +@fragment fn invert(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { |
| 14 | + let c = textureLoad(surface, vec2<i32>(p.xy), 0); |
| 15 | + return vec4<f32>(vec3<f32>(c.a) - c.rgb, c.a); |
| 16 | +} |
| 17 | +@fragment fn scale(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { |
| 18 | + let c = textureLoad(surface, vec2<i32>(p.xy), 0); |
| 19 | + return vec4<f32>(c.r * frame.factor.x, c.g, c.b, c.a); |
| 20 | +} |
| 21 | +@fragment fn excluded() -> @location(0) vec4<f32> { |
| 22 | + return textureLoad(surface, vec2<i32>(0, 0), 0); |
| 23 | +} |
| 24 | +"#; |
| 25 | +fn descriptor(entries: &[&str]) -> WgslPostprocessDescriptor { |
| 26 | + WgslPostprocessDescriptor { |
| 27 | + size: size(DevicePixels(4), DevicePixels(2)), |
| 28 | + uniform_size: 16, |
| 29 | + passes: entries |
| 30 | + .iter() |
| 31 | + .map(|entry| WgslPostprocessPass { source: SOURCE.into(), entry: (*entry).into() }) |
| 32 | + .collect::<Vec<_>>() |
| 33 | + .into(), |
| 34 | + } |
| 35 | +} |
| 36 | +fn rectangle(x: f32, y: f32, w: f32, h: f32) -> Bounds<ScaledPixels> { |
| 37 | + Bounds::new(point(ScaledPixels(x), ScaledPixels(y)), size(ScaledPixels(w), ScaledPixels(h))) |
| 38 | +} |
| 39 | +fn uniform(factor: f32) -> [u8; 16] { |
| 40 | + let mut bytes = [0; 16]; |
| 41 | + bytes[..4].copy_from_slice(&factor.to_le_bytes()); |
| 42 | + bytes |
| 43 | +} |
| 44 | +fn screen( |
| 45 | + device: &wgpu::Device, |
| 46 | + queue: &wgpu::Queue, |
| 47 | + format: wgpu::TextureFormat, |
| 48 | +) -> wgpu::Texture { |
| 49 | + let texture = device.create_texture(&wgpu::TextureDescriptor { |
| 50 | + label: Some("postprocess oracle screen"), |
| 51 | + size: wgpu::Extent3d { width: 12, height: 4, depth_or_array_layers: 1 }, |
| 52 | + mip_level_count: 1, |
| 53 | + sample_count: 1, |
| 54 | + dimension: wgpu::TextureDimension::D2, |
| 55 | + format, |
| 56 | + usage: wgpu::TextureUsages::RENDER_ATTACHMENT |
| 57 | + | wgpu::TextureUsages::COPY_SRC |
| 58 | + | wgpu::TextureUsages::COPY_DST, |
| 59 | + view_formats: &[], |
| 60 | + }); |
| 61 | + // 两种纹理格式写入相同的逻辑颜色,读回时再统一为 RGBA 进行像素比较。 |
| 62 | + let pixel = if format == wgpu::TextureFormat::Bgra8Unorm { |
| 63 | + [100, 60, 40, 255] |
| 64 | + } else { |
| 65 | + [40, 60, 100, 255] |
| 66 | + }; |
| 67 | + queue.write_texture( |
| 68 | + wgpu::TexelCopyTextureInfo { |
| 69 | + texture: &texture, |
| 70 | + mip_level: 0, |
| 71 | + origin: wgpu::Origin3d::ZERO, |
| 72 | + aspect: wgpu::TextureAspect::All, |
| 73 | + }, |
| 74 | + &pixel.repeat(48), |
| 75 | + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(48), rows_per_image: None }, |
| 76 | + texture.size(), |
| 77 | + ); |
| 78 | + texture |
| 79 | +} |
| 80 | +fn read(device: &wgpu::Device, queue: &wgpu::Queue, texture: &wgpu::Texture) -> Vec<[u8; 4]> { |
| 81 | + let buffer = device.create_buffer(&wgpu::BufferDescriptor { |
| 82 | + label: Some("oracle readback only"), |
| 83 | + size: 256 * 4, |
| 84 | + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, |
| 85 | + mapped_at_creation: false, |
| 86 | + }); |
| 87 | + let mut encoder = device.create_command_encoder(&Default::default()); |
| 88 | + encoder.copy_texture_to_buffer( |
| 89 | + wgpu::TexelCopyTextureInfo { |
| 90 | + texture, |
| 91 | + mip_level: 0, |
| 92 | + origin: wgpu::Origin3d::ZERO, |
| 93 | + aspect: wgpu::TextureAspect::All, |
| 94 | + }, |
| 95 | + wgpu::TexelCopyBufferInfo { |
| 96 | + buffer: &buffer, |
| 97 | + layout: wgpu::TexelCopyBufferLayout { |
| 98 | + offset: 0, |
| 99 | + bytes_per_row: Some(256), |
| 100 | + rows_per_image: None, |
| 101 | + }, |
| 102 | + }, |
| 103 | + texture.size(), |
| 104 | + ); |
| 105 | + queue.submit([encoder.finish()]); |
| 106 | + let (tx, rx) = std::sync::mpsc::channel(); |
| 107 | + buffer.slice(..).map_async(wgpu::MapMode::Read, move |result| tx.send(result).unwrap()); |
| 108 | + device |
| 109 | + .poll(wgpu::PollType::Wait { |
| 110 | + submission_index: None, |
| 111 | + timeout: Some(std::time::Duration::from_secs(5)), |
| 112 | + }) |
| 113 | + .unwrap(); |
| 114 | + rx.recv().unwrap().unwrap(); |
| 115 | + let data = buffer.slice(..).get_mapped_range(); |
| 116 | + let mut result = Vec::new(); |
| 117 | + for y in 0..4 { |
| 118 | + for x in 0..12 { |
| 119 | + let i = y * 256 + x * 4; |
| 120 | + let mut pixel: [u8; 4] = data[i..i + 4].try_into().unwrap(); |
| 121 | + if texture.format() == wgpu::TextureFormat::Bgra8Unorm { |
| 122 | + pixel.swap(0, 2); |
| 123 | + } |
| 124 | + result.push(pixel); |
| 125 | + } |
| 126 | + } |
| 127 | + drop(data); |
| 128 | + buffer.unmap(); |
| 129 | + result |
| 130 | +} |
| 131 | +fn prepare( |
| 132 | + device: &PostprocessDevice, |
| 133 | + owner: u64, |
| 134 | + desc: WgslPostprocessDescriptor, |
| 135 | + budgets: StreamImageBudgets, |
| 136 | + format: wgpu::TextureFormat, |
| 137 | +) -> NativePostprocess { |
| 138 | + let factory = device.factory(owner, desc, format, budgets, Default::default()).unwrap(); |
| 139 | + let prepared = std::thread::spawn(move || factory.run()).join().unwrap().unwrap().unwrap(); |
| 140 | + device.adopt(owner, prepared).unwrap() |
| 141 | +} |
| 142 | +fn retire(image: NativePostprocess) { |
| 143 | + if let Some(completion) = image.retire() { |
| 144 | + std::thread::spawn(move || completion.wait()).join().unwrap().unwrap(); |
| 145 | + } |
| 146 | +} |
| 147 | +#[test] |
| 148 | +#[ignore = "requires a hardware Vulkan adapter and PEBREL_EFFECT_ABI pointing to the product ABI"] |
| 149 | +fn native_vulkan_postprocess_contract() { |
| 150 | + let abi_path = std::env::var_os("PEBREL_EFFECT_ABI").expect("provide the product ABI file"); |
| 151 | + let application_abi = std::fs::read_to_string(&abi_path).expect("read UTF-8 product ABI"); |
| 152 | + println!("product ABI file: {}", std::path::Path::new(&abi_path).display()); |
| 153 | + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { |
| 154 | + backends: wgpu::Backends::VULKAN, |
| 155 | + flags: wgpu::InstanceFlags::default(), |
| 156 | + backend_options: wgpu::BackendOptions::default(), |
| 157 | + memory_budget_thresholds: wgpu::MemoryBudgetThresholds::default(), |
| 158 | + display: None, |
| 159 | + }); |
| 160 | + let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions { |
| 161 | + power_preference: wgpu::PowerPreference::HighPerformance, |
| 162 | + ..Default::default() |
| 163 | + })) |
| 164 | + .unwrap(); |
| 165 | + println!("hardware adapter: {:?}", adapter.get_info()); |
| 166 | + assert_ne!(adapter.get_info().device_type, wgpu::DeviceType::Cpu); |
| 167 | + let (device, queue) = pollster::block_on(adapter.request_device(&Default::default())).unwrap(); |
| 168 | + let (device, queue) = (Arc::new(device), Arc::new(queue)); |
| 169 | + let native = |
| 170 | + PostprocessDevice::new(device.clone(), queue.clone(), Arc::new(AtomicBool::new(false))) |
| 171 | + .unwrap(); |
| 172 | + for format in [wgpu::TextureFormat::Rgba8Unorm, wgpu::TextureFormat::Bgra8Unorm] { |
| 173 | + println!("validating texture format: {format:?}"); |
| 174 | + let bytes = 4 * 2 * 8 + 16; |
| 175 | + let budget = StreamImageBudget::new(bytes); |
| 176 | + let budgets = StreamImageBudgets::new(StreamImageBudget::new(bytes), budget.clone()); |
| 177 | + let mut image = |
| 178 | + prepare(&native, 1, descriptor(&["invert", "scale"]), budgets.clone(), format); |
| 179 | + assert_eq!(budget.used(), bytes); |
| 180 | + let texture = screen(&device, &queue, format); |
| 181 | + assert!( |
| 182 | + image |
| 183 | + .render(&texture, rectangle(1., 1., 4., 2.), rectangle(2., 1., 2., 2.), &[0; 32]) |
| 184 | + .is_err() |
| 185 | + ); |
| 186 | + image |
| 187 | + .render(&texture, rectangle(1., 1., 4., 2.), rectangle(2., 1., 2., 2.), &uniform(0.5)) |
| 188 | + .unwrap(); |
| 189 | + let pixels = read(&device, &queue, &texture); |
| 190 | + for y in 0..4 { |
| 191 | + for x in 0..12 { |
| 192 | + let pixel = pixels[y * 12 + x]; |
| 193 | + if (2..4).contains(&x) && (1..3).contains(&y) { |
| 194 | + assert!((107..=108).contains(&pixel[0])); |
| 195 | + assert_eq!(&pixel[1..], &[195, 155, 255]); |
| 196 | + } else { |
| 197 | + assert_eq!(pixel, [40, 60, 100, 255]); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + retire(image); |
| 202 | + assert_eq!(budget.used(), 0); |
| 203 | + let texture = screen(&device, &queue, format); |
| 204 | + let mut image = prepare(&native, 2, descriptor(&["excluded"]), budgets.clone(), format); |
| 205 | + image |
| 206 | + .render(&texture, rectangle(1., 1., 4., 2.), rectangle(2., 1., 2., 2.), &uniform(1.)) |
| 207 | + .unwrap(); |
| 208 | + assert_eq!(read(&device, &queue, &texture)[14], [0, 0, 0, 0]); |
| 209 | + retire(image); |
| 210 | + assert_eq!(budget.used(), 0); |
| 211 | + let texture = screen(&device, &queue, format); |
| 212 | + let mut image = prepare(&native, 3, descriptor(&["scale"]), budgets.clone(), format); |
| 213 | + image |
| 214 | + .render(&texture, rectangle(0., 0., 4., 2.), rectangle(0., 0., 12., 4.), &uniform(0.5)) |
| 215 | + .unwrap(); |
| 216 | + image |
| 217 | + .render(&texture, rectangle(6., 0., 4., 2.), rectangle(0., 0., 12., 4.), &uniform(0.25)) |
| 218 | + .unwrap(); |
| 219 | + let pixels = read(&device, &queue, &texture); |
| 220 | + assert_eq!(pixels[0], [20, 60, 100, 255]); |
| 221 | + assert_eq!(pixels[6], [10, 60, 100, 255]); |
| 222 | + retire(image); |
| 223 | + assert_eq!(budget.used(), 0); |
| 224 | + let mut image = prepare(&native, 4, descriptor(&["invert"; 8]), budgets.clone(), format); |
| 225 | + assert_eq!(budget.used(), bytes); |
| 226 | + let texture = screen(&device, &queue, format); |
| 227 | + image |
| 228 | + .render(&texture, rectangle(0., 0., 4., 2.), rectangle(0., 0., 12., 4.), &uniform(1.)) |
| 229 | + .unwrap(); |
| 230 | + assert!(read(&device, &queue, &texture).iter().all(|pixel| *pixel == [40, 60, 100, 255])); |
| 231 | + retire(image); |
| 232 | + assert_eq!(budget.used(), 0); |
| 233 | + let low = StreamImageBudgets::new( |
| 234 | + StreamImageBudget::new(bytes - 1), |
| 235 | + StreamImageBudget::new(bytes - 1), |
| 236 | + ); |
| 237 | + let factory = |
| 238 | + native.factory(5, descriptor(&["invert"]), format, low, Default::default()).unwrap(); |
| 239 | + assert!(std::thread::spawn(move || factory.run()).join().unwrap().is_err()); |
| 240 | + let cancelled = BackgroundShaderCancellation::default(); |
| 241 | + let factory = native |
| 242 | + .factory(6, descriptor(&["invert"]), format, budgets.clone(), cancelled.clone()) |
| 243 | + .unwrap(); |
| 244 | + cancelled.cancel(); |
| 245 | + assert!(std::thread::spawn(move || factory.run()).join().unwrap().unwrap().is_none()); |
| 246 | + assert_eq!(budget.used(), 0); |
| 247 | + let factory = native |
| 248 | + .factory(7, descriptor(&["invert"]), format, budgets.clone(), Default::default()) |
| 249 | + .unwrap(); |
| 250 | + let prepared = std::thread::spawn(move || factory.run()).join().unwrap().unwrap().unwrap(); |
| 251 | + native.invalidate().unwrap(); |
| 252 | + assert!(native.adopt(7, prepared).is_err()); |
| 253 | + assert_eq!(budget.used(), 0); |
| 254 | + let cancellation = BackgroundShaderCancellation::default(); |
| 255 | + let factory = native |
| 256 | + .factory(8, descriptor(&["invert"]), format, budgets.clone(), cancellation.clone()) |
| 257 | + .unwrap(); |
| 258 | + let other = cancellation.clone(); |
| 259 | + assert!( |
| 260 | + std::thread::spawn(move || factory.run_with_allocation_gate(move || other.cancel())) |
| 261 | + .join() |
| 262 | + .unwrap() |
| 263 | + .unwrap() |
| 264 | + .is_none() |
| 265 | + ); |
| 266 | + assert_eq!(budget.used(), 0); |
| 267 | + let factory = native |
| 268 | + .factory(9, descriptor(&["invert"]), format, budgets.clone(), Default::default()) |
| 269 | + .unwrap(); |
| 270 | + let prepared = std::thread::spawn(move || factory.run()).join().unwrap().unwrap().unwrap(); |
| 271 | + assert!(native.adopt(10, prepared).is_err()); |
| 272 | + assert_eq!(budget.used(), 0); |
| 273 | + let mut abi = descriptor(&["app_frame"]); |
| 274 | + abi.uniform_size = (13 + 256) * 16; |
| 275 | + abi.passes = vec![WgslPostprocessPass { |
| 276 | + source: format!( |
| 277 | + "{}\n@fragment fn app_frame()->@location(0) vec4<f32>{{return frame.palette[7u];}}", |
| 278 | + application_abi |
| 279 | + ) |
| 280 | + .into(), |
| 281 | + entry: "app_frame".into(), |
| 282 | + }] |
| 283 | + .into(); |
| 284 | + let large_bytes = abi.texture_bytes().unwrap() + abi.uniform_size as u64; |
| 285 | + let large_budget = StreamImageBudget::new(large_bytes); |
| 286 | + let mut image = prepare( |
| 287 | + &native, |
| 288 | + 11, |
| 289 | + abi, |
| 290 | + StreamImageBudgets::new(StreamImageBudget::new(large_bytes), large_budget.clone()), |
| 291 | + format, |
| 292 | + ); |
| 293 | + let mut payload = vec![0; 4304]; |
| 294 | + for (i, value) in [0.2f32, 0.4, 0.6, 1.0].iter().enumerate() { |
| 295 | + let offset = (13 + 7) * 16 + i * 4; |
| 296 | + payload[offset..offset + 4].copy_from_slice(&value.to_le_bytes()); |
| 297 | + } |
| 298 | + let texture = screen(&device, &queue, format); |
| 299 | + image |
| 300 | + .render(&texture, rectangle(0., 0., 4., 2.), rectangle(0., 0., 12., 4.), &payload) |
| 301 | + .unwrap(); |
| 302 | + assert_eq!(read(&device, &queue, &texture)[0], [51, 102, 153, 255]); |
| 303 | + retire(image); |
| 304 | + assert_eq!(large_budget.used(), 0); |
| 305 | + // 验证窗口左边缘裁剪使用局部坐标,而不是把负原点转换成无符号偏移。 |
| 306 | + let texture = screen(&device, &queue, format); |
| 307 | + let mut image = prepare(&native, 12, descriptor(&["invert"]), budgets.clone(), format); |
| 308 | + image |
| 309 | + .render(&texture, rectangle(-1., 1., 4., 2.), rectangle(0., 0., 12., 4.), &uniform(1.)) |
| 310 | + .unwrap(); |
| 311 | + let pixels = read(&device, &queue, &texture); |
| 312 | + for y in 0..4 { |
| 313 | + for x in 0..12 { |
| 314 | + let expected = if x < 3 && (1..3).contains(&y) { |
| 315 | + [215, 195, 155, 255] |
| 316 | + } else { |
| 317 | + [40, 60, 100, 255] |
| 318 | + }; |
| 319 | + assert_eq!(pixels[y * 12 + x], expected); |
| 320 | + } |
| 321 | + } |
| 322 | + retire(image); |
| 323 | + assert_eq!(budget.used(), 0); |
| 324 | + println!( |
| 325 | + "format {format:?}: native pixels, application ABI, uniform sequencing, masking, budgets, cancellation, epoch and retirement passed" |
| 326 | + ); |
| 327 | + } |
| 328 | +} |
0 commit comments