BsRenderTexturePool.h 4.7 KB

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