root/OpenSceneGraph/trunk/src/osg/TextureRectangle.cpp @ 10867

Revision 10867, 19.3 kB (checked in by robert, 5 years ago)

Added check for image compatibility with existing texture object, releasing the existing texture object in cases when the new image size/pixel format is not compatible.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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
14#include <osg/GLExtensions>
15#include <osg/TextureRectangle>
16#include <osg/ImageSequence>
17#include <osg/State>
18#include <osg/GLU>
19#include <osg/Notify>
20
21#include <osg/Timer>
22
23#ifndef GL_UNPACK_CLIENT_STORAGE_APPLE
24#define GL_UNPACK_CLIENT_STORAGE_APPLE    0x85B2
25#endif
26
27#ifndef GL_APPLE_vertex_array_range
28#define GL_VERTEX_ARRAY_RANGE_APPLE       0x851D
29#define GL_VERTEX_ARRAY_RANGE_LENGTH_APPLE 0x851E
30#define GL_VERTEX_ARRAY_STORAGE_HINT_APPLE 0x851F
31#define GL_VERTEX_ARRAY_RANGE_POINTER_APPLE 0x8521
32#define GL_STORAGE_CACHED_APPLE           0x85BE
33#define GL_STORAGE_SHARED_APPLE           0x85BF
34#endif
35
36// #define DO_TIMING
37
38using namespace osg;
39
40TextureRectangle::TextureRectangle():
41    _textureWidth(0),
42    _textureHeight(0)
43{
44    setWrap(WRAP_S, CLAMP);
45    setWrap(WRAP_T, CLAMP);
46
47    setFilter(MIN_FILTER, LINEAR);
48    setFilter(MAG_FILTER, LINEAR);
49}
50
51TextureRectangle::TextureRectangle(Image* image):
52            _textureWidth(0),
53            _textureHeight(0)
54{
55    setWrap(WRAP_S, CLAMP);
56    setWrap(WRAP_T, CLAMP);
57
58    setFilter(MIN_FILTER, LINEAR);
59    setFilter(MAG_FILTER, LINEAR);
60   
61    setImage(image);
62}
63
64TextureRectangle::TextureRectangle(const TextureRectangle& text,const CopyOp& copyop):
65    Texture(text,copyop),
66    _image(copyop(text._image.get())),
67    _textureWidth(text._textureWidth),
68    _textureHeight(text._textureHeight),
69    _subloadCallback(text._subloadCallback)
70{
71}
72
73TextureRectangle::~TextureRectangle()
74{
75}
76
77int TextureRectangle::compare(const StateAttribute& sa) const
78{
79    // check the types are equal and then create the rhs variable
80    // used by the COMPARE_StateAttribute_Parameter macro's below.
81    COMPARE_StateAttribute_Types(TextureRectangle,sa)
82
83    if (_image!=rhs._image) // smart pointer comparison.
84    {
85        if (_image.valid())
86        {
87            if (rhs._image.valid())
88            {
89                int result = _image->compare(*rhs._image);
90                if (result!=0) return result;
91            }
92            else
93            {
94                return 1; // valid lhs._image is greater than null.
95            }
96        }
97        else if (rhs._image.valid())
98        {
99            return -1; // valid rhs._image is greater than null.
100        }
101    }
102
103    if (!_image && !rhs._image)
104    {
105        // no image attached to either Texture2D
106        // but could these textures already be downloaded?
107        // check the _textureObjectBuffer to see if they have been
108        // downloaded
109
110        int result = compareTextureObjects(rhs);
111        if (result!=0) return result;
112    }
113
114    int result = compareTexture(rhs);
115    if (result!=0) return result;
116
117    // compare each parameter in turn against the rhs.
118    COMPARE_StateAttribute_Parameter(_textureWidth)
119    COMPARE_StateAttribute_Parameter(_textureHeight)
120    COMPARE_StateAttribute_Parameter(_subloadCallback)
121
122    return 0; // passed all the above comparison macro's, must be equal.
123}
124
125void TextureRectangle::setImage(Image* image)
126{
127    if (_image == image) return;
128
129    if (dynamic_cast<osg::ImageSequence*>(_image.get()))
130    {
131        setUpdateCallback(0);
132        setDataVariance(osg::Object::STATIC);
133    }
134
135    // delete old texture objects.
136    dirtyTextureObject();
137
138    _image = image;
139   
140    if (dynamic_cast<osg::ImageSequence*>(_image.get()))
141    {
142        setUpdateCallback(new ImageSequence::UpdateCallback());
143        setDataVariance(osg::Object::DYNAMIC);
144    }
145}
146
147
148void TextureRectangle::apply(State& state) const
149{
150    static bool s_rectangleSupported =
151            OSG_GL3_FEATURES ||
152            isGLExtensionSupported(state.getContextID(),"GL_ARB_texture_rectangle") ||
153            isGLExtensionSupported(state.getContextID(),"GL_EXT_texture_rectangle") ||
154            isGLExtensionSupported(state.getContextID(),"GL_NV_texture_rectangle");
155
156    if (!s_rectangleSupported)
157    {
158        notify(WARN)<<"Warning: TextureRectangle::apply(..) failed, texture rectangle is not support by your OpenGL drivers."<<std::endl;
159        return;
160    }
161
162    // get the contextID (user defined ID of 0 upwards) for the
163    // current OpenGL context.
164    const unsigned int contextID = state.getContextID();
165
166    Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get();
167    ElapsedTime elapsedTime(&(tom->getApplyTime()));
168    tom->getNumberApplied()++;
169
170
171    // get the texture object for the current contextID.
172    TextureObject* textureObject = getTextureObject(contextID);
173
174    if (textureObject)
175    {
176        if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount())
177        {
178            // compute the internal texture format, this set the _internalFormat to an appropriate value.
179            computeInternalFormat();
180
181            GLsizei new_width, new_height, new_numMipmapLevels;
182
183            // compute the dimensions of the texture.
184            computeRequiredTextureDimensions(state, *_image, new_width, new_height, new_numMipmapLevels);
185
186            if (!textureObject->match(GL_TEXTURE_RECTANGLE, new_numMipmapLevels, _internalFormat, new_width, new_height, 1, _borderWidth))
187            {
188                Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get());
189                _textureObjectBuffer[contextID] = 0;
190                textureObject = 0;
191            }
192        }
193    }
194
195    if (textureObject)
196    {
197        textureObject->bind();
198
199        if (getTextureParameterDirty(state.getContextID()))
200            applyTexParameters(GL_TEXTURE_RECTANGLE, state);
201
202        if (_subloadCallback.valid())
203        {
204            _subloadCallback->subload(*this, state);
205        }
206        else if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount())
207        {
208            applyTexImage_subload(GL_TEXTURE_RECTANGLE, _image.get(), state, _textureWidth, _textureHeight, _internalFormat);
209 
210            // update the modified count to show that it is upto date.
211            getModifiedCount(contextID) = _image->getModifiedCount();
212        }
213    }
214    else if (_subloadCallback.valid())
215    {
216        // we don't have a applyTexImage1D_subload yet so can't reuse.. so just generate a new texture object.       
217        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_RECTANGLE);
218
219        textureObject->bind();
220
221        applyTexParameters(GL_TEXTURE_RECTANGLE, state);
222
223        _subloadCallback->load(*this, state);
224
225        textureObject->setAllocated(1,_internalFormat,_textureWidth,_textureHeight,1,0);
226
227        // in theory the following line is redundant, but in practice
228        // have found that the first frame drawn doesn't apply the textures
229        // unless a second bind is called?!!
230        // perhaps it is the first glBind which is not required...
231        //glBindTexture(GL_TEXTURE_RECTANGLE, handle);
232    }
233    else if (_image.valid() && _image->data())
234    {
235
236
237        // keep the image around at least till we go out of scope.
238        osg::ref_ptr<osg::Image> image = _image;
239
240        // compute the internal texture format, this set the _internalFormat to an appropriate value.
241        computeInternalFormat();
242
243        _textureWidth = image->s();
244        _textureHeight = image->t();
245
246        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(
247                this, contextID,GL_TEXTURE_RECTANGLE,1,_internalFormat,_textureWidth,_textureHeight,1,0);
248
249        textureObject->bind();
250
251        applyTexParameters(GL_TEXTURE_RECTANGLE, state);
252
253        if (textureObject->isAllocated())
254        {
255            applyTexImage_subload(GL_TEXTURE_RECTANGLE, _image.get(), state, _textureWidth, _textureHeight, _internalFormat);
256        }
257        else
258        {
259            applyTexImage_load(GL_TEXTURE_RECTANGLE, _image.get(), state, _textureWidth, _textureHeight);
260            textureObject->setAllocated(true);
261        }
262
263        if (state.getMaxTexturePoolSize()==0 && _unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC)
264        {
265            TextureRectangle* non_const_this = const_cast<TextureRectangle*>(this);
266            non_const_this->_image = 0;
267        }
268
269    }
270    else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_internalFormat!=0) )
271    {
272        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(
273                this, contextID,GL_TEXTURE_RECTANGLE,0,_internalFormat,_textureWidth,_textureHeight,1,0);
274       
275        textureObject->bind();
276
277        applyTexParameters(GL_TEXTURE_RECTANGLE,state);
278
279        // no image present, but dimensions at set so lets create the texture
280        glTexImage2D( GL_TEXTURE_RECTANGLE, 0, _internalFormat,
281                     _textureWidth, _textureHeight, _borderWidth,
282                     _sourceFormat ? _sourceFormat : _internalFormat,
283                     _sourceType ? _sourceType : GL_UNSIGNED_BYTE,
284                     0);               
285                     
286        if (_readPBuffer.valid())
287        {
288            _readPBuffer->bindPBufferToTexture(GL_FRONT);
289        }
290       
291    }
292    else
293    {
294        glBindTexture(GL_TEXTURE_RECTANGLE, 0);
295    }
296}
297
298void TextureRectangle::applyTexImage_load(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight) const
299{
300    // if we don't have a valid image we can't create a texture!
301    if (!image || !image->data())
302        return;
303
304    // get the contextID (user defined ID of 0 upwards) for the
305    // current OpenGL context.
306    const unsigned int contextID = state.getContextID();
307    const Extensions* extensions = getExtensions(contextID,true);
308
309    // update the modified count to show that it is upto date.
310    getModifiedCount(contextID) = image->getModifiedCount();
311
312    // compute the internal texture format, sets _internalFormat.
313    computeInternalFormat();
314
315    glPixelStorei(GL_UNPACK_ALIGNMENT, image->getPacking());
316
317    bool useClientStorage = extensions->isClientStorageSupported() && getClientStorageHint();
318    if (useClientStorage)
319    {
320        glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE,GL_TRUE);
321
322        #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE)
323            glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_PRIORITY,0.0f);
324        #endif
325
326        #ifdef GL_TEXTURE_STORAGE_HINT_APPLE
327            glTexParameteri(target, GL_TEXTURE_STORAGE_HINT_APPLE , GL_STORAGE_CACHED_APPLE);
328        #endif
329    }
330
331    const unsigned char* dataPtr = image->data();
332    GLBufferObject* pbo = image->getOrCreateGLBufferObject(contextID);
333    if (pbo)
334    {
335        state.bindPixelBufferObject(pbo);
336        dataPtr = reinterpret_cast<unsigned char*>(pbo->getOffset(image->getBufferIndex()));
337    }
338
339    if(isCompressedInternalFormat(_internalFormat) && extensions->isCompressedTexImage2DSupported())
340    {
341        extensions->glCompressedTexImage2D(target, 0, _internalFormat,
342          image->s(), image->t(), 0,
343          image->getImageSizeInBytes(),
344          dataPtr);
345    }
346    else
347    {
348        glTexImage2D(target, 0, _internalFormat,
349          image->s(), image->t(), 0,
350          (GLenum)image->getPixelFormat(),
351          (GLenum)image->getDataType(),
352          dataPtr );
353    }
354   
355
356    if (pbo)
357    {
358        state.unbindPixelBufferObject();
359    }
360
361    inwidth = image->s();
362    inheight = image->t();
363
364    if (useClientStorage)
365    {
366        glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE,GL_FALSE);
367    }
368}
369
370void TextureRectangle::applyTexImage_subload(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLint& inInternalFormat) const
371{
372    // if we don't have a valid image we can't create a texture!
373    if (!image || !image->data())
374        return;
375
376    if (image->s()!=inwidth || image->t()!=inheight || image->getInternalTextureFormat()!=inInternalFormat)
377    {
378        applyTexImage_load(target, image, state, inwidth, inheight);
379        return;
380    }
381
382
383    // get the contextID (user defined ID of 0 upwards) for the
384    // current OpenGL context.
385    const unsigned int contextID = state.getContextID();
386    const Extensions* extensions = getExtensions(contextID,true);
387
388
389    // update the modified count to show that it is upto date.
390    getModifiedCount(contextID) = image->getModifiedCount();
391
392    // compute the internal texture format, sets _internalFormat.
393    computeInternalFormat();
394
395    glPixelStorei(GL_UNPACK_ALIGNMENT, image->getPacking());
396
397#ifdef DO_TIMING
398    osg::Timer_t start_tick = osg::Timer::instance()->tick();
399    osg::notify(osg::NOTICE)<<"TextureRectangle::apply pixelFormat = "<<std::hex<<image->getPixelFormat()<<std::dec<<std::endl;
400#endif
401    const unsigned char* dataPtr = image->data();
402    GLBufferObject* pbo = image->getOrCreateGLBufferObject(contextID);
403    if (pbo)
404    {
405        state.bindPixelBufferObject(pbo);
406        dataPtr = reinterpret_cast<unsigned char*>(pbo->getOffset(image->getBufferIndex()));
407#ifdef DO_TIMING
408        osg::notify(osg::NOTICE)<<"after PBO "<<osg::Timer::instance()->delta_m(start_tick,osg::Timer::instance()->tick())<<"ms"<<std::endl;
409#endif
410    }
411    else
412    {
413        osg::notify(osg::NOTICE)<<"    no PixelBufferObject "<<image->getBufferObject()<<", "<<image->getPixelBufferObject()<<" pbo="<<pbo<<std::endl;
414    }
415   
416
417    if(isCompressedInternalFormat(_internalFormat) && extensions->isCompressedTexSubImage2DSupported())
418    {
419        extensions->glCompressedTexSubImage2D(target, 0,
420          0,0,
421          image->s(), image->t(),
422          (GLenum)image->getPixelFormat(),
423          (GLenum)image->getDataType(),
424          dataPtr);
425    }
426    else
427    {
428        glTexSubImage2D(target, 0,
429          0,0,
430          image->s(), image->t(),
431          (GLenum)image->getPixelFormat(),
432          (GLenum)image->getDataType(),
433          dataPtr);
434    }
435
436    if (pbo)
437    {
438        state.unbindPixelBufferObject();
439    }
440
441#ifdef DO_TIMING
442    osg::notify(osg::NOTICE)<<"glTexSubImage2D "<<osg::Timer::instance()->delta_m(start_tick,osg::Timer::instance()->tick())<<"ms"<<std::endl;
443#endif
444}
445
446void TextureRectangle::computeInternalFormat() const
447{
448    if (_image.valid()) computeInternalFormatWithImage(*_image);
449    else computeInternalFormatType();
450}
451
452void TextureRectangle::copyTexImage2D(State& state, int x, int y, int width, int height )
453{
454    const unsigned int contextID = state.getContextID();
455   
456    if (_internalFormat==0) _internalFormat=GL_RGBA;
457
458    // get the globj for the current contextID.
459    TextureObject* textureObject = getTextureObject(contextID);
460   
461    if (textureObject)
462    {
463        if (width==(int)_textureWidth && height==(int)_textureHeight)
464        {
465            // we have a valid texture object which is the right size
466            // so lets play clever and use copyTexSubImage2D instead.
467            // this allows use to reuse the texture object and avoid
468            // expensive memory allocations.
469            copyTexSubImage2D(state,0 ,0, x, y, width, height);
470            return;
471        }
472        // the relevent texture object is not of the right size so
473        // needs to been deleted   
474        // remove previously bound textures.
475        dirtyTextureObject();
476        // note, dirtyTextureObject() dirties all the texture objects for
477        // this texture, is this right?  Perhaps we should dirty just the
478        // one for this context.  Note sure yet will leave till later.
479        // RO July 2001.
480    }
481   
482   
483    // remove any previously assigned images as these are nolonger valid.
484    _image = NULL;
485
486    // switch off mip-mapping.
487    //
488    _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_RECTANGLE);
489
490    textureObject->bind();
491   
492    applyTexParameters(GL_TEXTURE_RECTANGLE,state);
493
494
495/*    bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST);
496    bool hardwareMipMapOn = false;
497    if (needHardwareMipMap)
498    {
499        const Extensions* extensions = getExtensions(contextID,true);
500        bool generateMipMapSupported = extensions->isGenerateMipMapSupported();
501
502        hardwareMipMapOn = _useHardwareMipMapGeneration && generateMipMapSupported;
503       
504        if (!hardwareMipMapOn)
505        {
506            // have to swtich off mip mapping
507            notify(NOTICE)<<"Warning: Texture2D::copyTexImage2D(,,,,) switch of mip mapping as hardware support not available."<<std::endl;
508            _min_filter = LINEAR;
509        }
510    }
511*/   
512//    if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
513
514    glCopyTexImage2D( GL_TEXTURE_RECTANGLE, 0, _internalFormat, x, y, width, height, 0 );
515
516//    if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE);
517
518
519    _textureWidth = width;
520    _textureHeight = height;
521//    _numMipmapLevels = 1;
522   
523    textureObject->setAllocated(1,_internalFormat,_textureWidth,_textureHeight,1,0);
524
525
526    // inform state that this texture is the current one bound.
527    state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
528}
529
530void TextureRectangle::copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height )
531{
532    const unsigned int contextID = state.getContextID();
533
534    if (_internalFormat==0) _internalFormat=GL_RGBA;
535
536    // get the texture object for the current contextID.
537    TextureObject* textureObject = getTextureObject(contextID);
538   
539    if (textureObject)
540    {
541        // we have a valid image
542        textureObject->bind();
543       
544        applyTexParameters(GL_TEXTURE_RECTANGLE,state);
545
546/*        bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST);
547        bool hardwareMipMapOn = false;
548        if (needHardwareMipMap)
549        {
550            const Extensions* extensions = getExtensions(contextID,true);
551            bool generateMipMapSupported = extensions->isGenerateMipMapSupported();
552
553            hardwareMipMapOn = _useHardwareMipMapGeneration && generateMipMapSupported;
554
555            if (!hardwareMipMapOn)
556            {
557                // have to swtich off mip mapping
558                notify(NOTICE)<<"Warning: Texture2D::copyTexImage2D(,,,,) switch of mip mapping as hardware support not available."<<std::endl;
559                _min_filter = LINEAR;
560            }
561        }
562*/
563//        if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
564
565        glCopyTexSubImage2D( GL_TEXTURE_RECTANGLE, 0, xoffset, yoffset, x, y, width, height);
566
567//        if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE);
568
569        // inform state that this texture is the current one bound.
570        state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
571
572    }
573    else
574    {
575        // no texture object already exsits for this context so need to
576        // create it upfront - simply call copyTexImage2D.
577        copyTexImage2D(state,x,y,width,height);
578    }
579}
580
581void TextureRectangle::allocateMipmap(State&) const
582{
583    osg::notify(osg::NOTICE)<<"Warning: TextureRectangle::allocateMipmap(State&) called eroneously, GL_TEXTURE_RECTANGLE does not support mipmapping."<<std::endl;
584}
Note: See TracBrowser for help on using the browser.