BsVulkanTexture.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "BsTexture.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Vulkan
  10. * @{
  11. */
  12. /** Wrapper around a Vulkan image object that manages its usage and lifetime. */
  13. class VulkanImage : public VulkanResource
  14. {
  15. public:
  16. VulkanImage(VulkanResourceManager* owner, VkImage image, VkDeviceMemory memory, VkImageLayout layout,
  17. const TextureProperties& props);
  18. ~VulkanImage();
  19. /** Returns the internal handle to the Vulkan object. */
  20. VkImage getHandle() const { return mImage; }
  21. /** Returns the layout the image is currently in. */
  22. VkImageLayout getLayout() const { return mLayout; }
  23. /** Notifies the resource that the current image layout has changed. */
  24. void setLayout(VkImageLayout layout) { mLayout = layout; }
  25. /** Returns an image view that covers all faces and mip maps of the texture. */
  26. VkImageView getView() const { return mMainView; };
  27. /** Returns an image view that covers the specified faces and mip maps of the texture. */
  28. VkImageView getView(const TextureSurface& surface) const;
  29. /**
  30. * Returns a pointer to internal image memory for the specified sub-resource. Must be followed by unmap(). Caller
  31. * must ensure the image was created in CPU readable memory, and that image isn't currently being written to by the
  32. * GPU.
  33. *
  34. * @param[in] face Index of the face to map.
  35. * @param[in] mipLevel Index of the mip level to map.
  36. * @param[in] output Output object containing the pointer to the sub-resource data.
  37. */
  38. void map(UINT32 face, UINT32 mipLevel, PixelData& output) const;
  39. /** Unmaps a buffer previously mapped with map(). */
  40. void unmap();
  41. /**
  42. * Queues a command on the provided command buffer. The command copies the contents of the current image
  43. * subresource to the destination buffer.
  44. */
  45. void copy(VulkanTransferBuffer* cb, VulkanBuffer* destination, const VkExtent3D& extent,
  46. const VkImageSubresourceLayers& range, VkImageLayout layout);
  47. private:
  48. /** Creates a new view of the provided part (or entirety) of surface. */
  49. VkImageView createView(const TextureSurface& surface) const;
  50. /** Contains information about view for a specific surface(s) of this image. */
  51. struct ImageViewInfo
  52. {
  53. TextureSurface surface;
  54. VkImageView view;
  55. };
  56. VkImage mImage;
  57. VkDeviceMemory mMemory;
  58. VkImageLayout mLayout;
  59. VkImageView mMainView;
  60. mutable VkImageViewCreateInfo mImageViewCI;
  61. mutable Vector<ImageViewInfo> mImageInfos;
  62. };
  63. /** Vulkan implementation of a texture. */
  64. class VulkanTextureCore : public TextureCore
  65. {
  66. public:
  67. ~VulkanTextureCore();
  68. /**
  69. * Gets the resource wrapping the Vulkan image object, on the specified device. If texture device mask doesn't
  70. * include the provided device, null is returned.
  71. */
  72. VulkanImage* getResource(UINT32 deviceIdx) const { return mImages[deviceIdx]; }
  73. /**
  74. * Returns an image view that covers all faces and mip maps of the texture. Usable only on the specified device.
  75. * If texture device mask doesn't include the provided device, null is returned.
  76. */
  77. VkImageView getView(UINT32 deviceIdx) const;
  78. /**
  79. * Returns an image view that covers the specified faces and mip maps of the texture. Usable only on the specified
  80. * device. If texture device mask doesn't include the provided device, null is returned.
  81. */
  82. VkImageView getView(UINT32 deviceIdx, const TextureSurface& surface) const;
  83. protected:
  84. friend class VulkanTextureCoreManager;
  85. VulkanTextureCore(const TEXTURE_DESC& desc, const SPtr<PixelData>& initialData, GpuDeviceFlags deviceMask);
  86. /** @copydoc CoreObjectCore::initialize() */
  87. void initialize() override;
  88. /** @copydoc TextureCore::lockImpl */
  89. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  90. UINT32 queueIdx = 0) override;
  91. /** @copydoc TextureCore::unlockImpl */
  92. void unlockImpl() override;
  93. /** @copydoc TextureCore::copyImpl */
  94. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel,
  95. const SPtr<TextureCore>& target, UINT32 queueIdx = 0) override;
  96. /** @copydoc TextureCore::readData */
  97. void readDataImpl(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  98. UINT32 queueIdx = 0) override;
  99. /** @copydoc TextureCore::writeData */
  100. void writeDataImpl(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false,
  101. UINT32 queueIdx = 0) override;
  102. private:
  103. /** Creates a new image for the specified device, matching the current properties. */
  104. VulkanImage* createImage(VulkanDevice& device);
  105. /**
  106. * Creates a staging buffer that can be used for texture transfer operations.
  107. *
  108. * @param[in] device Device to create the buffer on.
  109. * @param[in] pixelData Object that describes the image sub-resource that will be in the buffer.
  110. * @param[in] needsRead True if we will be copying data from the buffer, false if just reading. True if both.
  111. * @return Newly allocated buffer.
  112. */
  113. VulkanBuffer* createStaging(VulkanDevice& device, const PixelData& pixelData, bool needsRead);
  114. VulkanImage* mImages[BS_MAX_DEVICES];
  115. GpuDeviceFlags mDeviceMask;
  116. VkAccessFlags mAccessFlags;
  117. VulkanBuffer* mStagingBuffer;
  118. UINT32 mMappedDeviceIdx;
  119. UINT32 mMappedGlobalQueueIdx;
  120. UINT32 mMappedMip;
  121. UINT32 mMappedFace;
  122. UINT32 mMappedRowPitch;
  123. UINT32 mMappedSlicePitch;
  124. GpuLockOptions mMappedLockOptions;
  125. VkImageCreateInfo mImageCI;
  126. bool mDirectlyMappable : 1;
  127. bool mSupportsGPUWrites : 1;
  128. bool mIsMapped : 1;
  129. };
  130. /** @} */
  131. }