BsVulkanTexture.h 9.8 KB

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