|
| 1 | +use wgpu_test::{gpu_test, GpuTestConfiguration, GpuTestInitializer, TestParameters}; |
| 2 | + |
| 3 | +pub fn all_tests(vec: &mut Vec<GpuTestInitializer>) { |
| 4 | + vec.push(RESOLVE_WITH_TRANSIENT); |
| 5 | +} |
| 6 | + |
| 7 | +#[gpu_test] |
| 8 | +static RESOLVE_WITH_TRANSIENT: GpuTestConfiguration = GpuTestConfiguration::new() |
| 9 | + .parameters(TestParameters::default()) |
| 10 | + .run_async(|ctx| async move { |
| 11 | + const SIZE: wgpu::Extent3d = wgpu::Extent3d { |
| 12 | + width: 256, |
| 13 | + height: 256, |
| 14 | + depth_or_array_layers: 1, |
| 15 | + }; |
| 16 | + |
| 17 | + let shader_src = " |
| 18 | + @vertex |
| 19 | + fn vs_main(@builtin(vertex_index) index: u32) -> @builtin(position) vec4f { |
| 20 | + let positions: array<vec2f, 3> = array<vec2f, 3>( |
| 21 | + vec2f(-1.0, -1.0), |
| 22 | + vec2f(-1.0, 3.0), |
| 23 | + vec2f(3.0, -1.0) |
| 24 | + ); |
| 25 | + return vec4f(positions[index], 0.0, 1.0); |
| 26 | + } |
| 27 | +
|
| 28 | + @fragment |
| 29 | + fn fs_main() -> @location(0) vec4f { |
| 30 | + return vec4f(1.0); |
| 31 | + } |
| 32 | + "; |
| 33 | + |
| 34 | + let shader = ctx |
| 35 | + .device |
| 36 | + .create_shader_module(wgpu::ShaderModuleDescriptor { |
| 37 | + label: None, |
| 38 | + source: wgpu::ShaderSource::Wgsl(shader_src.into()), |
| 39 | + }); |
| 40 | + |
| 41 | + let pipeline_desc = wgpu::RenderPipelineDescriptor { |
| 42 | + label: None, |
| 43 | + layout: None, |
| 44 | + vertex: wgpu::VertexState { |
| 45 | + buffers: &[], |
| 46 | + module: &shader, |
| 47 | + entry_point: Some("vs_main"), |
| 48 | + compilation_options: Default::default(), |
| 49 | + }, |
| 50 | + primitive: wgpu::PrimitiveState::default(), |
| 51 | + depth_stencil: None, |
| 52 | + multisample: wgpu::MultisampleState { |
| 53 | + count: 4, |
| 54 | + mask: !0, |
| 55 | + alpha_to_coverage_enabled: false, |
| 56 | + }, |
| 57 | + fragment: Some(wgpu::FragmentState { |
| 58 | + module: &shader, |
| 59 | + entry_point: Some("fs_main"), |
| 60 | + compilation_options: Default::default(), |
| 61 | + targets: &[Some(wgpu::ColorTargetState { |
| 62 | + format: wgpu::TextureFormat::Rgba8Unorm, |
| 63 | + blend: None, |
| 64 | + write_mask: wgpu::ColorWrites::ALL, |
| 65 | + })], |
| 66 | + }), |
| 67 | + multiview: None, |
| 68 | + cache: None, |
| 69 | + }; |
| 70 | + let pipeline = ctx.device.create_render_pipeline(&pipeline_desc); |
| 71 | + |
| 72 | + let transient_texture = ctx.device.create_texture(&wgpu::TextureDescriptor { |
| 73 | + label: None, |
| 74 | + size: SIZE, |
| 75 | + mip_level_count: 1, |
| 76 | + sample_count: 4, |
| 77 | + dimension: wgpu::TextureDimension::D2, |
| 78 | + format: wgpu::TextureFormat::Rgba8Unorm, |
| 79 | + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TRANSIENT, |
| 80 | + view_formats: &[], |
| 81 | + }); |
| 82 | + |
| 83 | + let target_texture = ctx.device.create_texture(&wgpu::TextureDescriptor { |
| 84 | + label: None, |
| 85 | + size: SIZE, |
| 86 | + mip_level_count: 1, |
| 87 | + sample_count: 1, |
| 88 | + dimension: wgpu::TextureDimension::D2, |
| 89 | + format: wgpu::TextureFormat::Rgba8Unorm, |
| 90 | + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, |
| 91 | + view_formats: &[], |
| 92 | + }); |
| 93 | + |
| 94 | + let readback_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { |
| 95 | + label: None, |
| 96 | + size: 256 * 256 * 4, |
| 97 | + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, |
| 98 | + mapped_at_creation: false, |
| 99 | + }); |
| 100 | + |
| 101 | + let mut encoder = ctx |
| 102 | + .device |
| 103 | + .create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); |
| 104 | + |
| 105 | + { |
| 106 | + let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { |
| 107 | + label: None, |
| 108 | + color_attachments: &[Some(wgpu::RenderPassColorAttachment { |
| 109 | + view: &transient_texture.create_view(&wgpu::TextureViewDescriptor::default()), |
| 110 | + depth_slice: None, |
| 111 | + resolve_target: Some( |
| 112 | + &target_texture.create_view(&wgpu::TextureViewDescriptor::default()), |
| 113 | + ), |
| 114 | + ops: wgpu::Operations { |
| 115 | + load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), |
| 116 | + store: wgpu::StoreOp::Discard, |
| 117 | + }, |
| 118 | + })], |
| 119 | + depth_stencil_attachment: None, |
| 120 | + timestamp_writes: None, |
| 121 | + occlusion_query_set: None, |
| 122 | + }); |
| 123 | + |
| 124 | + rpass.set_pipeline(&pipeline); |
| 125 | + rpass.draw(0..3, 0..1); |
| 126 | + } |
| 127 | + |
| 128 | + encoder.copy_texture_to_buffer( |
| 129 | + wgpu::TexelCopyTextureInfo { |
| 130 | + texture: &target_texture, |
| 131 | + mip_level: 0, |
| 132 | + origin: wgpu::Origin3d { x: 0, y: 0, z: 0 }, |
| 133 | + aspect: wgpu::TextureAspect::All, |
| 134 | + }, |
| 135 | + wgpu::TexelCopyBufferInfo { |
| 136 | + buffer: &readback_buffer, |
| 137 | + layout: wgpu::TexelCopyBufferLayout { |
| 138 | + offset: 0, |
| 139 | + bytes_per_row: Some(256 * 4), |
| 140 | + rows_per_image: Some(256), |
| 141 | + }, |
| 142 | + }, |
| 143 | + SIZE, |
| 144 | + ); |
| 145 | + |
| 146 | + ctx.queue.submit([encoder.finish()]); |
| 147 | + |
| 148 | + let slice = readback_buffer.slice(..); |
| 149 | + slice.map_async(wgpu::MapMode::Read, |_| ()); |
| 150 | + |
| 151 | + ctx.async_poll(wgpu::PollType::wait_indefinitely()) |
| 152 | + .await |
| 153 | + .unwrap(); |
| 154 | + |
| 155 | + let data = slice.get_mapped_range(); |
| 156 | + let succeeded = data.iter().all(|b| *b == u8::MAX); |
| 157 | + assert!(succeeded); |
| 158 | + }); |
0 commit comments