2009-09-08 12:08:10 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2009-09-17 02:12:32 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 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 PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2009-02-09 21:15:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PrecompiledHeader.h"
|
|
|
|
#include "Common.h"
|
2009-09-23 09:53:21 +00:00
|
|
|
|
2009-11-14 12:02:56 +00:00
|
|
|
#include <list>
|
|
|
|
#include <wx/datetime.h>
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-11-14 12:02:56 +00:00
|
|
|
#include "GS.h"
|
|
|
|
#include "Elfheader.h"
|
2009-02-09 21:15:56 +00:00
|
|
|
#include "SamplProf.h"
|
|
|
|
|
2009-09-23 09:53:21 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
// Uncomment this to enable profiling of the GS RingBufferCopy function.
|
|
|
|
//#define PCSX2_GSRING_SAMPLING_STATS
|
|
|
|
|
|
|
|
using namespace Threading;
|
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
#if 0 //PCSX2_DEBUG
|
2009-11-21 07:56:29 +00:00
|
|
|
# define MTGS_LOG Console.WriteLn
|
2009-02-09 21:15:56 +00:00
|
|
|
#else
|
2009-11-21 07:56:29 +00:00
|
|
|
# define MTGS_LOG 0&&
|
2009-02-09 21:15:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// forces the compiler to treat a non-volatile value as volatile.
|
|
|
|
// This allows us to declare the vars as non-volatile and only use
|
|
|
|
// them as volatile when appropriate (more optimized).
|
|
|
|
|
|
|
|
#define volatize(x) (*reinterpret_cast<volatile uint*>(&(x)))
|
|
|
|
|
2009-09-19 09:45:26 +00:00
|
|
|
|
2009-09-23 09:53:21 +00:00
|
|
|
// =====================================================================================================
|
|
|
|
// MTGS Threaded Class Implementation
|
|
|
|
// =====================================================================================================
|
2009-09-19 09:45:26 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
__aligned(32) MTGS_BufferedData RingBuffer;
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
extern bool renderswitch;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef RINGBUF_DEBUG_STACK
|
|
|
|
#include <list>
|
|
|
|
std::list<uint> ringposStack;
|
|
|
|
#endif
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
SysMtgsThread::SysMtgsThread() :
|
User Interface:
* Fixed and added better Emulation/System menu updating. Suspend/Resume is more consistent, and Reset grays itself out after being used.
* Entering plugin configurations auto-suspends the emulator.
* Changing plugins in the Configuration PAnel takes effect now without a restart.
* Added preliminary support for an ExtensibleConfirmation Dialog box (contains a sizer you can add content to, and also has an optional "[x] Do not show this again" checkbox).
Bugfixes:
* Added some mutex protection to cdvdNewDiskCB; "just in case."
* Resolved several recursion and deadlock scenarios when (very!) rapidly suspending, resuming, and resetting the emu.
Developments / Code Cleanups:
* Renamed SysCoreThread ExecutionModes: Suspend/Resume are now Opened/Closed (which more accurately reflects the fact they opena nd close the plugins, and helps avoid ambiguity with the "Paused" state).
* Added Exception::ThreadTimedOut, which is now thrown from Semaphore::Wait when recursive wxApp::Yield() calls are detected, and a deadlock occurs (basically cancels the current action which, most of the time, allows for full recovery).
* Major Threading namespace cleanups, documentations, etc.
* Removed wxScopedArray (scopedarray.h) and replaced it with a better implemeneted ScopedArray class.
* Removed toUTF8 class, which I only added a couple weeks ago because I didn't realize wxCharBuffer had an implicit typecast to (char*).
* Implemented more Source/Listener events for Pcsx2App. CoreThread events are sourced properly now, and added SettingsApplied and SettingsLoadSave Sources.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@2010 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-16 03:58:29 +00:00
|
|
|
SysThreadBase()
|
2009-02-09 21:15:56 +00:00
|
|
|
#ifdef RINGBUF_DEBUG_STACK
|
|
|
|
, m_lock_Stack()
|
|
|
|
#endif
|
|
|
|
{
|
2009-09-29 19:16:00 +00:00
|
|
|
m_name = L"MTGS";
|
2009-11-21 07:56:29 +00:00
|
|
|
|
|
|
|
// All other state vars are initialized by OnStart().
|
2009-02-17 01:38:02 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::OnStart()
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
{
|
2009-11-24 08:43:36 +00:00
|
|
|
m_PluginOpened = false;
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
m_ReadPos = 0;
|
2009-11-24 08:43:36 +00:00
|
|
|
m_WritePos = 0;
|
|
|
|
m_RingBufferIsBusy = false;
|
2009-09-23 09:53:21 +00:00
|
|
|
m_packet_size = 0;
|
2010-07-15 05:21:26 +00:00
|
|
|
m_packet_writepos = 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-11-24 22:49:05 +00:00
|
|
|
m_QueuedFrameCount = 0;
|
2010-07-13 16:34:27 +00:00
|
|
|
m_VsyncSignalListener = false;
|
2009-11-24 08:43:36 +00:00
|
|
|
m_SignalRingEnable = 0;
|
|
|
|
m_SignalRingPosition= 0;
|
|
|
|
|
2009-10-09 15:17:53 +00:00
|
|
|
m_CopyDataTally = 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-10-09 15:17:53 +00:00
|
|
|
_parent::OnStart();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
SysMtgsThread::~SysMtgsThread() throw()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-09-29 19:16:00 +00:00
|
|
|
_parent::Cancel();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::OnResumeReady()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-09-23 09:53:21 +00:00
|
|
|
m_sem_OpenDone.Reset();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::ResetGS()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
pxAssertDev( !IsOpen() || (m_ReadPos == m_WritePos), "Must close or terminate the GS thread prior to gsReset." );
|
2010-07-11 04:53:50 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
// MTGS Reset process:
|
|
|
|
// * clear the ringbuffer.
|
|
|
|
// * Signal a reset.
|
|
|
|
// * clear the path and byRegs structs (used by GIFtagDummy)
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
m_ReadPos = m_WritePos;
|
2010-07-11 04:53:50 +00:00
|
|
|
m_QueuedFrameCount = 0;
|
2010-07-13 16:34:27 +00:00
|
|
|
m_VsyncSignalListener = false;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-09-07 21:16:12 +00:00
|
|
|
MTGS_LOG( "MTGS: Sending Reset..." );
|
2009-02-09 21:15:56 +00:00
|
|
|
SendSimplePacket( GS_RINGTYPE_RESET, 0, 0, 0 );
|
|
|
|
SendSimplePacket( GS_RINGTYPE_FRAMESKIP, 0, 0, 0 );
|
2009-09-23 09:53:21 +00:00
|
|
|
SetEvent();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-09-23 09:53:21 +00:00
|
|
|
GIFPath_Reset();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2010-06-26 07:26:43 +00:00
|
|
|
struct RingCmdPacket_Vsync
|
|
|
|
{
|
2010-06-28 03:09:51 +00:00
|
|
|
u8 regset1[0x0f0];
|
|
|
|
u32 csr;
|
|
|
|
u32 imr;
|
2010-06-26 07:26:43 +00:00
|
|
|
GSRegSIGBLID siglblid;
|
|
|
|
};
|
|
|
|
|
2009-12-03 15:51:39 +00:00
|
|
|
void SysMtgsThread::PostVsyncEnd()
|
2009-02-20 04:36:55 +00:00
|
|
|
{
|
2010-06-26 07:26:43 +00:00
|
|
|
// Optimization note: Typically regset1 isn't needed. The regs in that area are typically
|
|
|
|
// changed infrequently, usually during video mode changes. However, on modern systems the
|
|
|
|
// 256-byte copy is only a few dozen cycles -- executed 60 times a second -- so probably
|
|
|
|
// not worth the effort or overhead of trying to selectively avoid it.
|
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
uint packsize = sizeof(RingCmdPacket_Vsync) / 16;
|
|
|
|
PrepDataPacket(GS_RINGTYPE_VSYNC, packsize);
|
2010-07-15 05:21:26 +00:00
|
|
|
MemCopy_WrappedDest( (u128*)PS2MEM_GS, RingBuffer.m_Ring, m_packet_writepos, RingBufferSize, 0xf );
|
2010-07-11 13:23:56 +00:00
|
|
|
|
|
|
|
u32* remainder = (u32*)GetDataPacketPtr();
|
|
|
|
remainder[0] = GSCSRr;
|
|
|
|
remainder[1] = GSIMR;
|
|
|
|
(GSRegSIGBLID&)remainder[2] = GSSIGLBLID;
|
2010-07-15 05:21:26 +00:00
|
|
|
m_packet_writepos = (m_packet_writepos + 1) & RingBufferMask;
|
2009-11-24 08:43:36 +00:00
|
|
|
|
2010-07-13 16:34:27 +00:00
|
|
|
SendDataPacket();
|
2010-07-11 04:53:50 +00:00
|
|
|
|
2010-07-13 16:34:27 +00:00
|
|
|
// Vsyncs should always start the GS thread, regardless of how little has actually be queued.
|
|
|
|
if (m_CopyDataTally != 0) SetEvent();
|
2010-07-11 04:53:50 +00:00
|
|
|
|
2010-07-13 16:34:27 +00:00
|
|
|
// If the MTGS is allowed to queue a lot of frames in advance, it creates input lag.
|
|
|
|
// Use the Queued FrameCount to stall the EE if another vsync (or two) are already queued
|
2010-07-13 16:36:57 +00:00
|
|
|
// in the ringbuffer. The queue limit is disabled when both FrameLimiting and Vsync are
|
|
|
|
// disabled, since the queue can have perverse effects on framerate benchmarking.
|
2010-07-11 04:53:50 +00:00
|
|
|
|
2011-03-18 16:52:56 +00:00
|
|
|
// Edit: It's possible that MTGS is that much faster than the GS plugin that it creates so much lag,
|
|
|
|
// a game becomes uncontrollable (software rendering for example).
|
|
|
|
// For that reason it's better to have the limit always in place, at the cost of a few max FPS in benchmarks.
|
|
|
|
// If those are needed back, it's better to increase the VsyncQueueSize via PCSX_vm.ini.
|
|
|
|
// (The Xenosaga engine is known to run into this, due to it throwing bulks of data in one frame followed by 2 empty frames.)
|
|
|
|
|
|
|
|
if ((AtomicIncrement(m_QueuedFrameCount) < EmuConfig.GS.VsyncQueueSize) /*|| (!EmuConfig.GS.VsyncEnable && !EmuConfig.GS.FrameLimitEnable)*/) return;
|
2010-07-11 04:53:50 +00:00
|
|
|
|
2010-07-13 16:34:27 +00:00
|
|
|
m_VsyncSignalListener = true;
|
2010-07-15 05:21:26 +00:00
|
|
|
//Console.WriteLn( Color_Blue, "(EEcore Sleep) Vsync\t\tringpos=0x%06x, writepos=0x%06x", volatize(m_ReadPos), m_WritePos );
|
2010-07-13 16:34:27 +00:00
|
|
|
m_sem_Vsync.WaitNoCancel();
|
2009-02-20 04:36:55 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
struct PacketTagType
|
|
|
|
{
|
|
|
|
u32 command;
|
|
|
|
u32 data[3];
|
|
|
|
};
|
2009-08-25 15:38:48 +00:00
|
|
|
|
2009-09-23 09:53:21 +00:00
|
|
|
static void dummyIrqCallback()
|
|
|
|
{
|
|
|
|
// dummy, because MTGS doesn't need this mess!
|
|
|
|
// (and zerogs does >_<)
|
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::OpenPlugin()
|
2009-09-23 09:53:21 +00:00
|
|
|
{
|
2009-10-19 01:50:52 +00:00
|
|
|
if( m_PluginOpened ) return;
|
2009-09-23 09:53:21 +00:00
|
|
|
|
2010-07-12 19:40:30 +00:00
|
|
|
memcpy_aligned( RingBuffer.Regs, PS2MEM_GS, sizeof(PS2MEM_GS) );
|
2009-09-23 09:53:21 +00:00
|
|
|
GSsetBaseMem( RingBuffer.Regs );
|
|
|
|
GSirqCallback( dummyIrqCallback );
|
|
|
|
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
int result;
|
2009-09-23 09:53:21 +00:00
|
|
|
|
|
|
|
if( GSopen2 != NULL )
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
result = GSopen2( (void*)&pDsp, 1 | (renderswitch ? 4 : 0) );
|
2009-09-23 09:53:21 +00:00
|
|
|
else
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
result = GSopen( (void*)&pDsp, "PCSX2", renderswitch ? 2 : 1 );
|
2009-09-23 09:53:21 +00:00
|
|
|
|
2009-12-04 22:37:20 +00:00
|
|
|
// Vsync on / off ?
|
|
|
|
if( renderswitch )
|
|
|
|
{
|
|
|
|
Console.Indent(2).WriteLn( "Forced software switch enabled." );
|
2011-03-27 22:16:48 +00:00
|
|
|
if (EmuConfig.GS.VsyncEnable && !EmuConfig.GS.ManagedVsync)
|
2009-12-04 22:37:20 +00:00
|
|
|
{
|
|
|
|
// Better turn Vsync off now, as in most cases sw rendering is not fast enough to support a steady 60fps.
|
|
|
|
// Having Vsync still enabled then means a big cut in speed and sloppy rendering.
|
2010-04-25 00:31:27 +00:00
|
|
|
// It's possible though that some users have very fast machines, and rather kept Vsync enabled,
|
2009-12-04 22:37:20 +00:00
|
|
|
// but let's assume this is the minority. At least for now ;)
|
|
|
|
GSsetVsync( false );
|
|
|
|
Console.Indent(2).WriteLn( "Vsync temporarily disabled" );
|
|
|
|
}
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
else
|
2009-12-04 22:37:20 +00:00
|
|
|
{
|
|
|
|
GSsetVsync( EmuConfig.GS.FrameLimitEnable && EmuConfig.GS.VsyncEnable );
|
|
|
|
}
|
2009-12-03 15:51:39 +00:00
|
|
|
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
if( result != 0 )
|
2009-09-23 09:53:21 +00:00
|
|
|
{
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
DevCon.WriteLn( "GSopen Failed: return code: 0x%x", result );
|
|
|
|
throw Exception::PluginOpenError( PluginId_GS );
|
2009-09-23 09:53:21 +00:00
|
|
|
}
|
|
|
|
|
2009-12-01 15:33:56 +00:00
|
|
|
// This is the preferred place to implement DXGI fullscreen overrides, using LoadLibrary.
|
|
|
|
// But I hate COM, I don't know to make this work, and I don't have DX10, so I give up
|
|
|
|
// and enjoy my working DX9 alt-enter instead. Someone else can fix this mess. --air
|
|
|
|
|
|
|
|
// Also: Prolly needs some DX10 header includes? Which ones? Too many, I gave up.
|
|
|
|
|
|
|
|
#if 0 // defined(__WXMSW__) && defined(_MSC_VER)
|
|
|
|
wxDynamicLibrary dynlib( L"dxgi.dll" );
|
|
|
|
SomeFuncTypeIDunno isThisEvenTheRightFunctionNameIDunno = dynlib.GetSymbol("CreateDXGIFactory");
|
|
|
|
if( isThisEvenTheRightFunctionNameIDunno )
|
|
|
|
{
|
|
|
|
// Is this how LoadLibrary for COM works? I dunno. I dont care.
|
|
|
|
|
|
|
|
IDXGIFactory* pFactory;
|
|
|
|
hr = isThisEvenTheRightFunctionNameIDunno(__uuidof(IDXGIFactory), (void**)(&pFactory) );
|
|
|
|
pFactory->MakeWindowAssociation((HWND)&pDsp, DXGI_MWA_NO_WINDOW_CHANGES);
|
|
|
|
pFactory->Release();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-19 01:50:52 +00:00
|
|
|
m_PluginOpened = true;
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
m_sem_OpenDone.Post();
|
|
|
|
|
2009-09-23 09:53:21 +00:00
|
|
|
GSsetGameCRC( ElfCRC, 0 );
|
|
|
|
}
|
|
|
|
|
2009-11-24 08:43:36 +00:00
|
|
|
class RingBufferLock : public ScopedLock
|
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
typedef ScopedLock _parent;
|
|
|
|
|
2009-11-24 08:43:36 +00:00
|
|
|
protected:
|
|
|
|
SysMtgsThread& m_mtgs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RingBufferLock( SysMtgsThread& mtgs )
|
2010-07-13 16:34:27 +00:00
|
|
|
: ScopedLock( mtgs.m_mtx_RingBufferBusy )
|
2009-11-24 08:43:36 +00:00
|
|
|
, m_mtgs( mtgs )
|
|
|
|
{
|
|
|
|
m_mtgs.m_RingBufferIsBusy = true;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-11-24 08:43:36 +00:00
|
|
|
virtual ~RingBufferLock() throw()
|
|
|
|
{
|
|
|
|
m_mtgs.m_RingBufferIsBusy = false;
|
|
|
|
}
|
2010-07-15 05:21:26 +00:00
|
|
|
|
|
|
|
void Acquire()
|
|
|
|
{
|
|
|
|
_parent::Acquire();
|
|
|
|
m_mtgs.m_RingBufferIsBusy = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Release()
|
|
|
|
{
|
|
|
|
m_mtgs.m_RingBufferIsBusy = false;
|
|
|
|
_parent::Release();
|
|
|
|
}
|
2009-11-24 08:43:36 +00:00
|
|
|
};
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::ExecuteTaskInThread()
|
2009-09-08 03:44:35 +00:00
|
|
|
{
|
2009-09-23 09:53:21 +00:00
|
|
|
#ifdef RINGBUF_DEBUG_STACK
|
|
|
|
PacketTagType prevCmd;
|
|
|
|
#endif
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
RingBufferLock busy( *this );
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
while( true )
|
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
busy.Release();
|
|
|
|
|
2009-10-23 20:24:59 +00:00
|
|
|
// Performance note: Both of these perform cancellation tests, but pthread_testcancel
|
|
|
|
// is very optimized (only 1 instruction test in most cases), so no point in trying
|
|
|
|
// to avoid it.
|
|
|
|
|
2009-11-01 09:27:16 +00:00
|
|
|
m_sem_event.WaitWithoutYield();
|
2009-10-23 20:24:59 +00:00
|
|
|
StateCheckInThread();
|
2010-07-15 05:21:26 +00:00
|
|
|
busy.Acquire();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
// note: m_ReadPos is intentionally not volatile, because it should only
|
2009-02-09 21:15:56 +00:00
|
|
|
// ever be modified by this thread.
|
2010-07-15 05:21:26 +00:00
|
|
|
while( m_ReadPos != volatize(m_WritePos))
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-01-04 11:51:09 +00:00
|
|
|
if( EmuConfig.GS.DisableOutput )
|
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
m_ReadPos = m_WritePos;
|
2010-01-04 11:51:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
pxAssert( m_ReadPos < RingBufferSize );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
const PacketTagType& tag = (PacketTagType&)RingBuffer[m_ReadPos];
|
2009-02-09 21:15:56 +00:00
|
|
|
u32 ringposinc = 1;
|
|
|
|
|
|
|
|
#ifdef RINGBUF_DEBUG_STACK
|
|
|
|
// pop a ringpos off the stack. It should match this one!
|
|
|
|
|
|
|
|
m_lock_Stack.Lock();
|
|
|
|
uptr stackpos = ringposStack.back();
|
2010-07-15 05:21:26 +00:00
|
|
|
if( stackpos != m_ReadPos )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
Console.Error( "MTGS Ringbuffer Critical Failure ---> %x to %x (prevCmd: %x)\n", stackpos, m_ReadPos, prevCmd.command );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
2010-07-15 05:21:26 +00:00
|
|
|
pxAssert( stackpos == m_ReadPos );
|
2009-02-09 21:15:56 +00:00
|
|
|
prevCmd = tag;
|
|
|
|
ringposStack.pop_back();
|
2009-10-31 19:18:29 +00:00
|
|
|
m_lock_Stack.Release();
|
2009-02-09 21:15:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
switch( tag.command )
|
|
|
|
{
|
2010-06-26 14:13:34 +00:00
|
|
|
case GS_RINGTYPE_P1:
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
uint datapos = (m_ReadPos+1) & RingBufferMask;
|
2009-02-09 21:15:56 +00:00
|
|
|
const int qsize = tag.data[0];
|
2010-07-11 04:53:50 +00:00
|
|
|
const u128* data = &RingBuffer[datapos];
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-06-26 14:13:34 +00:00
|
|
|
MTGS_LOG( "(MTGS Packet Read) ringtype=P1, qwc=%u", qsize );
|
2009-11-21 07:56:29 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
uint endpos = datapos + qsize;
|
|
|
|
if( endpos >= RingBufferSize )
|
|
|
|
{
|
|
|
|
uint firstcopylen = RingBufferSize - datapos;
|
|
|
|
GSgifTransfer( (u32*)data, firstcopylen );
|
|
|
|
datapos = endpos & RingBufferMask;
|
|
|
|
GSgifTransfer( (u32*)RingBuffer.m_Ring, datapos );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GSgifTransfer( (u32*)data, qsize );
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
ringposinc += qsize;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-06-26 14:13:34 +00:00
|
|
|
case GS_RINGTYPE_P2:
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
uint datapos = (m_ReadPos+1) & RingBufferMask;
|
2009-02-09 21:15:56 +00:00
|
|
|
const int qsize = tag.data[0];
|
2010-07-11 04:53:50 +00:00
|
|
|
const u128* data = &RingBuffer[datapos];
|
2010-06-26 07:48:16 +00:00
|
|
|
|
2010-06-26 14:13:34 +00:00
|
|
|
MTGS_LOG( "(MTGS Packet Read) ringtype=P2, qwc=%u", qsize );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
uint endpos = datapos + qsize;
|
|
|
|
if( endpos >= RingBufferSize )
|
|
|
|
{
|
|
|
|
uint firstcopylen = RingBufferSize - datapos;
|
|
|
|
GSgifTransfer2( (u32*)data, firstcopylen );
|
|
|
|
datapos = endpos & RingBufferMask;
|
|
|
|
GSgifTransfer2( (u32*)RingBuffer.m_Ring, datapos );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GSgifTransfer2( (u32*)data, qsize );
|
|
|
|
}
|
|
|
|
|
2010-06-26 14:13:34 +00:00
|
|
|
ringposinc += qsize;
|
|
|
|
}
|
2010-06-26 07:48:16 +00:00
|
|
|
break;
|
|
|
|
|
2010-06-26 14:13:34 +00:00
|
|
|
case GS_RINGTYPE_P3:
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
uint datapos = (m_ReadPos+1) & RingBufferMask;
|
2010-06-26 14:13:34 +00:00
|
|
|
const int qsize = tag.data[0];
|
2010-07-11 04:53:50 +00:00
|
|
|
const u128* data = &RingBuffer[datapos];
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-06-26 14:13:34 +00:00
|
|
|
MTGS_LOG( "(MTGS Packet Read) ringtype=P3, qwc=%u", qsize );
|
2009-02-20 04:36:55 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
uint endpos = datapos + qsize;
|
|
|
|
if( endpos >= RingBufferSize )
|
|
|
|
{
|
|
|
|
uint firstcopylen = RingBufferSize - datapos;
|
|
|
|
GSgifTransfer3( (u32*)data, firstcopylen );
|
|
|
|
datapos = endpos & RingBufferMask;
|
|
|
|
GSgifTransfer3( (u32*)RingBuffer.m_Ring, datapos );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GSgifTransfer3( (u32*)data, qsize );
|
|
|
|
}
|
|
|
|
|
2010-06-26 14:13:34 +00:00
|
|
|
ringposinc += qsize;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-06-26 14:13:34 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
switch( tag.command )
|
|
|
|
{
|
|
|
|
case GS_RINGTYPE_VSYNC:
|
|
|
|
{
|
|
|
|
const int qsize = tag.data[0];
|
|
|
|
ringposinc += qsize;
|
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
MTGS_LOG( "(MTGS Packet Read) ringtype=Vsync, field=%u, skip=%s", !!(((u32&)RingBuffer.Regs[0x1000]) & 0x2000) ? 0 : 1, tag.data[1] ? "true" : "false" );
|
2010-07-13 16:34:27 +00:00
|
|
|
|
2010-06-26 14:13:34 +00:00
|
|
|
// Mail in the important GS registers.
|
2010-07-17 12:11:37 +00:00
|
|
|
// This seemingly obtuse system is needed in order to handle cases where the vsync data wraps
|
|
|
|
// around the edge of the ringbuffer. If not for that I'd just use a struct. >_<
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
uint datapos = (m_ReadPos+1) & RingBufferMask;
|
|
|
|
MemCopy_WrappedSrc( RingBuffer.m_Ring, datapos, RingBufferSize, (u128*)RingBuffer.Regs, 0xf );
|
|
|
|
|
|
|
|
u32* remainder = (u32*)&RingBuffer[datapos];
|
2010-07-17 12:11:37 +00:00
|
|
|
((u32&)RingBuffer.Regs[0x1000]) = remainder[0];
|
|
|
|
((u32&)RingBuffer.Regs[0x1010]) = remainder[1];
|
|
|
|
((GSRegSIGBLID&)RingBuffer.Regs[0x1080]) = (GSRegSIGBLID&)remainder[2];
|
2010-07-13 16:34:27 +00:00
|
|
|
|
2010-06-28 03:09:51 +00:00
|
|
|
// CSR & 0x2000; is the pageflip id.
|
|
|
|
GSvsync(((u32&)RingBuffer.Regs[0x1000]) & 0x2000);
|
2010-06-26 14:13:34 +00:00
|
|
|
gsFrameSkip();
|
|
|
|
|
|
|
|
// if we're not using GSOpen2, then the GS window is on this thread (MTGS thread),
|
|
|
|
// so we need to call PADupdate from here.
|
|
|
|
if( (GSopen2 == NULL) && (PADupdate != NULL) )
|
|
|
|
PADupdate(0);
|
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
AtomicDecrement( m_QueuedFrameCount );
|
2010-07-13 16:34:27 +00:00
|
|
|
if (!!AtomicExchange(m_VsyncSignalListener, false))
|
|
|
|
m_sem_Vsync.Post();
|
|
|
|
|
|
|
|
busy.Release();
|
2010-06-26 14:13:34 +00:00
|
|
|
StateCheckInThread();
|
2010-07-13 16:34:27 +00:00
|
|
|
busy.Acquire();
|
2010-06-26 14:13:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GS_RINGTYPE_FRAMESKIP:
|
|
|
|
MTGS_LOG( "(MTGS Packet Read) ringtype=Frameskip" );
|
|
|
|
_gs_ResetFrameskip();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GS_RINGTYPE_FREEZE:
|
|
|
|
{
|
|
|
|
MTGS_FreezeData* data = (MTGS_FreezeData*)(*(uptr*)&tag.data[1]);
|
|
|
|
int mode = tag.data[0];
|
|
|
|
data->retval = GetCorePlugins().DoFreeze( PluginId_GS, mode, data->fdata );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GS_RINGTYPE_RESET:
|
|
|
|
MTGS_LOG( "(MTGS Packet Read) ringtype=Reset" );
|
|
|
|
if( GSreset != NULL ) GSreset();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GS_RINGTYPE_SOFTRESET:
|
|
|
|
{
|
|
|
|
int mask = tag.data[0];
|
|
|
|
MTGS_LOG( "(MTGS Packet Read) ringtype=SoftReset" );
|
|
|
|
GSgifSoftReset( mask );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GS_RINGTYPE_MODECHANGE:
|
|
|
|
// [TODO] some frameskip sync logic might be needed here!
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GS_RINGTYPE_CRC:
|
|
|
|
GSsetGameCRC( tag.data[0], 0 );
|
|
|
|
break;
|
2009-11-21 07:56:29 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
#ifdef PCSX2_DEVBUILD
|
2010-06-26 14:13:34 +00:00
|
|
|
default:
|
2010-07-15 05:21:26 +00:00
|
|
|
Console.Error("GSThreadProc, bad packet (%x) at m_ReadPos: %x, m_WritePos: %x", tag.command, m_ReadPos, m_WritePos);
|
2010-06-26 14:13:34 +00:00
|
|
|
pxFail( "Bad packet encountered in the MTGS Ringbuffer." );
|
2010-07-15 05:21:26 +00:00
|
|
|
m_ReadPos = m_WritePos;
|
2010-06-26 14:13:34 +00:00
|
|
|
continue;
|
2009-02-09 21:15:56 +00:00
|
|
|
#else
|
2010-06-26 14:13:34 +00:00
|
|
|
// Optimized performance in non-Dev builds.
|
|
|
|
jNO_DEFAULT;
|
2009-02-09 21:15:56 +00:00
|
|
|
#endif
|
2010-06-26 14:13:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
uint newringpos = (m_ReadPos + ringposinc) & RingBufferMask;
|
2010-07-11 04:53:50 +00:00
|
|
|
|
|
|
|
if( EmuConfig.GS.SynchronousMTGS )
|
|
|
|
{
|
|
|
|
pxAssert( m_WritePos == newringpos );
|
|
|
|
}
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
m_ReadPos = newringpos;
|
2009-11-24 08:43:36 +00:00
|
|
|
|
|
|
|
if( m_SignalRingEnable != 0 )
|
|
|
|
{
|
|
|
|
// The EEcore has requested a signal after some amount of processed data.
|
2009-11-24 20:11:43 +00:00
|
|
|
if( AtomicExchangeSub( m_SignalRingPosition, ringposinc ) <= 0 )
|
2009-11-24 08:43:36 +00:00
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
// Make sure to post the signal after the m_ReadPos has been updated...
|
2009-11-24 08:43:36 +00:00
|
|
|
AtomicExchange( m_SignalRingEnable, 0 );
|
|
|
|
m_sem_OnRingReset.Post();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-15 05:21:26 +00:00
|
|
|
|
|
|
|
busy.Release();
|
2009-11-24 08:43:36 +00:00
|
|
|
|
|
|
|
// Safety valve in case standard signals fail for some reason -- this ensures the EEcore
|
|
|
|
// won't sleep the eternity, even if SignalRingPosition didn't reach 0 for some reason.
|
2009-11-24 20:11:43 +00:00
|
|
|
// Important: Need to unlock the MTGS busy signal PRIOR, so that EEcore SetEvent() calls
|
|
|
|
// parallel to this handler aren't accidentally blocked.
|
2009-11-24 08:43:36 +00:00
|
|
|
if( AtomicExchange( m_SignalRingEnable, 0 ) != 0 )
|
|
|
|
{
|
2009-11-24 20:11:43 +00:00
|
|
|
//Console.Warning( "(MTGS Thread) Dangling RingSignal on empty buffer! signalpos=0x%06x", AtomicExchange( m_SignalRingPosition, 0 ) );
|
2009-11-24 08:43:36 +00:00
|
|
|
AtomicExchange( m_SignalRingPosition, 0 );
|
|
|
|
m_sem_OnRingReset.Post();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
2009-11-24 20:11:43 +00:00
|
|
|
|
2010-07-13 16:34:27 +00:00
|
|
|
if (!!AtomicExchange(m_VsyncSignalListener, false))
|
|
|
|
m_sem_Vsync.Post();
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
//Console.Warning( "(MTGS Thread) Nothing to do! ringpos=0x%06x", m_ReadPos );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
2009-10-19 01:50:52 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::ClosePlugin()
|
2009-10-19 01:50:52 +00:00
|
|
|
{
|
|
|
|
if( !m_PluginOpened ) return;
|
|
|
|
m_PluginOpened = false;
|
2010-04-27 13:12:03 +00:00
|
|
|
GetCorePlugins().Close( PluginId_GS );
|
2009-09-08 03:44:35 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::OnSuspendInThread()
|
2009-09-16 17:23:02 +00:00
|
|
|
{
|
2009-10-19 01:50:52 +00:00
|
|
|
ClosePlugin();
|
|
|
|
_parent::OnSuspendInThread();
|
2009-09-16 17:23:02 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::OnResumeInThread( bool isSuspended )
|
2009-09-08 03:44:35 +00:00
|
|
|
{
|
2009-10-07 19:20:11 +00:00
|
|
|
if( isSuspended )
|
2009-09-23 09:53:21 +00:00
|
|
|
OpenPlugin();
|
2009-10-19 01:50:52 +00:00
|
|
|
|
|
|
|
_parent::OnResumeInThread( isSuspended );
|
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::OnCleanupInThread()
|
2009-10-19 01:50:52 +00:00
|
|
|
{
|
|
|
|
ClosePlugin();
|
|
|
|
_parent::OnCleanupInThread();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Waits for the GS to empty out the entire ring buffer contents.
|
|
|
|
// Used primarily for plugin startup/shutdown.
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::WaitGS()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
pxAssertDev( !IsSelf(), "This method is only allowed from threads *not* named MTGS." );
|
2009-09-23 09:53:21 +00:00
|
|
|
|
User Interface:
* Fixed and added better Emulation/System menu updating. Suspend/Resume is more consistent, and Reset grays itself out after being used.
* Entering plugin configurations auto-suspends the emulator.
* Changing plugins in the Configuration PAnel takes effect now without a restart.
* Added preliminary support for an ExtensibleConfirmation Dialog box (contains a sizer you can add content to, and also has an optional "[x] Do not show this again" checkbox).
Bugfixes:
* Added some mutex protection to cdvdNewDiskCB; "just in case."
* Resolved several recursion and deadlock scenarios when (very!) rapidly suspending, resuming, and resetting the emu.
Developments / Code Cleanups:
* Renamed SysCoreThread ExecutionModes: Suspend/Resume are now Opened/Closed (which more accurately reflects the fact they opena nd close the plugins, and helps avoid ambiguity with the "Paused" state).
* Added Exception::ThreadTimedOut, which is now thrown from Semaphore::Wait when recursive wxApp::Yield() calls are detected, and a deadlock occurs (basically cancels the current action which, most of the time, allows for full recovery).
* Major Threading namespace cleanups, documentations, etc.
* Removed wxScopedArray (scopedarray.h) and replaced it with a better implemeneted ScopedArray class.
* Removed toUTF8 class, which I only added a couple weeks ago because I didn't realize wxCharBuffer had an implicit typecast to (char*).
* Implemented more Source/Listener events for Pcsx2App. CoreThread events are sourced properly now, and added SettingsApplied and SettingsLoadSave Sources.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@2010 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-16 03:58:29 +00:00
|
|
|
if( m_ExecMode == ExecMode_NoThreadYet || !IsRunning() ) return;
|
|
|
|
if( !pxAssertDev( IsOpen(), "MTGS Warning! WaitGS issued on a closed thread." ) ) return;
|
2009-09-23 09:53:21 +00:00
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
if( volatize(m_ReadPos) != m_WritePos )
|
2009-11-22 09:51:00 +00:00
|
|
|
{
|
2009-11-24 08:43:36 +00:00
|
|
|
SetEvent();
|
2010-05-07 03:20:58 +00:00
|
|
|
RethrowException();
|
|
|
|
|
2009-11-22 09:51:00 +00:00
|
|
|
do {
|
2010-07-13 16:34:27 +00:00
|
|
|
m_mtx_RingBufferBusy.Wait();
|
2010-05-07 03:20:58 +00:00
|
|
|
RethrowException();
|
2010-07-15 05:21:26 +00:00
|
|
|
} while( volatize(m_ReadPos) != m_WritePos );
|
2009-11-22 09:51:00 +00:00
|
|
|
}
|
2010-06-26 07:26:43 +00:00
|
|
|
|
|
|
|
// Completely synchronize GS and MTGS register states.
|
|
|
|
memcpy_fast( RingBuffer.Regs, PS2MEM_GS, sizeof(RingBuffer.Regs) );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the gsEvent flag and releases a timeslice.
|
|
|
|
// For use in loops that wait on the GS thread to do certain things.
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::SetEvent()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-11-24 08:43:36 +00:00
|
|
|
if( !m_RingBufferIsBusy )
|
|
|
|
m_sem_event.Post();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-11-24 08:43:36 +00:00
|
|
|
m_CopyDataTally = 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
u8* SysMtgsThread::GetDataPacketPtr() const
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
return (u8*)&RingBuffer[m_packet_writepos & RingBufferMask];
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Closes the data packet send command, and initiates the gs thread (if needed).
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::SendDataPacket()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
// make sure a previous copy block has been started somewhere.
|
2009-10-18 12:30:00 +00:00
|
|
|
pxAssert( m_packet_size != 0 );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
uint actualSize = ((m_packet_writepos - m_packet_startpos) & RingBufferMask)-1;
|
2010-07-11 04:53:50 +00:00
|
|
|
pxAssert( actualSize <= m_packet_size );
|
2010-07-15 05:21:26 +00:00
|
|
|
pxAssert( m_packet_writepos < RingBufferSize );
|
2010-07-11 04:53:50 +00:00
|
|
|
|
|
|
|
PacketTagType& tag = (PacketTagType&)RingBuffer[m_packet_startpos];
|
|
|
|
tag.data[0] = actualSize;
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
m_WritePos = m_packet_writepos;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-12-03 15:51:39 +00:00
|
|
|
if( EmuConfig.GS.SynchronousMTGS )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-11-21 07:56:29 +00:00
|
|
|
WaitGS();
|
|
|
|
}
|
2009-11-24 22:49:05 +00:00
|
|
|
else if( !m_RingBufferIsBusy )
|
2009-11-24 08:43:36 +00:00
|
|
|
{
|
|
|
|
m_CopyDataTally += m_packet_size;
|
|
|
|
if( m_CopyDataTally > 0x2000 ) SetEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_packet_size = 0;
|
2009-11-21 07:56:29 +00:00
|
|
|
|
2009-10-31 19:18:29 +00:00
|
|
|
//m_PacketLocker.Release();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
void SysMtgsThread::GenericStall( uint size )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-11-24 08:43:36 +00:00
|
|
|
// Note on volatiles: m_WritePos is not modified by the GS thread, so there's no need
|
|
|
|
// to use volatile reads here. We do cache it though, since we know it never changes,
|
|
|
|
// except for calls to RingbufferRestert() -- handled below.
|
2010-07-15 05:21:26 +00:00
|
|
|
const uint writepos = m_WritePos;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
// Sanity checks! (within the confines of our ringbuffer please!)
|
2009-10-18 12:30:00 +00:00
|
|
|
pxAssert( size < RingBufferSize );
|
|
|
|
pxAssert( writepos < RingBufferSize );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
// generic gs wait/stall.
|
|
|
|
// if the writepos is past the readpos then we're safe.
|
|
|
|
// But if not then we need to make sure the readpos is outside the scope of
|
|
|
|
// the block about to be written (writepos + size)
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
uint readpos = volatize(m_ReadPos);
|
2010-07-11 04:53:50 +00:00
|
|
|
uint freeroom;
|
2009-11-24 08:43:36 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
if (writepos < readpos)
|
|
|
|
freeroom = readpos - writepos;
|
|
|
|
else
|
|
|
|
freeroom = RingBufferSize - (writepos - readpos);
|
2009-11-24 08:43:36 +00:00
|
|
|
|
2010-07-17 00:14:41 +00:00
|
|
|
if (freeroom <= size)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-07-11 04:53:50 +00:00
|
|
|
// writepos will overlap readpos if we commit the data, so we need to wait until
|
|
|
|
// readpos is out past the end of the future write pos, or until it wraps around
|
|
|
|
// (in which case writepos will be >= readpos).
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
// Ideally though we want to wait longer, because if we just toss in this packet
|
|
|
|
// the next packet will likely stall up too. So lets set a condition for the MTGS
|
|
|
|
// thread to wake up the EE once there's a sizable chunk of the ringbuffer emptied.
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
uint somedone = (RingBufferSize - freeroom) / 4;
|
|
|
|
if( somedone < size+1 ) somedone = size + 1;
|
2009-11-24 08:43:36 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
// FMV Optimization: FMVs typically send *very* little data to the GS, in some cases
|
|
|
|
// every other frame is nothing more than a page swap. Sleeping the EEcore is a
|
|
|
|
// waste of time, and we get better results using a spinwait.
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
if( somedone > 0x80 )
|
|
|
|
{
|
|
|
|
pxAssertDev( m_SignalRingEnable == 0, "MTGS Thread Synchronization Error" );
|
|
|
|
m_SignalRingPosition = somedone;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-07-12 04:04:40 +00:00
|
|
|
//Console.WriteLn( Color_Blue, "(EEcore Sleep) PrepDataPacker \tringpos=0x%06x, writepos=0x%06x, signalpos=0x%06x", readpos, writepos, m_SignalRingPosition );
|
2009-11-24 08:43:36 +00:00
|
|
|
|
2010-07-17 00:14:41 +00:00
|
|
|
while(true) {
|
2010-07-11 04:53:50 +00:00
|
|
|
AtomicExchange( m_SignalRingEnable, 1 );
|
2009-11-24 08:43:36 +00:00
|
|
|
SetEvent();
|
2010-07-11 04:53:50 +00:00
|
|
|
m_sem_OnRingReset.WaitWithoutYield();
|
2010-07-15 05:21:26 +00:00
|
|
|
readpos = volatize(m_ReadPos);
|
2010-07-11 04:53:50 +00:00
|
|
|
//Console.WriteLn( Color_Blue, "(EEcore Awake) Report!\tringpos=0x%06x", readpos );
|
2010-07-17 00:14:41 +00:00
|
|
|
|
|
|
|
if (writepos < readpos)
|
|
|
|
freeroom = readpos - writepos;
|
|
|
|
else
|
|
|
|
freeroom = RingBufferSize - (writepos - readpos);
|
|
|
|
|
|
|
|
if (freeroom > size) break;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
pxAssertDev( m_SignalRingPosition <= 0, "MTGS Thread Synchronization Error" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-12 04:04:40 +00:00
|
|
|
//Console.WriteLn( Color_StrongGray, "(EEcore Spin) PrepDataPacket!" );
|
2010-07-11 04:53:50 +00:00
|
|
|
SetEvent();
|
2010-07-17 00:14:41 +00:00
|
|
|
while(true) {
|
2010-07-11 04:53:50 +00:00
|
|
|
SpinWait();
|
2010-07-15 05:21:26 +00:00
|
|
|
readpos = volatize(m_ReadPos);
|
2010-07-17 00:14:41 +00:00
|
|
|
|
|
|
|
if (writepos < readpos)
|
|
|
|
freeroom = readpos - writepos;
|
|
|
|
else
|
|
|
|
freeroom = RingBufferSize - (writepos - readpos);
|
|
|
|
|
|
|
|
if (freeroom > size) break;
|
|
|
|
}
|
2010-07-11 04:53:50 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-15 05:21:26 +00:00
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
void SysMtgsThread::PrepDataPacket( MTGS_RingCommand cmd, u32 size )
|
|
|
|
{
|
|
|
|
m_packet_size = size;
|
|
|
|
++size; // takes into account our RingCommand QWC.
|
|
|
|
GenericStall(size);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
// Command qword: Low word is the command, and the high word is the packet
|
|
|
|
// length in SIMDs (128 bits).
|
|
|
|
|
2009-09-23 09:53:21 +00:00
|
|
|
PacketTagType& tag = (PacketTagType&)RingBuffer[m_WritePos];
|
2010-06-26 07:26:43 +00:00
|
|
|
tag.command = cmd;
|
2009-02-09 21:15:56 +00:00
|
|
|
tag.data[0] = m_packet_size;
|
2010-07-11 04:53:50 +00:00
|
|
|
m_packet_startpos = m_WritePos;
|
2010-07-15 05:21:26 +00:00
|
|
|
m_packet_writepos = (m_WritePos + 1) & RingBufferMask;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2010-06-26 07:26:43 +00:00
|
|
|
// Returns the amount of giftag data processed (in simd128 values).
|
|
|
|
// Return value is used by VU1's XGKICK instruction to wrap the data
|
|
|
|
// around VU memory instead of having buffer overflow...
|
|
|
|
// Parameters:
|
|
|
|
// size - size of the packet data, in smd128's
|
2010-07-11 04:53:50 +00:00
|
|
|
void SysMtgsThread::PrepDataPacket( GIF_PATH pathidx, u32 size )
|
2010-06-26 07:26:43 +00:00
|
|
|
{
|
|
|
|
//m_PacketLocker.Acquire();
|
|
|
|
|
2010-07-11 04:53:50 +00:00
|
|
|
PrepDataPacket( (MTGS_RingCommand)pathidx, size );
|
2010-06-26 07:26:43 +00:00
|
|
|
}
|
|
|
|
|
2010-08-09 04:10:38 +00:00
|
|
|
__fi void SysMtgsThread::_FinishSimplePacket()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-07-15 05:21:26 +00:00
|
|
|
uint future_writepos = (m_WritePos+1) & RingBufferMask;
|
|
|
|
pxAssert( future_writepos != volatize(m_ReadPos) );
|
2009-10-18 12:30:00 +00:00
|
|
|
m_WritePos = future_writepos;
|
2009-11-21 07:56:29 +00:00
|
|
|
|
2009-12-03 15:51:39 +00:00
|
|
|
if( EmuConfig.GS.SynchronousMTGS )
|
2009-11-21 07:56:29 +00:00
|
|
|
WaitGS();
|
2009-11-24 08:43:36 +00:00
|
|
|
else
|
|
|
|
++m_CopyDataTally;
|
2009-11-21 07:56:29 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::SendSimplePacket( MTGS_RingCommand type, int data0, int data1, int data2 )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-03 06:05:48 +00:00
|
|
|
//ScopedLock locker( m_PacketLocker );
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
GenericStall(1);
|
2009-09-23 09:53:21 +00:00
|
|
|
PacketTagType& tag = (PacketTagType&)RingBuffer[m_WritePos];
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
tag.command = type;
|
|
|
|
tag.data[0] = data0;
|
|
|
|
tag.data[1] = data1;
|
|
|
|
tag.data[2] = data2;
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
_FinishSimplePacket();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::SendPointerPacket( MTGS_RingCommand type, u32 data0, void* data1 )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-03 06:05:48 +00:00
|
|
|
//ScopedLock locker( m_PacketLocker );
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
GenericStall(1);
|
2009-09-23 09:53:21 +00:00
|
|
|
PacketTagType& tag = (PacketTagType&)RingBuffer[m_WritePos];
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
tag.command = type;
|
|
|
|
tag.data[0] = data0;
|
|
|
|
*(uptr*)&tag.data[1] = (uptr)data1;
|
|
|
|
|
2010-07-15 05:21:26 +00:00
|
|
|
_FinishSimplePacket();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::SendGameCRC( u32 crc )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-09-23 09:53:21 +00:00
|
|
|
SendSimplePacket( GS_RINGTYPE_CRC, crc, 0, 0 );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::WaitForOpen()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-10-19 01:50:52 +00:00
|
|
|
if( m_PluginOpened ) return;
|
2009-10-17 18:21:30 +00:00
|
|
|
Resume();
|
2009-10-18 12:30:00 +00:00
|
|
|
|
|
|
|
// Two-phase timeout on MTGS opening, so that possible errors are handled
|
|
|
|
// in a timely fashion. We check for errors after 2 seconds, and then give it
|
2010-03-08 02:55:52 +00:00
|
|
|
// another 12 seconds if no errors occurred (this might seem long, but sometimes a
|
2009-10-18 12:30:00 +00:00
|
|
|
// GS plugin can be very stubborned, especially in debug mode builds).
|
|
|
|
|
|
|
|
if( !m_sem_OpenDone.Wait( wxTimeSpan(0, 0, 2, 0) ) )
|
|
|
|
{
|
|
|
|
RethrowException();
|
|
|
|
|
2010-03-08 02:55:52 +00:00
|
|
|
if( !m_sem_OpenDone.Wait( wxTimeSpan(0, 0, 12, 0) ) )
|
2009-10-18 12:30:00 +00:00
|
|
|
{
|
|
|
|
RethrowException();
|
2009-10-19 01:50:52 +00:00
|
|
|
|
2009-10-18 12:30:00 +00:00
|
|
|
// Not opened yet, and no exceptions. Weird? You decide!
|
2010-06-28 18:03:54 +00:00
|
|
|
// [TODO] : implement a user confirmation to cancel the action and exit the
|
2009-10-18 12:30:00 +00:00
|
|
|
// emulator forcefully, or to continue waiting on the GS.
|
2009-10-19 01:50:52 +00:00
|
|
|
|
2010-06-28 18:03:54 +00:00
|
|
|
throw Exception::PluginOpenError( PluginId_GS )
|
2010-12-14 07:28:05 +00:00
|
|
|
.SetBothMsgs(pxLt("The MTGS thread has become unresponsive while waiting for the GS plugin to open."));
|
2009-10-18 12:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
RethrowException();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 06:54:24 +00:00
|
|
|
void SysMtgsThread::Freeze( int mode, MTGS_FreezeData& data )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-04-27 13:12:03 +00:00
|
|
|
GetCorePlugins().Open( PluginId_GS );
|
2009-11-04 09:30:22 +00:00
|
|
|
SendPointerPacket( GS_RINGTYPE_FREEZE, mode, &data );
|
|
|
|
Resume();
|
|
|
|
WaitGS();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|