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