CmMultiRenderTexture.cpp 6.2 KB

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