2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2017-09-02 17:23:26 +00:00
|
|
|
#include "VideoCommon/PixelEngine.h"
|
|
|
|
|
2016-08-10 07:44:08 +00:00
|
|
|
#include <mutex>
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2022-01-29 21:28:17 +00:00
|
|
|
#include "Common/BitField.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/ChunkFile.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2022-01-07 02:50:18 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2015-06-06 05:20:51 +00:00
|
|
|
#include "Core/Core.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/CoreTiming.h"
|
|
|
|
#include "Core/HW/MMIO.h"
|
|
|
|
#include "Core/HW/ProcessorInterface.h"
|
2022-01-07 02:50:18 +00:00
|
|
|
#include "Core/System.h"
|
|
|
|
|
2014-09-17 01:04:37 +00:00
|
|
|
#include "VideoCommon/BoundingBox.h"
|
2015-01-30 22:48:23 +00:00
|
|
|
#include "VideoCommon/Fifo.h"
|
2017-09-02 17:23:26 +00:00
|
|
|
#include "VideoCommon/PerfQueryBase.h"
|
2021-06-09 11:42:21 +00:00
|
|
|
#include "VideoCommon/RenderBase.h"
|
2017-09-02 17:23:26 +00:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
2012-06-17 11:58:29 +00:00
|
|
|
|
2009-10-10 21:19:39 +00:00
|
|
|
namespace PixelEngine
|
|
|
|
{
|
2022-12-10 15:35:07 +00:00
|
|
|
enum
|
2022-01-29 21:28:17 +00:00
|
|
|
{
|
2022-12-10 15:35:07 +00:00
|
|
|
INT_CAUSE_PE_TOKEN = 0x200, // GP Token
|
|
|
|
INT_CAUSE_PE_FINISH = 0x400, // GP Finished
|
2009-10-10 21:19:39 +00:00
|
|
|
};
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
void PixelEngineManager::DoState(PointerWrap& p)
|
2017-01-04 11:45:40 +00:00
|
|
|
{
|
2022-12-10 15:35:07 +00:00
|
|
|
p.Do(m_z_conf);
|
|
|
|
p.Do(m_alpha_conf);
|
|
|
|
p.Do(m_dst_alpha_conf);
|
|
|
|
p.Do(m_alpha_mode_conf);
|
|
|
|
p.Do(m_alpha_read);
|
|
|
|
p.Do(m_control);
|
2016-08-10 07:44:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
p.Do(m_token);
|
|
|
|
p.Do(m_token_pending);
|
|
|
|
p.Do(m_token_interrupt_pending);
|
|
|
|
p.Do(m_finish_interrupt_pending);
|
|
|
|
p.Do(m_event_raised);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
p.Do(m_signal_token_interrupt);
|
|
|
|
p.Do(m_signal_finish_interrupt);
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
void PixelEngineManager::Init()
|
2009-10-10 21:19:39 +00:00
|
|
|
{
|
2022-12-10 15:35:07 +00:00
|
|
|
m_control.hex = 0;
|
|
|
|
m_z_conf.hex = 0;
|
|
|
|
m_alpha_conf.hex = 0;
|
|
|
|
m_dst_alpha_conf.hex = 0;
|
|
|
|
m_alpha_mode_conf.hex = 0;
|
|
|
|
m_alpha_read.hex = 0;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
m_token = 0;
|
|
|
|
m_token_pending = 0;
|
|
|
|
m_token_interrupt_pending = false;
|
|
|
|
m_finish_interrupt_pending = false;
|
|
|
|
m_event_raised = false;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
m_signal_token_interrupt = false;
|
|
|
|
m_signal_finish_interrupt = false;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
m_event_type_set_token_finish = Core::System::GetInstance().GetCoreTiming().RegisterEvent(
|
|
|
|
"SetTokenFinish", SetTokenFinish_OnMainThread_Static);
|
2009-10-10 21:19:39 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
void PixelEngineManager::RegisterMMIO(MMIO::Mapping* mmio, u32 base)
|
2009-10-10 21:19:39 +00:00
|
|
|
{
|
2014-02-02 15:08:09 +00:00
|
|
|
// Directly mapped registers.
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
u32 addr;
|
|
|
|
u16* ptr;
|
|
|
|
} directly_mapped_vars[] = {
|
2022-12-10 15:35:07 +00:00
|
|
|
{PE_ZCONF, &m_z_conf.hex},
|
|
|
|
{PE_ALPHACONF, &m_alpha_conf.hex},
|
|
|
|
{PE_DSTALPHACONF, &m_dst_alpha_conf.hex},
|
|
|
|
{PE_ALPHAMODE, &m_alpha_mode_conf.hex},
|
|
|
|
{PE_ALPHAREAD, &m_alpha_read.hex},
|
2014-02-02 15:08:09 +00:00
|
|
|
};
|
|
|
|
for (auto& mapped_var : directly_mapped_vars)
|
2009-10-10 21:19:39 +00:00
|
|
|
{
|
2014-02-02 15:08:09 +00:00
|
|
|
mmio->Register(base | mapped_var.addr, MMIO::DirectRead<u16>(mapped_var.ptr),
|
|
|
|
MMIO::DirectWrite<u16>(mapped_var.ptr));
|
2009-10-10 21:19:39 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-02-02 15:08:09 +00:00
|
|
|
// Performance queries registers: read only, need to call the video backend
|
|
|
|
// to get the results.
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
u32 addr;
|
|
|
|
PerfQueryType pqtype;
|
|
|
|
} pq_regs[] = {
|
|
|
|
{PE_PERF_ZCOMP_INPUT_ZCOMPLOC_L, PQ_ZCOMP_INPUT_ZCOMPLOC},
|
|
|
|
{PE_PERF_ZCOMP_OUTPUT_ZCOMPLOC_L, PQ_ZCOMP_OUTPUT_ZCOMPLOC},
|
|
|
|
{PE_PERF_ZCOMP_INPUT_L, PQ_ZCOMP_INPUT},
|
|
|
|
{PE_PERF_ZCOMP_OUTPUT_L, PQ_ZCOMP_OUTPUT},
|
|
|
|
{PE_PERF_BLEND_INPUT_L, PQ_BLEND_INPUT},
|
|
|
|
{PE_PERF_EFB_COPY_CLOCKS_L, PQ_EFB_COPY_CLOCKS},
|
|
|
|
};
|
|
|
|
for (auto& pq_reg : pq_regs)
|
2009-10-10 21:19:39 +00:00
|
|
|
{
|
2022-10-14 00:17:09 +00:00
|
|
|
mmio->Register(base | pq_reg.addr, MMIO::ComplexRead<u16>([pq_reg](Core::System&, u32) {
|
2014-02-02 15:08:09 +00:00
|
|
|
return g_video_backend->Video_GetQueryResult(pq_reg.pqtype) & 0xFFFF;
|
|
|
|
}),
|
|
|
|
MMIO::InvalidWrite<u16>());
|
2022-10-14 00:17:09 +00:00
|
|
|
mmio->Register(base | (pq_reg.addr + 2), MMIO::ComplexRead<u16>([pq_reg](Core::System&, u32) {
|
2014-02-02 15:08:09 +00:00
|
|
|
return g_video_backend->Video_GetQueryResult(pq_reg.pqtype) >> 16;
|
|
|
|
}),
|
|
|
|
MMIO::InvalidWrite<u16>());
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-02-02 15:08:09 +00:00
|
|
|
// Control register
|
2022-12-10 15:35:07 +00:00
|
|
|
mmio->Register(base | PE_CTRL_REGISTER, MMIO::DirectRead<u16>(&m_control.hex),
|
|
|
|
MMIO::ComplexWrite<u16>([](Core::System& system, u32, u16 val) {
|
|
|
|
auto& pe = system.GetPixelEngine();
|
|
|
|
|
2022-01-29 21:28:17 +00:00
|
|
|
UPECtrlReg tmpCtrl{.hex = val};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-01-29 21:28:17 +00:00
|
|
|
if (tmpCtrl.pe_token)
|
2022-12-10 15:35:07 +00:00
|
|
|
pe.m_signal_token_interrupt = false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-01-29 21:28:17 +00:00
|
|
|
if (tmpCtrl.pe_finish)
|
2022-12-10 15:35:07 +00:00
|
|
|
pe.m_signal_finish_interrupt = false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
pe.m_control.pe_token_enable = tmpCtrl.pe_token_enable.Value();
|
|
|
|
pe.m_control.pe_finish_enable = tmpCtrl.pe_finish_enable.Value();
|
|
|
|
pe.m_control.pe_token = false; // this flag is write only
|
|
|
|
pe.m_control.pe_finish = false; // this flag is write only
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-14 03:33:26 +00:00
|
|
|
DEBUG_LOG_FMT(PIXELENGINE, "(w16) CTRL_REGISTER: {:#06x}", val);
|
2022-12-10 15:35:07 +00:00
|
|
|
pe.UpdateInterrupts();
|
2014-02-02 15:08:09 +00:00
|
|
|
}));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-02-02 15:08:09 +00:00
|
|
|
// Token register, readonly.
|
2022-12-10 15:35:07 +00:00
|
|
|
mmio->Register(base | PE_TOKEN_REG, MMIO::DirectRead<u16>(&m_token), MMIO::InvalidWrite<u16>());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-02-02 15:08:09 +00:00
|
|
|
// BBOX registers, readonly and need to update a flag.
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
2022-10-14 00:17:09 +00:00
|
|
|
mmio->Register(base | (PE_BBOX_LEFT + 2 * i), MMIO::ComplexRead<u16>([i](Core::System&, u32) {
|
2021-06-09 11:42:21 +00:00
|
|
|
g_renderer->BBoxDisable();
|
2014-11-13 22:26:49 +00:00
|
|
|
return g_video_backend->Video_GetBoundingBox(i);
|
2014-02-02 15:08:09 +00:00
|
|
|
}),
|
|
|
|
MMIO::InvalidWrite<u16>());
|
2009-10-10 21:19:39 +00:00
|
|
|
}
|
2014-02-02 15:08:09 +00:00
|
|
|
}
|
2010-12-13 07:56:54 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
void PixelEngineManager::UpdateInterrupts()
|
2009-10-10 21:19:39 +00:00
|
|
|
{
|
|
|
|
// check if there is a token-interrupt
|
2016-08-08 20:35:01 +00:00
|
|
|
ProcessorInterface::SetInterrupt(INT_CAUSE_PE_TOKEN,
|
2022-12-10 15:35:07 +00:00
|
|
|
m_signal_token_interrupt && m_control.pe_token_enable);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2009-10-10 21:19:39 +00:00
|
|
|
// check if there is a finish-interrupt
|
2016-08-08 20:35:01 +00:00
|
|
|
ProcessorInterface::SetInterrupt(INT_CAUSE_PE_FINISH,
|
2022-12-10 15:35:07 +00:00
|
|
|
m_signal_finish_interrupt && m_control.pe_finish_enable);
|
2010-12-13 07:56:54 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
void PixelEngineManager::SetTokenFinish_OnMainThread_Static(Core::System& system, u64 userdata,
|
|
|
|
s64 cycles_late)
|
2009-10-10 21:19:39 +00:00
|
|
|
{
|
2022-12-10 15:35:07 +00:00
|
|
|
system.GetPixelEngine().SetTokenFinish_OnMainThread(system, userdata, cycles_late);
|
|
|
|
}
|
2016-08-10 07:44:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
void PixelEngineManager::SetTokenFinish_OnMainThread(Core::System& system, u64 userdata,
|
|
|
|
s64 cycles_late)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_token_finish_mutex);
|
|
|
|
m_event_raised = false;
|
|
|
|
|
|
|
|
m_token = m_token_pending;
|
2016-08-10 07:44:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
if (m_token_interrupt_pending)
|
2016-08-10 07:44:08 +00:00
|
|
|
{
|
2022-12-10 15:35:07 +00:00
|
|
|
m_token_interrupt_pending = false;
|
|
|
|
m_signal_token_interrupt = true;
|
2016-08-10 07:44:08 +00:00
|
|
|
UpdateInterrupts();
|
|
|
|
}
|
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
if (m_finish_interrupt_pending)
|
2016-08-10 07:44:08 +00:00
|
|
|
{
|
2022-12-10 15:35:07 +00:00
|
|
|
m_finish_interrupt_pending = false;
|
|
|
|
m_signal_finish_interrupt = true;
|
2016-08-10 07:44:08 +00:00
|
|
|
UpdateInterrupts();
|
|
|
|
lk.unlock();
|
|
|
|
Core::FrameUpdateOnCPUThread();
|
|
|
|
}
|
2009-10-10 21:19:39 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2016-08-10 07:44:08 +00:00
|
|
|
// Raise the event handler above on the CPU thread.
|
2022-12-10 15:35:07 +00:00
|
|
|
// m_token_finish_mutex must be locked.
|
2016-08-10 07:44:08 +00:00
|
|
|
// THIS IS EXECUTED FROM VIDEO THREAD
|
2022-12-10 15:35:07 +00:00
|
|
|
void PixelEngineManager::RaiseEvent(int cycles_into_future)
|
2009-10-10 21:19:39 +00:00
|
|
|
{
|
2022-12-10 15:35:07 +00:00
|
|
|
if (m_event_raised)
|
2016-08-10 07:44:08 +00:00
|
|
|
return;
|
2015-06-06 05:20:51 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
m_event_raised = true;
|
2016-07-07 13:52:08 +00:00
|
|
|
|
|
|
|
CoreTiming::FromThread from = CoreTiming::FromThread::NON_CPU;
|
2021-11-24 22:01:37 +00:00
|
|
|
s64 cycles = 0; // we don't care about timings for dual core mode.
|
2022-12-09 19:01:25 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
if (!system.IsDualCoreMode() || system.GetFifo().UseDeterministicGPUThread())
|
2021-11-24 22:01:37 +00:00
|
|
|
{
|
2016-07-07 13:52:08 +00:00
|
|
|
from = CoreTiming::FromThread::CPU;
|
2021-11-24 22:01:37 +00:00
|
|
|
|
|
|
|
// Hack: Dolphin's single-core gpu timings are way too fast. Enforce a minimum delay to give
|
|
|
|
// games time to setup any interrupt state
|
|
|
|
cycles = std::max(500, cycles_into_future);
|
|
|
|
}
|
2022-12-10 15:35:07 +00:00
|
|
|
Core::System::GetInstance().GetCoreTiming().ScheduleEvent(cycles, m_event_type_set_token_finish,
|
2022-11-26 08:29:46 +00:00
|
|
|
0, from);
|
2009-10-10 21:19:39 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-10 21:19:39 +00:00
|
|
|
// SetToken
|
|
|
|
// THIS IS EXECUTED FROM VIDEO THREAD
|
2022-12-10 15:35:07 +00:00
|
|
|
void PixelEngineManager::SetToken(const u16 token, const bool interrupt, int cycles_into_future)
|
2009-10-10 21:19:39 +00:00
|
|
|
{
|
2020-11-14 03:33:26 +00:00
|
|
|
DEBUG_LOG_FMT(PIXELENGINE, "VIDEO Backend raises INT_CAUSE_PE_TOKEN (btw, token: {:04x})", token);
|
2016-08-08 20:35:01 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_token_finish_mutex);
|
2016-08-10 07:44:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
m_token_pending = token;
|
|
|
|
m_token_interrupt_pending |= interrupt;
|
2016-08-10 07:44:08 +00:00
|
|
|
|
2021-11-24 22:01:37 +00:00
|
|
|
RaiseEvent(cycles_into_future);
|
2009-10-10 21:19:39 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-10 21:19:39 +00:00
|
|
|
// SetFinish
|
|
|
|
// THIS IS EXECUTED FROM VIDEO THREAD (BPStructs.cpp) when a new frame has been drawn
|
2022-12-10 15:35:07 +00:00
|
|
|
void PixelEngineManager::SetFinish(int cycles_into_future)
|
2009-10-10 21:19:39 +00:00
|
|
|
{
|
2020-11-14 03:33:26 +00:00
|
|
|
DEBUG_LOG_FMT(PIXELENGINE, "VIDEO Set Finish");
|
2016-08-10 07:44:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_token_finish_mutex);
|
2016-08-10 07:44:08 +00:00
|
|
|
|
2022-12-10 15:35:07 +00:00
|
|
|
m_finish_interrupt_pending |= true;
|
2016-08-10 07:44:08 +00:00
|
|
|
|
2021-11-24 22:01:37 +00:00
|
|
|
RaiseEvent(cycles_into_future);
|
2009-10-10 21:19:39 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2021-06-07 08:55:52 +00:00
|
|
|
} // namespace PixelEngine
|