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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "GSRenderer.h"
|
|
|
|
|
|
|
|
template<class Device> class GSTextureCache
|
|
|
|
{
|
|
|
|
typedef typename Device::Texture Texture;
|
|
|
|
|
|
|
|
public:
|
2009-05-14 16:41:52 +00:00
|
|
|
class GSSurface : public GSAlignedClass<16>
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
GSRenderer<Device>* m_renderer;
|
|
|
|
|
|
|
|
typedef typename Device::Texture Texture;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Texture m_texture;
|
|
|
|
Texture m_palette;
|
|
|
|
bool m_initpalette;
|
|
|
|
int m_age;
|
|
|
|
GSDirtyRectList m_dirty;
|
|
|
|
GIFRegTEX0 m_TEX0;
|
|
|
|
|
|
|
|
explicit GSSurface(GSRenderer<Device>* renderer)
|
|
|
|
: m_renderer(renderer)
|
|
|
|
, m_age(0)
|
|
|
|
, m_initpalette(false)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
m_TEX0.TBP0 = (uint32)~0;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~GSSurface()
|
|
|
|
{
|
|
|
|
m_renderer->m_dev.Recycle(m_texture);
|
|
|
|
m_renderer->m_dev.Recycle(m_palette);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Update()
|
|
|
|
{
|
|
|
|
m_age = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class GSRenderTarget : public GSSurface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool m_used;
|
|
|
|
|
|
|
|
explicit GSRenderTarget(GSRenderer<Device>* renderer)
|
|
|
|
: GSSurface(renderer)
|
|
|
|
, m_used(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Create(int w, int h)
|
|
|
|
{
|
|
|
|
// FIXME: initial data should be unswizzled from local mem in Update() if dirty
|
|
|
|
|
|
|
|
return m_renderer->m_dev.CreateRenderTarget(m_texture, w, h);
|
|
|
|
}
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
virtual void Read(const GSVector4i& r) = 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class GSDepthStencil : public GSSurface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool m_used;
|
|
|
|
|
|
|
|
explicit GSDepthStencil(GSRenderer<Device>* renderer)
|
|
|
|
: GSSurface(renderer)
|
|
|
|
, m_used(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Create(int w, int h)
|
|
|
|
{
|
|
|
|
// FIXME: initial data should be unswizzled from local mem in Update() if dirty
|
|
|
|
|
|
|
|
return m_renderer->m_dev.CreateDepthStencil(m_texture, w, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class GSTexture : public GSSurface
|
|
|
|
{
|
|
|
|
protected:
|
2009-05-14 16:41:52 +00:00
|
|
|
bool GetDirtyRect(GSVector4i& rr)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
int w = 1 << m_TEX0.TW;
|
|
|
|
int h = 1 << m_TEX0.TH;
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
GSVector4i r(0, 0, w, h);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSDirtyRect>::iterator i = m_dirty.begin(); i != m_dirty.end(); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
const GSVector4i& dirty = i->GetDirtyRect(m_TEX0).rintersect(r);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
if(!m_valid.rintersect(dirty).rempty())
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
// find the rect having the largest area, outside dirty, inside m_valid
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
GSVector4i left(m_valid.left, m_valid.top, min(m_valid.right, dirty.left), m_valid.bottom);
|
|
|
|
GSVector4i top(m_valid.left, m_valid.top, m_valid.right, min(m_valid.bottom, dirty.top));
|
|
|
|
GSVector4i right(max(m_valid.left, dirty.right), m_valid.top, m_valid.right, m_valid.bottom);
|
|
|
|
GSVector4i bottom(m_valid.left, max(m_valid.top, dirty.bottom), m_valid.right, m_valid.bottom);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
int leftsize = !left.rempty() ? left.width() * left.height() : 0;
|
|
|
|
int topsize = !top.rempty() ? top.width() * top.height() : 0;
|
|
|
|
int rightsize = !right.rempty() ? right.width() * right.height() : 0;
|
|
|
|
int bottomsize = !bottom.rempty() ? bottom.width() * bottom.height() : 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
// TODO: sort
|
|
|
|
|
|
|
|
m_valid =
|
|
|
|
leftsize > 0 ? left :
|
|
|
|
topsize > 0 ? top :
|
|
|
|
rightsize > 0 ? right :
|
|
|
|
bottomsize > 0 ? bottom :
|
2009-05-14 16:41:52 +00:00
|
|
|
GSVector4i::zero();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_dirty.clear();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
m_renderer->MinMaxUV(w, h, r);
|
|
|
|
|
|
|
|
if(GSUtil::IsRectInRect(r, m_valid))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(GSUtil::IsRectInRectH(r, m_valid) && (r.left >= m_valid.left || r.right <= m_valid.right))
|
|
|
|
{
|
|
|
|
r.top = m_valid.top;
|
|
|
|
r.bottom = m_valid.bottom;
|
|
|
|
if(r.left < m_valid.left) r.right = m_valid.left;
|
|
|
|
else r.left = m_valid.right; // if(r.right > m_valid.right)
|
|
|
|
}
|
|
|
|
else if(GSUtil::IsRectInRectV(r, m_valid) && (r.top >= m_valid.top || r.bottom <= m_valid.bottom))
|
|
|
|
{
|
|
|
|
r.left = m_valid.left;
|
|
|
|
r.right = m_valid.right;
|
|
|
|
if(r.top < m_valid.top) r.bottom = m_valid.top;
|
|
|
|
else r.top = m_valid.bottom; // if(r.bottom > m_valid.bottom)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
r = r.runion(m_valid);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
if(r.rempty())
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rr = r;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
GIFRegCLAMP m_CLAMP;
|
2009-05-14 16:41:52 +00:00
|
|
|
uint32* m_clut; // *
|
|
|
|
GSVector4i m_valid;
|
2009-02-09 21:15:56 +00:00
|
|
|
int m_bpp;
|
|
|
|
int m_bpp2;
|
|
|
|
bool m_rendered;
|
|
|
|
|
|
|
|
explicit GSTexture(GSRenderer<Device>* renderer)
|
|
|
|
: GSSurface(renderer)
|
|
|
|
, m_valid(0, 0, 0, 0)
|
|
|
|
, m_bpp(0)
|
|
|
|
, m_bpp2(0)
|
|
|
|
, m_rendered(false)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
m_clut = (uint32*)_aligned_malloc(256 * sizeof(uint32), 16);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
memset(m_clut, 0, sizeof(m_clut));
|
|
|
|
}
|
|
|
|
|
|
|
|
~GSTexture()
|
|
|
|
{
|
|
|
|
_aligned_free(m_clut);
|
|
|
|
}
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
__super::Update();
|
|
|
|
|
|
|
|
if(m_rendered)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GSVector4i r;
|
|
|
|
|
|
|
|
if(!GetDirtyRect(r))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_valid = m_valid.runion(r);
|
|
|
|
|
|
|
|
static uint8* bits = (uint8*)::_aligned_malloc(1024 * 1024 * 4, 16);
|
2009-05-15 07:48:51 +00:00
|
|
|
|
|
|
|
int pitch = ((r.width() + 3) & ~3) * 4;
|
2009-05-14 16:41:52 +00:00
|
|
|
|
|
|
|
if(m_renderer->m_psrr)
|
|
|
|
{
|
|
|
|
m_renderer->m_mem.ReadTextureNPNC(r, bits, pitch, m_renderer->m_context->TEX0, m_renderer->m_env.TEXA, m_renderer->m_context->CLAMP);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_renderer->m_mem.ReadTextureNP(r, bits, pitch, m_renderer->m_context->TEX0, m_renderer->m_env.TEXA, m_renderer->m_context->CLAMP);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_texture.Update(r, bits, pitch);
|
|
|
|
|
|
|
|
m_renderer->m_perfmon.Put(GSPerfMon::Unswizzle, r.width() * r.height() * m_bpp >> 3);
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
virtual bool Create() = 0;
|
|
|
|
virtual bool Create(GSRenderTarget* rt) = 0;
|
|
|
|
virtual bool Create(GSDepthStencil* ds) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GSRenderer<Device>* m_renderer;
|
2009-05-11 08:18:00 +00:00
|
|
|
list<GSRenderTarget*> m_rt;
|
|
|
|
list<GSDepthStencil*> m_ds;
|
|
|
|
list<GSTexture*> m_tex;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
template<class T> void RecycleByAge(list<T*>& l, int maxage = 60)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<T*>::iterator i = l.begin(); i != l.end(); )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
list<T*>::iterator j = i++;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
T* t = *j;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(++t->m_age > maxage)
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
l.erase(j);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual GSRenderTarget* CreateRenderTarget() = 0;
|
|
|
|
virtual GSDepthStencil* CreateDepthStencil() = 0;
|
|
|
|
virtual GSTexture* CreateTexture() = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSTextureCache(GSRenderer<Device>* renderer)
|
|
|
|
: m_renderer(renderer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~GSTextureCache()
|
|
|
|
{
|
|
|
|
RemoveAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveAll()
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSRenderTarget*>::iterator i = m_rt.begin(); i != m_rt.end(); i++)
|
|
|
|
{
|
|
|
|
delete *i;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_rt.clear();
|
|
|
|
|
|
|
|
for(list<GSDepthStencil*>::iterator i = m_ds.begin(); i != m_ds.end(); i++)
|
|
|
|
{
|
|
|
|
delete *i;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ds.clear();
|
|
|
|
|
|
|
|
for(list<GSTexture*>::iterator i = m_tex.begin(); i != m_tex.end(); i++)
|
|
|
|
{
|
|
|
|
delete *i;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_tex.clear();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSRenderTarget* GetRenderTarget(const GIFRegTEX0& TEX0, int w, int h, bool fb = false)
|
|
|
|
{
|
|
|
|
GSRenderTarget* rt = NULL;
|
|
|
|
|
|
|
|
if(rt == NULL)
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSRenderTarget*>::iterator i = m_rt.begin(); i != m_rt.end(); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
GSRenderTarget* rt2 = *i;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(rt2->m_TEX0.TBP0 == TEX0.TBP0)
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
m_rt.splice(m_rt.begin(), m_rt, i);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
rt = rt2;
|
|
|
|
|
|
|
|
if(!fb) rt->m_TEX0 = TEX0;
|
|
|
|
|
|
|
|
rt->Update();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rt == NULL && fb)
|
|
|
|
{
|
|
|
|
// HACK: try to find something close to the base pointer
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSRenderTarget*>::iterator i = m_rt.begin(); i != m_rt.end(); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
GSRenderTarget* rt2 = *i;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(rt2->m_TEX0.TBP0 <= TEX0.TBP0 && TEX0.TBP0 < rt2->m_TEX0.TBP0 + 0x700 && (!rt || rt2->m_TEX0.TBP0 >= rt->m_TEX0.TBP0))
|
|
|
|
{
|
|
|
|
rt = rt2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rt)
|
|
|
|
{
|
|
|
|
rt->Update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rt == NULL)
|
|
|
|
{
|
|
|
|
rt = CreateRenderTarget();
|
|
|
|
|
|
|
|
rt->m_TEX0 = TEX0;
|
|
|
|
|
|
|
|
if(!rt->Create(w, h))
|
|
|
|
{
|
|
|
|
delete rt;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_rt.push_front(rt);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(m_renderer->CanUpscale())
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
GSVector4i fr = m_renderer->GetFrameRect();
|
|
|
|
|
|
|
|
int ww = (int)(fr.left + rt->m_TEX0.TBW * 64);
|
|
|
|
int hh = (int)(fr.top + m_renderer->GetDisplayRect().height());
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
if(hh <= m_renderer->GetDeviceRect().height() / 2)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
hh *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ww > 0 && hh > 0)
|
|
|
|
{
|
|
|
|
rt->m_texture.m_scale.x = (float)w / ww;
|
|
|
|
rt->m_texture.m_scale.y = (float)h / hh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!fb)
|
|
|
|
{
|
|
|
|
rt->m_used = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rt;
|
|
|
|
}
|
|
|
|
|
|
|
|
GSDepthStencil* GetDepthStencil(const GIFRegTEX0& TEX0, int w, int h)
|
|
|
|
{
|
|
|
|
GSDepthStencil* ds = NULL;
|
|
|
|
|
|
|
|
if(ds == NULL)
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSDepthStencil*>::iterator i = m_ds.begin(); i != m_ds.end(); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
GSDepthStencil* ds2 = *i;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(ds2->m_TEX0.TBP0 == TEX0.TBP0)
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
m_ds.splice(m_ds.begin(), m_ds, i);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
ds = ds2;
|
|
|
|
|
|
|
|
ds->m_TEX0 = TEX0;
|
|
|
|
|
|
|
|
ds->Update();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ds == NULL)
|
|
|
|
{
|
|
|
|
ds = CreateDepthStencil();
|
|
|
|
|
|
|
|
ds->m_TEX0 = TEX0;
|
|
|
|
|
|
|
|
if(!ds->Create(w, h))
|
|
|
|
{
|
|
|
|
delete ds;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_ds.push_front(ds);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(m_renderer->m_context->DepthWrite())
|
|
|
|
{
|
|
|
|
ds->m_used = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
GSTexture* GetTexture()
|
|
|
|
{
|
|
|
|
const GIFRegTEX0& TEX0 = m_renderer->m_context->TEX0;
|
|
|
|
const GIFRegCLAMP& CLAMP = m_renderer->m_context->CLAMP;
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
const uint32* clut = m_renderer->m_mem.m_clut;
|
2009-02-09 21:15:56 +00:00
|
|
|
const int pal = GSLocalMemory::m_psm[TEX0.PSM].pal;
|
|
|
|
|
|
|
|
if(pal > 0)
|
|
|
|
{
|
|
|
|
m_renderer->m_mem.m_clut.Read(TEX0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
POSITION pos = m_tex.GetHeadPosition();
|
|
|
|
|
|
|
|
while(pos)
|
|
|
|
{
|
|
|
|
POSITION cur = pos;
|
|
|
|
|
|
|
|
GSSurface* s = m_tex.GetNext(pos);
|
|
|
|
|
|
|
|
if(s->m_TEX0.TBP0 == TEX0.CBP)
|
|
|
|
{
|
|
|
|
m_tex.RemoveAt(cur);
|
|
|
|
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = m_rt.GetHeadPosition();
|
|
|
|
|
|
|
|
while(pos)
|
|
|
|
{
|
|
|
|
POSITION cur = pos;
|
|
|
|
|
|
|
|
GSSurface* s = m_rt.GetNext(pos);
|
|
|
|
|
|
|
|
if(s->m_TEX0.TBP0 == TEX0.CBP)
|
|
|
|
{
|
|
|
|
m_rt.RemoveAt(cur);
|
|
|
|
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = m_ds.GetHeadPosition();
|
|
|
|
|
|
|
|
while(pos)
|
|
|
|
{
|
|
|
|
POSITION cur = pos;
|
|
|
|
|
|
|
|
GSSurface* s = m_ds.GetNext(pos);
|
|
|
|
|
|
|
|
if(s->m_TEX0.TBP0 == TEX0.CBP)
|
|
|
|
{
|
|
|
|
m_ds.RemoveAt(cur);
|
|
|
|
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
GSTexture* t = NULL;
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSTexture*>::iterator i = m_tex.begin(); i != m_tex.end(); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
t = *i;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(GSUtil::HasSharedBits(t->m_TEX0.TBP0, t->m_TEX0.PSM, TEX0.TBP0, TEX0.PSM))
|
|
|
|
{
|
|
|
|
if(TEX0.PSM == t->m_TEX0.PSM && TEX0.TBW == t->m_TEX0.TBW
|
|
|
|
&& TEX0.TW == t->m_TEX0.TW && TEX0.TH == t->m_TEX0.TH
|
2009-05-14 16:41:52 +00:00
|
|
|
&& (m_renderer->m_psrr || (CLAMP.WMS != 3 && t->m_CLAMP.WMS != 3 && CLAMP.WMT != 3 && t->m_CLAMP.WMT != 3 || CLAMP.u64 == t->m_CLAMP.u64))
|
2009-02-09 21:15:56 +00:00
|
|
|
&& (pal == 0 || TEX0.CPSM == t->m_TEX0.CPSM && GSVector4i::compare(t->m_clut, clut, pal * sizeof(clut[0]))))
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
m_tex.splice(m_tex.begin(), m_tex, i);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(t == NULL)
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSRenderTarget*>::iterator i = m_rt.begin(); i != m_rt.end(); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
GSRenderTarget* rt = *i;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
if(rt->m_dirty.empty() && GSUtil::HasSharedBits(rt->m_TEX0.TBP0, rt->m_TEX0.PSM, TEX0.TBP0, TEX0.PSM))
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
t = CreateTexture();
|
|
|
|
|
|
|
|
if(!t->Create(rt))
|
|
|
|
{
|
|
|
|
delete t;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_tex.push_front(t);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(t == NULL)
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSDepthStencil*>::iterator i = m_ds.begin(); i != m_ds.end(); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
GSDepthStencil* ds = *i;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
if(ds->m_dirty.empty() && ds->m_used && GSUtil::HasSharedBits(ds->m_TEX0.TBP0, ds->m_TEX0.PSM, TEX0.TBP0, TEX0.PSM))
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
t = CreateTexture();
|
|
|
|
|
|
|
|
if(!t->Create(ds))
|
|
|
|
{
|
|
|
|
delete t;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_tex.push_front(t);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(t == NULL)
|
|
|
|
{
|
|
|
|
t = CreateTexture();
|
|
|
|
|
|
|
|
if(!t->Create())
|
|
|
|
{
|
|
|
|
delete t;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_tex.push_front(t);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(pal > 0)
|
|
|
|
{
|
|
|
|
int size = pal * sizeof(clut[0]);
|
|
|
|
|
|
|
|
if(t->m_palette)
|
|
|
|
{
|
|
|
|
if(t->m_initpalette)
|
|
|
|
{
|
|
|
|
memcpy(t->m_clut, clut, size);
|
2009-05-14 16:41:52 +00:00
|
|
|
t->m_palette.Update(GSVector4i(0, 0, pal, 1), t->m_clut, size);
|
2009-02-09 21:15:56 +00:00
|
|
|
t->m_initpalette = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(GSVector4i::update(t->m_clut, clut, size))
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
t->m_palette.Update(GSVector4i(0, 0, pal, 1), t->m_clut, size);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(t->m_clut, clut, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t->Update();
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InvalidateTextures(const GIFRegFRAME& FRAME, const GIFRegZBUF& ZBUF)
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSTexture*>::iterator i = m_tex.begin(); i != m_tex.end(); )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
list<GSTexture*>::iterator j = i++;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
GSTexture* t = *j;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(GSUtil::HasSharedBits(FRAME.Block(), FRAME.PSM, t->m_TEX0.TBP0, t->m_TEX0.PSM)
|
|
|
|
|| GSUtil::HasSharedBits(ZBUF.Block(), ZBUF.PSM, t->m_TEX0.TBP0, t->m_TEX0.PSM))
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
m_tex.erase(j);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
void InvalidateVideoMem(const GIFRegBITBLTBUF& BITBLTBUF, const GSVector4i& r)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSTexture*>::iterator i = m_tex.begin(); i != m_tex.end(); )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
list<GSTexture*>::iterator j = i++;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
GSTexture* t = *j;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(GSUtil::HasSharedBits(BITBLTBUF.DBP, BITBLTBUF.DPSM, t->m_TEX0.TBP0, t->m_TEX0.PSM))
|
|
|
|
{
|
|
|
|
if(BITBLTBUF.DBW == t->m_TEX0.TBW && !t->m_rendered)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
t->m_dirty.push_back(GSDirtyRect(r, BITBLTBUF.DPSM));
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
m_tex.erase(j);
|
2009-02-09 21:15:56 +00:00
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(GSUtil::HasCompatibleBits(BITBLTBUF.DPSM, t->m_TEX0.PSM))
|
|
|
|
{
|
|
|
|
if(BITBLTBUF.DBW == t->m_TEX0.TBW && !t->m_rendered)
|
|
|
|
{
|
|
|
|
int rowsize = (int)BITBLTBUF.DBW * 8192;
|
|
|
|
int offset = ((int)BITBLTBUF.DBP - (int)t->m_TEX0.TBP0) * 256;
|
|
|
|
|
|
|
|
if(rowsize > 0 && offset % rowsize == 0)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
int y = m_renderer->m_mem.m_psm[BITBLTBUF.DPSM].pgs.y * offset / rowsize;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
GSVector4i r2(r.left, r.top + y, r.right, r.bottom + y);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
int w = 1 << t->m_TEX0.TW;
|
|
|
|
int h = 1 << t->m_TEX0.TH;
|
|
|
|
|
|
|
|
if(r2.bottom > 0 && r2.top < h && r2.right > 0 && r2.left < w)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
t->m_dirty.push_back(GSDirtyRect(r2, BITBLTBUF.DPSM));
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSRenderTarget*>::iterator i = m_rt.begin(); i != m_rt.end(); )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
list<GSRenderTarget*>::iterator j = i++;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
GSRenderTarget* rt = *j;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(GSUtil::HasSharedBits(BITBLTBUF.DBP, BITBLTBUF.DPSM, rt->m_TEX0.TBP0, rt->m_TEX0.PSM))
|
|
|
|
{
|
|
|
|
if(!found && GSUtil::HasCompatibleBits(BITBLTBUF.DPSM, rt->m_TEX0.PSM))
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
rt->m_dirty.push_back(GSDirtyRect(r, BITBLTBUF.DPSM));
|
2009-02-09 21:15:56 +00:00
|
|
|
rt->m_TEX0.TBW = BITBLTBUF.DBW;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
m_rt.erase(j);
|
2009-02-09 21:15:56 +00:00
|
|
|
delete rt;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(GSUtil::HasSharedBits(BITBLTBUF.DPSM, rt->m_TEX0.PSM) && BITBLTBUF.DBP < rt->m_TEX0.TBP0)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
uint32 rowsize = BITBLTBUF.DBW * 8192;
|
|
|
|
uint32 offset = (uint32)((rt->m_TEX0.TBP0 - BITBLTBUF.DBP) * 256);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(rowsize > 0 && offset % rowsize == 0)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
int y = m_renderer->m_mem.m_psm[BITBLTBUF.DPSM].pgs.y * offset / rowsize;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-03-24 13:13:51 +00:00
|
|
|
if(r.bottom > y)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
// TODO: do not add this rect above too
|
2009-05-14 16:41:52 +00:00
|
|
|
rt->m_dirty.push_back(GSDirtyRect(GSVector4i(r.left, r.top - y, r.right, r.bottom - y), BITBLTBUF.DPSM));
|
2009-02-09 21:15:56 +00:00
|
|
|
rt->m_TEX0.TBW = BITBLTBUF.DBW;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copypaste for ds
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSDepthStencil*>::iterator i = m_ds.begin(); i != m_ds.end(); )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
list<GSDepthStencil*>::iterator j = i++;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
GSDepthStencil* ds = *j;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(GSUtil::HasSharedBits(BITBLTBUF.DBP, BITBLTBUF.DPSM, ds->m_TEX0.TBP0, ds->m_TEX0.PSM))
|
|
|
|
{
|
|
|
|
if(!found && GSUtil::HasCompatibleBits(BITBLTBUF.DPSM, ds->m_TEX0.PSM))
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
ds->m_dirty.push_back(GSDirtyRect(r, BITBLTBUF.DPSM));
|
2009-02-09 21:15:56 +00:00
|
|
|
ds->m_TEX0.TBW = BITBLTBUF.DBW;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
m_ds.erase(j);
|
2009-02-09 21:15:56 +00:00
|
|
|
delete ds;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(GSUtil::HasSharedBits(BITBLTBUF.DPSM, ds->m_TEX0.PSM) && BITBLTBUF.DBP < ds->m_TEX0.TBP0)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
uint32 rowsize = BITBLTBUF.DBW * 8192;
|
|
|
|
uint32 offset = (uint32)((ds->m_TEX0.TBP0 - BITBLTBUF.DBP) * 256);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(rowsize > 0 && offset % rowsize == 0)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
int y = m_renderer->m_mem.m_psm[BITBLTBUF.DPSM].pgs.y * offset / rowsize;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-03-24 13:13:51 +00:00
|
|
|
if(r.bottom > y)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
// TODO: do not add this rect above too
|
2009-05-14 16:41:52 +00:00
|
|
|
ds->m_dirty.push_back(GSDirtyRect(GSVector4i(r.left, r.top - y, r.right, r.bottom - y), BITBLTBUF.DPSM));
|
2009-02-09 21:15:56 +00:00
|
|
|
ds->m_TEX0.TBW = BITBLTBUF.DBW;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
void InvalidateLocalMem(const GIFRegBITBLTBUF& BITBLTBUF, const GSVector4i& r)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
for(list<GSRenderTarget*>::iterator i = m_rt.begin(); i != m_rt.end(); )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
list<GSRenderTarget*>::iterator j = i++;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
GSRenderTarget* rt = *j;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(GSUtil::HasSharedBits(BITBLTBUF.SBP, BITBLTBUF.SPSM, rt->m_TEX0.TBP0, rt->m_TEX0.PSM))
|
|
|
|
{
|
|
|
|
if(GSUtil::HasCompatibleBits(BITBLTBUF.SPSM, rt->m_TEX0.PSM))
|
|
|
|
{
|
|
|
|
rt->Read(r);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(BITBLTBUF.SPSM == PSM_PSMCT32 && (rt->m_TEX0.PSM == PSM_PSMCT16 || rt->m_TEX0.PSM == PSM_PSMCT16S))
|
|
|
|
{
|
|
|
|
// ffx-2 riku changing to her default (shoots some reflecting glass at the end), 16-bit rt read as 32-bit
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
rt->Read(GSVector4i(r.left, r.top, r.right, r.top + (r.bottom - r.top) * 2));
|
2009-02-09 21:15:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
m_rt.erase(j);
|
2009-02-09 21:15:56 +00:00
|
|
|
delete rt;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
// no good, ffx does a lot of readback after exiting menu, at 0x02f00 this wrongly finds rt 0x02100 (0,448 - 512,480)
|
|
|
|
|
|
|
|
GSRenderTarget* rt2 = NULL;
|
|
|
|
int ymin = INT_MAX;
|
|
|
|
|
|
|
|
pos = m_rt.GetHeadPosition();
|
|
|
|
|
|
|
|
while(pos)
|
|
|
|
{
|
|
|
|
GSRenderTarget* rt = m_rt.GetNext(pos);
|
|
|
|
|
|
|
|
if(HasSharedBits(BITBLTBUF.SPSM, rt->m_TEX0.PSM) && BITBLTBUF.SBP > rt->m_TEX0.TBP0)
|
|
|
|
{
|
|
|
|
// ffx2 pause screen background
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
uint32 rowsize = BITBLTBUF.SBW * 8192;
|
|
|
|
uint32 offset = (uint32)((BITBLTBUF.SBP - rt->m_TEX0.TBP0) * 256);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(rowsize > 0 && offset % rowsize == 0)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
int y = m_renderer->m_mem.m_psm[BITBLTBUF.SPSM].pgs.y * offset / rowsize;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(y < ymin && y < 512)
|
|
|
|
{
|
|
|
|
rt2 = rt;
|
|
|
|
ymin = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rt2)
|
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
rt2->Read(GSVector4i(r.left, r.top + ymin, r.right, r.bottom + ymin));
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: ds
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncAge()
|
|
|
|
{
|
|
|
|
RecycleByAge(m_tex, 2);
|
|
|
|
RecycleByAge(m_rt);
|
|
|
|
RecycleByAge(m_ds);
|
|
|
|
}
|
|
|
|
};
|