0

I am making a project in Vulkan, and I want to use an SSBO modified in the GPU on CPU; but Vulkan doesn't have a function to map the buffer, only have a memory function. I tried everything about MemoryMapping, but nothing worked.

vandench
  • 1,973
  • 3
  • 19
  • 28
XUFAN
  • 1
  • A Vulkan implementation is not required to allow any piece of memory to both be usable as an SSBO and be mappable. You have to ask the implementation to see if some mappable memory (host-visible) can be used as an SSBO. If so, then you can do it; if not, then you can't. Of course, if there's only one type of memory, then the answer has to be yes. – Nicol Bolas Oct 06 '22 at 04:41

1 Answers1

0

With Vulkan, after creating the SSBO memory buffer and specifying memory property flag VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT (which will create the buffer from memory accessible by the system/CPU), use command vkMapMemory() and pass it the void *pointer to use to access the shader block.

The memcpy() command can then be used to read and write data to and from the block (be sure to use fences and avoid reading/writing while the GPU is still using the SSBO).

A quick note on casting and offsetting - whilst using the void pointer to write data to an SSBO with a single memcpy() call is fine, it can't be used to read in the same manner. The pointer has to be cast to the data type in use.

Also, offset arithmetic cannot be performed on void pointers to reach individual structs either.

The data type or struct to which the pointer is cast defines how increment/decrement works - it will do so by the size of said data type and not by bytes in the address (the latter may seem more intuitive).

For example:

(copy the fifth int from a block of ints...)

int theInt;
int *ssboBlockPointer = (int*)vTheSSBOMappedPointer;
memcpy(&theInt, ssboBlockPointer + 5, sizeof(int));

(or copy the 5th struct from a block of structs - offset will move 5 structs)

theStruct oneStruct;
theStruct *ssboBlockPointer = (theStruct*)vTheSSBOMappedPointer;
memcpy(&theStruct , ssboBlockPointer + 5, sizeof(theStruct));
MarvinWS
  • 21
  • 3