~ubuntu-branches/ubuntu/oneiric/nux/oneiric

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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
 * Copyright 2010 Inalogic® Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License, as
 * published by the  Free Software Foundation; either version 2.1 or 3.0
 * of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 *
 */


#include "GLResource.h"
#include "GraphicsDisplay.h"
#include "GpuDevice.h"
#include "GLDeviceObjects.h"
#include "IOpenGLSurface.h"

namespace nux
{

  NUX_IMPLEMENT_OBJECT_TYPE (IOpenGLSurface);

  IOpenGLSurface::~IOpenGLSurface()
  {

  }

  int IOpenGLSurface::RefCount() const
  {
    if (_BaseTexture)
      return _BaseTexture->RefCount();

    nuxAssert (0); // Surface with no underlying texture. That should not happen.
    return 0;
  }

  int IOpenGLSurface::LockRect (
    SURFACE_LOCKED_RECT *pLockedRect,
    const SURFACE_RECT *pRect)
  {
    // If _LockedRect.pBits or _LockedRect.Pitch are not equal to zero, then we have already Locked the buffer
    // Unlock it before locking again.
    nuxAssert (_LockedRect.pBits == 0);
    nuxAssert (_LockedRect.Pitch == 0);
    nuxAssert (_CompressedDataSize == 0);

    if ( (_LockedRect.pBits != 0) || (_LockedRect.Pitch != 0) || (_CompressedDataSize != 0) )
    {
      // already locked;
      return OGL_INVALID_LOCK;
    }

    _Rect.bottom = _Rect.left = _Rect.right = _Rect.top = 0;

    GLint unpack_alignment = GPixelFormats[_BaseTexture->_PixelFormat].RowMemoryAlignment;
    unsigned int halfUnpack = Log2 (unpack_alignment);

    CHECKGL ( glBindTexture (_STextureTarget, _BaseTexture->_OpenGLID) );

    unsigned int surface_size = 0;
    unsigned int BytePerPixel = 0;

    IOpenGLBaseTexture *texture = _BaseTexture;

    if (!((_BaseTexture->_ResourceType == RTTEXTURE) ||
          (_BaseTexture->_ResourceType == RTTEXTURERECTANGLE) ||
          (_BaseTexture->_ResourceType == RTCUBETEXTURE) ||
          (_BaseTexture->_ResourceType == RTVOLUMETEXTURE) ||
          (_BaseTexture->_ResourceType == RTANIMATEDTEXTURE)))
    {
      nuxAssertMsg (0, TEXT ("Unknown resource type") );
    }

    int texwidth, texheight;
    texwidth = ImageSurface::GetLevelDim (texture->_PixelFormat, texture->_Width, _SMipLevel);
    texheight = ImageSurface::GetLevelDim (texture->_PixelFormat, texture->_Height, _SMipLevel);

    if ( texture->_PixelFormat == BITFMT_DXT1 ||
         texture->_PixelFormat == BITFMT_DXT2 ||
         texture->_PixelFormat == BITFMT_DXT3 ||
         texture->_PixelFormat == BITFMT_DXT4 ||
         texture->_PixelFormat == BITFMT_DXT5)
    {
      if (texture->_PixelFormat == BITFMT_DXT1)
      {
        // We can conceive a 4x4 DXT1 block as if each texel uses 4 bits.
        // Actually, for DXT, we have 2 16-bits colors(5:6:5), and each texel uses 2 bits to interpolate
        // between the 2 colors.
        //    ---------------------
        //    |      COLOR0       | 16 bits
        //    ---------------------
        //    |      COLOR1       | 16 bits
        //    ---------------------
        //    | xx | xx | xx | xx | xx = 2 bits
        //    ---------------------
        //    | xx | xx | xx | xx |
        //    ---------------------
        //    | xx | xx | xx | xx |
        //    ---------------------
        //    | xx | xx | xx | xx |
        //    ---------------------

        // A line of n texel DXT1 data uses n/2 bytes (4 bits/texel). So the number of bytes used for a
        // texwidth texel, is texwidth/2 bytes.
        // Note that texwidth is divisible by 4(to to the upper rounding to 4), therefore, it is also divisible
        // by 2.

        // glCompressedTexImage2DARB, glCompressedTexImage3DARB,
        // glCompressedTexSubImage2DARB, glCompressedTexSubImage3DARB are not affected by glPixelStorei.
        surface_size = ImageSurface::GetLevelSize (texture->_PixelFormat, texture->_Width, texture->_Height, _SMipLevel);
        _CompressedDataSize = surface_size;
        _LockedRect.Pitch = ImageSurface::GetLevelPitch (texture->_PixelFormat, texture->_Width, texture->_Height, _SMipLevel);

        if (_Initialized == false)
        {
          InitializeLevel();
        }
      }
      else
      {
        // A line of n texel DXT3/5 data uses n bytes (1 byte/texel). So the number of bytes used for a
        // texwidth texels, is texwidth bytes.

        // glCompressedTexImage2DARB, glCompressedTexImage3DARB,
        // glCompressedTexSubImage2DARB, glCompressedTexSubImage3DARB are not affected by glPixelStorei.
        surface_size = ImageSurface::GetLevelSize (texture->_PixelFormat, texture->_Width, texture->_Height, _SMipLevel);
        _CompressedDataSize = surface_size;
        _LockedRect.Pitch = ImageSurface::GetLevelPitch (texture->_PixelFormat, texture->_Width, texture->_Height, _SMipLevel);

        if (_Initialized == false)
        {
          InitializeLevel();
        }
      }
    }
    else
    {
      _LockedRect.Pitch = ImageSurface::GetLevelPitch (texture->_PixelFormat, texture->_Width, texture->_Height, _SMipLevel);
      surface_size = ImageSurface::GetLevelSize (texture->_PixelFormat, texture->_Width, texture->_Height, _SMipLevel);

      if (_Initialized == false)
      {
        InitializeLevel();
      }
    }

    _Rect.left  = 0;
    _Rect.top   = 0;
    _Rect.bottom  = texheight;
    _Rect.right   = texwidth;


    if (GetGraphicsDisplay()->GetGpuDevice()->UsePixelBufferObjects() )
    {
      GetGraphicsDisplay()->GetGpuDevice()->AllocateUnpackPixelBufferIndex (&_AllocatedUnpackBuffer);
    }

    if (pRect == 0)
    {
      if (GetGraphicsDisplay()->GetGpuDevice()->UsePixelBufferObjects() )
      {
        // Mapping the entire area of the surface
        _LockedRect.pBits = GetGraphicsDisplay()->GetGpuDevice()->LockUnpackPixelBufferIndex (_AllocatedUnpackBuffer, surface_size);
        pLockedRect->pBits = _LockedRect.pBits;
        pLockedRect->Pitch = _LockedRect.Pitch;
      }
      else
      {
        //[DEBUGGING - NO PBO]
        // Mapping the entire area of the surface
        _LockedRect.pBits = new BYTE[surface_size];
        pLockedRect->pBits = _LockedRect.pBits;
        pLockedRect->Pitch = _LockedRect.Pitch;
      }
    }
    else
    {
      //[WARNING]
      // this section of code is suppose to handle rectangles that are not the size of the entire surface.
      // It works for uncompressed texture. However it hasn't been tested for compressed texture.

      // reserve and lock a surface size equal  to (RectWidth * RectHeight * BytePerPixel)
      int RectWidth = pRect->right - pRect->left;
      int RectHeight = pRect->bottom - pRect->top;

      nuxAssert (RectWidth >= 0);
      nuxAssert (RectHeight >= 0);


      unsigned int RectSize = ( ( (RectWidth * BytePerPixel + (unpack_alignment - 1) ) >> (halfUnpack) ) << (halfUnpack) ) * RectHeight;

      if (RectSize == 0)
      {
        pLockedRect->pBits = 0;
        pLockedRect->Pitch = 0;
        return OGL_INVALID_LOCK;
      }

      if (GetGraphicsDisplay()->GetGpuDevice()->UsePixelBufferObjects() )
      {
        _LockedRect.pBits = GetGraphicsDisplay()->GetGpuDevice()->LockUnpackPixelBufferIndex (_AllocatedUnpackBuffer, RectSize);
        pLockedRect->pBits = ( (BYTE *) _LockedRect.pBits);
        pLockedRect->Pitch = ( ( (RectWidth * BytePerPixel + (unpack_alignment - 1) ) >> (halfUnpack) ) << (halfUnpack) );
      }
      else
      {
        //[DEBUGGING - NO PBO]
        _LockedRect.pBits = new BYTE[RectSize];
        pLockedRect->pBits = ( (BYTE *) _LockedRect.pBits);
        pLockedRect->Pitch = ( ( (RectWidth * BytePerPixel + (unpack_alignment - 1) ) >> (halfUnpack) ) << (halfUnpack) );
      }

      _Rect.left  = pRect->left;
      _Rect.top   = pRect->top;
      _Rect.bottom  = pRect->bottom;
      _Rect.right   = pRect->right;
    }

    return OGL_OK;
  }

