BsRenderTexturePool.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include "BsTexture.h"
  8. namespace BansheeEngine
  9. {
  10. /** @addtogroup RenderBeast
  11. * @{
  12. */
  13. class RenderTexturePool;
  14. struct POOLED_RENDER_TEXTURE_DESC;
  15. /** Contains data about a single render texture in the texture pool. */
  16. struct PooledRenderTexture
  17. {
  18. PooledRenderTexture(RenderTexturePool* pool);
  19. ~PooledRenderTexture();
  20. SPtr<TextureCore> texture;
  21. private:
  22. friend class RenderTexturePool;
  23. RenderTexturePool* mPool;
  24. bool mIsFree;
  25. };
  26. /** Contains a pool of render textures meant to accommodate reuse of render textures of the same size and format. */
  27. class RenderTexturePool : public Module<RenderTexturePool>
  28. {
  29. public:
  30. ~RenderTexturePool();
  31. /**
  32. * Attempts to find the unused render texture with the specified parameters in the pool, or creates a new texture
  33. * otherwise. When done with the texture make sure to call release().
  34. *
  35. * @param[in] desc Descriptor structure that describes what kind of texture to retrieve.
  36. */
  37. SPtr<PooledRenderTexture> get(const POOLED_RENDER_TEXTURE_DESC& desc);
  38. /**
  39. * Releases a texture previously allocated with get(). The texture is returned to the pool so that it may be reused
  40. * later.
  41. *
  42. * @note
  43. * The texture will be removed from the pool if the last reference to it is deleted. Normally you would call
  44. * release() but 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. /** Registers a newly created render texture in the pool. */
  50. void _registerTexture(const SPtr<PooledRenderTexture>& texture);
  51. /** Unregisters a created render texture in the pool. */
  52. void _unregisterTexture(PooledRenderTexture* texture);
  53. /**
  54. * Checks does the provided texture match the parameters.
  55. *
  56. * @param[in] desc Descriptor structure that describes what kind of texture to match.
  57. * @return True if the texture matches the descriptor, false otherwise.
  58. */
  59. static bool matches(const SPtr<TextureCore>& texture, const POOLED_RENDER_TEXTURE_DESC& desc);
  60. Map<PooledRenderTexture*, std::weak_ptr<PooledRenderTexture>> mTextures;
  61. };
  62. /** Structure used for creating a new pooled render texture. */
  63. struct POOLED_RENDER_TEXTURE_DESC
  64. {
  65. public:
  66. /**
  67. * Creates a descriptor for a two dimensional render texture.
  68. *
  69. * @param[in] format Pixel format used by the texture surface.
  70. * @param[in] width Width of the render texture, in pixels.
  71. * @param[in] height Height of the render texture, in pixels.
  72. * @param[in] usage Usage flags that control in which way is the texture going to be used.
  73. * @param[in] samples If higher than 1, texture containing multiple samples per pixel is created.
  74. * @param[in] hwGamma Should the written pixels be gamma corrected.
  75. * @return Descriptor that is accepted by RenderTexturePool.
  76. */
  77. static POOLED_RENDER_TEXTURE_DESC create2D(PixelFormat format, UINT32 width, UINT32 height,
  78. INT32 usage = TU_STATIC, UINT32 samples = 0, bool hwGamma = false);
  79. /**
  80. * Creates a descriptor for a three dimensional render texture.
  81. *
  82. * @param[in] format Pixel format used by the texture surface.
  83. * @param[in] width Width of the render texture, in pixels.
  84. * @param[in] height Height of the render texture, in pixels.
  85. * @param[in] depth Depth of the render texture, in pixels.
  86. * @param[in] usage Usage flags that control in which way is the texture going to be used.
  87. * @return Descriptor that is accepted by RenderTexturePool.
  88. */
  89. static POOLED_RENDER_TEXTURE_DESC create3D(PixelFormat format, UINT32 width, UINT32 height, UINT32 depth,
  90. INT32 usage = TU_STATIC);
  91. /**
  92. * Creates a descriptor for a cube render texture.
  93. *
  94. * @param[in] format Pixel format used by the texture surface.
  95. * @param[in] width Width of the render texture, in pixels.
  96. * @param[in] height Height of the render texture, in pixels.
  97. * @param[in] usage Usage flags that control in which way is the texture going to be used.
  98. * @return Descriptor that is accepted by RenderTexturePool.
  99. */
  100. static POOLED_RENDER_TEXTURE_DESC createCube(PixelFormat format, UINT32 width, UINT32 height,
  101. INT32 usage = TU_STATIC);
  102. private:
  103. friend class RenderTexturePool;
  104. POOLED_RENDER_TEXTURE_DESC() { }
  105. UINT32 width;
  106. UINT32 height;
  107. UINT32 depth;
  108. UINT32 numSamples;
  109. PixelFormat format;
  110. TextureUsage flag;
  111. TextureType type;
  112. bool hwGamma;
  113. };
  114. /** @} */
  115. }