Code cleanup
Comments
This commit is contained in:
parent
61cdecf1c3
commit
7935025c2d
5 changed files with 208 additions and 252 deletions
|
|
@ -23,21 +23,20 @@ public:
|
|||
|
||||
vkglTF::Model scene;
|
||||
|
||||
struct {
|
||||
struct UniformData {
|
||||
glm::mat4 projection;
|
||||
glm::mat4 view;
|
||||
glm::mat4 model;
|
||||
} uboVS;
|
||||
|
||||
} uniformData;
|
||||
vks::Buffer uniformBuffer;
|
||||
|
||||
std::vector<int32_t> conditionalVisibility;
|
||||
std::vector<int32_t> conditionalVisibility{};
|
||||
vks::Buffer conditionalBuffer;
|
||||
|
||||
VkPipelineLayout pipelineLayout;
|
||||
VkPipeline pipeline;
|
||||
VkDescriptorSetLayout descriptorSetLayout;
|
||||
VkDescriptorSet descriptorSet;
|
||||
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
|
||||
VkPipeline pipeline{ VK_NULL_HANDLE };
|
||||
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
|
||||
VkDescriptorSet descriptorSet{ VK_NULL_HANDLE };
|
||||
|
||||
VulkanExample() : VulkanExampleBase()
|
||||
{
|
||||
|
|
@ -57,11 +56,13 @@ public:
|
|||
|
||||
~VulkanExample()
|
||||
{
|
||||
vkDestroyPipeline(device, pipeline, nullptr);
|
||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
||||
uniformBuffer.destroy();
|
||||
conditionalBuffer.destroy();
|
||||
if (device) {
|
||||
vkDestroyPipeline(device, pipeline, nullptr);
|
||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
||||
uniformBuffer.destroy();
|
||||
conditionalBuffer.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
void renderNode(vkglTF::Node *node, VkCommandBuffer commandBuffer) {
|
||||
|
|
@ -155,14 +156,16 @@ public:
|
|||
scene.loadFromFile(getAssetPath() + "models/gltf/glTF-Embedded/Buggy.gltf", vulkanDevice, queue);
|
||||
}
|
||||
|
||||
void setupDescriptorSets()
|
||||
void setupDescriptors()
|
||||
{
|
||||
// Pool
|
||||
std::vector<VkDescriptorPoolSize> poolSizes = {
|
||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
|
||||
};
|
||||
VkDescriptorPoolCreateInfo descriptorPoolCI = vks::initializers::descriptorPoolCreateInfo(poolSizes, 1);
|
||||
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolCI, nullptr, &descriptorPool));
|
||||
|
||||
// Layouts
|
||||
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
||||
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
|
||||
};
|
||||
|
|
@ -172,15 +175,7 @@ public:
|
|||
descriptorLayoutCI.pBindings = setLayoutBindings.data();
|
||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayoutCI, nullptr, &descriptorSetLayout));
|
||||
|
||||
std::array<VkDescriptorSetLayout, 2> setLayouts = {
|
||||
descriptorSetLayout, vkglTF::descriptorSetLayoutUbo
|
||||
};
|
||||
VkPipelineLayoutCreateInfo pipelineLayoutCI = vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), 2);
|
||||
VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::vec4), 0);
|
||||
pipelineLayoutCI.pushConstantRangeCount = 1;
|
||||
pipelineLayoutCI.pPushConstantRanges = &pushConstantRange;
|
||||
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelineLayout));
|
||||
|
||||
// Sets
|
||||
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
|
||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocateInfo, &descriptorSet));
|
||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
||||
|
|
@ -191,8 +186,17 @@ public:
|
|||
|
||||
void preparePipelines()
|
||||
{
|
||||
const std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
|
||||
// Layout
|
||||
std::array<VkDescriptorSetLayout, 2> setLayouts = {
|
||||
descriptorSetLayout, vkglTF::descriptorSetLayoutUbo
|
||||
};
|
||||
VkPipelineLayoutCreateInfo pipelineLayoutCI = vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), 2);
|
||||
VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::vec4), 0);
|
||||
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 blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
|
||||
|
|
@ -200,7 +204,8 @@ public:
|
|||
VkPipelineDepthStencilStateCreateInfo depthStencilStateCI = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);
|
||||
VkPipelineViewportStateCreateInfo viewportStateCI = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
|
||||
VkPipelineMultisampleStateCreateInfo multisampleStateCI = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0);
|
||||
VkPipelineDynamicStateCreateInfo dynamicStateCI = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(), static_cast<uint32_t>(dynamicStateEnables.size()), 0);
|
||||
const std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
|
||||
VkPipelineDynamicStateCreateInfo dynamicStateCI = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables, 0);
|
||||
|
||||
VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0);
|
||||
pipelineCI.pInputAssemblyState = &inputAssemblyStateCI;
|
||||
|
|
@ -229,17 +234,17 @@ public:
|
|||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
&uniformBuffer,
|
||||
sizeof(uboVS)));
|
||||
sizeof(UniformData)));
|
||||
VK_CHECK_RESULT(uniformBuffer.map());
|
||||
updateUniformBuffers();
|
||||
}
|
||||
|
||||
void updateUniformBuffers()
|
||||
{
|
||||
uboVS.projection = camera.matrices.perspective;
|
||||
uboVS.view = glm::scale(camera.matrices.view, glm::vec3(0.1f , -0.1f, 0.1f));
|
||||
uboVS.model = glm::translate(glm::mat4(1.0f), scene.dimensions.min);
|
||||
memcpy(uniformBuffer.mapped, &uboVS, sizeof(uboVS));
|
||||
uniformData.projection = camera.matrices.perspective;
|
||||
uniformData.view = glm::scale(camera.matrices.view, glm::vec3(0.1f , -0.1f, 0.1f));
|
||||
uniformData.model = glm::translate(glm::mat4(1.0f), scene.dimensions.min);
|
||||
memcpy(uniformBuffer.mapped, &uniformData, sizeof(UniformData));
|
||||
}
|
||||
|
||||
void updateConditionalBuffer()
|
||||
|
|
@ -309,7 +314,7 @@ public:
|
|||
loadAssets();
|
||||
prepareConditionalRendering();
|
||||
prepareUniformBuffers();
|
||||
setupDescriptorSets();
|
||||
setupDescriptors();
|
||||
preparePipelines();
|
||||
buildCommandBuffers();
|
||||
prepared = true;
|
||||
|
|
@ -319,15 +324,8 @@ public:
|
|||
{
|
||||
if (!prepared)
|
||||
return;
|
||||
draw();
|
||||
if (camera.updated) {
|
||||
updateUniformBuffers();
|
||||
}
|
||||
}
|
||||
|
||||
virtual void viewChanged()
|
||||
{
|
||||
updateUniformBuffers();
|
||||
draw();
|
||||
}
|
||||
|
||||
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue