-
Notifications
You must be signed in to change notification settings - Fork 946
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fea9e9
commit 7710f79
Showing
5 changed files
with
172 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use std::{num::NonZeroU64, ops::Range}; | ||
|
||
use wasm_bindgen_test::wasm_bindgen_test; | ||
use wgpu_test::{initialize_test, TestParameters, TestingContext}; | ||
|
||
fn fill_test(ctx: &TestingContext, range: Range<u64>, size: u64) -> bool { | ||
let gpu_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { | ||
label: Some("gpu_buffer"), | ||
size, | ||
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC, | ||
mapped_at_creation: false, | ||
}); | ||
|
||
let cpu_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { | ||
label: Some("cpu_buffer"), | ||
size, | ||
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, | ||
mapped_at_creation: false, | ||
}); | ||
|
||
// Initialize the whole buffer with values. | ||
let buffer_contents = vec![0xFF_u8; size as usize]; | ||
ctx.queue.write_buffer(&gpu_buffer, 0, &buffer_contents); | ||
|
||
let mut encoder = ctx | ||
.device | ||
.create_command_encoder(&wgpu::CommandEncoderDescriptor { | ||
label: Some("encoder"), | ||
}); | ||
|
||
encoder.clear_buffer( | ||
&gpu_buffer, | ||
range.start, | ||
NonZeroU64::new(range.end - range.start), | ||
); | ||
encoder.copy_buffer_to_buffer(&gpu_buffer, 0, &cpu_buffer, 0, size); | ||
|
||
ctx.queue.submit(Some(encoder.finish())); | ||
cpu_buffer.slice(..).map_async(wgpu::MapMode::Read, |_| ()); | ||
ctx.device.poll(wgpu::Maintain::Wait); | ||
|
||
let buffer_slice = cpu_buffer.slice(..); | ||
let buffer_data = buffer_slice.get_mapped_range(); | ||
|
||
let first_clear_byte = buffer_data | ||
.iter() | ||
.enumerate() | ||
.find_map(|(index, byte)| (*byte == 0x00).then_some(index)) | ||
.expect("No clear happened at all"); | ||
|
||
let first_dirty_byte = buffer_data | ||
.iter() | ||
.enumerate() | ||
.skip(first_clear_byte) | ||
.find_map(|(index, byte)| (*byte != 0x00).then_some(index)) | ||
.unwrap_or(size as usize); | ||
|
||
let second_clear_byte = buffer_data | ||
.iter() | ||
.enumerate() | ||
.skip(first_dirty_byte) | ||
.find_map(|(index, byte)| (*byte == 0x00).then_some(index)); | ||
|
||
if second_clear_byte.is_some() { | ||
eprintln!("Found multiple cleared ranges instead of a single clear range of {}..{} on a buffer of size {}.", range.start, range.end, size); | ||
return false; | ||
} | ||
|
||
let cleared_range = first_clear_byte as u64..first_dirty_byte as u64; | ||
|
||
if cleared_range != range { | ||
eprintln!( | ||
"Cleared range is {}..{}, but the clear range is {}..{} on a buffer of size {}.", | ||
cleared_range.start, cleared_range.end, range.start, range.end, size | ||
); | ||
return false; | ||
} | ||
|
||
eprintln!( | ||
"Cleared range is {}..{} on a buffer of size {}.", | ||
cleared_range.start, cleared_range.end, size | ||
); | ||
|
||
return true; | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
#[test] | ||
fn clear_buffer_bug() { | ||
initialize_test(TestParameters::default(), |ctx| { | ||
let mut succeeded = true; | ||
for power in 4..16 { | ||
let size = 1 << power; | ||
for start_offset in (0..=68).step_by(4) { | ||
for size_offset in (0..=68).step_by(4) { | ||
let range = start_offset..size + size_offset + start_offset; | ||
let result = fill_test(&ctx, range.clone(), 1 << 16); | ||
|
||
let copy_length = range.end - range.start; | ||
|
||
// This is the precondiiton for the nvidia bug. This is for reference only, | ||
// as the bug is worked around. | ||
let _succeeds = 'b: { | ||
if copy_length >= 4096 { | ||
if start_offset % 16 != 0 { | ||
if copy_length == 4096 { | ||
break 'b true; | ||
} | ||
if copy_length % 16 == 0 { | ||
break 'b false; | ||
} | ||
} | ||
} | ||
true | ||
}; | ||
|
||
succeeded &= result; | ||
} | ||
} | ||
} | ||
assert!(succeeded); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters