BsCoreThreadAccessor.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsRenderAPI.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. /**
  21. * @brief Queues a new generic command that will be added to the command queue. Returns an async operation object
  22. * that you may use to check if the operation has finished, and to retrieve the return value once finished.
  23. */
  24. AsyncOp queueReturnCommand(std::function<void(AsyncOp&)> commandCallback);
  25. /**
  26. * @brief Queues a new generic command that will be added to the command queue.
  27. */
  28. void queueCommand(std::function<void()> commandCallback);
  29. /**
  30. * @brief Makes all the currently queued commands available to the core thread. They will be executed
  31. * as soon as the core thread is ready. All queued commands are removed from the accessor.
  32. *
  33. * @param blockUntilComplete If true, the calling thread will block until the core thread finished executing
  34. * all currently queued commands. This is usually very expensive and should only be
  35. * used in performance non-critical code.
  36. */
  37. void submitToCoreThread(bool blockUntilComplete = false);
  38. /**
  39. * @brief Cancels all commands in the queue.
  40. */
  41. void cancelAll();
  42. private:
  43. CommandQueueBase* mCommandQueue;
  44. };
  45. /**
  46. * @brief Core thread accessor allows you to schedule core commands outside of the core thread. Provides a set of common
  47. * methods you may want to execute on the core thread, as well as a general command queuing methods.
  48. *
  49. * @note Queued commands are only executed after the call to submitToCoreThread, in the order they were submitted.
  50. */
  51. template <class CommandQueueSyncPolicy = CommandQueueNoSync>
  52. class BS_CORE_EXPORT CoreThreadAccessor : public CoreThreadAccessorBase
  53. {
  54. public:
  55. /**
  56. * @brief Constructor.
  57. *
  58. * @param threadId Identifier for the thread that created the accessor.
  59. */
  60. CoreThreadAccessor(BS_THREAD_ID_TYPE threadId)
  61. :CoreThreadAccessorBase(bs_new<CommandQueue<CommandQueueSyncPolicy>>(threadId))
  62. {
  63. }
  64. };
  65. }