Skip to content

Commit

Permalink
Refine Sampler Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Jan 2, 2025
1 parent 5046fe0 commit 1ec761b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ version = "23.0.0"
anyhow = "1.0.95"
argh = "0.1.13"
arrayvec = "0.7"
approx = "0.5"
bincode = "1"
bit-vec = "0.8"
bitflags = "2.6"
Expand Down
1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ webgl = ["wgpu/webgl"]
[dependencies]
anyhow.workspace = true
arrayvec.workspace = true
approx.workspace = true
bitflags.workspace = true
bytemuck.workspace = true
cfg-if.workspace = true
Expand Down
59 changes: 39 additions & 20 deletions tests/tests/samplers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ const MULTI_GROUP_BINDINGS: &str = r#"
const SAMPLER_CODE: &str = r#"
@compute @workgroup_size(1, 1, 1)
fn cs_main() {
// When sampling a 2x2 texture at 3/8, we can get a different result
// from each sampler if we vary linear/nearest filtering on S/T.
results[0] = textureSampleLevel(texture, sampler0, vec2f(3.0 / 8.0), 0.0);
results[1] = textureSampleLevel(texture, sampler1, vec2f(3.0 / 8.0), 0.0);
results[2] = textureSampleLevel(texture, sampler2, vec2f(3.0 / 8.0), 0.0);
// When sampling a 2x2 texture at the bottom left, we can change the address mode
// on S/T to get different values. This allows us to make sure the right sampler
// is being used.
results[0] = textureSampleLevel(texture, sampler0, vec2f(0.0, 1.0), 0.0);
results[1] = textureSampleLevel(texture, sampler1, vec2f(0.0, 1.0), 0.0);
results[2] = textureSampleLevel(texture, sampler2, vec2f(0.0, 1.0), 0.0);
}
"#;

Expand All @@ -163,12 +164,22 @@ enum GroupType {

#[gpu_test]
static SAMPLER_SINGLE_BIND_GROUP: GpuTestConfiguration = GpuTestConfiguration::new()
.parameters(TestParameters::default().test_features_limits())
.parameters(
TestParameters::default()
.test_features_limits()
// In OpenGL textures cannot be used with multiple samplers.
.skip(wgpu_test::FailureCase::backend(wgpu::Backends::GL)),
)
.run_sync(|ctx| sampler_bind_group(ctx, GroupType::Single));

#[gpu_test]
static SAMPLER_MULTI_BIND_GROUP: GpuTestConfiguration = GpuTestConfiguration::new()
.parameters(TestParameters::default().test_features_limits())
.parameters(
TestParameters::default()
.test_features_limits()
// In OpenGL textures cannot be used with multiple samplers.
.skip(wgpu_test::FailureCase::backend(wgpu::Backends::GL)),
)
.run_sync(|ctx| sampler_bind_group(ctx, GroupType::Multi));

fn sampler_bind_group(ctx: TestingContext, group_type: GroupType) {
Expand Down Expand Up @@ -353,20 +364,23 @@ fn sampler_bind_group(ctx: TestingContext, group_type: GroupType) {
},
);

let sampler_settings = [
(wgpu::FilterMode::Nearest, wgpu::FilterMode::Nearest),
(wgpu::FilterMode::Linear, wgpu::FilterMode::Nearest),
(wgpu::FilterMode::Nearest, wgpu::FilterMode::Linear),
let address_modes = [
(
wgpu::AddressMode::ClampToEdge,
wgpu::AddressMode::ClampToEdge,
),
(wgpu::AddressMode::Repeat, wgpu::AddressMode::ClampToEdge),
(wgpu::AddressMode::ClampToEdge, wgpu::AddressMode::Repeat),
];

let samplers = sampler_settings.map(|(mag_filter, min_filter)| {
let samplers = address_modes.map(|(address_mode_u, address_mode_v)| {
ctx.device.create_sampler(&wgpu::SamplerDescriptor {
label: None,
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_u,
address_mode_v,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter,
min_filter,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Nearest,
lod_min_clamp: 0.0,
lod_max_clamp: 100.0,
Expand Down Expand Up @@ -517,8 +531,13 @@ fn sampler_bind_group(ctx: TestingContext, group_type: GroupType) {

let f32_buffer: &[f32] = bytemuck::cast_slice(&buffer_data);

assert_eq!(
*f32_buffer,
[1.0, 0.0, 0.0, 1.0, 0.6249943, 0.2500038, 0.2500038, 1.0, 1.0, 0.0, 0.0, 1.0]
);
let correct_values: [f32; 12] = [
0.0, 0.0, 1.0, 1.0, //
0.5, 0.5, 1.0, 1.0, //
0.5, 0.0, 0.5, 1.0, //
];
let iter = f32_buffer.iter().zip(correct_values.iter());
for (&result, &value) in iter {
approx::assert_relative_eq!(result, value, max_relative = 0.0001);
}
}

0 comments on commit 1ec761b

Please sign in to comment.