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 (GL_R8); // GL_R32F
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 (GL_R8); // GL_R32F
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_RGBAF:
71 aFormat.SetNbComponents (4);
72 aFormat.SetInternalFormat (GL_RGBA8); // GL_RGBA32F
73 aFormat.SetPixelFormat (GL_RGBA);
74 aFormat.SetDataType (GL_FLOAT);
77 case Image_Format_BGRAF:
79 if (!theCtx->IsGlGreaterEqual (1, 2) && !theCtx->extBgra)
81 return OpenGl_TextureFormat();
83 aFormat.SetNbComponents (4);
84 aFormat.SetInternalFormat (GL_RGBA8); // GL_RGBA32F
85 aFormat.SetPixelFormat (GL_BGRA_EXT); // equals to GL_BGRA
86 aFormat.SetDataType (GL_FLOAT);
89 case Image_Format_RGBF:
91 aFormat.SetNbComponents (3);
92 aFormat.SetInternalFormat (GL_RGB8); // GL_RGB32F
93 aFormat.SetPixelFormat (GL_RGB);
94 aFormat.SetDataType (GL_FLOAT);
97 case Image_Format_BGRF:
99 #if !defined(GL_ES_VERSION_2_0)
100 aFormat.SetNbComponents (3);
101 aFormat.SetInternalFormat (GL_RGB8); // GL_RGB32F
102 aFormat.SetPixelFormat (GL_BGR); // equals to GL_BGR_EXT
103 aFormat.SetDataType (GL_FLOAT);
106 return OpenGl_TextureFormat();
109 case Image_Format_RGBA:
111 aFormat.SetNbComponents (4);
112 aFormat.SetInternalFormat (GL_RGBA8);
113 aFormat.SetPixelFormat (GL_RGBA);
114 aFormat.SetDataType (GL_UNSIGNED_BYTE);
116 && theCtx->ToRenderSRGB())
118 aFormat.SetInternalFormat (GL_SRGB8_ALPHA8);
122 case Image_Format_BGRA:
124 #if !defined(GL_ES_VERSION_2_0)
125 if (!theCtx->IsGlGreaterEqual (1, 2)
128 return OpenGl_TextureFormat();
130 aFormat.SetNbComponents (4);
131 aFormat.SetInternalFormat (GL_RGBA8);
133 && theCtx->ToRenderSRGB())
135 aFormat.SetInternalFormat (GL_SRGB8_ALPHA8);
139 && theCtx->ToRenderSRGB())
141 // GL_SRGB8_ALPHA8 with texture swizzling would be better
143 if (!theCtx->extBgra)
145 return OpenGl_TextureFormat();
147 aFormat.SetNbComponents (4);
148 aFormat.SetInternalFormat (GL_BGRA_EXT);
150 aFormat.SetPixelFormat (GL_BGRA_EXT); // equals to GL_BGRA
151 aFormat.SetDataType (GL_UNSIGNED_BYTE);
154 case Image_Format_RGB32:
156 #if !defined(GL_ES_VERSION_2_0)
157 // ask driver to convert data to RGB8 to save memory
158 aFormat.SetNbComponents (3);
159 aFormat.SetInternalFormat (GL_RGB8);
161 && theCtx->ToRenderSRGB())
163 aFormat.SetInternalFormat (GL_SRGB8);
166 // conversion is not supported
167 aFormat.SetNbComponents (4);
168 aFormat.SetInternalFormat (GL_RGBA8);
170 && theCtx->ToRenderSRGB())
172 aFormat.SetInternalFormat (GL_SRGB8_ALPHA8);
175 aFormat.SetPixelFormat (GL_RGBA);
176 aFormat.SetDataType (GL_UNSIGNED_BYTE);
179 case Image_Format_BGR32:
181 #if !defined(GL_ES_VERSION_2_0)
182 if (!theCtx->IsGlGreaterEqual(1, 2) && !theCtx->extBgra)
184 return OpenGl_TextureFormat();
186 aFormat.SetNbComponents (3);
187 aFormat.SetInternalFormat (GL_RGB8);
189 && theCtx->ToRenderSRGB())
191 aFormat.SetInternalFormat (GL_SRGB8);
195 && theCtx->ToRenderSRGB())
197 // GL_SRGB8_ALPHA8 with texture swizzling would be better
199 if (!theCtx->extBgra)
201 return OpenGl_TextureFormat();
203 aFormat.SetNbComponents (4);
204 aFormat.SetInternalFormat (GL_BGRA_EXT);
206 aFormat.SetPixelFormat (GL_BGRA_EXT); // equals to GL_BGRA
207 aFormat.SetDataType (GL_UNSIGNED_BYTE);
210 case Image_Format_RGB:
212 aFormat.SetNbComponents (3);
213 aFormat.SetInternalFormat (GL_RGB8);
214 aFormat.SetPixelFormat (GL_RGB);
215 aFormat.SetDataType (GL_UNSIGNED_BYTE);
217 && theCtx->ToRenderSRGB())
219 aFormat.SetInternalFormat (GL_SRGB8);
223 case Image_Format_BGR:
225 #if !defined(GL_ES_VERSION_2_0)
226 if (!theCtx->IsGlGreaterEqual (1, 2)
229 return OpenGl_TextureFormat();
231 aFormat.SetNbComponents (3);
232 aFormat.SetInternalFormat (GL_RGB8);
234 && theCtx->ToRenderSRGB())
236 aFormat.SetInternalFormat (GL_SRGB8);
238 aFormat.SetPixelFormat (GL_BGR); // equals to GL_BGR_EXT
239 aFormat.SetDataType (GL_UNSIGNED_BYTE);
243 case Image_Format_Gray:
245 aFormat.SetNbComponents (1);
246 if (theCtx->core11 == NULL)
248 aFormat.SetInternalFormat (GL_R8);
249 aFormat.SetPixelFormat (GL_RED);
253 #if !defined(GL_ES_VERSION_2_0)
254 aFormat.SetInternalFormat (GL_LUMINANCE8);
256 aFormat.SetInternalFormat (GL_LUMINANCE);
258 aFormat.SetPixelFormat (GL_LUMINANCE);
260 aFormat.SetDataType (GL_UNSIGNED_BYTE);
263 case Image_Format_Alpha:
265 aFormat.SetNbComponents (1);
266 if (theCtx->core11 == NULL)
268 aFormat.SetInternalFormat (GL_R8);
269 aFormat.SetPixelFormat (GL_RED);
273 #if !defined(GL_ES_VERSION_2_0)
274 aFormat.SetInternalFormat (GL_ALPHA8);
276 aFormat.SetInternalFormat (GL_ALPHA);
278 aFormat.SetPixelFormat (GL_ALPHA);
280 aFormat.SetDataType (GL_UNSIGNED_BYTE);
283 case Image_Format_UNKNOWN:
285 return OpenGl_TextureFormat();
288 return OpenGl_TextureFormat();
291 // =======================================================================
292 // function : FindSizedFormat
294 // =======================================================================
295 OpenGl_TextureFormat OpenGl_TextureFormat::FindSizedFormat (const Handle(OpenGl_Context)& theCtx,
296 GLint theSizedFormat)
298 OpenGl_TextureFormat aFormat;
299 switch (theSizedFormat)
303 aFormat.SetNbComponents (4);
304 aFormat.SetInternalFormat (theSizedFormat);
305 aFormat.SetPixelFormat (GL_RGBA);
306 aFormat.SetDataType (GL_FLOAT);
311 aFormat.SetNbComponents (1);
312 aFormat.SetInternalFormat (theSizedFormat);
313 aFormat.SetPixelFormat (GL_RED);
314 aFormat.SetDataType (GL_FLOAT);
319 aFormat.SetNbComponents (4);
320 aFormat.SetInternalFormat (theSizedFormat);
321 aFormat.SetPixelFormat (GL_RGBA);
322 aFormat.SetDataType (GL_HALF_FLOAT);
323 if (theCtx->hasHalfFloatBuffer == OpenGl_FeatureInExtensions)
325 #if defined(GL_ES_VERSION_2_0)
326 aFormat.SetDataType (GL_HALF_FLOAT_OES);
328 aFormat.SetDataType (GL_FLOAT);
335 aFormat.SetNbComponents (1);
336 aFormat.SetInternalFormat (theSizedFormat);
337 aFormat.SetPixelFormat (GL_RED);
338 aFormat.SetDataType (GL_HALF_FLOAT);
339 if (theCtx->hasHalfFloatBuffer == OpenGl_FeatureInExtensions)
341 #if defined(GL_ES_VERSION_2_0)
342 aFormat.SetDataType (GL_HALF_FLOAT_OES);
344 aFormat.SetDataType (GL_FLOAT);
349 case GL_SRGB8_ALPHA8:
353 aFormat.SetNbComponents (4);
354 aFormat.SetInternalFormat (theSizedFormat);
355 aFormat.SetPixelFormat (GL_RGBA);
356 aFormat.SetDataType (GL_UNSIGNED_BYTE);
357 if (theSizedFormat == GL_SRGB8_ALPHA8
358 && !theCtx->ToRenderSRGB())
360 aFormat.SetInternalFormat (GL_RGBA8); // fallback format
368 aFormat.SetNbComponents (3);
369 aFormat.SetInternalFormat (theSizedFormat);
370 aFormat.SetPixelFormat (GL_RGB);
371 aFormat.SetDataType (GL_UNSIGNED_BYTE);
372 if (theSizedFormat == GL_SRGB8
373 && !theCtx->ToRenderSRGB())
375 aFormat.SetInternalFormat (GL_RGB8); // fallback format
382 aFormat.SetNbComponents (1);
383 aFormat.SetInternalFormat (theSizedFormat);
384 aFormat.SetPixelFormat (GL_RED_INTEGER);
385 aFormat.SetDataType (GL_INT);
390 aFormat.SetNbComponents (2);
391 aFormat.SetInternalFormat (theSizedFormat);
392 aFormat.SetPixelFormat (GL_RG_INTEGER);
393 aFormat.SetDataType (GL_INT);
397 case GL_DEPTH24_STENCIL8:
399 aFormat.SetNbComponents (2);
400 aFormat.SetInternalFormat (theSizedFormat);
401 aFormat.SetPixelFormat (GL_DEPTH_STENCIL);
402 aFormat.SetDataType (GL_UNSIGNED_INT_24_8);
405 case GL_DEPTH32F_STENCIL8:
407 aFormat.SetNbComponents (2);
408 aFormat.SetInternalFormat (theSizedFormat);
409 aFormat.SetPixelFormat (GL_DEPTH_STENCIL);
410 aFormat.SetDataType (GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
413 case GL_DEPTH_COMPONENT16:
415 aFormat.SetNbComponents (1);
416 aFormat.SetInternalFormat (theSizedFormat);
417 aFormat.SetPixelFormat (GL_DEPTH_COMPONENT);
418 aFormat.SetDataType (GL_UNSIGNED_SHORT);
421 case GL_DEPTH_COMPONENT24:
423 aFormat.SetNbComponents (1);
424 aFormat.SetInternalFormat (theSizedFormat);
425 aFormat.SetPixelFormat (GL_DEPTH_COMPONENT);
426 aFormat.SetDataType (GL_UNSIGNED_INT);
429 case GL_DEPTH_COMPONENT32F:
431 aFormat.SetNbComponents (1);
432 aFormat.SetInternalFormat (theSizedFormat);
433 aFormat.SetPixelFormat (GL_DEPTH_COMPONENT);
434 aFormat.SetDataType (GL_FLOAT);