简体   繁体   English

将 GPU 上的 OpenGL 图像从 C++ 转移到 ZA7F5F35426B927411FC9231B5638217

[英]Transfer OpenGL image on GPU from C++ to Python for deep learning

I built a simulator in C++ with a pybind11 interface to run deep learning in Python using PyTorch.我在 C++ 中使用 pybind11 接口构建了一个模拟器,以使用 PyTorch 在 Python 中运行深度学习。 At each time step, I draw certain things from the simulator's scene using the SFML library (wrapper around openGL).在每个时间步,我使用 SFML 库(openGL 的包装器)从模拟器的场景中绘制某些东西。 I draw that on a texture, then get the pixels from that texture as follows:我在纹理上绘制它,然后从该纹理中获取像素,如下所示:

glBindTexture(GL_TEXTURE_2D, imageTexture.getTexture().getNativeHandle());
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, img.data());

I then move the pixels vector img from C++ to Python using the pybind11 interface.然后我使用 pybind11 接口将像素向量img从 C++ 移动到 Python 。 Problem is that this GPU-to-CPU operation is very slow.问题是这种 GPU 到 CPU 的操作非常慢。 Since the vector in Python is then transferred back to the GPU for fast deep learning (CNN) operations, I was wondering how I could avoid that step.由于 Python 中的向量随后被转移回 GPU 以进行快速深度学习 (CNN) 操作,因此我想知道如何避免该步骤。

My best guess so far is that I should at each step bind the texture in C++ (as in the code above), then right after that in the Python get the bound texture using CUDA, while keeping it on the GPU. My best guess so far is that I should at each step bind the texture in C++ (as in the code above), then right after that in the Python get the bound texture using CUDA, while keeping it on the GPU. However I couldn't figure out how to do that, I don't know much about GPUs and how CUDA/OpenGL work.但是我不知道该怎么做,我不太了解 GPU 以及 CUDA/OpenGL 的工作原理。 Pointers to the right direction would be very appreciated!指向正确方向的指针将不胜感激!

You should use PBO(Pixel Buffer Object) for this operation.您应该为此操作使用 PBO(像素缓冲区对象)。

Data transferring operation is very fast using PBO使用 PBO 的数据传输操作非常快

https://www.khronos.org/opengl/wiki/Pixel_Buffer_Object https://www.khronos.org/opengl/wiki/Pixel_Buffer_Object

GLuint w_pbo[2];

 // Create pbo objects and than

 // Do your drawings.

int w_readIndex = 0;
int w_writeIndex = 1;
glReadBuffer(GL_COLOR_ATTACHMENT0);
w_writeIndex = (w_writeIndex + 1) % 2;
w_readIndex = (w_readIndex + 1) % 2;
glBindBuffer(GL_PIXEL_PACK_BUFFER, w_pbo[w_writeIndex]);
// copy from framebuffer to PBO asynchronously. it will be ready in the NEXT frame
glReadPixels(0, 0, SCR_WIDTH, SCR_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
// now read other PBO which should be already in CPU memory
glBindBuffer(GL_PIXEL_PACK_BUFFER, w_pbo[w_readIndex]);
unsigned char* downsampleData = (unsigned char*)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);

Now you can use unsigned char* downsampleData to build the texture memory现在您可以使用 unsigned char* downsampleData 构建纹理 memory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM