BsRenderTexturePool.h 3.1 KB

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