Skip to content

Commit 3047510

Browse files
committed
[pr feedback] minor cleanup in zero_init_texture_after_discard, use hygene
1 parent 87dc9b0 commit 3047510

File tree

2 files changed

+35
-43
lines changed

2 files changed

+35
-43
lines changed

wgpu-core/src/command/memory_init.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
use std::collections::hash_map::Entry;
2-
use std::ops::Range;
3-
use std::vec::Drain;
1+
use std::{collections::hash_map::Entry, ops::Range, vec::Drain};
42

53
use hal::CommandEncoder;
64

7-
use crate::command::collect_zero_buffer_copies_for_clear_texture;
8-
use crate::device::Device;
9-
use crate::hub::Storage;
10-
use crate::id;
11-
use crate::id::TextureId;
12-
use crate::init_tracker::*;
13-
use crate::resource::{Buffer, Texture};
14-
use crate::track::TrackerSet;
15-
use crate::track::{ResourceTracker, TextureSelector, TextureState};
16-
use crate::FastHashMap;
5+
use crate::{
6+
command::collect_zero_buffer_copies_for_clear_texture,
7+
device::Device,
8+
hub::Storage,
9+
id::{self, TextureId},
10+
init_tracker::*,
11+
resource::{Buffer, Texture},
12+
track::{ResourceTracker, TextureSelector, TextureState, TrackerSet},
13+
FastHashMap,
14+
};
1715

1816
use super::{BakedCommands, DestroyedBufferError, DestroyedTextureError};
1917

wgpu/tests/zero_init_texture_after_discard.rs

