BsRenderTargets.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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)
  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_R8G8B8X8;
  18. mNormalFormat = PF_FLOAT_R11G11B10;
  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. // TODO - Clear gbuffer?
  51. }
  52. void RenderTargets::unbind()
  53. {
  54. RenderAPICore& rapi = RenderAPICore::instance();
  55. rapi.setRenderTarget(nullptr);
  56. RenderTexturePool& texPool = RenderTexturePool::instance();
  57. texPool.release(mDiffuseRT);
  58. texPool.release(mNormalRT);
  59. texPool.release(mDepthRT);
  60. }
  61. }