Commit Graph

37 Commits (master)

Author SHA1 Message Date
elsid 7f1e5b368e
Fix deadlock in physics system
* Reorder unlock and notify_all calls to avoid notifying when not all worker
  threads are waiting.
* Make sure main thread does not attempt to exclusively lock mSimulationMutex
  while not all workers are done with previous frame.
* Replace mNewFrame flag by counter to avoid modification from multiple
  threads.
3 years ago
psi29a d1c0cfa524 Merge branch 'lifetime' into 'master'
Maybe fix #6071

Closes #6071

See merge request OpenMW/openmw!1010

(cherry picked from commit 64750820957773a00d449d29551bb15fbf5fe08b)

1650dabe Assign the return value of weak_ptr::lock() to a variable, so that the
4 years ago
jvoisin c1e3869f25 Remove an unused struct 4 years ago
elsid 626e032931
Do not store callback inside Misc::Barrier
The only wait method can be provided with it so pass it as a template
parameter there.
4 years ago
fredzio 4fa0972b2d Tone down actor's skip simulation flag to an optional skip collision
detection flag.
4 years ago
fredzio 6e1c67a9ae Account for waterwalking when updating position. Otherwise we might
trace down the actor at waterlevel at the wrong coordinate.

Triggered by multimark mod with waterwalking effect.
4 years ago
fredzio b58244ac26 Guard the Bullet drawing method with a read lock on the
btCollisionWorld. It closes a race on the collision shapes coordinates.
4 years ago
fredzio dbd6e3bfee Replace pointless usage of shared_ptr by unique_ptr / non-owning raw
pointer for btCollisionWorld.
4 years ago
wareya 63f01d8c5f Prevent physics death spiral by falling back to true delta time when needed 4 years ago
wareya 1471ef003a fix async physics interpolation 4 years ago
fredzio 03b86c232b Apply the position offset even if the simulation is not performed
because we're too fast.
4 years ago
Frederic Chardon 7cd7fa2f08 Collect all available stats if OPENMW_OSG_STATS_FILE is set and point to
a valid file.
4 years ago
fredzio e37e5d4d16 Don't run unstuck if there is no simulation running in async case.
In this case, the actor mPreviousPosition is not updated, so the actor
position is interpolated between an old (stucked) position and the new
(unstucked) position. The new position is most likely "stucked", so the
unstuck code strikes again, making the actor "vibrates".

That's exactly what the sync code path does, and it doesn't exhibit this
behavior.
4 years ago
elsid 8b7f3fe908 Merge branch 'nodeadlock' into 'master'
Avoid a rare but possible deadlock around mCollisionWorldMutex.

See merge request OpenMW/openmw!525
4 years ago
fredzio 9bc687e209 Avoid a rare but possible deadlock around mCollisionWorldMutex.
What happened is that the last handle to an Actor shared_ptr was a
promoted weak_ptr. When the shared_ptr goes out of scope, the Actor dtor
is invoked. That involves removing the Actor collision object after
exclusively locking mCollisionWorldMutex. In this case, the lock was
already held in the outter scope of the promoted weak_ptr.

Reduce the scope of the mCollisionWorldMutex to never encompass the
lifetime of a promoted weak_ptr.
4 years ago
Andrei Kortunov 33648313a6 Initialize variables 4 years ago
wareya 18ef32ca82 values for this higher than sGroundOffset cause jittering on some surface; use safe-seeming value slightly less than sGroundOffset 4 years ago
Alexei Dobrohotov e9a8636d18 Merge branch 'restore_absolute_position_handling' into 'master'
Restore pre-async handling of absolute actors positionning

See merge request OpenMW/openmw!486
4 years ago
Alexei Dobrohotov ce36dd8a52 Merge branch 'misc_cleanup' into 'master'
Small cleanup

See merge request OpenMW/openmw!482
4 years ago
fredzio 5bd921fa3a Restore pre-async handling of absolute actors positionning
One of the issue since the introduction of async physics is the quirky
handling of scripted moves. Previous attempt to account for them was
based on detecting changes in actor position while the physics thread is
running. To this end, semantics of Actor::updatePosition() (which is
responsible for set the absolute position of an actor in the world) was
toned down to merely store the desired position, with the physics system
actually responsible for moving the actor. For the cases were complete
override of the physics simulation was needed, I introduced
Actor::resetPosition(), which actually have same semantics as
original updatePosition(). This in turn introduced a loads of new bugs
when the weakened semantics broke key assumptions inside the engine
(spawning, summoning, teleport, etc).
Instead of tracking them down, count on the newly introduced support for
object relative movements in the engine (World::moveObjectBy) to
register relative movements and restore original handling of absolute positionning.

