|
| 1 | +use wgpu::{ |
| 2 | + util::{BufferInitDescriptor, DeviceExt}, |
| 3 | + vertex_attr_array, |
| 4 | +}; |
| 5 | +use wgpu_test::{ |
| 6 | + gpu_test, FailureCase, GpuTestConfiguration, GpuTestInitializer, TestParameters, TestingContext, |
| 7 | +}; |
| 8 | + |
| 9 | +pub fn all_tests(vec: &mut Vec<GpuTestInitializer>) { |
| 10 | + vec.push(SET_ARRAY_STRIDE_TO_0); |
| 11 | +} |
| 12 | + |
| 13 | +#[gpu_test] |
| 14 | +static SET_ARRAY_STRIDE_TO_0: GpuTestConfiguration = GpuTestConfiguration::new() |
| 15 | + .parameters( |
| 16 | + TestParameters::default() |
| 17 | + .limits(wgpu::Limits::downlevel_defaults()) |
| 18 | + .expect_fail(FailureCase::backend(wgpu::Backends::METAL)), |
| 19 | + ) |
| 20 | + .run_async(set_array_stride_to_0); |
| 21 | + |
| 22 | +/// Tests that draws using a vertex buffer with stride of 0 works correctly (especially on the |
| 23 | +/// D3D12 backend; see commentary within). |
| 24 | +async fn set_array_stride_to_0(ctx: TestingContext) { |
| 25 | + let position_buffer_content: &[f32; 12] = &[ |
| 26 | + // Triangle 1 |
| 27 | + -1.0, -1.0, // Bottom left |
| 28 | + 1.0, 1.0, // Top right |
| 29 | + -1.0, 1.0, // Top left |
| 30 | + // Triangle 2 |
| 31 | + -1.0, -1.0, // Bottom left |
| 32 | + 1.0, -1.0, // Bottom right |
| 33 | + 1.0, 1.0, // Top right |
| 34 | + ]; |
| 35 | + let position_buffer = ctx.device.create_buffer_init(&BufferInitDescriptor { |
| 36 | + label: None, |
| 37 | + contents: bytemuck::cast_slice::<f32, u8>(position_buffer_content), |
| 38 | + usage: wgpu::BufferUsages::VERTEX, |
| 39 | + }); |
| 40 | + |
| 41 | + let color_buffer_content: &[f32; 4] = &[1.0, 1.0, 1.0, 1.0]; |
| 42 | + let color_buffer = ctx.device.create_buffer_init(&BufferInitDescriptor { |
| 43 | + label: None, |
| 44 | + contents: bytemuck::cast_slice::<f32, u8>(color_buffer_content), |
| 45 | + usage: wgpu::BufferUsages::VERTEX, |
| 46 | + }); |
| 47 | + |
| 48 | + let shader_src = " |
| 49 | + struct VertexOutput { |
| 50 | + @builtin(position) position: vec4f, |
| 51 | + @location(0) color: vec4f, |
| 52 | + } |
| 53 | +
|
| 54 | + @vertex |
| 55 | + fn vs_main(@location(0) position: vec2f, @location(1) color: vec4f) -> VertexOutput { |
| 56 | + return VertexOutput(vec4f(position, 0.0, 1.0), color); |
| 57 | + } |
| 58 | +
|
| 59 | + @fragment |
| 60 | + fn fs_main(@location(0) color: vec4f) -> @location(0) vec4f { |
| 61 | + return color; |
| 62 | + } |
| 63 | + "; |
| 64 | + |
| 65 | + let shader = ctx |
| 66 | + .device |
| 67 | + .create_shader_module(wgpu::ShaderModuleDescriptor { |
| 68 | + label: None, |
| 69 | + source: wgpu::ShaderSource::Wgsl(shader_src.into()), |
| 70 | + }); |
| 71 | + |
| 72 | + let vbl = [ |
| 73 | + wgpu::VertexBufferLayout { |
| 74 | + array_stride: 8, |
| 75 | + step_mode: wgpu::VertexStepMode::Vertex, |
| 76 | + attributes: &vertex_attr_array![0 => Float32x2], |
| 77 | + }, |
| 78 | + wgpu::VertexBufferLayout { |
| 79 | + array_stride: 0, |
| 80 | + step_mode: wgpu::VertexStepMode::Vertex, |
| 81 | + attributes: &vertex_attr_array![1 => Float32x4], |
| 82 | + }, |
| 83 | + ]; |
| 84 | + let pipeline_desc = wgpu::RenderPipelineDescriptor { |
| 85 | + label: None, |
| 86 | + layout: None, |
| 87 | + vertex: wgpu::VertexState { |
| 88 | + buffers: &vbl, |
| 89 | + module: &shader, |
| 90 | + entry_point: Some("vs_main"), |
| 91 | + compilation_options: Default::default(), |
| 92 | + }, |
| 93 | + primitive: wgpu::PrimitiveState::default(), |
| 94 | + depth_stencil: None, |
| 95 | + multisample: wgpu::MultisampleState::default(), |
| 96 | + fragment: Some(wgpu::FragmentState { |
| 97 | + module: &shader, |
| 98 | + entry_point: Some("fs_main"), |
| 99 | + compilation_options: Default::default(), |
| 100 | + targets: &[Some(wgpu::ColorTargetState { |
| 101 | + format: wgpu::TextureFormat::R8Unorm, |
| 102 | + blend: None, |
| 103 | + write_mask: wgpu::ColorWrites::ALL, |
| 104 | + })], |
| 105 | + }), |
| 106 | + multiview: None, |
| 107 | + cache: None, |
| 108 | + }; |
| 109 | + let mut first_pipeline_desc = pipeline_desc.clone(); |
| 110 | + let mut first_vbl = vbl.clone(); |
| 111 | + first_vbl[1].array_stride = 16; |
| 112 | + first_pipeline_desc.vertex.buffers = &first_vbl; |
| 113 | + let pipeline = ctx.device.create_render_pipeline(&pipeline_desc); |
| 114 | + let first_pipeline = ctx.device.create_render_pipeline(&first_pipeline_desc); |
| 115 | + |
| 116 | + let out_texture = ctx.device.create_texture(&wgpu::TextureDescriptor { |
| 117 | + label: None, |
| 118 | + size: wgpu::Extent3d { |
| 119 | + width: 256, |
| 120 | + height: 256, |
| 121 | + depth_or_array_layers: 1, |
| 122 | + }, |
| 123 | + mip_level_count: 1, |
| 124 | + sample_count: 1, |
| 125 | + dimension: wgpu::TextureDimension::D2, |
| 126 | + format: wgpu::TextureFormat::R8Unorm, |
| 127 | + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, |
| 128 | + view_formats: &[], |
| 129 | + }); |
| 130 | + let out_texture_view = out_texture.create_view(&wgpu::TextureViewDescriptor::default()); |
| 131 | + |
| 132 | + let readback_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { |
| 133 | + label: None, |
| 134 | + size: 256 * 256, |
| 135 | + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, |
| 136 | + mapped_at_creation: false, |
| 137 | + }); |
| 138 | + |
| 139 | + let mut encoder = ctx |
| 140 | + .device |
| 141 | + .create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); |
| 142 | + |
| 143 | + { |
| 144 | + let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { |
| 145 | + label: None, |
| 146 | + color_attachments: &[Some(wgpu::RenderPassColorAttachment { |
| 147 | + ops: wgpu::Operations::default(), |
| 148 | + resolve_target: None, |
| 149 | + view: &out_texture_view, |
| 150 | + depth_slice: None, |
| 151 | + })], |
| 152 | + depth_stencil_attachment: None, |
| 153 | + timestamp_writes: None, |
| 154 | + occlusion_query_set: None, |
| 155 | + }); |
| 156 | + |
| 157 | + // The D3D12 backend used to not set the stride of vertex buffers if it was 0. |
| 158 | + rpass.set_pipeline(&first_pipeline); // This call caused the D3D12 backend to set the stride for the 2nd vertex buffer to 16. |
| 159 | + rpass.set_pipeline(&pipeline); // This call doesn't set the stride for the 2nd vertex buffer to 0. |
| 160 | + rpass.set_vertex_buffer(0, position_buffer.slice(..)); |
| 161 | + rpass.set_vertex_buffer(1, color_buffer.slice(..)); |
| 162 | + rpass.draw(0..6, 0..1); // Causing this draw to be skipped since it would read OOB of the 2nd vertex buffer. |
| 163 | + } |
| 164 | + |
| 165 | + encoder.copy_texture_to_buffer( |
| 166 | + wgpu::TexelCopyTextureInfo { |
| 167 | + texture: &out_texture, |
| 168 | + mip_level: 0, |
| 169 | + origin: wgpu::Origin3d::ZERO, |
| 170 | + aspect: wgpu::TextureAspect::All, |
| 171 | + }, |
| 172 | + wgpu::TexelCopyBufferInfo { |
| 173 | + buffer: &readback_buffer, |
| 174 | + layout: wgpu::TexelCopyBufferLayout { |
| 175 | + offset: 0, |
| 176 | + bytes_per_row: Some(256), |
| 177 | + rows_per_image: None, |
| 178 | + }, |
| 179 | + }, |
| 180 | + wgpu::Extent3d { |
| 181 | + width: 256, |
| 182 | + height: 256, |
| 183 | + depth_or_array_layers: 1, |
| 184 | + }, |
| 185 | + ); |
| 186 | + |
| 187 | + ctx.queue.submit([encoder.finish()]); |
| 188 | + |
| 189 | + let slice = readback_buffer.slice(..); |
| 190 | + slice.map_async(wgpu::MapMode::Read, |_| ()); |
| 191 | + |
| 192 | + ctx.async_poll(wgpu::PollType::wait()).await.unwrap(); |
| 193 | + |
| 194 | + let data = slice.get_mapped_range(); |
| 195 | + let succeeded = data.iter().all(|b| *b == u8::MAX); |
| 196 | + assert!(succeeded); |
| 197 | +} |
0 commit comments