Skip to content

Commit a628ea9

Browse files
committed
GPU frametime measurement
1 parent c848f9c commit a628ea9

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

src/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ VkDescriptorPool modelDescriptorPool;
4343
VkDescriptorSet modelDescriptorSets[FRAMES_IN_FLIGHT];
4444
VulkanBuffer modelUniformBuffers[FRAMES_IN_FLIGHT];
4545

46+
VkQueryPool timestampQueryPools[FRAMES_IN_FLIGHT];
47+
4648
struct Camera {
4749
glm::vec3 cameraPosition;
4850
glm::vec3 cameraDirection;
@@ -254,6 +256,12 @@ void initApplication(SDL_Window* window) {
254256
VK(vkUpdateDescriptorSets(context->device, ARRAY_COUNT(descriptorWrites), descriptorWrites, 0, 0));
255257
}
256258
}
259+
for(uint32_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
260+
VkQueryPoolCreateInfo createInfo = {VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO};
261+
createInfo.queryType = VK_QUERY_TYPE_TIMESTAMP;
262+
createInfo.queryCount = 64;
263+
VKA(vkCreateQueryPool(context->device, &createInfo, 0, &timestampQueryPools[i]));
264+
}
257265

258266
VkVertexInputAttributeDescription vertexAttributeDescriptions[3] = {};
259267
vertexAttributeDescriptions[0].binding = 0;
@@ -366,6 +374,7 @@ glm::mat4 getProjectionInverseZ(float fovy, float width, float height, float zNe
366374
void renderApplication() {
367375
static float greenChannel = 0.0f;
368376
static float time = 0.0f;
377+
static double frameGpuAvg = 0.0;
369378
time += 0.01f;
370379
greenChannel += 0.01f;
371380
if (greenChannel > 1.0f) greenChannel = 0.0f;
@@ -385,6 +394,16 @@ void renderApplication() {
385394
ASSERT_VULKAN(result);
386395
}
387396

397+
// Query timestamps
398+
uint64_t timestamps[2] = {};
399+
VkResult timestampsValid = VK(vkGetQueryPoolResults(context->device, timestampQueryPools[frameIndex], 0, ARRAY_COUNT(timestamps), sizeof(timestamps), timestamps, sizeof(timestamps[0]), VK_QUERY_RESULT_64_BIT));
400+
if(timestampsValid == VK_SUCCESS) {
401+
double frameGpuBegin = double(timestamps[0]) * context->physicalDeviceProperties.limits.timestampPeriod * 1e-6;
402+
double frameGpuEnd = double(timestamps[1]) * context->physicalDeviceProperties.limits.timestampPeriod * 1e-6;
403+
frameGpuAvg = frameGpuAvg * 0.95 + (frameGpuEnd - frameGpuBegin) * 0.05;
404+
LOG_INFO("GPU frametime: ", frameGpuAvg, "ms");
405+
}
406+
388407
VKA(vkResetCommandPool(context->device, commandPools[frameIndex], 0));
389408

390409
VkCommandBufferBeginInfo beginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
@@ -393,6 +412,9 @@ void renderApplication() {
393412
VkCommandBuffer commandBuffer = commandBuffers[frameIndex];
394413
VKA(vkBeginCommandBuffer(commandBuffer, &beginInfo));
395414

415+
VK(vkCmdResetQueryPool(commandBuffer, timestampQueryPools[frameIndex], 0, 64));
416+
VK(vkCmdWriteTimestamp(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, timestampQueryPools[frameIndex], 0));
417+
396418
VkViewport viewport = { 0.0f, 0.0f, (float)swapchain.width, (float)swapchain.height, 0.0f, 1.0f};
397419
VkRect2D scissor = { {0, 0}, {swapchain.width, swapchain.height} };
398420
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
@@ -440,6 +462,8 @@ void renderApplication() {
440462

441463
vkCmdEndRenderPass(commandBuffer);
442464

465+
VK(vkCmdWriteTimestamp(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, timestampQueryPools[frameIndex], 1));
466+
443467
VKA(vkEndCommandBuffer(commandBuffer));
444468
}
445469

@@ -481,6 +505,10 @@ void shutdownApplication() {
481505
destroyBuffer(context, &modelUniformBuffers[i]);
482506
}
483507

508+
for(uint32_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
509+
vkDestroyQueryPool(context->device, timestampQueryPools[i], 0);
510+
}
511+
484512
VK(vkDestroyDescriptorPool(context->device, spriteDescriptorPool, 0));
485513
VK(vkDestroyDescriptorSetLayout(context->device, spriteDescriptorLayout, 0));
486514
destroyBuffer(context, &spriteVertexBuffer);
@@ -574,6 +602,7 @@ int main() {
574602
initApplication(window);
575603

576604
float delta = 0.0f;
605+
double frameCpuAvg = 0.0f;
577606
uint64_t perfCounterFrequency = SDL_GetPerformanceFrequency();
578607
uint64_t lastCounter = SDL_GetPerformanceCounter();
579608
while (handleMessage()) {
@@ -582,6 +611,8 @@ int main() {
582611

583612
uint64_t endCounter = SDL_GetPerformanceCounter();
584613
uint64_t counterElapsed = endCounter - lastCounter;
614+
frameCpuAvg = frameCpuAvg * 0.95f + delta * 0.05f * 1000.0f;
615+
//LOG_INFO("CPU frametime: ", frameCpuAvg, "ms");
585616
delta = ((float)counterElapsed) / (float) perfCounterFrequency;
586617
lastCounter = endCounter;
587618
}

0 commit comments

Comments
 (0)