Merge pull request #199 from gwilymk/small-improvements-while-working-on-bad-apple-demo

Small improvements while working on bad apple demo
This commit is contained in:
Gwilym Kuiper 2022-03-21 23:56:35 +00:00 committed by GitHub
commit 2ea8ec6eac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 49 deletions

View file

@ -1,6 +1,6 @@
use crate::image_loader::Image;
use crate::palette16::Palette16OptimisationResults; use crate::palette16::Palette16OptimisationResults;
use crate::TileSize; use crate::TileSize;
use crate::{image_loader::Image, ByteString};
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::{format_ident, quote}; use quote::{format_ident, quote};
@ -49,7 +49,7 @@ pub(crate) fn generate_code(
for inner_y in 0..tile_size / 8 { for inner_y in 0..tile_size / 8 {
for inner_x in 0..tile_size / 8 { for inner_x in 0..tile_size / 8 {
for j in inner_y * 8..inner_y * 8 + 8 { for j in inner_y * 8..inner_y * 8 + 8 {
for i in (inner_x * 8..inner_x * 8 + 8).rev() { for i in inner_x * 8..inner_x * 8 + 8 {
let colour = image.colour(x * tile_size + i, y * tile_size + j); let colour = image.colour(x * tile_size + i, y * tile_size + j);
tile_data.push(palette.colour_index(colour)); tile_data.push(palette.colour_index(colour));
} }
@ -59,8 +59,12 @@ pub(crate) fn generate_code(
} }
} }
let tile_data = tile_data.chunks(8) let tile_data: Vec<_> = tile_data
.map(|chunk| chunk.iter().fold(0u32, |acc, &x| (acc << 4) | (x as u32))); .chunks(2)
.map(|chunk| (chunk[1] << 4) | chunk[0])
.collect();
let data = ByteString(&tile_data);
let assignments = results.assignments.iter().map(|&x| x as u8); let assignments = results.assignments.iter().map(|&x| x as u8);
@ -73,9 +77,7 @@ pub(crate) fn generate_code(
#(#palette_data),* #(#palette_data),*
]; ];
const TILE_DATA: &[u32] = &[ const TILE_DATA: &[u8] = #data;
#(#tile_data),*
];
const PALETTE_ASSIGNMENT: &[u8] = &[ const PALETTE_ASSIGNMENT: &[u8] = &[
#(#assignments),* #(#assignments),*

View file

@ -311,24 +311,41 @@ static ChickenSprites: &[Sprite] = &[
), ),
]; ];
static MAP_TILES: [u32; 8 * 17] = [ static MAP_TILES: [u8; 8 * 17 * 4] = [
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x01111111, 0x01111111, 0x01111111, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x00000000, 0x01010101, 0x01010101, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x00000000, 0x00000000, 0x11110000, 0x11100000, 0x11000100, 0x10001100, 0x00011100, 0x00111100, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x00000000, 0x00000000, 0x01110001, 0x01100011, 0x01000111, 0x00001111, 0x00011111, 0x00111111, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x01, 0x11, 0x11, 0x11, 0x01, 0x11, 0x11, 0x11, 0x01,
0x00111111, 0x00111111, 0x00111111, 0x00111111, 0x00111111, 0x00111111, 0x00001111, 0x00000111, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x00000000, 0x00000000, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11, 0x11, 0x11, 0x11, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x10111111, 0x01101011, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x00, 0x00, 0x10, 0x11,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x01101011, 0x00, 0x01, 0x00, 0x11, 0x00, 0x11, 0x00, 0x10, 0x00, 0x11, 0x01, 0x00, 0x00, 0x11, 0x11, 0x00,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11101111, 0x10110111, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x01, 0x11, 0x00, 0x10, 0x01,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11101111, 0x11, 0x01, 0x00, 0x01, 0x11, 0x11, 0x00, 0x00, 0x11, 0x11, 0x01, 0x00, 0x11, 0x11, 0x11, 0x00,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111101, 0x11111011, 0x10111011, 0x11, 0x11, 0x11, 0x00, 0x11, 0x11, 0x11, 0x00, 0x11, 0x11, 0x11, 0x00, 0x11, 0x11, 0x11, 0x00,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x10111011, 0x11, 0x11, 0x11, 0x00, 0x11, 0x11, 0x11, 0x00, 0x11, 0x11, 0x00, 0x00, 0x11, 0x01, 0x00, 0x00,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11101111, 0x11101011, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11011111, 0x11010111, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x01111111, 0x11010111, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x10, 0x11, 0x10, 0x10, 0x01,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x10, 0x10, 0x01,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x10, 0x11, 0x11, 0x01, 0x11, 0x10,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x10, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x01, 0x11, 0x11, 0x11, 0x11, 0x10, 0x11, 0x11, 0x11, 0x10, 0x11, 0x10,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x10, 0x11, 0x10,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x10, 0x11, 0x11, 0x10, 0x10, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x01, 0x11, 0x11, 0x01, 0x01, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x01, 0x11, 0x01, 0x01, 0x11,
]; ];
static MAP_MAP: [u16; 1024] = [ static MAP_MAP: [u16; 1024] = [

View file

@ -157,6 +157,11 @@ impl BlockAllocator {
} }
pub unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { pub unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.dealloc_no_normalise(ptr, layout);
self.normalise();
}
pub unsafe fn dealloc_no_normalise(&self, ptr: *mut u8, layout: Layout) {
let new_layout = Block::either_layout(layout).pad_to_align(); let new_layout = Block::either_layout(layout).pad_to_align();
free(|key| { free(|key| {
let mut state = self.state.borrow(*key).borrow_mut(); let mut state = self.state.borrow(*key).borrow_mut();
@ -195,7 +200,6 @@ impl BlockAllocator {
} }
} }
}); });
self.normalise();
} }
} }

