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"
46 * Convert an 8-bit sRGB value from non-linear space to a
47 * linear RGB value in [0, 1].
48 * Implemented with a 256-entry lookup table.
51 nonlinear_to_linear(GLubyte cs8
)
53 static GLfloat table
[256];
54 static GLboolean tableReady
= GL_FALSE
;
56 /* compute lookup table now */
58 for (i
= 0; i
< 256; i
++) {
59 const GLfloat cs
= UBYTE_TO_FLOAT(i
);
61 table
[i
] = cs
/ 12.92f
;
64 table
[i
] = (GLfloat
) pow((cs
+ 0.055) / 1.055, 2.4);
74 /* Texel fetch routines for all supported formats
77 #include "texfetch_tmp.h"
80 #include "texfetch_tmp.h"
83 #include "texfetch_tmp.h"
86 * Null texel fetch function.
88 * Have to have this so the FetchTexel function pointer is never NULL.
90 static void fetch_null_texelf( const struct gl_texture_image
*texImage
,
91 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
93 (void) texImage
; (void) i
; (void) j
; (void) k
;
98 _mesa_warning(NULL
, "fetch_null_texelf() called!");
101 static void store_null_texel(struct gl_texture_image
*texImage
,
102 GLint i
, GLint j
, GLint k
, const void *texel
)
115 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
116 * XXX this is somewhat temporary.
120 FetchTexelFuncF Fetch1D
;
121 FetchTexelFuncF Fetch2D
;
122 FetchTexelFuncF Fetch3D
;
123 StoreTexelFunc StoreTexel
;
125 texfetch_funcs
[MESA_FORMAT_COUNT
] =
136 MESA_FORMAT_RGBA8888
,
137 fetch_texel_1d_f_rgba8888
,
138 fetch_texel_2d_f_rgba8888
,
139 fetch_texel_3d_f_rgba8888
,
143 MESA_FORMAT_RGBA8888_REV
,
144 fetch_texel_1d_f_rgba8888_rev
,
145 fetch_texel_2d_f_rgba8888_rev
,
146 fetch_texel_3d_f_rgba8888_rev
,
147 store_texel_rgba8888_rev
150 MESA_FORMAT_ARGB8888
,
151 fetch_texel_1d_f_argb8888
,
152 fetch_texel_2d_f_argb8888
,
153 fetch_texel_3d_f_argb8888
,
157 MESA_FORMAT_ARGB8888_REV
,
158 fetch_texel_1d_f_argb8888_rev
,
159 fetch_texel_2d_f_argb8888_rev
,
160 fetch_texel_3d_f_argb8888_rev
,
161 store_texel_argb8888_rev
164 MESA_FORMAT_XRGB8888
,
165 fetch_texel_1d_f_xrgb8888
,
166 fetch_texel_2d_f_xrgb8888
,
167 fetch_texel_3d_f_xrgb8888
,
171 MESA_FORMAT_XRGB8888_REV
,
172 fetch_texel_1d_f_xrgb8888_rev
,
173 fetch_texel_2d_f_xrgb8888_rev
,
174 fetch_texel_3d_f_xrgb8888_rev
,
175 store_texel_xrgb8888_rev
,
179 fetch_texel_1d_f_rgb888
,
180 fetch_texel_2d_f_rgb888
,
181 fetch_texel_3d_f_rgb888
,
186 fetch_texel_1d_f_bgr888
,
187 fetch_texel_2d_f_bgr888
,
188 fetch_texel_3d_f_bgr888
,
193 fetch_texel_1d_f_rgb565
,
194 fetch_texel_2d_f_rgb565
,
195 fetch_texel_3d_f_rgb565
,
199 MESA_FORMAT_RGB565_REV
,
200 fetch_texel_1d_f_rgb565_rev
,
201 fetch_texel_2d_f_rgb565_rev
,
202 fetch_texel_3d_f_rgb565_rev
,
203 store_texel_rgb565_rev
206 MESA_FORMAT_ARGB4444
,
207 fetch_texel_1d_f_argb4444
,
208 fetch_texel_2d_f_argb4444
,
209 fetch_texel_3d_f_argb4444
,
213 MESA_FORMAT_ARGB4444_REV
,
214 fetch_texel_1d_f_argb4444_rev
,
215 fetch_texel_2d_f_argb4444_rev
,
216 fetch_texel_3d_f_argb4444_rev
,
217 store_texel_argb4444_rev
220 MESA_FORMAT_RGBA5551
,
221 fetch_texel_1d_f_rgba5551
,
222 fetch_texel_2d_f_rgba5551
,
223 fetch_texel_3d_f_rgba5551
,
227 MESA_FORMAT_ARGB1555
,
228 fetch_texel_1d_f_argb1555
,
229 fetch_texel_2d_f_argb1555
,
230 fetch_texel_3d_f_argb1555
,
234 MESA_FORMAT_ARGB1555_REV
,
235 fetch_texel_1d_f_argb1555_rev
,
236 fetch_texel_2d_f_argb1555_rev
,
237 fetch_texel_3d_f_argb1555_rev
,
238 store_texel_argb1555_rev
242 fetch_texel_1d_f_al44
,
243 fetch_texel_2d_f_al44
,
244 fetch_texel_3d_f_al44
,
249 fetch_texel_1d_f_al88
,
250 fetch_texel_2d_f_al88
,
251 fetch_texel_3d_f_al88
,
255 MESA_FORMAT_AL88_REV
,
256 fetch_texel_1d_f_al88_rev
,
257 fetch_texel_2d_f_al88_rev
,
258 fetch_texel_3d_f_al88_rev
,
263 fetch_texel_1d_f_al1616
,
264 fetch_texel_2d_f_al1616
,
265 fetch_texel_3d_f_al1616
,
269 MESA_FORMAT_AL1616_REV
,
270 fetch_texel_1d_f_al1616_rev
,
271 fetch_texel_2d_f_al1616_rev
,
272 fetch_texel_3d_f_al1616_rev
,
273 store_texel_al1616_rev
277 fetch_texel_1d_f_rgb332
,
278 fetch_texel_2d_f_rgb332
,
279 fetch_texel_3d_f_rgb332
,
291 fetch_texel_1d_f_a16
,
292 fetch_texel_2d_f_a16
,
293 fetch_texel_3d_f_a16
,
305 fetch_texel_1d_f_l16
,
306 fetch_texel_2d_f_l16
,
307 fetch_texel_3d_f_l16
,
319 fetch_texel_1d_f_i16
,
320 fetch_texel_2d_f_i16
,
321 fetch_texel_3d_f_i16
,
326 fetch_texel_1d_f_ci8
,
327 fetch_texel_2d_f_ci8
,
328 fetch_texel_3d_f_ci8
,
333 fetch_texel_1d_f_ycbcr
,
334 fetch_texel_2d_f_ycbcr
,
335 fetch_texel_3d_f_ycbcr
,
339 MESA_FORMAT_YCBCR_REV
,
340 fetch_texel_1d_f_ycbcr_rev
,
341 fetch_texel_2d_f_ycbcr_rev
,
342 fetch_texel_3d_f_ycbcr_rev
,
343 store_texel_ycbcr_rev
354 fetch_texel_1d_f_rg88
,
355 fetch_texel_2d_f_rg88
,
356 fetch_texel_3d_f_rg88
,
360 MESA_FORMAT_RG88_REV
,
361 fetch_texel_1d_f_rg88_rev
,
362 fetch_texel_2d_f_rg88_rev
,
363 fetch_texel_3d_f_rg88_rev
,
364 store_texel_rg88_rev
,
368 fetch_texel_1d_f_r16
,
369 fetch_texel_2d_f_r16
,
370 fetch_texel_3d_f_r16
,
375 fetch_texel_1d_f_rg1616
,
376 fetch_texel_2d_f_rg1616
,
377 fetch_texel_3d_f_rg1616
,
381 MESA_FORMAT_RG1616_REV
,
382 fetch_texel_1d_f_rg1616_rev
,
383 fetch_texel_2d_f_rg1616_rev
,
384 fetch_texel_3d_f_rg1616_rev
,
385 store_texel_rg1616_rev
,
388 MESA_FORMAT_ARGB2101010
,
389 fetch_texel_1d_f_argb2101010
,
390 fetch_texel_2d_f_argb2101010
,
391 fetch_texel_3d_f_argb2101010
,
392 store_texel_argb2101010
396 fetch_texel_1d_f_z24_s8
,
397 fetch_texel_2d_f_z24_s8
,
398 fetch_texel_3d_f_z24_s8
,
403 fetch_texel_1d_f_s8_z24
,
404 fetch_texel_2d_f_s8_z24
,
405 fetch_texel_3d_f_s8_z24
,
410 fetch_texel_1d_f_z16
,
411 fetch_texel_2d_f_z16
,
412 fetch_texel_3d_f_z16
,
417 fetch_texel_1d_f_s8_z24
,
418 fetch_texel_2d_f_s8_z24
,
419 fetch_texel_3d_f_s8_z24
,
424 fetch_texel_1d_f_z24_s8
,
425 fetch_texel_2d_f_z24_s8
,
426 fetch_texel_3d_f_z24_s8
,
431 fetch_texel_1d_f_z32
,
432 fetch_texel_2d_f_z32
,
433 fetch_texel_3d_f_z32
,
445 fetch_texel_1d_srgb8
,
446 fetch_texel_2d_srgb8
,
447 fetch_texel_3d_srgb8
,
452 fetch_texel_1d_srgba8
,
453 fetch_texel_2d_srgba8
,
454 fetch_texel_3d_srgba8
,
459 fetch_texel_1d_sargb8
,
460 fetch_texel_2d_sargb8
,
461 fetch_texel_3d_sargb8
,
479 MESA_FORMAT_SRGB_DXT1
,
481 _mesa_fetch_texel_2d_f_srgb_dxt1
,
486 MESA_FORMAT_SRGBA_DXT1
,
488 _mesa_fetch_texel_2d_f_srgba_dxt1
,
493 MESA_FORMAT_SRGBA_DXT3
,
495 _mesa_fetch_texel_2d_f_srgba_dxt3
,
500 MESA_FORMAT_SRGBA_DXT5
,
502 _mesa_fetch_texel_2d_f_srgba_dxt5
,
508 MESA_FORMAT_RGB_FXT1
,
510 _mesa_fetch_texel_2d_f_rgb_fxt1
,
515 MESA_FORMAT_RGBA_FXT1
,
517 _mesa_fetch_texel_2d_f_rgba_fxt1
,
522 MESA_FORMAT_RGB_DXT1
,
524 _mesa_fetch_texel_2d_f_rgb_dxt1
,
529 MESA_FORMAT_RGBA_DXT1
,
531 _mesa_fetch_texel_2d_f_rgba_dxt1
,
536 MESA_FORMAT_RGBA_DXT3
,
538 _mesa_fetch_texel_2d_f_rgba_dxt3
,
543 MESA_FORMAT_RGBA_DXT5
,
545 _mesa_fetch_texel_2d_f_rgba_dxt5
,
550 MESA_FORMAT_RGBA_FLOAT32
,
551 fetch_texel_1d_f_rgba_f32
,
552 fetch_texel_2d_f_rgba_f32
,
553 fetch_texel_3d_f_rgba_f32
,
557 MESA_FORMAT_RGBA_FLOAT16
,
558 fetch_texel_1d_f_rgba_f16
,
559 fetch_texel_2d_f_rgba_f16
,
560 fetch_texel_3d_f_rgba_f16
,
564 MESA_FORMAT_RGB_FLOAT32
,
565 fetch_texel_1d_f_rgb_f32
,
566 fetch_texel_2d_f_rgb_f32
,
567 fetch_texel_3d_f_rgb_f32
,
571 MESA_FORMAT_RGB_FLOAT16
,
572 fetch_texel_1d_f_rgb_f16
,
573 fetch_texel_2d_f_rgb_f16
,
574 fetch_texel_3d_f_rgb_f16
,
578 MESA_FORMAT_ALPHA_FLOAT32
,
579 fetch_texel_1d_f_alpha_f32
,
580 fetch_texel_2d_f_alpha_f32
,
581 fetch_texel_3d_f_alpha_f32
,
582 store_texel_alpha_f32
585 MESA_FORMAT_ALPHA_FLOAT16
,
586 fetch_texel_1d_f_alpha_f16
,
587 fetch_texel_2d_f_alpha_f16
,
588 fetch_texel_3d_f_alpha_f16
,
589 store_texel_alpha_f16
592 MESA_FORMAT_LUMINANCE_FLOAT32
,
593 fetch_texel_1d_f_luminance_f32
,
594 fetch_texel_2d_f_luminance_f32
,
595 fetch_texel_3d_f_luminance_f32
,
596 store_texel_luminance_f32
599 MESA_FORMAT_LUMINANCE_FLOAT16
,
600 fetch_texel_1d_f_luminance_f16
,
601 fetch_texel_2d_f_luminance_f16
,
602 fetch_texel_3d_f_luminance_f16
,
603 store_texel_luminance_f16
606 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
,
607 fetch_texel_1d_f_luminance_alpha_f32
,
608 fetch_texel_2d_f_luminance_alpha_f32
,
609 fetch_texel_3d_f_luminance_alpha_f32
,
610 store_texel_luminance_alpha_f32
613 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
,
614 fetch_texel_1d_f_luminance_alpha_f16
,
615 fetch_texel_2d_f_luminance_alpha_f16
,
616 fetch_texel_3d_f_luminance_alpha_f16
,
617 store_texel_luminance_alpha_f16
620 MESA_FORMAT_INTENSITY_FLOAT32
,
621 fetch_texel_1d_f_intensity_f32
,
622 fetch_texel_2d_f_intensity_f32
,
623 fetch_texel_3d_f_intensity_f32
,
624 store_texel_intensity_f32
627 MESA_FORMAT_INTENSITY_FLOAT16
,
628 fetch_texel_1d_f_intensity_f16
,
629 fetch_texel_2d_f_intensity_f16
,
630 fetch_texel_3d_f_intensity_f16
,
631 store_texel_intensity_f16
634 /* non-normalized, signed int */
636 MESA_FORMAT_RGBA_INT8
,
637 fetch_texel_1d_rgba_int8
,
638 fetch_texel_2d_rgba_int8
,
639 fetch_texel_3d_rgba_int8
,
640 store_texel_rgba_int8
643 MESA_FORMAT_RGBA_INT16
,
644 fetch_texel_1d_rgba_int16
,
645 fetch_texel_2d_rgba_int16
,
646 fetch_texel_3d_rgba_int16
,
647 store_texel_rgba_int16
650 MESA_FORMAT_RGBA_INT32
,
651 fetch_texel_1d_rgba_int32
,
652 fetch_texel_2d_rgba_int32
,
653 fetch_texel_3d_rgba_int32
,
654 store_texel_rgba_int32
657 /* non-normalized, unsigned int */
659 MESA_FORMAT_RGBA_UINT8
,
660 fetch_texel_1d_rgba_uint8
,
661 fetch_texel_2d_rgba_uint8
,
662 fetch_texel_3d_rgba_uint8
,
663 store_texel_rgba_uint8
666 MESA_FORMAT_RGBA_UINT16
,
667 fetch_texel_1d_rgba_uint16
,
668 fetch_texel_2d_rgba_uint16
,
669 fetch_texel_3d_rgba_uint16
,
670 store_texel_rgba_uint16
673 MESA_FORMAT_RGBA_UINT32
,
674 fetch_texel_1d_rgba_uint32
,
675 fetch_texel_2d_rgba_uint32
,
676 fetch_texel_3d_rgba_uint32
,
677 store_texel_rgba_uint32
683 fetch_texel_1d_dudv8
,
684 fetch_texel_2d_dudv8
,
685 fetch_texel_3d_dudv8
,
689 /* signed, normalized */
691 MESA_FORMAT_SIGNED_R8
,
692 fetch_texel_1d_signed_r8
,
693 fetch_texel_2d_signed_r8
,
694 fetch_texel_3d_signed_r8
,
695 store_texel_signed_r8
698 MESA_FORMAT_SIGNED_RG88
,
699 fetch_texel_1d_signed_rg88
,
700 fetch_texel_2d_signed_rg88
,
701 fetch_texel_3d_signed_rg88
,
702 store_texel_signed_rg88
705 MESA_FORMAT_SIGNED_RGBX8888
,
706 fetch_texel_1d_signed_rgbx8888
,
707 fetch_texel_2d_signed_rgbx8888
,
708 fetch_texel_3d_signed_rgbx8888
,
709 store_texel_signed_rgbx8888
712 MESA_FORMAT_SIGNED_RGBA8888
,
713 fetch_texel_1d_signed_rgba8888
,
714 fetch_texel_2d_signed_rgba8888
,
715 fetch_texel_3d_signed_rgba8888
,
716 store_texel_signed_rgba8888
719 MESA_FORMAT_SIGNED_RGBA8888_REV
,
720 fetch_texel_1d_signed_rgba8888_rev
,
721 fetch_texel_2d_signed_rgba8888_rev
,
722 fetch_texel_3d_signed_rgba8888_rev
,
723 store_texel_signed_rgba8888_rev
726 MESA_FORMAT_SIGNED_R_16
,
727 fetch_texel_1d_signed_r_16
,
728 fetch_texel_2d_signed_r_16
,
729 fetch_texel_3d_signed_r_16
,
730 store_texel_signed_r_16
733 MESA_FORMAT_SIGNED_RG_16
,
734 fetch_texel_1d_signed_rg_16
,
735 fetch_texel_2d_signed_rg_16
,
736 fetch_texel_3d_signed_rg_16
,
737 store_texel_signed_rg_16
740 MESA_FORMAT_SIGNED_RGB_16
,
741 fetch_texel_1d_signed_rgb_16
,
742 fetch_texel_2d_signed_rgb_16
,
743 fetch_texel_3d_signed_rgb_16
,
744 store_texel_signed_rgb_16
747 MESA_FORMAT_SIGNED_RGBA_16
,
748 fetch_texel_1d_signed_rgba_16
,
749 fetch_texel_2d_signed_rgba_16
,
750 fetch_texel_3d_signed_rgba_16
,
751 store_texel_signed_rgba_16
755 fetch_texel_1d_rgba_16
,
756 fetch_texel_2d_rgba_16
,
757 fetch_texel_3d_rgba_16
,
764 _mesa_get_texel_fetch_func(gl_format format
, GLuint dims
)
767 /* check that the table entries are sorted by format name */
769 for (fmt
= 0; fmt
< MESA_FORMAT_COUNT
; fmt
++) {
770 assert(texfetch_funcs
[fmt
].Name
== fmt
);
774 assert(Elements(texfetch_funcs
) == MESA_FORMAT_COUNT
);
775 assert(format
< MESA_FORMAT_COUNT
);
779 return texfetch_funcs
[format
].Fetch1D
;
781 return texfetch_funcs
[format
].Fetch2D
;
783 return texfetch_funcs
[format
].Fetch3D
;
785 assert(0 && "bad dims in _mesa_get_texel_fetch_func");
792 _mesa_get_texel_store_func(gl_format format
)
794 assert(format
< MESA_FORMAT_COUNT
);
795 return texfetch_funcs
[format
].StoreTexel
;
800 * Adaptor for fetching a GLchan texel from a float-valued texture.
803 fetch_texel_float_to_chan(const struct gl_texture_image
*texImage
,
804 GLint i
, GLint j
, GLint k
, GLchan
*texelOut
)
807 GLenum baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
809 ASSERT(texImage
->FetchTexelf
);
810 texImage
->FetchTexelf(texImage
, i
, j
, k
, temp
);
811 if (baseFormat
== GL_DEPTH_COMPONENT
||
812 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
813 /* just one channel */
814 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[0], temp
[0]);
818 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[0], temp
[0]);
819 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[1], temp
[1]);
820 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[2], temp
[2]);
821 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[3], temp
[3]);
828 * Adaptor for fetching a float texel from a GLchan-valued texture.
831 fetch_texel_chan_to_float(const struct gl_texture_image
*texImage
,
832 GLint i
, GLint j
, GLint k
, GLfloat
*texelOut
)
835 GLenum baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
837 ASSERT(texImage
->FetchTexelc
);
838 texImage
->FetchTexelc(texImage
, i
, j
, k
, temp
);
839 if (baseFormat
== GL_DEPTH_COMPONENT
||
840 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
841 /* just one channel */
842 texelOut
[0] = CHAN_TO_FLOAT(temp
[0]);
846 texelOut
[0] = CHAN_TO_FLOAT(temp
[0]);
847 texelOut
[1] = CHAN_TO_FLOAT(temp
[1]);
848 texelOut
[2] = CHAN_TO_FLOAT(temp
[2]);
849 texelOut
[3] = CHAN_TO_FLOAT(temp
[3]);
856 * Initialize the texture image's FetchTexelc and FetchTexelf methods.
859 _mesa_set_fetch_functions(struct gl_texture_image
*texImage
, GLuint dims
)
861 ASSERT(dims
== 1 || dims
== 2 || dims
== 3);
862 GLuint format
= texImage
->TexFormat
;
864 if (texImage
->TexObject
->sRGBDecode
== GL_SKIP_DECODE_EXT
&&
865 _mesa_get_format_color_encoding(format
) == GL_SRGB
) {
866 format
= _mesa_get_srgb_format_linear(format
);
868 texImage
->FetchTexelf
=
869 _mesa_get_texel_fetch_func(format
, dims
);
871 texImage
->FetchTexelc
= fetch_texel_float_to_chan
;
873 ASSERT(texImage
->FetchTexelc
);
874 ASSERT(texImage
->FetchTexelf
);
878 _mesa_update_fetch_functions(struct gl_texture_object
*texObj
)
883 dims
= _mesa_get_texture_dimensions(texObj
->Target
);
885 for (face
= 0; face
< 6; face
++) {
886 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
887 if (texObj
->Image
[face
][i
]) {
888 _mesa_set_fetch_functions(texObj
->Image
[face
][i
], dims
);