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
) 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
] =
135 MESA_FORMAT_RGBA8888
,
136 fetch_texel_1d_f_rgba8888
,
137 fetch_texel_2d_f_rgba8888
,
138 fetch_texel_3d_f_rgba8888
,
142 MESA_FORMAT_RGBA8888_REV
,
143 fetch_texel_1d_f_rgba8888_rev
,
144 fetch_texel_2d_f_rgba8888_rev
,
145 fetch_texel_3d_f_rgba8888_rev
,
146 store_texel_rgba8888_rev
149 MESA_FORMAT_ARGB8888
,
150 fetch_texel_1d_f_argb8888
,
151 fetch_texel_2d_f_argb8888
,
152 fetch_texel_3d_f_argb8888
,
156 MESA_FORMAT_ARGB8888_REV
,
157 fetch_texel_1d_f_argb8888_rev
,
158 fetch_texel_2d_f_argb8888_rev
,
159 fetch_texel_3d_f_argb8888_rev
,
160 store_texel_argb8888_rev
163 MESA_FORMAT_XRGB8888
,
164 fetch_texel_1d_f_xrgb8888
,
165 fetch_texel_2d_f_xrgb8888
,
166 fetch_texel_3d_f_xrgb8888
,
170 MESA_FORMAT_XRGB8888_REV
,
171 fetch_texel_1d_f_xrgb8888_rev
,
172 fetch_texel_2d_f_xrgb8888_rev
,
173 fetch_texel_3d_f_xrgb8888_rev
,
174 store_texel_xrgb8888_rev
,
178 fetch_texel_1d_f_rgb888
,
179 fetch_texel_2d_f_rgb888
,
180 fetch_texel_3d_f_rgb888
,
185 fetch_texel_1d_f_bgr888
,
186 fetch_texel_2d_f_bgr888
,
187 fetch_texel_3d_f_bgr888
,
192 fetch_texel_1d_f_rgb565
,
193 fetch_texel_2d_f_rgb565
,
194 fetch_texel_3d_f_rgb565
,
198 MESA_FORMAT_RGB565_REV
,
199 fetch_texel_1d_f_rgb565_rev
,
200 fetch_texel_2d_f_rgb565_rev
,
201 fetch_texel_3d_f_rgb565_rev
,
202 store_texel_rgb565_rev
205 MESA_FORMAT_ARGB4444
,
206 fetch_texel_1d_f_argb4444
,
207 fetch_texel_2d_f_argb4444
,
208 fetch_texel_3d_f_argb4444
,
212 MESA_FORMAT_ARGB4444_REV
,
213 fetch_texel_1d_f_argb4444_rev
,
214 fetch_texel_2d_f_argb4444_rev
,
215 fetch_texel_3d_f_argb4444_rev
,
216 store_texel_argb4444_rev
219 MESA_FORMAT_RGBA5551
,
220 fetch_texel_1d_f_rgba5551
,
221 fetch_texel_2d_f_rgba5551
,
222 fetch_texel_3d_f_rgba5551
,
226 MESA_FORMAT_ARGB1555
,
227 fetch_texel_1d_f_argb1555
,
228 fetch_texel_2d_f_argb1555
,
229 fetch_texel_3d_f_argb1555
,
233 MESA_FORMAT_ARGB1555_REV
,
234 fetch_texel_1d_f_argb1555_rev
,
235 fetch_texel_2d_f_argb1555_rev
,
236 fetch_texel_3d_f_argb1555_rev
,
237 store_texel_argb1555_rev
241 fetch_texel_1d_f_al44
,
242 fetch_texel_2d_f_al44
,
243 fetch_texel_3d_f_al44
,
248 fetch_texel_1d_f_al88
,
249 fetch_texel_2d_f_al88
,
250 fetch_texel_3d_f_al88
,
254 MESA_FORMAT_AL88_REV
,
255 fetch_texel_1d_f_al88_rev
,
256 fetch_texel_2d_f_al88_rev
,
257 fetch_texel_3d_f_al88_rev
,
262 fetch_texel_1d_f_al1616
,
263 fetch_texel_2d_f_al1616
,
264 fetch_texel_3d_f_al1616
,
268 MESA_FORMAT_AL1616_REV
,
269 fetch_texel_1d_f_al1616_rev
,
270 fetch_texel_2d_f_al1616_rev
,
271 fetch_texel_3d_f_al1616_rev
,
272 store_texel_al1616_rev
276 fetch_texel_1d_f_rgb332
,
277 fetch_texel_2d_f_rgb332
,
278 fetch_texel_3d_f_rgb332
,
290 fetch_texel_1d_f_a16
,
291 fetch_texel_2d_f_a16
,
292 fetch_texel_3d_f_a16
,
311 fetch_texel_1d_f_ci8
,
312 fetch_texel_2d_f_ci8
,
313 fetch_texel_3d_f_ci8
,
318 fetch_texel_1d_f_ycbcr
,
319 fetch_texel_2d_f_ycbcr
,
320 fetch_texel_3d_f_ycbcr
,
324 MESA_FORMAT_YCBCR_REV
,
325 fetch_texel_1d_f_ycbcr_rev
,
326 fetch_texel_2d_f_ycbcr_rev
,
327 fetch_texel_3d_f_ycbcr_rev
,
328 store_texel_ycbcr_rev
339 fetch_texel_1d_f_rg88
,
340 fetch_texel_2d_f_rg88
,
341 fetch_texel_3d_f_rg88
,
345 MESA_FORMAT_RG88_REV
,
346 fetch_texel_1d_f_rg88_rev
,
347 fetch_texel_2d_f_rg88_rev
,
348 fetch_texel_3d_f_rg88_rev
,
349 store_texel_rg88_rev
,
353 fetch_texel_1d_f_r16
,
354 fetch_texel_2d_f_r16
,
355 fetch_texel_3d_f_r16
,
360 fetch_texel_1d_f_rg1616
,
361 fetch_texel_2d_f_rg1616
,
362 fetch_texel_3d_f_rg1616
,
366 MESA_FORMAT_RG1616_REV
,
367 fetch_texel_1d_f_rg1616_rev
,
368 fetch_texel_2d_f_rg1616_rev
,
369 fetch_texel_3d_f_rg1616_rev
,
370 store_texel_rg1616_rev
,
373 MESA_FORMAT_ARGB2101010
,
374 fetch_texel_1d_f_argb2101010
,
375 fetch_texel_2d_f_argb2101010
,
376 fetch_texel_3d_f_argb2101010
,
377 store_texel_argb2101010
381 fetch_texel_1d_f_z24_s8
,
382 fetch_texel_2d_f_z24_s8
,
383 fetch_texel_3d_f_z24_s8
,
388 fetch_texel_1d_f_s8_z24
,
389 fetch_texel_2d_f_s8_z24
,
390 fetch_texel_3d_f_s8_z24
,
395 fetch_texel_1d_f_z16
,
396 fetch_texel_2d_f_z16
,
397 fetch_texel_3d_f_z16
,
402 fetch_texel_1d_f_s8_z24
,
403 fetch_texel_2d_f_s8_z24
,
404 fetch_texel_3d_f_s8_z24
,
409 fetch_texel_1d_f_z24_s8
,
410 fetch_texel_2d_f_z24_s8
,
411 fetch_texel_3d_f_z24_s8
,
416 fetch_texel_1d_f_z32
,
417 fetch_texel_2d_f_z32
,
418 fetch_texel_3d_f_z32
,
430 fetch_texel_1d_srgb8
,
431 fetch_texel_2d_srgb8
,
432 fetch_texel_3d_srgb8
,
437 fetch_texel_1d_srgba8
,
438 fetch_texel_2d_srgba8
,
439 fetch_texel_3d_srgba8
,
444 fetch_texel_1d_sargb8
,
445 fetch_texel_2d_sargb8
,
446 fetch_texel_3d_sargb8
,
464 MESA_FORMAT_SRGB_DXT1
,
466 _mesa_fetch_texel_2d_f_srgb_dxt1
,
471 MESA_FORMAT_SRGBA_DXT1
,
473 _mesa_fetch_texel_2d_f_srgba_dxt1
,
478 MESA_FORMAT_SRGBA_DXT3
,
480 _mesa_fetch_texel_2d_f_srgba_dxt3
,
485 MESA_FORMAT_SRGBA_DXT5
,
487 _mesa_fetch_texel_2d_f_srgba_dxt5
,
493 MESA_FORMAT_RGB_FXT1
,
495 _mesa_fetch_texel_2d_f_rgb_fxt1
,
500 MESA_FORMAT_RGBA_FXT1
,
502 _mesa_fetch_texel_2d_f_rgba_fxt1
,
507 MESA_FORMAT_RGB_DXT1
,
509 _mesa_fetch_texel_2d_f_rgb_dxt1
,
514 MESA_FORMAT_RGBA_DXT1
,
516 _mesa_fetch_texel_2d_f_rgba_dxt1
,
521 MESA_FORMAT_RGBA_DXT3
,
523 _mesa_fetch_texel_2d_f_rgba_dxt3
,
528 MESA_FORMAT_RGBA_DXT5
,
530 _mesa_fetch_texel_2d_f_rgba_dxt5
,
535 MESA_FORMAT_RGBA_FLOAT32
,
536 fetch_texel_1d_f_rgba_f32
,
537 fetch_texel_2d_f_rgba_f32
,
538 fetch_texel_3d_f_rgba_f32
,
542 MESA_FORMAT_RGBA_FLOAT16
,
543 fetch_texel_1d_f_rgba_f16
,
544 fetch_texel_2d_f_rgba_f16
,
545 fetch_texel_3d_f_rgba_f16
,
549 MESA_FORMAT_RGB_FLOAT32
,
550 fetch_texel_1d_f_rgb_f32
,
551 fetch_texel_2d_f_rgb_f32
,
552 fetch_texel_3d_f_rgb_f32
,
556 MESA_FORMAT_RGB_FLOAT16
,
557 fetch_texel_1d_f_rgb_f16
,
558 fetch_texel_2d_f_rgb_f16
,
559 fetch_texel_3d_f_rgb_f16
,
563 MESA_FORMAT_ALPHA_FLOAT32
,
564 fetch_texel_1d_f_alpha_f32
,
565 fetch_texel_2d_f_alpha_f32
,
566 fetch_texel_3d_f_alpha_f32
,
567 store_texel_alpha_f32
570 MESA_FORMAT_ALPHA_FLOAT16
,
571 fetch_texel_1d_f_alpha_f16
,
572 fetch_texel_2d_f_alpha_f16
,
573 fetch_texel_3d_f_alpha_f16
,
574 store_texel_alpha_f16
577 MESA_FORMAT_LUMINANCE_FLOAT32
,
578 fetch_texel_1d_f_luminance_f32
,
579 fetch_texel_2d_f_luminance_f32
,
580 fetch_texel_3d_f_luminance_f32
,
581 store_texel_luminance_f32
584 MESA_FORMAT_LUMINANCE_FLOAT16
,
585 fetch_texel_1d_f_luminance_f16
,
586 fetch_texel_2d_f_luminance_f16
,
587 fetch_texel_3d_f_luminance_f16
,
588 store_texel_luminance_f16
591 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
,
592 fetch_texel_1d_f_luminance_alpha_f32
,
593 fetch_texel_2d_f_luminance_alpha_f32
,
594 fetch_texel_3d_f_luminance_alpha_f32
,
595 store_texel_luminance_alpha_f32
598 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
,
599 fetch_texel_1d_f_luminance_alpha_f16
,
600 fetch_texel_2d_f_luminance_alpha_f16
,
601 fetch_texel_3d_f_luminance_alpha_f16
,
602 store_texel_luminance_alpha_f16
605 MESA_FORMAT_INTENSITY_FLOAT32
,
606 fetch_texel_1d_f_intensity_f32
,
607 fetch_texel_2d_f_intensity_f32
,
608 fetch_texel_3d_f_intensity_f32
,
609 store_texel_intensity_f32
612 MESA_FORMAT_INTENSITY_FLOAT16
,
613 fetch_texel_1d_f_intensity_f16
,
614 fetch_texel_2d_f_intensity_f16
,
615 fetch_texel_3d_f_intensity_f16
,
616 store_texel_intensity_f16
619 /* non-normalized, signed int */
621 MESA_FORMAT_RGBA_INT8
,
622 fetch_texel_1d_rgba_int8
,
623 fetch_texel_2d_rgba_int8
,
624 fetch_texel_3d_rgba_int8
,
625 store_texel_rgba_int8
628 MESA_FORMAT_RGBA_INT16
,
629 fetch_texel_1d_rgba_int16
,
630 fetch_texel_2d_rgba_int16
,
631 fetch_texel_3d_rgba_int16
,
632 store_texel_rgba_int16
635 MESA_FORMAT_RGBA_INT32
,
636 fetch_texel_1d_rgba_int32
,
637 fetch_texel_2d_rgba_int32
,
638 fetch_texel_3d_rgba_int32
,
639 store_texel_rgba_int32
642 /* non-normalized, unsigned int */
644 MESA_FORMAT_RGBA_UINT8
,
645 fetch_texel_1d_rgba_uint8
,
646 fetch_texel_2d_rgba_uint8
,
647 fetch_texel_3d_rgba_uint8
,
648 store_texel_rgba_uint8
651 MESA_FORMAT_RGBA_UINT16
,
652 fetch_texel_1d_rgba_uint16
,
653 fetch_texel_2d_rgba_uint16
,
654 fetch_texel_3d_rgba_uint16
,
655 store_texel_rgba_uint16
658 MESA_FORMAT_RGBA_UINT32
,
659 fetch_texel_1d_rgba_uint32
,
660 fetch_texel_2d_rgba_uint32
,
661 fetch_texel_3d_rgba_uint32
,
662 store_texel_rgba_uint32
668 fetch_texel_1d_dudv8
,
669 fetch_texel_2d_dudv8
,
670 fetch_texel_3d_dudv8
,
674 /* signed, normalized */
676 MESA_FORMAT_SIGNED_R8
,
677 fetch_texel_1d_signed_r8
,
678 fetch_texel_2d_signed_r8
,
679 fetch_texel_3d_signed_r8
,
680 store_texel_signed_r8
683 MESA_FORMAT_SIGNED_RG88
,
684 fetch_texel_1d_signed_rg88
,
685 fetch_texel_2d_signed_rg88
,
686 fetch_texel_3d_signed_rg88
,
687 store_texel_signed_rg88
690 MESA_FORMAT_SIGNED_RGBX8888
,
691 fetch_texel_1d_signed_rgbx8888
,
692 fetch_texel_2d_signed_rgbx8888
,
693 fetch_texel_3d_signed_rgbx8888
,
694 store_texel_signed_rgbx8888
697 MESA_FORMAT_SIGNED_RGBA8888
,
698 fetch_texel_1d_signed_rgba8888
,
699 fetch_texel_2d_signed_rgba8888
,
700 fetch_texel_3d_signed_rgba8888
,
701 store_texel_signed_rgba8888
704 MESA_FORMAT_SIGNED_RGBA8888_REV
,
705 fetch_texel_1d_signed_rgba8888_rev
,
706 fetch_texel_2d_signed_rgba8888_rev
,
707 fetch_texel_3d_signed_rgba8888_rev
,
708 store_texel_signed_rgba8888_rev
711 MESA_FORMAT_SIGNED_R_16
,
712 fetch_texel_1d_signed_r_16
,
713 fetch_texel_2d_signed_r_16
,
714 fetch_texel_3d_signed_r_16
,
715 store_texel_signed_r_16
718 MESA_FORMAT_SIGNED_RG_16
,
719 fetch_texel_1d_signed_rg_16
,
720 fetch_texel_2d_signed_rg_16
,
721 fetch_texel_3d_signed_rg_16
,
722 store_texel_signed_rg_16
725 MESA_FORMAT_SIGNED_RGB_16
,
726 fetch_texel_1d_signed_rgb_16
,
727 fetch_texel_2d_signed_rgb_16
,
728 fetch_texel_3d_signed_rgb_16
,
729 store_texel_signed_rgb_16
732 MESA_FORMAT_SIGNED_RGBA_16
,
733 fetch_texel_1d_signed_rgba_16
,
734 fetch_texel_2d_signed_rgba_16
,
735 fetch_texel_3d_signed_rgba_16
,
736 store_texel_signed_rgba_16
740 fetch_texel_1d_rgba_16
,
741 fetch_texel_2d_rgba_16
,
742 fetch_texel_3d_rgba_16
,
748 static FetchTexelFuncF
749 _mesa_get_texel_fetch_func(gl_format format
, GLuint dims
)
752 /* check that the table entries are sorted by format name */
754 for (fmt
= 0; fmt
< MESA_FORMAT_COUNT
; fmt
++) {
755 assert(texfetch_funcs
[fmt
].Name
== fmt
);
759 assert(Elements(texfetch_funcs
) == MESA_FORMAT_COUNT
);
760 assert(format
< MESA_FORMAT_COUNT
);
764 return texfetch_funcs
[format
].Fetch1D
;
766 return texfetch_funcs
[format
].Fetch2D
;
768 return texfetch_funcs
[format
].Fetch3D
;
770 assert(0 && "bad dims in _mesa_get_texel_fetch_func");
777 _mesa_get_texel_store_func(gl_format format
)
779 assert(format
< MESA_FORMAT_COUNT
);
780 return texfetch_funcs
[format
].StoreTexel
;
785 * Adaptor for fetching a GLchan texel from a float-valued texture.
788 fetch_texel_float_to_chan(const struct gl_texture_image
*texImage
,
789 GLint i
, GLint j
, GLint k
, GLchan
*texelOut
)
792 GLenum baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
794 ASSERT(texImage
->FetchTexelf
);
795 texImage
->FetchTexelf(texImage
, i
, j
, k
, temp
);
796 if (baseFormat
== GL_DEPTH_COMPONENT
||
797 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
798 /* just one channel */
799 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[0], temp
[0]);
803 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[0], temp
[0]);
804 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[1], temp
[1]);
805 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[2], temp
[2]);
806 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[3], temp
[3]);
813 * Adaptor for fetching a float texel from a GLchan-valued texture.
816 fetch_texel_chan_to_float(const struct gl_texture_image
*texImage
,
817 GLint i
, GLint j
, GLint k
, GLfloat
*texelOut
)
820 GLenum baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
822 ASSERT(texImage
->FetchTexelc
);
823 texImage
->FetchTexelc(texImage
, i
, j
, k
, temp
);
824 if (baseFormat
== GL_DEPTH_COMPONENT
||
825 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
826 /* just one channel */
827 texelOut
[0] = CHAN_TO_FLOAT(temp
[0]);
831 texelOut
[0] = CHAN_TO_FLOAT(temp
[0]);
832 texelOut
[1] = CHAN_TO_FLOAT(temp
[1]);
833 texelOut
[2] = CHAN_TO_FLOAT(temp
[2]);
834 texelOut
[3] = CHAN_TO_FLOAT(temp
[3]);
841 * Initialize the texture image's FetchTexelc and FetchTexelf methods.
844 _mesa_set_fetch_functions(struct gl_texture_image
*texImage
, GLuint dims
)
846 ASSERT(dims
== 1 || dims
== 2 || dims
== 3);
848 texImage
->FetchTexelf
=
849 _mesa_get_texel_fetch_func(texImage
->TexFormat
, dims
);
851 texImage
->FetchTexelc
= fetch_texel_float_to_chan
;
853 ASSERT(texImage
->FetchTexelc
);
854 ASSERT(texImage
->FetchTexelf
);