BsRenderTargets.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsRenderTargets.h"
  4. #include "BsRenderTexturePool.h"
  5. #include "BsViewport.h"
  6. #include "BsRenderAPI.h"
  7. #include "BsTextureManager.h"
  8. #include "BsRendererUtility.h"
  9. namespace BansheeEngine
  10. {
  11. RenderTargets::RenderTargets(const SPtr<ViewportCore>& viewport, bool hdr, UINT32 numSamples)
  12. :mViewport(viewport), mNumSamples(numSamples), mHDR(hdr)
  13. {
  14. // Note: Consider customizable HDR format via options? e.g. smaller PF_FLOAT_R11G11B10 or larger 32-bit format
  15. mSceneColorFormat = hdr ? PF_FLOAT16_RGBA : PF_B8G8R8A8;
  16. mAlbedoFormat = PF_B8G8R8X8; // Note: Also consider customizable format (e.g. 16-bit float?)
  17. mNormalFormat = PF_UNORM_R10G10B10A2; // Note: Also consider customizable format (e.g. 16-bit float?)
  18. }
  19. SPtr<RenderTargets> RenderTargets::create(const SPtr<ViewportCore>& viewport, bool hdr, UINT32 numSamples)
  20. {
  21. return bs_shared_ptr<RenderTargets>(new (bs_alloc<RenderTargets>()) RenderTargets(viewport, hdr, numSamples));
  22. }
  23. void RenderTargets::allocate()
  24. {
  25. RenderTexturePool& texPool = RenderTexturePool::instance();
  26. UINT32 width = getWidth();
  27. UINT32 height = getHeight();
  28. // Note: Albedo is allocated as SRGB, meaning when reading from textures during depth pass we decode from sRGB into linear,
  29. // then back into sRGB when writing to albedo, and back to linear when reading from albedo during light pass. This /might/ have
  30. // a performance impact. In which case we could just use a higher precision albedo buffer, which can then store linear color
  31. // directly (storing linear in 8bit buffer causes too much detail to be lost in the blacks).
  32. SPtr<PooledRenderTexture> newColorRT = texPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(mSceneColorFormat, width, height, TU_RENDERTARGET,
  33. mNumSamples, false));
  34. SPtr<PooledRenderTexture> newAlbedoRT = texPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(mAlbedoFormat, width,
  35. height, TU_RENDERTARGET, mNumSamples, true));
  36. SPtr<PooledRenderTexture> newNormalRT = texPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(mNormalFormat, width,
  37. height, TU_RENDERTARGET, mNumSamples, false));
  38. SPtr<PooledRenderTexture> newDepthRT = texPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_D24S8, width, height,
  39. TU_DEPTHSTENCIL, mNumSamples, false));
  40. bool rebuildTargets = newColorRT != mSceneColorTex || newAlbedoRT != mAlbedoTex || newNormalRT != mNormalTex || newDepthRT != mDepthTex;
  41. mSceneColorTex = newColorRT;
  42. mAlbedoTex = newAlbedoRT;
  43. mNormalTex = newNormalRT;
  44. mDepthTex = newDepthRT;
  45. if (mGBufferRT == nullptr || mSceneColorRT == nullptr || rebuildTargets)
  46. {
  47. RENDER_TEXTURE_DESC_CORE gbufferDesc;
  48. gbufferDesc.colorSurfaces[0].texture = mSceneColorTex->texture;
  49. gbufferDesc.colorSurfaces[0].face = 0;
  50. gbufferDesc.colorSurfaces[0].numFaces = 1;
  51. gbufferDesc.colorSurfaces[0].mipLevel = 0;
  52. gbufferDesc.colorSurfaces[1].texture = mAlbedoTex->texture;
  53. gbufferDesc.colorSurfaces[1].face = 0;
  54. gbufferDesc.colorSurfaces[1].numFaces = 1;
  55. gbufferDesc.colorSurfaces[1].mipLevel = 0;
  56. gbufferDesc.colorSurfaces[2].texture = mNormalTex->texture;
  57. gbufferDesc.colorSurfaces[2].face = 0;
  58. gbufferDesc.colorSurfaces[2].numFaces = 1;
  59. gbufferDesc.colorSurfaces[2].mipLevel = 0;
  60. gbufferDesc.depthStencilSurface.texture = mDepthTex->texture;
  61. gbufferDesc.depthStencilSurface.face = 0;
  62. gbufferDesc.depthStencilSurface.mipLevel = 0;
  63. mGBufferRT = RenderTextureCore::create(gbufferDesc);
  64. RENDER_TEXTURE_DESC_CORE sceneColorDesc;
  65. sceneColorDesc.colorSurfaces[0].texture = mSceneColorTex->texture;
  66. sceneColorDesc.colorSurfaces[0].face = 0;
  67. sceneColorDesc.colorSurfaces[0].numFaces = 1;
  68. sceneColorDesc.colorSurfaces[0].mipLevel = 0;
  69. sceneColorDesc.depthStencilSurface.texture = mDepthTex->texture;
  70. sceneColorDesc.depthStencilSurface.face = 0;
  71. sceneColorDesc.depthStencilSurface.numFaces = 1;
  72. sceneColorDesc.depthStencilSurface.mipLevel = 0;
  73. mSceneColorRT = TextureCoreManager::instance().createRenderTexture(sceneColorDesc);
  74. }
  75. }
  76. void RenderTargets::release()
  77. {
  78. RenderAPICore& rapi = RenderAPICore::instance();
  79. rapi.setRenderTarget(nullptr);
  80. RenderTexturePool& texPool = RenderTexturePool::instance();
  81. texPool.release(mSceneColorTex);
  82. texPool.release(mAlbedoTex);
  83. texPool.release(mNormalTex);
  84. texPool.release(mDepthTex);
  85. }
  86. void RenderTargets::bindGBuffer()
  87. {
  88. RenderAPICore& rapi = RenderAPICore::instance();
  89. rapi.setRenderTarget(mGBufferRT);
  90. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  91. rapi.setViewport(area);
  92. UINT32 clearBuffers = 0;
  93. if (mViewport->getRequiresColorClear())
  94. clearBuffers |= FBT_COLOR;
  95. if (mViewport->getRequiresDepthClear())
  96. clearBuffers |= FBT_DEPTH;
  97. if (mViewport->getRequiresStencilClear())
  98. clearBuffers |= FBT_STENCIL;
  99. // Clear scene color, depth, stencil according to user defined values
  100. if (clearBuffers != 0)
  101. {
  102. RenderAPICore::instance().clearViewport(clearBuffers, mViewport->getClearColor(),
  103. mViewport->getClearDepthValue(), mViewport->getClearStencilValue(), 0x01);
  104. }
  105. // Clear all others
  106. RenderAPICore::instance().clearViewport(FBT_COLOR, Color::ZERO, 1.0f, 0, 0xFF & ~0x01);
  107. }
  108. void RenderTargets::bindSceneColor(bool readOnlyDepthStencil)
  109. {
  110. RenderAPICore& rapi = RenderAPICore::instance();
  111. rapi.setRenderTarget(mSceneColorRT, readOnlyDepthStencil);
  112. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  113. rapi.setViewport(area);
  114. }
  115. SPtr<TextureCore> RenderTargets::getTextureA() const
  116. {
  117. return mAlbedoTex->texture;
  118. }
  119. SPtr<TextureCore> RenderTargets::getTextureB() const
  120. {
  121. return mNormalTex->texture;
  122. }
  123. SPtr<TextureCore> RenderTargets::getTextureDepth() const
  124. {
  125. return mDepthTex->texture;
  126. }
  127. UINT32 RenderTargets::getWidth() const
  128. {
  129. return (UINT32)mViewport->getWidth();
  130. }
  131. UINT32 RenderTargets::getHeight() const
  132. {
  133. return (UINT32)mViewport->getHeight();
  134. }
  135. }