Code cleanup

Comments
This commit is contained in:
Sascha Willems 2024-01-01 17:41:48 +01:00
parent 61cdecf1c3
commit 7935025c2d
5 changed files with 208 additions and 252 deletions

View file

@ -23,21 +23,20 @@ public:
vkglTF::Model scene; vkglTF::Model scene;
struct { struct UniformData {
glm::mat4 projection; glm::mat4 projection;
glm::mat4 view; glm::mat4 view;
glm::mat4 model; glm::mat4 model;
} uboVS; } uniformData;
vks::Buffer uniformBuffer; vks::Buffer uniformBuffer;
std::vector<int32_t> conditionalVisibility; std::vector<int32_t> conditionalVisibility{};
vks::Buffer conditionalBuffer; vks::Buffer conditionalBuffer;
VkPipelineLayout pipelineLayout; VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
VkPipeline pipeline; VkPipeline pipeline{ VK_NULL_HANDLE };
VkDescriptorSetLayout descriptorSetLayout; VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
VkDescriptorSet descriptorSet; VkDescriptorSet descriptorSet{ VK_NULL_HANDLE };
VulkanExample() : VulkanExampleBase() VulkanExample() : VulkanExampleBase()
{ {
@ -57,11 +56,13 @@ public:
~VulkanExample() ~VulkanExample()
{ {
vkDestroyPipeline(device, pipeline, nullptr); if (device) {
vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyPipeline(device, pipeline, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
uniformBuffer.destroy(); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
conditionalBuffer.destroy(); uniformBuffer.destroy();
conditionalBuffer.destroy();
}
} }
void renderNode(vkglTF::Node *node, VkCommandBuffer commandBuffer) { void renderNode(vkglTF::Node *node, VkCommandBuffer commandBuffer) {
@ -155,14 +156,16 @@ public:
scene.loadFromFile(getAssetPath() + "models/gltf/glTF-Embedded/Buggy.gltf", vulkanDevice, queue); scene.loadFromFile(getAssetPath() + "models/gltf/glTF-Embedded/Buggy.gltf", vulkanDevice, queue);
} }
void setupDescriptorSets() void setupDescriptors()
{ {
// Pool
std::vector<VkDescriptorPoolSize> poolSizes = { std::vector<VkDescriptorPoolSize> poolSizes = {
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1), vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
}; };
VkDescriptorPoolCreateInfo descriptorPoolCI = vks::initializers::descriptorPoolCreateInfo(poolSizes, 1); VkDescriptorPoolCreateInfo descriptorPoolCI = vks::initializers::descriptorPoolCreateInfo(poolSizes, 1);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolCI, nullptr, &descriptorPool)); VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolCI, nullptr, &descriptorPool));
// Layouts
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = { std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0), vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
}; };
@ -172,15 +175,7 @@ public:
descriptorLayoutCI.pBindings = setLayoutBindings.data(); descriptorLayoutCI.pBindings = setLayoutBindings.data();
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayoutCI, nullptr, &descriptorSetLayout)); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayoutCI, nullptr, &descriptorSetLayout));
std::array<VkDescriptorSetLayout, 2> setLayouts = { // Sets
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));
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1); VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocateInfo, &descriptorSet)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocateInfo, &descriptorSet));
std::vector<VkWriteDescriptorSet> writeDescriptorSets = { std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
@ -191,8 +186,17 @@ public:
void preparePipelines() 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); 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); 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); 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); VkPipelineDepthStencilStateCreateInfo depthStencilStateCI = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);
VkPipelineViewportStateCreateInfo viewportStateCI = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0); VkPipelineViewportStateCreateInfo viewportStateCI = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
VkPipelineMultisampleStateCreateInfo multisampleStateCI = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 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); VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0);
pipelineCI.pInputAssemblyState = &inputAssemblyStateCI; pipelineCI.pInputAssemblyState = &inputAssemblyStateCI;
@ -229,17 +234,17 @@ public:
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffer, &uniformBuffer,
sizeof(uboVS))); sizeof(UniformData)));
VK_CHECK_RESULT(uniformBuffer.map()); VK_CHECK_RESULT(uniformBuffer.map());
updateUniformBuffers(); updateUniformBuffers();
} }
void updateUniformBuffers() void updateUniformBuffers()
{ {
uboVS.projection = camera.matrices.perspective; uniformData.projection = camera.matrices.perspective;
uboVS.view = glm::scale(camera.matrices.view, glm::vec3(0.1f , -0.1f, 0.1f)); uniformData.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); uniformData.model = glm::translate(glm::mat4(1.0f), scene.dimensions.min);
memcpy(uniformBuffer.mapped, &uboVS, sizeof(uboVS)); memcpy(uniformBuffer.mapped, &uniformData, sizeof(UniformData));
} }
void updateConditionalBuffer() void updateConditionalBuffer()
@ -309,7 +314,7 @@ public:
loadAssets(); loadAssets();
prepareConditionalRendering(); prepareConditionalRendering();
prepareUniformBuffers(); prepareUniformBuffers();
setupDescriptorSets(); setupDescriptors();
preparePipelines(); preparePipelines();
buildCommandBuffers(); buildCommandBuffers();
prepared = true; prepared = true;
@ -319,15 +324,8 @@ public:
{ {
if (!prepared) if (!prepared)
return; return;
draw();
if (camera.updated) {
updateUniformBuffers();
}
}
virtual void viewChanged()
{
updateUniformBuffers(); updateUniformBuffers();
draw();
} }
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay) virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)

