Debian Codegraph @

Source/Core/VideoBackends/D3D/D3DPerfQuery.cpp

raw symbols SCIP
1 // Copyright 2012 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/D3D/D3DPerfQuery.h"
5
6 #include "Common/CommonTypes.h"
7 #include "Common/Logging/Log.h"
8 #include "VideoBackends/D3D/D3DBase.h"
9 #include "VideoCommon/FramebufferManager.h"
10 #include "VideoCommon/VideoCommon.h"
11 #include "VideoCommon/VideoConfig.h"
12
13 namespace DX11
14 {
15 PerfQuery::PerfQuery() : m_query_read_pos()
16 {
17 for (ActiveQuery& entry : m_query_buffer)
18 {
19 D3D11_QUERY_DESC qdesc = CD3D11_QUERY_DESC(D3D11_QUERY_OCCLUSION, 0);
20 D3D::device->CreateQuery(&qdesc, &entry.query);
21 }
22
23 ResetQuery();
24 }
25
26 PerfQuery::~PerfQuery() = default;
27
28 void PerfQuery::EnableQuery(PerfQueryGroup group)
29 {
30 u32 query_count = m_query_count.load(std::memory_order_relaxed);
31
32 // Is this sane?
33 if (query_count > m_query_buffer.size() / 2)
34 {
35 WeakFlush();
36 query_count = m_query_count.load(std::memory_order_relaxed);
37 }
38
39 if (m_query_buffer.size() == query_count)
40 {
41 // TODO
42 FlushOne();
43 query_count = m_query_count.load(std::memory_order_relaxed);
44 ERROR_LOG_FMT(VIDEO, "Flushed query buffer early!");
45 }
46
47 // start query
48 if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP)
49 {
50 auto& entry = m_query_buffer[(m_query_read_pos + query_count) % m_query_buffer.size()];
51
52 D3D::context->Begin(entry.query.Get());
53 entry.query_group = group;
54
55 m_query_count.fetch_add(1, std::memory_order_relaxed);
56 }
57 }
58
59 void PerfQuery::DisableQuery(PerfQueryGroup group)
60 {
61 // stop query
62 if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP)
63 {
64 auto& entry = m_query_buffer[(m_query_read_pos + m_query_count.load(std::memory_order_relaxed) +
65 m_query_buffer.size() - 1) %
66 m_query_buffer.size()];
67 D3D::context->End(entry.query.Get());
68 }
69 }
70
71 void PerfQuery::ResetQuery()
72 {
73 m_query_count.store(0, std::memory_order_relaxed);
74 for (auto& result : m_results)
75 result.store(0, std::memory_order_relaxed);
76 }
77
78 u32 PerfQuery::GetQueryResult(PerfQueryType type)
79 {
80 u32 result = 0;
81
82 if (type == PQ_ZCOMP_INPUT_ZCOMPLOC || type == PQ_ZCOMP_OUTPUT_ZCOMPLOC)
83 {
84 result = m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed);
85 }
86 else if (type == PQ_ZCOMP_INPUT || type == PQ_ZCOMP_OUTPUT)
87 {
88 result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed);
89 }
90 else if (type == PQ_BLEND_INPUT)
91 {
92 result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed) +
93 m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed);
94 }
95 else if (type == PQ_EFB_COPY_CLOCKS)
96 {
97 result = m_results[PQG_EFB_COPY_CLOCKS].load(std::memory_order_relaxed);
98 }
99
100 return result / 4;
101 }
102
103 void PerfQuery::FlushOne()
104 {
105 auto& entry = m_query_buffer[m_query_read_pos];
106
107 UINT64 result = 0;
108 HRESULT hr = S_FALSE;
109 while (hr != S_OK)
110 {
111 // TODO: Might cause us to be stuck in an infinite loop!
112 hr = D3D::context->GetData(entry.query.Get(), &result, sizeof(result), 0);
113 }
114
115 // NOTE: Reported pixel metrics should be referenced to native resolution
116 // TODO: Dropping the lower 2 bits from this count should be closer to actual
117 // hardware behavior when drawing triangles.
118 u64 native_res_result = result * EFB_WIDTH / g_framebuffer_manager->GetEFBWidth() * EFB_HEIGHT /
119 g_framebuffer_manager->GetEFBHeight();
120 if (g_ActiveConfig.iMultisamples > 1)
121 native_res_result /= g_ActiveConfig.iMultisamples;
122 m_results[entry.query_group].fetch_add(static_cast<u32>(native_res_result),
123 std::memory_order_relaxed);
124
125 m_query_read_pos = (m_query_read_pos + 1) % m_query_buffer.size();
126 m_query_count.fetch_sub(1, std::memory_order_relaxed);
127 }
128
129 // TODO: could selectively flush things, but I don't think that will do much
130 void PerfQuery::FlushResults()
131 {
132 while (!IsFlushed())
133 FlushOne();
134 }
135
136 void PerfQuery::WeakFlush()
137 {
138 while (!IsFlushed())
139 {
140 auto& entry = m_query_buffer[m_query_read_pos];
141
142 UINT64 result = 0;
143 HRESULT hr = D3D::context->GetData(entry.query.Get(), &result, sizeof(result),
144 D3D11_ASYNC_GETDATA_DONOTFLUSH);
145
146 if (hr == S_OK)
147 {
148 // NOTE: Reported pixel metrics should be referenced to native resolution
149 const u64 native_res_result = result * EFB_WIDTH / g_framebuffer_manager->GetEFBWidth() *
150 EFB_HEIGHT / g_framebuffer_manager->GetEFBHeight();
151 m_results[entry.query_group].store(static_cast<u32>(native_res_result),
152 std::memory_order_relaxed);
153
154 m_query_read_pos = (m_query_read_pos + 1) % m_query_buffer.size();
155 m_query_count.fetch_sub(1, std::memory_order_relaxed);
156 }
157 else
158 {
159 break;
160 }
161 }
162 }
163
164 bool PerfQuery::IsFlushed() const
165 {
166 return m_query_count.load(std::memory_order_relaxed) == 0;
167 }
168
169 } // namespace DX11