Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does vulkan have a separate device handle type? #51

Closed
patrick-han opened this issue Mar 22, 2024 · 2 comments
Closed

Why does vulkan have a separate device handle type? #51

patrick-han opened this issue Mar 22, 2024 · 2 comments

Comments

@patrick-han
Copy link

In the DeviceMangers for DX11 and DX12, the member m_NvrhiDevice is of type nvrhi::DeviceHandle whereas in Vulkan's DeviceManager it is nvrhi::vulkan::DeviceHandle.

However the tutorial when invoking createDevice in the Vulkan section uses nvrhi::DeviceHandle: https://github.com/NVIDIAGameWorks/nvrhi/blob/main/doc/Tutorial.md

I notice if I don't use nvrhi::vulkan::DeviceHandle I cannot invoke functions like queueWaitForSemaphore. And on the other hand since createValidationLayer returns nvrhi::DeviceHandle, I cannot use it for those function calls that rely on the Vulkan handle. What is the intended usage of these handles and why does Vulkan seemingly have different treatment?

@apanteleev
Copy link
Contributor

nvrhi::vulkan::DeviceHandle is just a ref-count handle for nvrhi::vulkan::IDevice, which is derived from nvrhi::IDevice. There are similar derived types for other APIs, like nvrhi::d3d12::IDevice that provide additional functionality compared to regular nvrhi::IDevice. The Vulkan DeviceManager needs some of those additional functions of nvrhi::vulkan::IDevice so it uses that type of handle.

You can trivially upcast a nvrhi::vulkan::DeviceHandle into nvrhi::DeviceHandle, and to downcast, use getNativeObject like so:

nvrhi::vulkan::DeviceHandle nvrhiVulkanDevice = static_cast<nvrhi::vulkan::IDevice*>(
    deviceManager->GetDevice()->getNativeObject(nvrhi::ObjectTypes::Nvrhi_VK_Device));

This call will unwrap the validation layer, if it is present, and return a valid pointer only if the underlying implementation is actually a Vulkan device. Similar calls can be used for other backends (DX11, DX12).

@patrick-han
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants