ba00aab7 |
1 | // Copyright (c) 2017-2019 OPEN CASCADE SAS |
2 | // |
3 | // This file is part of Open CASCADE Technology software library. |
4 | // |
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. |
10 | // |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
13 | |
14 | #include <OpenGl_TextureFormat.hxx> |
15 | |
faff3767 |
16 | #include <Image_SupportedFormats.hxx> |
ba00aab7 |
17 | #include <OpenGl_Context.hxx> |
18 | |
a46ab511 |
19 | // ======================================================================= |
20 | // function : FormatFormat |
21 | // purpose : |
22 | // ======================================================================= |
23 | TCollection_AsciiString OpenGl_TextureFormat::FormatFormat (GLint theInternalFormat) |
24 | { |
25 | switch (theInternalFormat) |
26 | { |
27 | // RED variations (GL_RED, OpenGL 3.0+) |
28 | case GL_RED: return "GL_RED"; |
29 | case GL_R8: return "GL_R8"; |
30 | case 0x822A: return "GL_R16"; |
31 | case GL_R16F: return "GL_R16F"; // half-float |
32 | case GL_R32F: return "GL_R32F"; // float |
33 | case GL_R32I: return "GL_R32I"; |
34 | case GL_RED_INTEGER: return "GL_RED_INTEGER"; |
35 | // |
36 | case GL_RG: return "GL_RG"; |
37 | case GL_RG8: return "GL_RG8"; |
38 | case 0x822C: return "GL_RG16"; |
39 | case GL_RG16F: return "GL_RG16F"; |
40 | case GL_RG32F: return "GL_RG32F"; |
41 | case GL_RG32I: return "GL_RG32I"; |
42 | case GL_RG_INTEGER: return "GL_RG_INTEGER"; |
43 | // RGB variations |
44 | case GL_RGB: return "GL_RGB"; |
45 | case 0x804F: return "GL_RGB4"; |
46 | case 0x8050: return "GL_RGB5"; |
47 | case GL_RGB8: return "GL_RGB8"; |
48 | case GL_SRGB8: return "GL_SRGB8"; |
eee6a80a |
49 | case GL_SRGB_EXT: return "GL_SRGB_EXT"; |
a46ab511 |
50 | case 0x8052: return "GL_RGB10"; |
51 | case 0x8053: return "GL_RGB12"; |
52 | case 0x8054: return "GL_RGB16"; |
53 | case GL_RGB16F: return "GL_RGB16F"; // half-float |
54 | case GL_RGB32F: return "GL_RGB32F"; // float |
55 | case GL_RGB32I: return "GL_RGB32I"; |
56 | // RGBA variations |
57 | case GL_RGBA: return "GL_RGBA"; |
58 | case GL_RGBA8: return "GL_RGBA8"; |
eee6a80a |
59 | case GL_SRGB8_ALPHA8: return "GL_SRGB8_ALPHA8"; |
60 | case GL_SRGB_ALPHA_EXT: return "GL_SRGB_ALPHA_EXT"; |
a46ab511 |
61 | case GL_RGB10_A2: return "GL_RGB10_A2"; |
62 | case 0x805A: return "GL_RGBA12"; |
63 | case 0x805B: return "GL_RGBA16"; |
64 | case GL_RGBA16F: return "GL_RGBA16F"; // half-float |
65 | case GL_RGBA32F: return "GL_RGBA32F"; // float |
66 | case GL_RGBA32I: return "GL_RGBA32I"; |
67 | // |
68 | case 0x80E0: return "GL_BGR"; |
69 | case GL_BGRA_EXT: return "GL_BGRA"; |
70 | // ALPHA variations (deprecated) |
71 | case GL_ALPHA: return "GL_ALPHA"; |
72 | case 0x803C: return "GL_ALPHA8"; |
73 | case 0x803E: return "GL_ALPHA16"; |
74 | case GL_LUMINANCE: return "GL_LUMINANCE"; |
b9a372bb |
75 | case GL_LUMINANCE16: return "GL_LUMINANCE16"; |
a46ab511 |
76 | case GL_LUMINANCE_ALPHA: return "GL_LUMINANCE_ALPHA"; |
77 | // |
78 | case GL_DEPTH_COMPONENT: return "GL_DEPTH_COMPONENT"; |
79 | case GL_DEPTH_COMPONENT16: return "GL_DEPTH_COMPONENT16"; |
80 | case GL_DEPTH_COMPONENT24: return "GL_DEPTH_COMPONENT24"; |
81 | case GL_DEPTH_COMPONENT32F: return "GL_DEPTH_COMPONENT32F"; |
82 | case GL_DEPTH_STENCIL: return "GL_DEPTH_STENCIL"; |
83 | case GL_DEPTH24_STENCIL8: return "GL_DEPTH24_STENCIL8"; |
84 | case GL_DEPTH32F_STENCIL8: return "GL_DEPTH32F_STENCIL8"; |
85 | // |
86 | case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT"; |
87 | case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT: return "GL_COMPRESSED_SRGB_S3TC_DXT1_EXT"; |
88 | case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT"; |
89 | case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT"; |
90 | case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT"; |
91 | case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT"; |
92 | case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT"; |
93 | case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT"; |
94 | } |
95 | return OpenGl_Context::FormatGlEnumHex (theInternalFormat); |
96 | } |
97 | |
98 | // ======================================================================= |
99 | // function : FormatDataType |
100 | // purpose : |
101 | // ======================================================================= |
102 | TCollection_AsciiString OpenGl_TextureFormat::FormatDataType (GLint theDataType) |
103 | { |
104 | switch (theDataType) |
105 | { |
106 | case GL_UNSIGNED_BYTE: return "GL_UNSIGNED_BYTE"; |
107 | case GL_UNSIGNED_SHORT: return "GL_UNSIGNED_SHORT"; |
108 | case GL_INT: return "GL_INT"; |
109 | case GL_UNSIGNED_INT: return "GL_UNSIGNED_INT"; |
110 | case GL_FLOAT: return "GL_FLOAT"; |
111 | case GL_HALF_FLOAT: return "GL_HALF_FLOAT"; |
112 | case 0x8D61: return "GL_HALF_FLOAT_OES"; |
113 | case GL_UNSIGNED_INT_24_8: return "GL_UNSIGNED_INT_24_8"; |
ddb9ed48 |
114 | case GL_UNSIGNED_INT_2_10_10_10_REV: return "GL_UNSIGNED_INT_2_10_10_10_REV"; |
a46ab511 |
115 | case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: return "GL_FLOAT_32_UNSIGNED_INT_24_8_REV"; |
116 | } |
117 | return OpenGl_Context::FormatGlEnumHex (theDataType); |
118 | } |
119 | |
ba00aab7 |
120 | // ======================================================================= |
121 | // function : FindFormat |
122 | // purpose : |
123 | // ======================================================================= |
124 | OpenGl_TextureFormat OpenGl_TextureFormat::FindFormat (const Handle(OpenGl_Context)& theCtx, |
125 | Image_Format theFormat, |
126 | bool theIsColorMap) |
127 | { |
128 | OpenGl_TextureFormat aFormat; |
8f8fe4a9 |
129 | aFormat.SetImageFormat (theFormat); |
8f7159cb |
130 | const bool useRedRedAlpha = theCtx->GraphicsLibrary() != Aspect_GraphicsLibrary_OpenGLES |
131 | && theCtx->core11ffp == NULL; |
ba00aab7 |
132 | switch (theFormat) |
133 | { |
134 | case Image_Format_GrayF: |
135 | { |
136 | aFormat.SetNbComponents (1); |
92996979 |
137 | if (useRedRedAlpha) |
ba00aab7 |
138 | { |
67312b79 |
139 | aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_R32F : GL_R8); |
ba00aab7 |
140 | aFormat.SetPixelFormat (GL_RED); |
141 | } |
142 | else |
143 | { |
8f7159cb |
144 | aFormat.SetInternalFormat (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
145 | ? GL_LUMINANCE |
146 | : GL_LUMINANCE8); |
ba00aab7 |
147 | aFormat.SetPixelFormat (GL_LUMINANCE); |
148 | } |
149 | aFormat.SetDataType (GL_FLOAT); |
150 | return aFormat; |
151 | } |
152 | case Image_Format_AlphaF: |
153 | { |
154 | aFormat.SetNbComponents (1); |
92996979 |
155 | if (useRedRedAlpha) |
ba00aab7 |
156 | { |
67312b79 |
157 | aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_R32F : GL_R8); |
ba00aab7 |
158 | aFormat.SetPixelFormat (GL_RED); |
159 | } |
160 | else |
161 | { |
8f7159cb |
162 | aFormat.SetInternalFormat (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
163 | ? GL_ALPHA |
164 | : GL_ALPHA8); |
ba00aab7 |
165 | aFormat.SetPixelFormat (GL_ALPHA); |
166 | } |
167 | aFormat.SetDataType (GL_FLOAT); |
168 | return aFormat; |
169 | } |
67312b79 |
170 | case Image_Format_RGF: |
171 | { |
172 | if (!theCtx->arbTexRG) |
173 | { |
174 | return OpenGl_TextureFormat(); |
175 | } |
176 | aFormat.SetNbComponents (2); |
177 | aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_RG32F : GL_RG8); |
178 | aFormat.SetPixelFormat (GL_RG); |
179 | aFormat.SetDataType (GL_FLOAT); |
180 | return aFormat; |
181 | } |
ba00aab7 |
182 | case Image_Format_RGBAF: |
183 | { |
184 | aFormat.SetNbComponents (4); |
67312b79 |
185 | aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_RGBA32F : GL_RGBA8); |
ba00aab7 |
186 | aFormat.SetPixelFormat (GL_RGBA); |
187 | aFormat.SetDataType (GL_FLOAT); |
188 | return aFormat; |
189 | } |
190 | case Image_Format_BGRAF: |
191 | { |
192 | if (!theCtx->IsGlGreaterEqual (1, 2) && !theCtx->extBgra) |
193 | { |
194 | return OpenGl_TextureFormat(); |
195 | } |
196 | aFormat.SetNbComponents (4); |
67312b79 |
197 | aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_RGBA32F : GL_RGBA8); |
ba00aab7 |
198 | aFormat.SetPixelFormat (GL_BGRA_EXT); // equals to GL_BGRA |
199 | aFormat.SetDataType (GL_FLOAT); |
200 | return aFormat; |
201 | } |
202 | case Image_Format_RGBF: |
203 | { |
204 | aFormat.SetNbComponents (3); |
67312b79 |
205 | aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_RGB32F : GL_RGB8); |
ba00aab7 |
206 | aFormat.SetPixelFormat (GL_RGB); |
207 | aFormat.SetDataType (GL_FLOAT); |
208 | return aFormat; |
209 | } |
210 | case Image_Format_BGRF: |
211 | { |
8f7159cb |
212 | if (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES) |
213 | { |
214 | return OpenGl_TextureFormat(); |
215 | } |
216 | |
ba00aab7 |
217 | aFormat.SetNbComponents (3); |
67312b79 |
218 | aFormat.SetInternalFormat (theCtx->arbTexFloat ? GL_RGB32F : GL_RGB8); |
ba00aab7 |
219 | aFormat.SetPixelFormat (GL_BGR); // equals to GL_BGR_EXT |
220 | aFormat.SetDataType (GL_FLOAT); |
221 | return aFormat; |
ba00aab7 |
222 | } |
b9a372bb |
223 | case Image_Format_GrayF_half: |
224 | { |
225 | aFormat.SetNbComponents (1); |
226 | aFormat.SetInternalFormat (GL_R16F); |
227 | aFormat.SetPixelFormat (GL_RED); |
228 | aFormat.SetDataType (GL_HALF_FLOAT); |
229 | if (theCtx->hasHalfFloatBuffer == OpenGl_FeatureInExtensions |
230 | && theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES) |
231 | { |
232 | aFormat.SetDataType (GL_HALF_FLOAT_OES); |
233 | } |
234 | return aFormat; |
235 | } |
1ac837b2 |
236 | case Image_Format_RGF_half: |
237 | { |
238 | aFormat.SetNbComponents (2); |
239 | aFormat.SetInternalFormat (GL_RG16F); |
240 | aFormat.SetPixelFormat (GL_RG); |
241 | aFormat.SetDataType (GL_HALF_FLOAT); |
8f7159cb |
242 | if (theCtx->hasHalfFloatBuffer == OpenGl_FeatureInExtensions |
243 | && theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES) |
1ac837b2 |
244 | { |
1ac837b2 |
245 | aFormat.SetDataType (GL_HALF_FLOAT_OES); |
1ac837b2 |
246 | } |
247 | return aFormat; |
248 | } |
249 | case Image_Format_RGBAF_half: |
250 | { |
251 | aFormat.SetNbComponents (4); |
252 | aFormat.SetInternalFormat (GL_RGBA16F); |
253 | aFormat.SetPixelFormat (GL_RGBA); |
254 | aFormat.SetDataType (GL_HALF_FLOAT); |
8f7159cb |
255 | if (theCtx->hasHalfFloatBuffer == OpenGl_FeatureInExtensions |
256 | && theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES) |
1ac837b2 |
257 | { |
1ac837b2 |
258 | aFormat.SetDataType (GL_HALF_FLOAT_OES); |
1ac837b2 |
259 | } |
260 | return aFormat; |
261 | } |
ba00aab7 |
262 | case Image_Format_RGBA: |
263 | { |
264 | aFormat.SetNbComponents (4); |
265 | aFormat.SetInternalFormat (GL_RGBA8); |
266 | aFormat.SetPixelFormat (GL_RGBA); |
267 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
268 | if (theIsColorMap |
269 | && theCtx->ToRenderSRGB()) |
270 | { |
8f7159cb |
271 | if (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
272 | && !theCtx->IsGlGreaterEqual (3, 0)) |
eee6a80a |
273 | { |
274 | aFormat.SetPixelFormat (GL_SRGB_ALPHA_EXT); |
275 | } |
ba00aab7 |
276 | aFormat.SetInternalFormat (GL_SRGB8_ALPHA8); |
277 | } |
278 | return aFormat; |
279 | } |
280 | case Image_Format_BGRA: |
281 | { |
8f7159cb |
282 | if (theCtx->GraphicsLibrary() != Aspect_GraphicsLibrary_OpenGLES) |
ba00aab7 |
283 | { |
8f7159cb |
284 | if (!theCtx->IsGlGreaterEqual (1, 2) |
285 | && !theCtx->extBgra) |
286 | { |
287 | return OpenGl_TextureFormat(); |
288 | } |
289 | aFormat.SetNbComponents (4); |
290 | aFormat.SetInternalFormat (GL_RGBA8); |
291 | if (theIsColorMap |
292 | && theCtx->ToRenderSRGB()) |
293 | { |
294 | aFormat.SetInternalFormat (GL_SRGB8_ALPHA8); |
295 | } |
ba00aab7 |
296 | } |
8f7159cb |
297 | else |
ba00aab7 |
298 | { |
8f7159cb |
299 | if (theIsColorMap |
300 | && theCtx->ToRenderSRGB()) |
301 | { |
302 | // GL_SRGB8_ALPHA8 with texture swizzling would be better |
303 | } |
304 | if (!theCtx->extBgra) |
305 | { |
306 | return OpenGl_TextureFormat(); |
307 | } |
308 | aFormat.SetNbComponents (4); |
309 | aFormat.SetInternalFormat (GL_BGRA_EXT); |
ba00aab7 |
310 | } |
ba00aab7 |
311 | aFormat.SetPixelFormat (GL_BGRA_EXT); // equals to GL_BGRA |
312 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
313 | return aFormat; |
314 | } |
315 | case Image_Format_RGB32: |
316 | { |
8f7159cb |
317 | if (theCtx->GraphicsLibrary() != Aspect_GraphicsLibrary_OpenGLES) |
ba00aab7 |
318 | { |
8f7159cb |
319 | // ask driver to convert data to RGB8 to save memory |
320 | aFormat.SetNbComponents (3); |
321 | aFormat.SetInternalFormat (GL_RGB8); |
322 | aFormat.SetPixelFormat (GL_RGBA); |
323 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
324 | if (theIsColorMap |
325 | && theCtx->ToRenderSRGB()) |
326 | { |
327 | aFormat.SetInternalFormat (GL_SRGB8); |
328 | } |
ba00aab7 |
329 | } |
8f7159cb |
330 | else |
ba00aab7 |
331 | { |
8f7159cb |
332 | // conversion is not supported |
333 | aFormat.SetNbComponents (4); |
334 | aFormat.SetInternalFormat (GL_RGBA8); |
335 | aFormat.SetPixelFormat (GL_RGBA); |
336 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
337 | if (theIsColorMap |
338 | && theCtx->ToRenderSRGB()) |
eee6a80a |
339 | { |
8f7159cb |
340 | if (!theCtx->IsGlGreaterEqual (3, 0)) |
341 | { |
342 | aFormat.SetPixelFormat (GL_SRGB_ALPHA_EXT); |
343 | } |
344 | aFormat.SetInternalFormat (GL_SRGB8_ALPHA8); |
eee6a80a |
345 | } |
ba00aab7 |
346 | } |
ba00aab7 |
347 | return aFormat; |
348 | } |
349 | case Image_Format_BGR32: |
350 | { |
8f7159cb |
351 | if (theCtx->GraphicsLibrary() != Aspect_GraphicsLibrary_OpenGLES) |
ba00aab7 |
352 | { |
8f7159cb |
353 | if (!theCtx->IsGlGreaterEqual(1, 2) && !theCtx->extBgra) |
354 | { |
355 | return OpenGl_TextureFormat(); |
356 | } |
357 | aFormat.SetNbComponents (3); |
358 | aFormat.SetInternalFormat (GL_RGB8); |
359 | if (theIsColorMap |
360 | && theCtx->ToRenderSRGB()) |
361 | { |
362 | aFormat.SetInternalFormat (GL_SRGB8); |
363 | } |
ba00aab7 |
364 | } |
8f7159cb |
365 | else |
ba00aab7 |
366 | { |
8f7159cb |
367 | if (theIsColorMap |
368 | && theCtx->ToRenderSRGB()) |
369 | { |
370 | // GL_SRGB8_ALPHA8 with texture swizzling would be better |
371 | } |
372 | if (!theCtx->extBgra) |
373 | { |
374 | return OpenGl_TextureFormat(); |
375 | } |
376 | aFormat.SetNbComponents (4); |
377 | aFormat.SetInternalFormat (GL_BGRA_EXT); |
ba00aab7 |
378 | } |
ba00aab7 |
379 | aFormat.SetPixelFormat (GL_BGRA_EXT); // equals to GL_BGRA |
380 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
381 | return aFormat; |
382 | } |
383 | case Image_Format_RGB: |
384 | { |
385 | aFormat.SetNbComponents (3); |
386 | aFormat.SetInternalFormat (GL_RGB8); |
387 | aFormat.SetPixelFormat (GL_RGB); |
388 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
389 | if (theIsColorMap |
390 | && theCtx->ToRenderSRGB()) |
391 | { |
8f7159cb |
392 | if (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
393 | && !theCtx->IsGlGreaterEqual (3, 0)) |
eee6a80a |
394 | { |
395 | aFormat.SetPixelFormat (GL_SRGB_EXT); |
396 | } |
ba00aab7 |
397 | aFormat.SetInternalFormat (GL_SRGB8); |
398 | } |
399 | return aFormat; |
400 | } |
401 | case Image_Format_BGR: |
402 | { |
8f7159cb |
403 | if (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES) |
404 | { |
405 | return OpenGl_TextureFormat(); |
406 | } |
407 | |
ba00aab7 |
408 | if (!theCtx->IsGlGreaterEqual (1, 2) |
409 | && !theCtx->extBgra) |
410 | { |
411 | return OpenGl_TextureFormat(); |
412 | } |
413 | aFormat.SetNbComponents (3); |
414 | aFormat.SetInternalFormat (GL_RGB8); |
eee6a80a |
415 | aFormat.SetPixelFormat (GL_BGR); // equals to GL_BGR_EXT |
416 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
ba00aab7 |
417 | if (theIsColorMap |
418 | && theCtx->ToRenderSRGB()) |
419 | { |
420 | aFormat.SetInternalFormat (GL_SRGB8); |
421 | } |
ba00aab7 |
422 | return aFormat; |
423 | } |
424 | case Image_Format_Gray: |
425 | { |
426 | aFormat.SetNbComponents (1); |
92996979 |
427 | if (useRedRedAlpha) |
ba00aab7 |
428 | { |
429 | aFormat.SetInternalFormat (GL_R8); |
430 | aFormat.SetPixelFormat (GL_RED); |
431 | } |
432 | else |
433 | { |
8f7159cb |
434 | aFormat.SetInternalFormat (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
435 | ? GL_LUMINANCE |
436 | : GL_LUMINANCE8); |
ba00aab7 |
437 | aFormat.SetPixelFormat (GL_LUMINANCE); |
438 | } |
439 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
440 | return aFormat; |
441 | } |
442 | case Image_Format_Alpha: |
443 | { |
444 | aFormat.SetNbComponents (1); |
92996979 |
445 | if (useRedRedAlpha) |
ba00aab7 |
446 | { |
447 | aFormat.SetInternalFormat (GL_R8); |
448 | aFormat.SetPixelFormat (GL_RED); |
449 | } |
450 | else |
451 | { |
8f7159cb |
452 | aFormat.SetInternalFormat (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
453 | ? GL_ALPHA |
454 | : GL_ALPHA8); |
ba00aab7 |
455 | aFormat.SetPixelFormat (GL_ALPHA); |
456 | } |
457 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
458 | return aFormat; |
459 | } |
b9a372bb |
460 | case Image_Format_Gray16: |
461 | { |
462 | if (!theCtx->extTexR16) |
463 | { |
464 | return OpenGl_TextureFormat(); |
465 | } |
466 | |
467 | aFormat.SetNbComponents (1); |
468 | if (useRedRedAlpha |
469 | || theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES) |
470 | { |
471 | aFormat.SetInternalFormat (GL_R16); |
472 | aFormat.SetPixelFormat (GL_RED); |
473 | } |
474 | else |
475 | { |
476 | aFormat.SetInternalFormat (GL_LUMINANCE16); |
477 | aFormat.SetPixelFormat (GL_LUMINANCE); |
478 | } |
479 | aFormat.SetDataType (GL_UNSIGNED_SHORT); |
480 | return aFormat; |
481 | } |
ba00aab7 |
482 | case Image_Format_UNKNOWN: |
483 | { |
484 | return OpenGl_TextureFormat(); |
485 | } |
486 | } |
487 | return OpenGl_TextureFormat(); |
488 | } |
489 | |
490 | // ======================================================================= |
491 | // function : FindSizedFormat |
492 | // purpose : |
493 | // ======================================================================= |
494 | OpenGl_TextureFormat OpenGl_TextureFormat::FindSizedFormat (const Handle(OpenGl_Context)& theCtx, |
495 | GLint theSizedFormat) |
496 | { |
497 | OpenGl_TextureFormat aFormat; |
498 | switch (theSizedFormat) |
499 | { |
500 | case GL_RGBA32F: |
501 | { |
502 | aFormat.SetNbComponents (4); |
503 | aFormat.SetInternalFormat (theSizedFormat); |
504 | aFormat.SetPixelFormat (GL_RGBA); |
505 | aFormat.SetDataType (GL_FLOAT); |
8f8fe4a9 |
506 | aFormat.SetImageFormat (Image_Format_RGBAF); |
ba00aab7 |
507 | return aFormat; |
508 | } |
509 | case GL_R32F: |
510 | { |
511 | aFormat.SetNbComponents (1); |
512 | aFormat.SetInternalFormat (theSizedFormat); |
513 | aFormat.SetPixelFormat (GL_RED); |
514 | aFormat.SetDataType (GL_FLOAT); |
8f8fe4a9 |
515 | aFormat.SetImageFormat (Image_Format_GrayF); |
ba00aab7 |
516 | return aFormat; |
517 | } |
78c4e836 |
518 | case GL_RG32F: |
519 | { |
520 | aFormat.SetNbComponents (1); |
521 | aFormat.SetInternalFormat (theSizedFormat); |
522 | aFormat.SetPixelFormat (GL_RG); |
523 | aFormat.SetDataType (GL_FLOAT); |
8f8fe4a9 |
524 | aFormat.SetImageFormat (Image_Format_RGF); |
78c4e836 |
525 | return aFormat; |
526 | } |
ba00aab7 |
527 | case GL_RGBA16F: |
528 | { |
529 | aFormat.SetNbComponents (4); |
530 | aFormat.SetInternalFormat (theSizedFormat); |
531 | aFormat.SetPixelFormat (GL_RGBA); |
532 | aFormat.SetDataType (GL_HALF_FLOAT); |
1ac837b2 |
533 | aFormat.SetImageFormat (Image_Format_RGBAF_half); |
ba00aab7 |
534 | if (theCtx->hasHalfFloatBuffer == OpenGl_FeatureInExtensions) |
535 | { |
8f7159cb |
536 | aFormat.SetDataType (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
537 | ? GL_HALF_FLOAT_OES |
538 | : GL_FLOAT); |
ba00aab7 |
539 | } |
540 | return aFormat; |
541 | } |
542 | case GL_R16F: |
543 | { |
544 | aFormat.SetNbComponents (1); |
545 | aFormat.SetInternalFormat (theSizedFormat); |
546 | aFormat.SetPixelFormat (GL_RED); |
547 | aFormat.SetDataType (GL_HALF_FLOAT); |
b9a372bb |
548 | aFormat.SetImageFormat (Image_Format_GrayF_half); |
ba00aab7 |
549 | if (theCtx->hasHalfFloatBuffer == OpenGl_FeatureInExtensions) |
550 | { |
8f7159cb |
551 | aFormat.SetDataType (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
552 | ? GL_HALF_FLOAT_OES |
553 | : GL_FLOAT); |
ba00aab7 |
554 | } |
555 | return aFormat; |
1ac837b2 |
556 | } |
557 | case GL_RG16F: |
558 | { |
559 | aFormat.SetNbComponents (2); |
560 | aFormat.SetInternalFormat (theSizedFormat); |
561 | aFormat.SetPixelFormat (GL_RG); |
562 | aFormat.SetDataType (GL_HALF_FLOAT); |
563 | aFormat.SetImageFormat (Image_Format_RGF_half); |
564 | if (theCtx->hasHalfFloatBuffer == OpenGl_FeatureInExtensions) |
565 | { |
8f7159cb |
566 | aFormat.SetDataType (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
567 | ? GL_HALF_FLOAT_OES |
568 | : GL_FLOAT); |
1ac837b2 |
569 | } |
570 | return aFormat; |
ba00aab7 |
571 | } |
572 | case GL_SRGB8_ALPHA8: |
eee6a80a |
573 | case GL_SRGB_ALPHA_EXT: |
ba00aab7 |
574 | case GL_RGBA8: |
575 | case GL_RGBA: |
576 | { |
577 | aFormat.SetNbComponents (4); |
578 | aFormat.SetInternalFormat (theSizedFormat); |
579 | aFormat.SetPixelFormat (GL_RGBA); |
580 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
8f8fe4a9 |
581 | aFormat.SetImageFormat (Image_Format_RGBA); |
eee6a80a |
582 | if ((theSizedFormat == GL_SRGB8_ALPHA8 || theSizedFormat == GL_SRGB_ALPHA_EXT)) |
ba00aab7 |
583 | { |
eee6a80a |
584 | if (theCtx->ToRenderSRGB()) |
585 | { |
8f7159cb |
586 | if (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
587 | && !theCtx->IsGlGreaterEqual (3, 0)) |
eee6a80a |
588 | { |
589 | aFormat.SetPixelFormat (GL_SRGB_ALPHA_EXT); |
590 | } |
eee6a80a |
591 | } |
592 | else |
593 | { |
594 | aFormat.SetInternalFormat (GL_RGBA8); // fallback format |
595 | } |
ba00aab7 |
596 | } |
597 | return aFormat; |
598 | } |
599 | case GL_SRGB8: |
eee6a80a |
600 | case GL_SRGB_EXT: |
ba00aab7 |
601 | case GL_RGB8: |
602 | case GL_RGB: |
603 | { |
604 | aFormat.SetNbComponents (3); |
605 | aFormat.SetInternalFormat (theSizedFormat); |
606 | aFormat.SetPixelFormat (GL_RGB); |
607 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
8f8fe4a9 |
608 | aFormat.SetImageFormat (Image_Format_RGB); |
eee6a80a |
609 | if ((theSizedFormat == GL_SRGB8 || theSizedFormat == GL_SRGB_EXT)) |
ba00aab7 |
610 | { |
eee6a80a |
611 | if (theCtx->ToRenderSRGB()) |
612 | { |
8f7159cb |
613 | if (theCtx->GraphicsLibrary() == Aspect_GraphicsLibrary_OpenGLES |
614 | && !theCtx->IsGlGreaterEqual (3, 0)) |
eee6a80a |
615 | { |
616 | aFormat.SetPixelFormat (GL_SRGB_EXT); |
617 | } |
eee6a80a |
618 | } |
619 | else |
620 | { |
621 | aFormat.SetInternalFormat (GL_RGB8); // fallback format |
622 | } |
ba00aab7 |
623 | } |
624 | return aFormat; |
625 | } |
ddb9ed48 |
626 | case GL_RGB10_A2: |
627 | { |
628 | aFormat.SetNbComponents (4); |
629 | aFormat.SetInternalFormat (theSizedFormat); |
630 | aFormat.SetPixelFormat (GL_RGBA); |
631 | aFormat.SetDataType (GL_UNSIGNED_INT_2_10_10_10_REV); |
632 | aFormat.SetImageFormat (Image_Format_RGBA); |
633 | return aFormat; |
634 | } |
ba00aab7 |
635 | // integer types |
636 | case GL_R32I: |
637 | { |
638 | aFormat.SetNbComponents (1); |
639 | aFormat.SetInternalFormat (theSizedFormat); |
640 | aFormat.SetPixelFormat (GL_RED_INTEGER); |
641 | aFormat.SetDataType (GL_INT); |
642 | return aFormat; |
643 | } |
644 | case GL_RG32I: |
645 | { |
646 | aFormat.SetNbComponents (2); |
647 | aFormat.SetInternalFormat (theSizedFormat); |
648 | aFormat.SetPixelFormat (GL_RG_INTEGER); |
649 | aFormat.SetDataType (GL_INT); |
650 | return aFormat; |
651 | } |
652 | // depth formats |
653 | case GL_DEPTH24_STENCIL8: |
654 | { |
655 | aFormat.SetNbComponents (2); |
656 | aFormat.SetInternalFormat (theSizedFormat); |
657 | aFormat.SetPixelFormat (GL_DEPTH_STENCIL); |
658 | aFormat.SetDataType (GL_UNSIGNED_INT_24_8); |
659 | return aFormat; |
660 | } |
661 | case GL_DEPTH32F_STENCIL8: |
662 | { |
663 | aFormat.SetNbComponents (2); |
664 | aFormat.SetInternalFormat (theSizedFormat); |
665 | aFormat.SetPixelFormat (GL_DEPTH_STENCIL); |
666 | aFormat.SetDataType (GL_FLOAT_32_UNSIGNED_INT_24_8_REV); |
667 | return aFormat; |
668 | } |
669 | case GL_DEPTH_COMPONENT16: |
670 | { |
671 | aFormat.SetNbComponents (1); |
672 | aFormat.SetInternalFormat (theSizedFormat); |
673 | aFormat.SetPixelFormat (GL_DEPTH_COMPONENT); |
674 | aFormat.SetDataType (GL_UNSIGNED_SHORT); |
675 | return aFormat; |
676 | } |
677 | case GL_DEPTH_COMPONENT24: |
678 | { |
679 | aFormat.SetNbComponents (1); |
680 | aFormat.SetInternalFormat (theSizedFormat); |
681 | aFormat.SetPixelFormat (GL_DEPTH_COMPONENT); |
682 | aFormat.SetDataType (GL_UNSIGNED_INT); |
683 | return aFormat; |
684 | } |
685 | case GL_DEPTH_COMPONENT32F: |
686 | { |
687 | aFormat.SetNbComponents (1); |
688 | aFormat.SetInternalFormat (theSizedFormat); |
689 | aFormat.SetPixelFormat (GL_DEPTH_COMPONENT); |
690 | aFormat.SetDataType (GL_FLOAT); |
691 | return aFormat; |
692 | } |
693 | } |
694 | return aFormat; |
695 | } |
faff3767 |
696 | |
697 | // ======================================================================= |
698 | // function : FindCompressedFormat |
699 | // purpose : |
700 | // ======================================================================= |
701 | OpenGl_TextureFormat OpenGl_TextureFormat::FindCompressedFormat (const Handle(OpenGl_Context)& theCtx, |
702 | Image_CompressedFormat theFormat, |
703 | bool theIsColorMap) |
704 | { |
705 | OpenGl_TextureFormat aFormat; |
706 | if (!theCtx->SupportedTextureFormats()->IsSupported (theFormat)) |
707 | { |
708 | return aFormat; |
709 | } |
710 | |
711 | switch (theFormat) |
712 | { |
713 | case Image_CompressedFormat_UNKNOWN: |
714 | { |
715 | return aFormat; |
716 | } |
717 | case Image_CompressedFormat_RGB_S3TC_DXT1: |
718 | { |
719 | aFormat.SetNbComponents (3); |
720 | aFormat.SetPixelFormat (GL_RGB); |
721 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
722 | aFormat.SetInternalFormat (GL_COMPRESSED_RGB_S3TC_DXT1_EXT); |
723 | if (theIsColorMap |
724 | && theCtx->ToRenderSRGB()) |
725 | { |
726 | aFormat.SetInternalFormat (GL_COMPRESSED_SRGB_S3TC_DXT1_EXT); |
727 | } |
728 | return aFormat; |
729 | } |
730 | case Image_CompressedFormat_RGBA_S3TC_DXT1: |
731 | { |
732 | aFormat.SetNbComponents (4); |
733 | aFormat.SetPixelFormat (GL_RGBA); |
734 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
735 | aFormat.SetInternalFormat (GL_COMPRESSED_RGBA_S3TC_DXT1_EXT); |
736 | if (theIsColorMap |
737 | && theCtx->ToRenderSRGB()) |
738 | { |
739 | aFormat.SetInternalFormat (GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT); |
740 | } |
741 | return aFormat; |
742 | } |
743 | case Image_CompressedFormat_RGBA_S3TC_DXT3: |
744 | { |
745 | aFormat.SetNbComponents (4); |
746 | aFormat.SetPixelFormat (GL_RGBA); |
747 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
748 | aFormat.SetInternalFormat (GL_COMPRESSED_RGBA_S3TC_DXT3_EXT); |
749 | if (theIsColorMap |
750 | && theCtx->ToRenderSRGB()) |
751 | { |
752 | aFormat.SetInternalFormat (GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT); |
753 | } |
754 | return aFormat; |
755 | } |
756 | case Image_CompressedFormat_RGBA_S3TC_DXT5: |
757 | { |
758 | aFormat.SetNbComponents (4); |
759 | aFormat.SetPixelFormat (GL_RGBA); |
760 | aFormat.SetDataType (GL_UNSIGNED_BYTE); |
761 | aFormat.SetInternalFormat (GL_COMPRESSED_RGBA_S3TC_DXT5_EXT); |
762 | if (theIsColorMap |
763 | && theCtx->ToRenderSRGB()) |
764 | { |
765 | aFormat.SetInternalFormat (GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT); |
766 | } |
767 | return aFormat; |
768 | } |
769 | } |
770 | return aFormat; |
771 | } |