BsVulkanHardwareBuffer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. VulkanBuffer(VulkanResourceManager* owner, VkBuffer buffer, VkBufferView view, VkDeviceMemory memory);
  17. ~VulkanBuffer();
  18. /** Returns the internal handle to the Vulkan object. */
  19. VkBuffer getHandle() const { return mBuffer; }
  20. /** Returns a buffer view that covers the entire buffer. */
  21. VkBufferView getView() const { return mView; }
  22. /**
  23. * Returns a pointer to internal buffer memory. Must be followed by unmap(). Caller must ensure the buffer was
  24. * created in CPU readable memory, and that buffer isn't currently being written to by the GPU.
  25. */
  26. UINT8* map(VkDeviceSize offset, VkDeviceSize length) const;
  27. /** Unmaps a buffer previously mapped with map(). */
  28. void unmap();
  29. /**
  30. * Queues a command on the provided command buffer. The command copies the contents of the current buffer to
  31. * the destination buffer. Caller must ensure the provided offsets and lengths are within valid bounds of
  32. * both buffers.
  33. */
  34. void copy(VulkanTransferBuffer* cb, VulkanBuffer* destination, VkDeviceSize srcOffset, VkDeviceSize dstOffset,
  35. VkDeviceSize length);
  36. private:
  37. VkBuffer mBuffer;
  38. VkBufferView mView;
  39. VkDeviceMemory mMemory;
  40. };
  41. /** Class containing common functionality for all Vulkan hardware buffers. */
  42. class VulkanHardwareBuffer : public HardwareBuffer
  43. {
  44. public:
  45. /** Available types of Vulkan buffers. */
  46. enum BufferType
  47. {
  48. /** Contains geometry vertices and their properties. */
  49. BT_VERTEX = 0x1,
  50. /** Contains triangle to vertex mapping. */
  51. BT_INDEX = 0x2,
  52. /** Contains GPU program parameters. */
  53. BT_UNIFORM = 0x4,
  54. /** Generic read-only GPU buffer containing formatted data. */
  55. BT_GENERIC = 0x8,
  56. /** Generic read/write GPU buffer containing formatted data. */
  57. BT_STORAGE = 0x10,
  58. };
  59. VulkanHardwareBuffer(BufferType type, GpuBufferFormat format, GpuBufferUsage usage, UINT32 size,
  60. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  61. ~VulkanHardwareBuffer();
  62. /** @copydoc HardwareBuffer::readData */
  63. void readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx = 0, UINT32 queueIdx = 0) override;
  64. /** @copydoc HardwareBuffer::writeData */
  65. void writeData(UINT32 offset, UINT32 length, const void* source,
  66. BufferWriteType writeFlags = BWT_NORMAL, UINT32 queueIdx = 0) override;
  67. /** @copydoc HardwareBuffer::copyData */
  68. void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset, UINT32 dstOffset,
  69. UINT32 length, bool discardWholeBuffer = false, UINT32 queueIdx = 0) override;
  70. /**
  71. * Gets the resource wrapping the buffer object, on the specified device. If hardware buffer device mask doesn't
  72. * include the provided device, null is returned.
  73. */
  74. VulkanBuffer* getResource(UINT32 deviceIdx) const { return mBuffers[deviceIdx]; }
  75. protected:
  76. /** @copydoc HardwareBuffer::map */
  77. void* map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx, UINT32 queueIdx) override;
  78. /** @copydoc HardwareBuffer::unmap */
  79. void unmap() override;
  80. /** Creates a new buffer for the specified device, matching the current buffer properties. */
  81. VulkanBuffer* createBuffer(VulkanDevice& device, bool staging, bool readable);
  82. VulkanBuffer* mBuffers[BS_MAX_DEVICES];
  83. VulkanBuffer* mStagingBuffer;
  84. UINT32 mMappedDeviceIdx;
  85. UINT32 mMappedGlobalQueueIdx;
  86. UINT32 mMappedOffset;
  87. UINT32 mMappedSize;
  88. GpuLockOptions mMappedLockOptions;
  89. VkBufferCreateInfo mBufferCI;
  90. VkBufferViewCreateInfo mViewCI;
  91. VkBufferUsageFlags mUsageFlags;
  92. bool mDirectlyMappable : 1;
  93. bool mSupportsGPUWrites : 1;
  94. bool mRequiresView : 1;
  95. bool mReadable : 1;
  96. bool mIsMapped : 1;
  97. };
  98. /** @} */
  99. }