2016-03-21 04:10:14 +00:00
|
|
|
#include "TextureSheet.h"
|
|
|
|
|
|
|
|
namespace New3D {
|
|
|
|
|
|
|
|
TextureSheet::TextureSheet()
|
|
|
|
{
|
2022-07-11 16:10:41 +00:00
|
|
|
m_temp.resize(1024 * 1024 * 4); // temporary buffer for textures
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int TextureSheet::ToIndex(int x, int y)
|
|
|
|
{
|
|
|
|
return (y * 2048) + x;
|
|
|
|
}
|
|
|
|
|
2018-10-13 13:29:45 +00:00
|
|
|
std::shared_ptr<Texture> TextureSheet::BindTexture(const UINT16* src, int format, int x, int y, int width, int height)
|
2016-03-21 04:10:14 +00:00
|
|
|
{
|
|
|
|
//========
|
|
|
|
int index;
|
|
|
|
//========
|
|
|
|
|
|
|
|
x &= 2047;
|
|
|
|
y &= 2047;
|
|
|
|
|
2016-10-06 20:10:45 +00:00
|
|
|
if (width > 1024 || height > 1024) { // sanity checking
|
|
|
|
return nullptr;
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
index = ToIndex(x, y);
|
|
|
|
|
2017-03-24 20:54:12 +00:00
|
|
|
auto range = m_texMap.equal_range(index);
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2016-05-19 19:04:44 +00:00
|
|
|
if (range.first == range.second) {
|
|
|
|
|
|
|
|
// nothing found so create a new texture
|
2016-03-21 04:10:14 +00:00
|
|
|
|
|
|
|
std::shared_ptr<Texture> t(new Texture());
|
2017-03-24 20:54:12 +00:00
|
|
|
m_texMap.insert(std::pair<int, std::shared_ptr<Texture>>(index, t));
|
2018-10-13 13:29:45 +00:00
|
|
|
t->UploadTexture(src, m_temp.data(), format, x, y, width, height);
|
2016-03-21 04:10:14 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2016-05-19 19:04:44 +00:00
|
|
|
// iterate to try and find a match
|
2016-03-21 04:10:14 +00:00
|
|
|
|
|
|
|
for (auto it = range.first; it != range.second; ++it) {
|
|
|
|
|
|
|
|
int x2, y2, width2, height2, format2;
|
|
|
|
|
|
|
|
it->second->GetDetails(x2, y2, width2, height2, format2);
|
|
|
|
|
2017-03-24 20:54:12 +00:00
|
|
|
if (width == width2 && height == height2 && format == format2) {
|
2016-03-21 04:10:14 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 19:04:44 +00:00
|
|
|
// nothing found so create a new entry
|
|
|
|
|
2016-03-21 04:10:14 +00:00
|
|
|
std::shared_ptr<Texture> t(new Texture());
|
2017-03-24 20:54:12 +00:00
|
|
|
m_texMap.insert(std::pair<int, std::shared_ptr<Texture>>(index, t));
|
2018-10-13 13:29:45 +00:00
|
|
|
t->UploadTexture(src, m_temp.data(), format, x, y, width, height);
|
2016-03-21 04:10:14 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureSheet::Release()
|
|
|
|
{
|
2017-03-24 20:54:12 +00:00
|
|
|
m_texMap.clear();
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureSheet::Invalidate(int x, int y, int width, int height)
|
|
|
|
{
|
2016-11-22 20:30:10 +00:00
|
|
|
//============
|
|
|
|
int newX;
|
|
|
|
int newY;
|
|
|
|
int newWidth;
|
|
|
|
int newHeight;
|
2016-03-21 04:10:14 +00:00
|
|
|
int count;
|
|
|
|
int sWidth; // sample width
|
|
|
|
int sHeight; // sample height
|
2016-11-22 20:30:10 +00:00
|
|
|
//============
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2016-11-22 20:30:10 +00:00
|
|
|
if (width <= 512) {
|
|
|
|
newX = (x + width) - 512;
|
|
|
|
newWidth = 512;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newX = x;
|
|
|
|
newWidth = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (height <= 512) {
|
|
|
|
newY = (y + height) - 512;
|
|
|
|
newHeight = 512;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newY = y;
|
|
|
|
newHeight = height;
|
|
|
|
}
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2016-11-22 20:30:10 +00:00
|
|
|
CropTile(x, y, newX, newY, newWidth, newHeight);
|
|
|
|
|
|
|
|
sWidth = newWidth / 32;
|
|
|
|
sHeight = newHeight / 32;
|
2016-03-21 04:10:14 +00:00
|
|
|
count = sWidth * sHeight;
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
2016-11-22 20:30:10 +00:00
|
|
|
int posX = newX + ((i%sWidth) * 32);
|
|
|
|
int posY = newY + ((i / sWidth) * 32);
|
|
|
|
int index = ToIndex(posX, posY);
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2016-11-22 20:30:10 +00:00
|
|
|
if (posX >= x && posY >= y) { // invalidate this area of memory
|
2017-03-24 20:54:12 +00:00
|
|
|
m_texMap.erase(index);
|
2016-11-22 20:30:10 +00:00
|
|
|
}
|
|
|
|
else { // check for overlapping data tiles and invalidate as necessary
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2017-03-24 20:54:12 +00:00
|
|
|
auto range = m_texMap.equal_range(index);
|
2016-11-22 20:30:10 +00:00
|
|
|
|
2017-03-24 20:54:12 +00:00
|
|
|
for (auto it = range.first; it != range.second; ++it) {
|
2016-11-22 20:30:10 +00:00
|
|
|
|
2017-03-24 20:54:12 +00:00
|
|
|
if (it->second->CheckMapPos(x, x + width, y, y + height)) {
|
|
|
|
m_texMap.erase(index);
|
|
|
|
break;
|
2016-11-22 20:30:10 +00:00
|
|
|
}
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 20:30:10 +00:00
|
|
|
void TextureSheet::CropTile(int oldX, int oldY, int &newX, int &newY, int &newWidth, int &newHeight)
|
|
|
|
{
|
|
|
|
if (newX < 0) {
|
|
|
|
newWidth += newX;
|
|
|
|
newX = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newY < 0) {
|
|
|
|
newHeight += newY;
|
|
|
|
newY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldY >= 1024 && newY < 1024) { // gone into next memory page, limitation of our flat model
|
|
|
|
newHeight -= 1024 - newY;
|
|
|
|
newY = 1024;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 15:50:54 +00:00
|
|
|
int TextureSheet::GetTexFormat(int originalFormat, bool contour)
|
|
|
|
{
|
|
|
|
if (!contour) {
|
|
|
|
return originalFormat; // the same
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (originalFormat)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
return originalFormat + 7; // these formats are identical to 1-4, except they lose the 4 bit alpha part when contour is enabled
|
|
|
|
default:
|
|
|
|
return originalFormat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 01:37:29 +00:00
|
|
|
void TextureSheet::GetMicrotexPos(int basePage, int id, int& x, int& y)
|
|
|
|
{
|
2017-03-11 12:55:47 +00:00
|
|
|
int xCoords[8] = { 0, 0, 128, 128, 0, 0, 128, 128 };
|
|
|
|
int yCoords[8] = { 0, 128, 0, 128, 256, 384, 256, 384 };
|
2016-10-06 01:37:29 +00:00
|
|
|
|
|
|
|
// i'm assuming .. the micro texture map is always on the other memory bank to the base texture
|
|
|
|
// this logic works for all our current games
|
|
|
|
// the microtextures are always on the top left of each texture sheet
|
|
|
|
|
|
|
|
basePage = (basePage + 1) & 1; // wrap around base page
|
|
|
|
|
|
|
|
x = xCoords[id];
|
2017-03-11 01:18:43 +00:00
|
|
|
y = yCoords[id] + (basePage * 1024);
|
2016-10-06 01:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 04:10:14 +00:00
|
|
|
} // New3D
|