Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


OpenGL RCs and GDI DCs and SwapBuffers call

Posted on 1998-10-29
Medium Priority
Last Modified: 2012-05-04
According to all the documentation a Win32 window can have one DC and OpenGL needs to create and attach an RC to that DC to render.  I understand you may have multiple RCs but only one can be current at a time.  I create several threads, each creating their own window, each window has a DC and an RC.  The question is when I call SwapBuffers with a DC within one thread do I swap all the RCs of all the threads or just the one for that window/thread?

I have read the red, blue, white and superbible OpenGL books and some advanced Windows32 books.  The question of how DCs and RCs are (in)dependant of a window or thread and how SwapBuffers affects all the existing DCs and RCs is unclear?

Keith Radloff
Question by:kradloff
  • 2
  • 2
LVL 10

Accepted Solution

Lischke earned 300 total points
ID: 1113114
I think the documentation is qite clear in this regard. You only can have one RC current at a given time (for a specific DC). By calling SwapBuffers you only swap fore- and background buffers of that specific DC (which isn't necessarily a window DC but can also be a DC of a DIB, a printer, a meta file etc.).

Since each of your threads has an own window with an own DC/RC, you have to call SwapBuffers for each DC seperately in its painting routine. But you can savely share an RC if you take care of synchronizing access to it (critical section provide an efficient way).

Ciao, Mike

Author Comment

ID: 1113115
The answer you provided makes sense.  However I will disagree that the documentation is quite clear.  I cannot find any reference that in creating a thread/window/DC/RC combination that each DC for each thread/window is unique and/or different.

Could you point out a page and book that documents this?  I have the Red, Blue, Green, White, and SuperBible OpenGL books.

As well as if you have many different RCs based upon the same DC in the same thread, and you call swapbuffers() I would assume that all the RCs swap?  This would require the critical section that you mentioned, correct?

Thanks for your time Mike!

LVL 10

Expert Comment

ID: 1113116
Well, you are right concerning the uniqueness of DC/RC combinations. What I meant with this comment was that you can only have one active RC at a given time.

MSDN states:

Multithread OpenGL Drawing Strategies
The GDI does not support multiple threads. You must use a distinct device context and a distinct rendering context for each thread. This tends to limit the performance advantages of using multiple threads with single-processor systems running OpenGL applications. However, there are ways to use threads with a single processor system to greatly increase performance. For example, you can use a separate thread to pass OpenGL rendering calls to dedicated 3-D hardware.

Symmetric multiprocessing (SMP) systems can greatly benefit from using multiple threads. An obvious strategy is to use a separate thread for each processor to handle OpenGL rendering in separate windows. For example, in a flight-simulation application you could use separate processors and threads to render the front, back, and side views.

A thread can have only one current, active rendering context. When you use multiple threads and multiple rendering contexts, you must be careful to synchronize their use. For example, use one thread only to call SwapBuffers after all threads finish drawing.

<end of quote>

I never tried to use several RCs on one DC, but from my experience I'd assume that swapping the buffers on such a single DC means also swapping the buffers for all RCs.

What you should keep in mind is that an RC is just a set of context variables for a specific DC. As such one cannot speak of swapping the buffers of an RC. SwapBuffers is a GDI function not an OpenGL function!

Does this also make sense to you?

Ciao, Mike


Author Comment

ID: 1113117
This clears this up considerably, Thankyou!

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Keep your audience engaged and get the most out of your next presentation with these quick Prezi tips.
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
In this Micro Tutorial viewers will learn the basic shortcuts and functions of Illustrator. The viewer will learn about the paintbrush tool, anchor points, font sizing, and more.
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question