How To Create Array of Structs?
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Sep 2 09:56:19 UTC 2021
Hi Michael,
very interesting experiments - thanks for reaching out.
Eyeballing your code, I zoomed into this line:
https://github.com/brcolow/java-vulkan/blob/master/src/main/java/com/brcolow/game/Vulkan.java#L593
Here you want to set the `pStages` pointer, a pointer to a memory region
which presumably has to contain exactly `stageCount` structs of type
`VkPipelineShaderStageCreateInfo`.
I believe there's an extra getAddress in there; this:
```
VkGraphicsPipelineCreateInfo.pStages$set(pPipelineCreateInfo,
MemoryAccess.getAddress(pStages));
```
should be:
```
VkGraphicsPipelineCreateInfo.pStages$set(pPipelineCreateInfo, pStages);
```
That is, you want to pass the pointer to the `pStages` region you just
created, so that Vulkan will be able to see all elements. If you call
"MemoryAccess::getAddress" your code will read the first 64 bits from
the `pStages` region, turn those into a MemoryAddress, and set that into
the enclosing `pPipelineCreateInfo` struct. Which I don't think is what
you want?
Let me know if this works.
Cheers
Maurizio
On 02/09/2021 06:35, Michael Ennen wrote:
> I have been doing more exploring with the panama-foreign, ffi, and jextract
> branches - this time messing around with seeing if I can get a rainbow
> triangle to be rendered on the screen with Vulkan.
>
> I am really close. There is one problem I am having which is how to create
> an array of structs?
>
> See this, for example:
>
> https://github.com/brcolow/java-vulkan/blob/master/src/main/java/com/brcolow/game/Vulkan.java#L581
>
> If I set the stageCount to 2, then I get a validation exception about the
> second shader module having bogus values. I believe this is caused by me
> creating the array of structs incorrectly.
>
> I am trying to set the pStages element of this struct:
> https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkGraphicsPipelineCreateInfo.html
>
> to an array containing pVertShaderStageInfo and pFragShaderStageInfo.
>
> My attempt (I have tried other ways that also fail) to do that is:
>
> MemorySegment pStages = SegmentAllocator.ofScope(scope).allocateArray(
> C_POINTER, new Addressable[]{
> pVertShaderStageInfo, pFragShaderStageInfo});
> VkGraphicsPipelineCreateInfo.pStages$set(pPipelineCreateInfo,
> MemoryAccess.getAddress(pStages));
>
>
> If anyone could assist me with the proper way to create the
> VkPipelineShaderStageCreateInfo* array containing pVertShaderStageInfoand
> pFragShaderStageInfo - it would be greatly appreciated!
>
More information about the panama-dev
mailing list