BsVulkanGpuBuffer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVulkanGpuBuffer.h"
  4. #include "BsVulkanHardwareBuffer.h"
  5. #include "Profiling/BsRenderStats.h"
  6. #include "Error/BsException.h"
  7. namespace bs { namespace ct
  8. {
  9. VulkanGpuBuffer::VulkanGpuBuffer(const GPU_BUFFER_DESC& desc, GpuDeviceFlags deviceMask)
  10. : GpuBuffer(desc, deviceMask), mBuffer(nullptr), mDeviceMask(deviceMask)
  11. {
  12. if (desc.type != GBT_STANDARD)
  13. assert(desc.format == BF_UNKNOWN && "Format must be set to BF_UNKNOWN when using non-standard buffers");
  14. else
  15. assert(desc.elementSize == 0 && "No element size can be provided for standard buffer. Size is determined from format.");
  16. }
  17. VulkanGpuBuffer::~VulkanGpuBuffer()
  18. {
  19. if (mBuffer != nullptr)
  20. bs_delete(mBuffer);
  21. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_GpuBuffer);
  22. }
  23. void VulkanGpuBuffer::initialize()
  24. {
  25. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuBuffer);
  26. const GpuBufferProperties& props = getProperties();
  27. VulkanHardwareBuffer::BufferType bufferType;
  28. if (props.getType() == GBT_STRUCTURED)
  29. bufferType = VulkanHardwareBuffer::BT_STRUCTURED;
  30. else
  31. {
  32. if (props.getRandomGpuWrite())
  33. bufferType = VulkanHardwareBuffer::BT_STORAGE;
  34. else
  35. bufferType = VulkanHardwareBuffer::BT_GENERIC;
  36. }
  37. UINT32 size = props.getElementCount() * props.getElementSize();;
  38. mBuffer = bs_new<VulkanHardwareBuffer>(bufferType, props.getFormat(), props.getUsage(), size, mDeviceMask);
  39. GpuBuffer::initialize();
  40. }
  41. void* VulkanGpuBuffer::lock(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx, UINT32 queueIdx)
  42. {
  43. #if BS_PROFILING_ENABLED
  44. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  45. {
  46. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_GpuBuffer);
  47. }
  48. if (options == GBL_READ_WRITE || options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_DISCARD || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  49. {
  50. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_GpuBuffer);
  51. }
  52. #endif
  53. return mBuffer->lock(offset, length, options, deviceIdx, queueIdx);
  54. }
  55. void VulkanGpuBuffer::unlock()
  56. {
  57. mBuffer->unlock();
  58. }
  59. void VulkanGpuBuffer::readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx, UINT32 queueIdx)
  60. {
  61. mBuffer->readData(offset, length, dest, deviceIdx, queueIdx);
  62. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_GpuBuffer);
  63. }
  64. void VulkanGpuBuffer::writeData(UINT32 offset, UINT32 length, const void* source, BufferWriteType writeFlags,
  65. UINT32 queueIdx)
  66. {
  67. mBuffer->writeData(offset, length, source, writeFlags, queueIdx);
  68. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_GpuBuffer);
  69. }
  70. void VulkanGpuBuffer::copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset, UINT32 dstOffset, UINT32 length,
  71. bool discardWholeBuffer, const SPtr<CommandBuffer>& commandBuffer)
  72. {
  73. VulkanGpuBuffer& vkSrcBuffer = static_cast<VulkanGpuBuffer&>(srcBuffer);
  74. mBuffer->copyData(*vkSrcBuffer.mBuffer, srcOffset, dstOffset, length, discardWholeBuffer, commandBuffer);
  75. }
  76. VulkanBuffer* VulkanGpuBuffer::getResource(UINT32 deviceIdx) const
  77. {
  78. return mBuffer->getResource(deviceIdx);
  79. }
  80. }}