VideoCommon: use a new async event system for efb access

This commit is contained in:
degasus 2015-01-31 11:38:23 +01:00
parent 860c889454
commit bc248f8941
10 changed files with 201 additions and 58 deletions

View File

@ -0,0 +1,88 @@
#include "VideoCommon/AsyncRequests.h"
#include "VideoCommon/RenderBase.h"
AsyncRequests AsyncRequests::s_singleton;
AsyncRequests::AsyncRequests()
: m_enable(false)
{
}
void AsyncRequests::PullEventsInternal()
{
std::unique_lock<std::mutex> lock(m_mutex);
m_empty.store(true);
while (!m_queue.empty())
{
const Event& e = m_queue.front();
lock.unlock();
HandleEvent(e);
lock.lock();
m_queue.pop();
}
if (m_wake_me_up_again)
{
m_wake_me_up_again = false;
m_cond.notify_all();
}
}
void AsyncRequests::PushEvent(const AsyncRequests::Event& event, bool blocking)
{
std::unique_lock<std::mutex> lock(m_mutex);
m_empty.store(false);
m_wake_me_up_again |= blocking;
if (!m_enable)
return;
m_queue.push(event);
if (blocking)
{
m_cond.wait(lock, [this]{return m_queue.empty();});
}
}
void AsyncRequests::SetEnable(bool enable)
{
std::unique_lock<std::mutex> lock(m_mutex);
m_enable = enable;
if (!enable)
{
// flush the queue on disabling
while (!m_queue.empty())
m_queue.pop();
if (m_wake_me_up_again)
m_cond.notify_all();
}
}
void AsyncRequests::HandleEvent(const AsyncRequests::Event& e)
{
switch (e.type)
{
case Event::EFB_POKE_COLOR:
g_renderer->AccessEFB(POKE_COLOR, e.efb_poke.x, e.efb_poke.y, e.efb_poke.data);
break;
case Event::EFB_POKE_Z:
g_renderer->AccessEFB(POKE_Z, e.efb_poke.x, e.efb_poke.y, e.efb_poke.data);
break;
case Event::EFB_PEEK_COLOR:
*e.efb_peek.data = g_renderer->AccessEFB(PEEK_COLOR, e.efb_peek.x, e.efb_peek.y, 0);
break;
case Event::EFB_PEEK_Z:
*e.efb_peek.data = g_renderer->AccessEFB(PEEK_Z, e.efb_peek.x, e.efb_peek.y, 0);
break;
}
}

View File

@ -0,0 +1,71 @@
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#pragma once
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <queue>
#include "Common/CommonTypes.h"
class AsyncRequests
{
public:
struct Event
{
enum Type
{
EFB_POKE_COLOR,
EFB_POKE_Z,
EFB_PEEK_COLOR,
EFB_PEEK_Z,
} type;
u64 time;
union
{
struct
{
u16 x;
u16 y;
u32 data;
} efb_poke;
struct
{
u16 x;
u16 y;
u32* data;
} efb_peek;
};
};
AsyncRequests();
void PullEvents()
{
if (!m_empty.load())
PullEventsInternal();
}
void PushEvent(const Event& event, bool blocking = false);
void SetEnable(bool enable);
static AsyncRequests* GetInstance() { return &s_singleton; }
private:
void PullEventsInternal();
void HandleEvent(const Event& e);
static AsyncRequests s_singleton;
std::atomic<bool> m_empty;
std::queue<Event> m_queue;
std::mutex m_mutex;
std::condition_variable m_cond;
bool m_wake_me_up_again;
bool m_enable;
};

View File

