2015-05-23 22:32:32 -06:00
|
|
|
// Copyright 2015 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// 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"
|
|
|
|
|
2016-01-17 14:54:31 -07:00
|
|
|
#include <mutex>
|
|
|
|
|
2022-12-09 12:01:25 -07:00
|
|
|
#include "Core/System.h"
|
2015-03-05 09:12:24 -07:00
|
|
|
#include "VideoCommon/Fifo.h"
|
2015-01-31 03:38:23 -07:00
|
|
|
#include "VideoCommon/RenderBase.h"
|
2019-03-03 18:25:33 -07:00
|
|
|
#include "VideoCommon/Statistics.h"
|
2018-03-26 13:33:16 -06:00
|
|
|
#include "VideoCommon/VertexManagerBase.h"
|
2016-01-17 14:54:31 -07:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2019-06-29 03:27:53 -06:00
|
|
|
#include "VideoCommon/VideoState.h"
|
2015-01-31 03:38:23 -07:00
|
|
|
|
|
|
|
AsyncRequests AsyncRequests::s_singleton;
|
|
|
|
|
2018-04-01 17:01:55 -06:00
|
|
|
AsyncRequests::AsyncRequests() = default;
|
2015-01-31 03:38:23 -07:00
|
|
|
|
|
|
|
void AsyncRequests::PullEventsInternal()
|
|
|
|
{
|
2018-03-26 13:33:16 -06:00
|
|
|
// This is only called if the queue isn't empty.
|
|
|
|
// So just flush the pipeline to get accurate results.
|
|
|
|
g_vertex_manager->Flush();
|
|
|
|
|
2015-01-31 03:38:23 -07:00
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
2016-08-05 08:04:39 -06:00
|
|
|
m_empty.Set();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-31 03:38:23 -07:00
|
|
|
while (!m_queue.empty())
|
|
|
|
{
|
2015-05-01 10:58:11 -06:00
|
|
|
Event e = m_queue.front();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
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();
|
2017-01-23 00:51:46 -07:00
|
|
|
const auto t = first_event.type == Event::EFB_POKE_COLOR ? EFBAccessType::PokeColor :
|
|
|
|
EFBAccessType::PokeZ;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-05-01 10:58:11 -06:00
|
|
|
do
|
|
|
|
{
|
|
|
|
e = m_queue.front();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
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);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-05-01 10:58:11 -06:00
|
|
|
m_queue.pop();
|
|
|
|
} while (!m_queue.empty() && m_queue.front().type == first_event.type);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-05-01 10:58:11 -06:00
|
|
|
lock.unlock();
|
2015-12-19 07:34:56 -07:00
|
|
|
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;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-31 03:38:23 -07:00
|
|
|
lock.unlock();
|
|
|
|
HandleEvent(e);
|
|
|
|
lock.lock();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-31 03:38:23 -07:00
|
|
|
m_queue.pop();
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-31 03:38:23 -07:00
|
|
|
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);
|
2015-01-31 04:01:01 -07:00
|
|
|
|
|
|
|
if (m_passthrough)
|
|
|
|
{
|
|
|
|
HandleEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-05 08:04:39 -06:00
|
|
|
m_empty.Clear();
|
2015-01-31 03:38:23 -07:00
|
|
|
m_wake_me_up_again |= blocking;
|
|
|
|
|
|
|
|
if (!m_enable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_queue.push(event);
|
|
|
|
|
2022-12-09 12:01:25 -07:00
|
|
|
auto& system = Core::System::GetInstance();
|
2022-12-09 14:59:11 -07:00
|
|
|
system.GetFifo().RunGpu(system);
|
2015-01-31 03:38:23 -07:00
|
|
|
if (blocking)
|
|
|
|
{
|
|
|
|
m_cond.wait(lock, [this] { return m_queue.empty(); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 11:37:32 -06:00
|
|
|
void AsyncRequests::WaitForEmptyQueue()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
m_cond.wait(lock, [this] { return m_queue.empty(); });
|
|
|
|
}
|
|
|
|
|
2015-01-31 03:38:23 -07:00
|
|
|
void AsyncRequests::SetEnable(bool enable)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
m_enable = enable;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-31 03:38:23 -07:00
|
|
|
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:
|
2015-12-19 07:34:56 -07:00
|
|
|
{
|
2019-07-10 21:34:50 -06:00
|
|
|
INCSTAT(g_stats.this_frame.num_efb_pokes);
|
2015-12-19 07:34:56 -07:00
|
|
|
EfbPokeData poke = {e.efb_poke.x, e.efb_poke.y, e.efb_poke.data};
|
2017-01-23 00:51:46 -07:00
|
|
|
g_renderer->PokeEFB(EFBAccessType::PokeColor, &poke, 1);
|
2015-12-19 07:34:56 -07:00
|
|
|
}
|
2015-01-31 03:38:23 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-31 03:38:23 -07:00
|
|
|
case Event::EFB_POKE_Z:
|
2015-12-19 07:34:56 -07:00
|
|
|
{
|
2019-07-10 21:34:50 -06:00
|
|
|
INCSTAT(g_stats.this_frame.num_efb_pokes);
|
2015-12-19 07:34:56 -07:00
|
|
|
EfbPokeData poke = {e.efb_poke.x, e.efb_poke.y, e.efb_poke.data};
|
2017-01-23 00:51:46 -07:00
|
|
|
g_renderer->PokeEFB(EFBAccessType::PokeZ, &poke, 1);
|
2015-12-19 07:34:56 -07:00
|
|
|
}
|
2015-01-31 03:38:23 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-31 03:38:23 -07:00
|
|
|
case Event::EFB_PEEK_COLOR:
|
2019-07-10 21:34:50 -06:00
|
|
|
INCSTAT(g_stats.this_frame.num_efb_peeks);
|
2017-01-23 00:51:46 -07:00
|
|
|
*e.efb_peek.data =
|
|
|
|
g_renderer->AccessEFB(EFBAccessType::PeekColor, e.efb_peek.x, e.efb_peek.y, 0);
|
2015-01-31 03:38:23 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-31 03:38:23 -07:00
|
|
|
case Event::EFB_PEEK_Z:
|
2019-07-10 21:34:50 -06:00
|
|
|
INCSTAT(g_stats.this_frame.num_efb_peeks);
|
2017-01-23 00:51:46 -07:00
|
|
|
*e.efb_peek.data = g_renderer->AccessEFB(EFBAccessType::PeekZ, e.efb_peek.x, e.efb_peek.y, 0);
|
2015-01-31 03:38:23 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-31 04:01:01 -07:00
|
|
|
case Event::SWAP_EVENT:
|
2017-03-03 23:39:50 -07:00
|
|
|
g_renderer->Swap(e.swap_event.xfbAddr, e.swap_event.fbWidth, e.swap_event.fbStride,
|
2019-03-30 22:11:53 -06:00
|
|
|
e.swap_event.fbHeight, e.time);
|
2015-01-31 04:01:01 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-31 04:43:58 -07:00
|
|
|
case Event::BBOX_READ:
|
|
|
|
*e.bbox.data = g_renderer->BBoxRead(e.bbox.index);
|
2015-01-31 05:09:25 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-08-18 14:38:37 -06:00
|
|
|
case Event::FIFO_RESET:
|
2022-12-09 12:01:25 -07:00
|
|
|
Core::System::GetInstance().GetFifo().ResetVideoBuffer();
|
2022-08-18 14:38:37 -06:00
|
|
|
break;
|
|
|
|
|
2015-01-31 05:09:25 -07:00
|
|
|
case Event::PERF_QUERY:
|
|
|
|
g_perf_query->FlushResults();
|
|
|
|
break;
|
2019-06-29 02:35:12 -06:00
|
|
|
|
|
|
|
case Event::DO_SAVE_STATE:
|
2019-06-29 03:27:53 -06:00
|
|
|
VideoCommon_DoState(*e.do_save_state.p);
|
2019-06-29 02:35:12 -06:00
|
|
|
break;
|
2015-01-31 03:38:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 04:01:01 -07:00
|
|
|
void AsyncRequests::SetPassthrough(bool enable)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
m_passthrough = enable;
|
|
|
|
}
|