CmCoreThreadAccessor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include "CmFrameAlloc.h"
  12. namespace CamelotFramework
  13. {
  14. /**
  15. * @brief Core thread accessor allows you to schedule core commands outside of the core thread.
  16. *
  17. * @note All commands are queued and only executed after the call to submitToCoreThread, in the order they were called.
  18. */
  19. template <class CommandQueueSyncPolicy = CommandQueueNoSync>
  20. class CM_EXPORT CoreThreadAccessor
  21. {
  22. public:
  23. /**
  24. * @brief Constructor.
  25. *
  26. * @param threadId Identifier for the thread that created the accessor.
  27. */
  28. CoreThreadAccessor(CM_THREAD_ID_TYPE threadId)
  29. {
  30. mCommandQueue = cm_new<CommandQueue<CommandQueueSyncPolicy>>(threadId);
  31. }
  32. ~CoreThreadAccessor()
  33. {
  34. cm_delete(mCommandQueue);
  35. }
  36. /** @copydoc RenderSystem::disableTextureUnit() */
  37. void disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  38. {
  39. mCommandQueue->queue(boost::bind(&RenderSystem::disableTextureUnit, RenderSystem::instancePtr(), gptype, texUnit));
  40. }
  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::setDrawOperation() */
  72. void setDrawOperation(DrawOperationType op)
  73. {
  74. mCommandQueue->queue(boost::bind(&RenderSystem::setDrawOperation, RenderSystem::instancePtr(), op));
  75. }
  76. /** @copydoc RenderSystem::setClipPlanes() */
  77. void setClipPlanes(const PlaneList& clipPlanes)
  78. {
  79. mCommandQueue->queue(boost::bind(&RenderSystem::setClipPlanes, RenderSystem::instancePtr(), clipPlanes));
  80. }
  81. /** @copydoc RenderSystem::addClipPlane(const Plane&) */
  82. void addClipPlane(const Plane& p)
  83. {
  84. mCommandQueue->queue(boost::bind(&RenderSystem::addClipPlane, RenderSystem::instancePtr(), p));
  85. }
  86. /** @copydoc RenderSystem::addClipPlane(float, float, float, float) */
  87. void addClipPlane(float A, float B, float C, float D)
  88. {
  89. mCommandQueue->queue(boost::bind(&RenderSystem::addClipPlane, RenderSystem::instancePtr(), A, B, C, D));
  90. }
  91. /** @copydoc RenderSystem::resetClipPlanes() */
  92. void resetClipPlanes()
  93. {
  94. mCommandQueue->queue(boost::bind(&RenderSystem::resetClipPlanes, RenderSystem::instancePtr()));
  95. }
  96. /** @copydoc RenderSystem::setScissorTest() */
  97. void setScissorTest(UINT32 left = 0, UINT32 top = 0, UINT32 right = 800, UINT32 bottom = 600)
  98. {
  99. mCommandQueue->queue(boost::bind(&RenderSystem::setScissorRect, RenderSystem::instancePtr(), left, top, right, bottom));
  100. }
  101. /** @copydoc RenderSystem::setRenderTarget() */
  102. void setRenderTarget(RenderTargetPtr target)
  103. {
  104. mCommandQueue->queue(boost::bind(&RenderSystem::setRenderTarget, RenderSystem::instancePtr(), target));
  105. }
  106. /** @copydoc RenderSystem::bindGpuProgram() */
  107. void bindGpuProgram(HGpuProgram prg)
  108. {
  109. mCommandQueue->queue(boost::bind(&RenderSystem::bindGpuProgram, RenderSystem::instancePtr(), prg));
  110. }
  111. /** @copydoc RenderSystem::unbindGpuProgram() */
  112. void unbindGpuProgram(GpuProgramType gptype)
  113. {
  114. mCommandQueue->queue(boost::bind(&RenderSystem::unbindGpuProgram, RenderSystem::instancePtr(), gptype));
  115. }
  116. /** @copydoc RenderSystem::bindGpuParams() */
  117. void bindGpuParams(GpuProgramType gptype, const GpuParamsPtr& params)
  118. {
  119. mCommandQueue->queue(boost::bind(&RenderSystem::bindGpuParams, RenderSystem::instancePtr(), gptype, BindableGpuParams(params, gCoreThread().getFrameAlloc())));
  120. }
  121. /** @copydoc RenderSystem::beginFrame() */
  122. void beginRender(void)
  123. {
  124. mCommandQueue->queue(boost::bind(&RenderSystem::beginFrame, RenderSystem::instancePtr()));
  125. }
  126. /** @copydoc RenderSystem::endFrame() */
  127. void endRender(void)
  128. {
  129. mCommandQueue->queue(boost::bind(&RenderSystem::endFrame, RenderSystem::instancePtr()));
  130. }
  131. /**
  132. * @copydoc RenderSystem::clearRenderTarget()
  133. */
  134. void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0)
  135. {
  136. mCommandQueue->queue(boost::bind(&RenderSystem::clearRenderTarget, RenderSystem::instancePtr(), buffers, color, depth, stencil));
  137. }
  138. /**
  139. * @copydoc RenderSystem::clearViewport()
  140. */
  141. void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0)
  142. {
  143. mCommandQueue->queue(boost::bind(&RenderSystem::clearViewport, RenderSystem::instancePtr(), buffers, color, depth, stencil));
  144. }
  145. /** @copydoc RenderSystem::swapBuffers() */
  146. void swapBuffers(RenderTargetPtr target)
  147. {
  148. mCommandQueue->queue(boost::bind(&RenderSystem::swapBuffers, RenderSystem::instancePtr(), target));
  149. }
  150. /** @copydoc RenderSystem::render() */
  151. void render(const MeshBasePtr& mesh, UINT32 indexOffset = 0, UINT32 indexCount = 0, bool useIndices = true, DrawOperationType drawOp = DOT_TRIANGLE_LIST)
  152. {
  153. mCommandQueue->queue(boost::bind(&RenderSystem::render, RenderSystem::instancePtr(), mesh, indexOffset, indexCount, useIndices, drawOp));
  154. }
  155. /** @copydoc RenderSystem::draw() */
  156. void draw(UINT32 vertexOffset, UINT32 vertexCount)
  157. {
  158. mCommandQueue->queue(boost::bind(&RenderSystem::draw, RenderSystem::instancePtr(), vertexOffset, vertexCount));
  159. }
  160. /** @copydoc RenderSystem::drawIndexed() */
  161. void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount)
  162. {
  163. mCommandQueue->queue(boost::bind(&RenderSystem::drawIndexed, RenderSystem::instancePtr(), startIndex, indexCount, vertexOffset, vertexCount));
  164. }
  165. /**
  166. * @copydoc RenderSystem::writeSubresource()
  167. *
  168. * @param discardEntireBuffer When true the existing contents of the resource you are updating will be discarded. This can make the
  169. * operation faster. Resources with certain buffer types might require this flag to be in a specific state
  170. * otherwise the operation will fail.
  171. *
  172. * @note Resource is updated with data from "data" parameter when the async operation completes.
  173. * Until the async operation completes "data" is owned by the core thread and you won't
  174. * be able to access it.
  175. *
  176. * Normally dynamic buffers will require you to enable "discardEntireBuffer" flag, while static buffers require it disabled.
  177. */
  178. AsyncOp writeSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceDataPtr& data, bool discardEntireBuffer = false)
  179. {
  180. data->lock();
  181. return mCommandQueue->queueReturn(boost::bind(&RenderSystem::writeSubresource, RenderSystem::instancePtr(), resource, subresourceIdx, data, discardEntireBuffer, _1));
  182. }
  183. /**
  184. * @copydoc RenderSystem::writeSubresource()
  185. *
  186. * @note "data" parameter is populated with subresource data when the async operation completes.
  187. * Until the async operation completes "data" is owned by the core thread and you won't
  188. * be able to access it.
  189. */
  190. AsyncOp readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceDataPtr& data)
  191. {
  192. data->lock();
  193. return mCommandQueue->queueReturn(boost::bind(&RenderSystem::readSubresource, RenderSystem::instancePtr(), resource, subresourceIdx, data, _1));
  194. }
  195. void resizeWindow(RenderWindowPtr& renderWindow, UINT32 width, UINT32 height)
  196. {
  197. mCommandQueue->queue(boost::bind(&RenderWindow::resize, renderWindow.get(), width, height));
  198. }
  199. void moveWindow(RenderWindowPtr& renderWindow, INT32 left, INT32 top)
  200. {
  201. mCommandQueue->queue(boost::bind(&RenderWindow::move, renderWindow.get(), left, top));
  202. }
  203. void hideWindow(RenderWindowPtr& renderWindow)
  204. {
  205. mCommandQueue->queue(boost::bind(&RenderWindow::setHidden, renderWindow.get(), true));
  206. }
  207. void showWindow(RenderWindowPtr& renderWindow)
  208. {
  209. mCommandQueue->queue(boost::bind(&RenderWindow::setHidden, renderWindow.get(), false));
  210. }
  211. /**
  212. * @brief Makes all the currently queued commands available to the core thread. They will be executed
  213. * as soon as the core thread is ready.
  214. */
  215. void submitToCoreThread(bool blockUntilComplete = false)
  216. {
  217. Queue<QueuedCommand>::type* commands = mCommandQueue->flush();
  218. gCoreThread().queueCommand(boost::bind(&CommandQueueBase::playback, mCommandQueue, commands), blockUntilComplete);
  219. }
  220. /**
  221. * @brief Cancels all commands in the queue.
  222. */
  223. void cancelAll()
  224. {
  225. // Note that this won't free any Frame data allocated for all the canceled commands since
  226. // frame data will only get cleared at frame start
  227. mCommandQueue->cancelAll();
  228. }
  229. private:
  230. CommandQueue<CommandQueueSyncPolicy>* mCommandQueue;
  231. };
  232. }