BsVulkanHardwareBuffer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 BansheeEngine
  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 offset, VkDeviceSize length);
  35. private:
  36. VkBuffer mBuffer;
  37. VkBufferView mView;
  38. VkDeviceMemory mMemory;
  39. };
  40. /** Class containing common functionality for all Vulkan hardware buffers. */
  41. class VulkanHardwareBuffer : public HardwareBuffer
  42. {
  43. public:
  44. /** Available types of Vulkan buffers. */
  45. enum BufferType
  46. {
  47. /** Contains geometry vertices and their properties. */
  48. BT_VERTEX = 0x1,
  49. /** Contains triangle to vertex mapping. */
  50. BT_INDEX = 0x2,
  51. /** Contains GPU program parameters. */
  52. BT_UNIFORM = 0x4,
  53. /** Generic read-only GPU buffer containing formatted data. */
  54. BT_GENERIC = 0x8,
  55. /** Generic read/write GPU buffer containing formatted data. */
  56. BT_STORAGE = 0x10,
  57. };
  58. VulkanHardwareBuffer(BufferType type, GpuBufferFormat format, GpuBufferUsage usage, UINT32 size,
  59. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  60. ~VulkanHardwareBuffer();
  61. /** @copydoc HardwareBuffer::readData */
  62. void readData(UINT32 offset, UINT32 length, void* dest, UINT32 queueIdx = 0) override;
  63. /** @copydoc HardwareBuffer::writeData */
  64. void writeData(UINT32 offset, UINT32 length, const void* source,
  65. BufferWriteType writeFlags = BWT_NORMAL, UINT32 queueIdx = 0) override;
  66. /** @copydoc HardwareBuffer::copyData */
  67. void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset, UINT32 dstOffset,
  68. UINT32 length, bool discardWholeBuffer = false, UINT32 queueIdx = 0) override;
  69. /**
  70. * Gets the resource wrapping the buffer object, on the specified device. If hardware buffer device mask doesn't
  71. * include the provided device, null is returned.
  72. */
  73. VulkanBuffer* getResource(UINT32 deviceIdx) const { return mBuffers[deviceIdx]; }
  74. protected:
  75. /** @copydoc HardwareBuffer::map */
  76. void* map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx, UINT32 queueIdx) override;
  77. /** @copydoc HardwareBuffer::unmap */
  78. void unmap() override;
  79. /** Creates a new buffer for the specified device, matching the current buffer properties. */
  80. VulkanBuffer* createBuffer(VulkanDevice& device, bool staging, bool readable);
  81. VulkanBuffer* mBuffers[BS_MAX_DEVICES];
  82. VulkanBuffer* mStagingBuffer;
  83. UINT32 mMappedDeviceIdx;
  84. UINT32 mMappedGlobalQueueIdx;
  85. UINT32 mMappedOffset;
  86. UINT32 mMappedSize;
  87. GpuLockOptions mMappedLockOptions;
  88. VkBufferCreateInfo mBufferCI;
  89. VkBufferViewCreateInfo mViewCI;
  90. VkBufferUsageFlags mUsageFlags;
  91. bool mDirectlyMappable : 1;
  92. bool mSupportsGPUWrites : 1;
  93. bool mRequiresView : 1;
  94. bool mReadable : 1;
  95. bool mIsMapped : 1;
  96. };
  97. /** @} */
  98. }