18constexpr float kGpuLogIntervalSeconds = 2.0f;
20constexpr std::array kGpuRowNames = {
"shadow",
"gbuffer",
"ssao",
"lighting",
21 "forward",
"taa",
"ssr",
"bloom",
22 "tonemap",
"ui",
"other",
"total"};
28bool VulkanRenderer::CreateGpuProfiler() {
29 static_assert(kGpuRowNames.size() == GPU_ROW_COUNT);
31 VkPhysicalDeviceProperties props;
32 vkGetPhysicalDeviceProperties(physicalDevice, &props);
34 uint32_t familyCount = 0;
35 vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &familyCount,
37 std::vector<VkQueueFamilyProperties> families(familyCount);
38 vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &familyCount,
41 const uint32_t validBits =
42 QueueIDs.GraphicsIndex < familyCount
43 ? families[QueueIDs.GraphicsIndex].timestampValidBits
45 if (validBits == 0 || props.limits.timestampPeriod <= 0.0f) {
47 "GPU profiler: graphics queue has no timestamp support, "
48 "--gpuprofile ignored");
52 VkQueryPoolCreateInfo info{};
53 info.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
54 info.queryType = VK_QUERY_TYPE_TIMESTAMP;
55 info.queryCount = MAX_FRAMES_IN_FLIGHT * GPU_QUERIES_PER_FRAME;
56 if (vkCreateQueryPool(device, &info,
nullptr, &m_gpuQueryPool) !=
58 SLEAK_WARN(
"GPU profiler: query pool creation failed");
59 m_gpuQueryPool = VK_NULL_HANDLE;
63 m_gpuTickNs =
static_cast<double>(props.limits.timestampPeriod);
64 m_gpuTickMask = validBits >= std::numeric_limits<uint64_t>::digits
66 : ((1ull << validBits) - 1);
72 m_gpuWindowFrames = 0;
73 m_gpuWindowTimer.Reset();
74 m_gpuProfilerEnabled =
true;
76 SLEAK_INFO(
"GPU profiler: on ({} timestamp bits, {:.3f} ns/tick)",
77 validBits, m_gpuTickNs);
82void VulkanRenderer::CleanupGpuProfiler() {
83 m_gpuProfilerEnabled =
false;
84 if (m_gpuQueryPool != VK_NULL_HANDLE) {
85 vkDestroyQueryPool(device, m_gpuQueryPool,
nullptr);
86 m_gpuQueryPool = VK_NULL_HANDLE;
92void VulkanRenderer::CollectGpuProfile() {
93 if (!m_gpuProfilerEnabled)
return;
94 GpuProfileSlot& slot = m_gpuSlots[currentFrame];
95 if (!slot.submitted)
return;
96 slot.submitted =
false;
99 const uint32_t queryCount = 2 + 2 * slot.pairCount;
100 std::array<uint64_t, GPU_QUERIES_PER_FRAME * 2> data{};
101 const VkResult res = vkGetQueryPoolResults(
102 device, m_gpuQueryPool, currentFrame * GPU_QUERIES_PER_FRAME,
103 queryCount, queryCount * 2 *
sizeof(uint64_t), data.data(),
104 2 *
sizeof(uint64_t),
105 VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WITH_AVAILABILITY_BIT);
106 if (res != VK_SUCCESS || data[1] == 0 || data[3] == 0)
return;
108 auto ticksToMs = [
this](uint64_t begin, uint64_t end) {
109 const uint64_t ticks = (end - begin) & m_gpuTickMask;
110 return static_cast<float>(
static_cast<double>(ticks) * m_gpuTickNs *
114 std::array<float, GPU_ROW_COUNT> frameMs{};
115 std::array<bool, GPU_ROW_COUNT> ran{};
116 float passSum = 0.0f;
117 for (uint32_t i = 0; i < slot.pairCount; ++i) {
118 const uint64_t* begin = &data[(2 + 2 * i) * 2];
119 const uint64_t* end = &data[(3 + 2 * i) * 2];
120 if (begin[1] == 0 || end[1] == 0)
continue;
121 const float ms = ticksToMs(begin[0], end[0]);
122 const auto row =
static_cast<uint32_t
>(slot.pairPass[i]);
127 frameMs[GPU_ROW_TOTAL] = ticksToMs(data[0], data[2]);
128 frameMs[GPU_ROW_OTHER] = std::max(0.0f, frameMs[GPU_ROW_TOTAL] - passSum);
129 ran[GPU_ROW_TOTAL] =
true;
130 ran[GPU_ROW_OTHER] =
true;
132 for (uint32_t row = 0; row < GPU_ROW_COUNT; ++row) {
133 m_gpuLastMs[row] = ran[row] ? frameMs[row] : 0.0f;
134 if (!ran[row])
continue;
135 m_gpuSumMs[row] += frameMs[row];
136 m_gpuMaxMs[row] = std::max(m_gpuMaxMs[row], frameMs[row]);
141 const float elapsed = m_gpuWindowTimer.Elapsed();
142 if (elapsed < kGpuLogIntervalSeconds)
return;
143 LogGpuProfile(elapsed);
147 m_gpuWindowFrames = 0;
148 m_gpuWindowTimer.Reset();
152void VulkanRenderer::BeginGpuFrame() {
153 if (!m_gpuProfilerEnabled)
return;
154 GpuProfileSlot& slot = m_gpuSlots[currentFrame];
156 slot.recording =
true;
157 const uint32_t base = currentFrame * GPU_QUERIES_PER_FRAME;
158 vkCmdResetQueryPool(command, m_gpuQueryPool, base, GPU_QUERIES_PER_FRAME);
159 vkCmdWriteTimestamp(command, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
160 m_gpuQueryPool, base);
164void VulkanRenderer::EndGpuFrame() {
165 if (!m_gpuProfilerEnabled)
return;
166 GpuProfileSlot& slot = m_gpuSlots[currentFrame];
167 if (!slot.recording)
return;
168 if (slot.pairOpen) EndGpuPass(slot.pairPass[slot.pairCount]);
169 vkCmdWriteTimestamp(command, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
171 currentFrame * GPU_QUERIES_PER_FRAME + 1);
172 slot.recording =
false;
176void VulkanRenderer::MarkGpuFrameSubmitted() {
177 if (!m_gpuProfilerEnabled)
return;
178 m_gpuSlots[currentFrame].submitted =
true;
183void VulkanRenderer::BeginGpuPass(GpuPass pass) {
184 if (!m_gpuProfilerEnabled)
return;
185 GpuProfileSlot& slot = m_gpuSlots[currentFrame];
186 if (!slot.recording || slot.pairOpen || slot.pairCount >= GPU_MAX_PAIRS)
189 command, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, m_gpuQueryPool,
190 currentFrame * GPU_QUERIES_PER_FRAME + 2 + 2 * slot.pairCount);
191 slot.pairPass[slot.pairCount] = pass;
192 slot.pairOpen =
true;
196void VulkanRenderer::EndGpuPass(GpuPass pass) {
197 if (!m_gpuProfilerEnabled)
return;
198 GpuProfileSlot& slot = m_gpuSlots[currentFrame];
199 if (!slot.recording || !slot.pairOpen ||
200 slot.pairPass[slot.pairCount] != pass)
203 command, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, m_gpuQueryPool,
204 currentFrame * GPU_QUERIES_PER_FRAME + 3 + 2 * slot.pairCount);
205 slot.pairOpen =
false;
210void VulkanRenderer::LogGpuProfile(
float seconds)
const {
211 SLEAK_INFO(
"GPU profile: {} frames in {:.1f} s, avg / max ms",
212 m_gpuWindowFrames, seconds);
213 for (uint32_t row = 0; row < GPU_ROW_COUNT; ++row) {
214 if (m_gpuRuns[row] == 0)
continue;
215 const double avg = m_gpuSumMs[row] / m_gpuRuns[row];
216 const std::string runs =
217 m_gpuRuns[row] < m_gpuWindowFrames
218 ? std::format(
" ({}/{} frames)", m_gpuRuns[row],
221 SLEAK_INFO(
" {:<9}{:8.3f}{:8.3f}{}", kGpuRowNames[row], avg,
222 m_gpuMaxMs[row], runs);
229 if (!m_gpuProfilerEnabled)
return;
230 for (uint32_t row = 0; row < GPU_ROW_COUNT; ++row) {
231 if (row == GPU_ROW_OTHER)
continue;
233 [
this, row] {
return m_gpuLastMs[row]; });
void RegisterMetric(const std::string &name, std::function< float()> getter)
Register a custom metric (e.g. render distance from Game).
void RegisterBenchmarkMetrics(Benchmark &bench) override
Adds per-pass GPU millisecond columns when –gpuprofile is on.
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.