BsVulkanTexture.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. class VulkanImageSubresource;
  13. /** Descriptor used for initializing a VulkanImage. */
  14. struct VULKAN_IMAGE_DESC
  15. {
  16. VkImage image; /**< Internal Vulkan image object */
  17. VkDeviceMemory memory; /**< Memory bound to the image. */
  18. VkImageLayout layout; /**< Initial layout of the image. */
  19. TextureType type; /**< Type of the image. */
  20. VkFormat format; /**< Pixel format of the image. */
  21. UINT32 numFaces; /**< Number of faces (array slices, or cube-map faces). */
  22. UINT32 numMipLevels; /**< Number of mipmap levels per face. */
  23. bool isDepthStencil; /**< True if the image represents a depth-stencil surface. */
  24. bool isStorage; /**< True if the texture supports shader random access reads and writes. */
  25. };
  26. /** Wrapper around a Vulkan image object that manages its usage and lifetime. */
  27. class VulkanImage : public VulkanResource
  28. {
  29. public:
  30. /**
  31. * @param[in] owner Resource manager that keeps track of lifetime of this resource.
  32. * @param[in] image Internal image Vulkan object.
  33. * @param[in] memory Memory bound to the image.
  34. * @param[in] layout Initial layout of the image.
  35. * @param[in] props Properties describing the image.
  36. * @param[in] ownsImage If true, this object will take care of releasing the image and its memory, otherwise
  37. * it is expected they will be released externally.
  38. */
  39. VulkanImage(VulkanResourceManager* owner, VkImage image, VkDeviceMemory memory, VkImageLayout layout,
  40. const TextureProperties& props, bool ownsImage = true);
  41. /**
  42. * @param[in] owner Resource manager that keeps track of lifetime of this resource.
  43. * @param[in] desc Describes the image to assign.
  44. * @param[in] ownsImage If true, this object will take care of releasing the image and its memory, otherwise
  45. * it is expected they will be released externally.
  46. */
  47. VulkanImage(VulkanResourceManager* owner, const VULKAN_IMAGE_DESC& desc, bool ownsImage = true);
  48. ~VulkanImage();
  49. /** Returns the internal handle to the Vulkan object. */
  50. VkImage getHandle() const { return mImage; }
  51. /** Returns the layout the image is currently in. */
  52. VkImageLayout getLayout() const { return mLayout; }
  53. /** Notifies the resource that the current image layout has changed. */
  54. void setLayout(VkImageLayout layout) { mLayout = layout; }
  55. /** Returns an image view that covers all faces and mip maps of the texture. */
  56. VkImageView getView() const { return mMainView; };
  57. /** Returns an image view that covers the specified faces and mip maps of the texture. */
  58. VkImageView getView(const TextureSurface& surface) const;
  59. /** Retrieves a subresource range covering all the sub-resources of the image. */
  60. VkImageSubresourceRange getRange() const;
  61. /**
  62. * Retrieves a separate resource for a specific image face & mip level. This allows the caller to track subresource
  63. * usage individually, instead for the entire image.
  64. */
  65. VulkanImageSubresource* getSubresource(UINT32 face, UINT32 mipLevel);
  66. /**
  67. * Returns a pointer to internal image memory for the specified sub-resource. Must be followed by unmap(). Caller
  68. * must ensure the image was created in CPU readable memory, and that image isn't currently being written to by the
  69. * GPU.
  70. *
  71. * @param[in] face Index of the face to map.
  72. * @param[in] mipLevel Index of the mip level to map.
  73. * @param[in] output Output object containing the pointer to the sub-resource data.
  74. */
  75. void map(UINT32 face, UINT32 mipLevel, PixelData& output) const;
  76. /**
  77. * Returns a pointer to internal image memory for the entire resource. Must be followed by unmap(). Caller
  78. * must ensure the image was created in CPU readable memory, and that image isn't currently being written to by the
  79. * GPU.
  80. */
  81. UINT8* map(UINT32 offset, UINT32 size) const;
  82. /** Unmaps a buffer previously mapped with map(). */
  83. void unmap();
  84. /**
  85. * Queues a command on the provided command buffer. The command copies the contents of the current image
  86. * subresource to the destination buffer.
  87. */
  88. void copy(VulkanTransferBuffer* cb, VulkanBuffer* destination, const VkExtent3D& extent,
  89. const VkImageSubresourceLayers& range, VkImageLayout layout);
  90. /**
  91. * Determines a set of access flags based on the current image and provided image layout. This method makes
  92. * certain assumptions about image usage, so it might not be valid in all situations.
  93. */
  94. VkAccessFlags getAccessFlags(VkImageLayout layout);
  95. private:
  96. /** Creates a new view of the provided part (or entirety) of surface. */
  97. VkImageView createView(const TextureSurface& surface) const;
  98. /** Contains information about view for a specific surface(s) of this image. */
  99. struct ImageViewInfo
  100. {
  101. TextureSurface surface;
  102. VkImageView view;
  103. };
  104. VkImage mImage;
  105. VkDeviceMemory mMemory;
  106. VkImageLayout mLayout;
  107. VkImageView mMainView;
  108. bool mOwnsImage;
  109. bool mIsStorage;
  110. UINT32 mNumFaces;
  111. UINT32 mNumMipLevels;
  112. VulkanImageSubresource** mSubresources;
  113. mutable VkImageViewCreateInfo mImageViewCI;
  114. mutable Vector<ImageViewInfo> mImageInfos;
  115. };
  116. /** Represents a single sub-resource (face & mip level) of a larger image object. */
  117. class VulkanImageSubresource : public VulkanResource
  118. {
  119. public:
  120. VulkanImageSubresource(VulkanResourceManager* owner);
  121. };
  122. /** Vulkan implementation of a texture. */
  123. class VulkanTextureCore : public TextureCore
  124. {
  125. public:
  126. ~VulkanTextureCore();
  127. /**
  128. * Gets the resource wrapping the Vulkan image object, on the specified device. If texture device mask doesn't
  129. * include the provided device, null is returned.
  130. */
  131. VulkanImage* getResource(UINT32 deviceIdx) const { return mImages[deviceIdx]; }
  132. /**
  133. * Returns an image view that covers all faces and mip maps of the texture. Usable only on the specified device.
  134. * If texture device mask doesn't include the provided device, null is returned.
  135. */
  136. VkImageView getView(UINT32 deviceIdx) const;
  137. /**
  138. * Returns an image view that covers the specified faces and mip maps of the texture. Usable only on the specified
  139. * device. If texture device mask doesn't include the provided device, null is returned.
  140. */
  141. VkImageView getView(UINT32 deviceIdx, const TextureSurface& surface) const;
  142. protected:
  143. friend class VulkanTextureCoreManager;
  144. VulkanTextureCore(const TEXTURE_DESC& desc, const SPtr<PixelData>& initialData, GpuDeviceFlags deviceMask);
  145. /** @copydoc CoreObjectCore::initialize() */
  146. void initialize() override;
  147. /** @copydoc TextureCore::lockImpl */
  148. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  149. UINT32 queueIdx = 0) override;
  150. /** @copydoc TextureCore::unlockImpl */
  151. void unlockImpl() override;
  152. /** @copydoc TextureCore::copyImpl */
  153. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel,
  154. const SPtr<TextureCore>& target, UINT32 queueIdx = 0) override;
  155. /** @copydoc TextureCore::readData */
  156. void readDataImpl(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  157. UINT32 queueIdx = 0) override;
  158. /** @copydoc TextureCore::writeData */
  159. void writeDataImpl(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false,
  160. UINT32 queueIdx = 0) override;
  161. private:
  162. /** Creates a new image for the specified device, matching the current properties. */
  163. VulkanImage* createImage(VulkanDevice& device);
  164. /**
  165. * Creates a staging buffer that can be used for texture transfer operations.
  166. *
  167. * @param[in] device Device to create the buffer on.
  168. * @param[in] pixelData Object that describes the image sub-resource that will be in the buffer.
  169. * @param[in] needsRead True if we will be copying data from the buffer, false if just reading. True if both.
  170. * @return Newly allocated buffer.
  171. */
  172. VulkanBuffer* createStaging(VulkanDevice& device, const PixelData& pixelData, bool needsRead);
  173. /**
  174. * Copies all sub-resources from the source image to the destination image. Caller must ensure the images
  175. * are of the same size. The operation will be queued on the provided command buffer. The system assumes the
  176. * provided image matches the current texture properties (i.e. num faces, mips, size).
  177. */
  178. void copyImage(VulkanTransferBuffer* cb, VulkanImage* srcImage, VulkanImage* dstImage,
  179. VkImageLayout srcFinalLayout, VkImageLayout dstFinalLayout);
  180. /** Returns the optimal layout this image should normally be in. */
  181. VkImageLayout getOptimalLayout() const;
  182. VulkanImage* mImages[BS_MAX_DEVICES];
  183. GpuDeviceFlags mDeviceMask;
  184. VulkanBuffer* mStagingBuffer;
  185. UINT32 mMappedDeviceIdx;
  186. UINT32 mMappedGlobalQueueIdx;
  187. UINT32 mMappedMip;
  188. UINT32 mMappedFace;
  189. UINT32 mMappedRowPitch;
  190. UINT32 mMappedSlicePitch;
  191. GpuLockOptions mMappedLockOptions;
  192. VkImageCreateInfo mImageCI;
  193. bool mDirectlyMappable : 1;
  194. bool mSupportsGPUWrites : 1;
  195. bool mIsMapped : 1;
  196. };
  197. /** @} */
  198. }