CmRenderSystemContext.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "CmRenderSystemContext.h"
  2. #include "CmException.h"
  3. #include "CmRenderSystemManager.h"
  4. #include "CmRenderSystem.h"
  5. #include "CmDebug.h"
  6. namespace CamelotEngine
  7. {
  8. RenderSystemContext::RenderSystemContext(CM_THREAD_ID_TYPE threadId)
  9. :mMyThreadId(threadId), mIsExecuting(false)
  10. , waitForVerticalBlank(true)
  11. , cullingMode(CULL_CLOCKWISE)
  12. , vertexProgramBound(false)
  13. , geometryProgramBound(false)
  14. , fragmentProgramBound(false)
  15. , invertVertexWinding(false)
  16. {
  17. mCommands = new vector<RenderSystemCommand>::type();
  18. }
  19. /************************************************************************/
  20. /* FRAME CONTEXT */
  21. /************************************************************************/
  22. RenderSystemFrameContext::RenderSystemFrameContext(CM_THREAD_ID_TYPE threadId)
  23. :RenderSystemContext(threadId), mReadyCommands(nullptr)
  24. {
  25. }
  26. AsyncOp RenderSystemFrameContext::queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, UINT32 _callbackId)
  27. {
  28. #if CM_DEBUG_MODE
  29. #if CM_THREAD_SUPPORT != 0
  30. if(CM_THREAD_CURRENT_ID != mMyThreadId)
  31. {
  32. CM_EXCEPT(InternalErrorException, "Render system context accessed from an invalid thread.");
  33. }
  34. #endif
  35. #endif
  36. RenderSystemCommand newCommand(commandCallback, _callbackId);
  37. mCommands->push_back(newCommand);
  38. return newCommand.asyncOp;
  39. }
  40. void RenderSystemFrameContext::queueCommand(boost::function<void()> commandCallback, UINT32 _callbackId)
  41. {
  42. #if CM_DEBUG_MODE
  43. #if CM_THREAD_SUPPORT != 0
  44. if(CM_THREAD_CURRENT_ID != mMyThreadId)
  45. {
  46. CM_EXCEPT(InternalErrorException, "Render system context accessed from an invalid thread.");
  47. }
  48. #endif
  49. #endif
  50. RenderSystemCommand newCommand(commandCallback, _callbackId);
  51. mCommands->push_back(newCommand);
  52. }
  53. void RenderSystemFrameContext::submitToGpu()
  54. {
  55. {
  56. CM_LOCK_MUTEX(mCommandBufferMutex);
  57. if(mReadyCommands != nullptr)
  58. {
  59. delete mReadyCommands;
  60. mReadyCommands = nullptr;
  61. }
  62. mReadyCommands = mCommands;
  63. mCommands = new vector<RenderSystemCommand>::type();
  64. }
  65. }
  66. void RenderSystemFrameContext::playbackCommands()
  67. {
  68. #if CM_DEBUG_MODE
  69. RenderSystem* rs = RenderSystemManager::getActive();
  70. if(rs->getRenderThreadId() != CM_THREAD_CURRENT_ID)
  71. CM_EXCEPT(InternalErrorException, "This method should only be called from the render thread.");
  72. #endif
  73. vector<RenderSystemCommand>::type* currentCommands = nullptr;
  74. {
  75. CM_LOCK_MUTEX(mCommandBufferMutex)
  76. currentCommands = mReadyCommands;
  77. mReadyCommands = nullptr;
  78. mIsExecuting = true;
  79. }
  80. if(currentCommands == nullptr)
  81. {
  82. {
  83. CM_LOCK_MUTEX(mCommandBufferMutex);
  84. mIsExecuting = false;
  85. }
  86. CM_THREAD_NOTIFY_ALL(mContextPlaybackDoneCondition)
  87. return;
  88. }
  89. for(auto iter = currentCommands->begin(); iter != currentCommands->end(); ++iter)
  90. {
  91. RenderSystemCommand command = (*iter);
  92. if(command.returnsValue)
  93. {
  94. command.callbackWithReturnValue(command.asyncOp);
  95. if(!command.asyncOp.hasCompleted())
  96. {
  97. LOGDBG("Async operation return value wasn't resolved properly. Resolving automatically to nullptr. " \
  98. "Make sure to complete the operation before returning from the command callback method.");
  99. command.asyncOp.completeOperation(nullptr);
  100. }
  101. }
  102. else
  103. {
  104. command.callback();
  105. }
  106. }
  107. delete currentCommands;
  108. {
  109. CM_LOCK_MUTEX(mCommandBufferMutex);
  110. mIsExecuting = false;
  111. }
  112. CM_THREAD_NOTIFY_ALL(mContextPlaybackDoneCondition)
  113. }
  114. bool RenderSystemFrameContext::hasReadyCommands()
  115. {
  116. CM_LOCK_MUTEX(mCommandBufferMutex);
  117. if(mReadyCommands != nullptr && mReadyCommands->size() > 0)
  118. return true;
  119. return false;
  120. }
  121. void RenderSystemFrameContext::blockUntilExecuted()
  122. {
  123. #if CM_DEBUG_MODE
  124. RenderSystem* rs = RenderSystemManager::getActive();
  125. if(rs->getRenderThreadId() == CM_THREAD_CURRENT_ID)
  126. CM_EXCEPT(InternalErrorException, "This method should never be called from the render thread as it will cause a deadlock.");
  127. #endif
  128. {
  129. CM_LOCK_MUTEX_NAMED(mCommandBufferMutex, lock);
  130. while (mReadyCommands != nullptr && mReadyCommands->size() > 0 || mIsExecuting)
  131. {
  132. CM_THREAD_WAIT(mContextPlaybackDoneCondition, mCommandBufferMutex, lock)
  133. }
  134. }
  135. }
  136. /************************************************************************/
  137. /* IMMEDIATE CONTEXT */
  138. /************************************************************************/
  139. RenderSystemImmediateContext::RenderSystemImmediateContext(CM_THREAD_ID_TYPE threadId)
  140. :RenderSystemContext(threadId)
  141. {
  142. }
  143. AsyncOp RenderSystemImmediateContext::queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, UINT32 _callbackId)
  144. {
  145. CM_LOCK_MUTEX(mCommandBufferMutex)
  146. RenderSystemCommand newCommand(commandCallback, _callbackId);
  147. mCommands->push_back(newCommand);
  148. return newCommand.asyncOp;
  149. }
  150. void RenderSystemImmediateContext::queueCommand(boost::function<void()> commandCallback, UINT32 _callbackId)
  151. {
  152. CM_LOCK_MUTEX(mCommandBufferMutex)
  153. RenderSystemCommand newCommand(commandCallback, _callbackId);
  154. mCommands->push_back(newCommand);
  155. }
  156. void RenderSystemImmediateContext::submitToGpu()
  157. {
  158. // Do nothing
  159. }
  160. void RenderSystemImmediateContext::playbackCommands()
  161. {
  162. #if CM_DEBUG_MODE
  163. RenderSystem* rs = RenderSystemManager::getActive();
  164. if(rs->getRenderThreadId() != CM_THREAD_CURRENT_ID)
  165. CM_EXCEPT(InternalErrorException, "This method should only be called from the render thread.");
  166. #endif
  167. vector<RenderSystemCommand>::type* currentCommands = nullptr;
  168. {
  169. CM_LOCK_MUTEX(mCommandBufferMutex)
  170. currentCommands = mCommands;
  171. mCommands = new vector<RenderSystemCommand>::type();
  172. mIsExecuting = true;
  173. }
  174. if(currentCommands == nullptr)
  175. {
  176. {
  177. CM_LOCK_MUTEX(mCommandBufferMutex);
  178. mIsExecuting = false;
  179. }
  180. CM_THREAD_NOTIFY_ALL(mContextPlaybackDoneCondition)
  181. return;
  182. }
  183. for(auto iter = currentCommands->begin(); iter != currentCommands->end(); ++iter)
  184. {
  185. RenderSystemCommand command = (*iter);
  186. if(command.returnsValue)
  187. {
  188. command.callbackWithReturnValue(command.asyncOp);
  189. if(!command.asyncOp.hasCompleted())
  190. {
  191. LOGDBG("Async operation return value wasn't resolved properly. Resolving automatically to nullptr. " \
  192. "Make sure to complete the operation before returning from the command callback method.");
  193. command.asyncOp.completeOperation(nullptr);
  194. }
  195. }
  196. else
  197. {
  198. command.callback();
  199. }
  200. }
  201. delete currentCommands;
  202. {
  203. CM_LOCK_MUTEX(mCommandBufferMutex);
  204. mIsExecuting = false;
  205. }
  206. CM_THREAD_NOTIFY_ALL(mContextPlaybackDoneCondition)
  207. }
  208. bool RenderSystemImmediateContext::hasReadyCommands()
  209. {
  210. CM_LOCK_MUTEX(mCommandBufferMutex);
  211. if(mCommands != nullptr && mCommands->size() > 0)
  212. return true;
  213. return false;
  214. }
  215. void RenderSystemImmediateContext::blockUntilExecuted()
  216. {
  217. #if CM_DEBUG_MODE
  218. RenderSystem* rs = RenderSystemManager::getActive();
  219. if(rs->getRenderThreadId() == CM_THREAD_CURRENT_ID)
  220. CM_EXCEPT(InternalErrorException, "This method should never be called from the render thread as it will cause a deadlock.");
  221. #endif
  222. {
  223. CM_LOCK_MUTEX_NAMED(mCommandBufferMutex, lock);
  224. while (mCommands != nullptr && mCommands->size() > 0 || mIsExecuting)
  225. {
  226. CM_THREAD_WAIT(mContextPlaybackDoneCondition, mCommandBufferMutex, lock)
  227. }
  228. }
  229. }
  230. }