From 9ccb7cbd54300379bff4e63c692aefa7cf357bc2 Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Sat, 6 Jan 2024 18:00:43 +0100 Subject: [PATCH] Code cleanup --- examples/gltfloading/gltfloading.cpp | 77 ++++++++----------- .../gltfscenerendering/gltfscenerendering.cpp | 50 +++++------- .../gltfscenerendering/gltfscenerendering.h | 11 ++- examples/gltfskinning/gltfskinning.cpp | 53 ++++++------- examples/gltfskinning/gltfskinning.h | 15 ++-- 5 files changed, 88 insertions(+), 118 deletions(-) diff --git a/examples/gltfloading/gltfloading.cpp b/examples/gltfloading/gltfloading.cpp index 1cc2808e..827f7dbb 100644 --- a/examples/gltfloading/gltfloading.cpp +++ b/examples/gltfloading/gltfloading.cpp @@ -1,7 +1,7 @@ /* * Vulkan Example - glTF scene loading and rendering * -* Copyright (C) 2020-2022 by Sascha Willems - www.saschawillems.de +* Copyright (C) 2020-2023 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) */ @@ -395,16 +395,16 @@ public: } shaderData; struct Pipelines { - VkPipeline solid; - VkPipeline wireframe = VK_NULL_HANDLE; + VkPipeline solid{ VK_NULL_HANDLE }; + VkPipeline wireframe{ VK_NULL_HANDLE }; } pipelines; - VkPipelineLayout pipelineLayout; - VkDescriptorSet descriptorSet; + VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE }; + VkDescriptorSet descriptorSet{ VK_NULL_HANDLE }; struct DescriptorSetLayouts { - VkDescriptorSetLayout matrices; - VkDescriptorSetLayout textures; + VkDescriptorSetLayout matrices{ VK_NULL_HANDLE }; + VkDescriptorSetLayout textures{ VK_NULL_HANDLE }; } descriptorSetLayouts; VulkanExample() : VulkanExampleBase() @@ -419,18 +419,16 @@ public: ~VulkanExample() { - // Clean up used Vulkan resources - // Note : Inherited destructor cleans up resources stored in base class - vkDestroyPipeline(device, pipelines.solid, nullptr); - if (pipelines.wireframe != VK_NULL_HANDLE) { - vkDestroyPipeline(device, pipelines.wireframe, nullptr); + if (device) { + vkDestroyPipeline(device, pipelines.solid, nullptr); + if (pipelines.wireframe != VK_NULL_HANDLE) { + vkDestroyPipeline(device, pipelines.wireframe, nullptr); + } + vkDestroyPipelineLayout(device, pipelineLayout, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.matrices, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr); + shaderData.buffer.destroy(); } - - vkDestroyPipelineLayout(device, pipelineLayout, nullptr); - vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.matrices, nullptr); - vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr); - - shaderData.buffer.destroy(); } virtual void getEnabledFeatures() @@ -446,7 +444,6 @@ public: VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo(); VkClearValue clearValues[2]; - clearValues[0].color = defaultClearColor; clearValues[0].color = { { 0.25f, 0.25f, 0.25f, 1.0f } };; clearValues[1].depthStencil = { 1.0f, 0 }; @@ -617,15 +614,6 @@ public: // Descriptor set layout for passing material textures setLayoutBinding = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.textures)); - // Pipeline layout using both descriptor sets (set 0 = matrices, set 1 = material) - std::array setLayouts = { descriptorSetLayouts.matrices, descriptorSetLayouts.textures }; - VkPipelineLayoutCreateInfo pipelineLayoutCI= vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast(setLayouts.size())); - // We will use push constants to push the local matrices of a primitive to the vertex shader - VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), 0); - // Push constant ranges are part of the pipeline layout - pipelineLayoutCI.pushConstantRangeCount = 1; - pipelineLayoutCI.pPushConstantRanges = &pushConstantRange; - VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelineLayout)); // Descriptor set for scene matrices VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.matrices, 1); @@ -643,6 +631,18 @@ public: void preparePipelines() { + // Layout + // The pipeline layout uses both descriptor sets (set 0 = matrices, set 1 = material) + std::array setLayouts = { descriptorSetLayouts.matrices, descriptorSetLayouts.textures }; + VkPipelineLayoutCreateInfo pipelineLayoutCI = vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast(setLayouts.size())); + // We will use push constants to push the local matrices of a primitive to the vertex shader + VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), 0); + // Push constant ranges are part of the pipeline layout + pipelineLayoutCI.pushConstantRangeCount = 1; + pipelineLayoutCI.pPushConstantRanges = &pushConstantRange; + VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelineLayout)); + + // Pipeline VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE); VkPipelineRasterizationStateCreateInfo rasterizationStateCI = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0); VkPipelineColorBlendAttachmentState blendAttachmentStateCI = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE); @@ -651,7 +651,7 @@ public: VkPipelineViewportStateCreateInfo viewportStateCI = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0); VkPipelineMultisampleStateCreateInfo multisampleStateCI = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0); const std::vector dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; - VkPipelineDynamicStateCreateInfo dynamicStateCI = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(), static_cast(dynamicStateEnables.size()), 0); + VkPipelineDynamicStateCreateInfo dynamicStateCI = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables); // Vertex input bindings and attributes const std::vector vertexInputBindings = { vks::initializers::vertexInputBindingDescription(0, sizeof(VulkanglTFModel::Vertex), VK_VERTEX_INPUT_RATE_VERTEX), @@ -700,16 +700,9 @@ public: void prepareUniformBuffers() { // 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, - &shaderData.buffer, - sizeof(shaderData.values))); - + VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &shaderData.buffer, sizeof(shaderData.values))); // Map persistent VK_CHECK_RESULT(shaderData.buffer.map()); - - updateUniformBuffers(); } void updateUniformBuffers() @@ -732,16 +725,10 @@ public: } virtual void render() - { - renderFrame(); - if (camera.updated) { - updateUniformBuffers(); - } - } - - virtual void viewChanged() { updateUniformBuffers(); + renderFrame(); + } virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay) diff --git a/examples/gltfscenerendering/gltfscenerendering.cpp b/examples/gltfscenerendering/gltfscenerendering.cpp index 648126ed..7faef44c 100644 --- a/examples/gltfscenerendering/gltfscenerendering.cpp +++ b/examples/gltfscenerendering/gltfscenerendering.cpp @@ -1,7 +1,7 @@ /* * Vulkan Example - Scene rendering * -* Copyright (C) 2020-202- by Sascha Willems - www.saschawillems.de +* Copyright (C) 2020-2023 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * @@ -296,10 +296,12 @@ VulkanExample::VulkanExample() : VulkanExampleBase() VulkanExample::~VulkanExample() { - vkDestroyPipelineLayout(device, pipelineLayout, nullptr); - vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.matrices, nullptr); - vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr); - shaderData.buffer.destroy(); + if (device) { + vkDestroyPipelineLayout(device, pipelineLayout, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.matrices, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr); + shaderData.buffer.destroy(); + } } void VulkanExample::getEnabledFeatures() @@ -501,16 +503,6 @@ void VulkanExample::setupDescriptors() descriptorSetLayoutCI.bindingCount = 2; VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.textures)); - // Pipeline layout using both descriptor sets (set 0 = matrices, set 1 = material) - std::array setLayouts = { descriptorSetLayouts.matrices, descriptorSetLayouts.textures }; - VkPipelineLayoutCreateInfo pipelineLayoutCI = vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast(setLayouts.size())); - // We will use push constants to push the local matrices of a primitive to the vertex shader - VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), 0); - // Push constant ranges are part of the pipeline layout - pipelineLayoutCI.pushConstantRangeCount = 1; - pipelineLayoutCI.pPushConstantRanges = &pushConstantRange; - VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelineLayout)); - // Descriptor set for scene matrices VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.matrices, 1); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet)); @@ -533,6 +525,18 @@ void VulkanExample::setupDescriptors() void VulkanExample::preparePipelines() { + // Layout + // Pipeline layout uses both descriptor sets (set 0 = matrices, set 1 = material) + std::array setLayouts = { descriptorSetLayouts.matrices, descriptorSetLayouts.textures }; + VkPipelineLayoutCreateInfo pipelineLayoutCI = vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast(setLayouts.size())); + // We will use push constants to push the local matrices of a primitive to the vertex shader + VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), 0); + // Push constant ranges are part of the pipeline layout + pipelineLayoutCI.pushConstantRangeCount = 1; + pipelineLayoutCI.pPushConstantRanges = &pushConstantRange; + VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelineLayout)); + + // Pipelines VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE); VkPipelineRasterizationStateCreateInfo rasterizationStateCI = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0); VkPipelineColorBlendAttachmentState blendAttachmentStateCI = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE); @@ -599,13 +603,8 @@ void VulkanExample::preparePipelines() void VulkanExample::prepareUniformBuffers() { - VK_CHECK_RESULT(vulkanDevice->createBuffer( - VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - &shaderData.buffer, - sizeof(shaderData.values))); + VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &shaderData.buffer, sizeof(shaderData.values))); VK_CHECK_RESULT(shaderData.buffer.map()); - updateUniformBuffers(); } void VulkanExample::updateUniformBuffers() @@ -628,16 +627,9 @@ void VulkanExample::prepare() } void VulkanExample::render() -{ - renderFrame(); - if (camera.updated) { - updateUniformBuffers(); - } -} - -void VulkanExample::viewChanged() { updateUniformBuffers(); + renderFrame(); } void VulkanExample::OnUpdateUIOverlay(vks::UIOverlay* overlay) diff --git a/examples/gltfscenerendering/gltfscenerendering.h b/examples/gltfscenerendering/gltfscenerendering.h index 2afc4045..7e71bbfe 100644 --- a/examples/gltfscenerendering/gltfscenerendering.h +++ b/examples/gltfscenerendering/gltfscenerendering.h @@ -1,7 +1,7 @@ /* * Vulkan Example - Scene rendering * -* Copyright (C) 2020-2022 by Sascha Willems - www.saschawillems.de +* Copyright (C) 2020-2023 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * @@ -146,12 +146,12 @@ public: } values; } shaderData; - VkPipelineLayout pipelineLayout; - VkDescriptorSet descriptorSet; + VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE }; + VkDescriptorSet descriptorSet{ VK_NULL_HANDLE }; struct DescriptorSetLayouts { - VkDescriptorSetLayout matrices; - VkDescriptorSetLayout textures; + VkDescriptorSetLayout matrices{ VK_NULL_HANDLE }; + VkDescriptorSetLayout textures{ VK_NULL_HANDLE }; } descriptorSetLayouts; VulkanExample(); @@ -166,6 +166,5 @@ public: void updateUniformBuffers(); void prepare(); virtual void render(); - virtual void viewChanged(); virtual void OnUpdateUIOverlay(vks::UIOverlay* overlay); }; diff --git a/examples/gltfskinning/gltfskinning.cpp b/examples/gltfskinning/gltfskinning.cpp index 1c81fce3..6bade0c6 100644 --- a/examples/gltfskinning/gltfskinning.cpp +++ b/examples/gltfskinning/gltfskinning.cpp @@ -1,7 +1,7 @@ /* * Vulkan Example - glTF skinned animation * -* Copyright (C) 2020-2021 by Sascha Willems - www.saschawillems.de +* Copyright (C) 2020-2023 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) */ @@ -849,23 +849,6 @@ void VulkanExample::setupDescriptors() setLayoutBinding = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.jointMatrices)); - // The pipeline layout uses three sets: - // Set 0 = Scene matrices (VS) - // Set 1 = Joint matrices (VS) - // Set 2 = Material texture (FS) - std::array setLayouts = { - descriptorSetLayouts.matrices, - descriptorSetLayouts.jointMatrices, - descriptorSetLayouts.textures}; - VkPipelineLayoutCreateInfo pipelineLayoutCI = vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast(setLayouts.size())); - - // We will use push constants to push the local matrices of a primitive to the vertex shader - VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), 0); - // Push constant ranges are part of the pipeline layout - pipelineLayoutCI.pushConstantRangeCount = 1; - pipelineLayoutCI.pPushConstantRanges = &pushConstantRange; - VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelineLayout)); - // Descriptor set for scene matrices VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.matrices, 1); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet)); @@ -893,6 +876,25 @@ void VulkanExample::setupDescriptors() void VulkanExample::preparePipelines() { + // Layout + // The pipeline layout uses three sets: + // Set 0 = Scene matrices (VS) + // Set 1 = Joint matrices (VS) + // Set 2 = Material texture (FS) + std::array setLayouts = { + descriptorSetLayouts.matrices, + descriptorSetLayouts.jointMatrices, + descriptorSetLayouts.textures }; + VkPipelineLayoutCreateInfo pipelineLayoutCI = vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast(setLayouts.size())); + + // We will use push constants to push the local matrices of a primitive to the vertex shader + VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), 0); + // Push constant ranges are part of the pipeline layout + pipelineLayoutCI.pushConstantRangeCount = 1; + pipelineLayoutCI.pPushConstantRanges = &pushConstantRange; + VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelineLayout)); + + // Pipeline VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE); VkPipelineRasterizationStateCreateInfo rasterizationStateCI = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0); VkPipelineColorBlendAttachmentState blendAttachmentStateCI = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE); @@ -982,21 +984,12 @@ void VulkanExample::prepare() void VulkanExample::render() { - renderFrame(); - if (camera.updated) - { - updateUniformBuffers(); - } + updateUniformBuffers(); // POI: Advance animation - if (!paused) - { + if (!paused) { glTFModel.updateAnimation(frameTimer); } -} - -void VulkanExample::viewChanged() -{ - updateUniformBuffers(); + renderFrame(); } void VulkanExample::OnUpdateUIOverlay(vks::UIOverlay *overlay) diff --git a/examples/gltfskinning/gltfskinning.h b/examples/gltfskinning/gltfskinning.h index a8e08942..534b2667 100644 --- a/examples/gltfskinning/gltfskinning.h +++ b/examples/gltfskinning/gltfskinning.h @@ -1,7 +1,7 @@ /* * Vulkan Example - glTF skinned animation * -* Copyright (C) 2020 by Sascha Willems - www.saschawillems.de +* Copyright (C) 2020-2023 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) */ @@ -205,17 +205,17 @@ class VulkanExample : public VulkanExampleBase VkPipelineLayout pipelineLayout; struct Pipelines { - VkPipeline solid; - VkPipeline wireframe = VK_NULL_HANDLE; + VkPipeline solid{ VK_NULL_HANDLE }; + VkPipeline wireframe{ VK_NULL_HANDLE }; } pipelines; struct DescriptorSetLayouts { - VkDescriptorSetLayout matrices; - VkDescriptorSetLayout textures; - VkDescriptorSetLayout jointMatrices; + VkDescriptorSetLayout matrices{ VK_NULL_HANDLE }; + VkDescriptorSetLayout textures{ VK_NULL_HANDLE }; + VkDescriptorSetLayout jointMatrices{ VK_NULL_HANDLE }; } descriptorSetLayouts; - VkDescriptorSet descriptorSet; + VkDescriptorSet descriptorSet{ VK_NULL_HANDLE }; VulkanglTFModel glTFModel; @@ -231,6 +231,5 @@ class VulkanExample : public VulkanExampleBase void updateUniformBuffers(); void prepare(); virtual void render(); - virtual void viewChanged(); virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay); };