BsRenderTexturePool.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 bs { namespace ct
  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. TEXTURE_DESC texDesc;
  40. texDesc.type = desc.type;
  41. texDesc.width = desc.width;
  42. texDesc.height = desc.height;
  43. texDesc.depth = desc.depth;
  44. texDesc.format = desc.format;
  45. texDesc.usage = desc.flag;
  46. texDesc.hwGamma = desc.hwGamma;
  47. texDesc.numSamples = desc.numSamples;
  48. newTextureData->texture = TextureManager::instance().createTexture(texDesc);
  49. if ((desc.flag & (TU_RENDERTARGET | TU_DEPTHSTENCIL)) != 0)
  50. {
  51. RENDER_TEXTURE_DESC rtDesc;
  52. if ((desc.flag & TU_RENDERTARGET) != 0)
  53. {
  54. rtDesc.colorSurfaces[0].texture = newTextureData->texture;
  55. rtDesc.colorSurfaces[0].face = 0;
  56. rtDesc.colorSurfaces[0].numFaces = 1;
  57. rtDesc.colorSurfaces[0].mipLevel = 0;
  58. }
  59. if ((desc.flag & TU_DEPTHSTENCIL) != 0)
  60. {
  61. rtDesc.depthStencilSurface.texture = newTextureData->texture;
  62. rtDesc.depthStencilSurface.face = 0;
  63. rtDesc.depthStencilSurface.numFaces = 1;
  64. rtDesc.depthStencilSurface.mipLevel = 0;
  65. }
  66. newTextureData->renderTexture = TextureManager::instance().createRenderTexture(rtDesc);
  67. }
  68. return newTextureData;
  69. }
  70. void RenderTexturePool::release(const SPtr<PooledRenderTexture>& texture)
  71. {
  72. auto iterFind = mTextures.find(texture.get());
  73. iterFind->second.lock()->mIsFree = true;
  74. }
  75. bool RenderTexturePool::matches(const SPtr<TextureCore>& texture, const POOLED_RENDER_TEXTURE_DESC& desc)
  76. {
  77. const TextureProperties& texProps = texture->getProperties();
  78. bool match = texProps.getTextureType() == desc.type
  79. && texProps.getFormat() == desc.format
  80. && texProps.getWidth() == desc.width
  81. && texProps.getHeight() == desc.height
  82. && (texProps.getUsage() & desc.flag) == desc.flag
  83. && (
  84. (desc.type == TEX_TYPE_2D
  85. && texProps.isHardwareGammaEnabled() == desc.hwGamma
  86. && texProps.getNumSamples() == desc.numSamples)
  87. || (desc.type == TEX_TYPE_3D
  88. && texProps.getDepth() == desc.depth)
  89. || (desc.type == TEX_TYPE_CUBE_MAP)
  90. )
  91. ;
  92. return match;
  93. }
  94. void RenderTexturePool::_registerTexture(const SPtr<PooledRenderTexture>& texture)
  95. {
  96. mTextures.insert(std::make_pair(texture.get(), texture));
  97. }
  98. void RenderTexturePool::_unregisterTexture(PooledRenderTexture* texture)
  99. {
  100. mTextures.erase(texture);
  101. }
  102. POOLED_RENDER_TEXTURE_DESC POOLED_RENDER_TEXTURE_DESC::create2D(PixelFormat format, UINT32 width, UINT32 height,
  103. INT32 usage, UINT32 samples, bool hwGamma)
  104. {
  105. POOLED_RENDER_TEXTURE_DESC desc;
  106. desc.width = width;
  107. desc.height = height;
  108. desc.depth = 1;
  109. desc.format = format;
  110. desc.numSamples = samples;
  111. desc.flag = (TextureUsage)usage;
  112. desc.hwGamma = hwGamma;
  113. desc.type = TEX_TYPE_2D;
  114. return desc;
  115. }
  116. POOLED_RENDER_TEXTURE_DESC POOLED_RENDER_TEXTURE_DESC::create3D(PixelFormat format, UINT32 width, UINT32 height,
  117. UINT32 depth, INT32 usage)
  118. {
  119. POOLED_RENDER_TEXTURE_DESC desc;
  120. desc.width = width;
  121. desc.height = height;
  122. desc.depth = depth;
  123. desc.format = format;
  124. desc.numSamples = 1;
  125. desc.flag = (TextureUsage)usage;
  126. desc.hwGamma = false;
  127. desc.type = TEX_TYPE_3D;
  128. return desc;
  129. }
  130. POOLED_RENDER_TEXTURE_DESC POOLED_RENDER_TEXTURE_DESC::createCube(PixelFormat format, UINT32 width, UINT32 height,
  131. INT32 usage)
  132. {
  133. POOLED_RENDER_TEXTURE_DESC desc;
  134. desc.width = width;
  135. desc.height = height;
  136. desc.depth = 1;
  137. desc.format = format;
  138. desc.numSamples = 1;
  139. desc.flag = (TextureUsage)usage;
  140. desc.hwGamma = false;
  141. desc.type = TEX_TYPE_CUBE_MAP;
  142. return desc;
  143. }
  144. }}