BsRenderTexture.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsTexture.h"
  4. #include "BsRenderTarget.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Structure that describes a render texture color and depth/stencil surfaces.
  9. */
  10. struct BS_CORE_EXPORT RENDER_TEXTURE_DESC
  11. {
  12. RENDER_SURFACE_DESC colorSurface;
  13. RENDER_SURFACE_DESC depthStencilSurface;
  14. };
  15. /**
  16. * @see RENDER_TEXTURE_DESC
  17. *
  18. * @note References core textures instead of texture handles.
  19. */
  20. struct BS_CORE_EXPORT RENDER_TEXTURE_CORE_DESC
  21. {
  22. RENDER_SURFACE_CORE_DESC colorSurface;
  23. RENDER_SURFACE_CORE_DESC depthStencilSurface;
  24. };
  25. /**
  26. * @brief Contains various properties that describe a render texture.
  27. */
  28. class BS_CORE_EXPORT RenderTextureProperties : public RenderTargetProperties
  29. {
  30. public:
  31. RenderTextureProperties(const RENDER_TEXTURE_DESC& desc, bool requiresFlipping);
  32. RenderTextureProperties(const RENDER_TEXTURE_CORE_DESC& desc, bool requiresFlipping);
  33. virtual ~RenderTextureProperties() { }
  34. private:
  35. void construct(const TextureProperties* textureProps, bool requiresFlipping);
  36. friend class RenderTextureCore;
  37. friend class RenderTexture;
  38. };
  39. /**
  40. * @brief Provides access to internal render texture implementation usable only from the core thread.
  41. *
  42. * @note Core thread only.
  43. */
  44. class BS_CORE_EXPORT RenderTextureCore : public RenderTargetCore
  45. {
  46. public:
  47. RenderTextureCore(const RENDER_TEXTURE_CORE_DESC& desc);
  48. virtual ~RenderTextureCore();
  49. /**
  50. * @copydoc CoreObjectCore::initialize
  51. */
  52. virtual void initialize();
  53. /**
  54. * @brief Returns a color surface texture you may bind as an input to an GPU program.
  55. *
  56. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  57. */
  58. SPtr<TextureCore> getBindableColorTexture() const { return mDesc.colorSurface.texture; }
  59. /**
  60. * @brief Returns a depth/stencil surface texture you may bind as an input to an GPU program.
  61. *
  62. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  63. */
  64. SPtr<TextureCore> getBindableDepthStencilTexture() const { return mDesc.depthStencilSurface.texture; }
  65. /**
  66. * @brief Returns properties that describe the render texture.
  67. */
  68. const RenderTextureProperties& getProperties() const;
  69. protected:
  70. /**
  71. * @copydoc CoreObjectCore::syncFromCore
  72. */
  73. virtual CoreSyncData syncFromCore(FrameAlloc* allocator);
  74. /**
  75. * @copydoc CoreObjectCore::syncToCore
  76. */
  77. virtual void syncToCore(const CoreSyncData& data);
  78. private:
  79. /**
  80. * @brief Throws an exception of the color and depth/stencil buffers aren't compatible.
  81. */
  82. void throwIfBuffersDontMatch() const;
  83. protected:
  84. friend class RenderTexture;
  85. TextureViewPtr mColorSurface;
  86. TextureViewPtr mDepthStencilSurface;
  87. RENDER_TEXTURE_CORE_DESC mDesc;
  88. };
  89. /**
  90. * @brief Render target specialization that allows you to render into a texture you may
  91. * later bind in further render operations.
  92. *
  93. * @note Sim thread only. Retrieve core implementation from getCore()
  94. * for core thread only functionality.
  95. */
  96. class BS_CORE_EXPORT RenderTexture : public RenderTarget
  97. {
  98. public:
  99. virtual ~RenderTexture() { }
  100. /**
  101. * @brief Creates a new render texture with color and optionally depth/stencil surfaces.
  102. *
  103. * @param textureType Type of texture to render to.
  104. * @param width Width of the render texture, in pixels.
  105. * @param height Height of the render texture, in pixels.
  106. * @param format Pixel format used by the texture color surface.
  107. * @param hwGamma Should the written pixels be gamma corrected.
  108. * @param multisampleCount If higher than 1, texture containing multiple samples per pixel is created.
  109. * @param createDepth Should a depth/stencil surface be created along with the color surface.
  110. * @param depthStencilFormat Format used by the depth stencil surface, if one is created.
  111. */
  112. static RenderTexturePtr create(TextureType textureType, UINT32 width, UINT32 height,
  113. PixelFormat format = PF_R8G8B8A8, bool hwGamma = false, UINT32 multisampleCount = 0,
  114. bool createDepth = true, PixelFormat depthStencilFormat = PF_D24S8);
  115. /**
  116. * @copydoc TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC&)
  117. */
  118. static RenderTexturePtr create(const RENDER_TEXTURE_DESC& desc);
  119. /**
  120. * @brief Returns a color surface texture you may bind as an input to an GPU program.
  121. *
  122. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  123. */
  124. const HTexture& getBindableColorTexture() const { return mBindableColorTex; }
  125. /**
  126. * @brief Returns a depth/stencil surface texture you may bind as an input to an GPU program.
  127. *
  128. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  129. */
  130. const HTexture& getBindableDepthStencilTexture() const { return mBindableDepthStencilTex; }
  131. /**
  132. * @brief Retrieves a core implementation of a render texture usable only from the
  133. * core thread.
  134. *
  135. * @note Core thread only.
  136. */
  137. SPtr<RenderTextureCore> getCore() const;
  138. /**
  139. * @brief Returns properties that describe the render texture.
  140. */
  141. const RenderTextureProperties& getProperties() const;
  142. protected:
  143. friend class TextureManager;
  144. RenderTexture(const RENDER_TEXTURE_DESC& desc);
  145. /**
  146. * @copydoc RenderTexture::createCore
  147. */
  148. virtual SPtr<CoreObjectCore> createCore() const;
  149. /**
  150. * @copydoc CoreObjectCore::syncToCore
  151. */
  152. virtual CoreSyncData syncToCore(FrameAlloc* allocator);
  153. /**
  154. * @copydoc CoreObjectCore::syncFromCore
  155. */
  156. virtual void syncFromCore(const CoreSyncData& data);
  157. protected:
  158. HTexture mBindableColorTex;
  159. HTexture mBindableDepthStencilTex;
  160. RENDER_TEXTURE_DESC mDesc;
  161. };
  162. }