Additional refactoring, comments

This commit is contained in:
saschawillems 2016-08-17 20:32:05 +02:00
parent 04dffbadf3
commit d7725c9b79

View file

@ -70,19 +70,19 @@ public:
VkDescriptorSet descriptorSet; // Compute shader bindings VkDescriptorSet descriptorSet; // Compute shader bindings
VkPipelineLayout pipelineLayout; // Layout of the compute pipeline VkPipelineLayout pipelineLayout; // Layout of the compute pipeline
VkPipeline pipeline; // Compute pipeline for updating particle positions VkPipeline pipeline; // Compute pipeline for updating particle positions
struct computeUBO { // Compute shader uniform block object
float deltaT; // Frame delta time
float destX; // x position of the attractor
float destY; // y position of the attractor
int32_t particleCount = PARTICLE_COUNT;
} ubo;
} compute; } compute;
struct ComputeUBO { // SSBO particle declaration
float deltaT;
float destX;
float destY;
int32_t particleCount = PARTICLE_COUNT;
} computeUbo;
struct Particle { struct Particle {
glm::vec2 pos; glm::vec2 pos; // Particle position
glm::vec2 vel; glm::vec2 vel; // Particle velocity
glm::vec4 gradientPos; glm::vec4 gradientPos; // Texture coordiantes for the gradient ramp map
}; };
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
@ -93,9 +93,6 @@ public:
~VulkanExample() ~VulkanExample()
{ {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
// Graphics // Graphics
vkDestroyPipeline(device, graphics.pipeline, nullptr); vkDestroyPipeline(device, graphics.pipeline, nullptr);
vkDestroyPipelineLayout(device, graphics.pipelineLayout, nullptr); vkDestroyPipelineLayout(device, graphics.pipelineLayout, nullptr);
@ -183,13 +180,14 @@ public:
// Compute particle movement // Compute particle movement
// Add memory barrier to ensure that the (rendering) vertex shader operations have finished // Add memory barrier to ensure that the (graphics) vertex shader has fetched attributes before compute starts to write to the buffer
// Required as the compute shader will overwrite the vertex buffer data
VkBufferMemoryBarrier bufferBarrier = vkTools::initializers::bufferMemoryBarrier(); VkBufferMemoryBarrier bufferBarrier = vkTools::initializers::bufferMemoryBarrier();
bufferBarrier.buffer = compute.storageBuffer.buffer; bufferBarrier.buffer = compute.storageBuffer.buffer;
bufferBarrier.size = compute.storageBuffer.descriptor.range; bufferBarrier.size = compute.storageBuffer.descriptor.range;
bufferBarrier.srcAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; // Vertex shader invocations have finished reading from the buffer bufferBarrier.srcAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; // Vertex shader invocations have finished reading from the buffer
bufferBarrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT; // Compute shader has finished buffer writes bufferBarrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT; // Compute shader wants to write to the buffer
// Compute and graphics queue may have different queue families (see VulkanDevice::createLogicalDevice)
// For the barrier to work across different queues, we need to set their family indices
bufferBarrier.srcQueueFamilyIndex = vulkanDevice->queueFamilyIndices.graphics; // Required as compute and graphics queue may have different families bufferBarrier.srcQueueFamilyIndex = vulkanDevice->queueFamilyIndices.graphics; // Required as compute and graphics queue may have different families
bufferBarrier.dstQueueFamilyIndex = vulkanDevice->queueFamilyIndices.compute; // Required as compute and graphics queue may have different families bufferBarrier.dstQueueFamilyIndex = vulkanDevice->queueFamilyIndices.compute; // Required as compute and graphics queue may have different families
@ -210,12 +208,12 @@ public:
// Add memory barrier to ensure that compute shader has finished writing to the buffer // Add memory barrier to ensure that compute shader has finished writing to the buffer
// Without this the (rendering) vertex shader may display incomplete results (partial data from last frame) // Without this the (rendering) vertex shader may display incomplete results (partial data from last frame)
// Compute shader has finished writes to the buffer bufferBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; // Compute shader has finished writes to the buffer
bufferBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; bufferBarrier.dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; // Vertex shader invocations want to read from the buffer
// Vertex shader access (attribute binding)
bufferBarrier.dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
bufferBarrier.buffer = compute.storageBuffer.buffer; bufferBarrier.buffer = compute.storageBuffer.buffer;
bufferBarrier.size = compute.storageBuffer.descriptor.range; bufferBarrier.size = compute.storageBuffer.descriptor.range;
// Compute and graphics queue may have different queue families (see VulkanDevice::createLogicalDevice)
// For the barrier to work across different queues, we need to set their family indices
bufferBarrier.srcQueueFamilyIndex = vulkanDevice->queueFamilyIndices.compute; // Required as compute and graphics queue may have different families bufferBarrier.srcQueueFamilyIndex = vulkanDevice->queueFamilyIndices.compute; // Required as compute and graphics queue may have different families
bufferBarrier.dstQueueFamilyIndex = vulkanDevice->queueFamilyIndices.graphics; // Required as compute and graphics queue may have different families bufferBarrier.dstQueueFamilyIndex = vulkanDevice->queueFamilyIndices.graphics; // Required as compute and graphics queue may have different families
@ -231,11 +229,9 @@ public:
vkEndCommandBuffer(compute.commandBuffer); vkEndCommandBuffer(compute.commandBuffer);
} }
// Setup and fill the compute shader storage buffers for // Setup and fill the compute shader storage buffers containing the particles
// vertex positions and velocities
void prepareStorageBuffers() void prepareStorageBuffers()
{ {
std::mt19937 rGenerator; std::mt19937 rGenerator;
std::uniform_real_distribution<float> rDistribution(-1.0f, 1.0f); std::uniform_real_distribution<float> rDistribution(-1.0f, 1.0f);
@ -248,7 +244,7 @@ public:
particle.gradientPos.x = particle.pos.x / 2.0f; particle.gradientPos.x = particle.pos.x / 2.0f;
} }
uint32_t storageBufferSize = particleBuffer.size() * sizeof(Particle); VkDeviceSize storageBufferSize = particleBuffer.size() * sizeof(Particle);
// Staging // Staging
// SSBO won't be changed on the host after upload so copy to device local memory // SSBO won't be changed on the host after upload so copy to device local memory
@ -271,16 +267,9 @@ public:
// Copy to staging buffer // Copy to staging buffer
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
VkBufferCopy copyRegion = {}; VkBufferCopy copyRegion = {};
copyRegion.size = storageBufferSize; copyRegion.size = storageBufferSize;
vkCmdCopyBuffer( vkCmdCopyBuffer(copyCmd, stagingBuffer.buffer, compute.storageBuffer.buffer, 1, &copyRegion);
copyCmd,
stagingBuffer.buffer,
compute.storageBuffer.buffer,
1,
&copyRegion);
VulkanExampleBase::flushCommandBuffer(copyCmd, queue, true); VulkanExampleBase::flushCommandBuffer(copyCmd, queue, true);
stagingBuffer.destroy(); stagingBuffer.destroy();
@ -313,9 +302,9 @@ public:
// Assign to vertex buffer // Assign to vertex buffer
vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo(); vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();
vertices.inputState.vertexBindingDescriptionCount = vertices.bindingDescriptions.size(); vertices.inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertices.bindingDescriptions.size());
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data(); vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
vertices.inputState.vertexAttributeDescriptionCount = vertices.attributeDescriptions.size(); vertices.inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertices.attributeDescriptions.size());
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data(); vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
} }
@ -330,7 +319,7 @@ public:
VkDescriptorPoolCreateInfo descriptorPoolInfo = VkDescriptorPoolCreateInfo descriptorPoolInfo =
vkTools::initializers::descriptorPoolCreateInfo( vkTools::initializers::descriptorPoolCreateInfo(
poolSizes.size(), static_cast<uint32_t>(poolSizes.size()),
poolSizes.data(), poolSizes.data(),
2); 2);
@ -354,7 +343,7 @@ public:
VkDescriptorSetLayoutCreateInfo descriptorLayout = VkDescriptorSetLayoutCreateInfo descriptorLayout =
vkTools::initializers::descriptorSetLayoutCreateInfo( vkTools::initializers::descriptorSetLayoutCreateInfo(
setLayoutBindings.data(), setLayoutBindings.data(),
setLayoutBindings.size()); static_cast<uint32_t>(setLayoutBindings.size()));
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &graphics.descriptorSetLayout)); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &graphics.descriptorSetLayout));
@ -376,32 +365,21 @@ public:
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &graphics.descriptorSet)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &graphics.descriptorSet));
// Image descriptor for the color map texture
std::vector<VkDescriptorImageInfo> texDescriptors;
texDescriptors.push_back(vkTools::initializers::descriptorImageInfo(
textures.particle.sampler,
textures.particle.view,
VK_IMAGE_LAYOUT_GENERAL));
texDescriptors.push_back(vkTools::initializers::descriptorImageInfo(
textures.gradient.sampler,
textures.gradient.view,
VK_IMAGE_LAYOUT_GENERAL));
std::vector<VkWriteDescriptorSet> writeDescriptorSets; std::vector<VkWriteDescriptorSet> writeDescriptorSets;
// Binding 0 : Particle color map // Binding 0 : Particle color map
writeDescriptorSets.push_back(vkTools::initializers::writeDescriptorSet( writeDescriptorSets.push_back(vkTools::initializers::writeDescriptorSet(
graphics.descriptorSet, graphics.descriptorSet,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
0, 0,
&texDescriptors[0])); &textures.particle.descriptor));
// Binding 1 : Particle gradient ramp // Binding 1 : Particle gradient ramp
writeDescriptorSets.push_back(vkTools::initializers::writeDescriptorSet( writeDescriptorSets.push_back(vkTools::initializers::writeDescriptorSet(
graphics.descriptorSet, graphics.descriptorSet,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1, 1,
&texDescriptors[1])); &textures.gradient.descriptor));
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL); vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
} }
void preparePipelines() void preparePipelines()
@ -450,7 +428,7 @@ public:
VkPipelineDynamicStateCreateInfo dynamicState = VkPipelineDynamicStateCreateInfo dynamicState =
vkTools::initializers::pipelineDynamicStateCreateInfo( vkTools::initializers::pipelineDynamicStateCreateInfo(
dynamicStateEnables.data(), dynamicStateEnables.data(),
dynamicStateEnables.size(), static_cast<uint32_t>(dynamicStateEnables.size()),
0); 0);
// Rendering pipeline // Rendering pipeline
@ -474,7 +452,7 @@ public:
pipelineCreateInfo.pViewportState = &viewportState; pipelineCreateInfo.pViewportState = &viewportState;
pipelineCreateInfo.pDepthStencilState = &depthStencilState; pipelineCreateInfo.pDepthStencilState = &depthStencilState;
pipelineCreateInfo.pDynamicState = &dynamicState; pipelineCreateInfo.pDynamicState = &dynamicState;
pipelineCreateInfo.stageCount = shaderStages.size(); pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
pipelineCreateInfo.pStages = shaderStages.data(); pipelineCreateInfo.pStages = shaderStages.data();
pipelineCreateInfo.renderPass = renderPass; pipelineCreateInfo.renderPass = renderPass;
@ -494,7 +472,9 @@ public:
void prepareCompute() void prepareCompute()
{ {
// Create a compute capable device queue // Create a compute capable device queue
// todo: comment (queue families, etc.) // The VulkanDevice::createLogicalDevice functions finds a compute capable queue and prefers queue families that only support compute
// Depending on the implementation this may result in different queue family indices for graphics and computes,
// requiring proper synchronization (see the memory barriers in buildComputeCommandBuffer)
VkDeviceQueueCreateInfo queueCreateInfo = {}; VkDeviceQueueCreateInfo queueCreateInfo = {};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.pNext = NULL; queueCreateInfo.pNext = NULL;
@ -521,7 +501,7 @@ public:
VkDescriptorSetLayoutCreateInfo descriptorLayout = VkDescriptorSetLayoutCreateInfo descriptorLayout =
vkTools::initializers::descriptorSetLayoutCreateInfo( vkTools::initializers::descriptorSetLayoutCreateInfo(
setLayoutBindings.data(), setLayoutBindings.data(),
setLayoutBindings.size()); static_cast<uint32_t>(setLayoutBindings.size()));
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &compute.descriptorSetLayout)); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &compute.descriptorSetLayout));
@ -556,13 +536,10 @@ public:
&compute.uniformBuffer.descriptor) &compute.uniformBuffer.descriptor)
}; };
vkUpdateDescriptorSets(device, computeWriteDescriptorSets.size(), computeWriteDescriptorSets.data(), 0, NULL); vkUpdateDescriptorSets(device, static_cast<uint32_t>(computeWriteDescriptorSets.size()), computeWriteDescriptorSets.data(), 0, NULL);
// Create pipeline // Create pipeline
VkComputePipelineCreateInfo computePipelineCreateInfo = VkComputePipelineCreateInfo computePipelineCreateInfo = vkTools::initializers::computePipelineCreateInfo(compute.pipelineLayout, 0);
vkTools::initializers::computePipelineCreateInfo(
compute.pipelineLayout,
0);
computePipelineCreateInfo.stage = loadShader(getAssetPath() + "shaders/computeparticles/particle.comp.spv", VK_SHADER_STAGE_COMPUTE_BIT); computePipelineCreateInfo.stage = loadShader(getAssetPath() + "shaders/computeparticles/particle.comp.spv", VK_SHADER_STAGE_COMPUTE_BIT);
VK_CHECK_RESULT(vkCreateComputePipelines(device, pipelineCache, 1, &computePipelineCreateInfo, nullptr, &compute.pipeline)); VK_CHECK_RESULT(vkCreateComputePipelines(device, pipelineCache, 1, &computePipelineCreateInfo, nullptr, &compute.pipeline));
@ -573,8 +550,7 @@ public:
cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
VK_CHECK_RESULT(vkCreateCommandPool(device, &cmdPoolInfo, nullptr, &compute.commandPool)); VK_CHECK_RESULT(vkCreateCommandPool(device, &cmdPoolInfo, nullptr, &compute.commandPool));
// Create command buffer for compute operations // Create a command buffer for compute operations
// tood: differring indices? separate cmd pool?
VkCommandBufferAllocateInfo cmdBufAllocateInfo = VkCommandBufferAllocateInfo cmdBufAllocateInfo =
vkTools::initializers::commandBufferAllocateInfo( vkTools::initializers::commandBufferAllocateInfo(
compute.commandPool, compute.commandPool,
@ -587,7 +563,7 @@ public:
VkFenceCreateInfo fenceCreateInfo = vkTools::initializers::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT); VkFenceCreateInfo fenceCreateInfo = vkTools::initializers::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT);
VK_CHECK_RESULT(vkCreateFence(device, &fenceCreateInfo, nullptr, &compute.fence)); VK_CHECK_RESULT(vkCreateFence(device, &fenceCreateInfo, nullptr, &compute.fence));
//todo: comment // Build a single command buffer containing the compute dispatch commands
buildComputeCommandBuffer(); buildComputeCommandBuffer();
} }
@ -599,7 +575,7 @@ public:
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&compute.uniformBuffer, &compute.uniformBuffer,
sizeof(computeUbo)); sizeof(compute.ubo));
// Map for host access // Map for host access
VK_CHECK_RESULT(compute.uniformBuffer.map()); VK_CHECK_RESULT(compute.uniformBuffer.map());
@ -609,25 +585,26 @@ public:
void updateUniformBuffers() void updateUniformBuffers()
{ {
computeUbo.deltaT = frameTimer * 2.5f; compute.ubo.deltaT = frameTimer * 2.5f;
if (animate) if (animate)
{ {
computeUbo.destX = sin(glm::radians(timer*360.0)) * 0.75f; compute.ubo.destX = sin(glm::radians(timer * 360.0f)) * 0.75f;
computeUbo.destY = 0.f; compute.ubo.destY = 0.0f;
} }
else else
{ {
float normalizedMx = (mousePos.x - static_cast<float>(width / 2)) / static_cast<float>(width / 2); float normalizedMx = (mousePos.x - static_cast<float>(width / 2)) / static_cast<float>(width / 2);
float normalizedMy = (mousePos.y - static_cast<float>(height / 2)) / static_cast<float>(height / 2); float normalizedMy = (mousePos.y - static_cast<float>(height / 2)) / static_cast<float>(height / 2);
computeUbo.destX = normalizedMx; compute.ubo.destX = normalizedMx;
computeUbo.destY = normalizedMy; compute.ubo.destY = normalizedMy;
} }
memcpy(compute.uniformBuffer.mapped, &computeUbo, sizeof(computeUbo)); memcpy(compute.uniformBuffer.mapped, &compute.ubo, sizeof(compute.ubo));
} }
void draw() void draw()
{ {
// Submit graphics commands
VulkanExampleBase::prepareFrame(); VulkanExampleBase::prepareFrame();
submitInfo.commandBufferCount = 1; submitInfo.commandBufferCount = 1;
@ -636,8 +613,7 @@ public:
VulkanExampleBase::submitFrame(); VulkanExampleBase::submitFrame();
// Submit compute // Submit compute commands
// todo: async compute
vkWaitForFences(device, 1, &compute.fence, VK_TRUE, UINT64_MAX); vkWaitForFences(device, 1, &compute.fence, VK_TRUE, UINT64_MAX);
vkResetFences(device, 1, &compute.fence); vkResetFences(device, 1, &compute.fence);