diff --git a/src/xrt/compositor/render/render_interface.h b/src/xrt/compositor/render/render_interface.h index b3c6562b7..c8c4fee0b 100644 --- a/src/xrt/compositor/render/render_interface.h +++ b/src/xrt/compositor/render/render_interface.h @@ -435,6 +435,19 @@ render_ensure_scratch_image(struct render_resources *r, VkExtent2D extent); bool render_resources_get_timestamps(struct render_resources *r, uint64_t *out_gpu_start_ns, uint64_t *out_gpu_end_ns); +/*! + * Returns the duration for the latest GPU work that was submitted using + * @ref render_gfx or @ref render_compute cmd buf builders. + * + * Behaviour for this function is undefined if the GPU has not completed before + * calling this function, so make sure to call vkQueueWaitIdle or wait on the + * fence that the work was submitted with have fully completed. + * + * @public @memberof render_resources + */ +bool +render_resources_get_duration(struct render_resources *r, uint64_t *out_gpu_duration_ns); + /* * diff --git a/src/xrt/compositor/render/render_resources.c b/src/xrt/compositor/render/render_resources.c index 63b074fad..2ddaf0313 100644 --- a/src/xrt/compositor/render/render_resources.c +++ b/src/xrt/compositor/render/render_resources.c @@ -1357,3 +1357,41 @@ render_resources_get_timestamps(struct render_resources *r, uint64_t *out_gpu_st return true; } + +bool +render_resources_get_duration(struct render_resources *r, uint64_t *out_gpu_duration_ns) +{ + struct vk_bundle *vk = r->vk; + VkResult ret = VK_SUCCESS; + + /* + * Query how long things took. + */ + + VkQueryResultFlags flags = VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT; + uint64_t timestamps[2] = {0}; + + ret = vk->vkGetQueryPoolResults( // + vk->device, // device + r->query_pool, // queryPool + 0, // firstQuery + 2, // queryCount + sizeof(uint64_t) * 2, // dataSize + timestamps, // pData + sizeof(uint64_t), // stride + flags); // flags + + if (ret != VK_SUCCESS) { + return false; + } + + + /* + * Convert from ticks to nanoseconds + */ + + double duration_ticks = (double)(timestamps[1] - timestamps[0]); + *out_gpu_duration_ns = (uint64_t)(duration_ticks * vk->features.timestamp_period); + + return true; +}