BsRenderTexture.h 5.9 KB

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