CmCoreThreadAccessor.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmCommonEnums.h"
  4. #include "CmRenderSystem.h"
  5. #include "CmCommandQueue.h"
  6. #include "CmAsyncOp.h"
  7. #include "CmColor.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Contains some base functionality used for CoreThreadAccessor.
  12. *
  13. * @see CoreThreadAccesor
  14. */
  15. class CM_EXPORT CoreThreadAccessorBase
  16. {
  17. public:
  18. CoreThreadAccessorBase(CommandQueueBase* commandQueue);
  19. virtual ~CoreThreadAccessorBase();
  20. /** @copydoc RenderSystem::disableTextureUnit() */
  21. void disableTextureUnit(GpuProgramType gptype, UINT16 texUnit);
  22. /** @copydoc RenderSystem::setTexture() */
  23. void setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr &texPtr);
  24. /** @copydoc RenderSystem::setSamplerState() */
  25. void setSamplerState(GpuProgramType gptype, UINT16 texUnit, const SamplerStatePtr& samplerState);
  26. /** @copydoc RenderSystem::setBlendState() */
  27. void setBlendState(const BlendStatePtr& blendState);
  28. /** @copydoc RenderSystem::setRasterizerState() */
  29. void setRasterizerState(const RasterizerStatePtr& rasterizerState);
  30. /** @copydoc RenderSystem::setRasterizerState() */
  31. void setDepthStencilState(const DepthStencilStatePtr& depthStencilState, UINT32 stencilRefValue);
  32. /** @copydoc RenderSystem::setViewport() */
  33. void setViewport(const ViewportPtr& vp);
  34. /** @copydoc RenderSystem::setDrawOperation() */
  35. void setDrawOperation(DrawOperationType op);
  36. /** @copydoc RenderSystem::setClipPlanes() */
  37. void setClipPlanes(const PlaneList& clipPlanes);
  38. /** @copydoc RenderSystem::addClipPlane(const Plane&) */
  39. void addClipPlane(const Plane& p);
  40. /** @copydoc RenderSystem::resetClipPlanes() */
  41. void resetClipPlanes();
  42. /** @copydoc RenderSystem::setScissorTest() */
  43. void setScissorTest(UINT32 left = 0, UINT32 top = 0, UINT32 right = 800, UINT32 bottom = 600);
  44. /** @copydoc RenderSystem::setRenderTarget() */
  45. void setRenderTarget(RenderTargetPtr target);
  46. /** @copydoc RenderSystem::bindGpuProgram() */
  47. void bindGpuProgram(HGpuProgram prg);
  48. /** @copydoc RenderSystem::unbindGpuProgram() */
  49. void unbindGpuProgram(GpuProgramType gptype);
  50. /** @copydoc RenderSystem::bindGpuParams() */
  51. void bindGpuParams(GpuProgramType gptype, const GpuParamsPtr& params);
  52. /** @copydoc RenderSystem::beginFrame() */
  53. void beginRender();
  54. /** @copydoc RenderSystem::endFrame() */
  55. void endRender();
  56. /** @copydoc RenderSystem::clearRenderTarget() */
  57. void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
  58. /** @copydoc RenderSystem::clearViewport() */
  59. void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
  60. /** @copydoc RenderSystem::swapBuffers() */
  61. void swapBuffers(RenderTargetPtr target);
  62. /** @copydoc RenderSystem::render() */
  63. void render(const MeshBasePtr& mesh, UINT32 indexOffset = 0, UINT32 indexCount = 0, bool useIndices = true, DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  64. /** @copydoc RenderSystem::draw() */
  65. void draw(UINT32 vertexOffset, UINT32 vertexCount);
  66. /** @copydoc RenderSystem::drawIndexed() */
  67. void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount);
  68. /**
  69. * @brief Binds the specified parameters to the pass GPU programs and activates the pass and its
  70. * states for any further rendering.
  71. */
  72. void setPass(const PassPtr& pass, const PassParametersPtr& params);
  73. /**
  74. * @copydoc RenderSystem::writeSubresource()
  75. *
  76. * @param discardEntireBuffer When true the existing contents of the resource you are updating will be discarded. This can make the
  77. * operation faster. Resources with certain buffer types might require this flag to be in a specific state
  78. * otherwise the operation will fail.
  79. *
  80. * @note Resource is updated with data from "data" parameter when the async operation completes.
  81. * Until the async operation completes "data" is owned by the core thread and you won't
  82. * be able to access it.
  83. *
  84. * Normally dynamic buffers will require you to enable "discardEntireBuffer" flag, while static buffers require it disabled.
  85. */
  86. AsyncOp writeSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceDataPtr& data, bool discardEntireBuffer = false);
  87. /**
  88. * @copydoc RenderSystem::readSubresource()
  89. *
  90. * @note "data" parameter is populated with subresource data when the async operation completes.
  91. * Until the async operation completes "data" is owned by the core thread and you won't
  92. * be able to access it.
  93. */
  94. AsyncOp readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceDataPtr& data);
  95. /**
  96. * @brief Resize the provided window to specified width and height in pixels.
  97. */
  98. void resizeWindow(RenderWindowPtr& renderWindow, UINT32 width, UINT32 height);
  99. /**
  100. * @brief Move the provided window to specified screen coordinates.
  101. */
  102. void moveWindow(RenderWindowPtr& renderWindow, INT32 left, INT32 top);
  103. /**
  104. * @brief Hide the provided window. (Does not destroy it, just hides it).
  105. */
  106. void hideWindow(RenderWindowPtr& renderWindow);
  107. /**
  108. * @brief Shows a previously hidden window.
  109. */
  110. void showWindow(RenderWindowPtr& renderWindow);
  111. /**
  112. * @brief Queues a new generic command that will be added to the command queue.
  113. */
  114. AsyncOp queueReturnCommand(std::function<void(AsyncOp&)> commandCallback);
  115. /**
  116. * @brief Queues a new generic command that will be added to the command queue.
  117. */
  118. void queueCommand(std::function<void()> commandCallback);
  119. /**
  120. * @brief Makes all the currently queued commands available to the core thread. They will be executed
  121. * as soon as the core thread is ready. All queued commands are removed from the accessor.
  122. */
  123. void submitToCoreThread(bool blockUntilComplete = false);
  124. /**
  125. * @brief Cancels all commands in the queue.
  126. */
  127. void cancelAll();
  128. private:
  129. CommandQueueBase* mCommandQueue;
  130. };
  131. /**
  132. * @brief Core thread accessor allows you to schedule core commands outside of the core thread. Provides a set of common
  133. * methods you may want to execute on the core thread, as well as a general command queuing methods.
  134. *
  135. * @note Queued commands are only executed after the call to submitToCoreThread, in the order they were submitted.
  136. */
  137. template <class CommandQueueSyncPolicy = CommandQueueNoSync>
  138. class CM_EXPORT CoreThreadAccessor : public CoreThreadAccessorBase
  139. {
  140. public:
  141. /**
  142. * @brief Constructor.
  143. *
  144. * @param threadId Identifier for the thread that created the accessor.
  145. */
  146. CoreThreadAccessor(CM_THREAD_ID_TYPE threadId)
  147. :CoreThreadAccessorBase(cm_new<CommandQueue<CommandQueueSyncPolicy>>(threadId))
  148. {
  149. }
  150. };
  151. }