| 1 |
Creating an OpenGL context |
| 2 |
========================== |
| 3 |
|
| 4 |
This section describes how to configure an OpenGL context. For most |
| 5 |
applications the information described here is far too low-level to be of any |
| 6 |
concern, however more advanced applications can take advantage of the complete |
| 7 |
control pyglet provides. |
| 8 |
|
| 9 |
Displays, screens, configs and contexts |
| 10 |
--------------------------------------- |
| 11 |
|
| 12 |
.. figure:: img/context_flow.png |
| 13 |
|
| 14 |
Flow of construction, from the abstract Display to a newly |
| 15 |
created Window with its Context. |
| 16 |
|
| 17 |
Contexts and configs |
| 18 |
^^^^^^^^^^^^^^^^^^^^ |
| 19 |
|
| 20 |
When you draw on a window in pyglet, you are drawing to an OpenGL context. |
| 21 |
Every window has its own context, which is created when the window is created. |
| 22 |
You can access the window's context via its |
| 23 |
:attr:`~pyglet.window.Window.context` attribute. |
| 24 |
|
| 25 |
The context is created from an OpenGL configuration (or "config"), which |
| 26 |
describes various properties of the context such as what color format to use, |
| 27 |
how many buffers are available, and so on. You can access the config |
| 28 |
that was used to create a context via the context's |
| 29 |
:attr:`~pyglet.gl.Context.config` attribute. |
| 30 |
|
| 31 |
For example, here we create a window using the default config and examine some |
| 32 |
of its properties:: |
| 33 |
|
| 34 |
>>> import pyglet |
| 35 |
>>> window = pyglet.window.Window() |
| 36 |
>>> context = window.context |
| 37 |
>>> config = context.config |
| 38 |
>>> config.double_buffer |
| 39 |
c_int(1) |
| 40 |
>>> config.stereo |
| 41 |
c_int(0) |
| 42 |
>>> config.sample_buffers |
| 43 |
c_int(0) |
| 44 |
|
| 45 |
Note that the values of the config's attributes are all ctypes instances. |
| 46 |
This is because the config was not specified by pyglet. Rather, it has been |
| 47 |
selected by pyglet from a list of configs supported by the system. You can |
| 48 |
make no guarantee that a given config is valid on a system unless it was |
| 49 |
provided to you by the system. |
| 50 |
|
| 51 |
pyglet simplifies the process of selecting one of the system's configs by |
| 52 |
allowing you to create a "template" config which specifies only the values you |
| 53 |
are interested in. See :ref:`guide_simple-context-configuration` for details. |
| 54 |
|
| 55 |
|
| 56 |
.. _guide_displays: |
| 57 |
|
| 58 |
Displays |
| 59 |
^^^^^^^^ |
| 60 |
|
| 61 |
The system may actually support several different sets of configs, depending on |
| 62 |
which display device is being used. For example, a computer with two video |
| 63 |
cards may not support the same configs on each card. Another example is using |
| 64 |
X11 remotely: the display device will support different configurations than the |
| 65 |
local driver. Even a single video card on the local computer may support |
| 66 |
different configs for two monitors plugged in. |
| 67 |
|
| 68 |
In pyglet, a :class:`~pyglet.display.Display` is a collection of "screens" |
| 69 |
attached to a single display device. On Linux, the display device corresponds |
| 70 |
to the X11 display being used. On Windows and Mac OS X, there is only one |
| 71 |
display (as these operating systems present multiple video cards as a single |
| 72 |
virtual device). |
| 73 |
|
| 74 |
The :mod:`pyglet.display` module provides access to the display(s). Use the |
| 75 |
:func:`~pyglet.display.get_display` function to get the default display:: |
| 76 |
|
| 77 |
>>> display = pyglet.display.get_display() |
| 78 |
|
| 79 |
.. note:: |
| 80 |
|
| 81 |
On X11, you can use the :class:`~pyglet.display.Display` class directly to |
| 82 |
specify the display string to use, for example to use a remotely connected |
| 83 |
display. The name string is in the same format as used by the ``DISPLAY`` |
| 84 |
environment variable:: |
| 85 |
|
| 86 |
>>> display = pyglet.display.Display(name=':1') |
| 87 |
|
| 88 |
If you have multiple physical screens and you're using Xinerama, see |
| 89 |
:ref:`guide_screens` to select the desired screen as you would for Windows |
| 90 |
and Mac OS X. Otherwise, you can specify the screen number via the |
| 91 |
``x_screen`` argument:: |
| 92 |
|
| 93 |
>>> display = pyglet.display.Display(name=':1', x_screen=1) |
| 94 |
|
| 95 |
.. _guide_screens: |
| 96 |
|
| 97 |
Screens |
| 98 |
^^^^^^^ |
| 99 |
|
| 100 |
Once you have obtained a display, you can enumerate the screens that are |
| 101 |
connected. A screen is the physical display medium connected to the display |
| 102 |
device; for example a computer monitor, TV or projector. Most computers will |
| 103 |
have a single screen, however dual-head workstations and laptops connected to |
| 104 |
a projector are common cases where more than one screen will be present. |
| 105 |
|
| 106 |
In the following example the screens of a dual-head workstation are listed:: |
| 107 |
|
| 108 |
>>> for screen in display.get_screens(): |
| 109 |
... print(screen) |
| 110 |
... |
| 111 |
XlibScreen(screen=0, x=1280, y=0, width=1280, height=1024, xinerama=1) |
| 112 |
XlibScreen(screen=0, x=0, y=0, width=1280, height=1024, xinerama=1) |
| 113 |
|
| 114 |
Because this workstation is running Linux, the returned screens are |
| 115 |
``XlibScreen``, a subclass of :class:`~pyglet.display.Screen`. The |
| 116 |
``screen`` and ``xinerama`` attributes are specific to Linux, but the |
| 117 |
:attr:`~pyglet.display.Screen.x`, :attr:`~pyglet.display.Screen.y`, |
| 118 |
:attr:`~pyglet.display.Screen.width` and |
| 119 |
:attr:`~pyglet.display.Screen.height` attributes are present on all screens, |
| 120 |
and describe the screen's geometry, as shown below. |
| 121 |
|
| 122 |
.. figure:: img/screens.png |
| 123 |
|
| 124 |
Example arrangement of screens and their reported geometry. Note that the |
| 125 |
primary display (marked "1") is positioned on the right, according to this |
| 126 |
particular user's preference. |
| 127 |
|
| 128 |
There is always a "default" screen, which is the first screen returned by |
| 129 |
:meth:`~pyglet.display.Display.get_screens`. Depending on the operating system, |
| 130 |
the default screen is usually the one that contains the taskbar (on Windows) or |
| 131 |
menu bar (on OS X). |
| 132 |
You can access this screen directly using |
| 133 |
:meth:`~pyglet.display.Display.get_default_screen`. |
| 134 |
|
| 135 |
|
| 136 |
.. _guide_glconfig: |
| 137 |
|
| 138 |
OpenGL configuration options |
| 139 |
---------------------------- |
| 140 |
|
| 141 |
When configuring or selecting a :class:`~pyglet.gl.Config`, you do so based |
| 142 |
on the properties of that config. pyglet supports a fixed subset of the |
| 143 |
options provided by AGL, GLX, WGL and their extensions. In particular, these |
| 144 |
constraints are placed on all OpenGL configs: |
| 145 |
|
| 146 |
* Buffers are always component (RGB or RGBA) color, never palette indexed. |
| 147 |
* The "level" of a buffer is always 0 (this parameter is largely unsupported |
| 148 |
by modern OpenGL drivers anyway). |
| 149 |
* There is no way to set the transparent color of a buffer (again, this |
| 150 |
GLX-specific option is not well supported). |
| 151 |
* There is no support for pbuffers (equivalent functionality can be achieved |
| 152 |
much more simply and efficiently using framebuffer objects). |
| 153 |
|
| 154 |
The visible portion of the buffer, sometimes called the color buffer, is |
| 155 |
configured with the following attributes: |
| 156 |
|
| 157 |
``buffer_size`` |
| 158 |
Number of bits per sample. Common values are 24 and 32, which each |
| 159 |
dedicate 8 bits per color component. A buffer size of 16 is also |
| 160 |
possible, which usually corresponds to 5, 6, and 5 bits of red, green |
| 161 |
and blue, respectively. |
| 162 |
|
| 163 |
Usually there is no need to set this property, as the device driver |
| 164 |
will select a buffer size compatible with the current display mode |
| 165 |
by default. |
| 166 |
``red_size``, ``blue_size``, ``green_size``, ``alpha_size`` |
| 167 |
These each give the number of bits dedicated to their respective color |
| 168 |
component. You should avoid setting any of the red, green or blue |
| 169 |
sizes, as these are determined by the driver based on the |
| 170 |
``buffer_size`` property. |
| 171 |
|
| 172 |
If you require an alpha channel in your color buffer (for example, if |
| 173 |
you are compositing in multiple passes) you should specify |
| 174 |
``alpha_size=8`` to ensure that this channel is created. |
| 175 |
``sample_buffers`` and ``samples`` |
| 176 |
Configures the buffer for multisampling (MSAA), in which more than |
| 177 |
one color sample is used to determine the color of each pixel, |
| 178 |
leading to a higher quality, antialiased image. |
| 179 |
|
| 180 |
Enable multisampling (MSAA) by setting ``sample_buffers=1``, then |
| 181 |
give the number of samples per pixel to use in ``samples``. |
| 182 |
For example, ``samples=2`` is the fastest, lowest-quality multisample |
| 183 |
configuration. ``samples=4`` is still widely supported |
| 184 |
and fairly performant even on Intel HD and AMD Vega. |
| 185 |
Most modern GPUs support 2×, 4×, 8×, and 16× MSAA samples |
| 186 |
with fairly high performance. |
| 187 |
|
| 188 |
``stereo`` |
| 189 |
Creates separate left and right buffers, for use with stereo hardware. |
| 190 |
Only specialised video hardware such as stereoscopic glasses will |
| 191 |
support this option. When used, you will need to manually render to |
| 192 |
each buffer, for example using `glDrawBuffers`. |
| 193 |
``double_buffer`` |
| 194 |
Create separate front and back buffers. Without double-buffering, |
| 195 |
drawing commands are immediately visible on the screen, and the user |
| 196 |
will notice a visible flicker as the image is redrawn in front of |
| 197 |
them. |
| 198 |
|
| 199 |
It is recommended to set ``double_buffer=True``, which creates a |
| 200 |
separate hidden buffer to which drawing is performed. When the |
| 201 |
`Window.flip` is called, the buffers are swapped, |
| 202 |
making the new drawing visible virtually instantaneously. |
| 203 |
|
| 204 |
In addition to the color buffer, several other buffers can optionally be |
| 205 |
created based on the values of these properties: |
| 206 |
|
| 207 |
``depth_size`` |
| 208 |
A depth buffer is usually required for 3D rendering. The typical |
| 209 |
depth size is 24 bits. Specify ``0`` if you do not require a depth |
| 210 |
buffer. |
| 211 |
``stencil_size`` |
| 212 |
The stencil buffer is required for masking the other buffers and |
| 213 |
implementing certain volumetric shadowing algorithms. The typical |
| 214 |
stencil size is 8 bits; or specify ``0`` if you do not require it. |
| 215 |
``accum_red_size``, ``accum_blue_size``, ``accum_green_size``, ``accum_alpha_size`` |
| 216 |
The accumulation buffer can be used for simple antialiasing, |
| 217 |
depth-of-field, motion blur and other compositing operations. Its use |
| 218 |
nowadays is being superseded by the use of floating-point textures, |
| 219 |
however it is still a practical solution for implementing these |
| 220 |
effects on older hardware. |
| 221 |
|
| 222 |
If you require an accumulation buffer, specify ``8`` for each |
| 223 |
of these attributes (the alpha component is optional, of course). |
| 224 |
``aux_buffers`` |
| 225 |
Each auxiliary buffer is configured the same as the colour buffer. |
| 226 |
Up to four auxiliary buffers can typically be created. Specify ``0`` |
| 227 |
if you do not require any auxiliary buffers. |
| 228 |
|
| 229 |
Like the accumulation buffer, auxiliary buffers are used less often |
| 230 |
nowadays as more efficient techniques such as render-to-texture are |
| 231 |
available. They are almost universally available on older hardware, |
| 232 |
though, where the newer techniques are not possible. |
| 233 |
|
| 234 |
If you wish to work with OpenGL directly, you can request a higher level |
| 235 |
context. This is required if you wish to work with the modern OpenGL |
| 236 |
programmable pipeline. Please note, however, that pyglet currently uses |
| 237 |
legacy OpenGL functionality for many of its internal modules (such as |
| 238 |
the text, graphics, and sprite modules). Requesting a higher version |
| 239 |
context will currently prevent usage of these modules. |
| 240 |
|
| 241 |
``major_version`` |
| 242 |
This will be either 3 or 4, for an OpenGL 3.x or 4.x context. |
| 243 |
``minor_version`` |
| 244 |
The requested minor version of the context. In some cases, the OpenGL |
| 245 |
driver may return a higher version than requested. |
| 246 |
``forward_compatible`` |
| 247 |
Setting this to `True` will ask the driver to exclude legacy OpenGL |
| 248 |
features from the context. Khronos does not recommend this option. |
| 249 |
|
| 250 |
.. note:: |
| 251 |
To request a higher higher version OpenGL context on Mac OSX, it is necessary |
| 252 |
to disable the pyglet shadow context. To do this, set the pyglet option |
| 253 |
``pyglet.options['shadow_window']`` to ``False`` `before` creating a Window, |
| 254 |
or importing ``pyglet.window``. |
| 255 |
|
| 256 |
The default configuration |
| 257 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 258 |
|
| 259 |
If you create a :class:`~pyglet.window.Window` without specifying the context |
| 260 |
or config, pyglet will use a template config with the following properties: |
| 261 |
|
| 262 |
.. list-table:: |
| 263 |
:header-rows: 1 |
| 264 |
|
| 265 |
* - Attribute |
| 266 |
- Value |
| 267 |
* - double_buffer |
| 268 |
- True |
| 269 |
* - depth_size |
| 270 |
- 24 |
| 271 |
|
| 272 |
.. _guide_simple-context-configuration: |
| 273 |
|
| 274 |
Simple context configuration |
| 275 |
---------------------------- |
| 276 |
|
| 277 |
A context can only be created from a config that was provided by the system. |
| 278 |
Enumerating and comparing the attributes of all the possible configs is |
| 279 |
a complicated process, so pyglet provides a simpler interface based on |
| 280 |
"template" configs. |
| 281 |
|
| 282 |
To get the config with the attributes you need, construct a |
| 283 |
:class:`~pyglet.gl.Config` and set only the attributes you are interested in. |
| 284 |
You can then supply this config to the :class:`~pyglet.window.Window` |
| 285 |
constructor to create the context. |
| 286 |
|
| 287 |
For example, to create a window with an alpha channel:: |
| 288 |
|
| 289 |
config = pyglet.gl.Config(alpha_size=8) |
| 290 |
window = pyglet.window.Window(config=config) |
| 291 |
|
| 292 |
It is sometimes necessary to create the context yourself, rather than letting |
| 293 |
the :class:`~pyglet.window.Window` constructor do this for you. In this case |
| 294 |
use :meth:`~pyglet.display.Screen.get_best_config` to obtain a "complete" |
| 295 |
config, which you can then use to create the context:: |
| 296 |
|
| 297 |
display = pyglet.display.get_display() |
| 298 |
screen = display.get_default_screen() |
| 299 |
|
| 300 |
template = pyglet.gl.Config(alpha_size=8) |
| 301 |
config = screen.get_best_config(template) |
| 302 |
context = config.create_context(None) |
| 303 |
window = pyglet.window.Window(context=context) |
| 304 |
|
| 305 |
Note that you cannot create a context directly from a template (any |
| 306 |
:class:`~pyglet.gl.Config` you constructed yourself). The |
| 307 |
:class:`~pyglet.window.Window` constructor performs a similar process to the |
| 308 |
above to create the context if a template config is given. |
| 309 |
|
| 310 |
Not all configs will be possible on all machines. The call to |
| 311 |
:meth:`~pyglet.display.Screen.get_best_config` will raise |
| 312 |
:class:`~pyglet.window.NoSuchConfigException` if the hardware does not |
| 313 |
support the requested attributes. It will never return a config that does not |
| 314 |
meet or exceed the attributes you specify in the template. |
| 315 |
|
| 316 |
You can use this to support newer hardware features where available, but also |
| 317 |
accept a lesser config if necessary. For example, the following code creates |
| 318 |
a window with multisampling if possible, otherwise leaves multisampling off:: |
| 319 |
|
| 320 |
template = pyglet.gl.Config(sample_buffers=1, samples=4) |
| 321 |
try: |
| 322 |
config = screen.get_best_config(template) |
| 323 |
except pyglet.window.NoSuchConfigException: |
| 324 |
template = gl.Config() |
| 325 |
config = screen.get_best_config(template) |
| 326 |
window = pyglet.window.Window(config=config) |
| 327 |
|
| 328 |
Selecting the best configuration |
| 329 |
-------------------------------- |
| 330 |
|
| 331 |
Allowing pyglet to select the best configuration based on a template is |
| 332 |
sufficient for most applications, however some complex programs may want to |
| 333 |
specify their own algorithm for selecting a set of OpenGL attributes. |
| 334 |
|
| 335 |
You can enumerate a screen's configs using the |
| 336 |
:meth:`~pyglet.display.Screen.get_matching_configs` method. You must supply a |
| 337 |
template as a minimum specification, but you can supply an "empty" template |
| 338 |
(one with no attributes set) to get a list of all configurations supported by |
| 339 |
the screen. |
| 340 |
|
| 341 |
In the following example, all configurations with either an auxiliary buffer |
| 342 |
or an accumulation buffer are printed:: |
| 343 |
|
| 344 |
display = pyglet.display.get_display() |
| 345 |
screen = display.get_default_screen() |
| 346 |
|
| 347 |
for config in screen.get_matching_configs(gl.Config()): |
| 348 |
if config.aux_buffers or config.accum_red_size: |
| 349 |
print(config) |
| 350 |
|
| 351 |
As well as supporting more complex configuration selection algorithms, |
| 352 |
enumeration allows you to efficiently find the maximum value of an attribute |
| 353 |
(for example, the maximum samples per pixel), or present a list of possible |
| 354 |
configurations to the user. |
| 355 |
|
| 356 |
Sharing objects between contexts |
| 357 |
-------------------------------- |
| 358 |
|
| 359 |
Every window in pyglet has its own OpenGL context. Each context has its own |
| 360 |
OpenGL state, including the matrix stacks and current flags. However, |
| 361 |
contexts can optionally share their objects with one or more other contexts. |
| 362 |
Shareable objects include: |
| 363 |
|
| 364 |
* Textures |
| 365 |
* Display lists |
| 366 |
* Shader programs |
| 367 |
* Vertex and pixel buffer objects |
| 368 |
* Framebuffer objects |
| 369 |
|
| 370 |
There are two reasons for sharing objects. The first is to allow objects to |
| 371 |
be stored on the video card only once, even if used by more than one window. |
| 372 |
For example, you could have one window showing the actual game, with other |
| 373 |
"debug" windows showing the various objects as they are manipulated. Or, a |
| 374 |
set of widget textures required for a GUI could be shared between all the |
| 375 |
windows in an application. |
| 376 |
|
| 377 |
The second reason is to avoid having to recreate the objects when a context |
| 378 |
needs to be recreated. For example, if the user wishes to turn on |
| 379 |
multisampling, it is necessary to recreate the context. Rather than destroy |
| 380 |
the old one and lose all the objects already created, you can |
| 381 |
|
| 382 |
1. Create the new context, sharing object space with the old context, then |
| 383 |
2. Destroy the old context. The new context retains all the old objects. |
| 384 |
|
| 385 |
pyglet defines an :class:`~pyglet.gl.ObjectSpace`: a representation of a |
| 386 |
collection of objects used by one or more contexts. Each context has a single |
| 387 |
object space, accessible via its |
| 388 |
:py:attr:`~pyglet.gl.base.Context.object_space` attribute. |
| 389 |
|
| 390 |
By default, all contexts share the same object space as long as at least one |
| 391 |
context using it is "alive". If all the contexts sharing an object space are |
| 392 |
lost or destroyed, the object space will be destroyed also. This is why it is |
| 393 |
necessary to follow the steps outlined above for retaining objects when a |
| 394 |
context is recreated. |
| 395 |
|
| 396 |
pyglet creates a hidden "shadow" context as soon as :mod:`pyglet.gl` is |
| 397 |
imported. By default, all windows will share object space with this shadow |
| 398 |
context, so the above steps are generally not needed. The shadow context also |
| 399 |
allows objects such as textures to be loaded before a window is created (see |
| 400 |
``shadow_window`` in :data:`pyglet.options` for further details). |
| 401 |
|
| 402 |
When you create a :class:`~pyglet.gl.Context`, you tell pyglet which other |
| 403 |
context it will obtain an object space from. By default (when using the |
| 404 |
:class:`~pyglet.window.Window` constructor |
| 405 |
to create the context) the most recently created context will be used. You |
| 406 |
can specify another context, or specify no context (to create a new object |
| 407 |
space) in the :class:`~pyglet.gl.Context` constructor. |
| 408 |
|
| 409 |
It can be useful to keep track of which object space an object was created in. |
| 410 |
For example, when you load a font, pyglet caches the textures used and reuses |
| 411 |
them; but only if the font is being loaded on the same object space. The |
| 412 |
easiest way to do this is to set your own attributes on the |
| 413 |
:py:class:`~pyglet.gl.ObjectSpace` object. |
| 414 |
|
| 415 |
In the following example, an attribute is set on the object space indicating |
| 416 |
that game objects have been loaded. This way, if the context is recreated, |
| 417 |
you can check for this attribute to determine if you need to load them again:: |
| 418 |
|
| 419 |
context = pyglet.gl.current_context |
| 420 |
object_space = context.object_space |
| 421 |
object_space.my_game_objects_loaded = True |
| 422 |
|
| 423 |
Avoid using attribute names on :class:`~pyglet.gl.ObjectSpace` that begin with |
| 424 |
``"pyglet"``, as they may conflict with an internal module. |