Changes are relatively small:
- move resetPosition() content into updatePosition()
- call updatePosition() everywhere it was called before
- remove all added calls to the now non-existing resetPosition()

tldr; ditch last month worth of bug introduction and eradication and redo
it properly
4 years ago
fredzio 8e084dea2e Don't cache Ptr, it can be updated while the simulation is running. 4 years ago
fredzio 58297ffbf4 Move stats update into their own function. 4 years ago
fredzio 4e7c9b6696 Embed physics simulation results inside of actor class.
This gives finer control over reseting positions (switch off tcl is no
longer glitchy) and solve most of the erroneous usage of stale World::Ptr
indicated by:
"Error in frame: moveTo: object is not in this cell"
4 years ago
fredzio 7bae6691b6 Introduce World::moveObjectBy() function to translate an object relatively to
its current position.
Use it in relevant MWScripts opcode (move and moveworld).
Remove the fragile detection of scripted translation from PhysicsTaskScheduler.

No user visible change, just a more robust mechanism.
4 years ago
fredzio 66fe3b0d38 Modify projectile collision to work with async physics 4 years ago
fredzio 7843dad35d Don't let the actor "nowhere" after a teleport but move them in their
place.
This solve the problem where after loading, an empty frame was rendered
because the player is "nowhere".
4 years ago
fredzio 5a4872393a Rework actor position reset. While solving the issue with invalid
position being used under heavy load, I introduced a regression that
prevented the position to be updated in case of teleport.
Move the logic in its own function and decide in PhysicsSystem whether a
reset is needed.
4 years ago
fredzio ea2ba27084 Move the moment when the actor origin is saved before simulation so to
be sure the simulation is over. Otherwise, if the simulation is too slow
the position is wrong, and the actors would jump back and forth between
old and new position instead of actually moving.
4 years ago
Frederic Chardon 9aba55a21a Add the async physics worker to the profiler overlay. 4 years ago
Frederic Chardon bb5213670c Use bigger hammer to set Actor's position after teleporting. Otherwise traceDown() would use old collision object transform and gives incorrect results, making the Actor "fall" in the new position. 4 years ago
fredzio d64ed6cf53 Get rid of the StandingActorsMap. Just embed the necessary info into
Actor class.
4 years ago
fredzio e5fa457fe7 Properly account for interleaved move of actors.
Before this change, if an actor position was changed while the physics
simulation was running, the simulation result would be discarded. It is
fine in case of one off event such as teleport, but in the case of
scripts making use of this functionality to make lifts or conveyor (such
as Sotha Sil Expanded mod) it broke actor movement.

To alleviate this issue, at the end of the simulation, the position of the Actor
in the world is compared to the position it had at the beginning of the
simulation. A difference indicate a force move occured. In this case,
the Actor mPosition and mPreviousPosition are translated by the difference of position.

Since the Actor position will be really set while the next simulation runs, we
save it in the mNextPosition field.
4 years ago
Andrei Kortunov 64ba81ecf2 Fix some issues, found by CoverityScan 4 years ago
fredzio 1357bba0a0 Use some C++17 where it makes the code more readable
Also replace boost::optional
4 years ago
fredzio a036183248 Use a much simpler and assert-free check for Bullet multithreading support 4 years ago
fredzio 978af12c2d Restore fall damage 4 years ago
fredzio 3c2504b442 Process movement queue in one or several background threads
Before movement calculation, the main thread prepare a
vector of ActorFrameData, which contains all data necessary to perform
the simulation, and feed it to the solver. At the same time it fetches
the result from the previous background simulation, which in turn is
used by the game mechanics.
Other functions of the physics system (weapon hit for instance)
interrupt the background simulation, with some exceptions described
below.

The number of threads is controlled by the numeric setting

[Physics]
async num threads

In case 'async num threads' > 1 and Bullet doesn't support multiple threads,
1 async thread will be used. 0 means synchronous solver.
Additional settings (will be silently switched off if async num threads = 0)

[Physics]
defer aabb update

Update AABBs of actors and objects in the background thread(s). It is not an especially
costly operation, but it needs exclusive access to the collision world, which blocks
other operations. Since AABB needs to be updated for collision detection, one can queue
them to defer update before start of the movement solver. Extensive tests on as much
as one installation (mine) show no drawback having that switched on.

[Physics]
lineofsight keep inactive cache

Control for how long (how many frames) the line of sight (LOS) request will be kept updated.
When a request for LOS is made for the first time, the background threads are stopped to
service it. From now on, the LOS will be refreshed preemptively as part of the background
routine until it is not required for lineofsight keep inactive cache frames. This mean
that subsequent request will not interrupt the background computation.
4 years ago