systemless 0.106.0

High-Level Emulation for classic Macintosh applications
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
//! Pixel storage for classic OpenGL drawables. Coordinates at this boundary use
//! OpenGL's lower-left origin; QuickDraw's guest framebuffer uses the upper-left.

#![allow(dead_code)]

use super::{PpcFrontBuffer, PpcSectionMem};

const MAX_DRAWABLE_PIXELS: usize = 4096 * 4096;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClassicGlColorBuffer {
    Front,
    Back,
}

/// State used by `glClear`. `scissor` is in lower-left OpenGL coordinates.
/// See OpenGL 1.2.1, sections 4.1.2 and 4.2.2-4.2.3.
/// <https://registry.khronos.org/OpenGL/specs/gl/glspec121.pdf>
#[derive(Debug, Clone, Copy)]
pub struct ClassicGlClear {
    pub color: Option<(ClassicGlColorBuffer, [u8; 4])>,
    pub color_mask: [bool; 4],
    pub depth: Option<f32>,
    pub depth_mask: bool,
    pub stencil: Option<u8>,
    pub stencil_mask: u8,
    pub scissor: Option<(i32, i32, u32, u32)>,
}

#[derive(Debug, Clone)]
pub struct ClassicGlFramebuffer {
    width: usize,
    height: usize,
    front: Vec<[u8; 4]>,
    back: Option<Vec<[u8; 4]>>,
    depth: Vec<f32>,
    stencil: Vec<u8>,
}

impl ClassicGlFramebuffer {
    pub fn width(&self) -> u32 {
        self.width as u32
    }

    pub fn height(&self) -> u32 {
        self.height as u32
    }

    pub fn new(width: u32, height: u32, double_buffered: bool) -> Option<Self> {
        let (width, height) = (usize::try_from(width).ok()?, usize::try_from(height).ok()?);
        let len = width.checked_mul(height)?;
        if len == 0 || len > MAX_DRAWABLE_PIXELS {
            return None;
        }
        let color = vec![[0, 0, 0, 0]; len];
        Some(Self {
            width,
            height,
            front: color.clone(),
            back: double_buffered.then_some(color),
            depth: vec![1.0; len],
            stencil: vec![0; len],
        })
    }

    fn color(&self, buffer: ClassicGlColorBuffer) -> Option<&[[u8; 4]]> {
        match buffer {
            ClassicGlColorBuffer::Front => Some(&self.front),
            ClassicGlColorBuffer::Back => self.back.as_deref(),
        }
    }

    fn color_mut(&mut self, buffer: ClassicGlColorBuffer) -> Option<&mut [[u8; 4]]> {
        match buffer {
            ClassicGlColorBuffer::Front => Some(&mut self.front),
            ClassicGlColorBuffer::Back => self.back.as_deref_mut(),
        }
    }

    fn index(&self, x: u32, y: u32) -> Option<usize> {
        let (x, y) = (usize::try_from(x).ok()?, usize::try_from(y).ok()?);
        (x < self.width && y < self.height).then(|| y * self.width + x)
    }

    pub fn clear_color(&mut self, buffer: ClassicGlColorBuffer, rgba: [u8; 4]) -> bool {
        let Some(pixels) = self.color_mut(buffer) else {
            return false;
        };
        pixels.fill(rgba);
        true
    }

    pub fn clear(&mut self, clear: ClassicGlClear) -> bool {
        if let Some((buffer, _)) = clear.color {
            if self.color(buffer).is_none() {
                return false;
            }
        }
        let (left, bottom, right, top) = match clear.scissor {
            Some((x, y, width, height)) => (
                i64::from(x).clamp(0, self.width as i64) as usize,
                i64::from(y).clamp(0, self.height as i64) as usize,
                (i64::from(x) + i64::from(width)).clamp(0, self.width as i64) as usize,
                (i64::from(y) + i64::from(height)).clamp(0, self.height as i64) as usize,
            ),
            None => (0, 0, self.width, self.height),
        };
        for y in bottom..top {
            for x in left..right {
                let index = y * self.width + x;
                if let Some((buffer, rgba)) = clear.color {
                    let pixel = &mut self.color_mut(buffer).expect("buffer checked above")[index];
                    for component in 0..4 {
                        if clear.color_mask[component] {
                            pixel[component] = rgba[component];
                        }
                    }
                }
                if let Some(depth) = clear.depth {
                    if clear.depth_mask {
                        self.depth[index] = depth.clamp(0.0, 1.0);
                    }
                }
                if let Some(stencil) = clear.stencil {
                    let old = self.stencil[index];
                    self.stencil[index] =
                        (old & !clear.stencil_mask) | (stencil & clear.stencil_mask);
                }
            }
        }
        true
    }

    pub fn depth_at(&self, x: u32, y: u32) -> Option<f32> {
        self.depth.get(self.index(x, y)?).copied()
    }

    pub fn stencil_at(&self, x: u32, y: u32) -> Option<u8> {
        self.stencil.get(self.index(x, y)?).copied()
    }