@ -1,4 +1,5 @@
set(SRCS BoundingBox.cpp set(SRCS AsyncRequests.cpp
BoundingBox.cpp
BPFunctions.cpp BPFunctions.cpp
BPMemory.cpp BPMemory.cpp
BPStructs.cpp BPStructs.cpp

View File

@ -15,6 +15,7 @@
#include "Core/NetPlayProto.h" #include "Core/NetPlayProto.h"
#include "Core/HW/Memmap.h" #include "Core/HW/Memmap.h"
#include "VideoCommon/AsyncRequests.h"
#include "VideoCommon/CommandProcessor.h" #include "VideoCommon/CommandProcessor.h"
#include "VideoCommon/CPMemory.h" #include "VideoCommon/CPMemory.h"
#include "VideoCommon/DataReader.h" #include "VideoCommon/DataReader.h"
@ -282,11 +283,14 @@ void RunGpuLoop()
// This allows a system that we are maxing out in dual core mode to do other things // This allows a system that we are maxing out in dual core mode to do other things
bool yield_cpu = cpu_info.num_cores <= 2; bool yield_cpu = cpu_info.num_cores <= 2;
AsyncRequests::GetInstance()->SetEnable(true);
while (GpuRunningState) while (GpuRunningState)
{ {
g_video_backend->PeekMessages(); g_video_backend->PeekMessages();
VideoFifo_CheckAsyncRequest(); VideoFifo_CheckAsyncRequest();
AsyncRequests::GetInstance()->PullEvents();
if (g_use_deterministic_gpu_thread) if (g_use_deterministic_gpu_thread)
{ {
// All the fifo/CP stuff is on the CPU. We just need to run the opcode decoder. // All the fifo/CP stuff is on the CPU. We just need to run the opcode decoder.
@ -349,6 +353,7 @@ void RunGpuLoop()
// If we don't, s_swapRequested or s_efbAccessRequested won't be set to false // If we don't, s_swapRequested or s_efbAccessRequested won't be set to false
// leading the CPU thread to wait in Video_BeginField or Video_AccessEFB thus slowing things down. // leading the CPU thread to wait in Video_BeginField or Video_AccessEFB thus slowing things down.
VideoFifo_CheckAsyncRequest(); VideoFifo_CheckAsyncRequest();
AsyncRequests::GetInstance()->PullEvents();
CommandProcessor::isPossibleWaitingSetDrawDone = false; CommandProcessor::isPossibleWaitingSetDrawDone = false;
} }
@ -377,6 +382,7 @@ void RunGpuLoop()
} }
// wake up SyncGPU if we were interrupted // wake up SyncGPU if we were interrupted
s_video_buffer_cond.notify_all(); s_video_buffer_cond.notify_all();
AsyncRequests::GetInstance()->SetEnable(false);
} }

View File