View file

@ -2,14 +2,14 @@ use crate::display::palette16::Palette16;
pub struct TileData { pub struct TileData {
pub palettes: &'static [Palette16], pub palettes: &'static [Palette16],
pub tiles: &'static [u32], pub tiles: &'static [u8],
pub palette_assignments: &'static [u8], pub palette_assignments: &'static [u8],
} }
impl TileData { impl TileData {
pub const fn new( pub const fn new(
palettes: &'static [Palette16], palettes: &'static [Palette16],
tiles: &'static [u32], tiles: &'static [u8],
palette_assignments: &'static [u8], palette_assignments: &'static [u8],
) -> Self { ) -> Self {
TileData { TileData {

View file

@ -1,12 +1,12 @@
use core::{alloc::Layout, ptr::NonNull}; use core::{alloc::Layout, ptr::NonNull};
use alloc::vec;
use alloc::vec::Vec; use alloc::vec::Vec;
use crate::{ use crate::{
agb_alloc::{block_allocator::BlockAllocator, bump_allocator::StartEnd}, agb_alloc::{block_allocator::BlockAllocator, bump_allocator::StartEnd},
display::palette16, display::palette16,
dma::dma_copy16, dma::dma_copy16,
hash_map::HashMap,
memory_mapped::MemoryMapped1DArray, memory_mapped::MemoryMapped1DArray,
}; };
@ -51,12 +51,12 @@ impl TileFormat {
} }
pub struct TileSet<'a> { pub struct TileSet<'a> {
tiles: &'a [u32], tiles: &'a [u8],
format: TileFormat, format: TileFormat,
} }
impl<'a> TileSet<'a> { impl<'a> TileSet<'a> {
pub fn new(tiles: &'a [u32], format: TileFormat) -> Self { pub fn new(tiles: &'a [u8], format: TileFormat) -> Self {
Self { tiles, format } Self { tiles, format }
} }
@ -104,18 +104,20 @@ pub struct VRamManager<'a> {
generation: u16, generation: u16,
free_pointer: Option<usize>, free_pointer: Option<usize>,
tile_set_to_vram: Vec<Vec<Option<TileReference>>>, tile_set_to_vram: HashMap<(u16, u16), TileReference>,
reference_counts: Vec<(u16, Option<(TileSetReference, u16)>)>, reference_counts: Vec<(u16, Option<(TileSetReference, u16)>)>,
} }
impl<'a> VRamManager<'a> { impl<'a> VRamManager<'a> {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
let tile_set_to_vram: HashMap<(u16, u16), TileReference> = HashMap::with_capacity(256);
Self { Self {
tilesets: Vec::new(), tilesets: Vec::new(),
generation: 0, generation: 0,
free_pointer: None, free_pointer: None,
tile_set_to_vram: Default::default(), tile_set_to_vram,
reference_counts: Default::default(), reference_counts: Default::default(),
} }
} }
@ -124,7 +126,6 @@ impl<'a> VRamManager<'a> {
let generation = self.generation; let generation = self.generation;
self.generation = self.generation.wrapping_add(1); self.generation = self.generation.wrapping_add(1);
let num_tiles = tileset.num_tiles();
let tileset = ArenaStorageItem::Data(tileset, generation); let tileset = ArenaStorageItem::Data(tileset, generation);
let index = if let Some(ptr) = self.free_pointer.take() { let index = if let Some(ptr) = self.free_pointer.take() {
@ -145,10 +146,6 @@ impl<'a> VRamManager<'a> {
self.tilesets.len() - 1 self.tilesets.len() - 1
}; };
self.tile_set_to_vram
.resize(self.tilesets.len(), Default::default());
self.tile_set_to_vram[index] = vec![Default::default(); num_tiles];
TileSetReference::new(index as u16, generation) TileSetReference::new(index as u16, generation)
} }
@ -185,10 +182,10 @@ impl<'a> VRamManager<'a> {
} }
pub(crate) fn add_tile(&mut self, tile_set_ref: TileSetReference, tile: u16) -> TileIndex { pub(crate) fn add_tile(&mut self, tile_set_ref: TileSetReference, tile: u16) -> TileIndex {
let reference = self.tile_set_to_vram[tile_set_ref.id as usize][tile as usize]; let reference = self.tile_set_to_vram.get(&(tile_set_ref.id, tile));
if let Some(reference) = reference { if let Some(reference) = reference {
let index = Self::index_from_reference(reference); let index = Self::index_from_reference(*reference);
self.reference_counts[index].0 += 1; self.reference_counts[index].0 += 1;
return TileIndex::new(index); return TileIndex::new(index);
} }
@ -204,13 +201,13 @@ impl<'a> VRamManager<'a> {
"Stale tile data requested" "Stale tile data requested"
); );
let tile_offset = (tile as usize) * data.format.tile_size() / 4; let tile_offset = (tile as usize) * data.format.tile_size();
&data.tiles[tile_offset..(tile_offset + data.format.tile_size() / 4)] &data.tiles[tile_offset..(tile_offset + data.format.tile_size())]
} else { } else {
panic!("Tile set ref must point to existing tile set"); panic!("Tile set ref must point to existing tile set");
}; };
let tile_size_in_half_words = TileFormat::FourBpp.tile_size() / 2; let tile_size_in_half_words = tile_slice.len() / 2;
unsafe { unsafe {
dma_copy16( dma_copy16(
@ -224,7 +221,8 @@ impl<'a> VRamManager<'a> {
let index = Self::index_from_reference(tile_reference); let index = Self::index_from_reference(tile_reference);
self.tile_set_to_vram[tile_set_ref.id as usize][tile as usize] = Some(tile_reference); self.tile_set_to_vram
.insert((tile_set_ref.id, tile), tile_reference);
self.reference_counts self.reference_counts
.resize(self.reference_counts.len().max(index + 1), (0, None)); .resize(self.reference_counts.len().max(index + 1), (0, None));
@ -250,11 +248,11 @@ impl<'a> VRamManager<'a> {
let tile_reference = Self::reference_from_index(tile_index); let tile_reference = Self::reference_from_index(tile_index);
unsafe { unsafe {
TILE_ALLOCATOR.dealloc(tile_reference.0.cast().as_ptr(), TILE_LAYOUT); TILE_ALLOCATOR.dealloc_no_normalise(tile_reference.0.cast().as_ptr(), TILE_LAYOUT);
} }
let tile_ref = self.reference_counts[index].1.unwrap(); let tile_ref = self.reference_counts[index].1.unwrap();
self.tile_set_to_vram[tile_ref.0.id as usize][tile_ref.1 as usize] = None; self.tile_set_to_vram.remove(&(tile_ref.0.id, tile_ref.1));
self.reference_counts[index].1 = None; self.reference_counts[index].1 = None;
} }

View file

@ -657,7 +657,7 @@ impl<K, V> NodeStorage<K, V> {
where where
K: Eq, K: Eq,
{ {
for distance_to_initial_bucket in 0..=self.max_distance_to_initial_bucket { for distance_to_initial_bucket in 0..(self.max_distance_to_initial_bucket + 1) {
let location = fast_mod( let location = fast_mod(
self.nodes.len(), self.nodes.len(),
hash + distance_to_initial_bucket as HashType, hash + distance_to_initial_bucket as HashType,