BsVulkanRenderTexture.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVulkanRenderTexture.h"
  4. #include "BsVulkanFramebuffer.h"
  5. #include "BsVulkanTexture.h"
  6. #include "BsVulkanUtility.h"
  7. #include "BsVulkanRenderAPI.h"
  8. #include "BsVulkanDevice.h"
  9. namespace bs
  10. {
  11. VulkanRenderTextureCore::VulkanRenderTextureCore(const RENDER_TEXTURE_DESC_CORE& desc, UINT32 deviceIdx)
  12. :RenderTextureCore(desc, deviceIdx), mProperties(desc, false), mDeviceIdx(deviceIdx), mFramebuffer(nullptr)
  13. {
  14. }
  15. VulkanRenderTextureCore::~VulkanRenderTextureCore()
  16. {
  17. mFramebuffer->destroy();
  18. }
  19. void VulkanRenderTextureCore::initialize()
  20. {
  21. RenderTextureCore::initialize();
  22. VULKAN_FRAMEBUFFER_DESC fbDesc;
  23. fbDesc.width = mProperties.getWidth();
  24. fbDesc.height = mProperties.getHeight();
  25. fbDesc.layers = 1;
  26. fbDesc.numSamples = mProperties.getMultisampleCount() > 1 ? mProperties.getMultisampleCount() : 1;
  27. fbDesc.offscreen = true;
  28. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; ++i)
  29. {
  30. if (mColorSurfaces[i] == nullptr)
  31. continue;
  32. const SPtr<TextureView>& view = mColorSurfaces[i];
  33. VulkanTextureCore* texture = static_cast<VulkanTextureCore*>(view->getTexture().get());
  34. VulkanImage* image = texture->getResource(mDeviceIdx);
  35. if (image == nullptr)
  36. continue;
  37. TextureSurface surface;
  38. surface.mipLevel = view->getMostDetailedMip();
  39. surface.numMipLevels = view->getNumMips();
  40. if (texture->getProperties().getTextureType() == TEX_TYPE_3D)
  41. {
  42. if(view->getFirstArraySlice() > 0)
  43. LOGERR("Non-zero array slice offset not supported when rendering to a 3D texture.");
  44. if (view->getNumArraySlices() > 1)
  45. LOGERR("Cannot specify array slices when rendering to a 3D texture.");
  46. surface.arraySlice = 0;
  47. surface.numArraySlices = 1;
  48. fbDesc.color[i].baseLayer = 0;
  49. fbDesc.layers = 1;
  50. }
  51. else
  52. {
  53. surface.arraySlice = view->getFirstArraySlice();
  54. surface.numArraySlices = view->getNumArraySlices();
  55. fbDesc.color[i].baseLayer = view->getFirstArraySlice();
  56. fbDesc.layers = view->getNumArraySlices();
  57. }
  58. fbDesc.color[i].image = image;
  59. fbDesc.color[i].view = image->getView(surface, true);
  60. fbDesc.color[i].format = VulkanUtility::getPixelFormat(texture->getProperties().getFormat());
  61. }
  62. if(mDepthStencilSurface != nullptr)
  63. {
  64. const SPtr<TextureView>& view = mDepthStencilSurface;
  65. VulkanTextureCore* texture = static_cast<VulkanTextureCore*>(view->getTexture().get());
  66. VulkanImage* image = texture->getResource(mDeviceIdx);
  67. if (image != nullptr)
  68. {
  69. TextureSurface surface;
  70. surface.mipLevel = view->getMostDetailedMip();
  71. surface.numMipLevels = view->getNumMips();
  72. if (texture->getProperties().getTextureType() == TEX_TYPE_3D)
  73. {
  74. if (view->getFirstArraySlice() > 0)
  75. LOGERR("Non-zero array slice offset not supported when rendering to a 3D texture.");
  76. if (view->getNumArraySlices() > 1)
  77. LOGERR("Cannot specify array slices when rendering to a 3D texture.");
  78. surface.arraySlice = 0;
  79. surface.numArraySlices = 1;
  80. fbDesc.depth.baseLayer = 0;
  81. fbDesc.layers = 1;
  82. }
  83. else
  84. {
  85. surface.arraySlice = view->getFirstArraySlice();
  86. surface.numArraySlices = view->getNumArraySlices();
  87. fbDesc.depth.baseLayer = view->getFirstArraySlice();
  88. fbDesc.layers = view->getNumArraySlices();
  89. }
  90. fbDesc.depth.image = image;
  91. fbDesc.depth.view = image->getView(surface, true);
  92. fbDesc.depth.format = VulkanUtility::getPixelFormat(texture->getProperties().getFormat());
  93. fbDesc.depth.baseLayer = view->getFirstArraySlice();
  94. }
  95. }
  96. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPICore::instance());
  97. SPtr<VulkanDevice> device = rapi._getDevice(mDeviceIdx);
  98. mFramebuffer = device->getResourceManager().create<VulkanFramebuffer>(fbDesc);
  99. }
  100. void VulkanRenderTextureCore::getCustomAttribute(const String& name, void* data) const
  101. {
  102. if (name == "FB")
  103. {
  104. VulkanFramebuffer** fb = (VulkanFramebuffer**)data;
  105. *fb = mFramebuffer;
  106. return;
  107. }
  108. }
  109. VulkanRenderTexture::VulkanRenderTexture(const RENDER_TEXTURE_DESC& desc)
  110. :RenderTexture(desc), mProperties(desc, false)
  111. {
  112. }
  113. }