Debian Codegraph @

Source/Core/VideoCommon/Widescreen.cpp

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/Widescreen.h"
5
6 #include "Common/ChunkFile.h"
7 #include "Common/Logging/Log.h"
8 #include "Core/Config/SYSCONFSettings.h"
9 #include "Core/System.h"
10
11 #include "VideoCommon/Statistics.h"
12 #include "VideoCommon/VertexManagerBase.h"
13
14 std::unique_ptr<WidescreenManager> g_widescreen;
15
16 WidescreenManager::WidescreenManager()
17 {
18 if (std::optional<bool> is_game_widescreen = GetWidescreenOverride())
19 m_is_game_widescreen = *is_game_widescreen;
20
21 // Throw a warning as unsupported aspect ratio modes have no specific behavior to them
22 const bool is_valid_suggested_aspect_mode =
23 g_ActiveConfig.suggested_aspect_mode == AspectMode::Auto ||
24 g_ActiveConfig.suggested_aspect_mode == AspectMode::ForceStandard ||
25 g_ActiveConfig.suggested_aspect_mode == AspectMode::ForceWide;
26 if (!is_valid_suggested_aspect_mode)
27 {
28 WARN_LOG_FMT(VIDEO,
29 "Invalid suggested aspect ratio mode: only Auto, 4:3 and 16:9 are supported");
30 }
31
32 auto& system = Core::System::GetInstance();
33 auto& video_events = system.GetVideoEvents();
34
35 m_config_changed = video_events.config_changed_event.Register([this](u32 bits) {
36 if (bits & (CONFIG_CHANGE_BIT_ASPECT_RATIO))
37 {
38 // If the widescreen flag isn't being overridden by any settings,
39 // reset it to default if heuristic aren't running or to the last
40 // heuristic value if they were running.
41 if (std::optional<bool> is_game_widescreen = GetWidescreenOverride())
42 m_is_game_widescreen = *is_game_widescreen;
43 else
44 m_is_game_widescreen = (m_heuristic_state == HeuristicState::Active_Found_Anamorphic);
45 }
46 });
47
48 // VertexManager doesn't maintain statistics in Wii mode.
49 if (!system.IsWii())
50 {
51 m_update_widescreen = video_events.after_frame_event.Register(
52 [this](Core::System&) { UpdateWidescreenHeuristic(); });
53 }
54 }
55
56 std::optional<bool> WidescreenManager::GetWidescreenOverride() const
57 {
58 std::optional<bool> is_game_widescreen;
59
60 auto& system = Core::System::GetInstance();
61 if (system.IsWii())
62 is_game_widescreen = Config::Get(Config::SYSCONF_WIDESCREEN);
63
64 // suggested_aspect_mode overrides SYSCONF_WIDESCREEN
65 if (g_ActiveConfig.suggested_aspect_mode == AspectMode::ForceStandard)
66 is_game_widescreen = false;
67 else if (g_ActiveConfig.suggested_aspect_mode == AspectMode::ForceWide)
68 is_game_widescreen = true;
69
70 // If widescreen hack is disabled override game's AR if UI is set to 4:3 or 16:9.
71 if (!g_ActiveConfig.bWidescreenHack)
72 {
73 const auto aspect_mode = g_ActiveConfig.aspect_mode;
74 if (aspect_mode == AspectMode::ForceStandard)
75 is_game_widescreen = false;
76 else if (aspect_mode == AspectMode::ForceWide)
77 is_game_widescreen = true;
78 }
79
80 return is_game_widescreen;
81 }
82
83 // Heuristic to detect if a GameCube game is in 16:9 anamorphic widescreen mode.
84 // Cheats that change the game aspect ratio to natively unsupported ones won't be recognized here.
85 void WidescreenManager::UpdateWidescreenHeuristic()
86 {
87 // Reset to baseline state before the update
88 const auto flush_statistics = g_vertex_manager->ResetFlushAspectRatioCount();
89 const bool was_orthographically_anamorphic = m_was_orthographically_anamorphic;
90 m_heuristic_state = HeuristicState::Inactive;
91 m_was_orthographically_anamorphic = false;
92
93 // If suggested_aspect_mode (GameINI) is configured don't use heuristic.
94 // We also don't need to check "GetWidescreenOverride()" in this case as
95 // nothing would have changed there.
96 if (g_ActiveConfig.suggested_aspect_mode != AspectMode::Auto)
97 return;
98
99 std::optional<bool> is_game_widescreen = GetWidescreenOverride();
100
101 // If widescreen hack isn't active and aspect_mode (UI) is 4:3 or 16:9 don't use heuristic.
102 if (g_ActiveConfig.bWidescreenHack || (g_ActiveConfig.aspect_mode != AspectMode::ForceStandard &&
103 g_ActiveConfig.aspect_mode != AspectMode::ForceWide))
104 {
105 // Modify the threshold based on which aspect ratio we're already using:
106 // If the game's in 4:3, it probably won't switch to anamorphic, and vice-versa.
107 const u32 transition_threshold = g_ActiveConfig.widescreen_heuristic_transition_threshold;
108
109 const auto looks_normal = [transition_threshold](auto& counts) {
110 return counts.normal_vertex_count > counts.anamorphic_vertex_count * transition_threshold;
111 };
112 const auto looks_anamorphic = [transition_threshold](auto& counts) {
113 return counts.anamorphic_vertex_count > counts.normal_vertex_count * transition_threshold;
114 };
115
116 const auto& persp = flush_statistics.perspective;
117 const auto& ortho = flush_statistics.orthographic;
118
119 g_stats.avg_persp_proj_viewport_ratio = persp.average_ratio.Mean();
120 g_stats.avg_ortho_proj_viewport_ratio = ortho.average_ratio.Mean();
121
122 const auto ortho_looks_anamorphic = looks_anamorphic(ortho);
123 const auto persp_looks_normal = looks_normal(persp);
124
125 if (looks_anamorphic(persp) || ortho_looks_anamorphic)
126 {
127 // If either perspective or orthographic projections look anamorphic, it's a safe bet.
128 is_game_widescreen = true;
129 m_heuristic_state = HeuristicState::Active_Found_Anamorphic;
130 }
131 else if (persp_looks_normal || looks_normal(ortho))
132 {
133 // Many widescreen games (or AR/GeckoCodes) use anamorphic perspective projections
134 // with NON-anamorphic orthographic projections.
135 // This can cause incorrect changes to 4:3 when perspective projections are temporarily not
136 // shown. e.g. Animal Crossing's inventory menu.
137 // Unless we were in a situation which was orthographically anamorphic
138 // we won't consider orthographic data for changes from 16:9 to 4:3.
139 if (persp_looks_normal || was_orthographically_anamorphic)
140 is_game_widescreen = false;
141 m_heuristic_state = HeuristicState::Active_Found_Normal;
142 }
143 else
144 {
145 m_heuristic_state = HeuristicState::Active_NotFound;
146 }
147
148 m_was_orthographically_anamorphic = ortho_looks_anamorphic;
149 }
150
151 if (is_game_widescreen.has_value())
152 m_is_game_widescreen = is_game_widescreen.value();
153 }
154
155 void WidescreenManager::DoState(PointerWrap& p)
156 {
157 p.Do(m_is_game_widescreen);
158
159 if (p.IsReadMode())
160 {
161 m_was_orthographically_anamorphic = false;
162 m_heuristic_state = HeuristicState::Inactive;
163 }
164 }