RendererObject.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (C) 2009-2020, 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) const
  33. {
  34. ANKI_ASSERT(!"Object doesn't support that");
  35. }
  36. protected:
  37. Renderer* m_r; ///< Know your father
  38. GrManager& getGrManager();
  39. const GrManager& getGrManager() const;
  40. ResourceManager& getResourceManager();
  41. void* allocateFrameStagingMemory(PtrSize size, StagingGpuMemoryType usage, StagingGpuMemoryToken& token);
  42. U32 computeNumberOfSecondLevelCommandBuffers(U32 drawcallCount) const;
  43. /// Used in fullscreen quad draws.
  44. static void drawQuad(CommandBufferPtr& cmdb)
  45. {
  46. cmdb->drawArrays(PrimitiveTopology::TRIANGLES, 3, 1);
  47. }
  48. /// Dispatch a compute job equivelent to drawQuad
  49. static void dispatchPPCompute(CommandBufferPtr& cmdb, U32 workgroupSizeX, U32 workgroupSizeY, U32 outImageWidth,
  50. U32 outImageHeight)
  51. {
  52. const U32 sizeX = (outImageWidth + workgroupSizeX - 1) / workgroupSizeX;
  53. const U32 sizeY = (outImageHeight + workgroupSizeY - 1) / workgroupSizeY;
  54. cmdb->dispatchCompute(sizeX, sizeY, 1);
  55. }
  56. static void dispatchPPCompute(CommandBufferPtr& cmdb, U32 workgroupSizeX, U32 workgroupSizeY, U32 workgroupSizeZ,
  57. U32 outImageWidth, U32 outImageHeight, U32 outImageDepth)
  58. {
  59. const U32 sizeX = (outImageWidth + workgroupSizeX - 1) / workgroupSizeX;
  60. const U32 sizeY = (outImageHeight + workgroupSizeY - 1) / workgroupSizeY;
  61. const U32 sizeZ = (outImageDepth + workgroupSizeZ - 1) / workgroupSizeZ;
  62. cmdb->dispatchCompute(sizeX, sizeY, sizeZ);
  63. }
  64. template<typename TPtr>
  65. TPtr allocateUniforms(PtrSize size, StagingGpuMemoryToken& token)
  66. {
  67. return static_cast<TPtr>(allocateFrameStagingMemory(size, StagingGpuMemoryType::UNIFORM, token));
  68. }
  69. void bindUniforms(CommandBufferPtr& cmdb, U32 set, U32 binding, const StagingGpuMemoryToken& token) const;
  70. template<typename TPtr>
  71. TPtr allocateAndBindUniforms(PtrSize size, CommandBufferPtr& cmdb, U32 set, U32 binding)
  72. {
  73. StagingGpuMemoryToken token;
  74. TPtr ptr = allocateUniforms<TPtr>(size, token);
  75. bindUniforms(cmdb, set, binding, token);
  76. return ptr;
  77. }
  78. template<typename TPtr>
  79. TPtr allocateStorage(PtrSize size, StagingGpuMemoryToken& token)
  80. {
  81. return static_cast<TPtr>(allocateFrameStagingMemory(size, StagingGpuMemoryType::STORAGE, token));
  82. }
  83. void bindStorage(CommandBufferPtr& cmdb, U32 set, U32 binding, const StagingGpuMemoryToken& token) const;
  84. template<typename TPtr>
  85. TPtr allocateAndBindStorage(PtrSize size, CommandBufferPtr& cmdb, U32 set, U32 binding)
  86. {
  87. StagingGpuMemoryToken token;
  88. TPtr ptr = allocateStorage<TPtr>(size, token);
  89. bindStorage(cmdb, set, binding, token);
  90. return ptr;
  91. }
  92. void registerDebugRenderTarget(CString rtName);
  93. };
  94. /// @}
  95. } // end namespace anki