CmMultiRenderTexture.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "CmMultiRenderTexture.h"
  2. #include "CmTexture.h"
  3. #include "CmDepthStencilBuffer.h"
  4. #include "CmException.h"
  5. namespace CamelotEngine
  6. {
  7. MultiRenderTexture::MultiRenderTexture()
  8. {
  9. mSurfaces.resize(CM_MAX_MULTIPLE_RENDER_TARGETS);
  10. }
  11. void MultiRenderTexture::setColorSurface(UINT32 surfaceIdx, TexturePtr texture, UINT32 face, UINT32 numFaces, UINT32 mipLevel)
  12. {
  13. if(surfaceIdx < 0 || surfaceIdx >= CM_MAX_MULTIPLE_RENDER_TARGETS)
  14. CM_EXCEPT(InvalidParametersException, "Invalid surface index. 0 <= " + toString(surfaceIdx) + " < CM_MAX_MULTIPLE_RENDER_TARGETS");
  15. mSurfaces[surfaceIdx].texture = texture;
  16. if(texture == nullptr)
  17. {
  18. setColorSurfaceImpl(surfaceIdx, texture, face, numFaces, mipLevel);
  19. return;
  20. }
  21. mPriority = CM_REND_TO_TEX_RT_GROUP;
  22. mWidth = texture->getWidth();
  23. mHeight = texture->getWidth();
  24. mColorDepth = CamelotEngine::PixelUtil::getNumElemBits(texture->getFormat());
  25. mActive = true;
  26. mHwGamma = texture->isHardwareGammaEnabled();
  27. mFSAA = texture->getFSAA();
  28. mFSAAHint = texture->getFSAAHint();
  29. mSurfaces[surfaceIdx].format = texture->getFormat();
  30. mSurfaces[surfaceIdx].face = face;
  31. mSurfaces[surfaceIdx].numFaces = numFaces;
  32. mSurfaces[surfaceIdx].mipLevel = mipLevel;
  33. throwIfBuffersDontMatch();
  34. setColorSurfaceImpl(surfaceIdx, texture, face, numFaces, mipLevel);
  35. }
  36. void MultiRenderTexture::setDepthStencil(DepthStencilBufferPtr depthStencilBuffer)
  37. {
  38. mDepthStencilBuffer = depthStencilBuffer;
  39. throwIfBuffersDontMatch();
  40. setDepthStencilImpl(depthStencilBuffer);
  41. }
  42. void MultiRenderTexture::throwIfBuffersDontMatch() const
  43. {
  44. const SurfaceDesc* firstSurfaceDesc = nullptr;
  45. for(size_t i = 0; i < mSurfaces.size(); i++)
  46. {
  47. if(mSurfaces[i].texture == nullptr)
  48. continue;
  49. if(firstSurfaceDesc == nullptr)
  50. {
  51. firstSurfaceDesc = &mSurfaces[i];
  52. continue;
  53. }
  54. if(mSurfaces[i].texture->getWidth() != firstSurfaceDesc->texture->getWidth() ||
  55. mSurfaces[i].texture->getHeight() != firstSurfaceDesc->texture->getHeight() ||
  56. mSurfaces[i].texture->getFSAA() != firstSurfaceDesc->texture->getFSAA() ||
  57. mSurfaces[i].texture->getFSAAHint() != firstSurfaceDesc->texture->getFSAAHint())
  58. {
  59. String errorInfo = "\nWidth: " + toString(mSurfaces[i].texture->getWidth()) + "/" + toString(firstSurfaceDesc->texture->getWidth());
  60. errorInfo += "\nHeight: " + toString(mSurfaces[i].texture->getHeight()) + "/" + toString(firstSurfaceDesc->texture->getHeight());
  61. errorInfo += "\nFSAA: " + toString(mSurfaces[i].texture->getFSAA()) + "/" + toString(firstSurfaceDesc->texture->getFSAA());
  62. errorInfo += "\nFSAAHint: " + mSurfaces[i].texture->getFSAAHint() + "/" + firstSurfaceDesc->texture->getFSAAHint();
  63. CM_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  64. }
  65. }
  66. if(firstSurfaceDesc == nullptr)
  67. return;
  68. if(firstSurfaceDesc->texture->getTextureType() != TEX_TYPE_2D)
  69. CM_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  70. if((firstSurfaceDesc->face + firstSurfaceDesc->numFaces) >= firstSurfaceDesc->texture->getNumFaces())
  71. {
  72. CM_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  73. toString(firstSurfaceDesc->face + firstSurfaceDesc->numFaces) + ". Max num faces: " + toString(firstSurfaceDesc->texture->getNumFaces()));
  74. }
  75. if(firstSurfaceDesc->mipLevel >= firstSurfaceDesc->texture->getNumMipmaps())
  76. {
  77. CM_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  78. toString(firstSurfaceDesc->mipLevel) + ". Max num mipmaps: " + toString(firstSurfaceDesc->texture->getNumMipmaps()));
  79. }
  80. if(mDepthStencilBuffer == nullptr)
  81. return;
  82. if(mDepthStencilBuffer->getWidth() != firstSurfaceDesc->texture->getWidth() ||
  83. mDepthStencilBuffer->getHeight() != firstSurfaceDesc->texture->getHeight() ||
  84. mDepthStencilBuffer->getFsaa() != firstSurfaceDesc->texture->getFSAA() ||
  85. mDepthStencilBuffer->getFsaaHint() != firstSurfaceDesc->texture->getFSAAHint())
  86. {
  87. String errorInfo = "\nWidth: " + toString(mDepthStencilBuffer->getWidth()) + "/" + toString(firstSurfaceDesc->texture->getWidth());
  88. errorInfo += "\nHeight: " + toString(mDepthStencilBuffer->getHeight()) + "/" + toString(firstSurfaceDesc->texture->getHeight());
  89. errorInfo += "\nFSAA: " + toString(mDepthStencilBuffer->getFsaa()) + "/" + toString(firstSurfaceDesc->texture->getFSAA());
  90. errorInfo += "\nFSAAHint: " + mDepthStencilBuffer->getFsaaHint() + "/" + firstSurfaceDesc->texture->getFSAAHint();
  91. CM_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  92. }
  93. }
  94. void MultiRenderTexture::copyContentsToMemory( const PixelData &dst, FrameBuffer buffer /*= FB_AUTO */ )
  95. {
  96. throw std::exception("The method or operation is not implemented.");
  97. }
  98. }