BsGpuResourcePool.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsRenderBeastPrerequisites.h"
  5. #include "Utility/BsModule.h"
  6. #include "Image/BsPixelUtil.h"
  7. #include "Image/BsTexture.h"
  8. namespace bs { namespace ct
  9. {
  10. /** @addtogroup RenderBeast
  11. * @{
  12. */
  13. class GpuResourcePool;
  14. struct POOLED_RENDER_TEXTURE_DESC;
  15. struct POOLED_STORAGE_BUFFER_DESC;
  16. /** Contains data about a single render texture in the GPU resource pool. */
  17. struct PooledRenderTexture
  18. {
  19. PooledRenderTexture(GpuResourcePool* pool);
  20. ~PooledRenderTexture();
  21. SPtr<Texture> texture;
  22. SPtr<RenderTexture> renderTexture;
  23. private:
  24. friend class GpuResourcePool;
  25. GpuResourcePool* mPool;
  26. bool mIsFree;
  27. };
  28. /** Contains data about a single storage buffer in the GPU resource pool. */
  29. struct PooledStorageBuffer
  30. {
  31. PooledStorageBuffer(GpuResourcePool* pool);
  32. ~PooledStorageBuffer();
  33. SPtr<GpuBuffer> buffer;
  34. private:
  35. friend class GpuResourcePool;
  36. GpuResourcePool* mPool;
  37. bool mIsFree;
  38. };
  39. /**
  40. * Contains a pool of textures and buffers meant to accommodate reuse of such resources for the main purpose of using
  41. * them as write targets on the GPU.
  42. */
  43. class GpuResourcePool : public Module<GpuResourcePool>
  44. {
  45. public:
  46. ~GpuResourcePool();
  47. /**
  48. * Attempts to find the unused render texture with the specified parameters in the pool, or creates a new texture
  49. * otherwise. When done with the texture make sure to call release(const POOLED_RENDER_TEXTURE_DESC&).
  50. *
  51. * @param[in] desc Descriptor structure that describes what kind of texture to retrieve.
  52. */
  53. SPtr<PooledRenderTexture> get(const POOLED_RENDER_TEXTURE_DESC& desc);
  54. /**
  55. * Attempts to find the unused storage buffer with the specified parameters in the pool, or creates a new buffer
  56. * otherwise. When done with the buffer make sure to call release(const POOLED_STORAGE_BUFFER_DESC&).
  57. *
  58. * @param[in] desc Descriptor structure that describes what kind of buffer to retrieve.
  59. */
  60. SPtr<PooledStorageBuffer> get(const POOLED_STORAGE_BUFFER_DESC& desc);
  61. /**
  62. * Releases a texture previously allocated with get(const POOLED_RENDER_TEXTURE_DESC&). The texture is returned to
  63. * the pool so that it may be reused later.
  64. *
  65. * @note
  66. * The texture will be removed from the pool if the last reference to it is deleted. Normally you would call
  67. * release(const POOLED_RENDER_TEXTURE_DESC&) but keep a reference if you plan on using it later on.
  68. */
  69. void release(const SPtr<PooledRenderTexture>& texture);
  70. /**
  71. * Releases a buffer previously allocated with get(const POOLED_STORAGE_BUFFER_DESC&). The buffer is returned to the
  72. * pool so that it may be reused later.
  73. *
  74. * @note
  75. * The buffer will be removed from the pool if the last reference to it is deleted. Normally you would call
  76. * release(const POOLED_STORAGE_BUFFER_DESC&) but keep a reference if you plan on using it later on.
  77. */
  78. void release(const SPtr<PooledStorageBuffer>& buffer);
  79. private:
  80. friend struct PooledRenderTexture;
  81. friend struct PooledStorageBuffer;
  82. /** Registers a newly created render texture in the pool. */
  83. void _registerTexture(const SPtr<PooledRenderTexture>& texture);
  84. /** Unregisters a created render texture in the pool. */
  85. void _unregisterTexture(PooledRenderTexture* texture);
  86. /** Registers a newly created storage buffer in the pool. */
  87. void _registerBuffer(const SPtr<PooledStorageBuffer>& buffer);
  88. /** Unregisters a created storage buffer in the pool. */
  89. void _unregisterBuffer(PooledStorageBuffer* buffer);
  90. /**
  91. * Checks does the provided texture match the parameters.
  92. *
  93. * @param[in] desc Descriptor structure that describes what kind of texture to match.
  94. * @return True if the texture matches the descriptor, false otherwise.
  95. */
  96. static bool matches(const SPtr<Texture>& texture, const POOLED_RENDER_TEXTURE_DESC& desc);
  97. /**
  98. * Checks does the provided buffer match the parameters.
  99. *
  100. * @param[in] desc Descriptor structure that describes what kind of buffer to match.
  101. * @return True if the buffer matches the descriptor, false otherwise.
  102. */
  103. static bool matches(const SPtr<GpuBuffer>& buffer, const POOLED_STORAGE_BUFFER_DESC& desc);
  104. Map<PooledRenderTexture*, std::weak_ptr<PooledRenderTexture>> mTextures;
  105. Map<PooledStorageBuffer*, std::weak_ptr<PooledStorageBuffer>> mBuffers;
  106. };
  107. /** Structure used for creating a new pooled render texture. */
  108. struct POOLED_RENDER_TEXTURE_DESC
  109. {
  110. public:
  111. POOLED_RENDER_TEXTURE_DESC() {}
  112. /**
  113. * Creates a descriptor for a two dimensional render texture.
  114. *
  115. * @param[in] format Pixel format used by the texture surface.
  116. * @param[in] width Width of the render texture, in pixels.
  117. * @param[in] height Height of the render texture, in pixels.
  118. * @param[in] usage Usage flags that control in which way is the texture going to be used.
  119. * @param[in] samples If higher than 1, texture containing multiple samples per pixel is created.
  120. * @param[in] hwGamma Should the written pixels be gamma corrected.
  121. * @param[in] arraySize Number of textures in a texture array. Specify 1 for no array.
  122. * @param[in] mipCount Number of mip levels, excluding the root mip level.
  123. * @return Descriptor that is accepted by RenderTexturePool.
  124. */
  125. static POOLED_RENDER_TEXTURE_DESC create2D(PixelFormat format, UINT32 width, UINT32 height,
  126. INT32 usage = TU_STATIC, UINT32 samples = 0, bool hwGamma = false, UINT32 arraySize = 1, UINT32 mipCount = 0);
  127. /**
  128. * Creates a descriptor for a three dimensional render texture.
  129. *
  130. * @param[in] format Pixel format used by the texture surface.
  131. * @param[in] width Width of the render texture, in pixels.
  132. * @param[in] height Height of the render texture, in pixels.
  133. * @param[in] depth Depth of the render texture, in pixels.
  134. * @param[in] usage Usage flags that control in which way is the texture going to be used.
  135. * @return Descriptor that is accepted by RenderTexturePool.
  136. */
  137. static POOLED_RENDER_TEXTURE_DESC create3D(PixelFormat format, UINT32 width, UINT32 height, UINT32 depth,
  138. INT32 usage = TU_STATIC);
  139. /**
  140. * Creates a descriptor for a cube render texture.
  141. *
  142. * @param[in] format Pixel format used by the texture surface.
  143. * @param[in] width Width of the render texture, in pixels.
  144. * @param[in] height Height of the render texture, in pixels.
  145. * @param[in] usage Usage flags that control in which way is the texture going to be used.
  146. * @param[in] arraySize Number of textures in a texture array. Specify 1 for no array.
  147. * @return Descriptor that is accepted by RenderTexturePool.
  148. */
  149. static POOLED_RENDER_TEXTURE_DESC createCube(PixelFormat format, UINT32 width, UINT32 height,
  150. INT32 usage = TU_STATIC, UINT32 arraySize = 1);
  151. private:
  152. friend class GpuResourcePool;
  153. UINT32 width;
  154. UINT32 height;
  155. UINT32 depth;
  156. UINT32 numSamples;
  157. PixelFormat format;
  158. TextureUsage flag;
  159. TextureType type;
  160. bool hwGamma;
  161. UINT32 arraySize;
  162. UINT32 numMipLevels;
  163. };
  164. /** Structure used for describing a pooled storage buffer. */
  165. struct POOLED_STORAGE_BUFFER_DESC
  166. {
  167. public:
  168. POOLED_STORAGE_BUFFER_DESC() {}
  169. /**
  170. * Creates a descriptor for a storage buffer containing primitive data types.
  171. *
  172. * @param[in] format Format of individual buffer entries.
  173. * @param[in] numElements Number of elements in the buffer.
  174. */
  175. static POOLED_STORAGE_BUFFER_DESC createStandard(GpuBufferFormat format, UINT32 numElements);
  176. /**
  177. * Creates a descriptor for a storage buffer containing structures.
  178. *
  179. * @param[in] elementSize Size of a single structure in the buffer.
  180. * @param[in] numElements Number of elements in the buffer.
  181. */
  182. static POOLED_STORAGE_BUFFER_DESC createStructured(UINT32 elementSize, UINT32 numElements);
  183. private:
  184. friend class GpuResourcePool;
  185. GpuBufferType type;
  186. GpuBufferFormat format;
  187. UINT32 numElements;
  188. UINT32 elementSize;
  189. };
  190. /** @} */
  191. }}