RendererObject.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Renderer/Common.h>
  7. #include <AnKi/Util/StdTypes.h>
  8. #include <AnKi/Gr.h>
  9. #include <AnKi/Resource/ResourceManager.h>
  10. #include <AnKi/Resource/ShaderProgramResource.h>
  11. #include <AnKi/Core/GpuMemory/RebarTransientMemoryPool.h>
  12. namespace anki {
  13. // Forward
  14. class Renderer;
  15. class ResourceManager;
  16. /// @addtogroup renderer
  17. /// @{
  18. /// Renderer object.
  19. class RendererObject
  20. {
  21. public:
  22. RendererObject() = default;
  23. virtual ~RendererObject() = default;
  24. virtual void getDebugRenderTarget([[maybe_unused]] CString rtName,
  25. [[maybe_unused]] Array<RenderTargetHandle, kMaxDebugRenderTargets>& handles,
  26. [[maybe_unused]] ShaderProgramPtr& optionalShaderProgram) const
  27. {
  28. ANKI_ASSERT(!"Object doesn't support that");
  29. }
  30. protected:
  31. static ANKI_PURE Renderer& getRenderer();
  32. void* allocateRebarStagingMemory(PtrSize size, RebarAllocation& token);
  33. U32 computeNumberOfSecondLevelCommandBuffers(U32 drawcallCount) const;
  34. /// Used in fullscreen quad draws.
  35. static void drawQuad(CommandBufferPtr& cmdb)
  36. {
  37. cmdb->drawArrays(PrimitiveTopology::kTriangles, 3, 1);
  38. }
  39. /// Dispatch a compute job equivelent to drawQuad
  40. static void dispatchPPCompute(CommandBufferPtr& cmdb, U32 workgroupSizeX, U32 workgroupSizeY, U32 outImageWidth,
  41. U32 outImageHeight)
  42. {
  43. const U32 sizeX = (outImageWidth + workgroupSizeX - 1) / workgroupSizeX;
  44. const U32 sizeY = (outImageHeight + workgroupSizeY - 1) / workgroupSizeY;
  45. cmdb->dispatchCompute(sizeX, sizeY, 1);
  46. }
  47. static void dispatchPPCompute(CommandBufferPtr& cmdb, U32 workgroupSizeX, U32 workgroupSizeY, U32 workgroupSizeZ,
  48. U32 outImageWidth, U32 outImageHeight, U32 outImageDepth)
  49. {
  50. const U32 sizeX = (outImageWidth + workgroupSizeX - 1) / workgroupSizeX;
  51. const U32 sizeY = (outImageHeight + workgroupSizeY - 1) / workgroupSizeY;
  52. const U32 sizeZ = (outImageDepth + workgroupSizeZ - 1) / workgroupSizeZ;
  53. cmdb->dispatchCompute(sizeX, sizeY, sizeZ);
  54. }
  55. template<typename TPtr>
  56. TPtr allocateUniforms(PtrSize size, RebarAllocation& token)
  57. {
  58. return static_cast<TPtr>(allocateRebarStagingMemory(size, token));
  59. }
  60. void bindUniforms(CommandBufferPtr& cmdb, U32 set, U32 binding, const RebarAllocation& token) const;
  61. template<typename TPtr>
  62. TPtr allocateAndBindUniforms(PtrSize size, CommandBufferPtr& cmdb, U32 set, U32 binding)
  63. {
  64. RebarAllocation token;
  65. TPtr ptr = allocateUniforms<TPtr>(size, token);
  66. bindUniforms(cmdb, set, binding, token);
  67. return ptr;
  68. }
  69. template<typename TPtr>
  70. TPtr allocateStorage(PtrSize size, RebarAllocation& token)
  71. {
  72. return static_cast<TPtr>(allocateRebarStagingMemory(size, token));
  73. }
  74. void bindStorage(CommandBufferPtr& cmdb, U32 set, U32 binding, const RebarAllocation& token) const;
  75. template<typename TPtr>
  76. TPtr allocateAndBindStorage(PtrSize size, CommandBufferPtr& cmdb, U32 set, U32 binding)
  77. {
  78. RebarAllocation token;
  79. TPtr ptr = allocateStorage<TPtr>(size, token);
  80. bindStorage(cmdb, set, binding, token);
  81. return ptr;
  82. }
  83. void registerDebugRenderTarget(CString rtName);
  84. static Error loadShaderProgram(CString filename, ShaderProgramResourcePtr& rsrc, ShaderProgramPtr& grProg);
  85. };
  86. /// @}
  87. } // end namespace anki