Refactor tile handling functions to use std::span for improved performance and safety

- Updated UnpackBppTile and ConvertBpp functions to accept std::span instead of std::vector, enhancing flexibility and reducing unnecessary copies.
- Refactored LoadSNES4bppGFXToIndexedColorMatrix to return a std::vector instead of modifying a destination span, improving usability.
- Cleaned up variable names for consistency and readability throughout the affected functions.
This commit is contained in:
scawful
2025-05-10 11:00:38 -04:00
parent d97824c575
commit a393303d41
2 changed files with 37 additions and 31 deletions

View File

@@ -20,8 +20,8 @@ constexpr uint16_t TileVFlipBit = 0x8000;
// Bits used for tile name // Bits used for tile name
constexpr uint16_t TileNameMask = 0x03FF; constexpr uint16_t TileNameMask = 0x03FF;
snes_tile8 UnpackBppTile(const std::vector<uint8_t>& data, snes_tile8 UnpackBppTile(std::span<uint8_t> data, const uint32_t offset,
const uint32_t offset, const uint32_t bpp) { const uint32_t bpp) {
snes_tile8 tile; snes_tile8 tile;
assert(bpp >= 1 && bpp <= 8); assert(bpp >= 1 && bpp <= 8);
unsigned int bpp_pos[8]; // More for conveniance and readibility unsigned int bpp_pos[8]; // More for conveniance and readibility
@@ -112,8 +112,8 @@ std::vector<uint8_t> PackBppTile(const snes_tile8& tile, const uint32_t bpp) {
return output; return output;
} }
std::vector<uint8_t> ConvertBpp(const std::vector<uint8_t>& tiles, std::vector<uint8_t> ConvertBpp(std::span<uint8_t> tiles, uint32_t from_bpp,
uint32_t from_bpp, uint32_t to_bpp) { uint32_t to_bpp) {
unsigned int nb_tile = tiles.size() / (from_bpp * 8); unsigned int nb_tile = tiles.size() / (from_bpp * 8);
std::vector<uint8_t> converted(nb_tile * to_bpp * 8); std::vector<uint8_t> converted(nb_tile * to_bpp * 8);
@@ -376,43 +376,49 @@ void CopyTile8bpp16(int x, int y, int tile, std::vector<uint8_t>& bitmap,
} }
} }
void LoadSNES4bppGFXToIndexedColorMatrix(std::span<uint8_t> src, std::vector<uint8_t> LoadSNES4bppGFXToIndexedColorMatrix(
std::span<uint8_t> dest) { std::span<uint8_t> src) {
std::vector<uint8_t> dest;
uint8_t b0; uint8_t b0;
uint8_t b1; uint8_t b1;
uint8_t b2; uint8_t b2;
uint8_t b3; uint8_t b3;
int res; int res;
int mul; int mul;
int yAdder = 0; int y_adder = 0;
int srcIndex; int src_index;
int destX; int dest_x;
int destY; int dest_y;
int destIndex; int dest_index;
int mainIndexLimit = src.size() / 32; int main_index_limit = src.size() / 32;
for (int mainIndex = 0; mainIndex <= mainIndexLimit; mainIndex += 32) { for (int main_index = 0; main_index <= main_index_limit; main_index += 32) {
srcIndex = (mainIndex << 5); src_index = (main_index << 5);
if (srcIndex + 31 >= src.size()) return; if (src_index + 31 >= src.size()) {
destX = mainIndex & 0x0F; throw std::invalid_argument("src_index + 31 >= src.size()");
destY = mainIndex >> 4; }
destIndex = ((destY << 7) + destX) << 3; dest_x = main_index & 0x0F;
if (destIndex + 903 >= dest.size()) return; dest_y = main_index >> 4;
dest_index = ((dest_y << 7) + dest_x) << 3;
if (dest_index + 903 >= dest.size()) {
throw std::invalid_argument("dest_index + 903 >= dest.size()");
}
for (int i = 0; i < 16; i += 2) { for (int i = 0; i < 16; i += 2) {
mul = 1; mul = 1;
b0 = src[srcIndex + i]; b0 = src[src_index + i];
b1 = src[srcIndex + i + 1]; b1 = src[src_index + i + 1];
b2 = src[srcIndex + i + 16]; b2 = src[src_index + i + 16];
b3 = src[srcIndex + i + 17]; b3 = src[src_index + i + 17];
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
res = ((b0 & mul) | ((b1 & mul) << 1) | ((b2 & mul) << 2) | res = ((b0 & mul) | ((b1 & mul) << 1) | ((b2 & mul) << 2) |
((b3 & mul) << 3)) >> ((b3 & mul) << 3)) >>
j; j;
dest[destIndex + (7 - j) + yAdder] = res; dest[dest_index + (7 - j) + y_adder] = res;
mul <<= 1; mul <<= 1;
} }
yAdder += 128; y_adder += 128;
} }
} }
return dest;
} }
} // namespace gfx } // namespace gfx

View File

@@ -25,19 +25,19 @@ std::vector<uint8_t> SnesTo8bppSheet(std::span<uint8_t> sheet, int bpp,
std::vector<uint8_t> Bpp8SnesToIndexed(std::vector<uint8_t> data, std::vector<uint8_t> Bpp8SnesToIndexed(std::vector<uint8_t> data,
uint64_t bpp = 0); uint64_t bpp = 0);
snes_tile8 UnpackBppTile(const std::vector<uint8_t>& data, snes_tile8 UnpackBppTile(std::span<uint8_t> data, const uint32_t offset,
const uint32_t offset, const uint32_t bpp); const uint32_t bpp);
std::vector<uint8_t> PackBppTile(const snes_tile8& tile, const uint32_t bpp); std::vector<uint8_t> PackBppTile(const snes_tile8& tile, const uint32_t bpp);
std::vector<uint8_t> ConvertBpp(const std::vector<uint8_t>& tiles, std::vector<uint8_t> ConvertBpp(std::span<uint8_t> tiles, uint32_t from_bpp,
uint32_t from_bpp, uint32_t to_bpp); uint32_t to_bpp);
void CopyTile8bpp16(int x, int y, int tile, std::vector<uint8_t>& bitmap, void CopyTile8bpp16(int x, int y, int tile, std::vector<uint8_t>& bitmap,
std::vector<uint8_t>& blockset); std::vector<uint8_t>& blockset);
void LoadSNES4bppGFXToIndexedColorMatrix(std::span<uint8_t> src, std::vector<uint8_t> LoadSNES4bppGFXToIndexedColorMatrix(
std::span<uint8_t> dest); std::span<uint8_t> src);
/** /**
* @brief SNES 16-bit tile metadata container * @brief SNES 16-bit tile metadata container