turbine_process3d 0.12.1

Processing for the Turbine game engine
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! # Tile rendering algorithms

use crate::{PixelPos, Point, RayHit, RayHitAll, TilePos, Triangle, Uv};
use crate::cam::CameraPerspective;
use crate::frustrum::{
    frustum_planes_tile,
    frustum_planes_triangle_chunk_mask,
    near_dim,
};
use crate::mask::CompressedMasks;
use crate::ray::{ray_dir, ray_triangle_chunk_hit_update, ray_triangle_chunk_hit_all_update};
use crate::triangle::{chunk_iter, triangle_chunk};
use crate::produce::Produce;

/// Get optimal tile size for sub-tiling.
///
/// When sub-tiling is enabled, it should override the tile size set by the user,
/// to use a better number with more divisors.
pub const fn optimal_sub_tile_size(tile_size: u32) -> u32 {
    match tile_size {
        0..=12 => 12,
        13..=24 => 24,
        25..=36 => 36,
        37..=48 => 48,
        49..=60 => 60,
        _ => 120,
    }
}

/// Calculate the largest divisor sub-tile-size where average number of triangles
/// is less than `k`.
///
/// The input of `opt_tile_size` should be an output of `optimal_sub_tile_size`.
///
/// The initial condition of sub-tiling is `triangles_in_tile >= k`.
pub const fn sub_tile(opt_tile_size: u32, triangles_in_tile: u32, k: u32) -> u32 {
    match opt_tile_size {
        12 => {
            // Do binary search on `2, 3, 4, 6`.
            let n = opt_tile_size / 4;
            let y = triangles_in_tile / (n * n);
            if y >= k {
                let n = opt_tile_size / 3;
                let y = triangles_in_tile / (n * n);
                if y >= k {return 2}
                else {return 3}
            };

            let n = opt_tile_size / 6;
            let y = triangles_in_tile / (n * n);
            if y >= k {return 4}
            else {return 6}
        }
        24 => {
            // Do binary search on `2 3 4 6 8 12`.
            let n = opt_tile_size / 6;
            let y = triangles_in_tile / (n * n);
            if y >= k {
                let n = opt_tile_size / 3;
                let y = triangles_in_tile / (n * n);
                if y >= k {return 2};

                let n = opt_tile_size / 4;
                let y = triangles_in_tile / (n * n);
                if y >= k {return 3}
                else {return 4}
            }

            let n = opt_tile_size / 8;
            let y = opt_tile_size / (n * n);
            if y >= k {return 6};

            let n = opt_tile_size / 12;
            let y = opt_tile_size / (n * n);
            if y >= k {return 8}
            else {return 12}
        }
        36 => {
            // Do binary search on `2 3 4 6 9 12 18`.
            let n = opt_tile_size / 6;
            let y = triangles_in_tile / (n * n);
            if y >= k {
                let n = opt_tile_size / 3;
                let y = triangles_in_tile / (n * n);
                if y >= k {return 2}

                let n = opt_tile_size / 4;
                let y = triangles_in_tile / (n * n);
                if y >= k {return 3}
                else {return 4}
            }

            let n = opt_tile_size / 12;
            let y = triangles_in_tile / (n * n);
            if y >= k {
                let n = opt_tile_size / 9;
                let y = triangles_in_tile / (n * n);
                if y >= k {return 6}
                else {return 9}
            }

            let n = opt_tile_size / 18;
            let y = triangles_in_tile / (n * n);
            if y >= k {return 12}
            else {return 18}
        }
        48 => {
            // Do binary search on `2 3 4 6 8 12 16 24`.
            let n = opt_tile_size / 8;
            let y = triangles_in_tile / (n * n);
            if y >= k {
                let n = opt_tile_size / 4;
                let y = triangles_in_tile / (n * n);
                if y >= k {
                    let n = opt_tile_size / 3;
                    let y = triangles_in_tile / (n * n);
                    if y >= k {return 2}
                    else {return 3}
                }

                let n = opt_tile_size / 6;
                let y = triangles_in_tile / (n * n);
                if y >= k {return 4}
                else {return 6}
            }

            let n = opt_tile_size / 16;
            let y = triangles_in_tile / (n * n);
            if y >= k {
                let n = opt_tile_size / 12;
                let y = triangles_in_tile / (n * n);
                if y >= k {return 8}
                else {return 12}
            }

            let n = opt_tile_size / 24;
            let y = triangles_in_tile / (n * n);
            if y >= k {return 16}
            else {return 24}
        }
        60 => {
            // Do binary search on `2 3 4 5 6 10 12 15 20 30`.
            let n = opt_tile_size / 10;
            let y = triangles_in_tile / (n * n);
            if y >= k {
                let n = opt_tile_size / 4;
                let y = triangles_in_tile / (n * n);
                if y >= k {
                    let n = opt_tile_size / 3;
                    let y = triangles_in_tile / (n * n);
                    if y >= k {return 2}
                    else {return 3}
                }

                let n = opt_tile_size / 6;
                let y = triangles_in_tile / (n * n);
                if y >= k {return 5}
                else {return 6}
            }

            let n = opt_tile_size / 15;
            let y = triangles_in_tile / (n * n);
            if y >= k {return 12}

            let n = opt_tile_size / 20;
            let y = triangles_in_tile / (n * n);
            if y >= k {return 15}

            let n = opt_tile_size / 30;
            let y = triangles_in_tile / (n * n);
            if y >= k {return 20}
            else {return 30}
        }
        _ => {
            // Do binary search on `2 3 4 5 6 8 10 12 15 20 24 30 40 60`.
            let n = opt_tile_size / 12;
            let y = triangles_in_tile / (n * n);
            if y >= k {
                let n = opt_tile_size / 6;
                let y = triangles_in_tile / (n * n);
                if y >= k {
                    let n = opt_tile_size / 4;
                    let y = triangles_in_tile / (n * n);
                    if y >= k {
                        let n = opt_tile_size / 3;
                        let y = triangles_in_tile / (n * n);
                        if y >= k {return 2}
                        else {return 3}
                    }

                    let n = opt_tile_size / 5;
                    let y = triangles_in_tile / (n * n);
                    if y >= k {return 4}
                    else {return 5}
                }

                let n = opt_tile_size / 10;
                let y = triangles_in_tile / (n * n);
                if y >= k {
                    let n = opt_tile_size / 8;
                    let y = triangles_in_tile / (n * n);
                    if y >= k {return 6}
                    else {return 8}
                } else {return 10}
            }

            let n = opt_tile_size / 24;
            let y = triangles_in_tile / (n * n);
            if y >= k {
                let n = opt_tile_size / 20;
                let y = triangles_in_tile / (n * n);
                if y >= k {
                    let n = opt_tile_size / 15;
                    let y = triangles_in_tile / (n * n);
                    if y >= k {return 12}
                    else {return 15}
                } else {return 20}
            }

            let n = opt_tile_size / 40;
            let y = triangles_in_tile / (n * n);
            if y >= k {
                let n = opt_tile_size / 30;
                let y = triangles_in_tile / (n * n);
                if y >= k {return 24}
                else {return 30}
            }

            let n = opt_tile_size / 60;
            let y = triangles_in_tile / (n * n);
            if y >= k {return 40}
            else {return 60}
        }
    }
}

