1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <OpenGl_Texture.hxx>
17 #include <OpenGl_ArbFBO.hxx>
18 #include <OpenGl_Context.hxx>
19 #include <OpenGl_GlCore15.hxx>
20 #include <Graphic3d_TextureParams.hxx>
21 #include <Standard_Assert.hxx>
22 #include <Image_PixMap.hxx>
24 IMPLEMENT_STANDARD_HANDLE (OpenGl_Texture, OpenGl_Resource)
25 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Texture, OpenGl_Resource)
27 //! Simple class to reset unpack alignment settings
28 struct OpenGl_UnpackAlignmentSentry
31 //! Reset unpack alignment settings to safe values
34 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
35 #if !defined(GL_ES_VERSION_2_0)
36 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
40 ~OpenGl_UnpackAlignmentSentry()
47 // =======================================================================
48 // function : OpenGl_Texture
50 // =======================================================================
51 OpenGl_Texture::OpenGl_Texture (const Handle(Graphic3d_TextureParams)& theParams)
53 myTextureId (NO_TEXTURE),
54 myTarget (GL_TEXTURE_2D),
57 myTextFormat (GL_RGBA),
58 myHasMipmaps (Standard_False),
61 if (myParams.IsNull())
63 myParams = new Graphic3d_TextureParams();
67 // =======================================================================
68 // function : ~OpenGl_Texture
70 // =======================================================================
71 OpenGl_Texture::~OpenGl_Texture()
76 // =======================================================================
77 // function : HasMipmaps
79 // =======================================================================
80 const Standard_Boolean OpenGl_Texture::HasMipmaps() const
85 // =======================================================================
86 // function : GetParams
88 // =======================================================================
89 const Handle(Graphic3d_TextureParams)& OpenGl_Texture::GetParams() const
94 // =======================================================================
95 // function : SetParams
97 // =======================================================================
98 void OpenGl_Texture::SetParams (const Handle(Graphic3d_TextureParams)& theParams)
100 myParams = theParams;
103 // =======================================================================
106 // =======================================================================
107 bool OpenGl_Texture::Create (const Handle(OpenGl_Context)& )
109 if (myTextureId == NO_TEXTURE)
111 glGenTextures (1, &myTextureId);
113 return myTextureId != NO_TEXTURE;
116 // =======================================================================
117 // function : Release
119 // =======================================================================
120 void OpenGl_Texture::Release (OpenGl_Context* theGlCtx)
122 if (myTextureId == NO_TEXTURE)
127 // application can not handle this case by exception - this is bug in code
128 Standard_ASSERT_RETURN (theGlCtx != NULL,
129 "OpenGl_Texture destroyed without GL context! Possible GPU memory leakage...",);
131 if (theGlCtx->IsValid())
133 glDeleteTextures (1, &myTextureId);
135 myTextureId = NO_TEXTURE;
136 mySizeX = mySizeY = 0;
139 // =======================================================================
142 // =======================================================================
143 void OpenGl_Texture::Bind (const Handle(OpenGl_Context)& theCtx,
144 const GLenum theTextureUnit) const
146 if (theCtx->IsGlGreaterEqual (1, 5))
148 theCtx->core15fwd->glActiveTexture (theTextureUnit);
150 glBindTexture (myTarget, myTextureId);
153 // =======================================================================
156 // =======================================================================
157 void OpenGl_Texture::Unbind (const Handle(OpenGl_Context)& theCtx,
158 const GLenum theTextureUnit) const
160 if (theCtx->IsGlGreaterEqual (1, 5))
162 theCtx->core15fwd->glActiveTexture (theTextureUnit);
164 glBindTexture (myTarget, NO_TEXTURE);
167 //=======================================================================
168 //function : GetDataFormat
170 //=======================================================================
171 bool OpenGl_Texture::GetDataFormat (const Handle(OpenGl_Context)& theCtx,
172 const Image_PixMap& theData,
173 GLint& theTextFormat,
174 GLenum& thePixelFormat,
177 theTextFormat = GL_RGBA8;
180 switch (theData.Format())
182 case Image_PixMap::ImgGrayF:
184 if (theCtx->core11 == NULL)
186 theTextFormat = GL_R8; // GL_R32F
187 thePixelFormat = GL_RED;
191 theTextFormat = GL_LUMINANCE8;
192 thePixelFormat = GL_LUMINANCE;
194 theDataType = GL_FLOAT;
197 case Image_PixMap::ImgAlphaF:
199 if (theCtx->core11 == NULL)
201 theTextFormat = GL_R8; // GL_R32F
202 thePixelFormat = GL_RED;
206 theTextFormat = GL_ALPHA8;
207 thePixelFormat = GL_ALPHA;
209 theDataType = GL_FLOAT;
212 case Image_PixMap::ImgRGBAF:
214 theTextFormat = GL_RGBA8; // GL_RGBA32F
215 thePixelFormat = GL_RGBA;
216 theDataType = GL_FLOAT;
219 case Image_PixMap::ImgBGRAF:
221 if (!theCtx->IsGlGreaterEqual (1, 2) && !theCtx->extBgra)
225 theTextFormat = GL_RGBA8; // GL_RGBA32F
226 thePixelFormat = GL_BGRA_EXT; // equals to GL_BGRA
227 theDataType = GL_FLOAT;
230 case Image_PixMap::ImgRGBF:
232 theTextFormat = GL_RGB8; // GL_RGB32F
233 thePixelFormat = GL_RGB;
234 theDataType = GL_FLOAT;
237 case Image_PixMap::ImgBGRF:
239 #if !defined(GL_ES_VERSION_2_0)
240 theTextFormat = GL_RGB8; // GL_RGB32F
241 thePixelFormat = GL_BGR; // equals to GL_BGR_EXT
242 theDataType = GL_FLOAT;
248 case Image_PixMap::ImgRGBA:
250 theTextFormat = GL_RGBA8;
251 thePixelFormat = GL_RGBA;
252 theDataType = GL_UNSIGNED_BYTE;
255 case Image_PixMap::ImgBGRA:
257 if (!theCtx->IsGlGreaterEqual (1, 2) && !theCtx->extBgra)
261 theTextFormat = GL_RGBA8;
262 thePixelFormat = GL_BGRA_EXT; // equals to GL_BGRA
263 theDataType = GL_UNSIGNED_BYTE;
266 case Image_PixMap::ImgRGB32:
268 theTextFormat = GL_RGB8;
269 thePixelFormat = GL_RGBA;
270 theDataType = GL_UNSIGNED_BYTE;
273 case Image_PixMap::ImgBGR32:
275 if (!theCtx->IsGlGreaterEqual (1, 2) && !theCtx->extBgra)
279 theTextFormat = GL_RGB8;
280 thePixelFormat = GL_BGRA_EXT; // equals to GL_BGRA
281 theDataType = GL_UNSIGNED_BYTE;
284 case Image_PixMap::ImgRGB:
286 theTextFormat = GL_RGB8;
287 thePixelFormat = GL_RGB;
288 theDataType = GL_UNSIGNED_BYTE;
291 case Image_PixMap::ImgBGR:
293 #if !defined(GL_ES_VERSION_2_0)
294 if (!theCtx->IsGlGreaterEqual (1, 2) && !theCtx->extBgra)
298 theTextFormat = GL_RGB8;
299 thePixelFormat = GL_BGR; // equals to GL_BGR_EXT
300 theDataType = GL_UNSIGNED_BYTE;
306 case Image_PixMap::ImgGray:
308 if (theCtx->core11 == NULL)
310 theTextFormat = GL_R8;
311 thePixelFormat = GL_RED;
315 theTextFormat = GL_LUMINANCE8;
316 thePixelFormat = GL_LUMINANCE;
318 theDataType = GL_UNSIGNED_BYTE;
321 case Image_PixMap::ImgAlpha:
323 if (theCtx->core11 == NULL)
325 theTextFormat = GL_R8;
326 thePixelFormat = GL_RED;
330 theTextFormat = GL_ALPHA8;
331 thePixelFormat = GL_ALPHA;
333 theDataType = GL_UNSIGNED_BYTE;
336 case Image_PixMap::ImgUNKNOWN:
344 // =======================================================================
347 // =======================================================================
348 bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
349 const Standard_Integer theTextFormat,
350 const GLenum thePixelFormat,
351 const GLenum theDataType,
352 const Standard_Integer theSizeX,
353 const Standard_Integer theSizeY,
354 const Graphic3d_TypeOfTexture theType,
355 const Image_PixMap* theImage)
357 if (!Create (theCtx))
359 Release (theCtx.operator->());
362 myHasMipmaps = Standard_False;
363 myTextFormat = thePixelFormat;
364 #if !defined(GL_ES_VERSION_2_0)
365 const GLint anIntFormat = theTextFormat;
367 // ES does not support sized formats and format conversions - them detected from data type
368 const GLint anIntFormat = thePixelFormat;
370 const GLsizei aWidth = theSizeX;
371 const GLsizei aHeight = theSizeY;
372 const GLsizei aMaxSize = theCtx->MaxTextureSize();
374 // Notice that formally general NPOT textures are required by OpenGL 2.0 specifications
375 // however some hardware (NV30 - GeForce FX, RadeOn 9xxx and Xxxx) supports GLSL but not NPOT!
376 // Trying to create NPOT textures on such hardware will not fail
377 // but driver will fall back into software rendering,
378 const bool toForceP2 = !theCtx->IsGlGreaterEqual (3, 0) && !theCtx->arbNPTW;
379 const GLsizei aWidthOut = toForceP2 ? OpenGl_Context::GetPowerOfTwo (aWidth, aMaxSize) : Min (aWidth, aMaxSize);
380 const GLsizei aHeightOut = toForceP2 ? OpenGl_Context::GetPowerOfTwo (aHeight, aMaxSize) : Min (aHeight, aMaxSize);
382 #if !defined(GL_ES_VERSION_2_0)
383 GLint aTestWidth = 0;
384 GLint aTestHeight = 0;
386 GLvoid* aDataPtr = (theImage != NULL) ? (GLvoid* )theImage->Data() : NULL;
388 // setup the alignment
389 OpenGl_UnpackAlignmentSentry anUnpackSentry;
390 if (aDataPtr != NULL)
392 const GLint anAligment = Min ((GLint )theImage->MaxRowAligmentBytes(), 8); // OpenGL supports alignment upto 8 bytes
393 glPixelStorei (GL_UNPACK_ALIGNMENT, anAligment);
395 #if !defined(GL_ES_VERSION_2_0)
396 // notice that GL_UNPACK_ROW_LENGTH is not available on OpenGL ES 2.0 without GL_EXT_unpack_subimage extension
397 const GLint anExtraBytes = GLint(theImage->RowExtraBytes());
398 const GLint aPixelsWidth = GLint(theImage->SizeRowBytes() / theImage->SizePixelBytes());
399 glPixelStorei (GL_UNPACK_ROW_LENGTH, (anExtraBytes >= anAligment) ? aPixelsWidth : 0);
405 case Graphic3d_TOT_1D:
407 #if !defined(GL_ES_VERSION_2_0)
408 myTarget = GL_TEXTURE_1D;
410 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
411 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
414 if (aDataPtr != NULL)
416 if (aWidth != aWidthOut)
418 glPixelStorei (GL_PACK_ALIGNMENT, 1);
419 glPixelStorei (GL_PACK_ROW_LENGTH, 0);
420 if (!aCopy.InitTrash (theImage->Format(), Standard_Size(aWidthOut), 1)
421 || gluScaleImage (thePixelFormat,
422 aWidth, 1, theDataType, theImage->Data(),
423 aWidthOut, 1, theDataType, aCopy.ChangeData()) != 0)
426 Release (theCtx.operator->());
430 aDataPtr = (GLvoid* )aCopy.Data();
431 anUnpackSentry.Reset();
435 // use proxy to check texture could be created or not
436 glTexImage1D (GL_PROXY_TEXTURE_1D, 0, anIntFormat,
438 thePixelFormat, theDataType, NULL);
439 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &aTestWidth);
442 // no memory or broken input parameters
444 Release (theCtx.operator->());
448 glTexImage1D (GL_TEXTURE_1D, 0, anIntFormat,
450 thePixelFormat, theDataType, aDataPtr);
451 if (glGetError() != GL_NO_ERROR)
454 Release (theCtx.operator->());
467 case Graphic3d_TOT_2D:
469 myTarget = GL_TEXTURE_2D;
471 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
472 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
475 if (aDataPtr != NULL)
477 if (aWidth != aWidthOut || aHeight != aHeightOut)
479 #if !defined(GL_ES_VERSION_2_0)
481 glPixelStorei (GL_PACK_ALIGNMENT, 1);
482 glPixelStorei (GL_PACK_ROW_LENGTH, 0);
483 if (!aCopy.InitTrash (theImage->Format(), Standard_Size(aWidthOut), Standard_Size(aHeightOut))
484 || gluScaleImage (thePixelFormat,
485 aWidth, aHeight, theDataType, theImage->Data(),
486 aWidthOut, aHeightOut, theDataType, aCopy.ChangeData()) != 0)
489 Release (theCtx.operator->());
493 aDataPtr = (GLvoid* )aCopy.Data();
494 anUnpackSentry.Reset();
497 Release (theCtx.operator->());
503 #if !defined(GL_ES_VERSION_2_0)
504 // use proxy to check texture could be created or not
505 glTexImage2D (GL_PROXY_TEXTURE_2D, 0, anIntFormat,
506 aWidthOut, aHeightOut, 0,
507 thePixelFormat, theDataType, NULL);
508 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &aTestWidth);
509 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &aTestHeight);
510 if (aTestWidth == 0 || aTestHeight == 0)
512 // no memory or broken input parameters
514 Release (theCtx.operator->());
519 glTexImage2D (GL_TEXTURE_2D, 0, anIntFormat,
520 aWidthOut, aHeightOut, 0,
521 thePixelFormat, theDataType, aDataPtr);
522 if (glGetError() != GL_NO_ERROR)
525 Release (theCtx.operator->());
530 mySizeY = aHeightOut;
535 case Graphic3d_TOT_2D_MIPMAP:
537 myTarget = GL_TEXTURE_2D;
538 myHasMipmaps = Standard_True;
540 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
541 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
543 if (theCtx->arbFBO != NULL
544 && aWidth == aWidthOut && aHeight == aHeightOut)
546 #if !defined(GL_ES_VERSION_2_0)
547 // use proxy to check texture could be created or not
548 glTexImage2D (GL_PROXY_TEXTURE_2D, 0, anIntFormat,
549 aWidthOut, aHeightOut, 0,
550 thePixelFormat, theDataType, NULL);
551 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &aTestWidth);
552 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &aTestHeight);
553 if (aTestWidth == 0 || aTestHeight == 0)
555 // no memory or broken input parameters
557 Release (theCtx.operator->());
562 // upload main picture
563 glTexImage2D (GL_TEXTURE_2D, 0, anIntFormat,
564 aWidthOut, aHeightOut, 0,
565 thePixelFormat, theDataType, theImage->Data());
566 if (glGetError() != GL_NO_ERROR)
569 Release (theCtx.operator->());
574 mySizeY = aHeightOut;
577 //glHint (GL_GENERATE_MIPMAP_HINT, GL_NICEST);
578 theCtx->arbFBO->glGenerateMipmap (GL_TEXTURE_2D);
585 #if !defined(GL_ES_VERSION_2_0)
586 bool isCreated = gluBuild2DMipmaps (GL_TEXTURE_2D, anIntFormat,
588 thePixelFormat, theDataType, theImage->Data()) == 0;
591 glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &mySizeX);
592 glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &mySizeY);
605 Release (theCtx.operator->());
611 // =======================================================================
614 // =======================================================================
615 bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
616 const Image_PixMap& theImage,
617 const Graphic3d_TypeOfTexture theType)
619 if (theImage.IsEmpty())
621 Release (theCtx.operator->());
628 if (!GetDataFormat (theCtx, theImage, aTextFormat, aPixelFormat, aDataType))
630 Release (theCtx.operator->());
635 aTextFormat, aPixelFormat, aDataType,
636 (Standard_Integer)theImage.SizeX(),
637 (Standard_Integer)theImage.SizeY(),
641 // =======================================================================
642 // function : InitRectangle
644 // =======================================================================
645 bool OpenGl_Texture::InitRectangle (const Handle(OpenGl_Context)& theCtx,
646 const Standard_Integer theSizeX,
647 const Standard_Integer theSizeY,
648 const OpenGl_TextureFormat& theFormat)
650 if (!Create (theCtx) || !theCtx->IsGlGreaterEqual (3, 0))
655 #if !defined(GL_ES_VERSION_2_0)
656 myTarget = GL_TEXTURE_RECTANGLE;
658 const GLsizei aSizeX = Min (theCtx->MaxTextureSize(), theSizeX);
659 const GLsizei aSizeY = Min (theCtx->MaxTextureSize(), theSizeY);
663 if (myParams->Filter() == Graphic3d_TOTF_NEAREST)
665 glTexParameteri (myTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
666 glTexParameteri (myTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
670 glTexParameteri (myTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
671 glTexParameteri (myTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
674 const GLint anIntFormat = theFormat.Internal();
675 myTextFormat = theFormat.Format();
677 glTexImage2D (GL_PROXY_TEXTURE_RECTANGLE,
687 GLint aTestSizeX = 0;
688 GLint aTestSizeY = 0;
690 glGetTexLevelParameteriv (
691 GL_PROXY_TEXTURE_RECTANGLE, 0, GL_TEXTURE_WIDTH, &aTestSizeX);
692 glGetTexLevelParameteriv (
693 GL_PROXY_TEXTURE_RECTANGLE, 0, GL_TEXTURE_HEIGHT, &aTestSizeY);
695 if (aTestSizeX == 0 || aTestSizeY == 0)
701 glTexImage2D (myTarget,
711 if (glGetError() != GL_NO_ERROR)