BsRenderTexturePool.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. POOLED_RENDER_TEXTURE_DESC() {}
  68. /**
  69. * Creates a descriptor for a two dimensional render texture.
  70. *
  71. * @param[in] format Pixel format used by the texture surface.
  72. * @param[in] width Width of the render texture, in pixels.
  73. * @param[in] height Height of the render texture, in pixels.
  74. * @param[in] usage Usage flags that control in which way is the texture going to be used.
  75. * @param[in] samples If higher than 1, texture containing multiple samples per pixel is created.
  76. * @param[in] hwGamma Should the written pixels be gamma corrected.
  77. * @return Descriptor that is accepted by RenderTexturePool.
  78. */
  79. static POOLED_RENDER_TEXTURE_DESC create2D(PixelFormat format, UINT32 width, UINT32 height,
  80. INT32 usage = TU_STATIC, UINT32 samples = 0, bool hwGamma = false);
  81. /**
  82. * Creates a descriptor for a three dimensional render texture.
  83. *
  84. * @param[in] format Pixel format used by the texture surface.
  85. * @param[in] width Width of the render texture, in pixels.
  86. * @param[in] height Height of the render texture, in pixels.
  87. * @param[in] depth Depth of the render texture, in pixels.
  88. * @param[in] usage Usage flags that control in which way is the texture going to be used.
  89. * @return Descriptor that is accepted by RenderTexturePool.
  90. */
  91. static POOLED_RENDER_TEXTURE_DESC create3D(PixelFormat format, UINT32 width, UINT32 height, UINT32 depth,
  92. INT32 usage = TU_STATIC);
  93. /**
  94. * Creates a descriptor for a cube render texture.
  95. *
  96. * @param[in] format Pixel format used by the texture surface.
  97. * @param[in] width Width of the render texture, in pixels.
  98. * @param[in] height Height of the render texture, in pixels.
  99. * @param[in] usage Usage flags that control in which way is the texture going to be used.
  100. * @return Descriptor that is accepted by RenderTexturePool.
  101. */
  102. static POOLED_RENDER_TEXTURE_DESC createCube(PixelFormat format, UINT32 width, UINT32 height,
  103. INT32 usage = TU_STATIC);
  104. private:
  105. friend class RenderTexturePool;
  106. UINT32 width;
  107. UINT32 height;
  108. UINT32 depth;
  109. UINT32 numSamples;
  110. PixelFormat format;
  111. TextureUsage flag;
  112. TextureType type;
  113. bool hwGamma;
  114. };
  115. /** @} */
  116. }