Skip to content

GL \ Device Context and GL Context

Luca Piccioni edited this page Mar 9, 2017 · 4 revisions

OpenGL.Net offers raw API that you can use for implementing your own abstraction layer. However, it offers also a set of utilities for creating the appropriate device contextes depending on the platform and the host-specific environment.

Device Context Overview

The DeviceContext abstraction is used for creating/destroying GL contexts, managing current GL context and managing buffers swapping (double-buffering and V-Sync). When a GL context is made current, it should be made current on the DeviceContext instance that has created it; platform-specific behavior may allow mixing device contexts and GL contexts.

A DeviceContext can be created using one of the following DeviceContext methods:

  • static DeviceContext Create(IntPtr display, IntPtr windowHandle)
  • static DeviceContext Create()
  • static DeviceContext Create(INativePBuffer nativeBuffer)

Basic factory

Device contexts are normally created by native handles offered by the underlying platform API. The method public static DeviceContext Create(IntPtr display, IntPtr windowHandle) should be used when using a native window as rendering destination.

Native windows must be created by the user, following the instructions found in Surfaces.

Windowless Factory

It is possible to create DeviceContext instances that apparently they are not linked to any native window by using static DeviceContext Create(). However, you should be aware that the instances created with this method are bound to a common native window. The common (and hidden) native window is created at OpenGL.Net initialization time, and kept for offering the device context factory method.

Indeed it's possible to use concurrently multiple device contexts created using this method, but they should be used for off-screen rendering purposes.

P-Buffer Factory

Most of the platforms offers a pixel buffer storage which able users to perform off-screen rendering on platform-specific resources, called P-Buffers. The method static DeviceContext Create(INativePBuffer nativeBuffer) can be used for creating DeviceContext instances able to render on the specified INativePBuffer instance.

The appropriate INativePBuffer instance can be created by INativePBuffer CreatePBuffer(DevicePixelFormat pixelFormat, uint width, uint height). Since the P-Buffer pixel format is already determined at construction time, you must not set the DeviceContext pixel format.

GL Context Overview

Once you have the most appropriate DeviceContext implementation, you should initialize it for managing GL context instances. Each GL context instance is identified by an IntPtr (that is simply a pointer).

Here is the DeviceContext abstract interface:

  • IntPtr CreateContext(IntPtr sharedContext)
  • IntPtr CreateContextAttrib(IntPtr sharedContext, int[] attribsList)
  • bool MakeCurrent(IntPtr ctx)
  • bool DeleteContext(IntPtr ctx)
  • void SwapBuffers()
  • bool SwapInterval(int interval)
  • DevicePixelFormatCollection PixelsFormats { get; }
  • void ChoosePixelFormat(DevicePixelFormat pixelFormat)
  • void SetPixelFormat(DevicePixelFormat pixelFormat)

Before creating any GL context, a pixel format must be defined for the device context. You must set the pixel format only if the DeviceContext was created by Create(IntPtr display, IntPtr windowHandle); in the other cases you shouldn't attemp to set any pixel format.

You can use SetPixelFormat or ChoosePixelFormat, depending whether you want to manually select the pixel format.