BsRenderTargets.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /**
  38. * Allocates and handles all the required render targets for rendering a scene from a specific view.
  39. *
  40. * @note Core thread only.
  41. */
  42. class RenderTargets
  43. {
  44. public:
  45. /**
  46. * Prepares any internal data for rendering. Should be called at the beginning of each frame, before allocating,
  47. * retrieving or binding any textures. Must eventually be followed by cleanup().
  48. */
  49. void prepare();
  50. /**
  51. * Cleans up any internal data after rendering. Should be called after done rendering for a frame. All allocations
  52. * must be released at this point and no further allocations or texture binds should be done until the next call
  53. * to prepare().
  54. */
  55. void cleanup();
  56. /** Returns the depth buffer as a bindable texture. */
  57. SPtr<Texture> getSceneDepth() const;
  58. /**
  59. * Allocates the textures required for rendering. Allocations are pooled so this is generally a fast operation
  60. * unless the size or other render target options changed. This must be called before binding render targets.
  61. */
  62. void allocate(RenderTargetType type);
  63. /**
  64. * Deallocates textures by returning them to the pool. This should be done when the caller is done using the render
  65. * targets, so that other systems might re-use them. This will not release any memory unless all render targets
  66. * pointing to those textures go out of scope.
  67. */
  68. void release(RenderTargetType type);
  69. /** Binds the GBuffer render target for rendering. */
  70. void bindGBuffer();
  71. /** Returns the first color texture of the gbuffer as a bindable texture. */
  72. SPtr<Texture> getGBufferA() const;
  73. /** Returns the second color texture of the gbuffer as a bindable texture. */
  74. SPtr<Texture> getGBufferB() const;
  75. /** Returns the third color texture of the gbuffer as a bindable texture. */
  76. SPtr<Texture> getGBufferC() const;
  77. /**
  78. * Binds the scene color render target for rendering. If using MSAA this texture will be allocated as a texture
  79. * with multiple samples.
  80. */
  81. void bindSceneColor(bool readOnlyDepthStencil);
  82. /** Binds the light accumulation render target for rendering. */
  83. void bindLightAccumulation();
  84. /**
  85. * Binds the light occlusion render target for rendering. Light occlusion texture and GBuffer must be allocated,
  86. * and depth must have been previously rendered to the depth buffer. Target is cleared before the method returns.
  87. */
  88. void bindLightOcclusion();
  89. /**
  90. * Generates the hierarchical Z buffer from the current contents of the scene depth buffer. Generated buffer can be
  91. * accessed from getHiZ(). When no longer required, buffer must be released by calling releaseHiZ().
  92. */
  93. void generateHiZ();
  94. /** Releases the memory used by the hierarchical z-buffer texture generated by generateHiZ(). */
  95. void releaseHiZ();
  96. /** Returns the hierarchical Z buffer texture generated by calling generateHiZ(). */
  97. SPtr<Texture> getHiZ() const;
  98. /** Returns the texture containing (potentially multisampled) scene color. */
  99. SPtr<Texture> getSceneColor() const;
  100. /**
  101. * Flattened, buffer version of the texture returned by getSceneColor(). Only available when MSAA is used, since
  102. * random writes to multisampled textures aren't supported on all render backends.
  103. */
  104. SPtr<GpuBuffer> getSceneColorBuffer() const;
  105. /**
  106. * Returns a non-MSAA version of the scene color texture. If MSAA is not used this is equivalent to calling
  107. * getSceneColor() (as long as @p secondary is set to false).
  108. *
  109. * @param[in] secondary If true, a seconday scene color texture will be returned. This texture can be used
  110. * for ping-pong operations between it and the primary scene color.
  111. */
  112. SPtr<Texture> getResolvedSceneColor(bool secondary = false) const;
  113. /**
  114. * Returns a non-MSAA version of the scene color render target. If MSAA is not used this will return the default
  115. * scene color render target (as long as @p secondary is set to false).
  116. *
  117. * @param[in] secondary If true, a seconday scene color target will be returned. This target can be used
  118. * for ping-pong operations between it and the primary scene color.
  119. */
  120. SPtr<RenderTarget> getResolvedSceneColorRT(bool secondary = false) const;
  121. /** Returns the texture for storing of the intermediate lighting information. */
  122. SPtr<Texture> getLightAccumulation() const;
  123. /** Returns the texture for storing shadow/attenuation data for single light, from viewers perspective. */
  124. SPtr<Texture> getLightOcclusion() const;
  125. /**
  126. * Flattened, buffer version of the texture returned by getLightAccumulation(). Required when MSAA is used, since
  127. * random writes to multisampled textures aren't supported on all render backends.
  128. */
  129. SPtr<GpuBuffer> getLightAccumulationBuffer() const;
  130. /** Checks if the targets support HDR rendering. */
  131. bool getHDR() const { return mHDR; }
  132. /** Returns the number of samples per pixel supported by the targets. */
  133. UINT32 getNumSamples() const { return mViewTarget.numSamples; }
  134. /** Gets the width of the targets, in pixels. */
  135. UINT32 getWidth() const { return mWidth; }
  136. /** Gets the height of the targets, in pixels. */
  137. UINT32 getHeight() const { return mHeight; }
  138. /**
  139. * Creates a new set of render targets. Note in order to actually use the render targets you need to call the
  140. * relevant allocate* method before use.
  141. *
  142. * @param[in] view Information about the view that the render targets will be used for. Determines size
  143. * of the render targets, and the output color render target.
  144. * @param[in] hdr Should the render targets support high dynamic range rendering.
  145. */
  146. static SPtr<RenderTargets> create(const RENDERER_VIEW_TARGET_DESC& view, bool hdr);
  147. private:
  148. RenderTargets(const RENDERER_VIEW_TARGET_DESC& view, bool hdr);
  149. RENDERER_VIEW_TARGET_DESC mViewTarget;
  150. SPtr<PooledRenderTexture> mAlbedoTex;
  151. SPtr<PooledRenderTexture> mNormalTex;
  152. SPtr<PooledRenderTexture> mRoughMetalTex;
  153. SPtr<PooledRenderTexture> mDepthTex;
  154. SPtr<PooledRenderTexture> mLightAccumulationTex;
  155. SPtr<PooledRenderTexture> mLightOcclusionTex;
  156. SPtr<PooledStorageBuffer> mFlattenedLightAccumulationBuffer;
  157. SPtr<PooledRenderTexture> mSceneColorTex;
  158. SPtr<PooledStorageBuffer> mFlattenedSceneColorBuffer;
  159. SPtr<PooledRenderTexture> mResolvedSceneColorTex1;
  160. SPtr<PooledRenderTexture> mResolvedSceneColorTex2;
  161. SPtr<PooledRenderTexture> mHiZ;
  162. SPtr<RenderTexture> mGBufferRT;
  163. SPtr<RenderTexture> mSceneColorRT;
  164. SPtr<RenderTexture> mLightAccumulationRT;
  165. SPtr<RenderTexture> mLightOcclusionRT;
  166. PixelFormat mSceneColorFormat;
  167. PixelFormat mAlbedoFormat;
  168. PixelFormat mNormalFormat;
  169. bool mHDR;
  170. UINT32 mWidth;
  171. UINT32 mHeight;
  172. // Note: Only a single instance of this is needed, move it to a Module eventually
  173. BuildHiZ mBuildHiZ;
  174. };
  175. /** @} */
  176. }}