Ms.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "anki/renderer/Ms.h"
  6. #include "anki/renderer/Renderer.h"
  7. #include "anki/util/Logger.h"
  8. #include "anki/scene/Camera.h"
  9. #include "anki/scene/SceneGraph.h"
  10. #include "anki/misc/ConfigSet.h"
  11. namespace anki {
  12. //==============================================================================
  13. const Array<PixelFormat, Ms::ATTACHMENT_COUNT> Ms::RT_PIXEL_FORMATS = {
  14. PixelFormat(ComponentFormat::R8G8B8A8, TransformFormat::UNORM),
  15. PixelFormat(ComponentFormat::R8G8B8A8, TransformFormat::UNORM),
  16. PixelFormat(ComponentFormat::R10G10B10A2, TransformFormat::UNORM)};
  17. const PixelFormat Ms::DEPTH_RT_PIXEL_FORMAT(
  18. ComponentFormat::D24, TransformFormat::FLOAT);
  19. //==============================================================================
  20. Ms::~Ms()
  21. {}
  22. //==============================================================================
  23. Error Ms::createRt(U32 index, U32 samples)
  24. {
  25. Plane& plane = m_planes[index];
  26. m_r->createRenderTarget(m_r->getWidth(), m_r->getHeight(),
  27. DEPTH_RT_PIXEL_FORMAT, samples, SamplingFilter::NEAREST, 4,
  28. plane.m_depthRt);
  29. m_r->createRenderTarget(m_r->getWidth(), m_r->getHeight(),
  30. RT_PIXEL_FORMATS[0], samples, SamplingFilter::NEAREST, 1, plane.m_rt0);
  31. m_r->createRenderTarget(m_r->getWidth(), m_r->getHeight(),
  32. RT_PIXEL_FORMATS[1], samples, SamplingFilter::NEAREST, 1, plane.m_rt1);
  33. m_r->createRenderTarget(m_r->getWidth(), m_r->getHeight(),
  34. RT_PIXEL_FORMATS[2], samples, SamplingFilter::NEAREST, 1, plane.m_rt2);
  35. AttachmentLoadOperation loadop = AttachmentLoadOperation::DONT_CARE;
  36. #if ANKI_DEBUG
  37. loadop = AttachmentLoadOperation::CLEAR;
  38. #endif
  39. FramebufferPtr::Initializer fbInit;
  40. fbInit.m_colorAttachmentsCount = ATTACHMENT_COUNT;
  41. fbInit.m_colorAttachments[0].m_texture = plane.m_rt0;
  42. fbInit.m_colorAttachments[0].m_loadOperation = loadop;
  43. fbInit.m_colorAttachments[0].m_clearValue.m_colorf = {{1.0, 0.0, 0.0, 0.0}};
  44. fbInit.m_colorAttachments[1].m_texture = plane.m_rt1;
  45. fbInit.m_colorAttachments[1].m_loadOperation = loadop;
  46. fbInit.m_colorAttachments[1].m_clearValue.m_colorf = {{0.0, 1.0, 0.0, 0.0}};
  47. fbInit.m_colorAttachments[2].m_texture = plane.m_rt2;
  48. fbInit.m_colorAttachments[2].m_loadOperation = loadop;
  49. fbInit.m_colorAttachments[2].m_clearValue.m_colorf = {{0.0, 0.0, 1.0, 0.0}};
  50. fbInit.m_depthStencilAttachment.m_texture = plane.m_depthRt;
  51. fbInit.m_depthStencilAttachment.m_loadOperation =
  52. AttachmentLoadOperation::CLEAR;
  53. fbInit.m_depthStencilAttachment.m_clearValue.m_depthStencil.m_depth = 1.0;
  54. plane.m_fb.create(&getGrManager(), fbInit);
  55. return ErrorCode::NONE;
  56. }
  57. //==============================================================================
  58. Error Ms::init(const ConfigSet& initializer)
  59. {
  60. Error err = initInternal(initializer);
  61. if(err)
  62. {
  63. ANKI_LOGE("Failed to initialize material stage");
  64. }
  65. return err;
  66. }
  67. //==============================================================================
  68. Error Ms::initInternal(const ConfigSet& initializer)
  69. {
  70. if(initializer.getNumber("samples") > 1)
  71. {
  72. ANKI_CHECK(createRt(0, initializer.getNumber("samples")));
  73. }
  74. ANKI_CHECK(createRt(1, 1));
  75. return ErrorCode::NONE;
  76. }
  77. //==============================================================================
  78. Error Ms::run(CommandBufferPtr& cmdb)
  79. {
  80. // Chose the multisampled or the singlesampled framebuffer
  81. U planeId = 0;
  82. if(m_r->getSamples() == 1)
  83. {
  84. planeId = 1;
  85. }
  86. cmdb.setViewport(0, 0, m_r->getWidth(), m_r->getHeight());
  87. m_planes[planeId].m_fb.bind(cmdb);
  88. // render all
  89. m_r->getSceneDrawer().prepareDraw(
  90. RenderingStage::MATERIAL, Pass::MS_FS, cmdb);
  91. SceneNode& cam = m_r->getActiveCamera();
  92. VisibilityTestResults& vi =
  93. cam.getComponent<FrustumComponent>().getVisibilityTestResults();
  94. auto it = vi.getRenderablesBegin();
  95. auto end = vi.getRenderablesEnd();
  96. for(; it != end; ++it)
  97. {
  98. ANKI_CHECK(m_r->getSceneDrawer().render(cam, *it));
  99. }
  100. m_r->getSceneDrawer().finishDraw();
  101. // If there is multisampling then resolve to singlesampled
  102. if(m_r->getSamples() > 1)
  103. {
  104. #if 0
  105. fbo[1].blitFrom(fbo[0], UVec2(0U), UVec2(r->getWidth(), r->getHeight()),
  106. UVec2(0U), UVec2(r->getWidth(), r->getHeight()),
  107. GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT,
  108. GL_NEAREST_BASE);
  109. #endif
  110. ANKI_ASSERT(0 && "TODO");
  111. }
  112. return ErrorCode::NONE;
  113. }
  114. //==============================================================================
  115. void Ms::generateMipmaps(CommandBufferPtr& cmdb)
  116. {
  117. U planeId = (m_r->getSamples() == 1) ? 1 : 0;
  118. m_planes[planeId].m_depthRt.generateMipmaps(cmdb);
  119. }
  120. } // end namespace anki