Skip to content

Commit

Permalink
Upgrade to Vulkan 1.4
Browse files Browse the repository at this point in the history
This mostly just involves using core push descriptor APIs instead of an
extension; in the future we might use other features like SPIRV module
blobs.
  • Loading branch information
zeux committed Jan 17, 2025
1 parent 08294ca commit 94d4fad
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion extern/volk
26 changes: 19 additions & 7 deletions src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// Synchronization validation is disabled by default in Debug since it's rather slow
#define SYNC_VALIDATION CONFIG_SYNCVAL

// We have a strict requirement for latest Vulkan version to be available
#define API_VERSION VK_API_VERSION_1_4

#ifdef _WIN32
#include <Windows.h>
#endif
Expand All @@ -38,10 +41,14 @@ static bool isLayerSupported(const char* name)

VkInstance createInstance()
{
assert(volkGetInstanceVersion() >= VK_API_VERSION_1_3);
if (volkGetInstanceVersion() < API_VERSION)
{
fprintf(stderr, "ERROR: Vulkan 1.%d instance not found\n", VK_VERSION_MINOR(API_VERSION));
return 0;
}

VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
appInfo.apiVersion = VK_API_VERSION_1_3;
appInfo.apiVersion = API_VERSION;

VkInstanceCreateInfo createInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
createInfo.pApplicationInfo = &appInfo;
Expand Down Expand Up @@ -191,7 +198,7 @@ VkPhysicalDevice pickPhysicalDevice(VkPhysicalDevice* physicalDevices, uint32_t
if (props.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU)
continue;

printf("GPU%d: %s\n", i, props.deviceName);
printf("GPU%d: %s (Vulkan 1.%d)\n", i, props.deviceName, VK_VERSION_MINOR(props.apiVersion));

uint32_t familyIndex = getGraphicsFamilyIndex(physicalDevices[i]);
if (familyIndex == VK_QUEUE_FAMILY_IGNORED)
Expand All @@ -200,7 +207,7 @@ VkPhysicalDevice pickPhysicalDevice(VkPhysicalDevice* physicalDevices, uint32_t
if (!supportsPresentation(physicalDevices[i], familyIndex))
continue;

if (props.apiVersion < VK_API_VERSION_1_3)
if (props.apiVersion < API_VERSION)
continue;

if (ngpu && atoi(ngpu) == i)
Expand Down Expand Up @@ -230,7 +237,7 @@ VkPhysicalDevice pickPhysicalDevice(VkPhysicalDevice* physicalDevices, uint32_t
}
else
{
printf("ERROR: No GPUs found\n");
fprintf(stderr, "ERROR: No compatible GPU found\n");
}

return result;
Expand All @@ -247,7 +254,6 @@ VkDevice createDevice(VkInstance instance, VkPhysicalDevice physicalDevice, uint

std::vector<const char*> extensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME,
};

if (meshShadingSupported)
Expand Down Expand Up @@ -296,6 +302,11 @@ VkDevice createDevice(VkInstance instance, VkPhysicalDevice physicalDevice, uint
features13.synchronization2 = true;
features13.maintenance4 = true;

VkPhysicalDeviceVulkan14Features features14 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_4_FEATURES };
features14.maintenance5 = true;
features14.maintenance6 = true;
features14.pushDescriptor = true;

// This will only be used if meshShadingSupported=true (see below)
VkPhysicalDeviceMeshShaderFeaturesEXT featuresMesh = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT };
featuresMesh.taskShader = true;
Expand All @@ -320,8 +331,9 @@ VkDevice createDevice(VkInstance instance, VkPhysicalDevice physicalDevice, uint
features.pNext = &features11;
features11.pNext = &features12;
features12.pNext = &features13;
features13.pNext = &features14;

void** ppNext = &features13.pNext;
void** ppNext = &features14.pNext;

if (meshShadingSupported)
{
Expand Down
25 changes: 15 additions & 10 deletions src/niagara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void dispatch(VkCommandBuffer commandBuffer, const Program& program, uint32_t th
vkCmdPushConstants(commandBuffer, program.layout, program.pushConstantStages, 0, sizeof(pushConstants), &pushConstants);

if (program.pushDescriptorCount)
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, program.updateTemplate, program.layout, 0, pushDescriptors);
vkCmdPushDescriptorSetWithTemplate(commandBuffer, program.updateTemplate, program.layout, 0, pushDescriptors);

vkCmdDispatch(commandBuffer, getGroupCount(threadCountX, program.localSizeX), getGroupCount(threadCountY, program.localSizeY), 1);
}
Expand Down Expand Up @@ -345,7 +345,8 @@ int main(int argc, const char** argv)
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

VkInstance instance = createInstance();
assert(instance);
if (!instance)
return -1;

volkLoadInstanceOnly(instance);

Expand All @@ -356,7 +357,11 @@ int main(int argc, const char** argv)
VK_CHECK(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices));

VkPhysicalDevice physicalDevice = pickPhysicalDevice(physicalDevices, physicalDeviceCount);
assert(physicalDevice);
if (!physicalDevice)
{
vkDestroyInstance(instance, 0);
return -1;
}

uint32_t extensionCount = 0;
VK_CHECK(vkEnumerateDeviceExtensionProperties(physicalDevice, 0, &extensionCount, 0));
Expand Down Expand Up @@ -1197,7 +1202,7 @@ int main(int argc, const char** argv)
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, tasksubmitPipeline);

DescriptorInfo descriptors[] = { dccb.buffer, dcb.buffer };
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, tasksubmitProgram.updateTemplate, tasksubmitProgram.layout, 0, descriptors);
vkCmdPushDescriptorSetWithTemplate(commandBuffer, tasksubmitProgram.updateTemplate, tasksubmitProgram.layout, 0, descriptors);

vkCmdDispatch(commandBuffer, 1, 1, 1);
}
Expand Down Expand Up @@ -1245,7 +1250,7 @@ int main(int argc, const char** argv)

DescriptorInfo pyramidDesc(depthSampler, depthPyramid.imageView, VK_IMAGE_LAYOUT_GENERAL);
DescriptorInfo descriptors[] = { dcb.buffer, db.buffer, mlb.buffer, mvb.buffer, pyramidDesc, cib.buffer, ccb.buffer };
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, clustercullProgram.updateTemplate, clustercullProgram.layout, 0, descriptors);
vkCmdPushDescriptorSetWithTemplate(commandBuffer, clustercullProgram.updateTemplate, clustercullProgram.layout, 0, descriptors);

CullData passData = cullData;
passData.postPass = postPass;
Expand All @@ -1262,7 +1267,7 @@ int main(int argc, const char** argv)
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, clustersubmitPipeline);

DescriptorInfo descriptors2[] = { ccb.buffer, cib.buffer };
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, clustersubmitProgram.updateTemplate, clustersubmitProgram.layout, 0, descriptors2);
vkCmdPushDescriptorSetWithTemplate(commandBuffer, clustersubmitProgram.updateTemplate, clustersubmitProgram.layout, 0, descriptors2);

vkCmdDispatch(commandBuffer, 1, 1, 1);

Expand Down Expand Up @@ -1323,7 +1328,7 @@ int main(int argc, const char** argv)
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, postPass >= 1 ? clusterpostPipeline : clusterPipeline);

DescriptorInfo descriptors[] = { dcb.buffer, db.buffer, mlb.buffer, mdb.buffer, vb.buffer, cib.buffer, DescriptorInfo(), textureSampler, mtb.buffer };
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, clusterProgram.updateTemplate, clusterProgram.layout, 0, descriptors);
vkCmdPushDescriptorSetWithTemplate(commandBuffer, clusterProgram.updateTemplate, clusterProgram.layout, 0, descriptors);

vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, clusterProgram.layout, 1, 1, &textureSet.second, 0, nullptr);

Expand All @@ -1337,7 +1342,7 @@ int main(int argc, const char** argv)

DescriptorInfo pyramidDesc(depthSampler, depthPyramid.imageView, VK_IMAGE_LAYOUT_GENERAL);
DescriptorInfo descriptors[] = { dcb.buffer, db.buffer, mlb.buffer, mdb.buffer, vb.buffer, mvb.buffer, pyramidDesc, textureSampler, mtb.buffer };
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, meshtaskProgram.updateTemplate, meshtaskProgram.layout, 0, descriptors);
vkCmdPushDescriptorSetWithTemplate(commandBuffer, meshtaskProgram.updateTemplate, meshtaskProgram.layout, 0, descriptors);

vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, meshtaskProgram.layout, 1, 1, &textureSet.second, 0, nullptr);

Expand All @@ -1349,7 +1354,7 @@ int main(int argc, const char** argv)
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, postPass >= 1 ? meshpostPipeline : meshPipeline);

DescriptorInfo descriptors[] = { dcb.buffer, db.buffer, vb.buffer, DescriptorInfo(), DescriptorInfo(), DescriptorInfo(), DescriptorInfo(), textureSampler, mtb.buffer };
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, meshProgram.updateTemplate, meshProgram.layout, 0, descriptors);
vkCmdPushDescriptorSetWithTemplate(commandBuffer, meshProgram.updateTemplate, meshProgram.layout, 0, descriptors);

vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, meshProgram.layout, 1, 1, &textureSet.second, 0, nullptr);

Expand Down Expand Up @@ -1636,7 +1641,7 @@ int main(int argc, const char** argv)
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, debugtextPipeline);

DescriptorInfo descriptors[] = { { swapchainImageViews[imageIndex], VK_IMAGE_LAYOUT_GENERAL } };
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, debugtextProgram.updateTemplate, debugtextProgram.layout, 0, descriptors);
vkCmdPushDescriptorSetWithTemplate(commandBuffer, debugtextProgram.updateTemplate, debugtextProgram.layout, 0, descriptors);

// debug text goes here!
uint64_t triangleCount = pipelineResults[0] + pipelineResults[1] + pipelineResults[2];
Expand Down
4 changes: 2 additions & 2 deletions src/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ static VkDescriptorSetLayout createSetLayout(VkDevice device, Shaders shaders)
}

VkDescriptorSetLayoutCreateInfo setCreateInfo = { VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO };
setCreateInfo.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR;
setCreateInfo.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT;
setCreateInfo.bindingCount = uint32_t(setBindings.size());
setCreateInfo.pBindings = setBindings.data();

Expand Down Expand Up @@ -382,7 +382,7 @@ static VkDescriptorUpdateTemplate createUpdateTemplate(VkDevice device, VkPipeli
createInfo.descriptorUpdateEntryCount = uint32_t(entries.size());
createInfo.pDescriptorUpdateEntries = entries.data();

createInfo.templateType = VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR;
createInfo.templateType = VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS;
createInfo.pipelineBindPoint = bindPoint;
createInfo.pipelineLayout = layout;

Expand Down

0 comments on commit 94d4fad

Please sign in to comment.