How can I access to device memory in nvbufsurface?

- Board:
    * Jetpack:        4.3 [L4T 32.3.1]
    * Type:           AGX Xavier
    * Name:           NVIDIA Jetson AGX Xavier
    * GPU-Arch:       7.2
  - Libraries:
    * cuDNN:          7.6.3.28-1+cuda10.0
    * VisionWorks:    1.6.0.500n
    * OpenCV:         4.1.2 compiled CUDA: YES
    * CUDA:           10.0.326
    * TensorRT:       6.0.1.10-1+cuda10.0

Hi.

I want to access to device memory in nvbufsurface.
I access to cpu memory like below.

GstMapInfo in_map_info;
NvBufSurface* in_surf = nullptr;

if (!gst_buffer_map(inbuf, &in_map_info, GST_MAP_READ))
    return GST_FLOW_ERROR;
in_surf = (NvBufSurface*) (in_map_info.data);

NvBufSurfaceMap(in_surf, -1, -1, NVBUF_MAP_READ);
NvBufSurfaceSyncForCpu(in_surf, -1, -1);

Mat mat = Mat(in_surf->surfaceList[0].height, in_surf->surfaceList[0].width, CV_8UC4,
    in_surf->surfaceList[0].mappedAddr.addr[0], in_surf->surfaceList[0].pitch);
cvtColor(mat, mat, COLOR_RGBA2BGR);

But, I want to access directly device memory and use cuda functions.
How can I do?

Thanks.

Hi,

You can find some information in this header:
/opt/nvidia/deepstream/deepstream-4.0/sources/includes/nvbufsurface.h

Please map the surface with NvBufSurfaceMapEglImage.
And the pointer is surf->surfaceList->mappedAddr->eglImage.

Thanks.

Hi,

I saw that and my code is like below.

NvBufSurfaceMapEglImage(in_surf, 0);
CUeglFrame eglFrame;
CUgraphicsResource pResource = NULL;    
cuGraphicsEGLRegisterImage(&pResource, in_surf->surfaceList[i].mappedAddr.eglImage, 
    CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
cuGraphicsResourceGetMappedEglFrame(&eglFrame, pResource, 0, 0);
cuCtxSynchronize();
cuda::GpuMat d_mat(in_surf->surfaceList[0].height, in_surf->surfaceList[0].width, CV_8UC4, eglFrame.frame.pPitch[0]);

Is it the right way?

Is “eglFrame.frame.pPitch” the device memory pointer?

Thanks.

Hi,

Could you try this:

CUgraphicsResource resource;
CUeglFrame eglFrame;

NvBufSurfaceMapEglImage(surface, -1);
cuGraphicsEGLRegisterImage(&resource, surface->surfaceList[0].mappedAddr.eglImage, CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
cuGraphicsResourceGetMappedEglFrame(&eglFrame, resource, 0, 0);

void* gpu_ptr;
gpu_ptr = eglFrame.frame.pPitch[0];
...

Thanks.

1 Like

Hi,
Please also check
https://devtalk.nvidia.com/default/topic/1047620/deepstream-sdk/how-to-create-opencv-gpumat-from-nvstream-/post/5397368/#5397368
The sample involves OpenCV. You may ignore it and check how NvBufSurf/cuGraphics APIs work.

1 Like