cranpose-render-wgpu 0.22.4

GPU renderer for Cranpose scenes through wgpu
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::sync::{Arc, atomic::Ordering, mpsc};

use web_time::{Duration, Instant};

use super::{CompileLane, MOVING_FRAMES_PER_BUILD, PacedLanding, PipelineCompiler};
use crate::present_runtime::PresentStatus;

/// Both lanes run every job off the calling thread. Each starts its jobs in
/// queue order, on several threads, so they may finish in any.
#[test]
fn each_lane_runs_its_jobs_off_the_calling_thread() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    assert!(compiler.is_active());
    let caller = std::thread::current().id();
    for lane in [CompileLane::Demanded, CompileLane::WarmUp] {
        let (finished, observed) = mpsc::channel();
        for index in 0..3 {
            let finished = finished.clone();
            compiler.enqueue(lane, move || {
                finished.send((index, std::thread::current().id())).unwrap();
            });
        }
        let mut indices: Vec<_> = (0..3)
            .map(|_| {
                let (index, thread) = observed.recv_timeout(Duration::from_secs(5)).unwrap();
                assert_ne!(thread, caller);
                index
            })
            .collect();
        indices.sort_unstable();
        assert_eq!(indices, [0, 1, 2]);
    }
}

#[test]
fn a_demanded_job_runs_while_a_warm_up_is_still_compiling() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    let (started, warm_up_started) = mpsc::channel();
    let (release, blocked) = mpsc::channel::<()>();
    compiler.enqueue(CompileLane::WarmUp, move || {
        started.send(()).unwrap();
        blocked.recv().unwrap();
    });
    warm_up_started
        .recv_timeout(Duration::from_secs(5))
        .unwrap();
    let (ran, demanded_ran) = mpsc::channel();
    compiler.enqueue(CompileLane::Demanded, move || ran.send(()).unwrap());
    assert_eq!(
        demanded_ran.recv_timeout(Duration::from_secs(5)),
        Ok(()),
        "a demanded job must not wait behind a warm-up"
    );
    release.send(()).unwrap();
}

/// Holds every warm-up thread of `compiler` until the returned senders
/// drop, so nothing queued for them lands meanwhile.
pub(crate) fn hold_warm_ups(compiler: &PipelineCompiler) -> Vec<mpsc::Sender<()>> {
    hold(compiler, CompileLane::WarmUp, compiler.warm_up_threads())
}

/// Holds `count` threads of `compiler` inside jobs on `lane` until the
/// returned senders drop.
fn hold(compiler: &PipelineCompiler, lane: CompileLane, count: usize) -> Vec<mpsc::Sender<()>> {
    let (started, holding) = mpsc::channel();
    let releases = (0..count)
        .map(|_| {
            let (release, held) = mpsc::channel::<()>();
            let started = started.clone();
            compiler.enqueue(lane, move || {
                started
                    .send(())
                    .expect("the compiler test channel stays open");
                let _ = held.recv();
            });
            release
        })
        .collect();
    for _ in 0..count {
        holding
            .recv_timeout(Duration::from_secs(5))
            .expect("the compiler test channel stays open");
    }
    releases
}

/// A screen of new materials demands many pipelines at once; once frames
/// stop building their own, the warm-up threads take them while the
/// demanded lane's own thread is busy.
#[test]
fn demanded_jobs_compile_on_idle_warm_up_threads_once_spread() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    compiler.spread_demand(true);
    let threads = 1 + compiler.warm_up_threads();
    let _held = hold(&compiler, CompileLane::Demanded, threads);
}

/// While a frame builds its own pipelines, demanded jobs keep to their one
/// thread, so no more compiles run beside the frame's.
#[test]
fn a_demanded_job_waits_for_its_own_thread_until_spread() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    let held = hold(&compiler, CompileLane::Demanded, 1);
    let (ran, demanded_ran) = mpsc::channel();
    compiler.enqueue(CompileLane::Demanded, move || ran.send(()).unwrap());
    assert!(
        demanded_ran
            .recv_timeout(Duration::from_millis(100))
            .is_err(),
        "idle warm-up threads leave it queued"
    );
    compiler.spread_demand(true);
    assert_eq!(
        demanded_ran.recv_timeout(Duration::from_secs(5)),
        Ok(()),
        "a warm-up thread takes it once spread"
    );
    drop(held);
}

/// A thread that frees up while a warm-up and a demanded job are queued
/// takes the demanded job, though the warm-up was queued first.
#[test]
fn a_warm_up_waits_while_demanded_jobs_are_queued() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    compiler.spread_demand(true);
    let threads = 1 + compiler.warm_up_threads();
    let mut held = hold(&compiler, CompileLane::Demanded, threads);
    let (ran, order) = mpsc::channel();
    let warm_up = ran.clone();
    compiler.enqueue(CompileLane::WarmUp, move || {
        warm_up.send("warm-up").unwrap();
    });
    compiler.enqueue(CompileLane::Demanded, move || ran.send("demanded").unwrap());
    assert!(
        order.recv_timeout(Duration::from_millis(100)).is_err(),
        "every thread is held"
    );
    held.pop();
    assert_eq!(
        order.recv_timeout(Duration::from_secs(5)),
        Ok("demanded"),
        "the one free thread takes the demanded job first"
    );
    drop(held);
    assert_eq!(order.recv_timeout(Duration::from_secs(5)), Ok("warm-up"));
}

