stride3d / stride3d/stride

Make BepuSimulation.ThreadCount actually configurable

Open
#3,269 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
7.8k
Forks
1.2k
Avg merge
2d 17h
Merged PRs (30d)
49

Description

Is your feature request related to a problem? Please describe.

BepuSimulation has a ThreadCount property, but setting it has no effect. The property is only read once, in the constructor:

https://github.com/stride3d/stride/blob/releases/4.2.1.2487/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BepuSimulation.cs#L285-L292

As far as I understand it, the constructor runs before any properties can be set from the outside, and at that moment ThreadCount is still at its default of -1. So the check ThreadCount > -1 never sees a custom value, no matter where the property gets assigned. The automatic thread count wins every time. Setting ThreadCount later does not help either, because _threadDispatcher is a readonly field that is created once in the constructor and never recreated.

Describe the solution you'd like

Setting ThreadCount at runtime should change the number of threads the physics simulation uses:

var sim = bepuConfiguration.BepuSimulations[0];
sim.ThreadCount = 4;  // physics now uses 4 threads
sim.ThreadCount = -1; // back to automatic selection

From a conversation with @Eideren on discord: the ThreadDispatcher can be disposed and recreated when the value changes, but the ContactEventsManager receives the same thread count and has to be adapted alongside.

Describe alternatives you've considered

  • Leaving it as is: the automatic selection (all cores minus one or two) is a reasonable default, but see the use case below.

Additional context

Use case: the automatic selection takes almost all cores. That is fine when the game is the only heavy process, but in host applications with other systems running inparallel to the game loop (in my case vvvv gamma patches doing for example video decoding and audio processing alongside the simulation) it is useful to reserve cores by giving physics fewer threads.

Found while wrapping Stride.BepuPhysics for vvvv gamma (https://github.com/bj-rn/VL.Stride.BepuPhysics).
Happy to test a fix in against version 4.2.1.2487 which is currently used by vvvv.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BepuSimulation.cs at the constructor code linked in the issue, then trace the readonly _threadDispatcher and ContactEventsManager usage. Make runtime changes to ThreadCount recreate the dispatcher and keep the contact-events thread count aligned; verify both explicit counts and -1 automatic selection, including the vvvv gamma use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
game-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.