BsCoreThreadAccessor.h 7.3 KB

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