BsGpuResourcePool.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGpuResourcePool.h"
  4. #include "BsRenderTexture.h"
  5. #include "BsTexture.h"
  6. #include "BsGpuBuffer.h"
  7. #include "BsTextureManager.h"
  8. namespace bs { namespace ct
  9. {
  10. PooledRenderTexture::PooledRenderTexture(GpuResourcePool* pool)
  11. :mPool(pool), mIsFree(false)
  12. { }
  13. PooledRenderTexture::~PooledRenderTexture()
  14. {
  15. if (mPool != nullptr)
  16. mPool->_unregisterTexture(this);
  17. }
  18. PooledStorageBuffer::PooledStorageBuffer(GpuResourcePool* pool)
  19. :mPool(pool), mIsFree(false)
  20. { }
  21. PooledStorageBuffer::~PooledStorageBuffer()
  22. {
  23. if (mPool != nullptr)
  24. mPool->_unregisterBuffer(this);
  25. }
  26. GpuResourcePool::~GpuResourcePool()
  27. {
  28. for (auto& texture : mTextures)
  29. texture.second.lock()->mPool = nullptr;
  30. for (auto& buffer : mBuffers)
  31. buffer.second.lock()->mPool = nullptr;
  32. }
  33. SPtr<PooledRenderTexture> GpuResourcePool::get(const POOLED_RENDER_TEXTURE_DESC& desc)
  34. {
  35. for (auto& texturePair : mTextures)
  36. {
  37. SPtr<PooledRenderTexture> textureData = texturePair.second.lock();
  38. if (!textureData->mIsFree)
  39. continue;
  40. if (textureData->texture == nullptr)
  41. continue;
  42. if (matches(textureData->texture, desc))
  43. {
  44. textureData->mIsFree = false;
  45. return textureData;
  46. }
  47. }
  48. SPtr<PooledRenderTexture> newTextureData = bs_shared_ptr_new<PooledRenderTexture>(this);
  49. _registerTexture(newTextureData);
  50. TEXTURE_DESC texDesc;
  51. texDesc.type = desc.type;
  52. texDesc.width = desc.width;
  53. texDesc.height = desc.height;
  54. texDesc.depth = desc.depth;
  55. texDesc.format = desc.format;
  56. texDesc.usage = desc.flag;
  57. texDesc.hwGamma = desc.hwGamma;
  58. texDesc.numSamples = desc.numSamples;
  59. newTextureData->texture = TextureManager::instance().createTexture(texDesc);
  60. if ((desc.flag & (TU_RENDERTARGET | TU_DEPTHSTENCIL)) != 0)
  61. {
  62. RENDER_TEXTURE_DESC rtDesc;
  63. if ((desc.flag & TU_RENDERTARGET) != 0)
  64. {
  65. rtDesc.colorSurfaces[0].texture = newTextureData->texture;
  66. rtDesc.colorSurfaces[0].face = 0;
  67. rtDesc.colorSurfaces[0].numFaces = 1;
  68. rtDesc.colorSurfaces[0].mipLevel = 0;
  69. }
  70. if ((desc.flag & TU_DEPTHSTENCIL) != 0)
  71. {
  72. rtDesc.depthStencilSurface.texture = newTextureData->texture;
  73. rtDesc.depthStencilSurface.face = 0;
  74. rtDesc.depthStencilSurface.numFaces = 1;
  75. rtDesc.depthStencilSurface.mipLevel = 0;
  76. }
  77. newTextureData->renderTexture = TextureManager::instance().createRenderTexture(rtDesc);
  78. }
  79. return newTextureData;
  80. }
  81. SPtr<PooledStorageBuffer> GpuResourcePool::get(const POOLED_STORAGE_BUFFER_DESC& desc)
  82. {
  83. for (auto& bufferPair : mBuffers)
  84. {
  85. SPtr<PooledStorageBuffer> bufferData = bufferPair.second.lock();
  86. if (!bufferData->mIsFree)
  87. continue;
  88. if (bufferData->buffer == nullptr)
  89. continue;
  90. if (matches(bufferData->buffer, desc))
  91. {
  92. bufferData->mIsFree = false;
  93. return bufferData;
  94. }
  95. }
  96. SPtr<PooledStorageBuffer> newBufferData = bs_shared_ptr_new<PooledStorageBuffer>(this);
  97. _registerBuffer(newBufferData);
  98. GPU_BUFFER_DESC bufferDesc;
  99. bufferDesc.type = desc.type;
  100. bufferDesc.elementSize = desc.elementSize;
  101. bufferDesc.elementCount = desc.numElements;
  102. bufferDesc.format = desc.format;
  103. bufferDesc.randomGpuWrite = true;
  104. newBufferData->buffer = GpuBuffer::create(bufferDesc);
  105. return newBufferData;
  106. }
  107. void GpuResourcePool::release(const SPtr<PooledRenderTexture>& texture)
  108. {
  109. auto iterFind = mTextures.find(texture.get());
  110. iterFind->second.lock()->mIsFree = true;
  111. }
  112. void GpuResourcePool::release(const SPtr<PooledStorageBuffer>& buffer)
  113. {
  114. auto iterFind = mBuffers.find(buffer.get());
  115. iterFind->second.lock()->mIsFree = true;
  116. }
  117. bool GpuResourcePool::matches(const SPtr<Texture>& texture, const POOLED_RENDER_TEXTURE_DESC& desc)
  118. {
  119. const TextureProperties& texProps = texture->getProperties();
  120. bool match = texProps.getTextureType() == desc.type
  121. && texProps.getFormat() == desc.format
  122. && texProps.getWidth() == desc.width
  123. && texProps.getHeight() == desc.height
  124. && (texProps.getUsage() & desc.flag) == desc.flag
  125. && (
  126. (desc.type == TEX_TYPE_2D
  127. && texProps.isHardwareGammaEnabled() == desc.hwGamma
  128. && texProps.getNumSamples() == desc.numSamples)
  129. || (desc.type == TEX_TYPE_3D
  130. && texProps.getDepth() == desc.depth)
  131. || (desc.type == TEX_TYPE_CUBE_MAP)
  132. )
  133. ;
  134. return match;
  135. }
  136. bool GpuResourcePool::matches(const SPtr<GpuBuffer>& buffer, const POOLED_STORAGE_BUFFER_DESC& desc)
  137. {
  138. const GpuBufferProperties& props = buffer->getProperties();
  139. bool match = props.getType() == desc.type && props.getElementCount() == desc.numElements;
  140. if(match)
  141. {
  142. if (desc.type == GBT_STANDARD)
  143. match = props.getFormat() == desc.format;
  144. else // Structured
  145. match = props.getElementSize() == desc.elementSize;
  146. }
  147. return match;
  148. }
  149. void GpuResourcePool::_registerTexture(const SPtr<PooledRenderTexture>& texture)
  150. {
  151. mTextures.insert(std::make_pair(texture.get(), texture));
  152. }
  153. void GpuResourcePool::_unregisterTexture(PooledRenderTexture* texture)
  154. {
  155. mTextures.erase(texture);
  156. }
  157. void GpuResourcePool::_registerBuffer(const SPtr<PooledStorageBuffer>& buffer)
  158. {
  159. mBuffers.insert(std::make_pair(buffer.get(), buffer));
  160. }
  161. void GpuResourcePool::_unregisterBuffer(PooledStorageBuffer* buffer)
  162. {
  163. mBuffers.erase(buffer);
  164. }
  165. POOLED_RENDER_TEXTURE_DESC POOLED_RENDER_TEXTURE_DESC::create2D(PixelFormat format, UINT32 width, UINT32 height,
  166. INT32 usage, UINT32 samples, bool hwGamma)
  167. {
  168. POOLED_RENDER_TEXTURE_DESC desc;
  169. desc.width = width;
  170. desc.height = height;
  171. desc.depth = 1;
  172. desc.format = format;
  173. desc.numSamples = samples;
  174. desc.flag = (TextureUsage)usage;
  175. desc.hwGamma = hwGamma;
  176. desc.type = TEX_TYPE_2D;
  177. return desc;
  178. }
  179. POOLED_RENDER_TEXTURE_DESC POOLED_RENDER_TEXTURE_DESC::create3D(PixelFormat format, UINT32 width, UINT32 height,
  180. UINT32 depth, INT32 usage)
  181. {
  182. POOLED_RENDER_TEXTURE_DESC desc;
  183. desc.width = width;
  184. desc.height = height;
  185. desc.depth = depth;
  186. desc.format = format;
  187. desc.numSamples = 1;
  188. desc.flag = (TextureUsage)usage;
  189. desc.hwGamma = false;
  190. desc.type = TEX_TYPE_3D;
  191. return desc;
  192. }
  193. POOLED_RENDER_TEXTURE_DESC POOLED_RENDER_TEXTURE_DESC::createCube(PixelFormat format, UINT32 width, UINT32 height,
  194. INT32 usage)
  195. {
  196. POOLED_RENDER_TEXTURE_DESC desc;
  197. desc.width = width;
  198. desc.height = height;
  199. desc.depth = 1;
  200. desc.format = format;
  201. desc.numSamples = 1;
  202. desc.flag = (TextureUsage)usage;
  203. desc.hwGamma = false;
  204. desc.type = TEX_TYPE_CUBE_MAP;
  205. return desc;
  206. }
  207. POOLED_STORAGE_BUFFER_DESC POOLED_STORAGE_BUFFER_DESC::createStandard(GpuBufferFormat format, UINT32 numElements)
  208. {
  209. POOLED_STORAGE_BUFFER_DESC desc;
  210. desc.type = GBT_STANDARD;
  211. desc.format = format;
  212. desc.numElements = numElements;
  213. desc.elementSize = 0;
  214. return desc;
  215. }
  216. POOLED_STORAGE_BUFFER_DESC POOLED_STORAGE_BUFFER_DESC::createStructured(UINT32 elementSize, UINT32 numElements)
  217. {
  218. POOLED_STORAGE_BUFFER_DESC desc;
  219. desc.type = GBT_STRUCTURED;
  220. desc.format = BF_UNKNOWN;
  221. desc.numElements = numElements;
  222. desc.elementSize = elementSize;
  223. return desc;
  224. }
  225. }}