Code cleanup

This commit is contained in:
Sascha Willems 2024-01-21 12:52:14 +01:00
parent 922dbd4827
commit a546f466c1
3 changed files with 177 additions and 368 deletions

View file

@ -3,7 +3,7 @@
* *
* Updated compute shader by Lukas Bergdoll (https://github.com/Voultapher) * Updated compute shader by Lukas Bergdoll (https://github.com/Voultapher)
* *
* Copyright (C) 2016-2021 by Sascha Willems - www.saschawillems.de * Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
* *
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
@ -29,14 +29,19 @@ public:
vks::Texture2D gradient; vks::Texture2D gradient;
} textures; } textures;
struct { // SSBO particle declaration
VkPipelineVertexInputStateCreateInfo inputState; struct Particle {
std::vector<VkVertexInputBindingDescription> bindingDescriptions; glm::vec2 pos; // Particle position
std::vector<VkVertexInputAttributeDescription> attributeDescriptions; glm::vec2 vel; // Particle velocity
} vertices; glm::vec4 gradientPos; // Texture coordinates for the gradient ramp map
};
// We use a shader storage buffer object to store the particlces
// This is updated by the compute pipeline and displayed as a vertex buffer by the graphics pipeline
vks::Buffer storageBuffer;
// Resources for the graphics part of the example // Resources for the graphics part of the example
struct { struct Graphics {
uint32_t queueFamilyIndex; // Used to check if compute and graphics queue families differ and require additional barriers uint32_t queueFamilyIndex; // Used to check if compute and graphics queue families differ and require additional barriers
VkDescriptorSetLayout descriptorSetLayout; // Particle system rendering shader binding layout VkDescriptorSetLayout descriptorSetLayout; // Particle system rendering shader binding layout
VkDescriptorSet descriptorSet; // Particle system rendering shader bindings VkDescriptorSet descriptorSet; // Particle system rendering shader bindings
@ -46,10 +51,8 @@ public:
} graphics; } graphics;
// Resources for the compute part of the example // Resources for the compute part of the example
struct { struct Compute {
uint32_t queueFamilyIndex; // Used to check if compute and graphics queue families differ and require additional barriers uint32_t queueFamilyIndex; // Used to check if compute and graphics queue families differ and require additional barriers
vks::Buffer storageBuffer; // (Shader) storage buffer object containing the particles
vks::Buffer uniformBuffer; // Uniform buffer object containing particle system parameters
VkQueue queue; // Separate queue for compute commands (queue family may differ from the one used for graphics) VkQueue queue; // Separate queue for compute commands (queue family may differ from the one used for graphics)
VkCommandPool commandPool; // Use a separate command pool (queue family may differ from the one used for graphics) VkCommandPool commandPool; // Use a separate command pool (queue family may differ from the one used for graphics)
VkCommandBuffer commandBuffer; // Command buffer storing the dispatch commands and barriers VkCommandBuffer commandBuffer; // Command buffer storing the dispatch commands and barriers
@ -58,21 +61,15 @@ 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 vks::Buffer uniformBuffer; // Uniform buffer object containing particle system parameters
struct UniformData { // Compute shader uniform block object
float deltaT; // Frame delta time float deltaT; // Frame delta time
float destX; // x position of the attractor float destX; // x position of the attractor
float destY; // y position of the attractor float destY; // y position of the attractor
int32_t particleCount = PARTICLE_COUNT; int32_t particleCount = PARTICLE_COUNT;
} ubo; } uniformData;
} compute; } compute;
// SSBO particle declaration
struct Particle {
glm::vec2 pos; // Particle position
glm::vec2 vel; // Particle velocity
glm::vec4 gradientPos; // Texture coordinates for the gradient ramp map
};
VulkanExample() : VulkanExampleBase() VulkanExample() : VulkanExampleBase()
{ {
title = "Compute shader particle system"; title = "Compute shader particle system";
@ -80,6 +77,7 @@ public:
~VulkanExample() ~VulkanExample()
{ {
if (device) {
// Graphics // Graphics
vkDestroyPipeline(device, graphics.pipeline, nullptr); vkDestroyPipeline(device, graphics.pipeline, nullptr);
vkDestroyPipelineLayout(device, graphics.pipelineLayout, nullptr); vkDestroyPipelineLayout(device, graphics.pipelineLayout, nullptr);
@ -87,7 +85,6 @@ public:
vkDestroySemaphore(device, graphics.semaphore, nullptr); vkDestroySemaphore(device, graphics.semaphore, nullptr);
// Compute // Compute
compute.storageBuffer.destroy();
compute.uniformBuffer.destroy(); compute.uniformBuffer.destroy();
vkDestroyPipelineLayout(device, compute.pipelineLayout, nullptr); vkDestroyPipelineLayout(device, compute.pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, compute.descriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(device, compute.descriptorSetLayout, nullptr);
@ -95,9 +92,11 @@ public:
vkDestroySemaphore(device, compute.semaphore, nullptr); vkDestroySemaphore(device, compute.semaphore, nullptr);
vkDestroyCommandPool(device, compute.commandPool, nullptr); vkDestroyCommandPool(device, compute.commandPool, nullptr);
storageBuffer.destroy();
textures.particle.destroy(); textures.particle.destroy();
textures.gradient.destroy(); textures.gradient.destroy();
} }
}
void loadAssets() void loadAssets()
{ {
@ -140,9 +139,9 @@ public:
VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT, VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT,
compute.queueFamilyIndex, compute.queueFamilyIndex,
graphics.queueFamilyIndex, graphics.queueFamilyIndex,
compute.storageBuffer.buffer, storageBuffer.buffer,
0, 0,
compute.storageBuffer.size storageBuffer.size
}; };
vkCmdPipelineBarrier( vkCmdPipelineBarrier(
@ -168,7 +167,7 @@ public:
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipelineLayout, 0, 1, &graphics.descriptorSet, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipelineLayout, 0, 1, &graphics.descriptorSet, 0, NULL);
VkDeviceSize offsets[1] = { 0 }; VkDeviceSize offsets[1] = { 0 };
vkCmdBindVertexBuffers(drawCmdBuffers[i], 0, 1, &compute.storageBuffer.buffer, offsets); vkCmdBindVertexBuffers(drawCmdBuffers[i], 0, 1, &storageBuffer.buffer, offsets);
vkCmdDraw(drawCmdBuffers[i], PARTICLE_COUNT, 1, 0, 0); vkCmdDraw(drawCmdBuffers[i], PARTICLE_COUNT, 1, 0, 0);
drawUI(drawCmdBuffers[i]); drawUI(drawCmdBuffers[i]);
@ -186,9 +185,9 @@ public:
0, 0,
graphics.queueFamilyIndex, graphics.queueFamilyIndex,
compute.queueFamilyIndex, compute.queueFamilyIndex,
compute.storageBuffer.buffer, storageBuffer.buffer,
0, 0,
compute.storageBuffer.size storageBuffer.size
}; };
vkCmdPipelineBarrier( vkCmdPipelineBarrier(
@ -225,9 +224,9 @@ public:
VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_SHADER_WRITE_BIT,
graphics.queueFamilyIndex, graphics.queueFamilyIndex,
compute.queueFamilyIndex, compute.queueFamilyIndex,
compute.storageBuffer.buffer, storageBuffer.buffer,
0, 0,
compute.storageBuffer.size storageBuffer.size
}; };
vkCmdPipelineBarrier( vkCmdPipelineBarrier(
@ -257,9 +256,9 @@ public:
0, 0,
compute.queueFamilyIndex, compute.queueFamilyIndex,
graphics.queueFamilyIndex, graphics.queueFamilyIndex,
compute.storageBuffer.buffer, storageBuffer.buffer,
0, 0,
compute.storageBuffer.size storageBuffer.size
}; };
vkCmdPipelineBarrier( vkCmdPipelineBarrier(
@ -307,14 +306,14 @@ public:
// The SSBO will be used as a storage buffer for the compute pipeline and as a vertex buffer in the graphics pipeline // The SSBO will be used as a storage buffer for the compute pipeline and as a vertex buffer in the graphics pipeline
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
&compute.storageBuffer, &storageBuffer,
storageBufferSize); storageBufferSize);
// Copy from staging buffer to storage buffer // Copy from staging buffer to storage buffer
VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
VkBufferCopy copyRegion = {}; VkBufferCopy copyRegion = {};
copyRegion.size = storageBufferSize; copyRegion.size = storageBufferSize;
vkCmdCopyBuffer(copyCmd, stagingBuffer.buffer, compute.storageBuffer.buffer, 1, &copyRegion); vkCmdCopyBuffer(copyCmd, stagingBuffer.buffer, storageBuffer.buffer, 1, &copyRegion);
// Execute a transfer barrier to the compute queue, if necessary // Execute a transfer barrier to the compute queue, if necessary
if (graphics.queueFamilyIndex != compute.queueFamilyIndex) if (graphics.queueFamilyIndex != compute.queueFamilyIndex)
{ {
@ -326,9 +325,9 @@ public:
0, 0,
graphics.queueFamilyIndex, graphics.queueFamilyIndex,
compute.queueFamilyIndex, compute.queueFamilyIndex,
compute.storageBuffer.buffer, storageBuffer.buffer,
0, 0,
compute.storageBuffer.size storageBuffer.size
}; };
vkCmdPipelineBarrier( vkCmdPipelineBarrier(
@ -343,96 +342,37 @@ public:
vulkanDevice->flushCommandBuffer(copyCmd, queue, true); vulkanDevice->flushCommandBuffer(copyCmd, queue, true);
stagingBuffer.destroy(); stagingBuffer.destroy();
// Binding description
vertices.bindingDescriptions.resize(1);
vertices.bindingDescriptions[0] =
vks::initializers::vertexInputBindingDescription(
0,
sizeof(Particle),
VK_VERTEX_INPUT_RATE_VERTEX);
// Attribute descriptions
// Describes memory layout and shader positions
vertices.attributeDescriptions.resize(2);
// Location 0 : Position
vertices.attributeDescriptions[0] =
vks::initializers::vertexInputAttributeDescription(
0,
0,
VK_FORMAT_R32G32_SFLOAT,
offsetof(Particle, pos));
// Location 1 : Gradient position
vertices.attributeDescriptions[1] =
vks::initializers::vertexInputAttributeDescription(
0,
1,
VK_FORMAT_R32G32B32A32_SFLOAT,
offsetof(Particle, gradientPos));
// Assign to vertex buffer
vertices.inputState = vks::initializers::pipelineVertexInputStateCreateInfo();
vertices.inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertices.bindingDescriptions.size());
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
vertices.inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertices.attributeDescriptions.size());
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
} }
// The descriptor pool will be shared between graphics and compute
void setupDescriptorPool() void setupDescriptorPool()
{ {
std::vector<VkDescriptorPoolSize> poolSizes = std::vector<VkDescriptorPoolSize> poolSizes = {
{
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1), vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1), vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1),
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2) vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2)
}; };
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 2);
VkDescriptorPoolCreateInfo descriptorPoolInfo =
vks::initializers::descriptorPoolCreateInfo(
static_cast<uint32_t>(poolSizes.size()),
poolSizes.data(),
2);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
} }
void setupDescriptorSetLayout() void prepareGraphics()
{ {
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings; prepareStorageBuffers();
prepareUniformBuffers();
// Descriptor set layout
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
// Binding 0 : Particle color map // Binding 0 : Particle color map
setLayoutBindings.push_back(vks::initializers::descriptorSetLayoutBinding( vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0),
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_SHADER_STAGE_FRAGMENT_BIT,
0));
// Binding 1 : Particle gradient ramp // Binding 1 : Particle gradient ramp
setLayoutBindings.push_back(vks::initializers::descriptorSetLayoutBinding( vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1)
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, };
VK_SHADER_STAGE_FRAGMENT_BIT, VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
1));
VkDescriptorSetLayoutCreateInfo descriptorLayout =
vks::initializers::descriptorSetLayoutCreateInfo(
setLayoutBindings.data(),
static_cast<uint32_t>(setLayoutBindings.size()));
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &graphics.descriptorSetLayout)); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &graphics.descriptorSetLayout));
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = // Descriptor set
vks::initializers::pipelineLayoutCreateInfo( VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &graphics.descriptorSetLayout, 1);
&graphics.descriptorSetLayout,
1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &graphics.pipelineLayout));
}
void setupDescriptorSet()
{
VkDescriptorSetAllocateInfo allocInfo =
vks::initializers::descriptorSetAllocateInfo(
descriptorPool,
&graphics.descriptorSetLayout,
1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &graphics.descriptorSet)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &graphics.descriptorSet));
std::vector<VkWriteDescriptorSet> writeDescriptorSets; std::vector<VkWriteDescriptorSet> writeDescriptorSets;
@ -450,71 +390,44 @@ public:
&textures.gradient.descriptor)); &textures.gradient.descriptor));
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL); vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
}
void preparePipelines() // Pipeline layout
{ VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&graphics.descriptorSetLayout, 1);
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &graphics.pipelineLayout));
vks::initializers::pipelineInputAssemblyStateCreateInfo(
VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
0,
VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterizationState = // Pipeline
vks::initializers::pipelineRasterizationStateCreateInfo( VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_POINT_LIST, 0, VK_FALSE);
VK_POLYGON_MODE_FILL, VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
VK_CULL_MODE_NONE, VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
VK_FRONT_FACE_COUNTER_CLOCKWISE, VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
0); VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_FALSE, VK_FALSE, VK_COMPARE_OP_ALWAYS);
VkPipelineViewportStateCreateInfo viewportState = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
VkPipelineColorBlendAttachmentState blendAttachmentState = VkPipelineMultisampleStateCreateInfo multisampleState = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0);
vks::initializers::pipelineColorBlendAttachmentState( std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
0xf, VkPipelineDynamicStateCreateInfo dynamicState = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables);
VK_FALSE);
VkPipelineColorBlendStateCreateInfo colorBlendState =
vks::initializers::pipelineColorBlendStateCreateInfo(
1,
&blendAttachmentState);
VkPipelineDepthStencilStateCreateInfo depthStencilState =
vks::initializers::pipelineDepthStencilStateCreateInfo(
VK_FALSE,
VK_FALSE,
VK_COMPARE_OP_ALWAYS);
VkPipelineViewportStateCreateInfo viewportState =
vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
VkPipelineMultisampleStateCreateInfo multisampleState =
vks::initializers::pipelineMultisampleStateCreateInfo(
VK_SAMPLE_COUNT_1_BIT,
0);
std::vector<VkDynamicState> dynamicStateEnables = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR
};
VkPipelineDynamicStateCreateInfo dynamicState =
vks::initializers::pipelineDynamicStateCreateInfo(
dynamicStateEnables.data(),
static_cast<uint32_t>(dynamicStateEnables.size()),
0);
// Rendering pipeline
// Load shaders
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages; std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
// Vertex Input state
std::vector<VkVertexInputBindingDescription> inputBindings = {
vks::initializers::vertexInputBindingDescription(0, sizeof(Particle), VK_VERTEX_INPUT_RATE_VERTEX)
};
std::vector<VkVertexInputAttributeDescription> inputAttributes = {
// Location 0 : Position
vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(Particle, pos)),
// Location 1 : Velocity (used for color gradient lookup)
vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(Particle, gradientPos)),
};
VkPipelineVertexInputStateCreateInfo vertexInputState = vks::initializers::pipelineVertexInputStateCreateInfo();
vertexInputState.vertexBindingDescriptionCount = static_cast<uint32_t>(inputBindings.size());
vertexInputState.pVertexBindingDescriptions = inputBindings.data();
vertexInputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(inputAttributes.size());
vertexInputState.pVertexAttributeDescriptions = inputAttributes.data();
shaderStages[0] = loadShader(getShadersPath() + "computeparticles/particle.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); shaderStages[0] = loadShader(getShadersPath() + "computeparticles/particle.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getShadersPath() + "computeparticles/particle.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); shaderStages[1] = loadShader(getShadersPath() + "computeparticles/particle.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
VkGraphicsPipelineCreateInfo pipelineCreateInfo = VkGraphicsPipelineCreateInfo pipelineCreateInfo = vks::initializers::pipelineCreateInfo(graphics.pipelineLayout, renderPass, 0);
vks::initializers::pipelineCreateInfo( pipelineCreateInfo.pVertexInputState = &vertexInputState;
graphics.pipelineLayout,
renderPass,
0);
pipelineCreateInfo.pVertexInputState = &vertices.inputState;
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState; pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
pipelineCreateInfo.pRasterizationState = &rasterizationState; pipelineCreateInfo.pRasterizationState = &rasterizationState;
pipelineCreateInfo.pColorBlendState = &colorBlendState; pipelineCreateInfo.pColorBlendState = &colorBlendState;
@ -537,15 +450,6 @@ public:
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_DST_ALPHA; blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_DST_ALPHA;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &graphics.pipeline)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &graphics.pipeline));
}
void prepareGraphics()
{
prepareStorageBuffers();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorSet();
// Semaphore for compute & graphics sync // Semaphore for compute & graphics sync
VkSemaphoreCreateInfo semaphoreCreateInfo = vks::initializers::semaphoreCreateInfo(); VkSemaphoreCreateInfo semaphoreCreateInfo = vks::initializers::semaphoreCreateInfo();
@ -582,37 +486,18 @@ public:
VK_SHADER_STAGE_COMPUTE_BIT, VK_SHADER_STAGE_COMPUTE_BIT,
1), 1),
}; };
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
VkDescriptorSetLayoutCreateInfo descriptorLayout =
vks::initializers::descriptorSetLayoutCreateInfo(
setLayoutBindings.data(),
static_cast<uint32_t>(setLayoutBindings.size()));
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &compute.descriptorSetLayout)); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &compute.descriptorSetLayout));
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &compute.descriptorSetLayout,1);
vks::initializers::pipelineLayoutCreateInfo(
&compute.descriptorSetLayout,
1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &compute.pipelineLayout));
VkDescriptorSetAllocateInfo allocInfo =
vks::initializers::descriptorSetAllocateInfo(
descriptorPool,
&compute.descriptorSetLayout,
1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &compute.descriptorSet)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &compute.descriptorSet));
std::vector<VkWriteDescriptorSet> computeWriteDescriptorSets = {
std::vector<VkWriteDescriptorSet> computeWriteDescriptorSets =
{
// Binding 0 : Particle position storage buffer // Binding 0 : Particle position storage buffer
vks::initializers::writeDescriptorSet( vks::initializers::writeDescriptorSet(
compute.descriptorSet, compute.descriptorSet,
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
0, 0,
&compute.storageBuffer.descriptor), &storageBuffer.descriptor),
// Binding 1 : Uniform buffer // Binding 1 : Uniform buffer
vks::initializers::writeDescriptorSet( vks::initializers::writeDescriptorSet(
compute.descriptorSet, compute.descriptorSet,
@ -620,10 +505,11 @@ public:
1, 1,
&compute.uniformBuffer.descriptor) &compute.uniformBuffer.descriptor)
}; };
vkUpdateDescriptorSets(device, static_cast<uint32_t>(computeWriteDescriptorSets.size()), computeWriteDescriptorSets.data(), 0, NULL); vkUpdateDescriptorSets(device, static_cast<uint32_t>(computeWriteDescriptorSets.size()), computeWriteDescriptorSets.data(), 0, NULL);
// Create pipeline // Create pipeline
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&compute.descriptorSetLayout, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &compute.pipelineLayout));
VkComputePipelineCreateInfo computePipelineCreateInfo = vks::initializers::computePipelineCreateInfo(compute.pipelineLayout, 0); VkComputePipelineCreateInfo computePipelineCreateInfo = vks::initializers::computePipelineCreateInfo(compute.pipelineLayout, 0);
computePipelineCreateInfo.stage = loadShader(getShadersPath() + "computeparticles/particle.comp.spv", VK_SHADER_STAGE_COMPUTE_BIT); computePipelineCreateInfo.stage = loadShader(getShadersPath() + "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));
@ -644,72 +530,13 @@ public:
// Build a single command buffer containing the compute dispatch commands // Build a single command buffer containing the compute dispatch commands
buildComputeCommandBuffer(); buildComputeCommandBuffer();
// SRS - By reordering compute and graphics within draw(), the following code is no longer needed:
// If graphics and compute queue family indices differ, acquire and immediately release the storage buffer, so that the initial acquire from the graphics command buffers are matched up properly
/*
if (graphics.queueFamilyIndex != compute.queueFamilyIndex)
{
// Create a transient command buffer for setting up the initial buffer transfer state
VkCommandBuffer transferCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, compute.commandPool, true);
VkBufferMemoryBarrier acquire_buffer_barrier =
{
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
nullptr,
0,
VK_ACCESS_SHADER_WRITE_BIT,
graphics.queueFamilyIndex,
compute.queueFamilyIndex,
compute.storageBuffer.buffer,
0,
compute.storageBuffer.size
};
vkCmdPipelineBarrier(
transferCmd,
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0,
0, nullptr,
1, &acquire_buffer_barrier,
0, nullptr);
VkBufferMemoryBarrier release_buffer_barrier =
{
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
nullptr,
VK_ACCESS_SHADER_WRITE_BIT,
0,
compute.queueFamilyIndex,
graphics.queueFamilyIndex,
compute.storageBuffer.buffer,
0,
compute.storageBuffer.size
};
vkCmdPipelineBarrier(
transferCmd,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
0,
0, nullptr,
1, &release_buffer_barrier,
0, nullptr);
vulkanDevice->flushCommandBuffer(transferCmd, compute.queue, compute.commandPool);
}
*/
} }
// Prepare and initialize uniform buffer containing shader uniforms // Prepare and initialize uniform buffer containing shader uniforms
void prepareUniformBuffers() void prepareUniformBuffers()
{ {
// Compute shader uniform buffer block // Compute shader uniform buffer block
vulkanDevice->createBuffer( vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &compute.uniformBuffer, sizeof(Compute::UniformData));
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&compute.uniformBuffer,
sizeof(compute.ubo));
// Map for host access // Map for host access
VK_CHECK_RESULT(compute.uniformBuffer.map()); VK_CHECK_RESULT(compute.uniformBuffer.map());
@ -718,21 +545,21 @@ public:
void updateUniformBuffers() void updateUniformBuffers()
{ {
compute.ubo.deltaT = paused ? 0.0f : frameTimer * 2.5f; compute.uniformData.deltaT = paused ? 0.0f : frameTimer * 2.5f;
if (!attachToCursor) if (!attachToCursor)
{ {
compute.ubo.destX = sin(glm::radians(timer * 360.0f)) * 0.75f; compute.uniformData.destX = sin(glm::radians(timer * 360.0f)) * 0.75f;
compute.ubo.destY = 0.0f; compute.uniformData.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);
compute.ubo.destX = normalizedMx; compute.uniformData.destX = normalizedMx;
compute.ubo.destY = normalizedMy; compute.uniformData.destY = normalizedMy;
} }
memcpy(compute.uniformBuffer.mapped, &compute.ubo, sizeof(compute.ubo)); memcpy(compute.uniformBuffer.mapped, &compute.uniformData, sizeof(Compute::UniformData));
} }
void draw() void draw()