/// From camera perspective, tile and a list of triangles, get mask of intersecting triangles.
///
/// This is used as a preparation stage before sampling each tile in parallel.
///
/// The algorithm does not clear the masks before pushing new ones.
pub fn tile_mask<T: Produce<Triangle> + ?Sized>(
    persp: &CameraPerspective,
    dim: Uv,
    tile_pos: Uv,
    tile_size: Uv,
    list: &T,
    masks: &mut CompressedMasks,
) {
    let fr = frustum_planes_tile(persp, dim, tile_pos, tile_size);
    let mut i = 0;
    let n = list.virtual_length();
    loop {
        if i >= n {break}
        let (chunk, bits) = triangle_chunk(list, i);
        masks.push(frustum_planes_triangle_chunk_mask(&fr, &chunk, bits));
        i += 64;
    }
}

/// From camera perspective, tile and a list of triangles, get mask of intersecting triangles.
///
/// This is used as a preparation stage before sampling each tile in parallel.
///
/// The algorithm does not clear the masks before pushing new ones.
pub fn tile_mask_with_pre_mask<T: Produce<Triangle> + ?Sized>(
    persp: &CameraPerspective,
    dim: Uv,
    tile_pos: Uv,
    tile_size: Uv,
    list: &T,
    masks: &mut CompressedMasks,
    pre_masks: &CompressedMasks,
) {
    let fr = frustum_planes_tile(persp, dim, tile_pos, tile_size);
    let iter = chunk_iter(list, pre_masks);
    let mut last_off = 0;
    for (off, (chunk, mask)) in iter {
        for _ in (last_off..off).step_by(64) {masks.push(0)};
        masks.push(frustum_planes_triangle_chunk_mask(&fr, &chunk, mask));
        last_off = off + 64;
    }
}

