mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-07-31 13:40:12 +02:00
Stop copying the gravity process's input every frame
In the vast majority of cases (i.e. when Gravity::Exchange is indirectly called from BeforeSim), the input is cleared to zeros anyway, so it's less work in Exchange to actually just exchange the old input with the new one than to copy the new one into the old one. Also make gravity functions private in Simulation.
This commit is contained in:
@@ -3740,6 +3740,7 @@ void Simulation::BeforeSim()
|
||||
air->update_airh();
|
||||
|
||||
DispatchNewtonianGravity();
|
||||
// gravIn is now potentially garbage, which is ok, we were going to clear it for the frame anyway
|
||||
for (auto p : gravIn.mass.Size().OriginRect())
|
||||
{
|
||||
gravIn.mass[p] = 0.f;
|
||||
@@ -3964,6 +3965,8 @@ void Simulation::ResetNewtonianGravity(GravityInput newGravIn, GravityOutput new
|
||||
{
|
||||
gravIn = newGravIn;
|
||||
DispatchNewtonianGravity();
|
||||
// gravIn is now potentially garbage, set it again
|
||||
gravIn = newGravIn;
|
||||
if (grav)
|
||||
{
|
||||
gravOut = newGravOut;
|
||||
@@ -3981,7 +3984,10 @@ void Simulation::EnableNewtonianGravity(bool enable)
|
||||
if (!grav && enable)
|
||||
{
|
||||
grav = Gravity::Create();
|
||||
auto oldGravIn = gravIn;
|
||||
DispatchNewtonianGravity();
|
||||
// gravIn is now potentially garbage, set it again
|
||||
gravIn = std::move(oldGravIn);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -220,10 +220,11 @@ public:
|
||||
~Simulation();
|
||||
|
||||
void EnableNewtonianGravity(bool enable);
|
||||
void ResetNewtonianGravity(GravityInput newGravIn, GravityOutput newGravOut);
|
||||
void DispatchNewtonianGravity();
|
||||
void UpdateGravityMask();
|
||||
|
||||
private:
|
||||
CoordStack& getCoordStackSingleton();
|
||||
|
||||
void ResetNewtonianGravity(GravityInput newGravIn, GravityOutput newGravOut);
|
||||
void DispatchNewtonianGravity();
|
||||
void UpdateGravityMask();
|
||||
};
|
||||
|
@@ -204,7 +204,7 @@ void GravityImpl::Init()
|
||||
});
|
||||
}
|
||||
|
||||
void Gravity::Exchange(GravityOutput &gravOut, const GravityInput &gravIn)
|
||||
void Gravity::Exchange(GravityOutput &gravOut, GravityInput &gravIn)
|
||||
{
|
||||
auto *fftGravity = static_cast<GravityImpl *>(this);
|
||||
|
||||
@@ -231,7 +231,7 @@ void Gravity::Exchange(GravityOutput &gravOut, const GravityInput &gravIn)
|
||||
std::memcmp(&fftGravity->gravIn.mask[{ 0, 0 }], &gravIn.mask[{ 0, 0 }], NCELL * sizeof(float)))
|
||||
{
|
||||
fftGravity->copyGravOut = true;
|
||||
fftGravity->gravIn = gravIn;
|
||||
std::swap(gravIn, fftGravity->gravIn);
|
||||
}
|
||||
|
||||
fftGravity->Dispatch();
|
||||
|
@@ -8,7 +8,8 @@ protected:
|
||||
Gravity() = default;
|
||||
|
||||
public:
|
||||
void Exchange(GravityOutput &gravOut, const GravityInput &gravIn);
|
||||
// potentially clobbers gravIn
|
||||
void Exchange(GravityOutput &gravOut, GravityInput &gravIn);
|
||||
|
||||
static GravityPtr Create();
|
||||
};
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#include "Gravity.h"
|
||||
|
||||
void Gravity::Exchange(GravityOutput &gravOut, const GravityInput &gravIn)
|
||||
void Gravity::Exchange(GravityOutput &gravOut, GravityInput &gravIn)
|
||||
{
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user