2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (c) 2009 VMware, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 * Texel fetch/store functions
32 * \author Gareth Hughes
38 #include "texcompress.h"
39 #include "texcompress_fxt1.h"
40 #include "texcompress_s3tc.h"
45 * Convert an 8-bit sRGB value from non-linear space to a
46 * linear RGB value in [0, 1].
47 * Implemented with a 256-entry lookup table.
50 nonlinear_to_linear(GLubyte cs8
)
52 static GLfloat table
[256];
53 static GLboolean tableReady
= GL_FALSE
;
55 /* compute lookup table now */
57 for (i
= 0; i
< 256; i
++) {
58 const GLfloat cs
= UBYTE_TO_FLOAT(i
);
60 table
[i
] = cs
/ 12.92f
;
63 table
[i
] = (GLfloat
) _mesa_pow((cs
+ 0.055) / 1.055, 2.4);
73 /* Texel fetch routines for all supported formats
76 #include "texfetch_tmp.h"
79 #include "texfetch_tmp.h"
82 #include "texfetch_tmp.h"
85 * Null texel fetch function.
87 * Have to have this so the FetchTexel function pointer is never NULL.
89 static void fetch_null_texelf( const struct gl_texture_image
*texImage
,
90 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
92 (void) texImage
; (void) i
; (void) j
; (void) k
;
97 _mesa_warning(NULL
, "fetch_null_texelf() called!");
100 static void store_null_texel(struct gl_texture_image
*texImage
,
101 GLint i
, GLint j
, GLint k
, const void *texel
)
114 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
115 * XXX this is somewhat temporary.
119 FetchTexelFuncF Fetch1D
;
120 FetchTexelFuncF Fetch2D
;
121 FetchTexelFuncF Fetch3D
;
122 StoreTexelFunc StoreTexel
;
124 texfetch_funcs
[MESA_FORMAT_COUNT
] =
128 fetch_texel_1d_srgb8
,
129 fetch_texel_2d_srgb8
,
130 fetch_texel_3d_srgb8
,
135 fetch_texel_1d_srgba8
,
136 fetch_texel_2d_srgba8
,
137 fetch_texel_3d_srgba8
,
142 fetch_texel_1d_sargb8
,
143 fetch_texel_2d_sargb8
,
144 fetch_texel_3d_sargb8
,
162 MESA_FORMAT_RGB_FXT1
,
164 _mesa_fetch_texel_2d_f_rgb_fxt1
,
169 MESA_FORMAT_RGBA_FXT1
,
171 _mesa_fetch_texel_2d_f_rgba_fxt1
,
176 MESA_FORMAT_RGB_DXT1
,
178 _mesa_fetch_texel_2d_f_rgb_dxt1
,
183 MESA_FORMAT_RGBA_DXT1
,
185 _mesa_fetch_texel_2d_f_rgba_dxt1
,
190 MESA_FORMAT_RGBA_DXT3
,
192 _mesa_fetch_texel_2d_f_rgba_dxt3
,
197 MESA_FORMAT_RGBA_DXT5
,
199 _mesa_fetch_texel_2d_f_rgba_dxt5
,
204 MESA_FORMAT_SRGB_DXT1
,
206 _mesa_fetch_texel_2d_f_srgb_dxt1
,
211 MESA_FORMAT_SRGBA_DXT1
,
213 _mesa_fetch_texel_2d_f_srgba_dxt1
,
218 MESA_FORMAT_SRGBA_DXT3
,
220 _mesa_fetch_texel_2d_f_srgba_dxt3
,
225 MESA_FORMAT_SRGBA_DXT5
,
227 _mesa_fetch_texel_2d_f_srgba_dxt5
,
232 MESA_FORMAT_RGBA_FLOAT32
,
233 fetch_texel_1d_f_rgba_f32
,
234 fetch_texel_2d_f_rgba_f32
,
235 fetch_texel_3d_f_rgba_f32
,
239 MESA_FORMAT_RGBA_FLOAT16
,
240 fetch_texel_1d_f_rgba_f16
,
241 fetch_texel_2d_f_rgba_f16
,
242 fetch_texel_3d_f_rgba_f16
,
246 MESA_FORMAT_RGB_FLOAT32
,
247 fetch_texel_1d_f_rgb_f32
,
248 fetch_texel_2d_f_rgb_f32
,
249 fetch_texel_3d_f_rgb_f32
,
253 MESA_FORMAT_RGB_FLOAT16
,
254 fetch_texel_1d_f_rgb_f16
,
255 fetch_texel_2d_f_rgb_f16
,
256 fetch_texel_3d_f_rgb_f16
,
260 MESA_FORMAT_ALPHA_FLOAT32
,
261 fetch_texel_1d_f_alpha_f32
,
262 fetch_texel_2d_f_alpha_f32
,
263 fetch_texel_3d_f_alpha_f32
,
264 store_texel_alpha_f32
267 MESA_FORMAT_ALPHA_FLOAT16
,
268 fetch_texel_1d_f_alpha_f16
,
269 fetch_texel_2d_f_alpha_f16
,
270 fetch_texel_3d_f_alpha_f16
,
271 store_texel_alpha_f16
274 MESA_FORMAT_LUMINANCE_FLOAT32
,
275 fetch_texel_1d_f_luminance_f32
,
276 fetch_texel_2d_f_luminance_f32
,
277 fetch_texel_3d_f_luminance_f32
,
278 store_texel_luminance_f32
281 MESA_FORMAT_LUMINANCE_FLOAT16
,
282 fetch_texel_1d_f_luminance_f16
,
283 fetch_texel_2d_f_luminance_f16
,
284 fetch_texel_3d_f_luminance_f16
,
285 store_texel_luminance_f16
288 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
,
289 fetch_texel_1d_f_luminance_alpha_f32
,
290 fetch_texel_2d_f_luminance_alpha_f32
,
291 fetch_texel_3d_f_luminance_alpha_f32
,
292 store_texel_luminance_alpha_f32
295 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
,
296 fetch_texel_1d_f_luminance_alpha_f16
,
297 fetch_texel_2d_f_luminance_alpha_f16
,
298 fetch_texel_3d_f_luminance_alpha_f16
,
299 store_texel_luminance_alpha_f16
302 MESA_FORMAT_INTENSITY_FLOAT32
,
303 fetch_texel_1d_f_intensity_f32
,
304 fetch_texel_2d_f_intensity_f32
,
305 fetch_texel_3d_f_intensity_f32
,
306 store_texel_intensity_f32
309 MESA_FORMAT_INTENSITY_FLOAT16
,
310 fetch_texel_1d_f_intensity_f16
,
311 fetch_texel_2d_f_intensity_f16
,
312 fetch_texel_3d_f_intensity_f16
,
313 store_texel_intensity_f16
317 fetch_texel_1d_dudv8
,
318 fetch_texel_2d_dudv8
,
319 fetch_texel_3d_dudv8
,
323 MESA_FORMAT_SIGNED_RGBA8888
,
324 fetch_texel_1d_signed_rgba8888
,
325 fetch_texel_2d_signed_rgba8888
,
326 fetch_texel_3d_signed_rgba8888
,
327 store_texel_signed_rgba8888
330 MESA_FORMAT_SIGNED_RGBA8888_REV
,
331 fetch_texel_1d_signed_rgba8888_rev
,
332 fetch_texel_2d_signed_rgba8888_rev
,
333 fetch_texel_3d_signed_rgba8888_rev
,
334 store_texel_signed_rgba8888_rev
337 MESA_FORMAT_SIGNED_RGBA_16
,
338 NULL
, /* XXX to do */
344 MESA_FORMAT_RGBA8888
,
345 fetch_texel_1d_f_rgba8888
,
346 fetch_texel_2d_f_rgba8888
,
347 fetch_texel_3d_f_rgba8888
,
351 MESA_FORMAT_RGBA8888_REV
,
352 fetch_texel_1d_f_rgba8888_rev
,
353 fetch_texel_2d_f_rgba8888_rev
,
354 fetch_texel_3d_f_rgba8888_rev
,
355 store_texel_rgba8888_rev
358 MESA_FORMAT_ARGB8888
,
359 fetch_texel_1d_f_argb8888
,
360 fetch_texel_2d_f_argb8888
,
361 fetch_texel_3d_f_argb8888
,
365 MESA_FORMAT_ARGB8888_REV
,
366 fetch_texel_1d_f_argb8888_rev
,
367 fetch_texel_2d_f_argb8888_rev
,
368 fetch_texel_3d_f_argb8888_rev
,
369 store_texel_argb8888_rev
372 MESA_FORMAT_XRGB8888
,
373 fetch_texel_1d_f_xrgb8888
,
374 fetch_texel_2d_f_xrgb8888
,
375 fetch_texel_3d_f_xrgb8888
,
380 fetch_texel_1d_f_rgb888
,
381 fetch_texel_2d_f_rgb888
,
382 fetch_texel_3d_f_rgb888
,
387 fetch_texel_1d_f_bgr888
,
388 fetch_texel_2d_f_bgr888
,
389 fetch_texel_3d_f_bgr888
,
394 fetch_texel_1d_f_rgb565
,
395 fetch_texel_2d_f_rgb565
,
396 fetch_texel_3d_f_rgb565
,
400 MESA_FORMAT_RGB565_REV
,
401 fetch_texel_1d_f_rgb565_rev
,
402 fetch_texel_2d_f_rgb565_rev
,
403 fetch_texel_3d_f_rgb565_rev
,
404 store_texel_rgb565_rev
407 MESA_FORMAT_ARGB4444
,
408 fetch_texel_1d_f_argb4444
,
409 fetch_texel_2d_f_argb4444
,
410 fetch_texel_3d_f_argb4444
,
414 MESA_FORMAT_ARGB4444_REV
,
415 fetch_texel_1d_f_argb4444_rev
,
416 fetch_texel_2d_f_argb4444_rev
,
417 fetch_texel_3d_f_argb4444_rev
,
418 store_texel_argb4444_rev
421 MESA_FORMAT_RGBA5551
,
422 fetch_texel_1d_f_rgba5551
,
423 fetch_texel_2d_f_rgba5551
,
424 fetch_texel_3d_f_rgba5551
,
428 MESA_FORMAT_ARGB1555
,
429 fetch_texel_1d_f_argb1555
,
430 fetch_texel_2d_f_argb1555
,
431 fetch_texel_3d_f_argb1555
,
435 MESA_FORMAT_ARGB1555_REV
,
436 fetch_texel_1d_f_argb1555_rev
,
437 fetch_texel_2d_f_argb1555_rev
,
438 fetch_texel_3d_f_argb1555_rev
,
439 store_texel_argb1555_rev
443 fetch_texel_1d_f_al88
,
444 fetch_texel_2d_f_al88
,
445 fetch_texel_3d_f_al88
,
449 MESA_FORMAT_AL88_REV
,
450 fetch_texel_1d_f_al88_rev
,
451 fetch_texel_2d_f_al88_rev
,
452 fetch_texel_3d_f_al88_rev
,
457 fetch_texel_1d_f_rgb332
,
458 fetch_texel_2d_f_rgb332
,
459 fetch_texel_3d_f_rgb332
,
485 fetch_texel_1d_f_ci8
,
486 fetch_texel_2d_f_ci8
,
487 fetch_texel_3d_f_ci8
,
492 fetch_texel_1d_f_ycbcr
,
493 fetch_texel_2d_f_ycbcr
,
494 fetch_texel_3d_f_ycbcr
,
498 MESA_FORMAT_YCBCR_REV
,
499 fetch_texel_1d_f_ycbcr_rev
,
500 fetch_texel_2d_f_ycbcr_rev
,
501 fetch_texel_3d_f_ycbcr_rev
,
502 store_texel_ycbcr_rev
506 fetch_texel_1d_f_z24_s8
,
507 fetch_texel_2d_f_z24_s8
,
508 fetch_texel_3d_f_z24_s8
,
513 fetch_texel_1d_f_s8_z24
,
514 fetch_texel_2d_f_s8_z24
,
515 fetch_texel_3d_f_s8_z24
,
520 fetch_texel_1d_f_z16
,
521 fetch_texel_2d_f_z16
,
522 fetch_texel_3d_f_z16
,
527 fetch_texel_1d_f_s8_z24
,
528 fetch_texel_2d_f_s8_z24
,
529 fetch_texel_3d_f_s8_z24
,
534 fetch_texel_1d_f_z24_s8
,
535 fetch_texel_2d_f_z24_s8
,
536 fetch_texel_3d_f_z24_s8
,
541 fetch_texel_1d_f_z32
,
542 fetch_texel_2d_f_z32
,
543 fetch_texel_3d_f_z32
,
549 static FetchTexelFuncF
550 _mesa_get_texel_fetch_func(gl_format format
, GLuint dims
)
554 /* XXX replace loop with direct table lookup */
555 for (i
= 0; i
< MESA_FORMAT_COUNT
; i
++) {
556 if (texfetch_funcs
[i
].Name
== format
) {
559 f
= texfetch_funcs
[i
].Fetch1D
;
562 f
= texfetch_funcs
[i
].Fetch2D
;
565 f
= texfetch_funcs
[i
].Fetch3D
;
569 f
= fetch_null_texelf
;
578 _mesa_get_texel_store_func(gl_format format
)
581 /* XXX replace loop with direct table lookup */
582 for (i
= 0; i
< MESA_FORMAT_COUNT
; i
++) {
583 if (texfetch_funcs
[i
].Name
== format
) {
584 if (texfetch_funcs
[i
].StoreTexel
)
585 return texfetch_funcs
[i
].StoreTexel
;
587 return store_null_texel
;
596 * Adaptor for fetching a GLchan texel from a float-valued texture.
599 fetch_texel_float_to_chan(const struct gl_texture_image
*texImage
,
600 GLint i
, GLint j
, GLint k
, GLchan
*texelOut
)
603 GLenum baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
605 ASSERT(texImage
->FetchTexelf
);
606 texImage
->FetchTexelf(texImage
, i
, j
, k
, temp
);
607 if (baseFormat
== GL_DEPTH_COMPONENT
||
608 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
609 /* just one channel */
610 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[0], temp
[0]);
614 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[0], temp
[0]);
615 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[1], temp
[1]);
616 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[2], temp
[2]);
617 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[3], temp
[3]);
624 * Adaptor for fetching a float texel from a GLchan-valued texture.
627 fetch_texel_chan_to_float(const struct gl_texture_image
*texImage
,
628 GLint i
, GLint j
, GLint k
, GLfloat
*texelOut
)
631 GLenum baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
633 ASSERT(texImage
->FetchTexelc
);
634 texImage
->FetchTexelc(texImage
, i
, j
, k
, temp
);
635 if (baseFormat
== GL_DEPTH_COMPONENT
||
636 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
637 /* just one channel */
638 texelOut
[0] = CHAN_TO_FLOAT(temp
[0]);
642 texelOut
[0] = CHAN_TO_FLOAT(temp
[0]);
643 texelOut
[1] = CHAN_TO_FLOAT(temp
[1]);
644 texelOut
[2] = CHAN_TO_FLOAT(temp
[2]);
645 texelOut
[3] = CHAN_TO_FLOAT(temp
[3]);
652 * Initialize the texture image's FetchTexelc and FetchTexelf methods.
655 _mesa_set_fetch_functions(struct gl_texture_image
*texImage
, GLuint dims
)
657 ASSERT(dims
== 1 || dims
== 2 || dims
== 3);
658 ASSERT(texImage
->TexFormat
);
660 if (!texImage
->FetchTexelf
) {
661 texImage
->FetchTexelf
=
662 _mesa_get_texel_fetch_func(texImage
->TexFormat
, dims
);
665 /* now check if we need to use a float/chan adaptor */
666 if (!texImage
->FetchTexelc
) {
667 texImage
->FetchTexelc
= fetch_texel_float_to_chan
;
670 ASSERT(texImage
->FetchTexelc
);
671 ASSERT(texImage
->FetchTexelf
);