root/OpenSceneGraph/trunk/src/osg/Texture2DArray.cpp @ 13041

Revision 13041, 29.4 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13#include <osg/GLExtensions>
14#include <osg/Texture2DArray>
15#include <osg/State>
16#include <osg/Notify>
17
18#include <string.h>
19
20
21using namespace osg;
22
23Texture2DArray::Texture2DArray():
24            _textureWidth(0),
25            _textureHeight(0),
26            _textureDepth(0),
27            _numMipmapLevels(0)
28{
29}
30
31Texture2DArray::Texture2DArray(const Texture2DArray& text,const CopyOp& copyop):
32            Texture(text,copyop),
33            _textureWidth(text._textureWidth),
34            _textureHeight(text._textureHeight),
35            _textureDepth(text._textureDepth),
36            _numMipmapLevels(text._numMipmapLevels),
37            _subloadCallback(text._subloadCallback)
38{
39    // copy all images by iterating through all of them
40    for (int i=0; i < text._textureDepth; i++)
41    {
42        _images.push_back(copyop(text._images[i].get()));
43        _modifiedCount.push_back(ImageModifiedCount());
44    }
45}
46
47Texture2DArray::~Texture2DArray()
48{
49}
50
51int Texture2DArray::compare(const StateAttribute& sa) const
52{
53    // check the types are equal and then create the rhs variable
54    // used by the COMPARE_StateAttribute_Parameter macros below.
55    COMPARE_StateAttribute_Types(Texture2DArray,sa)
56
57    bool noImages = true;
58    for (int n=0; n < _textureDepth; n++)
59    {
60        if (noImages && _images[n].valid()) noImages = false;
61        if (noImages && rhs._images[n].valid()) noImages = false;
62
63        if (_images[n]!=rhs._images[n]) // smart pointer comparison.
64        {
65            if (_images[n].valid())
66            {
67                if (rhs._images[n].valid())
68                {
69                    int result = _images[n]->compare(*rhs._images[n]);
70                    if (result!=0) return result;
71                }
72                else
73                {
74                    return 1; // valid lhs._image is greater than null.
75                }
76            }
77            else if (rhs._images[n].valid())
78            {
79                return -1; // valid rhs._image is greater than null.
80            }
81        }
82    }
83
84
85    if (noImages)
86    {
87        int result = compareTextureObjects(rhs);
88        if (result!=0) return result;
89    }
90
91    int result = compareTexture(rhs);
92    if (result!=0) return result;
93
94    // compare each parameter in turn against the rhs.
95    COMPARE_StateAttribute_Parameter(_textureWidth)
96    COMPARE_StateAttribute_Parameter(_textureHeight)
97    COMPARE_StateAttribute_Parameter(_textureDepth)
98    COMPARE_StateAttribute_Parameter(_subloadCallback)
99
100    return 0; // passed all the above comparison macros, must be equal.
101}
102
103void Texture2DArray::setImage(unsigned int layer, Image* image)
104{
105    // check if the layer exceeds the texture depth
106    if (static_cast<int>(layer) >= _textureDepth)
107    {
108        // print warning and do nothing
109        OSG_WARN<<"Warning: Texture2DArray::setImage(..) failed, the given layer number is bigger then the size of the texture array."<<std::endl;
110        return;
111    }
112
113    if (_images[layer] == image) return;
114
115    unsigned numImageRequireUpdateBefore = 0;
116    for (unsigned int i=0; i<getNumImages(); ++i)
117    {
118        if (_images[i].valid() && _images[i]->requiresUpdateCall()) ++numImageRequireUpdateBefore;
119    }
120
121    // set image
122    _images[layer] = image;
123   _modifiedCount[layer].setAllElementsTo(0);
124
125    // find out if we need to reset the update callback to handle the animation of image
126    unsigned numImageRequireUpdateAfter = 0;
127    for (unsigned int i=0; i<getNumImages(); ++i)
128    {
129        if (_images[i].valid() && _images[i]->requiresUpdateCall()) ++numImageRequireUpdateAfter;
130    }
131
132    if (numImageRequireUpdateBefore>0)
133    {
134        if (numImageRequireUpdateAfter==0)
135        {
136            setUpdateCallback(0);
137            setDataVariance(osg::Object::STATIC);
138        }
139    }
140    else if (numImageRequireUpdateAfter>0)
141    {
142        setUpdateCallback(new Image::UpdateCallback());
143        setDataVariance(osg::Object::DYNAMIC);
144    }
145}
146
147void Texture2DArray::setTextureSize(int width, int height, int depth)
148{
149    // set dimensions
150    _textureWidth = width;
151    _textureHeight = height;
152    setTextureDepth(depth);
153}
154
155void Texture2DArray::setTextureDepth(int depth)
156{
157    // if we decrease the number of layers, then delete non-used
158    if (depth < _textureDepth)
159    {
160        _images.resize(depth);
161        _modifiedCount.resize(depth);
162    }
163
164    // if we increase the array, then add new empty elements
165    if (depth > _textureDepth)
166    {
167        _images.resize(depth, ref_ptr<Image>(0));
168        _modifiedCount.resize(depth, ImageModifiedCount());
169    }
170
171    // resize the texture array
172    _textureDepth = depth;
173}
174
175Image* Texture2DArray::getImage(unsigned int layer)
176{
177    return _images[layer].get();
178}
179
180const Image* Texture2DArray::getImage(unsigned int layer) const
181{
182    return _images[layer].get();
183}
184
185bool Texture2DArray::imagesValid() const
186{
187    if (_textureDepth < 1) return false;
188    for (int n=0; n < _textureDepth; n++)
189    {
190        if (!_images[n].valid() || !_images[n]->data())
191            return false;
192    }
193    return true;
194}
195
196void Texture2DArray::computeInternalFormat() const
197{
198    if (imagesValid()) computeInternalFormatWithImage(*_images[0]);
199    else computeInternalFormatType();
200}
201
202
203void Texture2DArray::apply(State& state) const
204{
205    // get the contextID (user defined ID of 0 upwards) for the
206    // current OpenGL context.
207    const unsigned int contextID = state.getContextID();
208
209    Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get();
210    ElapsedTime elapsedTime(&(tom->getApplyTime()));
211    tom->getNumberApplied()++;
212
213    const Extensions* extensions = getExtensions(contextID,true);
214
215    // if not supported, then return
216    if (!extensions->isTexture2DArraySupported() || !extensions->isTexture3DSupported())
217    {
218        OSG_WARN<<"Warning: Texture2DArray::apply(..) failed, 2D texture arrays are not support by OpenGL driver."<<std::endl;
219        return;
220    }
221
222    // get the texture object for the current contextID.
223    TextureObject* textureObject = getTextureObject(contextID);
224
225    if (textureObject && _textureDepth>0)
226    {
227        const osg::Image* image = _images[0].get();
228        if (image && getModifiedCount(0, contextID) != image->getModifiedCount())
229        {
230            // compute the internal texture format, this set the _internalFormat to an appropriate value.
231            computeInternalFormat();
232
233            GLsizei new_width, new_height, new_numMipmapLevels;
234
235            // compute the dimensions of the texture.
236            computeRequiredTextureDimensions(state, *image, new_width, new_height, new_numMipmapLevels);
237
238            if (!textureObject->match(GL_TEXTURE_2D_ARRAY_EXT, new_numMipmapLevels, _internalFormat, new_width, new_height, 1, _borderWidth))
239            {
240                Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get());
241                _textureObjectBuffer[contextID] = 0;
242                textureObject = 0;
243            }
244        }
245    }
246
247    // if we already have an texture object, then
248    if (textureObject)
249    {
250        // bind texture object
251        textureObject->bind();
252
253        // if texture parameters changed, then reset them
254        if (getTextureParameterDirty(state.getContextID())) applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT,state);
255
256        // if subload is specified, then use it to subload the images to GPU memory
257        if (_subloadCallback.valid())
258        {
259            _subloadCallback->subload(*this,state);
260        }
261        else
262        {
263            // for each image of the texture array do
264            for (GLsizei n=0; n < _textureDepth; n++)
265            {
266                osg::Image* image = _images[n].get();
267
268                // if image content is modified, then upload it to the GPU memory
269                if (image && getModifiedCount(n,contextID) != image->getModifiedCount())
270                {
271                    applyTexImage2DArray_subload(state, image, _textureWidth, _textureHeight, n, _internalFormat, _numMipmapLevels);
272                    getModifiedCount(n,contextID) = image->getModifiedCount();
273                }
274            }
275        }
276
277    }
278
279    // there is no texture object, but exists a subload callback, so use it to upload images
280    else if (_subloadCallback.valid())
281    {
282        // generate texture (i.e. glGenTexture) and apply parameters
283        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID, GL_TEXTURE_2D_ARRAY_EXT);
284        textureObject->bind();
285        applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT, state);
286        _subloadCallback->load(*this,state);
287    }
288
289    // nothing before, but we have valid images, so do manual upload and create texture object manually
290    // TODO: we assume _images[0] is valid, however this may not be always the case
291    //       some kind of checking for the first valid image is required (Art, may 2008)
292    else if (imagesValid())
293    {
294        // compute the internal texture format, this set the _internalFormat to an appropriate value.
295        computeInternalFormat();
296
297        // compute the dimensions of the texture.
298        computeRequiredTextureDimensions(state,*_images[0],_textureWidth, _textureHeight, _numMipmapLevels);
299
300        // create texture object
301        textureObject = generateTextureObject(
302                this, contextID,GL_TEXTURE_2D_ARRAY_EXT,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0);
303
304        // bind texture
305        textureObject->bind();
306        applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT, state);
307
308        _textureObjectBuffer[contextID] = textureObject;
309
310        // First we need to allocate the texture memory
311        int sourceFormat = _sourceFormat ? _sourceFormat : _internalFormat;
312
313        if( isCompressedInternalFormat( sourceFormat ) &&
314            sourceFormat == _internalFormat &&
315            extensions->isCompressedTexImage3DSupported() )
316        {
317            extensions->glCompressedTexImage3D( GL_TEXTURE_2D_ARRAY_EXT, 0, _internalFormat,
318                     _textureWidth, _textureHeight, _textureDepth, _borderWidth,
319                     _images[0]->getImageSizeInBytes() * _textureDepth,
320                     0);
321        }
322        else
323        {
324            // Override compressed source format with safe GL_RGBA value which not generate error
325            // We can safely do this as source format is not important when source data is NULL
326            if( isCompressedInternalFormat( sourceFormat ) )
327                sourceFormat = GL_RGBA;
328
329            extensions->glTexImage3D( GL_TEXTURE_2D_ARRAY_EXT, 0, _internalFormat,
330                     _textureWidth, _textureHeight, _textureDepth, _borderWidth,
331                     sourceFormat, _sourceType ? _sourceType : GL_UNSIGNED_BYTE,
332                     0);
333        }
334
335       // For certain we have to manually allocate memory for mipmaps if images are compressed
336       // if not allocated OpenGL will produce errors on mipmap upload.
337       // I have not tested if this is neccessary for plain texture formats but
338       // common sense suggests its required as well.
339       if( _min_filter != LINEAR && _min_filter != NEAREST && _images[0]->isMipmap() )
340           allocateMipmap( state );
341
342        // now for each layer we upload it into the memory
343        for (GLsizei n=0; n<_textureDepth; n++)
344        {
345            // if image is valid then upload it to the texture memory
346            osg::Image* image = _images[n].get();
347            if (image)
348            {
349                // now load the image data into the memory, this will also check if image do have valid properties
350                applyTexImage2DArray_subload(state, image, _textureWidth, _textureHeight, n, _internalFormat, _numMipmapLevels);
351                getModifiedCount(n,contextID) = image->getModifiedCount();
352            }
353        }
354
355        const Texture::Extensions* texExtensions = Texture::getExtensions(contextID,true);
356        // source images have no mipmamps but we could generate them...
357        if( _min_filter != LINEAR && _min_filter != NEAREST && !_images[0]->isMipmap() &&
358            _useHardwareMipMapGeneration && texExtensions->isGenerateMipMapSupported() )
359            {
360                _numMipmapLevels = Image::computeNumberOfMipmapLevels( _textureWidth, _textureHeight );
361                generateMipmap( state );
362            }
363
364        textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0);
365
366        // unref image data?
367        if (isSafeToUnrefImageData(state))
368        {
369            Texture2DArray* non_const_this = const_cast<Texture2DArray*>(this);
370            for (int n=0; n<_textureDepth; n++)
371            {
372                if (_images[n].valid() && _images[n]->getDataVariance()==STATIC)
373                {
374                    non_const_this->_images[n] = NULL;
375                }
376            }
377        }
378
379    }
380
381    // No images present, but dimensions are set. So create empty texture
382    else if ( (_textureWidth > 0) && (_textureHeight > 0) && (_textureDepth > 0) && (_internalFormat!=0) )
383    {
384        // generate texture
385        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(
386                this, contextID, GL_TEXTURE_2D_ARRAY_EXT,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0);
387
388        textureObject->bind();
389        applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT,state);
390
391        extensions->glTexImage3D( GL_TEXTURE_2D_ARRAY_EXT, 0, _internalFormat,
392                     _textureWidth, _textureHeight, _textureDepth,
393                     _borderWidth,
394                     _sourceFormat ? _sourceFormat : _internalFormat,
395                     _sourceType ? _sourceType : GL_UNSIGNED_BYTE,
396                     0);
397
398    }
399
400    // nothing before, so just unbind the texture target
401    else
402    {
403        glBindTexture( GL_TEXTURE_2D_ARRAY_EXT, 0 );
404    }
405
406    // if texture object is now valid and we have to allocate mipmap levels, then
407    if (textureObject != 0 && _texMipmapGenerationDirtyList[contextID])
408    {
409        generateMipmap(state);
410    }
411}
412
413
414void Texture2DArray::applyTexImage2DArray_subload(State& state, Image* image, GLsizei inwidth, GLsizei inheight, GLsizei indepth, GLint inInternalFormat, GLsizei& numMipmapLevels) const
415{
416    // if we don't have a valid image we can't create a texture!
417    if (!imagesValid())
418        return;
419
420    // get the contextID (user defined ID of 0 upwards) for the
421    // current OpenGL context.
422    const unsigned int contextID = state.getContextID();
423    const Extensions* extensions = getExtensions(contextID,true);
424    const Texture::Extensions* texExtensions = Texture::getExtensions(contextID,true);
425    GLenum target = GL_TEXTURE_2D_ARRAY_EXT;
426
427    // compute the internal texture format, this set the _internalFormat to an appropriate value.
428    computeInternalFormat();
429
430    // select the internalFormat required for the texture.
431    // bool compressed = isCompressedInternalFormat(_internalFormat);
432    bool compressed_image = isCompressedInternalFormat((GLenum)image->getPixelFormat());
433
434    // if the required layer is exceeds the maximum allowed layer sizes
435    if (indepth > extensions->maxLayerCount())
436    {
437        // we give a warning and do nothing
438        OSG_WARN<<"Warning: Texture2DArray::applyTexImage2DArray_subload(..) the given layer number exceeds the maximum number of supported layers."<<std::endl;
439        return;
440    }
441
442    //Rescale if resize hint is set or NPOT not supported or dimensions exceed max size
443    if( _resizeNonPowerOfTwoHint || !texExtensions->isNonPowerOfTwoTextureSupported(_min_filter)
444        || inwidth > extensions->max2DSize()
445        || inheight > extensions->max2DSize())
446        image->ensureValidSizeForTexturing(extensions->max2DSize());
447
448    // image size or format has changed, this is not allowed, hence return
449    if (image->s()!=inwidth ||
450        image->t()!=inheight ||
451        image->getInternalTextureFormat()!=inInternalFormat )
452    {
453        OSG_WARN<<"Warning: Texture2DArray::applyTexImage2DArray_subload(..) given image do have wrong dimension or internal format."<<std::endl;
454        return;
455    }
456
457    glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking());
458#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
459    glPixelStorei(GL_UNPACK_ROW_LENGTH,image->getRowLength());
460#endif
461
462    bool useHardwareMipmapGeneration =
463        !image->isMipmap() && _useHardwareMipMapGeneration && texExtensions->isGenerateMipMapSupported();
464
465    // if no special mipmapping is required, then
466    if( _min_filter == LINEAR || _min_filter == NEAREST || useHardwareMipmapGeneration )
467    {
468        if( _min_filter == LINEAR || _min_filter == NEAREST )
469            numMipmapLevels = 1;
470        else //Hardware Mipmap Generation
471            numMipmapLevels = image->getNumMipmapLevels();
472
473        // upload non-compressed image
474        if ( !compressed_image )
475        {
476            extensions->glTexSubImage3D( target, 0,
477                                      0, 0, indepth,
478                                      inwidth, inheight, 1,
479                                      (GLenum)image->getPixelFormat(),
480                                      (GLenum)image->getDataType(),
481                                      image->data() );
482        }
483
484        // if we support compression and image is compressed, then
485        else if (extensions->isCompressedTexImage3DSupported())
486        {
487            // OSG_WARN<<"glCompressedTexImage3D "<<inwidth<<", "<<inheight<<", "<<indepth<<std::endl;
488
489            GLint blockSize, size;
490            getCompressedSize(_internalFormat, inwidth, inheight, 1, blockSize,size);
491
492            extensions->glCompressedTexSubImage3D(target, 0,
493                0, 0, indepth,
494                inwidth, inheight, 1,
495                (GLenum)image->getPixelFormat(),
496                size,
497                image->data());
498        }
499
500    // we want to use mipmapping, so enable it
501    }else
502    {
503        // image does not provide mipmaps, so we have to create them
504        if( !image->isMipmap() )
505        {
506            numMipmapLevels = 1;
507            OSG_WARN<<"Warning: Texture2DArray::applyTexImage2DArray_subload(..) mipmap layer not passed, and auto mipmap generation turned off or not available. Check texture's min/mag filters & hardware mipmap generation."<<std::endl;
508
509            // the image object does provide mipmaps, so upload the in the certain levels of a layer
510        }else
511        {
512            numMipmapLevels = image->getNumMipmapLevels();
513
514            int width  = image->s();
515            int height = image->t();
516
517            if( !compressed_image )
518            {
519
520                for( GLsizei k = 0 ; k < numMipmapLevels  && (width || height ) ;k++)
521                {
522                    if (width == 0)
523                       width = 1;
524                    if (height == 0)
525                       height = 1;
526
527                    extensions->glTexSubImage3D( target, k, 0, 0, indepth,
528                                              width, height, 1,
529                                              (GLenum)image->getPixelFormat(),
530                                              (GLenum)image->getDataType(),
531                                               image->getMipmapData(k));
532
533                    width >>= 1;
534                    height >>= 1;
535                }
536            }
537            else if (extensions->isCompressedTexImage3DSupported())
538            {
539                GLint blockSize,size;
540                for( GLsizei k = 0 ; k < numMipmapLevels  && (width || height) ;k++)
541                {
542                    if (width == 0)
543                        width = 1;
544                    if (height == 0)
545                        height = 1;
546
547                    getCompressedSize(image->getInternalTextureFormat(), width, height, 1, blockSize,size);
548
549//                    state.checkGLErrors("before extensions->glCompressedTexSubImage3D(");
550
551                    extensions->glCompressedTexSubImage3D(target, k, 0, 0, indepth,
552                                                       width, height, 1,
553                                                       (GLenum)image->getPixelFormat(),
554                                                       size,
555                                                       image->getMipmapData(k));
556
557//                    state.checkGLErrors("after extensions->glCompressedTexSubImage3D(");
558
559                    width >>= 1;
560                    height >>= 1;
561                }
562            }
563        }
564
565    }
566}
567
568
569void Texture2DArray::copyTexSubImage2DArray(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height )
570{
571    const unsigned int contextID = state.getContextID();
572    const Extensions* extensions = getExtensions(contextID,true);
573
574    // get the texture object for the current contextID.
575    TextureObject* textureObject = getTextureObject(contextID);
576
577    // if texture object is valid
578    if (textureObject != 0)
579    {
580        textureObject->bind();
581
582        applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT,state);
583        extensions->glCopyTexSubImage3D( GL_TEXTURE_2D_ARRAY_EXT, 0, xoffset,yoffset,zoffset, x, y, width, height);
584
585        // inform state that this texture is the current one bound.
586        state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
587
588    }
589    else
590    {
591        OSG_WARN<<"Warning: Texture2DArray::copyTexSubImage2DArray(..) failed, cannot not copy to a non existant texture."<<std::endl;
592    }
593}
594
595void Texture2DArray::allocateMipmap(State& state) const
596{
597    const unsigned int contextID = state.getContextID();
598
599    // get the texture object for the current contextID.
600    TextureObject* textureObject = getTextureObject(contextID);
601
602    if (textureObject && _textureWidth != 0 && _textureHeight != 0 && _textureDepth != 0)
603    {
604        const Extensions* extensions = getExtensions(contextID,true);
605
606        int safeSourceFormat = _sourceFormat ? _sourceFormat : _internalFormat;
607
608        // Make sure source format does not contain compressed formats value (like DXT3)
609        // they are invalid when passed to glTexImage3D source format parameter
610        if( isCompressedInternalFormat( safeSourceFormat ) )
611        {
612            if( safeSourceFormat != _internalFormat || !extensions->isCompressedTexImage3DSupported() )
613                safeSourceFormat = GL_RGBA;
614        }
615
616        // bind texture
617        textureObject->bind();
618
619        // compute number of mipmap levels
620        int width = _textureWidth;
621        int height = _textureHeight;
622        int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height);
623
624        // we do not reallocate the level 0, since it was already allocated
625        width >>= 1;
626        height >>= 1;
627
628        for( GLsizei k = 1; k < numMipmapLevels  && (width || height); k++)
629        {
630            if (width == 0)
631                width = 1;
632            if (height == 0)
633                height = 1;
634
635            if( isCompressedInternalFormat(safeSourceFormat) )
636            {
637                int size = 0, blockSize = 0;
638
639                getCompressedSize( _internalFormat, width, height, _textureDepth, blockSize, size);
640
641                extensions->glCompressedTexImage3D( GL_TEXTURE_2D_ARRAY_EXT, k, _internalFormat,
642                                                    width, height, _textureDepth, _borderWidth,
643                                                    size,
644                                                    NULL);
645            }
646            else
647            {
648                extensions->glTexImage3D( GL_TEXTURE_2D_ARRAY_EXT, k, _internalFormat,
649                     width, height, _textureDepth, _borderWidth,
650                     safeSourceFormat, _sourceType ? _sourceType : GL_UNSIGNED_BYTE,
651                     NULL);
652            }
653
654            width >>= 1;
655            height >>= 1;
656        }
657
658        // inform state that this texture is the current one bound.
659        state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
660    }
661}
662
663typedef buffered_value< ref_ptr<Texture2DArray::Extensions> > BufferedExtensions;
664static BufferedExtensions s_extensions;
665
666Texture2DArray::Extensions* Texture2DArray::getExtensions(unsigned int contextID,bool createIfNotInitalized)
667{
668    if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions(contextID);
669    return s_extensions[contextID].get();
670}
671
672void Texture2DArray::setExtensions(unsigned int contextID,Extensions* extensions)
673{
674    s_extensions[contextID] = extensions;
675}
676
677Texture2DArray::Extensions::Extensions(unsigned int contextID)
678{
679    setupGLExtensions(contextID);
680}
681
682Texture2DArray::Extensions::Extensions(const Extensions& rhs):
683    Referenced()
684{
685    _isTexture3DSupported = rhs._isTexture3DSupported;
686    _isTexture2DArraySupported = rhs._isTexture2DArraySupported;
687
688    _max2DSize = rhs._max2DSize;
689    _maxLayerCount = rhs._maxLayerCount;
690
691    _glTexImage3D = rhs._glTexImage3D;
692    _glTexSubImage3D = rhs._glTexSubImage3D;
693    _glCopyTexSubImage3D = rhs._glCopyTexSubImage3D;
694    _glCompressedTexImage3D = rhs._glCompressedTexImage3D;
695    _glCompressedTexSubImage3D = rhs._glCompressedTexSubImage3D;;
696}
697
698void Texture2DArray::Extensions::lowestCommonDenominator(const Extensions& rhs)
699{
700    if (!rhs._isTexture3DSupported)                 _isTexture3DSupported = false;
701    if (!rhs._isTexture2DArraySupported)            _isTexture2DArraySupported = false;
702    if (rhs._max2DSize<_max2DSize)                  _max2DSize = rhs._max2DSize;
703    if (rhs._maxLayerCount<_maxLayerCount)          _maxLayerCount = rhs._maxLayerCount;
704
705    if (!rhs._glTexImage3D)                         _glTexImage3D = 0;
706    if (!rhs._glTexSubImage3D)                      _glTexSubImage3D = 0;
707    if (!rhs._glCompressedTexImage3D)               _glTexImage3D = 0;
708    if (!rhs._glCompressedTexSubImage3D)            _glTexSubImage3D = 0;
709    if (!rhs._glCopyTexSubImage3D)                  _glCopyTexSubImage3D = 0;
710}
711
712void Texture2DArray::Extensions::setupGLExtensions(unsigned int contextID)
713{
714    _isTexture3DSupported = OSG_GL3_FEATURES ||
715                            isGLExtensionSupported(contextID,"GL_EXT_texture3D") ||
716                            strncmp((const char*)glGetString(GL_VERSION),"1.2",3)>=0;
717
718    _isTexture2DArraySupported = OSG_GL3_FEATURES || isGLExtensionSupported(contextID,"GL_EXT_texture_array");
719
720    _max2DSize = 0;
721    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_max2DSize);
722    _maxLayerCount = 0;
723    glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS_EXT, &_maxLayerCount);
724
725    setGLExtensionFuncPtr(_glTexImage3D, "glTexImage3D","glTexImage3DEXT");
726    setGLExtensionFuncPtr(_glTexSubImage3D, "glTexSubImage3D","glTexSubImage3DEXT");
727    setGLExtensionFuncPtr(_glCompressedTexImage3D, "glCompressedTexImage3D","glCompressedTexImage3DARB");
728    setGLExtensionFuncPtr(_glCompressedTexSubImage3D, "glCompressedTexSubImage3D","glCompressedTexSubImage3DARB");
729    setGLExtensionFuncPtr(_glCopyTexSubImage3D, "glCopyTexSubImage3D","glCopyTexSubImage3DEXT");
730}
731
732void Texture2DArray::Extensions::glTexImage3D( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels) const
733{
734    if (_glTexImage3D)
735    {
736        _glTexImage3D( target, level, internalFormat, width, height, depth, border, format, type, pixels);
737    }
738    else
739    {
740        OSG_WARN<<"Error: glTexImage3D not supported by OpenGL driver"<<std::endl;
741    }
742}
743
744void Texture2DArray::Extensions::glTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels) const
745{
746    if (_glTexSubImage3D)
747    {
748        _glTexSubImage3D( target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels);
749    }
750    else
751    {
752        OSG_WARN<<"Error: glTexSubImage3D not supported by OpenGL driver"<<std::endl;
753    }
754}
755
756void Texture2DArray::Extensions::glCompressedTexImage3D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid *data) const
757{
758    if (_glCompressedTexImage3D)
759    {
760        _glCompressedTexImage3D(target, level, internalformat, width, height, depth, border, imageSize, data);
761    }
762    else
763    {
764        OSG_WARN<<"Error: glCompressedTexImage3D not supported by OpenGL driver"<<std::endl;
765    }
766}
767
768void Texture2DArray::Extensions::glCompressedTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid *data ) const
769{
770    if (_glCompressedTexSubImage3D)
771    {
772        _glCompressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, data);
773    }
774    else
775    {
776        OSG_WARN<<"Error: glCompressedTexImage2D not supported by OpenGL driver"<<std::endl;
777    }
778}
779
780void Texture2DArray::Extensions::glCopyTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ) const
781{
782    if (_glCopyTexSubImage3D)
783    {
784        _glCopyTexSubImage3D(target, level, xoffset, yoffset, zoffset, x, y, width, height);
785    }
786    else
787    {
788        OSG_WARN<<"Error: glCopyTexSubImage3D not supported by OpenGL driver"<<std::endl;
789    }
790}
Note: See TracBrowser for help on using the browser.