LWJGL Forum

Programming => Vulkan => Topic started by: juha on February 24, 2020, 10:51:25

Title: Should vkGetInstanceProcAddr() accept null argument for instance parameter?
Post by: juha on February 24, 2020, 10:51:25
Hello all,

First post here, on a journey to learn Vulkan. I've hit an issue I need some help with.

According to Vulkan spec, the vkGetInstanceProcAddr() should allow a NULL Vulkan instance argument when querying non-instance specific commands:

Quote
3.1. Command Function Pointers

PFN_vkVoidFunction vkGetInstanceProcAddr(
    VkInstance                                  instance,
    const char*                                 pName);

instance is the instance that the function pointer will be compatible with, or NULL for commands not dependent on any instance.


I am attempting to query the presence of vkEnumerateInstanceVersion() which was added to Vulkan 1.1 and is not present in Vulkan 1.0.

According to Javadoc: https://javadoc.lwjgl.org/org/lwjgl/vulkan/VK10.html#nvkGetInstanceProcAddr(org.lwjgl.vulkan.VkInstance,long) (https://javadoc.lwjgl.org/org/lwjgl/vulkan/VK10.html#nvkGetInstanceProcAddr(org.lwjgl.vulkan.VkInstance,long))

vkGetInstanceProcAddr behavior


instance                    pName                                          return value
---------------------------------------------------------------------------------------------
*                           NULL                                          undefined
invalid instance            *                                             undefined
NULL                        EnumerateInstanceVersion                      fp



Also from https://javadoc.lwjgl.org/org/lwjgl/vulkan/VkApplicationInfo.html (https://javadoc.lwjgl.org/org/lwjgl/vulkan/VkApplicationInfo.html)

Quote
Because Vulkan 1.0 implementations may fail with ERROR_INCOMPATIBLE_DRIVER, applications should determine the version of Vulkan available before calling CreateInstance. If the GetInstanceProcAddr returns NULL for EnumerateInstanceVersion, it is a Vulkan 1.0 implementation. Otherwise, the application can call EnumerateInstanceVersion to determine the version of Vulkan.


However, a method call yields a NullPointerException:

Code: [Select]
   
 VK10.vkGetInstanceProcAddr(null, "vkEnumerateInstanceVersion");

Exception in thread "main" java.lang.NullPointerException
at org.lwjgl.vulkan.VK10.nvkGetInstanceProcAddr(VK10.java:3322)
at org.lwjgl.vulkan.VK10.vkGetInstanceProcAddr(VK10.java:3461)


Presumably due to address() being called on a null instance.

Am I misunderstanding how the API should be used, or should a null argument for instance be allowed, and checked?


Title: Re: Should vkGetInstanceProcAddr() accept null argument for instance parameter?
Post by: juha on February 24, 2020, 11:43:51
One rather unpleasant workaround seems to be to force creation of Vulkan instance at zero address, i.e. NULL handle. This requires turning off some basic LWJGL safety checks:

Code: [Select]

Configuration.DISABLE_CHECKS.set(true);

...

long eivAddress = VK10.vkGetInstanceProcAddr(
  new VkInstance(
    VK10.VK_NULL_HANDLE,
    VkInstanceCreateInfo.calloc()
      .sType(VK10.VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO)
  ),
  "vkEnumerateInstanceVersion"
);

if (eivAddress != VK10.VK_NULL_HANDLE)
{
  System.err.println(">> NOT VERSION 1.0");
}

Title: Re: Should vkGetInstanceProcAddr() accept null argument for instance parameter?
Post by: spasi on March 06, 2020, 16:55:45
Hey juha,

Thank you for this post, I'm exploring a way to address this.
Title: Re: Should vkGetInstanceProcAddr() accept null argument for instance parameter?
Post by: spasi on May 07, 2020, 21:38:26
This will be fixed in the next 3.2.4 snapshot.
Title: Re: Should vkGetInstanceProcAddr() accept null argument for instance parameter?
Post by: juha on May 16, 2020, 07:01:25
Excellent. Thanks for your work on this.