CmCommandQueue.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "CmCommandQueue.h"
  2. #include "CmException.h"
  3. #include "CmRenderSystem.h"
  4. #include "CmDebug.h"
  5. namespace CamelotEngine
  6. {
  7. CommandQueue::CommandQueue(CM_THREAD_ID_TYPE threadId, bool allowAllThreads)
  8. :mMyThreadId(threadId), mAllowAllThreads(allowAllThreads)
  9. {
  10. mCommands = new vector<Command>::type();
  11. }
  12. CommandQueue::~CommandQueue()
  13. {
  14. #if CM_DEBUG_MODE
  15. #if CM_THREAD_SUPPORT != 0
  16. if(!mAllowAllThreads && CM_THREAD_CURRENT_ID != mMyThreadId)
  17. {
  18. CM_EXCEPT(InternalErrorException, "Command queue accessed outside of its creation thread.");
  19. }
  20. #endif
  21. #endif
  22. if(mCommands != nullptr)
  23. delete mCommands;
  24. }
  25. AsyncOp CommandQueue::queueReturn(boost::function<void(AsyncOp&)> commandCallback, bool _notifyWhenComplete, UINT32 _callbackId)
  26. {
  27. #if CM_DEBUG_MODE
  28. #if CM_THREAD_SUPPORT != 0
  29. if(!mAllowAllThreads && CM_THREAD_CURRENT_ID != mMyThreadId)
  30. {
  31. CM_EXCEPT(InternalErrorException, "Command queue accessed outside of its creation thread.");
  32. }
  33. #endif
  34. #endif
  35. Command newCommand(commandCallback, _notifyWhenComplete, _callbackId);
  36. mCommands->push_back(newCommand);
  37. #if CM_FORCE_SINGLETHREADED_RENDERING
  38. vector<CommandQueue::Command>::type* commands = flush();
  39. playback(commands);
  40. #endif
  41. return newCommand.asyncOp;
  42. }
  43. void CommandQueue::queue(boost::function<void()> commandCallback, bool _notifyWhenComplete, UINT32 _callbackId)
  44. {
  45. #if CM_DEBUG_MODE
  46. #if CM_THREAD_SUPPORT != 0
  47. if(!mAllowAllThreads && CM_THREAD_CURRENT_ID != mMyThreadId)
  48. {
  49. CM_EXCEPT(InternalErrorException, "Command queue accessed outside of its creation thread.");
  50. }
  51. #endif
  52. #endif
  53. Command newCommand(commandCallback, _notifyWhenComplete, _callbackId);
  54. mCommands->push_back(newCommand);
  55. #if CM_FORCE_SINGLETHREADED_RENDERING
  56. vector<CommandQueue::Command>::type* commands = flush();
  57. playback(commands);
  58. #endif
  59. }
  60. vector<CommandQueue::Command>::type* CommandQueue::flush()
  61. {
  62. vector<Command>::type* oldCommands = nullptr;
  63. {
  64. CM_LOCK_MUTEX(mCommandBufferMutex);
  65. oldCommands = mCommands;
  66. mCommands = new vector<Command>::type();
  67. }
  68. return oldCommands;
  69. }
  70. void CommandQueue::playback(vector<CommandQueue::Command>::type* commands, boost::function<void(UINT32)> notifyCallback)
  71. {
  72. #if CM_DEBUG_MODE
  73. RenderSystem* rs = RenderSystem::instancePtr();
  74. if(rs->getRenderThreadId() != CM_THREAD_CURRENT_ID)
  75. CM_EXCEPT(InternalErrorException, "This method should only be called from the render thread.");
  76. #endif
  77. if(commands == nullptr)
  78. return;
  79. for(auto iter = commands->begin(); iter != commands->end(); ++iter)
  80. {
  81. Command& command = (*iter);
  82. if(command.returnsValue)
  83. {
  84. command.callbackWithReturnValue(command.asyncOp);
  85. if(!command.asyncOp.hasCompleted())
  86. {
  87. LOGDBG("Async operation return value wasn't resolved properly. Resolving automatically to nullptr. " \
  88. "Make sure to complete the operation before returning from the command callback method.");
  89. command.asyncOp.completeOperation(nullptr);
  90. }
  91. }
  92. else
  93. {
  94. command.callback();
  95. }
  96. if(command.notifyWhenComplete && !notifyCallback.empty())
  97. {
  98. notifyCallback(command.callbackId);
  99. }
  100. }
  101. delete commands;
  102. }
  103. void CommandQueue::playback(vector<Command>::type* commands)
  104. {
  105. playback(commands, boost::function<void(UINT32)>());
  106. }
  107. bool CommandQueue::isEmpty()
  108. {
  109. CM_LOCK_MUTEX(mCommandBufferMutex);
  110. if(mCommands != nullptr && mCommands->size() > 0)
  111. return false;
  112. return true;
  113. }
  114. }