BsVulkanTexture.h 8.1 KB

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