// A first screen's stand-ins build side by side, one on each thread.
#[test]
fn stand_ins_take_every_thread() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    let threads = 1 + compiler.warm_up_threads();
    let _held = hold(&compiler, CompileLane::StandIn, threads);
}

// No other job starts while a stand-in builds: a driver compiles the jobs
// it is given side by side, and a launch's first frame waits for its
// stand-ins.
#[test]
fn nothing_starts_while_a_stand_in_builds() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    compiler.spread_demand(true);
    let held = hold(&compiler, CompileLane::StandIn, 1);
    let (ran, order) = mpsc::channel();
    let warm_up = ran.clone();
    compiler.enqueue(CompileLane::WarmUp, move || {
        warm_up.send("warm-up").unwrap();
    });
    compiler.enqueue(CompileLane::Demanded, move || ran.send("demanded").unwrap());
    assert!(
        order.recv_timeout(Duration::from_millis(100)).is_err(),
        "both wait for the stand-in"
    );
    drop(held);
    let mut started: Vec<_> = (0..2)
        .map(|_| order.recv_timeout(Duration::from_secs(5)).unwrap())
        .collect();
    started.sort_unstable();
    assert_eq!(started, ["demanded", "warm-up"]);
}

#[test]
fn dropping_every_handle_skips_the_jobs_not_started() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    let handle = compiler.clone();
    let (started, observed) = mpsc::channel();
    let threads = compiler.warm_up_threads();
    let releases: Vec<_> = (0..threads)
        .map(|_| {
            let (release, blocked) = mpsc::channel::<()>();
            let started = started.clone();
            compiler.enqueue(CompileLane::WarmUp, move || {
                started.send("blocking").unwrap();
                blocked.recv().unwrap();
            });
            release
        })
        .collect();
    let (ran, second_ran) = mpsc::channel();
    compiler.enqueue(CompileLane::WarmUp, move || ran.send("second").unwrap());
    for _ in 0..threads {
        assert_eq!(
            observed.recv_timeout(Duration::from_secs(5)).unwrap(),
            "blocking"
        );
    }
    drop(compiler);
    assert!(handle.is_active(), "a surviving handle keeps the worker");
    // The last handle waits for the jobs in flight, so they are released
    // while that drop waits.
    let releaser = std::thread::spawn(move || {
        std::thread::sleep(Duration::from_millis(50));
        for release in releases {
            release.send(()).expect("a blocking job is waiting");
        }
    });
    drop(handle);
    releaser.join().expect("the releaser ends");
    assert_eq!(
        second_ran.recv_timeout(Duration::from_millis(500)),
        Err(mpsc::RecvTimeoutError::Disconnected),
        "the queued job must be dropped, not run"
    );
}

#[test]
fn an_inactive_compiler_drops_jobs() {
    let compiler = PipelineCompiler::inactive();
    assert!(!compiler.is_active());
    let (ran, observed) = mpsc::channel();
    compiler.enqueue(CompileLane::Demanded, move || ran.send(()).unwrap());
    let deadline = Instant::now() + Duration::from_millis(100);
    assert!(observed.recv_timeout(deadline - Instant::now()).is_err());
}

#[test]
fn the_last_handle_waits_for_the_compile_in_flight() {
    use std::sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    };

    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    let (started, compiling) = mpsc::channel();
    let finished = Arc::new(AtomicBool::new(false));
    let done = Arc::clone(&finished);
    compiler.enqueue(CompileLane::WarmUp, move || {
        started.send(()).expect("the test is waiting");
        std::thread::sleep(Duration::from_millis(200));
        done.store(true, Ordering::Release);
    });
    compiling
        .recv_timeout(Duration::from_secs(5))
        .expect("the compile starts");

    drop(compiler);

    assert!(
        finished.load(Ordering::Acquire),
        "a dropped compiler must not leave a compile running, which a process exit can crash"
    );
}

/// Runs a job on the demanded lane and returns once it is counted built.
fn build_one(compiler: &PipelineCompiler) {
    let landing = compiler.landing().expect("a spawned compiler");
    let built = landing.built();
    compiler.enqueue(CompileLane::Demanded, || {});
    let deadline = Instant::now() + Duration::from_secs(5);
    while landing.built() == built {
        assert!(Instant::now() < deadline, "the lane runs its jobs");
        std::thread::sleep(Duration::from_millis(1));
    }
}

