dream-ini 0.2.0

Import Morrowind.ini settings into OpenMW configuration files
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
// SPDX-License-Identifier: GPL-3.0-only

use super::TriangleVertices;
use super::math::{
    edge, edge_covers_pixel, edge_step_x, f32_to_usize_ceil_clamped, f32_to_usize_floor_clamped,
    same_f32, usize_to_f32,
};
use super::types::TriangleRasterBounds;

const TRIANGLE_SCANLINE_NARROWING_GUARD_PX: usize = 2;

#[derive(Clone, Copy)]
pub(super) struct TriangleBoundaryIncludes {
    pub(super) edge0: bool,
    pub(super) edge1: bool,
    pub(super) edge2: bool,
}

#[derive(Clone, Copy)]
pub(super) struct TriangleCoverage {
    pub(super) inv_area: f32,
    pub(super) includes_boundary: TriangleBoundaryIncludes,
}

#[derive(Clone, Copy)]
pub(super) struct TriangleRowSearch<'a> {
    pub(super) vertices: TriangleVertices<'a>,
    pub(super) coverage: TriangleCoverage,
    pub(super) y: usize,
    pub(super) candidate_start_x: usize,
    pub(super) candidate_end_x: usize,
    pub(super) probe_start_x: usize,
    pub(super) probe_end_x: usize,
    pub(super) hinted: bool,
    pub(super) collect_stats: bool,
}

#[derive(Clone, Copy)]
pub(super) struct TriangleRowStateSearch {
    pub(super) coverage: TriangleCoverage,
    pub(super) row_start_edges: (f32, f32, f32),
    pub(super) x_steps: (f32, f32, f32),
    pub(super) candidate_start_x: usize,
    pub(super) candidate_end_x: usize,
    pub(super) collect_stats: bool,
}

pub(super) struct TriangleRowEndpoints {
    pub(super) span: Option<(usize, usize)>,
    pub(super) endpoint_probe_px: usize,
    pub(super) hint_probe_px: usize,
    pub(super) canary_probe_px: usize,
    pub(super) fallback_probe_px: usize,
    pub(super) direct_probe_px: usize,
    pub(super) fell_back: bool,
}

pub(super) fn triangle_row_endpoints(search: TriangleRowSearch<'_>) -> TriangleRowEndpoints {
    let (mut span_start, first_probe_px) = triangle_first_covered_x(
        search.probe_start_x,
        search.probe_end_x,
        search.y,
        search.vertices,
        search.coverage,
        search.collect_stats,
    );
    let mut hint_probe_px = 0;
    let mut canary_probe_px = 0;
    let mut fallback_probe_px = 0;
    let mut direct_probe_px = 0;
    if search.hinted {
        hint_probe_px += first_probe_px;
    } else {
        direct_probe_px += first_probe_px;
    }
    let mut fell_back = false;
    if search.hinted {
        fell_back = triangle_hint_needs_fallback(&search, span_start, &mut canary_probe_px);
        if fell_back {
            let (fallback_span_start, fallback_first_probe_px) = triangle_first_covered_x(
                search.candidate_start_x,
                search.candidate_end_x,
                search.y,
                search.vertices,
                search.coverage,
                search.collect_stats,
            );
            fallback_probe_px += fallback_first_probe_px;
            span_start = fallback_span_start;
        }
    }

    let Some(span_start) = span_start else {
        return TriangleRowEndpoints {
            span: None,
            endpoint_probe_px: hint_probe_px
                + canary_probe_px
                + fallback_probe_px
                + direct_probe_px,
            hint_probe_px,
            canary_probe_px,
            fallback_probe_px,
            direct_probe_px,
            fell_back,
        };
    };
    let (last_start_x, last_end_x) = if fell_back {
        (search.candidate_start_x, search.candidate_end_x)
    } else {
        (search.probe_start_x, search.probe_end_x)
    };
    let (last_x, last_probe_px) = triangle_last_covered_x(
        last_start_x,
        last_end_x,
        search.y,
        search.vertices,
        search.coverage,
        search.collect_stats,
    );
    if fell_back {
        fallback_probe_px += last_probe_px;
    } else if search.hinted {
        hint_probe_px += last_probe_px;
    } else {
        direct_probe_px += last_probe_px;
    }
    let span_end = last_x.map_or(span_start + 1, |last_x| last_x.max(span_start) + 1);
    TriangleRowEndpoints {
        span: Some((span_start, span_end)),
        endpoint_probe_px: hint_probe_px + canary_probe_px + fallback_probe_px + direct_probe_px,
        hint_probe_px,
        canary_probe_px,
        fallback_probe_px,
        direct_probe_px,
        fell_back,
    }
}

