dolphin/Source/Core/VideoCommon/AsyncRequests.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

176 lines
4.1 KiB
C++
Raw Normal View History

2015-05-23 22:32:32 -06:00
// Copyright 2015 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2015-05-23 22:32:32 -06:00
2021-12-09 19:22:16 -07:00
#include "VideoCommon/AsyncRequests.h"
#include <mutex>
#include "VideoCommon/Fifo.h"
#include "VideoCommon/RenderBase.h"
2019-03-03 18:25:33 -07:00
#include "VideoCommon/Statistics.h"
#include "VideoCommon/VertexManagerBase.h"
#include "VideoCommon/VideoBackendBase.h"
#include "VideoCommon/VideoCommon.h"
#include "VideoCommon/VideoState.h"
AsyncRequests AsyncRequests::s_singleton;
AsyncRequests::AsyncRequests() = default;
void AsyncRequests::PullEventsInternal()
{
// This is only called if the queue isn't empty.
// So just flush the pipeline to get accurate results.
g_vertex_manager->Flush();
std::unique_lock<std::mutex> lock(m_mutex);
m_empty.Set();
while (!m_queue.empty())
{
2015-05-01 10:58:11 -06:00
Event e = m_queue.front();
2015-05-01 10:58:11 -06:00
// try to merge as many efb pokes as possible
// it's a bit hacky, but some games render a complete frame in this way
if ((e.type == Event::EFB_POKE_COLOR || e.type == Event::EFB_POKE_Z))
{
m_merged_efb_pokes.clear();
Event first_event = m_queue.front();
const auto t = first_event.type == Event::EFB_POKE_COLOR ? EFBAccessType::PokeColor :
EFBAccessType::PokeZ;
2015-05-01 10:58:11 -06:00
do
{
e = m_queue.front();
2015-05-01 10:58:11 -06:00
EfbPokeData d;
d.data = e.efb_poke.data;
d.x = e.efb_poke.x;
d.y = e.efb_poke.y;
m_merged_efb_pokes.push_back(d);
2015-05-01 10:58:11 -06:00
m_queue.pop();
} while (!m_queue.empty() && m_queue.front().type == first_event.type);
2015-05-01 10:58:11 -06:00
lock.unlock();
g_renderer->PokeEFB(t, m_merged_efb_pokes.data(), m_merged_efb_pokes.size());
2015-05-01 10:58:11 -06:00
lock.lock();
continue;
}
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);
if (m_passthrough)
{
HandleEvent(event);
return;
}
m_empty.Clear();
m_wake_me_up_again |= blocking;
if (!m_enable)
return;
m_queue.push(event);
2016-01-12 14:44:58 -07:00
Fifo::RunGpu();
if (blocking)
{
m_cond.wait(lock, [this] { return m_queue.empty(); });
}
}
void AsyncRequests::WaitForEmptyQueue()
{
std::unique_lock<std::mutex> lock(m_mutex);
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:
{
INCSTAT(g_stats.this_frame.num_efb_pokes);
EfbPokeData poke = {e.efb_poke.x, e.efb_poke.y, e.efb_poke.data};
g_renderer->PokeEFB(EFBAccessType::PokeColor, &poke, 1);
}
break;
case Event::EFB_POKE_Z:
{
INCSTAT(g_stats.this_frame.num_efb_pokes);
EfbPokeData poke = {e.efb_poke.x, e.efb_poke.y, e.efb_poke.data};
g_renderer->PokeEFB(EFBAccessType::PokeZ, &poke, 1);
}
break;
case Event::EFB_PEEK_COLOR:
INCSTAT(g_stats.this_frame.num_efb_peeks);
*e.efb_peek.data =
g_renderer->AccessEFB(EFBAccessType::PeekColor, e.efb_peek.x, e.efb_peek.y, 0);
break;
case Event::EFB_PEEK_Z:
INCSTAT(g_stats.this_frame.num_efb_peeks);
*e.efb_peek.data = g_renderer->AccessEFB(EFBAccessType::PeekZ, e.efb_peek.x, e.efb_peek.y, 0);
break;
case Event::SWAP_EVENT:
g_renderer->Swap(e.swap_event.xfbAddr, e.swap_event.fbWidth, e.swap_event.fbStride,
e.swap_event.fbHeight, e.time);
break;
2015-01-31 04:43:58 -07:00
case Event::BBOX_READ:
*e.bbox.data = g_renderer->BBoxRead(e.bbox.index);
break;
case Event::PERF_QUERY:
g_perf_query->FlushResults();
break;
case Event::DO_SAVE_STATE:
VideoCommon_DoState(*e.do_save_state.p);
break;
}
}
void AsyncRequests::SetPassthrough(bool enable)
{
std::unique_lock<std::mutex> lock(m_mutex);
m_passthrough = enable;
}