  int IOpenGLSurface::UnlockRect()
  {
    if (_LockedRect.pBits == 0)
    {
      return OGL_INVALID_UNLOCK;
    }

    CHECKGL ( glPixelStorei (GL_UNPACK_ALIGNMENT, _BaseTexture->GetFormatRowMemoryAlignment() ) );

    BYTE *DataPtr = 0;

    if (_STextureTarget == GL_TEXTURE_2D || _STextureTarget == GL_TEXTURE_RECTANGLE_ARB || _STextureTarget == GL_TEXTURE_CUBE_MAP || _STextureTarget == GL_TEXTURE_3D)
    {
      int w = _Rect.right - _Rect.left;
      int h = _Rect.bottom - _Rect.top;
      CHECKGL ( glBindTexture (_STextureTarget, _BaseTexture->_OpenGLID) );

      if (GetGraphicsDisplay()->GetGpuDevice()->UsePixelBufferObjects() )
      {
        // Unmap the texture image buffer
        GetGraphicsDisplay()->GetGpuDevice()->BindUnpackPixelBufferIndex (_AllocatedUnpackBuffer);
        CHECKGL ( glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB) );
        DataPtr = NUX_BUFFER_OFFSET (0);
      }
      else
      {
        CHECKGL ( glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0) );
        DataPtr = (BYTE *) _LockedRect.pBits;
      }

