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)
|
|
|
|
{
|
2009-07-01 22:29:24 +00:00
|
|
|
memset(m_pages, 0, sizeof(m_pages));
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSTextureCacheSW::~GSTextureCacheSW()
|
|
|
|
{
|
|
|
|
RemoveAll();
|
|
|
|
}
|
|
|
|
|
2009-05-20 15:35:31 +00:00
|
|
|
const GSTextureCacheSW::GSTexture* GSTextureCacheSW::Lookup(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GSVector4i& r)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
const GSLocalMemory::psm_t& psm = GSLocalMemory::m_psm[TEX0.PSM];
|
|
|
|
|
|
|
|
GSTexture* t = NULL;
|
|
|
|
|
2009-07-12 13:46:05 +00:00
|
|
|
list<GSTexture*>& m = m_map[TEX0.TBP0 >> 5];
|
2009-06-27 03:32:33 +00:00
|
|
|
|
2009-07-12 13:46:05 +00:00
|
|
|
for(list<GSTexture*>::iterator i = m.begin(); i != m.end(); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-12 13:46:05 +00:00
|
|
|
GSTexture* t2 = *i;
|
2009-05-11 08:18:00 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
if(((TEX0.u32[0] ^ t2->m_TEX0.u32[0]) | ((TEX0.u32[1] ^ t2->m_TEX0.u32[1]) & 3)) != 0) // TBP0 TBW PSM TW TH
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-06-17 11:24:42 +00:00
|
|
|
if((psm.trbpp == 16 || psm.trbpp == 24) && TEX0.TCC && TEXA != t2->m_TEXA)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-07-12 13:46:05 +00:00
|
|
|
m.splice(m.begin(), m, i);
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
t = t2;
|
|
|
|
|
|
|
|
t->m_age = 0;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(t == NULL)
|
|
|
|
{
|
|
|
|
t = new GSTexture(m_state);
|
|
|
|
|
2009-07-16 21:36:07 +00:00
|
|
|
m_textures.insert(t);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
const GSOffset* o = m_state->m_context->offset.tex;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
GSVector2i bs = (TEX0.TBP0 & 31) == 0 ? psm.pgs : psm.bs;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
int tw = 1 << TEX0.TW;
|
|
|
|
int th = 1 << TEX0.TH;
|
2009-05-07 06:32:10 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
for(int y = 0; y < th; y += bs.y)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-22 03:55:28 +00:00
|
|
|
uint32 base = o->block.row[y >> 3];
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
for(int x = 0; x < tw; x += bs.x)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-22 03:55:28 +00:00
|
|
|
uint32 page = (base + o->block.col[x >> 3]) >> 5;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-06-27 03:32:33 +00:00
|
|
|
if(page < MAX_PAGES)
|
2009-05-07 06:32:10 +00:00
|
|
|
{
|
2009-07-01 22:29:24 +00:00
|
|
|
m_pages[page >> 5] |= 1 << (page & 31);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < countof(m_pages); i++)
|
|
|
|
{
|
|
|
|
if(uint32 p = m_pages[i])
|
|
|
|
{
|
|
|
|
m_pages[i] = 0;
|
|
|
|
|
2009-07-12 13:46:05 +00:00
|
|
|
list<GSTexture*>* m = &m_map[i << 5];
|
2009-07-01 22:29:24 +00:00
|
|
|
|
2009-07-16 21:36:07 +00:00
|
|
|
unsigned long j;
|
|
|
|
|
|
|
|
while(_BitScanForward(&j, p))
|
2009-07-01 22:29:24 +00:00
|
|
|
{
|
2009-07-16 21:36:07 +00:00
|
|
|
p ^= 1 << j;
|
|
|
|
|
|
|
|
m[j].push_front(t);
|
2009-05-07 06:32:10 +00:00
|
|
|
}
|
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-07-12 13:46:05 +00:00
|
|
|
RemoveAt(t);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-12 13:46:05 +00:00
|
|
|
t = NULL;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
void GSTextureCacheSW::InvalidateVideoMem(const GSOffset* o, const GSVector4i& rect)
|
|
|
|
{
|
|
|
|
uint32 bp = o->bp;
|
|
|
|
uint32 bw = o->bw;
|
|
|
|
uint32 psm = o->psm;
|
|
|
|
|
|
|
|
GSVector2i bs = (bp & 31) == 0 ? GSLocalMemory::m_psm[psm].pgs : GSLocalMemory::m_psm[psm].bs;
|
|
|
|
|
|
|
|
GSVector4i r = rect.ralign<GSVector4i::Outside>(bs);
|
|
|
|
|
|
|
|
for(int y = r.top; y < r.bottom; y += bs.y)
|
|
|
|
{
|
|
|
|
uint32 base = o->block.row[y >> 3];
|
|
|
|
|
|
|
|
for(int x = r.left; x < r.right; x += bs.x)
|
|
|
|
{
|
|
|
|
uint32 page = (base + o->block.col[x >> 3]) >> 5;
|
|
|
|
|
|
|
|
if(page < MAX_PAGES)
|
|
|
|
{
|
|
|
|
const list<GSTexture*>& map = m_map[page];
|
|
|
|
|
|
|
|
for(list<GSTexture*>::const_iterator i = map.begin(); i != map.end(); i++)
|
|
|
|
{
|
|
|
|
GSTexture* t = *i;
|
|
|
|
|
|
|
|
if(GSUtil::HasSharedBits(psm, t->m_TEX0.PSM))
|
|
|
|
{
|
|
|
|
t->m_valid[page] = 0;
|
|
|
|
t->m_complete = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
void GSTextureCacheSW::RemoveAll()
|
|
|
|
{
|
2009-07-16 21:36:07 +00:00
|
|
|
for_each(m_textures.begin(), m_textures.end(), delete_object());
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_textures.clear();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < MAX_PAGES; i++)
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
m_map[i].clear();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-12 13:46:05 +00:00
|
|
|
void GSTextureCacheSW::RemoveAt(GSTexture* t)
|
|
|
|
{
|
|
|
|
m_textures.erase(t);
|
|
|
|
|
|
|
|
for(uint32 start = t->m_TEX0.TBP0 >> 5, end = countof(m_map) - 1; start <= end; start++)
|
|
|
|
{
|
|
|
|
list<GSTexture*>& m = m_map[start];
|
|
|
|
|
|
|
|
for(list<GSTexture*>::iterator i = m.begin(); i != m.end(); )
|
|
|
|
{
|
|
|
|
list<GSTexture*>::iterator j = i++;
|
|
|
|
|
|
|
|
if(*j == t) {m.erase(j); break;}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
void GSTextureCacheSW::IncAge()
|
|
|
|
{
|
2009-07-16 21:36:07 +00:00
|
|
|
for(hash_set<GSTexture*>::iterator i = m_textures.begin(); i != m_textures.end(); )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-16 21:36:07 +00:00
|
|
|
hash_set<GSTexture*>::iterator j = i++;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-16 21:36:07 +00:00
|
|
|
GSTexture* t = *j;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
if(++t->m_age > 30)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-12 13:46:05 +00:00
|
|
|
RemoveAt(t);
|
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_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-20 15:35:31 +00:00
|
|
|
bool GSTextureCacheSW::GSTexture::Update(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GSVector4i& 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;
|
|
|
|
|
|
|
|
const GSLocalMemory::psm_t& psm = GSLocalMemory::m_psm[TEX0.PSM];
|
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
GSVector2i bs = psm.bs;
|
|
|
|
|
2009-07-16 21:36:07 +00:00
|
|
|
int tw = std::max<int>(1 << TEX0.TW, bs.x);
|
|
|
|
int th = std::max<int>(1 << TEX0.TH, bs.y);
|
2009-07-04 15:14:04 +00:00
|
|
|
|
|
|
|
GSVector4i r = rect.ralign<GSVector4i::Outside>(bs);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
if(r.eq(GSVector4i(0, 0, tw, th)))
|
|
|
|
{
|
|
|
|
m_complete = true; // lame, but better than nothing
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(m_buff == NULL)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
m_buff = _aligned_malloc(tw * th * sizeof(uint32), 16);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(m_buff == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-24 09:24:09 +00:00
|
|
|
m_tw = std::max<int>(TEX0.TW, psm.pal > 0 ? 5 : 3); // makes one row 32 bytes at least, matches the smallest block size that is allocated above for m_buff
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
GSLocalMemory& mem = m_state->m_mem;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
const GSOffset* o = m_state->m_context->offset.tex;
|
2009-06-27 03:32:33 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
bool repeating = m_TEX0.IsRepeating();
|
2009-07-02 16:05:03 +00:00
|
|
|
|
|
|
|
uint32 blocks = 0;
|
|
|
|
|
2009-05-07 06:32:10 +00:00
|
|
|
GSLocalMemory::readTextureBlock rtxb = psm.rtxbP;
|
2009-06-27 03:32:33 +00:00
|
|
|
|
2009-07-02 16:05:03 +00:00
|
|
|
int shift = psm.pal == 0 ? 2 : 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-02 16:05:03 +00:00
|
|
|
uint32 pitch = (1 << m_tw) << shift;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
uint8* dst = (uint8*)m_buff + pitch * r.top;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
for(int y = r.top, block_pitch = pitch * bs.y; y < r.bottom; y += bs.y, dst += block_pitch)
|
2009-05-07 06:32:10 +00:00
|
|
|
{
|
2009-07-22 03:55:28 +00:00
|
|
|
uint32 base = o->block.row[y >> 3];
|
2009-07-02 16:05:03 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
for(int x = r.left; x < r.right; x += bs.x)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-22 03:55:28 +00:00
|
|
|
uint32 block = base + o->block.col[x >> 3];
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-02 16:05:03 +00:00
|
|
|
if(block < MAX_BLOCKS)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-02 16:05:03 +00:00
|
|
|
uint32 row = block >> 5;
|
|
|
|
uint32 col = 1 << (block & 31);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-02 16:05:03 +00:00
|
|
|
if((m_valid[row] & col) == 0)
|
2009-05-09 08:37:02 +00:00
|
|
|
{
|
2009-07-02 16:05:03 +00:00
|
|
|
if(!repeating)
|
2009-05-09 08:37:02 +00:00
|
|
|
{
|
|
|
|
m_valid[row] |= col;
|
2009-07-02 16:05:03 +00:00
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-02 16:05:03 +00:00
|
|
|
(mem.*rtxb)(block, &dst[x << shift], pitch, TEXA);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-02 16:05:03 +00:00
|
|
|
blocks++;
|
2009-05-09 08:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-07 06:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-02 16:05:03 +00:00
|
|
|
if(blocks > 0)
|
|
|
|
{
|
|
|
|
if(repeating)
|
2009-05-07 06:32:10 +00:00
|
|
|
{
|
2009-07-04 15:14:04 +00:00
|
|
|
for(int y = r.top; y < r.bottom; y += bs.y)
|
2009-05-07 06:32:10 +00:00
|
|
|
{
|
2009-07-22 03:55:28 +00:00
|
|
|
uint32 base = o->block.row[y >> 3];
|
2009-05-09 08:37:02 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
for(int x = r.left; x < r.right; x += bs.x)
|
2009-05-09 08:37:02 +00:00
|
|
|
{
|
2009-07-22 03:55:28 +00:00
|
|
|
uint32 block = base + o->block.col[x >> 3];
|
2009-05-09 08:37:02 +00:00
|
|
|
|
2009-07-02 16:05:03 +00:00
|
|
|
if(block < MAX_BLOCKS)
|
2009-05-09 08:37:02 +00:00
|
|
|
{
|
2009-07-02 16:05:03 +00:00
|
|
|
uint32 row = block >> 5;
|
|
|
|
uint32 col = 1 << (block & 31);
|
2009-05-09 08:37:02 +00:00
|
|
|
|
2009-07-02 16:05:03 +00:00
|
|
|
m_valid[row] |= col;
|
2009-05-09 08:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-07 06:32:10 +00:00
|
|
|
}
|
2009-05-09 08:37:02 +00:00
|
|
|
}
|
2009-05-07 06:32:10 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
m_state->m_perfmon.Put(GSPerfMon::Unswizzle, bs.x * bs.y * blocks << shift);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2009-05-07 06:32:10 +00:00
|
|
|
}
|