/usr/lib/python2.7/dist-packages/pyglet/gl/__init__.py is in python-pyglet 1.1.4.dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | # ----------------------------------------------------------------------------
# pyglet
# Copyright (c) 2006-2008 Alex Holkner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * Neither the name of pyglet nor the names of its
# contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------
'''OpenGL and GLU interface.
This package imports all OpenGL, GLU and registered OpenGL extension
functions. Functions have identical signatures to their C counterparts. For
example::
from pyglet.gl import *
# [...omitted: set up a GL context and framebuffer]
glBegin(GL_QUADS)
glVertex3f(0, 0, 0)
glVertex3f(0.1, 0.2, 0.3)
glVertex3f(0.1, 0.2, 0.3)
glEnd()
OpenGL is documented in full at the `OpenGL Reference Pages`_.
The `OpenGL Programming Guide`_ is a popular reference manual organised by
topic. The free online version documents only OpenGL 1.1. `Later editions`_
cover more recent versions of the API and can be purchased from a book store.
.. _OpenGL Reference Pages: http://www.opengl.org/documentation/red_book/
.. _OpenGL Programming Guide: http://fly.cc.fer.hr/~unreal/theredbook/
.. _Later editions: http://www.opengl.org/documentation/red_book/
The following subpackages are imported into this "mega" package already (and
so are available by importing ``pyglet.gl``):
``pyglet.gl.gl``
OpenGL
``pyglet.gl.glu``
GLU
``pyglet.gl.gl.glext_arb``
ARB registered OpenGL extension functions
``pyglet.gl.gl.glext_missing``
ARB registered OpenGL extension functions not included in the ARB C header
These subpackages are also available, but are not imported into this namespace
by default:
``pyglet.gl.glext_nv``
nVidia OpenGL extension functions
``pyglet.gl.agl``
AGL (Mac OS X OpenGL context functions)
``pyglet.gl.glx``
GLX (Linux OpenGL context functions)
``pyglet.gl.glxext_arb``
ARB registered GLX extension functions
``pyglet.gl.glxext_nv``
nvidia GLX extension functions
``pyglet.gl.wgl``
WGL (Windows OpenGL context functions)
``pyglet.gl.wglext_arb``
ARB registered WGL extension functions
``pyglet.gl.wglext_nv``
nvidia WGL extension functions
The information modules are provided for convenience, and are documented
below.
'''
__docformat__ = 'restructuredtext'
__version__ = '$Id: __init__.py 2541 2009-12-31 04:31:11Z benjamin.coder.smith@gmail.com $'
from pyglet.gl.lib import GLException
from pyglet.gl.gl import *
from pyglet.gl.glu import *
from pyglet.gl.glext_arb import *
from pyglet.gl.glext_missing import *
from pyglet.gl import gl_info
import sys as _sys
_is_epydoc = hasattr(_sys, 'is_epydoc') and _sys.is_epydoc
# List of contexts currently in use, so we can create new contexts that
# share objects with. Remember to remove from this list when context is
# destroyed.
_contexts = []
#: The active OpenGL context.
#:
#: You can change the current context by calling `Context.set_current`; do not
#: modify this global.
#:
#: :type: `Context`
#:
#: :since: pyglet 1.1
current_context = None
def get_current_context():
'''Return the active OpenGL context.
You can change the current context by calling `Context.set_current`.
:deprecated: Use `current_context`
:rtype: `Context`
:return: the context to which OpenGL commands are directed, or None
if there is no selected context.
'''
return current_context
class Config(object):
'''Graphics configuration.
A GLConfig stores the preferences for OpenGL attributes such as the
number of auxiliary buffers, size of the colour and depth buffers,
double buffering, stenciling, multi- and super-sampling, and so on.
Different platforms support a different set of attributes, so these
are set with a string key and a value which is integer or boolean.
See also `pyglet.window.Screen.get_best_config` and
`pyglet.window.Screen.get_matching_configs`.
:Ivariables:
`double_buffer` : bool
Specify the presence of a back-buffer for every color buffer.
`stereo` : bool
Specify the presence of separate left and right buffer sets.
`buffer_size` : int
Total bits per sample per color buffer.
`aux_buffers` : int
The number of auxiliary color buffers.
`sample_buffers` : int
The number of multisample buffers.
`samples` : int
The number of samples per pixel, or 0 if there are no multisample
buffers.
`red_size` : int
Bits per sample per buffer devoted to the red component.
`green_size` : int
Bits per sample per buffer devoted to the green component.
`blue_size` : int
Bits per sample per buffer devoted to the blue component.
`alpha_size` : int
Bits per sample per buffer devoted to the alpha component.
`depth_size` : int
Bits per sample in the depth buffer.
`stencil_size` : int
Bits per sample in the stencil buffer.
`accum_red_size` : int
Bits per pixel devoted to the red component in the accumulation
buffer.
`accum_green_size` : int
Bits per pixel devoted to the green component in the accumulation
buffer.
`accum_blue_size` : int
Bits per pixel devoted to the blue component in the accumulation
buffer.
`accum_alpha_size` : int
Bits per pixel devoted to the alpha component in the accumulation
buffer.
'''
_attribute_names = [
'double_buffer',
'stereo',
'buffer_size',
'aux_buffers',
'sample_buffers',
'samples',
'red_size',
'green_size',
'blue_size',
'alpha_size',
'depth_size',
'stencil_size',
'accum_red_size',
'accum_green_size',
'accum_blue_size',
'accum_alpha_size',
]
def __init__(self, **kwargs):
'''Create a template config with the given attributes.
Specify attributes as keyword arguments, for example::
template = Config(double_buffer=True)
'''
for name in self._attribute_names:
if name in kwargs:
setattr(self, name, kwargs[name])
else:
setattr(self, name, None)
def get_gl_attributes(self):
'''Return a list of attributes set on this config.
:rtype: list of tuple (name, value)
:return: All attributes, with unset attributes having a value of
``None``.
'''
return [(name, getattr(self, name)) for name in self._attribute_names]
def create_context(self, share):
'''Create a GL context that satisfies this configuration.
:Parameters:
`share` : `Context`
If not None, a context with which to share objects with.
:rtype: `Context`
:return: The new context.
'''
raise ConfigException(
'This config is not complete. Use Screen.get_matching_configs')
def is_complete(self):
'''Determine if this config is complete and able to create a context.
Configs created directly are not complete, they can only serve
as templates for retrieving a supported config from the system.
For example, `pyglet.window.Screen.get_matching_configs` returns
complete configs.
:rtype: bool
:return: True if the config is complete and can create a context.
'''
return False
def __repr__(self):
import pprint
return '%s(%s)' % (self.__class__.__name__,
pprint.pformat(self.get_gl_attributes()))
class ObjectSpace(object):
def __init__(self):
# Textures and buffers scheduled for deletion the next time this
# object space is active.
self._doomed_textures = []
self._doomed_buffers = []
class Context(object):
'''OpenGL context for drawing.
Windows in pyglet each have their own GL context. This class boxes
the context in a platform-independent manner. Applications will have
no need to deal with contexts directly.
:Ivariables:
`object_space` : `ObjectSpace`
An object which is shared between all contexts that share
GL objects.
'''
#: Context share behaviour indicating that objects should not be
#: shared with existing contexts.
CONTEXT_SHARE_NONE = None
#: Context share behaviour indicating that objects are shared with
#: the most recently created context (the default).
CONTEXT_SHARE_EXISTING = 1
# Used for error checking, True if currently within a glBegin/End block.
# Ignored if error checking is disabled.
_gl_begin = False
# gl_info.GLInfo instance, filled in on first set_current
_info = None
# List of (attr, check) for each driver/device-specific workaround that is
# implemented. The `attr` attribute on this context is set to the result
# of evaluating `check(gl_info)` the first time this context is used.
_workaround_checks = [
# GDI Generic renderer on Windows does not implement
# GL_UNPACK_ROW_LENGTH correctly.
('_workaround_unpack_row_length',
lambda info: info.get_renderer() == 'GDI Generic'),
# Reportedly segfaults in text_input.py example.
# "ATI Radeon X1600 OpenGL Engine"
# glGenBuffers not exported by
# "ATI Radeon X1270 x86/MMX/3DNow!/SSE2"
('_workaround_vbo',
lambda info: info.get_renderer().startswith('ATI Radeon X')),
# Some ATI cards on OS X start drawing from a VBO before it's written
# to. In these cases pyglet needs to call glFinish() to flush the
# pipeline after updating a buffer but before rendering.
('_workaround_vbo_finish',
lambda info: ('ATI' in info.get_renderer() and
info.have_version(1, 5) and
_sys.platform == 'darwin')),
]
def __init__(self, context_share=None):
self.window = None
_contexts.append(self)
if context_share:
assert context_share in _contexts
self.object_space = context_share.object_space
else:
self.object_space = ObjectSpace()
def __repr__(self):
return '%s()' % self.__class__.__name__
def set_current(self):
global current_context
assert self in _contexts
current_context = self
# Implement workarounds
if not self._info:
self._info = gl_info.GLInfo()
self._info.set_active_context()
for attr, check in self._workaround_checks:
setattr(self, attr, check(self._info))
# Release textures on this context scheduled for deletion
if self.object_space._doomed_textures:
textures = self.object_space._doomed_textures
textures = (GLuint * len(textures))(*textures)
glDeleteTextures(len(textures), textures)
self.object_space._doomed_textures = []
# Release buffers on this context scheduled for deletion
if self.object_space._doomed_buffers:
buffers = self.object_space._doomed_buffers
buffers = (GLuint * len(buffers))(*buffers)
glDeleteBuffers(len(buffers), buffers)
self.object_space._doomed_buffers = []
def destroy(self):
'''Release the context.
The context will not be usable after being destroyed. Each platform
has its own convention for releasing the context and the buffer(s)
that depend on it in the correct order; this should never be called
by an application.
'''
global current_context
if current_context is self:
current_context = None
gl_info.remove_active_context()
# Switch back to shadow context.
if _shadow_window is not None:
_shadow_window.switch_to()
_contexts.remove(self)
def delete_texture(self, texture_id):
'''Safely delete a texture belonging to this context.
Usually, the texture is released immediately using
``glDeleteTextures``, however if another context that does not share
this context's object space is currently active, the deletion will
be deferred until an appropriate context is activated.
:Parameters:
`texture_id` : int
The OpenGL name of the texture to delete.
'''
if self.object_space is current_context.object_space:
id = GLuint(texture_id)
glDeleteTextures(1, id)
else:
self.object_space._doomed_textures.append(texture_id)
def delete_buffer(self, buffer_id):
'''Safely delete a buffer object belonging to this context.
This method behaves similarly to `delete_texture`, though for
``glDeleteBuffers`` instead of ``glDeleteTextures``.
:Parameters:
`buffer_id` : int
The OpenGL name of the buffer to delete.
:since: pyglet 1.1
'''
if self.object_space is current_context.object_space and False:
id = GLuint(buffer_id)
glDeleteBuffers(1, id)
else:
self.object_space._doomed_buffers.append(buffer_id)
class ContextException(Exception):
pass
class ConfigException(Exception):
pass
import pyglet as _pyglet
if _pyglet.options['debug_texture']:
_debug_texture_total = 0
_debug_texture_sizes = {}
_debug_texture = None
def _debug_texture_alloc(texture, size):
global _debug_texture_total
_debug_texture_sizes[texture] = size
_debug_texture_total += size
print '%d (+%d)' % (_debug_texture_total, size)
def _debug_texture_dealloc(texture):
global _debug_texture_total
size = _debug_texture_sizes[texture]
del _debug_texture_sizes[texture]
_debug_texture_total -= size
print '%d (-%d)' % (_debug_texture_total, size)
_glBindTexture = glBindTexture
def glBindTexture(target, texture):
global _debug_texture
_debug_texture = texture
return _glBindTexture(target, texture)
_glTexImage2D = glTexImage2D
def glTexImage2D(target, level, internalformat, width, height, border,
format, type, pixels):
try:
_debug_texture_dealloc(_debug_texture)
except KeyError:
pass
if internalformat in (1, GL_ALPHA, GL_INTENSITY, GL_LUMINANCE):
depth = 1
elif internalformat in (2, GL_RGB16, GL_RGBA16):
depth = 2
elif internalformat in (3, GL_RGB):
depth = 3
else:
depth = 4 # Pretty crap assumption
size = (width + 2 * border) * (height + 2 * border) * depth
_debug_texture_alloc(_debug_texture, size)
return _glTexImage2D(target, level, internalformat, width, height,
border, format, type, pixels)
_glDeleteTextures = glDeleteTextures
def glDeleteTextures(n, textures):
if not hasattr(textures, '__len__'):
_debug_texture_dealloc(textures.value)
else:
for i in range(n):
_debug_texture_dealloc(textures[i].value)
return _glDeleteTextures(n, textures)
def _create_shadow_window():
global _shadow_window
import pyglet
if not pyglet.options['shadow_window'] or _is_epydoc:
return
from pyglet.window import Window
_shadow_window = Window(width=1, height=1, visible=False)
_shadow_window.switch_to()
from pyglet import app
app.windows.remove(_shadow_window)
_shadow_window = None
# Import pyglet.window now if it isn't currently being imported (this creates
# the shadow window).
if (not _is_epydoc and
'pyglet.window' not in _sys.modules and
_pyglet.options['shadow_window']):
# trickery is for circular import
_pyglet.gl = _sys.modules[__name__]
import pyglet.window
|