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

Revision 10867, 16.6 kB (checked in by robert, 4 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#include <osg/GLExtensions>
14#include <osg/Texture1D>
15#include <osg/ImageSequence>
16#include <osg/State>
17#include <osg/GLU>
18
19typedef void (APIENTRY * MyCompressedTexImage1DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLint border, GLsizei imageSize, const GLvoid *data);
20
21using namespace osg;
22
23Texture1D::Texture1D():
24            _textureWidth(0),
25            _numMipmapLevels(0)
26{
27}
28
29Texture1D::Texture1D(osg::Image* image):
30            _textureWidth(0),
31            _numMipmapLevels(0)
32{
33    setImage(image);
34}
35
36Texture1D::Texture1D(const Texture1D& text,const CopyOp& copyop):
37            Texture(text,copyop),
38            _image(copyop(text._image.get())),
39            _textureWidth(text._textureWidth),
40            _numMipmapLevels(text._numMipmapLevels),
41            _subloadCallback(text._subloadCallback)
42{
43}
44
45Texture1D::~Texture1D()
46{
47}
48
49int Texture1D::compare(const StateAttribute& sa) const
50{
51    // check the types are equal and then create the rhs variable
52    // used by the COMPARE_StateAttribute_Parameter macro's below.
53    COMPARE_StateAttribute_Types(Texture1D,sa)
54
55    if (_image!=rhs._image) // smart pointer comparison.
56    {
57        if (_image.valid())
58        {
59            if (rhs._image.valid())
60            {
61                int result = _image->compare(*rhs._image);
62                if (result!=0) return result;
63            }
64            else
65            {
66                return 1; // valid lhs._image is greater than null.
67            }
68        }
69        else if (rhs._image.valid())
70        {
71            return -1; // valid rhs._image is greater than null.
72        }
73    }
74
75    if (!_image && !rhs._image)
76    {
77        // no image attached to either Texture2D
78        // but could these textures already be downloaded?
79        // check the _textureObjectBuffer to see if they have been
80        // downloaded
81
82        int result = compareTextureObjects(rhs);
83        if (result!=0) return result;
84    }
85
86    int result = compareTexture(rhs);
87    if (result!=0) return result;
88
89    // compare each parameter in turn against the rhs.
90    COMPARE_StateAttribute_Parameter(_textureWidth)
91    COMPARE_StateAttribute_Parameter(_subloadCallback)
92
93    return 0; // passed all the above comparison macro's, must be equal.
94}
95
96void Texture1D::setImage(Image* image)
97{
98    if (_image == image) return;
99
100    if (dynamic_cast<osg::ImageSequence*>(_image.get()))
101    {
102        setUpdateCallback(0);
103        setDataVariance(osg::Object::STATIC);
104    }
105
106    // delete old texture objects.
107    dirtyTextureObject();
108
109    _image = image;
110    _modifiedCount.setAllElementsTo(0);
111   
112    if (dynamic_cast<osg::ImageSequence*>(_image.get()))
113    {
114        setUpdateCallback(new ImageSequence::UpdateCallback());
115        setDataVariance(osg::Object::DYNAMIC);
116    }
117}
118
119
120void Texture1D::apply(State& state) const
121{
122#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
123    // get the contextID (user defined ID of 0 upwards) for the
124    // current OpenGL context.
125    const unsigned int contextID = state.getContextID();
126
127    Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get();
128    ElapsedTime elapsedTime(&(tom->getApplyTime()));
129    tom->getNumberApplied()++;
130
131    // get the texture object for the current contextID.
132    TextureObject* textureObject = getTextureObject(contextID);
133
134    if (textureObject)
135    {
136        if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount())
137        {
138            // compute the internal texture format, this set the _internalFormat to an appropriate value.
139            computeInternalFormat();
140
141            GLsizei new_width = _image->s();
142            GLsizei new_numMipmapLevels = _numMipmapLevels;
143
144            if (!textureObject->match(GL_TEXTURE_1D, new_numMipmapLevels, _internalFormat, new_width, 1, 1, _borderWidth))
145            {
146                Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get());
147                _textureObjectBuffer[contextID] = 0;
148                textureObject = 0;
149            }
150        }
151    }
152
153    if (textureObject)
154    {
155        textureObject->bind();
156
157        if (getTextureParameterDirty(state.getContextID())) applyTexParameters(GL_TEXTURE_1D,state);
158
159        if (_subloadCallback.valid())
160        {
161            _subloadCallback->subload(*this,state);
162        }
163        else if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount())
164        {
165            applyTexImage1D(GL_TEXTURE_1D,_image.get(),state, _textureWidth, _numMipmapLevels);
166
167            // update the modified count to show that it is upto date.
168            getModifiedCount(contextID) = _image->getModifiedCount();
169        }
170
171    }
172    else if (_subloadCallback.valid())
173    {
174
175        // we don't have a applyTexImage1D_subload yet so can't reuse.. so just generate a new texture object.       
176        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID, GL_TEXTURE_1D);
177
178        textureObject->bind();
179
180        applyTexParameters(GL_TEXTURE_1D,state);
181
182        _subloadCallback->load(*this,state);
183
184        textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,1,1,0);
185
186        // in theory the following line is redundent, but in practice
187        // have found that the first frame drawn doesn't apply the textures
188        // unless a second bind is called?!!
189        // perhaps it is the first glBind which is not required...
190        //glBindTexture( GL_TEXTURE_1D, handle );
191
192    }
193    else if (_image.valid() && _image->data())
194    {
195
196        // we don't have a applyTexImage1D_subload yet so can't reuse.. so just generate a new texture object.       
197        textureObject = generateTextureObject(this, contextID,GL_TEXTURE_1D);
198
199        textureObject->bind();
200
201        applyTexParameters(GL_TEXTURE_1D,state);
202
203        applyTexImage1D(GL_TEXTURE_1D,_image.get(),state, _textureWidth, _numMipmapLevels);
204
205        textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,1,1,0);
206
207        // update the modified count to show that it is upto date.
208        getModifiedCount(contextID) = _image->getModifiedCount();
209   
210        _textureObjectBuffer[contextID] = textureObject;
211   
212        if (state.getMaxTexturePoolSize()==0 && _unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC)
213        {
214            Texture1D* non_const_this = const_cast<Texture1D*>(this);
215            non_const_this->_image = 0;
216        }
217       
218    }
219    else if ( (_textureWidth!=0) && (_internalFormat!=0) )
220    {
221        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(
222                this,contextID,GL_TEXTURE_1D,_numMipmapLevels,_internalFormat,_textureWidth,1,1,0);
223       
224        textureObject->bind();
225
226        applyTexParameters(GL_TEXTURE_1D,state);
227
228        // no image present, but dimensions are set so lets create the texture
229        glTexImage1D( GL_TEXTURE_1D, 0, _internalFormat,
230                     _textureWidth, _borderWidth,
231                     _sourceFormat ? _sourceFormat : _internalFormat,
232                     _sourceType ? _sourceType : GL_UNSIGNED_BYTE,
233                     0);               
234                     
235        if (_readPBuffer.valid())
236        {
237            _readPBuffer->bindPBufferToTexture(GL_FRONT);
238        }
239
240    }
241    else
242    {
243        glBindTexture( GL_TEXTURE_1D, 0 );
244    }
245
246    // if texture object is now valid and we have to allocate mipmap levels, then
247    if (textureObject != 0 && _texMipmapGenerationDirtyList[contextID])
248    {
249        generateMipmap(state);
250    }
251#else
252    osg::notify(osg::NOTICE)<<"Warning: Texture1D::apply(State& state) not supported."<<std::endl;
253#endif
254}
255
256void Texture1D::computeInternalFormat() const
257{
258    if (_image.valid()) computeInternalFormatWithImage(*_image);
259    else computeInternalFormatType();
260}
261
262void Texture1D::applyTexImage1D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& numMipmapLevels) const
263{
264#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
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
275    // compute the internal texture format, this set the _internalFormat to an appropriate value.
276    computeInternalFormat();
277
278    // select the internalFormat required for the texture.
279    bool compressed = isCompressedInternalFormat(_internalFormat);
280   
281    //Rescale if resize hint is set or NPOT not supported or dimension exceeds max size
282    if( _resizeNonPowerOfTwoHint || !extensions->isNonPowerOfTwoTextureSupported(_min_filter) || inwidth > extensions->maxTextureSize() )
283    {
284        // this is not thread safe... should really create local image data and rescale to that as per Texture2D.
285        image->ensureValidSizeForTexturing(extensions->maxTextureSize());
286    }
287
288    glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking());
289
290    static MyCompressedTexImage1DArbProc glCompressedTexImage1D_ptr =
291        convertPointerType<MyCompressedTexImage1DArbProc, void*>(getGLExtensionFuncPtr("glCompressedTexImage1DARB"));
292
293    if( _min_filter == LINEAR || _min_filter == NEAREST )
294    {
295        if ( !compressed )
296        {
297            numMipmapLevels = 1;
298            glTexImage1D( target, 0, _internalFormat,
299                image->s(), _borderWidth,
300                (GLenum)image->getPixelFormat(),
301                (GLenum)image->getDataType(),
302                image->data() );
303
304        }
305        else if(glCompressedTexImage1D_ptr)
306        {
307            numMipmapLevels = 1;
308            GLint blockSize = ( _internalFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ? 8 : 16 );
309            GLint size = ((image->s()+3)/4)*((image->t()+3)/4)*blockSize;
310            glCompressedTexImage1D_ptr(target, 0, _internalFormat,
311                  image->s(), _borderWidth,
312                  size,
313                  image->data());               
314
315        }
316
317    }
318    else
319    {
320        if(!image->isMipmap())
321        {
322
323            numMipmapLevels = 1;
324
325#ifdef OSG_GLU_AVAILABLE
326            gluBuild1DMipmaps( target, _internalFormat,
327                image->s(),
328                (GLenum)image->getPixelFormat(), (GLenum)image->getDataType(),
329                image->data() );
330#else
331            osg::notify(osg::NOTICE)<<"Warning: gluBuild1DMipmaps(..) not supported."<<std::endl;
332#endif
333
334        }
335        else
336        {
337            numMipmapLevels = image->getNumMipmapLevels();
338
339            int width  = image->s();
340
341            if( !compressed )
342            {
343                for( GLsizei k = 0 ; k < numMipmapLevels  && width ;k++)
344                {
345
346                    glTexImage1D( target, k, _internalFormat,
347                         width,_borderWidth,
348                        (GLenum)image->getPixelFormat(),
349                        (GLenum)image->getDataType(),
350                        image->getMipmapData(k));
351
352                    width >>= 1;
353                }
354            }
355            else if(glCompressedTexImage1D_ptr)
356            {
357                GLint blockSize = ( _internalFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ? 8 : 16 );
358                GLint size = 0;
359                for( GLsizei k = 0 ; k < numMipmapLevels  && width ;k++)
360                {
361
362                    size = ((width+3)/4)*blockSize;
363                    glCompressedTexImage1D_ptr(target, k, _internalFormat,
364                        width,  _borderWidth, size, image->getMipmapData(k));               
365
366                    width >>= 1;
367                }
368            }
369        }
370
371    }
372
373    inwidth = image->s();
374#else
375    osg::notify(osg::NOTICE)<<"Warning: Texture1D::applyTexImage1D(State& state) not supported."<<std::endl;
376#endif
377}
378
379void Texture1D::copyTexImage1D(State& state, int x, int y, int width)
380{
381#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
382    const unsigned int contextID = state.getContextID();
383
384    // get the texture object for the current contextID.
385    TextureObject* textureObject = getTextureObject(contextID);
386
387    if (textureObject != 0)
388    {
389        if (width==(int)_textureWidth)
390        {
391            // we have a valid texture object which is the right size
392            // so lets play clever and use copyTexSubImage1D instead.
393            // this allows use to reuse the texture object and avoid
394            // expensive memory allocations.
395            copyTexSubImage1D(state,0 ,x, y, width);
396            return;
397        }
398        // the relevent texture object is not of the right size so
399        // needs to been deleted   
400        // remove previously bound textures.
401        dirtyTextureObject();
402        // note, dirtyTextureObject() dirties all the texture objects for
403        // this texture, is this right?  Perhaps we should dirty just the
404        // one for this context.  Note sure yet will leave till later.
405        // RO July 2001.
406    }
407   
408   
409    // remove any previously assigned images as these are nolonger valid.
410    _image = NULL;
411
412    // switch off mip-mapping.
413    _min_filter = LINEAR;
414    _mag_filter = LINEAR;
415
416    _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_1D);
417
418    textureObject->bind();
419
420
421    applyTexParameters(GL_TEXTURE_1D,state);
422    glCopyTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, x, y, width, 0 );
423
424    _textureWidth = width;
425    _numMipmapLevels = 1;
426   
427    textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,1,1,0);
428
429    // inform state that this texture is the current one bound.
430    state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
431#else
432    osg::notify(osg::NOTICE)<<"Warning: Texture1D::copyTexImage1D(..) not supported."<<std::endl;
433#endif
434}
435
436void Texture1D::copyTexSubImage1D(State& state, int xoffset, int x, int y, int width)
437{
438#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
439    const unsigned int contextID = state.getContextID();
440
441    // get the texture object for the current contextID.
442    TextureObject* textureObject = getTextureObject(contextID);
443
444    if (textureObject != 0)
445    {
446
447        textureObject->bind();
448
449        // we have a valid image
450        applyTexParameters(GL_TEXTURE_1D,state);
451        glCopyTexSubImage1D( GL_TEXTURE_1D, 0, xoffset, x, y, width);
452
453        // inform state that this texture is the current one bound.
454        state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
455
456    }
457    else
458    {
459        // no texture object already exsits for this context so need to
460        // create it upfront - simply call copyTexImage1D.
461        copyTexImage1D(state,x,y,width);
462    }
463#else
464    osg::notify(osg::NOTICE)<<"Warning: Texture1D::copyTexSubImage1D(..) not supported."<<std::endl;
465#endif
466}
467
468void Texture1D::allocateMipmap(State& state) const
469{
470#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
471    const unsigned int contextID = state.getContextID();
472
473    // get the texture object for the current contextID.
474    TextureObject* textureObject = getTextureObject(contextID);
475   
476    if (textureObject && _textureWidth != 0)
477    {
478        // bind texture
479        textureObject->bind();
480
481        // compute number of mipmap levels
482        int width = _textureWidth;
483        int numMipmapLevels = Image::computeNumberOfMipmapLevels(width);
484
485        // we do not reallocate the level 0, since it was already allocated
486        width >>= 1;
487       
488        for( GLsizei k = 1; k < numMipmapLevels  && width; k++)
489        {
490            if (width == 0)
491                width = 1;
492
493            glTexImage1D( GL_TEXTURE_1D, k, _internalFormat,
494                     width, _borderWidth,
495                     _sourceFormat ? _sourceFormat : _internalFormat,
496                     _sourceType ? _sourceType : GL_UNSIGNED_BYTE, NULL);
497
498            width >>= 1;
499        }
500               
501        // inform state that this texture is the current one bound.
502        state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);       
503    }
504#else
505    osg::notify(osg::NOTICE)<<"Warning: Texture1D::allocateMipmap(..) not supported."<<std::endl;
506#endif
507}
Note: See TracBrowser for help on using the browser.