mirror of
https://github.com/KytyPS5/KytyPS5.git
synced 2026-10-26 12:56:54 +01:00
graphics: count occlusion queries on the host GPU behind --occlusion-queries
This commit is contained in:
@@ -36,6 +36,8 @@ struct GraphicContext {
|
||||
bool provoking_vertex_last_enabled = false;
|
||||
bool maintenance8_enabled = false;
|
||||
bool conditional_rendering_enabled = false;
|
||||
bool occlusion_queries_enabled = false;
|
||||
bool occlusion_query_precise_enabled = false;
|
||||
bool supports_block_texel_view = false;
|
||||
bool mesh_shader_enabled = false;
|
||||
vk::PhysicalDeviceMeshShaderPropertiesEXT mesh_shader_properties = {};
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
#include "graphics/host_gpu/renderer/occlusionQueries.h"
|
||||
|
||||
#include "graphics/host_gpu/graphicContext.h"
|
||||
#include "graphics/host_gpu/renderer/commandScheduler.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace Libs::Graphics {
|
||||
|
||||
// Queries no dump has read yet; past them a draw counts one sample.
|
||||
constexpr uint32_t QUERY_COUNT = 16384;
|
||||
|
||||
OcclusionQueries::OcclusionQueries(GraphicContext& graphics, CommandScheduler& scheduler)
|
||||
: m_scheduler(scheduler) {
|
||||
if (!graphics.occlusion_queries_enabled) {
|
||||
return;
|
||||
}
|
||||
m_device = graphics.device;
|
||||
const vk::QueryPoolCreateInfo info {.queryType = vk::QueryType::eOcclusion,
|
||||
.queryCount = QUERY_COUNT};
|
||||
RequireVulkanSuccess(m_device.createQueryPool(&info, nullptr, &m_pool), "vkCreateQueryPool");
|
||||
m_device.resetQueryPool(m_pool, 0, QUERY_COUNT);
|
||||
m_results.resize(QUERY_COUNT);
|
||||
if (graphics.occlusion_query_precise_enabled) {
|
||||
m_control = vk::QueryControlFlagBits::ePrecise;
|
||||
}
|
||||
}
|
||||
|
||||
OcclusionQueries::~OcclusionQueries() {
|
||||
if (m_pool) {
|
||||
m_device.destroyQueryPool(m_pool, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t OcclusionQueries::BeginQuery(vk::CommandBuffer command) {
|
||||
if (m_begun - m_read == QUERY_COUNT) {
|
||||
m_uncounted++;
|
||||
return -1;
|
||||
}
|
||||
const auto query = static_cast<uint32_t>(m_begun++ % QUERY_COUNT);
|
||||
command.beginQuery(m_pool, query, m_control);
|
||||
return static_cast<int32_t>(query);
|
||||
}
|
||||
|
||||
void OcclusionQueries::Dump(uint64_t address) {
|
||||
// Begins dump to the results, ends 8 bytes into them.
|
||||
if ((address & 8u) == 0) {
|
||||
m_open++;
|
||||
} else if (m_open != 0) {
|
||||
m_open--;
|
||||
}
|
||||
m_dumps.push_back({address, m_scheduler.CurrentTick(), m_begun, m_uncounted});
|
||||
}
|
||||
|
||||
void OcclusionQueries::PublishDumps() {
|
||||
if (const auto last = m_dumps.back().queries; last != m_read) {
|
||||
m_scheduler.Wait(m_dumps.back().tick);
|
||||
// The unread queries wrap around the pool at most once.
|
||||
for (uint64_t read = m_read; read < last;) {
|
||||
const auto first = static_cast<uint32_t>(read % QUERY_COUNT);
|
||||
const auto count =
|
||||
static_cast<uint32_t>(std::min<uint64_t>(last - read, QUERY_COUNT - first));
|
||||
RequireVulkanSuccess(
|
||||
m_device.getQueryPoolResults(m_pool, first, count, count * sizeof(uint64_t),
|
||||
m_results.data() + first, sizeof(uint64_t),
|
||||
vk::QueryResultFlagBits::e64),
|
||||
"vkGetQueryPoolResults");
|
||||
m_device.resetQueryPool(m_pool, first, count);
|
||||
read += count;
|
||||
}
|
||||
}
|
||||
for (const auto& dump: m_dumps) {
|
||||
for (; m_read < dump.queries; m_read++) {
|
||||
m_samples += m_results[m_read % QUERY_COUNT];
|
||||
}
|
||||
WriteZPassCounters(reinterpret_cast<volatile uint64_t*>(dump.address),
|
||||
m_samples + dump.uncounted, (dump.address & 8u) == 0);
|
||||
}
|
||||
m_dumps.clear();
|
||||
}
|
||||
|
||||
} // namespace Libs::Graphics
|
||||
@@ -0,0 +1,85 @@
|
||||
#ifndef EMULATOR_SRC_GRAPHICS_HOST_GPU_RENDERER_OCCLUSIONQUERIES_H_
|
||||
#define EMULATOR_SRC_GRAPHICS_HOST_GPU_RENDERER_OCCLUSIONQUERIES_H_
|
||||
|
||||
#include "common/common.h"
|
||||
#include "graphics/host_gpu/vulkanCommon.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace Libs::Graphics {
|
||||
|
||||
class CommandScheduler;
|
||||
struct GraphicContext;
|
||||
|
||||
// The guest's query results (16-byte aligned): a begin/end pair of running z-pass counters per DB,
|
||||
// 16 bytes apart; bit 63 marks a written counter. A query's samples are the sum of end - begin.
|
||||
constexpr uint32_t OCCLUSION_QUERY_DBS = 16;
|
||||
constexpr uint64_t OCCLUSION_QUERY_READY = 1ull << 63u;
|
||||
|
||||
// A dump of the running count, all of it in DB 0. A begin also clears its end counters.
|
||||
inline void WriteZPassCounters(volatile uint64_t* results, uint64_t samples, bool begin) {
|
||||
for (uint32_t db = 0; db < OCCLUSION_QUERY_DBS; db++) {
|
||||
results[db * 2] = OCCLUSION_QUERY_READY | (db == 0 ? samples : 0);
|
||||
if (begin) {
|
||||
results[db * 2 + 1] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The guest's z-pass counters from host occlusion queries: a draw counts its samples while a guest
|
||||
// query is open, and the dumps' counters are written before the guest can see the GPU ran them.
|
||||
class OcclusionQueries {
|
||||
public:
|
||||
OcclusionQueries(GraphicContext& graphics, CommandScheduler& scheduler);
|
||||
~OcclusionQueries();
|
||||
KYTY_CLASS_NO_COPY(OcclusionQueries);
|
||||
|
||||
[[nodiscard]] bool Enabled() const { return m_pool != nullptr; }
|
||||
|
||||
// In the draw's rendering: the query that counts its samples, or -1.
|
||||
[[nodiscard]] int32_t Begin(vk::CommandBuffer command) {
|
||||
return m_open == 0 ? -1 : BeginQuery(command);
|
||||
}
|
||||
void End(vk::CommandBuffer command, int32_t query) const {
|
||||
if (query >= 0) {
|
||||
command.endQuery(m_pool, static_cast<uint32_t>(query));
|
||||
}
|
||||
}
|
||||
|
||||
// A PIXEL_PIPE_STAT_DUMP to address, after the draws recorded so far.
|
||||
void Dump(uint64_t address);
|
||||
// Waits for the GPU to run the dumps and writes their counters.
|
||||
void Publish() {
|
||||
if (!m_dumps.empty()) {
|
||||
PublishDumps();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
struct PendingDump {
|
||||
uint64_t address;
|
||||
uint64_t tick;
|
||||
uint64_t queries;
|
||||
uint64_t uncounted;
|
||||
};
|
||||
|
||||
int32_t BeginQuery(vk::CommandBuffer command);
|
||||
void PublishDumps();
|
||||
|
||||
CommandScheduler& m_scheduler;
|
||||
vk::Device m_device;
|
||||
vk::QueryPool m_pool;
|
||||
vk::QueryControlFlags m_control;
|
||||
std::vector<PendingDump> m_dumps;
|
||||
std::vector<uint64_t> m_results;
|
||||
uint32_t m_open = 0;
|
||||
uint64_t m_begun = 0;
|
||||
uint64_t m_read = 0;
|
||||
uint64_t m_samples = 0;
|
||||
uint64_t m_uncounted = 0;
|
||||
};
|
||||
|
||||
} // namespace Libs::Graphics
|
||||
|
||||
#endif // EMULATOR_SRC_GRAPHICS_HOST_GPU_RENDERER_OCCLUSIONQUERIES_H_
|
||||
@@ -16,7 +16,7 @@ RenderContext::RenderContext(GraphicContext& graphics)
|
||||
m_pipeline_cache(graphics, &m_image_heap), m_sampler_cache(graphics),
|
||||
m_buffer_cache(graphics, m_command_scheduler, m_page_manager, m_texture_cache),
|
||||
m_texture_cache(graphics, m_command_scheduler, m_page_manager, m_buffer_cache),
|
||||
m_image_heap(*this, m_page_manager) {
|
||||
m_image_heap(*this, m_page_manager), m_occlusion_queries(graphics, m_command_scheduler) {
|
||||
EXIT_NOT_IMPLEMENTED(!Common::Thread::IsMainThread());
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "graphics/host_gpu/renderer/cache/samplerCache.h"
|
||||
#include "graphics/host_gpu/renderer/cache/textureCache.h"
|
||||
#include "graphics/host_gpu/renderer/commandScheduler.h"
|
||||
#include "graphics/host_gpu/renderer/occlusionQueries.h"
|
||||
#include "graphics/host_gpu/renderer/pipeline/descriptorHeap.h"
|
||||
#include "graphics/host_gpu/renderer/pipeline/imageHeap.h"
|
||||
#include "graphics/host_gpu/renderer/pipeline/pipelineCache.h"
|
||||
@@ -49,6 +50,7 @@ public:
|
||||
BufferCache& GetBufferCache() { return m_buffer_cache; }
|
||||
TextureCache& GetTextureCache() { return m_texture_cache; }
|
||||
RenderExecutor& GetRenderExecutor() { return m_render_executor; }
|
||||
OcclusionQueries& GetOcclusionQueries() { return m_occlusion_queries; }
|
||||
|
||||
[[nodiscard]] bool HandleFault(PageFaultAccess access, uint64_t fault_vaddr) noexcept;
|
||||
[[nodiscard]] bool InvalidateMemory(uint64_t vaddr, uint64_t size);
|
||||
@@ -79,6 +81,7 @@ private:
|
||||
BufferCache m_buffer_cache;
|
||||
TextureCache m_texture_cache;
|
||||
ImageHeap m_image_heap;
|
||||
OcclusionQueries m_occlusion_queries;
|
||||
mutable std::shared_mutex m_mapped_ranges_mutex;
|
||||
RangeSet m_mapped_ranges;
|
||||
std::unique_ptr<GuestGpu> m_gpu;
|
||||
|
||||
@@ -1284,6 +1284,8 @@ void RenderExecutor::ExecutePreparedDraw(uint64_t submit_id, CommandBuffer& buff
|
||||
if (!draw.IsIndexed()) {
|
||||
SetDrawDebugPhase(buffer, submit_id, draw, 0x500u);
|
||||
}
|
||||
auto& occlusion_queries = m_context.GetOcclusionQueries();
|
||||
const int32_t occlusion_query = occlusion_queries.Begin(vk_buffer);
|
||||
if (mesh_active) {
|
||||
vk_buffer.drawMeshTasksEXT(mesh_groups, draw.instance_count, 1);
|
||||
} else if (args_buffer.first == nullptr) {
|
||||
@@ -1295,6 +1297,7 @@ void RenderExecutor::ExecutePreparedDraw(uint64_t submit_id, CommandBuffer& buff
|
||||
vk_buffer.drawIndirect(args_buffer.first->Handle(), args_buffer.second,
|
||||
emit.indirect.count, emit.indirect.stride);
|
||||
}
|
||||
occlusion_queries.End(vk_buffer, occlusion_query);
|
||||
Common::DebugCounters::Add(Common::DebugCounters::Counter::Draws);
|
||||
Common::DebugCounters::Add(Common::DebugCounters::Counter::DrawInstances, draw.instance_count);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user