/// Calculate the normalized tile position.
///
/// Dimension is the size of image in pixels.
///
/// Indices are in the grid of tiles.
///
/// Tile size is both width and height in pixels.
pub fn tile_pos(dim: PixelPos, [i, j]: TilePos, tile_size: u32) -> Uv {
    let x = (i as f32 * tile_size as f32) / dim[0] as f32 * 2.0 - 1.0;
    let y = (j as f32 * tile_size as f32) / dim[1] as f32 * 2.0 - 1.0;
    [x, y]
}

/// Calculate the normalized tile size.
///
/// Dimension is the size of image in pixels.
///
/// Indices are in the grid of tiles.
///
/// Tile size is both width and height in pixels.
pub fn tile_size(dim: PixelPos, [i, j]: TilePos, tile_size: u32) -> Uv {
    let ts = tile_size as f32;
    let tw = (dim[0] as f32 / ts).ceil() * ts;
    let th = (dim[1] as f32 / ts).ceil() * ts;
    let mix = i as f32 * ts;
    let miy = j as f32 * ts;
    let max = (i + 1) as f32 * ts;
    let may = (j + 1) as f32 * ts;
    [
        (max.min(tw) - mix) / dim[0] as f32 * 2.0,
        (may.min(th) - miy) / dim[1] as f32 * 2.0,
    ]
}

/// Calculate the grid size of tiles needed to cover image.
///
/// Dimension is the size of image in pixels.
///
/// Indices are in the grid of tiles.
pub fn tile_grid(dim: PixelPos, tile_size: u32) -> [u32; 2] {
    let tw = (dim[0] as f32 / tile_size as f32).ceil();
    let th = (dim[1] as f32 / tile_size as f32).ceil();
    [tw as u32, th as u32]
}

/// Prepare row sub-tile compressed masks.
pub fn pre_row_sub_masks(dim: PixelPos, n_tile_size: u32) -> Vec<Vec<CompressedMasks>> {
    let h = tile_grid(dim, n_tile_size)[1];
    vec![vec![]; h as usize]
}

/// Prepare compressed masks.
pub fn pre_masks(
    dim: PixelPos,
    n_tile_size: u32,
) -> Vec<CompressedMasks> {
    let [w, h] = tile_grid(dim, n_tile_size);
    let n = w * h;
    vec![CompressedMasks::new(); n as usize]
}

/// Fake masks that includes all triangles.
///
/// This is used for testing.
pub fn fake_all_masks<T: Produce<Triangle> + ?Sized + Sync>(
    _persp: &CameraPerspective,
    _dim: PixelPos,
    _n_tile_size: u32,
    list: &T,
    masks: &mut [CompressedMasks]
) {
    for masks in masks {
        masks.clear();
        masks.push_ones(list.virtual_length() as u64);
    }
}

