CmDeferredRenderContext.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmDeferredRenderContextFwd.h"
  4. #include "CmCommonEnums.h"
  5. #include "CmRenderSystem.h"
  6. #include "CmCommandQueue.h"
  7. #include "CmSamplerState.h"
  8. #include "CmGpuProgram.h"
  9. #include "CmCoreThread.h"
  10. #include "CmColor.h"
  11. namespace CamelotFramework
  12. {
  13. /**
  14. * @brief Deferred render context allows you to execute RenderSystem 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 DeferredRenderContext
  20. {
  21. public:
  22. /**
  23. * @brief Constructor.
  24. *
  25. * @param rs Render system to be used by the context.
  26. * @param threadId Identifier for the thread that created the context.
  27. * @param syncedAccess If false, the deferred render context can only be safely accessed from the thread that created it.
  28. * If true, deferred render context can be accessed from any thread safely, however there will be a slight performance
  29. * hit due to synchronization. In most cases you will want not to use synced access and instead create a separate context
  30. * for specific threads.
  31. */
  32. DeferredRenderContext(CM_THREAD_ID_TYPE threadId)
  33. {
  34. mCommandQueue = cm_new<CommandQueue<CommandQueueSyncPolicy>>(threadId);
  35. }
  36. ~DeferredRenderContext()
  37. {
  38. cm_delete(mCommandQueue);
  39. }
  40. /** @copydoc RenderSystem::disableTextureUnit() */
  41. void disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  42. {
  43. mCommandQueue->queue(boost::bind(&RenderSystem::disableTextureUnit, RenderSystem::instancePtr(), gptype, texUnit));
  44. }
  45. /** @copydoc RenderSystem::setPointParameters() */
  46. void setPointParameters(float size, bool attenuationEnabled, float constant, float linear, float quadratic, float minSize, float maxSize);
  47. /** @copydoc RenderSystem::setTexture() */
  48. void setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr &texPtr)
  49. {
  50. mCommandQueue->queue(boost::bind(&RenderSystem::setTexture, RenderSystem::instancePtr(), gptype, unit, enabled, texPtr));
  51. }
  52. /** @copydoc RenderSystem::setSamplerState() */
  53. void setSamplerState(GpuProgramType gptype, UINT16 texUnit, const SamplerStatePtr& samplerState)
  54. {
  55. mCommandQueue->queue(boost::bind(&RenderSystem::setSamplerState, RenderSystem::instancePtr(), gptype, texUnit, samplerState));
  56. }
  57. /** @copydoc RenderSystem::setBlendState() */
  58. void setBlendState(const BlendStatePtr& blendState)
  59. {
  60. mCommandQueue->queue(boost::bind(&RenderSystem::setBlendState, RenderSystem::instancePtr(), blendState));
  61. }
  62. /** @copydoc RenderSystem::setRasterizerState() */
  63. void setRasterizerState(const RasterizerStatePtr& rasterizerState)
  64. {
  65. mCommandQueue->queue(boost::bind(&RenderSystem::setRasterizerState, RenderSystem::instancePtr(), rasterizerState));
  66. }
  67. /** @copydoc RenderSystem::setRasterizerState() */
  68. void setDepthStencilState(const DepthStencilStatePtr& depthStencilState, UINT32 stencilRefValue)
  69. {
  70. mCommandQueue->queue(boost::bind(&RenderSystem::setDepthStencilState, RenderSystem::instancePtr(), depthStencilState, stencilRefValue));
  71. }
  72. /** @copydoc RenderSystem::setViewport() */
  73. void setViewport(ViewportPtr& vp)
  74. {
  75. mCommandQueue->queue(boost::bind(&RenderSystem::setViewport, RenderSystem::instancePtr(), vp));
  76. }
  77. /** @copydoc RenderSystem::setVertexBuffer() */
  78. void setVertexBuffer(UINT32 index, const VertexBufferPtr& buffer)
  79. {
  80. mCommandQueue->queue(boost::bind(&RenderSystem::setVertexBuffer, RenderSystem::instancePtr(), index, buffer));
  81. }
  82. /** @copydoc RenderSystem::setIndexBuffer() */
  83. void setIndexBuffer(const IndexBufferPtr& buffer)
  84. {
  85. mCommandQueue->queue(boost::bind(&RenderSystem::setIndexBuffer, RenderSystem::instancePtr(), buffer));
  86. }
  87. /** @copydoc RenderSystem::setVertexDeclaration() */
  88. void setVertexDeclaration(VertexDeclarationPtr vertexDeclaration)
  89. {
  90. mCommandQueue->queue(boost::bind(&RenderSystem::setVertexDeclaration, RenderSystem::instancePtr(), vertexDeclaration));
  91. }
  92. /** @copydoc RenderSystem::setDrawOperation() */
  93. void setDrawOperation(DrawOperationType op)
  94. {
  95. mCommandQueue->queue(boost::bind(&RenderSystem::setDrawOperation, RenderSystem::instancePtr(), op));
  96. }
  97. /** @copydoc RenderSystem::setClipPlanes() */
  98. void setClipPlanes(const PlaneList& clipPlanes)
  99. {
  100. mCommandQueue->queue(boost::bind(&RenderSystem::setClipPlanes, RenderSystem::instancePtr(), clipPlanes));
  101. }
  102. /** @copydoc RenderSystem::addClipPlane(const Plane&) */
  103. void addClipPlane(const Plane& p)
  104. {
  105. mCommandQueue->queue(boost::bind(&RenderSystem::addClipPlane, RenderSystem::instancePtr(), p));
  106. }
  107. /** @copydoc RenderSystem::addClipPlane(float, float, float, float) */
  108. void addClipPlane(float A, float B, float C, float D)
  109. {
  110. mCommandQueue->queue(boost::bind(&RenderSystem::addClipPlane, RenderSystem::instancePtr(), A, B, C, D));
  111. }
  112. /** @copydoc RenderSystem::resetClipPlanes() */
  113. void resetClipPlanes()
  114. {
  115. mCommandQueue->queue(boost::bind(&RenderSystem::resetClipPlanes, RenderSystem::instancePtr()));
  116. }
  117. /** @copydoc RenderSystem::setScissorTest() */
  118. void setScissorTest(UINT32 left = 0, UINT32 top = 0, UINT32 right = 800, UINT32 bottom = 600)
  119. {
  120. mCommandQueue->queue(boost::bind(&RenderSystem::setScissorRect, RenderSystem::instancePtr(), left, top, right, bottom));
  121. }
  122. /** @copydoc RenderSystem::setRenderTarget() */
  123. void setRenderTarget(RenderTargetPtr target)
  124. {
  125. mCommandQueue->queue(boost::bind(&RenderSystem::setRenderTarget, RenderSystem::instancePtr(), target));
  126. }
  127. /** @copydoc RenderSystem::bindGpuProgram() */
  128. void bindGpuProgram(HGpuProgram prg)
  129. {
  130. mCommandQueue->queue(boost::bind(&RenderSystem::bindGpuProgram, RenderSystem::instancePtr(), prg));
  131. }
  132. /** @copydoc RenderSystem::unbindGpuProgram() */
  133. void unbindGpuProgram(GpuProgramType gptype)
  134. {
  135. mCommandQueue->queue(boost::bind(&RenderSystem::unbindGpuProgram, RenderSystem::instancePtr(), gptype));
  136. }
  137. /** @copydoc RenderSystem::bindGpuParams() */
  138. void bindGpuParams(GpuProgramType gptype, BindableGpuParams& params)
  139. {
  140. mCommandQueue->queue(boost::bind(&RenderSystem::bindGpuParams, RenderSystem::instancePtr(), gptype, params));
  141. }
  142. /** @copydoc RenderSystem::beginFrame() */
  143. void beginFrame(void)
  144. {
  145. mCommandQueue->queue(boost::bind(&RenderSystem::beginFrame, RenderSystem::instancePtr()));
  146. }
  147. /** @copydoc RenderSystem::endFrame() */
  148. void endFrame(void)
  149. {
  150. mCommandQueue->queue(boost::bind(&RenderSystem::endFrame, RenderSystem::instancePtr()));
  151. }
  152. /** @copydoc RenderSystem::clear() */
  153. void clear(RenderTargetPtr target, unsigned int buffers, const Color& color = Color::Black, float depth = 1.0f, unsigned short stencil = 0)
  154. {
  155. mCommandQueue->queue(boost::bind(&RenderSystem::clear, RenderSystem::instancePtr(), target, buffers, color, depth, stencil));
  156. }
  157. /** @copydoc RenderSystem::swapBuffers() */
  158. void swapBuffers(RenderTargetPtr target)
  159. {
  160. mCommandQueue->queue(boost::bind(&RenderSystem::swapBuffers, RenderSystem::instancePtr(), target));
  161. }
  162. /** @copydoc RenderSystem::render() */
  163. void render(const RenderOperation& op)
  164. {
  165. mCommandQueue->queue(boost::bind(&RenderSystem::render, RenderSystem::instancePtr(), op));
  166. }
  167. /** @copydoc RenderSystem::draw() */
  168. void draw(UINT32 vertexCount)
  169. {
  170. mCommandQueue->queue(boost::bind(&RenderSystem::draw, RenderSystem::instancePtr(), vertexCount));
  171. }
  172. /** @copydoc RenderSystem::drawIndexed() */
  173. void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexCount)
  174. {
  175. mCommandQueue->queue(boost::bind(&RenderSystem::drawIndexed, RenderSystem::instancePtr(), startIndex, indexCount, vertexCount));
  176. }
  177. /**
  178. * @copydoc RenderSystem::writeSubresource()
  179. *
  180. * @note Resource is updated with data from "data" parameter when the async operation completes.
  181. * Until the async operation completes "data" is owned by the core thread and you won't
  182. * be able to access it.
  183. */
  184. AsyncOp writeSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceData& data)
  185. {
  186. data.lock();
  187. return mCommandQueue->queueReturn(boost::bind(&RenderSystem::writeSubresource, RenderSystem::instancePtr(), resource, subresourceIdx, boost::cref(data), _1));
  188. }
  189. /**
  190. * @copydoc RenderSystem::writeSubresource()
  191. *
  192. * @note "data" parameter is populated with subresource data when the async operation completes.
  193. * Until the async operation completes "data" is owned by the core thread and you won't
  194. * be able to access it.
  195. */
  196. AsyncOp readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, GpuResourceData& data)
  197. {
  198. data.lock();
  199. return mCommandQueue->queueReturn(boost::bind(&RenderSystem::readSubresource, RenderSystem::instancePtr(), resource, subresourceIdx, boost::ref(data), _1));
  200. }
  201. /**
  202. * @brief Makes all the currently queued commands available to the core thread. They will be executed
  203. * as soon as the core thread is ready.
  204. */
  205. void submitToCoreThread(bool blockUntilComplete = false)
  206. {
  207. Queue<QueuedCommand>::type* commands = mCommandQueue->flush();
  208. gCoreThread().queueCommand(boost::bind(&CommandQueueBase::playback, mCommandQueue, commands), blockUntilComplete);
  209. }
  210. /**
  211. * @brief Cancels all commands in the queue.
  212. */
  213. void cancelAll()
  214. {
  215. mCommandQueue->cancelAll();
  216. }
  217. private:
  218. CommandQueue<CommandQueueSyncPolicy>* mCommandQueue;
  219. };
  220. }