Skip to content

Commit

Permalink
Code cleanup, simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaWillems committed Jan 20, 2024
1 parent 3dd195f commit b92e210
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 401 deletions.
263 changes: 100 additions & 163 deletions examples/hdr/hdr.cpp

Large diffs are not rendered by default.

99 changes: 38 additions & 61 deletions examples/multithreading/multithreading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,25 @@ class VulkanExample : public VulkanExampleBase
} 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
Expand All @@ -72,7 +70,7 @@ class VulkanExample : public VulkanExampleBase
};

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
Expand All @@ -86,7 +84,7 @@ class VulkanExample : public VulkanExampleBase

// 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;
Expand Down Expand Up @@ -116,19 +114,16 @@ class VulkanExample : public VulkanExampleBase

~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)
Expand Down Expand Up @@ -412,27 +407,18 @@ class VulkanExample : public VulkanExampleBase
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);
Expand Down Expand Up @@ -476,6 +462,19 @@ class VulkanExample : public VulkanExampleBase
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
Expand All @@ -498,34 +497,12 @@ class VulkanExample : public VulkanExampleBase
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)
Expand Down
Loading

0 comments on commit b92e210

Please sign in to comment.