CmCoreThreadAccessor.h 10 KB

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