indicatrix-worker 0.7.2

Coordinator and render worker for an indicatrix-backed design catalogue. `serve` (default build) is the coordinator: it serves indicatrix-net's read-only design-library protocol to viewers over mutual TLS and accepts joining render workers on a separate worker port. The optional `worker` feature adds render capacity -- `join` (dial a coordinator and render its requests), `serve --render` (render with the coordinator's own CPU/GPU) and `render` (a one-shot scene-to-PNG CLI).
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Tests for [`emit_tick`]: no zero-sample `PREVIEW`, skipping a redundant full-scale
//! `PREVIEW` alongside a `FRAME` delta, and (S1) double-buffer swap correctness.

use super::fixtures::{
    decode_events, decode_payload, render_request_with_preview, shared_state,
    shared_state_with_pending_frame, stream_config_with_preview_at, tiny_scene,
};
use crate::{
    render_core,
    stream_emit::{
        downsample::downsample_preview,
        emitter::{EmitterAccum, emit_tick},
    },
};
use glam::Vec3;
use indicatrix_net::messages::{RenderRequest, StreamEvent, TransferMode};
use std::sync::{Arc, Mutex};

/// `run_stream` makes the first cadence tick "due" before any sample lands, so
/// `emit_tick` must not build a full-resolution PREVIEW of all-zeros at that point
/// (pure waste -- ~99.5 MB at 4K). Pins: no `StreamEvent::Preview` before
/// `samples_done > 0`; `StreamEvent::Progress` still goes out at 0/N.
#[test]
fn emit_tick_skips_the_preview_before_any_sample_has_landed() {
    let scene = tiny_scene();
    let pixel_count = scene.width as usize * scene.height as usize;
    let request = render_request_with_preview(scene);
    let state = Arc::new(Mutex::new(shared_state(pixel_count, 0)));
    let mut emitter = EmitterAccum::new(pixel_count);
    let mut emission_count: u32 = 0;
    let mut out = Vec::new();

    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();

    let events = decode_events(&out);
    assert!(
        !events.iter().any(|e| matches!(e, StreamEvent::Preview(_))),
        "must not emit a PREVIEW before samples_done > 0: {events:?}"
    );
    assert!(
        events.iter().any(|e| matches!(e, StreamEvent::Progress(_))),
        "PROGRESS must still be emitted at 0/N: {events:?}"
    );
}

/// Counterpart: once a sample has landed, PREVIEW resumes. `samples_done` is 4, the
/// emitter's `running_total` is pre-seeded (standing in for earlier ticks), and this
/// tick's `pending_delta` is empty -- the state between two cadence ticks with nothing
/// new since the last one.
#[test]
fn emit_tick_resumes_the_preview_once_a_sample_has_landed() {
    let scene = tiny_scene();
    let pixel_count = scene.width as usize * scene.height as usize;
    let request = render_request_with_preview(scene);
    let state = Arc::new(Mutex::new(shared_state(pixel_count, 4)));
    let mut emitter = EmitterAccum::from_running_total(vec![Vec3::ONE; pixel_count]);
    let mut emission_count: u32 = 0;
    let mut out = Vec::new();

    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();

    let events = decode_events(&out);
    assert!(
        events.iter().any(|e| matches!(e, StreamEvent::Preview(_))),
        "PREVIEW must resume once samples_done > 0: {events:?}"
    );
}

/// A `PREVIEW` configured at exactly the frame's own resolution is redundant with a
/// `FRAME` delta sent in the same tick -- a client's `Accumulator` already reconstructs
/// the cumulative image by summing `FRAME` deltas, so sending both doubles bandwidth for
/// no new information. Pins: under `LiveProgressive`, with a pending delta and a
/// full-scale `PREVIEW` configured, only `FRAME` and `PROGRESS` go out.
#[test]
fn emit_tick_skips_a_full_scale_preview_when_a_frame_delta_is_also_sent() {
    let scene = tiny_scene();
    let pixel_count = scene.width as usize * scene.height as usize;
    let request = RenderRequest {
        intent: indicatrix_net::messages::RequestIntent::Batch,
        request_id: 1,
        scene: scene.clone(),
        first_sample: 0,
        samples: 8,
        stream: stream_config_with_preview_at(
            TransferMode::LiveProgressive,
            scene.width,
            scene.height,
        ),
    };
    let state = Arc::new(Mutex::new(shared_state_with_pending_frame(
        pixel_count,
        4,
        &vec![Vec3::ONE; pixel_count],
    )));
    let mut emitter = EmitterAccum::new(pixel_count);
    let mut emission_count: u32 = 0;
    let mut out = Vec::new();

    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();

    let events = decode_events(&out);
    assert!(
        events.iter().any(|e| matches!(e, StreamEvent::Frame(_))),
        "expected a FRAME delta: {events:?}"
    );
    assert!(
        !events.iter().any(|e| matches!(e, StreamEvent::Preview(_))),
        "a full-scale PREVIEW alongside a FRAME delta is redundant and must be skipped: {events:?}"
    );
    assert!(
        events.iter().any(|e| matches!(e, StreamEvent::Progress(_))),
        "PROGRESS must still be emitted: {events:?}"
    );
}

/// Counterpart: a full-scale `PREVIEW` is not redundant under `FinalOnly`, since
/// `emit_tick` never sends a `FRAME` under that mode (`emit_final` sends the only
/// `FRAME`, once tracing completes) -- `PREVIEW` is the only progressive image a client
/// gets meanwhile, and must still go out even at matching resolution.
#[test]
fn emit_tick_still_sends_a_full_scale_preview_under_final_only() {
    let scene = tiny_scene();
    let pixel_count = scene.width as usize * scene.height as usize;
    let request = RenderRequest {
        intent: indicatrix_net::messages::RequestIntent::Batch,
        request_id: 1,
        scene: scene.clone(),
        first_sample: 0,
        samples: 8,
        stream: stream_config_with_preview_at(TransferMode::FinalOnly, scene.width, scene.height),
    };
    let state = Arc::new(Mutex::new(shared_state(pixel_count, 4)));
    let mut emitter = EmitterAccum::from_running_total(vec![Vec3::ONE; pixel_count]);
    let mut emission_count: u32 = 0;
    let mut out = Vec::new();

    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();

    let events = decode_events(&out);
    assert!(
        !events.iter().any(|e| matches!(e, StreamEvent::Frame(_))),
        "FinalOnly must never send a FRAME from emit_tick: {events:?}"
    );
    assert!(
        events.iter().any(|e| matches!(e, StreamEvent::Preview(_))),
        "a full-scale PREVIEW is still the only progressive image under FinalOnly and must \
         not be skipped: {events:?}"
    );
}

/// A genuinely downscaled `PREVIEW` is never skipped even alongside a `FRAME` delta --
/// only an exact full-scale match is redundant; a smaller image is cheaper and still
/// the only progressive image at that reduced size.
#[test]
fn emit_tick_still_sends_a_downscaled_preview_alongside_a_frame_delta() {
    let scene = tiny_scene();
    let pixel_count = scene.width as usize * scene.height as usize;
    let request = RenderRequest {
        intent: indicatrix_net::messages::RequestIntent::Batch,
        request_id: 1,
        scene,
        first_sample: 0,
        samples: 8,
        stream: stream_config_with_preview_at(TransferMode::LiveProgressive, 2, 2),
    };
    let state = Arc::new(Mutex::new(shared_state_with_pending_frame(
        pixel_count,
        4,
        &vec![Vec3::ONE; pixel_count],
    )));
    let mut emitter = EmitterAccum::new(pixel_count);
    let mut emission_count: u32 = 0;
    let mut out = Vec::new();

    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();

    let events = decode_events(&out);
    assert!(events.iter().any(|e| matches!(e, StreamEvent::Frame(_))));
    assert!(
        events.iter().any(|e| matches!(e, StreamEvent::Preview(_))),
        "a downscaled PREVIEW must never be skipped: {events:?}"
    );
}

/// The `FRAME` `emit_tick` writes is exactly what was pending in `state.pending_delta`,
/// bit-for-bit (`radiance::encode`/`decode` and `PendingDelta::swap_with` are pure
/// reinterpretation/pointer-swap, no arithmetic) -- not a value the swap could have
/// silently corrupted or duplicated.
#[test]
fn emit_tick_frame_delta_equals_exactly_what_the_tracer_had_pending() {
    let scene = tiny_scene();
    let pixel_count = scene.width as usize * scene.height as usize;
    let contribution = render_core::trace_samples(&scene, 0, 4, 1);
    let request = RenderRequest {
        intent: indicatrix_net::messages::RequestIntent::Batch,
        request_id: 7,
        scene: scene.clone(),
        first_sample: 0,
        samples: 8,
        // Downscaled, not full-scale, so the redundancy skip doesn't apply and this
        // tick writes both a FRAME and a PREVIEW to check.
        stream: stream_config_with_preview_at(TransferMode::LiveProgressive, 2, 2),
    };
    let state = Arc::new(Mutex::new(shared_state_with_pending_frame(
        pixel_count,
        4,
        &contribution,
    )));
    let mut emitter = EmitterAccum::new(pixel_count);
    let mut emission_count: u32 = 0;
    let mut out = Vec::new();

    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();

    let frame = decode_payload(&out, scene.width, scene.height, |e| {
        matches!(e, StreamEvent::Frame(_))
    });
    assert_eq!(
        frame, contribution,
        "the FRAME delta must equal exactly what the tracer had folded into pending_delta"
    );
}

