BsRenderTexturePool.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if (textureData->texture == nullptr)
  29. continue;
  30. if (matches(textureData->texture, format, width, height, hwGamma, samples))
  31. {
  32. textureData->mIsFree = false;
  33. return textureData;
  34. }
  35. }
  36. SPtr<PooledRenderTexture> newTextureData = bs_shared_ptr_new<PooledRenderTexture>(this);
  37. _registerTexture(newTextureData);
  38. newTextureData->texture = TextureCoreManager::instance().createTexture(TEX_TYPE_2D, width, height, 1, 0,
  39. format, depth ? TU_DEPTHSTENCIL : TU_RENDERTARGET, hwGamma, samples);
  40. return newTextureData;
  41. }
  42. void RenderTexturePool::release(const SPtr<PooledRenderTexture>& texture)
  43. {
  44. auto iterFind = mTextures.find(texture.get());
  45. iterFind->second.lock()->mIsFree = true;
  46. }
  47. bool RenderTexturePool::matches(const SPtr<TextureCore>& texture, PixelFormat format, UINT32 width, UINT32 height, bool hwGamma, UINT32 samples)
  48. {
  49. const TextureProperties& texProps = texture->getProperties();
  50. return texProps.getFormat() == format && texProps.getWidth() == width && texProps.getHeight() == height &&
  51. texProps.isHardwareGammaEnabled() == hwGamma && texProps.getMultisampleCount() == samples;
  52. }
  53. void RenderTexturePool::_registerTexture(const SPtr<PooledRenderTexture>& texture)
  54. {
  55. mTextures.insert(std::make_pair(texture.get(), texture));
  56. }
  57. void RenderTexturePool::_unregisterTexture(PooledRenderTexture* texture)
  58. {
  59. mTextures.erase(texture);
  60. }
  61. }