View file

@ -434,7 +434,6 @@ public:
// The descriptor pool will be shared between graphics and compute // The descriptor pool will be shared between graphics and compute
void setupDescriptorPool() void setupDescriptorPool()
{ {
// @todo: probably wrong
std::vector<VkDescriptorPoolSize> poolSizes = { std::vector<VkDescriptorPoolSize> poolSizes = {
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2), vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2),
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 4), vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 4),

View file

@ -20,22 +20,15 @@ public:
vkglTF::Model plane; vkglTF::Model plane;
struct { struct UniformDataVertexShader {
vks::Buffer vertexShader;
vks::Buffer fragmentShader;
} uniformBuffers;
struct {
struct {
glm::mat4 projection; glm::mat4 projection;
glm::mat4 view; glm::mat4 view;
glm::mat4 model; glm::mat4 model;
glm::vec4 lightPos = glm::vec4(0.0f, -2.0f, 0.0f, 1.0f); glm::vec4 lightPos = glm::vec4(0.0f, -2.0f, 0.0f, 1.0f);
glm::vec4 cameraPos; glm::vec4 cameraPos;
} vertexShader; } uniformDataVertexShader;
struct { struct UniformDataFragmentShader {
float heightScale = 0.1f; float heightScale = 0.1f;
// Basic parallax mapping needs a bias to look any good (and is hard to tweak) // Basic parallax mapping needs a bias to look any good (and is hard to tweak)
float parallaxBias = -0.02f; float parallaxBias = -0.02f;
@ -43,14 +36,17 @@ public:
float numLayers = 48.0f; float numLayers = 48.0f;
// (Parallax) mapping mode to use // (Parallax) mapping mode to use
int32_t mappingMode = 4; int32_t mappingMode = 4;
} fragmentShader; } uniformDataFragmentShader;
} ubos; struct {
vks::Buffer vertexShader;
vks::Buffer fragmentShader;
} uniformBuffers;
VkPipelineLayout pipelineLayout; VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
VkPipeline pipeline; VkPipeline pipeline{ VK_NULL_HANDLE };
VkDescriptorSetLayout descriptorSetLayout; VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
VkDescriptorSet descriptorSet; VkDescriptorSet descriptorSet{ VK_NULL_HANDLE };
const std::vector<std::string> mappingModes = { const std::vector<std::string> mappingModes = {
"Color only", "Color only",
@ -72,17 +68,16 @@ public:
~VulkanExample() ~VulkanExample()
{ {
if (device) {
vkDestroyPipeline(device, pipeline, nullptr); vkDestroyPipeline(device, pipeline, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
uniformBuffers.vertexShader.destroy(); uniformBuffers.vertexShader.destroy();
uniformBuffers.fragmentShader.destroy(); uniformBuffers.fragmentShader.destroy();
textures.colorMap.destroy(); textures.colorMap.destroy();
textures.normalHeightMap.destroy(); textures.normalHeightMap.destroy();
} }
}
void loadAssets() void loadAssets()
{ {
@ -136,52 +131,53 @@ public:
} }
} }
void setupDescriptorPool() void setupDescriptors()
{
// Example uses two ubos and two image sampler
std::vector<VkDescriptorPoolSize> poolSizes =
{ {
// Pool
std::vector<VkDescriptorPoolSize> poolSizes = {
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2), vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2),
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2) vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2)
}; };
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 2);
VkDescriptorPoolCreateInfo descriptorPoolInfo =
vks::initializers::descriptorPoolCreateInfo(poolSizes, 2);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
}
void setupDescriptorSetLayout() // Layout
{
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = { std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0), // Binding 0: Vertex shader uniform buffer // Binding 0: Vertex shader uniform buffer
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1), // Binding 1: Fragment shader color map image sampler vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 2), // Binding 2: Fragment combined normal and heightmap // Binding 1: Fragment shader color map image sampler
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT, 3), // Binding 3: Fragment shader uniform buffer vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1),
// Binding 2: Fragment combined normal and heightmap
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 2),
// Binding 3: Fragment shader uniform buffer
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT, 3),
}; };
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings); VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout)); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1); // Set
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
}
void setupDescriptorSet()
{
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1); VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
std::vector<VkWriteDescriptorSet> writeDescriptorSets = { std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.vertexShader.descriptor), // Binding 0: Vertex shader uniform buffer // Binding 0: Vertex shader uniform buffer
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &textures.colorMap.descriptor), // Binding 1: Fragment shader image sampler vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.vertexShader.descriptor),
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2, &textures.normalHeightMap.descriptor), // Binding 2: Combined normal and heightmap // Binding 1: Fragment shader image sampler
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3, &uniformBuffers.fragmentShader.descriptor), // Binding 3: Fragment shader uniform buffer vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &textures.colorMap.descriptor),
// Binding 2: Combined normal and heightmap
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2, &textures.normalHeightMap.descriptor),
// Binding 3: Fragment shader uniform buffer
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3, &uniformBuffers.fragmentShader.descriptor),
}; };
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL); vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
} }
void preparePipelines() void preparePipelines()
{ {
// Layout
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
// Pipeline
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE); VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_COUNTER_CLOCKWISE); VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_COUNTER_CLOCKWISE);
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE); VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
@ -214,43 +210,33 @@ public:
void prepareUniformBuffers() void prepareUniformBuffers()
{ {
// Vertex shader uniform buffer // Vertex shader uniform buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.vertexShader, sizeof(UniformDataVertexShader)));
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.vertexShader,
sizeof(ubos.vertexShader)));
// Fragment shader uniform buffer // Fragment shader uniform buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.fragmentShader, sizeof(UniformDataFragmentShader)));
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.fragmentShader,
sizeof(ubos.fragmentShader)));
// Map persistent // Map persistent
VK_CHECK_RESULT(uniformBuffers.vertexShader.map()); VK_CHECK_RESULT(uniformBuffers.vertexShader.map());
VK_CHECK_RESULT(uniformBuffers.fragmentShader.map()); VK_CHECK_RESULT(uniformBuffers.fragmentShader.map());
updateUniformBuffers(); updateUniformBuffers();
} }
void updateUniformBuffers() void updateUniformBuffers()
{ {
// Vertex shader // Vertex shader
ubos.vertexShader.projection = camera.matrices.perspective; uniformDataVertexShader.projection = camera.matrices.perspective;
ubos.vertexShader.view = camera.matrices.view; uniformDataVertexShader.view = camera.matrices.view;
ubos.vertexShader.model = glm::scale(glm::mat4(1.0f), glm::vec3(0.2f)); uniformDataVertexShader.model = glm::scale(glm::mat4(1.0f), glm::vec3(0.2f));
if (!paused) { if (!paused) {
ubos.vertexShader.lightPos.x = sin(glm::radians(timer * 360.0f)) * 1.5f; uniformDataVertexShader.lightPos.x = sin(glm::radians(timer * 360.0f)) * 1.5f;
ubos.vertexShader.lightPos.z = cos(glm::radians(timer * 360.0f)) * 1.5f; uniformDataVertexShader.lightPos.z = cos(glm::radians(timer * 360.0f)) * 1.5f;
} }
ubos.vertexShader.cameraPos = glm::vec4(camera.position, -1.0f) * -1.0f; uniformDataVertexShader.cameraPos = glm::vec4(camera.position, -1.0f) * -1.0f;
memcpy(uniformBuffers.vertexShader.mapped, &ubos.vertexShader, sizeof(ubos.vertexShader)); memcpy(uniformBuffers.vertexShader.mapped, &uniformDataVertexShader, sizeof(UniformDataVertexShader));
// Fragment shader // Fragment shader
memcpy(uniformBuffers.fragmentShader.mapped, &ubos.fragmentShader, sizeof(ubos.fragmentShader)); memcpy(uniformBuffers.fragmentShader.mapped, &uniformDataFragmentShader, sizeof(UniformDataFragmentShader));
} }
void draw() void draw()
@ -267,10 +253,8 @@ public:
VulkanExampleBase::prepare(); VulkanExampleBase::prepare();
loadAssets(); loadAssets();
prepareUniformBuffers(); prepareUniformBuffers();
setupDescriptorSetLayout(); setupDescriptors();
preparePipelines(); preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers(); buildCommandBuffers();
prepared = true; prepared = true;
} }
@ -279,17 +263,16 @@ public:
{ {
if (!prepared) if (!prepared)
return; return;
draw(); if (!paused || camera.updated) {
if (!paused || camera.updated)
{
updateUniformBuffers(); updateUniformBuffers();
} }
draw();
} }
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay) virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
{ {
if (overlay->header("Settings")) { if (overlay->header("Settings")) {
if (overlay->comboBox("Mode", &ubos.fragmentShader.mappingMode, mappingModes)) { if (overlay->comboBox("Mode", &uniformDataFragmentShader.mappingMode, mappingModes)) {
updateUniformBuffers(); updateUniformBuffers();
} }
} }