Code cleanup
This commit is contained in:
parent
24a13f18dc
commit
99d6cce6b5
3 changed files with 71 additions and 136 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Vulkan Example - Animated gears using multiple uniform buffers
|
* Vulkan Example - Animated gears using multiple uniform buffers
|
||||||
*
|
*
|
||||||
* Copyright (C) 2016 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)
|
||||||
*/
|
*/
|
||||||
|
|
@ -9,7 +9,6 @@
|
||||||
#include "vulkangear.h"
|
#include "vulkangear.h"
|
||||||
#include "vulkanexamplebase.h"
|
#include "vulkanexamplebase.h"
|
||||||
|
|
||||||
#define VERTEX_BUFFER_BIND_ID 0
|
|
||||||
#define ENABLE_VALIDATION false
|
#define ENABLE_VALIDATION false
|
||||||
|
|
||||||
class VulkanExample : public VulkanExampleBase
|
class VulkanExample : public VulkanExampleBase
|
||||||
|
|
@ -21,14 +20,11 @@ public:
|
||||||
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
||||||
} vertices;
|
} vertices;
|
||||||
|
|
||||||
struct {
|
|
||||||
VkPipeline solid;
|
|
||||||
} pipelines;
|
|
||||||
|
|
||||||
std::vector<VulkanGear*> gears;
|
std::vector<VulkanGear*> gears;
|
||||||
|
|
||||||
VkPipelineLayout pipelineLayout;
|
VkPipeline pipeline{ VK_NULL_HANDLE };
|
||||||
VkDescriptorSetLayout descriptorSetLayout;
|
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
|
||||||
|
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
|
||||||
|
|
||||||
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
||||||
{
|
{
|
||||||
|
|
@ -44,7 +40,7 @@ public:
|
||||||
{
|
{
|
||||||
// Clean up used Vulkan resources
|
// Clean up used Vulkan resources
|
||||||
// Note : Inherited destructor cleans up resources stored in base class
|
// Note : Inherited destructor cleans up resources stored in base class
|
||||||
vkDestroyPipeline(device, pipelines.solid, nullptr);
|
vkDestroyPipeline(device, pipeline, nullptr);
|
||||||
|
|
||||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
||||||
|
|
@ -86,7 +82,7 @@ public:
|
||||||
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
|
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
|
||||||
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
||||||
|
|
||||||
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.solid);
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||||||
|
|
||||||
for (auto& gear : gears)
|
for (auto& gear : gears)
|
||||||
{
|
{
|
||||||
|
|
@ -144,7 +140,7 @@ public:
|
||||||
vertices.bindingDescriptions.resize(1);
|
vertices.bindingDescriptions.resize(1);
|
||||||
vertices.bindingDescriptions[0] =
|
vertices.bindingDescriptions[0] =
|
||||||
vks::initializers::vertexInputBindingDescription(
|
vks::initializers::vertexInputBindingDescription(
|
||||||
VERTEX_BUFFER_BIND_ID,
|
0,
|
||||||
sizeof(Vertex),
|
sizeof(Vertex),
|
||||||
VK_VERTEX_INPUT_RATE_VERTEX);
|
VK_VERTEX_INPUT_RATE_VERTEX);
|
||||||
|
|
||||||
|
|
@ -154,21 +150,21 @@ public:
|
||||||
// Location 0 : Position
|
// Location 0 : Position
|
||||||
vertices.attributeDescriptions[0] =
|
vertices.attributeDescriptions[0] =
|
||||||
vks::initializers::vertexInputAttributeDescription(
|
vks::initializers::vertexInputAttributeDescription(
|
||||||
VERTEX_BUFFER_BIND_ID,
|
0,
|
||||||
0,
|
0,
|
||||||
VK_FORMAT_R32G32B32_SFLOAT,
|
VK_FORMAT_R32G32B32_SFLOAT,
|
||||||
0);
|
0);
|
||||||
// Location 1 : Normal
|
// Location 1 : Normal
|
||||||
vertices.attributeDescriptions[1] =
|
vertices.attributeDescriptions[1] =
|
||||||
vks::initializers::vertexInputAttributeDescription(
|
vks::initializers::vertexInputAttributeDescription(
|
||||||
VERTEX_BUFFER_BIND_ID,
|
0,
|
||||||
1,
|
1,
|
||||||
VK_FORMAT_R32G32B32_SFLOAT,
|
VK_FORMAT_R32G32B32_SFLOAT,
|
||||||
sizeof(float) * 3);
|
sizeof(float) * 3);
|
||||||
// Location 2 : Color
|
// Location 2 : Color
|
||||||
vertices.attributeDescriptions[2] =
|
vertices.attributeDescriptions[2] =
|
||||||
vks::initializers::vertexInputAttributeDescription(
|
vks::initializers::vertexInputAttributeDescription(
|
||||||
VERTEX_BUFFER_BIND_ID,
|
0,
|
||||||
2,
|
2,
|
||||||
VK_FORMAT_R32G32B32_SFLOAT,
|
VK_FORMAT_R32G32B32_SFLOAT,
|
||||||
sizeof(float) * 6);
|
sizeof(float) * 6);
|
||||||
|
|
@ -183,18 +179,10 @@ public:
|
||||||
void setupDescriptorPool()
|
void setupDescriptorPool()
|
||||||
{
|
{
|
||||||
// One UBO for each gear
|
// One UBO for each gear
|
||||||
std::vector<VkDescriptorPoolSize> poolSizes =
|
std::vector<VkDescriptorPoolSize> poolSizes = {
|
||||||
{
|
|
||||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3),
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3),
|
||||||
};
|
};
|
||||||
|
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, static_cast<uint32_t>(gears.size()));
|
||||||
VkDescriptorPoolCreateInfo descriptorPoolInfo =
|
|
||||||
vks::initializers::descriptorPoolCreateInfo(
|
|
||||||
static_cast<uint32_t>(poolSizes.size()),
|
|
||||||
poolSizes.data(),
|
|
||||||
// Three descriptor sets (for each gear)
|
|
||||||
3);
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -203,25 +191,13 @@ public:
|
||||||
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings =
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings =
|
||||||
{
|
{
|
||||||
// Binding 0 : Vertex shader uniform buffer
|
// Binding 0 : Vertex shader uniform buffer
|
||||||
vks::initializers::descriptorSetLayoutBinding(
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0)
|
||||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
||||||
VK_SHADER_STAGE_VERTEX_BIT,
|
|
||||||
0)
|
|
||||||
};
|
};
|
||||||
|
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, &descriptorSetLayout));
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
||||||
|
|
||||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
|
||||||
vks::initializers::pipelineLayoutCreateInfo(
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
||||||
&descriptorSetLayout,
|
|
||||||
1);
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupDescriptorSets()
|
void setupDescriptorSets()
|
||||||
|
|
@ -288,12 +264,7 @@ public:
|
||||||
shaderStages[0] = loadShader(getShadersPath() + "gears/gears.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
shaderStages[0] = loadShader(getShadersPath() + "gears/gears.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
||||||
shaderStages[1] = loadShader(getShadersPath() + "gears/gears.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
shaderStages[1] = loadShader(getShadersPath() + "gears/gears.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||||
|
|
||||||
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
|
VkGraphicsPipelineCreateInfo pipelineCreateInfo = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0);
|
||||||
vks::initializers::pipelineCreateInfo(
|
|
||||||
pipelineLayout,
|
|
||||||
renderPass,
|
|
||||||
0);
|
|
||||||
|
|
||||||
pipelineCreateInfo.pVertexInputState = &vertices.inputState;
|
pipelineCreateInfo.pVertexInputState = &vertices.inputState;
|
||||||
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
|
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
|
||||||
pipelineCreateInfo.pRasterizationState = &rasterizationState;
|
pipelineCreateInfo.pRasterizationState = &rasterizationState;
|
||||||
|
|
@ -305,7 +276,7 @@ public:
|
||||||
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
|
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
|
||||||
pipelineCreateInfo.pStages = shaderStages.data();
|
pipelineCreateInfo.pStages = shaderStages.data();
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid));
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipeline));
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateUniformBuffers()
|
void updateUniformBuffers()
|
||||||
|
|
@ -319,14 +290,9 @@ public:
|
||||||
void draw()
|
void draw()
|
||||||
{
|
{
|
||||||
VulkanExampleBase::prepareFrame();
|
VulkanExampleBase::prepareFrame();
|
||||||
|
|
||||||
// Command buffer to be submitted to the queue
|
|
||||||
submitInfo.commandBufferCount = 1;
|
submitInfo.commandBufferCount = 1;
|
||||||
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
||||||
|
|
||||||
// Submit to queue
|
|
||||||
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
||||||
|
|
||||||
VulkanExampleBase::submitFrame();
|
VulkanExampleBase::submitFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -347,13 +313,8 @@ public:
|
||||||
{
|
{
|
||||||
if (!prepared)
|
if (!prepared)
|
||||||
return;
|
return;
|
||||||
vkDeviceWaitIdle(device);
|
updateUniformBuffers();
|
||||||
draw();
|
draw();
|
||||||
vkDeviceWaitIdle(device);
|
|
||||||
if (!paused)
|
|
||||||
{
|
|
||||||
updateUniformBuffers();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void viewChanged()
|
virtual void viewChanged()
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Vulkan Example - Animated gears using multiple uniform buffers
|
* Vulkan Example - Animated gears using multiple uniform buffers
|
||||||
*
|
*
|
||||||
* See readme.md for details
|
|
||||||
*
|
|
||||||
* Copyright (C) 2016-2023 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)
|
||||||
|
|
@ -169,87 +167,65 @@ void VulkanGear::generate(GearInfo *gearinfo, VkQueue queue)
|
||||||
size_t vertexBufferSize = vBuffer.size() * sizeof(Vertex);
|
size_t vertexBufferSize = vBuffer.size() * sizeof(Vertex);
|
||||||
size_t indexBufferSize = iBuffer.size() * sizeof(uint32_t);
|
size_t indexBufferSize = iBuffer.size() * sizeof(uint32_t);
|
||||||
|
|
||||||
bool useStaging = true;
|
vks::Buffer vertexStaging, indexStaging;
|
||||||
|
|
||||||
if (useStaging)
|
// Create staging buffers
|
||||||
{
|
// Vertex data
|
||||||
vks::Buffer vertexStaging, indexStaging;
|
vulkanDevice->createBuffer(
|
||||||
|
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||||
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
||||||
|
&vertexStaging,
|
||||||
|
vertexBufferSize,
|
||||||
|
vBuffer.data());
|
||||||
|
// Index data
|
||||||
|
vulkanDevice->createBuffer(
|
||||||
|
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||||
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
||||||
|
&indexStaging,
|
||||||
|
indexBufferSize,
|
||||||
|
iBuffer.data());
|
||||||
|
|
||||||
// Create staging buffers
|
// Create device local buffers
|
||||||
// Vertex data
|
// Vertex buffer
|
||||||
vulkanDevice->createBuffer(
|
vulkanDevice->createBuffer(
|
||||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||||
&vertexStaging,
|
&vertexBuffer,
|
||||||
vertexBufferSize,
|
vertexBufferSize);
|
||||||
vBuffer.data());
|
// Index buffer
|
||||||
// Index data
|
vulkanDevice->createBuffer(
|
||||||
vulkanDevice->createBuffer(
|
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
||||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
&indexBuffer,
|
||||||
&indexStaging,
|
indexBufferSize);
|
||||||
indexBufferSize,
|
|
||||||
iBuffer.data());
|
|
||||||
|
|
||||||
// Create device local buffers
|
// Copy from staging buffers
|
||||||
// Vertex buffer
|
VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
||||||
vulkanDevice->createBuffer(
|
|
||||||
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
||||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
|
||||||
&vertexBuffer,
|
|
||||||
vertexBufferSize);
|
|
||||||
// Index buffer
|
|
||||||
vulkanDevice->createBuffer(
|
|
||||||
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
||||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
|
||||||
&indexBuffer,
|
|
||||||
indexBufferSize);
|
|
||||||
|
|
||||||
// Copy from staging buffers
|
VkBufferCopy copyRegion = {};
|
||||||
VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
|
||||||
|
|
||||||
VkBufferCopy copyRegion = {};
|
copyRegion.size = vertexBufferSize;
|
||||||
|
vkCmdCopyBuffer(
|
||||||
|
copyCmd,
|
||||||
|
vertexStaging.buffer,
|
||||||
|
vertexBuffer.buffer,
|
||||||
|
1,
|
||||||
|
©Region);
|
||||||
|
|
||||||
copyRegion.size = vertexBufferSize;
|
copyRegion.size = indexBufferSize;
|
||||||
vkCmdCopyBuffer(
|
vkCmdCopyBuffer(
|
||||||
copyCmd,
|
copyCmd,
|
||||||
vertexStaging.buffer,
|
indexStaging.buffer,
|
||||||
vertexBuffer.buffer,
|
indexBuffer.buffer,
|
||||||
1,
|
1,
|
||||||
©Region);
|
©Region);
|
||||||
|
|
||||||
copyRegion.size = indexBufferSize;
|
vulkanDevice->flushCommandBuffer(copyCmd, queue, true);
|
||||||
vkCmdCopyBuffer(
|
|
||||||
copyCmd,
|
|
||||||
indexStaging.buffer,
|
|
||||||
indexBuffer.buffer,
|
|
||||||
1,
|
|
||||||
©Region);
|
|
||||||
|
|
||||||
vulkanDevice->flushCommandBuffer(copyCmd, queue, true);
|
vkDestroyBuffer(vulkanDevice->logicalDevice, vertexStaging.buffer, nullptr);
|
||||||
|
vkFreeMemory(vulkanDevice->logicalDevice, vertexStaging.memory, nullptr);
|
||||||
vkDestroyBuffer(vulkanDevice->logicalDevice, vertexStaging.buffer, nullptr);
|
vkDestroyBuffer(vulkanDevice->logicalDevice, indexStaging.buffer, nullptr);
|
||||||
vkFreeMemory(vulkanDevice->logicalDevice, vertexStaging.memory, nullptr);
|
vkFreeMemory(vulkanDevice->logicalDevice, indexStaging.memory, nullptr);
|
||||||
vkDestroyBuffer(vulkanDevice->logicalDevice, indexStaging.buffer, nullptr);
|
|
||||||
vkFreeMemory(vulkanDevice->logicalDevice, indexStaging.memory, nullptr);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Vertex buffer
|
|
||||||
vulkanDevice->createBuffer(
|
|
||||||
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
|
||||||
&vertexBuffer,
|
|
||||||
vertexBufferSize,
|
|
||||||
vBuffer.data());
|
|
||||||
// Index buffer
|
|
||||||
vulkanDevice->createBuffer(
|
|
||||||
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
|
||||||
&indexBuffer,
|
|
||||||
indexBufferSize,
|
|
||||||
iBuffer.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
indexCount = static_cast<uint32_t>(iBuffer.size());
|
indexCount = static_cast<uint32_t>(iBuffer.size());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Vulkan Example - Animated gears using multiple uniform buffers
|
* Vulkan Example - Animated gears using multiple uniform buffers
|
||||||
*
|
*
|
||||||
* See readme.md for details
|
* Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
|
||||||
*
|
|
||||||
* Copyright (C) 2015 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)
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue