BsVulkanTexture.h 8.0 KB

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