BsVulkanTexture.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /**
  56. * Returns an image view that covers all faces and mip maps of the texture.
  57. *
  58. * @param[in] framebuffer Set to true if the view will be used as a framebuffer attachment. Ensures proper
  59. * attachment flags are set on the view.
  60. */
  61. VkImageView getView(bool framebuffer) const;
  62. /**
  63. * Returns an image view that covers the specified faces and mip maps of the texture.
  64. *
  65. * @param[in] surface Surface that describes which faces and mip levels to retrieve the view for.
  66. * @param[in] framebuffer Set to true if the view will be used as a framebuffer attachment. Ensures proper
  67. * attachment flags are set on the view.
  68. */
  69. VkImageView getView(const TextureSurface& surface, bool framebuffer) const;
  70. /** Retrieves a subresource range covering all the sub-resources of the image. */
  71. VkImageSubresourceRange getRange() const;
  72. /**
  73. * Retrieves a separate resource for a specific image face & mip level. This allows the caller to track subresource
  74. * usage individually, instead for the entire image.
  75. */
  76. VulkanImageSubresource* getSubresource(UINT32 face, UINT32 mipLevel);
  77. /**
  78. * Returns a pointer to internal image memory for the specified sub-resource. Must be followed by unmap(). Caller
  79. * must ensure the image was created in CPU readable memory, and that image isn't currently being written to by the
  80. * GPU.
  81. *
  82. * @param[in] face Index of the face to map.
  83. * @param[in] mipLevel Index of the mip level to map.
  84. * @param[in] output Output object containing the pointer to the sub-resource data.
  85. */
  86. void map(UINT32 face, UINT32 mipLevel, PixelData& output) const;
  87. /**
  88. * Returns a pointer to internal image memory for the entire resource. Must be followed by unmap(). Caller
  89. * must ensure the image was created in CPU readable memory, and that image isn't currently being written to by the
  90. * GPU.
  91. */
  92. UINT8* map(UINT32 offset, UINT32 size) const;
  93. /** Unmaps a buffer previously mapped with map(). */
  94. void unmap();
  95. /**
  96. * Queues a command on the provided command buffer. The command copies the contents of the current image
  97. * subresource to the destination buffer.
  98. */
  99. void copy(VulkanTransferBuffer* cb, VulkanBuffer* destination, const VkExtent3D& extent,
  100. const VkImageSubresourceLayers& range, VkImageLayout layout);
  101. /**
  102. * Determines a set of access flags based on the current image and provided image layout. This method makes
  103. * certain assumptions about image usage, so it might not be valid in all situations.
  104. */
  105. VkAccessFlags getAccessFlags(VkImageLayout layout);
  106. private:
  107. /** Creates a new view of the provided part (or entirety) of surface. */
  108. VkImageView createView(const TextureSurface& surface, VkImageAspectFlags aspectMask) const;
  109. /** Contains information about view for a specific surface(s) of this image. */
  110. struct ImageViewInfo
  111. {
  112. TextureSurface surface;
  113. bool framebuffer;
  114. VkImageView view;
  115. };
  116. VkImage mImage;
  117. VkDeviceMemory mMemory;
  118. VkImageLayout mLayout;
  119. VkImageView mMainView;
  120. VkImageView mFramebufferMainView;
  121. bool mOwnsImage;
  122. bool mIsStorage;
  123. bool mIsDepthStencil;
  124. UINT32 mNumFaces;
  125. UINT32 mNumMipLevels;
  126. VulkanImageSubresource** mSubresources;
  127. mutable VkImageViewCreateInfo mImageViewCI;
  128. mutable Vector<ImageViewInfo> mImageInfos;
  129. };
  130. /** Represents a single sub-resource (face & mip level) of a larger image object. */
  131. class VulkanImageSubresource : public VulkanResource
  132. {
  133. public:
  134. VulkanImageSubresource(VulkanResourceManager* owner);
  135. };
  136. /** Vulkan implementation of a texture. */
  137. class VulkanTextureCore : public TextureCore
  138. {
  139. public:
  140. ~VulkanTextureCore();
  141. /**
  142. * Gets the resource wrapping the Vulkan image object, on the specified device. If texture device mask doesn't
  143. * include the provided device, null is returned.
  144. */
  145. VulkanImage* getResource(UINT32 deviceIdx) const { return mImages[deviceIdx]; }
  146. protected:
  147. friend class VulkanTextureCoreManager;
  148. VulkanTextureCore(const TEXTURE_DESC& desc, const SPtr<PixelData>& initialData, GpuDeviceFlags deviceMask);
  149. /** @copydoc CoreObjectCore::initialize() */
  150. void initialize() override;
  151. /** @copydoc TextureCore::lockImpl */
  152. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  153. UINT32 queueIdx = 0) override;
  154. /** @copydoc TextureCore::unlockImpl */
  155. void unlockImpl() override;
  156. /** @copydoc TextureCore::copyImpl */
  157. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel,
  158. const SPtr<TextureCore>& target, UINT32 queueIdx = 0) override;
  159. /** @copydoc TextureCore::readData */
  160. void readDataImpl(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  161. UINT32 queueIdx = 0) override;
  162. /** @copydoc TextureCore::writeData */
  163. void writeDataImpl(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false,
  164. UINT32 queueIdx = 0) override;
  165. private:
  166. /** Creates a new image for the specified device, matching the current properties. */
  167. VulkanImage* createImage(VulkanDevice& device);
  168. /**
  169. * Creates a staging buffer that can be used for texture transfer operations.
  170. *
  171. * @param[in] device Device to create the buffer on.
  172. * @param[in] pixelData Object that describes the image sub-resource that will be in the buffer.
  173. * @param[in] needsRead True if we will be copying data from the buffer, false if just reading. True if both.
  174. * @return Newly allocated buffer.
  175. */
  176. VulkanBuffer* createStaging(VulkanDevice& device, const PixelData& pixelData, bool needsRead);
  177. /**
  178. * Copies all sub-resources from the source image to the destination image. Caller must ensure the images
  179. * are of the same size. The operation will be queued on the provided command buffer. The system assumes the
  180. * provided image matches the current texture properties (i.e. num faces, mips, size).
  181. */
  182. void copyImage(VulkanTransferBuffer* cb, VulkanImage* srcImage, VulkanImage* dstImage,
  183. VkImageLayout srcFinalLayout, VkImageLayout dstFinalLayout);
  184. /** Returns the optimal layout this image should normally be in. */
  185. VkImageLayout getOptimalLayout() const;
  186. VulkanImage* mImages[BS_MAX_DEVICES];
  187. GpuDeviceFlags mDeviceMask;
  188. VulkanBuffer* mStagingBuffer;
  189. UINT32 mMappedDeviceIdx;
  190. UINT32 mMappedGlobalQueueIdx;
  191. UINT32 mMappedMip;
  192. UINT32 mMappedFace;
  193. UINT32 mMappedRowPitch;
  194. UINT32 mMappedSlicePitch;
  195. GpuLockOptions mMappedLockOptions;
  196. VkImageCreateInfo mImageCI;
  197. bool mDirectlyMappable : 1;
  198. bool mSupportsGPUWrites : 1;
  199. bool mIsMapped : 1;
  200. };
  201. /** @} */
  202. }