BsVulkanRenderTexture.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. VulkanRenderTexture::VulkanRenderTexture(const RENDER_TEXTURE_DESC& desc)
  12. :RenderTexture(desc), mProperties(desc, false)
  13. {
  14. }
  15. namespace ct
  16. {
  17. VulkanRenderTexture::VulkanRenderTexture(const RENDER_TEXTURE_DESC& desc, UINT32 deviceIdx)
  18. :RenderTexture(desc, deviceIdx), mProperties(desc, false), mDeviceIdx(deviceIdx), mFramebuffer(nullptr)
  19. {
  20. }
  21. VulkanRenderTexture::~VulkanRenderTexture()
  22. {
  23. mFramebuffer->destroy();
  24. }
  25. void VulkanRenderTexture::initialize()
  26. {
  27. RenderTexture::initialize();
  28. VULKAN_FRAMEBUFFER_DESC fbDesc;
  29. fbDesc.width = mProperties.width;
  30. fbDesc.height = mProperties.height;
  31. fbDesc.layers = mProperties.numSlices;
  32. fbDesc.numSamples = mProperties.multisampleCount > 1 ? mProperties.multisampleCount : 1;
  33. fbDesc.offscreen = true;
  34. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; ++i)
  35. {
  36. if (mColorSurfaces[i] == nullptr)
  37. continue;
  38. const SPtr<TextureView>& view = mColorSurfaces[i];
  39. VulkanTexture* texture = static_cast<VulkanTexture*>(mDesc.colorSurfaces[i].texture.get());
  40. VulkanImage* image = texture->getResource(mDeviceIdx);
  41. if (image == nullptr)
  42. continue;
  43. TextureSurface surface;
  44. surface.mipLevel = view->getMostDetailedMip();
  45. surface.numMipLevels = view->getNumMips();
  46. if (texture->getProperties().getTextureType() == TEX_TYPE_3D)
  47. {
  48. if(view->getFirstArraySlice() > 0)
  49. LOGERR("Non-zero array slice offset not supported when rendering to a 3D texture.");
  50. if (view->getNumArraySlices() > 1)
  51. LOGERR("Cannot specify array slices when rendering to a 3D texture.");
  52. surface.face = 0;
  53. surface.numFaces = mProperties.numSlices;
  54. fbDesc.color[i].baseLayer = 0;
  55. }
  56. else
  57. {
  58. surface.face = view->getFirstArraySlice();
  59. surface.numFaces = view->getNumArraySlices();
  60. fbDesc.color[i].baseLayer = view->getFirstArraySlice();
  61. fbDesc.layers = view->getNumArraySlices();
  62. }
  63. fbDesc.color[i].image = image;
  64. fbDesc.color[i].surface = surface;
  65. fbDesc.color[i].format = VulkanUtility::getPixelFormat(texture->getProperties().getFormat(),
  66. texture->getProperties().isHardwareGammaEnabled());
  67. }
  68. if(mDepthStencilSurface != nullptr)
  69. {
  70. const SPtr<TextureView>& view = mDepthStencilSurface;
  71. VulkanTexture* texture = static_cast<VulkanTexture*>(mDesc.depthStencilSurface.texture.get());
  72. VulkanImage* image = texture->getResource(mDeviceIdx);
  73. if (image != nullptr)
  74. {
  75. TextureSurface surface;
  76. surface.mipLevel = view->getMostDetailedMip();
  77. surface.numMipLevels = view->getNumMips();
  78. if (texture->getProperties().getTextureType() == TEX_TYPE_3D)
  79. {
  80. if (view->getFirstArraySlice() > 0)
  81. LOGERR("Non-zero array slice offset not supported when rendering to a 3D texture.");
  82. if (view->getNumArraySlices() > 1)
  83. LOGERR("Cannot specify array slices when rendering to a 3D texture.");
  84. surface.face = 0;
  85. surface.numFaces = 1;
  86. fbDesc.depth.baseLayer = 0;
  87. }
  88. else
  89. {
  90. surface.face = view->getFirstArraySlice();
  91. surface.numFaces = view->getNumArraySlices();
  92. fbDesc.depth.baseLayer = view->getFirstArraySlice();
  93. fbDesc.layers = view->getNumArraySlices();
  94. }
  95. fbDesc.depth.image = image;
  96. fbDesc.depth.surface = surface;
  97. fbDesc.depth.format = VulkanUtility::getPixelFormat(texture->getProperties().getFormat(),
  98. texture->getProperties().isHardwareGammaEnabled());
  99. fbDesc.depth.baseLayer = view->getFirstArraySlice();
  100. }
  101. }
  102. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  103. SPtr<VulkanDevice> device = rapi._getDevice(mDeviceIdx);
  104. mFramebuffer = device->getResourceManager().create<VulkanFramebuffer>(fbDesc);
  105. }
  106. void VulkanRenderTexture::getCustomAttribute(const String& name, void* data) const
  107. {
  108. if (name == "FB")
  109. {
  110. VulkanFramebuffer** fb = (VulkanFramebuffer**)data;
  111. *fb = mFramebuffer;
  112. return;
  113. }
  114. }
  115. }
  116. }