@ -1,6 +1,7 @@
#include "Common/Event.h" #include "Common/Event.h"
#include "Core/ConfigManager.h" #include "Core/ConfigManager.h"
#include "VideoCommon/AsyncRequests.h"
#include "VideoCommon/BoundingBox.h" #include "VideoCommon/BoundingBox.h"
#include "VideoCommon/BPStructs.h" #include "VideoCommon/BPStructs.h"
#include "VideoCommon/CommandProcessor.h" #include "VideoCommon/CommandProcessor.h"
@ -20,8 +21,6 @@ bool s_BackendInitialized = false;
Common::Flag s_swapRequested; Common::Flag s_swapRequested;
static Common::Flag s_FifoShuttingDown; static Common::Flag s_FifoShuttingDown;
static Common::Flag s_efbAccessRequested;
static Common::Event s_efbAccessReadyEvent;
static Common::Flag s_perfQueryRequested; static Common::Flag s_perfQueryRequested;
static Common::Event s_perfQueryReadyEvent; static Common::Event s_perfQueryReadyEvent;
@ -39,16 +38,6 @@ static volatile struct
u32 fbHeight; u32 fbHeight;
} s_beginFieldArgs; } s_beginFieldArgs;
static struct
{
EFBAccessType type;
u32 x;
u32 y;
u32 Data;
} s_accessEFBArgs;
static u32 s_AccessEFBResult = 0;
void VideoBackendHardware::EmuStateChange(EMUSTATE_CHANGE newState) void VideoBackendHardware::EmuStateChange(EMUSTATE_CHANGE newState)
{ {
EmulatorState((newState == EMUSTATE_CHANGE_PLAY) ? true : false); EmulatorState((newState == EMUSTATE_CHANGE_PLAY) ? true : false);
@ -64,7 +53,6 @@ void VideoBackendHardware::Video_ExitLoop()
{ {
ExitGpuLoop(); ExitGpuLoop();
s_FifoShuttingDown.Set(); s_FifoShuttingDown.Set();
s_efbAccessReadyEvent.Set();
s_perfQueryReadyEvent.Set(); s_perfQueryReadyEvent.Set();
} }
@ -152,44 +140,36 @@ bool VideoBackendHardware::Video_Screenshot(const std::string& filename)
return true; return true;
} }
void VideoFifo_CheckEFBAccess()
{
if (s_efbAccessRequested.IsSet())
{
s_AccessEFBResult = g_renderer->AccessEFB(s_accessEFBArgs.type, s_accessEFBArgs.x, s_accessEFBArgs.y, s_accessEFBArgs.Data);
s_efbAccessRequested.Clear();
s_efbAccessReadyEvent.Set();
}
}
u32 VideoBackendHardware::Video_AccessEFB(EFBAccessType type, u32 x, u32 y, u32 InputData) u32 VideoBackendHardware::Video_AccessEFB(EFBAccessType type, u32 x, u32 y, u32 InputData)
{ {
if (s_BackendInitialized && g_ActiveConfig.bEFBAccessEnable) if (!g_ActiveConfig.bEFBAccessEnable)
{ {
SyncGPU(SYNC_GPU_EFB_POKE); return 0;
s_accessEFBArgs.type = type;
s_accessEFBArgs.x = x;
s_accessEFBArgs.y = y;
s_accessEFBArgs.Data = InputData;
s_efbAccessRequested.Set();
if (SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread)
{
s_efbAccessReadyEvent.Reset();
if (s_FifoShuttingDown.IsSet())
return 0;
s_efbAccessRequested.Set();
s_efbAccessReadyEvent.Wait();
}
else
VideoFifo_CheckEFBAccess();
return s_AccessEFBResult;
} }
return 0; if (type == POKE_COLOR || type == POKE_Z)
{
AsyncRequests::Event e;
e.type = type == POKE_COLOR ? AsyncRequests::Event::EFB_POKE_COLOR : AsyncRequests::Event::EFB_POKE_Z;
e.time = 0;
e.efb_poke.data = InputData;
e.efb_poke.x = x;
e.efb_poke.y = y;
AsyncRequests::GetInstance()->PushEvent(e, 0);
return 0;
}
else
{
AsyncRequests::Event e;
u32 result;
e.type = type == PEEK_COLOR ? AsyncRequests::Event::EFB_PEEK_COLOR : AsyncRequests::Event::EFB_PEEK_Z;
e.time = 0;
e.efb_peek.x = x;
e.efb_peek.y = y;
e.efb_peek.data = &result;
AsyncRequests::GetInstance()->PushEvent(e, 1);
return result;
}
} }
static void VideoFifo_CheckPerfQueryRequest() static void VideoFifo_CheckPerfQueryRequest()
@ -267,12 +247,9 @@ void VideoBackendHardware::InitializeShared()
VideoCommon_Init(); VideoCommon_Init();
s_swapRequested.Clear(); s_swapRequested.Clear();
s_efbAccessRequested.Clear();
s_perfQueryRequested.Clear(); s_perfQueryRequested.Clear();
s_FifoShuttingDown.Clear(); s_FifoShuttingDown.Clear();
memset((void*)&s_beginFieldArgs, 0, sizeof(s_beginFieldArgs)); memset((void*)&s_beginFieldArgs, 0, sizeof(s_beginFieldArgs));
memset(&s_accessEFBArgs, 0, sizeof(s_accessEFBArgs));
s_AccessEFBResult = 0;
m_invalid = false; m_invalid = false;
} }
@ -292,10 +269,7 @@ void VideoBackendHardware::DoState(PointerWrap& p)
p.DoMarker("VideoCommon"); p.DoMarker("VideoCommon");
p.Do(s_swapRequested); p.Do(s_swapRequested);
p.Do(s_efbAccessRequested);
p.Do(s_beginFieldArgs); p.Do(s_beginFieldArgs);
p.Do(s_accessEFBArgs);
p.Do(s_AccessEFBResult);
p.DoMarker("VideoBackendHardware"); p.DoMarker("VideoBackendHardware");
// Refresh state. // Refresh state.
@ -335,7 +309,6 @@ void VideoBackendHardware::RunLoop(bool enable)
void VideoFifo_CheckAsyncRequest() void VideoFifo_CheckAsyncRequest()
{ {
VideoFifo_CheckSwapRequest(); VideoFifo_CheckSwapRequest();
VideoFifo_CheckEFBAccess();
VideoFifo_CheckPerfQueryRequest(); VideoFifo_CheckPerfQueryRequest();
VideoFifo_CheckBBoxRequest(); VideoFifo_CheckBBoxRequest();
} }