pub(super) fn triangle_row_state_endpoints(search: TriangleRowStateSearch) -> TriangleRowEndpoints {
    let (span_start, first_probe_px) = triangle_row_state_first_covered_x(search);
    let Some(span_start) = span_start else {
        return TriangleRowEndpoints {
            span: None,
            endpoint_probe_px: first_probe_px,
            hint_probe_px: 0,
            canary_probe_px: 0,
            fallback_probe_px: 0,
            direct_probe_px: first_probe_px,
            fell_back: false,
        };
    };

    let (last_x, last_probe_px) = triangle_row_state_last_covered_x(search);
    let span_end = last_x.map_or(span_start + 1, |last_x| last_x.max(span_start) + 1);
    let endpoint_probe_px = first_probe_px + last_probe_px;
    TriangleRowEndpoints {
        span: Some((span_start, span_end)),
        endpoint_probe_px,
        hint_probe_px: 0,
        canary_probe_px: 0,
        fallback_probe_px: 0,
        direct_probe_px: endpoint_probe_px,
        fell_back: false,
    }
}

fn triangle_row_state_first_covered_x(search: TriangleRowStateSearch) -> (Option<usize>, usize) {
    let (step0, step1, step2) = search.x_steps;
    let (row_edge0, row_edge1, row_edge2) = search.row_start_edges;
    let mut probe_px = 0;
    for x in search.candidate_start_x..search.candidate_end_x {
        if search.collect_stats {
            probe_px += 1;
        }
        let dx = usize_to_f32(x - search.candidate_start_x);
        let edge0 = row_edge0 + step0 * dx;
        let edge1 = row_edge1 + step1 * dx;
        let edge2 = row_edge2 + step2 * dx;
        if triangle_row_state_covers_pixel(search.coverage, edge0, edge1, edge2) {
            return (Some(x), probe_px);
        }
    }
    (None, probe_px)
}

fn triangle_row_state_last_covered_x(search: TriangleRowStateSearch) -> (Option<usize>, usize) {
    let Some(last_candidate_x) = search.candidate_end_x.checked_sub(1) else {
        return (None, 0);
    };
    if last_candidate_x < search.candidate_start_x {
        return (None, 0);
    }

    let (step0, step1, step2) = search.x_steps;
    let (row_edge0, row_edge1, row_edge2) = search.row_start_edges;
    let mut probe_px = 0;
    for x in (search.candidate_start_x..search.candidate_end_x).rev() {
        if search.collect_stats {
            probe_px += 1;
        }
        let dx = usize_to_f32(x - search.candidate_start_x);
        let edge0 = row_edge0 + step0 * dx;
        let edge1 = row_edge1 + step1 * dx;
        let edge2 = row_edge2 + step2 * dx;
        if triangle_row_state_covers_pixel(search.coverage, edge0, edge1, edge2) {
            return (Some(x), probe_px);
        }
    }
    (None, probe_px)
}

fn triangle_row_state_covers_pixel(
    coverage: TriangleCoverage,
    edge0: f32,
    edge1: f32,
    edge2: f32,
) -> bool {
    edge_covers_pixel(edge0 * coverage.inv_area, coverage.includes_boundary.edge0)
        && edge_covers_pixel(edge1 * coverage.inv_area, coverage.includes_boundary.edge1)
        && edge_covers_pixel(edge2 * coverage.inv_area, coverage.includes_boundary.edge2)
}

fn triangle_hint_needs_fallback(
    search: &TriangleRowSearch<'_>,
    span_start: Option<usize>,
    endpoint_probe_px: &mut usize,
) -> bool {
    if span_start.is_none() {
        return true;
    }
    if search.probe_start_x > search.candidate_start_x {
        if search.collect_stats {
            *endpoint_probe_px += 1;
        }
        if triangle_covers_pixel(
            search.probe_start_x - 1,
            search.y,
            search.vertices,
            search.coverage,
        ) {
            return true;
        }
    }
    if search.probe_end_x < search.candidate_end_x {
        if search.collect_stats {
            *endpoint_probe_px += 1;
        }
        if triangle_covers_pixel(
            search.probe_end_x,
            search.y,
            search.vertices,
            search.coverage,
        ) {
            return true;
        }
    }
    false
}

fn triangle_first_covered_x(
    start_x: usize,
    end_x: usize,
    y: usize,
    vertices: TriangleVertices<'_>,
    coverage: TriangleCoverage,
    collect_stats: bool,
) -> (Option<usize>, usize) {
    let mut probe_px = 0;
    for x in start_x..end_x {
        if collect_stats {
            probe_px += 1;
        }
        if triangle_covers_pixel(x, y, vertices, coverage) {
            return (Some(x), probe_px);
        }
    }
    (None, probe_px)
}

fn triangle_last_covered_x(
    start_x: usize,
    end_x: usize,
    y: usize,
    vertices: TriangleVertices<'_>,
    coverage: TriangleCoverage,
    collect_stats: bool,
) -> (Option<usize>, usize) {
    let mut probe_px = 0;
    for x in (start_x..end_x).rev() {
        if collect_stats {
            probe_px += 1;
        }
        if triangle_covers_pixel(x, y, vertices, coverage) {
            return (Some(x), probe_px);
        }
    }
    (None, probe_px)
}

