diff --git a/README.md b/README.md index bec6ca4..0b1c607 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,29 @@ 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) +* Ethan Brooks + Tested on: Windows 7, Intel(R) Xeon(R), GeForce GTX 1070 8GB (SIG Lab) - ### (TODO: Your README) +![](https://github.com/lobachevzky/Project6-Vulkan-Flocking/blob/master/boids.gif) + +* Why do you think Vulkan expects explicit descriptors for things like generating pipelines and commands? HINT: this may relate to something in the comments about some components using pre-allocated GPU memory. + + This is because descriptors can be reused. This way the GPU does not need to reallocate memory for the objects every time they are reused. + +* Describe a situation besides flip-flop buffers in which you may need multiple descriptor sets to fit one descriptor layout. + + If you were swapping out textures in the same pipeline, you might assign one texture to one descriptor set and another texture to a different descriptor set. + +* What are some problems to keep in mind when using multiple Vulkan queues? + * take into consideration that different queues may be backed by different hardware + * take into consideration that the same buffer may be used across multiple queues + + Queues often need to be synchronized -- for example. One a graphics queue might start rendering before another compute queue is done calculating the values to be rendered. It is also important to take advantage of the fact that compute pipelines can run in parallel by spreading work among multiple compute pipelines + +* What is one advantage of using compute commands that can share data with a rendering pipeline? + + This eliminates the need to copy data back and forth between pipelines. - Include screenshots, analysis, etc. (Remember, this is public, so don't put - anything here that you don't want to share with the world.) ### Credits diff --git a/boids.gif b/boids.gif new file mode 100644 index 0000000..75e8e10 Binary files /dev/null and b/boids.gif differ diff --git a/data/shaders/computeparticles/particle.comp b/data/shaders/computeparticles/particle.comp index b7dc2f7..921cba0 100644 --- a/data/shaders/computeparticles/particle.comp +++ b/data/shaders/computeparticles/particle.comp @@ -5,8 +5,8 @@ struct Particle { - vec2 pos; - vec2 vel; + vec2 pos; + vec2 vel; }; // LOOK: These bindings correspond to the DescriptorSetLayouts and @@ -31,47 +31,88 @@ layout (local_size_x = 16, local_size_y = 16) in; // frame rate. layout (binding = 2) uniform UBO { - float deltaT; - float rule1Distance; - float rule2Distance; - float rule3Distance; - float rule1Scale; - float rule2Scale; - float rule3Scale; - int particleCount; + float deltaT; + float rule1Distance; + float rule2Distance; + float rule3Distance; + float rule1Scale; + float rule2Scale; + float rule3Scale; + int particleCount; } ubo; void main() { - // LOOK: This is very similar to a CUDA kernel. - // Right now, the compute shader only advects the particles with their - // velocity and handles wrap-around. - // TODO: implement flocking behavior. + // LOOK: This is very similar to a CUDA kernel. + // Right now, the compute shader only advects the particles with their + // velocity and handles wrap-around. + // TODO: implement flocking behavior. // Current SSBO index uint index = gl_GlobalInvocationID.x; - // Don't try to write beyond particle count - if (index >= ubo.particleCount) - return; + // 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 thisPos = particlesA[index].pos.xy; + vec2 thisVel = particlesA[index].vel.xy; - // clamp velocity for a more pleasing simulation. - vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1); + vec2 center = vec2(0); + vec2 separate = vec2(0); + vec2 cohesion = vec2(0); + uint neighborCount = 0; - // kinematic update - vPos += vVel * ubo.deltaT; + for (int i = 0; i < ubo.particleCount; i++) { + vec2 thatPos = particlesA[i].pos.xy; + float dist = distance(thisPos, thatPos); + + // Rule 1: Cohesion: boids fly towards the center of mass of neighboring + // boids + if (dist < ubo.rule1Distance) { + center += thatPos; + neighborCount++; + } + + // Rule 2: Separation: boids try to keep a small distance away from + // each other + if (dist < ubo.rule2Distance) { + separate -= thatPos - thisPos; + } + + // Rule 3: Alignment: boids try to match the velocities of neighboring + // boids + if (dist < ubo.rule3Distance) { + cohesion += particlesA[i].vel.xy; + } + } + + vec2 toCenter = vec2(0); + if (neighborCount > 0) { + center /= neighborCount; + toCenter = (center - thisPos); + // cohesion /= neighborCount; + cohesion -= thisVel; + } + + thisVel += toCenter * ubo.rule1Scale + + separate * ubo.rule2Scale + + cohesion * ubo.rule3Scale; + + + // clamp velocity for a more pleasing simulation. + thisVel = normalize(thisVel) * clamp(length(thisVel), 0.0, 0.1); + + // kinematic update + thisPos += thisVel * ubo.deltaT; // Wrap around boundary - if (vPos.x < -1.0) vPos.x = 1.0; - if (vPos.x > 1.0) vPos.x = -1.0; - if (vPos.y < -1.0) vPos.y = 1.0; - if (vPos.y > 1.0) vPos.y = -1.0; + if (thisPos.x < -1.0) thisPos.x = 1.0; + if (thisPos.x > 1.0) thisPos.x = -1.0; + if (thisPos.y < -1.0) thisPos.y = 1.0; + if (thisPos.y > 1.0) thisPos.y = -1.0; - particlesB[index].pos.xy = vPos; + particlesB[index].pos.xy = thisPos; // Write back - particlesB[index].vel.xy = vVel; + particlesB[index].vel.xy = thisVel; } diff --git a/data/shaders/computeparticles/particle.comp.spv b/data/shaders/computeparticles/particle.comp.spv index 059ab59..d73bcb6 100644 Binary files a/data/shaders/computeparticles/particle.comp.spv and b/data/shaders/computeparticles/particle.comp.spv differ diff --git a/vulkanBoids/vulkanBoids.cpp b/vulkanBoids/vulkanBoids.cpp index 9b2f122..dddb999 100644 --- a/vulkanBoids/vulkanBoids.cpp +++ b/vulkanBoids/vulkanBoids.cpp @@ -157,7 +157,9 @@ class VulkanExample : public VulkanExampleBase for (auto& particle : particleBuffer) { particle.pos = glm::vec2(rDistribution(rGenerator), rDistribution(rGenerator)); + // TODO: add randomized velocities with a slight scale here, something like 0.1f. + particle.vel = glm::vec2(rDistribution(rGenerator), rDistribution(rGenerator)) * 0.1f; } VkDeviceSize storageBufferSize = particleBuffer.size() * sizeof(Particle); @@ -244,7 +246,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. @@ -540,13 +542,34 @@ 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(computeWriteDescriptorSets.size()), computeWriteDescriptorSets.data(), 0, NULL); @@ -590,6 +613,8 @@ 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]); + std::swap(compute.storageBufferA, compute.storageBufferB); } // Record command buffers for drawing using the graphics pipeline