From 09520658a1f4eb6b731f8e406c3e8a5410d8dee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Sun, 11 Jul 2021 20:10:39 +0200 Subject: [PATCH] adding test for render in 2bpp and using mesen formula for getRealColor function --- sources/Debugger/CGramDebug.cpp | 26 +++++++----------- sources/PPU/PPUUtils.cpp | 19 ++++++------- sources/PPU/PPUUtils.hpp | 4 +-- tests/PPU/testTileRenderer.cpp | 47 +++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 28 deletions(-) diff --git a/sources/Debugger/CGramDebug.cpp b/sources/Debugger/CGramDebug.cpp index 387f4f9..ad5ae94 100644 --- a/sources/Debugger/CGramDebug.cpp +++ b/sources/Debugger/CGramDebug.cpp @@ -8,6 +8,7 @@ #include #include #include "Utility/Utility.hpp" +#include "PPU/PPUUtils.hpp" namespace ComSquare::Debugger { @@ -45,16 +46,14 @@ namespace ComSquare::Debugger uint8_t blue = (cgramValue & 0x7D00U) >> 10U; uint8_t green = (cgramValue & 0x03E0U) >> 5U; uint8_t red = (cgramValue & 0x001FU); - uint24_t hexColorValue = 0; + uint32_t hexColorValue = PPU::Utils::getRealColor(cgramValue); this->_ui.indexLineEdit->setText(std::to_string(addr / 2).c_str()); this->_ui.valueLineEdit->setText(Utility::to_hex(cgramValue).c_str()); this->_ui.rLineEdit->setText(std::to_string(red).c_str()); this->_ui.gLineEdit->setText(std::to_string(green).c_str()); this->_ui.bLineEdit->setText(std::to_string(blue).c_str()); - hexColorValue += (red * 255U / 31U) << 16U; - hexColorValue += (green * 255U / 31U) << 8U; - hexColorValue += (blue * 255U / 31U); + hexColorValue >>= 8; this->_ui.hexLineEdit->setText(Utility::to_hex(hexColorValue).c_str()); } @@ -81,27 +80,20 @@ namespace ComSquare::Debugger QVariant CGramModel::data(const QModelIndex &index, int role) const { - u_int16_t addressValue; - uint8_t red; - uint8_t green; - uint8_t blue; if (role == Qt::TextAlignmentRole) return Qt::AlignCenter; if (role != Qt::BackgroundRole) return QVariant(); + int idDisplayTile = index.row() * 16 + index.column(); uint16_t cgramAddress = idDisplayTile / 8 * 16 + (idDisplayTile % 8 * 2); - addressValue = this->_ppu.cgramRead(cgramAddress); + uint16_t addressValue = this->_ppu.cgramRead(cgramAddress); addressValue += this->_ppu.cgramRead(cgramAddress + 1) << 8U; + uint32_t color = PPU::Utils::getRealColor(addressValue); - blue = (addressValue & 0x7D00U) >> 10U; - green = (addressValue & 0x03E0U) >> 5U; - red = (addressValue & 0x001FU); - - red = red * 255U / 31U; - green = green * 255U / 31U; - blue = blue * 255U / 31U; - return QColor(red, green, blue); + return QColor(static_cast((color & 0xFF000000) >> 24), + static_cast((color & 0x00FF0000) >> 16), + static_cast((color & 0x0000FF00) >> 8)); } } \ No newline at end of file diff --git a/sources/PPU/PPUUtils.cpp b/sources/PPU/PPUUtils.cpp index eb41ab1..2ea779a 100644 --- a/sources/PPU/PPUUtils.cpp +++ b/sources/PPU/PPUUtils.cpp @@ -7,16 +7,17 @@ namespace ComSquare::PPU::Utils { - uint32_t getRealColor(uint16_t color) + uint8_t To8Bit(int color) { - uint8_t blue = (color & 0x7D00U) >> 10U; - uint8_t green = (color & 0x03E0U) >> 5U; - uint8_t red = (color & 0x001FU); - uint32_t pixelTmp = 0xFF; + return (uint)((color << 3) + (color >> 2)); + } - pixelTmp += (red * 255U / 31U) << 24U; - pixelTmp += (green * 255U / 31U) << 16U; - pixelTmp += (blue * 255U / 31U) << 8U; - return pixelTmp; + uint32_t getRealColor(uint16_t cgramColor) + { + uint b = To8Bit(cgramColor >> 10); + uint g = To8Bit((cgramColor >> 5) & 0x1F); + uint r = To8Bit(cgramColor & 0x1F); + + return (0x000000FF | (r << 24) | (g << 16) | (b << 8)); } } \ No newline at end of file diff --git a/sources/PPU/PPUUtils.hpp b/sources/PPU/PPUUtils.hpp index 5e704a4..485b0b3 100644 --- a/sources/PPU/PPUUtils.hpp +++ b/sources/PPU/PPUUtils.hpp @@ -18,8 +18,8 @@ namespace ComSquare::PPU namespace ComSquare::PPU::Utils { - //! @brief Transform SNES color code BGR to uint32_t RGB - uint32_t getRealColor(uint16_t color); + //! @brief Transform SNES color code BGR to uint32_t RGBA + uint32_t getRealColor(uint16_t cgramColor); //! @brief Used to parse easily VRAM Tile information union TileData { struct { diff --git a/tests/PPU/testTileRenderer.cpp b/tests/PPU/testTileRenderer.cpp index 1f421ca..c511635 100644 --- a/tests/PPU/testTileRenderer.cpp +++ b/tests/PPU/testTileRenderer.cpp @@ -526,4 +526,51 @@ TEST_CASE("getPalette 8bpp", "[PPU][TileRenderer]") } i = 0; } +} + +TEST_CASE("render 2bpp", "[PPU][TileRenderer]") +{ + ComSquare::Ram::Ram vram(100, static_cast(0), "vramTest"); + ComSquare::Ram::Ram cgram(512, static_cast(0), "cgramTest"); + ComSquare::PPU::TileRenderer tileRenderer(vram, cgram); + + tileRenderer.setBpp(2); + tileRenderer.setPaletteIndex(1); + + std::vector vramValues { + "7C00BA7C827C7C00", + "1000D6007C003800" + }; + + std::vector cgramValues = { + 0xCE, 0x69, 0xDF, 0x63, 0xDE, 0x16, 0x8B, 0x00, + 0x00, 0x00, 0xBF, 0x67, 0x98, 0x42, 0x0E, 0x15 + }; + //{0x3600, 0x67bf, 0x4298, 0x150e}, +// 0 FFEFCE C6A584 734229 + uint32_t correctValues[8][8] = { + {0x00000000, 0xFFEFCEFF, 0xFFEFCEFF, 0xFFEFCEFF, 0xFFEFCEFF, 0xFFEFCEFF, 0x00000000, 0x00000000}, + {0xFFEFCEFF, 0xC6A584FF, 0x734229FF, 0x734229FF, 0x734229FF, 0xC6A584FF, 0xFFEFCEFF, 0x00000000}, + {0xFFEFCEFF, 0xC6A584FF, 0xC6A584FF, 0xC6A584FF, 0xC6A584FF, 0xC6A584FF, 0xFFEFCEFF, 0x00000000}, + {0x00000000, 0xFFEFCEFF, 0xFFEFCEFF, 0xFFEFCEFF, 0xFFEFCEFF, 0xFFEFCEFF, 0x00000000, 0x00000000}, + {0x00000000, 0x00000000, 0x00000000, 0xFFEFCEFF, 0x00000000, 0x00000000, 0x00000000, 0x00000000}, + {0xFFEFCEFF, 0xFFEFCEFF, 0x00000000, 0xFFEFCEFF, 0x00000000, 0xFFEFCEFF, 0xFFEFCEFF, 0x00000000}, + {0x00000000, 0xFFEFCEFF, 0xFFEFCEFF, 0xFFEFCEFF, 0xFFEFCEFF, 0xFFEFCEFF, 0x00000000, 0x00000000}, + {0x00000000, 0x00000000, 0xFFEFCEFF, 0xFFEFCEFF, 0xFFEFCEFF, 0x00000000, 0x00000000, 0x00000000} + }; + + fillRAM(vramValues, vram); + fillRAM(cgramValues, cgram); + + tileRenderer.render(0); + + int i = 0; + int j = 0; + for (const auto &row : tileRenderer.buffer) { + for (const auto &pixel : row) { + CHECK(correctValues[i][j++] == pixel); + } + j = 0; + i++; + } } \ No newline at end of file