Refactored particle fire example

This commit is contained in:
saschawillems 2016-06-05 20:14:04 +02:00
parent 54801493aa
commit 52a7da9ace

View file

@ -124,8 +124,9 @@ public:
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
{ {
zoom = -90.0f; zoom = -75.0f;
rotation = { -15.0f, 45.0f, 0.0f }; rotation = { -15.0f, 45.0f, 0.0f };
enableTextOverlay = true;
title = "Vulkan Example - Particle system"; title = "Vulkan Example - Particle system";
zoomSpeed *= 1.5f; zoomSpeed *= 1.5f;
timerSpeed *= 8.0f; timerSpeed *= 8.0f;
@ -178,30 +179,19 @@ public:
renderPassBeginInfo.clearValueCount = 2; renderPassBeginInfo.clearValueCount = 2;
renderPassBeginInfo.pClearValues = clearValues; renderPassBeginInfo.pClearValues = clearValues;
VkResult err;
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i) for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
{ {
// Set target frame buffer // Set target frame buffer
renderPassBeginInfo.framebuffer = frameBuffers[i]; renderPassBeginInfo.framebuffer = frameBuffers[i];
err = vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo); VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
assert(!err);
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport = vkTools::initializers::viewport( VkViewport viewport = vkTools::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
(float)width,
(float)height,
0.0f,
1.0f);
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
VkRect2D scissor = vkTools::initializers::rect2D( VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0,0);
width,
height,
0,
0);
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
// Environment // Environment
@ -216,38 +206,10 @@ public:
vkCmdEndRenderPass(drawCmdBuffers[i]); vkCmdEndRenderPass(drawCmdBuffers[i]);
err = vkEndCommandBuffer(drawCmdBuffers[i]); VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
assert(!err);
} }
} }
void draw()
{
VkResult err;
// Get next image in the swap chain (back/front buffer)
err = swapChain.acquireNextImage(semaphores.presentComplete, &currentBuffer);
assert(!err);
submitPostPresentBarrier(swapChain.buffers[currentBuffer].image);
// Command buffer to be sumitted to the queue
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
// Submit to queue
err = vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE);
assert(!err);
submitPrePresentBarrier(swapChain.buffers[currentBuffer].image);
err = swapChain.queuePresent(queue, currentBuffer, semaphores.renderComplete);
assert(!err);
err = vkQueueWaitIdle(queue);
assert(!err);
}
float rnd(float range) float rnd(float range)
{ {
return range * (rand() / double(RAND_MAX)); return range * (rand() / double(RAND_MAX));
@ -323,8 +285,7 @@ public:
&particles.memory); &particles.memory);
// Map the memory and store the pointer for reuse // Map the memory and store the pointer for reuse
VkResult err = vkMapMemory(device, particles.memory, 0, particles.size, 0, &particles.mappedMemory); VK_CHECK_RESULT(vkMapMemory(device, particles.memory, 0, particles.size, 0, &particles.mappedMemory));
assert(!err);
} }
void updateParticles() void updateParticles()
@ -399,8 +360,7 @@ public:
samplerCreateInfo.anisotropyEnable = VK_TRUE; samplerCreateInfo.anisotropyEnable = VK_TRUE;
// Use a different border color (than the normal texture loader) for additive blending // Use a different border color (than the normal texture loader) for additive blending
samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK; samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
VkResult err = vkCreateSampler(device, &samplerCreateInfo, nullptr, &textures.particles.sampler); VK_CHECK_RESULT(vkCreateSampler(device, &samplerCreateInfo, nullptr, &textures.particles.sampler));
assert(!err);
} }
void loadMeshes() void loadMeshes()
@ -486,8 +446,7 @@ public:
poolSizes.data(), poolSizes.data(),
2); 2);
VkResult vkRes = vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool); VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
assert(!vkRes);
} }
void setupDescriptorSetLayout() void setupDescriptorSetLayout()
@ -516,16 +475,14 @@ public:
setLayoutBindings.data(), setLayoutBindings.data(),
setLayoutBindings.size()); setLayoutBindings.size());
VkResult err = vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
assert(!err);
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
vkTools::initializers::pipelineLayoutCreateInfo( vkTools::initializers::pipelineLayoutCreateInfo(
&descriptorSetLayout, &descriptorSetLayout,
1); 1);
err = vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout); VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
assert(!err);
} }
void setupDescriptorSets() void setupDescriptorSets()
@ -536,8 +493,7 @@ public:
&descriptorSetLayout, &descriptorSetLayout,
1); 1);
VkResult vkRes = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
assert(!vkRes);
// Image descriptor for the color map texture // Image descriptor for the color map texture
VkDescriptorImageInfo texDescriptorSmoke = VkDescriptorImageInfo texDescriptorSmoke =
@ -576,8 +532,7 @@ public:
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL); vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
// Environment // Environment
vkRes = vkAllocateDescriptorSets(device, &allocInfo, &meshes.environment.descriptorSet); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &meshes.environment.descriptorSet));
assert(!vkRes);
VkDescriptorImageInfo texDescriptorColorMap = VkDescriptorImageInfo texDescriptorColorMap =
vkTools::initializers::descriptorImageInfo( vkTools::initializers::descriptorImageInfo(
@ -701,8 +656,7 @@ public:
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD; blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
VkResult err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.particles); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.particles));
assert(!err);
// Environment rendering pipeline (normal mapped) // Environment rendering pipeline (normal mapped)
shaderStages[0] = loadShader(getAssetPath() + "shaders/particlefire/normalmap.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); shaderStages[0] = loadShader(getAssetPath() + "shaders/particlefire/normalmap.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
@ -711,8 +665,8 @@ public:
blendAttachmentState.blendEnable = VK_FALSE; blendAttachmentState.blendEnable = VK_FALSE;
depthStencilState.depthWriteEnable = VK_TRUE; depthStencilState.depthWriteEnable = VK_TRUE;
inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.environment); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.environment));
assert(!err);
meshes.environment.pipeline = pipelines.environment; meshes.environment.pipeline = pipelines.environment;
meshes.environment.pipelineLayout = pipelineLayout; meshes.environment.pipelineLayout = pipelineLayout;
} }
@ -723,6 +677,7 @@ public:
// Vertex shader uniform buffer block // Vertex shader uniform buffer block
createBuffer( createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
sizeof(uboVS), sizeof(uboVS),
&uboVS, &uboVS,
&uniformData.fire.buffer, &uniformData.fire.buffer,
@ -732,6 +687,7 @@ public:
// Vertex shader uniform buffer block // Vertex shader uniform buffer block
createBuffer( createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
sizeof(uboEnv), sizeof(uboEnv),
&uboEnv, &uboEnv,
&uniformData.environment.buffer, &uniformData.environment.buffer,
@ -748,8 +704,7 @@ public:
uboEnv.lightPos.y = 0.0f; uboEnv.lightPos.y = 0.0f;
uboEnv.lightPos.z = cos(timer * 2 * M_PI) * 1.5f; uboEnv.lightPos.z = cos(timer * 2 * M_PI) * 1.5f;
uint8_t *pData; uint8_t *pData;
VkResult err = vkMapMemory(device, uniformData.environment.memory, 0, sizeof(uboEnv), 0, (void **)&pData); VK_CHECK_RESULT(vkMapMemory(device, uniformData.environment.memory, 0, sizeof(uboEnv), 0, (void **)&pData));
assert(!err);
memcpy(pData, &uboEnv, sizeof(uboEnv)); memcpy(pData, &uboEnv, sizeof(uboEnv));
vkUnmapMemory(device, uniformData.environment.memory); vkUnmapMemory(device, uniformData.environment.memory);
} }
@ -770,8 +725,7 @@ public:
uboVS.viewportDim = glm::vec2((float)width, (float)height); uboVS.viewportDim = glm::vec2((float)width, (float)height);
uint8_t *pData; uint8_t *pData;
VkResult err = vkMapMemory(device, uniformData.fire.memory, 0, sizeof(uboVS), 0, (void **)&pData); VK_CHECK_RESULT(vkMapMemory(device, uniformData.fire.memory, 0, sizeof(uboVS), 0, (void **)&pData));
assert(!err);
memcpy(pData, &uboVS, sizeof(uboVS)); memcpy(pData, &uboVS, sizeof(uboVS));
vkUnmapMemory(device, uniformData.fire.memory); vkUnmapMemory(device, uniformData.fire.memory);
@ -780,12 +734,25 @@ public:
uboEnv.model = uboVS.model; uboEnv.model = uboVS.model;
uboEnv.normal = glm::inverseTranspose(uboEnv.model); uboEnv.normal = glm::inverseTranspose(uboEnv.model);
uboEnv.cameraPos = glm::vec4(0.0, 0.0, zoom, 0.0); uboEnv.cameraPos = glm::vec4(0.0, 0.0, zoom, 0.0);
err = vkMapMemory(device, uniformData.environment.memory, 0, sizeof(uboEnv), 0, (void **)&pData); VK_CHECK_RESULT(vkMapMemory(device, uniformData.environment.memory, 0, sizeof(uboEnv), 0, (void **)&pData));
assert(!err);
memcpy(pData, &uboEnv, sizeof(uboEnv)); memcpy(pData, &uboEnv, sizeof(uboEnv));
vkUnmapMemory(device, uniformData.environment.memory); vkUnmapMemory(device, uniformData.environment.memory);
} }
void draw()
{
VulkanExampleBase::prepareFrame();
// Command buffer to be sumitted to the queue
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
// Submit to queue
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
VulkanExampleBase::submitFrame();
}
void prepare() void prepare()
{ {
VulkanExampleBase::prepare(); VulkanExampleBase::prepare();
@ -806,9 +773,7 @@ public:
{ {
if (!prepared) if (!prepared)
return; return;
vkDeviceWaitIdle(device);
draw(); draw();
vkDeviceWaitIdle(device);
if (!paused) if (!paused)
{ {
updateUniformBufferLight(); updateUniformBufferLight();