Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project 6: Austin Eng #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ Vulkan Flocking: compute and shading in one pipeline!

**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 6**

* (TODO) YOUR NAME HERE
Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab)
* Austin Eng
* Tested on: Windows 10, i7-4770K @ 3.50GHz 16GB, GTX 780 3072MB (Personal Computer)

### (TODO: Your README)
## Vulkan Boids
![](vulkan_boids.gif)

Include screenshots, analysis, etc. (Remember, this is public, so don't put
anything here that you don't want to share with the world.)
This is a 2D port of my [CUDA flocking](https://github.com/austinEng/Project1-CUDA-Flocking) project over to Vulkan.

Vulkan requires explicit descriptors for pipelines and commands so that it knows exactly what types of commands to expect. Since we declare everything, it doesn't have to dynamically allocate memory for commands. Vulkan can optimize if we preallocate the buffers we will use.

Multiple descriptor sets in a layout can be useful for ping-ponging buffers as we are doing in this flocking example. However, more generally, it can be used when two independent sets of data are needed in a one computation. For example, we may have a descriptor set for particle data and another for mesh data when computing particle-mesh collisions.

Vulkan also allows multiple command queues. However, one must be careful because commands may be pulled from these queues in an arbitrary order leading to problems if the intent was to execute commands sequentially. Furthermore, if a buffer is mutated by a command, then it may unexpectedly mutate if future commands were relying on old data.

Sharing data between compute and rendering pipelines allows for greater performance because all data stays on the GPU. We don't have to incur costs of slow memory transfers involved when pulling computed data from the GPU and then copying to it for rendering again.

### Credits

Expand Down
36 changes: 35 additions & 1 deletion data/shaders/computeparticles/particle.comp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,48 @@ void main()

// Current SSBO index
uint index = gl_GlobalInvocationID.x;
// Don't try to write beyond particle count
// Don't try to write beyond particle count
if (index >= ubo.particleCount)
return;

// Read position and velocity
vec2 vPos = particlesA[index].pos.xy;
vec2 vVel = particlesA[index].vel.xy;

vec2 cMass = vec2(0.0, 0.0);
vec2 cVel = vec2(0.0, 0.0);
vec2 colVel = vec2(0.0, 0.0);
int cMassCount = 0;
int cVelCount = 0;

vec2 pos;
vec2 vel;
for (int i = 0; i < ubo.particleCount; ++i) {
if (i == index) continue;
pos = particlesA[i].pos.xy;
vel = particlesA[i].vel.xy;

if (distance(pos, vPos) < ubo.rule1Distance) {
cMass += pos;
cMassCount++;
}
if (distance(pos, vPos) < ubo.rule2Distance) {
colVel -= (pos - vPos);
}
if (distance(pos, vPos) < ubo.rule3Distance) {
cVel += vel;
cVelCount++;
}
}
if (cMassCount > 0) {
cMass = cMass / cMassCount - vPos;
}
if (cVelCount > 0) {
cVel = cVel / cVelCount;
}

vVel += cMass * ubo.rule1Scale + colVel * ubo.rule2Scale + cVel * ubo.rule3Scale;

// clamp velocity for a more pleasing simulation.
vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);

Expand Down
Binary file modified data/shaders/computeparticles/particle.comp.spv
Binary file not shown.
32 changes: 27 additions & 5 deletions vulkanBoids/vulkanBoids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
// using a Uniform Buffer. These parameters should yield a stable and pleasing simulation for an
// implementation based off the code here: http://studio.sketchpad.cc/sp/pad/view/ro.9cbgCRcgbPOI6/rev.23
#define RULE1DISTANCE 0.1f // cohesion
#define RULE2DISTANCE 0.05f // separation
#define RULE3DISTANCE 0.05f // alignment
#define RULE2DISTANCE 0.025f // separation
#define RULE3DISTANCE 0.025f // alignment
#define RULE1SCALE 0.02f
#define RULE2SCALE 0.05f
#define RULE3SCALE 0.01f
#define RULE3SCALE 0.005f

class VulkanExample : public VulkanExampleBase
{
Expand Down Expand Up @@ -158,6 +158,7 @@ class VulkanExample : public VulkanExampleBase
{
particle.pos = glm::vec2(rDistribution(rGenerator), rDistribution(rGenerator));
// TODO: add randomized velocities with a slight scale here, something like 0.1f.
particle.vel = 0.1f * glm::vec2(rDistribution(rGenerator), rDistribution(rGenerator));
}

VkDeviceSize storageBufferSize = particleBuffer.size() * sizeof(Particle);
Expand Down Expand Up @@ -244,7 +245,7 @@ class VulkanExample : public VulkanExampleBase
VERTEX_BUFFER_BIND_ID,
1,
VK_FORMAT_R32G32_SFLOAT,
offsetof(Particle, pos)); // TODO: change this so that we can color the particles based on velocity.
offsetof(Particle, vel)); // TODO: change this so that we can color the particles based on velocity.

// vertices.inputState encapsulates everything we need for these particular buffers to
// interface with the graphics pipeline.
Expand Down Expand Up @@ -540,13 +541,33 @@ class VulkanExample : public VulkanExampleBase
compute.descriptorSets[0],
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2,
&compute.uniformBuffer.descriptor)
&compute.uniformBuffer.descriptor),

// TODO: write the second descriptorSet, using the top for reference.
// We want the descriptorSets to be used for flip-flopping:
// on one frame, we use one descriptorSet with the compute pass,
// on the next frame, we use the other.
// What has to be different about how the second descriptorSet is written here?
// Binding 0 : Particle position storage buffer
vkTools::initializers::writeDescriptorSet(
compute.descriptorSets[1], // LOOK: which descriptor set to write to?
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
0, // LOOK: which binding in the descriptor set Layout?
&compute.storageBufferB.descriptor), // LOOK: which SSBO?

// Binding 1 : Particle position storage buffer
vkTools::initializers::writeDescriptorSet(
compute.descriptorSets[1],
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
1,
&compute.storageBufferA.descriptor),

// Binding 2 : Uniform buffer
vkTools::initializers::writeDescriptorSet(
compute.descriptorSets[1],
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2,
&compute.uniformBuffer.descriptor)
};

vkUpdateDescriptorSets(device, static_cast<uint32_t>(computeWriteDescriptorSets.size()), computeWriteDescriptorSets.data(), 0, NULL);
Expand Down Expand Up @@ -590,6 +611,7 @@ class VulkanExample : public VulkanExampleBase
// We also want to flip what SSBO we draw with in the next
// pass through the graphics pipeline.
// Feel free to use std::swap here. You should need it twice.
std::swap(compute.descriptorSets[0], compute.descriptorSets[1]);
}

// Record command buffers for drawing using the graphics pipeline
Expand Down
Binary file added vulkan_boids.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.