BsVulkanEventQuery.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVulkanEventQuery.h"
  4. #include "BsVulkanDevice.h"
  5. #include "BsVulkanCommandBuffer.h"
  6. #include "Profiling/BsRenderStats.h"
  7. namespace bs { namespace ct
  8. {
  9. VulkanEvent::VulkanEvent(VulkanResourceManager* owner)
  10. :VulkanResource(owner, false)
  11. {
  12. VkDevice vkDevice = owner->getDevice().getLogical();
  13. VkEventCreateInfo eventCI;
  14. eventCI.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
  15. eventCI.pNext = nullptr;
  16. eventCI.flags = 0;
  17. VkResult result = vkCreateEvent(vkDevice, &eventCI, gVulkanAllocator, &mEvent);
  18. assert(result == VK_SUCCESS);
  19. }
  20. VulkanEvent::~VulkanEvent()
  21. {
  22. VkDevice vkDevice = mOwner->getDevice().getLogical();
  23. vkDestroyEvent(vkDevice, mEvent, gVulkanAllocator);
  24. }
  25. bool VulkanEvent::isSignaled() const
  26. {
  27. VkDevice vkDevice = mOwner->getDevice().getLogical();
  28. return vkGetEventStatus(vkDevice, mEvent) == VK_EVENT_SET;
  29. }
  30. void VulkanEvent::reset()
  31. {
  32. VkDevice vkDevice = mOwner->getDevice().getLogical();
  33. VkResult result = vkResetEvent(vkDevice, mEvent);
  34. assert(result == VK_SUCCESS);
  35. }
  36. VulkanEventQuery::VulkanEventQuery(VulkanDevice& device)
  37. :mDevice(device), mEvent(nullptr)
  38. {
  39. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Query);
  40. }
  41. VulkanEventQuery::~VulkanEventQuery()
  42. {
  43. if (mEvent != nullptr)
  44. mEvent->destroy();
  45. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Query);
  46. }
  47. void VulkanEventQuery::begin(const SPtr<CommandBuffer>& cb)
  48. {
  49. if (mEvent != nullptr)
  50. {
  51. if (mEvent->isBound())
  52. {
  53. // Clear current event and create a new one
  54. mEvent->destroy();
  55. mEvent = mDevice.getResourceManager().create<VulkanEvent>();
  56. }
  57. else
  58. {
  59. // Re-use existing event
  60. mEvent->reset();
  61. }
  62. }
  63. else // Create new event
  64. mEvent = mDevice.getResourceManager().create<VulkanEvent>();
  65. VulkanCommandBuffer* vulkanCB;
  66. if (cb != nullptr)
  67. vulkanCB = static_cast<VulkanCommandBuffer*>(cb.get());
  68. else
  69. vulkanCB = static_cast<VulkanCommandBuffer*>(gVulkanRenderAPI()._getMainCommandBuffer());
  70. VulkanCmdBuffer* internalCB = vulkanCB->getInternal();
  71. internalCB->registerResource(mEvent, VulkanUseFlag::Read);
  72. internalCB->setEvent(mEvent);
  73. setActive(true);
  74. }
  75. bool VulkanEventQuery::isReady() const
  76. {
  77. if (mEvent == nullptr)
  78. return false;
  79. return mEvent->isSignaled();
  80. }
  81. }}