BsMultiRenderTexture.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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, desc.colorSurfaces[i].numFaces, 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, desc.depthStencilSurface.numFaces, 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. mColorSurfaces[i]->getTexture()->getMultisampleHint() != firstSurfaceDesc->getTexture()->getMultisampleHint())
  66. {
  67. String errorInfo = "\nWidth: " + toString(mColorSurfaces[i]->getTexture()->getWidth()) + "/" + toString(firstSurfaceDesc->getTexture()->getWidth());
  68. errorInfo += "\nHeight: " + toString(mColorSurfaces[i]->getTexture()->getHeight()) + "/" + toString(firstSurfaceDesc->getTexture()->getHeight());
  69. errorInfo += "\nMultisample Count: " + toString(mColorSurfaces[i]->getTexture()->getMultisampleCount()) + "/" + toString(firstSurfaceDesc->getTexture()->getMultisampleCount());
  70. errorInfo += "\nMultisample Hint: " + mColorSurfaces[i]->getTexture()->getMultisampleHint() + "/" + firstSurfaceDesc->getTexture()->getMultisampleHint();
  71. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  72. }
  73. }
  74. if(firstSurfaceDesc == nullptr)
  75. return;
  76. if(firstSurfaceDesc->getTexture()->getTextureType() != TEX_TYPE_2D)
  77. BS_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  78. if((firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) >= firstSurfaceDesc->getTexture()->getNumFaces())
  79. {
  80. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  81. toString(firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) + ". Max num faces: " + toString(firstSurfaceDesc->getTexture()->getNumFaces()));
  82. }
  83. if(firstSurfaceDesc->getMostDetailedMip() >= firstSurfaceDesc->getTexture()->getNumMipmaps())
  84. {
  85. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  86. toString(firstSurfaceDesc->getMostDetailedMip()) + ". Max num mipmaps: " + toString(firstSurfaceDesc->getTexture()->getNumMipmaps()));
  87. }
  88. if(mDepthStencilSurface == nullptr)
  89. return;
  90. if(mDepthStencilSurface->getTexture()->getWidth() != firstSurfaceDesc->getTexture()->getWidth() ||
  91. mDepthStencilSurface->getTexture()->getHeight() != firstSurfaceDesc->getTexture()->getHeight() ||
  92. mDepthStencilSurface->getTexture()->getMultisampleCount() != firstSurfaceDesc->getTexture()->getMultisampleCount() ||
  93. mDepthStencilSurface->getTexture()->getMultisampleHint() != firstSurfaceDesc->getTexture()->getMultisampleHint())
  94. {
  95. String errorInfo = "\nWidth: " + toString(mDepthStencilSurface->getTexture()->getWidth()) + "/" + toString(firstSurfaceDesc->getTexture()->getWidth());
  96. errorInfo += "\nHeight: " + toString(mDepthStencilSurface->getTexture()->getHeight()) + "/" + toString(firstSurfaceDesc->getTexture()->getHeight());
  97. errorInfo += "\nMultisample Count: " + toString(mDepthStencilSurface->getTexture()->getMultisampleCount()) + "/" + toString(firstSurfaceDesc->getTexture()->getMultisampleCount());
  98. errorInfo += "\nMultisample Hint: " + mDepthStencilSurface->getTexture()->getMultisampleHint() + "/" + firstSurfaceDesc->getTexture()->getMultisampleHint();
  99. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  100. }
  101. }
  102. void MultiRenderTextureCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  103. {
  104. throw std::exception("The method or operation is not implemented.");
  105. }
  106. MultiRenderTexture* MultiRenderTextureCore::getNonCore() const
  107. {
  108. return static_cast<MultiRenderTexture*>(mParent);
  109. }
  110. void MultiRenderTexture::initialize(const MULTI_RENDER_TEXTURE_DESC& desc)
  111. {
  112. mDesc = desc;
  113. mProperties = createProperties();
  114. MultiRenderTextureProperties* properties = static_cast<MultiRenderTextureProperties*>(mProperties);
  115. for (size_t i = 0; i < desc.colorSurfaces.size(); i++)
  116. {
  117. TexturePtr texture = desc.colorSurfaces[i].texture;
  118. if (texture != nullptr)
  119. {
  120. properties->mWidth = texture->getWidth();
  121. properties->mHeight = texture->getWidth();
  122. properties->mColorDepth = BansheeEngine::PixelUtil::getNumElemBits(texture->getFormat());
  123. properties->mActive = true;
  124. properties->mHwGamma = texture->isHardwareGammaEnabled();
  125. properties->mMultisampleCount = texture->getMultisampleCount();
  126. properties->mMultisampleHint = texture->getMultisampleHint();
  127. properties->mIsWindow = false;
  128. properties->mRequiresTextureFlipping = requiresTextureFlipping();
  129. break;
  130. }
  131. }
  132. RenderTarget::initialize();
  133. }
  134. const MultiRenderTextureProperties& MultiRenderTexture::getProperties() const
  135. {
  136. THROW_IF_CORE_THREAD;
  137. return static_cast<const MultiRenderTextureProperties&>(RenderTarget::getProperties());
  138. }
  139. MultiRenderTextureCore* MultiRenderTexture::getCore() const
  140. {
  141. return static_cast<MultiRenderTextureCore*>(mCore);
  142. }
  143. RenderTargetCore* MultiRenderTexture::createCore()
  144. {
  145. MultiRenderTextureProperties* coreProperties = bs_new<MultiRenderTextureProperties>();
  146. MultiRenderTextureProperties* myProperties = static_cast<MultiRenderTextureProperties*>(mProperties);
  147. *coreProperties = *myProperties;
  148. return createCore(coreProperties, mDesc);
  149. }
  150. MultiRenderTexturePtr MultiRenderTexture::create(const MULTI_RENDER_TEXTURE_DESC& desc)
  151. {
  152. return TextureManager::instance().createMultiRenderTexture(desc);
  153. }
  154. }