    pub fn pixel(&self, buffer: ClassicGlColorBuffer, x: u32, y: u32) -> Option<[u8; 4]> {
        self.color(buffer)?.get(self.index(x, y)?).copied()
    }

    pub fn set_pixel(
        &mut self,
        buffer: ClassicGlColorBuffer,
        x: u32,
        y: u32,
        rgba: [u8; 4],
    ) -> bool {
        let Some(index) = self.index(x, y) else {
            return false;
        };
        let Some(pixel) = self
            .color_mut(buffer)
            .and_then(|pixels| pixels.get_mut(index))
        else {
            return false;
        };
        *pixel = rgba;
        true
    }

    /// Copies the entire pixel rectangle in OpenGL's bottom-up row order.
    pub fn read_rgba(
        &self,
        buffer: ClassicGlColorBuffer,
        x: u32,
        y: u32,
        width: u32,
        height: u32,
    ) -> Option<Vec<u8>> {
        let end_x = x.checked_add(width)?;
        let end_y = y.checked_add(height)?;
        if usize::try_from(end_x).ok()? > self.width || usize::try_from(end_y).ok()? > self.height {
            return None;
        }
        let len = usize::try_from(width.checked_mul(height)?.checked_mul(4)?).ok()?;
        let mut result = Vec::with_capacity(len);
        for row in y..end_y {
            for col in x..end_x {
                result.extend_from_slice(&self.pixel(buffer, col, row)?);
            }
        }
        Some(result)
    }

    /// Presents a completed color buffer to a 16-bit QuickDraw RGB555 surface.
    /// The guest rows are top-down and big-endian.
    pub fn present_rgb555(
        &self,
        buffer: ClassicGlColorBuffer,
        memory: &mut PpcSectionMem,
        surface: PpcFrontBuffer,
    ) -> bool {
        if surface.depth != 16
            || usize::try_from(surface.width).ok() != Some(self.width)
            || usize::try_from(surface.height).ok() != Some(self.height)
            || surface.row_bytes < surface.width.saturating_mul(2)
        {
            return false;
        }
        let Some(pixels) = self.color(buffer) else {
            return false;
        };
        let mut row = vec![0; self.width * 2];
        for guest_y in 0..self.height {
            let source_y = self.height - guest_y - 1;
            for x in 0..self.width {
                let [r, g, b, _] = pixels[source_y * self.width + x];
                let rgb555 =
                    (u16::from(r >> 3) << 10) | (u16::from(g >> 3) << 5) | u16::from(b >> 3);
                row[x * 2..x * 2 + 2].copy_from_slice(&rgb555.to_be_bytes());
            }
            let Some(address) = u32::try_from(guest_y)
                .ok()
                .and_then(|y| y.checked_mul(surface.row_bytes))
                .and_then(|offset| surface.base_addr.checked_add(offset))
            else {
                return false;
            };
            if memory.write_bytes(address, &row).is_none() {
                return false;
            }
        }
        true
    }

