CmRenderTexture.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "CmRenderTexture.h"
  2. #include "CmException.h"
  3. #include "CmPixelBuffer.h"
  4. #include "CmTexture.h"
  5. #include "CmTextureManager.h"
  6. #include "CmResources.h"
  7. namespace BansheeEngine
  8. {
  9. RenderTexture::RenderTexture()
  10. :mColorSurface(nullptr), mDepthStencilSurface(nullptr)
  11. {
  12. }
  13. RenderTexture::~RenderTexture()
  14. {
  15. }
  16. RenderTexturePtr RenderTexture::create(TextureType textureType, UINT32 width, UINT32 height,
  17. PixelFormat format, bool hwGamma, UINT32 fsaa, const String& fsaaHint,
  18. bool createDepth, PixelFormat depthStencilFormat)
  19. {
  20. return TextureManager::instance().createRenderTexture(textureType, width, height, format, hwGamma, fsaa, fsaaHint, createDepth, depthStencilFormat);
  21. }
  22. void RenderTexture::destroy_internal()
  23. {
  24. if(mColorSurface != nullptr)
  25. Texture::releaseView(mColorSurface);
  26. if(mDepthStencilSurface != nullptr)
  27. Texture::releaseView(mDepthStencilSurface);
  28. RenderTarget::destroy_internal();
  29. }
  30. void RenderTexture::initialize(const RENDER_TEXTURE_DESC& desc)
  31. {
  32. if(desc.colorSurface.texture != nullptr)
  33. {
  34. TexturePtr texture = desc.colorSurface.texture;
  35. if(texture->getUsage() != TU_RENDERTARGET)
  36. CM_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  37. mColorSurface = Texture::requestView(texture, desc.colorSurface.mipLevel, 1,
  38. desc.colorSurface.face, desc.colorSurface.numFaces, GVU_RENDERTARGET);
  39. mWidth = texture->getWidth();
  40. mHeight = texture->getHeight();
  41. mColorDepth = BansheeEngine::PixelUtil::getNumElemBits(texture->getFormat());
  42. mActive = true;
  43. mHwGamma = texture->isHardwareGammaEnabled();
  44. mFSAA = texture->getFSAA();
  45. mFSAAHint = texture->getFSAAHint();
  46. }
  47. if(desc.depthStencilSurface.texture != nullptr)
  48. {
  49. TexturePtr texture = desc.depthStencilSurface.texture;
  50. if(texture->getUsage() != TU_DEPTHSTENCIL)
  51. CM_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  52. mDepthStencilSurface = Texture::requestView(texture, desc.depthStencilSurface.mipLevel, 1,
  53. desc.depthStencilSurface.face, desc.depthStencilSurface.numFaces, GVU_DEPTHSTENCIL);
  54. }
  55. throwIfBuffersDontMatch();
  56. assert(mColorSurface != nullptr);
  57. assert(mColorSurface->getTexture() != nullptr);
  58. if(mColorSurface->getTexture()->getTextureType() != TEX_TYPE_2D)
  59. CM_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  60. if((mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) > mColorSurface->getTexture()->getNumFaces())
  61. {
  62. CM_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  63. toString(mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) +
  64. ". Max num faces: " + toString(mColorSurface->getTexture()->getNumFaces()));
  65. }
  66. if(mColorSurface->getMostDetailedMip() > mColorSurface->getTexture()->getNumMipmaps())
  67. {
  68. CM_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  69. toString(mColorSurface->getMostDetailedMip()) + ". Max num mipmaps: " + toString(mColorSurface->getTexture()->getNumMipmaps()));
  70. }
  71. RenderTarget::initialize();
  72. // Create non-persistent resource handles for the used textures (we only need them because a lot of the code accepts only handles,
  73. // since they're non persistent they don't really have any benefit over shared pointers)
  74. if(mColorSurface != nullptr)
  75. mBindableColorTex = gResources()._createResourceHandle(mColorSurface->getTexture());
  76. if(mDepthStencilSurface != nullptr)
  77. mBindableDepthStencilTex = gResources()._createResourceHandle(mDepthStencilSurface->getTexture());
  78. }
  79. void RenderTexture::throwIfBuffersDontMatch() const
  80. {
  81. if(mColorSurface == nullptr || mDepthStencilSurface == nullptr)
  82. return;
  83. if(mColorSurface->getTexture()->getWidth() != mDepthStencilSurface->getTexture()->getWidth() ||
  84. mColorSurface->getTexture()->getHeight() != mDepthStencilSurface->getTexture()->getHeight() ||
  85. mColorSurface->getTexture()->getFSAA() != mDepthStencilSurface->getTexture()->getFSAA() ||
  86. mColorSurface->getTexture()->getFSAAHint() != mDepthStencilSurface->getTexture()->getFSAAHint())
  87. {
  88. String errorInfo = "\nWidth: " + toString(mColorSurface->getTexture()->getWidth()) + "/" + toString(mDepthStencilSurface->getTexture()->getWidth());
  89. errorInfo += "\nHeight: " + toString(mColorSurface->getTexture()->getHeight()) + "/" + toString(mDepthStencilSurface->getTexture()->getHeight());
  90. errorInfo += "\nFSAA: " + toString(mColorSurface->getTexture()->getFSAA()) + "/" + toString(mDepthStencilSurface->getTexture()->getFSAA());
  91. errorInfo += "\nFSAAHint: " + mColorSurface->getTexture()->getFSAAHint() + "/" + mDepthStencilSurface->getTexture()->getFSAAHint();
  92. CM_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  93. }
  94. }
  95. void RenderTexture::copyToMemory( const PixelData &dst, FrameBuffer buffer /*= FB_AUTO */ )
  96. {
  97. throw std::exception("The method or operation is not implemented.");
  98. }
  99. }