#[test]
fn a_pipeline_landing_after_a_placeholder_wakes_the_app_once() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    let landing = compiler
        .landing()
        .expect("a spawned compiler reports landings");
    let (woke, wakes) = mpsc::channel();
    landing.wake_with(Box::new(move || {
        let _ = woke.send(());
    }));
    build_one(&compiler);
    assert!(
        wakes.try_recv().is_err(),
        "a landing nothing waits for wakes nothing"
    );
    landing.await_since(landing.built());
    build_one(&compiler);
    assert!(
        wakes.recv_timeout(Duration::from_secs(5)).is_ok(),
        "the awaited landing wakes the app"
    );
    assert!(wakes.try_recv().is_err(), "once");
    assert!(landing.take_landed());
    assert!(!landing.take_landed(), "reading the landing clears it");
}

// The present thread writes its account of a frame after it read the
// landing: a pipeline that lands in between leaves the account saying no
// frame is due, and the landing keeps the next frame due until a frame
// takes it. The app the landing woke must not sleep on the account.
#[test]
fn a_landing_keeps_a_frame_due_over_a_present_account_read_before_it() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    let landing = compiler
        .landing()
        .expect("a spawned compiler reports landings");
    let status = PresentStatus::default();
    assert!(status.landing.set(Arc::clone(&landing)).is_ok());
    landing.await_since(landing.built());
    status.needs_frame_warmup.store(false, Ordering::Relaxed);
    assert!(!status.warmup_due(), "nothing has landed yet");
    build_one(&compiler);
    assert!(
        status.warmup_due(),
        "the landing asks for the frame that draws its pipeline"
    );
    assert!(landing.take_landed());
    assert!(
        !status.warmup_due(),
        "the frame that takes the landing ends what it asked"
    );
}

#[test]
fn a_pipeline_landing_before_its_placeholder_is_noted_still_counts() {
    let compiler = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    let landing = compiler
        .landing()
        .expect("a spawned compiler reports landings");
    let frame_start = landing.built();
    build_one(&compiler);
    landing.await_since(frame_start);
    assert!(
        landing.landed(),
        "a pipeline built during the frame that drew the placeholder replaces it next frame"
    );
}

/// Metal compiles in the system's compiler service, so a Metal compiler
/// keeps more warm-ups running side by side than one whose driver compiles
/// in the thread that asks.
#[test]
fn a_metal_compiler_runs_more_warm_ups_side_by_side() {
    let metal = PipelineCompiler::spawn(wgpu::Backend::Metal);
    let vulkan = PipelineCompiler::spawn(wgpu::Backend::Vulkan);
    assert!(metal.warm_up_threads() >= 3);
    assert!(metal.warm_up_threads() >= vulkan.warm_up_threads());
    let _held = hold(&metal, CompileLane::WarmUp, metal.warm_up_threads());
}

// The web runs a paced build only after the GPU has done the frame that ran
// the last one, and the next frame's submit is where it learns that: a wait
// nothing ever clears would stop every pipeline after the first.
#[test]
fn a_paced_build_waits_for_the_frame_before_it_and_not_longer() {
    let (_lock, device, queue) = crate::frame_graph::upload_test_device();
    let texture = device.create_texture(&wgpu::TextureDescriptor {
        label: Some("Paced landing frame"),
        size: wgpu::Extent3d {
            width: 1,
            height: 1,
            depth_or_array_layers: 1,
        },
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format: wgpu::TextureFormat::Rgba8Unorm,
        usage: wgpu::TextureUsages::COPY_SRC,
        view_formats: &[],
    });
    let landing = PacedLanding::default();
    assert!(landing.may_build());
    landing.built();
    // The frame that ran the build, submitted and done.
    crate::frame_graph::read_test_texture(&device, &queue, &texture);
    landing.watch(&queue);
    assert!(!landing.may_build());
    // The next frame's submit.
    crate::frame_graph::read_test_texture(&device, &queue, &texture);
    assert!(landing.may_build());
}

// A page that draws no frames runs its paced builds between them, and no
// frame's submit tells when the GPU has done the last: the build's own submit
// and a poll must, or the page would build one pipeline and wait for a frame.
#[test]
fn a_paced_build_between_frames_lands_without_a_frame() {
    let (_lock, device, queue) = crate::frame_graph::upload_test_device();
    let landing = PacedLanding::default();
    landing.built_between_frames(&queue);
    assert!(!landing.may_build());
    device
        .poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: None,
        })
        .expect("the build's submit completes");
    assert!(landing.may_build());
}

// A paced build waits for a frame whose scene stands still, and the demo's
// glass tiles tab, whose clock never stops, drew its glass's stand-in for
// good: a demanded build runs after a bounded number of moving frames, and
// moving frames with nothing demanded count nothing toward the next one.
#[test]
fn a_moving_scene_gets_its_demanded_builds_after_a_bounded_wait() {
    let landing = PacedLanding::default();
    for _ in 0..2 * MOVING_FRAMES_PER_BUILD {
        assert!(!landing.waited_out(false));
    }
    for _ in 1..MOVING_FRAMES_PER_BUILD {
        assert!(!landing.waited_out(true));
    }
    assert!(landing.waited_out(true));
    landing.built();
    assert!(!landing.waited_out(true));
}