BsRenderTexture.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "Image/BsTexture.h"
  6. #include "RenderAPI/BsRenderTarget.h"
  7. namespace bs
  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. namespace ct { struct RENDER_TEXTURE_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 ct::RENDER_TEXTURE_DESC& desc, bool requiresFlipping);
  25. virtual ~RenderTextureProperties() { }
  26. private:
  27. void construct(const TextureProperties* textureProps, UINT32 numSlices, UINT32 mipLevel, bool requiresFlipping,
  28. bool hwGamma);
  29. friend class ct::RenderTexture;
  30. friend class RenderTexture;
  31. };
  32. /**
  33. * Render target specialization that allows you to render into one or multiple textures. Such textures can then be used
  34. * in other operations as GPU program input.
  35. *
  36. * @note Sim thread only. Retrieve core implementation from getCore() for core thread only functionality.
  37. */
  38. class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Rendering) RenderTexture : public RenderTarget
  39. {
  40. public:
  41. virtual ~RenderTexture() { }
  42. /** @copydoc TextureManager::createRenderTexture(const TEXTURE_DESC&, bool, PixelFormat) */
  43. static SPtr<RenderTexture> create(const TEXTURE_DESC& colorDesc,
  44. bool createDepth = true, PixelFormat depthStencilFormat = PF_D32);
  45. /** @copydoc TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC&) */
  46. static SPtr<RenderTexture> create(const RENDER_TEXTURE_DESC& desc);
  47. /**
  48. * Returns a color surface texture you may bind as an input to an GPU program.
  49. *
  50. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  51. */
  52. const HTexture& getColorTexture(UINT32 idx) const { return mBindableColorTex[idx]; }
  53. /**
  54. * Returns a depth/stencil surface texture you may bind as an input to an GPU program.
  55. *
  56. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  57. */
  58. const HTexture& getDepthStencilTexture() const { return mBindableDepthStencilTex; }
  59. /**
  60. * Retrieves a core implementation of a render texture usable only from the core thread.
  61. *
  62. * @note Core thread only.
  63. */
  64. SPtr<ct::RenderTexture> getCore() const;
  65. /** Returns properties that describe the render texture. */
  66. const RenderTextureProperties& getProperties() const;
  67. protected:
  68. friend class TextureManager;
  69. RenderTexture(const RENDER_TEXTURE_DESC& desc);
  70. /** @copydoc CoreObject::createCore */
  71. SPtr<ct::CoreObject> createCore() const override;
  72. /** @copydoc CoreObject::syncToCore */
  73. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  74. protected:
  75. HTexture mBindableColorTex[BS_MAX_MULTIPLE_RENDER_TARGETS];
  76. HTexture mBindableDepthStencilTex;
  77. RENDER_TEXTURE_DESC mDesc;
  78. };
  79. /** @} */
  80. namespace ct
  81. {
  82. /** @addtogroup RenderAPI-Internal
  83. * @{
  84. */
  85. /**
  86. * @see bs::RENDER_TEXTURE_DESC
  87. *
  88. * @note References core textures instead of texture handles.
  89. */
  90. struct BS_CORE_EXPORT RENDER_TEXTURE_DESC
  91. {
  92. RENDER_SURFACE_DESC colorSurfaces[BS_MAX_MULTIPLE_RENDER_TARGETS];
  93. RENDER_SURFACE_DESC depthStencilSurface;
  94. };
  95. /**
  96. * Provides access to internal render texture implementation usable only from the core thread.
  97. *
  98. * @note Core thread only.
  99. */
  100. class BS_CORE_EXPORT RenderTexture : public RenderTarget
  101. {
  102. public:
  103. RenderTexture(const RENDER_TEXTURE_DESC& desc, UINT32 deviceIdx);
  104. virtual ~RenderTexture();
  105. /** @copydoc CoreObject::initialize */
  106. void initialize() override;
  107. /** @copydoc TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC&, UINT32) */
  108. static SPtr<RenderTexture> create(const RENDER_TEXTURE_DESC& desc, UINT32 deviceIdx = 0);
  109. /**
  110. * Returns a color surface texture you may bind as an input to an GPU program.
  111. *
  112. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  113. */
  114. SPtr<Texture> getColorTexture(UINT32 idx) const { return mDesc.colorSurfaces[idx].texture; }
  115. /**
  116. * Returns a depth/stencil surface texture you may bind as an input to an GPU program.
  117. *
  118. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  119. */
  120. SPtr<Texture> getDepthStencilTexture() const { return mDesc.depthStencilSurface.texture; }
  121. /** Returns properties that describe the render texture. */
  122. const RenderTextureProperties& getProperties() const;
  123. protected:
  124. /** @copydoc CoreObject::syncToCore */
  125. void syncToCore(const CoreSyncData& data) override;
  126. private:
  127. /** Throws an exception of the color and depth/stencil buffers aren't compatible. */
  128. void throwIfBuffersDontMatch() const;
  129. protected:
  130. friend class bs::RenderTexture;
  131. SPtr<TextureView> mColorSurfaces[BS_MAX_MULTIPLE_RENDER_TARGETS];
  132. SPtr<TextureView> mDepthStencilSurface;
  133. RENDER_TEXTURE_DESC mDesc;
  134. };
  135. /** @} */
  136. }
  137. }