BsVulkanTexture.h 10 KB

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