BsRenderTargets.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "BsRenderTargets.h"
  2. #include "BsRenderTexturePool.h"
  3. #include "BsViewport.h"
  4. #include "BsRenderAPI.h"
  5. #include "BsTextureManager.h"
  6. namespace BansheeEngine
  7. {
  8. RenderTargets::RenderTargets(const SPtr<ViewportCore>& viewport, bool hdr, UINT32 numSamples)
  9. :mNumSamples(numSamples), mHDR(hdr), mViewport(viewport)
  10. {
  11. if (hdr)
  12. mDiffuseFormat = PF_FLOAT_R11G11B10;
  13. else
  14. mDiffuseFormat = PF_B8G8R8X8;
  15. mNormalFormat = PF_UNORM_R10G10B10A2;
  16. }
  17. SPtr<RenderTargets> RenderTargets::create(const SPtr<ViewportCore>& viewport, bool hdr, UINT32 numSamples)
  18. {
  19. return bs_shared_ptr<RenderTargets>(new (bs_alloc<RenderTargets>()) RenderTargets(viewport, hdr, numSamples));
  20. }
  21. void RenderTargets::allocate()
  22. {
  23. RenderTexturePool& texPool = RenderTexturePool::instance();
  24. UINT32 width = getWidth();
  25. UINT32 height = getHeight();
  26. SPtr<PooledRenderTexture> newAlbedoRT = texPool.get(mDiffuseFormat, width, height, false, mNumSamples);
  27. SPtr<PooledRenderTexture> newNormalRT = texPool.get(mNormalFormat, width, height, false, mNumSamples);
  28. SPtr<PooledRenderTexture> newDepthRT = texPool.get(PF_D24S8, width, height, false, mNumSamples);
  29. SPtr<PooledRenderTexture> newColorRT = nullptr;
  30. // See if I can use the final output color target for gbuffer rendering, this saves a little memory
  31. SPtr<RenderTargetCore> resolvedRT = mViewport->getTarget();
  32. const RenderTargetProperties& resolvedRTProps = resolvedRT->getProperties();
  33. bool useResolvedColor = !resolvedRTProps.isWindow() &&
  34. mViewport->getWidth() == getWidth() &&
  35. mViewport->getHeight() == getHeight() &&
  36. ((resolvedRTProps.getMultisampleCount() <= 1) == (mNumSamples <= 1) ||
  37. resolvedRTProps.getMultisampleCount() == mNumSamples);
  38. if (!useResolvedColor)
  39. newColorRT = texPool.get(PF_B8G8R8X8, width, height, false, mNumSamples);
  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 || rebuildTargets)
  46. {
  47. MULTI_RENDER_TEXTURE_CORE_DESC gbufferDesc;
  48. gbufferDesc.colorSurfaces.resize(3);
  49. SPtr<TextureCore> sceneColorTex = nullptr;
  50. if (newColorRT != nullptr)
  51. sceneColorTex = newColorRT->texture;
  52. else // Re-using output scene color texture
  53. {
  54. SPtr<RenderTextureCore> resolvedRTex = std::static_pointer_cast<RenderTextureCore>(resolvedRT);
  55. sceneColorTex = resolvedRTex->getBindableColorTexture();
  56. }
  57. gbufferDesc.colorSurfaces[0].texture = sceneColorTex;
  58. gbufferDesc.colorSurfaces[0].face = 0;
  59. gbufferDesc.colorSurfaces[0].mipLevel = 0;
  60. gbufferDesc.colorSurfaces[1].texture = mAlbedoTex->texture;
  61. gbufferDesc.colorSurfaces[1].face = 0;
  62. gbufferDesc.colorSurfaces[1].mipLevel = 0;
  63. gbufferDesc.colorSurfaces[2].texture = mNormalTex->texture;
  64. gbufferDesc.colorSurfaces[2].face = 0;
  65. gbufferDesc.colorSurfaces[2].mipLevel = 0;
  66. gbufferDesc.depthStencilSurface.texture = mDepthTex->texture;
  67. gbufferDesc.depthStencilSurface.face = 0;
  68. gbufferDesc.depthStencilSurface.mipLevel = 0;
  69. mGBufferRT = TextureCoreManager::instance().createMultiRenderTexture(gbufferDesc);
  70. }
  71. }
  72. void RenderTargets::release()
  73. {
  74. RenderAPICore& rapi = RenderAPICore::instance();
  75. rapi.setRenderTarget(nullptr);
  76. RenderTexturePool& texPool = RenderTexturePool::instance();
  77. texPool.release(mAlbedoTex);
  78. texPool.release(mNormalTex);
  79. texPool.release(mDepthTex);
  80. }
  81. void RenderTargets::bind()
  82. {
  83. RenderAPICore& rapi = RenderAPICore::instance();
  84. rapi.setRenderTarget(mGBufferRT);
  85. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  86. rapi.setViewport(area);
  87. }
  88. SPtr<TextureCore> RenderTargets::getTextureA() const
  89. {
  90. return mAlbedoTex->texture;
  91. }
  92. SPtr<TextureCore> RenderTargets::getTextureB() const
  93. {
  94. return mNormalTex->texture;
  95. }
  96. SPtr<TextureCore> RenderTargets::getTextureDepth() const
  97. {
  98. return mDepthTex->texture;
  99. }
  100. UINT32 RenderTargets::getWidth() const
  101. {
  102. return (UINT32)mViewport->getWidth();
  103. }
  104. UINT32 RenderTargets::getHeight() const
  105. {
  106. return (UINT32)mViewport->getHeight();
  107. }
  108. }