gpu: Refactor Gpu::bg to Gpu::background

Former-commit-id: 78aa4ad525ba9df0a4944dfac0c5723e35633390
This commit is contained in:
Michel Heily 2020-02-16 01:40:59 +02:00
parent aeab3a82a3
commit 392ad1bd71
6 changed files with 46 additions and 46 deletions

View file

@ -166,7 +166,7 @@ pub struct Gpu {
pub dispcnt: DisplayControl, pub dispcnt: DisplayControl,
pub dispstat: DisplayStatus, pub dispstat: DisplayStatus,
pub bg: [Background; 4], pub backgrounds: [Background; 4],
pub bg_aff: [BgAffine; 2], pub bg_aff: [BgAffine; 2],
pub win0: Window, pub win0: Window,
@ -195,7 +195,7 @@ impl Gpu {
Gpu { Gpu {
dispcnt: DisplayControl(0x80), dispcnt: DisplayControl(0x80),
dispstat: DisplayStatus(0), dispstat: DisplayStatus(0),
bg: [ backgrounds: [
Background::default(), Background::default(),
Background::default(), Background::default(),
Background::default(), Background::default(),

View file

@ -13,14 +13,14 @@ impl Gpu {
let vsize = (self.mosaic.bg_vsize() + 1) as usize; let vsize = (self.mosaic.bg_vsize() + 1) as usize;
for bg in 0..4 { for bg in 0..4 {
if self.dispcnt.enable_bg(bg) && self.bg[bg].bgcnt.mosaic() { if self.dispcnt.enable_bg(bg) && self.backgrounds[bg].bgcnt.mosaic() {
let y = self.vcount as usize; let y = self.vcount as usize;
if y % vsize == 0 { if y % vsize == 0 {
self.bg[bg].mosaic_first_row = self.bg[bg].line.clone(); self.backgrounds[bg].mosaic_first_row = self.backgrounds[bg].line.clone();
} }
for x in 0..DISPLAY_WIDTH { for x in 0..DISPLAY_WIDTH {
let color = self.bg[bg].mosaic_first_row[(x / hsize) * hsize]; let color = self.backgrounds[bg].mosaic_first_row[(x / hsize) * hsize];
self.bg[bg].line[x] = color; self.backgrounds[bg].line[x] = color;
} }
} }
} }

View file

@ -19,14 +19,14 @@ impl Gpu {
for x in 0..DISPLAY_WIDTH { for x in 0..DISPLAY_WIDTH {
let t = utils::transform_bg_point(ref_point, x as i32, pa, pc); let t = utils::transform_bg_point(ref_point, x as i32, pa, pc);
if !SCREEN_VIEWPORT.contains_point(t) { if !SCREEN_VIEWPORT.contains_point(t) {
self.bg[bg].line[x] = Rgb15::TRANSPARENT; self.backgrounds[bg].line[x] = Rgb15::TRANSPARENT;
continue; continue;
} }
let pixel_index = index2d!(u32, t.0, t.1, DISPLAY_WIDTH); let pixel_index = index2d!(u32, t.0, t.1, DISPLAY_WIDTH);
let pixel_ofs = 2 * pixel_index; let pixel_ofs = 2 * pixel_index;
let color = Rgb15(self.vram.read_16(pixel_ofs)); let color = Rgb15(self.vram.read_16(pixel_ofs));
self.bg[bg].line[x] = color; self.backgrounds[bg].line[x] = color;
} }
} }
@ -46,14 +46,14 @@ impl Gpu {
for x in 0..DISPLAY_WIDTH { for x in 0..DISPLAY_WIDTH {
let t = utils::transform_bg_point(ref_point, x as i32, pa, pc); let t = utils::transform_bg_point(ref_point, x as i32, pa, pc);
if !SCREEN_VIEWPORT.contains_point(t) { if !SCREEN_VIEWPORT.contains_point(t) {
self.bg[bg].line[x] = Rgb15::TRANSPARENT; self.backgrounds[bg].line[x] = Rgb15::TRANSPARENT;
continue; continue;
} }
let bitmap_index = index2d!(u32, t.0, t.1, DISPLAY_WIDTH); let bitmap_index = index2d!(u32, t.0, t.1, DISPLAY_WIDTH);
let bitmap_ofs = page_ofs + (bitmap_index as u32); let bitmap_ofs = page_ofs + (bitmap_index as u32);
let index = self.vram.read_8(bitmap_ofs) as u32; let index = self.vram.read_8(bitmap_ofs) as u32;
let color = self.get_palette_color(index, 0, 0); let color = self.get_palette_color(index, 0, 0);
self.bg[bg].line[x] = color; self.backgrounds[bg].line[x] = color;
} }
} }
} }

View file

@ -9,12 +9,12 @@ use crate::core::Bus;
impl Gpu { impl Gpu {
pub(in super::super) fn render_reg_bg(&mut self, bg: usize) { pub(in super::super) fn render_reg_bg(&mut self, bg: usize) {
let (h_ofs, v_ofs) = (self.bg[bg].bghofs as u32, self.bg[bg].bgvofs as u32); let (h_ofs, v_ofs) = (self.backgrounds[bg].bghofs as u32, self.backgrounds[bg].bgvofs as u32);
let tileset_base = self.bg[bg].bgcnt.char_block(); let tileset_base = self.backgrounds[bg].bgcnt.char_block();
let tilemap_base = self.bg[bg].bgcnt.screen_block(); let tilemap_base = self.backgrounds[bg].bgcnt.screen_block();
let (tile_size, pixel_format) = self.bg[bg].bgcnt.tile_format(); let (tile_size, pixel_format) = self.backgrounds[bg].bgcnt.tile_format();
let (bg_width, bg_height) = self.bg[bg].bgcnt.size_regular(); let (bg_width, bg_height) = self.backgrounds[bg].bgcnt.size_regular();
let screen_y = self.vcount as u32; let screen_y = self.vcount as u32;
let mut screen_x = 0; let mut screen_x = 0;
@ -64,7 +64,7 @@ impl Gpu {
PixelFormat::BPP8 => 0u32, PixelFormat::BPP8 => 0u32,
}; };
let color = self.get_palette_color(index as u32, palette_bank, 0); let color = self.get_palette_color(index as u32, palette_bank, 0);
self.bg[bg].line[screen_x as usize] = color; self.backgrounds[bg].line[screen_x as usize] = color;
screen_x += 1; screen_x += 1;
if (DISPLAY_WIDTH as u32) == screen_x { if (DISPLAY_WIDTH as u32) == screen_x {
return; return;
@ -83,17 +83,17 @@ impl Gpu {
pub(in super::super) fn render_aff_bg(&mut self, bg: usize) { pub(in super::super) fn render_aff_bg(&mut self, bg: usize) {
assert!(bg == 2 || bg == 3); assert!(bg == 2 || bg == 3);
let texture_size = 128 << self.bg[bg].bgcnt.bg_size(); let texture_size = 128 << self.backgrounds[bg].bgcnt.bg_size();
let viewport = ViewPort::new(texture_size, texture_size); let viewport = ViewPort::new(texture_size, texture_size);
let ref_point = self.get_ref_point(bg); let ref_point = self.get_ref_point(bg);
let pa = self.bg_aff[bg - 2].pa as i16 as i32; let pa = self.bg_aff[bg - 2].pa as i16 as i32;
let pc = self.bg_aff[bg - 2].pc as i16 as i32; let pc = self.bg_aff[bg - 2].pc as i16 as i32;
let screen_block = self.bg[bg].bgcnt.screen_block(); let screen_block = self.backgrounds[bg].bgcnt.screen_block();
let char_block = self.bg[bg].bgcnt.char_block(); let char_block = self.backgrounds[bg].bgcnt.char_block();
let wraparound = self.bg[bg].bgcnt.affine_wraparound(); let wraparound = self.backgrounds[bg].bgcnt.affine_wraparound();
for screen_x in 0..(DISPLAY_WIDTH as i32) { for screen_x in 0..(DISPLAY_WIDTH as i32) {
let mut t = utils::transform_bg_point(ref_point, screen_x, pa, pc); let mut t = utils::transform_bg_point(ref_point, screen_x, pa, pc);
@ -103,7 +103,7 @@ impl Gpu {
t.0 = t.0.rem_euclid(texture_size); t.0 = t.0.rem_euclid(texture_size);
t.1 = t.1.rem_euclid(texture_size); t.1 = t.1.rem_euclid(texture_size);
} else { } else {
self.bg[bg].line[screen_x as usize] = Rgb15::TRANSPARENT; self.backgrounds[bg].line[screen_x as usize] = Rgb15::TRANSPARENT;
continue; continue;
} }
} }
@ -121,7 +121,7 @@ impl Gpu {
0, 0,
0, 0,
); );
self.bg[bg].line[screen_x as usize] = color; self.backgrounds[bg].line[screen_x as usize] = color;
} }
} }
} }

View file

@ -54,7 +54,7 @@ impl Gpu {
} }
} }
} }
backgrounds.sort_by_key(|bg| (self.bg[*bg].bgcnt.priority(), *bg)); backgrounds.sort_by_key(|bg| (self.backgrounds[*bg].bgcnt.priority(), *bg));
backgrounds backgrounds
} }
@ -62,10 +62,10 @@ impl Gpu {
#[allow(unused)] #[allow(unused)]
fn layer_to_pixel(&self, x: usize, y: usize, layer: &RenderLayer) -> Rgb15 { fn layer_to_pixel(&self, x: usize, y: usize, layer: &RenderLayer) -> Rgb15 {
match layer.kind { match layer.kind {
RenderLayerKind::Background0 => self.bg[0].line[x], RenderLayerKind::Background0 => self.backgrounds[0].line[x],
RenderLayerKind::Background1 => self.bg[1].line[x], RenderLayerKind::Background1 => self.backgrounds[1].line[x],
RenderLayerKind::Background2 => self.bg[2].line[x], RenderLayerKind::Background2 => self.backgrounds[2].line[x],
RenderLayerKind::Background3 => self.bg[3].line[x], RenderLayerKind::Background3 => self.backgrounds[3].line[x],
RenderLayerKind::Objects => self.obj_buffer_get(x, y).color, RenderLayerKind::Objects => self.obj_buffer_get(x, y).color,
RenderLayerKind::Backdrop => Rgb15(self.palette_ram.read_16(0)), RenderLayerKind::Backdrop => Rgb15(self.palette_ram.read_16(0)),
} }
@ -164,14 +164,14 @@ impl Gpu {
layers.push_unchecked(RenderLayer::backdrop(backdrop_color)); layers.push_unchecked(RenderLayer::backdrop(backdrop_color));
} }
for bg in backgrounds.into_iter() { for bg in backgrounds.iter() {
let bg_pixel = self.bg[*bg].line[x]; let bg_pixel = self.backgrounds[*bg].line[x];
if !bg_pixel.is_transparent() { if !bg_pixel.is_transparent() {
unsafe { unsafe {
layers.push_unchecked(RenderLayer::background( layers.push_unchecked(RenderLayer::background(
*bg, *bg,
bg_pixel, bg_pixel,
self.bg[*bg].bgcnt.priority(), self.backgrounds[*bg].bgcnt.priority(),
)); ));
} }
} }

View file

@ -58,10 +58,10 @@ impl Bus for IoDevices {
REG_DISPCNT => io.gpu.dispcnt.0, REG_DISPCNT => io.gpu.dispcnt.0,
REG_DISPSTAT => io.gpu.dispstat.0, REG_DISPSTAT => io.gpu.dispstat.0,
REG_VCOUNT => io.gpu.vcount as u16, REG_VCOUNT => io.gpu.vcount as u16,
REG_BG0CNT => io.gpu.bg[0].bgcnt.0, REG_BG0CNT => io.gpu.backgrounds[0].bgcnt.0,
REG_BG1CNT => io.gpu.bg[1].bgcnt.0, REG_BG1CNT => io.gpu.backgrounds[1].bgcnt.0,
REG_BG2CNT => io.gpu.bg[2].bgcnt.0, REG_BG2CNT => io.gpu.backgrounds[2].bgcnt.0,
REG_BG3CNT => io.gpu.bg[3].bgcnt.0, REG_BG3CNT => io.gpu.backgrounds[3].bgcnt.0,
REG_WIN0H => ((io.gpu.win0.left as u16) << 8 | (io.gpu.win0.right as u16)), REG_WIN0H => ((io.gpu.win0.left as u16) << 8 | (io.gpu.win0.right as u16)),
REG_WIN1H => ((io.gpu.win1.left as u16) << 8 | (io.gpu.win1.right as u16)), REG_WIN1H => ((io.gpu.win1.left as u16) << 8 | (io.gpu.win1.right as u16)),
REG_WIN0V => ((io.gpu.win0.top as u16) << 8 | (io.gpu.win0.bottom as u16)), REG_WIN0V => ((io.gpu.win0.top as u16) << 8 | (io.gpu.win0.bottom as u16)),
@ -122,18 +122,18 @@ impl Bus for IoDevices {
match io_addr { match io_addr {
REG_DISPCNT => io.gpu.dispcnt.0 = value, REG_DISPCNT => io.gpu.dispcnt.0 = value,
REG_DISPSTAT => io.gpu.dispstat.0 |= value & !3, REG_DISPSTAT => io.gpu.dispstat.0 |= value & !3,
REG_BG0CNT => io.gpu.bg[0].bgcnt.0 = value, REG_BG0CNT => io.gpu.backgrounds[0].bgcnt.0 = value,
REG_BG1CNT => io.gpu.bg[1].bgcnt.0 = value, REG_BG1CNT => io.gpu.backgrounds[1].bgcnt.0 = value,
REG_BG2CNT => io.gpu.bg[2].bgcnt.0 = value, REG_BG2CNT => io.gpu.backgrounds[2].bgcnt.0 = value,
REG_BG3CNT => io.gpu.bg[3].bgcnt.0 = value, REG_BG3CNT => io.gpu.backgrounds[3].bgcnt.0 = value,
REG_BG0HOFS => io.gpu.bg[0].bghofs = value & 0x1ff, REG_BG0HOFS => io.gpu.backgrounds[0].bghofs = value & 0x1ff,
REG_BG0VOFS => io.gpu.bg[0].bgvofs = value & 0x1ff, REG_BG0VOFS => io.gpu.backgrounds[0].bgvofs = value & 0x1ff,
REG_BG1HOFS => io.gpu.bg[1].bghofs = value & 0x1ff, REG_BG1HOFS => io.gpu.backgrounds[1].bghofs = value & 0x1ff,
REG_BG1VOFS => io.gpu.bg[1].bgvofs = value & 0x1ff, REG_BG1VOFS => io.gpu.backgrounds[1].bgvofs = value & 0x1ff,
REG_BG2HOFS => io.gpu.bg[2].bghofs = value & 0x1ff, REG_BG2HOFS => io.gpu.backgrounds[2].bghofs = value & 0x1ff,
REG_BG2VOFS => io.gpu.bg[2].bgvofs = value & 0x1ff, REG_BG2VOFS => io.gpu.backgrounds[2].bgvofs = value & 0x1ff,
REG_BG3HOFS => io.gpu.bg[3].bghofs = value & 0x1ff, REG_BG3HOFS => io.gpu.backgrounds[3].bghofs = value & 0x1ff,
REG_BG3VOFS => io.gpu.bg[3].bgvofs = value & 0x1ff, REG_BG3VOFS => io.gpu.backgrounds[3].bgvofs = value & 0x1ff,
REG_BG2X_L | REG_BG3X_L => { REG_BG2X_L | REG_BG3X_L => {
let index = (io_addr - REG_BG2X_L) / 0x10; let index = (io_addr - REG_BG2X_L) / 0x10;
let t = io.gpu.bg_aff[index as usize].x as u32; let t = io.gpu.bg_aff[index as usize].x as u32;