Skip to content

Commit

Permalink
Add image comparison to the mipmap example
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Jun 23, 2021
1 parent 3d8a4ba commit ebbbf22
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@

# Output from capture example
wgpu/red.png

# Output from invalid comparison tests
**/screenshot-difference.png
4 changes: 4 additions & 0 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ test = true
name="texture-arrays"
required-features = ["spirv"]

[[example]]
name="mipmap"
test = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.73" # remember to change version in wiki as well
web-sys = { version = "=0.3.50", features = [
Expand Down
4 changes: 2 additions & 2 deletions wgpu/examples/boids/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ impl framework::Example for Example {
/// a TriangleList draw call for all NUM_PARTICLES at 3 vertices each
fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
_spawner: &framework::Spawner,
) {
// create render pass descriptor and its color attachments
let color_attachments = [wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
Expand Down
4 changes: 2 additions & 2 deletions wgpu/examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl framework::Example for Example {

fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
_spawner: &framework::Spawner,
Expand Down Expand Up @@ -323,7 +323,7 @@ impl framework::Example for Example {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(clear_color),
Expand Down
4 changes: 2 additions & 2 deletions wgpu/examples/conservative-raster/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl framework::Example for Example {

fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
_spawner: &framework::Spawner,
Expand Down Expand Up @@ -285,7 +285,7 @@ impl framework::Example for Example {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("full resolution"),
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
Expand Down
4 changes: 2 additions & 2 deletions wgpu/examples/cube/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl framework::Example for Example {

fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
_spawner: &framework::Spawner,
Expand All @@ -344,7 +344,7 @@ impl framework::Example for Example {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
Expand Down
109 changes: 107 additions & 2 deletions wgpu/examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use winit::{
event_loop::{ControlFlow, EventLoop},
};

#[path = "../tests/common/mod.rs"]
mod test_common;

#[rustfmt::skip]
#[allow(unused)]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
Expand Down Expand Up @@ -54,7 +57,7 @@ pub trait Example: 'static + Sized {
fn update(&mut self, event: WindowEvent);
fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
spawner: &Spawner,
Expand Down Expand Up @@ -280,7 +283,7 @@ fn start<E: Example>(
}
};

example.render(&frame.output, &device, &queue, &spawner);
example.render(&frame.output.view, &device, &queue, &spawner);
}
_ => {}
}
Expand Down Expand Up @@ -361,6 +364,108 @@ pub fn run<E: Example>(title: &str) {
});
}

#[cfg(test)]
pub fn test<E: Example>(image_path: &str, width: u32, height: u32, tollerance: u8, max_outliers: usize) {
use std::num::NonZeroU32;

assert_eq!(width % 64, 0, "width needs to be aligned 64");

let _optional = E::optional_features();
let features = E::required_features();
let mut limits = E::required_limits();
if limits == wgpu::Limits::default() {
limits = test_common::lowest_reasonable_limits();
}

test_common::initialize_test(
test_common::TestParameters::default()
.features(features)
.limits(limits),
|ctx| {
let spawner = Spawner::new();

let dst_texture = ctx.device.create_texture(&wgpu::TextureDescriptor {
label: Some("destination"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsage::RENDER_ATTACHMENT | wgpu::TextureUsage::COPY_SRC,
});

let dst_view = dst_texture.create_view(&wgpu::TextureViewDescriptor::default());

let dst_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("image map buffer"),
size: width as u64 * height as u64 * 4,
usage: wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::MAP_READ,
mapped_at_creation: false,
});

let mut example = E::init(
&wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
width,
height,
present_mode: wgpu::PresentMode::Fifo,
},
&ctx.adapter,
&ctx.device,
&ctx.queue,
);

example.render(&dst_view, &ctx.device, &ctx.queue, &spawner);

let mut cmd_buf = ctx
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());

cmd_buf.copy_texture_to_buffer(
wgpu::ImageCopyTexture {
texture: &dst_texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
},
wgpu::ImageCopyBuffer {
buffer: &dst_buffer,
layout: wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: NonZeroU32::new(width * 4),
rows_per_image: None,
},
},
wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);

ctx.queue.submit(Some(cmd_buf.finish()));

let dst_buffer_slice = dst_buffer.slice(..);
let _ = dst_buffer_slice.map_async(wgpu::MapMode::Read);
ctx.device.poll(wgpu::Maintain::Wait);
let bytes = dst_buffer_slice.get_mapped_range().to_vec();

test_common::image::compare_image_output(
&image_path,
width,
height,
&bytes,
tollerance,
max_outliers,
);
},
);
}

// This allows treating the framework as a standalone example,
// thus avoiding listing the example names in `Cargo.toml`.
#[allow(dead_code)]
Expand Down
18 changes: 16 additions & 2 deletions wgpu/examples/mipmap/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ impl framework::Example for Example {

fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
_spawner: &framework::Spawner,
Expand All @@ -453,7 +453,7 @@ impl framework::Example for Example {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(clear_color),
Expand All @@ -474,3 +474,17 @@ impl framework::Example for Example {
fn main() {
framework::run::<Example>("mipmap");
}

#[test]
fn mipmap() {
framework::test::<Example>(
concat!(
env!("CARGO_MANIFEST_DIR"),
"/examples/mipmap/screenshot.png"
),
1024,
768,
10, // Mipmap sampling is highly variant between impls.
10,
);
}
Binary file modified wgpu/examples/mipmap/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions wgpu/examples/msaa-line/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl framework::Example for Example {

fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
_spawner: &framework::Spawner,
Expand Down Expand Up @@ -254,14 +254,14 @@ impl framework::Example for Example {
};
let rpass_color_attachment = if self.sample_count == 1 {
wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops,
}
} else {
wgpu::RenderPassColorAttachment {
view: &self.multisampled_framebuffer,
resolve_target: Some(&frame.view),
resolve_target: Some(&view),
ops,
}
};
Expand Down
4 changes: 2 additions & 2 deletions wgpu/examples/shadow/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ impl framework::Example for Example {

fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
_spawner: &framework::Spawner,
Expand Down Expand Up @@ -781,7 +781,7 @@ impl framework::Example for Example {
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
Expand Down
4 changes: 2 additions & 2 deletions wgpu/examples/skybox/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl framework::Example for Skybox {

fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
spawner: &framework::Spawner,
Expand All @@ -415,7 +415,7 @@ impl framework::Example for Skybox {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
Expand Down
4 changes: 2 additions & 2 deletions wgpu/examples/texture-arrays/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl framework::Example for Example {
}
fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
_spawner: &framework::Spawner,
Expand All @@ -290,7 +290,7 @@ impl framework::Example for Example {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
Expand Down
6 changes: 3 additions & 3 deletions wgpu/examples/water/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ impl framework::Example for Example {
#[allow(clippy::eq_op)]
fn render(
&mut self,
frame: &wgpu::SwapChainTexture,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
_spawner: &framework::Spawner,
Expand Down Expand Up @@ -734,7 +734,7 @@ impl framework::Example for Example {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(back_color),
Expand All @@ -761,7 +761,7 @@ impl framework::Example for Example {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.view,
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
Expand Down
Loading

0 comments on commit ebbbf22

Please sign in to comment.