BsVulkanTexture.h 6.5 KB

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