BsRenderTexturePool.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. :mPool(pool), mIsFree(false)
  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(const POOLED_RENDER_TEXTURE_DESC& desc)
  23. {
  24. for (auto& texturePair : mTextures)
  25. {
  26. SPtr<PooledRenderTexture> textureData = texturePair.second.lock();
  27. if (!textureData->mIsFree)
  28. continue;
  29. if (textureData->texture == nullptr)
  30. continue;
  31. if (matches(textureData->texture, desc))
  32. {
  33. textureData->mIsFree = false;
  34. return textureData;
  35. }
  36. }
  37. SPtr<PooledRenderTexture> newTextureData = bs_shared_ptr_new<PooledRenderTexture>(this);
  38. _registerTexture(newTextureData);
  39. newTextureData->texture = TextureCoreManager::instance().createTexture(desc.type, desc.width, desc.height,
  40. desc.depth, 0, desc.format, desc.flag, desc.hwGamma, desc.numSamples);
  41. if ((desc.flag & (TU_RENDERTARGET | TU_DEPTHSTENCIL)) != 0)
  42. {
  43. RENDER_TEXTURE_CORE_DESC rtDesc;
  44. if ((desc.flag & TU_RENDERTARGET) != 0)
  45. {
  46. rtDesc.colorSurface.texture = newTextureData->texture;
  47. rtDesc.colorSurface.face = 0;
  48. rtDesc.colorSurface.numFaces = desc.depth;
  49. rtDesc.colorSurface.mipLevel = 0;
  50. }
  51. if ((desc.flag & TU_DEPTHSTENCIL) != 0)
  52. {
  53. rtDesc.depthStencilSurface.texture = newTextureData->texture;
  54. rtDesc.depthStencilSurface.face = 0;
  55. rtDesc.depthStencilSurface.numFaces = 1;
  56. rtDesc.depthStencilSurface.mipLevel = 0;
  57. }
  58. newTextureData->renderTexture = TextureCoreManager::instance().createRenderTexture(rtDesc);
  59. }
  60. return newTextureData;
  61. }
  62. void RenderTexturePool::release(const SPtr<PooledRenderTexture>& texture)
  63. {
  64. auto iterFind = mTextures.find(texture.get());
  65. iterFind->second.lock()->mIsFree = true;
  66. }
  67. bool RenderTexturePool::matches(const SPtr<TextureCore>& texture, const POOLED_RENDER_TEXTURE_DESC& desc)
  68. {
  69. const TextureProperties& texProps = texture->getProperties();
  70. bool match = texProps.getTextureType() == desc.type
  71. && texProps.getFormat() == desc.format
  72. && texProps.getWidth() == desc.width
  73. && texProps.getHeight() == desc.height
  74. && (texProps.getUsage() & desc.flag) == desc.flag
  75. && (
  76. (desc.type == TEX_TYPE_2D
  77. && texProps.isHardwareGammaEnabled() == desc.hwGamma
  78. && texProps.getMultisampleCount() == desc.numSamples)
  79. || (desc.type == TEX_TYPE_3D
  80. && texProps.getDepth() == desc.depth)
  81. || (desc.type == TEX_TYPE_CUBE_MAP)
  82. )
  83. ;
  84. return match;
  85. }
  86. void RenderTexturePool::_registerTexture(const SPtr<PooledRenderTexture>& texture)
  87. {
  88. mTextures.insert(std::make_pair(texture.get(), texture));
  89. }
  90. void RenderTexturePool::_unregisterTexture(PooledRenderTexture* texture)
  91. {
  92. mTextures.erase(texture);
  93. }
  94. POOLED_RENDER_TEXTURE_DESC POOLED_RENDER_TEXTURE_DESC::create2D(PixelFormat format, UINT32 width, UINT32 height,
  95. INT32 usage, UINT32 samples, bool hwGamma)
  96. {
  97. POOLED_RENDER_TEXTURE_DESC desc;
  98. desc.width = width;
  99. desc.height = height;
  100. desc.depth = 1;
  101. desc.format = format;
  102. desc.numSamples = samples;
  103. desc.flag = (TextureUsage)usage;
  104. desc.hwGamma = hwGamma;
  105. desc.type = TEX_TYPE_2D;
  106. return desc;
  107. }
  108. POOLED_RENDER_TEXTURE_DESC POOLED_RENDER_TEXTURE_DESC::create3D(PixelFormat format, UINT32 width, UINT32 height,
  109. UINT32 depth, INT32 usage)
  110. {
  111. POOLED_RENDER_TEXTURE_DESC desc;
  112. desc.width = width;
  113. desc.height = height;
  114. desc.depth = depth;
  115. desc.format = format;
  116. desc.numSamples = 1;
  117. desc.flag = (TextureUsage)usage;
  118. desc.hwGamma = false;
  119. desc.type = TEX_TYPE_3D;
  120. return desc;
  121. }
  122. POOLED_RENDER_TEXTURE_DESC POOLED_RENDER_TEXTURE_DESC::createCube(PixelFormat format, UINT32 width, UINT32 height,
  123. INT32 usage)
  124. {
  125. POOLED_RENDER_TEXTURE_DESC desc;
  126. desc.width = width;
  127. desc.height = height;
  128. desc.depth = 1;
  129. desc.format = format;
  130. desc.numSamples = 1;
  131. desc.flag = (TextureUsage)usage;
  132. desc.hwGamma = false;
  133. desc.type = TEX_TYPE_CUBE_MAP;
  134. return desc;
  135. }
  136. }