BsRenderTexture.cpp 5.5 KB

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