Code cleanup, simplification

This commit is contained in:
Sascha Willems 2024-01-20 13:05:02 +01:00
parent 3dd195fe35
commit b92e210bbe
8 changed files with 285 additions and 396 deletions

View file

@ -30,27 +30,25 @@ public:
} matrices;
struct {
VkPipeline phong;
VkPipeline starsphere;
VkPipeline phong{ VK_NULL_HANDLE };
VkPipeline starsphere{ VK_NULL_HANDLE };
} pipelines;
VkPipelineLayout pipelineLayout;
VkCommandBuffer primaryCommandBuffer;
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
VkCommandBuffer primaryCommandBuffer{ VK_NULL_HANDLE };
// Secondary scene command buffers used to store backdrop and user interface
struct SecondaryCommandBuffers {
VkCommandBuffer background;
VkCommandBuffer ui;
VkCommandBuffer background{ VK_NULL_HANDLE };
VkCommandBuffer ui{ VK_NULL_HANDLE };
} secondaryCommandBuffers;
// Number of animated objects to be renderer
// by using threads and secondary command buffers
uint32_t numObjectsPerThread;
uint32_t numObjectsPerThread{ 0 };
// Multi threaded stuff
// Max. number of concurrent threads
uint32_t numThreads;
uint32_t numThreads{ 0 };
// Use push constants to update shader
// parameters on a per-thread base
@ -72,7 +70,7 @@ public:
};
struct ThreadData {
VkCommandPool commandPool;
VkCommandPool commandPool{ VK_NULL_HANDLE };
// One command buffer per render object
std::vector<VkCommandBuffer> commandBuffer;
// One push constant block per render object
@ -86,7 +84,7 @@ public:
// Fence to wait for all command buffers to finish before
// presenting to the swap chain
VkFence renderFence = {};
VkFence renderFence{ VK_NULL_HANDLE };
// View frustum for culling invisible objects
vks::Frustum frustum;
@ -116,19 +114,16 @@ public:
~VulkanExample()
{
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
vkDestroyPipeline(device, pipelines.phong, nullptr);
vkDestroyPipeline(device, pipelines.starsphere, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
for (auto& thread : threadData) {
vkFreeCommandBuffers(device, thread.commandPool, static_cast<uint32_t>(thread.commandBuffer.size()), thread.commandBuffer.data());
vkDestroyCommandPool(device, thread.commandPool, nullptr);
if (device) {
vkDestroyPipeline(device, pipelines.phong, nullptr);
vkDestroyPipeline(device, pipelines.starsphere, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
for (auto& thread : threadData) {
vkFreeCommandBuffers(device, thread.commandPool, static_cast<uint32_t>(thread.commandBuffer.size()), thread.commandBuffer.data());
vkDestroyCommandPool(device, thread.commandPool, nullptr);
}
vkDestroyFence(device, renderFence, nullptr);
}
vkDestroyFence(device, renderFence, nullptr);
}
float rnd(float range)
@ -412,27 +407,18 @@ public:
models.starSphere.loadFromFile(getAssetPath() + "models/sphere.gltf", vulkanDevice, queue, glTFLoadingFlags);
}
void setupPipelineLayout()
void preparePipelines()
{
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
vks::initializers::pipelineLayoutCreateInfo(nullptr, 0);
// Layout
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(nullptr, 0);
// Push constants for model matrices
VkPushConstantRange pushConstantRange =
vks::initializers::pushConstantRange(
VK_SHADER_STAGE_VERTEX_BIT,
sizeof(ThreadPushConstantBlock),
0);
VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(ThreadPushConstantBlock), 0);
// Push constant ranges are part of the pipeline layout
pPipelineLayoutCreateInfo.pushConstantRangeCount = 1;
pPipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
}
void preparePipelines()
{
// 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);
@ -476,6 +462,19 @@ public:
frustum.update(matrices.projection * matrices.view);
}
void prepare()
{
VulkanExampleBase::prepare();
// Create a fence for synchronization
VkFenceCreateInfo fenceCreateInfo = vks::initializers::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT);
vkCreateFence(device, &fenceCreateInfo, nullptr, &renderFence);
loadAssets();
preparePipelines();
prepareMultiThreadedRenderer();
updateMatrices();
prepared = true;
}
void draw()
{
// Wait for fence to signal that all command buffers are ready
@ -498,34 +497,12 @@ public:
VulkanExampleBase::submitFrame();
}
void prepare()
{
VulkanExampleBase::prepare();
// Create a fence for synchronization
VkFenceCreateInfo fenceCreateInfo = vks::initializers::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT);
vkCreateFence(device, &fenceCreateInfo, nullptr, &renderFence);
loadAssets();
setupPipelineLayout();
preparePipelines();
prepareMultiThreadedRenderer();
updateMatrices();
prepared = true;
}
virtual void render()
{
if (!prepared)
return;
draw();
if (camera.updated)
{
updateMatrices();
}
}
virtual void viewChanged()
{
updateMatrices();
draw();
}
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)