implements PLN-0018

This commit is contained in:
2026-04-17 13:24:25 +01:00
parent ed05f337ce
commit 5a0476e8b0
5 changed files with 323 additions and 42 deletions
+91 -39
View File
@@ -71,8 +71,10 @@ pub struct Gfx {
/// Target color for the HUD fade effect.
pub hud_fade_color: Color,
/// Internal cache used to sort sprites into priority groups to optimize rendering.
priority_buckets: [Vec<usize>; 5],
/// Internal sprite count for the current frame state.
sprite_count: usize,
/// Internal cache used to sort sprites by layer while keeping stable priority order.
layer_buckets: [Vec<usize>; 4],
}
const GLYPH_UNKNOWN: [u8; 5] = [0x7, 0x7, 0x7, 0x7, 0x7];
@@ -213,6 +215,9 @@ impl GfxBridge for Gfx {
fn render_scene_from_cache(&mut self, cache: &SceneViewportCache, update: &ResolverUpdate) {
self.render_scene_from_cache(cache, update)
}
fn load_frame_sprites(&mut self, sprites: &[Sprite]) {
self.load_frame_sprites(sprites)
}
fn draw_text(&mut self, x: i32, y: i32, text: &str, color: Color) {
self.draw_text(x, y, text, color)
}
@@ -224,6 +229,7 @@ impl GfxBridge for Gfx {
&self.sprites[index]
}
fn sprite_mut(&mut self, index: usize) -> &mut Sprite {
self.sprite_count = self.sprite_count.max(index.saturating_add(1)).min(self.sprites.len());
&mut self.sprites[index]
}
@@ -262,6 +268,7 @@ impl Gfx {
glyph: EMPTY_GLYPH,
x: 0,
y: 0,
layer: 0,
bank_id: 0,
active: false,
flip_x: false,
@@ -277,12 +284,12 @@ impl Gfx {
back: vec![0; len],
glyph_banks,
sprites: [EMPTY_SPRITE; 512],
sprite_count: 0,
scene_fade_level: 31,
scene_fade_color: Color::BLACK,
hud_fade_level: 31,
hud_fade_color: Color::BLACK,
priority_buckets: [
Vec::with_capacity(128),
layer_buckets: [
Vec::with_capacity(128),
Vec::with_capacity(128),
Vec::with_capacity(128),
@@ -530,23 +537,33 @@ impl Gfx {
std::mem::swap(&mut self.front, &mut self.back);
}
pub fn load_frame_sprites(&mut self, sprites: &[Sprite]) {
self.sprite_count = sprites.len().min(self.sprites.len());
for (index, sprite) in sprites.iter().copied().take(self.sprites.len()).enumerate() {
self.sprites[index] = Sprite { active: true, ..sprite };
}
for sprite in self.sprites.iter_mut().skip(self.sprite_count) {
sprite.active = false;
}
}
/// The main rendering pipeline.
///
/// This method composes the final frame by rasterizing layers and sprites in the
/// correct priority order into the back buffer.
/// Follows the hardware model where layers and sprites are composed every frame.
pub fn render_all(&mut self) {
self.populate_priority_buckets();
// 1. Priority 0 sprites: drawn at the very back, behind everything else.
Self::draw_bucket_on_buffer(
&mut self.back,
self.w,
self.h,
&self.priority_buckets[0],
&self.sprites,
&*self.glyph_banks,
);
self.populate_layer_buckets();
for bucket in &self.layer_buckets {
Self::draw_bucket_on_buffer(
&mut self.back,
self.w,
self.h,
bucket,
&self.sprites,
&*self.glyph_banks,
);
}
// 2. Scene-only fallback path: sprites and fades still work even before a
// cache-backed world composition request is issued for the frame.
@@ -563,18 +580,17 @@ impl Gfx {
/// plus sprite state and fade controls.
pub fn render_scene_from_cache(&mut self, cache: &SceneViewportCache, update: &ResolverUpdate) {
self.back.fill(Color::BLACK.raw());
self.populate_priority_buckets();
Self::draw_bucket_on_buffer(
&mut self.back,
self.w,
self.h,
&self.priority_buckets[0],
&self.sprites,
&*self.glyph_banks,
);
self.populate_layer_buckets();
for layer_index in 0..cache.layers.len() {
Self::draw_bucket_on_buffer(
&mut self.back,
self.w,
self.h,
&self.layer_buckets[layer_index],
&self.sprites,
&*self.glyph_banks,
);
Self::draw_cache_layer_to_buffer(
&mut self.back,
self.w,
@@ -583,31 +599,26 @@ impl Gfx {
&update.copy_requests[layer_index],
&*self.glyph_banks,
);
Self::draw_bucket_on_buffer(
&mut self.back,
self.w,
self.h,
&self.priority_buckets[layer_index + 1],
&self.sprites,
&*self.glyph_banks,
);
}
Self::apply_fade_to_buffer(&mut self.back, self.scene_fade_level, self.scene_fade_color);
Self::apply_fade_to_buffer(&mut self.back, self.hud_fade_level, self.hud_fade_color);
}
fn populate_priority_buckets(&mut self) {
for bucket in self.priority_buckets.iter_mut() {
fn populate_layer_buckets(&mut self) {
for bucket in self.layer_buckets.iter_mut() {
bucket.clear();
}
for (idx, sprite) in self.sprites.iter().enumerate() {
if sprite.active && sprite.priority < 5 {
self.priority_buckets[sprite.priority as usize].push(idx);
for (idx, sprite) in self.sprites.iter().take(self.sprite_count).enumerate() {
if sprite.active && (sprite.layer as usize) < self.layer_buckets.len() {
self.layer_buckets[sprite.layer as usize].push(idx);
}
}
for bucket in self.layer_buckets.iter_mut() {
bucket.sort_by_key(|&idx| self.sprites[idx].priority);
}
}
fn draw_cache_layer_to_buffer(
@@ -1004,6 +1015,7 @@ mod tests {
glyph: Glyph { glyph_id: 0, palette_id: 4 },
x: 0,
y: 0,
layer: 0,
bank_id: 0,
active: true,
flip_x: false,
@@ -1014,17 +1026,57 @@ mod tests {
glyph: Glyph { glyph_id: 0, palette_id: 4 },
x: 0,
y: 0,
layer: 2,
bank_id: 0,
active: true,
flip_x: false,
flip_y: false,
priority: 2,
};
gfx.sprite_count = 2;
gfx.render_scene_from_cache(&cache, &update);
assert_eq!(gfx.back[0], Color::BLUE.raw());
}
#[test]
fn load_frame_sprites_replaces_slot_first_submission_for_render_state() {
let banks = Arc::new(MemoryBanks::new());
let mut gfx = Gfx::new(16, 16, banks as Arc<dyn GlyphBankPoolAccess>);
gfx.load_frame_sprites(&[
Sprite {
glyph: Glyph { glyph_id: 1, palette_id: 2 },
x: 2,
y: 3,
layer: 1,
bank_id: 4,
active: false,
flip_x: true,
flip_y: false,
priority: 7,
},
Sprite {
glyph: Glyph { glyph_id: 5, palette_id: 6 },
x: 7,
y: 8,
layer: 3,
bank_id: 9,
active: false,
flip_x: false,
flip_y: true,
priority: 1,
},
]);
assert_eq!(gfx.sprite_count, 2);
assert!(gfx.sprites[0].active);
assert!(gfx.sprites[1].active);
assert!(!gfx.sprites[2].active);
assert_eq!(gfx.sprites[0].layer, 1);
assert_eq!(gfx.sprites[1].glyph.glyph_id, 5);
}
}
/// Blends in RGB565 per channel with saturation.