File: extensions.py

package info (click to toggle)
pyopengl 3.1.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,024 kB
  • sloc: python: 108,056; sh: 13; makefile: 8
file content (331 lines) | stat: -rw-r--r-- 10,786 bytes parent folder | download
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
"""Extension module support methods

This module provides the tools required to check whether
an extension is available
"""

from OpenGL.latebind import LateBind
from OpenGL._bytes import bytes, unicode, as_8_bit
import OpenGL as root
import sys
import logging
import re

_log = logging.getLogger('OpenGL.extensions')
VERSION_PREFIX = as_8_bit('GL_VERSION_GL_')
CURRENT_GL_VERSION = None
AVAILABLE_GL_EXTENSIONS = []
AVAILABLE_GLU_EXTENSIONS = []

# version tuple -> list of implicitly included extensions...
VERSION_EXTENSIONS = [
    (
        (3, 0),
        [
            as_8_bit('GL_ARB_vertex_array_object'),
            as_8_bit('GL_ARB_texture_buffer_object'),
            as_8_bit('GL_ARB_framebuffer_object'),
            as_8_bit('GL_ARB_map_buffer_range'),
        ],
    ),
    (
        (3, 1),
        [
            as_8_bit('GL_ARB_copy_buffer'),
            as_8_bit('GL_ARB_uniform_buffer_object'),
        ],
    ),
    (
        (3, 2),
        [
            as_8_bit('GL_ARB_draw_elements_base_vertex'),
            as_8_bit('GL_ARB_provoking_vertex'),
            as_8_bit('GL_ARB_sync'),
            as_8_bit('GL_ARB_texture_multisample'),
        ],
    ),
    (
        (3, 3),
        [
            as_8_bit('GL_ARB_texture_multisample'),
            as_8_bit('GL_ARB_blend_func_extended'),
            as_8_bit('GL_ARB_sampler_objects'),
            as_8_bit('GL_ARB_explicit_attrib_location'),
            as_8_bit('GL_ARB_occlusion_query2'),
            as_8_bit('GL_ARB_shader_bit_encoding'),
            as_8_bit('GL_ARB_texture_rgb10_a2ui'),
            as_8_bit('GL_ARB_texture_swizzle'),
            as_8_bit('GL_ARB_timer_query'),
            as_8_bit('GL_ARB_vertex_type_2_10_10_10_rev'),
        ],
    ),
    (
        (4, 0),
        [
            as_8_bit('GL_ARB_texture_query_lod'),
            as_8_bit('GL_ARB_draw_indirect'),
            as_8_bit('GL_ARB_gpu_shader5'),
            as_8_bit('GL_ARB_gpu_shader_fp64'),
            as_8_bit('GL_ARB_shader_subroutine'),
            as_8_bit('GL_ARB_tessellation_shader'),
            as_8_bit('GL_ARB_texture_buffer_object_rgb32'),
            as_8_bit('GL_ARB_texture_cube_map_array'),
            as_8_bit('GL_ARB_texture_gather'),
            as_8_bit('GL_ARB_transform_feedback2'),
            as_8_bit('GL_ARB_transform_feedback3'),
        ],
    ),
    (
        (4, 1),
        [
            as_8_bit('GL_ARB_ES2_compatibility'),
            as_8_bit('GL_ARB_get_program_binary'),
            as_8_bit('GL_ARB_separate_shader_objects'),
            as_8_bit('GL_ARB_shader_precision'),
            as_8_bit('GL_ARB_vertex_attrib_64bit'),
            as_8_bit('GL_ARB_viewport_array'),
        ],
    ),
    (
        (4, 2),
        [
            as_8_bit('GL_ARB_base_instance'),
            as_8_bit('GL_ARB_shading_language_420pack'),
            as_8_bit('GL_ARB_transform_feedback_instanced'),
            as_8_bit('GL_ARB_compressed_texture_pixel_storage'),
            as_8_bit('GL_ARB_conservative_depth'),
            as_8_bit('GL_ARB_internalformat_query'),
            as_8_bit('GL_ARB_map_buffer_alignment'),
            as_8_bit('GL_ARB_shader_atomic_counters'),
            as_8_bit('GL_ARB_shader_image_load_store'),
            as_8_bit('GL_ARB_shading_language_packing'),
            as_8_bit('GL_ARB_texture_storage'),
        ],
    ),
]


class ExtensionQuerier(object):
    prefix = None
    version_prefix = None
    assumed_version = [1, 0]

    version = extensions = None
    version_string = extensions_string = None

    registered = []

    def __init__(self):
        self.registered.append(self)

    @classmethod
    def hasExtension(self, specifier):
        for registered in self.registered:
            result = registered(specifier)
            if result:
                return result
        return False

    def __call__(self, specifier):
        specifier = as_8_bit(specifier).replace(as_8_bit('.'), as_8_bit('_'))
        if not specifier.startswith(as_8_bit(self.prefix)):
            return None

        if specifier.startswith(as_8_bit(self.version_prefix)):
            specifier = [
                int(x)
                for x in specifier[len(self.version_prefix) :].split(as_8_bit('_'))
            ]
            if specifier[:2] <= self.assumed_version:
                return True
            version = self.getVersion()
            if not version:
                return version
            return specifier <= version
        else:
            extensions = self.getExtensions()
            return extensions and specifier in extensions

    def getVersion(self):
        if not self.version:
            self.version = self.pullVersion()
        return self.version

    def getExtensions(self):
        if not self.extensions:
            self.extensions = self.pullExtensions()
        return self.extensions