pub(super) fn triangle_hint_x_range(
    vertices: TriangleVertices<'_>,
    inv_area: f32,
    bounds: TriangleRasterBounds,
    pixel_center_y: f32,
    candidate_start_x: usize,
    candidate_end_x: usize,
) -> Option<(usize, usize)> {
    let TriangleVertices { v0, v1, v2 } = vertices;
    let mut min_center_x = f32::NEG_INFINITY;
    let mut max_center_x = f32::INFINITY;
    for (a, b) in [(v1.pos, v2.pos), (v2.pos, v0.pos), (v0.pos, v1.pos)] {
        let slope_x = edge_step_x(a, b) * inv_area;
        let at_origin = edge(a, b, egui::pos2(0.0, pixel_center_y)) * inv_area;
        if !slope_x.is_finite() || !at_origin.is_finite() {
            return None;
        }
        if same_f32(slope_x, 0.0) {
            if at_origin < 0.0 {
                return None;
            }
            continue;
        }
        let boundary_x = -at_origin / slope_x;
        if !boundary_x.is_finite() {
            return None;
        }
        if slope_x > 0.0 {
            min_center_x = min_center_x.max(boundary_x);
        } else {
            max_center_x = max_center_x.min(boundary_x);
        }
    }
    if !min_center_x.is_finite() || !max_center_x.is_finite() || min_center_x > max_center_x {
        return None;
    }

    let start_x = f32_to_usize_ceil_clamped(min_center_x - 0.5, bounds.max_x)
        .max(candidate_start_x)
        .saturating_sub(TRIANGLE_SCANLINE_NARROWING_GUARD_PX)
        .max(candidate_start_x)
        .max(bounds.min_x);
    let end_x = f32_to_usize_floor_clamped(max_center_x - 0.5, bounds.max_x)
        .saturating_add(1 + TRIANGLE_SCANLINE_NARROWING_GUARD_PX)
        .min(candidate_end_x)
        .min(bounds.max_x)
        .max(start_x);
    if start_x >= end_x {
        return None;
    }
    Some((start_x, end_x))
}

fn triangle_covers_pixel(
    x: usize,
    y: usize,
    vertices: TriangleVertices<'_>,
    coverage: TriangleCoverage,
) -> bool {
    let TriangleVertices { v0, v1, v2 } = vertices;
    let pixel_center = egui::pos2(usize_to_f32(x) + 0.5, usize_to_f32(y) + 0.5);
    let w0 = edge(v1.pos, v2.pos, pixel_center) * coverage.inv_area;
    let w1 = edge(v2.pos, v0.pos, pixel_center) * coverage.inv_area;
    let w2 = edge(v0.pos, v1.pos, pixel_center) * coverage.inv_area;
    edge_covers_pixel(w0, coverage.includes_boundary.edge0)
        && edge_covers_pixel(w1, coverage.includes_boundary.edge1)
        && edge_covers_pixel(w2, coverage.includes_boundary.edge2)
}

pub(super) fn triangle_scanline_x_range(
    positions: [egui::Pos2; 3],
    bounds: TriangleRasterBounds,
    pixel_center_y: f32,
) -> (usize, usize) {
    let mut intersections = [0.0; 3];
    let mut count = 0;
    for (a, b) in [
        (positions[0], positions[1]),
        (positions[1], positions[2]),
        (positions[2], positions[0]),
    ] {
        if same_f32(a.y, b.y) {
            continue;
        }
        let min_y = a.y.min(b.y);
        let max_y = a.y.max(b.y);
        if pixel_center_y < min_y || pixel_center_y > max_y {
            continue;
        }
        let t = (pixel_center_y - a.y) / (b.y - a.y);
        let intersection = a.x + (b.x - a.x) * t;
        if !intersection.is_finite() {
            return (bounds.min_x, bounds.max_x);
        }
        intersections[count] = intersection;
        count += 1;
    }
    if count < 2 {
        return (bounds.min_x, bounds.max_x);
    }

    let mut min_x = intersections[0];
    let mut max_x = intersections[0];
    for intersection in intersections.iter().take(count).skip(1) {
        min_x = min_x.min(*intersection);
        max_x = max_x.max(*intersection);
    }

    let start_x = f32_to_usize_floor_clamped(min_x - 0.5, bounds.max_x)
        .max(bounds.min_x)
        .saturating_sub(TRIANGLE_SCANLINE_NARROWING_GUARD_PX)
        .max(bounds.min_x);
    let end_x = f32_to_usize_ceil_clamped(max_x - 0.5, bounds.max_x)
        .saturating_add(1 + TRIANGLE_SCANLINE_NARROWING_GUARD_PX)
        .min(bounds.max_x)
        .max(start_x);
    if start_x >= end_x {
        return (bounds.min_x, bounds.max_x);
    }
    (start_x, end_x)
}