How to use OpenCL to write directly to linux framebuffer with zero copy?

I am using OpenCL to process some images and want to use it to write an RGBA image directly to a framebuffer. The workflow is shown below:

1) map framebuffer for user space.

2) create OpenCL buffer using clCreateBuffer with "CL_MEM_ALLOC_HOST_PTR" flags

3) use clEnqueueMapBuffer to map the results to a framebuffer.

However, this will not work. Nothing on the screen. Then I found that the mapped virtual address from the framebuffer does not match the virtual address displayed by OpenCL. Can any body do zero copy data from GPU to framebuffer? Any help on which approach should I use for this?

Some key codes:

if ((fd_fb = open("/dev/fb0", O_RDWR, 0)) < 0) {
    printf("Unable to open /dev/fb0\n");
    return -1;
}
fb0 = (unsigned char *)mmap(0, fb0_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
...
cmDevSrc4 = clCreateBuffer(cxGPUContext, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, sizeof(cl_uchar) * imagesize * 4, NULL, &status);
...
fb0 = (unsigned char*)clEnqueueMapBuffer(cqCommandQueue, cmDevSrc4, CL_TRUE, CL_MAP_READ, 0, sizeof(cl_uchar) * imagesize * 4, 0, NULL, NULL, &ciErr);

      

+3


source to share


1 answer


For a null copy with an existing buffer, you need to use a flag CL_MEM_USE_HOST_PTR

in the function call clCreateBuffer()

. Also, you need to specify a pointer to an existing buffer as the second to the last argument.



I don't know how linux framebuffer works, but it is possible that even with a zero copy from the device to host it results in additional data being copied to the GPU for rendering. This way you can render the OpenCL buffer directly using OpenGL. Check out the extension cl_khr_gl_sharing

for OpenCL.

+1


source







All Articles