Skip to content

Commit

Permalink
[Vulkan improvements]
Browse files Browse the repository at this point in the history
* Implement Vulkan dynamic rendering support
*
Implement descriptor update templates support
  • Loading branch information
milkru committed Aug 13, 2022
1 parent 3f18b70 commit 7073bcb
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 240 deletions.
61 changes: 53 additions & 8 deletions src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static VkInstance createInstance()
applicationInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
applicationInfo.pEngineName = "vulkanizer";
applicationInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
applicationInfo.apiVersion = VK_API_VERSION_1_2;
applicationInfo.apiVersion = VK_API_VERSION_1_3;

VkInstanceCreateInfo instanceCreateInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
instanceCreateInfo.pApplicationInfo = &applicationInfo;
Expand Down Expand Up @@ -123,6 +123,47 @@ static uint32_t tryGetGraphicsQueueFamilyIndex(
return ~0u;
}

static const char* kRequiredDeviceExtensions[] =
{
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME,
VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME
};

static bool isDeviceExtensionAvailable(
const std::vector<VkExtensionProperties>& _availableExtensions,
const char* _extensionName)
{
for (const VkExtensionProperties& availableExtension : _availableExtensions)
{
if (strcmp(availableExtension.extensionName, _extensionName) == 0)
{
return true;
}
}

return false;
}

static bool areRequiredDeviceExtensionSupported(const VkPhysicalDevice inDevice)
{
uint32_t extensionCount;
vkEnumerateDeviceExtensionProperties(inDevice, nullptr, &extensionCount, nullptr);

std::vector<VkExtensionProperties> availableExtensions(extensionCount);
vkEnumerateDeviceExtensionProperties(inDevice, nullptr, &extensionCount, availableExtensions.data());

for (const char* requiredExtensionName : kRequiredDeviceExtensions)
{
if (!isDeviceExtensionAvailable(availableExtensions, requiredExtensionName))
{
return false;
}
}

return true;
}

static VkPhysicalDevice tryPickPhysicalDevice(
VkInstance _instance,
VkSurfaceKHR _surface)
Expand Down Expand Up @@ -154,6 +195,11 @@ static VkPhysicalDevice tryPickPhysicalDevice(
continue;
}

if (!areRequiredDeviceExtensionSupported(potentialPhysicalDevice))
{
continue;
}

physicalDevice = potentialPhysicalDevice;
break;
}
Expand Down Expand Up @@ -186,12 +232,7 @@ static VkDevice createDevice(
uint32_t _queueFamilyIndex,
bool _bMeshShadingSupported)
{
std::vector<const char*> deviceExtensions =
{
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME
};

std::vector<const char*> deviceExtensions(kRequiredDeviceExtensions, std::end(kRequiredDeviceExtensions));
if (_bMeshShadingSupported)
{
deviceExtensions.push_back(VK_NV_MESH_SHADER_EXTENSION_NAME);
Expand All @@ -217,6 +258,9 @@ static VkDevice createDevice(
deviceFeatures12.shaderFloat16 = VK_TRUE;
deviceFeatures12.shaderInt8 = VK_TRUE;

VkPhysicalDeviceDynamicRenderingFeaturesKHR dynamicRenderingFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_FEATURES_KHR };
dynamicRenderingFeatures.dynamicRendering = VK_TRUE;

VkPhysicalDeviceMeshShaderFeaturesNV meshShaderFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_NV };
meshShaderFeatures.taskShader = VK_TRUE;
meshShaderFeatures.meshShader = VK_TRUE;
Expand All @@ -230,10 +274,11 @@ static VkDevice createDevice(
deviceCreateInfo.pNext = &deviceFeatures2;
deviceFeatures2.pNext = &deviceFeatures11;
deviceFeatures11.pNext = &deviceFeatures12;
deviceFeatures12.pNext = &dynamicRenderingFeatures;

if (_bMeshShadingSupported)
{
deviceFeatures12.pNext = &meshShaderFeatures;
dynamicRenderingFeatures.pNext = &meshShaderFeatures;
}

VkDevice device;
Expand Down
37 changes: 21 additions & 16 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ struct GUI
std::array<Buffer, kMaxFramesInFlightCount> indexBuffers;
VkSampler fontSampler;
Image fontImage;
VkDescriptorSetLayout descriptorSetLayout;
VkPipelineLayout pipelineLayout;
VkDescriptorUpdateTemplate descriptorUpdateTemplate;
VkPipeline graphicsPipeline;
VkDescriptorSetLayout descriptorSetLayout;
};

static GUI& getGUI()
Expand Down Expand Up @@ -105,7 +106,8 @@ static void createFontTexture()
}

