BsRenderTexture.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /** @copydoc TextureManager::createRenderTexture(const TEXTURE_DESC&, bool, PixelFormat) */
  42. static SPtr<RenderTexture> create(const TEXTURE_DESC& colorDesc,
  43. bool createDepth = true, PixelFormat depthStencilFormat = PF_D24S8);
  44. /** @copydoc TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC&) */
  45. static SPtr<RenderTexture> create(const RENDER_TEXTURE_DESC& desc);
  46. /**
  47. * Returns a color surface texture you may bind as an input to an GPU program.
  48. *
  49. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  50. */
  51. const HTexture& getColorTexture(UINT32 idx) const { return mBindableColorTex[idx]; }
  52. /**
  53. * Returns a depth/stencil 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. const HTexture& getDepthStencilTexture() const { return mBindableDepthStencilTex; }
  58. /**
  59. * Retrieves a core implementation of a render texture usable only from the core thread.
  60. *
  61. * @note Core thread only.
  62. */
  63. SPtr<RenderTextureCore> getCore() const;
  64. /** Returns properties that describe the render texture. */
  65. const RenderTextureProperties& getProperties() const;
  66. protected:
  67. friend class TextureManager;
  68. RenderTexture(const RENDER_TEXTURE_DESC& desc);
  69. /** @copydoc CoreObject::createCore */
  70. SPtr<CoreObjectCore> createCore() const override;
  71. /** @copydoc CoreObject::syncToCore */
  72. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  73. protected:
  74. HTexture mBindableColorTex[BS_MAX_MULTIPLE_RENDER_TARGETS];
  75. HTexture mBindableDepthStencilTex;
  76. RENDER_TEXTURE_DESC mDesc;
  77. };
  78. /** @} */
  79. /** @addtogroup RenderAPI-Internal
  80. * @{
  81. */
  82. /**
  83. * @see RENDER_TEXTURE_DESC
  84. *
  85. * @note References core textures instead of texture handles.
  86. */
  87. struct BS_CORE_EXPORT RENDER_TEXTURE_DESC_CORE
  88. {
  89. RENDER_SURFACE_DESC_CORE colorSurfaces[BS_MAX_MULTIPLE_RENDER_TARGETS];
  90. RENDER_SURFACE_DESC_CORE depthStencilSurface;
  91. };
  92. /**
  93. * Provides access to internal render texture implementation usable only from the core thread.
  94. *
  95. * @note Core thread only.
  96. */
  97. class BS_CORE_EXPORT RenderTextureCore : public RenderTargetCore
  98. {
  99. public:
  100. RenderTextureCore(const RENDER_TEXTURE_DESC_CORE& desc, GpuDeviceFlags deviceMask);
  101. virtual ~RenderTextureCore();
  102. /** @copydoc CoreObjectCore::initialize */
  103. void initialize() override;
  104. /** @copydoc TextureCoreManager::createRenderTexture(const RENDER_TEXTURE_DESC_CORE&, GpuDeviceFlags) */
  105. static SPtr<RenderTextureCore> create(const RENDER_TEXTURE_DESC_CORE& desc, GpuDeviceFlags deviceMask = GDF_DEFAULT);
  106. /**
  107. * Returns a color surface texture you may bind as an input to an GPU program.
  108. *
  109. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  110. */
  111. SPtr<TextureCore> getColorTexture(UINT32 idx) const { return mDesc.colorSurfaces[idx].texture; }
  112. /**
  113. * Returns a depth/stencil surface texture you may bind as an input to an GPU program.
  114. *
  115. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  116. */
  117. SPtr<TextureCore> getDepthStencilTexture() const { return mDesc.depthStencilSurface.texture; }
  118. /** Returns properties that describe the render texture. */
  119. const RenderTextureProperties& getProperties() const;
  120. protected:
  121. /** @copydoc CoreObjectCore::syncToCore */
  122. void syncToCore(const CoreSyncData& data) override;
  123. private:
  124. /** Throws an exception of the color and depth/stencil buffers aren't compatible. */
  125. void throwIfBuffersDontMatch() const;
  126. protected:
  127. friend class RenderTexture;
  128. SPtr<TextureView> mColorSurfaces[BS_MAX_MULTIPLE_RENDER_TARGETS];
  129. SPtr<TextureView> mDepthStencilSurface;
  130. RENDER_TEXTURE_DESC_CORE mDesc;
  131. };
  132. /** @} */
  133. }