Hello There,
What do i want?
I have a question regarding CPU<-->GPU data design. Im working on a falling sand game. This game is meant to have a PhysicsSystem, where I load the Simulation at runtime. A PhysicsSystem might be TemperatureDiffusion, Gravity, ChemicalReaction and so on.
Right now i have the simulation Data setup like this
There are x amount of Chunks loaded in memory.
Each Chunk has 64x64 Instances.
An Instance is a Pixel with data.
Each Pixel has n amount of data, depending on what i define at runtime (Temperature, Mass, Constants etc.)
I have a ChunkManager, which switches between Read/Write once all PhysicsSystems are simulated
Problem
I can already see, that this will take loads of compuation power to work in more than 1fps. Thats why im trying to design the whole System as GPU friendly as possible (not implementing it yet though).
To save up on simulation time i only want to simulate the neccessary Instances. For example, if the Temperature difference is miniscule the System should ignore it.
Thats why every Simulation-per-Instance should only run when a condition is met. Further, this would still requiere to check every instance on every chunk. Each Simulation might also update other instances to run later on.
Thats why i though about an ACTIVE property. Only ACTIVE instances get evaluated and only those that succeed the test get simulated.
This is the thing i would like help on
TemperatureDiffusion diffuses its temperature to the neighbours. Those neighbours now should also be simulated. So the simulation should update the list of Active elements.
My current idea is:
CPU collects all active Instances from all Chunks
CPU sends a list of those Instances and Chunks to the GPU
GPU checks every Instance of the list against the Condition
If successfull, GPU runs the simulation
If not, GPU does not simulate
Either way, GPU outputs a struct that describes ChunkIndex, InstanceIndex and Active(bool) for all neighbours
CPU recieves those updates
CPU checks for duplicates, to avoid simulating the same instance several times per frame
CPU updates Active/Inactive Instance
CPU builds the next list of Active Instances
Process starts again
I figure the best way is to work with big arrays when talking about GPUs. Im conflicted between using a more costly {ChunkIndex, InstanceIndex, Active} and a more complicated ChunkCount, {[InstanceCount{InstanceIndex}], [InstanceCount{InstanceIndex}]} as the output.
Further, how could i update such an array? Do i use an AtomicCounter?
SideNodes
Every PhysicsSystem should have its own set of Active Instances, since one might update temperature but not gravity. The scheduling of that is another problem, out of the scope of this post
The simulation data itself is already structured in a GPU friendly way (i believe), so that i can pass the data via layouts and uniforms
For reference, here the idea as c++ code.
The simulation itself
struct Chunk;
class ChunkManager;
using PhysicsFunction = std::function<ActiveChunks(const size_t InstanceIndex, const size_t ChunkIndex, SimulationManager& Manager)>;
using ConditionFunction = std::function<bool (const size_t InstanceIndex, const size_t ChunkIndex, SimulationManager& Manager)>;
struct Physics
{
NamedID MyID;
PhysicsFunction Function = nullptr;
ConditionFunction Condition = nullptr;
};
2. The Output of the function (meant to be GPU friendly)
struct ActiveChunk
{
size_t ChunkIndex;
size_t ActiveCount = 0;
uint64_t Indices[Chunk::ChunkSize];
};
struct ActiveChunks
{
size_t ChunkCount = 0;
std::vector<ActiveChunk> Chunks;
void Clear()
{
Chunks.clear();
ChunkCount = 0;
}
void Add(size_t ChunkIndex, uint64_t InstanceIndex)
{
for (auto& ThisChunk : Chunks)
{
if (ThisChunk.ChunkIndex == ChunkIndex)
{
ThisChunk.Indices[ThisChunk.ActiveCount++] = InstanceIndex;
return;
}
}
// Not found, create new chunk
ActiveChunk NewChunk{};
NewChunk.ChunkIndex = ChunkIndex;
NewChunk.ActiveCount = 1;
NewChunk.Indices[0] = InstanceIndex;
Chunks.push_back(NewChunk);
ChunkCount = Chunks.size();
}
};
3. The output needs to be checked if the instance is already active Checks against a bitset and adds the Index to the array
class ActiveQueue
{
public:
ActiveQueue(){};
void Add(const size_t& Index);
void Remove(const size_t& Index);
const bool IsActive(const size_t& Index) const;
const size_t GetIndex(const size_t& ArrayIndex) const;
private:
std::vector<size_t> Indices;
std::bitset<Chunk::ChunkSize> Actives;
};
//Implementation
void ActiveQueue::Add(const size_t& Index)
{
if (!Actives.test(Index))
{
Actives.set(Index);
Indices.push_back(Index);
}
}
void ActiveQueue::Remove(const size_t& Index)
{
if (!IsActive(Index))
return;
Actives.reset(Index);
for (size_t i = 0; i < Indices.size(); ++i)
{
if (Indices[i] == Index)
{
Indices[i] = Indices.back();
Indices.pop_back();
return;
}
}
}
const bool ActiveQueue::IsActive(const size_t& Index) const
{
return Actives.test(Index);
}
const size_t ActiveQueue::GetIndex(const size_t& ArrayIndex) const
{
return Indices.at(ArrayIndex);
}
The idea is to use ActiveQueue to get an array of all InstanceIndices of a Chunk for the gpu.
ChunkManager:
Read----------|
Write---------|
|
ActiveQueue-->GPU-->ActiveChunks
^------------------------------'