The use of Foreign Memory API in game engine
In my project I am using memory segments to store the positions of vertices of the terrain. I alloc a big memory segment that will be used for many tiles (before profiling the allocation took a big percentage of execution time).
In my first version of the game, I used Bytebuffers, in that version I needed to calculate the number of bytes I need to alloc. One of the reasons I tryed use memory segments was the struct layouts,wich I can ask for the size and offsets in the struct:
public static final StructLayout BUFFER_LAYOUT = structLayout( sequenceLayout(N_VERTICES_PER_TILE, sequenceLayout(3, JAVA_FLOAT)).withName(POSITION_ELEMENT), sequenceLayout(N_VERTICES_PER_TILE, sequenceLayout(3, JAVA_FLOAT)).withName(NORMAL_ELEMENT) );
private static final MemorySegment MEMORY = Arena.ofConfined().allocate(structLayout(sequenceLayout(MAX_TILES_STORED, BUFFER_LAYOUT)));
static final long POSITION_OFFSET = BUFFER_LAYOUT.byteOffset(PathElement.groupElement(POSITION_ELEMENT)); static final long NORMAL_OFFSET = BUFFER_LAYOUT.byteOffset(PathElement.groupElement(NORMAL_ELEMENT)); static final long POSITION_SIZE = BUFFER_LAYOUT.select(PathElement.groupElement(POSITION_ELEMENT)).byteSize(); static final long NORMAL_SIZE = BUFFER_LAYOUT.select(PathElement.groupElement(NORMAL_ELEMENT)).byteSize();
The video that make me try to use it and learn a little: https://www.youtube.com/watch?v=lVORyDhQzf8
Discussion in the ATmosphere