BsCoreThreadAccessor.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsRenderSystem.h"
  4. #include "BsCommandQueue.h"
  5. #include "BsAsyncOp.h"
  6. #include "BsViewport.h"
  7. #include "BsColor.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Contains some base functionality used for CoreThreadAccessor.
  12. *
  13. * @see CoreThreadAccesor
  14. */
  15. class BS_CORE_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(Viewport 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. * @copydoc RenderSystem::writeSubresource()
  70. *
  71. * @param discardEntireBuffer When true the existing contents of the resource you are updating will be discarded. This can make the
  72. * operation faster. Resources with certain buffer types might require this flag to be in a specific state
  73. * otherwise the operation will fail.
  74. *
  75. * @note Resource is updated with data from "data" parameter when the async operation completes.
  76. * Until the async operation completes "data" is owned by the core thread and you won't
  77. * be able to access it.
  78. *
  79. * Normally dynamic buffers will require you to enable "discardEntireBuffer" flag, while static buffers require it disabled.
  80. */
  81. AsyncOp writeSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceDataPtr& data, bool discardEntireBuffer = false);
  82. /**
  83. * @copydoc RenderSystem::readSubresource()
  84. *
  85. * @note "data" parameter is populated with subresource data when the async operation completes.
  86. * Until the async operation completes "data" is owned by the core thread and you won't
  87. * be able to access it.
  88. */
  89. AsyncOp readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceDataPtr& data);
  90. /**
  91. * @brief Resize the provided window to specified width and height in pixels.
  92. */
  93. void resizeWindow(const RenderWindowPtr& renderWindow, UINT32 width, UINT32 height);
  94. /**
  95. * @brief Move the provided window to specified screen coordinates.
  96. */
  97. void moveWindow(const RenderWindowPtr& renderWindow, INT32 left, INT32 top);
  98. /**
  99. * @brief Hide the provided window. (Does not destroy it, just hides it).
  100. */
  101. void hideWindow(const RenderWindowPtr& renderWindow);
  102. /**
  103. * @brief Shows a previously hidden window.
  104. */
  105. void showWindow(const RenderWindowPtr& renderWindow);
  106. /**
  107. * @copydoc RenderWindow::setFullscreen(UINT32, UINT32, float, UINT32)
  108. */
  109. void setFullscreen(const RenderWindowPtr& renderWindow, UINT32 width, UINT32 height, float refreshRate = 60.0f, UINT32 monitorIdx = 0);
  110. /**
  111. * @copydoc RenderWindow::setFullscreen(const VideoMode&)
  112. */
  113. void setFullscreen(const RenderWindowPtr& renderWindow, const VideoMode& mode);
  114. /**
  115. * @copydoc RenderWindow::setWindowed
  116. */
  117. void setWindowed(const RenderWindowPtr& renderWindow, UINT32 width, UINT32 height);
  118. /**
  119. * @copydoc RenderTargetcore::setPriority
  120. */
  121. void setPriority(const RenderTargetPtr& renderTarget, UINT32 priority);
  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. }