RendererObject.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2009-2021, 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/StagingGpuMemoryManager.h>
  12. namespace anki
  13. {
  14. // Forward
  15. class Renderer;
  16. class ResourceManager;
  17. class ConfigSet;
  18. /// @addtogroup renderer
  19. /// @{
  20. /// Renderer object.
  21. class RendererObject
  22. {
  23. public:
  24. RendererObject(Renderer* r)
  25. : m_r(r)
  26. {
  27. }
  28. virtual ~RendererObject()
  29. {
  30. }
  31. HeapAllocator<U8> getAllocator() const;
  32. virtual void getDebugRenderTarget(CString rtName, RenderTargetHandle& handle,
  33. ShaderProgramPtr& optionalShaderProgram) const
  34. {
  35. ANKI_ASSERT(!"Object doesn't support that");
  36. }
  37. protected:
  38. Renderer* m_r; ///< Know your father
  39. GrManager& getGrManager();
  40. const GrManager& getGrManager() const;
  41. ResourceManager& getResourceManager();
  42. void* allocateFrameStagingMemory(PtrSize size, StagingGpuMemoryType usage, StagingGpuMemoryToken& token);
  43. U32 computeNumberOfSecondLevelCommandBuffers(U32 drawcallCount) const;
  44. /// Used in fullscreen quad draws.
  45. static void drawQuad(CommandBufferPtr& cmdb)
  46. {
  47. cmdb->drawArrays(PrimitiveTopology::TRIANGLES, 3, 1);
  48. }
  49. /// Dispatch a compute job equivelent to drawQuad
  50. static void dispatchPPCompute(CommandBufferPtr& cmdb, U32 workgroupSizeX, U32 workgroupSizeY, U32 outImageWidth,
  51. U32 outImageHeight)
  52. {
  53. const U32 sizeX = (outImageWidth + workgroupSizeX - 1) / workgroupSizeX;
  54. const U32 sizeY = (outImageHeight + workgroupSizeY - 1) / workgroupSizeY;
  55. cmdb->dispatchCompute(sizeX, sizeY, 1);
  56. }
  57. static void dispatchPPCompute(CommandBufferPtr& cmdb, U32 workgroupSizeX, U32 workgroupSizeY, U32 workgroupSizeZ,
  58. U32 outImageWidth, U32 outImageHeight, U32 outImageDepth)
  59. {
  60. const U32 sizeX = (outImageWidth + workgroupSizeX - 1) / workgroupSizeX;
  61. const U32 sizeY = (outImageHeight + workgroupSizeY - 1) / workgroupSizeY;
  62. const U32 sizeZ = (outImageDepth + workgroupSizeZ - 1) / workgroupSizeZ;
  63. cmdb->dispatchCompute(sizeX, sizeY, sizeZ);
  64. }
  65. template<typename TPtr>
  66. TPtr allocateUniforms(PtrSize size, StagingGpuMemoryToken& token)
  67. {
  68. return static_cast<TPtr>(allocateFrameStagingMemory(size, StagingGpuMemoryType::UNIFORM, token));
  69. }
  70. void bindUniforms(CommandBufferPtr& cmdb, U32 set, U32 binding, const StagingGpuMemoryToken& token) const;
  71. template<typename TPtr>
  72. TPtr allocateAndBindUniforms(PtrSize size, CommandBufferPtr& cmdb, U32 set, U32 binding)
  73. {
  74. StagingGpuMemoryToken token;
  75. TPtr ptr = allocateUniforms<TPtr>(size, token);
  76. bindUniforms(cmdb, set, binding, token);
  77. return ptr;
  78. }
  79. template<typename TPtr>
  80. TPtr allocateStorage(PtrSize size, StagingGpuMemoryToken& token)
  81. {
  82. return static_cast<TPtr>(allocateFrameStagingMemory(size, StagingGpuMemoryType::STORAGE, token));
  83. }
  84. void bindStorage(CommandBufferPtr& cmdb, U32 set, U32 binding, const StagingGpuMemoryToken& token) const;
  85. template<typename TPtr>
  86. TPtr allocateAndBindStorage(PtrSize size, CommandBufferPtr& cmdb, U32 set, U32 binding)
  87. {
  88. StagingGpuMemoryToken token;
  89. TPtr ptr = allocateStorage<TPtr>(size, token);
  90. bindStorage(cmdb, set, binding, token);
  91. return ptr;
  92. }
  93. void registerDebugRenderTarget(CString rtName);
  94. };
  95. /// @}
  96. } // end namespace anki