2015-05-24 04:32:32 +00:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-09-15 12:50:34 +00:00
|
|
|
#include "VideoBackends/D3D/D3DPerfQuery.h"
|
2019-06-01 11:55:09 +00:00
|
|
|
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
|
|
|
#include "VideoCommon/RenderBase.h"
|
2019-07-17 00:18:48 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2013-03-01 18:30:37 +00:00
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
|
|
|
PerfQuery::PerfQuery() : m_query_read_pos()
|
|
|
|
{
|
2014-02-15 06:12:13 +00:00
|
|
|
for (ActiveQuery& entry : m_query_buffer)
|
2013-03-01 18:30:37 +00:00
|
|
|
{
|
|
|
|
D3D11_QUERY_DESC qdesc = CD3D11_QUERY_DESC(D3D11_QUERY_OCCLUSION, 0);
|
2014-02-15 06:12:13 +00:00
|
|
|
D3D::device->CreateQuery(&qdesc, &entry.query);
|
2013-03-01 18:30:37 +00:00
|
|
|
}
|
2014-02-15 06:12:13 +00:00
|
|
|
|
2013-03-01 18:30:37 +00:00
|
|
|
ResetQuery();
|
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:37 +00:00
|
|
|
PerfQuery::~PerfQuery() = default;
|
2013-03-01 18:30:37 +00:00
|
|
|
|
|
|
|
void PerfQuery::EnableQuery(PerfQueryGroup type)
|
|
|
|
{
|
2021-05-21 10:48:27 +00:00
|
|
|
u32 query_count = m_query_count.load(std::memory_order_relaxed);
|
2021-05-13 17:30:30 +00:00
|
|
|
|
2013-03-01 18:30:37 +00:00
|
|
|
// Is this sane?
|
2021-05-13 17:30:30 +00:00
|
|
|
if (query_count > m_query_buffer.size() / 2)
|
2021-05-21 10:48:27 +00:00
|
|
|
{
|
2013-03-01 18:30:37 +00:00
|
|
|
WeakFlush();
|
2021-05-21 10:48:27 +00:00
|
|
|
query_count = m_query_count.load(std::memory_order_relaxed);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-05-13 17:30:30 +00:00
|
|
|
if (m_query_buffer.size() == query_count)
|
2013-03-01 18:30:37 +00:00
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
FlushOne();
|
2021-05-21 10:48:27 +00:00
|
|
|
query_count = m_query_count.load(std::memory_order_relaxed);
|
2020-11-09 08:01:58 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Flushed query buffer early!");
|
2013-03-01 18:30:37 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-01 18:30:37 +00:00
|
|
|
// start query
|
|
|
|
if (type == PQG_ZCOMP_ZCOMPLOC || type == PQG_ZCOMP)
|
|
|
|
{
|
2021-05-13 17:30:30 +00:00
|
|
|
auto& entry = m_query_buffer[(m_query_read_pos + query_count) % m_query_buffer.size()];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-09 13:31:37 +00:00
|
|
|
D3D::context->Begin(entry.query.Get());
|
2013-03-01 18:30:37 +00:00
|
|
|
entry.query_type = type;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-05-13 17:30:30 +00:00
|
|
|
m_query_count.fetch_add(1, std::memory_order_relaxed);
|
2013-03-01 18:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::DisableQuery(PerfQueryGroup type)
|
|
|
|
{
|
|
|
|
// stop query
|
|
|
|
if (type == PQG_ZCOMP_ZCOMPLOC || type == PQG_ZCOMP)
|
|
|
|
{
|
2021-05-13 17:30:30 +00: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-15 06:12:13 +00:00
|
|
|
m_query_buffer.size()];
|
2019-03-09 13:31:37 +00:00
|
|
|
D3D::context->End(entry.query.Get());
|
2013-03-01 18:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::ResetQuery()
|
|
|
|
{
|
2021-05-13 17:30:30 +00: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 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 PerfQuery::GetQueryResult(PerfQueryType type)
|
|
|
|
{
|
|
|
|
u32 result = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-01 18:30:37 +00:00
|
|
|
if (type == PQ_ZCOMP_INPUT_ZCOMPLOC || type == PQ_ZCOMP_OUTPUT_ZCOMPLOC)
|
2021-05-13 17:30:30 +00:00
|
|
|
{
|
|
|
|
result = m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed);
|
|
|
|
}
|
2013-03-01 18:30:37 +00:00
|
|
|
else if (type == PQ_ZCOMP_INPUT || type == PQ_ZCOMP_OUTPUT)
|
2021-05-13 17:30:30 +00:00
|
|
|
{
|
|
|
|
result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed);
|
|
|
|
}
|
2013-03-01 18:30:37 +00:00
|
|
|
else if (type == PQ_BLEND_INPUT)
|
2021-05-13 17:30:30 +00:00
|
|
|
{
|
|
|
|
result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed) +
|
|
|
|
m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed);
|
|
|
|
}
|
2013-03-01 18:30:37 +00:00
|
|
|
else if (type == PQ_EFB_COPY_CLOCKS)
|
2021-05-13 17:30:30 +00:00
|
|
|
{
|
|
|
|
result = m_results[PQG_EFB_COPY_CLOCKS].load(std::memory_order_relaxed);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-06-13 05:41:29 +00:00
|
|
|
return result;
|
2013-03-01 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::FlushOne()
|
|
|
|
{
|
|
|
|
auto& entry = m_query_buffer[m_query_read_pos];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-01 18:30:37 +00: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 13:31:37 +00:00
|
|
|
hr = D3D::context->GetData(entry.query.Get(), &result, sizeof(result), 0);
|
2013-03-01 18:30:37 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-01 22:12:41 +00:00
|
|
|
// NOTE: Reported pixel metrics should be referenced to native resolution
|
2016-06-13 05:41:29 +00:00
|
|
|
// TODO: Dropping the lower 2 bits from this count should be closer to actual
|
|
|
|
// hardware behavior when drawing triangles.
|
2021-05-13 17:30:30 +00: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 08:43:46 +00:00
|
|
|
|
2014-02-15 06:12:13 +00:00
|
|
|
m_query_read_pos = (m_query_read_pos + 1) % m_query_buffer.size();
|
2021-05-13 17:30:30 +00:00
|
|
|
m_query_count.fetch_sub(1, std::memory_order_relaxed);
|
2013-03-01 18:30:37 +00: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 08:43:46 +00:00
|
|
|
|
2013-03-01 18:30:37 +00:00
|
|
|
UINT64 result = 0;
|
2019-03-09 13:31:37 +00:00
|
|
|
HRESULT hr = D3D::context->GetData(entry.query.Get(), &result, sizeof(result),
|
|
|
|
D3D11_ASYNC_GETDATA_DONOTFLUSH);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-01 18:30:37 +00:00
|
|
|
if (hr == S_OK)
|
|
|
|
{
|
2013-03-01 22:57:56 +00:00
|
|
|
// NOTE: Reported pixel metrics should be referenced to native resolution
|
2021-05-13 17:30:30 +00: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 08:43:46 +00:00
|
|
|
|
2014-02-15 06:12:13 +00:00
|
|
|
m_query_read_pos = (m_query_read_pos + 1) % m_query_buffer.size();
|
2021-05-13 17:30:30 +00:00
|
|
|
m_query_count.fetch_sub(1, std::memory_order_relaxed);
|
2013-03-01 18:30:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PerfQuery::IsFlushed() const
|
|
|
|
{
|
2021-05-13 17:30:30 +00:00
|
|
|
return m_query_count.load(std::memory_order_relaxed) == 0;
|
2013-03-01 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:37 +00:00
|
|
|
} // namespace DX11
|