|
1 |
| -use wgpu_test::{fail, gpu_test, FailureCase, GpuTestConfiguration, TestParameters}; |
| 1 | +use wgpu::util::DeviceExt; |
| 2 | +use wgpu::CommandEncoder; |
| 3 | +use wgpu_test::{ |
| 4 | + fail, gpu_test, FailureCase, GpuTestConfiguration, TestParameters, TestingContext, |
| 5 | +}; |
2 | 6 |
|
3 | 7 | #[gpu_test]
|
4 | 8 | static DROP_ENCODER: GpuTestConfiguration = GpuTestConfiguration::new().run_sync(|ctx| {
|
@@ -72,3 +76,227 @@ static DROP_ENCODER_AFTER_ERROR: GpuTestConfiguration = GpuTestConfiguration::ne
|
72 | 76 | // The encoder is still open!
|
73 | 77 | drop(encoder);
|
74 | 78 | });
|
| 79 | + |
| 80 | +// TODO: This should also apply to render passes once the lifetime bound is lifted. |
| 81 | +#[gpu_test] |
| 82 | +static ENCODER_OPERATIONS_FAIL_WHILE_COMPUTE_PASS_ALIVE: GpuTestConfiguration = |
| 83 | + GpuTestConfiguration::new() |
| 84 | + .parameters(TestParameters::default().features( |
| 85 | + wgpu::Features::CLEAR_TEXTURE |
| 86 | + | wgpu::Features::TIMESTAMP_QUERY |
| 87 | + | wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS, |
| 88 | + )) |
| 89 | + .run_sync(encoder_operations_fail_while_compute_pass_alive); |
| 90 | + |
| 91 | +fn encoder_operations_fail_while_compute_pass_alive(ctx: TestingContext) { |
| 92 | + let buffer_source = ctx |
| 93 | + .device |
| 94 | + .create_buffer_init(&wgpu::util::BufferInitDescriptor { |
| 95 | + label: None, |
| 96 | + contents: &[0u8; 4], |
| 97 | + usage: wgpu::BufferUsages::COPY_SRC, |
| 98 | + }); |
| 99 | + let buffer_dest = ctx |
| 100 | + .device |
| 101 | + .create_buffer_init(&wgpu::util::BufferInitDescriptor { |
| 102 | + label: None, |
| 103 | + contents: &[0u8; 4], |
| 104 | + usage: wgpu::BufferUsages::COPY_DST, |
| 105 | + }); |
| 106 | + |
| 107 | + let texture_desc = wgpu::TextureDescriptor { |
| 108 | + label: None, |
| 109 | + size: wgpu::Extent3d { |
| 110 | + width: 1, |
| 111 | + height: 1, |
| 112 | + depth_or_array_layers: 1, |
| 113 | + }, |
| 114 | + mip_level_count: 1, |
| 115 | + sample_count: 1, |
| 116 | + dimension: wgpu::TextureDimension::D2, |
| 117 | + format: wgpu::TextureFormat::Rgba8Unorm, |
| 118 | + usage: wgpu::TextureUsages::COPY_DST, |
| 119 | + view_formats: &[], |
| 120 | + }; |
| 121 | + let texture_dst = ctx.device.create_texture(&texture_desc); |
| 122 | + let texture_src = ctx.device.create_texture(&wgpu::TextureDescriptor { |
| 123 | + usage: wgpu::TextureUsages::COPY_SRC, |
| 124 | + ..texture_desc |
| 125 | + }); |
| 126 | + let query_set = ctx.device.create_query_set(&wgpu::QuerySetDescriptor { |
| 127 | + count: 1, |
| 128 | + ty: wgpu::QueryType::Timestamp, |
| 129 | + label: None, |
| 130 | + }); |
| 131 | + |
| 132 | + #[allow(clippy::type_complexity)] |
| 133 | + let recording_ops: Vec<(_, Box<dyn Fn(&mut CommandEncoder)>)> = vec![ |
| 134 | + ( |
| 135 | + "begin_compute_pass", |
| 136 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 137 | + encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default()); |
| 138 | + }), |
| 139 | + ), |
| 140 | + ( |
| 141 | + "begin_render_pass", |
| 142 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 143 | + encoder.begin_render_pass(&wgpu::RenderPassDescriptor::default()); |
| 144 | + }), |
| 145 | + ), |
| 146 | + ( |
| 147 | + "copy_buffer_to_buffer", |
| 148 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 149 | + encoder.copy_buffer_to_buffer(&buffer_source, 0, &buffer_dest, 0, 4); |
| 150 | + }), |
| 151 | + ), |
| 152 | + ( |
| 153 | + "copy_buffer_to_texture", |
| 154 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 155 | + encoder.copy_buffer_to_texture( |
| 156 | + wgpu::ImageCopyBuffer { |
| 157 | + buffer: &buffer_source, |
| 158 | + layout: wgpu::ImageDataLayout { |
| 159 | + offset: 0, |
| 160 | + bytes_per_row: Some(4), |
| 161 | + rows_per_image: None, |
| 162 | + }, |
| 163 | + }, |
| 164 | + texture_dst.as_image_copy(), |
| 165 | + texture_dst.size(), |
| 166 | + ); |
| 167 | + }), |
| 168 | + ), |
| 169 | + ( |
| 170 | + "copy_texture_to_buffer", |
| 171 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 172 | + encoder.copy_texture_to_buffer( |
| 173 | + wgpu::ImageCopyTexture { |
| 174 | + texture: &texture_src, |
| 175 | + mip_level: 0, |
| 176 | + origin: wgpu::Origin3d::ZERO, |
| 177 | + aspect: wgpu::TextureAspect::All, |
| 178 | + }, |
| 179 | + wgpu::ImageCopyBuffer { |
| 180 | + buffer: &buffer_dest, |
| 181 | + layout: wgpu::ImageDataLayout { |
| 182 | + offset: 0, |
| 183 | + bytes_per_row: Some(4), |
| 184 | + rows_per_image: None, |
| 185 | + }, |
| 186 | + }, |
| 187 | + texture_dst.size(), |
| 188 | + ); |
| 189 | + }), |
| 190 | + ), |
| 191 | + ( |
| 192 | + "copy_texture_to_texture", |
| 193 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 194 | + encoder.copy_texture_to_texture( |
| 195 | + wgpu::ImageCopyTexture { |
| 196 | + texture: &texture_src, |
| 197 | + mip_level: 0, |
| 198 | + origin: wgpu::Origin3d::ZERO, |
| 199 | + aspect: wgpu::TextureAspect::All, |
| 200 | + }, |
| 201 | + wgpu::ImageCopyTexture { |
| 202 | + texture: &texture_dst, |
| 203 | + mip_level: 0, |
| 204 | + origin: wgpu::Origin3d::ZERO, |
| 205 | + aspect: wgpu::TextureAspect::All, |
| 206 | + }, |
| 207 | + texture_dst.size(), |
| 208 | + ); |
| 209 | + }), |
| 210 | + ), |
| 211 | + ( |
| 212 | + "clear_texture", |
| 213 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 214 | + encoder.clear_texture(&texture_dst, &wgpu::ImageSubresourceRange::default()); |
| 215 | + }), |
| 216 | + ), |
| 217 | + ( |
| 218 | + "clear_buffer", |
| 219 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 220 | + encoder.clear_buffer(&buffer_dest, 0, None); |
| 221 | + }), |
| 222 | + ), |
| 223 | + ( |
| 224 | + "insert_debug_marker", |
| 225 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 226 | + encoder.insert_debug_marker("marker"); |
| 227 | + }), |
| 228 | + ), |
| 229 | + ( |
| 230 | + "push_debug_group", |
| 231 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 232 | + encoder.push_debug_group("marker"); |
| 233 | + }), |
| 234 | + ), |
| 235 | + ( |
| 236 | + "pop_debug_group", |
| 237 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 238 | + encoder.pop_debug_group(); |
| 239 | + }), |
| 240 | + ), |
| 241 | + ( |
| 242 | + "resolve_query_set", |
| 243 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 244 | + encoder.resolve_query_set(&query_set, 0..1, &buffer_dest, 0); |
| 245 | + }), |
| 246 | + ), |
| 247 | + ( |
| 248 | + "write_timestamp", |
| 249 | + Box::new(|encoder: &mut wgpu::CommandEncoder| { |
| 250 | + encoder.write_timestamp(&query_set, 0); |
| 251 | + }), |
| 252 | + ), |
| 253 | + ]; |
| 254 | + |
| 255 | + for (op_name, op) in recording_ops.iter() { |
| 256 | + let mut encoder = ctx |
| 257 | + .device |
| 258 | + .create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); |
| 259 | + |
| 260 | + let pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default()); |
| 261 | + |
| 262 | + ctx.device.push_error_scope(wgpu::ErrorFilter::Validation); |
| 263 | + |
| 264 | + log::info!("Testing operation {} on a locked command encoder", op_name); |
| 265 | + fail( |
| 266 | + &ctx.device, |
| 267 | + || op(&mut encoder), |
| 268 | + Some("Command encoder is locked"), |
| 269 | + ); |
| 270 | + |
| 271 | + // Drop the pass - this also fails now since the encoder is invalid: |
| 272 | + fail( |
| 273 | + &ctx.device, |
| 274 | + || drop(pass), |
| 275 | + Some("Command encoder is invalid"), |
| 276 | + ); |
| 277 | + // Also, it's not possible to create a new pass on the encoder: |
| 278 | + fail( |
| 279 | + &ctx.device, |
| 280 | + || encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default()), |
| 281 | + Some("Command encoder is invalid"), |
| 282 | + ); |
| 283 | + } |
| 284 | + |
| 285 | + // Test encoder finishing separately since it consumes the encoder and doesn't fit above pattern. |
| 286 | + { |
| 287 | + let mut encoder = ctx |
| 288 | + .device |
| 289 | + .create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); |
| 290 | + let pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default()); |
| 291 | + fail( |
| 292 | + &ctx.device, |
| 293 | + || encoder.finish(), |
| 294 | + Some("Command encoder is locked"), |
| 295 | + ); |
| 296 | + fail( |
| 297 | + &ctx.device, |
| 298 | + || drop(pass), |
| 299 | + Some("Command encoder is invalid"), |
| 300 | + ); |
| 301 | + } |
| 302 | +} |
0 commit comments