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