BsRenderTexture.h 5.6 KB

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