From 20039b93472c173b826a353e7694285429e04c7e Mon Sep 17 00:00:00 2001 From: chyyran Date: Tue, 24 Sep 2024 17:58:16 -0400 Subject: [PATCH] test: abstract test framework --- librashader-test/src/render/d3d11.rs | 7 +++ librashader-test/src/render/gl/mod.rs | 14 ++++++ librashader-test/src/render/mod.rs | 66 +++++++++++---------------- librashader-test/src/render/vk/mod.rs | 7 +++ librashader-test/src/render/wgpu.rs | 7 +++ 5 files changed, 62 insertions(+), 39 deletions(-) diff --git a/librashader-test/src/render/d3d11.rs b/librashader-test/src/render/d3d11.rs index bf2ab75..74edd9f 100644 --- a/librashader-test/src/render/d3d11.rs +++ b/librashader-test/src/render/d3d11.rs @@ -6,6 +6,13 @@ use librashader::runtime::{Size, Viewport}; use std::path::Path; impl RenderTest for Direct3D11 { + fn new(path: impl AsRef) -> anyhow::Result + where + Self: Sized, + { + Direct3D11::new(path) + } + fn render(&self, path: impl AsRef, frame_count: usize) -> anyhow::Result { let (renderbuffer, rtv) = self.create_renderbuffer(self.image_bytes.size)?; diff --git a/librashader-test/src/render/gl/mod.rs b/librashader-test/src/render/gl/mod.rs index 11f9ba1..d189cab 100644 --- a/librashader-test/src/render/gl/mod.rs +++ b/librashader-test/src/render/gl/mod.rs @@ -22,6 +22,13 @@ pub struct OpenGl3(OpenGl); pub struct OpenGl4(OpenGl); impl RenderTest for OpenGl3 { + fn new(path: impl AsRef) -> anyhow::Result + where + Self: Sized, + { + OpenGl3::new(path) + } + fn render(&self, path: impl AsRef, frame_count: usize) -> anyhow::Result { let mut filter_chain = unsafe { FilterChain::load_from_path( @@ -41,6 +48,13 @@ impl RenderTest for OpenGl3 { } impl RenderTest for OpenGl4 { + fn new(path: impl AsRef) -> anyhow::Result + where + Self: Sized, + { + OpenGl4::new(path) + } + fn render(&self, path: impl AsRef, frame_count: usize) -> anyhow::Result { let mut filter_chain = unsafe { FilterChain::load_from_path( diff --git a/librashader-test/src/render/mod.rs b/librashader-test/src/render/mod.rs index be3c825..f8d65ad 100644 --- a/librashader-test/src/render/mod.rs +++ b/librashader-test/src/render/mod.rs @@ -7,6 +7,11 @@ use std::path::Path; /// Test harness to set up a device, render a triangle, and apply a shader pub trait RenderTest { + /// Create a new instance of the test harness. + fn new(path: impl AsRef) -> anyhow::Result + where + Self: Sized; + /// Render a shader onto an image buffer, applying the provided shader. /// /// The test should render in linear colour space for proper comparison against @@ -24,6 +29,10 @@ pub trait RenderTest { #[cfg(test)] mod test { + use crate::render::d3d11::Direct3D11; + use crate::render::gl::{OpenGl3, OpenGl4}; + use crate::render::vk::Vulkan; + use crate::render::wgpu::Wgpu; use crate::render::RenderTest; use image::codecs::png::PngEncoder; use std::fs::File; @@ -34,70 +43,49 @@ mod test { // const FILTER_PATH: &str = // "../test/shaders_slang/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp"; - #[test] - pub fn test_d3d11() -> anyhow::Result<()> { - let d3d11 = super::d3d11::Direct3D11::new(IMAGE_PATH)?; - let image = d3d11.render(FILTER_PATH, 100)?; + fn do_test() -> anyhow::Result<()> { + let test = T::new(IMAGE_PATH)?; + let image = test.render(FILTER_PATH, 100)?; let out = File::create("out.png")?; image.write_with_encoder(PngEncoder::new(out))?; Ok(()) } + #[test] + pub fn test_d3d11() -> anyhow::Result<()> { + do_test::() + } + #[test] pub fn test_wgpu() -> anyhow::Result<()> { - let wgpu = super::wgpu::Wgpu::new(IMAGE_PATH)?; - let image = wgpu.render(FILTER_PATH, 100)?; - - let out = File::create("out.png")?; - image.write_with_encoder(PngEncoder::new(out))?; - Ok(()) + do_test::() } #[test] pub fn test_vk() -> anyhow::Result<()> { - let vulkan = super::vk::Vulkan::new(IMAGE_PATH)?; - let image = vulkan.render(FILTER_PATH, 100)?; - // - let out = File::create("out.png")?; - image.write_with_encoder(PngEncoder::new(out))?; - Ok(()) + do_test::() } #[test] pub fn test_gl3() -> anyhow::Result<()> { - - let gl = super::gl::OpenGl3::new(IMAGE_PATH)?; - let image = gl.render(FILTER_PATH, 1000)?; - - let out = File::create("out.png")?; - image.write_with_encoder(PngEncoder::new(out))?; - Ok(()) + do_test::() } #[test] pub fn test_gl4() -> anyhow::Result<()> { - - let gl = super::gl::OpenGl4::new(IMAGE_PATH)?; - let image = gl.render(FILTER_PATH, 1000)?; - - let out = File::create("out.png")?; - image.write_with_encoder(PngEncoder::new(out))?; - Ok(()) + do_test::() } - #[test] - pub fn compare() -> anyhow::Result<()> { - let d3d11 = super::d3d11::Direct3D11::new(IMAGE_PATH)?; - let wgpu = super::wgpu::Wgpu::new(IMAGE_PATH)?; + pub fn compare() -> anyhow::Result<()> { + let a = A::new(IMAGE_PATH)?; + let b = B::new(IMAGE_PATH)?; - let wgpu_image = wgpu.render(FILTER_PATH, 100)?; - let d3d11_image = d3d11.render(FILTER_PATH, 100)?; - - let similarity = image_compare::rgba_hybrid_compare(&wgpu_image, &d3d11_image)?; + let a_image = a.render(FILTER_PATH, 100)?; + let b_image = b.render(FILTER_PATH, 100)?; + let similarity = image_compare::rgba_hybrid_compare(&a_image, &b_image)?; assert!(similarity.score > 0.95); - Ok(()) } } diff --git a/librashader-test/src/render/vk/mod.rs b/librashader-test/src/render/vk/mod.rs index 80220aa..331d121 100644 --- a/librashader-test/src/render/vk/mod.rs +++ b/librashader-test/src/render/vk/mod.rs @@ -25,6 +25,13 @@ pub struct Vulkan { } impl RenderTest for Vulkan { + fn new(path: impl AsRef) -> anyhow::Result + where + Self: Sized, + { + Vulkan::new(path) + } + fn render(&self, path: impl AsRef, frame_count: usize) -> anyhow::Result { unsafe { let mut filter_chain = FilterChain::load_from_path( diff --git a/librashader-test/src/render/wgpu.rs b/librashader-test/src/render/wgpu.rs index 61ece45..7ad65d9 100644 --- a/librashader-test/src/render/wgpu.rs +++ b/librashader-test/src/render/wgpu.rs @@ -49,6 +49,13 @@ impl BufferDimensions { } impl RenderTest for Wgpu { + fn new(path: impl AsRef) -> anyhow::Result + where + Self: Sized, + { + Wgpu::new(path) + } + fn render(&self, path: impl AsRef, frame_count: usize) -> anyhow::Result { let mut chain = FilterChain::load_from_path( path,