Use new struct AnimationTimer instead of Instant to prevent random test failures. (#7685)

This commit is contained in:
Vecvec 2025-05-14 14:52:49 +12:00 committed by GitHub
parent 4c66478a94
commit 85001b2436
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 56 additions and 36 deletions

View File

@ -1,4 +1,4 @@
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task, time::Instant};
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task};
use bytemuck::{Pod, Zeroable};
use glam::{Affine3A, Mat4, Quat, Vec3};
@ -6,6 +6,8 @@ use wgpu::util::DeviceExt;
use wgpu::StoreOp;
use crate::utils;
// from cube
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
@ -135,7 +137,7 @@ struct Example {
compute_bind_group: wgpu::BindGroup,
blit_pipeline: wgpu::RenderPipeline,
blit_bind_group: wgpu::BindGroup,
start_inst: Instant,
animation_timer: utils::AnimationTimer,
}
impl crate::framework::Example for Example {
@ -392,8 +394,6 @@ impl crate::framework::Example for Example {
queue.submit(Some(encoder.finish()));
let start_inst = Instant::now();
Example {
rt_target,
rt_view,
@ -406,7 +406,7 @@ impl crate::framework::Example for Example {
compute_bind_group,
blit_pipeline,
blit_bind_group,
start_inst,
animation_timer: utils::AnimationTimer::default(),
}
}
@ -425,7 +425,7 @@ impl crate::framework::Example for Example {
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
device.push_error_scope(wgpu::ErrorFilter::Validation);
let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;
let anim_time = self.animation_timer.time();
self.tlas_package[0].as_mut().unwrap().transform =
affine_to_rows(&Affine3A::from_rotation_translation(

Binary file not shown.

Before

Width:  |  Height:  |  Size: 479 KiB

After

Width:  |  Height:  |  Size: 310 KiB

View File

@ -1,7 +1,8 @@
use crate::utils;
use bytemuck::{Pod, Zeroable};
use glam::{Mat4, Quat, Vec3};
use std::ops::IndexMut;
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task, time::Instant};
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task};
use wgpu::util::DeviceExt;
// from cube
@ -101,7 +102,7 @@ struct Example {
tlas_package: wgpu::TlasPackage,
pipeline: wgpu::RenderPipeline,
bind_group: wgpu::BindGroup,
start_inst: Instant,
animation_timer: utils::AnimationTimer,
}
impl crate::framework::Example for Example {
@ -264,8 +265,6 @@ impl crate::framework::Example for Example {
queue.submit(Some(encoder.finish()));
let start_inst = Instant::now();
Example {
uniforms,
uniform_buf,
@ -273,7 +272,7 @@ impl crate::framework::Example for Example {
tlas_package,
pipeline,
bind_group,
start_inst,
animation_timer: utils::AnimationTimer::default(),
}
}
@ -306,7 +305,7 @@ impl crate::framework::Example for Example {
let side_count = 8;
let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;
let anim_time = self.animation_timer.time();
for x in 0..side_count {
for y in 0..side_count {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 584 KiB

After

Width:  |  Height:  |  Size: 336 KiB

View File

@ -1,9 +1,10 @@
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task, time::Instant};
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task};
use bytemuck::{Pod, Zeroable};
use glam::{Affine3A, Mat4, Quat, Vec3};
use wgpu::util::DeviceExt;
use crate::utils;
use wgpu::StoreOp;
// from cube
@ -125,7 +126,7 @@ struct Example {
compute_bind_group: wgpu::BindGroup,
blit_pipeline: wgpu::RenderPipeline,
blit_bind_group: wgpu::BindGroup,
start_inst: Instant,
animation_timer: utils::AnimationTimer,
}
impl crate::framework::Example for Example {
@ -385,8 +386,6 @@ impl crate::framework::Example for Example {
queue.submit(Some(encoder.finish()));
let start_inst = Instant::now();
Example {
rt_target,
tlas_package,
@ -394,7 +393,7 @@ impl crate::framework::Example for Example {
compute_bind_group,
blit_pipeline,
blit_bind_group,
start_inst,
animation_timer: utils::AnimationTimer::default(),
}
}
@ -413,7 +412,7 @@ impl crate::framework::Example for Example {
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
device.push_error_scope(wgpu::ErrorFilter::Validation);
let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;
let anim_time = self.animation_timer.time();
self.tlas_package[0].as_mut().unwrap().transform =
affine_to_rows(&Affine3A::from_rotation_translation(

View File

@ -1,8 +1,9 @@
use crate::utils;
use bytemuck::{Pod, Zeroable};
use glam::{Mat4, Quat, Vec3};
use std::f32::consts::PI;
use std::ops::IndexMut;
use std::{borrow::Cow, future::Future, iter, mem, ops::Range, pin::Pin, task, time::Instant};
use std::{borrow::Cow, future::Future, iter, mem, ops::Range, pin::Pin, task};
use wgpu::util::DeviceExt;
// from cube
@ -310,8 +311,8 @@ struct Example {
tlas_package: wgpu::TlasPackage,
pipeline: wgpu::RenderPipeline,
bind_group: wgpu::BindGroup,
start_inst: Instant,
scene_components: SceneComponents,
animation_timer: utils::AnimationTimer,
}
impl crate::framework::Example for Example {
@ -434,16 +435,14 @@ impl crate::framework::Example for Example {
],
});
let start_inst = Instant::now();
Example {
uniforms,
uniform_buf,
tlas_package,
pipeline,
bind_group,
start_inst,
scene_components,
animation_timer: utils::AnimationTimer::default(),
}
}
@ -476,7 +475,7 @@ impl crate::framework::Example for Example {
let side_count = 2;
let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;
let anim_time = self.animation_timer.time();
for x in 0..side_count {
for y in 0..side_count {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 77 KiB

View File

@ -1,10 +1,12 @@
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task, time::Instant};
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task};
use bytemuck::{Pod, Zeroable};
use glam::{Mat4, Vec3};
use wgpu::util::DeviceExt;
use wgpu::{vertex_attr_array, IndexFormat, VertexBufferLayout};
use crate::utils;
// from cube
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
@ -79,7 +81,7 @@ struct Example {
index_buf: wgpu::Buffer,
pipeline: wgpu::RenderPipeline,
bind_group: wgpu::BindGroup,
start_inst: Instant,
animation_timer: utils::AnimationTimer,
}
const CAM_LOOK_AT: Vec3 = Vec3::new(0.0, 1.0, -1.5);
@ -290,7 +292,7 @@ impl crate::framework::Example for Example {
],
});
let start_inst = Instant::now();
let animation_timer = utils::AnimationTimer::default();
Example {
uniforms,
@ -299,7 +301,7 @@ impl crate::framework::Example for Example {
index_buf,
pipeline,
bind_group,
start_inst,
animation_timer,
}
}
@ -322,10 +324,9 @@ impl crate::framework::Example for Example {
const LIGHT_DISTANCE: f32 = 5.0;
const TIME_SCALE: f32 = -0.2;
const INITIAL_TIME: f32 = 1.0;
let cos = (self.start_inst.elapsed().as_secs_f32() * TIME_SCALE + INITIAL_TIME).cos()
* LIGHT_DISTANCE;
let sin = (self.start_inst.elapsed().as_secs_f32() * TIME_SCALE + INITIAL_TIME).sin()
* LIGHT_DISTANCE;
let time = self.animation_timer.time();
let cos = (time * TIME_SCALE + INITIAL_TIME).cos() * LIGHT_DISTANCE;
let sin = (time * TIME_SCALE + INITIAL_TIME).sin() * LIGHT_DISTANCE;
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -1,6 +1,5 @@
use glam::{Mat4, Vec3};
use std::mem;
use std::time::Instant;
use wgpu::util::{BufferInitDescriptor, DeviceExt};
use wgpu::{include_wgsl, BufferUsages, IndexFormat, SamplerDescriptor};
use wgpu::{
@ -9,6 +8,8 @@ use wgpu::{
CreateBlasDescriptor, CreateTlasDescriptor, TlasInstance, TlasPackage,
};
use crate::utils;
struct Example {
tlas_package: TlasPackage,
compute_pipeline: wgpu::ComputePipeline,
@ -16,7 +17,7 @@ struct Example {
bind_group: wgpu::BindGroup,
blit_bind_group: wgpu::BindGroup,
storage_texture: wgpu::Texture,
start: Instant,
animation_timer: utils::AnimationTimer,
}
#[repr(C)]
@ -360,7 +361,7 @@ impl crate::framework::Example for Example {
bind_group,
blit_bind_group,
storage_texture: storage_tex,
start: Instant::now(),
animation_timer: utils::AnimationTimer::default(),
}
}
@ -376,7 +377,7 @@ impl crate::framework::Example for Example {
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
self.tlas_package[0].as_mut().unwrap().transform =
Mat4::from_rotation_y(self.start.elapsed().as_secs_f32())
Mat4::from_rotation_y(self.animation_timer.time())
.transpose()
.to_cols_array()[..12]
.try_into()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 116 KiB

After

Width:  |  Height:  |  Size: 116 KiB

View File

@ -1,5 +1,6 @@
#[cfg(not(target_arch = "wasm32"))]
use std::io::Write;
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
@ -264,3 +265,23 @@ pub(crate) async fn get_adapter_with_capabilities_or_from_env(
);
adapter
}
/// A custom timer that only starts counting after the first call to get its time value.
/// Useful because some examples have animations that would otherwise get started at initialization
/// leading to random CI fails.
#[derive(Default)]
pub struct AnimationTimer {
start_time: Option<Instant>,
}
impl AnimationTimer {
pub fn time(&mut self) -> f32 {
match self.start_time {
None => {
self.start_time = Some(Instant::now());
0.0
}
Some(ref instant) => instant.elapsed().as_secs_f32(),
}
}
}