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

Revision 10596, 18.2 kB (checked in by robert, 5 years ago)

Added status collection to full range of texture classes

  • 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 = isGLExtensionSupported(state.getContextID(),"GL_ARB_texture_rectangle")
151            || isGLExtensionSupported(state.getContextID(),"GL_EXT_texture_rectangle")
152            || isGLExtensionSupported(state.getContextID(),"GL_NV_texture_rectangle");
153
154    if (!s_rectangleSupported)
155    {
156        notify(WARN)<<"Warning: TextureRectangle::apply(..) failed, texture rectangle is not support by your OpenGL drivers."<<std::endl;
157        return;
158    }
159
160    // get the contextID (user defined ID of 0 upwards) for the
161    // current OpenGL context.
162    const unsigned int contextID = state.getContextID();
163
164    Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID);
165    ElapsedTime elapsedTime(&(tom->getApplyTime()));
166    tom->getNumberApplied()++;
167
168
169    // get the texture object for the current contextID.
170    TextureObject* textureObject = getTextureObject(contextID);
171   
172   
173    if (textureObject != 0)
174    {
175
176        textureObject->bind();
177        if (getTextureParameterDirty(state.getContextID()))
178            applyTexParameters(GL_TEXTURE_RECTANGLE, state);
179
180        if (_subloadCallback.valid())
181        {
182            _subloadCallback->subload(*this, state);
183        }
184        else if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount())
185        {
186            applyTexImage_subload(GL_TEXTURE_RECTANGLE, _image.get(), state, _textureWidth, _textureHeight, _internalFormat);
187 
188            // update the modified count to show that it is upto date.
189            getModifiedCount(contextID) = _image->getModifiedCount();
190        }
191    }
192    else if (_subloadCallback.valid())
193    {
194        // we don't have a applyTexImage1D_subload yet so can't reuse.. so just generate a new texture object.       
195        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_RECTANGLE);
196
197        textureObject->bind();
198
199        applyTexParameters(GL_TEXTURE_RECTANGLE, state);
200
201        _subloadCallback->load(*this, state);
202
203        textureObject->setAllocated(1,_internalFormat,_textureWidth,_textureHeight,1,0);
204
205        // in theory the following line is redundant, but in practice
206        // have found that the first frame drawn doesn't apply the textures
207        // unless a second bind is called?!!
208        // perhaps it is the first glBind which is not required...
209        //glBindTexture(GL_TEXTURE_RECTANGLE, handle);
210    }
211    else if (_image.valid() && _image->data())
212    {
213
214        // we don't have a applyTexImage1D_subload yet so can't reuse.. so just generate a new texture object.
215        textureObject = generateTextureObject(this, contextID,GL_TEXTURE_RECTANGLE);
216       
217        textureObject->bind();
218
219        applyTexParameters(GL_TEXTURE_RECTANGLE, state);
220     
221        applyTexImage_load(GL_TEXTURE_RECTANGLE, _image.get(), state, _textureWidth, _textureHeight);
222
223        textureObject->setAllocated(1,_internalFormat,_textureWidth,_textureHeight,1,0);
224       
225        _textureObjectBuffer[contextID] = textureObject;
226
227        if (_unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC)
228        {
229            TextureRectangle* non_const_this = const_cast<TextureRectangle*>(this);
230            non_const_this->_image = 0;
231        }
232
233
234    }
235    else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_internalFormat!=0) )
236    {
237        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(
238                this, contextID,GL_TEXTURE_RECTANGLE,0,_internalFormat,_textureWidth,_textureHeight,1,0);
239       
240        textureObject->bind();
241
242        applyTexParameters(GL_TEXTURE_RECTANGLE,state);
243
244        // no image present, but dimensions at set so lets create the texture
245        glTexImage2D( GL_TEXTURE_RECTANGLE, 0, _internalFormat,
246                     _textureWidth, _textureHeight, _borderWidth,
247                     _sourceFormat ? _sourceFormat : _internalFormat,
248                     _sourceType ? _sourceType : GL_UNSIGNED_BYTE,
249                     0);               
250                     
251        if (_readPBuffer.valid())
252        {
253            _readPBuffer->bindPBufferToTexture(GL_FRONT);
254        }
255       
256    }
257    else
258    {
259        glBindTexture(GL_TEXTURE_RECTANGLE, 0);
260    }
261}
262
263void TextureRectangle::applyTexImage_load(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight) const
264{
265    // if we don't have a valid image we can't create a texture!
266    if (!image || !image->data())
267        return;
268
269    // get the contextID (user defined ID of 0 upwards) for the
270    // current OpenGL context.
271    const unsigned int contextID = state.getContextID();
272    const Extensions* extensions = getExtensions(contextID,true);
273
274    // update the modified count to show that it is upto date.
275    getModifiedCount(contextID) = image->getModifiedCount();
276
277    // compute the internal texture format, sets _internalFormat.
278    computeInternalFormat();
279
280    glPixelStorei(GL_UNPACK_ALIGNMENT, image->getPacking());
281
282    bool useClientStorage = extensions->isClientStorageSupported() && getClientStorageHint();
283    if (useClientStorage)
284    {
285        glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE,GL_TRUE);
286        glTexParameterf(target,GL_TEXTURE_PRIORITY,0.0f);
287       
288        #ifdef GL_TEXTURE_STORAGE_HINT_APPLE   
289            glTexParameteri(target, GL_TEXTURE_STORAGE_HINT_APPLE , GL_STORAGE_CACHED_APPLE);
290        #endif
291    }
292
293    unsigned char* dataMinusOffset = 0;
294    unsigned char* dataPlusOffset = 0;
295
296    const PixelBufferObject* pbo = image->getPixelBufferObject();
297    if (pbo && pbo->isPBOSupported(contextID))
298    {
299        state.bindPixelBufferObject(pbo);
300        dataMinusOffset = image->data();
301        dataPlusOffset = reinterpret_cast<unsigned char*>(pbo->offset());
302    }
303    else
304    {
305        pbo = 0;
306    }
307
308    if(isCompressedInternalFormat(_internalFormat) && extensions->isCompressedTexImage2DSupported())
309    {
310        extensions->glCompressedTexImage2D(target, 0, _internalFormat,
311          image->s(), image->t(), 0,
312          image->getImageSizeInBytes(),
313          image->data() - dataMinusOffset + dataPlusOffset);               
314    }
315    else
316    {
317        glTexImage2D(target, 0, _internalFormat,
318          image->s(), image->t(), 0,
319          (GLenum)image->getPixelFormat(),
320          (GLenum)image->getDataType(),
321          image->data() - dataMinusOffset + dataPlusOffset );
322    }
323   
324
325    if (pbo)
326    {
327        state.unbindPixelBufferObject();
328    }
329
330    inwidth = image->s();
331    inheight = image->t();
332
333    if (useClientStorage)
334    {
335        glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE,GL_FALSE);
336    }
337}
338
339void TextureRectangle::applyTexImage_subload(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLint& inInternalFormat) const
340{
341    // if we don't have a valid image we can't create a texture!
342    if (!image || !image->data())
343        return;
344
345    if (image->s()!=inwidth || image->t()!=inheight || image->getInternalTextureFormat()!=inInternalFormat)
346    {
347        applyTexImage_load(target, image, state, inwidth, inheight);
348        return;
349    }
350
351
352    // get the contextID (user defined ID of 0 upwards) for the
353    // current OpenGL context.
354    const unsigned int contextID = state.getContextID();
355    const Extensions* extensions = getExtensions(contextID,true);
356
357
358    // update the modified count to show that it is upto date.
359    getModifiedCount(contextID) = image->getModifiedCount();
360
361    // compute the internal texture format, sets _internalFormat.
362    computeInternalFormat();
363
364    glPixelStorei(GL_UNPACK_ALIGNMENT, image->getPacking());
365
366#ifdef DO_TIMING
367    osg::Timer_t start_tick = osg::Timer::instance()->tick();
368    osg::notify(osg::NOTICE)<<"glTexSubImage2D pixelFormat = "<<std::hex<<image->getPixelFormat()<<std::dec<<std::endl;
369#endif
370    unsigned char* dataMinusOffset = 0;
371    unsigned char* dataPlusOffset = 0;
372
373    const PixelBufferObject* pbo = image->getPixelBufferObject();
374    if (pbo && pbo->isPBOSupported(contextID))
375    {
376        state.bindPixelBufferObject(pbo);
377        dataMinusOffset = image->data();
378        dataPlusOffset = reinterpret_cast<unsigned char*>(pbo->offset()); // -dataMinusOffset+dataPlusOffset
379
380#ifdef DO_TIMING
381        osg::notify(osg::NOTICE)<<"after PBO "<<osg::Timer::instance()->delta_m(start_tick,osg::Timer::instance()->tick())<<"ms"<<std::endl;
382#endif
383
384    }
385    else
386    {
387        pbo = 0;
388    }
389   
390
391    if(isCompressedInternalFormat(_internalFormat) && extensions->isCompressedTexSubImage2DSupported())
392    {
393        extensions->glCompressedTexSubImage2D(target, 0,
394          0,0,
395          image->s(), image->t(),
396          (GLenum)image->getPixelFormat(),
397          (GLenum)image->getDataType(),
398          image->data() - dataMinusOffset + dataPlusOffset);               
399    }
400    else
401    {
402        glTexSubImage2D(target, 0,
403          0,0,
404          image->s(), image->t(),
405          (GLenum)image->getPixelFormat(),
406          (GLenum)image->getDataType(),
407          image->data() - dataMinusOffset + dataPlusOffset  );
408    }
409
410    if (pbo)
411    {
412        state.unbindPixelBufferObject();
413    }
414
415#ifdef DO_TIMING
416    osg::notify(osg::NOTICE)<<"glTexSubImage2D "<<osg::Timer::instance()->delta_m(start_tick,osg::Timer::instance()->tick())<<"ms"<<std::endl;
417#endif
418   
419}
420
421void TextureRectangle::computeInternalFormat() const
422{
423    if (_image.valid()) computeInternalFormatWithImage(*_image);
424    else computeInternalFormatType();
425}
426
427void TextureRectangle::copyTexImage2D(State& state, int x, int y, int width, int height )
428{
429    const unsigned int contextID = state.getContextID();
430   
431    if (_internalFormat==0) _internalFormat=GL_RGBA;
432
433    // get the globj for the current contextID.
434    TextureObject* textureObject = getTextureObject(contextID);
435   
436    if (textureObject)
437    {
438        if (width==(int)_textureWidth && height==(int)_textureHeight)
439        {
440            // we have a valid texture object which is the right size
441            // so lets play clever and use copyTexSubImage2D instead.
442            // this allows use to reuse the texture object and avoid
443            // expensive memory allocations.
444            copyTexSubImage2D(state,0 ,0, x, y, width, height);
445            return;
446        }
447        // the relevent texture object is not of the right size so
448        // needs to been deleted   
449        // remove previously bound textures.
450        dirtyTextureObject();
451        // note, dirtyTextureObject() dirties all the texture objects for
452        // this texture, is this right?  Perhaps we should dirty just the
453        // one for this context.  Note sure yet will leave till later.
454        // RO July 2001.
455    }
456   
457   
458    // remove any previously assigned images as these are nolonger valid.
459    _image = NULL;
460
461    // switch off mip-mapping.
462    //
463    _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_RECTANGLE);
464
465    textureObject->bind();
466   
467    applyTexParameters(GL_TEXTURE_RECTANGLE,state);
468
469
470/*    bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST);
471    bool hardwareMipMapOn = false;
472    if (needHardwareMipMap)
473    {
474        const Extensions* extensions = getExtensions(contextID,true);
475        bool generateMipMapSupported = extensions->isGenerateMipMapSupported();
476
477        hardwareMipMapOn = _useHardwareMipMapGeneration && generateMipMapSupported;
478       
479        if (!hardwareMipMapOn)
480        {
481            // have to swtich off mip mapping
482            notify(NOTICE)<<"Warning: Texture2D::copyTexImage2D(,,,,) switch of mip mapping as hardware support not available."<<std::endl;
483            _min_filter = LINEAR;
484        }
485    }
486*/   
487//    if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
488
489    glCopyTexImage2D( GL_TEXTURE_RECTANGLE, 0, _internalFormat, x, y, width, height, 0 );
490
491//    if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE);
492
493
494    _textureWidth = width;
495    _textureHeight = height;
496//    _numMipmapLevels = 1;
497   
498    textureObject->setAllocated(1,_internalFormat,_textureWidth,_textureHeight,1,0);
499
500
501    // inform state that this texture is the current one bound.
502    state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
503}
504
505void TextureRectangle::copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height )
506{
507    const unsigned int contextID = state.getContextID();
508
509    if (_internalFormat==0) _internalFormat=GL_RGBA;
510
511    // get the texture object for the current contextID.
512    TextureObject* textureObject = getTextureObject(contextID);
513   
514    if (textureObject)
515    {
516        // we have a valid image
517        textureObject->bind();
518       
519        applyTexParameters(GL_TEXTURE_RECTANGLE,state);
520
521/*        bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST);
522        bool hardwareMipMapOn = false;
523        if (needHardwareMipMap)
524        {
525            const Extensions* extensions = getExtensions(contextID,true);
526            bool generateMipMapSupported = extensions->isGenerateMipMapSupported();
527
528            hardwareMipMapOn = _useHardwareMipMapGeneration && generateMipMapSupported;
529
530            if (!hardwareMipMapOn)
531            {
532                // have to swtich off mip mapping
533                notify(NOTICE)<<"Warning: Texture2D::copyTexImage2D(,,,,) switch of mip mapping as hardware support not available."<<std::endl;
534                _min_filter = LINEAR;
535            }
536        }
537*/
538//        if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
539
540        glCopyTexSubImage2D( GL_TEXTURE_RECTANGLE, 0, xoffset, yoffset, x, y, width, height);
541
542//        if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE);
543
544        // inform state that this texture is the current one bound.
545        state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
546
547    }
548    else
549    {
550        // no texture object already exsits for this context so need to
551        // create it upfront - simply call copyTexImage2D.
552        copyTexImage2D(state,x,y,width,height);
553    }
554}
555
556void TextureRectangle::allocateMipmap(State&) const
557{
558    osg::notify(osg::NOTICE)<<"Warning: TextureRectangle::allocateMipmap(State&) called eroneously, GL_TEXTURE_RECTANGLE does not support mipmapping."<<std::endl;
559}
Note: See TracBrowser for help on using the browser.