aboutsummaryrefslogtreecommitdiff
path: root/ctru-rs/src/services/gspgpu.rs
blob: e1fa12dc98cee1068e5e6cd365708a61d5619874 (plain) (blame)
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
//! GSPGPU service

use std::convert::From;

#[derive(Copy, Clone, Debug)]
pub enum Event {
    Psc0,
    Psc1,
    VBlank0,
    VBlank1,
    PPF,
    P3D,
    DMA,
}

/// The different framebuffer formats supported by the 3DS
#[derive(Copy, Clone, Debug)]
pub enum FramebufferFormat {
    /// RGBA8. 4 bytes per pixel
    Rgba8,
    /// BGR8. 3 bytes per pixel
    Bgr8,
    /// RGB565. 2 bytes per pixel
    Rgb565,
    /// RGB5A1. 2 bytes per pixel
    Rgb5A1,
    /// RGBA4. 2 bytes per pixel
    Rgba4,
}

impl FramebufferFormat {
    /// Returns the number of bytes per pixel used by this FramebufferFormat
    pub fn pixel_depth_bytes(&self) -> usize {
        use self::FramebufferFormat::*;
        match *self {
            Rgba8 => 4,
            Bgr8 => 3,
            Rgb565 => 2,
            Rgb5A1 => 2,
            Rgba4 => 2,
        }
    }
}

/// Waits for a GSPGPU event to occur.
///
/// `discard_current` determines whether to discard the current event and wait for the next event
pub fn wait_for_event(ev: Event, discard_current: bool) {
    unsafe {
        ::libctru::gspWaitForEvent(ev.into(), discard_current);
    }
}

impl From<::libctru::GSPGPU_FramebufferFormat> for FramebufferFormat {
    fn from(g: ::libctru::GSPGPU_FramebufferFormat) -> Self {
        use self::FramebufferFormat::*;
        match g {
            ::libctru::GSP_RGBA8_OES => Rgba8,
            ::libctru::GSP_BGR8_OES => Bgr8,
            ::libctru::GSP_RGB565_OES => Rgb565,
            ::libctru::GSP_RGB5_A1_OES => Rgb5A1,
            ::libctru::GSP_RGBA4_OES => Rgba4,
            _ => unreachable!(),
        }
    }
}

impl From<FramebufferFormat> for ::libctru::GSPGPU_FramebufferFormat {
    fn from(g: FramebufferFormat) -> Self {
        use self::FramebufferFormat::*;
        match g {
            Rgba8 => ::libctru::GSP_RGBA8_OES,
            Bgr8 => ::libctru::GSP_BGR8_OES,
            Rgb565 => ::libctru::GSP_RGB565_OES,
            Rgb5A1 => ::libctru::GSP_RGB5_A1_OES,
            Rgba4 => ::libctru::GSP_RGBA4_OES,
        }
    }
}

impl From<Event> for ::libctru::GSPGPU_Event {
    fn from(ev: Event) -> Self {
        use self::Event::*;
        match ev {
            Psc0 => ::libctru::GSPGPU_EVENT_PSC0,
            Psc1 => ::libctru::GSPGPU_EVENT_PSC1,
            VBlank0 => ::libctru::GSPGPU_EVENT_VBlank0,
            VBlank1 => ::libctru::GSPGPU_EVENT_VBlank1,
            PPF => ::libctru::GSPGPU_EVENT_PPF,
            P3D => ::libctru::GSPGPU_EVENT_P3D,
            DMA => ::libctru::GSPGPU_EVENT_DMA,
        }
    }
}