View file

@ -5,17 +5,13 @@
* *
* Uses an offscreen buffer with lower resolution to demonstrate the effect of conservative rasterization * Uses an offscreen buffer with lower resolution to demonstrate the effect of conservative rasterization
* *
* Copyright 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)
*/ */
#include "vulkanexamplebase.h" #include "vulkanexamplebase.h"
#define FB_COLOR_FORMAT VK_FORMAT_R8G8B8A8_UNORM
#define ZOOM_FACTOR 16
class VulkanExample : public VulkanExampleBase class VulkanExample : public VulkanExampleBase
{ {
public: public:
@ -32,40 +28,35 @@ public:
struct Triangle { struct Triangle {
vks::Buffer vertices; vks::Buffer vertices;
vks::Buffer indices; vks::Buffer indices;
uint32_t indexCount; uint32_t indexCount{ 0 };
} triangle; } triangle;
vks::Buffer uniformBuffer; struct UniformData {
struct UniformBuffers {
vks::Buffer scene;
} uniformBuffers;
struct UboScene {
glm::mat4 projection; glm::mat4 projection;
glm::mat4 model; glm::mat4 model;
} uboScene; } uniformData;
vks::Buffer uniformBuffer;
struct PipelineLayouts { struct PipelineLayouts {
VkPipelineLayout scene; VkPipelineLayout scene{ VK_NULL_HANDLE };
VkPipelineLayout fullscreen; VkPipelineLayout fullscreen{ VK_NULL_HANDLE };
} pipelineLayouts; } pipelineLayouts;
struct Pipelines { struct Pipelines {
VkPipeline triangle; VkPipeline triangle{ VK_NULL_HANDLE };
VkPipeline triangleConservativeRaster; VkPipeline triangleConservativeRaster{ VK_NULL_HANDLE };
VkPipeline triangleOverlay; VkPipeline triangleOverlay{ VK_NULL_HANDLE };
VkPipeline fullscreen; VkPipeline fullscreen{ VK_NULL_HANDLE };
} pipelines; } pipelines;
struct DescriptorSetLayouts { struct DescriptorSetLayouts {
VkDescriptorSetLayout scene; VkDescriptorSetLayout scene{ VK_NULL_HANDLE };
VkDescriptorSetLayout fullscreen; VkDescriptorSetLayout fullscreen{ VK_NULL_HANDLE };
} descriptorSetLayouts; } descriptorSetLayouts;
struct DescriptorSets { struct DescriptorSets {
VkDescriptorSet scene; VkDescriptorSet scene{ VK_NULL_HANDLE };
VkDescriptorSet fullscreen; VkDescriptorSet fullscreen{ VK_NULL_HANDLE };
} descriptorSets; } descriptorSets;
// Framebuffer for offscreen rendering // Framebuffer for offscreen rendering
@ -81,7 +72,7 @@ public:
VkRenderPass renderPass; VkRenderPass renderPass;
VkSampler sampler; VkSampler sampler;
VkDescriptorImageInfo descriptor; VkDescriptorImageInfo descriptor;
} offscreenPass; } offscreenPass{};
VulkanExample() : VulkanExampleBase() VulkanExample() : VulkanExampleBase()
{ {
@ -101,33 +92,33 @@ public:
~VulkanExample() ~VulkanExample()
{ {
// Frame buffer if (device) {
vkDestroyImageView(device, offscreenPass.color.view, nullptr);
vkDestroyImage(device, offscreenPass.color.image, nullptr);
vkFreeMemory(device, offscreenPass.color.mem, nullptr);
vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
vkDestroyImage(device, offscreenPass.depth.image, nullptr);
vkFreeMemory(device, offscreenPass.depth.mem, nullptr);
vkDestroyImageView(device, offscreenPass.color.view, nullptr); vkDestroyRenderPass(device, offscreenPass.renderPass, nullptr);
vkDestroyImage(device, offscreenPass.color.image, nullptr); vkDestroySampler(device, offscreenPass.sampler, nullptr);
vkFreeMemory(device, offscreenPass.color.mem, nullptr); vkDestroyFramebuffer(device, offscreenPass.frameBuffer, nullptr);
vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
vkDestroyImage(device, offscreenPass.depth.image, nullptr);
vkFreeMemory(device, offscreenPass.depth.mem, nullptr);
vkDestroyRenderPass(device, offscreenPass.renderPass, nullptr); vkDestroyPipeline(device, pipelines.triangle, nullptr);
vkDestroySampler(device, offscreenPass.sampler, nullptr); vkDestroyPipeline(device, pipelines.triangleOverlay, nullptr);
vkDestroyFramebuffer(device, offscreenPass.frameBuffer, nullptr); vkDestroyPipeline(device, pipelines.triangleConservativeRaster, nullptr);
vkDestroyPipeline(device, pipelines.fullscreen, nullptr);
vkDestroyPipeline(device, pipelines.triangle, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.fullscreen, nullptr);
vkDestroyPipeline(device, pipelines.triangleOverlay, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.scene, nullptr);
vkDestroyPipeline(device, pipelines.triangleConservativeRaster, nullptr);
vkDestroyPipeline(device, pipelines.fullscreen, nullptr);
vkDestroyPipelineLayout(device, pipelineLayouts.fullscreen, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.scene, nullptr);
vkDestroyPipelineLayout(device, pipelineLayouts.scene, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.fullscreen, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.scene, nullptr); uniformBuffer.destroy();
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.fullscreen, nullptr); triangle.vertices.destroy();
triangle.indices.destroy();
uniformBuffers.scene.destroy(); }
triangle.vertices.destroy();
triangle.indices.destroy();
} }
void getEnabledFeatures() void getEnabledFeatures()
@ -141,8 +132,13 @@ public:
*/ */
void prepareOffscreen() void prepareOffscreen()
{ {
offscreenPass.width = width / ZOOM_FACTOR; // We "magnify" the offscreen rendered triangle so that the conservative rasterization feature is easier to see
offscreenPass.height = height / ZOOM_FACTOR; const int32_t magnification = 16;
offscreenPass.width = width / magnification;
offscreenPass.height = height / magnification;
const VkFormat fbColorFormat = VK_FORMAT_R8G8B8A8_UNORM;
// Find a suitable depth format // Find a suitable depth format
VkFormat fbDepthFormat; VkFormat fbDepthFormat;
@ -152,7 +148,7 @@ public:
// Color attachment // Color attachment
VkImageCreateInfo image = vks::initializers::imageCreateInfo(); VkImageCreateInfo image = vks::initializers::imageCreateInfo();
image.imageType = VK_IMAGE_TYPE_2D; image.imageType = VK_IMAGE_TYPE_2D;
image.format = FB_COLOR_FORMAT; image.format = fbColorFormat;
image.extent.width = offscreenPass.width; image.extent.width = offscreenPass.width;
image.extent.height = offscreenPass.height; image.extent.height = offscreenPass.height;
image.extent.depth = 1; image.extent.depth = 1;
@ -175,7 +171,7 @@ public:
VkImageViewCreateInfo colorImageView = vks::initializers::imageViewCreateInfo(); VkImageViewCreateInfo colorImageView = vks::initializers::imageViewCreateInfo();
colorImageView.viewType = VK_IMAGE_VIEW_TYPE_2D; colorImageView.viewType = VK_IMAGE_VIEW_TYPE_2D;
colorImageView.format = FB_COLOR_FORMAT; colorImageView.format = fbColorFormat;
colorImageView.subresourceRange = {}; colorImageView.subresourceRange = {};
colorImageView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; colorImageView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
colorImageView.subresourceRange.baseMipLevel = 0; colorImageView.subresourceRange.baseMipLevel = 0;
@ -228,7 +224,7 @@ public:
std::array<VkAttachmentDescription, 2> attchmentDescriptions = {}; std::array<VkAttachmentDescription, 2> attchmentDescriptions = {};
// Color attachment // Color attachment
attchmentDescriptions[0].format = FB_COLOR_FORMAT; attchmentDescriptions[0].format = fbColorFormat;
attchmentDescriptions[0].samples = VK_SAMPLE_COUNT_1_BIT; attchmentDescriptions[0].samples = VK_SAMPLE_COUNT_1_BIT;
attchmentDescriptions[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attchmentDescriptions[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attchmentDescriptions[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; attchmentDescriptions[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
@ -457,8 +453,9 @@ public:
stagingBuffers.indices.destroy(); stagingBuffers.indices.destroy();
} }
void setupDescriptorPool() void setupDescriptors()
{ {
// Pool
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),
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2) vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2)
@ -466,13 +463,10 @@ public:
VkDescriptorPoolCreateInfo descriptorPoolInfo = VkDescriptorPoolCreateInfo descriptorPoolInfo =
vks::initializers::descriptorPoolCreateInfo(poolSizes, 2); vks::initializers::descriptorPoolCreateInfo(poolSizes, 2);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
}
void setupDescriptorSetLayout() // Layouts
{
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings; std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings;
VkDescriptorSetLayoutCreateInfo descriptorLayout; VkDescriptorSetLayoutCreateInfo descriptorLayout;
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo;
// Scene rendering // Scene rendering
setLayoutBindings = { setLayoutBindings = {
@ -480,8 +474,6 @@ public:
}; };
descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size())); descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size()));
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.scene)); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.scene));
pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.scene, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.scene));
// Fullscreen pass // Fullscreen pass
setLayoutBindings = { setLayoutBindings = {
@ -489,19 +481,15 @@ public:
}; };
descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size())); descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size()));
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.fullscreen)); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.fullscreen));
pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.fullscreen, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.fullscreen));
}
void setupDescriptorSet() // Sets
{
VkDescriptorSetAllocateInfo descriptorSetAllocInfo; VkDescriptorSetAllocateInfo descriptorSetAllocInfo;
// Scene rendering // Scene rendering
descriptorSetAllocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.scene, 1); descriptorSetAllocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.scene, 1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, &descriptorSets.scene)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, &descriptorSets.scene));
std::vector<VkWriteDescriptorSet> offScreenWriteDescriptorSets = { std::vector<VkWriteDescriptorSet> offScreenWriteDescriptorSets = {
vks::initializers::writeDescriptorSet(descriptorSets.scene, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.scene.descriptor), vks::initializers::writeDescriptorSet(descriptorSets.scene, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffer.descriptor),
}; };
vkUpdateDescriptorSets(device, static_cast<uint32_t>(offScreenWriteDescriptorSets.size()), offScreenWriteDescriptorSets.data(), 0, nullptr); vkUpdateDescriptorSets(device, static_cast<uint32_t>(offScreenWriteDescriptorSets.size()), offScreenWriteDescriptorSets.data(), 0, nullptr);
@ -516,38 +504,25 @@ public:
void preparePipelines() void preparePipelines()
{ {
VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo;
vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE); pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.scene, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.scene));
VkPipelineColorBlendAttachmentState blendAttachmentState = pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.fullscreen, 1);
vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE); VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.fullscreen));
VkPipelineColorBlendStateCreateInfo colorBlendStateCI =
vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
VkPipelineDepthStencilStateCreateInfo depthStencilStateCI =
vks::initializers::pipelineDepthStencilStateCreateInfo(VK_FALSE, VK_FALSE, 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);
std::vector<VkDynamicState> dynamicStateEnables = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR,
};
VkPipelineDynamicStateCreateInfo dynamicStateCI =
vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables);
VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
VkPipelineColorBlendStateCreateInfo colorBlendStateCI = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
VkPipelineDepthStencilStateCreateInfo depthStencilStateCI = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_FALSE, VK_FALSE, 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);
std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, };
VkPipelineDynamicStateCreateInfo dynamicStateCI = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables);
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages; std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
VkGraphicsPipelineCreateInfo pipelineCreateInfo = VkPipelineRasterizationStateCreateInfo rasterizationStateCI = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_CLOCKWISE, 0);
vks::initializers::pipelineCreateInfo(pipelineLayouts.fullscreen, renderPass, 0);
VkPipelineRasterizationStateCreateInfo rasterizationStateCI =
vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_CLOCKWISE, 0);
/* /*
Conservative rasterization setup Conservative rasterization setup
@ -579,6 +554,7 @@ public:
vertexInputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexInputAttributes.size()); vertexInputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexInputAttributes.size());
vertexInputState.pVertexAttributeDescriptions = vertexInputAttributes.data(); vertexInputState.pVertexAttributeDescriptions = vertexInputAttributes.data();
VkGraphicsPipelineCreateInfo pipelineCreateInfo = vks::initializers::pipelineCreateInfo(pipelineLayouts.fullscreen, renderPass, 0);
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyStateCI; pipelineCreateInfo.pInputAssemblyState = &inputAssemblyStateCI;
pipelineCreateInfo.pRasterizationState = &rasterizationStateCI; pipelineCreateInfo.pRasterizationState = &rasterizationStateCI;
pipelineCreateInfo.pColorBlendState = &colorBlendStateCI; pipelineCreateInfo.pColorBlendState = &colorBlendStateCI;
@ -643,17 +619,17 @@ public:
VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.scene, &uniformBuffer,
sizeof(uboScene))); sizeof(UniformData)));
VK_CHECK_RESULT(uniformBuffers.scene.map()); VK_CHECK_RESULT(uniformBuffer.map());
updateUniformBuffersScene(); updateUniformBuffersScene();
} }
void updateUniformBuffersScene() void updateUniformBuffersScene()
{ {
uboScene.projection = camera.matrices.perspective; uniformData.projection = camera.matrices.perspective;
uboScene.model = camera.matrices.view; uniformData.model = camera.matrices.view;
memcpy(uniformBuffers.scene.mapped, &uboScene, sizeof(uboScene)); memcpy(uniformBuffer.mapped, &uniformData, sizeof(UniformData));
} }
void draw() void draw()
@ -671,10 +647,8 @@ public:
loadAssets(); loadAssets();
prepareOffscreen(); prepareOffscreen();
prepareUniformBuffers(); prepareUniformBuffers();
setupDescriptorSetLayout(); setupDescriptors();
preparePipelines(); preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers(); buildCommandBuffers();
prepared = true; prepared = true;
} }
@ -683,9 +657,8 @@ public:
{ {
if (!prepared) if (!prepared)
return; return;
updateUniformBuffersScene();
draw(); draw();
if (camera.updated)
updateUniformBuffersScene();
} }
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay) virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)

View file

@ -49,11 +49,12 @@ public:
~VulkanExample() ~VulkanExample()
{ {
// Note : Inherited destructor cleans up resources stored in base class if (device) {
vkDestroyPipeline(device, pipeline, nullptr); vkDestroyPipeline(device, pipeline, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
uniformBuffer.destroy(); uniformBuffer.destroy();
}
} }
void loadAssets() void loadAssets()

View file

@ -15,27 +15,16 @@ public:
bool wireframe = true; bool wireframe = true;
bool glow = true; bool glow = true;
struct Scene { struct Models {
vkglTF::Model scene, sceneGlow;
vkglTF::Model model; } models;
std::vector<std::string> modelPartNames;
void loadFromFile(std::string filename, vks::VulkanDevice* vulkanDevice, VkQueue queue)
{
const uint32_t glTFLoadingFlags = vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::PreMultiplyVertexColors | vkglTF::FileLoadingFlags::FlipY;
model.loadFromFile(filename, vulkanDevice, queue, glTFLoadingFlags);
}
};
Scene scene, sceneGlow;
vks::Buffer uniformBuffer;
struct UBOVS { struct UBOVS {
glm::mat4 projection; glm::mat4 projection;
glm::mat4 model; glm::mat4 model;
glm::vec4 lightPos = glm::vec4(0.0f, 5.0f, 15.0f, 1.0f); glm::vec4 lightPos = glm::vec4(0.0f, 5.0f, 15.0f, 1.0f);
} uboVS; } uniformData;
vks::Buffer uniformBuffer;
struct Pipelines { struct Pipelines {
VkPipeline toonshading; VkPipeline toonshading;
@ -51,7 +40,7 @@ public:
// Framebuffer for offscreen rendering // Framebuffer for offscreen rendering
struct FrameBufferAttachment { struct FrameBufferAttachment {
VkImage image; VkImage image;
VkDeviceMemory mem; VkDeviceMemory memory;
VkImageView view; VkImageView view;
}; };
struct OffscreenPass { struct OffscreenPass {
@ -98,34 +87,34 @@ public:
~VulkanExample() ~VulkanExample()
{ {
// Clean up used Vulkan resources if (device) {
// Note : Inherited destructor cleans up resources stored in base class vkDestroyPipeline(device, pipelines.toonshading, nullptr);
vkDestroyPipeline(device, pipelines.toonshading, nullptr); vkDestroyPipeline(device, pipelines.color, nullptr);
vkDestroyPipeline(device, pipelines.color, nullptr); vkDestroyPipeline(device, pipelines.postprocess, nullptr);
vkDestroyPipeline(device, pipelines.postprocess, nullptr); if (pipelines.wireframe != VK_NULL_HANDLE) {
if (pipelines.wireframe != VK_NULL_HANDLE) { vkDestroyPipeline(device, pipelines.wireframe, nullptr);
vkDestroyPipeline(device, pipelines.wireframe, nullptr); }
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
uniformBuffer.destroy();
// Offscreen
// Color attachment
vkDestroyImageView(device, offscreenPass.color.view, nullptr);
vkDestroyImage(device, offscreenPass.color.image, nullptr);
vkFreeMemory(device, offscreenPass.color.memory, nullptr);
// Depth attachment
vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
vkDestroyImage(device, offscreenPass.depth.image, nullptr);
vkFreeMemory(device, offscreenPass.depth.memory, nullptr);
vkDestroyRenderPass(device, offscreenPass.renderPass, nullptr);
vkDestroySampler(device, offscreenPass.sampler, nullptr);
vkDestroyFramebuffer(device, offscreenPass.frameBuffer, nullptr);
} }
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
uniformBuffer.destroy();
// Offscreen
// Color attachment
vkDestroyImageView(device, offscreenPass.color.view, nullptr);
vkDestroyImage(device, offscreenPass.color.image, nullptr);
vkFreeMemory(device, offscreenPass.color.mem, nullptr);
// Depth attachment
vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
vkDestroyImage(device, offscreenPass.depth.image, nullptr);
vkFreeMemory(device, offscreenPass.depth.mem, nullptr);
vkDestroyRenderPass(device, offscreenPass.renderPass, nullptr);
vkDestroySampler(device, offscreenPass.sampler, nullptr);
vkDestroyFramebuffer(device, offscreenPass.frameBuffer, nullptr);
} }
/* /*
@ -290,8 +279,8 @@ public:
vkGetImageMemoryRequirements(device, offscreenPass.color.image, &memReqs); vkGetImageMemoryRequirements(device, offscreenPass.color.image, &memReqs);
memAlloc.allocationSize = memReqs.size; memAlloc.allocationSize = memReqs.size;
memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.color.mem)); VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.color.memory));
VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.color.image, offscreenPass.color.mem, 0)); VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.color.image, offscreenPass.color.memory, 0));
VkImageViewCreateInfo colorImageView = vks::initializers::imageViewCreateInfo(); VkImageViewCreateInfo colorImageView = vks::initializers::imageViewCreateInfo();
colorImageView.viewType = VK_IMAGE_VIEW_TYPE_2D; colorImageView.viewType = VK_IMAGE_VIEW_TYPE_2D;
@ -328,8 +317,8 @@ public:
vkGetImageMemoryRequirements(device, offscreenPass.depth.image, &memReqs); vkGetImageMemoryRequirements(device, offscreenPass.depth.image, &memReqs);
memAlloc.allocationSize = memReqs.size; memAlloc.allocationSize = memReqs.size;
memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.depth.mem)); VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.depth.memory));
VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.depth.image, offscreenPass.depth.mem, 0)); VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.depth.image, offscreenPass.depth.memory, 0));
VkImageViewCreateInfo depthStencilView = vks::initializers::imageViewCreateInfo(); VkImageViewCreateInfo depthStencilView = vks::initializers::imageViewCreateInfo();
depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D; depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D;
@ -428,18 +417,20 @@ public:
void loadAssets() void loadAssets()
{ {
scene.loadFromFile(getAssetPath() + "models/treasure_smooth.gltf", vulkanDevice, queue); const uint32_t glTFLoadingFlags = vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::PreMultiplyVertexColors | vkglTF::FileLoadingFlags::FlipY;
sceneGlow.loadFromFile(getAssetPath() + "models/treasure_glow.gltf", vulkanDevice, queue); models.scene.loadFromFile(getAssetPath() + "models/treasure_smooth.gltf", vulkanDevice, queue, glTFLoadingFlags);
models.sceneGlow.loadFromFile(getAssetPath() + "models/treasure_glow.gltf", vulkanDevice, queue, glTFLoadingFlags);
} }
void drawScene(Scene& scene, VkCommandBuffer cmdBuffer) // We use a custom draw function so we can insert debug labels with the names of the glTF nodes
void drawModel(vkglTF::Model &model, VkCommandBuffer cmdBuffer)
{ {
scene.model.bindBuffers(cmdBuffer); model.bindBuffers(cmdBuffer);
for (auto i = 0; i < scene.model.nodes.size(); i++) for (auto i = 0; i < model.nodes.size(); i++)
{ {
// Insert a label for the current model's name // Insert a label for the current model's name
cmdInsertLabel(cmdBuffer, scene.model.nodes[i]->name.c_str(), { 0.0f, 0.0f, 0.0f, 0.0f }); cmdInsertLabel(cmdBuffer, model.nodes[i]->name.c_str(), { 0.0f, 0.0f, 0.0f, 0.0f });
scene.model.drawNode(scene.model.nodes[i], cmdBuffer); model.drawNode(model.nodes[i], cmdBuffer);
} }
} }
@ -482,7 +473,7 @@ public:
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.color); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.color);
drawScene(sceneGlow, drawCmdBuffers[i]); drawModel(models.sceneGlow, drawCmdBuffers[i]);
vkCmdEndRenderPass(drawCmdBuffers[i]); vkCmdEndRenderPass(drawCmdBuffers[i]);
@ -525,7 +516,7 @@ public:
cmdBeginLabel(drawCmdBuffers[i], "Toon shading draw", { 0.78f, 0.74f, 0.9f, 1.0f }); cmdBeginLabel(drawCmdBuffers[i], "Toon shading draw", { 0.78f, 0.74f, 0.9f, 1.0f });
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.toonshading); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.toonshading);
drawScene(scene, drawCmdBuffers[i]); drawModel(models.scene, drawCmdBuffers[i]);
cmdEndLabel(drawCmdBuffers[i]); cmdEndLabel(drawCmdBuffers[i]);
@ -538,7 +529,7 @@ public:
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.wireframe); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.wireframe);
drawScene(scene, drawCmdBuffers[i]); drawModel(models.scene, drawCmdBuffers[i]);
cmdEndLabel(drawCmdBuffers[i]); cmdEndLabel(drawCmdBuffers[i]);
@ -559,7 +550,9 @@ public:
cmdEndLabel(drawCmdBuffers[i]); cmdEndLabel(drawCmdBuffers[i]);
} }
cmdBeginLabel(drawCmdBuffers[i], "UI overlay", { 0.23f, 0.65f, 0.28f, 1.0f });
drawUI(drawCmdBuffers[i]); drawUI(drawCmdBuffers[i]);
cmdEndLabel(drawCmdBuffers[i]);
vkCmdEndRenderPass(drawCmdBuffers[i]); vkCmdEndRenderPass(drawCmdBuffers[i]);
@ -682,10 +675,10 @@ public:
setObjectName(device, VK_OBJECT_TYPE_SAMPLER, (uint64_t)offscreenPass.sampler, "Off-screen framebuffer default sampler"); setObjectName(device, VK_OBJECT_TYPE_SAMPLER, (uint64_t)offscreenPass.sampler, "Off-screen framebuffer default sampler");
setObjectName(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)uniformBuffer.buffer, "Scene uniform buffer block"); setObjectName(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)uniformBuffer.buffer, "Scene uniform buffer block");
setObjectName(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)scene.model.vertices.buffer, "Scene vertex buffer"); setObjectName(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)models.scene.vertices.buffer, "Scene vertex buffer");
setObjectName(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)scene.model.indices.buffer, "Scene index buffer"); setObjectName(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)models.scene.indices.buffer, "Scene index buffer");
setObjectName(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)sceneGlow.model.vertices.buffer, "Glow vertex buffer"); setObjectName(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)models.sceneGlow.vertices.buffer, "Glow vertex buffer");
setObjectName(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)sceneGlow.model.indices.buffer, "Glow index buffer"); setObjectName(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)models.sceneGlow.indices.buffer, "Glow index buffer");
// Shader module count starts at 2 when UI overlay in base class is enabled // Shader module count starts at 2 when UI overlay in base class is enabled
uint32_t moduleIndex = settings.overlay ? 2 : 0; uint32_t moduleIndex = settings.overlay ? 2 : 0;
@ -716,7 +709,7 @@ public:
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffer, &uniformBuffer,
sizeof(uboVS))); sizeof(uniformData)));
// Map persistent // Map persistent
VK_CHECK_RESULT(uniformBuffer.map()); VK_CHECK_RESULT(uniformBuffer.map());
@ -726,9 +719,9 @@ public:
void updateUniformBuffers() void updateUniformBuffers()
{ {
uboVS.projection = camera.matrices.perspective; uniformData.projection = camera.matrices.perspective;
uboVS.model = camera.matrices.view; uniformData.model = camera.matrices.view;
memcpy(uniformBuffer.mapped, &uboVS, sizeof(uboVS)); memcpy(uniformBuffer.mapped, &uniformData, sizeof(uniformData));
} }
void draw() void draw()
@ -762,20 +755,14 @@ public:
{ {
if (!prepared) if (!prepared)
return; return;
draw();
if (camera.updated)
updateUniformBuffers();
}
virtual void viewChanged()
{
updateUniformBuffers(); updateUniformBuffers();
draw();
} }
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay) virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
{ {
if (overlay->header("Info")) { if (overlay->header("Info")) {
overlay->text("VK_EXT_debug_utils %s", (debugUtilsSupported? "active" : "not present")); overlay->text("VK_EXT_debug_utils %s", (debugUtilsSupported? "supported" : "not supported"));
} }
if (overlay->header("Settings")) { if (overlay->header("Settings")) {
if (overlay->checkBox("Glow", &glow)) { if (overlay->checkBox("Glow", &glow)) {

View file

@ -1,7 +1,7 @@
/* /*
* Vulkan Example - Using mesh shaders * Vulkan Example - Basic sample for using mesh and task shader to replace the traditional vertex pipeline
* *
* Copyright (C) 2022 by Sascha Willems - www.saschawillems.de * Copyright (C) 2022-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)
*/ */
@ -19,14 +19,14 @@ public:
} uniformData; } uniformData;
vks::Buffer uniformBuffer; vks::Buffer uniformBuffer;
uint32_t indexCount; uint32_t indexCount{ 0 };
VkPipeline pipeline; VkPipeline pipeline{ VK_NULL_HANDLE };
VkPipelineLayout pipelineLayout; VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
VkDescriptorSet descriptorSet; VkDescriptorSet descriptorSet{ VK_NULL_HANDLE };
VkDescriptorSetLayout descriptorSetLayout; VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
PFN_vkCmdDrawMeshTasksEXT vkCmdDrawMeshTasksEXT; PFN_vkCmdDrawMeshTasksEXT vkCmdDrawMeshTasksEXT{ VK_NULL_HANDLE };
VkPhysicalDeviceMeshShaderFeaturesEXT enabledMeshShaderFeatures{}; VkPhysicalDeviceMeshShaderFeaturesEXT enabledMeshShaderFeatures{};
@ -39,7 +39,7 @@ public:
camera.setRotation(glm::vec3(0.0f, 15.0f, 0.0f)); camera.setRotation(glm::vec3(0.0f, 15.0f, 0.0f));
camera.setTranslation(glm::vec3(0.0f, 0.0f, -5.0f)); camera.setTranslation(glm::vec3(0.0f, 0.0f, -5.0f));
// Extension require at least Vulkan 1.1 // The mesh shader extension requires at least Vulkan Core 1.1
apiVersion = VK_API_VERSION_1_1; apiVersion = VK_API_VERSION_1_1;
// Extensions required by mesh shading // Extensions required by mesh shading
@ -49,18 +49,8 @@ public:
// Required by VK_KHR_spirv_1_4 // Required by VK_KHR_spirv_1_4
enabledDeviceExtensions.push_back(VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME); enabledDeviceExtensions.push_back(VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME);
}
~VulkanExample() // We need to enable the mesh and task shader feature using a new struct introduced with the extension
{
vkDestroyPipeline(device, pipeline, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
uniformBuffer.destroy();
}
void getEnabledFeatures()
{
enabledMeshShaderFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT; enabledMeshShaderFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT;
enabledMeshShaderFeatures.meshShader = VK_TRUE; enabledMeshShaderFeatures.meshShader = VK_TRUE;
enabledMeshShaderFeatures.taskShader = VK_TRUE; enabledMeshShaderFeatures.taskShader = VK_TRUE;
@ -68,6 +58,15 @@ public:
deviceCreatepNextChain = &enabledMeshShaderFeatures; deviceCreatepNextChain = &enabledMeshShaderFeatures;
} }
~VulkanExample()
{
if (device) {
vkDestroyPipeline(device, pipeline, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
uniformBuffer.destroy();
}
}
void buildCommandBuffers() void buildCommandBuffers()
{ {
@ -103,6 +102,8 @@ public:
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
// Use mesh and task shader to draw the scene
vkCmdDrawMeshTasksEXT(drawCmdBuffers[i], 1, 1, 1); vkCmdDrawMeshTasksEXT(drawCmdBuffers[i], 1, 1, 1);
drawUI(drawCmdBuffers[i]); drawUI(drawCmdBuffers[i]);
@ -158,10 +159,6 @@ public:
VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0); VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0);
// Mesh shading doesn't require vertex input state
pipelineCI.pInputAssemblyState = nullptr;
pipelineCI.pVertexInputState = nullptr;
pipelineCI.pRasterizationState = &rasterizationState; pipelineCI.pRasterizationState = &rasterizationState;
pipelineCI.pColorBlendState = &colorBlendState; pipelineCI.pColorBlendState = &colorBlendState;
pipelineCI.pMultisampleState = &multisampleState; pipelineCI.pMultisampleState = &multisampleState;
@ -171,8 +168,14 @@ public:
pipelineCI.stageCount = static_cast<uint32_t>(shaderStages.size()); pipelineCI.stageCount = static_cast<uint32_t>(shaderStages.size());
pipelineCI.pStages = shaderStages.data(); pipelineCI.pStages = shaderStages.data();
// Not using a vertex shader, mesh shading doesn't require vertex input state
pipelineCI.pInputAssemblyState = nullptr;
pipelineCI.pVertexInputState = nullptr;
// Instead of a vertex shader, we use a mesh and task shader
shaderStages[0] = loadShader(getShadersPath() + "meshshader/meshshader.mesh.spv", VK_SHADER_STAGE_MESH_BIT_EXT); shaderStages[0] = loadShader(getShadersPath() + "meshshader/meshshader.mesh.spv", VK_SHADER_STAGE_MESH_BIT_EXT);
shaderStages[1] = loadShader(getShadersPath() + "meshshader/meshshader.task.spv", VK_SHADER_STAGE_TASK_BIT_EXT); shaderStages[1] = loadShader(getShadersPath() + "meshshader/meshshader.task.spv", VK_SHADER_STAGE_TASK_BIT_EXT);
shaderStages[2] = loadShader(getShadersPath() + "meshshader/meshshader.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); shaderStages[2] = loadShader(getShadersPath() + "meshshader/meshshader.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline));
} }
@ -196,11 +199,9 @@ public:
void draw() void draw()
{ {
VulkanExampleBase::prepareFrame(); VulkanExampleBase::prepareFrame();
submitInfo.commandBufferCount = 1; submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer]; submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE)); VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
VulkanExampleBase::submitFrame(); VulkanExampleBase::submitFrame();
} }
@ -222,12 +223,8 @@ public:
{ {
if (!prepared) if (!prepared)
return; return;
draw();
}
virtual void viewChanged()
{
updateUniformBuffers(); updateUniformBuffers();
draw();
} }
}; };