2015-05-23 22:32:32 -06:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-05-23 22:32:32 -06:00
|
|
|
|
2020-09-15 06:50:34 -06:00
|
|
|
#include "VideoBackends/D3D/D3DPerfQuery.h"
|
2019-06-01 05:55:09 -06:00
|
|
|
|
2016-01-17 14:54:31 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
|
|
|
#include "VideoCommon/RenderBase.h"
|
2019-07-16 18:18:48 -06:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2013-03-01 11:30:37 -07:00
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
|
|
|
PerfQuery::PerfQuery() : m_query_read_pos()
|
|
|
|
{
|
2014-02-14 23:12:13 -07:00
|
|
|
for (ActiveQuery& entry : m_query_buffer)
|
2013-03-01 11:30:37 -07:00
|
|
|
{
|
|
|
|
D3D11_QUERY_DESC qdesc = CD3D11_QUERY_DESC(D3D11_QUERY_OCCLUSION, 0);
|
2014-02-14 23:12:13 -07:00
|
|
|
D3D::device->CreateQuery(&qdesc, &entry.query);
|
2013-03-01 11:30:37 -07:00
|
|
|
}
|
2014-02-14 23:12:13 -07:00
|
|
|
|
2013-03-01 11:30:37 -07:00
|
|
|
ResetQuery();
|
|
|
|
}
|
|
|
|
|
2019-03-09 06:31:37 -07:00
|
|
|
PerfQuery::~PerfQuery() = default;
|
2013-03-01 11:30:37 -07:00
|
|
|
|
|
|
|
void PerfQuery::EnableQuery(PerfQueryGroup type)
|
|
|
|
{
|
2021-05-21 04:48:27 -06:00
|
|
|
u32 query_count = m_query_count.load(std::memory_order_relaxed);
|
2021-05-13 11:30:30 -06:00
|
|
|
|
2013-03-01 11:30:37 -07:00
|
|
|
// Is this sane?
|
2021-05-13 11:30:30 -06:00
|
|
|
if (query_count > m_query_buffer.size() / 2)
|
2021-05-21 04:48:27 -06:00
|
|
|
{
|
2013-03-01 11:30:37 -07:00
|
|
|
WeakFlush();
|
2021-05-21 04:48:27 -06:00
|
|
|
query_count = m_query_count.load(std::memory_order_relaxed);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-05-13 11:30:30 -06:00
|
|
|
if (m_query_buffer.size() == query_count)
|
2013-03-01 11:30:37 -07:00
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
FlushOne();
|
2021-05-21 04:48:27 -06:00
|
|
|
query_count = m_query_count.load(std::memory_order_relaxed);
|
2020-11-09 01:01:58 -07:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Flushed query buffer early!");
|
2013-03-01 11:30:37 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-01 11:30:37 -07:00
|
|
|
// start query
|
|
|
|
if (type == PQG_ZCOMP_ZCOMPLOC || type == PQG_ZCOMP)
|
|
|
|
{
|
2021-05-13 11:30:30 -06:00
|
|
|
auto& entry = m_query_buffer[(m_query_read_pos + query_count) % m_query_buffer.size()];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-03-09 06:31:37 -07:00
|
|
|
D3D::context->Begin(entry.query.Get());
|
2013-03-01 11:30:37 -07:00
|
|
|
entry.query_type = type;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-05-13 11:30:30 -06:00
|
|
|
m_query_count.fetch_add(1, std::memory_order_relaxed);
|
2013-03-01 11:30:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::DisableQuery(PerfQueryGroup type)
|
|
|
|
{
|
|
|
|
// stop query
|
|
|
|
if (type == PQG_ZCOMP_ZCOMPLOC || type == PQG_ZCOMP)
|
|
|
|
{
|
2021-05-13 11:30:30 -06:00
|
|
|
auto& entry = m_query_buffer[(m_query_read_pos + m_query_count.load(std::memory_order_relaxed) +
|
|
|
|
m_query_buffer.size() - 1) %
|
2014-02-14 23:12:13 -07:00
|
|
|
m_query_buffer.size()];
|
2019-03-09 06:31:37 -07:00
|
|
|
D3D::context->End(entry.query.Get());
|
2013-03-01 11:30:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::ResetQuery()
|
|
|
|
{
|
2021-05-13 11:30:30 -06:00
|
|
|
m_query_count.store(0, std::memory_order_relaxed);
|
|
|
|
for (size_t i = 0; i < m_results.size(); ++i)
|
|
|
|
m_results[i].store(0, std::memory_order_relaxed);
|
2013-03-01 11:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 PerfQuery::GetQueryResult(PerfQueryType type)
|
|
|
|
{
|
|
|
|
u32 result = 0;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-01 11:30:37 -07:00
|
|
|
if (type == PQ_ZCOMP_INPUT_ZCOMPLOC || type == PQ_ZCOMP_OUTPUT_ZCOMPLOC)
|
2021-05-13 11:30:30 -06:00
|
|
|
{
|
|
|
|
result = m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed);
|
|
|
|
}
|
2013-03-01 11:30:37 -07:00
|
|
|
else if (type == PQ_ZCOMP_INPUT || type == PQ_ZCOMP_OUTPUT)
|
2021-05-13 11:30:30 -06:00
|
|
|
{
|
|
|
|
result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed);
|
|
|
|
}
|
2013-03-01 11:30:37 -07:00
|
|
|
else if (type == PQ_BLEND_INPUT)
|
2021-05-13 11:30:30 -06:00
|
|
|
{
|
|
|
|
result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed) +
|
|
|
|
m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed);
|
|
|
|
}
|
2013-03-01 11:30:37 -07:00
|
|
|
else if (type == PQ_EFB_COPY_CLOCKS)
|
2021-05-13 11:30:30 -06:00
|
|
|
{
|
|
|
|
result = m_results[PQG_EFB_COPY_CLOCKS].load(std::memory_order_relaxed);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-06-12 23:41:29 -06:00
|
|
|
return result;
|
2013-03-01 11:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::FlushOne()
|
|
|
|
{
|
|
|
|
auto& entry = m_query_buffer[m_query_read_pos];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-01 11:30:37 -07:00
|
|
|
UINT64 result = 0;
|
|
|
|
HRESULT hr = S_FALSE;
|
|
|
|
while (hr != S_OK)
|
|
|
|
{
|
|
|
|
// TODO: Might cause us to be stuck in an infinite loop!
|
2019-03-09 06:31:37 -07:00
|
|
|
hr = D3D::context->GetData(entry.query.Get(), &result, sizeof(result), 0);
|
2013-03-01 11:30:37 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-01 15:12:41 -07:00
|
|
|
// NOTE: Reported pixel metrics should be referenced to native resolution
|
2016-06-12 23:41:29 -06:00
|
|
|
// TODO: Dropping the lower 2 bits from this count should be closer to actual
|
|
|
|
// hardware behavior when drawing triangles.
|
2021-05-13 11:30:30 -06:00
|
|
|
const u64 native_res_result = result * EFB_WIDTH / g_renderer->GetTargetWidth() * EFB_HEIGHT /
|
|
|
|
g_renderer->GetTargetHeight();
|
|
|
|
m_results[entry.query_type].fetch_add(static_cast<u32>(native_res_result),
|
|
|
|
std::memory_order_relaxed);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-02-14 23:12:13 -07:00
|
|
|
m_query_read_pos = (m_query_read_pos + 1) % m_query_buffer.size();
|
2021-05-13 11:30:30 -06:00
|
|
|
m_query_count.fetch_sub(1, std::memory_order_relaxed);
|
2013-03-01 11:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: could selectively flush things, but I don't think that will do much
|
|
|
|
void PerfQuery::FlushResults()
|
|
|
|
{
|
|
|
|
while (!IsFlushed())
|
|
|
|
FlushOne();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::WeakFlush()
|
|
|
|
{
|
|
|
|
while (!IsFlushed())
|
|
|
|
{
|
|
|
|
auto& entry = m_query_buffer[m_query_read_pos];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-01 11:30:37 -07:00
|
|
|
UINT64 result = 0;
|
2019-03-09 06:31:37 -07:00
|
|
|
HRESULT hr = D3D::context->GetData(entry.query.Get(), &result, sizeof(result),
|
|
|
|
D3D11_ASYNC_GETDATA_DONOTFLUSH);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-01 11:30:37 -07:00
|
|
|
if (hr == S_OK)
|
|
|
|
{
|
2013-03-01 15:57:56 -07:00
|
|
|
// NOTE: Reported pixel metrics should be referenced to native resolution
|
2021-05-13 11:30:30 -06:00
|
|
|
const u64 native_res_result = result * EFB_WIDTH / g_renderer->GetTargetWidth() * EFB_HEIGHT /
|
|
|
|
g_renderer->GetTargetHeight();
|
|
|
|
m_results[entry.query_type].store(static_cast<u32>(native_res_result),
|
|
|
|
std::memory_order_relaxed);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-02-14 23:12:13 -07:00
|
|
|
m_query_read_pos = (m_query_read_pos + 1) % m_query_buffer.size();
|
2021-05-13 11:30:30 -06:00
|
|
|
m_query_count.fetch_sub(1, std::memory_order_relaxed);
|
2013-03-01 11:30:37 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PerfQuery::IsFlushed() const
|
|
|
|
{
|
2021-05-13 11:30:30 -06:00
|
|
|
return m_query_count.load(std::memory_order_relaxed) == 0;
|
2013-03-01 11:30:37 -07:00
|
|
|
}
|
|
|
|
|
2019-03-09 06:31:37 -07:00
|
|
|
} // namespace DX11
|