I have a Python project which requires a pointer to GPU memory using OpenGL
So far I have attempted the following:
import numpy as np
from OpenGL.GL import *
import cv2
import pygame
height = 480
width = 640
#pygame
#-----------------------
pygame.init()
pygame.display.set_caption('Texture Receiver Example')
pygame.display.set_mode((width, height),
pygame.OPENGL | pygame.DOUBLEBUF)
TextureID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, TextureID)
#--------------------------------
pbo = glGenBuffers(1) # create a pixel buffer object
openglframe =np.random.randint(low=0,high=255,size=(height,width,4)) # array with the resolution of the image I want to use, in 4 channels for RGBA, used for the byte size of glBufferData
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo)
glBufferData(GL_PIXEL_PACK_BUFFER,openglframe , GL_DYNAMIC_READ) # configures the pixel buffer
openglcudamatcudamat = cv2.cuda.GpuMat(height,width,cv2.CV_8UC3) # temporary texture used for mapping the address from OpenGL as a CUDA GpuMat
outputcudamat = cv2.cuda.GpuMat(height,width,cv2.CV_8UC3) # the OpenGL texture should end up as this CUDA GpuMat
glActiveTexture(GL_TEXTURE0) # activate the OpenGL texture slot
glBindTexture(GL_TEXTURE_2D, TextureID) # selects the texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, openglframe) # Load the image into OpenGL
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo) #selects the pixel buffer
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,0)
# After this is the Python part
mapdata = glMapBuffer(GL_PIXEL_PACK_BUFFER,GL_READ_ONLY) # returns a ctype pointer of the pixel buffer
openglcudamat=cv2.cuda.createGpuMatFromCudaMemory(rows=height,cols=width,type=cv2.CV_8UC4,cudaMemoryAddress=mapdata)
openglcudamat.copyTo(outputcudamat) # copy the OpenGL texture from temporary CUDA GpuMat
# And finally, unmap the pixel buffer in OpenGl
glUnmapBuffer(GL_PIXEL_PACK_BUFFER)
outputimage = outputcudamat.download()
cv2.imshow("test",outputimage)
print (outputimage)
the line in question is
mapdata = glMapBuffer(GL_PIXEL_PACK_BUFFER,GL_READ_ONLY)
does this return a pointer to system memory, or to GPU memory?