File: GLTexture.py

package info (click to toggle)
pymca 5.8.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,392 kB
  • sloc: python: 155,456; ansic: 15,843; makefile: 116; sh: 73; xml: 55
file content (336 lines) | stat: -rw-r--r-- 13,842 bytes parent folder | download | duplicates (6)
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
# /*#########################################################################
#
# The PyMca X-Ray Fluorescence Toolkit
#
# Copyright (c) 2004-2014 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
__author__ = "T. Vincent - ESRF Data Analysis"
__contact__ = "thomas.vincent@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__doc__ = """
This module provides classes wrapping OpenGL texture.
"""


# import ######################################################################

from .gl import *  # noqa
from ctypes import c_void_p
import numpy as np


# texture #####################################################################

class Texture2D(object):
    """Wraps OpenGL texture2D"""
    def __init__(self, internalFormat, width, height,
                 format_=None, type_=GL_FLOAT, data=None, texUnit=0,
                 minFilter=None, magFilter=None, wrapS=None, wrapT=None,
                 unpackAlign=1,
                 unpackRowLength=0, unpackSkipPixels=0, unpackSkipRows=0):
        self._internalFormat = internalFormat
        self._width, self._height = width, height
        self.texUnit = texUnit

        self._tid = glGenTextures(1)
        self.bind(self.texUnit)

        if minFilter is not None:
            glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter)
        if magFilter is not None:
            glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter)
        if wrapS is not None:
            glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS)
        if wrapT is not None:
            glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT)

        glPixelStorei(GL_UNPACK_ALIGNMENT, unpackAlign)
        if unpackRowLength:
            glPixelStorei(GL_UNPACK_ROW_LENGTH, unpackRowLength)
        if unpackSkipPixels:
            glPixelStorei(GL_UNPACK_SKIP_PIXELS, unpackSkipPixels)
        if unpackSkipRows:
            glPixelStorei(GL_UNPACK_SKIP_ROWS, unpackSkipRows)

        glTexImage2D(GL_TEXTURE_2D, 0, self.internalFormat,
                     self.width, self.height, 0,
                     format_ if format_ is not None else self.internalFormat,
                     type_,
                     data if data is not None else c_void_p(0))

        if unpackRowLength:
            glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)
        if unpackSkipPixels:
            glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0)
        if unpackSkipRows:
            glPixelStorei(GL_UNPACK_SKIP_ROWS, 0)

        glBindTexture(GL_TEXTURE_2D, 0)

    @property
    def internalFormat(self):
        return self._internalFormat

    @property
    def width(self):
        return self._width

    @property
    def height(self):
        return self._height

    @property
    def tid(self):
        """OpenGL texture ID"""
        try:
            return self._tid
        except AttributeError:
            raise RuntimeError("No OpenGL texture resource, \
                               discard has already been called")

    def discard(self):
        if hasattr(self, '_tid'):
            if bool(glDeleteTextures):  # Test for __del__
                glDeleteTextures(np.array((self._tid,)))
            del self._tid

    def __del__(self):
        self.discard()

    def bind(self, texUnit=None):
        texUnit = texUnit if texUnit is not None else self.texUnit
        glActiveTexture(GL_TEXTURE0 + texUnit)
        glBindTexture(GL_TEXTURE_2D, self.tid)

    def update(self, format_, type_, data,
               xOffset=0, yOffset=0, width=None, height=None,
               texUnit=None, unpackAlign=1,
               unpackRowLength=0, unpackSkipPixels=0, unpackSkipRows=0):

        if unpackRowLength:
            glPixelStorei(GL_UNPACK_ROW_LENGTH, unpackRowLength)
        if unpackSkipPixels:
            glPixelStorei(GL_UNPACK_SKIP_PIXELS, unpackSkipPixels)
        if unpackSkipRows:
            glPixelStorei(GL_UNPACK_SKIP_ROWS, unpackSkipRows)

        glPixelStorei(GL_UNPACK_ALIGNMENT, unpackAlign)

        self.bind(texUnit)
        glTexSubImage2D(GL_TEXTURE_2D, 0,
                        xOffset, yOffset,
                        width or self.width,
                        height or self.height,
                        format_, type_, data)
        glBindTexture(GL_TEXTURE_2D, 0)

        if unpackRowLength:
            glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)
        if unpackSkipPixels:
            glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0)
        if unpackSkipRows:
            glPixelStorei(GL_UNPACK_SKIP_ROWS, 0)


def _checkTexture2D(internalFormat, width, height,
                    format_=None, type_=GL_FLOAT, border=0):
    """Check if texture size with provided parameters is supported

    :rtype: bool
    """
    glTexImage2D(GL_PROXY_TEXTURE_2D, 0, internalFormat,
                 width, height, border,
                 format_ or internalFormat,
                 type_, c_void_p(0))
    width = glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH)
    return bool(width)


MIN_TEXTURE_SIZE = 64


def _getMaxSquareTexture2DSize(internalFormat=GL_RGBA,
                               format_=None, type_=GL_FLOAT, border=0):
    """Returns a supported size for a corresponding square texture

    :returns: GL_MAX_TEXTURE_SIZE or a smaller supported size (not optimal)
    :rtype: tuple
    """
    # Is this useful?
    maxTexSize = glGetIntegerv(GL_MAX_TEXTURE_SIZE)
    while maxTexSize > MIN_TEXTURE_SIZE and \
        not _checkTexture2D(internalFormat, maxTexSize, maxTexSize,
                            format_, type_, border):
        maxTexSize = maxTexSize // 2
    return max(MIN_TEXTURE_SIZE, maxTexSize)


class Image(object):
    """Image of any size eventually using multiple textures or larger texture
    """

    _WRAP_S = GL_CLAMP_TO_EDGE
    _WRAP_T = GL_CLAMP_TO_EDGE
    _MIN_FILTER = GL_NEAREST
    _MAG_FILTER = GL_NEAREST

    def __init__(self, internalFormat, width, height,
                 format_=None, type_=GL_FLOAT, data=None,
                 texUnit=0, unpackAlign=1):
        self.internalFormat = internalFormat
        self.width, self.height = width, height

        if _checkTexture2D(internalFormat, width, height, format_, type_):
            texture = Texture2D(internalFormat, width, height,
                                format_, type_, data, texUnit=texUnit,
                                minFilter=self._MIN_FILTER,
                                magFilter=self._MAG_FILTER,
                                wrapS=self._WRAP_S, wrapT=self._WRAP_T,
                                unpackAlign=unpackAlign)
            vertices = np.array((
                (0., 0.,        0., 0.),
                (width, 0.,     1., 0.),
                (0., height,    0., 1.),
                (width, height, 1., 1.)), dtype=np.float32)
            self.tiles = ((texture, vertices,
                           {'xOrigData': 0, 'yOrigData': 0,
                            'wData': width, 'hData': height}),)

        else:
            # Handle dimension too large: make tiles
            maxTexSize = _getMaxSquareTexture2DSize(internalFormat,
                                                    format_, type_)

            nCols = (width+maxTexSize-1) // maxTexSize
            colWidths = [width // nCols] * nCols
            colWidths[-1] += width % nCols

            nRows = (height+maxTexSize-1) // maxTexSize
            rowHeights = [height//nRows] * nRows
            rowHeights[-1] += height % nRows

            tiles = []
            yOrig = 0
            for hData in rowHeights:
                xOrig = 0
                for wData in colWidths:
                    if (hData < MIN_TEXTURE_SIZE or wData < MIN_TEXTURE_SIZE) \
                        and not _checkTexture2D(internalFormat, wData, hData,
                                                format_, type_):
                        # Ensure texture size is at least MIN_TEXTURE_SIZE
                        tH = max(hData, MIN_TEXTURE_SIZE)
                        tW = max(wData, MIN_TEXTURE_SIZE)

                        uMax, vMax = float(wData)/tW, float(hData)/tH

                        texture = Texture2D(internalFormat, tW, tH,
                                            format_, type_,
                                            None, texUnit=texUnit,
                                            minFilter=self._MIN_FILTER,
                                            magFilter=self._MAG_FILTER,
                                            wrapS=self._WRAP_S,
                                            wrapT=self._WRAP_T,
                                            unpackAlign=unpackAlign)
                        texture.update(format_, type_, data,
                                       width=wData, height=hData,
                                       unpackRowLength=width,
                                       unpackSkipPixels=xOrig,
                                       unpackSkipRows=yOrig)
                    else:
                        uMax, vMax = 1, 1
                        texture = Texture2D(internalFormat, wData, hData,
                                            format_, type_,
                                            data, texUnit=texUnit,
                                            minFilter=self._MIN_FILTER,
                                            magFilter=self._MAG_FILTER,
                                            wrapS=self._WRAP_S,
                                            wrapT=self._WRAP_T,
                                            unpackAlign=unpackAlign,
                                            unpackRowLength=width,
                                            unpackSkipPixels=xOrig,
                                            unpackSkipRows=yOrig)
                    vertices = np.array((
                        (xOrig, yOrig,         0., 0.),
                        (xOrig + wData, yOrig,     uMax, 0.),
                        (xOrig, yOrig + hData,     0., vMax),
                        (xOrig + wData, yOrig + hData, uMax, vMax)),
                        dtype=np.float32)
                    tiles.append((texture, vertices,
                                  {'xOrigData': xOrig, 'yOrigData': yOrig,
                                   'wData': wData, 'hData': hData}))
                    xOrig += wData
                yOrig += hData
            self.tiles = tuple(tiles)

    def discard(self):
        for texture, vertices, _ in self.tiles:
            texture.discard()
        del self.tiles

    def updateAll(self, format_, type_, data, texUnit=0, unpackAlign=1):
        if not hasattr(self, 'tiles'):
            raise RuntimeError("No texture, discard has already been called")

        assert data.shape[:2] == (self.height, self.width)
        if len(self.tiles) == 1:
            self.tiles[0][0].update(format_, type_, data,
                                    width=self.width, height=self.height,
                                    texUnit=texUnit, unpackAlign=unpackAlign)
        else:
            for texture, _, info in self.tiles:
                texture.update(format_, type_, data,
                               width=info['wData'], height=info['hData'],
                               texUnit=texUnit, unpackAlign=unpackAlign,
                               unpackRowLength=self.width,
                               unpackSkipPixels=info['xOrigData'],
                               unpackSkipRows=info['yOrigData'])

    def render(self, posAttrib, texAttrib, texUnit=0):
        try:
            tiles = self.tiles
        except AttributeError:
            raise RuntimeError("No texture, discard has already been called")

        for texture, vertices, _ in tiles:
            texture.bind(texUnit)

            stride = vertices.shape[-1] * vertices.itemsize
            glEnableVertexAttribArray(posAttrib)
            glVertexAttribPointer(posAttrib,
                                  2,
                                  GL_FLOAT,
                                  GL_FALSE,
                                  stride, vertices)

            texCoordsPtr = c_void_p(vertices.ctypes.data +
                                    2 * vertices.itemsize)
            glEnableVertexAttribArray(texAttrib)
            glVertexAttribPointer(texAttrib,
                                  2,
                                  GL_FLOAT,
                                  GL_FALSE,
                                  stride, texCoordsPtr)
            glDrawArrays(GL_TRIANGLE_STRIP, 0, len(vertices))