BsRenderTexturePool.h 3.1 KB

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