From 7d4142e6aca5257602a7d82db5625adbb6fbc832 Mon Sep 17 00:00:00 2001 From: Anders Leino Date: Tue, 7 Jan 2025 12:51:51 +0200 Subject: Fix some robustness issues in the examples (#5984) * examples: Make hello-world example exit gracefully if VK cannot be initialized - Check if VK API function pointers are valid before using them - Return 0 and exit if VK initialization fails - Enable hello-world example * examples: Fixes for ray-tracing examples - Assert that accelleration structure buffer is not nullptr - Check if buffer creation succeeded before proceeding - This makes initialization not hang, but it still fails. Therefore, the test expectations are just updated to point to another issue. - Enable ray-tracing tests on Windows --- examples/hello-world/main.cpp | 9 ++++++++- examples/ray-tracing-pipeline/main.cpp | 4 ++++ examples/ray-tracing/main.cpp | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/hello-world/main.cpp b/examples/hello-world/main.cpp index 7e84d83e5..7e4211b12 100644 --- a/examples/hello-world/main.cpp +++ b/examples/hello-world/main.cpp @@ -80,7 +80,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 (vkAPI.device == VK_NULL_HANDLE) ? 0 : result; RETURN_ON_FAIL(createComputePipelineFromShader()); RETURN_ON_FAIL(createInOutBuffers()); RETURN_ON_FAIL(dispatchCompute()); @@ -511,6 +515,9 @@ int HelloWorldExample::printComputeResults() HelloWorldExample::~HelloWorldExample() { + if (vkAPI.device == VK_NULL_HANDLE) + return; + vkAPI.vkDestroyPipeline(vkAPI.device, pipeline, nullptr); for (int i = 0; i < 3; i++) { diff --git a/examples/ray-tracing-pipeline/main.cpp b/examples/ray-tracing-pipeline/main.cpp index 0fbb857f0..a288e75b5 100644 --- a/examples/ray-tracing-pipeline/main.cpp +++ b/examples/ray-tracing-pipeline/main.cpp @@ -382,6 +382,8 @@ struct RayTracing : public WindowedAppBase asDraftBufferDesc.sizeInBytes = (size_t)accelerationStructurePrebuildInfo.resultDataMaxSize; ComPtr draftBuffer = gDevice->createBufferResource(asDraftBufferDesc); + if (!draftBuffer) + return SLANG_FAIL; IBufferResource::Desc scratchBufferDesc; scratchBufferDesc.type = IResource::Type::Buffer; scratchBufferDesc.defaultState = ResourceState::UnorderedAccess; @@ -389,6 +391,8 @@ struct RayTracing : public WindowedAppBase (size_t)accelerationStructurePrebuildInfo.scratchDataSize; ComPtr scratchBuffer = gDevice->createBufferResource(scratchBufferDesc); + if (!scratchBuffer) + return SLANG_FAIL; // Build acceleration structure. ComPtr compactedSizeQuery; diff --git a/examples/ray-tracing/main.cpp b/examples/ray-tracing/main.cpp index 11529a753..6b908a14e 100644 --- a/examples/ray-tracing/main.cpp +++ b/examples/ray-tracing/main.cpp @@ -373,12 +373,16 @@ struct RayTracing : public WindowedAppBase asDraftBufferDesc.defaultState = ResourceState::AccelerationStructure; asDraftBufferDesc.sizeInBytes = accelerationStructurePrebuildInfo.resultDataMaxSize; ComPtr draftBuffer = gDevice->createBufferResource(asDraftBufferDesc); + if (!draftBuffer) + return SLANG_FAIL; IBufferResource::Desc scratchBufferDesc; scratchBufferDesc.type = IResource::Type::Buffer; scratchBufferDesc.defaultState = ResourceState::UnorderedAccess; scratchBufferDesc.sizeInBytes = accelerationStructurePrebuildInfo.scratchDataSize; ComPtr scratchBuffer = gDevice->createBufferResource(scratchBufferDesc); + if (!scratchBuffer) + return SLANG_FAIL; // Build acceleration structure. ComPtr compactedSizeQuery; -- cgit v1.2.3