    /// Mutable views of one color buffer with the shared depth and stencil
    /// planes, for the rasterizer.
    pub fn raster_target(
        &mut self,
        buffer: ClassicGlColorBuffer,
    ) -> Option<super::classic_gl::RasterTarget<'_>> {
        let (width, height) = (self.width, self.height);
        let color = match buffer {
            ClassicGlColorBuffer::Front => &mut self.front,
            ClassicGlColorBuffer::Back => self.back.as_mut()?,
        };
        Some(super::classic_gl::RasterTarget {
            width,
            height,
            color,
            depth: &mut self.depth,
            stencil: &mut self.stencil,
        })
    }

    /// Presents a completed color buffer to a 16-bit RGB555 or 32-bit
    /// xRGB8888 QuickDraw surface of the same size.
    pub fn present(
        &self,
        buffer: ClassicGlColorBuffer,
        memory: &mut PpcSectionMem,
        surface: PpcFrontBuffer,
    ) -> bool {
        match surface.depth {
            16 => self.present_rgb555(buffer, memory, surface),
            32 => self.present_xrgb8888(buffer, memory, surface),
            _ => false,
        }
    }

    fn present_xrgb8888(
        &self,
        buffer: ClassicGlColorBuffer,
        memory: &mut PpcSectionMem,
        surface: PpcFrontBuffer,
    ) -> bool {
        if usize::try_from(surface.width).ok() != Some(self.width)
            || usize::try_from(surface.height).ok() != Some(self.height)
            || surface.row_bytes < surface.width.saturating_mul(4)
        {
            return false;
        }
        let Some(pixels) = self.color(buffer) else {
            return false;
        };
        let mut row = vec![0; self.width * 4];
        for guest_y in 0..self.height {
            let source_y = self.height - guest_y - 1;
            for x in 0..self.width {
                let [r, g, b, _] = pixels[source_y * self.width + x];
                row[x * 4..x * 4 + 4].copy_from_slice(&[0, r, g, b]);
            }
            let Some(address) = u32::try_from(guest_y)
                .ok()
                .and_then(|y| y.checked_mul(surface.row_bytes))
                .and_then(|offset| surface.base_addr.checked_add(offset))
            else {
                return false;
            };
            if memory.write_bytes(address, &row).is_none() {
                return false;
            }
        }
        true
    }

    pub fn swap(&mut self) -> bool {
        let Some(back) = self.back.as_mut() else {
            return false;
        };
        std::mem::swap(&mut self.front, back);
        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ppc::PpcMemory;

    #[test]
    fn back_buffer_clear_readback_and_swap() {
        let mut frame = ClassicGlFramebuffer::new(2, 2, true).unwrap();
        assert!(frame.clear_color(ClassicGlColorBuffer::Back, [255, 32, 0, 255]));
        assert_eq!(
            frame.read_rgba(ClassicGlColorBuffer::Back, 0, 0, 2, 1),
            Some(vec![255, 32, 0, 255, 255, 32, 0, 255])
        );
        assert_eq!(frame.pixel(ClassicGlColorBuffer::Front, 0, 0), Some([0; 4]));
        assert!(frame.swap());
        assert_eq!(
            frame.pixel(ClassicGlColorBuffer::Front, 0, 0),
            Some([255, 32, 0, 255])
        );
    }

    #[test]
    fn clear_respects_scissor_and_write_masks() {
        let mut frame = ClassicGlFramebuffer::new(3, 2, true).unwrap();
        assert!(frame.clear_color(ClassicGlColorBuffer::Back, [10, 20, 30, 40]));
        assert!(frame.clear(ClassicGlClear {
            color: Some((ClassicGlColorBuffer::Back, [1, 2, 3, 4])),
            color_mask: [true, false, true, false],
            depth: Some(0.25),
            depth_mask: true,
            stencil: Some(0xf0),
            stencil_mask: 0x0f,
            scissor: Some((1, 0, 1, 1)),
        }));
        assert_eq!(
            frame.pixel(ClassicGlColorBuffer::Back, 1, 0),
            Some([1, 20, 3, 40])
        );
        assert_eq!(
            frame.pixel(ClassicGlColorBuffer::Back, 0, 0),
            Some([10, 20, 30, 40])
        );
        assert_eq!(
            frame.pixel(ClassicGlColorBuffer::Back, 1, 1),
            Some([10, 20, 30, 40])
        );
        assert_eq!(frame.depth_at(1, 0), Some(0.25));
        assert_eq!(frame.depth_at(1, 1), Some(1.0));
        assert_eq!(frame.stencil_at(1, 0), Some(0));
        assert!(frame.clear(ClassicGlClear {
            color: None,
            color_mask: [false; 4],
            depth: Some(0.5),
            depth_mask: false,
            stencil: Some(0x0a),
            stencil_mask: 0x0f,
            scissor: Some((-1, 0, 2, 1)),
        }));
        assert_eq!(frame.depth_at(0, 0), Some(1.0));
        assert_eq!(frame.stencil_at(0, 0), Some(0x0a));
        assert_eq!(frame.stencil_at(1, 0), Some(0));
    }

    #[test]
    fn presenting_flips_rows_and_writes_big_endian_rgb555() {
        let mut frame = ClassicGlFramebuffer::new(1, 2, false).unwrap();
        assert!(frame.set_pixel(ClassicGlColorBuffer::Front, 0, 0, [255, 0, 0, 255]));
        assert!(frame.set_pixel(ClassicGlColorBuffer::Front, 0, 1, [0, 255, 0, 255]));
        let mut memory = PpcSectionMem::new();
        memory.add_region(0x1000, vec![0; 4]);
        let surface = PpcFrontBuffer {
            base_addr: 0x1000,
            row_bytes: 2,
            width: 1,
            height: 2,
            depth: 16,
        };
        assert!(frame.present_rgb555(ClassicGlColorBuffer::Front, &mut memory, surface));
        assert_eq!(memory.read_u16_be(0x1000), Some(0x03e0));
        assert_eq!(memory.read_u16_be(0x1002), Some(0x7c00));
    }

    #[test]
    fn readback_uses_lower_left_origin_and_checks_bounds() {
        let mut frame = ClassicGlFramebuffer::new(2, 2, false).unwrap();
        assert!(frame.set_pixel(ClassicGlColorBuffer::Front, 0, 0, [1, 2, 3, 4]));
        assert!(frame.set_pixel(ClassicGlColorBuffer::Front, 0, 1, [5, 6, 7, 8]));
        assert_eq!(
            frame.read_rgba(ClassicGlColorBuffer::Front, 0, 0, 1, 2),
            Some(vec![1, 2, 3, 4, 5, 6, 7, 8])
        );
        assert_eq!(
            frame.read_rgba(ClassicGlColorBuffer::Front, 1, 1, 2, 1),
            None
        );
        assert!(!frame.swap());
        assert!(ClassicGlFramebuffer::new(4097, 4097, true).is_none());
    }
}