/// Collect sub-masks per tile row.
pub fn row_sub_masks<T: Produce<Triangle> + ?Sized + Sync>(
    persp: &CameraPerspective,
    dim: PixelPos,
    n_tile_size: u32,
    grid: [u32; 2],
    triangle_limit_per_tile: u32,
    list: &T,
    masks: &[CompressedMasks],
    sub_masks: &mut Vec<Vec<CompressedMasks>>
) {
    use rayon::prelude::*;

    let ndim = near_dim(&persp);
    let w = grid[0];
    sub_masks.par_iter_mut().enumerate().for_each(|(tj, sm)| {
        let tj = tj as u32;
        sm.clear();
        for (ti, val) in row_sub_tile_iter(n_tile_size, grid, tj, triangle_limit_per_tile, masks) {
            if let Some((st, offset)) = val {
                let k = tj * w + ti;
                let pre_masks = &masks[k as usize];
                let n = n_tile_size / st;
                assert_eq!(sm.len(), offset);
                for j in 0..n {
                    for i in 0..n {
                        let ind: usize = offset + (j * n + i) as usize;
                        while sm.len() <= ind {
                            sm.push(CompressedMasks::new())
                        }
                        let m = &mut sm[ind];
                        m.clear();
                        let pos = [ti * n + i, tj * n + j];
                        let tpos = tile_pos(dim, pos, st);
                        let tsize = tile_size(dim, pos, st);
                        tile_mask_with_pre_mask(persp, ndim, tpos, tsize, list, m, pre_masks);
                    }
                }
            }
        }
    });
}

/// Collect all masks per tile, using pre-masks at lower resolution.
///
/// This speeds up compression, because one can iterate faster over
/// the virtual triangles using the pre-masks.
///
/// `scale_to_pre_tile_size` specifies the ratio of pre-tile-size divided by tile-size.
pub fn masks_with_pre_masks<T: Produce<Triangle> + ?Sized + Sync>(
    persp: &CameraPerspective,
    dim: PixelPos,
    n_tile_size: u32,
    scale_to_pre_tile_size: u32,
    list: &T,
    masks: &mut [CompressedMasks],
    pre_masks: &mut [CompressedMasks],
) {
    use rayon::prelude::*;

    let s = scale_to_pre_tile_size;
    let w = tile_grid(dim, n_tile_size)[0];
    let w2 = tile_grid(dim, n_tile_size * s)[0];
    let ndim = near_dim(&persp);
    masks.par_iter_mut().enumerate().for_each(|(k,  masks)| {
        masks.clear();
        let i = k as u32 % w;
        let j = k as u32 / w;
        let pre_masks = &pre_masks[((j / s) * w2 + i / s) as usize];
        let tpos = tile_pos(dim, [i, j], n_tile_size);
        let tsize = tile_size(dim, [i, j], n_tile_size);
        tile_mask_with_pre_mask(persp, ndim, tpos, tsize, list, masks, pre_masks);
    });
}

/// Collect all masks per tile.
pub fn masks<T: Produce<Triangle> + ?Sized + Sync>(
    persp: &CameraPerspective,
    dim: PixelPos,
    n_tile_size: u32,
    list: &T,
    masks: &mut [CompressedMasks]
) {
    use rayon::prelude::*;

    let w = tile_grid(dim, n_tile_size)[0];
    let ndim = near_dim(&persp);
    masks.par_iter_mut().enumerate().for_each(|(k,  masks)| {
        masks.clear();
        let i = k as u32 % w;
        let j = k as u32 / w;
        let tpos = tile_pos(dim, [i, j], n_tile_size);
        let tsize = tile_size(dim, [i, j], n_tile_size);
        tile_mask(persp, ndim, tpos, tsize, list, masks);
    });
}

