Results 1 to 10 of 10

Thread: calling glReadPixels from another thread

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2005
    Posts
    4

    calling glReadPixels from another thread

    I am trying to call glReadPixels from a thread that is not my rendering thread and I get no data. I understand it's because the two threads don't have the same rendering context. I have tried sharing my rendering context but it is not allowed. All I need to do is access my frame buffer from another thread and I don't want to manually copy the buffer between the threads. Anyone have any suggestions?

    Thanks in advance

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Ukraine
    Posts
    101

    Re: calling glReadPixels from another thread

    You can pass pointer to memory area from second thread to the thead which own the context, and let the first thread read pixels for you, in such way you'll avoid the unnecessary copy operation. I think your paralelism will not hurt from this, as actually you don't want to read framebuffer data anytime, but just when frame is rendered.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2005
    Posts
    4

    Re: calling glReadPixels from another thread

    This would work, but I would still have to read the frame buffer in my rendering thread. I want o avoid this because I am rendering on the whole screen and the glReadPixels call greatly reduces my frame rate.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Ukraine
    Posts
    101

    Re: calling glReadPixels from another thread

    In any case you can not render something while you read back data, so I doubt that it is possible to gain some performance win from fact that you read from second thread while rendering thread stall and waiting for second thread finish the reading...

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,662

    Re: calling glReadPixels from another thread

    What do you mean with :

    Originally posted by cloza:
    I have tried sharing my rendering context but it is not allowed.
    What prevents you from sharing your contexts ? What OS, gl window manager do you use ? What languages ?

  6. #6
    Junior Member Newbie
    Join Date
    Mar 2005
    Posts
    4

    Re: calling glReadPixels from another thread

    I meant I tried using the same rendering context for my second thread by using glMakeCurrent and I got a error because it was already in use. My code is in C++ on Windows. I am not using straight openGL. I am using a commercial rendering package that is built on openGL.

  7. #7
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,662

    Re: calling glReadPixels from another thread

    There should have a function in order to create your context that allows you to specify another variable for a second shared context.

    Under linux / glx:

    Code :
    GLXContext glXCreateContext (Display *dpy, XVisualInfo *vis, GLXContext share_list, Bool direct);
    Sorry, but I don't know for your commercial product. But there should have almost the same function just because I can see 'glMakeCurrent' that looks like glXMakeCurrent and if I remember well wglMakeCurrent too.
    You can surely find further documentation about opengl and multithreading on this website.

    Hope this helps.

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Feb 2002
    Location
    Bonn, Germany
    Posts
    1,633

    Re: calling glReadPixels from another thread

    It's not possible.

    A single context can't be current in multiple threads.
    "Objects" can be shared between multiple contexts. But glReadPixels is certainly not an object ...

  9. #9
    Junior Member Newbie
    Join Date
    Mar 2005
    Posts
    4

    Re: calling glReadPixels from another thread

    I've found a way to do it but it will only work under Windows. Here's the code:

    Code :
    // create a DC for the screen and create     
    // a memory DC compatible with screen DC
    HDC hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
    HDC hMemDC = CreateCompatibleDC(hScrDC);
     
    // create a bitmap compatible with the screen DC
    HBITMAP hBitmap = CreateCompatibleBitmap(hScrDC, width, height);
     
    // select new bitmap into memory DC
    (HBITMAP*)SelectObject(hMemDC, hBitmap);
    // copy pixel data from screen DC to memory DC 
    BitBlt(hMemDC, origX, origY, width, height, hScrDC, srcOrigX, srcOrigY, SRCCOPY);
     
    BITMAP bmp;
    // get updated bitmap back
    GetObject(hBitmap, sizeof(BITMAP), &bmp);
     
    BITMAPINFO bi;
    // get pixel data in RGB format
    // bi struct specifies the format data
    // pdata will hold pixel data
    GetDIBits(hMemDC, hBitmap, 0, height, pdata, &bi, DIB_RGB_COLORS);
    pdata holds raw RGB so it can be written to bitmap or AVI file. It can be run in another thread or from another application because it uses the screens pixel data.

  10. #10
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,662

    Re: calling glReadPixels from another thread

    Thanks for your sharing ! And you're right, this will work only under Windows and strangely it has nothing to do with wgl.

Similar Threads

  1. Framebuffer scaling before calling glReadPixels
    By Skovsgaard in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-04-2016, 01:36 AM
  2. Calling multiple glReadPixels & glDrawArrays on a FBO, in Opengl ES
    By sanchiski in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 02-17-2014, 05:43 AM
  3. glRotatef() Without Calling It
    By g0bl1n in forum OpenGL: General
    Replies: 5
    Last Post: 11-10-2011, 06:37 PM
  4. calling glutCreateWindow from within a dll.
    By casterman in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 10-02-2003, 07:36 AM
  5. calling 2D in 3D
    By copperdawn in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 05-27-2000, 12:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean