BsRenderTexture.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 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, bool requiresFlipping);
  28. friend class ct::RenderTexture;
  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_D32);
  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<ct::RenderTexture> 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<ct::CoreObject> 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. namespace ct
  80. {
  81. /** @addtogroup RenderAPI-Internal
  82. * @{
  83. */
  84. /**
  85. * @see bs::RENDER_TEXTURE_DESC
  86. *
  87. * @note References core textures instead of texture handles.
  88. */
  89. struct BS_CORE_EXPORT RENDER_TEXTURE_DESC
  90. {
  91. RENDER_SURFACE_DESC colorSurfaces[BS_MAX_MULTIPLE_RENDER_TARGETS];
  92. RENDER_SURFACE_DESC depthStencilSurface;
  93. };
  94. /**
  95. * Provides access to internal render texture implementation usable only from the core thread.
  96. *
  97. * @note Core thread only.
  98. */
  99. class BS_CORE_EXPORT RenderTexture : public RenderTarget
  100. {
  101. public:
  102. RenderTexture(const RENDER_TEXTURE_DESC& desc, UINT32 deviceIdx);
  103. virtual ~RenderTexture();
  104. /** @copydoc CoreObject::initialize */
  105. void initialize() override;
  106. /** @copydoc TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC&, UINT32) */
  107. static SPtr<RenderTexture> create(const RENDER_TEXTURE_DESC& desc, UINT32 deviceIdx = 0);
  108. /**
  109. * Returns a color surface texture you may bind as an input to an GPU program.
  110. *
  111. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  112. */
  113. SPtr<Texture> getColorTexture(UINT32 idx) const { return mDesc.colorSurfaces[idx].texture; }
  114. /**
  115. * Returns a depth/stencil surface texture you may bind as an input to an GPU program.
  116. *
  117. * @note Be aware that you cannot bind a render texture for reading and writing at the same time.
  118. */
  119. SPtr<Texture> getDepthStencilTexture() const { return mDesc.depthStencilSurface.texture; }
  120. /** Returns properties that describe the render texture. */
  121. const RenderTextureProperties& getProperties() const;
  122. protected:
  123. /** @copydoc CoreObject::syncToCore */
  124. void syncToCore(const CoreSyncData& data) override;
  125. private:
  126. /** Throws an exception of the color and depth/stencil buffers aren't compatible. */
  127. void throwIfBuffersDontMatch() const;
  128. protected:
  129. friend class bs::RenderTexture;
  130. SPtr<TextureView> mColorSurfaces[BS_MAX_MULTIPLE_RENDER_TARGETS];
  131. SPtr<TextureView> mDepthStencilSurface;
  132. RENDER_TEXTURE_DESC mDesc;
  133. };
  134. /** @} */
  135. }
  136. }