BsVulkanTexture.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "Image/BsTexture.h"
  7. namespace bs { namespace ct
  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. UINT32 usage; /** Determines how will the image be used. */
  24. };
  25. /** Wrapper around a Vulkan image object that manages its usage and lifetime. */
  26. class VulkanImage : public VulkanResource
  27. {
  28. public:
  29. /**
  30. * @param[in] owner Resource manager that keeps track of lifetime of this resource.
  31. * @param[in] image Internal image Vulkan object.
  32. * @param[in] memory Memory bound to the image.
  33. * @param[in] layout Initial layout of the image.
  34. * @param[in] props Properties describing the image.
  35. * @param[in] ownsImage If true, this object will take care of releasing the image and its memory, otherwise
  36. * it is expected they will be released externally.
  37. */
  38. VulkanImage(VulkanResourceManager* owner, VkImage image, VkDeviceMemory memory, VkImageLayout layout,
  39. const TextureProperties& props, bool ownsImage = true);
  40. /**
  41. * @param[in] owner Resource manager that keeps track of lifetime of this resource.
  42. * @param[in] desc Describes the image to assign.
  43. * @param[in] ownsImage If true, this object will take care of releasing the image and its memory, otherwise
  44. * it is expected they will be released externally.
  45. */
  46. VulkanImage(VulkanResourceManager* owner, const VULKAN_IMAGE_DESC& desc, bool ownsImage = true);
  47. ~VulkanImage();
  48. /** Returns the internal handle to the Vulkan object. */
  49. VkImage getHandle() const { return mImage; }
  50. /** Returns the preferred (not necessarily current) layout of the image. */
  51. VkImageLayout getOptimalLayout() const;
  52. /**
  53. * Returns an image view that covers all faces and mip maps of the texture.
  54. *
  55. * @param[in] framebuffer Set to true if the view will be used as a framebuffer attachment. Ensures proper
  56. * attachment flags are set on the view.
  57. */
  58. VkImageView getView(bool framebuffer) const;
  59. /**
  60. * Returns an image view that covers the specified faces and mip maps of the texture.
  61. *
  62. * @param[in] surface Surface that describes which faces and mip levels to retrieve the view for.
  63. * @param[in] framebuffer Set to true if the view will be used as a framebuffer attachment. Ensures proper
  64. * attachment flags are set on the view.
  65. */
  66. VkImageView getView(const TextureSurface& surface, bool framebuffer) const;
  67. /** Get aspect flags that represent the contents of this image. */
  68. VkImageAspectFlags getAspectFlags() const;
  69. /** Retrieves a subresource range covering all the sub-resources of the image. */
  70. VkImageSubresourceRange getRange() const;
  71. /** Retrieves a subresource range covering all the specified sub-resource range of the image. */
  72. VkImageSubresourceRange getRange(const TextureSurface& surface) const;
  73. /**
  74. * Retrieves a separate resource for a specific image face & mip level. This allows the caller to track subresource
  75. * usage individually, instead for the entire image.
  76. */
  77. VulkanImageSubresource* getSubresource(UINT32 face, UINT32 mipLevel);
  78. /**
  79. * Returns a pointer to internal image memory for the specified sub-resource. Must be followed by unmap(). Caller
  80. * must ensure the image was created in CPU readable memory, and that image isn't currently being written to by the
  81. * GPU.
  82. *
  83. * @param[in] face Index of the face to map.
  84. * @param[in] mipLevel Index of the mip level to map.
  85. * @param[in] output Output object containing the pointer to the sub-resource data.
  86. */
  87. void map(UINT32 face, UINT32 mipLevel, PixelData& output) const;
  88. /**
  89. * Returns a pointer to internal image memory for the entire resource. Must be followed by unmap(). Caller
  90. * must ensure the image was created in CPU readable memory, and that image isn't currently being written to by the
  91. * GPU.
  92. */
  93. UINT8* map(UINT32 offset, UINT32 size) const;
  94. /** Unmaps a buffer previously mapped with map(). */
  95. void unmap();
  96. /**
  97. * Queues a command on the provided command buffer. The command copies the contents of the current image
  98. * subresource to the destination buffer.
  99. */
  100. void copy(VulkanTransferBuffer* cb, VulkanBuffer* destination, const VkExtent3D& extent,
  101. const VkImageSubresourceLayers& range, VkImageLayout layout);
  102. /**
  103. * Determines a set of access flags based on the current image and provided image layout. This method makes
  104. * certain assumptions about image usage, so it might not be valid in all situations.
  105. *
  106. * @param[in] layout Layout the image is currently in.
  107. * @param[in] readOnly True if the image is only going to be read without writing, allows the system to
  108. * set less general access flags. If unsure, set to false.
  109. */
  110. VkAccessFlags getAccessFlags(VkImageLayout layout, bool readOnly = false);
  111. /**
  112. * Generates a set of image barriers that are grouped depending on the current layout of individual sub-resources
  113. * in the specified range. The method will try to reduce the number of generated barriers by grouping as many
  114. * sub-resources as possibly.
  115. */
  116. void getBarriers(const VkImageSubresourceRange& range, Vector<VkImageMemoryBarrier>& barriers);
  117. private:
  118. /** Creates a new view of the provided part (or entirety) of surface. */
  119. VkImageView createView(const TextureSurface& surface, VkImageAspectFlags aspectMask) const;
  120. /** Contains information about view for a specific surface(s) of this image. */
  121. struct ImageViewInfo
  122. {
  123. TextureSurface surface;
  124. bool framebuffer;
  125. VkImageView view;
  126. };
  127. VkImage mImage;
  128. VkDeviceMemory mMemory;
  129. VkImageView mMainView;
  130. VkImageView mFramebufferMainView;
  131. INT32 mUsage;
  132. bool mOwnsImage;
  133. UINT32 mNumFaces;
  134. UINT32 mNumMipLevels;
  135. VulkanImageSubresource** mSubresources;
  136. mutable VkImageViewCreateInfo mImageViewCI;
  137. mutable Vector<ImageViewInfo> mImageInfos;
  138. };
  139. /** Represents a single sub-resource (face & mip level) of a larger image object. */
  140. class VulkanImageSubresource : public VulkanResource
  141. {
  142. public:
  143. VulkanImageSubresource(VulkanResourceManager* owner, VkImageLayout layout);
  144. /**
  145. * Returns the layout the subresource is currently in. Note that this is only used to communicate layouts between
  146. * different command buffers, and will only be updated only after command buffer submit() call. In short this means
  147. * you should only care about this value on the core thread.
  148. */
  149. VkImageLayout getLayout() const { return mLayout; }
  150. /** Notifies the resource that the current subresource layout has changed. */
  151. void setLayout(VkImageLayout layout) { mLayout = layout; }
  152. private:
  153. VkImageLayout mLayout;
  154. };
  155. /** Vulkan implementation of a texture. */
  156. class VulkanTexture : public Texture
  157. {
  158. public:
  159. ~VulkanTexture();
  160. /**
  161. * Gets the resource wrapping the Vulkan image object, on the specified device. If texture device mask doesn't
  162. * include the provided device, null is returned.
  163. */
  164. VulkanImage* getResource(UINT32 deviceIdx) const { return mImages[deviceIdx]; }
  165. protected:
  166. friend class VulkanTextureManager;
  167. VulkanTexture(const TEXTURE_DESC& desc, const SPtr<PixelData>& initialData, GpuDeviceFlags deviceMask);
  168. /** @copydoc CoreObject::initialize() */
  169. void initialize() override;
  170. /** @copydoc Texture::lockImpl */
  171. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  172. UINT32 queueIdx = 0) override;
  173. /** @copydoc Texture::unlockImpl */
  174. void unlockImpl() override;
  175. /** @copydoc Texture::copyImpl */
  176. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 dstFace, UINT32 dstMipLevel,
  177. const SPtr<Texture>& target, const SPtr<CommandBuffer>& commandBuffer) override;
  178. /** @copydoc Texture::readData */
  179. void readDataImpl(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  180. UINT32 queueIdx = 0) override;
  181. /** @copydoc Texture::writeData */
  182. void writeDataImpl(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false,
  183. UINT32 queueIdx = 0) override;
  184. private:
  185. /** Creates a new image for the specified device, matching the current properties. */
  186. VulkanImage* createImage(VulkanDevice& device, PixelFormat format);
  187. /**
  188. * Creates a staging buffer that can be used for texture transfer operations.
  189. *
  190. * @param[in] device Device to create the buffer on.
  191. * @param[in] pixelData Object that describes the image sub-resource that will be in the buffer.
  192. * @param[in] needsRead True if we will be copying data from the buffer, false if just reading. True if both.
  193. * @return Newly allocated buffer.
  194. */
  195. VulkanBuffer* createStaging(VulkanDevice& device, const PixelData& pixelData, bool needsRead);
  196. /**
  197. * Copies all sub-resources from the source image to the destination image. Caller must ensure the images
  198. * are of the same size. The operation will be queued on the provided command buffer. The system assumes the
  199. * provided image matches the current texture properties (i.e. num faces, mips, size).
  200. */
  201. void copyImage(VulkanTransferBuffer* cb, VulkanImage* srcImage, VulkanImage* dstImage,
  202. VkImageLayout srcFinalLayout, VkImageLayout dstFinalLayout);
  203. VulkanImage* mImages[BS_MAX_DEVICES];
  204. PixelFormat mInternalFormats[BS_MAX_DEVICES];
  205. GpuDeviceFlags mDeviceMask;
  206. VulkanBuffer* mStagingBuffer;
  207. UINT32 mMappedDeviceIdx;
  208. UINT32 mMappedGlobalQueueIdx;
  209. UINT32 mMappedMip;
  210. UINT32 mMappedFace;
  211. UINT32 mMappedRowPitch;
  212. UINT32 mMappedSlicePitch;
  213. GpuLockOptions mMappedLockOptions;
  214. VkImageCreateInfo mImageCI;
  215. bool mDirectlyMappable : 1;
  216. bool mSupportsGPUWrites : 1;
  217. bool mIsMapped : 1;
  218. };
  219. /** @} */
  220. }}