2
0

BsRenderTexture.h 6.2 KB

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