/// Render depth of a tile using a camera perspective, image resolution,
/// tile position, tile size and triangle list with mask, into a tile depth and index buffer.
///
/// Iterates through triangle chunks and updates the tile depth and index buffer.
///
/// Ray direction is recreated for each triangle chunk.
///
/// Requires compressed masks per tile to be prepared in advance.
pub fn render_tile_depth<T: Produce<Triangle> + ?Sized, const TILE_SIZE: usize>(
    persp: &CameraPerspective,
    dim: PixelPos,
    pos: PixelPos,
    list: &T,
    masks: &CompressedMasks,
    tile: &mut [[RayHit; TILE_SIZE]; TILE_SIZE],
) {
    let n_tile_size = TILE_SIZE as u32;
    let eye = [0.0; 3];
    let iter = chunk_iter(list, masks);
    for (off, (chunk, mask)) in iter {
        for j in 0..n_tile_size {
            for i in 0..n_tile_size {
                let dir: Point = ray_dir(persp, eye, [pos[0] + i, pos[1] + j], dim);
                ray_triangle_chunk_hit_update((eye, dir), &chunk, mask, off,
                    &mut tile[j as usize][i as usize]);
            }
        }
    }
}

/// Creates an iterator for row sub-tiling, where `(col, None)` means no sub-tiling and
/// `(col, Some((st, offset)))` means sub-tiling of size `st` with sub-mask offset.
///
/// The sub-mask offset is relative to some list per row that
/// stores compressed masks for sub-tiling.
pub fn row_sub_tile_iter(
    n_tile_size: u32,
    grid: [u32; 2],
    row: u32,
    triangle_limit_per_tile: u32,
    masks: &[CompressedMasks]
) -> impl Iterator<Item = (u32, Option<(u32, usize)>)> {
    let w = grid[0];
    (0..w).scan(0usize, move |offset, col| {
        let k = row * w + col;
        let triangles = masks[k as usize].count_ones() as u32;
        Some((col, if triangles >= triangle_limit_per_tile {
            let st = sub_tile(n_tile_size, triangles, triangle_limit_per_tile);
            let n = n_tile_size / st;
            let start = *offset;
            *offset += (n * n) as usize;
            Some((st, start))
        } else {None}))
    })
}

/// Render depth of row of sub-tiles using a camera perspective, image resolution,
/// tile position, tile size, sub-tile size and triangle list with mask,
/// into a tile depth and index buffer.
///
/// This can be used to render semi-transparent objects,
/// because the ray visits all triangles.
///
/// Notice that there is no guaranteed order.
/// This has to be managed either by pre-ordering or by post-processing.
///
/// `RayHitAll` in `tile` should be initialized to `Some((0.0, IndexFlag::from_parts(0, false)))`.
/// When `None`, the ray will not progress further.
/// Check `IndexFlag::flag` to see whether the ray hit something new.
///
/// Iterates through all triangles in chunks and updates the tile depth and index buffer.
///
/// Ray direction is recreated for each triangle chunk.
///
/// Requires compressed masks per tile to be prepared in advance.
///
/// Returns `true` if there is something to render.
/// You can use a loop and break when this is `false`.
pub fn render_row_sub_tile_depth_all<T: Produce<Triangle> + ?Sized, const TILE_SIZE: usize>(
    persp: &CameraPerspective,
    dim: PixelPos,
    pos: PixelPos,
    sub_tile_size: u32,
    list: &T,
    sub_masks: &[CompressedMasks],
    tile: &mut [[RayHitAll; TILE_SIZE]; TILE_SIZE],
) -> bool {
    use crate::IndexFlag;

    let n_tile_size = TILE_SIZE as u32;
    let n = n_tile_size / sub_tile_size;
    let eye = [0.0; 3];
    let mut alive = false;

    // For each sub-tile.
    for kj in 0..n {
        for ki in 0..n {
            // Iterate through the chunks for that specific sub-tile.
            let k = kj * n + ki;
            let iter = chunk_iter(list, &sub_masks[k as usize]);
            let sub_tile_pos = [ki * sub_tile_size, kj * sub_tile_size];
            for (off, (chunk, mask)) in iter {
                // For each ray in the sub-tile.
                for j in 0..sub_tile_size {
                    for i in 0..sub_tile_size {
                        let i = sub_tile_pos[0] + i;
                        let j = sub_tile_pos[1] + j;

                        let hit = &mut tile[j as usize][i as usize];
                        let dir: Point = ray_dir(persp, eye, [pos[0] + i, pos[1] + j], dim);
                        ray_triangle_chunk_hit_all_update((eye, dir), &chunk, mask, off, hit);
                        if let Some((d, index_flag)) = hit {
                            if !index_flag.flag() {
                                let ind = index_flag.index();
                                let new_ind = ind.max(off + 64);
                                *hit = Some((*d, IndexFlag::from_parts(new_ind, false)));
                            }
                        }
                        alive |= hit.is_some();
                    }
                }
            }
        }
    }

    // Terminate rays when not hitting anything new.
    let len = list.virtual_length();
    for j in 0..TILE_SIZE {
        for i in 0..TILE_SIZE {
            let hit = &mut tile[j][i];
            if let Some((_, index_flag)) = hit {
                if !index_flag.flag() || index_flag.index() >= len {*hit = None};
            }
        }
    }

    alive
}

