CmMultiRenderTexture.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. RenderTarget::destroy_internal();
  16. }
  17. void MultiRenderTexture::setColorSurface(UINT32 surfaceIdx, TexturePtr texture, UINT32 face, UINT32 numFaces, UINT32 mipLevel)
  18. {
  19. if(surfaceIdx < 0 || surfaceIdx >= CM_MAX_MULTIPLE_RENDER_TARGETS)
  20. CM_EXCEPT(InvalidParametersException, "Invalid surface index. 0 <= " + toString(surfaceIdx) + " < CM_MAX_MULTIPLE_RENDER_TARGETS");
  21. if(texture != nullptr && texture->getUsage() != TU_RENDERTARGET)
  22. CM_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  23. if(mColorSurfaces[surfaceIdx] != nullptr)
  24. {
  25. mColorSurfaces[surfaceIdx]->getTexture()->releaseView(mColorSurfaces[surfaceIdx]);
  26. mColorSurfaces[surfaceIdx] = nullptr;
  27. }
  28. if(texture == nullptr)
  29. {
  30. setColorSurfaceImpl(surfaceIdx, texture, face, numFaces, mipLevel);
  31. return;
  32. }
  33. mColorSurfaces[surfaceIdx] = Texture::requestView(texture, mipLevel, 1, face, numFaces, GVU_RENDERTARGET);
  34. mPriority = CM_REND_TO_TEX_RT_GROUP;
  35. mWidth = texture->getWidth();
  36. mHeight = texture->getWidth();
  37. mColorDepth = CamelotEngine::PixelUtil::getNumElemBits(texture->getFormat());
  38. mActive = true;
  39. mHwGamma = texture->isHardwareGammaEnabled();
  40. mFSAA = texture->getFSAA();
  41. mFSAAHint = texture->getFSAAHint();
  42. throwIfBuffersDontMatch();
  43. setColorSurfaceImpl(surfaceIdx, texture, face, numFaces, mipLevel);
  44. }
  45. void MultiRenderTexture::setDepthStencilSurface(TexturePtr depthStencil, UINT32 face, UINT32 numFaces, UINT32 mipLevel)
  46. {
  47. if(depthStencil != nullptr && depthStencil->getUsage() != TU_DEPTHSTENCIL)
  48. CM_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  49. if(mDepthStencilSurface != nullptr)
  50. {
  51. Texture::releaseView(mDepthStencilSurface);
  52. mDepthStencilSurface = nullptr;
  53. }
  54. if(depthStencil == nullptr)
  55. return;
  56. mDepthStencilSurface = Texture::requestView(depthStencil, mipLevel, 1, face, numFaces, GVU_DEPTHSTENCIL);
  57. throwIfBuffersDontMatch();
  58. setDepthStencilImpl(depthStencil, face, numFaces, mipLevel);
  59. }
  60. void MultiRenderTexture::throwIfBuffersDontMatch() const
  61. {
  62. const TextureView* firstSurfaceDesc = nullptr;
  63. for(size_t i = 0; i < mColorSurfaces.size(); i++)
  64. {
  65. if(mColorSurfaces[i] == nullptr)
  66. continue;
  67. if(firstSurfaceDesc == nullptr)
  68. {
  69. firstSurfaceDesc = mColorSurfaces[i];
  70. continue;
  71. }
  72. if(mColorSurfaces[i]->getTexture()->getWidth() != firstSurfaceDesc->getTexture()->getWidth() ||
  73. mColorSurfaces[i]->getTexture()->getHeight() != firstSurfaceDesc->getTexture()->getHeight() ||
  74. mColorSurfaces[i]->getTexture()->getFSAA() != firstSurfaceDesc->getTexture()->getFSAA() ||
  75. mColorSurfaces[i]->getTexture()->getFSAAHint() != firstSurfaceDesc->getTexture()->getFSAAHint())
  76. {
  77. String errorInfo = "\nWidth: " + toString(mColorSurfaces[i]->getTexture()->getWidth()) + "/" + toString(firstSurfaceDesc->getTexture()->getWidth());
  78. errorInfo += "\nHeight: " + toString(mColorSurfaces[i]->getTexture()->getHeight()) + "/" + toString(firstSurfaceDesc->getTexture()->getHeight());
  79. errorInfo += "\nFSAA: " + toString(mColorSurfaces[i]->getTexture()->getFSAA()) + "/" + toString(firstSurfaceDesc->getTexture()->getFSAA());
  80. errorInfo += "\nFSAAHint: " + mColorSurfaces[i]->getTexture()->getFSAAHint() + "/" + firstSurfaceDesc->getTexture()->getFSAAHint();
  81. CM_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  82. }
  83. }
  84. if(firstSurfaceDesc == nullptr)
  85. return;
  86. if(firstSurfaceDesc->getTexture()->getTextureType() != TEX_TYPE_2D)
  87. CM_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  88. if((firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) >= firstSurfaceDesc->getTexture()->getNumFaces())
  89. {
  90. CM_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  91. toString(firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) + ". Max num faces: " + toString(firstSurfaceDesc->getTexture()->getNumFaces()));
  92. }
  93. if(firstSurfaceDesc->getMostDetailedMip() >= firstSurfaceDesc->getTexture()->getNumMipmaps())
  94. {
  95. CM_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  96. toString(firstSurfaceDesc->getMostDetailedMip()) + ". Max num mipmaps: " + toString(firstSurfaceDesc->getTexture()->getNumMipmaps()));
  97. }
  98. if(mDepthStencilSurface == nullptr)
  99. return;
  100. if(mDepthStencilSurface->getTexture()->getWidth() != firstSurfaceDesc->getTexture()->getWidth() ||
  101. mDepthStencilSurface->getTexture()->getHeight() != firstSurfaceDesc->getTexture()->getHeight() ||
  102. mDepthStencilSurface->getTexture()->getFSAA() != firstSurfaceDesc->getTexture()->getFSAA() ||
  103. mDepthStencilSurface->getTexture()->getFSAAHint() != firstSurfaceDesc->getTexture()->getFSAAHint())
  104. {
  105. String errorInfo = "\nWidth: " + toString(mDepthStencilSurface->getTexture()->getWidth()) + "/" + toString(firstSurfaceDesc->getTexture()->getWidth());
  106. errorInfo += "\nHeight: " + toString(mDepthStencilSurface->getTexture()->getHeight()) + "/" + toString(firstSurfaceDesc->getTexture()->getHeight());
  107. errorInfo += "\nFSAA: " + toString(mDepthStencilSurface->getTexture()->getFSAA()) + "/" + toString(firstSurfaceDesc->getTexture()->getFSAA());
  108. errorInfo += "\nFSAAHint: " + mDepthStencilSurface->getTexture()->getFSAAHint() + "/" + firstSurfaceDesc->getTexture()->getFSAAHint();
  109. CM_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  110. }
  111. }
  112. void MultiRenderTexture::copyContentsToMemory( const PixelData &dst, FrameBuffer buffer /*= FB_AUTO */ )
  113. {
  114. throw std::exception("The method or operation is not implemented.");
  115. }
  116. }