BsVulkanTexture.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 the layout the image is currently in. Note that this is only used to communicate layouts between
  63. * different command buffers, and will only be updated only after command buffer submit() call. In short this means
  64. * you should only care about this value on the core thread.
  65. */
  66. VkImageLayout getLayout() const { return mLayout; }
  67. /** Notifies the resource that the current image layout has changed. */
  68. void setLayout(VkImageLayout layout) { mLayout = layout; }
  69. /**
  70. * Returns an image view that covers all faces and mip maps of the texture.
  71. *
  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(bool framebuffer) const;
  76. /**
  77. * Returns an image view that covers the specified faces and mip maps of the texture.
  78. *
  79. * @param[in] surface Surface that describes which faces and mip levels to retrieve the view for.
  80. * @param[in] framebuffer Set to true if the view will be used as a framebuffer attachment. Ensures proper
  81. * attachment flags are set on the view.
  82. */
  83. VkImageView getView(const TextureSurface& surface, bool framebuffer) const;
  84. /** Get aspect flags that represent the contents of this image. */
  85. VkImageAspectFlags getAspectFlags() const;
  86. /** Retrieves a subresource range covering all the sub-resources of the image. */
  87. VkImageSubresourceRange getRange() const;
  88. /**
  89. * Retrieves a separate resource for a specific image face & mip level. This allows the caller to track subresource
  90. * usage individually, instead for the entire image.
  91. */
  92. VulkanImageSubresource* getSubresource(UINT32 face, UINT32 mipLevel);
  93. /**
  94. * Returns a pointer to internal image memory for the specified sub-resource. Must be followed by unmap(). Caller
  95. * must ensure the image was created in CPU readable memory, and that image isn't currently being written to by the
  96. * GPU.
  97. *
  98. * @param[in] face Index of the face to map.
  99. * @param[in] mipLevel Index of the mip level to map.
  100. * @param[in] output Output object containing the pointer to the sub-resource data.
  101. */
  102. void map(UINT32 face, UINT32 mipLevel, PixelData& output) const;
  103. /**
  104. * Returns a pointer to internal image memory for the entire resource. Must be followed by unmap(). Caller
  105. * must ensure the image was created in CPU readable memory, and that image isn't currently being written to by the
  106. * GPU.
  107. */
  108. UINT8* map(UINT32 offset, UINT32 size) const;
  109. /** Unmaps a buffer previously mapped with map(). */
  110. void unmap();
  111. /**
  112. * Queues a command on the provided command buffer. The command copies the contents of the current image
  113. * subresource to the destination buffer.
  114. */
  115. void copy(VulkanTransferBuffer* cb, VulkanBuffer* destination, const VkExtent3D& extent,
  116. const VkImageSubresourceLayers& range, VkImageLayout layout);
  117. /**
  118. * Determines a set of access flags based on the current image and provided image layout. This method makes
  119. * certain assumptions about image usage, so it might not be valid in all situations.
  120. *
  121. * @param[in] layout Layout the image is currently in.
  122. * @param[in] readOnly True if the image is only going to be read without writing, allows the system to
  123. * set less general access flags. If unsure, set to false.
  124. */
  125. VkAccessFlags getAccessFlags(VkImageLayout layout, bool readOnly = false);
  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. VkImageLayout mLayout;
  139. VkImageView mMainView;
  140. VkImageView mFramebufferMainView;
  141. VulkanImageUsage mUsage;
  142. bool mOwnsImage;
  143. UINT32 mNumFaces;
  144. UINT32 mNumMipLevels;
  145. VulkanImageSubresource** mSubresources;
  146. mutable VkImageViewCreateInfo mImageViewCI;
  147. mutable Vector<ImageViewInfo> mImageInfos;
  148. };
  149. /** Represents a single sub-resource (face & mip level) of a larger image object. */
  150. class VulkanImageSubresource : public VulkanResource
  151. {
  152. public:
  153. VulkanImageSubresource(VulkanResourceManager* owner);
  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 destFace, UINT32 destMipLevel,
  177. const SPtr<Texture>& target, UINT32 queueIdx = 0) 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. }}