BsRenderTargets.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 ViewportCore& viewport, bool hdr, UINT32 numSamples)
  9. :mNumSamples(numSamples), mHDR(hdr)
  10. {
  11. // TODO - Round up width/height so it's divisible by 8?
  12. mWidth = (UINT32)viewport.getWidth();
  13. mHeight = (UINT32)viewport.getHeight();
  14. if (hdr)
  15. mDiffuseFormat = PF_FLOAT_R11G11B10;
  16. else
  17. mDiffuseFormat = PF_B8G8R8X8;
  18. mNormalFormat = PF_UNORM_R10G10B10A2;
  19. }
  20. SPtr<RenderTargets> RenderTargets::create(const ViewportCore& viewport, bool hdr, UINT32 numSamples)
  21. {
  22. return bs_shared_ptr<RenderTargets>(new (bs_alloc<RenderTargets>()) RenderTargets(viewport, hdr, numSamples));
  23. }
  24. void RenderTargets::bind()
  25. {
  26. RenderTexturePool& texPool = RenderTexturePool::instance();
  27. mDiffuseRT = texPool.get(mDiffuseFormat, mWidth, mHeight, false, mNumSamples);
  28. mNormalRT = texPool.get(mNormalFormat, mWidth, mHeight, false, mNumSamples);
  29. mDepthRT = texPool.get(PF_D24S8, mWidth, mHeight, false, mNumSamples);
  30. // Note: I'm making an assumption here that textures retrieved from render texture pool
  31. // won't change, which should be true as long as I don't request these same sizes & formats
  32. // somewhere else at the same time while binding the gbuffer (which shouldn't happen).
  33. if (mGBuffer == nullptr)
  34. {
  35. MULTI_RENDER_TEXTURE_CORE_DESC gbufferDesc;
  36. gbufferDesc.colorSurfaces.resize(2);
  37. gbufferDesc.colorSurfaces[0].texture = mDiffuseRT->texture;
  38. gbufferDesc.colorSurfaces[0].face = 0;
  39. gbufferDesc.colorSurfaces[0].mipLevel = 0;
  40. gbufferDesc.colorSurfaces[1].texture = mNormalRT->texture;
  41. gbufferDesc.colorSurfaces[1].face = 0;
  42. gbufferDesc.colorSurfaces[1].mipLevel = 0;
  43. gbufferDesc.depthStencilSurface.texture = mDepthRT->texture;
  44. gbufferDesc.depthStencilSurface.face = 0;
  45. gbufferDesc.depthStencilSurface.mipLevel = 0;
  46. mGBuffer = TextureCoreManager::instance().createMultiRenderTexture(gbufferDesc);
  47. }
  48. RenderAPICore& rapi = RenderAPICore::instance();
  49. rapi.setRenderTarget(mGBuffer);
  50. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  51. rapi.setViewport(area);
  52. }
  53. void RenderTargets::unbind()
  54. {
  55. RenderAPICore& rapi = RenderAPICore::instance();
  56. rapi.setRenderTarget(nullptr);
  57. RenderTexturePool& texPool = RenderTexturePool::instance();
  58. texPool.release(mDiffuseRT);
  59. texPool.release(mNormalRT);
  60. texPool.release(mDepthRT);
  61. }
  62. SPtr<TextureCore> RenderTargets::getTextureA() const
  63. {
  64. return mDiffuseRT->texture;
  65. }
  66. SPtr<TextureCore> RenderTargets::getTextureB() const
  67. {
  68. return mNormalRT->texture;
  69. }
  70. SPtr<TextureCore> RenderTargets::getTextureDepth() const
  71. {
  72. return mDepthRT->texture;
  73. }
  74. }