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

Revision 10924, 19.3 kB (checked in by robert, 4 years ago)

Refactored the way that osg::Image/ImageSequence manages the update callback that needs to be attached to Textures to make it possible to use the Image::update() mechansim in other subclasses from osg::Image.
To enable the automatic attachment of the required update callback to call osg::Image::update(..) subclasses from osg::Image will
need to implement the osg::Image::requestUpdateCall() and return true, and implement the osg::Image::update(NodeVisitor?*) method to recieve the update call during the update traversal.

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