BsRenderTargets.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "BsRendererCamera.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 final scene color information. */
  23. RTT_SceneColor
  24. };
  25. /**
  26. * Allocates and handles all the required render targets for rendering a scene from a specific view.
  27. *
  28. * @note Core thread only.
  29. */
  30. class RenderTargets
  31. {
  32. public:
  33. /**
  34. * Prepares any internal data for rendering. Should be called at the beginning of each frame, before allocating,
  35. * retrieving or binding any textures. Must eventually be followed by cleanup().
  36. */
  37. void prepare();
  38. /**
  39. * Cleans up any internal data after rendering. Should be called after done rendering for a frame. All allocations
  40. * must be released at this point and no further allocations or texture binds should be done until the next call
  41. * to prepare().
  42. */
  43. void cleanup();
  44. /** Returns the depth buffer as a bindable texture. */
  45. SPtr<Texture> getSceneDepth() const;
  46. /**
  47. * Allocates the textures required for rendering. Allocations are pooled so this is generally a fast operation
  48. * unless the size or other render target options changed. This must be called before binding render targets.
  49. */
  50. void allocate(RenderTargetType type);
  51. /**
  52. * Deallocates textures by returning them to the pool. This should be done when the caller is done using the render
  53. * targets, so that other systems might re-use them. This will not release any memory unless all render targets
  54. * pointing to those textures go out of scope.
  55. */
  56. void release(RenderTargetType type);
  57. /** Binds the GBuffer render target for rendering. */
  58. void bindGBuffer();
  59. /** Returns the first color texture of the gbuffer as a bindable texture. */
  60. SPtr<Texture> getGBufferA() const;
  61. /** Returns the second color texture of the gbuffer as a bindable texture. */
  62. SPtr<Texture> getGBufferB() const;
  63. /** Returns the third color texture of the gbuffer as a bindable texture. */
  64. SPtr<Texture> getGBufferC() const;
  65. /** Binds the scene color render target for rendering. */
  66. void bindSceneColor(bool readOnlyDepthStencil);
  67. /**
  68. * Returns the texture for storing the final scene color. If using MSAA see getSceneColorBuffer() instead. Only
  69. * available after bindSceneColor() has been called from this frame.
  70. **/
  71. SPtr<Texture> getSceneColor() const;
  72. /**
  73. * Flattened, buffer version of the texture returned by getSceneColor(). Required when MSAA is used, since
  74. * random writes to multisampled textures aren't supported on all render backends.
  75. */
  76. SPtr<GpuBuffer> getSceneColorBuffer() const;
  77. /** Returns the texture for storing of the intermediate lighting information. */
  78. SPtr<Texture> getLightAccumulation() const;
  79. /**
  80. * Flattened, buffer version of the texture returned by getLightAccumulation(). Required when MSAA is used, since
  81. * random writes to multisampled textures aren't supported on all render backends.
  82. */
  83. SPtr<GpuBuffer> getLightAccumulationBuffer() const;
  84. /**
  85. * Returns a scene color texture with a single-sample per pixel. If no multisampling is used, this is the same as
  86. * getSceneColor().
  87. */
  88. SPtr<Texture> getResolvedSceneColor() const;
  89. /** Returns a render target that can be used for rendering to the texture returned by getResolvedSceneColor(). */
  90. SPtr<RenderTexture> getResolvedSceneColorRT() const;
  91. /** Checks if the targets support HDR rendering. */
  92. bool getHDR() const { return mHDR; }
  93. /** Returns the number of samples per pixel supported by the targets. */
  94. UINT32 getNumSamples() const { return mViewTarget.numSamples; }
  95. /** Gets the width of the targets, in pixels. */
  96. UINT32 getWidth() const { return mWidth; }
  97. /** Gets the height of the targets, in pixels. */
  98. UINT32 getHeight() const { return mHeight; }
  99. /**
  100. * Creates a new set of render targets. Note in order to actually use the render targets you need to call the
  101. * relevant allocate* method before use.
  102. *
  103. * @param[in] view Information about the view that the render targets will be used for. Determines size
  104. * of the render targets, and the output color render target.
  105. * @param[in] hdr Should the render targets support high dynamic range rendering.
  106. */
  107. static SPtr<RenderTargets> create(const RENDERER_VIEW_TARGET_DESC& view, bool hdr);
  108. private:
  109. RenderTargets(const RENDERER_VIEW_TARGET_DESC& view, bool hdr);
  110. RENDERER_VIEW_TARGET_DESC mViewTarget;
  111. SPtr<PooledRenderTexture> mAlbedoTex;
  112. SPtr<PooledRenderTexture> mNormalTex;
  113. SPtr<PooledRenderTexture> mRoughMetalTex;
  114. SPtr<PooledRenderTexture> mDepthTex;
  115. SPtr<PooledRenderTexture> mLightAccumulationTex;
  116. SPtr<PooledStorageBuffer> mFlattenedLightAccumulationBuffer;
  117. SPtr<PooledRenderTexture> mSceneColorTex;
  118. SPtr<PooledRenderTexture> mSceneColorNonMSAATex;
  119. SPtr<PooledStorageBuffer> mFlattenedSceneColorBuffer;
  120. SPtr<RenderTexture> mGBufferRT;
  121. SPtr<RenderTexture> mSceneColorRT;
  122. PixelFormat mSceneColorFormat;
  123. PixelFormat mAlbedoFormat;
  124. PixelFormat mNormalFormat;
  125. bool mHDR;
  126. UINT32 mWidth;
  127. UINT32 mHeight;
  128. };
  129. /** @} */
  130. }}