BsMultiRenderTexture.cpp 6.6 KB

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