VkTimestampQuery.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Gr/Vulkan/VkTimestampQuery.h>
  6. #include <AnKi/Gr/Vulkan/VkGrManager.h>
  7. namespace anki {
  8. TimestampQuery* TimestampQuery::newInstance()
  9. {
  10. TimestampQueryImpl* impl = anki::newInstance<TimestampQueryImpl>(GrMemoryPool::getSingleton(), "N/A");
  11. const Error err = impl->init();
  12. if(err)
  13. {
  14. deleteInstance(GrMemoryPool::getSingleton(), impl);
  15. impl = nullptr;
  16. }
  17. return impl;
  18. }
  19. TimestampQueryResult TimestampQuery::getResult(Second& timestamp) const
  20. {
  21. ANKI_VK_SELF_CONST(TimestampQueryImpl);
  22. ANKI_ASSERT(self.m_handle);
  23. timestamp = -1.0;
  24. VkResult res;
  25. U64 value;
  26. ANKI_VK_CHECKF(res = vkGetQueryPoolResults(getVkDevice(), self.m_handle.getQueryPool(), self.m_handle.getQueryIndex(), 1, sizeof(value), &value,
  27. sizeof(value), VK_QUERY_RESULT_64_BIT));
  28. TimestampQueryResult qout = TimestampQueryResult::kNotAvailable;
  29. if(res == VK_SUCCESS)
  30. {
  31. value *= self.m_timestampPeriod;
  32. timestamp = Second(value) / Second(1000000000);
  33. qout = TimestampQueryResult::kAvailable;
  34. }
  35. else if(res == VK_NOT_READY)
  36. {
  37. qout = TimestampQueryResult::kNotAvailable;
  38. }
  39. else
  40. {
  41. ANKI_ASSERT(0);
  42. }
  43. return qout;
  44. }
  45. TimestampQueryImpl::~TimestampQueryImpl()
  46. {
  47. if(m_handle)
  48. {
  49. TimestampQueryFactory::getSingleton().deleteQuery(m_handle);
  50. }
  51. }
  52. Error TimestampQueryImpl::init()
  53. {
  54. ANKI_CHECK(TimestampQueryFactory::getSingleton().newQuery(m_handle));
  55. m_timestampPeriod = U64(getGrManagerImpl().getVulkanCapabilities().m_timestampPeriod);
  56. return Error::kNone;
  57. }
  58. } // end namespace anki