bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Frame capture testing tool

Open DGriffin91 opened this issue 2 years ago • 3 comments

Objective

  • Provide a way to test the rendered output images from various scenes.
  • Implements #4572 as a testing tool. This should allow us to start using this functionality for testing without needing to settle on the ideal API for inclusion in the engine first.

Solution

  • A plugin for copying images from vram back to an Image
  • A plugin for testing scenes.
  • An example test scene.

There would be a folder of test images,

Currently main.rs creates a new bevy app and tests with an example scene. The .run() returns after the test so there could be a set of scenes tested here (or elsewhere if appropriate)

DGriffin91 avatar Aug 02 '22 05:08 DGriffin91

Cool! I'll give this a review another time, but just wanted to note an initial thought: it would probably be good to integrate into scene_viewer so we can load scenes and take screenshots of those, then compare against references or something for regressions testing. Maybe we'd need to add some kind of configuration file for scene_viewer to be able to set up some known configurations (camera, lighting, etc) for tests.

superdump avatar Aug 02 '22 07:08 superdump

Very cool! This is a great resource. I used some of this code to build a plugin for exporting image sequences out of Bevy: https://github.com/paulkre/bevy_image_export

Unfortunately, it doesn't work with the 2D pipeline yet (2D shapes are invisible in the output image). Using a RenderTarget::Image for a 2D camera works fine though. It seems like the copy_texture_to_buffer method is being called too early in the pipeline. I can't figure out where to insert the custom "image copier" node in the render graph, so that it runs after 2D shapes have been rendered.

paulkre avatar Sep 07 '22 11:09 paulkre

Apparently, a 2D camera does not work properly if it is parented to another 2D camera. I could not find the source of the problem though.

paulkre avatar Sep 10 '22 21:09 paulkre

I have been trying this out, and I think that as it is written it only works correctly for images which have a width of a multiple of 256.

It seems that two different padded_bytes_per_row are being used:

https://github.com/bevyengine/bevy/blob/0aa4a49863421d3f102246924de10ac515c77113/tools/frame_capture/src/image_copy.rs#L87-L88

https://github.com/bevyengine/bevy/blob/0aa4a49863421d3f102246924de10ac515c77113/tools/frame_capture/src/image_copy.rs#L150-L153

Changing the first one to:

 let padded_bytes_per_row =
            RenderDevice::align_copy_bytes_per_row((size.width) as usize * 4);

produces the same padded_bytes_per_row in both situations and produces the correct image regardless of width.

AlanRace avatar Nov 19 '22 10:11 AlanRace

@AlanRace Good catch! I need to figure out how to make it not depend on there being 4 color channels though. Will probably reference https://github.com/gfx-rs/wgpu/blob/c434b94ca20e9ccab8b0afdd7406c595dd37e7c3/wgpu/src/util/device.rs#L112

DGriffin91 avatar Nov 19 '22 11:11 DGriffin91

@DGriffin91 Hello, I want to use your tools, but I got this error. Is there something to need before cargo run?

thread 'Compute Task Pool (1)' panicked at 'called `Result::unwrap()` on an `Err` value: IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" })', tools/frame_capture/src/scene_tester.rs:177:22
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'task has failed', /Users/gim-yujin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/async-task-4.4.0/src/task.rs:452:45

Youdiie avatar Aug 02 '23 09:08 Youdiie

@Youdiie I'm guessing that's because this test_images dir doesn't exist. https://github.com/bevyengine/bevy/pull/5550/files#diff-630fc550358d7e46d3891918b8dd2edb1a225c06337f82b07ce657ac9afadba8R165

Not sure if you are aware of the new screenshot feature that landed in bevy 0.11, if you can do what you need with it, it would be probably easier than trying to use this code: https://bevyengine.org/news/bevy-0-11/#screenshot-api

DGriffin91 avatar Aug 02 '23 17:08 DGriffin91

@DGriffin91 Thanks for replying! I wanted to save high quality frame capture and couldn't resize with screenshot feature in bevy 0.11. Thanks for @paulkre, I could resize frame capture with bevy_image_export.

Youdiie avatar Aug 03 '23 04:08 Youdiie