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
2010-04-25 00:31:27 +00:00
* the Free Software Foundation , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
2009-02-09 21:15:56 +00:00
* http : //www.gnu.org/copyleft/gpl.html
*
*/
# pragma once
# include "GS.h"
# include "GSLocalMemory.h"
# include "GSDrawingContext.h"
# include "GSDrawingEnvironment.h"
# include "GSVertex.h"
2012-01-05 02:40:24 +00:00
# include "GSVertexTrace.h"
2009-02-09 21:15:56 +00:00
# include "GSUtil.h"
# include "GSPerfMon.h"
# include "GSVector.h"
# include "GSDevice.h"
# include "GSCrc.h"
# include "GSAlignedClass.h"
# include "GSDump.h"
2011-02-07 01:59:05 +00:00
class GSState : public GSAlignedClass < 32 >
2009-02-09 21:15:56 +00:00
{
2011-12-20 14:33:28 +00:00
// RESTRICT prevents multiple loads of the same part of the register when accessing its bitfields (the compiler is happy to know that memory writes in-between will not go there)
typedef void ( GSState : : * GIFPackedRegHandler ) ( const GIFPackedReg * RESTRICT r ) ;
2011-02-18 01:56:05 +00:00
2009-02-09 21:15:56 +00:00
GIFPackedRegHandler m_fpGIFPackedRegHandlers [ 16 ] ;
2012-01-08 17:10:00 +00:00
GIFPackedRegHandler m_fpGIFPackedRegHandlerXYZ [ 8 ] [ 4 ] ;
2009-02-09 21:15:56 +00:00
2011-12-20 14:33:28 +00:00
void GIFPackedRegHandlerNull ( const GIFPackedReg * RESTRICT r ) ;
void GIFPackedRegHandlerRGBA ( const GIFPackedReg * RESTRICT r ) ;
void GIFPackedRegHandlerSTQ ( const GIFPackedReg * RESTRICT r ) ;
void GIFPackedRegHandlerUV ( const GIFPackedReg * RESTRICT r ) ;
2012-01-08 17:10:00 +00:00
template < uint32 prim , uint32 adc > void GIFPackedRegHandlerXYZF2 ( const GIFPackedReg * RESTRICT r ) ;
template < uint32 prim , uint32 adc > void GIFPackedRegHandlerXYZ2 ( const GIFPackedReg * RESTRICT r ) ;
2011-12-20 14:33:28 +00:00
void GIFPackedRegHandlerFOG ( const GIFPackedReg * RESTRICT r ) ;
void GIFPackedRegHandlerA_D ( const GIFPackedReg * RESTRICT r ) ;
void GIFPackedRegHandlerNOP ( const GIFPackedReg * RESTRICT r ) ;
2010-07-19 15:49:30 +00:00
2011-12-20 14:33:28 +00:00
typedef void ( GSState : : * GIFRegHandler ) ( const GIFReg * RESTRICT r ) ;
2009-02-09 21:15:56 +00:00
GIFRegHandler m_fpGIFRegHandlers [ 256 ] ;
2012-01-08 17:10:00 +00:00
GIFRegHandler m_fpGIFRegHandlerXYZ [ 8 ] [ 4 ] ;
2009-02-09 21:15:56 +00:00
2012-01-18 11:47:31 +00:00
typedef void ( GSState : : * GIFPackedRegHandlerC ) ( const GIFPackedReg * RESTRICT r , uint32 size ) ;
2012-01-21 12:34:36 +00:00
GIFPackedRegHandlerC m_fpGIFPackedRegHandlersC [ 2 ] ;
2012-01-18 11:47:31 +00:00
GIFPackedRegHandlerC m_fpGIFPackedRegHandlerSTQRGBAXYZF2 [ 8 ] ;
2012-01-21 12:34:36 +00:00
GIFPackedRegHandlerC m_fpGIFPackedRegHandlerSTQRGBAXYZ2 [ 8 ] ;
2012-01-18 11:47:31 +00:00
template < uint32 prim > void GIFPackedRegHandlerSTQRGBAXYZF2 ( const GIFPackedReg * RESTRICT r , uint32 size ) ;
2012-01-21 12:34:36 +00:00
template < uint32 prim > void GIFPackedRegHandlerSTQRGBAXYZ2 ( const GIFPackedReg * RESTRICT r , uint32 size ) ;
2012-01-21 04:44:04 +00:00
void GIFPackedRegHandlerNOP ( const GIFPackedReg * RESTRICT r , uint32 size ) ;
2012-01-18 11:47:31 +00:00
2012-01-08 17:10:00 +00:00
template < int i > void ApplyTEX0 ( GIFRegTEX0 & TEX0 ) ;
2012-01-27 11:56:49 +00:00
void ApplyPRIM ( uint32 prim ) ;
2010-07-19 15:49:30 +00:00
2011-12-20 14:33:28 +00:00
void GIFRegHandlerNull ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerPRIM ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerRGBAQ ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerST ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerUV ( const GIFReg * RESTRICT r ) ;
2012-01-08 17:10:00 +00:00
template < uint32 prim , uint32 adc > void GIFRegHandlerXYZF2 ( const GIFReg * RESTRICT r ) ;
template < uint32 prim , uint32 adc > void GIFRegHandlerXYZ2 ( const GIFReg * RESTRICT r ) ;
2011-12-20 14:33:28 +00:00
template < int i > void GIFRegHandlerTEX0 ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerCLAMP ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerFOG ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerNOP ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerTEX1 ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerTEX2 ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerXYOFFSET ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerPRMODECONT ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerPRMODE ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerTEXCLUT ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerSCANMSK ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerMIPTBP1 ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerMIPTBP2 ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerTEXA ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerFOGCOL ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerTEXFLUSH ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerSCISSOR ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerALPHA ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerDIMX ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerDTHE ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerCOLCLAMP ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerTEST ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerPABE ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerFBA ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerFRAME ( const GIFReg * RESTRICT r ) ;
template < int i > void GIFRegHandlerZBUF ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerBITBLTBUF ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerTRXPOS ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerTRXREG ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerTRXDIR ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerHWREG ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerSIGNAL ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerFINISH ( const GIFReg * RESTRICT r ) ;
void GIFRegHandlerLABEL ( const GIFReg * RESTRICT r ) ;
2009-02-09 21:15:56 +00:00
int m_version ;
int m_sssize ;
bool m_mt ;
void ( * m_irq ) ( ) ;
bool m_path3hack ;
2009-04-18 09:34:25 +00:00
struct GSTransferBuffer
{
2010-04-25 00:31:27 +00:00
int x , y ;
int start , end , total ;
2009-04-18 09:34:25 +00:00
bool overflow ;
2009-05-14 16:41:52 +00:00
uint8 * buff ;
2009-04-18 09:34:25 +00:00
GSTransferBuffer ( ) ;
virtual ~ GSTransferBuffer ( ) ;
void Init ( int tx , int ty ) ;
bool Update ( int tw , int th , int bpp , int & len ) ;
} m_tr ;
2009-02-09 21:15:56 +00:00
protected :
2010-02-19 10:37:31 +00:00
bool IsBadFrame ( int & skip , int UserHacks_SkipDraw ) ;
2009-02-09 21:15:56 +00:00
2012-01-05 02:40:24 +00:00
GSVertex m_v ;
2012-01-23 04:11:24 +00:00
float m_q ;
2012-01-09 08:41:33 +00:00
GSVector4 m_scissor ;
uint32 m_ofxy ;
2012-02-08 16:57:14 +00:00
bool m_texflush ;
2012-01-08 17:10:00 +00:00
struct
{
2012-01-19 04:53:36 +00:00
GSVertex * buff ;
2012-01-08 17:10:00 +00:00
size_t head , tail , next , maxcount ; // head: first vertex, tail: last vertex + 1, next: last indexed + 1
GSVector4 xy [ 4 ] ;
size_t xy_tail ;
} m_vertex ;
struct
{
uint32 * buff ;
size_t tail ;
} m_index ;
2009-02-09 21:15:56 +00:00
2012-01-09 08:41:33 +00:00
void UpdateContext ( ) ;
void UpdateScissor ( ) ;
2012-01-05 02:40:24 +00:00
virtual void UpdateVertexKick ( ) ;
2009-02-09 21:15:56 +00:00
2012-01-05 02:40:24 +00:00
void GrowVertexBuffer ( ) ;
2010-08-17 04:38:49 +00:00
2012-01-05 02:40:24 +00:00
template < uint32 prim >
2012-01-06 00:17:52 +00:00
void VertexKick ( uint32 skip ) ;
2012-01-05 02:40:24 +00:00
// following functions need m_vt to be initialized
2012-01-19 04:53:36 +00:00
GSVertexTrace m_vt ;
2012-01-05 02:40:24 +00:00
void GetTextureMinMax ( GSVector4i & r , const GIFRegTEX0 & TEX0 , const GIFRegCLAMP & CLAMP , bool linear ) ;
void GetAlphaMinMax ( ) ;
bool TryAlphaTest ( uint32 & fm , uint32 & zm ) ;
bool IsOpaque ( ) ;
2009-02-09 21:15:56 +00:00
public :
2010-07-11 04:38:33 +00:00
GIFPath m_path [ 4 ] ;
2009-03-31 03:19:21 +00:00
GIFRegPRIM * PRIM ;
GSPrivRegSet * m_regs ;
2009-02-09 21:15:56 +00:00
GSLocalMemory m_mem ;
GSDrawingEnvironment m_env ;
GSDrawingContext * m_context ;
GSPerfMon m_perfmon ;
2009-05-14 16:41:52 +00:00
uint32 m_crc ;
2009-02-09 21:15:56 +00:00
int m_options ;
int m_frameskip ;
2009-07-16 21:36:07 +00:00
bool m_framelimit ;
2009-02-09 21:15:56 +00:00
CRC : : Game m_game ;
GSDump m_dump ;
2012-01-08 17:10:00 +00:00
bool m_nativeres ;
2009-02-09 21:15:56 +00:00
2012-03-08 17:18:22 +00:00
bool isPackedUV_HackFlag ;
2012-01-29 10:12:20 +00:00
int s_n ;
bool s_dump ;
bool s_save ;
bool s_savez ;
int s_saven ;
2009-02-09 21:15:56 +00:00
public :
2012-01-19 04:53:36 +00:00
GSState ( ) ;
2009-02-09 21:15:56 +00:00
virtual ~ GSState ( ) ;
void ResetHandlers ( ) ;
2009-05-14 16:41:52 +00:00
GSVector4i GetDisplayRect ( int i = - 1 ) ;
GSVector4i GetFrameRect ( int i = - 1 ) ;
2009-05-20 15:35:31 +00:00
GSVector2i GetDeviceSize ( int i = - 1 ) ;
2009-05-14 16:41:52 +00:00
2009-02-09 21:15:56 +00:00
bool IsEnabled ( int i ) ;
2009-05-14 16:41:52 +00:00
2011-10-08 13:05:15 +00:00
float GetFPS ( ) ;
2009-02-09 21:15:56 +00:00
virtual void Reset ( ) ;
virtual void Flush ( ) ;
2012-01-05 02:40:24 +00:00
virtual void FlushPrim ( ) ;
virtual void FlushWrite ( ) ;
virtual void Draw ( ) = 0 ;
2009-05-14 16:41:52 +00:00
virtual void InvalidateVideoMem ( const GIFRegBITBLTBUF & BITBLTBUF , const GSVector4i & r ) { }
2011-12-20 14:33:28 +00:00
virtual void InvalidateLocalMem ( const GIFRegBITBLTBUF & BITBLTBUF , const GSVector4i & r , bool clut = false ) { }
2009-02-09 21:15:56 +00:00
void Move ( ) ;
2010-07-19 15:49:30 +00:00
void Write ( const uint8 * mem , int len ) ;
2009-05-14 16:41:52 +00:00
void Read ( uint8 * mem , int len ) ;
2009-02-09 21:15:56 +00:00
2009-05-14 16:41:52 +00:00
void SoftReset ( uint32 mask ) ;
void WriteCSR ( uint32 csr ) { m_regs - > CSR . u32 [ 1 ] = csr ; }
void ReadFIFO ( uint8 * mem , int size ) ;
2010-07-19 15:49:30 +00:00
template < int index > void Transfer ( const uint8 * mem , uint32 size ) ;
2009-02-09 21:15:56 +00:00
int Freeze ( GSFreezeData * fd , bool sizeonly ) ;
int Defrost ( const GSFreezeData * fd ) ;
2009-05-14 16:41:52 +00:00
void GetLastTag ( uint32 * tag ) { * tag = m_path3hack ; m_path3hack = 0 ; }
virtual void SetGameCRC ( uint32 crc , int options ) ;
2009-07-16 21:36:07 +00:00
void SetFrameSkip ( int skip ) ;
2009-09-18 23:48:12 +00:00
void SetRegsMem ( uint8 * basemem ) ;
2009-09-18 19:54:56 +00:00
void SetIrqCallback ( void ( * irq ) ( ) ) ;
2011-02-07 01:59:05 +00:00
void SetMultithreaded ( bool mt = true ) ;
2009-02-09 21:15:56 +00:00
} ;