BsRenderTexturePool.h 2.7 KB

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