Debian Codegraph @

Source/Core/VideoCommon/BoundingBox.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/BoundingBox.h"
5
6 #include <algorithm>
7
8 #include "Common/Assert.h"
9 #include "Common/ChunkFile.h"
10 #include "Common/CommonTypes.h"
11 #include "VideoCommon/PixelShaderManager.h"
12 #include "VideoCommon/VideoConfig.h"
13
14 #include <algorithm>
15
16 std::unique_ptr<BoundingBox> g_bounding_box;
17
18 void BoundingBox::Enable(PixelShaderManager& pixel_shader_manager)
19 {
20 m_is_active = true;
21 pixel_shader_manager.SetBoundingBoxActive(m_is_active);
22 }
23
24 void BoundingBox::Disable(PixelShaderManager& pixel_shader_manager)
25 {
26 m_is_active = false;
27 pixel_shader_manager.SetBoundingBoxActive(m_is_active);
28 }
29
30 void BoundingBox::Flush()
31 {
32 if (!g_ActiveConfig.bBBoxEnable || !g_backend_info.bSupportsBBox)
33 return;
34
35 m_is_valid = false;
36
37 if (std::ranges::none_of(m_dirty, std::identity{}))
38 return;
39
40 // TODO: Does this make any difference over just writing all the values?
41 // Games only ever seem to write all 4 values at once anyways.
42 for (u32 start = 0; start < NUM_BBOX_VALUES; ++start)
43 {
44 if (!m_dirty[start])
45 continue;
46
47 u32 end = start + 1;
48 while (end < NUM_BBOX_VALUES && m_dirty[end])
49 ++end;
50
51 for (u32 i = start; i < end; ++i)
52 m_dirty[i] = false;
53
54 Write(start, std::span(m_values.begin() + start, m_values.begin() + end));
55 }
56 }
57
58 void BoundingBox::Readback()
59 {
60 if (!g_backend_info.bSupportsBBox)
61 return;
62
63 auto read_values = Read(0, NUM_BBOX_VALUES);
64
65 // Preserve dirty values, that way we don't need to sync.
66 for (u32 i = 0; i < NUM_BBOX_VALUES; i++)
67 {
68 if (!m_dirty[i])
69 m_values[i] = read_values[i];
70 }
71
72 m_is_valid = true;
73 }
74
75 u16 BoundingBox::Get(u32 index)
76 {
77 ASSERT(index < NUM_BBOX_VALUES);
78
79 if (!g_ActiveConfig.bBBoxEnable || !g_backend_info.bSupportsBBox)
80 return m_bounding_box_fallback[index];
81
82 if (!m_is_valid)
83 Readback();
84
85 return static_cast<u16>(m_values[index]);
86 }
87
88 void BoundingBox::Set(u32 index, u16 value)
89 {
90 ASSERT(index < NUM_BBOX_VALUES);
91
92 if (!g_ActiveConfig.bBBoxEnable || !g_backend_info.bSupportsBBox)
93 {
94 m_bounding_box_fallback[index] = value;
95 return;
96 }
97
98 if (m_is_valid && m_values[index] == value)
99 return;
100
101 m_values[index] = value;
102 m_dirty[index] = true;
103 }
104
105 // FIXME: This may not work correctly if we're in the middle of a draw.
106 // We should probably ensure that state saves only happen on frame boundaries.
107 // Nonetheless, it has been designed to be as safe as possible.
108 void BoundingBox::DoState(PointerWrap& p)
109 {
110 p.DoArray(m_bounding_box_fallback);
111 p.Do(m_is_active);
112 p.DoArray(m_values);
113 p.DoArray(m_dirty);
114 p.Do(m_is_valid);
115
116 // We handle saving the backend values specially rather than using Readback() and Flush() so that
117 // we don't mess up the current cache state
118 std::vector<BBoxType> backend_values(NUM_BBOX_VALUES);
119 if (p.IsReadMode())
120 {
121 p.Do(backend_values);
122
123 if (g_backend_info.bSupportsBBox)
124 Write(0, backend_values);
125 }
126 else
127 {
128 if (g_backend_info.bSupportsBBox)
129 backend_values = Read(0, NUM_BBOX_VALUES);
130
131 p.Do(backend_values);
132 }
133 }