Skip to content

Commit

Permalink
Merge pull request #240 from naivisoftware/fix_computeflocking
Browse files Browse the repository at this point in the history
Fix storage buffer binding update of flock system render material
  • Loading branch information
cklosters committed Oct 4, 2022
2 parents f258273 + c8ce6c8 commit 647322d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions demos/computeflocking/module/src/flockingsystemcomponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace nap

// Collect compute instances under this entity
getEntityInstance()->getComponentsOfType<ComputeComponentInstance>(mComputeInstances);
mCurrentComputeInstance = mComputeInstances[mComputeInstanceIndex];
mCurrentComputeInstance = mComputeInstances.front();

// Initialize base class
if (!RenderableMeshComponentInstance::init(errorState))
Expand All @@ -148,6 +148,7 @@ namespace nap
for (auto& comp : mComputeInstances)
comp->setInvocations(mNumBoids);

mComputeInstanceIndex = -1;
return true;
}

Expand Down Expand Up @@ -200,11 +201,12 @@ namespace nap
}

// Update vertex shader buffer bindings
auto* storage_binding = rtti_cast<BufferBindingStructInstance>(mCurrentComputeInstance->getMaterialInstance().findBinding("BoidBuffer_Out"));
if (storage_binding != nullptr && storage_binding != nullptr)
auto* compute_storage_binding = rtti_cast<BufferBindingStructInstance>(mCurrentComputeInstance->getMaterialInstance().findBinding("BoidBuffer_Out"));
if (compute_storage_binding != nullptr)
{
auto& storage_buffer = storage_binding->getBuffer();
storage_binding->setBuffer(storage_buffer);
auto* render_storage_binding = rtti_cast<BufferBindingStructInstance>(getMaterialInstance().findBinding("VERTSSBO"));
if (render_storage_binding != nullptr)
render_storage_binding->setBuffer(compute_storage_binding->getBuffer());
}

// Update fragment shader uniforms
Expand All @@ -231,16 +233,17 @@ namespace nap

void FlockingSystemComponentInstance::compute()
{
// Update current compute instance and index
assert(mComputeInstanceIndex >= -1);
mComputeInstanceIndex = (mComputeInstanceIndex + 1) % mComputeInstances.size();
mCurrentComputeInstance = mComputeInstances[mComputeInstanceIndex];

// Update the compute material uniforms of the current compute instance
updateComputeMaterial(mCurrentComputeInstance);

// Compute the current compute instance
// This updates the boid storage buffers to use for rendering
mRenderService->computeObjects({ mCurrentComputeInstance });

// Update current compute instance and index
mComputeInstanceIndex = (mComputeInstanceIndex + 1) % mComputeInstances.size();
mCurrentComputeInstance = mComputeInstances[mComputeInstanceIndex];
}


Expand Down
2 changes: 1 addition & 1 deletion demos/computeflocking/module/src/flockingsystemcomponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ namespace nap

std::vector<ComputeComponentInstance*> mComputeInstances; //< Compute instances found in the entity
ComputeComponentInstance* mCurrentComputeInstance = nullptr; //< The current compute instance
uint mComputeInstanceIndex = 0; //< Current compute instance index
int mComputeInstanceIndex = 0; //< Current compute instance index

ComponentInstancePtr<PerspCameraComponent> mPerspCameraComponent = { this, &FlockingSystemComponent::mPerspCameraComponent };
ComponentInstancePtr<TransformComponent> mTargetTransformComponent = { this, &FlockingSystemComponent::mTargetTransformComponent };
Expand Down

0 comments on commit 647322d

Please sign in to comment.