Code cleanup

This commit is contained in:
Sascha Willems 2024-01-21 11:09:15 +01:00
parent 91b85e41cf
commit 922dbd4827
4 changed files with 154 additions and 247 deletions

View file

@ -29,34 +29,34 @@ public:
vks::Buffer vsOffScreen;
} uniformBuffers;
struct UBO {
struct UniformData {
glm::mat4 projection;
glm::mat4 view;
glm::mat4 model;
glm::vec4 lightPos = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
} uboShared;
} uniformData;
struct {
VkPipeline debug;
VkPipeline shaded;
VkPipeline shadedOffscreen;
VkPipeline mirror;
VkPipeline debug{ VK_NULL_HANDLE };
VkPipeline shaded{ VK_NULL_HANDLE };
VkPipeline shadedOffscreen{ VK_NULL_HANDLE };
VkPipeline mirror{ VK_NULL_HANDLE };
} pipelines;
struct {
VkPipelineLayout textured;
VkPipelineLayout shaded;
VkPipelineLayout textured{ VK_NULL_HANDLE };
VkPipelineLayout shaded{ VK_NULL_HANDLE };
} pipelineLayouts;
struct {
VkDescriptorSet offscreen;
VkDescriptorSet mirror;
VkDescriptorSet model;
VkDescriptorSet offscreen{ VK_NULL_HANDLE };
VkDescriptorSet mirror{ VK_NULL_HANDLE };
VkDescriptorSet model{ VK_NULL_HANDLE };
} descriptorSets;
struct {
VkDescriptorSetLayout textured;
VkDescriptorSetLayout shaded;
VkDescriptorSetLayout textured{ VK_NULL_HANDLE };
VkDescriptorSetLayout shaded{ VK_NULL_HANDLE };
} descriptorSetLayouts;
// Framebuffer for offscreen rendering
@ -72,7 +72,7 @@ public:
VkRenderPass renderPass;
VkSampler sampler;
VkDescriptorImageInfo descriptor;
} offscreenPass;
} offscreenPass{};
glm::vec3 modelPosition = glm::vec3(0.0f, -1.0f, 0.0f);
glm::vec3 modelRotation = glm::vec3(0.0f);
@ -92,40 +92,39 @@ public:
~VulkanExample()
{
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
if (device) {
// Frame buffer
// Frame buffer
// Color attachment
vkDestroyImageView(device, offscreenPass.color.view, nullptr);
vkDestroyImage(device, offscreenPass.color.image, nullptr);
vkFreeMemory(device, offscreenPass.color.mem, nullptr);
// Color attachment
vkDestroyImageView(device, offscreenPass.color.view, nullptr);
vkDestroyImage(device, offscreenPass.color.image, nullptr);
vkFreeMemory(device, offscreenPass.color.mem, nullptr);
// Depth attachment
vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
vkDestroyImage(device, offscreenPass.depth.image, nullptr);
vkFreeMemory(device, offscreenPass.depth.mem, nullptr);
// Depth attachment
vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
vkDestroyImage(device, offscreenPass.depth.image, nullptr);
vkFreeMemory(device, offscreenPass.depth.mem, nullptr);
vkDestroyRenderPass(device, offscreenPass.renderPass, nullptr);
vkDestroySampler(device, offscreenPass.sampler, nullptr);
vkDestroyFramebuffer(device, offscreenPass.frameBuffer, nullptr);
vkDestroyRenderPass(device, offscreenPass.renderPass, nullptr);
vkDestroySampler(device, offscreenPass.sampler, nullptr);
vkDestroyFramebuffer(device, offscreenPass.frameBuffer, nullptr);
vkDestroyPipeline(device, pipelines.debug, nullptr);
vkDestroyPipeline(device, pipelines.shaded, nullptr);
vkDestroyPipeline(device, pipelines.shadedOffscreen, nullptr);
vkDestroyPipeline(device, pipelines.mirror, nullptr);
vkDestroyPipeline(device, pipelines.debug, nullptr);
vkDestroyPipeline(device, pipelines.shaded, nullptr);
vkDestroyPipeline(device, pipelines.shadedOffscreen, nullptr);
vkDestroyPipeline(device, pipelines.mirror, nullptr);
vkDestroyPipelineLayout(device, pipelineLayouts.textured, nullptr);
vkDestroyPipelineLayout(device, pipelineLayouts.shaded, nullptr);
vkDestroyPipelineLayout(device, pipelineLayouts.textured, nullptr);
vkDestroyPipelineLayout(device, pipelineLayouts.shaded, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.shaded, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textured, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.shaded, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textured, nullptr);
// Uniform buffers
uniformBuffers.vsShared.destroy();
uniformBuffers.vsMirror.destroy();
uniformBuffers.vsOffScreen.destroy();
// Uniform buffers
uniformBuffers.vsShared.destroy();
uniformBuffers.vsMirror.destroy();
uniformBuffers.vsOffScreen.destroy();
}
}
// Setup the offscreen framebuffer for rendering the mirrored scene
@ -402,111 +401,80 @@ public:
models.example.loadFromFile(getAssetPath() + "models/chinesedragon.gltf", vulkanDevice, queue, glTFLoadingFlags);
}
void setupDescriptorPool()
void setupDescriptors()
{
// Pool
std::vector<VkDescriptorPoolSize> poolSizes = {
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 6),
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 8)
};
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 5);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
}
void setupDescriptorSetLayout()
{
// Layout
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings;
VkDescriptorSetLayoutCreateInfo descriptorLayoutInfo;
VkPipelineLayoutCreateInfo pipelineLayoutInfo;
// Binding 0 : Vertex shader uniform buffer
setLayoutBindings.push_back(vks::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
VK_SHADER_STAGE_VERTEX_BIT,
0));
// Binding 1 : Fragment shader image sampler
setLayoutBindings.push_back(vks::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_SHADER_STAGE_FRAGMENT_BIT,
1));
// Shaded layouts (only use first layout binding)
descriptorLayoutInfo = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), 1);
// Shaded layout
setLayoutBindings = {
// Binding 0 : Vertex shader uniform buffer
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
};
descriptorLayoutInfo = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayoutInfo, nullptr, &descriptorSetLayouts.shaded));
pipelineLayoutInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.shaded, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayouts.shaded));
// Textured layouts (use all layout bindings)
descriptorLayoutInfo = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size()));
// Textured layouts
setLayoutBindings = {
// Binding 0 : Vertex shader uniform buffer
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
// Binding 1 : Fragment shader image sampler
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1)
};
descriptorLayoutInfo = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayoutInfo, nullptr, &descriptorSetLayouts.textured));
pipelineLayoutInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.textured, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayouts.textured));
}
void setupDescriptorSet()
{
// Sets
// Mirror plane descriptor set
VkDescriptorSetAllocateInfo allocInfo =
vks::initializers::descriptorSetAllocateInfo(
descriptorPool,
&descriptorSetLayouts.textured,
1);
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.textured, 1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.mirror));
std::vector<VkWriteDescriptorSet> writeDescriptorSets =
{
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
// Binding 0 : Vertex shader uniform buffer
vks::initializers::writeDescriptorSet(
descriptorSets.mirror,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
0,
&uniformBuffers.vsMirror.descriptor),
vks::initializers::writeDescriptorSet(descriptorSets.mirror, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.vsMirror.descriptor),
// Binding 1 : Fragment shader texture sampler
vks::initializers::writeDescriptorSet(
descriptorSets.mirror,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1,
&offscreenPass.descriptor),
vks::initializers::writeDescriptorSet(descriptorSets.mirror, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &offscreenPass.descriptor),
};
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr);
// Shaded descriptor sets
allocInfo.pSetLayouts = &descriptorSetLayouts.shaded;
allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.shaded, 1);
// Model
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.model));
std::vector<VkWriteDescriptorSet> modelWriteDescriptorSets =
{
std::vector<VkWriteDescriptorSet> modelWriteDescriptorSets = {
// Binding 0 : Vertex shader uniform buffer
vks::initializers::writeDescriptorSet(
descriptorSets.model,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
0,
&uniformBuffers.vsShared.descriptor)
vks::initializers::writeDescriptorSet(descriptorSets.model, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.vsShared.descriptor)
};
vkUpdateDescriptorSets(device, static_cast<uint32_t>(modelWriteDescriptorSets.size()), modelWriteDescriptorSets.data(), 0, nullptr);
// Offscreen
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.offscreen));
std::vector<VkWriteDescriptorSet> offScreenWriteDescriptorSets =
{
std::vector<VkWriteDescriptorSet> offScreenWriteDescriptorSets = {
// Binding 0 : Vertex shader uniform buffer
vks::initializers::writeDescriptorSet(
descriptorSets.offscreen,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
0,
&uniformBuffers.vsOffScreen.descriptor)
vks::initializers::writeDescriptorSet(descriptorSets.offscreen, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.vsOffScreen.descriptor)
};
vkUpdateDescriptorSets(device, static_cast<uint32_t>(offScreenWriteDescriptorSets.size()), offScreenWriteDescriptorSets.data(), 0, nullptr);
}
void preparePipelines()
{
// Layouts
VkPipelineLayoutCreateInfo pipelineLayoutInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.shaded, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayouts.shaded));
pipelineLayoutInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.textured, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayouts.textured));
// Pipelines
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_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE,0);
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
@ -562,26 +530,11 @@ public:
void prepareUniformBuffers()
{
// Mesh vertex shader uniform buffer block
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.vsShared,
sizeof(uboShared)));
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.vsShared, sizeof(UniformData)));
// Mirror plane vertex shader uniform buffer block
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.vsMirror,
sizeof(uboShared)));
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.vsMirror, sizeof(UniformData)));
// Offscreen vertex shader uniform buffer block
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.vsOffScreen,
sizeof(uboShared)));
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.vsOffScreen, sizeof(UniformData)));
// Map persistent
VK_CHECK_RESULT(uniformBuffers.vsShared.map());
VK_CHECK_RESULT(uniformBuffers.vsMirror.map());
@ -593,43 +546,29 @@ public:
void updateUniformBuffers()
{
uboShared.projection = camera.matrices.perspective;
uboShared.view = camera.matrices.view;
uniformData.projection = camera.matrices.perspective;
uniformData.view = camera.matrices.view;
// Model
uboShared.model = glm::mat4(1.0f);
uboShared.model = glm::rotate(uboShared.model, glm::radians(modelRotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
uboShared.model = glm::translate(uboShared.model, modelPosition);
memcpy(uniformBuffers.vsShared.mapped, &uboShared, sizeof(uboShared));
uniformData.model = glm::mat4(1.0f);
uniformData.model = glm::rotate(uniformData.model, glm::radians(modelRotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
uniformData.model = glm::translate(uniformData.model, modelPosition);
memcpy(uniformBuffers.vsShared.mapped, &uniformData, sizeof(UniformData));
// Mirror
uboShared.model = glm::mat4(1.0f);
memcpy(uniformBuffers.vsMirror.mapped, &uboShared, sizeof(uboShared));
uniformData.model = glm::mat4(1.0f);
memcpy(uniformBuffers.vsMirror.mapped, &uniformData, sizeof(UniformData));
}
void updateUniformBufferOffscreen()
{
uboShared.projection = camera.matrices.perspective;
uboShared.view = camera.matrices.view;
uboShared.model = glm::mat4(1.0f);
uboShared.model = glm::rotate(uboShared.model, glm::radians(modelRotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
uboShared.model = glm::scale(uboShared.model, glm::vec3(1.0f, -1.0f, 1.0f));
uboShared.model = glm::translate(uboShared.model, modelPosition);
memcpy(uniformBuffers.vsOffScreen.mapped, &uboShared, sizeof(uboShared));
}
void draw()
{
VulkanExampleBase::prepareFrame();
// Command buffer to be submitted 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();
uniformData.projection = camera.matrices.perspective;
uniformData.view = camera.matrices.view;
uniformData.model = glm::mat4(1.0f);
uniformData.model = glm::rotate(uniformData.model, glm::radians(modelRotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
uniformData.model = glm::scale(uniformData.model, glm::vec3(1.0f, -1.0f, 1.0f));
uniformData.model = glm::translate(uniformData.model, modelPosition);
memcpy(uniformBuffers.vsOffScreen.mapped, &uniformData, sizeof(UniformData));
}
void prepare()
@ -638,14 +577,21 @@ public:
loadAssets();
prepareOffscreen();
prepareUniformBuffers();
setupDescriptorSetLayout();
setupDescriptors();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
prepared = true;
}
void draw()
{
VulkanExampleBase::prepareFrame();
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
VulkanExampleBase::submitFrame();
}
virtual void render()
{
if (!prepared)