Skip to content

Commit

Permalink
fixed sizing issues for radius cache
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoryGraborenko committed Dec 22, 2024
1 parent 26a3294 commit f853595
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Src/Shaders/GridCache3D.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn ItemMain(item_index: i32, pos: vec3f) {
let index: i32 = (grid_pos.x + (grid_pos.y + grid_pos.z * Uniform.GridSize.y) * Uniform.GridSize.x) * 3;

#ifdef PHASE_INDEX
atomicAdd(&TotalSize, 1);
atomicAdd(&b_Index[index], 1);
atomicCompareExchangeWeak(&b_Index[index + 1], 0, -1);
#elifdef PHASE_ALLOCATE
Expand Down Expand Up @@ -48,6 +49,7 @@ fn ItemRadiusMain(item_index: i32, pos: vec3f, radius: f32) {
let grid_pos = vec3i(x, y, z);
let index : i32 = (grid_pos.x + (grid_pos.y + grid_pos.z * Uniform.GridSize.y) * Uniform.GridSize.x) * 3;
#ifdef PHASE_INDEX
atomicAdd(&TotalSize, 1);
atomicAdd(&b_Index[index], 1);
atomicCompareExchangeWeak(&b_Index[index + 1], 0, -1);
#elifdef PHASE_ALLOCATE
Expand Down
4 changes: 2 additions & 2 deletions Src/UnitTests/EntityTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ namespace Test {
////////////////////////////////////////////////////////////////////////////////
void GPUEntityCache3D(bool use_cursor, bool use_radius = false) {

const int prey_count = 50;
const int prey_count = 2000;
const int hunter_count = 20;

const float map_radius = 50;
Expand All @@ -560,7 +560,7 @@ namespace Test {
Neshny::GPUEntity hunter_entities("Hunter", &GPUOther::p_Id, "Id");
#endif

prey_entities.Init(1000);
prey_entities.Init(prey_count + 1);
hunter_entities.Init(1000);

Neshny::Grid3DCache cache(prey_entities, "ThreeDim", use_radius ? std::optional<std::string_view>("Float") : std::nullopt);
Expand Down
35 changes: 27 additions & 8 deletions Src/WebGPU/PipelineWebGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,9 @@ void Grid3DCache::GenerateCache(iVec3 grid_size, Vec3 grid_min, Vec3 grid_max) {
m_GridMin = grid_min;
m_GridMax = grid_max;

m_RequiredItemCacheSize = std::max(m_RequiredItemCacheSize, m_Entity.GetMaxCount() * (m_RadiusName.has_value() ? 4 : 1)); // quadruple entity count to begin with for inefficient use of space
m_GridIndices.EnsureSizeBytes(grid_size.x * grid_size.y * grid_size.z * 3 * sizeof(int), true);
m_GridItems.EnsureSizeBytes(m_Entity.GetMaxCount() * sizeof(int), false);
m_GridItems.EnsureSizeBytes(m_RequiredItemCacheSize * sizeof(int), false);

std::string func_call = m_RadiusName.has_value() ? std::format("ItemRadiusMain(item_index, item.{}, item.{})", m_PosName, *m_RadiusName) : std::format("ItemMain(item_index, item.{})", m_PosName);
std::string main_func = std::format("fn {0}Main(item_index: i32, item: {0}) {{ {1}; }}", m_Entity.GetName(), func_call);
Expand All @@ -849,12 +850,31 @@ void Grid3DCache::GenerateCache(iVec3 grid_size, Vec3 grid_min, Vec3 grid_max) {
fVec4(grid_max.x, grid_max.y, grid_max.z, 0)
};

Neshny::EntityPipeline::IterateEntity(std::format("{}:INDEX", base_id), m_Entity, "GridCache3D", true)
.AddBuffer("b_Index", m_GridIndices, MemberSpec::T_INT, EntityPipeline::BufferAccess::READ_WRITE_ATOMIC)
.AddCode("#define PHASE_INDEX")
.AddCode(main_func)
.SetUniform(uniform)
.Run();
while (true) {
auto async = Neshny::EntityPipeline::IterateEntity(std::format("{}:INDEX", base_id), m_Entity, "GridCache3D", true)
.AddBuffer("b_Index", m_GridIndices, MemberSpec::T_INT, EntityPipeline::BufferAccess::READ_WRITE_ATOMIC)
.AddCode("#define PHASE_INDEX")
.AddCode(main_func)
.AddInputOutputVar("TotalSize", 0)
.SetUniform(uniform)
.Run([this] (const Neshny::EntityPipeline::OutputResults& results) {
int total_size;
results.GetValue("TotalSize", total_size);
if (m_RadiusName.has_value()) {
total_size *= 2; // twice the required size to allow flexibility in future frames
}
m_RequiredItemCacheSize = std::max(total_size, m_RequiredItemCacheSize);
});
if (m_InitialRun) {
m_InitialRun = false;
if (m_RadiusName.has_value()) {
async.Wait();
m_GridItems.EnsureSizeBytes(m_RequiredItemCacheSize * sizeof(int), false);
}
continue;
}
break;
}

for (int i = 0; i < 2; i++) {
Neshny::EntityPipeline::IterateEntity(std::format("{}:ALLOC", base_id), m_Entity, "GridCache3D", true)
Expand Down Expand Up @@ -887,7 +907,6 @@ void Grid3DCache::Bind(EntityPipeline& target_stage, bool initial_creation) {
target_stage.AddBuffer(std::format("b_{0}GridIndices", name), m_GridIndices, MemberSpec::Type::T_INT, EntityPipeline::BufferAccess::READ_ONLY);
target_stage.AddBuffer(std::format("b_{0}GridItems", name), m_GridItems, MemberSpec::Type::T_INT, EntityPipeline::BufferAccess::READ_ONLY);

target_stage.AddStructBuffer<Grid3DCacheUniform>(std::format("b_{0}GridUniform", name), std::format("{0}GridUniformStruct", name), m_Uniform, EntityPipeline::BufferAccess::READ_ONLY, false);
target_stage.AddStructBuffer<GridCacheUniform>(std::format("b_{0}GridUniform", name), std::format("{0}GridUniformStruct", name), m_Uniform, EntityPipeline::BufferAccess::READ_ONLY, false);

if (!initial_creation) {
Expand Down
2 changes: 2 additions & 0 deletions Src/WebGPU/PipelineWebGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ class Grid3DCache : public BaseCache {
iVec3 m_GridSize;
Vec3 m_GridMin;
Vec3 m_GridMax;
int m_RequiredItemCacheSize = 0;
bool m_InitialRun = true;
};

} // namespace Neshny
Expand Down

0 comments on commit f853595

Please sign in to comment.