Skip to content

Commit

Permalink
Define tolerance data to improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Dec 1, 2022
1 parent 5b58cf8 commit 3d37a31
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions player/tests/data/clear-buffer-texture.ron
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
dimension: r#2d,
format: "rgba8unorm",
usage: 27,
view_formats: [],
)),
// First fill the texture to ensure it wasn't just zero initialized or "happened" to be zero.
WriteTexture(
Expand Down
1 change: 1 addition & 0 deletions player/tests/data/quad.ron
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
dimension: r#2d,
format: "rgba8unorm",
usage: 27,
view_formats: [],
)),
CreateTextureView(
id: Id(0, 1, Empty),
Expand Down
2 changes: 2 additions & 0 deletions player/tests/data/zero-init-texture-binding.ron
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
dimension: r#2d,
format: "rgba8unorm",
usage: 5, // SAMPLED + COPY_SRC
view_formats: [],
)),
CreateTextureView(
id: Id(0, 1, Empty),
Expand All @@ -54,6 +55,7 @@
dimension: r#2d,
format: "rgba8unorm",
usage: 9, // STORAGE + COPY_SRC
view_formats: [],
)),
CreateTextureView(
id: Id(1, 1, Empty),
Expand Down
1 change: 1 addition & 0 deletions player/tests/data/zero-init-texture-copytobuffer.ron
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
dimension: r#2d,
format: "rgba8unorm",
usage: 1, // COPY_SRC
view_formats: [],
)),
CreateBuffer(
Id(0, 1, Empty),
Expand Down
1 change: 1 addition & 0 deletions player/tests/data/zero-init-texture-rendertarget.ron
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
dimension: r#2d,
format: "rgba8unorm",
usage: 17, // RENDER_ATTACHMENT + COPY_SRC
view_formats: [],
)),
CreateTextureView(
id: Id(0, 1, Empty),
Expand Down
36 changes: 20 additions & 16 deletions wgpu/tests/shader_view_format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ use wgpu::{util::DeviceExt, TextureFormat};
fn reinterpret_srgb_ness() {
let parameters = TestParameters::default();
initialize_test(parameters, |ctx| {
let shader = ctx
.device
.create_shader_module(wgpu::include_wgsl!("view_format.wgsl"));

let size = wgpu::Extent3d {
width: 2,
height: 2,
depth_or_array_layers: 1,
};
let unorm_data: [[u8; 4]; 4] = [
[180, 0, 0, 255],
[0, 84, 0, 127],
Expand All @@ -28,6 +19,16 @@ fn reinterpret_srgb_ness() {
[12, 116, 23, 90],
];

let size = wgpu::Extent3d {
width: 2,
height: 2,
depth_or_array_layers: 1,
};

let shader = ctx
.device
.create_shader_module(wgpu::include_wgsl!("view_format.wgsl"));

// Reinterpret Rgba8Unorm as Rgba8UnormSrgb
reinterpret(
&ctx,
Expand Down Expand Up @@ -111,7 +112,7 @@ fn reinterpret(
label: None,
});

let out_tex = ctx.device.create_texture(&wgpu::TextureDescriptor {
let target_tex = ctx.device.create_texture(&wgpu::TextureDescriptor {
label: None,
size,
mip_level_count: 1,
Expand All @@ -121,7 +122,7 @@ fn reinterpret(
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
});
let target_view = out_tex.create_view(&wgpu::TextureViewDescriptor::default());
let target_view = target_tex.create_view(&wgpu::TextureViewDescriptor::default());

let mut encoder = ctx
.device
Expand Down Expand Up @@ -153,7 +154,7 @@ fn reinterpret(
.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
encoder.copy_texture_to_buffer(
wgpu::ImageCopyTexture {
texture: &out_tex,
texture: &target_tex,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
Expand All @@ -175,15 +176,18 @@ fn reinterpret(
ctx.device.poll(wgpu::Maintain::Wait);

let data: Vec<u8> = slice.get_mapped_range().to_vec();
let tolerance_data: [[u8; 4]; 4] = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [1, 1, 1, 0]];

for h in 0..size.height {
let offset = h * wgpu::COPY_BYTES_PER_ROW_ALIGNMENT;
for w in 0..size.width {
let expect = expect_data[(h * size.width + w) as usize];
let tolerance = tolerance_data[(h * size.width + w) as usize];
let index = (w * 4 + offset) as usize;
if calc_difference(expect[0], data[index]) > 1
|| calc_difference(expect[1], data[index + 1]) > 1
|| calc_difference(expect[2], data[index + 2]) > 1
|| calc_difference(expect[3], data[index + 3]) > 0
if calc_difference(expect[0], data[index]) > tolerance[0]
|| calc_difference(expect[1], data[index + 1]) > tolerance[1]
|| calc_difference(expect[2], data[index + 2]) > tolerance[2]
|| calc_difference(expect[3], data[index + 3]) > tolerance[3]
{
panic!(
"Reinterpret {:?} as {:?} mismatch! expect {:?} get [{}, {}, {}, {}]",
Expand Down

0 comments on commit 3d37a31

Please sign in to comment.