BsVulkanHardwareBuffer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "RenderAPI/BsHardwareBuffer.h"
  7. namespace bs { namespace ct
  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] allocation Information about 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, VmaAllocation allocation,
  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 length are within valid bounds of
  51. * both buffers.
  52. */
  53. void copy(VulkanCmdBuffer* 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(VulkanCmdBuffer* cb, VulkanImage* destination, const VkExtent3D& extent,
  60. const VkImageSubresourceLayers& range, VkImageLayout layout);
  61. /**
  62. * Queues a command on the provided command buffer. The command copies the contents of the provided memory location
  63. * the destination buffer. Caller must ensure the provided offset and length are within valid bounds of
  64. * both buffers. Caller must ensure the offset and size is a multiple of 4, and size is equal to or less then 65536.
  65. */
  66. void update(VulkanCmdBuffer* cb, UINT8* data, VkDeviceSize offset, VkDeviceSize length);
  67. private:
  68. VkBuffer mBuffer;
  69. VkBufferView mView;
  70. VmaAllocation mAllocation;
  71. UINT32 mRowPitch;
  72. UINT32 mSliceHeight;
  73. };
  74. /** Class containing common functionality for all Vulkan hardware buffers. */
  75. class VulkanHardwareBuffer : public HardwareBuffer
  76. {
  77. public:
  78. /** Available types of Vulkan buffers. */
  79. enum BufferType
  80. {
  81. /** Contains geometry vertices and their properties. */
  82. BT_VERTEX,
  83. /** Contains triangle to vertex mapping. */
  84. BT_INDEX,
  85. /** Contains GPU program parameters. */
  86. BT_UNIFORM,
  87. /** Generic read-only GPU buffer containing non-formatted data. */
  88. BT_GENERIC,
  89. /** Generic read/write GPU buffer containing non-formatted data. */
  90. BT_STORAGE,
  91. /** Read/write GPU buffer containing structured data. */
  92. BT_STRUCTURED
  93. };
  94. VulkanHardwareBuffer(BufferType type, GpuBufferFormat format, GpuBufferUsage usage, UINT32 size,
  95. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  96. ~VulkanHardwareBuffer();
  97. /** @copydoc HardwareBuffer::readData */
  98. void readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx = 0, UINT32 queueIdx = 0) override;
  99. /** @copydoc HardwareBuffer::writeData */
  100. void writeData(UINT32 offset, UINT32 length, const void* source,
  101. BufferWriteType writeFlags = BWT_NORMAL, UINT32 queueIdx = 0) override;
  102. /** @copydoc HardwareBuffer::copyData */
  103. void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset, UINT32 dstOffset,
  104. UINT32 length, bool discardWholeBuffer = false, const SPtr<CommandBuffer>& commandBuffer = nullptr) override;
  105. /**
  106. * Gets the resource wrapping the buffer object, on the specified device. If hardware buffer device mask doesn't
  107. * include the provided device, null is returned.
  108. */
  109. VulkanBuffer* getResource(UINT32 deviceIdx) const { return mBuffers[deviceIdx]; }
  110. protected:
  111. /** @copydoc HardwareBuffer::map */
  112. void* map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx, UINT32 queueIdx) override;
  113. /** @copydoc HardwareBuffer::unmap */
  114. void unmap() override;
  115. /** Creates a new buffer for the specified device, matching the current buffer properties. */
  116. VulkanBuffer* createBuffer(VulkanDevice& device, UINT32 size, bool staging, bool readable);
  117. VulkanBuffer* mBuffers[BS_MAX_DEVICES];
  118. VulkanBuffer* mStagingBuffer;
  119. UINT8* mStagingMemory;
  120. UINT32 mMappedDeviceIdx;
  121. UINT32 mMappedGlobalQueueIdx;
  122. UINT32 mMappedOffset;
  123. UINT32 mMappedSize;
  124. GpuLockOptions mMappedLockOptions;
  125. VkBufferCreateInfo mBufferCI;
  126. VkBufferViewCreateInfo mViewCI;
  127. VkBufferUsageFlags mUsageFlags;
  128. bool mDirectlyMappable : 1;
  129. bool mSupportsGPUWrites : 1;
  130. bool mRequiresView : 1;
  131. bool mIsMapped : 1;
  132. };
  133. /** @} */
  134. }}