LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: bcbradle on April 25, 2017, 04:13:14

Title: How can you know what goes in a PointerBuffer?
Post by: bcbradle on April 25, 2017, 04:13:14
There isn't any template or type information for PointerBuffer, it is difficult to understand what is actually put inside it by looking at the docs.

I would imagine that the char * const * of the vulkan specification for the ppEnabledExtensionNames field of the VkDeviceCreateInfo struct implies that PointerBuffers like the ppEnabledExtensionNames argument in the set method of the VkDeviceCreateInfo class actually contain pointers to null terminated strings.

However I'm interested in what goes inside the PointerBuffers of:
ppData argument of vkMapMemory (void** in c)
pDataSize argument of vkGetPipelineCacheData (size_t* in c)


Title: Re: How can you know what goes in a PointerBuffer?
Post by: spasi on April 25, 2017, 06:37:22
actually contain pointers to null terminated strings.

Correct.

ppData argument of vkMapMemory (void** in c)

You don't put anything in ppData, it's an output parameter. From the javadoc: "points to a pointer in which is returned a host-accessible pointer to the beginning of the mapped range". You simply pass a PointerBuffer with capacity 1. Example code:

Code: [Select]
try (MemoryStack stack = stackPush()) {
    PointerBuffer ppData = stack.callocPointer(1);
    vkMapMemory(device, memory, offset, size, flags, ppData);
    // omitted: error handling
    ByteBuffer data = ppData.get(0, size);
}

pDataSize argument of vkGetPipelineCacheData (size_t* in c)

Similar to the above, but pDataSize is an input/output parameter. Again, from the javadoc: "If pData is NULL, then the maximum size of the data that can be retrieved from the pipeline cache, in bytes, is returned in pDataSize. Otherwise, pDataSize must point to a variable set by the user to the size of the buffer, in bytes, pointed to by pData, and on return the variable is overwritten with the amount of data actually written to pData."

There isn't any template or type information for PointerBuffer, it is difficult to understand what is actually put inside it by looking at the docs.

LWJGL makes no effort to make PointerBuffer type-safe. Two reasons: a) PointerBuffer existed in LWJGL 2 and we wanted to be compatible and b) Pointers/handles are represented as longs in LWJGL, there are no classes available to be used as type parameters. The only pointer types that are type-safe in LWJGL are pointers to structs/struct-arrays and callbacks. PointerBuffers are similar to ByteBuffers; the user decides what the bytes in a ByteBuffer represent, just like they decide what the pointers in a PointerBuffer point to.

This is all going to change in LWJGL 4, which is planned to be released when Project Panama (http://openjdk.java.net/projects/panama/) and Project Valhalla (http://openjdk.java.net/projects/valhalla/) are available. These two projects combined are the holy grail when it comes to Java-native interop; zero overhead native calls, combined with zero-overhead and type-safe pointer wrappers.