View File

@ -6,5 +6,4 @@
extern bool s_BackendInitialized; extern bool s_BackendInitialized;
extern Common::Flag s_swapRequested; extern Common::Flag s_swapRequested;
void VideoFifo_CheckEFBAccess();
void VideoFifo_CheckSwapRequestAt(u32 xfbAddr, u32 fbWidth, u32 fbHeight); void VideoFifo_CheckSwapRequestAt(u32 xfbAddr, u32 fbWidth, u32 fbHeight);

View File

@ -116,7 +116,6 @@ void Renderer::RenderToXFB(u32 xfbAddr, const EFBRectangle& sourceRc, u32 fbWidt
if (!fbWidth || !fbHeight) if (!fbWidth || !fbHeight)
return; return;
VideoFifo_CheckEFBAccess();
VideoFifo_CheckSwapRequestAt(xfbAddr, fbWidth, fbHeight); VideoFifo_CheckSwapRequestAt(xfbAddr, fbWidth, fbHeight);
XFBWrited = true; XFBWrited = true;

View File

@ -166,8 +166,6 @@ void VertexManager::Flush()
// loading a state will invalidate BP, so check for it // loading a state will invalidate BP, so check for it
g_video_backend->CheckInvalidState(); g_video_backend->CheckInvalidState();
VideoFifo_CheckEFBAccess();
#if defined(_DEBUG) || defined(DEBUGFAST) #if defined(_DEBUG) || defined(DEBUGFAST)
PRIM_LOG("frame%d:\n texgen=%d, numchan=%d, dualtex=%d, ztex=%d, cole=%d, alpe=%d, ze=%d", g_ActiveConfig.iSaveTargetId, xfmem.numTexGen.numTexGens, PRIM_LOG("frame%d:\n texgen=%d, numchan=%d, dualtex=%d, ztex=%d, cole=%d, alpe=%d, ze=%d", g_ActiveConfig.iSaveTargetId, xfmem.numTexGen.numTexGens,
xfmem.numChan.numColorChans, xfmem.dualTexTrans.enabled, bpmem.ztex2.op, xfmem.numChan.numColorChans, xfmem.dualTexTrans.enabled, bpmem.ztex2.op,

View File

@ -35,6 +35,7 @@
</ImportGroup> </ImportGroup>
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<ItemGroup> <ItemGroup>
<ClCompile Include="AsyncRequests.cpp" />
<ClCompile Include="AVIDump.cpp" /> <ClCompile Include="AVIDump.cpp" />
<ClCompile Include="BoundingBox.cpp" /> <ClCompile Include="BoundingBox.cpp" />
<ClCompile Include="BPFunctions.cpp" /> <ClCompile Include="BPFunctions.cpp" />
@ -84,6 +85,7 @@
<ClCompile Include="XFStructs.cpp" /> <ClCompile Include="XFStructs.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="AsyncRequests.h" />
<ClInclude Include="AVIDump.h" /> <ClInclude Include="AVIDump.h" />
<ClInclude Include="BoundingBox.h" /> <ClInclude Include="BoundingBox.h" />
<ClInclude Include="BPFunctions.h" /> <ClInclude Include="BPFunctions.h" />

View File

@ -146,6 +146,9 @@
<ClCompile Include="TextureDecoder_x64.cpp"> <ClCompile Include="TextureDecoder_x64.cpp">
<Filter>Decoding</Filter> <Filter>Decoding</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="AsyncRequests.cpp">
<Filter>Util</Filter>
</ClCompile>
<ClCompile Include="BoundingBox.cpp"> <ClCompile Include="BoundingBox.cpp">
<Filter>Util</Filter> <Filter>Util</Filter>
</ClCompile> </ClCompile>
@ -290,6 +293,9 @@
<ClInclude Include="VertexLoaderUtils.h"> <ClInclude Include="VertexLoaderUtils.h">
<Filter>Vertex Loading</Filter> <Filter>Vertex Loading</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="AsyncRequests.h">
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="BoundingBox.h"> <ClInclude Include="BoundingBox.h">
<Filter>Util</Filter> <Filter>Util</Filter>
</ClInclude> </ClInclude>