      IOpenGLTexture2D *texture = (IOpenGLTexture2D *) _BaseTexture;

      if ( /*texture->_PixelFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ||*/
        texture->_PixelFormat == BITFMT_DXT1 ||
        texture->_PixelFormat == BITFMT_DXT2 ||
        texture->_PixelFormat == BITFMT_DXT3 ||
        texture->_PixelFormat == BITFMT_DXT4 ||
        texture->_PixelFormat == BITFMT_DXT5)
      {
        nuxAssert (_CompressedDataSize != 0);
        int width = Max<int> (1, texture->_Width >> _SMipLevel);
        int height = Max<int> (1, texture->_Height >> _SMipLevel);

        int xoffset = _Rect.left;
        int yoffset = _Rect.top;

        if (_STextureTarget != GL_TEXTURE_3D)
        {
          glCompressedTexSubImage2DARB (_SSurfaceTarget,
                                        _SMipLevel,                 // level
                                        xoffset,
                                        yoffset,
                                        width,
                                        height,
                                        GPixelFormats[texture->_PixelFormat].PlatformFormat,
                                        _CompressedDataSize,        // image Size
                                        DataPtr                     // data
                                       );
          CHECKGL_MSG (glCompressedTexSubImage2DARB);
        }
        else
        {
          glCompressedTexSubImage3DARB (_SSurfaceTarget,
                                        _SMipLevel,                 // level
                                        xoffset,
                                        yoffset,
                                        0,
                                        width,
                                        height,
                                        _SSlice,
                                        GPixelFormats[texture->_PixelFormat].PlatformFormat,
                                        _CompressedDataSize,        // image Size
                                        DataPtr                     // data
                                       );
          CHECKGL_MSG (glCompressedTexSubImage3DARB);
        }

        // We can use glCompressedTexImage2DARB if we are sure we always lock
        // the entire area of the surface.
        //            glCompressedTexImage2DARB(_SSurfaceTarget,
        //                _SMipLevel,                 // level
        //                texture->_Format,
        //                width,
        //                height,
        //                0,                          // border
        //                _CompressedDataSize,        // image Size
        //                _LockedRect.pBits           // data
        //                );
        //            CHECKGL_MSG(glCompressedTexImage2DARB);


        //            { //[DEBUGGING - Red Texture]
        //                // This is going to put some red in the texture.
        //                // The texture is not compressed.
        //                BYTE *color_array = new BYTE[width*height*4];
        //                for(int i = 0; i < width*height*4; i += 4)
        //                {
        //                    color_array[i + 0] = 0xff;
        //                    color_array[i + 1] = _SMipLevel * 0x3F;
        //                    color_array[i + 2] = 0x0;
        //                    color_array[i + 3] = 0xFF;
        //                }
        //                glTexImage2D(GL_TEXTURE_2D,
        //                    _SMipLevel,
        //                    GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, color_array);
        //                CHECKGL_MSG(glTexImage2D);
        //                delete [] color_array;
        //            }
      }
      else
      {
        //CHECKGL( glPixelStorei(GL_UNPACK_ROW_LENGTH, w) );
        if (_STextureTarget != GL_TEXTURE_3D)
        {
          CHECKGL ( glTexSubImage2D (_SSurfaceTarget,
                                     _SMipLevel,
                                     _Rect.left,
                                     _Rect.top,
                                     w,
                                     h,
                                     GPixelFormats[texture->_PixelFormat].Format,
                                     GPixelFormats[texture->_PixelFormat].type,
                                     DataPtr
                                    ) );
        }
        else
        {
          CHECKGL ( glTexSubImage3D (_SSurfaceTarget,
                                     _SMipLevel,
                                     _Rect.left,
                                     _Rect.top,
                                     _SSlice,          // z offset
                                     w,
                                     h,
                                     1,
                                     GPixelFormats[texture->_PixelFormat].Format,
                                     GPixelFormats[texture->_PixelFormat].type,
                                     DataPtr
                                    ) );
        }

        //CHECKGL( glPixelStorei(GL_UNPACK_ROW_LENGTH, 0) );

        //            CHECKGL( glTexImage2D(_SSurfaceTarget,
        //                _SMipLevel,
        //                GPixelFormats[texture->_PixelFormat].PlatformFormat,
        //                w,
        //                h,
        //                0,
        //                //### todo:
        //                //  - find the correct format matching the internal format
        //                //  - find the correct type matching the internal format
        //                GPixelFormats[texture->_PixelFormat].Format,
        //                GPixelFormats[texture->_PixelFormat].type,
        //                DataPtr
        //                ) );

      }
    }
    else
    {
      nuxDebugMsg (TEXT("[IOpenGLSurface::UnlockRect] Incorrect Texture Target."));
    }

    if (GetGraphicsDisplay()->GetGpuDevice()->UsePixelBufferObjects() )
    {
      CHECKGL ( glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0) );
      GetGraphicsDisplay()->GetGpuDevice()->FreeUnpackPixelBufferIndex (_AllocatedUnpackBuffer);
    }
    else
    {
      //[DEBUGGING - NO PBO]
      if (_LockedRect.pBits)
      {
        delete [] ( (BYTE *) _LockedRect.pBits);
      }
    }

    CHECKGL ( glPixelStorei (GL_UNPACK_ALIGNMENT, GetGraphicsDisplay()->GetGpuDevice()->GetPixelStoreAlignment() ) );

    _LockedRect.pBits = 0;
    _LockedRect.Pitch = 0;
    _CompressedDataSize = 0;

    return OGL_OK;
  }

  int IOpenGLSurface::InitializeLevel()
  {
    // Because we use SubImage when unlocking surfaces, we must first get some dummy data in the surface before we can make a lock.
    int texwidth = ImageSurface::GetLevelWidth (_BaseTexture->_PixelFormat, _BaseTexture->_Width, _SMipLevel);
    int texheight = ImageSurface::GetLevelHeight (_BaseTexture->_PixelFormat, _BaseTexture->_Height, _SMipLevel);
    int size = ImageSurface::GetLevelSize (_BaseTexture->_PixelFormat, _BaseTexture->_Width, _BaseTexture->_Height, _SMipLevel);
    int MemAlignment = ImageSurface::GetMemAlignment (_BaseTexture->_PixelFormat);

    nuxAssert ( texwidth > 0 ); // Should never happen
    nuxAssert ( texheight > 0 ); // Should never happen
    nuxAssert ( size > 0 ); // Should never happen

    BYTE *DummyBuffer = new BYTE[size];
    Memset (DummyBuffer, 0, size);

    CHECKGL ( glPixelStorei (GL_UNPACK_ALIGNMENT, MemAlignment) );

    if ( _BaseTexture->_PixelFormat == BITFMT_DXT1 ||
         _BaseTexture->_PixelFormat == BITFMT_DXT2 ||
         _BaseTexture->_PixelFormat == BITFMT_DXT3 ||
         _BaseTexture->_PixelFormat == BITFMT_DXT4 ||
         _BaseTexture->_PixelFormat == BITFMT_DXT5)
    {
      if (_STextureTarget != GL_TEXTURE_3D)
      {
        glCompressedTexImage2DARB (
          _SSurfaceTarget,
          _SMipLevel,                 // level
          GPixelFormats[_BaseTexture->_PixelFormat].PlatformFormat,
          texwidth,
          texheight,
          0,                          // border
          size,                       // image Size
          DummyBuffer                           // data
        );
        CHECKGL_MSG (glCompressedTexImage2DARB);
      }
      else
      {
        //             glCompressedTexImage3DARB(
        //                 _SSurfaceTarget,
        //                 _SMipLevel,                 // level
        //                 GPixelFormats[_BaseTexture->_PixelFormat].PlatformFormat,
        //                 texwidth,
        //                 texheight,
        //                 _SSlice,
        //                 0,                          // border
        //                 size,                       // image Size
        //                 DummyBuffer                           // data
        //                 );
        //             CHECKGL_MSG(glCompressedTexImage3DARB);
      }
    }
    else
    {
      if (_STextureTarget != GL_TEXTURE_3D)
      {
        glTexImage2D (
          _SSurfaceTarget,
          _SMipLevel,                 // level
          GPixelFormats[_BaseTexture->_PixelFormat].PlatformFormat,
          texwidth,
          texheight,
          0,                          // border
          GPixelFormats[_BaseTexture->_PixelFormat].Format,
          GPixelFormats[_BaseTexture->_PixelFormat].type,
          DummyBuffer);
        CHECKGL_MSG (glTexImage2D);
      }
      else
      {
        //             glTexImage3D(
        //                 _SSurfaceTarget,
        //                 _SMipLevel,                 // level
        //                 GPixelFormats[PixelFormat].PlatformFormat,
        //                 texwidth,
        //                 texheight,
        //                 _SSlice,
        //                 0,                          // border
        //                 GPixelFormats[PixelFormat].Format,
        //                 GPixelFormats[PixelFormat].type,
        //                 DummyBuffer);
        //             CHECKGL_MSG(glTexImage3D);
      }
    }

    delete [] DummyBuffer;


    //    { //[DEBUGGING - Red Texture]
    //        // This is going to put some red in the texture.
    //        // The texture is not compressed.
    //        BYTE *color_array = new BYTE[texwidth*texheight*4];
    //        for(unsigned int i = 0; i < texwidth*texheight*4; i += 4)
    //        {
    //            color_array[i + 0] = 0xff;
    //            color_array[i + 1] = _SMipLevel * 0x3F;
    //            color_array[i + 2] = 0x0;
    //            color_array[i + 3] = 0xFF;
    //        }
    //        glTexImage2D(GL_TEXTURE_2D,
    //            _SMipLevel,
    //            GL_RGBA8, texwidth, texheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, color_array);
    //        CHECKGL_MSG(glTexImage2D);
    //        delete [] color_array;
    //    }

    CHECKGL (glPixelStorei (GL_UNPACK_ALIGNMENT, GetGraphicsDisplay()->GetGpuDevice()->GetPixelStoreAlignment()));

    _Initialized = true;
    return OGL_OK;
  }

  void IOpenGLSurface::CopyRenderTarget (int x, int y, int width, int height)
  {
    CHECKGL (glPixelStorei (GL_UNPACK_ALIGNMENT, _BaseTexture->GetFormatRowMemoryAlignment ()));

    if (_STextureTarget == GL_TEXTURE_2D || _STextureTarget == GL_TEXTURE_RECTANGLE_ARB || _STextureTarget == GL_TEXTURE_CUBE_MAP || _STextureTarget == GL_TEXTURE_3D)
    {
      CHECKGL ( glBindTexture (_STextureTarget, _BaseTexture->_OpenGLID) );


#ifndef NUX_OPENGLES_20
      if (_STextureTarget != GL_TEXTURE_3D)
      {
        CHECKGL (glCopyTexImage2D (_SSurfaceTarget,
          _SMipLevel,
          GPixelFormats [_BaseTexture->_PixelFormat].Format,
          x,
          y,
          width,
          height,
          0));
      }
      else
      {
        CHECKGL (glCopyTexSubImage3D (_SSurfaceTarget,
          _SMipLevel,
          0,
          0,
          0,
          x,
          y,
          width,
          height));
      }
#else
      if (_STextureTarget != GL_TEXTURE_3D)
      {
        CHECKGL (glCopyTexImage2D (_SSurfaceTarget,
          _SMipLevel,
          GPixelFormats [texture->_PixelFormat].Format,
          x,
          y,
          width,
          height,
          0));
      }
#endif
    }
  }

  void* IOpenGLSurface::GetSurfaceData (int &width, int &height, int &format)
  {
    width = 0;
    height = 0;
    format = BITFMT_UNKNOWN;

    // Because we use SubImage when unlocking surfaces, we must first get some dummy data in the surface before we can make a lock.
    int texwidth = ImageSurface::GetLevelWidth (_BaseTexture->_PixelFormat, _BaseTexture->_Width, _SMipLevel);
    int texheight = ImageSurface::GetLevelHeight (_BaseTexture->_PixelFormat, _BaseTexture->_Height, _SMipLevel);

    nuxAssert (texwidth > 0); // Should never happen
    nuxAssert (texheight > 0); // Should never happen

    CHECKGL (glBindTexture (_STextureTarget, _BaseTexture->_OpenGLID));

    CHECKGL (glPixelStorei (GL_PACK_ALIGNMENT, 1));
    int size = texwidth * texheight * 4; // assume a memory alignment of 1

    unsigned char *img = new unsigned char [size];

    CHECKGL (glGetTexImage (_STextureTarget, _SMipLevel, GL_RGBA, GL_UNSIGNED_BYTE, img));

    width = _BaseTexture->_Width;
    height = _BaseTexture->_Height;
    format = BITFMT_R8G8B8A8;
    return img;
  }

}