BsCoreThreadAccessor.h 7.2 KB

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