CmMultiRenderTexture.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "CmMultiRenderTexture.h"
  2. #include "CmTexture.h"
  3. #include "CmException.h"
  4. namespace CamelotEngine
  5. {
  6. MultiRenderTexture::MultiRenderTexture()
  7. {
  8. mColorSurfaces.resize(CM_MAX_MULTIPLE_RENDER_TARGETS);
  9. }
  10. MultiRenderTexture::~MultiRenderTexture()
  11. {
  12. }
  13. void MultiRenderTexture::destroy_internal()
  14. {
  15. for(auto iter = mColorSurfaces.begin(); iter != mColorSurfaces.end(); ++iter)
  16. {
  17. if(*iter != nullptr)
  18. Texture::releaseView(*iter);
  19. }
  20. if(mDepthStencilSurface != nullptr)
  21. Texture::releaseView(mDepthStencilSurface);
  22. RenderTarget::destroy_internal();
  23. }
  24. void MultiRenderTexture::setColorSurface(UINT32 surfaceIdx, TexturePtr texture, UINT32 face, UINT32 numFaces, UINT32 mipLevel)
  25. {
  26. if(surfaceIdx < 0 || surfaceIdx >= CM_MAX_MULTIPLE_RENDER_TARGETS)
  27. CM_EXCEPT(InvalidParametersException, "Invalid surface index. 0 <= " + toString(surfaceIdx) + " < CM_MAX_MULTIPLE_RENDER_TARGETS");
  28. if(texture != nullptr && texture->getUsage() != TU_RENDERTARGET)
  29. CM_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  30. if(mColorSurfaces[surfaceIdx] != nullptr)
  31. {
  32. mColorSurfaces[surfaceIdx]->getTexture()->releaseView(mColorSurfaces[surfaceIdx]);
  33. mColorSurfaces[surfaceIdx] = nullptr;
  34. }
  35. if(texture == nullptr)
  36. {
  37. setColorSurfaceImpl(surfaceIdx, texture, face, numFaces, mipLevel);
  38. return;
  39. }
  40. mColorSurfaces[surfaceIdx] = Texture::requestView(texture, mipLevel, 1, face, numFaces, GVU_RENDERTARGET);
  41. mPriority = CM_REND_TO_TEX_RT_GROUP;
  42. mWidth = texture->getWidth();
  43. mHeight = texture->getWidth();
  44. mColorDepth = CamelotEngine::PixelUtil::getNumElemBits(texture->getFormat());
  45. mActive = true;
  46. mHwGamma = texture->isHardwareGammaEnabled();
  47. mFSAA = texture->getFSAA();
  48. mFSAAHint = texture->getFSAAHint();
  49. throwIfBuffersDontMatch();
  50. setColorSurfaceImpl(surfaceIdx, texture, face, numFaces, mipLevel);
  51. }
  52. void MultiRenderTexture::setDepthStencilSurface(TexturePtr depthStencil, UINT32 face, UINT32 numFaces, UINT32 mipLevel)
  53. {
  54. if(depthStencil != nullptr && depthStencil->getUsage() != TU_DEPTHSTENCIL)
  55. CM_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  56. if(mDepthStencilSurface != nullptr)
  57. {
  58. Texture::releaseView(mDepthStencilSurface);
  59. mDepthStencilSurface = nullptr;
  60. }
  61. if(depthStencil == nullptr)
  62. return;
  63. mDepthStencilSurface = Texture::requestView(depthStencil, mipLevel, 1, face, numFaces, GVU_DEPTHSTENCIL);
  64. throwIfBuffersDontMatch();
  65. setDepthStencilImpl(depthStencil, face, numFaces, mipLevel);
  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()->getFSAA() != firstSurfaceDesc->getTexture()->getFSAA() ||
  82. mColorSurfaces[i]->getTexture()->getFSAAHint() != firstSurfaceDesc->getTexture()->getFSAAHint())
  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 += "\nFSAA: " + toString(mColorSurfaces[i]->getTexture()->getFSAA()) + "/" + toString(firstSurfaceDesc->getTexture()->getFSAA());
  87. errorInfo += "\nFSAAHint: " + mColorSurfaces[i]->getTexture()->getFSAAHint() + "/" + firstSurfaceDesc->getTexture()->getFSAAHint();
  88. CM_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. CM_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  95. if((firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) >= firstSurfaceDesc->getTexture()->getNumFaces())
  96. {
  97. CM_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. CM_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()->getFSAA() != firstSurfaceDesc->getTexture()->getFSAA() ||
  110. mDepthStencilSurface->getTexture()->getFSAAHint() != firstSurfaceDesc->getTexture()->getFSAAHint())
  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 += "\nFSAA: " + toString(mDepthStencilSurface->getTexture()->getFSAA()) + "/" + toString(firstSurfaceDesc->getTexture()->getFSAA());
  115. errorInfo += "\nFSAAHint: " + mDepthStencilSurface->getTexture()->getFSAAHint() + "/" + firstSurfaceDesc->getTexture()->getFSAAHint();
  116. CM_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  117. }
  118. }
  119. void MultiRenderTexture::copyContentsToMemory( const PixelData &dst, FrameBuffer buffer /*= FB_AUTO */ )
  120. {
  121. throw std::exception("The method or operation is not implemented.");
  122. }
  123. }