CmMultiRenderTexture.cpp 6.0 KB

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