273. 图案表转帧缓冲

将图案表调试网格渲染到帧缓冲中。

273 / 356 · tests/chapter_05_rendering_pipeline/test_273_pattern_table_to_framebuffer.py

待创建的文件

emulator/rendering/pattern_table_renderer.py

为什么需要这一步

模拟器已经有了纯粹的 CHR 辅助函数

decode_pattern_table(pattern_table_bytes)
    -> 256 decoded 8x8 tiles of color indexes

build_pattern_table_debug_grid(decoded_tiles)
    -> 128x128 color-index debug grid

而渲染管线现在也有了

color_index_grid_to_framebuffer(grid, palette)
    -> RGB Framebuffer

这一步把这些部件组合成一个纯粹的辅助函数

pattern_table bytes + RGB palette -> 128x128 Framebuffer

什么是图案表调试帧缓冲?图案表包含 256 个图块,每个图块是 8x8 像素。为了便于调试,我们把这些图块排列成:

16 tiles across * 8 pixels = 128 pixels wide
16 tiles down   * 8 pixels = 128 pixels high

建议的实现示例

from emulator.ppu.chr_decoder import (
    build_pattern_table_debug_grid,
    decode_pattern_table,
)
from emulator.rendering.color_index_renderer import color_index_grid_to_framebuffer
from emulator.rendering.framebuffer import Framebuffer, RGBColor


def pattern_table_to_framebuffer(
    pattern_table_bytes: bytes,
    palette: list[RGBColor],
) -> Framebuffer:
    decoded_tiles = decode_pattern_table(pattern_table_bytes)
    grid = build_pattern_table_debug_grid(decoded_tiles)
    return color_index_grid_to_framebuffer(grid, palette)

架构规则

这个渲染器应该组合已有的纯函数,不要在这里重复实现 CHR 解码逻辑。

重要的测试数据规则

只使用合成的 CHR 字节。本测试中不要使用商业 ROM 的 CHR 数据。

本步骤不涉及

  • pygame 显示
  • 写入图像文件
  • 名称表渲染
  • 调色板 RAM 查找
  • 精灵渲染

运行本课

uv run pytest tests/chapter_05_rendering_pipeline/test_273_pattern_table_to_framebuffer.py -v