BsRenderTexture.cpp 5.2 KB

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