BsVulkanTexture.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /** Wrapper around a Vulkan image object that manages its usage and lifetime. */
  13. class VulkanImage : public VulkanResource
  14. {
  15. public:
  16. VulkanImage(VulkanResourceManager* owner, VkImage image, VkDeviceMemory memory, VkImageLayout layout,
  17. const TextureProperties& props);
  18. ~VulkanImage();
  19. /** Returns the internal handle to the Vulkan object. */
  20. VkImage getHandle() const { return mImage; }
  21. /** Returns the layout the image is currently in. */
  22. VkImageLayout getLayout() const { return mLayout; }
  23. /** Notifies the resource that the current image layout has changed. */
  24. void setLayout(VkImageLayout layout) { mLayout = layout; }
  25. /** Returns an image view that covers all faces and mip maps of the texture. */
  26. VkImageView getView() const { return mMainView; };
  27. /** Returns an image view that covers the specified faces and mip maps of the texture. */
  28. VkImageView getView(const TextureSurface& surface) const;
  29. /**
  30. * Queues a command on the provided command buffer. The command copies the contents of the current image
  31. * subresource to the destination buffer.
  32. */
  33. void copy(VulkanTransferBuffer* cb, VulkanBuffer* destination, const VkExtent3D& extent,
  34. const VkImageSubresourceLayers& range, VkImageLayout layout);
  35. private:
  36. /** Creates a new view of the provided part (or entirety) of surface. */
  37. VkImageView createView(const TextureSurface& surface) const;
  38. /** Contains information about view for a specific surface(s) of this image. */
  39. struct ImageViewInfo
  40. {
  41. TextureSurface surface;
  42. VkImageView view;
  43. };
  44. VkImage mImage;
  45. VkDeviceMemory mMemory;
  46. VkImageLayout mLayout;
  47. VkImageView mMainView;
  48. mutable VkImageViewCreateInfo mImageViewCI;
  49. mutable Vector<ImageViewInfo> mImageInfos;
  50. };
  51. /** Vulkan implementation of a texture. */
  52. class VulkanTextureCore : public TextureCore
  53. {
  54. public:
  55. ~VulkanTextureCore();
  56. /**
  57. * Gets the resource wrapping the Vulkan image object, on the specified device. If texture device mask doesn't
  58. * include the provided device, null is returned.
  59. */
  60. VulkanImage* getResource(UINT32 deviceIdx) const { return mImages[deviceIdx]; }
  61. /**
  62. * Returns an image view that covers all faces and mip maps of the texture. Usable only on the specified device.
  63. * If texture device mask doesn't include the provided device, null is returned.
  64. */
  65. VkImageView getView(UINT32 deviceIdx) const;
  66. /**
  67. * Returns an image view that covers the specified faces and mip maps of the texture. Usable only on the specified
  68. * device. If texture device mask doesn't include the provided device, null is returned.
  69. */
  70. VkImageView getView(UINT32 deviceIdx, const TextureSurface& surface) const;
  71. protected:
  72. friend class VulkanTextureCoreManager;
  73. VulkanTextureCore(const TEXTURE_DESC& desc, const SPtr<PixelData>& initialData, GpuDeviceFlags deviceMask);
  74. /** @copydoc CoreObjectCore::initialize() */
  75. void initialize() override;
  76. /** @copydoc TextureCore::lockImpl */
  77. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  78. UINT32 queueIdx = 0) override;
  79. /** @copydoc TextureCore::unlockImpl */
  80. void unlockImpl() override;
  81. /** @copydoc TextureCore::copyImpl */
  82. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel,
  83. const SPtr<TextureCore>& target, UINT32 queueIdx = 0) override;
  84. /** @copydoc TextureCore::readData */
  85. void readDataImpl(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  86. UINT32 queueIdx = 0) override;
  87. /** @copydoc TextureCore::writeData */
  88. void writeDataImpl(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false,
  89. UINT32 queueIdx = 0) override;
  90. private:
  91. /** Creates a new image for the specified device, matching the current properties. */
  92. VulkanImage* createImage(VulkanDevice& device);
  93. /**
  94. * Creates a new buffer for the specified device, with enough space to hold the provided mip level of this
  95. * texture.
  96. */
  97. VulkanBuffer* createStaging(VulkanDevice& device, UINT32 mipLevel, bool needsRead);
  98. VulkanImage* mImages[BS_MAX_DEVICES];
  99. GpuDeviceFlags mDeviceMask;
  100. VkAccessFlags mAccessFlags;
  101. VulkanBuffer* mStagingBuffer;
  102. UINT32 mMappedDeviceIdx;
  103. UINT32 mMappedGlobalQueueIdx;
  104. UINT32 mMappedMip;
  105. UINT32 mMappedFace;
  106. GpuLockOptions mMappedLockOptions;
  107. VkImageCreateInfo mImageCI;
  108. bool mDirectlyMappable : 1;
  109. bool mSupportsGPUWrites : 1;
  110. bool mIsMapped : 1;
  111. };
  112. /** @} */
  113. }