Sharing NvBuffer contents via NvBufferMemMapEx()


I would like to share the contents of a NvBuffer in process A with another process B. If I understand the documentation correctly, the NvBufferMemMapEx() function combined with the various NvBufferMemSync* functions is the way to go. I am, however, not quite sure how to do this right and can’t seem to make this work, because I do not know which function to call when and with what arguments.

What I basically want is to have a shared view on the buffer between the processes, so that when I update the buffer in process A, process B can work with the updated buffer, without copying the content.

Currently, my code looks something like this:

Process A:

 // Some function to generate the buffer
int bufferFDProcessA = getBuffer(); 

// Send the NvBufferParams und NvBufferParamsEx struct objects of this buffer to process B

// Wait for process B to to call NvBufferMemMapEx()

// Update buffer contents
void* bufferPtr = nullptr;
NvBufferMemMap(bufferFDProcessA , 0, NvBufferMem_Write, &bufferPtr);
((char*)bufferPtr)[0] = 1;
NvBufferMemSyncForDevice(bufferFD, 0, &bufferPtr)

Process B:

// Receive buffer params from process A
NvBufferParams paramsA  = receiveParamsFromProcessA();
NvBufferParamsEx paramsExA  = receiveParamsExFromProcessA();

// Unclear: what buffer fd to use...Create new buffer or use fd from process A?
int bufferFD = ...
void* bufferPtr = nullptr;
NvBufferMemMapEx(bufferFD, &(paramsExA), 0, NvBufferMem_Read, &bufferPtr)

// Notify process A that memory mapping is done and wait a bit

// Synchronize and get new data
NvBufferMemSyncForCpuEx(bufferFD, &(bufferParamsEx.paramsEx), 0, &bufferPtr)
char value = ((char*) bufferPtr)[0]; // Should be 1

Currently this is not working and I am honestly not quite sure what to do here, since the change of the buffer in process A is not reflected in the buffer of process B. I am also not quite sure, which buffer descriptor to use when calling NvBufferMemMapEx(). Do I use the FD from process A, which can be accesses via NvBufferParams.dmabuf_fd? Or do I have create a new buffer in process B and use the FD of that?

The API documentation is not really clear at this point and I can’t find a single example code snippet anywhere, where these functions have been used before. Can anyone help me with this?

There are discussion in the topic:
How to share the buffer in process context?

Please take a look.

thank you for your reply! I saw that discussion, but I’m not quite sure that NvBufferTransformEx is what I want. If I understand that function correctly, it copys the data from one buffer to another, while applying some transformations in the process. I would like, however, to avoid any copies, if possible.

Or did I maybe misunderstand what the function does?


I just wanted to ask, if there is maybe someone who could help with this issue. It’s still an open question for me, whether NvBufferMemMapEx does, what I think it is supposed to be doing.

By now I know that NvBufferTransformEx is definitely too slow for our purposes.

There is a buffer copy but it is done on hardware engine, so the performance shall be good and don’t take CPU usage.