2023-01-31 04:58:54 +00:00
|
|
|
// Copyright 2023 Dolphin Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#include "VideoCommon/Widescreen.h"
|
|
|
|
|
|
|
|
#include "Common/ChunkFile.h"
|
2023-12-18 18:16:58 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2023-01-31 04:58:54 +00:00
|
|
|
#include "Core/Config/SYSCONFSettings.h"
|
2024-01-31 01:56:56 +00:00
|
|
|
#include "Core/System.h"
|
2023-01-31 04:58:54 +00:00
|
|
|
|
2023-09-05 20:34:43 +00:00
|
|
|
#include "VideoCommon/Statistics.h"
|
2023-01-31 04:58:54 +00:00
|
|
|
#include "VideoCommon/VertexManagerBase.h"
|
|
|
|
|
|
|
|
std::unique_ptr<WidescreenManager> g_widescreen;
|
|
|
|
|
|
|
|
WidescreenManager::WidescreenManager()
|
|
|
|
{
|
2024-10-16 01:39:23 +00:00
|
|
|
if (std::optional<bool> is_game_widescreen = GetWidescreenOverride())
|
|
|
|
m_is_game_widescreen = *is_game_widescreen;
|
2023-12-18 18:16:58 +00:00
|
|
|
|
|
|
|
// Throw a warning as unsupported aspect ratio modes have no specific behavior to them
|
|
|
|
const bool is_valid_suggested_aspect_mode =
|
|
|
|
g_ActiveConfig.suggested_aspect_mode == AspectMode::Auto ||
|
|
|
|
g_ActiveConfig.suggested_aspect_mode == AspectMode::ForceStandard ||
|
|
|
|
g_ActiveConfig.suggested_aspect_mode == AspectMode::ForceWide;
|
|
|
|
if (!is_valid_suggested_aspect_mode)
|
|
|
|
{
|
|
|
|
WARN_LOG_FMT(VIDEO,
|
|
|
|
"Invalid suggested aspect ratio mode: only Auto, 4:3 and 16:9 are supported");
|
|
|
|
}
|
2023-01-31 04:58:54 +00:00
|
|
|
|
2023-01-31 06:21:15 +00:00
|
|
|
m_config_changed = ConfigChangedEvent::Register(
|
|
|
|
[this](u32 bits) {
|
|
|
|
if (bits & (CONFIG_CHANGE_BIT_ASPECT_RATIO))
|
2023-12-18 18:16:58 +00:00
|
|
|
{
|
|
|
|
// If the widescreen flag isn't being overridden by any settings,
|
|
|
|
// reset it to default if heuristic aren't running or to the last
|
|
|
|
// heuristic value if they were running.
|
2024-10-16 01:39:23 +00:00
|
|
|
if (std::optional<bool> is_game_widescreen = GetWidescreenOverride())
|
|
|
|
m_is_game_widescreen = *is_game_widescreen;
|
|
|
|
else
|
|
|
|
m_is_game_widescreen = (m_heuristic_state == HeuristicState::Active_Found_Anamorphic);
|
2023-12-18 18:16:58 +00:00
|
|
|
}
|
2023-01-31 06:21:15 +00:00
|
|
|
},
|
|
|
|
"Widescreen");
|
2023-01-31 04:58:54 +00:00
|
|
|
|
|
|
|
// VertexManager doesn't maintain statistics in Wii mode.
|
2024-01-31 01:56:56 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
if (!system.IsWii())
|
2023-01-31 04:58:54 +00:00
|
|
|
{
|
2024-01-31 18:12:06 +00:00
|
|
|
m_update_widescreen = AfterFrameEvent::Register(
|
|
|
|
[this](Core::System&) { UpdateWidescreenHeuristic(); }, "WideScreen Heuristic");
|
2023-01-31 04:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 18:16:58 +00:00
|
|
|
std::optional<bool> WidescreenManager::GetWidescreenOverride() const
|
2023-01-31 04:58:54 +00:00
|
|
|
{
|
2024-02-20 00:42:52 +00:00
|
|
|
std::optional<bool> is_game_widescreen;
|
|
|
|
|
2024-01-31 01:56:56 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
if (system.IsWii())
|
2024-02-20 00:42:52 +00:00
|
|
|
is_game_widescreen = Config::Get(Config::SYSCONF_WIDESCREEN);
|
2023-01-31 04:58:54 +00:00
|
|
|
|
|
|
|
// suggested_aspect_mode overrides SYSCONF_WIDESCREEN
|
2023-12-12 22:27:01 +00:00
|
|
|
if (g_ActiveConfig.suggested_aspect_mode == AspectMode::ForceStandard)
|
2024-02-20 00:42:52 +00:00
|
|
|
is_game_widescreen = false;
|
2023-12-12 22:27:01 +00:00
|
|
|
else if (g_ActiveConfig.suggested_aspect_mode == AspectMode::ForceWide)
|
2024-02-20 00:42:52 +00:00
|
|
|
is_game_widescreen = true;
|
2023-01-31 04:58:54 +00:00
|
|
|
|
|
|
|
// If widescreen hack is disabled override game's AR if UI is set to 4:3 or 16:9.
|
|
|
|
if (!g_ActiveConfig.bWidescreenHack)
|
|
|
|
{
|
|
|
|
const auto aspect_mode = g_ActiveConfig.aspect_mode;
|
2023-12-12 22:27:01 +00:00
|
|
|
if (aspect_mode == AspectMode::ForceStandard)
|
2024-02-20 00:42:52 +00:00
|
|
|
is_game_widescreen = false;
|
2023-12-12 22:27:01 +00:00
|
|
|
else if (aspect_mode == AspectMode::ForceWide)
|
2024-02-20 00:42:52 +00:00
|
|
|
is_game_widescreen = true;
|
|
|
|
}
|
|
|
|
|
2023-12-18 18:16:58 +00:00
|
|
|
return is_game_widescreen;
|
2023-01-31 04:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Heuristic to detect if a GameCube game is in 16:9 anamorphic widescreen mode.
|
2023-09-09 12:48:37 +00:00
|
|
|
// Cheats that change the game aspect ratio to natively unsupported ones won't be recognized here.
|
2023-01-31 04:58:54 +00:00
|
|
|
void WidescreenManager::UpdateWidescreenHeuristic()
|
|
|
|
{
|
2023-12-18 18:16:58 +00:00
|
|
|
// Reset to baseline state before the update
|
2023-01-31 04:58:54 +00:00
|
|
|
const auto flush_statistics = g_vertex_manager->ResetFlushAspectRatioCount();
|
2023-12-18 18:16:58 +00:00
|
|
|
const bool was_orthographically_anamorphic = m_was_orthographically_anamorphic;
|
|
|
|
m_heuristic_state = HeuristicState::Inactive;
|
|
|
|
m_was_orthographically_anamorphic = false;
|
2023-01-31 04:58:54 +00:00
|
|
|
|
|
|
|
// If suggested_aspect_mode (GameINI) is configured don't use heuristic.
|
2023-12-18 18:16:58 +00:00
|
|
|
// We also don't need to check "GetWidescreenOverride()" in this case as
|
|
|
|
// nothing would have changed there.
|
2023-01-31 04:58:54 +00:00
|
|
|
if (g_ActiveConfig.suggested_aspect_mode != AspectMode::Auto)
|
|
|
|
return;
|
|
|
|
|
2023-12-18 18:16:58 +00:00
|
|
|
std::optional<bool> is_game_widescreen = GetWidescreenOverride();
|
2024-02-20 00:42:52 +00:00
|
|
|
|
|
|
|
// If widescreen hack isn't active and aspect_mode (UI) is 4:3 or 16:9 don't use heuristic.
|
2023-12-18 18:16:58 +00:00
|
|
|
if (g_ActiveConfig.bWidescreenHack || (g_ActiveConfig.aspect_mode != AspectMode::ForceStandard &&
|
|
|
|
g_ActiveConfig.aspect_mode != AspectMode::ForceWide))
|
|
|
|
{
|
|
|
|
// Modify the threshold based on which aspect ratio we're already using:
|
|
|
|
// If the game's in 4:3, it probably won't switch to anamorphic, and vice-versa.
|
|
|
|
const u32 transition_threshold = g_ActiveConfig.widescreen_heuristic_transition_threshold;
|
2023-01-31 04:58:54 +00:00
|
|
|
|
2023-12-18 18:16:58 +00:00
|
|
|
const auto looks_normal = [transition_threshold](auto& counts) {
|
|
|
|
return counts.normal_vertex_count > counts.anamorphic_vertex_count * transition_threshold;
|
|
|
|
};
|
|
|
|
const auto looks_anamorphic = [transition_threshold](auto& counts) {
|
|
|
|
return counts.anamorphic_vertex_count > counts.normal_vertex_count * transition_threshold;
|
|
|
|
};
|
2023-01-31 04:58:54 +00:00
|
|
|
|
2023-12-18 18:16:58 +00:00
|
|
|
const auto& persp = flush_statistics.perspective;
|
|
|
|
const auto& ortho = flush_statistics.orthographic;
|
2023-01-31 04:58:54 +00:00
|
|
|
|
2023-09-05 20:34:43 +00:00
|
|
|
g_stats.avg_persp_proj_viewport_ratio = persp.average_ratio.Mean();
|
|
|
|
g_stats.avg_ortho_proj_viewport_ratio = ortho.average_ratio.Mean();
|
|
|
|
|
2023-12-18 18:16:58 +00:00
|
|
|
const auto ortho_looks_anamorphic = looks_anamorphic(ortho);
|
|
|
|
const auto persp_looks_normal = looks_normal(persp);
|
2023-01-31 04:58:54 +00:00
|
|
|
|
2023-12-18 18:16:58 +00:00
|
|
|
if (looks_anamorphic(persp) || ortho_looks_anamorphic)
|
|
|
|
{
|
|
|
|
// If either perspective or orthographic projections look anamorphic, it's a safe bet.
|
|
|
|
is_game_widescreen = true;
|
|
|
|
m_heuristic_state = HeuristicState::Active_Found_Anamorphic;
|
|
|
|
}
|
|
|
|
else if (persp_looks_normal || looks_normal(ortho))
|
|
|
|
{
|
|
|
|
// Many widescreen games (or AR/GeckoCodes) use anamorphic perspective projections
|
|
|
|
// with NON-anamorphic orthographic projections.
|
|
|
|
// This can cause incorrect changes to 4:3 when perspective projections are temporarily not
|
|
|
|
// shown. e.g. Animal Crossing's inventory menu.
|
|
|
|
// Unless we were in a situation which was orthographically anamorphic
|
|
|
|
// we won't consider orthographic data for changes from 16:9 to 4:3.
|
|
|
|
if (persp_looks_normal || was_orthographically_anamorphic)
|
|
|
|
is_game_widescreen = false;
|
|
|
|
m_heuristic_state = HeuristicState::Active_Found_Normal;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_heuristic_state = HeuristicState::Active_NotFound;
|
|
|
|
}
|
2023-01-31 04:58:54 +00:00
|
|
|
|
2023-12-18 18:16:58 +00:00
|
|
|
m_was_orthographically_anamorphic = ortho_looks_anamorphic;
|
2023-01-31 04:58:54 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 18:16:58 +00:00
|
|
|
if (is_game_widescreen.has_value())
|
|
|
|
m_is_game_widescreen = is_game_widescreen.value();
|
2023-01-31 04:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WidescreenManager::DoState(PointerWrap& p)
|
|
|
|
{
|
|
|
|
p.Do(m_is_game_widescreen);
|
|
|
|
|
|
|
|
if (p.IsReadMode())
|
|
|
|
{
|
|
|
|
m_was_orthographically_anamorphic = false;
|
2023-12-18 18:16:58 +00:00
|
|
|
m_heuristic_state = HeuristicState::Inactive;
|
2023-01-31 04:58:54 +00:00
|
|
|
}
|
2023-01-31 06:21:15 +00:00
|
|
|
}
|