2
0

BsRenderTexturePool.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma once
  2. #include "BsRenderBeastPrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsPixelUtil.h"
  5. namespace BansheeEngine
  6. {
  7. class RenderTexturePool;
  8. /**
  9. * @brief Contains data about a single render texture in the texture pool.
  10. */
  11. struct PooledRenderTexture
  12. {
  13. PooledRenderTexture(RenderTexturePool* pool);
  14. ~PooledRenderTexture();
  15. SPtr<TextureCore> texture;
  16. private:
  17. friend class RenderTexturePool;
  18. RenderTexturePool* mPool;
  19. bool mIsFree;
  20. };
  21. /**
  22. * @brief Contains a pool of render textures meant to accommodate reuse of
  23. * render textures of the same size and format.
  24. */
  25. class RenderTexturePool : public Module<RenderTexturePool>
  26. {
  27. public:
  28. ~RenderTexturePool();
  29. /**
  30. * @brief Attempts to find the unused render texture with the specified parameters in the pool,
  31. * or creates a new texture otherwise. When done with the texture make sure to call ::free.
  32. *
  33. * @param format Pixel format used by the texture color surface.
  34. * @param width Width of the render texture, in pixels.
  35. * @param height Height of the render texture, in pixels.
  36. * @param hwGamma Should the written pixels be gamma corrected.
  37. * @param samples If higher than 1, texture containing multiple samples per pixel is created.
  38. */
  39. SPtr<PooledRenderTexture> get(PixelFormat format, UINT32 width, UINT32 height, bool hwGamma = false, UINT32 samples = 0);
  40. /**
  41. * @brief Releases a texture previously allocated with ::get. The texture is returned to the pool so that it may be reused later.
  42. *
  43. * @note The texture will be removed from the pool if the last reference to it is deleted. Normally you would call ::release but
  44. * keep a reference if you plan on using it later on.
  45. */
  46. void release(const SPtr<PooledRenderTexture>& texture);
  47. private:
  48. friend struct PooledRenderTexture;
  49. /**
  50. * @brief Registers a newly created render texture in the pool.
  51. */
  52. void _registerTexture(const SPtr<PooledRenderTexture>& texture);
  53. /**
  54. * @brief Unregisters a created render texture in the pool.
  55. */
  56. void _unregisterTexture(PooledRenderTexture* texture);
  57. /**
  58. * @brief Checks does the provided texture match the parameters.
  59. *
  60. * @param texture Texture to match against the parameters.
  61. * @param format Pixel format used by the texture color surface.
  62. * @param width Width of the render texture, in pixels.
  63. * @param height Height of the render texture, in pixels.
  64. * @param hwGamma Should the written pixels be gamma corrected.
  65. * @param samples If higher than 1, texture containing multiple samples per pixel is created.
  66. */
  67. bool matches(const SPtr<TextureCore>& texture, PixelFormat format, UINT32 width, UINT32 height, bool hwGamma, UINT32 samples);
  68. Map<PooledRenderTexture*, std::weak_ptr<PooledRenderTexture>> mTextures;
  69. };
  70. }