BsRenderTargets.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsRenderBeastPrerequisites.h"
  5. #include "BsPixelUtil.h"
  6. #include "BsRendererView.h"
  7. namespace bs { namespace ct
  8. {
  9. /** @addtogroup RenderBeast
  10. * @{
  11. */
  12. /** Types of render target textures that can be allocated by RenderTargets manager. */
  13. enum RenderTargetType
  14. {
  15. /**
  16. * Buffer containing albedo, normals, metalness/roughness and other material data, populated during base pass and
  17. * used during lighting and other operations.
  18. */
  19. RTT_GBuffer,
  20. /** Buffer containing intermediate lighting information used during deferred lighting pass. */
  21. RTT_LightAccumulation,
  22. /** Buffer containing temporary combined occlusion data for a specific light (from shadow maps or attenuation. */
  23. RTT_LightOcclusion,
  24. /** Buffer containing (potentially multisampled) scene color information. */
  25. RTT_SceneColor,
  26. /**
  27. * Buffer containing non-MSAA final scene color information. If MSAA is not used then this is equivalent to
  28. * RTT_SceneColor;
  29. */
  30. RTT_ResolvedSceneColor,
  31. /**
  32. * Secondary resolved scene color texture that can be used for ping-ponging between primary and secondary scene
  33. * color textures. Primarily useful for post-processing effects.
  34. */
  35. RTT_ResolvedSceneColorSecondary,
  36. /**
  37. * Buffer containing a hierarchical Z buffer. If passed to RenderTargets::generate() it will allocate and build
  38. * the hierarchical Z buffer from the current contents of the scene depth buffer. Generated buffer can be
  39. * accessed from getHiZ(). If using MSAA render targets, make sure to first generate the RTT_ResolvedDepth
  40. * target as HiZ can't be built from multisampled depth buffer.
  41. */
  42. RTT_HiZ,
  43. /**
  44. * Buffer containing the resolved (non-multisampled) depth buffer. If multisampling isn't used then this will be
  45. * the same buffer as the scene depth buffer. If passed to RenderTargets::generate() it will allocate and resolve
  46. * the scene depth buffer into the newly allocated buffer. Scene depth must be previously allocated and populated
  47. * with scene data.
  48. */
  49. RTT_ResolvedDepth
  50. };
  51. /**
  52. * Allocates and handles all the required render targets for rendering a scene from a specific view.
  53. *
  54. * @note Core thread only.
  55. */
  56. class RenderTargets
  57. {
  58. public:
  59. /**
  60. * Prepares any internal data for rendering. Should be called at the beginning of each frame, before allocating,
  61. * retrieving or binding any textures. Must eventually be followed by cleanup().
  62. */
  63. void prepare();
  64. /**
  65. * Cleans up any internal data after rendering. Should be called after done rendering for a frame. All allocations
  66. * must be released at this point and no further allocations or texture binds should be done until the next call
  67. * to prepare().
  68. */
  69. void cleanup();
  70. /**
  71. * Allocates the textures required for rendering. Allocations are pooled so this is generally a fast operation
  72. * unless the size or other render target options changed. This must be called before binding render targets.
  73. * Some render target types are also automatically populated with data, in which case calling generate() instead
  74. * of allocate is a better option - see individual target descriptions for more details.
  75. */
  76. void allocate(RenderTargetType type);
  77. /**
  78. * Deallocates textures by returning them to the pool. This should be done when the caller is done using the render
  79. * targets, so that other systems might re-use them. This will not release any memory unless all render targets
  80. * pointing to those textures go out of scope.
  81. */
  82. void release(RenderTargetType type);
  83. /**
  84. * Generates contents for the specified render target type. Target must first be allocated by calling allocate().
  85. * Note that not all target types can have their contents automatically generated, most are meant to be populated
  86. * by external code. And those that can usually have prerequisites. See individual target descriptions for more
  87. * details.
  88. */
  89. void generate(RenderTargetType type);
  90. /** Binds the GBuffer render target for rendering. */
  91. void bindGBuffer();
  92. /**
  93. * Binds the scene color render target for rendering. If using MSAA this texture will be allocated as a texture
  94. * with multiple samples.
  95. */
  96. void bindSceneColor(bool readOnlyDepthStencil);
  97. /** Binds the light accumulation render target for rendering. */
  98. void bindLightAccumulation();
  99. /**
  100. * Binds the light occlusion render target for rendering. Light occlusion texture and GBuffer must be allocated,
  101. * and depth must have been previously rendered to the depth buffer. Target is cleared before the method returns.
  102. */
  103. void bindLightOcclusion();
  104. /** Returns the first color texture of the gbuffer as a bindable texture. */
  105. SPtr<Texture> getGBufferA() const;
  106. /** Returns the second color texture of the gbuffer as a bindable texture. */
  107. SPtr<Texture> getGBufferB() const;
  108. /** Returns the third color texture of the gbuffer as a bindable texture. */
  109. SPtr<Texture> getGBufferC() const;
  110. /** Returns the depth buffer as a bindable texture. */
  111. SPtr<Texture> getSceneDepth() const;
  112. /** Returns the hierarchical Z buffer texture generated by calling generateHiZ(). */
  113. SPtr<Texture> getHiZ() const;
  114. /** Returns the texture containing (potentially multisampled) scene color. */
  115. SPtr<Texture> getSceneColor() const;
  116. /**
  117. * Flattened, buffer version of the texture returned by getSceneColor(). Only available when MSAA is used, since
  118. * random writes to multisampled textures aren't supported on all render backends.
  119. */
  120. SPtr<GpuBuffer> getSceneColorBuffer() const;
  121. /**
  122. * Returns a non-MSAA version of the scene color texture. If MSAA is not used this is equivalent to calling
  123. * getSceneColor() (as long as @p secondary is set to false).
  124. *
  125. * @param[in] secondary If true, a seconday scene color texture will be returned. This texture can be used
  126. * for ping-pong operations between it and the primary scene color.
  127. */
  128. SPtr<Texture> getResolvedSceneColor(bool secondary = false) const;
  129. /**
  130. * Returns a non-MSAA version of the scene color render target. If MSAA is not used this will return the default
  131. * scene color render target (as long as @p secondary is set to false).
  132. *
  133. * @param[in] secondary If true, a seconday scene color target will be returned. This target can be used
  134. * for ping-pong operations between it and the primary scene color.
  135. */
  136. SPtr<RenderTarget> getResolvedSceneColorRT(bool secondary = false) const;
  137. /**
  138. * Returns the non-MSAA version of the scene depth texture. If MSAA is not used this is equivalent to calling
  139. * getSceneDepth().
  140. */
  141. SPtr<Texture> getResolvedDepth() const;
  142. /** Returns the texture for storing of the intermediate lighting information. */
  143. SPtr<Texture> getLightAccumulation() const;
  144. /** Returns the texture for storing shadow/attenuation data for single light, from viewers perspective. */
  145. SPtr<Texture> getLightOcclusion() const;
  146. /**
  147. * Flattened, buffer version of the texture returned by getLightAccumulation(). Required when MSAA is used, since
  148. * random writes to multisampled textures aren't supported on all render backends.
  149. */
  150. SPtr<GpuBuffer> getLightAccumulationBuffer() const;
  151. /** Checks if the targets support HDR rendering. */
  152. bool getHDR() const { return mHDR; }
  153. /** Returns the number of samples per pixel supported by the targets. */
  154. UINT32 getNumSamples() const { return mViewTarget.numSamples; }
  155. /** Gets the width of the targets, in pixels. */
  156. UINT32 getWidth() const { return mWidth; }
  157. /** Gets the height of the targets, in pixels. */
  158. UINT32 getHeight() const { return mHeight; }
  159. /**
  160. * Creates a new set of render targets. Note in order to actually use the render targets you need to call the
  161. * relevant allocate* method before use.
  162. *
  163. * @param[in] view Information about the view that the render targets will be used for. Determines size
  164. * of the render targets, and the output color render target.
  165. * @param[in] hdr Should the render targets support high dynamic range rendering.
  166. */
  167. static SPtr<RenderTargets> create(const RENDERER_VIEW_TARGET_DESC& view, bool hdr);
  168. private:
  169. RenderTargets(const RENDERER_VIEW_TARGET_DESC& view, bool hdr);
  170. RENDERER_VIEW_TARGET_DESC mViewTarget;
  171. SPtr<PooledRenderTexture> mAlbedoTex;
  172. SPtr<PooledRenderTexture> mNormalTex;
  173. SPtr<PooledRenderTexture> mRoughMetalTex;
  174. SPtr<PooledRenderTexture> mDepthTex;
  175. SPtr<PooledRenderTexture> mLightAccumulationTex;
  176. SPtr<PooledRenderTexture> mLightOcclusionTex;
  177. SPtr<PooledStorageBuffer> mFlattenedLightAccumulationBuffer;
  178. SPtr<PooledRenderTexture> mSceneColorTex;
  179. SPtr<PooledStorageBuffer> mFlattenedSceneColorBuffer;
  180. SPtr<PooledRenderTexture> mResolvedSceneColorTex1;
  181. SPtr<PooledRenderTexture> mResolvedSceneColorTex2;
  182. SPtr<PooledRenderTexture> mHiZ;
  183. SPtr<PooledRenderTexture> mResolvedDepthTex;
  184. SPtr<RenderTexture> mGBufferRT;
  185. SPtr<RenderTexture> mSceneColorRT;
  186. SPtr<RenderTexture> mLightAccumulationRT;
  187. SPtr<RenderTexture> mLightOcclusionRT;
  188. PixelFormat mSceneColorFormat;
  189. PixelFormat mAlbedoFormat;
  190. PixelFormat mNormalFormat;
  191. bool mHDR;
  192. UINT32 mWidth;
  193. UINT32 mHeight;
  194. // Note: Only a single instance of this is needed, move it to a Module eventually
  195. BuildHiZ mBuildHiZ;
  196. };
  197. /** @} */
  198. }}