Debian Codegraph @

Source/Core/VideoCommon/BPFunctions.h

raw symbols SCIP
1 // Copyright 2009 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 // ------------------------------------------
5 // Video backend must define these functions
6 // ------------------------------------------
7
8 #pragma once
9
10 #include <utility>
11 #include <vector>
12
13 #include "Common/MathUtil.h"
14 #include "VideoCommon/BPMemory.h"
15
16 class FramebufferManager;
17 struct Viewport;
18
19 namespace BPFunctions
20 {
21 struct ScissorRange
22 {
23 constexpr ScissorRange() = default;
24 constexpr ScissorRange(int offset_, int start_, int end_)
25 : offset(offset_), start(start_), end(end_)
26 {
27 }
28 int offset = 0;
29 int start = 0;
30 int end = 0;
31 };
32
33 struct ScissorRect
34 {
35 constexpr ScissorRect(ScissorRange x_range, ScissorRange y_range)
36 : // Rectangle ctor takes x0, y0, x1, y1.
37 rect(x_range.start, y_range.start, x_range.end, y_range.end), x_off(x_range.offset),
38 y_off(y_range.offset)
39 {
40 }
41
42 MathUtil::Rectangle<int> rect;
43 int x_off;
44 int y_off;
45
46 int GetArea() const;
47 };
48
49 // Although the GameCube/Wii have only one scissor configuration and only one viewport
50 // configuration, some values can result in multiple parts of the screen being updated.
51 // This can happen if the scissor offset combined with the bottom or right coordinate ends up
52 // exceeding 1024; then, both sides of the screen will be drawn to, while the middle is not.
53 // Major Minor's Majestic March causes this to happen during loading screens and other scrolling
54 // effects, though it draws on top of one of them.
55 // This can also happen if the scissor rectangle is particularly large, but this will usually
56 // involve drawing content outside of the viewport, which Dolphin does not currently handle.
57 //
58 // The hardware backends can currently only use one viewport and scissor rectangle, so we need to
59 // pick the "best" rectangle based on how much of the viewport would be rendered to the screen.
60 // If we choose the wrong one, then content might not actually show up when the game is expecting it
61 // to. This does happen on Major Minor's Majestic March for the final few frames of the horizontal
62 // scrolling animation, but it isn't that important. Note that the assumption that a "best"
63 // rectangle exists is based on games only wanting to draw one rectangle, and accidentally
64 // configuring the scissor offset and size of the scissor rectangle such that multiple show up;
65 // there are no known games where this is not the case.
66 //
67 // An ImGui overlay that displays the scissor rectangle configuration as well as the generated
68 // rectangles is available by setting OverlayScissorStats (GFX_OVERLAY_SCISSOR_STATS)
69 // under [Settings] to True in GFX.ini.
70 struct ScissorResult
71 {
72 ScissorResult(ScissorPos scissor_top_left, ScissorPos scissor_bottom_right,
73 ScissorOffset scissor_offset, const Viewport& viewport);
74 ~ScissorResult() = default;
75 ScissorResult(const ScissorResult& other)
76 : scissor_tl{.hex = other.scissor_tl.hex}, scissor_br{.hex = other.scissor_br.hex},
77 scissor_off{.hex = other.scissor_off.hex}, viewport_left{other.viewport_left},
78 viewport_right{other.viewport_right}, viewport_top{other.viewport_top},
79 viewport_bottom{other.viewport_bottom}, rectangles{other.rectangles}
80 {
81 }
82 ScissorResult& operator=(const ScissorResult& other)
83 {
84 if (this == &other)
85 return *this;
86 scissor_tl.hex = other.scissor_tl.hex;
87 scissor_br.hex = other.scissor_br.hex;
88 scissor_off.hex = other.scissor_off.hex;
89 viewport_left = other.viewport_left;
90 viewport_right = other.viewport_right;
91 viewport_top = other.viewport_top;
92 viewport_bottom = other.viewport_bottom;
93 rectangles = other.rectangles;
94 return *this;
95 }
96 ScissorResult(ScissorResult&& other)
97 : scissor_tl{.hex = other.scissor_tl.hex}, scissor_br{.hex = other.scissor_br.hex},
98 scissor_off{.hex = other.scissor_off.hex}, viewport_left{other.viewport_left},
99 viewport_right{other.viewport_right}, viewport_top{other.viewport_top},
100 viewport_bottom{other.viewport_bottom}, rectangles{std::move(other.rectangles)}
101 {
102 }
103 ScissorResult& operator=(ScissorResult&& other)
104 {
105 if (this == &other)
106 return *this;
107 scissor_tl.hex = other.scissor_tl.hex;
108 scissor_br.hex = other.scissor_br.hex;
109 scissor_off.hex = other.scissor_off.hex;
110 viewport_left = other.viewport_left;
111 viewport_right = other.viewport_right;
112 viewport_top = other.viewport_top;
113 viewport_bottom = other.viewport_bottom;
114 rectangles = std::move(other.rectangles);
115 return *this;
116 }
117
118 // Input values, for use in statistics
119 ScissorPos scissor_tl;
120 ScissorPos scissor_br;
121 ScissorOffset scissor_off;
122 float viewport_left;
123 float viewport_right;
124 float viewport_top;
125 float viewport_bottom;
126
127 std::vector<ScissorRect> rectangles;
128
129 ScissorRect Best() const;
130
131 bool ScissorMatches(const ScissorResult& other) const
132 {
133 return scissor_tl.hex == other.scissor_tl.hex && scissor_br.hex == other.scissor_br.hex &&
134 scissor_off.hex == other.scissor_off.hex;
135 }
136 bool ViewportMatches(const ScissorResult& other) const
137 {
138 return viewport_left == other.viewport_left && viewport_right == other.viewport_right &&
139 viewport_top == other.viewport_top && viewport_bottom == other.viewport_bottom;
140 }
141 bool Matches(const ScissorResult& other, bool compare_scissor, bool compare_viewport) const
142 {
143 if (compare_scissor && !ScissorMatches(other))
144 return false;
145 if (compare_viewport && !ViewportMatches(other))
146 return false;
147 return true;
148 }
149
150 private:
151 ScissorResult(ScissorPos scissor_top_left, ScissorPos scissor_bottom_right,
152 ScissorOffset scissor_offset, std::pair<float, float> viewport_x,
153 std::pair<float, float> viewport_y);
154
155 int GetViewportArea(const ScissorRect& rect) const;
156 bool IsWorse(const ScissorRect& lhs, const ScissorRect& rhs) const;
157 };
158
159 ScissorResult ComputeScissorRects(ScissorPos scissor_top_left, ScissorPos scissor_bottom_right,
160 ScissorOffset scissor_offset, const Viewport& viewport);
161
162 void FlushPipeline();
163 void SetGenerationMode();
164 void SetScissorAndViewport(FramebufferManager* frame_buffer_manager, ScissorPos scissor_top_left,
165 ScissorPos scissor_bottom_right, ScissorOffset scissor_offset,
166 Viewport viewport);
167 void SetDepthMode();
168 void SetBlendMode();
169
170 // Returns true if the EFB was triggered to clear
171 bool ClearScreen(FramebufferManager* frame_buffer_manager, const MathUtil::Rectangle<int>& rc,
172 bool color_enable, bool alpha_enable, bool z_enable, PixelFormat pixel_format,
173 u32 clear_color_ar, u32 clear_color_gb, u32 clear_z_value);
174
175 void OnPixelFormatChange(FramebufferManager* frame_buffer_manager, PixelFormat pixel_format,
176 DepthFormat z_format);
177 void SetInterlacingMode(const BPCmd& bp);
178 } // namespace BPFunctions