static void createPipeline(
VkRenderPass _renderPass,
VkFormat _colorFormat,
VkFormat _depthFormat,
std::vector<Shader> _shaders)
{
GUI& gui = getGUI();
Expand Down Expand Up @@ -209,6 +211,11 @@ static void createPipeline(
dynamicStateCreateInfo.dynamicStateCount = ARRAY_SIZE(dynamicStates);
dynamicStateCreateInfo.pDynamicStates = dynamicStates;

VkPipelineRenderingCreateInfoKHR pipelineRenderingCreateInfo = { VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR };
pipelineRenderingCreateInfo.colorAttachmentCount = 1;
pipelineRenderingCreateInfo.pColorAttachmentFormats = &_colorFormat;
pipelineRenderingCreateInfo.depthAttachmentFormat = _depthFormat;

VkGraphicsPipelineCreateInfo pipelineCreateInfo = { VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO };
pipelineCreateInfo.stageCount = uint32_t(shaderStageCreateInfos.size());
pipelineCreateInfo.pStages = shaderStageCreateInfos.data();
Expand All @@ -221,17 +228,20 @@ static void createPipeline(
pipelineCreateInfo.pDepthStencilState = &depthStencilStateCreateInfo;
pipelineCreateInfo.pDynamicState = &dynamicStateCreateInfo;
pipelineCreateInfo.layout = gui.pipelineLayout;
pipelineCreateInfo.renderPass = _renderPass;
pipelineCreateInfo.renderPass = VK_NULL_HANDLE;
pipelineCreateInfo.pNext = &pipelineRenderingCreateInfo;
pipelineCreateInfo.subpass = 0;
pipelineCreateInfo.basePipelineIndex = -1;
pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;


VK_CALL(vkCreateGraphicsPipelines(gui.device.deviceVk, VK_NULL_HANDLE, 1, &pipelineCreateInfo, nullptr, &gui.graphicsPipeline));
}

void initializeGUI(
Device _device,
VkRenderPass _renderPass,
VkFormat _colorFormat,
VkFormat _depthFormat,
float _windowWidth,
float _windowHeight)
{
Expand Down Expand Up @@ -261,16 +271,17 @@ void initializeGUI(
Shader vertexShader = createShader(gui.device, "shaders/gui.vert.spv");
Shader fragmentShader = createShader(gui.device, "shaders/gui.frag.spv");

gui.descriptorSetLayout = createDescriptorSetLayout(gui.device.deviceVk, { vertexShader, fragmentShader });

VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(gui.pushConstantBlock);

gui.descriptorSetLayout = createDescriptorSetLayout(gui.device.deviceVk, { vertexShader, fragmentShader });
gui.pipelineLayout = createPipelineLayout(gui.device.deviceVk, { gui.descriptorSetLayout }, { pushConstantRange });
gui.descriptorUpdateTemplate = createDescriptorUpdateTemplate(gui.device.deviceVk,
gui.descriptorSetLayout, gui.pipelineLayout, VK_PIPELINE_BIND_POINT_GRAPHICS, { vertexShader, fragmentShader });

createPipeline(_renderPass, { vertexShader, fragmentShader });
createPipeline(_colorFormat, _depthFormat, { vertexShader, fragmentShader });

destroyShader(gui.device, fragmentShader);
destroyShader(gui.device, vertexShader);
Expand All @@ -295,6 +306,7 @@ void terminateGUI()
destroyImage(gui.device, gui.fontImage);

vkDestroyPipeline(gui.device.deviceVk, gui.graphicsPipeline, nullptr);
vkDestroyDescriptorUpdateTemplate(gui.device.deviceVk, gui.descriptorUpdateTemplate, nullptr);
vkDestroyPipelineLayout(gui.device.deviceVk, gui.pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(gui.device.deviceVk, gui.descriptorSetLayout, nullptr);

Expand Down Expand Up @@ -496,15 +508,8 @@ void drawFrameGUI(
fontImageDescriptorInfo.imageView = gui.fontImage.view;
fontImageDescriptorInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;

VkWriteDescriptorSet writeDescriptorSets[1]{};
writeDescriptorSets[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSets[0].dstBinding = 0;
writeDescriptorSets[0].descriptorCount = 1;
writeDescriptorSets[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
writeDescriptorSets[0].pImageInfo = &fontImageDescriptorInfo;

vkCmdPushDescriptorSetKHR(_commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
gui.pipelineLayout, 0, ARRAY_SIZE(writeDescriptorSets), writeDescriptorSets);
vkCmdPushDescriptorSetWithTemplateKHR(_commandBuffer,
gui.descriptorUpdateTemplate, gui.pipelineLayout, 0, &fontImageDescriptorInfo);

ImDrawData* pDrawData = ImGui::GetDrawData();
int32_t globalIndexOffset = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

void initializeGUI(
Device _device,
VkRenderPass _renderPass,
VkFormat _colorFormat,
VkFormat _depthFormat,
float _width,
float _height);

Expand Down
Loading

0 comments on commit 7073bcb

Please sign in to comment.