1 // Copyright (c) 2017-2019 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <OpenGl_TextureFormat.hxx>
16 #include <OpenGl_Context.hxx>
18 // =======================================================================
19 // function : FindFormat
21 // =======================================================================
22 OpenGl_TextureFormat OpenGl_TextureFormat::FindFormat (const Handle(OpenGl_Context)& theCtx,
23 Image_Format theFormat,
26 OpenGl_TextureFormat aFormat;
29 case Image_Format_GrayF:
31 aFormat.SetNbComponents (1);
32 if (theCtx->core11 == NULL)
34 aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_R32F : GL_R8);
35 aFormat.SetPixelFormat (GL_RED);
39 #if !defined(GL_ES_VERSION_2_0)
40 aFormat.SetInternalFormat (GL_LUMINANCE8);
42 aFormat.SetInternalFormat (GL_LUMINANCE);
44 aFormat.SetPixelFormat (GL_LUMINANCE);
46 aFormat.SetDataType (GL_FLOAT);
49 case Image_Format_AlphaF:
51 aFormat.SetNbComponents (1);
52 if (theCtx->core11 == NULL)
54 aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_R32F : GL_R8);
55 aFormat.SetPixelFormat (GL_RED);
59 #if !defined(GL_ES_VERSION_2_0)
60 aFormat.SetInternalFormat (GL_ALPHA8);
62 aFormat.SetInternalFormat (GL_ALPHA);
64 aFormat.SetPixelFormat (GL_ALPHA);
66 aFormat.SetDataType (GL_FLOAT);
69 case Image_Format_RGF:
71 if (!theCtx->arbTexRG)
73 return OpenGl_TextureFormat();
75 aFormat.SetNbComponents (2);
76 aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_RG32F : GL_RG8);
77 aFormat.SetPixelFormat (GL_RG);
78 aFormat.SetDataType (GL_FLOAT);
81 case Image_Format_RGBAF:
83 aFormat.SetNbComponents (4);
84 aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_RGBA32F : GL_RGBA8);
85 aFormat.SetPixelFormat (GL_RGBA);
86 aFormat.SetDataType (GL_FLOAT);
89 case Image_Format_BGRAF:
91 if (!theCtx->IsGlGreaterEqual (1, 2) && !theCtx->extBgra)
93 return OpenGl_TextureFormat();
95 aFormat.SetNbComponents (4);
96 aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_RGBA32F : GL_RGBA8);
97 aFormat.SetPixelFormat (GL_BGRA_EXT); // equals to GL_BGRA
98 aFormat.SetDataType (GL_FLOAT);
101 case Image_Format_RGBF:
103 aFormat.SetNbComponents (3);
104 aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_RGB32F : GL_RGB8);
105 aFormat.SetPixelFormat (GL_RGB);
106 aFormat.SetDataType (GL_FLOAT);
109 case Image_Format_BGRF:
111 #if !defined(GL_ES_VERSION_2_0)
112 aFormat.SetNbComponents (3);
113 aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_RGB32F : GL_RGB8);
114 aFormat.SetPixelFormat (GL_BGR); // equals to GL_BGR_EXT
115 aFormat.SetDataType (GL_FLOAT);
118 return OpenGl_TextureFormat();
121 case Image_Format_RGBA:
123 aFormat.SetNbComponents (4);
124 aFormat.SetInternalFormat (GL_RGBA8);
125 aFormat.SetPixelFormat (GL_RGBA);
126 aFormat.SetDataType (GL_UNSIGNED_BYTE);
128 && theCtx->ToRenderSRGB())
130 aFormat.SetInternalFormat (GL_SRGB8_ALPHA8);
134 case Image_Format_BGRA:
136 #if !defined(GL_ES_VERSION_2_0)
137 if (!theCtx->IsGlGreaterEqual (1, 2)
140 return OpenGl_TextureFormat();
142 aFormat.SetNbComponents (4);
143 aFormat.SetInternalFormat (GL_RGBA8);
145 && theCtx->ToRenderSRGB())
147 aFormat.SetInternalFormat (GL_SRGB8_ALPHA8);
151 && theCtx->ToRenderSRGB())
153 // GL_SRGB8_ALPHA8 with texture swizzling would be better
155 if (!theCtx->extBgra)
157 return OpenGl_TextureFormat();
159 aFormat.SetNbComponents (4);
160 aFormat.SetInternalFormat (GL_BGRA_EXT);
162 aFormat.SetPixelFormat (GL_BGRA_EXT); // equals to GL_BGRA
163 aFormat.SetDataType (GL_UNSIGNED_BYTE);
166 case Image_Format_RGB32:
168 #if !defined(GL_ES_VERSION_2_0)
169 // ask driver to convert data to RGB8 to save memory
170 aFormat.SetNbComponents (3);
171 aFormat.SetInternalFormat (GL_RGB8);
173 && theCtx->ToRenderSRGB())
175 aFormat.SetInternalFormat (GL_SRGB8);
178 // conversion is not supported
179 aFormat.SetNbComponents (4);
180 aFormat.SetInternalFormat (GL_RGBA8);
182 && theCtx->ToRenderSRGB())
184 aFormat.SetInternalFormat (GL_SRGB8_ALPHA8);
187 aFormat.SetPixelFormat (GL_RGBA);
188 aFormat.SetDataType (GL_UNSIGNED_BYTE);
191 case Image_Format_BGR32:
193 #if !defined(GL_ES_VERSION_2_0)
194 if (!theCtx->IsGlGreaterEqual(1, 2) && !theCtx->extBgra)
196 return OpenGl_TextureFormat();
198 aFormat.SetNbComponents (3);
199 aFormat.SetInternalFormat (GL_RGB8);
201 && theCtx->ToRenderSRGB())
203 aFormat.SetInternalFormat (GL_SRGB8);
207 && theCtx->ToRenderSRGB())
209 // GL_SRGB8_ALPHA8 with texture swizzling would be better
211 if (!theCtx->extBgra)
213 return OpenGl_TextureFormat();
215 aFormat.SetNbComponents (4);
216 aFormat.SetInternalFormat (GL_BGRA_EXT);
218 aFormat.SetPixelFormat (GL_BGRA_EXT); // equals to GL_BGRA
219 aFormat.SetDataType (GL_UNSIGNED_BYTE);
222 case Image_Format_RGB:
224 aFormat.SetNbComponents (3);
225 aFormat.SetInternalFormat (GL_RGB8);
226 aFormat.SetPixelFormat (GL_RGB);
227 aFormat.SetDataType (GL_UNSIGNED_BYTE);
229 && theCtx->ToRenderSRGB())
231 aFormat.SetInternalFormat (GL_SRGB8);
235 case Image_Format_BGR:
237 #if !defined(GL_ES_VERSION_2_0)
238 if (!theCtx->IsGlGreaterEqual (1, 2)
241 return OpenGl_TextureFormat();
243 aFormat.SetNbComponents (3);
244 aFormat.SetInternalFormat (GL_RGB8);
246 && theCtx->ToRenderSRGB())
248 aFormat.SetInternalFormat (GL_SRGB8);
250 aFormat.SetPixelFormat (GL_BGR); // equals to GL_BGR_EXT
251 aFormat.SetDataType (GL_UNSIGNED_BYTE);
255 case Image_Format_Gray:
257 aFormat.SetNbComponents (1);
258 if (theCtx->core11 == NULL)
260 aFormat.SetInternalFormat (GL_R8);
261 aFormat.SetPixelFormat (GL_RED);
265 #if !defined(GL_ES_VERSION_2_0)
266 aFormat.SetInternalFormat (GL_LUMINANCE8);
268 aFormat.SetInternalFormat (GL_LUMINANCE);
270 aFormat.SetPixelFormat (GL_LUMINANCE);
272 aFormat.SetDataType (GL_UNSIGNED_BYTE);
275 case Image_Format_Alpha:
277 aFormat.SetNbComponents (1);
278 if (theCtx->core11 == NULL)
280 aFormat.SetInternalFormat (GL_R8);
281 aFormat.SetPixelFormat (GL_RED);
285 #if !defined(GL_ES_VERSION_2_0)
286 aFormat.SetInternalFormat (GL_ALPHA8);
288 aFormat.SetInternalFormat (GL_ALPHA);
290 aFormat.SetPixelFormat (GL_ALPHA);
292 aFormat.SetDataType (GL_UNSIGNED_BYTE);
295 case Image_Format_UNKNOWN:
297 return OpenGl_TextureFormat();
300 return OpenGl_TextureFormat();
303 // =======================================================================
304 // function : FindSizedFormat
306 // =======================================================================
307 OpenGl_TextureFormat OpenGl_TextureFormat::FindSizedFormat (const Handle(OpenGl_Context)& theCtx,
308 GLint theSizedFormat)
310 OpenGl_TextureFormat aFormat;
311 switch (theSizedFormat)
315 aFormat.SetNbComponents (4);
316 aFormat.SetInternalFormat (theSizedFormat);
317 aFormat.SetPixelFormat (GL_RGBA);
318 aFormat.SetDataType (GL_FLOAT);
323 aFormat.SetNbComponents (1);
324 aFormat.SetInternalFormat (theSizedFormat);
325 aFormat.SetPixelFormat (GL_RED);
326 aFormat.SetDataType (GL_FLOAT);
331 aFormat.SetNbComponents (4);
332 aFormat.SetInternalFormat (theSizedFormat);
333 aFormat.SetPixelFormat (GL_RGBA);
334 aFormat.SetDataType (GL_HALF_FLOAT);
335 if (theCtx->hasHalfFloatBuffer == OpenGl_FeatureInExtensions)
337 #if defined(GL_ES_VERSION_2_0)
338 aFormat.SetDataType (GL_HALF_FLOAT_OES);
340 aFormat.SetDataType (GL_FLOAT);
347 aFormat.SetNbComponents (1);
348 aFormat.SetInternalFormat (theSizedFormat);
349 aFormat.SetPixelFormat (GL_RED);
350 aFormat.SetDataType (GL_HALF_FLOAT);
351 if (theCtx->hasHalfFloatBuffer == OpenGl_FeatureInExtensions)
353 #if defined(GL_ES_VERSION_2_0)
354 aFormat.SetDataType (GL_HALF_FLOAT_OES);
356 aFormat.SetDataType (GL_FLOAT);
361 case GL_SRGB8_ALPHA8:
365 aFormat.SetNbComponents (4);
366 aFormat.SetInternalFormat (theSizedFormat);
367 aFormat.SetPixelFormat (GL_RGBA);
368 aFormat.SetDataType (GL_UNSIGNED_BYTE);
369 if (theSizedFormat == GL_SRGB8_ALPHA8
370 && !theCtx->ToRenderSRGB())
372 aFormat.SetInternalFormat (GL_RGBA8); // fallback format
380 aFormat.SetNbComponents (3);
381 aFormat.SetInternalFormat (theSizedFormat);
382 aFormat.SetPixelFormat (GL_RGB);
383 aFormat.SetDataType (GL_UNSIGNED_BYTE);
384 if (theSizedFormat == GL_SRGB8
385 && !theCtx->ToRenderSRGB())
387 aFormat.SetInternalFormat (GL_RGB8); // fallback format
394 aFormat.SetNbComponents (1);
395 aFormat.SetInternalFormat (theSizedFormat);
396 aFormat.SetPixelFormat (GL_RED_INTEGER);
397 aFormat.SetDataType (GL_INT);
402 aFormat.SetNbComponents (2);
403 aFormat.SetInternalFormat (theSizedFormat);
404 aFormat.SetPixelFormat (GL_RG_INTEGER);
405 aFormat.SetDataType (GL_INT);
409 case GL_DEPTH24_STENCIL8:
411 aFormat.SetNbComponents (2);
412 aFormat.SetInternalFormat (theSizedFormat);
413 aFormat.SetPixelFormat (GL_DEPTH_STENCIL);
414 aFormat.SetDataType (GL_UNSIGNED_INT_24_8);
417 case GL_DEPTH32F_STENCIL8:
419 aFormat.SetNbComponents (2);
420 aFormat.SetInternalFormat (theSizedFormat);
421 aFormat.SetPixelFormat (GL_DEPTH_STENCIL);
422 aFormat.SetDataType (GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
425 case GL_DEPTH_COMPONENT16:
427 aFormat.SetNbComponents (1);
428 aFormat.SetInternalFormat (theSizedFormat);
429 aFormat.SetPixelFormat (GL_DEPTH_COMPONENT);
430 aFormat.SetDataType (GL_UNSIGNED_SHORT);
433 case GL_DEPTH_COMPONENT24:
435 aFormat.SetNbComponents (1);
436 aFormat.SetInternalFormat (theSizedFormat);
437 aFormat.SetPixelFormat (GL_DEPTH_COMPONENT);
438 aFormat.SetDataType (GL_UNSIGNED_INT);
441 case GL_DEPTH_COMPONENT32F:
443 aFormat.SetNbComponents (1);
444 aFormat.SetInternalFormat (theSizedFormat);
445 aFormat.SetPixelFormat (GL_DEPTH_COMPONENT);
446 aFormat.SetDataType (GL_FLOAT);