CmCoreThreadAccessor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 BansheeEngine
  13. {
  14. /**
  15. * @brief Core thread accessor allows you to schedule core commands outside of the core thread. Provides a set of common
  16. * methods you may want to execute on the core thread, as well as a general command queuing methods.
  17. *
  18. * @note Queued commands are only executed after the call to submitToCoreThread, in the order they were submitted.
  19. */
  20. template <class CommandQueueSyncPolicy = CommandQueueNoSync>
  21. class CM_EXPORT CoreThreadAccessor
  22. {
  23. public:
  24. /**
  25. * @brief Constructor.
  26. *
  27. * @param threadId Identifier for the thread that created the accessor.
  28. */
  29. CoreThreadAccessor(CM_THREAD_ID_TYPE threadId)
  30. {
  31. mCommandQueue = cm_new<CommandQueue<CommandQueueSyncPolicy>>(threadId);
  32. }
  33. ~CoreThreadAccessor()
  34. {
  35. cm_delete(mCommandQueue);
  36. }
  37. /** @copydoc RenderSystem::disableTextureUnit() */
  38. void disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  39. {
  40. mCommandQueue->queue(std::bind(&RenderSystem::disableTextureUnit, RenderSystem::instancePtr(), gptype, texUnit));
  41. }
  42. /** @copydoc RenderSystem::setTexture() */
  43. void setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr &texPtr)
  44. {
  45. mCommandQueue->queue(std::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(std::bind(&RenderSystem::setSamplerState, RenderSystem::instancePtr(), gptype, texUnit, samplerState));
  51. }
  52. /** @copydoc RenderSystem::setBlendState() */
  53. void setBlendState(const BlendStatePtr& blendState)
  54. {
  55. mCommandQueue->queue(std::bind(&RenderSystem::setBlendState, RenderSystem::instancePtr(), blendState));
  56. }
  57. /** @copydoc RenderSystem::setRasterizerState() */
  58. void setRasterizerState(const RasterizerStatePtr& rasterizerState)
  59. {
  60. mCommandQueue->queue(std::bind(&RenderSystem::setRasterizerState, RenderSystem::instancePtr(), rasterizerState));
  61. }
  62. /** @copydoc RenderSystem::setRasterizerState() */
  63. void setDepthStencilState(const DepthStencilStatePtr& depthStencilState, UINT32 stencilRefValue)
  64. {
  65. mCommandQueue->queue(std::bind(&RenderSystem::setDepthStencilState, RenderSystem::instancePtr(), depthStencilState, stencilRefValue));
  66. }
  67. /** @copydoc RenderSystem::setViewport() */
  68. void setViewport(const ViewportPtr& vp)
  69. {
  70. mCommandQueue->queue(std::bind(&RenderSystem::setViewport, RenderSystem::instancePtr(), vp));
  71. }
  72. /** @copydoc RenderSystem::setDrawOperation() */
  73. void setDrawOperation(DrawOperationType op)
  74. {
  75. mCommandQueue->queue(std::bind(&RenderSystem::setDrawOperation, RenderSystem::instancePtr(), op));
  76. }
  77. /** @copydoc RenderSystem::setClipPlanes() */
  78. void setClipPlanes(const PlaneList& clipPlanes)
  79. {
  80. mCommandQueue->queue(std::bind(&RenderSystem::setClipPlanes, RenderSystem::instancePtr(), clipPlanes));
  81. }
  82. /** @copydoc RenderSystem::addClipPlane(const Plane&) */
  83. void addClipPlane(const Plane& p)
  84. {
  85. mCommandQueue->queue(std::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(std::bind(&RenderSystem::addClipPlane, RenderSystem::instancePtr(), A, B, C, D));
  91. }
  92. /** @copydoc RenderSystem::resetClipPlanes() */
  93. void resetClipPlanes()
  94. {
  95. mCommandQueue->queue(std::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(std::bind(&RenderSystem::setScissorRect, RenderSystem::instancePtr(), left, top, right, bottom));
  101. }
  102. /** @copydoc RenderSystem::setRenderTarget() */
  103. void setRenderTarget(RenderTargetPtr target)
  104. {
  105. mCommandQueue->queue(std::bind(&RenderSystem::setRenderTarget, RenderSystem::instancePtr(), target));
  106. }
  107. /** @copydoc RenderSystem::bindGpuProgram() */
  108. void bindGpuProgram(HGpuProgram prg)
  109. {
  110. mCommandQueue->queue(std::bind(&RenderSystem::bindGpuProgram, RenderSystem::instancePtr(), prg));
  111. }
  112. /** @copydoc RenderSystem::unbindGpuProgram() */
  113. void unbindGpuProgram(GpuProgramType gptype)
  114. {
  115. mCommandQueue->queue(std::bind(&RenderSystem::unbindGpuProgram, RenderSystem::instancePtr(), gptype));
  116. }
  117. /** @copydoc RenderSystem::bindGpuParams() */
  118. void bindGpuParams(GpuProgramType gptype, const GpuParamsPtr& params)
  119. {
  120. mCommandQueue->queue(std::bind(&RenderSystem::bindGpuParams, RenderSystem::instancePtr(), gptype, BindableGpuParams(params, gCoreThread().getFrameAlloc())));
  121. }
  122. /** @copydoc RenderSystem::beginFrame() */
  123. void beginRender(void)
  124. {
  125. mCommandQueue->queue(std::bind(&RenderSystem::beginFrame, RenderSystem::instancePtr()));
  126. }
  127. /** @copydoc RenderSystem::endFrame() */
  128. void endRender(void)
  129. {
  130. mCommandQueue->queue(std::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(std::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(std::bind(&RenderSystem::clearViewport, RenderSystem::instancePtr(), buffers, color, depth, stencil));
  145. }
  146. /** @copydoc RenderSystem::swapBuffers() */
  147. void swapBuffers(RenderTargetPtr target)
  148. {
  149. mCommandQueue->queue(std::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(std::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(std::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(std::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(std::bind(&RenderSystem::writeSubresource, RenderSystem::instancePtr(), resource,
  183. subresourceIdx, data, discardEntireBuffer, std::placeholders::_1));
  184. }
  185. /**
  186. * @copydoc RenderSystem::readSubresource()
  187. *
  188. * @note "data" parameter is populated with subresource data when the async operation completes.
  189. * Until the async operation completes "data" is owned by the core thread and you won't
  190. * be able to access it.
  191. */
  192. AsyncOp readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceDataPtr& data)
  193. {
  194. data->lock();
  195. return mCommandQueue->queueReturn(std::bind(&RenderSystem::readSubresource, RenderSystem::instancePtr(),
  196. resource, subresourceIdx, data, std::placeholders::_1));
  197. }
  198. /**
  199. * @brief Resize the provided window to specified width and height in pixels.
  200. */
  201. void resizeWindow(RenderWindowPtr& renderWindow, UINT32 width, UINT32 height)
  202. {
  203. mCommandQueue->queue(std::bind(&RenderWindow::resize, renderWindow.get(), width, height));
  204. }
  205. /**
  206. * @brief Move the provided window to specified screen coordinates.
  207. */
  208. void moveWindow(RenderWindowPtr& renderWindow, INT32 left, INT32 top)
  209. {
  210. mCommandQueue->queue(std::bind(&RenderWindow::move, renderWindow.get(), left, top));
  211. }
  212. /**
  213. * @brief Hide the provided window. (Does not destroy it, just hides it).
  214. */
  215. void hideWindow(RenderWindowPtr& renderWindow)
  216. {
  217. mCommandQueue->queue(std::bind(&RenderWindow::setHidden, renderWindow.get(), true));
  218. }
  219. /**
  220. * @brief Shows a previously hidden window.
  221. */
  222. void showWindow(RenderWindowPtr& renderWindow)
  223. {
  224. mCommandQueue->queue(std::bind(&RenderWindow::setHidden, renderWindow.get(), false));
  225. }
  226. /**
  227. * @brief Queues a new generic command that will be added to the command queue.
  228. */
  229. AsyncOp queueReturnCommand(std::function<void(AsyncOp&)> commandCallback)
  230. {
  231. return mCommandQueue->queueReturn(commandCallback);
  232. }
  233. /**
  234. * @brief Queues a new generic command that will be added to the command queue.
  235. */
  236. void queueCommand(std::function<void()> commandCallback)
  237. {
  238. mCommandQueue->queue(commandCallback);
  239. }
  240. /**
  241. * @brief Makes all the currently queued commands available to the core thread. They will be executed
  242. * as soon as the core thread is ready. All queued commands are removed from the accessor.
  243. */
  244. void submitToCoreThread(bool blockUntilComplete = false)
  245. {
  246. Queue<QueuedCommand>::type* commands = mCommandQueue->flush();
  247. gCoreThread().queueCommand(std::bind(&CommandQueueBase::playback, mCommandQueue, commands), blockUntilComplete);
  248. }
  249. /**
  250. * @brief Cancels all commands in the queue.
  251. */
  252. void cancelAll()
  253. {
  254. // Note that this won't free any Frame data allocated for all the canceled commands since
  255. // frame data will only get cleared at frame start
  256. mCommandQueue->cancelAll();
  257. }
  258. private:
  259. CommandQueue<CommandQueueSyncPolicy>* mCommandQueue;
  260. };
  261. }