BsCoreObjectCore.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsAsyncOp.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Represents part of a CoreObject that is meant to be used specifically
  8. * on the core thread.
  9. *
  10. * @note Core thread only.
  11. *
  12. * Different CoreObject implementations should implement this class for their
  13. * own needs.
  14. */
  15. class BS_CORE_EXPORT CoreObjectCore
  16. {
  17. protected:
  18. /**
  19. * @brief Values that represent current state of the object
  20. */
  21. enum Flags
  22. {
  23. CGCO_INITIALIZED = 0x01, /**< Object has been initialized and can be used. */
  24. CGCO_SCHEDULED_FOR_INIT = 0x02 /**< Object has been scheduled for initialization but core thread has not completed it yet. */
  25. };
  26. public:
  27. CoreObjectCore();
  28. virtual ~CoreObjectCore();
  29. /**
  30. * @brief Called on the core thread when the object is first created.
  31. */
  32. virtual void initialize();
  33. /**
  34. * @brief Internal method. Sets a shared this pointer to this object. This MUST be called immediately after construction.
  35. *
  36. * @note Called automatically by the factory creation methods so user should not call this manually.
  37. */
  38. void _setThisPtr(std::shared_ptr<CoreObjectCore> ptrThis);
  39. /**
  40. * @brief Returns a shared_ptr version of "this" pointer.
  41. */
  42. SPtr<CoreObjectCore> getThisPtr() const { return mThis.lock(); }
  43. protected:
  44. friend class CoreObjectManager;
  45. friend class CoreObject;
  46. /**
  47. * @brief Copy internal dirty data to a memory buffer that will be used
  48. * for updating sim thread version of that data.
  49. *
  50. * @note This generally happens at the end of a core thread frame. Data is then passed
  51. * to the sim thread and will be available on the next sim thread frame.
  52. */
  53. virtual CoreSyncData syncFromCore(FrameAlloc* allocator) { return CoreSyncData(); }
  54. /**
  55. * @brief Update internal data from provided memory buffer that
  56. * was populated with data from the sim thread.
  57. *
  58. * @note This generally happens at the start of a core thread frame. Data used was
  59. * recorded on the previous sim thread frame.
  60. */
  61. virtual void syncToCore(const CoreSyncData& data) { }
  62. /**
  63. * @brief Marks the core data as dirty. This causes the syncFromCore()
  64. * method to trigger the next time objects are synced between core and sim threads.
  65. *
  66. * @param flags Optional flags in case you want to signal that only part of the
  67. * internal data is dirty. syncFromCore() will be called regardless
  68. * and it's up to the implementation to read the flags value if needed.
  69. */
  70. void markCoreDirty(UINT32 flags = 0xFFFFFFFF) { mCoreDirtyFlags |= flags; }
  71. /**
  72. * @brief Marks the core data as clean. Normally called right after syncFromCore()
  73. * has been called.
  74. */
  75. void markCoreClean() { mCoreDirtyFlags = 0; }
  76. /**
  77. * @brief Checks is the core dirty flag set. This is used by external systems
  78. * to know when internal data has changed and sim thread potentially needs to be notified.
  79. */
  80. bool isCoreDirty() const { return mCoreDirtyFlags != 0; }
  81. /**
  82. * @brief Blocks the current thread until the resource is fully initialized.
  83. *
  84. * @note If you call this without calling initialize first a deadlock will occur.
  85. * You should not call this from core thread.
  86. */
  87. void synchronize();
  88. /**
  89. * @brief Returns true if the object has been properly initialized. You are not
  90. * allowed to call any methods on the object until it is initialized.
  91. */
  92. bool isInitialized() const { return (mFlags & CGCO_INITIALIZED) != 0; }
  93. bool isScheduledToBeInitialized() const { return (mFlags & CGCO_SCHEDULED_FOR_INIT) != 0; }
  94. void setIsInitialized(bool initialized) { mFlags = initialized ? mFlags | CGCO_INITIALIZED : mFlags & ~CGCO_INITIALIZED; }
  95. void setScheduledToBeInitialized(bool scheduled) { mFlags = scheduled ? mFlags | CGCO_SCHEDULED_FOR_INIT : mFlags & ~CGCO_SCHEDULED_FOR_INIT; }
  96. UINT32 mCoreDirtyFlags;
  97. volatile UINT8 mFlags;
  98. std::weak_ptr<CoreObjectCore> mThis;
  99. BS_STATIC_THREAD_SYNCHRONISER(mCoreGpuObjectLoadedCondition)
  100. BS_STATIC_MUTEX(mCoreGpuObjectLoadedMutex)
  101. };
  102. }