/// Render depth of a tile using a camera perspective, image resolution,
/// tile position, tile size and triangle list with mask, into a tile depth and index buffer.
///
/// This can be used to render semi-transparent objects,
/// because the ray visits all triangles.
///
/// Notice that there is no guaranteed order.
/// This has to be managed either by pre-ordering or by post-processing.
///
/// `RayHitAll` in `tile` should be initialized to `Some((0.0, IndexFlag::from_parts(0, false)))`.
/// When `None`, the ray will not progress further.
/// Check `IndexFlag::flag` to see whether the ray hit something new.
///
/// Iterates through all triangles in chunks and updates the tile depth and index buffer.
///
/// Ray direction is recreated for each triangle chunk.
///
/// Requires compressed masks per tile to be prepared in advance.
///
/// Returns `true` if there is something to render.
/// You can use a loop and break when this is `false`.
pub fn render_tile_depth_all<T: Produce<Triangle> + ?Sized, const TILE_SIZE: usize>(
    persp: &CameraPerspective,
    dim: PixelPos,
    pos: PixelPos,
    list: &T,
    masks: &CompressedMasks,
    tile: &mut [[RayHitAll; TILE_SIZE]; TILE_SIZE],
) -> bool {
    use crate::IndexFlag;

    let n_tile_size = TILE_SIZE as u32;
    let eye = [0.0; 3];
    let iter = chunk_iter(list, masks);
    let mut alive = false;
    for (off, (chunk, mask)) in iter {
        let mut inner_alive = false;
        for j in 0..n_tile_size {
            for i in 0..n_tile_size {
                let hit = &mut tile[j as usize][i as usize];
                let dir: Point = ray_dir(persp, eye, [pos[0] + i, pos[1] + j], dim);
                ray_triangle_chunk_hit_all_update((eye, dir), &chunk, mask, off, hit);
                if let Some((d, index_flag)) = hit {
                    if !index_flag.flag() {
                        let ind = index_flag.index();
                        let new_ind = ind.max(off + 64);
                        *hit = Some((*d, IndexFlag::from_parts(new_ind, false)));
                    }
                }
                inner_alive |= hit.is_some();
            }
        }
        alive |= inner_alive;
        // Skip iteration if there no rays alive or nothing to render.
        if !inner_alive {return alive}
    }

    // Terminate rays when not hitting anything new.
    let len = list.virtual_length();
    for j in 0..TILE_SIZE {
        for i in 0..TILE_SIZE {
            let hit = &mut tile[j][i];
            if let Some((_, index_flag)) = hit {
                if !index_flag.flag() || index_flag.index() >= len {*hit = None};
            }
        }
    }

    alive
}