2
0

BsVulkanFramebuffer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. namespace bs { namespace ct
  7. {
  8. /** @addtogroup Vulkan
  9. * @{
  10. */
  11. /** Represents a single attachment in a Vulkan frame-buffer. */
  12. struct VULKAN_ATTACHMENT_DESC
  13. {
  14. /** Image to attach or null if none. */
  15. VulkanImage* image = nullptr;
  16. /** Surface representing the sub-resource of the image to use as an attachment. */
  17. TextureSurface surface;
  18. /** Format of the attached image. */
  19. VkFormat format = VK_FORMAT_UNDEFINED;
  20. /** Initial layer of the surface as pointed to by the provided image view. */
  21. UINT32 baseLayer = 0;
  22. };
  23. /** Contains parameters used for initializing VulkanFrameBuffer object. */
  24. struct VULKAN_FRAMEBUFFER_DESC
  25. {
  26. /** Images describing the color attachments. */
  27. VULKAN_ATTACHMENT_DESC color[BS_MAX_MULTIPLE_RENDER_TARGETS];
  28. /** Image describing the depth attachment. */
  29. VULKAN_ATTACHMENT_DESC depth;
  30. /** Width of the images, in pixels. All images must be the same size. */
  31. UINT32 width = 0;
  32. /** Height of the images, in pixels. All images must be the same size. */
  33. UINT32 height = 0;
  34. /** Number of image layers to render to. This value is used for all provided surfaces. */
  35. UINT32 layers = 0;
  36. /** Number of samples in the attached images. All attachments must have the same number of samples. */
  37. UINT32 numSamples = 0;
  38. /** Set to true if framebuffer represents an offscreen surface that will not be presented. */
  39. bool offscreen = false;
  40. };
  41. /** Information about a single framebuffer attachment. */
  42. struct VulkanFramebufferAttachment
  43. {
  44. VulkanImage* image = nullptr;
  45. TextureSurface surface;
  46. UINT32 baseLayer = 0;
  47. VkImageLayout finalLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  48. UINT32 index = 0;
  49. };
  50. /** Vulkan frame buffer containing one or multiple color surfaces, and an optional depth surface. */
  51. class VulkanFramebuffer : public VulkanResource
  52. {
  53. public:
  54. /** Creates a new frame buffer with the specified image views attached.
  55. *
  56. * @param[in] owner Resource manager that allocated this resource.
  57. * @param[in] desc Description of the frame buffer.
  58. */
  59. VulkanFramebuffer(VulkanResourceManager* owner, const VULKAN_FRAMEBUFFER_DESC& desc);
  60. ~VulkanFramebuffer();
  61. /** Returns a unique ID of this framebuffer. */
  62. UINT32 getId() const { return mId; }
  63. /**
  64. * Gets internal Vulkan render pass object.
  65. *
  66. * @param[in] loadMask Mask that control which render target surface contents should be preserved on load.
  67. * @param[in] readMask Mask that controls which render targets can be read by shaders while they're bound.
  68. * @param[in] clearMask Mask that controls which render targets should be cleared on render pass start. Target
  69. * cannot have both load and clear bits set. If load bit is set, clear will be ignored.
  70. */
  71. VkRenderPass getRenderPass(RenderSurfaceMask loadMask, RenderSurfaceMask readMask,
  72. ClearMask clearMask) const;
  73. /**
  74. * Gets internal Vulkan framebuffer object.
  75. *
  76. * @param[in] loadMask Mask that control which render target surface contents should be preserved on load.
  77. * @param[in] readMask Mask that controls which render targets can be read by shaders while they're bound.
  78. * @param[in] clearMask Mask that controls which render targets should be cleared on render pass start. Target
  79. * cannot have both load and clear bits set. If load bit is set, clear will be ignored.
  80. */
  81. VkFramebuffer getFramebuffer(RenderSurfaceMask loadMask, RenderSurfaceMask readMask,
  82. ClearMask clearMask) const;
  83. /**
  84. * Gets the number of layers in each framebuffer surface. A layer is an element in a texture array, or a depth
  85. * slice in a 3D texture).
  86. */
  87. UINT32 getNumLayers() const { return mNumLayers; }
  88. /** Returns information about a color attachment at the specified index. */
  89. const VulkanFramebufferAttachment& getColorAttachment(UINT32 colorIdx) const { return mColorAttachments[colorIdx]; }
  90. /** Returns information about a depth-stencil attachment. */
  91. const VulkanFramebufferAttachment& getDepthStencilAttachment() const { return mDepthStencilAttachment; }
  92. /** Gets the total number of frame-buffer attachments, including both color and depth. */
  93. UINT32 getNumAttachments() const { return mNumAttachments; }
  94. /** Gets the number of color frame-buffer attachments. */
  95. UINT32 getNumColorAttachments() const { return mNumColorAttachments; }
  96. /** Returns true if the framebuffer has a depth attachment. */
  97. bool hasDepthAttachment() const { return mHasDepth; }
  98. /** Returns sample flags that determine if the framebuffer supports multi-sampling, and for how many samples. */
  99. VkSampleCountFlagBits getSampleFlags() const { return mSampleFlags; }
  100. /**
  101. * Returns the maximum required number of clear entries to provide in a render pass start structure. This depends on
  102. * the clear mask and the attachments on the framebuffer.
  103. */
  104. UINT32 getNumClearEntries(ClearMask clearMask) const;
  105. private:
  106. /** Information about a single frame-buffer variant. */
  107. struct Variant
  108. {
  109. VkRenderPass renderPass;
  110. VkFramebuffer framebuffer;
  111. };
  112. /** Key used for identifying different types of frame-buffer variants. */
  113. struct VariantKey
  114. {
  115. VariantKey(RenderSurfaceMask loadMask, RenderSurfaceMask readMask, ClearMask clearMask);
  116. class HashFunction
  117. {
  118. public:
  119. size_t operator()(const VariantKey& key) const;
  120. };
  121. class EqualFunction
  122. {
  123. public:
  124. bool operator()(const VariantKey& lhs, const VariantKey& rhs) const;
  125. };
  126. RenderSurfaceMask loadMask;
  127. RenderSurfaceMask readMask;
  128. ClearMask clearMask;
  129. };
  130. /** Creates a new variant of the framebuffer. */
  131. Variant createVariant(RenderSurfaceMask loadMask, RenderSurfaceMask readMask, ClearMask clearMask) const;
  132. UINT32 mId;
  133. Variant mDefault;
  134. mutable UnorderedMap<VariantKey, Variant, VariantKey::HashFunction, VariantKey::EqualFunction> mVariants;
  135. UINT32 mNumAttachments;
  136. UINT32 mNumColorAttachments;
  137. UINT32 mNumLayers;
  138. VulkanFramebufferAttachment mColorAttachments[BS_MAX_MULTIPLE_RENDER_TARGETS];
  139. VulkanFramebufferAttachment mDepthStencilAttachment;
  140. bool mHasDepth;
  141. VkSampleCountFlagBits mSampleFlags;
  142. mutable VkAttachmentDescription mAttachments[BS_MAX_MULTIPLE_RENDER_TARGETS + 1];
  143. mutable VkImageView mAttachmentViews[BS_MAX_MULTIPLE_RENDER_TARGETS + 1];
  144. mutable VkAttachmentReference mColorReferences[BS_MAX_MULTIPLE_RENDER_TARGETS];
  145. mutable VkAttachmentReference mDepthReference;
  146. mutable VkSubpassDescription mSubpassDesc;
  147. mutable VkSubpassDependency mDependencies[2];
  148. mutable VkRenderPassCreateInfo mRenderPassCI;
  149. mutable VkFramebufferCreateInfo mFramebufferCI;
  150. static UINT32 sNextValidId;
  151. };
  152. /** @} */
  153. }}