BsRenderTexture.h 6.0 KB

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