BsRenderTexturePool.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "BsRenderTexturePool.h"
  2. #include "BsRenderTexture.h"
  3. #include "BsTexture.h"
  4. #include "BsTextureManager.h"
  5. namespace BansheeEngine
  6. {
  7. PooledRenderTexture::PooledRenderTexture(RenderTexturePool* pool)
  8. :mIsFree(false), mPool(pool)
  9. { }
  10. PooledRenderTexture::~PooledRenderTexture()
  11. {
  12. if (mPool != nullptr)
  13. mPool->_unregisterTexture(this);
  14. }
  15. RenderTexturePool::~RenderTexturePool()
  16. {
  17. for (auto& texture : mTextures)
  18. texture.second.lock()->mPool = nullptr;
  19. }
  20. SPtr<PooledRenderTexture> RenderTexturePool::get(PixelFormat format, UINT32 width, UINT32 height, bool hwGamma, UINT32 samples)
  21. {
  22. bool depth = PixelUtil::isDepth(format);
  23. for (auto& texturePair : mTextures)
  24. {
  25. SPtr<PooledRenderTexture> textureData = texturePair.second.lock();
  26. if (!textureData->mIsFree)
  27. continue;
  28. SPtr<TextureCore> textureCore;
  29. if (!depth)
  30. textureCore = textureData->texture->getBindableColorTexture();
  31. else
  32. textureCore = textureData->texture->getBindableDepthStencilTexture();
  33. if (textureCore == nullptr)
  34. continue;
  35. if (matches(textureCore, format, width, height, hwGamma, samples))
  36. {
  37. textureData->mIsFree = false;
  38. return textureData;
  39. }
  40. }
  41. SPtr<PooledRenderTexture> newTextureData = bs_shared_ptr_new<PooledRenderTexture>(this);
  42. _registerTexture(newTextureData);
  43. RENDER_SURFACE_CORE_DESC surfaceDesc;
  44. surfaceDesc.texture = TextureCoreManager::instance().createTexture(TEX_TYPE_2D, width, height, 1, 0,
  45. format, depth ? TU_DEPTHSTENCIL : TU_RENDERTARGET, hwGamma, samples);
  46. surfaceDesc.face = 0;
  47. surfaceDesc.mipLevel = 0;
  48. RENDER_TEXTURE_CORE_DESC desc;
  49. if (!depth)
  50. desc.colorSurface = surfaceDesc;
  51. else
  52. desc.depthStencilSurface = surfaceDesc;
  53. newTextureData->texture = TextureCoreManager::instance().createRenderTexture(desc);
  54. return newTextureData;
  55. }
  56. void RenderTexturePool::release(const SPtr<PooledRenderTexture>& texture)
  57. {
  58. auto iterFind = mTextures.find(texture.get());
  59. iterFind->second.lock()->mIsFree = true;
  60. }
  61. bool RenderTexturePool::matches(const SPtr<TextureCore>& texture, PixelFormat format, UINT32 width, UINT32 height, bool hwGamma, UINT32 samples)
  62. {
  63. const TextureProperties& texProps = texture->getProperties();
  64. return texProps.getFormat() == format && texProps.getWidth() == width && texProps.getHeight() == height &&
  65. texProps.isHardwareGammaEnabled() == hwGamma && texProps.getMultisampleCount() == samples;
  66. }
  67. void RenderTexturePool::_registerTexture(const SPtr<PooledRenderTexture>& texture)
  68. {
  69. mTextures.insert(std::make_pair(texture.get(), texture));
  70. }
  71. void RenderTexturePool::_unregisterTexture(PooledRenderTexture* texture)
  72. {
  73. mTextures.erase(texture);
  74. }
  75. }