|
| 1 | +use std::num::NonZeroU32; |
| 2 | + |
| 3 | +use wgpu::*; |
| 4 | +use wgpu_test::{ |
| 5 | + gpu_test, image::ReadbackBuffers, FailureCase, GpuTestConfiguration, TestParameters, |
| 6 | + TestingContext, |
| 7 | +}; |
| 8 | + |
| 9 | +#[gpu_test] |
| 10 | +static BINDING_ARRAY_STORAGE_TEXTURES: GpuTestConfiguration = GpuTestConfiguration::new() |
| 11 | + .parameters( |
| 12 | + TestParameters::default() |
| 13 | + .features( |
| 14 | + Features::TEXTURE_BINDING_ARRAY |
| 15 | + | Features::STORAGE_RESOURCE_BINDING_ARRAY |
| 16 | + | Features::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING |
| 17 | + | Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES, |
| 18 | + ) |
| 19 | + .limits(Limits { |
| 20 | + max_storage_textures_per_shader_stage: 17, |
| 21 | + ..Limits::default() |
| 22 | + }) |
| 23 | + .expect_fail(FailureCase::backend(Backends::METAL)), |
| 24 | + ) |
| 25 | + .run_async(binding_array_storage_textures); |
| 26 | + |
| 27 | +/// Test to see how texture bindings array work and additionally making sure |
| 28 | +/// that non-uniform indexing is working correctly. |
| 29 | +/// |
| 30 | +/// If non-uniform indexing is not working correctly, AMD will produce the wrong |
| 31 | +/// output due to non-native support for non-uniform indexing within a WARP. |
| 32 | +async fn binding_array_storage_textures(ctx: TestingContext) { |
| 33 | + let shader = r#" |
| 34 | + @group(0) @binding(0) |
| 35 | + var textures: binding_array<texture_storage_2d<rgba8unorm, read_write> >; |
| 36 | +
|
| 37 | + @compute |
| 38 | + @workgroup_size(4, 4, 1) |
| 39 | + fn compMain(@builtin(global_invocation_id) id: vec3u) { |
| 40 | + // Read from the 4x4 textures in 0-15, then write to the 4x4 texture in 16 |
| 41 | +
|
| 42 | + let pixel = vec2u(id.xy); |
| 43 | + let index = pixel.y * 4 + pixel.x; |
| 44 | +
|
| 45 | + let color = textureLoad(textures[index], vec2u(0)); |
| 46 | + textureStore(textures[16], pixel, color); |
| 47 | + } |
| 48 | + "#; |
| 49 | + |
| 50 | + let module = ctx |
| 51 | + .device |
| 52 | + .create_shader_module(wgpu::ShaderModuleDescriptor { |
| 53 | + label: Some("Binding Array Texture"), |
| 54 | + source: wgpu::ShaderSource::Wgsl(shader.into()), |
| 55 | + }); |
| 56 | + |
| 57 | + let image = image::load_from_memory(include_bytes!("../3x3_colors.png")).unwrap(); |
| 58 | + // Resize image to 4x4 |
| 59 | + let image = image |
| 60 | + .resize_exact(4, 4, image::imageops::FilterType::Gaussian) |
| 61 | + .into_rgba8(); |
| 62 | + |
| 63 | + // Create one texture for each pixel |
| 64 | + let mut input_views = Vec::with_capacity(64); |
| 65 | + for data in image.pixels() { |
| 66 | + let texture = ctx.device.create_texture(&wgpu::TextureDescriptor { |
| 67 | + label: None, |
| 68 | + size: Extent3d { |
| 69 | + width: 1, |
| 70 | + height: 1, |
| 71 | + depth_or_array_layers: 1, |
| 72 | + }, |
| 73 | + mip_level_count: 1, |
| 74 | + sample_count: 1, |
| 75 | + dimension: TextureDimension::D2, |
| 76 | + format: TextureFormat::Rgba8Unorm, |
| 77 | + usage: TextureUsages::STORAGE_BINDING | TextureUsages::COPY_DST, |
| 78 | + view_formats: &[], |
| 79 | + }); |
| 80 | + |
| 81 | + ctx.queue.write_texture( |
| 82 | + TexelCopyTextureInfo { |
| 83 | + texture: &texture, |
| 84 | + mip_level: 0, |
| 85 | + origin: Origin3d::ZERO, |
| 86 | + aspect: TextureAspect::All, |
| 87 | + }, |
| 88 | + &data.0, |
| 89 | + TexelCopyBufferLayout { |
| 90 | + offset: 0, |
| 91 | + bytes_per_row: Some(4), |
| 92 | + rows_per_image: Some(1), |
| 93 | + }, |
| 94 | + Extent3d { |
| 95 | + width: 1, |
| 96 | + height: 1, |
| 97 | + depth_or_array_layers: 1, |
| 98 | + }, |
| 99 | + ); |
| 100 | + |
| 101 | + input_views.push(texture.create_view(&TextureViewDescriptor::default())); |
| 102 | + } |
| 103 | + |
| 104 | + let output_texture = ctx.device.create_texture(&wgpu::TextureDescriptor { |
| 105 | + label: Some("Output Texture"), |
| 106 | + size: Extent3d { |
| 107 | + width: 4, |
| 108 | + height: 4, |
| 109 | + depth_or_array_layers: 1, |
| 110 | + }, |
| 111 | + mip_level_count: 1, |
| 112 | + sample_count: 1, |
| 113 | + dimension: TextureDimension::D2, |
| 114 | + format: TextureFormat::Rgba8Unorm, |
| 115 | + usage: TextureUsages::STORAGE_BINDING | TextureUsages::COPY_SRC, |
| 116 | + view_formats: &[], |
| 117 | + }); |
| 118 | + |
| 119 | + let output_view = output_texture.create_view(&TextureViewDescriptor::default()); |
| 120 | + |
| 121 | + let bind_group_layout = ctx |
| 122 | + .device |
| 123 | + .create_bind_group_layout(&BindGroupLayoutDescriptor { |
| 124 | + label: Some("Bind Group Layout"), |
| 125 | + entries: &[BindGroupLayoutEntry { |
| 126 | + binding: 0, |
| 127 | + visibility: ShaderStages::COMPUTE, |
| 128 | + ty: BindingType::StorageTexture { |
| 129 | + access: StorageTextureAccess::ReadWrite, |
| 130 | + format: TextureFormat::Rgba8Unorm, |
| 131 | + view_dimension: TextureViewDimension::D2, |
| 132 | + }, |
| 133 | + count: Some(NonZeroU32::new(4 * 4 + 1).unwrap()), |
| 134 | + }], |
| 135 | + }); |
| 136 | + |
| 137 | + let mut input_view_references: Vec<_> = input_views.iter().collect(); |
| 138 | + input_view_references.push(&output_view); |
| 139 | + |
| 140 | + let bind_group = ctx.device.create_bind_group(&BindGroupDescriptor { |
| 141 | + label: Some("Bind Group"), |
| 142 | + layout: &bind_group_layout, |
| 143 | + entries: &[BindGroupEntry { |
| 144 | + binding: 0, |
| 145 | + resource: BindingResource::TextureViewArray(&input_view_references), |
| 146 | + }], |
| 147 | + }); |
| 148 | + |
| 149 | + let pipeline_layout = ctx |
| 150 | + .device |
| 151 | + .create_pipeline_layout(&PipelineLayoutDescriptor { |
| 152 | + label: Some("Pipeline Layout"), |
| 153 | + bind_group_layouts: &[&bind_group_layout], |
| 154 | + push_constant_ranges: &[], |
| 155 | + }); |
| 156 | + |
| 157 | + let pipeline = ctx |
| 158 | + .device |
| 159 | + .create_compute_pipeline(&ComputePipelineDescriptor { |
| 160 | + label: Some("Compute Pipeline"), |
| 161 | + layout: Some(&pipeline_layout), |
| 162 | + module: &module, |
| 163 | + entry_point: Some("compMain"), |
| 164 | + compilation_options: Default::default(), |
| 165 | + cache: None, |
| 166 | + }); |
| 167 | + |
| 168 | + let mut encoder = ctx |
| 169 | + .device |
| 170 | + .create_command_encoder(&CommandEncoderDescriptor { label: None }); |
| 171 | + { |
| 172 | + let mut render_pass = encoder.begin_compute_pass(&ComputePassDescriptor { |
| 173 | + label: None, |
| 174 | + timestamp_writes: None, |
| 175 | + }); |
| 176 | + render_pass.set_pipeline(&pipeline); |
| 177 | + render_pass.set_bind_group(0, &bind_group, &[]); |
| 178 | + render_pass.dispatch_workgroups(1, 1, 1); |
| 179 | + } |
| 180 | + |
| 181 | + let readback_buffers = ReadbackBuffers::new(&ctx.device, &output_texture); |
| 182 | + readback_buffers.copy_from(&ctx.device, &mut encoder, &output_texture); |
| 183 | + |
| 184 | + ctx.queue.submit(Some(encoder.finish())); |
| 185 | + |
| 186 | + readback_buffers.assert_buffer_contents(&ctx, &image).await; |
| 187 | +} |
0 commit comments