BsVulkanHardwareBuffer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsVulkanPrerequisites.h"
  5. #include "BsVulkanResource.h"
  6. #include "BsHardwareBuffer.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Vulkan
  10. * @{
  11. */
  12. /** Wrapper around a Vulkan buffer object that manages its usage and lifetime. */
  13. class VulkanBuffer : public VulkanResource
  14. {
  15. public:
  16. /**
  17. * @param[in] owner Manager that takes care of tracking and releasing of this object.
  18. * @param[in] buffer Actual low-level Vulkan buffer handle.
  19. * @param[in] view Optional handle to the buffer view.
  20. * @param[in] memory Memory mapped to the buffer.
  21. * @param[in] rowPitch If buffer maps to an image sub-resource, length of a single row (in elements).
  22. * @param[in] slicePitch If buffer maps to an image sub-resource, size of a single 2D surface (in elements).
  23. */
  24. VulkanBuffer(VulkanResourceManager* owner, VkBuffer buffer, VkBufferView view, VkDeviceMemory memory,
  25. UINT32 rowPitch = 0, UINT32 slicePitch = 0);
  26. ~VulkanBuffer();
  27. /** Returns the internal handle to the Vulkan object. */
  28. VkBuffer getHandle() const { return mBuffer; }
  29. /** Returns a buffer view that covers the entire buffer. */
  30. VkBufferView getView() const { return mView; }
  31. /**
  32. * If buffer represents an image sub-resource, this is the number of elements that separate one row of the
  33. * sub-resource from another (if no padding, it is equal to image width).
  34. */
  35. UINT32 getRowPitch() const { return mRowPitch; }
  36. /**
  37. * If buffer represents an image sub-resource, this is the number of elements that separate one column of the
  38. * sub-resource from another (if no padding, it is equal to image height). Only relevant for 3D images.
  39. */
  40. UINT32 getSliceHeight() const { return mSliceHeight; }
  41. /**
  42. * Returns a pointer to internal buffer memory. Must be followed by unmap(). Caller must ensure the buffer was
  43. * created in CPU readable memory, and that buffer isn't currently being written to by the GPU.
  44. */
  45. UINT8* map(VkDeviceSize offset, VkDeviceSize length) const;
  46. /** Unmaps a buffer previously mapped with map(). */
  47. void unmap();
  48. /**
  49. * Queues a command on the provided command buffer. The command copies the contents of the current buffer to
  50. * the destination buffer. Caller must ensure the provided offsets and lengths are within valid bounds of
  51. * both buffers.
  52. */
  53. void copy(VulkanTransferBuffer* cb, VulkanBuffer* destination, VkDeviceSize srcOffset, VkDeviceSize dstOffset,
  54. VkDeviceSize length);
  55. /**
  56. * Queues a command on the provided command buffer. The command copies the contents of the current buffer to
  57. * the destination image subresource.
  58. */
  59. void copy(VulkanTransferBuffer* cb, VulkanImage* destination, const VkExtent3D& extent,
  60. const VkImageSubresourceLayers& range, VkImageLayout layout);
  61. private:
  62. VkBuffer mBuffer;
  63. VkBufferView mView;
  64. VkDeviceMemory mMemory;
  65. UINT32 mRowPitch;
  66. UINT32 mSliceHeight;
  67. };
  68. /** Class containing common functionality for all Vulkan hardware buffers. */
  69. class VulkanHardwareBuffer : public HardwareBuffer
  70. {
  71. public:
  72. /** Available types of Vulkan buffers. */
  73. enum BufferType
  74. {
  75. /** Contains geometry vertices and their properties. */
  76. BT_VERTEX = 0x1,
  77. /** Contains triangle to vertex mapping. */
  78. BT_INDEX = 0x2,
  79. /** Contains GPU program parameters. */
  80. BT_UNIFORM = 0x4,
  81. /** Generic read-only GPU buffer containing formatted data. */
  82. BT_GENERIC = 0x8,
  83. /** Generic read/write GPU buffer containing formatted data. */
  84. BT_STORAGE = 0x10,
  85. };
  86. VulkanHardwareBuffer(BufferType type, GpuBufferFormat format, GpuBufferUsage usage, UINT32 size,
  87. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  88. ~VulkanHardwareBuffer();
  89. /** @copydoc HardwareBuffer::readData */
  90. void readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx = 0, UINT32 queueIdx = 0) override;
  91. /** @copydoc HardwareBuffer::writeData */
  92. void writeData(UINT32 offset, UINT32 length, const void* source,
  93. BufferWriteType writeFlags = BWT_NORMAL, UINT32 queueIdx = 0) override;
  94. /** @copydoc HardwareBuffer::copyData */
  95. void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset, UINT32 dstOffset,
  96. UINT32 length, bool discardWholeBuffer = false, UINT32 queueIdx = 0) override;
  97. /**
  98. * Gets the resource wrapping the buffer object, on the specified device. If hardware buffer device mask doesn't
  99. * include the provided device, null is returned.
  100. */
  101. VulkanBuffer* getResource(UINT32 deviceIdx) const { return mBuffers[deviceIdx]; }
  102. protected:
  103. /** @copydoc HardwareBuffer::map */
  104. void* map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx, UINT32 queueIdx) override;
  105. /** @copydoc HardwareBuffer::unmap */
  106. void unmap() override;
  107. /** Creates a new buffer for the specified device, matching the current buffer properties. */
  108. VulkanBuffer* createBuffer(VulkanDevice& device, bool staging, bool readable);
  109. VulkanBuffer* mBuffers[BS_MAX_DEVICES];
  110. VulkanBuffer* mStagingBuffer;
  111. UINT32 mMappedDeviceIdx;
  112. UINT32 mMappedGlobalQueueIdx;
  113. UINT32 mMappedOffset;
  114. UINT32 mMappedSize;
  115. GpuLockOptions mMappedLockOptions;
  116. VkBufferCreateInfo mBufferCI;
  117. VkBufferViewCreateInfo mViewCI;
  118. VkBufferUsageFlags mUsageFlags;
  119. bool mDirectlyMappable : 1;
  120. bool mSupportsGPUWrites : 1;
  121. bool mRequiresView : 1;
  122. bool mReadable : 1;
  123. bool mIsMapped : 1;
  124. };
  125. /** @} */
  126. }