/// `PREVIEW` is the downscaled cumulative sum -- here, the first delta ever folded in,
/// so just `contribution` downsampled -- built from `EmitterAccum::running_total`,
/// never from `state`.
#[test]
fn emit_tick_preview_equals_the_downscaled_cumulative_sum() {
    let scene = tiny_scene();
    let pixel_count = scene.width as usize * scene.height as usize;
    let contribution = render_core::trace_samples(&scene, 0, 4, 1);
    let request = RenderRequest {
        intent: indicatrix_net::messages::RequestIntent::Batch,
        request_id: 8,
        scene: scene.clone(),
        first_sample: 0,
        samples: 8,
        stream: stream_config_with_preview_at(TransferMode::LiveProgressive, 2, 2),
    };
    let state = Arc::new(Mutex::new(shared_state_with_pending_frame(
        pixel_count,
        4,
        &contribution,
    )));
    let mut emitter = EmitterAccum::new(pixel_count);
    let mut emission_count: u32 = 0;
    let mut out = Vec::new();

    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();

    let preview = decode_payload(&out, 2, 2, |e| matches!(e, StreamEvent::Preview(_)));
    let expected = downsample_preview(&contribution, scene.width, scene.height, 2, 2);
    assert_eq!(
        preview, expected,
        "PREVIEW must equal the downscaled cumulative running total"
    );
}

/// Once `emit_tick` has swapped a delta out of `state.pending_delta`, anything the
/// tracer adds afterward lands in fresh, separate memory -- it can never corrupt the
/// `FRAME`/`PREVIEW` already written, nor merge into a completed tick's
/// `EmitterAccum::running_total`.
#[test]
fn emit_tick_swap_leaves_the_emitter_copy_independent_of_the_shared_pending_delta() {
    let scene = tiny_scene();
    let pixel_count = scene.width as usize * scene.height as usize;
    let first = render_core::trace_samples(&scene, 0, 4, 1);
    let request = RenderRequest {
        intent: indicatrix_net::messages::RequestIntent::Batch,
        request_id: 9,
        scene: scene.clone(),
        first_sample: 0,
        samples: 8,
        stream: stream_config_with_preview_at(TransferMode::LiveProgressive, 2, 2),
    };
    let state = Arc::new(Mutex::new(shared_state_with_pending_frame(
        pixel_count,
        4,
        &first,
    )));
    let mut emitter = EmitterAccum::new(pixel_count);
    let mut emission_count: u32 = 0;
    let mut out = Vec::new();

    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();

    // Simulate the tracer producing a second contribution right after the swap, as
    // `run_tracer`'s loop does between cadence ticks.
    let second = render_core::trace_samples(&scene, 4, 4, 1);
    {
        let mut guard = state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        guard.pending_delta.add(4, 4, &second);
    }

    let frame = decode_payload(&out, scene.width, scene.height, |e| {
        matches!(e, StreamEvent::Frame(_))
    });
    assert_eq!(
        frame, first,
        "the FRAME already written must be unaffected by data added to pending_delta \
         after the swap that produced it"
    );
    assert_eq!(
        emitter.running_total(),
        &first[..],
        "EmitterAccum::running_total must be genuinely separate memory from \
         state.pending_delta's buffer, not an alias of it"
    );
}

/// A second `PREVIEW` opportunity that hasn't advanced `samples_done` by
/// at least 1% of the request's sample budget since the last one this emitter actually
/// wrote is suppressed -- the gate that keeps a coordinator `FinalImageRequest` job
/// (whose running total, and so `samples_done`, only changes when a chunk merges) from
/// resending an unchanged `PREVIEW` on every 1 s cadence tick in between. A later tick
/// that does clear the threshold resumes sending.
#[test]
fn emit_tick_preview_is_gated_by_one_percent_of_the_sample_budget_since_the_last_one() {
    let scene = tiny_scene();
    let pixel_count = scene.width as usize * scene.height as usize;
    let request = RenderRequest {
        intent: indicatrix_net::messages::RequestIntent::Batch,
        request_id: 1,
        scene,
        first_sample: 0,
        samples: 200, // 1% of 200 == 2 samples.
        stream: stream_config_with_preview_at(TransferMode::FinalOnly, 2, 2),
    };
    let state = Arc::new(Mutex::new(shared_state(pixel_count, 4)));
    let mut emitter = EmitterAccum::from_running_total(vec![Vec3::ONE; pixel_count]);
    let mut emission_count: u32 = 0;

    // First opportunity: nothing written yet this request, so it always qualifies.
    let mut out = Vec::new();
    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();
    assert!(
        decode_events(&out)
            .iter()
            .any(|e| matches!(e, StreamEvent::Preview(_))),
        "the first preview opportunity must always qualify"
    );

    // Second tick: samples_done advances by only 1 (< the 2-sample threshold) -> the
    // running total is otherwise unchanged, so re-sending would be pure waste.
    state
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .samples_done = 5;
    let mut out = Vec::new();
    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();
    assert!(
        !decode_events(&out)
            .iter()
            .any(|e| matches!(e, StreamEvent::Preview(_))),
        "a preview that has not advanced by 1% of the sample budget must be suppressed"
    );

    // Third tick: cumulative advance from the last WRITTEN preview (samples_done 4)
    // now reaches the 2-sample threshold -> resumes.
    state
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .samples_done = 6;
    let mut out = Vec::new();
    emit_tick(
        &mut out,
        &request,
        &state,
        &mut emitter,
        &mut emission_count,
    )
    .unwrap();
    assert!(
        decode_events(&out)
            .iter()
            .any(|e| matches!(e, StreamEvent::Preview(_))),
        "a preview that has advanced by 1% since the last one must resume"
    );
}