BsVulkanTexture.h 8.3 KB

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