Skip to content

Commit 85001b2

Browse files
authored
Use new struct AnimationTimer instead of Instant to prevent random test failures. (#7685)
1 parent 4c66478 commit 85001b2

File tree

12 files changed

+56
-36
lines changed

12 files changed

+56
-36
lines changed

examples/features/src/ray_cube_compute/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task, time::Instant};
1+
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task};
22

33
use bytemuck::{Pod, Zeroable};
44
use glam::{Affine3A, Mat4, Quat, Vec3};
55
use wgpu::util::DeviceExt;
66

77
use wgpu::StoreOp;
88

9+
use crate::utils;
10+
911
// from cube
1012
#[repr(C)]
1113
#[derive(Clone, Copy, Pod, Zeroable)]
@@ -135,7 +137,7 @@ struct Example {
135137
compute_bind_group: wgpu::BindGroup,
136138
blit_pipeline: wgpu::RenderPipeline,
137139
blit_bind_group: wgpu::BindGroup,
138-
start_inst: Instant,
140+
animation_timer: utils::AnimationTimer,
139141
}
140142

141143
impl crate::framework::Example for Example {
@@ -392,8 +394,6 @@ impl crate::framework::Example for Example {
392394

393395
queue.submit(Some(encoder.finish()));
394396

395-
let start_inst = Instant::now();
396-
397397
Example {
398398
rt_target,
399399
rt_view,
@@ -406,7 +406,7 @@ impl crate::framework::Example for Example {
406406
compute_bind_group,
407407
blit_pipeline,
408408
blit_bind_group,
409-
start_inst,
409+
animation_timer: utils::AnimationTimer::default(),
410410
}
411411
}
412412

@@ -425,7 +425,7 @@ impl crate::framework::Example for Example {
425425
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
426426
device.push_error_scope(wgpu::ErrorFilter::Validation);
427427

428-
let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;
428+
let anim_time = self.animation_timer.time();
429429

430430
self.tlas_package[0].as_mut().unwrap().transform =
431431
affine_to_rows(&Affine3A::from_rotation_translation(
Loading

examples/features/src/ray_cube_fragment/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use crate::utils;
12
use bytemuck::{Pod, Zeroable};
23
use glam::{Mat4, Quat, Vec3};
34
use std::ops::IndexMut;
4-
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task, time::Instant};
5+
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task};
56
use wgpu::util::DeviceExt;
67

78
// from cube
@@ -101,7 +102,7 @@ struct Example {
101102
tlas_package: wgpu::TlasPackage,
102103
pipeline: wgpu::RenderPipeline,
103104
bind_group: wgpu::BindGroup,
104-
start_inst: Instant,
105+
animation_timer: utils::AnimationTimer,
105106
}
106107

107108
impl crate::framework::Example for Example {
@@ -264,16 +265,14 @@ impl crate::framework::Example for Example {
264265

265266
queue.submit(Some(encoder.finish()));
266267

267-
let start_inst = Instant::now();
268-
269268
Example {
270269
uniforms,
271270
uniform_buf,
272271
blas,
273272
tlas_package,
274273
pipeline,
275274
bind_group,
276-
start_inst,
275+
animation_timer: utils::AnimationTimer::default(),
277276
}
278277
}
279278

@@ -306,7 +305,7 @@ impl crate::framework::Example for Example {
306305

307306
let side_count = 8;
308307

309-
let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;
308+
let anim_time = self.animation_timer.time();
310309

311310
for x in 0..side_count {
312311
for y in 0..side_count {
Loading

examples/features/src/ray_cube_normals/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task, time::Instant};
1+
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task};
22

33
use bytemuck::{Pod, Zeroable};
44
use glam::{Affine3A, Mat4, Quat, Vec3};
55
use wgpu::util::DeviceExt;
66

7+
use crate::utils;
78
use wgpu::StoreOp;
89

910
// from cube
@@ -125,7 +126,7 @@ struct Example {
125126
compute_bind_group: wgpu::BindGroup,
126127
blit_pipeline: wgpu::RenderPipeline,
127128
blit_bind_group: wgpu::BindGroup,
128-
start_inst: Instant,
129+
animation_timer: utils::AnimationTimer,
129130
}
130131

131132
impl crate::framework::Example for Example {
@@ -385,16 +386,14 @@ impl crate::framework::Example for Example {
385386

386387
queue.submit(Some(encoder.finish()));
387388

388-
let start_inst = Instant::now();
389-
390389
Example {
391390
rt_target,
392391
tlas_package,
393392
compute_pipeline,
394393
compute_bind_group,
395394
blit_pipeline,
396395
blit_bind_group,
397-
start_inst,
396+
animation_timer: utils::AnimationTimer::default(),
398397
}
399398
}
400399

@@ -413,7 +412,7 @@ impl crate::framework::Example for Example {
413412
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
414413
device.push_error_scope(wgpu::ErrorFilter::Validation);
415414

416-
let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;
415+
let anim_time = self.animation_timer.time();
417416

418417
self.tlas_package[0].as_mut().unwrap().transform =
419418
affine_to_rows(&Affine3A::from_rotation_translation(

examples/features/src/ray_scene/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use crate::utils;
12
use bytemuck::{Pod, Zeroable};
23
use glam::{Mat4, Quat, Vec3};
34
use std::f32::consts::PI;
45
use std::ops::IndexMut;
5-
use std::{borrow::Cow, future::Future, iter, mem, ops::Range, pin::Pin, task, time::Instant};
6+
use std::{borrow::Cow, future::Future, iter, mem, ops::Range, pin::Pin, task};
67
use wgpu::util::DeviceExt;
78

89
// from cube
@@ -310,8 +311,8 @@ struct Example {
310311
tlas_package: wgpu::TlasPackage,
311312
pipeline: wgpu::RenderPipeline,
312313
bind_group: wgpu::BindGroup,
313-
start_inst: Instant,
314314
scene_components: SceneComponents,
315+
animation_timer: utils::AnimationTimer,
315316
}
316317

317318
impl crate::framework::Example for Example {
@@ -434,16 +435,14 @@ impl crate::framework::Example for Example {
434435
],
435436
});
436437

437-
let start_inst = Instant::now();
438-
439438
Example {
440439
uniforms,
441440
uniform_buf,
442441
tlas_package,
443442
pipeline,
444443
bind_group,
445-
start_inst,
446444
scene_components,
445+
animation_timer: utils::AnimationTimer::default(),
447446
}
448447
}
449448

@@ -476,7 +475,7 @@ impl crate::framework::Example for Example {
476475

477476
let side_count = 2;
478477

479-
let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;
478+
let anim_time = self.animation_timer.time();
480479

481480
for x in 0..side_count {
482481
for y in 0..side_count {
-878 Bytes
Loading

examples/features/src/ray_shadows/mod.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task, time::Instant};
1+
use std::{borrow::Cow, future::Future, iter, mem, pin::Pin, task};
22

33
use bytemuck::{Pod, Zeroable};
44
use glam::{Mat4, Vec3};
55
use wgpu::util::DeviceExt;
66
use wgpu::{vertex_attr_array, IndexFormat, VertexBufferLayout};
77

8+
use crate::utils;
9+
810
// from cube
911
#[repr(C)]
1012
#[derive(Clone, Copy, Pod, Zeroable)]
@@ -79,7 +81,7 @@ struct Example {
7981
index_buf: wgpu::Buffer,
8082
pipeline: wgpu::RenderPipeline,
8183
bind_group: wgpu::BindGroup,
82-
start_inst: Instant,
84+
animation_timer: utils::AnimationTimer,
8385
}
8486

8587
const CAM_LOOK_AT: Vec3 = Vec3::new(0.0, 1.0, -1.5);
@@ -290,7 +292,7 @@ impl crate::framework::Example for Example {
290292
],
291293
});
292294

293-
let start_inst = Instant::now();
295+
let animation_timer = utils::AnimationTimer::default();
294296

295297
Example {
296298
uniforms,
@@ -299,7 +301,7 @@ impl crate::framework::Example for Example {
299301
index_buf,
300302
pipeline,
301303
bind_group,
302-
start_inst,
304+
animation_timer,
303305
}
304306
}
305307

@@ -322,10 +324,9 @@ impl crate::framework::Example for Example {
322324
const LIGHT_DISTANCE: f32 = 5.0;
323325
const TIME_SCALE: f32 = -0.2;
324326
const INITIAL_TIME: f32 = 1.0;
325-
let cos = (self.start_inst.elapsed().as_secs_f32() * TIME_SCALE + INITIAL_TIME).cos()
326-
* LIGHT_DISTANCE;
327-
let sin = (self.start_inst.elapsed().as_secs_f32() * TIME_SCALE + INITIAL_TIME).sin()
328-
* LIGHT_DISTANCE;
327+
let time = self.animation_timer.time();
328+
let cos = (time * TIME_SCALE + INITIAL_TIME).cos() * LIGHT_DISTANCE;
329+
let sin = (time * TIME_SCALE + INITIAL_TIME).sin() * LIGHT_DISTANCE;
329330

330331
let mut encoder =
331332
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
94 Bytes
Loading

examples/features/src/ray_traced_triangle/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use glam::{Mat4, Vec3};
22
use std::mem;
3-
use std::time::Instant;
43
use wgpu::util::{BufferInitDescriptor, DeviceExt};
54
use wgpu::{include_wgsl, BufferUsages, IndexFormat, SamplerDescriptor};
65
use wgpu::{
@@ -9,14 +8,16 @@ use wgpu::{
98
CreateBlasDescriptor, CreateTlasDescriptor, TlasInstance, TlasPackage,
109
};
1110

11+
use crate::utils;
12+
1213
struct Example {
1314
tlas_package: TlasPackage,
1415
compute_pipeline: wgpu::ComputePipeline,
1516
blit_pipeline: wgpu::RenderPipeline,
1617
bind_group: wgpu::BindGroup,
1718
blit_bind_group: wgpu::BindGroup,
1819
storage_texture: wgpu::Texture,
19-
start: Instant,
20+
animation_timer: utils::AnimationTimer,
2021
}
2122

2223
#[repr(C)]
@@ -360,7 +361,7 @@ impl crate::framework::Example for Example {
360361
bind_group,
361362
blit_bind_group,
362363
storage_texture: storage_tex,
363-
start: Instant::now(),
364+
animation_timer: utils::AnimationTimer::default(),
364365
}
365366
}
366367

@@ -376,7 +377,7 @@ impl crate::framework::Example for Example {
376377

377378
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
378379
self.tlas_package[0].as_mut().unwrap().transform =
379-
Mat4::from_rotation_y(self.start.elapsed().as_secs_f32())
380+
Mat4::from_rotation_y(self.animation_timer.time())
380381
.transpose()
381382
.to_cols_array()[..12]
382383
.try_into()

0 commit comments

Comments
 (0)