Skip to content

Commit

Permalink
Avoid several stack allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
kelpsyberry committed Feb 7, 2024
1 parent ab22c0e commit aaefca1
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
13 changes: 8 additions & 5 deletions core/src/ds_slot/rom/key1.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use crate::{cpu::arm7, utils::Bytes};

#[derive(Clone)]
#[repr(C)]
pub struct KeyBuffer<const LEVEL_3: bool> {
key_buf: [u32; 0x412],
key_code: [u32; 3],
}

impl<const LEVEL_3: bool> KeyBuffer<LEVEL_3> {
pub fn new<const MODULO: usize>(id_code: u32, arm7_bios: &Bytes<{ arm7::BIOS_SIZE }>) -> Self {
let key_code = [id_code, id_code >> 1, id_code << 1];
let mut key_buf = [0; 0x412];
for (i, word) in key_buf.iter_mut().enumerate() {
pub fn new_boxed<const MODULO: usize>(
id_code: u32,
arm7_bios: &Bytes<{ arm7::BIOS_SIZE }>,
) -> Box<Self> {
let mut result = unsafe { Box::<Self>::new_zeroed().assume_init() };
result.key_code = [id_code, id_code >> 1, id_code << 1];
for (i, word) in result.key_buf.iter_mut().enumerate() {
*word = arm7_bios.read_le(0x30 + (i << 2));
}
let mut result = KeyBuffer { key_buf, key_code };
result.apply_key_code::<MODULO>();
result.apply_key_code::<MODULO>();
result
Expand Down
2 changes: 1 addition & 1 deletion core/src/ds_slot/rom/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Normal {
contents,
rom_mask,
chip_id,
key_buf: arm7_bios.map(|bios| Box::new(key1::KeyBuffer::new::<2>(game_code, bios))),
key_buf: arm7_bios.map(|bios| key1::KeyBuffer::new_boxed::<2>(game_code, bios)),
stage: Stage::Initial,
})
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/gpu/engine_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ impl Engine3d {
gx_status: GxStatus(0),

gx_fifo_irq_requested: false,
gx_fifo: Box::new(Fifo::new()),
gx_fifo: unsafe { Box::new_zeroed().assume_init() },
gx_pipe: Fifo::new(),
cur_packed_commands: 0,
remaining_command_params: 0,
Expand Down Expand Up @@ -507,7 +507,7 @@ impl Engine3d {

vert_ram_level: 0,
poly_ram_level: 0,
vert_ram: Box::new([ScreenVertex::new(); 6144]),
vert_ram: unsafe { Box::new_zeroed().assume_init() },
poly_ram: unsafe { Box::new_zeroed().assume_init() },

rendering_state: RenderingState {
Expand Down
2 changes: 1 addition & 1 deletion render/soft-3d/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ fn depth_test_less_back_facing(a: u32, b: u32, _: PixelAttrs) -> bool {
impl Renderer {
pub fn new() -> Self {
Renderer {
color_buffer: Box::new(Scanline([Color::splat(0); 256])),
color_buffer: unsafe { Box::new_zeroed().assume_init() },
depth_buffer: unsafe { Box::new_zeroed().assume_init() },
attr_buffer: unsafe { Box::new_zeroed().assume_init() },
polys: Vec::with_capacity(2048),
Expand Down
4 changes: 2 additions & 2 deletions render/wgpu-2d/src/common/gfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl GfxThreadChannels {
struct FrameData {
output_3d: Box<[Scanline<u32>; SCREEN_HEIGHT]>,
framebuffer: Box<[[Scanline<BgObjPixel>; SCREEN_HEIGHT]; 2]>,
fb_scanline_flags: [[ScanlineFlags; SCREEN_HEIGHT]; 2],
fb_scanline_flags: Box<[[ScanlineFlags; SCREEN_HEIGHT]; 2]>,
engine_3d_enabled: bool,
frame_index: u64,
}
Expand All @@ -175,7 +175,7 @@ impl Default for FrameData {
FrameData {
output_3d: Box::new_zeroed().assume_init(),
framebuffer: Box::new_zeroed().assume_init(),
fb_scanline_flags: [[ScanlineFlags::default(); SCREEN_HEIGHT]; 2],
fb_scanline_flags: Box::new_zeroed().assume_init(),
engine_3d_enabled: false,
frame_index: 0,
}
Expand Down
4 changes: 2 additions & 2 deletions render/wgpu-2d/src/threaded/lockstep_scanlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ struct ThreadData {

fns: (FnPtrs<EngineA>, FnPtrs<EngineB>),
buffers: [Buffers; 2],
fb_scanline_flags: [[ScanlineFlags; SCREEN_HEIGHT]; 2],
fb_scanline_flags: Box<[[ScanlineFlags; SCREEN_HEIGHT]; 2]>,
gfx_data: GfxData,
}

Expand Down Expand Up @@ -590,7 +590,7 @@ impl ThreadData {

fns: (FnPtrs::new(), FnPtrs::new()),
buffers: [buffers!(), buffers!()],
fb_scanline_flags: [[ScanlineFlags::default(); SCREEN_HEIGHT]; 2],
fb_scanline_flags: unsafe { Box::new_zeroed().assume_init() },
gfx_data,
},
color_output_view,
Expand Down
7 changes: 3 additions & 4 deletions render/wgpu-3d/src/threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use emu_utils::triple_buffer;
use parking_lot::RwLock;
use std::{
cell::UnsafeCell,
hint,
mem::{self, MaybeUninit},
hint, mem,
sync::{
atomic::{AtomicBool, AtomicU64, AtomicU8, Ordering},
Arc,
Expand Down Expand Up @@ -43,7 +42,7 @@ struct FrameData {
pub struct Tx {
shared_data: Arc<SharedData>,
frame_tx: triple_buffer::Sender<FrameData>,
last_gx_data: GxData,
last_gx_data: Box<GxData>,
texture_dirty: [u8; 3],
tex_pal_dirty: [u8; 3],
cur_frame_index: u64,
Expand Down Expand Up @@ -249,7 +248,7 @@ pub fn init(
Tx {
shared_data: Arc::clone(&shared_data),
frame_tx,
last_gx_data: unsafe { MaybeUninit::zeroed().assume_init() },
last_gx_data: unsafe { Box::new_zeroed().assume_init() },
texture_dirty: [0; 3],
tex_pal_dirty: [0; 3],
cur_frame_index: 0,
Expand Down

0 comments on commit aaefca1

Please sign in to comment.