6#ifndef CORI_MAX_PHYSICS_THREADS
7 #define CORI_MAX_PHYSICS_THREADS 4
16 m_CurrentTaskIndex.store(0, std::memory_order_relaxed);
32 params.workerCount = m_WorkerCount;
35 params.userTaskContext =
this;
39 m_World = Cori::Physics::World(params);
46 auto* physicsSystem =
static_cast<PhysicsSystem*
>(userContext);
47 const uint16_t workerCount = physicsSystem->m_WorkerCount;
49 if (workerCount <= 1 || itemCount < minRange) {
50 task(0, itemCount, 0, taskContext);
54 const uint16_t taskIndex = physicsSystem->m_CurrentTaskIndex.fetch_add(1, std::memory_order_relaxed);
57 Box2DTaskGroup& taskGroup = physicsSystem->m_TaskPool[taskIndex];
59 taskGroup.futures.clear();
61 int32_t itemsPerThread = (itemCount + workerCount - 1) / workerCount;
62 itemsPerThread = std::max(itemsPerThread, minRange);
64 int32_t startIndex = 0;
65 for (uint16_t i = 0; i < workerCount && startIndex < itemCount; ++i) {
66 const int32_t endIndex = std::min(startIndex + itemsPerThread, itemCount);
70 task(startIndex, endIndex, i, taskContext);
73 startIndex = endIndex;
80 if (taskPtr ==
nullptr) {
84 auto* taskGroup =
static_cast<Box2DTaskGroup*
>(taskPtr);
86 for (
auto& future : taskGroup->futures) {
91 void PhysicsSystem::OnRigidBodyCreate(entt::registry& registry, entt::entity entity) {
92 Entity e = entt::handle{ registry, entity };
94 const auto type = rb.GetType();
95 if (type == b2_kinematicBody || type == b2_dynamicBody) {
#define CORI_MULTITHREADED_PHYSICS
#define CORI_PHYSICS_TASK_POOL_SIZE
#define CORI_MAX_PHYSICS_THREADS
#define CORI_CORE_ASSERT(x,...)
#define CORI_PROFILE_FUNCTION()
static std::future< std::invoke_result_t< F, Args... > > SubmitWorkerTask(F &&f, Args &&... args)
Submits a task to be executed on the worker thread.
static uint16_t GetWorkerCount()
Returns a number of available worker threads.
A GameTimer is responsible for managing everything that is connected with time, ticks,...
float GetTimestep() const
Returns the current timeStep, scale is in seconds.
Entities are the essential part of WorldSystem.
T & AddComponent(Args &&... args)
Adds a component to the entity.
decltype(auto) GetComponents()
Retries the references to the requested components of the entity.
System responsible for physics.
static void FinishTask(void *taskPtr, void *userContext)
bool Create(Physics::World::Params params)
void OnTickUpdate(Core::GameTimer &gameTimer) override
static void * EnqueueTask(b2TaskCallback *task, int32_t itemCount, int32_t minRange, void *taskContext, void *userContext)