2010-04-25 00:31:27 +00:00
/*
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 .
2010-04-25 00:31:27 +00:00
*
2009-02-09 21:15:56 +00:00
* 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 .
2010-04-25 00:31:27 +00:00
*
2009-02-09 21:15:56 +00:00
* You should have received a copy of the GNU General Public License
* along with GNU Make ; see the file COPYING . If not , write to
2012-09-09 18:16:11 +00:00
* the Free Software Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA USA .
2009-02-09 21:15:56 +00:00
* http : //www.gnu.org/copyleft/gpl.html
*
*/
2011-02-19 03:36:30 +00:00
# include "stdafx.h"
2009-02-09 21:15:56 +00:00
# include "GSTextureCache.h"
2009-05-22 01:22:52 +00:00
GSTextureCache : : GSTextureCache ( GSRenderer * r )
: m_renderer ( r )
{
2012-06-19 01:12:01 +00:00
m_spritehack = ! ! theApp . GetConfig ( " UserHacks " , 0 ) ? theApp . GetConfig ( " UserHacks_SpriteHack " , 0 ) : 0 ;
2014-10-07 17:11:43 +00:00
2012-03-06 20:01:27 +00:00
UserHacks_HalfPixelOffset = ! ! theApp . GetConfig ( " UserHacks " , 0 ) & & ! ! theApp . GetConfig ( " UserHacks_HalfPixelOffset " , 0 ) ;
2009-07-06 16:35:06 +00:00
m_paltex = ! ! theApp . GetConfig ( " paltex " , 0 ) ;
2011-02-19 03:36:30 +00:00
2015-06-10 10:32:00 +00:00
m_can_convert_depth = theApp . GetConfig ( " Renderer " , 12 ) = = 12 ? theApp . GetConfig ( " texture_cache_depth " , 1 ) : 0 ;
2011-02-19 03:36:30 +00:00
m_temp = ( uint8 * ) _aligned_malloc ( 1024 * 1024 * sizeof ( uint32 ) , 32 ) ;
2009-05-22 01:22:52 +00:00
}
GSTextureCache : : ~ GSTextureCache ( )
{
RemoveAll ( ) ;
2011-02-19 03:36:30 +00:00
_aligned_free ( m_temp ) ;
2009-05-22 01:22:52 +00:00
}
2015-05-13 18:00:25 +00:00
void GSTextureCache : : RemovePartial ( )
{
//m_src.RemoveAll();
for ( int type = 0 ; type < 2 ; type + + )
{
for_each ( m_dst [ type ] . begin ( ) , m_dst [ type ] . end ( ) , delete_object ( ) ) ;
m_dst [ type ] . clear ( ) ;
}
}
2009-05-22 01:22:52 +00:00
void GSTextureCache : : RemoveAll ( )
{
2009-06-27 03:32:33 +00:00
m_src . RemoveAll ( ) ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
for ( int type = 0 ; type < 2 ; type + + )
2009-05-22 01:22:52 +00:00
{
2009-07-04 17:37:06 +00:00
for_each ( m_dst [ type ] . begin ( ) , m_dst [ type ] . end ( ) , delete_object ( ) ) ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
m_dst [ type ] . clear ( ) ;
2009-05-22 01:22:52 +00:00
}
}
2009-06-27 03:32:33 +00:00
GSTextureCache : : Source * GSTextureCache : : LookupSource ( const GIFRegTEX0 & TEX0 , const GIFRegTEXA & TEXA , const GSVector4i & r )
2009-05-22 01:22:52 +00:00
{
2009-06-27 03:32:33 +00:00
const GSLocalMemory : : psm_t & psm = GSLocalMemory : : m_psm [ TEX0 . PSM ] ;
2014-04-14 18:25:02 +00:00
//const GSLocalMemory::psm_t& cpsm = psm.pal > 0 ? GSLocalMemory::m_psm[TEX0.CPSM] : psm;
2012-06-19 00:12:35 +00:00
GIFRegTEXA plainTEXA ;
plainTEXA . AEM = 1 ;
plainTEXA . TA0 = 0 ;
plainTEXA . TA1 = 0x80 ;
m_renderer - > m_mem . m_clut . Read32 ( TEX0 , plainTEXA ) ;
2009-06-27 03:32:33 +00:00
const uint32 * clut = m_renderer - > m_mem . m_clut ;
2010-04-25 00:31:27 +00:00
2009-06-27 03:32:33 +00:00
Source * src = NULL ;
2009-05-22 01:22:52 +00:00
2009-07-12 13:46:05 +00:00
list < Source * > & m = m_src . m_map [ TEX0 . TBP0 > > 5 ] ;
2009-05-22 01:22:52 +00:00
2009-07-12 13:46:05 +00:00
for ( list < Source * > : : iterator i = m . begin ( ) ; i ! = m . end ( ) ; i + + )
2009-05-22 01:22:52 +00:00
{
2009-07-12 13:46:05 +00:00
Source * s = * i ;
2009-05-22 01:22:52 +00:00
2009-07-04 15:14:04 +00:00
if ( ( ( TEX0 . u32 [ 0 ] ^ s - > m_TEX0 . u32 [ 0 ] ) | ( ( TEX0 . u32 [ 1 ] ^ s - > m_TEX0 . u32 [ 1 ] ) & 3 ) ) ! = 0 ) // TBP0 TBW PSM TW TH
2009-05-22 01:22:52 +00:00
{
2009-06-27 03:32:33 +00:00
continue ;
2009-05-22 01:22:52 +00:00
}
2009-07-22 03:55:28 +00:00
if ( s - > m_palette = = NULL & & psm . pal > 0 & & ! GSVector4i : : compare64 ( clut , s - > m_clut , psm . pal * sizeof ( clut [ 0 ] ) ) )
2009-05-22 01:22:52 +00:00
{
2009-06-27 03:32:33 +00:00
continue ;
2009-05-22 01:22:52 +00:00
}
2009-07-12 13:46:05 +00:00
m . splice ( m . begin ( ) , m , i ) ;
2009-06-27 03:32:33 +00:00
src = s ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
break ;
2009-05-22 01:22:52 +00:00
}
2009-06-27 03:32:33 +00:00
Target * dst = NULL ;
2015-05-13 18:03:04 +00:00
bool half_right = false ;
2009-05-22 01:22:52 +00:00
2011-12-16 19:13:58 +00:00
# ifdef DISABLE_HW_TEXTURE_CACHE
2011-05-09 01:36:57 +00:00
if ( 0 )
# else
2009-06-27 03:32:33 +00:00
if ( src = = NULL )
2011-05-09 01:36:57 +00:00
# endif
2009-05-22 01:22:52 +00:00
{
2009-07-04 15:14:04 +00:00
uint32 bp = TEX0 . TBP0 ;
uint32 psm = TEX0 . PSM ;
2011-12-23 20:58:10 +00:00
// Arc the Lad finds the wrong surface here when looking for a depth stencil.
// Since we're currently not caching depth stencils (check ToDo in CreateSource) we should not look for it here.
2014-10-07 17:11:43 +00:00
2011-12-23 20:58:10 +00:00
// (Simply not doing this code at all makes a lot of previsouly missing stuff show (but breaks pretty much everything
// else.)
2014-10-07 17:11:43 +00:00
2015-06-05 21:50:38 +00:00
for ( list < Target * > : : iterator i = m_dst [ RenderTarget ] . begin ( ) ; i ! = m_dst [ RenderTarget ] . end ( ) ; i + + )
2009-05-22 01:22:52 +00:00
{
2015-06-05 21:50:38 +00:00
Target * t = * i ;
2009-05-22 01:22:52 +00:00
2015-06-05 21:50:38 +00:00
if ( t - > m_used & & t - > m_dirty . empty ( ) ) {
if ( GSUtil : : HasSharedBits ( bp , psm , t - > m_TEX0 . TBP0 , t - > m_TEX0 . PSM ) ) {
dst = t ;
2015-05-13 18:03:04 +00:00
2015-06-05 21:50:38 +00:00
break ;
2015-05-13 18:03:04 +00:00
2015-06-05 21:50:38 +00:00
} else if ( ( t - > m_TEX0 . TBW > = 16 ) & & GSUtil : : HasSharedBits ( bp , psm , t - > m_TEX0 . TBP0 + t - > m_TEX0 . TBW * 0x10 , t - > m_TEX0 . PSM ) ) {
// Detect half of the render target (fix snow engine game)
// Target Page (8KB) have always a width of 64 pixels
// Half of the Target is TBW/2 pages * 8KB / (1 block * 256B) = 0x10
half_right = true ;
dst = t ;
2009-05-22 01:22:52 +00:00
2015-06-05 21:50:38 +00:00
break ;
}
}
}
2015-06-07 16:42:30 +00:00
if ( dst = = NULL & & CanConvertDepth ( ) ) {
2015-06-05 21:50:38 +00:00
// Let's try a trick to avoid to use wrongly a depth buffer
// Unfortunately, I don't have any Arc the Lad testcase
//
// 1/ Check only current frame, I guess it is only used as a postprocessing effect
for ( list < Target * > : : iterator i = m_dst [ DepthStencil ] . begin ( ) ; i ! = m_dst [ DepthStencil ] . end ( ) ; i + + ) {
Target * t = * i ;
if ( ! t - > m_age & & t - > m_used & & t - > m_dirty . empty ( ) & & GSUtil : : HasSharedBits ( bp , psm , t - > m_TEX0 . TBP0 , t - > m_TEX0 . PSM ) )
{
dst = t ;
break ;
2009-06-27 03:32:33 +00:00
}
2009-05-22 01:22:52 +00:00
}
}
}
2009-06-27 03:32:33 +00:00
if ( src = = NULL )
2009-05-22 01:22:52 +00:00
{
2015-05-13 06:49:32 +00:00
# ifdef ENABLE_OGL_DEBUG
if ( dst ) {
2015-06-05 21:37:06 +00:00
GL_CACHE ( " TC: dst %s hit (%s): %d (0x%x) " , dst - > m_type ? " Depth " : " Color " , half_right ? " half " : " full " ,
2015-05-13 06:49:32 +00:00
dst - > m_texture ? dst - > m_texture - > GetID ( ) : 0 ,
2015-05-16 17:28:22 +00:00
TEX0 . TBP0 ) ;
2015-05-13 06:49:32 +00:00
} else {
2015-05-16 17:28:22 +00:00
GL_CACHE ( " TC: src miss (0x%x) " , TEX0 . TBP0 ) ;
2015-05-13 06:49:32 +00:00
}
# endif
2015-05-13 18:03:04 +00:00
src = CreateSource ( TEX0 , TEXA , dst , half_right ) ;
2009-05-22 01:22:52 +00:00
2009-07-31 23:59:06 +00:00
if ( src = = NULL )
2009-05-22 01:22:52 +00:00
{
return NULL ;
}
2015-06-05 21:37:06 +00:00
2015-05-13 06:49:32 +00:00
# ifdef ENABLE_OGL_DEBUG
} else {
2015-05-16 17:28:22 +00:00
GL_CACHE ( " TC: src hit: %d (0x%x) " ,
2015-05-13 06:49:32 +00:00
src - > m_texture ? src - > m_texture - > GetID ( ) : 0 ,
2015-05-16 17:28:22 +00:00
TEX0 . TBP0 ) ;
2015-05-13 06:49:32 +00:00
# endif
2009-05-22 01:22:52 +00:00
}
2012-06-11 10:57:32 +00:00
if ( src - > m_palette )
2009-05-22 01:22:52 +00:00
{
2009-06-27 03:32:33 +00:00
int size = psm . pal * sizeof ( clut [ 0 ] ) ;
2009-05-22 01:22:52 +00:00
2012-06-11 10:57:32 +00:00
if ( src - > m_initpalette | | ! GSVector4i : : update ( src - > m_clut , clut , size ) )
2009-06-17 11:24:42 +00:00
{
2012-06-11 10:57:32 +00:00
src - > m_palette - > Update ( GSVector4i ( 0 , 0 , psm . pal , 1 ) , src - > m_clut , size ) ;
src - > m_initpalette = false ;
2009-06-17 11:24:42 +00:00
}
2009-06-27 03:32:33 +00:00
}
2009-06-17 11:24:42 +00:00
2011-04-25 18:18:21 +00:00
src - > Update ( r ) ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
m_src . m_used = true ;
2009-06-09 10:13:28 +00:00
2009-06-27 03:32:33 +00:00
return src ;
}
2009-06-20 20:28:36 +00:00
2009-07-31 23:59:06 +00:00
GSTextureCache : : Target * GSTextureCache : : LookupTarget ( const GIFRegTEX0 & TEX0 , int w , int h , int type , bool used )
2009-06-27 03:32:33 +00:00
{
2009-07-04 15:14:04 +00:00
uint32 bp = TEX0 . TBP0 ;
2009-06-27 03:32:33 +00:00
Target * dst = NULL ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
for ( list < Target * > : : iterator i = m_dst [ type ] . begin ( ) ; i ! = m_dst [ type ] . end ( ) ; i + + )
2009-05-22 01:22:52 +00:00
{
2009-06-27 03:32:33 +00:00
Target * t = * i ;
2009-05-22 01:22:52 +00:00
2009-07-04 15:14:04 +00:00
if ( bp = = t - > m_TEX0 . TBP0 )
2009-06-27 03:32:33 +00:00
{
m_dst [ type ] . splice ( m_dst [ type ] . begin ( ) , m_dst [ type ] , i ) ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
dst = t ;
2009-05-22 01:22:52 +00:00
2015-06-06 11:56:08 +00:00
#if 0
2015-05-26 09:16:49 +00:00
// Likely the root cause of tons and tons of bug
if ( dst - > m_TEX0 . PSM ! = TEX0 . PSM ) {
GL_INS ( " TC: ERROR: use a target with format 0x%x as 0x%x without any conversion " , dst - > m_TEX0 . PSM , TEX0 . PSM ) ;
}
# endif
2009-07-31 23:59:06 +00:00
dst - > m_TEX0 = TEX0 ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
break ;
2009-05-22 01:22:52 +00:00
}
}
2015-06-05 21:51:13 +00:00
if ( dst ) {
GL_CACHE ( " TC: Lookup Target(T%d) %dx%d, hit: %d (0x%x) " , type , w , h , dst - > m_texture - > GetID ( ) , bp ) ;
dst - > Update ( ) ;
2015-06-08 07:44:47 +00:00
} else if ( CanConvertDepth ( ) ) {
2015-05-15 13:12:49 +00:00
2015-06-08 07:44:47 +00:00
int rev_type = ( type = = DepthStencil ) ? RenderTarget : DepthStencil ;
GSVector4 sRect ( 0 , 0 , 1.0 , 1.0 ) ;
GSVector4 dRect ( 0 , 0 , w , h ) ;
2015-06-05 21:51:13 +00:00
2015-06-08 07:44:47 +00:00
// Depth stencil/RT can be an older RT/DS but only check recent RT/DS to avoid to pick
// some bad data.
for ( list < Target * > : : iterator i = m_dst [ rev_type ] . begin ( ) ; i ! = m_dst [ rev_type ] . end ( ) ; i + + )
{
Target * t = * i ;
if ( ! t - > m_age & & bp = = t - > m_TEX0 . TBP0 )
{
dst = CreateTarget ( TEX0 , w , h , type ) ;
if ( type = = DepthStencil ) {
2015-06-05 21:51:13 +00:00
GL_CACHE ( " TC: Lookup Target(Depth) %dx%d, hit Color (0x%x) " , w , h , bp ) ;
m_renderer - > m_dev - > StretchRect ( t - > m_texture , sRect , dst - > m_texture , dRect , 12 , false ) ;
2015-06-08 07:44:47 +00:00
} else {
GL_CACHE ( " TC: Lookup Target(Color) %dx%d, hit Depth (0x%x) " , w , h , bp ) ;
m_renderer - > m_dev - > StretchRect ( t - > m_texture , sRect , dst - > m_texture , dRect , 11 , false ) ;
2015-06-05 21:51:13 +00:00
}
2015-06-08 07:44:47 +00:00
break ;
2015-06-05 21:51:13 +00:00
}
}
2015-06-08 07:44:47 +00:00
}
2009-06-27 03:32:33 +00:00
2015-06-08 07:44:47 +00:00
if ( dst = = NULL )
{
GL_CACHE ( " TC: Lookup Target(T%d) %dx%d, miss (0x%x) " , type , w , h , bp ) ;
2015-05-15 13:12:49 +00:00
2015-06-08 07:44:47 +00:00
dst = CreateTarget ( TEX0 , w , h , type ) ;
2015-06-05 21:51:13 +00:00
2015-06-08 07:44:47 +00:00
if ( dst = = NULL )
return NULL ;
2009-05-22 01:22:52 +00:00
}
2009-06-27 03:32:33 +00:00
if ( m_renderer - > CanUpscale ( ) )
2009-05-22 01:22:52 +00:00
{
2011-02-19 03:36:30 +00:00
int multiplier = m_renderer - > GetUpscaleMultiplier ( ) ;
2011-02-18 01:56:05 +00:00
2011-02-19 03:36:30 +00:00
if ( multiplier > 1 ) // it's limited to a maximum of 4 on reading the config
2009-05-22 01:22:52 +00:00
{
2011-02-18 01:56:05 +00:00
dst - > m_texture - > SetScale ( GSVector2 ( ( float ) multiplier , ( float ) multiplier ) ) ;
2009-06-27 03:32:33 +00:00
}
2009-10-21 22:57:20 +00:00
else
2009-06-27 03:32:33 +00:00
{
2009-10-21 22:57:20 +00:00
GSVector4i fr = m_renderer - > GetFrameRect ( ) ;
2009-12-30 13:27:28 +00:00
int ww = ( int ) ( fr . left + m_renderer - > GetDisplayRect ( ) . width ( ) ) ;
2009-10-21 22:57:20 +00:00
int hh = ( int ) ( fr . top + m_renderer - > GetDisplayRect ( ) . height ( ) ) ;
if ( hh < = m_renderer - > GetDeviceSize ( ) . y / 2 )
{
hh * = 2 ;
}
2015-06-05 21:37:06 +00:00
// Gregory: I'm sure this sillyness is related to the usage of a 32bits
// buffer as a 16 bits format. In this case the height of the buffer is
// multiplyed by 2 (Hence a scissor bigger than the RT)
2011-02-18 01:56:05 +00:00
// This vp2 fix doesn't work most of the time
2009-10-21 22:57:20 +00:00
if ( hh < 512 & & m_renderer - > m_context - > SCISSOR . SCAY1 = = 511 ) // vp2
{
hh = 512 ;
}
if ( ww > 0 & & hh > 0 )
{
2015-05-28 20:41:02 +00:00
dst - > m_texture - > SetScale ( GSVector2 ( ( float ) w / ww , ( float ) h / hh ) ) ;
2009-10-21 22:57:20 +00:00
}
2009-05-22 01:22:52 +00:00
}
}
2009-06-27 03:32:33 +00:00
if ( used )
{
dst - > m_used = true ;
}
2009-06-09 10:13:28 +00:00
2009-06-27 03:32:33 +00:00
return dst ;
2009-05-22 01:22:52 +00:00
}
2009-07-31 23:59:06 +00:00
GSTextureCache : : Target * GSTextureCache : : LookupTarget ( const GIFRegTEX0 & TEX0 , int w , int h )
{
uint32 bp = TEX0 . TBP0 ;
Target * dst = NULL ;
for ( list < Target * > : : iterator i = m_dst [ RenderTarget ] . begin ( ) ; i ! = m_dst [ RenderTarget ] . end ( ) ; i + + )
{
Target * t = * i ;
if ( bp = = t - > m_TEX0 . TBP0 )
{
dst = t ;
2015-05-16 17:28:22 +00:00
GL_CACHE ( " TC: Lookup Frame %dx%d, perfect hit: %d (0x%x) " , w , h , dst - > m_texture - > GetID ( ) , bp ) ;
2015-05-15 13:12:49 +00:00
2009-07-31 23:59:06 +00:00
break ;
}
2010-04-25 00:31:27 +00:00
else
2009-07-31 23:59:06 +00:00
{
// HACK: try to find something close to the base pointer
2014-05-06 07:25:00 +00:00
if ( t - > m_TEX0 . TBP0 < = bp & & bp < t - > m_TEX0 . TBP0 + 0xe00UL & & ( ! dst | | t - > m_TEX0 . TBP0 > = dst - > m_TEX0 . TBP0 ) )
2009-07-31 23:59:06 +00:00
{
2015-05-17 10:19:08 +00:00
GL_CACHE ( " TC: Lookup Frame %dx%d, close hit: %d (0x%x, took 0x%x) " , w , h , t - > m_texture - > GetID ( ) , bp , t - > m_TEX0 . TBP0 ) ;
2009-07-31 23:59:06 +00:00
dst = t ;
}
}
}
if ( dst = = NULL )
{
2015-05-16 17:28:22 +00:00
GL_CACHE ( " TC: Lookup Frame %dx%d, miss (0x%x) " , w , h , bp ) ;
2015-05-15 13:12:49 +00:00
2009-07-31 23:59:06 +00:00
dst = CreateTarget ( TEX0 , w , h , RenderTarget ) ;
2015-05-15 13:12:49 +00:00
2009-07-31 23:59:06 +00:00
if ( dst = = NULL )
{
return NULL ;
}
2012-01-18 11:47:31 +00:00
m_renderer - > m_dev - > ClearRenderTarget ( dst - > m_texture , 0 ) ; // new frame buffers after reset should be cleared, don't display memory garbage
2009-07-31 23:59:06 +00:00
}
else
{
dst - > Update ( ) ;
}
dst - > m_used = true ;
return dst ;
}
2015-06-05 21:37:06 +00:00
// Goal: Depth And Target at the same address is not possible. On GS it is
// the same memory but not on the Dx/GL. Therefore a write to the Depth/Target
// must invalidate the Target/Depth respectively
void GSTextureCache : : InvalidateVideoMemType ( int type , uint32 bp )
{
2015-06-07 16:42:30 +00:00
if ( ! CanConvertDepth ( ) )
return ;
2015-06-05 21:37:06 +00:00
for ( list < Target * > : : iterator i = m_dst [ type ] . begin ( ) ; i ! = m_dst [ type ] . end ( ) ; i + + )
{
Target * t = * i ;
if ( bp = = t - > m_TEX0 . TBP0 )
{
GL_CACHE ( " TC: InvalidateVideoMemType: Remove Target(T%d) %d (0x%x) " , type ,
t - > m_texture ? t - > m_texture - > GetID ( ) : 0 ,
t - > m_TEX0 . TBP0 ) ;
m_dst [ type ] . erase ( i ) ;
delete t ;
break ;
}
}
}
2014-10-24 18:49:30 +00:00
// Goal: invalidate data sent to the GPU when the source (GS memory) is modified
// Called each time you want to write to the GS memory
2015-05-15 18:40:09 +00:00
void GSTextureCache : : InvalidateVideoMem ( GSOffset * off , const GSVector4i & rect , bool target )
2009-05-22 01:22:52 +00:00
{
2015-05-15 18:40:09 +00:00
if ( ! off ) return ; // Fixme. Crashes Dual Hearts, maybe others as well. Was fine before r1549.
2010-04-25 00:31:27 +00:00
2015-05-15 18:40:09 +00:00
uint32 bp = off - > bp ;
uint32 bw = off - > bw ;
uint32 psm = off - > psm ;
2009-07-04 15:14:04 +00:00
2009-06-27 03:32:33 +00:00
if ( ! target )
2009-05-22 01:22:52 +00:00
{
2014-10-24 18:49:30 +00:00
// Remove Source that have same BP as the render target (color&dss)
// rendering will dirty the copy
2009-07-12 13:46:05 +00:00
const list < Source * > & m = m_src . m_map [ bp > > 5 ] ;
2009-05-22 01:22:52 +00:00
2009-07-12 13:46:05 +00:00
for ( list < Source * > : : const_iterator i = m . begin ( ) ; i ! = m . end ( ) ; )
2009-05-22 01:22:52 +00:00
{
2009-07-12 13:46:05 +00:00
list < Source * > : : const_iterator j = i + + ;
2009-05-22 01:22:52 +00:00
2009-07-12 13:46:05 +00:00
Source * s = * j ;
2009-06-27 03:32:33 +00:00
2009-07-04 15:14:04 +00:00
if ( GSUtil : : HasSharedBits ( bp , psm , s - > m_TEX0 . TBP0 , s - > m_TEX0 . PSM ) )
2009-05-22 01:22:52 +00:00
{
2009-06-27 03:32:33 +00:00
m_src . RemoveAt ( s ) ;
2009-05-22 01:22:52 +00:00
}
}
2015-05-13 18:01:25 +00:00
2015-06-03 06:40:49 +00:00
if ( bw > = 16 ) {
// Detect half of the render target (fix snow engine game)
// Target Page (8KB) have always a width of 64 pixels
// Half of the Target is TBW/2 pages * 8KB / (1 block * 256B) = 0x10
uint32 bbp = bp + bw * 0x10 ;
2015-05-13 18:01:25 +00:00
const list < Source * > & m = m_src . m_map [ bbp > > 5 ] ;
for ( list < Source * > : : const_iterator i = m . begin ( ) ; i ! = m . end ( ) ; )
{
list < Source * > : : const_iterator j = i + + ;
Source * s = * j ;
if ( GSUtil : : HasSharedBits ( bbp , psm , s - > m_TEX0 . TBP0 , s - > m_TEX0 . PSM ) )
{
m_src . RemoveAt ( s ) ;
}
}
}
2009-06-27 03:32:33 +00:00
}
2011-12-23 15:53:53 +00:00
GSVector4i r ;
2012-01-06 01:20:01 +00:00
uint32 * pages = ( uint32 * ) m_temp ;
2014-10-07 17:11:43 +00:00
2015-05-15 18:40:09 +00:00
off - > GetPages ( rect , pages , & r ) ;
2011-12-23 15:53:53 +00:00
2009-07-04 15:14:04 +00:00
bool found = false ;
2012-01-05 02:40:24 +00:00
for ( const uint32 * p = pages ; * p ! = GSOffset : : EOP ; p + + )
2009-06-27 03:32:33 +00:00
{
2011-12-23 15:53:53 +00:00
uint32 page = * p ;
2009-06-27 03:32:33 +00:00
2011-12-23 15:53:53 +00:00
const list < Source * > & m = m_src . m_map [ page ] ;
for ( list < Source * > : : const_iterator i = m . begin ( ) ; i ! = m . end ( ) ; )
2009-05-22 01:22:52 +00:00
{
2011-12-23 15:53:53 +00:00
list < Source * > : : const_iterator j = i + + ;
2009-06-27 03:32:33 +00:00
2011-12-23 15:53:53 +00:00
Source * s = * j ;
if ( GSUtil : : HasSharedBits ( psm , s - > m_TEX0 . PSM ) )
2009-05-22 01:22:52 +00:00
{
2011-12-31 15:41:07 +00:00
uint32 * RESTRICT valid = s - > m_valid ;
2011-12-23 15:53:53 +00:00
bool b = bp = = s - > m_TEX0 . TBP0 ;
2009-05-22 01:22:52 +00:00
2011-12-23 15:53:53 +00:00
if ( ! s - > m_target )
2009-05-22 01:22:52 +00:00
{
2014-10-24 18:49:30 +00:00
// Invalidate data of input texture
2011-12-23 15:53:53 +00:00
if ( s - > m_repeating )
2009-05-22 01:22:52 +00:00
{
2011-12-31 15:41:07 +00:00
vector < GSVector2i > & l = s - > m_p2t [ page ] ;
2014-10-07 17:11:43 +00:00
2011-12-31 15:41:07 +00:00
for ( vector < GSVector2i > : : iterator k = l . begin ( ) ; k ! = l . end ( ) ; k + + )
2009-06-27 03:32:33 +00:00
{
2011-12-31 15:41:07 +00:00
valid [ k - > x ] & = k - > y ;
2009-06-27 03:32:33 +00:00
}
2009-05-22 01:22:52 +00:00
}
2011-12-23 15:53:53 +00:00
else
{
2011-12-31 15:41:07 +00:00
valid [ page ] = 0 ;
2011-12-23 15:53:53 +00:00
}
s - > m_complete = false ;
2015-03-17 08:39:50 +00:00
found | = b ;
2011-12-23 15:53:53 +00:00
}
else
{
2014-10-24 18:49:30 +00:00
// render target used as input texture
2011-12-23 15:53:53 +00:00
// TODO
if ( b )
{
m_src . RemoveAt ( s ) ;
}
2009-05-22 01:22:52 +00:00
}
}
}
}
2009-06-27 03:32:33 +00:00
if ( ! target ) return ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
for ( int type = 0 ; type < 2 ; type + + )
{
for ( list < Target * > : : iterator i = m_dst [ type ] . begin ( ) ; i ! = m_dst [ type ] . end ( ) ; )
2009-05-22 01:22:52 +00:00
{
2009-06-27 03:32:33 +00:00
list < Target * > : : iterator j = i + + ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
Target * t = * j ;
2009-05-22 01:22:52 +00:00
2009-07-04 15:14:04 +00:00
if ( GSUtil : : HasSharedBits ( bp , psm , t - > m_TEX0 . TBP0 , t - > m_TEX0 . PSM ) )
2009-05-22 01:22:52 +00:00
{
2009-07-04 15:14:04 +00:00
if ( ! found & & GSUtil : : HasCompatibleBits ( psm , t - > m_TEX0 . PSM ) )
2009-05-22 01:22:52 +00:00
{
2009-07-04 15:14:04 +00:00
t - > m_dirty . push_back ( GSDirtyRect ( r , psm ) ) ;
t - > m_TEX0 . TBW = bw ;
2009-06-27 03:32:33 +00:00
}
else
{
m_dst [ type ] . erase ( j ) ;
2015-06-05 21:37:06 +00:00
GL_CACHE ( " TC: Remove Target(T%d) %d (0x%x) " , type ,
2015-05-16 15:32:24 +00:00
t - > m_texture ? t - > m_texture - > GetID ( ) : 0 ,
2015-05-16 17:28:22 +00:00
t - > m_TEX0 . TBP0 ) ;
2009-06-27 03:32:33 +00:00
delete t ;
2009-05-22 01:22:52 +00:00
continue ;
}
}
2009-07-04 15:14:04 +00:00
if ( GSUtil : : HasSharedBits ( psm , t - > m_TEX0 . PSM ) & & bp < t - > m_TEX0 . TBP0 )
2009-05-22 01:22:52 +00:00
{
2009-07-04 15:14:04 +00:00
uint32 rowsize = bw * 8192 ;
uint32 offset = ( uint32 ) ( ( t - > m_TEX0 . TBP0 - bp ) * 256 ) ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
if ( rowsize > 0 & & offset % rowsize = = 0 )
2009-05-22 01:22:52 +00:00
{
2009-07-04 15:14:04 +00:00
int y = GSLocalMemory : : m_psm [ psm ] . pgs . y * offset / rowsize ;
2009-06-27 03:32:33 +00:00
if ( r . bottom > y )
{
// TODO: do not add this rect above too
2009-07-04 15:14:04 +00:00
t - > m_dirty . push_back ( GSDirtyRect ( GSVector4i ( r . left , r . top - y , r . right , r . bottom - y ) , psm ) ) ;
t - > m_TEX0 . TBW = bw ;
2009-06-27 03:32:33 +00:00
continue ;
}
2009-05-22 01:22:52 +00:00
}
}
}
}
}
2014-10-24 18:49:30 +00:00
// Goal: retrive the data from the GPU to the GS memory.
// Called each time you want to read from the GS memory
2015-05-15 18:40:09 +00:00
void GSTextureCache : : InvalidateLocalMem ( GSOffset * off , const GSVector4i & r )
2009-05-22 01:22:52 +00:00
{
2015-05-15 18:40:09 +00:00
uint32 bp = off - > bp ;
uint32 psm = off - > psm ;
//uint32 bw = off->bw;
2010-02-26 17:03:58 +00:00
// No depth handling please.
if ( psm = = PSM_PSMZ32 | | psm = = PSM_PSMZ24 | | psm = = PSM_PSMZ16 | | psm = = PSM_PSMZ16S )
return ;
2009-07-04 15:14:04 +00:00
2011-05-09 22:14:35 +00:00
// This is a shorter but potentially slower version of the below, commented out code.
// It works for all the games mentioned below and fixes a couple of other ones as well
2011-05-10 16:02:52 +00:00
// (Busen0: Wizardry and Chaos Legion).
2011-05-09 22:14:35 +00:00
// Also in a few games the below code ran the Grandia3 case when it shouldn't :p
2009-06-27 03:32:33 +00:00
for ( list < Target * > : : iterator i = m_dst [ RenderTarget ] . begin ( ) ; i ! = m_dst [ RenderTarget ] . end ( ) ; )
2009-05-22 01:22:52 +00:00
{
2009-06-27 03:32:33 +00:00
list < Target * > : : iterator j = i + + ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
Target * t = * j ;
2009-05-22 01:22:52 +00:00
2010-02-26 17:03:58 +00:00
if ( t - > m_TEX0 . PSM ! = PSM_PSMZ32 & & t - > m_TEX0 . PSM ! = PSM_PSMZ24 & & t - > m_TEX0 . PSM ! = PSM_PSMZ16 & & t - > m_TEX0 . PSM ! = PSM_PSMZ16S )
2009-05-22 01:22:52 +00:00
{
2010-02-26 17:03:58 +00:00
if ( GSUtil : : HasSharedBits ( bp , psm , t - > m_TEX0 . TBP0 , t - > m_TEX0 . PSM ) )
2009-05-22 01:22:52 +00:00
{
2014-10-24 18:49:30 +00:00
// GH Note: Read will do a StretchRect and then will sizzle data to the GS memory
// t->m_valid will do the full target texture whereas r.intersect(t->m_valid) will be limited
// to the useful part for the transfer.
// 1/ Logically intersect must be enough, except if we miss some call to InvalidateLocalMem
// or it need the depth part too
// 2/ Read function is slow but I suspect the swizzle part to be costly. Maybe a compute shader
// that do the swizzle at the same time of the Stretching could save CPU computation.
2011-05-09 22:14:35 +00:00
// note: r.rintersect breaks Wizardry and Chaos Legion
2011-05-10 16:02:52 +00:00
// Read(t, t->m_valid) works in all tested games but is very slow in GUST titles ><
if ( r . x = = 0 & & r . y = = 0 ) // Full screen read?
Read ( t , t - > m_valid ) ;
else // Block level read?
Read ( t , r . rintersect ( t - > m_valid ) ) ;
2009-05-22 01:22:52 +00:00
}
}
}
2014-10-07 17:11:43 +00:00
2011-05-09 22:14:35 +00:00
//GSTextureCache::Target* rt2 = NULL;
//int ymin = INT_MAX;
//for(list<Target*>::iterator i = m_dst[RenderTarget].begin(); i != m_dst[RenderTarget].end(); )
//{
// list<Target*>::iterator j = i++;
// Target* t = *j;
// if (t->m_TEX0.PSM != PSM_PSMZ32 && t->m_TEX0.PSM != PSM_PSMZ24 && t->m_TEX0.PSM != PSM_PSMZ16 && t->m_TEX0.PSM != PSM_PSMZ16S)
// {
// if(GSUtil::HasSharedBits(bp, psm, t->m_TEX0.TBP0, t->m_TEX0.PSM))
// {
// if(GSUtil::HasCompatibleBits(psm, t->m_TEX0.PSM))
// {
// Read(t, r.rintersect(t->m_valid));
// return;
// }
// else if(psm == PSM_PSMCT32 && (t->m_TEX0.PSM == PSM_PSMCT16 || t->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
// Read(t, GSVector4i(r.left, r.top, r.right, r.top + (r.bottom - r.top) * 2).rintersect(t->m_valid));
// return;
// }
// else
// {
// if (psm == PSM_PSMT4HH && t->m_TEX0.PSM == PSM_PSMCT32)
// {
// // Silent Hill Origins shadows: Read 8 bit using only the HIGH bits (4 bit) texture as 32 bit.
// Read(t, r.rintersect(t->m_valid));
// return;
// }
// else
// {
// //printf("Trashing render target. We have a %d type texture and we are trying to write into a %d type texture\n", t->m_TEX0.PSM, psm);
// m_dst[RenderTarget].erase(j);
// delete t;
// }
// }
// }
// // Grandia3, FFX, FFX-2 pause menus. t->m_TEX0.TBP0 magic number checks because otherwise kills xs2 videos
// if( (GSUtil::HasSharedBits(psm, t->m_TEX0.PSM) && (bp > t->m_TEX0.TBP0) )
// && ((t->m_TEX0.TBP0 == 0) || (t->m_TEX0.TBP0==3328) || (t->m_TEX0.TBP0==3584) ))
// {
// //printf("first : %d-%d child : %d-%d\n", psm, bp, t->m_TEX0.PSM, t->m_TEX0.TBP0);
// uint32 rowsize = bw * 8192;
// uint32 offset = (uint32)((bp - t->m_TEX0.TBP0) * 256);
// if(rowsize > 0 && offset % rowsize == 0)
// {
// int y = GSLocalMemory::m_psm[psm].pgs.y * offset / rowsize;
// if(y < ymin && y < 512)
// {
// rt2 = t;
// ymin = y;
// }
// }
// }
// }
//}
//if(rt2)
//{
// Read(rt2, GSVector4i(r.left, r.top + ymin, r.right, r.bottom + ymin));
//}
2014-10-07 17:11:43 +00:00
2009-05-22 01:22:52 +00:00
// TODO: ds
}
2009-11-06 17:13:36 +00:00
2009-05-22 01:22:52 +00:00
void GSTextureCache : : IncAge ( )
{
2009-06-27 03:32:33 +00:00
int maxage = m_src . m_used ? 3 : 30 ;
2014-10-24 18:49:30 +00:00
// You can't use m_map[page] because Source* are duplicated on several pages.
2009-07-16 21:36:07 +00:00
for ( hash_set < Source * > : : iterator i = m_src . m_surfaces . begin ( ) ; i ! = m_src . m_surfaces . end ( ) ; )
2009-06-27 03:32:33 +00:00
{
2009-07-16 21:36:07 +00:00
hash_set < Source * > : : iterator j = i + + ;
2009-06-09 10:13:28 +00:00
2009-07-16 21:36:07 +00:00
Source * s = * j ;
2009-06-27 03:32:33 +00:00
if ( + + s - > m_age > maxage )
{
m_src . RemoveAt ( s ) ;
}
}
m_src . m_used = false ;
2009-11-10 11:38:32 +00:00
// Clearing of Rendertargets causes flickering in many scene transitions.
// Sigh, this seems to be used to invalidate surfaces. So set a huge maxage to avoid flicker,
// but still invalidate surfaces. (Disgaea 2 fmv when booting the game through the BIOS)
// Original maxage was 4 here, Xenosaga 2 needs at least 240, else it flickers on scene transitions.
maxage = 400 ; // ffx intro scene changes leave the old image untouched for a couple of frames and only then start using it
2009-06-27 03:32:33 +00:00
2009-11-10 11:38:32 +00:00
for ( int type = 0 ; type < 2 ; type + + )
{
for ( list < Target * > : : iterator i = m_dst [ type ] . begin ( ) ; i ! = m_dst [ type ] . end ( ) ; )
{
list < Target * > : : iterator j = i + + ;
2009-06-27 03:32:33 +00:00
2009-11-10 11:38:32 +00:00
Target * t = * j ;
2009-06-27 03:32:33 +00:00
2009-11-10 11:38:32 +00:00
if ( + + t - > m_age > maxage )
{
m_dst [ type ] . erase ( j ) ;
2015-05-16 17:28:22 +00:00
GL_CACHE ( " TC: Remove Target(T%d): %d (0x%x) due to age " , type ,
2015-05-16 15:32:24 +00:00
t - > m_texture ? t - > m_texture - > GetID ( ) : 0 ,
2015-05-16 17:28:22 +00:00
t - > m_TEX0 . TBP0 ) ;
2009-06-27 03:32:33 +00:00
2009-11-10 11:38:32 +00:00
delete t ;
}
}
}
2009-05-22 01:22:52 +00:00
}
2009-11-09 00:32:52 +00:00
//Fixme: Several issues in here. Not handling depth stencil, pitch conversion doesnt work.
2015-05-13 18:03:04 +00:00
GSTextureCache : : Source * GSTextureCache : : CreateSource ( const GIFRegTEX0 & TEX0 , const GIFRegTEXA & TEXA , Target * dst , bool half_right )
2009-05-22 01:22:52 +00:00
{
2012-06-11 00:00:18 +00:00
const GSLocalMemory : : psm_t & psm = GSLocalMemory : : m_psm [ TEX0 . PSM ] ;
2011-04-25 18:18:21 +00:00
Source * src = new Source ( m_renderer , TEX0 , TEXA , m_temp ) ;
2009-05-22 01:22:52 +00:00
2009-07-31 23:59:06 +00:00
int tw = 1 < < TEX0 . TW ;
int th = 1 < < TEX0 . TH ;
2014-04-14 18:25:02 +00:00
//int tp = TEX0.TBW << 6;
2010-04-25 00:31:27 +00:00
2010-03-14 19:35:10 +00:00
bool hack = false ;
2009-06-27 03:32:33 +00:00
2014-10-07 17:11:43 +00:00
if ( m_spritehack & & ( TEX0 . PSM = = PSM_PSMT8 | | TEX0 . PSM = = PSM_PSMT8H ) )
2012-06-19 01:12:01 +00:00
{
src - > m_spritehack_t = true ;
2014-10-07 17:11:43 +00:00
if ( m_spritehack = = 2 & & TEX0 . CPSM ! = PSM_PSMCT16 )
src - > m_spritehack_t = false ;
}
2012-06-19 01:12:01 +00:00
else
src - > m_spritehack_t = false ;
2012-06-17 14:33:34 +00:00
if ( dst )
2009-07-31 23:59:06 +00:00
{
// TODO: clean up this mess
2009-05-22 01:22:52 +00:00
2009-07-31 23:59:06 +00:00
src - > m_target = true ;
2009-06-28 02:02:14 +00:00
2012-06-17 14:33:34 +00:00
dst - > Update ( ) ;
2009-08-02 23:07:30 +00:00
GSTexture * tmp = NULL ;
2010-04-25 00:31:27 +00:00
if ( dst - > m_texture - > IsMSAA ( ) )
2009-08-02 23:07:30 +00:00
{
tmp = dst - > m_texture ;
dst - > m_texture = m_renderer - > m_dev - > Resolve ( dst - > m_texture ) ;
}
2009-07-31 23:59:06 +00:00
// do not round here!!! if edge becomes a black pixel and addressing mode is clamp => everything outside the clamped area turns into black (kh2 shadows)
2009-07-06 16:35:06 +00:00
2009-08-02 23:07:30 +00:00
int w = ( int ) ( dst - > m_texture - > GetScale ( ) . x * tw ) ;
2010-04-25 00:31:27 +00:00
int h = ( int ) ( dst - > m_texture - > GetScale ( ) . y * th ) ;
2009-07-06 16:35:06 +00:00
2009-07-31 23:59:06 +00:00
GSVector2i dstsize = dst - > m_texture - > GetSize ( ) ;
2009-07-06 16:35:06 +00:00
2009-07-31 23:59:06 +00:00
// pitch conversion
2009-07-06 16:35:06 +00:00
2009-07-31 23:59:06 +00:00
if ( dst - > m_TEX0 . TBW ! = TEX0 . TBW ) // && dst->m_TEX0.PSM == TEX0.PSM
{
2010-02-20 02:50:58 +00:00
// This is so broken :p
////Better not do the code below, "fixes" like every game that ever gets here..
////Edit: Ratchet and Clank needs this to show most of it's graphics at all.
////Someone else fix this please, I can't :p
////delete src; return NULL;
2009-10-22 01:52:41 +00:00
2010-02-20 02:50:58 +00:00
//// sfex3 uses this trick (bw: 10 -> 5, wraps the right side below the left)
2009-07-06 16:35:06 +00:00
2010-02-20 02:50:58 +00:00
//ASSERT(dst->m_TEX0.TBW > TEX0.TBW); // otherwise scale.x need to be reduced to make the larger texture fit (TODO)
2009-07-06 16:35:06 +00:00
2010-02-20 02:50:58 +00:00
//src->m_texture = m_renderer->m_dev->CreateRenderTarget(dstsize.x, dstsize.y, false);
2009-07-06 16:35:06 +00:00
2010-02-20 02:50:58 +00:00
//GSVector4 size = GSVector4(dstsize).xyxy();
//GSVector4 scale = GSVector4(dst->m_texture->GetScale()).xyxy();
2009-07-06 16:35:06 +00:00
2010-02-20 02:50:58 +00:00
//int blockWidth = 64;
//int blockHeight = TEX0.PSM == PSM_PSMCT32 || TEX0.PSM == PSM_PSMCT24 ? 32 : 64;
2009-07-06 16:35:06 +00:00
2010-02-20 02:50:58 +00:00
//GSVector4i br(0, 0, blockWidth, blockHeight);
2009-07-06 16:35:06 +00:00
2010-02-20 02:50:58 +00:00
//int sw = (int)dst->m_TEX0.TBW << 6;
2009-07-06 16:35:06 +00:00
2010-02-20 02:50:58 +00:00
//int dw = (int)TEX0.TBW << 6;
//int dh = 1 << TEX0.TH;
2009-07-06 16:35:06 +00:00
2010-02-20 02:50:58 +00:00
//if(sw != 0)
//for(int dy = 0; dy < dh; dy += blockHeight)
//{
// for(int dx = 0; dx < dw; dx += blockWidth)
// {
2015-05-15 18:40:09 +00:00
// int off = dy * dw / blockHeight + dx;
2009-07-06 16:35:06 +00:00
2015-05-15 18:40:09 +00:00
// int sx = off % sw;
// int sy = off / sw;
2009-07-06 16:35:06 +00:00
2015-05-15 18:47:14 +00:00
// GSVector4 sRect = GSVector4(GSVector4i(sx, sy).xyxy() + br) * scale / size;
2015-05-15 18:49:25 +00:00
// GSVector4 dRect = GSVector4(GSVector4i(dx, dy).xyxy() + br) * scale;
2009-07-06 16:35:06 +00:00
2015-05-15 18:49:25 +00:00
// m_renderer->m_dev->StretchRect(dst->m_texture, sRect, src->m_texture, dRect);
2009-07-06 16:35:06 +00:00
2010-02-20 02:50:58 +00:00
// // TODO: this is quite a lot of StretchRect, do it with one Draw
// }
//}
2009-07-31 23:59:06 +00:00
}
2010-04-06 21:14:15 +00:00
else if ( tw < 1024 )
2009-10-24 18:39:36 +00:00
{
// FIXME: timesplitters blurs the render target by blending itself over a couple of times
2011-04-15 17:12:22 +00:00
hack = true ;
2011-04-15 15:42:28 +00:00
//if(tw == 256 && th == 128 && (TEX0.TBP0 == 0 || TEX0.TBP0 == 0x00e00))
//{
// delete src;
// return NULL;
//}
2009-10-24 18:39:36 +00:00
}
2009-07-31 23:59:06 +00:00
// width/height conversion
2009-07-06 16:35:06 +00:00
2009-08-02 23:07:30 +00:00
GSVector2 scale = dst - > m_texture - > GetScale ( ) ;
2009-07-06 16:35:06 +00:00
2015-05-15 18:49:25 +00:00
GSVector4 dRect ( 0 , 0 , w , h ) ;
2009-07-06 16:35:06 +00:00
2015-06-05 21:37:06 +00:00
// Try to extract a texture bigger than the RT. Current solution is to rescale the size
// of the texture to fit in the RT. In my opinion, it would be better to increase the size of
// the RT
2010-04-25 00:31:27 +00:00
if ( w > dstsize . x )
2009-07-31 23:59:06 +00:00
{
scale . x = ( float ) dstsize . x / tw ;
2015-05-15 18:49:25 +00:00
dRect . z = ( float ) dstsize . x * scale . x / dst - > m_texture - > GetScale ( ) . x ;
2009-07-31 23:59:06 +00:00
w = dstsize . x ;
}
2010-04-25 00:31:27 +00:00
if ( h > dstsize . y )
2009-07-31 23:59:06 +00:00
{
scale . y = ( float ) dstsize . y / th ;
2015-05-15 18:49:25 +00:00
dRect . w = ( float ) dstsize . y * scale . y / dst - > m_texture - > GetScale ( ) . y ;
2009-07-31 23:59:06 +00:00
h = dstsize . y ;
}
2009-12-20 02:50:34 +00:00
2015-05-15 18:47:14 +00:00
GSVector4 sRect ( 0 , 0 , w , h ) ;
2011-02-19 03:36:30 +00:00
2015-05-15 18:45:31 +00:00
GSTexture * sTex = src - > m_texture ? src - > m_texture : dst - > m_texture ;
2015-05-15 18:43:57 +00:00
GSTexture * dTex = m_renderer - > m_dev - > CreateRenderTarget ( w , h , false ) ;
2014-10-24 18:49:30 +00:00
// GH: by default (m_paltex == 0) GSdx converts texture to the 32 bit format
// However it is different here. We want to reuse a Render Target as a texture.
// Because the texture is already on the GPU, CPU can't convert it.
2015-05-25 07:46:51 +00:00
bool linear = true ;
if ( psm . pal > 0 ) {
2012-06-11 10:57:32 +00:00
src - > m_palette = m_renderer - > m_dev - > CreateTexture ( 256 , 1 ) ;
2015-05-25 07:46:51 +00:00
// Palette is used to interpret the alpha channel of the RT as an index.
// Star Ocean 3 uses it to emulate a stencil buffer.
// It is a very bad idea to force bilinear filtering on it.
linear = false ;
}
2009-07-06 16:35:06 +00:00
2015-06-05 21:50:38 +00:00
int shader = dst - > m_type ! = RenderTarget ? 11 : 0 ;
2009-07-31 23:59:06 +00:00
if ( ! src - > m_texture )
2009-07-06 16:35:06 +00:00
{
2015-05-15 18:43:57 +00:00
src - > m_texture = dTex ;
2009-07-31 23:59:06 +00:00
}
2009-07-06 16:35:06 +00:00
2015-06-05 21:50:38 +00:00
if ( ( sRect = = dRect ) . alltrue ( ) & & ! shader )
2009-07-31 23:59:06 +00:00
{
2015-06-18 19:17:43 +00:00
if ( half_right ) {
// You typically hit this code in snow engine game. Dstsize is the size of of Dx/GL RT
// which is arbitrary set to 1280 (biggest RT used by GS). h/w are based on the input texture
// so the only reliable way to find the real size of the target is to use the TBW value.
2015-06-18 21:02:40 +00:00
float real_width = dst - > m_TEX0 . TBW * 64u * dst - > m_texture - > GetScale ( ) . x ;
m_renderer - > m_dev - > CopyRect ( sTex , dTex , GSVector4i ( real_width / 2.0f , 0 , real_width , h ) ) ;
2015-06-18 19:17:43 +00:00
} else {
2015-05-13 18:03:04 +00:00
m_renderer - > m_dev - > CopyRect ( sTex , dTex , GSVector4i ( 0 , 0 , w , h ) ) ; // <= likely wrong dstsize.x could be bigger than w
2015-06-18 19:17:43 +00:00
}
2009-07-31 23:59:06 +00:00
}
else
{
2015-06-05 21:37:06 +00:00
// Different size or not the same format
2015-05-15 18:47:14 +00:00
sRect . z / = sTex - > GetWidth ( ) ;
sRect . w / = sTex - > GetHeight ( ) ;
2009-07-06 16:35:06 +00:00
2015-05-13 18:03:04 +00:00
if ( half_right ) {
sRect . x = sRect . z / 2.0f ;
}
2015-06-05 21:50:38 +00:00
m_renderer - > m_dev - > StretchRect ( sTex , sRect , dTex , dRect , shader , linear ) ;
2009-07-31 23:59:06 +00:00
}
2009-07-06 16:35:06 +00:00
2015-05-15 18:43:57 +00:00
if ( dTex ! = src - > m_texture )
2009-07-31 23:59:06 +00:00
{
m_renderer - > m_dev - > Recycle ( src - > m_texture ) ;
2009-07-06 16:35:06 +00:00
2015-05-15 18:43:57 +00:00
src - > m_texture = dTex ;
2009-07-06 16:35:06 +00:00
}
2011-09-01 13:25:08 +00:00
if ( src - > m_texture )
src - > m_texture - > SetScale ( scale ) ;
else
ASSERT ( 0 ) ;
2009-07-31 23:59:06 +00:00
2009-08-02 23:07:30 +00:00
if ( tmp ! = NULL )
{
2014-10-24 18:49:30 +00:00
// tmp is texture before a MultiSample resolve
2009-08-02 23:07:30 +00:00
m_renderer - > m_dev - > Recycle ( dst - > m_texture ) ;
dst - > m_texture = tmp ;
}
2010-05-15 04:51:15 +00:00
// Offset hack. Can be enabled via GSdx options.
// The offset will be used in Draw().
2011-02-19 03:36:30 +00:00
2010-05-15 04:51:15 +00:00
float modx = 0.0f ;
float mody = 0.0f ;
2011-02-19 03:36:30 +00:00
if ( UserHacks_HalfPixelOffset & & hack )
2010-05-15 04:51:15 +00:00
{
2011-02-19 03:36:30 +00:00
switch ( m_renderer - > GetUpscaleMultiplier ( ) )
2010-05-15 04:51:15 +00:00
{
case 2 : modx = 2.2f ; mody = 2.2f ; dst - > m_texture - > LikelyOffset = true ; break ;
case 3 : modx = 3.1f ; mody = 3.1f ; dst - > m_texture - > LikelyOffset = true ; break ;
case 4 : modx = 4.2f ; mody = 4.2f ; dst - > m_texture - > LikelyOffset = true ; break ;
case 5 : modx = 5.3f ; mody = 5.3f ; dst - > m_texture - > LikelyOffset = true ; break ;
case 6 : modx = 6.2f ; mody = 6.2f ; dst - > m_texture - > LikelyOffset = true ; break ;
default : modx = 0.0f ; mody = 0.0f ; dst - > m_texture - > LikelyOffset = false ; break ;
}
}
2011-02-19 03:36:30 +00:00
2010-05-15 04:51:15 +00:00
dst - > m_texture - > OffsetHack_modx = modx ;
dst - > m_texture - > OffsetHack_mody = mody ;
2009-07-06 16:35:06 +00:00
}
2012-06-11 10:57:32 +00:00
else
{
if ( m_paltex & & psm . pal > 0 )
{
src - > m_texture = m_renderer - > m_dev - > CreateTexture ( tw , th , Get8bitFormat ( ) ) ;
src - > m_palette = m_renderer - > m_dev - > CreateTexture ( 256 , 1 ) ;
}
else
src - > m_texture = m_renderer - > m_dev - > CreateTexture ( tw , th ) ;
}
2009-07-06 16:35:06 +00:00
2009-07-31 23:59:06 +00:00
if ( src - > m_texture = = NULL )
{
ASSERT ( 0 ) ;
2010-03-14 19:35:10 +00:00
delete src ;
2009-07-31 23:59:06 +00:00
return NULL ;
}
2009-07-06 16:35:06 +00:00
2009-07-31 23:59:06 +00:00
if ( psm . pal > 0 )
2009-07-06 16:35:06 +00:00
{
2009-07-31 23:59:06 +00:00
memcpy ( src - > m_clut , ( const uint32 * ) m_renderer - > m_mem . m_clut , psm . pal * sizeof ( uint32 ) ) ;
2009-07-06 16:35:06 +00:00
}
2009-07-31 23:59:06 +00:00
m_src . Add ( src , TEX0 , m_renderer - > m_context - > offset . tex ) ;
return src ;
}
2009-11-09 00:32:52 +00:00
2009-07-31 23:59:06 +00:00
GSTextureCache : : Target * GSTextureCache : : CreateTarget ( const GIFRegTEX0 & TEX0 , int w , int h , int type )
{
2015-06-10 13:37:56 +00:00
Target * t = new Target ( m_renderer , TEX0 , m_temp , CanConvertDepth ( ) ) ;
2009-07-06 16:35:06 +00:00
2009-07-31 23:59:06 +00:00
// FIXME: initial data should be unswizzled from local mem in Update() if dirty
t - > m_type = type ;
if ( type = = RenderTarget )
2009-07-06 16:35:06 +00:00
{
2009-08-02 23:07:30 +00:00
t - > m_texture = m_renderer - > m_dev - > CreateRenderTarget ( w , h , true ) ;
2009-07-31 23:59:06 +00:00
t - > m_used = true ; // FIXME
2009-07-06 16:35:06 +00:00
}
2009-07-31 23:59:06 +00:00
else if ( type = = DepthStencil )
2009-07-06 16:35:06 +00:00
{
2009-08-02 23:07:30 +00:00
t - > m_texture = m_renderer - > m_dev - > CreateDepthStencil ( w , h , true ) ;
2009-07-06 16:35:06 +00:00
}
2009-07-31 23:59:06 +00:00
if ( t - > m_texture = = NULL )
2009-07-06 16:35:06 +00:00
{
2009-07-31 23:59:06 +00:00
ASSERT ( 0 ) ;
2010-03-14 19:35:10 +00:00
delete t ;
2009-07-31 23:59:06 +00:00
return NULL ;
2009-07-06 16:35:06 +00:00
}
2009-07-31 23:59:06 +00:00
m_dst [ type ] . push_front ( t ) ;
2009-07-06 16:35:06 +00:00
2009-07-31 23:59:06 +00:00
return t ;
}
// GSTextureCache::Surface
2009-07-06 16:35:06 +00:00
2011-02-20 23:53:00 +00:00
GSTextureCache : : Surface : : Surface ( GSRenderer * r , uint8 * temp )
2009-07-31 23:59:06 +00:00
: m_renderer ( r )
, m_texture ( NULL )
, m_age ( 0 )
2013-06-28 17:32:37 +00:00
, m_temp ( temp )
2009-07-31 23:59:06 +00:00
{
2011-02-19 09:05:15 +00:00
m_TEX0 . TBP0 = 0x3fff ;
2009-07-31 23:59:06 +00:00
}
GSTextureCache : : Surface : : ~ Surface ( )
{
m_renderer - > m_dev - > Recycle ( m_texture ) ;
}
void GSTextureCache : : Surface : : Update ( )
{
m_age = 0 ;
}
// GSTextureCache::Source
2011-04-25 18:18:21 +00:00
GSTextureCache : : Source : : Source ( GSRenderer * r , const GIFRegTEX0 & TEX0 , const GIFRegTEXA & TEXA , uint8 * temp )
2011-02-20 23:53:00 +00:00
: Surface ( r , temp )
2009-07-31 23:59:06 +00:00
, m_palette ( NULL )
, m_initpalette ( true )
, m_target ( false )
, m_complete ( false )
2011-04-26 00:56:54 +00:00
, m_p2t ( NULL )
2009-07-31 23:59:06 +00:00
{
2011-04-25 18:18:21 +00:00
m_TEX0 = TEX0 ;
m_TEXA = TEXA ;
2009-07-31 23:59:06 +00:00
memset ( m_valid , 0 , sizeof ( m_valid ) ) ;
2011-02-07 01:59:05 +00:00
m_clut = ( uint32 * ) _aligned_malloc ( 256 * sizeof ( uint32 ) , 32 ) ;
2009-07-31 23:59:06 +00:00
2013-06-28 17:32:37 +00:00
memset ( m_clut , 0 , 256 * sizeof ( uint32 ) ) ;
2009-07-31 23:59:06 +00:00
2011-02-07 01:59:05 +00:00
m_write . rect = ( GSVector4i * ) _aligned_malloc ( 3 * sizeof ( GSVector4i ) , 32 ) ;
2009-07-31 23:59:06 +00:00
m_write . count = 0 ;
2011-04-25 18:18:21 +00:00
m_repeating = m_TEX0 . IsRepeating ( ) ;
if ( m_repeating )
{
2011-04-26 00:56:54 +00:00
m_p2t = r - > m_mem . GetPage2TileMap ( m_TEX0 ) ;
2011-04-25 18:18:21 +00:00
}
2009-07-31 23:59:06 +00:00
}
GSTextureCache : : Source : : ~ Source ( )
{
m_renderer - > m_dev - > Recycle ( m_palette ) ;
_aligned_free ( m_clut ) ;
_aligned_free ( m_write . rect ) ;
2009-07-06 16:35:06 +00:00
}
2011-04-25 18:18:21 +00:00
void GSTextureCache : : Source : : Update ( const GSVector4i & rect )
2009-06-12 19:09:17 +00:00
{
2011-02-19 03:36:30 +00:00
Surface : : Update ( ) ;
2009-06-12 19:09:17 +00:00
2009-07-01 21:14:12 +00:00
if ( m_complete | | m_target )
2009-06-27 03:32:33 +00:00
{
return ;
}
2009-06-12 19:09:17 +00:00
2009-07-04 15:14:04 +00:00
GSVector2i bs = GSLocalMemory : : m_psm [ m_TEX0 . PSM ] . bs ;
2009-06-27 03:32:33 +00:00
2009-07-16 21:36:07 +00:00
int tw = std : : max < int > ( 1 < < m_TEX0 . TW , bs . x ) ;
int th = std : : max < int > ( 1 < < m_TEX0 . TH , bs . y ) ;
2011-03-12 22:10:58 +00:00
GSVector4i r = rect . ralign < Align_Outside > ( bs ) ;
2009-06-27 03:32:33 +00:00
2009-07-16 21:36:07 +00:00
if ( r . eq ( GSVector4i ( 0 , 0 , tw , th ) ) )
2009-07-01 21:14:12 +00:00
{
m_complete = true ; // lame, but better than nothing
}
2015-05-15 18:40:09 +00:00
const GSOffset * off = m_renderer - > m_context - > offset . tex ;
2009-06-27 03:32:33 +00:00
2009-06-28 02:02:14 +00:00
uint32 blocks = 0 ;
2009-06-27 03:32:33 +00:00
2011-04-25 18:18:21 +00:00
if ( m_repeating )
2009-06-12 19:09:17 +00:00
{
2011-04-25 01:44:00 +00:00
for ( int y = r . top ; y < r . bottom ; y + = bs . y )
2009-06-27 03:32:33 +00:00
{
2015-05-15 18:40:09 +00:00
uint32 base = off - > block . row [ y > > 3 ] ;
2009-06-27 03:32:33 +00:00
2011-04-25 18:18:21 +00:00
for ( int x = r . left , i = ( y < < 7 ) + x ; x < r . right ; x + = bs . x , i + = bs . x )
2009-06-27 03:32:33 +00:00
{
2015-05-15 18:40:09 +00:00
uint32 block = base + off - > block . col [ x > > 3 ] ;
2009-06-27 03:32:33 +00:00
2011-04-25 01:44:00 +00:00
if ( block < MAX_BLOCKS )
2009-06-27 03:32:33 +00:00
{
2011-04-25 18:18:21 +00:00
uint32 addr = i > > 3 ;
uint32 row = addr > > 5 ;
uint32 col = 1 < < ( addr & 31 ) ;
2011-04-25 01:44:00 +00:00
if ( ( m_valid [ row ] & col ) = = 0 )
2009-07-01 21:14:12 +00:00
{
m_valid [ row ] | = col ;
2009-06-12 19:09:17 +00:00
2011-04-25 01:44:00 +00:00
Write ( GSVector4i ( x , y , x + bs . x , y + bs . y ) ) ;
2009-06-12 19:09:17 +00:00
2011-04-25 01:44:00 +00:00
blocks + + ;
}
2009-06-27 03:32:33 +00:00
}
}
}
}
2011-04-25 01:44:00 +00:00
else
2009-06-28 02:02:14 +00:00
{
2011-04-25 01:44:00 +00:00
for ( int y = r . top ; y < r . bottom ; y + = bs . y )
2009-06-12 19:09:17 +00:00
{
2015-05-15 18:40:09 +00:00
uint32 base = off - > block . row [ y > > 3 ] ;
2011-04-25 01:44:00 +00:00
for ( int x = r . left ; x < r . right ; x + = bs . x )
2009-06-27 03:32:33 +00:00
{
2015-05-15 18:40:09 +00:00
uint32 block = base + off - > block . col [ x > > 3 ] ;
2009-06-27 03:32:33 +00:00
2011-04-25 01:44:00 +00:00
if ( block < MAX_BLOCKS )
2009-06-27 03:32:33 +00:00
{
2011-04-25 01:44:00 +00:00
uint32 row = block > > 5 ;
uint32 col = 1 < < ( block & 31 ) ;
2009-06-27 03:32:33 +00:00
2011-04-25 01:44:00 +00:00
if ( ( m_valid [ row ] & col ) = = 0 )
2009-06-27 03:32:33 +00:00
{
2009-07-01 21:14:12 +00:00
m_valid [ row ] | = col ;
2011-04-25 01:44:00 +00:00
2011-04-25 18:18:21 +00:00
Write ( GSVector4i ( x , y , x + bs . x , y + bs . y ) ) ;
2011-04-25 01:44:00 +00:00
blocks + + ;
2009-06-27 03:32:33 +00:00
}
}
}
2009-06-12 19:09:17 +00:00
}
2011-04-25 01:44:00 +00:00
}
2009-06-27 03:32:33 +00:00
2011-04-25 01:44:00 +00:00
if ( blocks > 0 )
{
2012-06-11 03:27:16 +00:00
m_renderer - > m_perfmon . Put ( GSPerfMon : : Unswizzle , bs . x * bs . y * blocks < < ( m_palette ? 2 : 0 ) ) ;
2009-06-27 03:32:33 +00:00
2009-06-28 02:02:14 +00:00
Flush ( m_write . count ) ;
}
}
2009-06-27 03:32:33 +00:00
2009-06-28 02:02:14 +00:00
void GSTextureCache : : Source : : Write ( const GSVector4i & r )
{
m_write . rect [ m_write . count + + ] = r ;
while ( m_write . count > = 2 )
{
GSVector4i & a = m_write . rect [ m_write . count - 2 ] ;
GSVector4i & b = m_write . rect [ m_write . count - 1 ] ;
if ( ( a = = b . zyxw ( ) ) . mask ( ) = = 0xfff0 )
{
a . right = b . right ; // extend right
m_write . count - - ;
2009-06-12 19:09:17 +00:00
}
2009-06-28 02:02:14 +00:00
else if ( ( a = = b . xwzy ( ) ) . mask ( ) = = 0xff0f )
{
a . bottom = b . bottom ; // extend down
2009-06-12 19:09:17 +00:00
2009-06-28 02:02:14 +00:00
m_write . count - - ;
}
else
{
break ;
}
}
2009-06-12 19:09:17 +00:00
2009-06-28 02:02:14 +00:00
if ( m_write . count > 2 )
{
Flush ( 1 ) ;
}
2009-06-27 03:32:33 +00:00
}
2009-06-12 19:09:17 +00:00
2009-06-28 02:02:14 +00:00
void GSTextureCache : : Source : : Flush ( uint32 count )
2009-06-27 03:32:33 +00:00
{
2012-06-17 18:39:18 +00:00
// This function as written will not work for paletted formats copied from framebuffers
// because they are 8 or 4 bit formats on the GS and the GS local memory module reads
// these into an 8 bit format while the D3D surfaces are 32 bit.
// However the function is never called for these cases. This is just for information
// should someone wish to use this function for these cases later.
2009-06-28 02:02:14 +00:00
const GSLocalMemory : : psm_t & psm = GSLocalMemory : : m_psm [ m_TEX0 . PSM ] ;
2009-06-27 03:32:33 +00:00
2009-06-28 02:02:14 +00:00
int tw = 1 < < m_TEX0 . TW ;
int th = 1 < < m_TEX0 . TH ;
2009-06-27 03:32:33 +00:00
2009-06-28 02:02:14 +00:00
GSVector4i tr ( 0 , 0 , tw , th ) ;
2009-06-27 03:32:33 +00:00
2009-06-28 02:02:14 +00:00
int pitch = max ( tw , psm . bs . x ) * sizeof ( uint32 ) ;
2009-07-04 15:14:04 +00:00
GSLocalMemory & mem = m_renderer - > m_mem ;
2009-06-28 02:02:14 +00:00
2015-05-15 18:40:09 +00:00
const GSOffset * off = m_renderer - > m_context - > offset . tex ;
2009-07-22 03:55:28 +00:00
2009-06-28 02:02:14 +00:00
GSLocalMemory : : readTexture rtx = psm . rtx ;
2012-06-27 00:57:44 +00:00
GIFRegTEXA plainTEXA ;
plainTEXA . AEM = 1 ;
plainTEXA . TA0 = 0 ;
plainTEXA . TA1 = 0x80 ;
2012-06-11 03:27:16 +00:00
if ( m_palette )
2009-07-06 16:35:06 +00:00
{
pitch > > = 2 ;
rtx = psm . rtxP ;
}
2011-02-19 03:36:30 +00:00
uint8 * buff = m_temp ;
2009-10-12 19:58:03 +00:00
2009-06-28 02:02:14 +00:00
for ( uint32 i = 0 ; i < count ; i + + )
2009-06-27 03:32:33 +00:00
{
2009-06-28 02:02:14 +00:00
GSVector4i r = m_write . rect [ i ] ;
2009-06-27 19:05:36 +00:00
2009-06-28 02:02:14 +00:00
if ( ( r > tr ) . mask ( ) & 0xff00 )
2009-06-27 19:05:36 +00:00
{
2015-05-15 18:40:09 +00:00
( mem . * rtx ) ( off , r , buff , pitch , m_TEXA ) ;
2009-06-27 03:32:33 +00:00
2009-06-28 02:02:14 +00:00
m_texture - > Update ( r . rintersect ( tr ) , buff , pitch ) ;
2009-06-27 19:05:36 +00:00
}
else
{
2009-06-28 02:02:14 +00:00
GSTexture : : GSMap m ;
if ( m_texture - > Map ( m , & r ) )
{
2015-05-15 18:40:09 +00:00
( mem . * rtx ) ( off , r , m . bits , m . pitch , plainTEXA ) ;
2009-06-28 02:02:14 +00:00
m_texture - > Unmap ( ) ;
}
else
{
2015-05-15 18:40:09 +00:00
( mem . * rtx ) ( off , r , buff , pitch , plainTEXA ) ;
2009-06-27 19:05:36 +00:00
2009-06-28 02:02:14 +00:00
m_texture - > Update ( r , buff , pitch ) ;
}
2009-06-27 19:05:36 +00:00
}
2009-06-12 19:09:17 +00:00
}
2009-06-28 02:02:14 +00:00
if ( count < m_write . count )
{
2013-09-05 20:01:47 +00:00
// Warning src and destination overlap. Memmove must be used instead of memcpy
memmove ( & m_write . rect [ 0 ] , & m_write . rect [ count ] , ( m_write . count - count ) * sizeof ( m_write . rect [ 0 ] ) ) ;
2009-06-28 02:02:14 +00:00
}
m_write . count - = count ;
2009-06-12 19:09:17 +00:00
}
2009-06-27 03:32:33 +00:00
// GSTextureCache::Target
2009-05-22 01:22:52 +00:00
2015-06-10 08:07:40 +00:00
GSTextureCache : : Target : : Target ( GSRenderer * r , const GIFRegTEX0 & TEX0 , uint8 * temp , bool depth_supported )
2011-02-20 23:53:00 +00:00
: Surface ( r , temp )
2009-06-27 03:32:33 +00:00
, m_type ( - 1 )
2009-05-22 01:22:52 +00:00
, m_used ( false )
2015-06-10 08:07:40 +00:00
, m_depth_supported ( depth_supported )
2009-05-22 01:22:52 +00:00
{
2011-04-25 18:18:21 +00:00
m_TEX0 = TEX0 ;
2009-07-12 13:46:05 +00:00
m_valid = GSVector4i : : zero ( ) ;
2009-05-22 01:22:52 +00:00
}
2009-06-27 03:32:33 +00:00
void GSTextureCache : : Target : : Update ( )
2009-06-12 19:09:17 +00:00
{
2011-02-19 03:36:30 +00:00
Surface : : Update ( ) ;
2009-06-12 19:09:17 +00:00
2009-06-27 03:32:33 +00:00
// FIXME: the union of the rects may also update wrong parts of the render target (but a lot faster :)
2015-06-10 08:07:40 +00:00
// GH: it must be doable
// 1/ rescale the new t to the good size
// 2/ copy each rectangle (rescale the rectangle) (use CopyRect or multiple vertex)
// Alternate
// 1/ uses multiple vertex rectangle
2009-06-27 03:32:33 +00:00
2009-06-20 20:28:36 +00:00
GSVector4i r = m_dirty . GetDirtyRectAndClear ( m_TEX0 , m_texture - > GetSize ( ) ) ;
2015-06-10 08:07:40 +00:00
if ( r . rempty ( ) ) return ;
2009-06-20 20:28:36 +00:00
2015-06-10 08:07:40 +00:00
int w = r . width ( ) ;
int h = r . height ( ) ;
2009-06-20 20:28:36 +00:00
2015-06-10 08:07:40 +00:00
GIFRegTEXA TEXA ;
2009-07-22 03:55:28 +00:00
2015-06-10 08:07:40 +00:00
TEXA . AEM = 1 ;
TEXA . TA0 = 0 ;
TEXA . TA1 = 0x80 ;
2009-06-12 19:09:17 +00:00
2015-06-10 08:07:40 +00:00
GSTexture * t = m_renderer - > m_dev - > CreateTexture ( w , h ) ;
if ( t = = NULL ) return ;
2009-05-22 01:22:52 +00:00
2015-06-10 08:07:40 +00:00
// No handling please
if ( ( m_type = = DepthStencil ) & & ! m_depth_supported ) {
// do the most likely thing a direct write would do, clear it
GL_INS ( " ERROR: Update DepthStencil dummy " ) ;
2009-06-17 11:24:42 +00:00
2015-06-10 08:07:40 +00:00
if ( ( m_renderer - > m_game . flags & CRC : : ZWriteMustNotClear ) = = 0 )
m_renderer - > m_dev - > ClearDepth ( m_texture , 0 ) ;
2009-05-22 01:22:52 +00:00
2015-06-10 08:07:40 +00:00
return ;
}
2009-05-22 01:22:52 +00:00
2015-06-10 08:07:40 +00:00
const GSOffset * off = m_renderer - > m_mem . GetOffset ( m_TEX0 . TBP0 , m_TEX0 . TBW , m_TEX0 . PSM ) ;
2010-04-25 00:31:27 +00:00
2015-06-10 08:07:40 +00:00
GSTexture : : GSMap m ;
if ( t - > Map ( m ) )
{
m_renderer - > m_mem . ReadTexture ( off , r , m . bits , m . pitch , TEXA ) ;
2009-05-22 01:22:52 +00:00
2015-06-10 08:07:40 +00:00
t - > Unmap ( ) ;
}
else
{
int pitch = ( ( w + 3 ) & ~ 3 ) * 4 ;
2009-05-22 01:22:52 +00:00
2015-06-10 08:07:40 +00:00
m_renderer - > m_mem . ReadTexture ( off , r , m_temp , pitch , TEXA ) ;
2009-06-27 03:32:33 +00:00
2015-06-10 08:07:40 +00:00
t - > Update ( r . rsize ( ) , m_temp , pitch ) ;
}
// m_renderer->m_perfmon.Put(GSPerfMon::Unswizzle, w * h * 4);
// Copy the new GS memory content into the destination texture.
if ( m_type = = RenderTarget )
{
GL_INS ( " ERROR: Update RenderTarget " ) ;
m_renderer - > m_dev - > StretchRect ( t , m_texture , GSVector4 ( r ) * GSVector4 ( m_texture - > GetScale ( ) ) . xyxy ( ) ) ;
2009-06-27 03:32:33 +00:00
}
else if ( m_type = = DepthStencil )
2009-05-22 01:22:52 +00:00
{
2015-06-10 08:07:40 +00:00
GL_INS ( " ERROR: Update DepthStencil " ) ;
2009-06-27 03:32:33 +00:00
2015-06-10 08:07:40 +00:00
m_renderer - > m_dev - > StretchRect ( t , m_texture , GSVector4 ( r ) * GSVector4 ( m_texture - > GetScale ( ) ) . xyxy ( ) , 12 ) ;
2009-05-22 01:22:52 +00:00
}
2015-06-10 08:07:40 +00:00
m_renderer - > m_dev - > Recycle ( t ) ;
2009-06-27 03:32:33 +00:00
}
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
// GSTextureCache::SourceMap
2009-05-22 01:22:52 +00:00
2015-05-15 18:40:09 +00:00
void GSTextureCache : : SourceMap : : Add ( Source * s , const GIFRegTEX0 & TEX0 , const GSOffset * off )
2009-06-27 03:32:33 +00:00
{
2009-07-16 21:36:07 +00:00
m_surfaces . insert ( s ) ;
2009-05-22 01:22:52 +00:00
2009-07-02 00:47:50 +00:00
if ( s - > m_target )
{
// TODO
2014-10-24 18:49:30 +00:00
// GH: I don't know why but it seems we only consider the first page for a render target
2009-07-12 13:46:05 +00:00
m_map [ TEX0 . TBP0 > > 5 ] . push_front ( s ) ;
2009-07-02 00:47:50 +00:00
return ;
}
2014-10-24 18:49:30 +00:00
// Remaining code will compute a list of pages that are dirty (in a similar fashion as GSOffset::GetPages)
// (Maybe GetPages could be used instead, perf opt?)
// The source pointer will be stored/duplicated in all m_map[array of pages]
2009-06-27 03:32:33 +00:00
const GSLocalMemory : : psm_t & psm = GSLocalMemory : : m_psm [ TEX0 . PSM ] ;
2009-05-22 01:22:52 +00:00
2009-07-06 16:35:06 +00:00
GSVector2i bs = ( TEX0 . TBP0 & 31 ) = = 0 ? psm . pgs : psm . bs ;
2009-07-04 15:14:04 +00:00
int tw = 1 < < TEX0 . TW ;
int th = 1 < < TEX0 . TH ;
2009-06-27 03:32:33 +00:00
for ( int y = 0 ; y < th ; y + = bs . y )
2009-05-22 01:22:52 +00:00
{
2015-05-15 18:40:09 +00:00
uint32 base = off - > block . row [ y > > 3 ] ;
2009-05-28 02:57:01 +00:00
2009-06-27 03:32:33 +00:00
for ( int x = 0 ; x < tw ; x + = bs . x )
{
2015-05-15 18:40:09 +00:00
uint32 page = ( base + off - > block . col [ x > > 3 ] ) > > 5 ;
2009-05-22 01:22:52 +00:00
2009-07-01 21:14:12 +00:00
if ( page < MAX_PAGES )
2009-06-27 03:32:33 +00:00
{
2009-07-01 21:14:12 +00:00
m_pages [ page > > 5 ] | = 1 < < ( page & 31 ) ;
2009-06-27 03:32:33 +00:00
}
}
}
2009-06-28 02:02:14 +00:00
2013-06-29 12:02:03 +00:00
for ( size_t i = 0 ; i < countof ( m_pages ) ; i + + )
2009-07-01 21:14:12 +00:00
{
2009-07-01 22:29:24 +00:00
if ( uint32 p = m_pages [ i ] )
2009-07-01 21:14:12 +00:00
{
2009-07-01 22:29:24 +00:00
m_pages [ i ] = 0 ;
2009-07-12 13:46:05 +00:00
list < Source * > * m = & m_map [ i < < 5 ] ;
2009-07-01 21:14:12 +00:00
2009-07-16 21:36:07 +00:00
unsigned long j ;
while ( _BitScanForward ( & j , p ) )
2009-07-01 21:14:12 +00:00
{
2009-07-16 21:36:07 +00:00
p ^ = 1 < < j ;
m [ j ] . push_front ( s ) ;
2009-07-01 21:14:12 +00:00
}
}
}
2009-05-22 01:22:52 +00:00
}
2009-06-27 03:32:33 +00:00
void GSTextureCache : : SourceMap : : RemoveAll ( )
2009-05-22 01:22:52 +00:00
{
2009-07-16 21:36:07 +00:00
for_each ( m_surfaces . begin ( ) , m_surfaces . end ( ) , delete_object ( ) ) ;
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
m_surfaces . clear ( ) ;
2009-05-22 01:22:52 +00:00
2013-06-29 12:02:03 +00:00
for ( size_t i = 0 ; i < countof ( m_map ) ; i + + )
2009-05-22 01:22:52 +00:00
{
2009-06-27 03:32:33 +00:00
m_map [ i ] . clear ( ) ;
2009-05-22 01:22:52 +00:00
}
2009-06-27 03:32:33 +00:00
}
2009-05-22 01:22:52 +00:00
2009-06-27 03:32:33 +00:00
void GSTextureCache : : SourceMap : : RemoveAt ( Source * s )
{
m_surfaces . erase ( s ) ;
2015-05-16 17:28:22 +00:00
GL_CACHE ( " TC: Remove Src Texture: %d (0x%x) " ,
2015-05-13 06:49:32 +00:00
s - > m_texture ? s - > m_texture - > GetID ( ) : 0 ,
2015-05-16 17:28:22 +00:00
s - > m_TEX0 . TBP0 ) ;
2015-05-13 06:49:32 +00:00
2014-10-24 18:49:30 +00:00
// Source (except render target) is duplicated for each page they use.
2013-06-29 12:02:03 +00:00
for ( size_t start = s - > m_TEX0 . TBP0 > > 5 , end = s - > m_target ? start : countof ( m_map ) - 1 ; start < = end ; start + + )
2009-05-22 01:22:52 +00:00
{
2009-07-12 13:46:05 +00:00
list < Source * > & m = m_map [ start ] ;
2009-07-02 00:47:50 +00:00
2009-07-12 13:46:05 +00:00
for ( list < Source * > : : iterator i = m . begin ( ) ; i ! = m . end ( ) ; )
2009-07-02 00:47:50 +00:00
{
2009-07-12 13:46:05 +00:00
list < Source * > : : iterator j = i + + ;
if ( * j = = s ) { m . erase ( j ) ; break ; }
2009-07-02 00:47:50 +00:00
}
2009-05-22 01:22:52 +00:00
}
2009-06-27 03:32:33 +00:00
delete s ;
2010-04-25 00:31:27 +00:00
}