BsMultiRenderTexture.cpp 7.5 KB

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