CommandBufferImpl.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/Gr/CommandBuffer.h>
  7. #include <AnKi/Gr/gl/StateTracker.h>
  8. #include <AnKi/Util/Assert.h>
  9. #include <AnKi/Util/Allocator.h>
  10. namespace anki
  11. {
  12. // Forward
  13. class GlState;
  14. /// @addtogroup opengl
  15. /// @{
  16. template<typename T>
  17. using CommandBufferAllocator = StackAllocator<T>;
  18. /// The base of all GL commands.
  19. class GlCommand
  20. {
  21. public:
  22. GlCommand* m_nextCommand = nullptr;
  23. virtual ~GlCommand()
  24. {
  25. }
  26. /// Execute command
  27. virtual ANKI_USE_RESULT Error operator()(GlState& state) = 0;
  28. };
  29. /// A number of GL commands organized in a chain
  30. class CommandBufferImpl final : public CommandBuffer
  31. {
  32. public:
  33. GlCommand* m_firstCommand = nullptr;
  34. GlCommand* m_lastCommand = nullptr;
  35. CommandBufferAllocator<U8> m_alloc;
  36. Bool m_immutable = false;
  37. CommandBufferFlag m_flags;
  38. #if ANKI_EXTRA_CHECKS
  39. Bool m_executed = false;
  40. #endif
  41. StateTracker m_state;
  42. /// Default constructor
  43. CommandBufferImpl(GrManager* manager, CString name)
  44. : CommandBuffer(manager, name)
  45. {
  46. }
  47. ~CommandBufferImpl()
  48. {
  49. destroy();
  50. }
  51. /// Initialize.
  52. void init(const CommandBufferInitInfo& init);
  53. /// Get the internal allocator.
  54. CommandBufferAllocator<U8> getInternalAllocator() const
  55. {
  56. return m_alloc;
  57. }
  58. /// Create a new command and add it to the chain.
  59. template<typename TCommand, typename... TArgs>
  60. void pushBackNewCommand(TArgs&&... args);
  61. /// Execute all commands
  62. ANKI_USE_RESULT Error executeAllCommands();
  63. /// Fake that it's been executed
  64. void makeExecuted()
  65. {
  66. #if ANKI_EXTRA_CHECKS
  67. m_executed = true;
  68. #endif
  69. }
  70. /// Make immutable
  71. void makeImmutable()
  72. {
  73. m_immutable = true;
  74. }
  75. Bool isEmpty() const
  76. {
  77. return m_firstCommand == nullptr;
  78. }
  79. Bool isSecondLevel() const
  80. {
  81. return !!(m_flags & CommandBufferFlag::SECOND_LEVEL);
  82. }
  83. void flushDrawcall(CommandBuffer& cmdb);
  84. private:
  85. void destroy();
  86. };
  87. template<typename TCommand, typename... TArgs>
  88. inline void CommandBufferImpl::pushBackNewCommand(TArgs&&... args)
  89. {
  90. ANKI_ASSERT(!m_immutable);
  91. TCommand* newCommand = m_alloc.template newInstance<TCommand>(std::forward<TArgs>(args)...);
  92. if(m_firstCommand != nullptr)
  93. {
  94. ANKI_ASSERT(m_lastCommand != nullptr);
  95. ANKI_ASSERT(m_lastCommand->m_nextCommand == nullptr);
  96. m_lastCommand->m_nextCommand = newCommand;
  97. m_lastCommand = newCommand;
  98. }
  99. else
  100. {
  101. ANKI_ASSERT(m_lastCommand == nullptr);
  102. m_firstCommand = newCommand;
  103. m_lastCommand = newCommand;
  104. }
  105. }
  106. /// @}
  107. } // end namespace anki