BsCoreThreadAccessor.h 7.5 KB

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