CmCoreThreadAccessor.h 10 KB

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