BsRenderTexturePool.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsRenderTexturePool.h"
  4. #include "BsRenderTexture.h"
  5. #include "BsTexture.h"
  6. #include "BsTextureManager.h"
  7. namespace BansheeEngine
  8. {
  9. PooledRenderTexture::PooledRenderTexture(RenderTexturePool* pool)
  10. :mIsFree(false), mPool(pool)
  11. { }
  12. PooledRenderTexture::~PooledRenderTexture()
  13. {
  14. if (mPool != nullptr)
  15. mPool->_unregisterTexture(this);
  16. }
  17. RenderTexturePool::~RenderTexturePool()
  18. {
  19. for (auto& texture : mTextures)
  20. texture.second.lock()->mPool = nullptr;
  21. }
  22. SPtr<PooledRenderTexture> RenderTexturePool::get(PixelFormat format, UINT32 width, UINT32 height, bool hwGamma, UINT32 samples)
  23. {
  24. bool depth = PixelUtil::isDepth(format);
  25. for (auto& texturePair : mTextures)
  26. {
  27. SPtr<PooledRenderTexture> textureData = texturePair.second.lock();
  28. if (!textureData->mIsFree)
  29. continue;
  30. if (textureData->texture == nullptr)
  31. continue;
  32. if (matches(textureData->texture, format, width, height, hwGamma, samples))
  33. {
  34. textureData->mIsFree = false;
  35. return textureData;
  36. }
  37. }
  38. SPtr<PooledRenderTexture> newTextureData = bs_shared_ptr_new<PooledRenderTexture>(this);
  39. _registerTexture(newTextureData);
  40. newTextureData->texture = TextureCoreManager::instance().createTexture(TEX_TYPE_2D, width, height, 1, 0,
  41. format, depth ? TU_DEPTHSTENCIL : TU_RENDERTARGET, hwGamma, samples);
  42. return newTextureData;
  43. }
  44. void RenderTexturePool::release(const SPtr<PooledRenderTexture>& texture)
  45. {
  46. auto iterFind = mTextures.find(texture.get());
  47. iterFind->second.lock()->mIsFree = true;
  48. }
  49. bool RenderTexturePool::matches(const SPtr<TextureCore>& texture, PixelFormat format, UINT32 width, UINT32 height, bool hwGamma, UINT32 samples)
  50. {
  51. const TextureProperties& texProps = texture->getProperties();
  52. return texProps.getFormat() == format && texProps.getWidth() == width && texProps.getHeight() == height &&
  53. texProps.isHardwareGammaEnabled() == hwGamma && texProps.getMultisampleCount() == samples;
  54. }
  55. void RenderTexturePool::_registerTexture(const SPtr<PooledRenderTexture>& texture)
  56. {
  57. mTextures.insert(std::make_pair(texture.get(), texture));
  58. }
  59. void RenderTexturePool::_unregisterTexture(PooledRenderTexture* texture)
  60. {
  61. mTextures.erase(texture);
  62. }
  63. }