diff --git a/README.md b/README.md index bec6ca4..8c4d4a7 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,45 @@ -Vulkan Flocking: compute and shading in one pipeline! -====================== +# University of Pennsylvania, CIS 565: GPU Programming and Architecture. +Project 6: Vulkan Flocking: compute and shading in one pipeline! +=============== -**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 6** +## User resources +- **Name:** David Grosman. +- **Tested on:** Microsoft Windows 7 Professional, i7-5600U @ 2.6GHz, 256GB, GeForce 840M (Personal laptop). -* (TODO) YOUR NAME HERE - Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab) +### Demo Video/GIF - ### (TODO: Your README) +![](img/Preview.gif) +![](img/PreviewTwo.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.) +### Project Description +In this project, we were introduced to the workings of a basic Vulkan compute-and-shading application. I implemented a 2D version of the Boids algorithm that I implemented back in Project 1. Covered concepts included setting up basic compute and graphics pipelines, setting up vertex buffers that can be shared between both pipelines, creating commands for each pipeline, and binding information to each pipeline. + +### Project Analysis + +1. __***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.***__ + +Since Vulkan is a low-level Graphics API, it expects its users to specify low-level information such as pipeline information (Buffer, image and shader resources binding) and thus explicit descriptors should also be specified to bound them to command buffer for use by the next draw. + +2. __***Describe a situation besides flip-flop buffers in which you may need multiple descriptor sets to fit one descriptor layout.***__ + +Since a desciptor set correspond to a layout binding in the descriptor set layout, which in turn corresponds with something like `layout(std140, binding = 0)` in `particle.comp`. It is easy to see that a rendering a scene with multiple models (with different vertex/index buffer, normal buffer, etc.) would be another situation in which you would need multiple descriptor sets to fit one descriptor layout. + +3. __***What are some problems to keep in mind when using multiple Vulkan queues?***__ + +The most obvious advantage in using multiple queues is that Commands submitted to different queues may execute in parallel or even out of order with respect to one another. However, this is only advantageous if queues do not need a lot of synchronization between themselves. Since it is the user's responsability to ensure correct work coordination through given primitives such as semaphores, barriers and fences, it makes it harder to use multiple Vulkan queues (in exchange for better performance). + * **Take into consideration that different queues may be backed by different hardware** + + Since the queues are backed by different hardware, it means that optimizations for one machine might decrease the performance on another machine (ie.: if a queue is backed by a special hardware which isn't present on all machines running the application these machines might need to back it on the software side which will probably be slower.) + + * **Take into consideration that the same buffer may be used across multiple queues** + + Race-conditions might occur if writing some output in a buffer used accross multiple queues. + * **What is one advantage of using compute commands that can share data with a rendering pipeline?** + + One of the advantage is to avoid duplicating large data buffers necessary by the compute and graphics queue. + +### Blooper +![](img/Centrifuge.gif) ### Credits diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index a30387e..3639083 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -50,7 +50,7 @@ class VulkanExampleBase bool enableVSync = false; // Device features enabled by the example // If not set, no additional features are enabled (may result in validation layer errors) - VkPhysicalDeviceFeatures enabledFeatures = {}; + VkPhysicalDeviceFeatures enabledFeatures /*= {}*/; // fps timer (one second interval) float fpsTimer = 0.0f; // Create application wide Vulkan instance diff --git a/data/shaders/computeparticles/particle.comp b/data/shaders/computeparticles/particle.comp index b7dc2f7..e9cbd96 100644 --- a/data/shaders/computeparticles/particle.comp +++ b/data/shaders/computeparticles/particle.comp @@ -41,37 +41,78 @@ layout (binding = 2) uniform UBO int particleCount; } ubo; +vec2 computeVelocityChange(int N, uint iSelf) +{ + vec2 sumCOM = vec2(0.0, 0.0); + vec2 sumDelta = vec2(0.0, 0.0); + vec2 sumVel = vec2(0.0, 0.0); + int numBoidsRule[2] = { 0, 0 }; + for (int boidIdx = 0; boidIdx < N; ++boidIdx) + { + if (boidIdx == iSelf) + continue; + + vec2 vOffset = particlesA[iSelf].pos - particlesA[boidIdx].pos; + const float distance = sqrt( dot(vOffset, vOffset) ); + + // Rule 1: boids fly towards their local perceived center of mass, which excludes themselves + if (distance < ubo.rule1Distance) + { + sumCOM += particlesA[boidIdx].pos; + ++numBoidsRule[0]; + } + + // Rule 2: boids try to stay a distance d away from each other + if (distance < ubo.rule2Distance) + { + sumDelta += vOffset; + } + + // Rule 3: boids try to match the speed of surrounding boids + if (distance < ubo.rule3Distance) + { + sumVel += particlesA[boidIdx].vel; + ++numBoidsRule[1]; + } + } + + vec2 totalVel = vec2(0.0f, 0.0f); + if (numBoidsRule[0] > 0) { + totalVel += (sumCOM / float(numBoidsRule[0]) - particlesA[iSelf].pos) * ubo.rule1Scale; + } + totalVel += sumDelta * ubo.rule2Scale; + if (numBoidsRule[1] > 0) { + totalVel += (sumVel) * ubo.rule3Scale; + } + + return totalVel; +} + + 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. - // Current SSBO index - uint index = gl_GlobalInvocationID.x; + const uint iSelf = gl_GlobalInvocationID.x; // Don't try to write beyond particle count - if (index >= ubo.particleCount) + if (iSelf >= ubo.particleCount) { return; + } - // Read position and velocity - vec2 vPos = particlesA[index].pos.xy; - vec2 vVel = particlesA[index].vel.xy; + vec2 vNewVel = computeVelocityChange(ubo.particleCount, iSelf); - // clamp velocity for a more pleasing simulation. - vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1); + // clamp velocity for a more pleasing simulation. + vNewVel = normalize(vNewVel) * clamp(length(vNewVel), 0.0, 0.1); - // kinematic update - vPos += vVel * ubo.deltaT; + // kinematic update + vec2 vPos = particlesA[iSelf].pos.xy + vNewVel * 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; - - particlesB[index].pos.xy = vPos; + 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; // Write back - particlesB[index].vel.xy = vVel; -} + particlesB[iSelf].pos.xy = vPos; + particlesB[iSelf].vel.xy = vNewVel; +} \ No newline at end of file diff --git a/data/shaders/computeparticles/particle.comp.spv b/data/shaders/computeparticles/particle.comp.spv index 059ab59..a0b857e 100644 Binary files a/data/shaders/computeparticles/particle.comp.spv and b/data/shaders/computeparticles/particle.comp.spv differ diff --git a/img/Centrifuge.gif b/img/Centrifuge.gif new file mode 100644 index 0000000..c809b14 Binary files /dev/null and b/img/Centrifuge.gif differ diff --git a/img/Centrifuge2.gif b/img/Centrifuge2.gif new file mode 100644 index 0000000..bd518e5 Binary files /dev/null and b/img/Centrifuge2.gif differ diff --git a/img/Preview.gif b/img/Preview.gif new file mode 100644 index 0000000..d8f9f54 Binary files /dev/null and b/img/Preview.gif differ diff --git a/img/PreviewTwo.gif b/img/PreviewTwo.gif new file mode 100644 index 0000000..ee16d2d Binary files /dev/null and b/img/PreviewTwo.gif differ diff --git a/vulkanBoids/vulkanBoids.cpp b/vulkanBoids/vulkanBoids.cpp index 9b2f122..9379d99 100644 --- a/vulkanBoids/vulkanBoids.cpp +++ b/vulkanBoids/vulkanBoids.cpp @@ -36,9 +36,9 @@ #define RULE1DISTANCE 0.1f // cohesion #define RULE2DISTANCE 0.05f // separation #define RULE3DISTANCE 0.05f // alignment -#define RULE1SCALE 0.02f -#define RULE2SCALE 0.05f -#define RULE3SCALE 0.01f +#define RULE1SCALE 0.01f +#define RULE2SCALE 0.02f +#define RULE3SCALE 0.3f class VulkanExample : public VulkanExampleBase { @@ -157,7 +157,7 @@ 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 = 0.1f * glm::vec2(rDistribution(rGenerator), rDistribution(rGenerator)); } VkDeviceSize storageBufferSize = particleBuffer.size() * sizeof(Particle); @@ -244,7 +244,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)); // vertices.inputState encapsulates everything we need for these particular buffers to // interface with the graphics pipeline. @@ -517,9 +517,11 @@ class VulkanExample : public VulkanExampleBase { // LOOK // WriteDescriptorSet writes each of these descriptors into the specified descriptorSet. - // THese first few are written into compute.descriptorSet[0]. // Each of these corresponds to a layout binding in the descriptor set layout, // which in turn corresponds with something like `layout(std140, binding = 0)` in `particle.comp`. + // 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. // Binding 0 : Particle position storage buffer vkTools::initializers::writeDescriptorSet( @@ -540,13 +542,28 @@ 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); @@ -589,7 +606,8 @@ class VulkanExample : public VulkanExampleBase // in one configuration. // 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