How to Pass Struct by Value?

Jorn Vernee jorn.vernee at oracle.com
Wed Sep 30 09:47:55 UTC 2020


Hi Micheal,

When I look at the d3d12.h header, I only find this declaration of 
GetCPUDescriptorHandleForHeapStart:

         D3D12_CPU_DESCRIPTOR_HANDLE ( STDMETHODCALLTYPE 
*GetCPUDescriptorHandleForHeapStart )(
             ID3D12DescriptorHeap * This);

Which is in ID3D12DescriptorHeapVtbl.

The return type being defined as:

typedef struct D3D12_CPU_DESCRIPTOR_HANDLE
     {
     SIZE_T ptr;
     }     D3D12_CPU_DESCRIPTOR_HANDLE;

I'd expect this to be linked and invoked as:

MemorySegment heapDescriptorVtbl = 
asSegment(ID3D12DescriptorHeap.lpVtbl$get(pHeapDescriptor), 
ID3D12DescriptorHeapVtbl.$LAYOUT()); MemoryAddress 
getCPUDescriptorHandleForHeapStartAddr = 
ID3D12DescriptorHeapVtbl.GetCPUDescriptorHandleForHeapStart$get(heapDescriptorVtbl); 
MethodHandle ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart = 
getInstance().downcallHandle(getCPUDescriptorHandleForHeapStartAddr, 
MethodType.methodType(MemorySegment.class, MemoryAddress.class), 
FunctionDescriptor.of(D3D12_CPU_DESCRIPTOR_HANDLE.$LAYOUT(), 
C_POINTER)); MemorySegment handle = (MemorySegment) 
ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart.invokeExact(pHeapDescriptor.address()); 
// ^^^ don't forget to close this after using

The linker will take care of handling all the ABI details.

HTH,
Jorn

On 30/09/2020 08:36, Michael Ennen wrote:
> I am messing around with DirectX 12 more and I am getting stuck on how to
> call the "GetCPUDescriptorHandleForHeapStart " method. It must take a
> struct by-value and not as a pointer reference.
> https://docs.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12descriptorheap-getcpudescriptorhandleforheapstart
>
> There seems to be a bit of an issue with that method as I see some Github
> issues talking about it's weird ABI, but I don't think I am hitting that
> issue (see https://github.com/halide/Halide/issues/5156 for example).
>
> I have the following:
>
> MemorySegment heapDescriptorVtbl =
> asSegment(ID3D12DescriptorHeap.lpVtbl$get(pHeapDescriptor),
> ID3D12DescriptorHeapVtbl.$LAYOUT());
> MemoryAddress getCPUDescriptorHandleForHeapStartAddr =
> ID3D12DescriptorHeapVtbl.GetCPUDescriptorHandleForHeapStart$get(heapDescriptorVtbl);
> var pRtvHandle = D3D12_CPU_DESCRIPTOR_HANDLE.allocate(scope);
> MethodHandle ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart =
> getInstance().downcallHandle(getCPUDescriptorHandleForHeapStartAddr,
> MethodType.methodType(void.class, MemoryAddress.class, MemoryAddress.class),
> FunctionDescriptor.ofVoid(C_POINTER, C_POINTER));
> ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart.invokeExact(
> pHeapDescriptor.address(), pRtvHandle.address());
>
> But it is not right as I am passing in D3D12_CPU_DESCRIPTOR_HANDLE as a
> pointer.
>
> I tried exploring the Panama API and looking at tests but just didn't find
> what I was looking for (how to pass a struct by value, if that's possible
> with the ABI semantics).
>
> My current work is here:
>
> https://github.com/brcolow/java-dx12/
>
> Thanks very much.


More information about the panama-dev mailing list