2009-02-09 21:15:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* This Program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This Program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "GSTextureCacheSW.h"
|
|
|
|
|
|
|
|
GSTextureCacheSW::GSTextureCacheSW(GSState* state)
|
|
|
|
: m_state(state)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GSTextureCacheSW::~GSTextureCacheSW()
|
|
|
|
{
|
|
|
|
RemoveAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
const GSTextureCacheSW::GSTexture* GSTextureCacheSW::Lookup(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const CRect* r)
|
|
|
|
{
|
|
|
|
GSLocalMemory& mem = m_state->m_mem;
|
|
|
|
|
|
|
|
const GSLocalMemory::psm_t& psm = GSLocalMemory::m_psm[TEX0.PSM];
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
const CAtlMap<GSTexture*, bool>& map = m_map[TEX0.TBP0 >> 5];
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
GSTexture* t = NULL;
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
POSITION pos = map.GetStartPosition();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
while(pos)
|
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
GSTexture* t2 = map.GetNextKey(pos);
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
// if(t2->m_TEX0.TBP0 != TEX0.TBP0 || t2->m_TEX0.TBW != TEX0.TBW || t2->m_TEX0.PSM != TEX0.PSM || t2->m_TEX0.TW != TEX0.TW || t2->m_TEX0.TH != TEX0.TH)
|
|
|
|
if(((t2->m_TEX0.ai32[0] ^ TEX0.ai32[0]) | ((t2->m_TEX0.ai32[1] ^ TEX0.ai32[1]) & 3)) != 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-05-07 12:34:58 +00:00
|
|
|
if((psm.trbpp == 16 || psm.trbpp == 24) && (t2->m_TEX0.TCC != TEX0.TCC || TEX0.TCC && !(t2->m_TEXA == (GSVector4i)TEXA).alltrue()))
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = t2;
|
|
|
|
|
|
|
|
t->m_age = 0;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(t == NULL)
|
|
|
|
{
|
|
|
|
t = new GSTexture(m_state);
|
|
|
|
|
|
|
|
t->m_pos = m_textures.AddTail(t);
|
|
|
|
|
|
|
|
int tw = 1 << TEX0.TW;
|
|
|
|
int th = 1 << TEX0.TH;
|
|
|
|
|
|
|
|
DWORD bp = TEX0.TBP0;
|
|
|
|
DWORD bw = TEX0.TBW;
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
CSize s = (bp & 31) == 0 ? psm.pgs : psm.bs;
|
|
|
|
|
|
|
|
for(int y = 0; y < th; y += s.cy)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
DWORD base = psm.bn(0, y, bp, bw);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
for(int x = 0; x < tw; x += s.cx)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
DWORD page = (base + psm.blockOffset[x >> 3]) >> 5;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
if(page >= MAX_PAGES)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
m_map[page].SetAt(t, true);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!t->Update(TEX0, TEXA, r))
|
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
printf("!@#$%\n"); // memory allocation may fail if the game is too hungry
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
m_textures.RemoveAt(t->m_pos);
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
for(int i = 0; i < MAX_PAGES; i++)
|
|
|
|
{
|
|
|
|
m_map[i].RemoveKey(t);
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
delete t;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSTextureCacheSW::RemoveAll()
|
|
|
|
{
|
|
|
|
POSITION pos = m_textures.GetHeadPosition();
|
|
|
|
|
|
|
|
while(pos)
|
|
|
|
{
|
|
|
|
delete m_textures.GetNext(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_textures.RemoveAll();
|
|
|
|
|
|
|
|
for(int i = 0; i < MAX_PAGES; i++)
|
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
m_map[i].RemoveAll();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSTextureCacheSW::IncAge()
|
|
|
|
{
|
|
|
|
POSITION pos = m_textures.GetHeadPosition();
|
|
|
|
|
|
|
|
while(pos)
|
|
|
|
{
|
|
|
|
POSITION cur = pos;
|
|
|
|
|
|
|
|
GSTexture* t = m_textures.GetNext(pos);
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
if(++t->m_age > 30)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
m_textures.RemoveAt(cur);
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
for(int i = 0; i < MAX_PAGES; i++)
|
|
|
|
{
|
|
|
|
m_map[i].RemoveKey(t);
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
void GSTextureCacheSW::InvalidateVideoMem(const GIFRegBITBLTBUF& BITBLTBUF, const CRect& rect)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
const GSLocalMemory::psm_t& psm = GSLocalMemory::m_psm[BITBLTBUF.DPSM];
|
|
|
|
|
|
|
|
DWORD bp = BITBLTBUF.DBP;
|
|
|
|
DWORD bw = BITBLTBUF.DBW;
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
CSize s = (bp & 31) == 0 ? psm.pgs : psm.bs;
|
|
|
|
|
|
|
|
CRect r;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
r.left = rect.left & ~(s.cx - 1);
|
|
|
|
r.top = rect.top & ~(s.cy - 1);
|
|
|
|
r.right = (rect.right + (s.cx - 1)) & ~(s.cx - 1);
|
|
|
|
r.bottom = (rect.bottom + (s.cy - 1)) & ~(s.cy - 1);
|
|
|
|
|
|
|
|
for(int y = r.top; y < r.bottom; y += s.cy)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
DWORD base = psm.bn(0, y, bp, bw);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
for(int x = r.left; x < r.right; x += s.cx)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
DWORD page = (base + psm.blockOffset[x >> 3]) >> 5;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
if(page >= MAX_PAGES)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
const CAtlMap<GSTexture*, bool>& map = m_map[page];
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
POSITION pos = map.GetStartPosition();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
while(pos)
|
|
|
|
{
|
|
|
|
GSTexture* t = map.GetNextKey(pos);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
t->m_valid[page] = 0;
|
|
|
|
|
|
|
|
t->m_complete = false;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
GSTextureCacheSW::GSTexture::GSTexture(GSState* state)
|
|
|
|
: m_state(state)
|
|
|
|
, m_buff(NULL)
|
|
|
|
, m_tw(0)
|
|
|
|
, m_age(0)
|
2009-05-07 06:32:10 +00:00
|
|
|
, m_pos(NULL)
|
|
|
|
, m_complete(false)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
memset(m_valid, 0, sizeof(m_valid));
|
|
|
|
}
|
|
|
|
|
|
|
|
GSTextureCacheSW::GSTexture::~GSTexture()
|
|
|
|
{
|
|
|
|
if(m_buff)
|
|
|
|
{
|
|
|
|
_aligned_free(m_buff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
bool GSTextureCacheSW::GSTexture::Update(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const CRect* rect)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
if(m_complete)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_TEX0 = TEX0;
|
|
|
|
m_TEXA = TEXA;
|
|
|
|
|
|
|
|
GSLocalMemory& mem = m_state->m_mem;
|
|
|
|
|
|
|
|
const GSLocalMemory::psm_t& psm = GSLocalMemory::m_psm[TEX0.PSM];
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
DWORD bp = TEX0.TBP0;
|
|
|
|
DWORD bw = TEX0.TBW;
|
|
|
|
|
|
|
|
CSize s = psm.bs;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
int tw = max(1 << TEX0.TW, s.cx);
|
|
|
|
int th = max(1 << TEX0.TH, s.cy);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(m_buff == NULL)
|
|
|
|
{
|
|
|
|
m_buff = _aligned_malloc(tw * th * sizeof(DWORD), 16);
|
|
|
|
|
|
|
|
if(m_buff == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_tw = max(psm.pal > 0 ? 5 : 3, TEX0.TW); // makes one row 32 bytes at least, matches the smallest block size that is allocated above for m_buff
|
|
|
|
}
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
CRect r(0, 0, tw, th);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
if(rect)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
r.left = rect->left & ~(s.cx - 1);
|
|
|
|
r.top = rect->top & ~(s.cy - 1);
|
|
|
|
r.right = (rect->right + (s.cx - 1)) & ~(s.cx - 1);
|
|
|
|
r.bottom = (rect->bottom + (s.cy - 1)) & ~(s.cy - 1);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
if(r.left == 0 && r.top == 0 && r.right == tw && r.bottom == th)
|
|
|
|
{
|
|
|
|
m_complete = true; // lame, but better than nothing
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
GSLocalMemory::readTextureBlock rtxb = psm.rtxbP;
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
int bytes = psm.pal > 0 ? 1 : 4;
|
|
|
|
|
|
|
|
DWORD pitch = (1 << m_tw) * bytes;
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
BYTE* dst = (BYTE*)m_buff + pitch * r.top;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
DWORD blocks = 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
for(int y = r.top, o = pitch * s.cy; y < r.bottom; y += s.cy, dst += o)
|
|
|
|
{
|
|
|
|
DWORD base = psm.bn(0, y, bp, bw);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
for(int x = r.left; x < r.right; x += s.cx)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
DWORD block = base + psm.blockOffset[x >> 3];
|
|
|
|
|
|
|
|
if(block >= MAX_BLOCKS)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-07 06:32:10 +00:00
|
|
|
continue;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
DWORD row = block >> 5;
|
|
|
|
DWORD col = 1 << (block & 31);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
if(m_valid[row] & col)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
// unfortunatelly a block may be part of the same texture multiple times at different places (when (1 << tw) > (tbw << 6), ex. 1024 > 640),
|
|
|
|
// so just can't set the block's bit to valid in one pass, even if 99.9% of the games don't address the repeated part at the right side
|
2009-05-07 12:34:58 +00:00
|
|
|
|
|
|
|
// TODO: still bogus if those repeated parts aren't fetched together
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
// m_valid[row] |= col;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
(mem.*rtxb)(block, &dst[x * bytes], pitch, TEXA);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
blocks++;
|
|
|
|
}
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
m_state->m_perfmon.Put(GSPerfMon::Unswizzle, s.cx * s.cy * bytes * blocks);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
for(int y = r.top; y < r.bottom; y += s.cy)
|
|
|
|
{
|
|
|
|
DWORD base = psm.bn(0, y, bp, bw);
|
|
|
|
|
|
|
|
for(int x = r.left; x < r.right; x += s.cx)
|
|
|
|
{
|
|
|
|
DWORD block = base + psm.blockOffset[x >> 3];
|
|
|
|
|
|
|
|
if(block >= MAX_BLOCKS)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD row = block >> 5;
|
|
|
|
DWORD col = 1 << (block & 31);
|
|
|
|
|
|
|
|
m_valid[row] |= col;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2009-05-07 06:32:10 +00:00
|
|
|
}
|