CmCoreThreadAccessor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmCommonEnums.h"
  4. #include "CmRenderSystem.h"
  5. #include "CmCommandQueue.h"
  6. #include "CmSamplerState.h"
  7. #include "CmGpuProgram.h"
  8. #include "CmCoreThread.h"
  9. #include "CmColor.h"
  10. namespace CamelotFramework
  11. {
  12. /**
  13. * @brief Core thread accessor allows you to schedule core commands outside of the core thread.
  14. *
  15. * @note All commands are queued and only executed after the call to submitToCoreThread, in the order they were called.
  16. */
  17. template <class CommandQueueSyncPolicy = CommandQueueNoSync>
  18. class CM_EXPORT CoreThreadAccessor
  19. {
  20. public:
  21. /**
  22. * @brief Constructor.
  23. *
  24. * @param threadId Identifier for the thread that created the accessor.
  25. */
  26. CoreThreadAccessor(CM_THREAD_ID_TYPE threadId)
  27. {
  28. mCommandQueue = cm_new<CommandQueue<CommandQueueSyncPolicy>>(threadId);
  29. }
  30. ~CoreThreadAccessor()
  31. {
  32. cm_delete(mCommandQueue);
  33. }
  34. /** @copydoc RenderSystem::disableTextureUnit() */
  35. void disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  36. {
  37. mCommandQueue->queue(boost::bind(&RenderSystem::disableTextureUnit, RenderSystem::instancePtr(), gptype, texUnit));
  38. }
  39. /** @copydoc RenderSystem::setPointParameters() */
  40. void setPointParameters(float size, bool attenuationEnabled, float constant, float linear, float quadratic, float minSize, float maxSize);
  41. /** @copydoc RenderSystem::setTexture() */
  42. void setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr &texPtr)
  43. {
  44. mCommandQueue->queue(boost::bind(&RenderSystem::setTexture, RenderSystem::instancePtr(), gptype, unit, enabled, texPtr));
  45. }
  46. /** @copydoc RenderSystem::setSamplerState() */
  47. void setSamplerState(GpuProgramType gptype, UINT16 texUnit, const SamplerStatePtr& samplerState)
  48. {
  49. mCommandQueue->queue(boost::bind(&RenderSystem::setSamplerState, RenderSystem::instancePtr(), gptype, texUnit, samplerState));
  50. }
  51. /** @copydoc RenderSystem::setBlendState() */
  52. void setBlendState(const BlendStatePtr& blendState)
  53. {
  54. mCommandQueue->queue(boost::bind(&RenderSystem::setBlendState, RenderSystem::instancePtr(), blendState));
  55. }
  56. /** @copydoc RenderSystem::setRasterizerState() */
  57. void setRasterizerState(const RasterizerStatePtr& rasterizerState)
  58. {
  59. mCommandQueue->queue(boost::bind(&RenderSystem::setRasterizerState, RenderSystem::instancePtr(), rasterizerState));
  60. }
  61. /** @copydoc RenderSystem::setRasterizerState() */
  62. void setDepthStencilState(const DepthStencilStatePtr& depthStencilState, UINT32 stencilRefValue)
  63. {
  64. mCommandQueue->queue(boost::bind(&RenderSystem::setDepthStencilState, RenderSystem::instancePtr(), depthStencilState, stencilRefValue));
  65. }
  66. /** @copydoc RenderSystem::setViewport() */
  67. void setViewport(const ViewportPtr& vp)
  68. {
  69. mCommandQueue->queue(boost::bind(&RenderSystem::setViewport, RenderSystem::instancePtr(), vp));
  70. }
  71. /** @copydoc RenderSystem::setVertexBuffer() */
  72. void setVertexBuffer(UINT32 index, const VertexBufferPtr& buffer)
  73. {
  74. mCommandQueue->queue(boost::bind(&RenderSystem::setVertexBuffer, RenderSystem::instancePtr(), index, buffer));
  75. }
  76. /** @copydoc RenderSystem::setIndexBuffer() */
  77. void setIndexBuffer(const IndexBufferPtr& buffer)
  78. {
  79. mCommandQueue->queue(boost::bind(&RenderSystem::setIndexBuffer, RenderSystem::instancePtr(), buffer));
  80. }
  81. /** @copydoc RenderSystem::setVertexDeclaration() */
  82. void setVertexDeclaration(VertexDeclarationPtr vertexDeclaration)
  83. {
  84. mCommandQueue->queue(boost::bind(&RenderSystem::setVertexDeclaration, RenderSystem::instancePtr(), vertexDeclaration));
  85. }
  86. /** @copydoc RenderSystem::setDrawOperation() */
  87. void setDrawOperation(DrawOperationType op)
  88. {
  89. mCommandQueue->queue(boost::bind(&RenderSystem::setDrawOperation, RenderSystem::instancePtr(), op));
  90. }
  91. /** @copydoc RenderSystem::setClipPlanes() */
  92. void setClipPlanes(const PlaneList& clipPlanes)
  93. {
  94. mCommandQueue->queue(boost::bind(&RenderSystem::setClipPlanes, RenderSystem::instancePtr(), clipPlanes));
  95. }
  96. /** @copydoc RenderSystem::addClipPlane(const Plane&) */
  97. void addClipPlane(const Plane& p)
  98. {
  99. mCommandQueue->queue(boost::bind(&RenderSystem::addClipPlane, RenderSystem::instancePtr(), p));
  100. }
  101. /** @copydoc RenderSystem::addClipPlane(float, float, float, float) */
  102. void addClipPlane(float A, float B, float C, float D)
  103. {
  104. mCommandQueue->queue(boost::bind(&RenderSystem::addClipPlane, RenderSystem::instancePtr(), A, B, C, D));
  105. }
  106. /** @copydoc RenderSystem::resetClipPlanes() */
  107. void resetClipPlanes()
  108. {
  109. mCommandQueue->queue(boost::bind(&RenderSystem::resetClipPlanes, RenderSystem::instancePtr()));
  110. }
  111. /** @copydoc RenderSystem::setScissorTest() */
  112. void setScissorTest(UINT32 left = 0, UINT32 top = 0, UINT32 right = 800, UINT32 bottom = 600)
  113. {
  114. mCommandQueue->queue(boost::bind(&RenderSystem::setScissorRect, RenderSystem::instancePtr(), left, top, right, bottom));
  115. }
  116. /** @copydoc RenderSystem::setRenderTarget() */
  117. void setRenderTarget(RenderTargetPtr target)
  118. {
  119. mCommandQueue->queue(boost::bind(&RenderSystem::setRenderTarget, RenderSystem::instancePtr(), target));
  120. }
  121. /** @copydoc RenderSystem::bindGpuProgram() */
  122. void bindGpuProgram(HGpuProgram prg)
  123. {
  124. mCommandQueue->queue(boost::bind(&RenderSystem::bindGpuProgram, RenderSystem::instancePtr(), prg));
  125. }
  126. /** @copydoc RenderSystem::unbindGpuProgram() */
  127. void unbindGpuProgram(GpuProgramType gptype)
  128. {
  129. mCommandQueue->queue(boost::bind(&RenderSystem::unbindGpuProgram, RenderSystem::instancePtr(), gptype));
  130. }
  131. /** @copydoc RenderSystem::bindGpuParams() */
  132. void bindGpuParams(GpuProgramType gptype, BindableGpuParams& params)
  133. {
  134. mCommandQueue->queue(boost::bind(&RenderSystem::bindGpuParams, RenderSystem::instancePtr(), gptype, params));
  135. }
  136. /** @copydoc RenderSystem::beginFrame() */
  137. void beginFrame(void)
  138. {
  139. mCommandQueue->queue(boost::bind(&RenderSystem::beginFrame, RenderSystem::instancePtr()));
  140. }
  141. /** @copydoc RenderSystem::endFrame() */
  142. void endFrame(void)
  143. {
  144. mCommandQueue->queue(boost::bind(&RenderSystem::endFrame, RenderSystem::instancePtr()));
  145. }
  146. /**
  147. * @copydoc RenderSystem::clearRenderTarget()
  148. */
  149. void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0)
  150. {
  151. mCommandQueue->queue(boost::bind(&RenderSystem::clearRenderTarget, RenderSystem::instancePtr(), buffers, color, depth, stencil));
  152. }
  153. /**
  154. * @copydoc RenderSystem::clearViewport()
  155. */
  156. void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0)
  157. {
  158. mCommandQueue->queue(boost::bind(&RenderSystem::clearViewport, RenderSystem::instancePtr(), buffers, color, depth, stencil));
  159. }
  160. /** @copydoc RenderSystem::swapBuffers() */
  161. void swapBuffers(RenderTargetPtr target)
  162. {
  163. mCommandQueue->queue(boost::bind(&RenderSystem::swapBuffers, RenderSystem::instancePtr(), target));
  164. }
  165. /** @copydoc RenderSystem::render() */
  166. void render(const RenderOpMesh& op)
  167. {
  168. mCommandQueue->queue(boost::bind(&RenderSystem::render, RenderSystem::instancePtr(), op));
  169. }
  170. /** @copydoc RenderSystem::draw() */
  171. void draw(UINT32 vertexCount)
  172. {
  173. mCommandQueue->queue(boost::bind(&RenderSystem::draw, RenderSystem::instancePtr(), vertexCount));
  174. }
  175. /** @copydoc RenderSystem::drawIndexed() */
  176. void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexCount)
  177. {
  178. mCommandQueue->queue(boost::bind(&RenderSystem::drawIndexed, RenderSystem::instancePtr(), startIndex, indexCount, vertexCount));
  179. }
  180. /**
  181. * @copydoc RenderSystem::writeSubresource()
  182. *
  183. * @note Resource is updated with data from "data" parameter when the async operation completes.
  184. * Until the async operation completes "data" is owned by the core thread and you won't
  185. * be able to access it.
  186. */
  187. AsyncOp writeSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceData& data)
  188. {
  189. data.lock();
  190. return mCommandQueue->queueReturn(boost::bind(&RenderSystem::writeSubresource, RenderSystem::instancePtr(), resource, subresourceIdx, boost::cref(data), _1));
  191. }
  192. /**
  193. * @copydoc RenderSystem::writeSubresource()
  194. *
  195. * @note "data" parameter is populated with subresource data when the async operation completes.
  196. * Until the async operation completes "data" is owned by the core thread and you won't
  197. * be able to access it.
  198. */
  199. AsyncOp readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, GpuResourceData& data)
  200. {
  201. data.lock();
  202. return mCommandQueue->queueReturn(boost::bind(&RenderSystem::readSubresource, RenderSystem::instancePtr(), resource, subresourceIdx, boost::ref(data), _1));
  203. }
  204. void resizeWindow(RenderWindowPtr& renderWindow, UINT32 width, UINT32 height)
  205. {
  206. mCommandQueue->queue(boost::bind(&RenderWindow::resize, renderWindow.get(), width, height));
  207. }
  208. void moveWindow(RenderWindowPtr& renderWindow, INT32 left, INT32 top)
  209. {
  210. mCommandQueue->queue(boost::bind(&RenderWindow::move, renderWindow.get(), left, top));
  211. }
  212. void startResize(RenderWindowPtr& renderWindow, WindowResizeDirection direction)
  213. {
  214. mCommandQueue->queue(boost::bind(&RenderWindow::startResize, renderWindow.get(), direction));
  215. }
  216. void endResize(RenderWindowPtr& renderWindow)
  217. {
  218. mCommandQueue->queue(boost::bind(&RenderWindow::endResize, renderWindow.get()));
  219. }
  220. void startMove(RenderWindowPtr& renderWindow)
  221. {
  222. mCommandQueue->queue(boost::bind(&RenderWindow::startMove, renderWindow.get()));
  223. }
  224. void endMove(RenderWindowPtr& renderWindow)
  225. {
  226. mCommandQueue->queue(boost::bind(&RenderWindow::endMove, renderWindow.get()));
  227. }
  228. /**
  229. * @brief Makes all the currently queued commands available to the core thread. They will be executed
  230. * as soon as the core thread is ready.
  231. */
  232. void submitToCoreThread(bool blockUntilComplete = false)
  233. {
  234. Queue<QueuedCommand>::type* commands = mCommandQueue->flush();
  235. gCoreThread().queueCommand(boost::bind(&CommandQueueBase::playback, mCommandQueue, commands), blockUntilComplete);
  236. }
  237. /**
  238. * @brief Cancels all commands in the queue.
  239. */
  240. void cancelAll()
  241. {
  242. mCommandQueue->cancelAll();
  243. }
  244. private:
  245. CommandQueue<CommandQueueSyncPolicy>* mCommandQueue;
  246. };
  247. }