class _GLQuerier(ExtensionQuerier):
    prefix = as_8_bit('GL_')
    version_prefix = as_8_bit('GL_VERSION_GL_')
    assumed_version = [1, 1]

    # If you call OpenGL's glGetString for a context which is
    # an ES context, you get an ES-style string, here we're going
    # to parse out the ES value, but this is one of those "the issue is the usage"
    # problems. You shouldn't do this, but if you do, this will make
    # the issue clearer
    version_matcher = re.compile(
        r'^(?P<api_marker>OpenGL (ES )?)?(?P<version>\d+([.]\d+)+)'
    )
    is_opengl_es = False

    def pullVersion(self):
        """Retrieve 2-int declaration of major/minor GL version

        returns [int(major),int(minor)] or False if not loaded
        """
        from OpenGL import platform

        if not platform.PLATFORM.CurrentContextIsValid():
            return False
        from OpenGL.raw.GL.VERSION.GL_1_1 import glGetString
        from OpenGL.raw.GL.VERSION.GL_1_1 import GL_VERSION

        new = glGetString(GL_VERSION)

        self.version_string = new.decode('utf-8', errors='ignore')
        match = self.version_matcher.match(self.version_string)
        if match:
            self.version_string = match.group('version')
            api_marker = match.group('api_marker')
            if api_marker:
                if api_marker == 'OpenGL ES':
                    _log.warning(
                        "Importing OpenGL in an OpenGL ES context is not advisable, generally"
                    )
                    self.is_opengl_es = True
        if new:
            return [int(x) for x in self.version_string.split('.')]
        else:
            return False  # not yet loaded/supported

    def pullExtensions(self):
        from OpenGL import platform

        if not platform.PLATFORM.CurrentContextIsValid():
            return False
        from OpenGL.raw.GL._types import GLint
        from OpenGL.raw.GL.VERSION.GL_1_1 import glGetString, glGetError
        from OpenGL.raw.GL.VERSION.GL_1_1 import GL_EXTENSIONS
        from OpenGL import error

        try:
            extensions = glGetString(GL_EXTENSIONS)
            if glGetError():
                raise error.GLError()
            if extensions:
                extensions = extensions.split()
            else:
                return False
        except (AttributeError, error.GLError):
            # OpenGL 3.0 deprecates glGetString( GL_EXTENSIONS )
            from OpenGL.raw.GL.VERSION.GL_3_0 import GL_NUM_EXTENSIONS, glGetStringi
            from OpenGL.raw.GL.VERSION.GL_1_1 import glGetIntegerv

            count = GLint()
            glGetIntegerv(GL_NUM_EXTENSIONS, count)
            extensions = []
            for i in range(count.value):
                extension = glGetStringi(GL_EXTENSIONS, i)
                extensions.append(extension)
        # Add included-by-reference extensions...
        version = self.getVersion()
        if not version:
            # should not be possible?
            return version
        check = tuple(version[:2])
        for v, v_exts in VERSION_EXTENSIONS:
            if v <= check:
                for v_ext in v_exts:
                    if v_ext not in extensions:
                        extensions.append(as_8_bit(v_ext))
            else:
                break
        return extensions


GLQuerier = _GLQuerier()


class _GLUQuerier(ExtensionQuerier):
    prefix = as_8_bit('GLU_')
    version_prefix = as_8_bit('GLU_VERSION_GL_')

    def pullVersion(self):
        from OpenGL.GLU import gluGetString, GLU_VERSION

        return [
            int(x)
            for x in gluGetString(GLU_VERSION).split(as_8_bit('_'))
            if x.isdigit()
        ]

    def pullExtensions(self):
        from OpenGL.GLU import gluGetString, GLU_EXTENSIONS

        return gluGetString(GLU_EXTENSIONS).split()


GLUQuerier = _GLUQuerier()


def hasExtension(specifier):
    return ExtensionQuerier.hasExtension(specifier)


hasGLExtension = hasGLUExtension = hasExtension


class _Alternate(LateBind):
    def __init__(self, name, *alternates):
        """Initialize set of alternative implementations of the same function"""
        self.__name__ = name
        self._alternatives = alternates
        if root.MODULE_ANNOTATIONS:
            frame = sys._getframe().f_back
            if frame and frame.f_back and '__name__' in frame.f_back.f_globals:
                self.__module__ = frame.f_back.f_globals['__name__']

    def __bool__(self):
        from OpenGL import error

        try:
            return bool(self.getFinalCall())
        except error.NullFunctionError as err:
            return False

    __nonzero__ = __bool__  # Python 2.6 compatibility

    def finalise(self):
        """Call, doing a late lookup and bind to find an implementation"""
        for alternate in self._alternatives:
            if alternate:
                #                _log.info(
                #                    """Chose alternate: %s from %s""",
                #                    alternate.__name__,
                #                    ", ".join([x.__name__ for x in self._alternatives])
                #                )
                return alternate
        from OpenGL import error

        raise error.NullFunctionError(
            """Attempt to call an undefined alternate function (%s), check for bool(%s) before calling"""
            % (
                ', '.join([x.__name__ for x in self._alternatives]),
                self.__name__,
            )
        )


def alternate(name, *functions):
    """Construct a callable that functions as the first implementation found of given set of alternatives

    if name is a function then its name will be used....
    """
    if not isinstance(name, (bytes, unicode)):
        functions = (name,) + functions
        name = name.__name__
    return type(name, (_Alternate,), {})(name, *functions)