+24-30
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn discarding_color_target_resets_texture_init_state_check_visible_on_copy_after
1212
let mut encoder = ctx
1313
.device
1414
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
15-
drop(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
15+
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
1616
label: Some("Color Discard"),
1717
color_attachments: &[wgpu::RenderPassColorAttachment {
1818
view: &texture.create_view(&wgpu::TextureViewDescriptor::default()),
@@ -23,7 +23,7 @@ fn discarding_color_target_resets_texture_init_state_check_visible_on_copy_after
2323
},
2424
}],
2525
depth_stencil_attachment: None,
26-
}));
26+
});
2727
ctx.queue.submit([encoder.finish()]);
2828
}
2929
{
@@ -47,7 +47,7 @@ fn discarding_color_target_resets_texture_init_state_check_visible_on_copy_in_sa
4747
let mut encoder = ctx
4848
.device
4949
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
50-
drop(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
50+
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
5151
label: Some("Color Discard"),
5252
color_attachments: &[wgpu::RenderPassColorAttachment {
5353
view: &texture.create_view(&wgpu::TextureViewDescriptor::default()),
@@ -58,7 +58,7 @@ fn discarding_color_target_resets_texture_init_state_check_visible_on_copy_in_sa
5858
},
5959
}],
6060
depth_stencil_attachment: None,
61-
}));
61+
});
6262
copy_texture_to_buffer(&mut encoder, &texture, &readback_buffer);
6363
ctx.queue.submit([encoder.finish()]);
6464
}
@@ -79,7 +79,7 @@ fn discarding_depth_target_resets_texture_init_state_check_visible_on_copy_in_sa
7979
let mut encoder = ctx
8080
.device
8181
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
82-
drop(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
82+
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
8383
label: Some("Depth Discard"),
8484
color_attachments: &[],
8585
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
@@ -93,7 +93,7 @@ fn discarding_depth_target_resets_texture_init_state_check_visible_on_copy_in_sa
9393
store: false, // discard!
9494
}),
9595
}),
96-
}));
96+
});
9797
copy_texture_to_buffer(&mut encoder, &texture, &readback_buffer);
9898
ctx.queue.submit([encoder.finish()]);
9999
}
@@ -109,12 +109,12 @@ fn discarding_either_depth_or_stencil_aspect() {
109109
&ctx,
110110
wgpu::TextureFormat::Depth24PlusStencil8,
111111
);
112-
// TODO: How do we test this other than "doesn't crash"? we can't copy the texture to/from buffers!
112+
// TODO: How do we test this other than "doesn't crash"? We can't copy the texture to/from buffers, so we would need to do a copy in a shader
113113
{
114114
let mut encoder = ctx
115115
.device
116116
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
117-
drop(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
117+
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
118118
label: Some("Depth Discard, Stencil Load"),
119119
color_attachments: &[],
120120
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
@@ -128,14 +128,14 @@ fn discarding_either_depth_or_stencil_aspect() {
128128
store: true,
129129
}),
130130
}),
131-
}));
131+
});
132132
ctx.queue.submit([encoder.finish()]);
133133
}
134134
{
135135
let mut encoder = ctx
136136
.device
137137
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
138-
drop(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
138+
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
139139
label: Some("Depth Load, Stencil Discard"),
140140
color_attachments: &[],
141141
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
@@ -149,7 +149,7 @@ fn discarding_either_depth_or_stencil_aspect() {
149149
store: false, // discard!
150150
}),
151151
}),
152-
}));
152+
});
153153
ctx.queue.submit([encoder.finish()]);
154154
}
155155
});
@@ -174,8 +174,7 @@ fn create_white_texture_and_readback_buffer(
174174
let format_desc = format.describe();
175175

176176
// Size for tests is chosen so that we don't need to care about buffer alignments.
177-
assert_eq!(format_desc.block_dimensions.0, 1);
178-
assert_eq!(format_desc.block_dimensions.1, 1);
177+
assert_eq!(format_desc.block_dimensions, (1, 1));
179178
assert_eq!(format_desc.block_size as u32, BYTES_PER_PIXEL);
180179
assert_eq!(
181180
(TEXTURE_SIZE.width * format_desc.block_size as u32) % wgpu::COPY_BYTES_PER_ROW_ALIGNMENT,
@@ -209,14 +208,11 @@ fn create_white_texture_and_readback_buffer(
209208
// This behavior is curious, but does not violate any spec - it is wgpu's job to pass this test no matter what a render target discard does.
210209

211210
// ... but that said, for depth/stencil textures we need to do a clear.
212-
if format == wgpu::TextureFormat::Depth32Float
213-
|| format == wgpu::TextureFormat::Depth24PlusStencil8
214-
|| format == wgpu::TextureFormat::Depth24Plus
215-
{
211+
if format_desc.sample_type == wgpu::TextureSampleType::Depth {
216212
let mut encoder = ctx
217213
.device
218214
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
219-
drop(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
215+
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
220216
label: Some("Depth/Stencil setup"),
221217
color_attachments: &[],
222218
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
@@ -230,7 +226,7 @@ fn create_white_texture_and_readback_buffer(
230226
store: true,
231227
}),
232228
}),
233-
}));
229+
});
234230
ctx.queue.submit([encoder.finish()]);
235231
} else {
236232
let data = vec![255; buffer_size as usize];
@@ -267,7 +263,7 @@ fn copy_texture_to_buffer(
267263
wgpu::ImageCopyTexture {
268264
texture,
269265
mip_level: 0,
270-
origin: wgpu::Origin3d { x: 0, y: 0, z: 0 },
266+
origin: wgpu::Origin3d::ZERO,
271267
aspect: wgpu::TextureAspect::All,
272268
},
273269
wgpu::ImageCopyBuffer {
@@ -279,16 +275,14 @@ fn copy_texture_to_buffer(
279275
}
280276

281277
fn assert_buffer_is_zero(readback_buffer: &wgpu::Buffer, device: &wgpu::Device) {
282-
{
283-
let buffer_slice = readback_buffer.slice(..);
284-
let _ = buffer_slice.map_async(wgpu::MapMode::Read);
285-
device.poll(wgpu::Maintain::Wait);
286-
let buffer_view = buffer_slice.get_mapped_range();
278+
let buffer_slice = readback_buffer.slice(..);
279+
let _ = buffer_slice.map_async(wgpu::MapMode::Read);
280+
device.poll(wgpu::Maintain::Wait);
281+
let buffer_view = buffer_slice.get_mapped_range();
287282

288-
assert!(
289-
buffer_view.iter().all(|b| *b == 0),
290-
"texture was not fully cleared"
291-
);
292-
}
283+
assert!(
284+
buffer_view.iter().all(|b| *b == 0),
285+
"texture was not fully cleared"
286+
);
293287
readback_buffer.unmap();
294288
}

0 commit comments

Comments
 (0)