I am curious how GDI integrates with OpengGL in order to create image for computer screen.

I know that GDI provide Device Context which describes window properties and as far as I know it also offer 2D software based rendering.

Then based on Device Context, Rendering Context is created for OpenGL.

During rendering OpenGL maintain framebuffer objects which are stored in GPU memory and those buffers are then pushed to the screen somehow? However what is the role in GDI in this process? I know that GDI is responsible for transferring image to the graphics devices... So does it mean that framebuffer contents are transferred to CPU RAM and then again to GPU in order to transfer it to display?! It doesn't sound logic...

Why such a software based drawing component (GDI) is used with OpenGL which should be able to send image straight from framebuffer (GPU memory) to display? I can't understand that...

How Windows UI is rendered? Desktop, folders, Start Menu etc. it is software based or gpu based rendering?


1 Answer 1


The video adapter sends to the monitor a video buffer stored somewhere in video memory. This is something the adapter does, and it is responsibility of the device driver.

I am talking about a single monitor for simplicity. In the case of multiple monitor, each one will have a video buffer associated with it, and Windows needs to keep track of their relative position to know what parts of each Window is on each monitor.

What Windows actually puts into the buffer that is send to the monitor needs to be composed from the images of the multiple windows that are to appear on the screen. It is responsibility of Windows to manage those windows so it will know what window will draw where (that is to handle position, size, z-order).

Now, to draw on a given window, there is the GDI API, which allows you to access a buffer dedicated to store the image of a particular window of your application, and do graphic operations on it (such as drawing bitmaps, lines, etc.). Most of these operations happen on CPU.

Windows has to take the images in the buffers of the windows to construct the image that the video adapter will send to the monitor. Modern Windows uses GPU for this process.

However, you do not want to use GDI to draw images, instead you want to use OpenGL. Well, you need a buffer that OpenGL can access, and you need to associate it to the window, so Windows knows where to put it.

How do you get the buffer for the window? You request it via GDI. However, GDI does not give you a naked pointer to the buffer; it gives you a pointer to a structure that has the relevant information for GDI - that we call the Device Context Handle (hDC). That is NOT useful for OpenGL! OpenGL need its own structure, so call a function to create an object that OpenGL understands from the hDC, that object is the Rendering Context.

Note: it is also possible to get an hDC to render directly on a monitor, bypassing the composition step, and use that.

Now that you can tell OpenGL to render to that context, the output from OpenGL will end up in the buffer associated to your window. In addition, from there Windows can take the image and compose it into the result presented to the user.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .