BsMultiRenderTexture.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "BsMultiRenderTexture.h"
  2. #include "BsTexture.h"
  3. #include "BsException.h"
  4. #include "BsDebug.h"
  5. #include "BsCoreThread.h"
  6. #include "BsTextureManager.h"
  7. namespace BansheeEngine
  8. {
  9. MultiRenderTextureCore::MultiRenderTextureCore(MultiRenderTexture* parent, MultiRenderTextureProperties* properties, const MULTI_RENDER_TEXTURE_DESC& desc)
  10. :RenderTargetCore(parent, properties)
  11. {
  12. mColorSurfaces.resize(BS_MAX_MULTIPLE_RENDER_TARGETS);
  13. for (size_t i = 0; i < desc.colorSurfaces.size(); i++)
  14. {
  15. if (desc.colorSurfaces[i].texture != nullptr)
  16. {
  17. if (i >= BS_MAX_MULTIPLE_RENDER_TARGETS)
  18. {
  19. LOGWRN("Render texture index is larger than the maximum number of supported render targets. Index: " + toString((int)i) +
  20. ". Max. number of render targets: " + toString(BS_MAX_MULTIPLE_RENDER_TARGETS));
  21. continue;
  22. }
  23. TexturePtr texture = desc.colorSurfaces[i].texture;
  24. if (texture->getUsage() != TU_RENDERTARGET)
  25. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  26. mColorSurfaces[i] = Texture::requestView(texture, desc.colorSurfaces[i].mipLevel, 1,
  27. desc.colorSurfaces[i].face, 1, GVU_RENDERTARGET);
  28. }
  29. }
  30. if (desc.depthStencilSurface.texture != nullptr)
  31. {
  32. TexturePtr texture = desc.depthStencilSurface.texture;
  33. if (texture->getUsage() != TU_DEPTHSTENCIL)
  34. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  35. mDepthStencilSurface = Texture::requestView(texture, desc.depthStencilSurface.mipLevel, 1,
  36. desc.depthStencilSurface.face, 1, GVU_DEPTHSTENCIL);
  37. }
  38. throwIfBuffersDontMatch();
  39. }
  40. MultiRenderTextureCore::~MultiRenderTextureCore()
  41. {
  42. for (auto iter = mColorSurfaces.begin(); iter != mColorSurfaces.end(); ++iter)
  43. {
  44. if (*iter != nullptr)
  45. Texture::releaseView(*iter);
  46. }
  47. if (mDepthStencilSurface != nullptr)
  48. Texture::releaseView(mDepthStencilSurface);
  49. }
  50. void MultiRenderTextureCore::throwIfBuffersDontMatch() const
  51. {
  52. TextureViewPtr firstSurfaceDesc = nullptr;
  53. for(size_t i = 0; i < mColorSurfaces.size(); i++)
  54. {
  55. if(mColorSurfaces[i] == nullptr)
  56. continue;
  57. if(firstSurfaceDesc == nullptr)
  58. {
  59. firstSurfaceDesc = mColorSurfaces[i];
  60. continue;
  61. }
  62. if(mColorSurfaces[i]->getTexture()->getWidth() != firstSurfaceDesc->getTexture()->getWidth() ||
  63. mColorSurfaces[i]->getTexture()->getHeight() != firstSurfaceDesc->getTexture()->getHeight() ||
  64. mColorSurfaces[i]->getTexture()->getMultisampleCount() != firstSurfaceDesc->getTexture()->getMultisampleCount())
  65. {
  66. String errorInfo = "\nWidth: " + toString(mColorSurfaces[i]->getTexture()->getWidth()) + "/" + toString(firstSurfaceDesc->getTexture()->getWidth());
  67. errorInfo += "\nHeight: " + toString(mColorSurfaces[i]->getTexture()->getHeight()) + "/" + toString(firstSurfaceDesc->getTexture()->getHeight());
  68. errorInfo += "\nMultisample Count: " + toString(mColorSurfaces[i]->getTexture()->getMultisampleCount()) + "/" + toString(firstSurfaceDesc->getTexture()->getMultisampleCount());
  69. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  70. }
  71. }
  72. if(firstSurfaceDesc == nullptr)
  73. return;
  74. if(firstSurfaceDesc->getTexture()->getTextureType() != TEX_TYPE_2D)
  75. BS_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  76. if((firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) >= firstSurfaceDesc->getTexture()->getNumFaces())
  77. {
  78. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  79. toString(firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) + ". Max num faces: " + toString(firstSurfaceDesc->getTexture()->getNumFaces()));
  80. }
  81. if(firstSurfaceDesc->getMostDetailedMip() >= firstSurfaceDesc->getTexture()->getNumMipmaps())
  82. {
  83. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  84. toString(firstSurfaceDesc->getMostDetailedMip()) + ". Max num mipmaps: " + toString(firstSurfaceDesc->getTexture()->getNumMipmaps()));
  85. }
  86. if(mDepthStencilSurface == nullptr)
  87. return;
  88. if(mDepthStencilSurface->getTexture()->getWidth() != firstSurfaceDesc->getTexture()->getWidth() ||
  89. mDepthStencilSurface->getTexture()->getHeight() != firstSurfaceDesc->getTexture()->getHeight() ||
  90. mDepthStencilSurface->getTexture()->getMultisampleCount() != firstSurfaceDesc->getTexture()->getMultisampleCount())
  91. {
  92. String errorInfo = "\nWidth: " + toString(mDepthStencilSurface->getTexture()->getWidth()) + "/" + toString(firstSurfaceDesc->getTexture()->getWidth());
  93. errorInfo += "\nHeight: " + toString(mDepthStencilSurface->getTexture()->getHeight()) + "/" + toString(firstSurfaceDesc->getTexture()->getHeight());
  94. errorInfo += "\nMultisample Count: " + toString(mDepthStencilSurface->getTexture()->getMultisampleCount()) + "/" + toString(firstSurfaceDesc->getTexture()->getMultisampleCount());
  95. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  96. }
  97. }
  98. void MultiRenderTextureCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  99. {
  100. throw std::exception("The method or operation is not implemented.");
  101. }
  102. MultiRenderTexture* MultiRenderTextureCore::getNonCore() const
  103. {
  104. return static_cast<MultiRenderTexture*>(mParent);
  105. }
  106. void MultiRenderTexture::initialize(const MULTI_RENDER_TEXTURE_DESC& desc)
  107. {
  108. mDesc = desc;
  109. mProperties = createProperties();
  110. MultiRenderTextureProperties* properties = static_cast<MultiRenderTextureProperties*>(mProperties);
  111. for (size_t i = 0; i < desc.colorSurfaces.size(); i++)
  112. {
  113. TexturePtr texture = desc.colorSurfaces[i].texture;
  114. if (texture != nullptr)
  115. {
  116. properties->mWidth = texture->getWidth();
  117. properties->mHeight = texture->getWidth();
  118. properties->mColorDepth = BansheeEngine::PixelUtil::getNumElemBits(texture->getFormat());
  119. properties->mActive = true;
  120. properties->mHwGamma = texture->isHardwareGammaEnabled();
  121. properties->mMultisampleCount = texture->getMultisampleCount();
  122. properties->mIsWindow = false;
  123. properties->mRequiresTextureFlipping = requiresTextureFlipping();
  124. break;
  125. }
  126. }
  127. RenderTarget::initialize();
  128. }
  129. const MultiRenderTextureProperties& MultiRenderTexture::getProperties() const
  130. {
  131. THROW_IF_CORE_THREAD;
  132. return static_cast<const MultiRenderTextureProperties&>(RenderTarget::getProperties());
  133. }
  134. MultiRenderTextureCore* MultiRenderTexture::getCore() const
  135. {
  136. return static_cast<MultiRenderTextureCore*>(mCore);
  137. }
  138. RenderTargetCore* MultiRenderTexture::createCore()
  139. {
  140. MultiRenderTextureProperties* coreProperties = bs_new<MultiRenderTextureProperties>();
  141. MultiRenderTextureProperties* myProperties = static_cast<MultiRenderTextureProperties*>(mProperties);
  142. *coreProperties = *myProperties;
  143. return createCore(coreProperties, mDesc);
  144. }
  145. MultiRenderTexturePtr MultiRenderTexture::create(const MULTI_RENDER_TEXTURE_DESC& desc)
  146. {
  147. return TextureManager::instance().createMultiRenderTexture(desc);
  148. }
  149. }