Skip to content

Commit

Permalink
Fix some robustness issues in the examples (#5984)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
aleino-nv authored Jan 7, 2025
1 parent 5621ace commit 7d4142e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
9 changes: 8 additions & 1 deletion examples/hello-world/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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++)
{
Expand Down
4 changes: 4 additions & 0 deletions examples/ray-tracing-pipeline/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,17 @@ struct RayTracing : public WindowedAppBase
asDraftBufferDesc.sizeInBytes =
(size_t)accelerationStructurePrebuildInfo.resultDataMaxSize;
ComPtr<IBufferResource> draftBuffer = gDevice->createBufferResource(asDraftBufferDesc);
if (!draftBuffer)
return SLANG_FAIL;
IBufferResource::Desc scratchBufferDesc;
scratchBufferDesc.type = IResource::Type::Buffer;
scratchBufferDesc.defaultState = ResourceState::UnorderedAccess;
scratchBufferDesc.sizeInBytes =
(size_t)accelerationStructurePrebuildInfo.scratchDataSize;
ComPtr<IBufferResource> scratchBuffer =
gDevice->createBufferResource(scratchBufferDesc);
if (!scratchBuffer)
return SLANG_FAIL;

// Build acceleration structure.
ComPtr<IQueryPool> compactedSizeQuery;
Expand Down
4 changes: 4 additions & 0 deletions examples/ray-tracing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,16 @@ struct RayTracing : public WindowedAppBase
asDraftBufferDesc.defaultState = ResourceState::AccelerationStructure;
asDraftBufferDesc.sizeInBytes = accelerationStructurePrebuildInfo.resultDataMaxSize;
ComPtr<IBufferResource> 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<IBufferResource> scratchBuffer =
gDevice->createBufferResource(scratchBufferDesc);
if (!scratchBuffer)
return SLANG_FAIL;

// Build acceleration structure.
ComPtr<IQueryPool> compactedSizeQuery;
Expand Down
5 changes: 2 additions & 3 deletions 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
windows:x86_64:debug:ray-tracing # See issue 5988
windows:x86_64:debug:ray-tracing-pipeline # See issue 5988
1 change: 1 addition & 0 deletions tools/gfx/d3d12/d3d12-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2231,6 +2231,7 @@ Result DeviceImpl::createAccelerationStructure(
IAccelerationStructure** outAS)
{
#if SLANG_GFX_HAS_DXR_SUPPORT
assert(desc.buffer != nullptr);
RefPtr<AccelerationStructureImpl> result = new AccelerationStructureImpl();
result->m_device5 = m_device5;
result->m_buffer = static_cast<BufferResourceImpl*>(desc.buffer);
Expand Down

0 comments on commit 7d4142e

Please sign in to comment.