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_GlCore32.hxx>
20 #include <OpenGl_Sampler.hxx>
21 #include <Graphic3d_TextureParams.hxx>
22 #include <TCollection_ExtendedString.hxx>
23 #include <Standard_Assert.hxx>
24 #include <Image_PixMap.hxx>
26 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Texture, OpenGl_NamedResource)
28 //! Simple class to reset unpack alignment settings
29 struct OpenGl_UnpackAlignmentSentry
32 //! Reset unpack alignment settings to safe values
35 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
36 #if !defined(GL_ES_VERSION_2_0)
37 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
41 OpenGl_UnpackAlignmentSentry() {}
43 ~OpenGl_UnpackAlignmentSentry()
50 // =======================================================================
51 // function : OpenGl_Texture
53 // =======================================================================
54 OpenGl_Texture::OpenGl_Texture (const TCollection_AsciiString& theResourceId,
55 const Handle(Graphic3d_TextureParams)& theParams)
56 : OpenGl_NamedResource (theResourceId),
57 mySampler (new OpenGl_Sampler (theParams)),
59 myTextureId (NO_TEXTURE),
60 myTarget (GL_TEXTURE_2D),
64 myTextFormat (GL_RGBA),
65 mySizedFormat(GL_RGBA8),
67 myHasMipmaps (Standard_False),
73 // =======================================================================
74 // function : ~OpenGl_Texture
76 // =======================================================================
77 OpenGl_Texture::~OpenGl_Texture()
82 // =======================================================================
85 // =======================================================================
86 bool OpenGl_Texture::Create (const Handle(OpenGl_Context)& theCtx)
88 if (myTextureId != NO_TEXTURE)
93 theCtx->core11fwd->glGenTextures (1, &myTextureId);
94 if (myTextureId == NO_TEXTURE)
99 //mySampler->Create (theCtx); // do not create sampler object by default
103 // =======================================================================
104 // function : Release
106 // =======================================================================
107 void OpenGl_Texture::Release (OpenGl_Context* theGlCtx)
109 mySampler->Release (theGlCtx);
110 if (myTextureId == NO_TEXTURE)
115 // application can not handle this case by exception - this is bug in code
116 Standard_ASSERT_RETURN (theGlCtx != NULL,
117 "OpenGl_Texture destroyed without GL context! Possible GPU memory leakage...",);
119 if (theGlCtx->IsValid())
121 glDeleteTextures (1, &myTextureId);
123 myTextureId = NO_TEXTURE;
124 mySizeX = mySizeY = mySizeZ = 0;
127 // =======================================================================
128 // function : applyDefaultSamplerParams
130 // =======================================================================
131 void OpenGl_Texture::applyDefaultSamplerParams (const Handle(OpenGl_Context)& theCtx)
133 OpenGl_Sampler::applySamplerParams (theCtx, mySampler->Parameters(), NULL, myTarget, myHasMipmaps);
134 if (mySampler->IsValid() && !mySampler->IsImmutable())
136 OpenGl_Sampler::applySamplerParams (theCtx, mySampler->Parameters(), mySampler.get(), myTarget, myHasMipmaps);
140 // =======================================================================
143 // =======================================================================
144 void OpenGl_Texture::Bind (const Handle(OpenGl_Context)& theCtx,
145 const Graphic3d_TextureUnit theTextureUnit) const
147 if (theCtx->core15fwd != NULL)
149 theCtx->core15fwd->glActiveTexture (GL_TEXTURE0 + theTextureUnit);
151 mySampler->Bind (theCtx, theTextureUnit);
152 glBindTexture (myTarget, myTextureId);
155 // =======================================================================
158 // =======================================================================
159 void OpenGl_Texture::Unbind (const Handle(OpenGl_Context)& theCtx,
160 const Graphic3d_TextureUnit theTextureUnit) const
162 if (theCtx->core15fwd != NULL)
164 theCtx->core15fwd->glActiveTexture (GL_TEXTURE0 + theTextureUnit);
166 mySampler->Unbind (theCtx, theTextureUnit);
167 glBindTexture (myTarget, NO_TEXTURE);
170 //=======================================================================
171 //function : InitSamplerObject
173 //=======================================================================
174 bool OpenGl_Texture::InitSamplerObject (const Handle(OpenGl_Context)& theCtx)
176 return myTextureId != NO_TEXTURE
177 && mySampler->Init (theCtx, *this);
180 //=======================================================================
181 //function : GetDataFormat
183 //=======================================================================
184 bool OpenGl_Texture::GetDataFormat (const Handle(OpenGl_Context)& theCtx,
185 const Image_PixMap& theData,
186 GLint& theTextFormat,
187 GLenum& thePixelFormat,
190 theTextFormat = GL_RGBA8;
193 switch (theData.Format())
195 case Image_Format_GrayF:
197 if (theCtx->core11 == NULL)
199 theTextFormat = GL_R8; // GL_R32F
200 thePixelFormat = GL_RED;
204 #if !defined(GL_ES_VERSION_2_0)
205 theTextFormat = GL_LUMINANCE8;
207 theTextFormat = GL_LUMINANCE;
209 thePixelFormat = GL_LUMINANCE;
211 theDataType = GL_FLOAT;
214 case Image_Format_AlphaF:
216 if (theCtx->core11 == NULL)
218 theTextFormat = GL_R8; // GL_R32F
219 thePixelFormat = GL_RED;
223 #if !defined(GL_ES_VERSION_2_0)
224 theTextFormat = GL_ALPHA8;
226 theTextFormat = GL_ALPHA;
228 thePixelFormat = GL_ALPHA;
230 theDataType = GL_FLOAT;
233 case Image_Format_RGBAF:
235 theTextFormat = GL_RGBA8; // GL_RGBA32F
236 thePixelFormat = GL_RGBA;
237 theDataType = GL_FLOAT;
240 case Image_Format_BGRAF:
242 if (!theCtx->IsGlGreaterEqual (1, 2) && !theCtx->extBgra)
246 theTextFormat = GL_RGBA8; // GL_RGBA32F
247 thePixelFormat = GL_BGRA_EXT; // equals to GL_BGRA
248 theDataType = GL_FLOAT;
251 case Image_Format_RGBF:
253 theTextFormat = GL_RGB8; // GL_RGB32F
254 thePixelFormat = GL_RGB;
255 theDataType = GL_FLOAT;
258 case Image_Format_BGRF:
260 #if !defined(GL_ES_VERSION_2_0)
261 theTextFormat = GL_RGB8; // GL_RGB32F
262 thePixelFormat = GL_BGR; // equals to GL_BGR_EXT
263 theDataType = GL_FLOAT;
269 case Image_Format_RGBA:
271 theTextFormat = GL_RGBA8;
272 thePixelFormat = GL_RGBA;
273 theDataType = GL_UNSIGNED_BYTE;
276 case Image_Format_BGRA:
278 #if !defined(GL_ES_VERSION_2_0)
279 if (!theCtx->IsGlGreaterEqual (1, 2) && !theCtx->extBgra)
283 theTextFormat = GL_RGBA8;
285 if (!theCtx->extBgra)
289 theTextFormat = GL_BGRA_EXT;
291 thePixelFormat = GL_BGRA_EXT; // equals to GL_BGRA
292 theDataType = GL_UNSIGNED_BYTE;
295 case Image_Format_RGB32:
297 #if !defined(GL_ES_VERSION_2_0)
298 // ask driver to convert data to RGB8 to save memory
299 theTextFormat = GL_RGB8;
301 // conversion is not supported
302 theTextFormat = GL_RGBA8;
304 thePixelFormat = GL_RGBA;
305 theDataType = GL_UNSIGNED_BYTE;
308 case Image_Format_BGR32:
310 #if !defined(GL_ES_VERSION_2_0)
311 if (!theCtx->IsGlGreaterEqual(1, 2) && !theCtx->extBgra)
315 theTextFormat = GL_RGB8;
317 if (!theCtx->extBgra)
321 theTextFormat = GL_BGRA_EXT;
323 thePixelFormat = GL_BGRA_EXT; // equals to GL_BGRA
324 theDataType = GL_UNSIGNED_BYTE;
327 case Image_Format_RGB:
329 theTextFormat = GL_RGB8;
330 thePixelFormat = GL_RGB;
331 theDataType = GL_UNSIGNED_BYTE;
334 case Image_Format_BGR:
336 #if !defined(GL_ES_VERSION_2_0)
337 if (!theCtx->IsGlGreaterEqual (1, 2) && !theCtx->extBgra)
341 theTextFormat = GL_RGB8;
342 thePixelFormat = GL_BGR; // equals to GL_BGR_EXT
343 theDataType = GL_UNSIGNED_BYTE;
349 case Image_Format_Gray:
351 if (theCtx->core11 == NULL)
353 theTextFormat = GL_R8;
354 thePixelFormat = GL_RED;
358 #if !defined(GL_ES_VERSION_2_0)
359 theTextFormat = GL_LUMINANCE8;
361 theTextFormat = GL_LUMINANCE;
363 thePixelFormat = GL_LUMINANCE;
365 theDataType = GL_UNSIGNED_BYTE;
368 case Image_Format_Alpha:
370 if (theCtx->core11 == NULL)
372 theTextFormat = GL_R8;
373 thePixelFormat = GL_RED;
377 #if !defined(GL_ES_VERSION_2_0)
378 theTextFormat = GL_ALPHA8;
380 theTextFormat = GL_ALPHA;
382 thePixelFormat = GL_ALPHA;
384 theDataType = GL_UNSIGNED_BYTE;
387 case Image_Format_UNKNOWN:
395 // =======================================================================
398 // =======================================================================
399 bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
400 const Standard_Integer theTextFormat,
401 const GLenum thePixelFormat,
402 const GLenum theDataType,
403 const Standard_Integer theSizeX,
404 const Standard_Integer theSizeY,
405 const Graphic3d_TypeOfTexture theType,
406 const Image_PixMap* theImage)
411 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
412 "Error: texture of 0 size cannot be created.");
413 Release (theCtx.operator->());
417 #if !defined(GL_ES_VERSION_2_0)
418 const GLenum aTarget = theType == Graphic3d_TOT_1D
422 const GLenum aTarget = GL_TEXTURE_2D;
424 const Standard_Boolean toCreateMipMaps = (theType == Graphic3d_TOT_2D_MIPMAP);
425 const bool toPatchExisting = IsValid()
426 && myTextFormat == thePixelFormat
427 && myTarget == aTarget
428 && myHasMipmaps == toCreateMipMaps
429 && mySizeX == theSizeX
430 && (mySizeY == theSizeY || theType == Graphic3d_TOT_1D);
431 if (!Create (theCtx))
433 Release (theCtx.operator->());
437 if (theImage != NULL)
439 myIsAlpha = theImage->Format() == Image_Format_Alpha
440 || theImage->Format() == Image_Format_AlphaF;
444 myIsAlpha = thePixelFormat == GL_ALPHA;
447 myHasMipmaps = toCreateMipMaps;
448 myTextFormat = thePixelFormat;
449 mySizedFormat = theTextFormat;
451 #if !defined(GL_ES_VERSION_2_0)
452 const GLint anIntFormat = theTextFormat;
454 // ES 2.0 does not support sized formats and format conversions - them detected from data type
455 const GLint anIntFormat = theCtx->IsGlGreaterEqual (3, 0) ? theTextFormat : thePixelFormat;
458 if (theDataType == GL_FLOAT && !theCtx->arbTexFloat)
460 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
461 "Error: floating-point textures are not supported by hardware.");
462 Release (theCtx.operator->());
466 const GLsizei aMaxSize = theCtx->MaxTextureSize();
467 if (theSizeX > aMaxSize
468 || theSizeY > aMaxSize)
470 TCollection_ExtendedString aWarnMessage = TCollection_ExtendedString ("Error: Texture dimension - ")
471 + theSizeX + "x" + theSizeY + " exceeds hardware limits (" + aMaxSize + "x" + aMaxSize + ")";
473 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aWarnMessage);
474 Release (theCtx.operator->());
477 #if !defined(GL_ES_VERSION_2_0)
478 else if (!theCtx->IsGlGreaterEqual (3, 0) && !theCtx->arbNPTW)
480 // Notice that formally general NPOT textures are required by OpenGL 2.0 specifications
481 // however some hardware (NV30 - GeForce FX, RadeOn 9xxx and Xxxx) supports GLSL but not NPOT!
482 // Trying to create NPOT textures on such hardware will not fail
483 // but driver will fall back into software rendering,
484 const GLsizei aWidthP2 = OpenGl_Context::GetPowerOfTwo (theSizeX, aMaxSize);
485 const GLsizei aHeightP2 = OpenGl_Context::GetPowerOfTwo (theSizeY, aMaxSize);
487 if (theSizeX != aWidthP2
488 || (theType != Graphic3d_TOT_1D && theSizeY != aHeightP2))
490 TCollection_ExtendedString aWarnMessage =
491 TCollection_ExtendedString ("Error: NPOT Textures (") + theSizeX + "x" + theSizeY + ") are not supported by hardware.";
493 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aWarnMessage);
495 Release (theCtx.operator->());
500 else if (!theCtx->IsGlGreaterEqual (3, 0) && theType == Graphic3d_TOT_2D_MIPMAP)
502 // Mipmap NPOT textures are not supported by OpenGL ES 2.0.
503 const GLsizei aWidthP2 = OpenGl_Context::GetPowerOfTwo (theSizeX, aMaxSize);
504 const GLsizei aHeightP2 = OpenGl_Context::GetPowerOfTwo (theSizeY, aMaxSize);
506 if (theSizeX != aWidthP2
507 || theSizeY != aHeightP2)
509 TCollection_ExtendedString aWarnMessage =
510 TCollection_ExtendedString ("Error: Mipmap NPOT Textures (") + theSizeX + "x" + theSizeY + ") are not supported by OpenGL ES 2.0";
512 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aWarnMessage);
514 Release (theCtx.operator->());
520 #if !defined(GL_ES_VERSION_2_0)
521 GLint aTestWidth = 0;
522 GLint aTestHeight = 0;
524 GLvoid* aDataPtr = (theImage != NULL) ? (GLvoid* )theImage->Data() : NULL;
526 // setup the alignment
527 OpenGl_UnpackAlignmentSentry anUnpackSentry;
528 (void)anUnpackSentry; // avoid compiler warning
530 if (aDataPtr != NULL)
532 const GLint anAligment = Min ((GLint )theImage->MaxRowAligmentBytes(), 8); // OpenGL supports alignment upto 8 bytes
533 glPixelStorei (GL_UNPACK_ALIGNMENT, anAligment);
535 #if !defined(GL_ES_VERSION_2_0)
536 // notice that GL_UNPACK_ROW_LENGTH is not available on OpenGL ES 2.0 without GL_EXT_unpack_subimage extension
537 const GLint anExtraBytes = GLint(theImage->RowExtraBytes());
538 const GLint aPixelsWidth = GLint(theImage->SizeRowBytes() / theImage->SizePixelBytes());
539 glPixelStorei (GL_UNPACK_ROW_LENGTH, (anExtraBytes >= anAligment) ? aPixelsWidth : 0);
546 case Graphic3d_TOT_1D:
548 #if !defined(GL_ES_VERSION_2_0)
550 applyDefaultSamplerParams (theCtx);
553 glTexSubImage1D (GL_TEXTURE_1D, 0, 0,
554 theSizeX, thePixelFormat, theDataType, aDataPtr);
559 // use proxy to check texture could be created or not
560 glTexImage1D (GL_PROXY_TEXTURE_1D, 0, anIntFormat,
562 thePixelFormat, theDataType, NULL);
563 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_1D, 0, GL_TEXTURE_WIDTH, &aTestWidth);
564 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_1D, 0, GL_TEXTURE_INTERNAL_FORMAT, &mySizedFormat);
567 // no memory or broken input parameters
569 Release (theCtx.operator->());
573 glTexImage1D (GL_TEXTURE_1D, 0, anIntFormat,
575 thePixelFormat, theDataType, aDataPtr);
576 if (glGetError() != GL_NO_ERROR)
579 Release (theCtx.operator->());
589 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
590 "Error: 1D textures are not supported by hardware.");
591 Release (theCtx.operator->());
595 case Graphic3d_TOT_2D:
598 applyDefaultSamplerParams (theCtx);
601 glTexSubImage2D (GL_TEXTURE_2D, 0,
604 thePixelFormat, theDataType, aDataPtr);
609 #if !defined(GL_ES_VERSION_2_0)
610 // use proxy to check texture could be created or not
611 glTexImage2D (GL_PROXY_TEXTURE_2D, 0, anIntFormat,
612 theSizeX, theSizeY, 0,
613 thePixelFormat, theDataType, NULL);
614 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &aTestWidth);
615 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &aTestHeight);
616 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &mySizedFormat);
617 if (aTestWidth == 0 || aTestHeight == 0)
619 // no memory or broken input parameters
621 Release (theCtx.operator->());
626 glTexImage2D (GL_TEXTURE_2D, 0, anIntFormat,
627 theSizeX, theSizeY, 0,
628 thePixelFormat, theDataType, aDataPtr);
629 const GLenum anErr = glGetError();
630 if (anErr != GL_NO_ERROR)
632 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
633 TCollection_AsciiString ("Error: 2D texture ") + theSizeX + "x" + theSizeY
634 + " IF: " + int(anIntFormat) + " PF: " + int(thePixelFormat) + " DT: " + int(theDataType)
635 + " can not be created with error " + int(anErr) + ".");
637 Release (theCtx.operator->());
647 case Graphic3d_TOT_2D_MIPMAP:
650 applyDefaultSamplerParams (theCtx);
653 glTexSubImage2D (GL_TEXTURE_2D, 0,
656 thePixelFormat, theDataType, aDataPtr);
657 if (theCtx->arbFBO != NULL)
660 theCtx->arbFBO->glGenerateMipmap (GL_TEXTURE_2D);
661 if (glGetError() != GL_NO_ERROR)
664 Release (theCtx.operator->());
673 #if !defined(GL_ES_VERSION_2_0)
674 // use proxy to check texture could be created or not
675 glTexImage2D (GL_PROXY_TEXTURE_2D, 0, anIntFormat,
676 theSizeX, theSizeY, 0,
677 thePixelFormat, theDataType, NULL);
678 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &aTestWidth);
679 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &aTestHeight);
680 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &mySizedFormat);
681 if (aTestWidth == 0 || aTestHeight == 0)
683 // no memory or broken input parameters
685 Release (theCtx.operator->());
690 // upload main picture
691 glTexImage2D (GL_TEXTURE_2D, 0, anIntFormat,
692 theSizeX, theSizeY, 0,
693 thePixelFormat, theDataType, theImage->Data());
694 const GLenum aTexImgErr = glGetError();
695 if (aTexImgErr != GL_NO_ERROR)
697 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
698 TCollection_AsciiString ("Error: 2D texture ") + theSizeX + "x" + theSizeY
699 + " IF: " + int(anIntFormat) + " PF: " + int(thePixelFormat) + " DT: " + int(theDataType)
700 + " can not be created with error " + int(aTexImgErr) + ".");
702 Release (theCtx.operator->());
709 if (theCtx->arbFBO != NULL)
712 //glHint (GL_GENERATE_MIPMAP_HINT, GL_NICEST);
713 theCtx->arbFBO->glGenerateMipmap (GL_TEXTURE_2D);
715 if (glGetError() != GL_NO_ERROR)
718 Release (theCtx.operator->());
724 const TCollection_ExtendedString aWarnMessage ("Warning: generating mipmaps requires GL_ARB_framebuffer_object extension which is missing.");
726 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aWarnMessage);
729 Release (theCtx.operator->());
738 Release (theCtx.operator->());
742 // =======================================================================
745 // =======================================================================
746 bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
747 const Image_PixMap& theImage,
748 const Graphic3d_TypeOfTexture theType)
750 if (theImage.IsEmpty())
752 Release (theCtx.operator->());
759 if (!GetDataFormat (theCtx, theImage, aTextFormat, aPixelFormat, aDataType))
761 Release (theCtx.operator->());
766 aTextFormat, aPixelFormat, aDataType,
767 (Standard_Integer)theImage.SizeX(),
768 (Standard_Integer)theImage.SizeY(),
772 // =======================================================================
773 // function : Init2DMultisample
775 // =======================================================================
776 bool OpenGl_Texture::Init2DMultisample (const Handle(OpenGl_Context)& theCtx,
777 const GLsizei theNbSamples,
778 const GLint theTextFormat,
779 const GLsizei theSizeX,
780 const GLsizei theSizeY)
783 || theNbSamples > theCtx->MaxMsaaSamples()
789 myNbSamples = OpenGl_Context::GetPowerOfTwo (theNbSamples, theCtx->MaxMsaaSamples());
790 myTarget = GL_TEXTURE_2D_MULTISAMPLE;
791 myHasMipmaps = false;
792 if(theSizeX > theCtx->MaxTextureSize()
793 || theSizeY > theCtx->MaxTextureSize())
799 //myTextFormat = theTextFormat;
800 mySizedFormat = theTextFormat;
801 #if !defined(GL_ES_VERSION_2_0)
802 if (theCtx->Functions()->glTexStorage2DMultisample != NULL)
804 theCtx->Functions()->glTexStorage2DMultisample (myTarget, myNbSamples, theTextFormat, theSizeX, theSizeY, GL_FALSE);
808 theCtx->Functions()->glTexImage2DMultisample (myTarget, myNbSamples, theTextFormat, theSizeX, theSizeY, GL_FALSE);
811 theCtx->Functions() ->glTexStorage2DMultisample (myTarget, myNbSamples, theTextFormat, theSizeX, theSizeY, GL_FALSE);
813 if (theCtx->core11fwd->glGetError() != GL_NO_ERROR)
826 // =======================================================================
827 // function : InitRectangle
829 // =======================================================================
830 bool OpenGl_Texture::InitRectangle (const Handle(OpenGl_Context)& theCtx,
831 const Standard_Integer theSizeX,
832 const Standard_Integer theSizeY,
833 const OpenGl_TextureFormat& theFormat)
835 if (!Create (theCtx) || !theCtx->IsGlGreaterEqual (3, 0))
840 #if !defined(GL_ES_VERSION_2_0)
841 myTarget = GL_TEXTURE_RECTANGLE;
843 myHasMipmaps = false;
845 const GLsizei aSizeX = Min (theCtx->MaxTextureSize(), theSizeX);
846 const GLsizei aSizeY = Min (theCtx->MaxTextureSize(), theSizeY);
849 applyDefaultSamplerParams (theCtx);
851 myTextFormat = theFormat.Format();
852 mySizedFormat = theFormat.Internal();
854 // setup the alignment
855 OpenGl_UnpackAlignmentSentry::Reset();
857 glTexImage2D (GL_PROXY_TEXTURE_RECTANGLE, 0, mySizedFormat,
859 myTextFormat, GL_FLOAT, NULL);
861 GLint aTestSizeX = 0;
862 GLint aTestSizeY = 0;
864 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_RECTANGLE, 0, GL_TEXTURE_WIDTH, &aTestSizeX);
865 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_RECTANGLE, 0, GL_TEXTURE_HEIGHT, &aTestSizeY);
866 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_RECTANGLE, 0, GL_TEXTURE_INTERNAL_FORMAT, &mySizedFormat);
868 if (aTestSizeX == 0 || aTestSizeY == 0)
874 glTexImage2D (myTarget, 0, mySizedFormat,
876 myTextFormat, GL_FLOAT, NULL);
878 if (glGetError() != GL_NO_ERROR)
897 // =======================================================================
900 // =======================================================================
901 bool OpenGl_Texture::Init3D (const Handle(OpenGl_Context)& theCtx,
902 const GLint theTextFormat,
903 const GLenum thePixelFormat,
904 const GLenum theDataType,
905 const Standard_Integer theSizeX,
906 const Standard_Integer theSizeY,
907 const Standard_Integer theSizeZ,
908 const void* thePixels)
910 if (theCtx->Functions()->glTexImage3D == NULL)
912 TCollection_ExtendedString aMsg ("Error: three-dimensional textures are not supported by hardware.");
914 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
917 GL_DEBUG_SEVERITY_HIGH,
928 myTarget = GL_TEXTURE_3D;
930 myHasMipmaps = false;
932 const GLsizei aSizeX = Min (theCtx->MaxTextureSize(), theSizeX);
933 const GLsizei aSizeY = Min (theCtx->MaxTextureSize(), theSizeY);
934 const GLsizei aSizeZ = Min (theCtx->MaxTextureSize(), theSizeZ);
938 if (theDataType == GL_FLOAT && !theCtx->arbTexFloat)
940 TCollection_ExtendedString aMsg ("Error: floating-point textures are not supported by hardware.");
942 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
945 GL_DEBUG_SEVERITY_HIGH,
948 Release (theCtx.operator->());
953 mySizedFormat = theTextFormat;
955 // setup the alignment
956 OpenGl_UnpackAlignmentSentry::Reset();
958 #if !defined (GL_ES_VERSION_2_0)
959 theCtx->core15fwd->glTexImage3D (GL_PROXY_TEXTURE_3D, 0, mySizedFormat,
960 aSizeX, aSizeY, aSizeZ, 0,
961 thePixelFormat, theDataType, NULL);
963 GLint aTestSizeX = 0;
964 GLint aTestSizeY = 0;
965 GLint aTestSizeZ = 0;
967 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &aTestSizeX);
968 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_HEIGHT, &aTestSizeY);
969 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_DEPTH, &aTestSizeZ);
970 glGetTexLevelParameteriv (GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_INTERNAL_FORMAT, &mySizedFormat);
972 if (aTestSizeX == 0 || aTestSizeY == 0 || aTestSizeZ == 0)
975 Release (theCtx.operator->());
980 applyDefaultSamplerParams (theCtx);
981 theCtx->Functions()->glTexImage3D (myTarget, 0, mySizedFormat,
982 aSizeX, aSizeY, aSizeZ, 0,
983 thePixelFormat, theDataType, thePixels);
985 if (glGetError() != GL_NO_ERROR)
988 Release (theCtx.operator->());
1000 // =======================================================================
1001 // function : PixelSizeOfPixelFormat
1003 // =======================================================================
1004 Standard_Size OpenGl_Texture::PixelSizeOfPixelFormat (Standard_Integer theInternalFormat)
1006 switch(theInternalFormat)
1008 // RED variations (GL_RED, OpenGL 3.0+)
1010 case GL_R8: return 1;
1011 case GL_R16: return 2;
1012 case GL_R16F: return 2;
1013 case GL_R32F: return 4;
1015 case GL_RGB: return 3;
1016 case GL_RGB8: return 3;
1017 case GL_RGB16: return 6;
1018 case GL_RGB16F: return 6;
1019 case GL_RGB32F: return 12;
1021 case GL_RGBA: return 4;
1022 case GL_RGBA8: return 4;
1023 case GL_RGB10_A2: return 4;
1024 case GL_RGBA12: return 6;
1025 case GL_RGBA16: return 8;
1026 case GL_RGBA16F: return 8;
1027 case GL_RGBA32F: return 16;
1029 case GL_BGRA_EXT: return 4;
1030 // ALPHA variations (deprecated)
1032 case GL_ALPHA8: return 1;
1033 case GL_ALPHA16: return 2;
1034 case GL_LUMINANCE: return 1;
1035 case GL_LUMINANCE_ALPHA: return 2;
1037 case GL_DEPTH24_STENCIL8: return 4;
1038 case GL_DEPTH32F_STENCIL8: return 8;
1039 case GL_DEPTH_COMPONENT16: return 2;
1040 case GL_DEPTH_COMPONENT24: return 3;
1041 case GL_DEPTH_COMPONENT32F: return 4;
1046 // =======================================================================
1047 // function : EstimatedDataSize
1049 // =======================================================================
1050 Standard_Size OpenGl_Texture::EstimatedDataSize() const
1057 Standard_Size aSize = PixelSizeOfPixelFormat (mySizedFormat) * mySizeX * myNbSamples;
1060 aSize *= Standard_Size(mySizeY);
1064 aSize *= Standard_Size(mySizeZ);
1068 aSize = aSize + aSize / 3;