CmMultiRenderTexture.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "CmMultiRenderTexture.h"
  2. #include "CmTexture.h"
  3. #include "CmException.h"
  4. #include "CmDebug.h"
  5. namespace BansheeEngine
  6. {
  7. MultiRenderTexture::MultiRenderTexture()
  8. {
  9. mColorSurfaces.resize(BS_MAX_MULTIPLE_RENDER_TARGETS);
  10. }
  11. MultiRenderTexture::~MultiRenderTexture()
  12. {
  13. }
  14. void MultiRenderTexture::initialize(const MULTI_RENDER_TEXTURE_DESC& desc)
  15. {
  16. bool colorSurfacePropertiesSet = false;
  17. for(size_t i = 0; i < desc.colorSurfaces.size(); i++)
  18. {
  19. if(desc.colorSurfaces[i].texture != nullptr)
  20. {
  21. if(i >= BS_MAX_MULTIPLE_RENDER_TARGETS)
  22. {
  23. LOGWRN("Render texture index is larger than the maximum number of supported render targets. Index: " + toString((int)i) +
  24. ". Max. number of render targets: " + toString(BS_MAX_MULTIPLE_RENDER_TARGETS));
  25. continue;
  26. }
  27. TexturePtr texture = desc.colorSurfaces[i].texture;
  28. if(texture->getUsage() != TU_RENDERTARGET)
  29. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  30. mColorSurfaces[i] = Texture::requestView(texture, desc.colorSurfaces[i].mipLevel, 1,
  31. desc.colorSurfaces[i].face, desc.colorSurfaces[i].numFaces, GVU_RENDERTARGET);
  32. if(!colorSurfacePropertiesSet)
  33. {
  34. mWidth = texture->getWidth();
  35. mHeight = texture->getWidth();
  36. mColorDepth = BansheeEngine::PixelUtil::getNumElemBits(texture->getFormat());
  37. mActive = true;
  38. mHwGamma = texture->isHardwareGammaEnabled();
  39. mMultisampleCount = texture->getMultisampleCount();
  40. mMultisampleHint = texture->getMultisampleHint();
  41. colorSurfacePropertiesSet = true;
  42. }
  43. }
  44. }
  45. if(desc.depthStencilSurface.texture != nullptr)
  46. {
  47. TexturePtr texture = desc.depthStencilSurface.texture;
  48. if(texture->getUsage() != TU_DEPTHSTENCIL)
  49. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  50. mDepthStencilSurface = Texture::requestView(texture, desc.depthStencilSurface.mipLevel, 1,
  51. desc.depthStencilSurface.face, desc.depthStencilSurface.numFaces, GVU_DEPTHSTENCIL);
  52. }
  53. throwIfBuffersDontMatch();
  54. RenderTarget::initialize();
  55. }
  56. void MultiRenderTexture::destroy_internal()
  57. {
  58. for(auto iter = mColorSurfaces.begin(); iter != mColorSurfaces.end(); ++iter)
  59. {
  60. if(*iter != nullptr)
  61. Texture::releaseView(*iter);
  62. }
  63. if(mDepthStencilSurface != nullptr)
  64. Texture::releaseView(mDepthStencilSurface);
  65. RenderTarget::destroy_internal();
  66. }
  67. void MultiRenderTexture::throwIfBuffersDontMatch() const
  68. {
  69. TextureViewPtr firstSurfaceDesc = nullptr;
  70. for(size_t i = 0; i < mColorSurfaces.size(); i++)
  71. {
  72. if(mColorSurfaces[i] == nullptr)
  73. continue;
  74. if(firstSurfaceDesc == nullptr)
  75. {
  76. firstSurfaceDesc = mColorSurfaces[i];
  77. continue;
  78. }
  79. if(mColorSurfaces[i]->getTexture()->getWidth() != firstSurfaceDesc->getTexture()->getWidth() ||
  80. mColorSurfaces[i]->getTexture()->getHeight() != firstSurfaceDesc->getTexture()->getHeight() ||
  81. mColorSurfaces[i]->getTexture()->getMultisampleCount() != firstSurfaceDesc->getTexture()->getMultisampleCount() ||
  82. mColorSurfaces[i]->getTexture()->getMultisampleHint() != firstSurfaceDesc->getTexture()->getMultisampleHint())
  83. {
  84. String errorInfo = "\nWidth: " + toString(mColorSurfaces[i]->getTexture()->getWidth()) + "/" + toString(firstSurfaceDesc->getTexture()->getWidth());
  85. errorInfo += "\nHeight: " + toString(mColorSurfaces[i]->getTexture()->getHeight()) + "/" + toString(firstSurfaceDesc->getTexture()->getHeight());
  86. errorInfo += "\nMultisample Count: " + toString(mColorSurfaces[i]->getTexture()->getMultisampleCount()) + "/" + toString(firstSurfaceDesc->getTexture()->getMultisampleCount());
  87. errorInfo += "\nMultisample Hint: " + mColorSurfaces[i]->getTexture()->getMultisampleHint() + "/" + firstSurfaceDesc->getTexture()->getMultisampleHint();
  88. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  89. }
  90. }
  91. if(firstSurfaceDesc == nullptr)
  92. return;
  93. if(firstSurfaceDesc->getTexture()->getTextureType() != TEX_TYPE_2D)
  94. BS_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  95. if((firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) >= firstSurfaceDesc->getTexture()->getNumFaces())
  96. {
  97. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  98. toString(firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) + ". Max num faces: " + toString(firstSurfaceDesc->getTexture()->getNumFaces()));
  99. }
  100. if(firstSurfaceDesc->getMostDetailedMip() >= firstSurfaceDesc->getTexture()->getNumMipmaps())
  101. {
  102. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  103. toString(firstSurfaceDesc->getMostDetailedMip()) + ". Max num mipmaps: " + toString(firstSurfaceDesc->getTexture()->getNumMipmaps()));
  104. }
  105. if(mDepthStencilSurface == nullptr)
  106. return;
  107. if(mDepthStencilSurface->getTexture()->getWidth() != firstSurfaceDesc->getTexture()->getWidth() ||
  108. mDepthStencilSurface->getTexture()->getHeight() != firstSurfaceDesc->getTexture()->getHeight() ||
  109. mDepthStencilSurface->getTexture()->getMultisampleCount() != firstSurfaceDesc->getTexture()->getMultisampleCount() ||
  110. mDepthStencilSurface->getTexture()->getMultisampleHint() != firstSurfaceDesc->getTexture()->getMultisampleHint())
  111. {
  112. String errorInfo = "\nWidth: " + toString(mDepthStencilSurface->getTexture()->getWidth()) + "/" + toString(firstSurfaceDesc->getTexture()->getWidth());
  113. errorInfo += "\nHeight: " + toString(mDepthStencilSurface->getTexture()->getHeight()) + "/" + toString(firstSurfaceDesc->getTexture()->getHeight());
  114. errorInfo += "\nMultisample Count: " + toString(mDepthStencilSurface->getTexture()->getMultisampleCount()) + "/" + toString(firstSurfaceDesc->getTexture()->getMultisampleCount());
  115. errorInfo += "\nMultisample Hint: " + mDepthStencilSurface->getTexture()->getMultisampleHint() + "/" + firstSurfaceDesc->getTexture()->getMultisampleHint();
  116. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  117. }
  118. }
  119. void MultiRenderTexture::copyToMemory(const PixelData &dst, FrameBuffer buffer)
  120. {
  121. throw std::exception("The method or operation is not implemented.");
  122. }
  123. }