OpenGL pixel transfer inside GPU

236 Views Asked by At

I have been working with VR recently and encountered some OpenGL related problem. The API i use for VR capture a video stream and write it to a texture, I, then, want to submit this texture to a headset. But there is an incompatibility in the API : the texture I get from the stream has an undefined internal format and cannot be submitted to the headset directly.

I am working on a workaround, for now, I have used a GPU -> CPU -> GPU transfer : I read the first texture pixel (with glReadPixels) and write them into a buffer, then I use this buffer to create a texture with the correct format. This works fine but has some latencies due to data transfers.

I have been trying to do a direct GPU copy but failed :

  • I tried using PBO but have problems with invalid operation (following http://www.songho.ca/opengl/gl_pbo.html), here is the code with a invalid operation on glReadPixels

    // Initialization           
    glGenBuffers(1, &pbo);
    glGenTextures(1, &dstTexture);
    glBindTexture(GL_TEXTURE_2D, dstTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glBindTexture(GL_TEXTURE_2D, 0);
    
    // Copy to GPU
    glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
    glBufferData(GL_PIXEL_PACK_BUFFER, bufferSize, NULL, GL_DYNAMIC_DRAW);
    
    glBindTexture(GL_TEXTURE_2D, id);  // texture given by the API
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    
    glBindTexture(GL_TEXTURE_2D, 0);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
    glBufferData(GL_PIXEL_UNPACK_BUFFER, bufferSize, NULL, GL_DYNAMIC_READ);
    glBindTexture(GL_TEXTURE_2D, dstTexture);
    
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    
  • I tried using FBO but encountered pointer exceptions.

  • glCopyImageSubData does not work because the first texture internal format is not recognised.

What are the steps to do a direct GPU copy?

0

There are 0 best solutions below