Skip to content

Commit

Permalink
examples: Make hello-world example exit gracefully if VK cannot be in…
Browse files Browse the repository at this point in the history
…itialized

- Check if VK API function pointers are valid before using them
- Return 0 and exit if VK initialization fails
- Enable hello-world example
  • Loading branch information
aleino-nv committed Jan 2, 2025
1 parent e3b71cf commit 7c3d79d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
29 changes: 19 additions & 10 deletions examples/hello-world/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct HelloWorldExample : public TestBase
{
// The Vulkan functions pointers result from loading the vulkan library.
VulkanAPI vkAPI;
bool vkAPIValid = false;

// Vulkan objects used in this example.
VkQueue queue;
Expand Down Expand Up @@ -80,7 +81,11 @@ int main(int argc, char* argv[])

int HelloWorldExample::run()
{
RETURN_ON_FAIL(initVulkanInstanceAndDevice());
// If VK failed to initialize, skip running but return success anyway.
// This allows our automated testing to distinguish between essential failures and the
// case where the application is just not supported.
if (int result = initVulkanInstanceAndDevice())
return vkAPIValid ? result : 0;
RETURN_ON_FAIL(createComputePipelineFromShader());
RETURN_ON_FAIL(createInOutBuffers());
RETURN_ON_FAIL(dispatchCompute());
Expand All @@ -95,6 +100,7 @@ int HelloWorldExample::initVulkanInstanceAndDevice()
printf("Failed to load Vulkan.\n");
return -1;
}
vkAPIValid = true;

VkCommandPoolCreateInfo poolCreateInfo = {};
poolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Expand Down Expand Up @@ -511,15 +517,18 @@ int HelloWorldExample::printComputeResults()

HelloWorldExample::~HelloWorldExample()
{
vkAPI.vkDestroyPipeline(vkAPI.device, pipeline, nullptr);
for (int i = 0; i < 3; i++)
if (vkAPIValid)
{
vkAPI.vkDestroyBuffer(vkAPI.device, inOutBuffers[i], nullptr);
vkAPI.vkFreeMemory(vkAPI.device, bufferMemories[i], nullptr);
vkAPI.vkDestroyPipeline(vkAPI.device, pipeline, nullptr);
for (int i = 0; i < 3; i++)
{
vkAPI.vkDestroyBuffer(vkAPI.device, inOutBuffers[i], nullptr);
vkAPI.vkFreeMemory(vkAPI.device, bufferMemories[i], nullptr);
}
vkAPI.vkDestroyBuffer(vkAPI.device, stagingBuffer, nullptr);
vkAPI.vkFreeMemory(vkAPI.device, stagingMemory, nullptr);
vkAPI.vkDestroyPipelineLayout(vkAPI.device, pipelineLayout, nullptr);
vkAPI.vkDestroyDescriptorSetLayout(vkAPI.device, descriptorSetLayout, nullptr);
vkAPI.vkDestroyCommandPool(vkAPI.device, commandPool, nullptr);
}
vkAPI.vkDestroyBuffer(vkAPI.device, stagingBuffer, nullptr);
vkAPI.vkFreeMemory(vkAPI.device, stagingMemory, nullptr);
vkAPI.vkDestroyPipelineLayout(vkAPI.device, pipelineLayout, nullptr);
vkAPI.vkDestroyDescriptorSetLayout(vkAPI.device, descriptorSetLayout, nullptr);
vkAPI.vkDestroyCommandPool(vkAPI.device, commandPool, nullptr);
}
1 change: 0 additions & 1 deletion tests/expected-example-failure-github.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ macos:aarch64:(debug|release):hello-world # See issue 5520
macos:aarch64:(debug|release):model-viewer # See issue 5520
macos:aarch64:(debug|release):ray-tracing # See issue 5520
macos:aarch64:(debug|release):ray-tracing-pipeline # See issue 5520
windows:x86_64:debug:hello-world # See issue 5520
windows:x86_64:debug:ray-tracing # See issue 5520
windows:x86_64:debug:ray-tracing-pipeline # See issue 5520

0 comments on commit 7c3d79d

Please sign in to comment.