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
39 #include "texcompress.h"
40 #include "texcompress_fxt1.h"
41 #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_al88
,
243 fetch_texel_2d_f_al88
,
244 fetch_texel_3d_f_al88
,
248 MESA_FORMAT_AL88_REV
,
249 fetch_texel_1d_f_al88_rev
,
250 fetch_texel_2d_f_al88_rev
,
251 fetch_texel_3d_f_al88_rev
,
256 fetch_texel_1d_f_al1616
,
257 fetch_texel_2d_f_al1616
,
258 fetch_texel_3d_f_al1616
,
262 MESA_FORMAT_AL1616_REV
,
263 fetch_texel_1d_f_al1616_rev
,
264 fetch_texel_2d_f_al1616_rev
,
265 fetch_texel_3d_f_al1616_rev
,
266 store_texel_al1616_rev
270 fetch_texel_1d_f_rgb332
,
271 fetch_texel_2d_f_rgb332
,
272 fetch_texel_3d_f_rgb332
,
298 fetch_texel_1d_f_ci8
,
299 fetch_texel_2d_f_ci8
,
300 fetch_texel_3d_f_ci8
,
305 fetch_texel_1d_f_ycbcr
,
306 fetch_texel_2d_f_ycbcr
,
307 fetch_texel_3d_f_ycbcr
,
311 MESA_FORMAT_YCBCR_REV
,
312 fetch_texel_1d_f_ycbcr_rev
,
313 fetch_texel_2d_f_ycbcr_rev
,
314 fetch_texel_3d_f_ycbcr_rev
,
315 store_texel_ycbcr_rev
319 fetch_texel_1d_f_z24_s8
,
320 fetch_texel_2d_f_z24_s8
,
321 fetch_texel_3d_f_z24_s8
,
326 fetch_texel_1d_f_s8_z24
,
327 fetch_texel_2d_f_s8_z24
,
328 fetch_texel_3d_f_s8_z24
,
333 fetch_texel_1d_f_z16
,
334 fetch_texel_2d_f_z16
,
335 fetch_texel_3d_f_z16
,
340 fetch_texel_1d_f_s8_z24
,
341 fetch_texel_2d_f_s8_z24
,
342 fetch_texel_3d_f_s8_z24
,
347 fetch_texel_1d_f_z24_s8
,
348 fetch_texel_2d_f_z24_s8
,
349 fetch_texel_3d_f_z24_s8
,
354 fetch_texel_1d_f_z32
,
355 fetch_texel_2d_f_z32
,
356 fetch_texel_3d_f_z32
,
368 fetch_texel_1d_srgb8
,
369 fetch_texel_2d_srgb8
,
370 fetch_texel_3d_srgb8
,
375 fetch_texel_1d_srgba8
,
376 fetch_texel_2d_srgba8
,
377 fetch_texel_3d_srgba8
,
382 fetch_texel_1d_sargb8
,
383 fetch_texel_2d_sargb8
,
384 fetch_texel_3d_sargb8
,
402 MESA_FORMAT_SRGB_DXT1
,
404 _mesa_fetch_texel_2d_f_srgb_dxt1
,
409 MESA_FORMAT_SRGBA_DXT1
,
411 _mesa_fetch_texel_2d_f_srgba_dxt1
,
416 MESA_FORMAT_SRGBA_DXT3
,
418 _mesa_fetch_texel_2d_f_srgba_dxt3
,
423 MESA_FORMAT_SRGBA_DXT5
,
425 _mesa_fetch_texel_2d_f_srgba_dxt5
,
431 MESA_FORMAT_RGB_FXT1
,
433 _mesa_fetch_texel_2d_f_rgb_fxt1
,
438 MESA_FORMAT_RGBA_FXT1
,
440 _mesa_fetch_texel_2d_f_rgba_fxt1
,
445 MESA_FORMAT_RGB_DXT1
,
447 _mesa_fetch_texel_2d_f_rgb_dxt1
,
452 MESA_FORMAT_RGBA_DXT1
,
454 _mesa_fetch_texel_2d_f_rgba_dxt1
,
459 MESA_FORMAT_RGBA_DXT3
,
461 _mesa_fetch_texel_2d_f_rgba_dxt3
,
466 MESA_FORMAT_RGBA_DXT5
,
468 _mesa_fetch_texel_2d_f_rgba_dxt5
,
473 MESA_FORMAT_RGBA_FLOAT32
,
474 fetch_texel_1d_f_rgba_f32
,
475 fetch_texel_2d_f_rgba_f32
,
476 fetch_texel_3d_f_rgba_f32
,
480 MESA_FORMAT_RGBA_FLOAT16
,
481 fetch_texel_1d_f_rgba_f16
,
482 fetch_texel_2d_f_rgba_f16
,
483 fetch_texel_3d_f_rgba_f16
,
487 MESA_FORMAT_RGB_FLOAT32
,
488 fetch_texel_1d_f_rgb_f32
,
489 fetch_texel_2d_f_rgb_f32
,
490 fetch_texel_3d_f_rgb_f32
,
494 MESA_FORMAT_RGB_FLOAT16
,
495 fetch_texel_1d_f_rgb_f16
,
496 fetch_texel_2d_f_rgb_f16
,
497 fetch_texel_3d_f_rgb_f16
,
501 MESA_FORMAT_ALPHA_FLOAT32
,
502 fetch_texel_1d_f_alpha_f32
,
503 fetch_texel_2d_f_alpha_f32
,
504 fetch_texel_3d_f_alpha_f32
,
505 store_texel_alpha_f32
508 MESA_FORMAT_ALPHA_FLOAT16
,
509 fetch_texel_1d_f_alpha_f16
,
510 fetch_texel_2d_f_alpha_f16
,
511 fetch_texel_3d_f_alpha_f16
,
512 store_texel_alpha_f16
515 MESA_FORMAT_LUMINANCE_FLOAT32
,
516 fetch_texel_1d_f_luminance_f32
,
517 fetch_texel_2d_f_luminance_f32
,
518 fetch_texel_3d_f_luminance_f32
,
519 store_texel_luminance_f32
522 MESA_FORMAT_LUMINANCE_FLOAT16
,
523 fetch_texel_1d_f_luminance_f16
,
524 fetch_texel_2d_f_luminance_f16
,
525 fetch_texel_3d_f_luminance_f16
,
526 store_texel_luminance_f16
529 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
,
530 fetch_texel_1d_f_luminance_alpha_f32
,
531 fetch_texel_2d_f_luminance_alpha_f32
,
532 fetch_texel_3d_f_luminance_alpha_f32
,
533 store_texel_luminance_alpha_f32
536 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
,
537 fetch_texel_1d_f_luminance_alpha_f16
,
538 fetch_texel_2d_f_luminance_alpha_f16
,
539 fetch_texel_3d_f_luminance_alpha_f16
,
540 store_texel_luminance_alpha_f16
543 MESA_FORMAT_INTENSITY_FLOAT32
,
544 fetch_texel_1d_f_intensity_f32
,
545 fetch_texel_2d_f_intensity_f32
,
546 fetch_texel_3d_f_intensity_f32
,
547 store_texel_intensity_f32
550 MESA_FORMAT_INTENSITY_FLOAT16
,
551 fetch_texel_1d_f_intensity_f16
,
552 fetch_texel_2d_f_intensity_f16
,
553 fetch_texel_3d_f_intensity_f16
,
554 store_texel_intensity_f16
557 /* non-normalized, signed int */
559 MESA_FORMAT_RGBA_INT8
,
560 fetch_texel_1d_rgba_int8
,
561 fetch_texel_2d_rgba_int8
,
562 fetch_texel_3d_rgba_int8
,
563 store_texel_rgba_int8
566 MESA_FORMAT_RGBA_INT16
,
567 fetch_texel_1d_rgba_int16
,
568 fetch_texel_2d_rgba_int16
,
569 fetch_texel_3d_rgba_int16
,
570 store_texel_rgba_int16
573 MESA_FORMAT_RGBA_INT32
,
574 fetch_texel_1d_rgba_int32
,
575 fetch_texel_2d_rgba_int32
,
576 fetch_texel_3d_rgba_int32
,
577 store_texel_rgba_int32
580 /* non-normalized, unsigned int */
582 MESA_FORMAT_RGBA_UINT8
,
583 fetch_texel_1d_rgba_uint8
,
584 fetch_texel_2d_rgba_uint8
,
585 fetch_texel_3d_rgba_uint8
,
586 store_texel_rgba_uint8
589 MESA_FORMAT_RGBA_UINT16
,
590 fetch_texel_1d_rgba_uint16
,
591 fetch_texel_2d_rgba_uint16
,
592 fetch_texel_3d_rgba_uint16
,
593 store_texel_rgba_uint16
596 MESA_FORMAT_RGBA_UINT32
,
597 fetch_texel_1d_rgba_uint32
,
598 fetch_texel_2d_rgba_uint32
,
599 fetch_texel_3d_rgba_uint32
,
600 store_texel_rgba_uint32
606 fetch_texel_1d_dudv8
,
607 fetch_texel_2d_dudv8
,
608 fetch_texel_3d_dudv8
,
612 /* signed, normalized */
614 MESA_FORMAT_SIGNED_R8
,
615 fetch_texel_1d_signed_r8
,
616 fetch_texel_2d_signed_r8
,
617 fetch_texel_3d_signed_r8
,
618 store_texel_signed_r8
621 MESA_FORMAT_SIGNED_RG88
,
622 fetch_texel_1d_signed_rg88
,
623 fetch_texel_2d_signed_rg88
,
624 fetch_texel_3d_signed_rg88
,
625 store_texel_signed_rg88
628 MESA_FORMAT_SIGNED_RGBX8888
,
629 fetch_texel_1d_signed_rgbx8888
,
630 fetch_texel_2d_signed_rgbx8888
,
631 fetch_texel_3d_signed_rgbx8888
,
632 store_texel_signed_rgbx8888
635 MESA_FORMAT_SIGNED_RGBA8888
,
636 fetch_texel_1d_signed_rgba8888
,
637 fetch_texel_2d_signed_rgba8888
,
638 fetch_texel_3d_signed_rgba8888
,
639 store_texel_signed_rgba8888
642 MESA_FORMAT_SIGNED_RGBA8888_REV
,
643 fetch_texel_1d_signed_rgba8888_rev
,
644 fetch_texel_2d_signed_rgba8888_rev
,
645 fetch_texel_3d_signed_rgba8888_rev
,
646 store_texel_signed_rgba8888_rev
649 MESA_FORMAT_SIGNED_R_16
,
650 fetch_texel_1d_signed_r_16
,
651 fetch_texel_2d_signed_r_16
,
652 fetch_texel_3d_signed_r_16
,
653 store_texel_signed_r_16
656 MESA_FORMAT_SIGNED_RG_16
,
657 fetch_texel_1d_signed_rg_16
,
658 fetch_texel_2d_signed_rg_16
,
659 fetch_texel_3d_signed_rg_16
,
660 store_texel_signed_rg_16
663 MESA_FORMAT_SIGNED_RGB_16
,
664 fetch_texel_1d_signed_rgb_16
,
665 fetch_texel_2d_signed_rgb_16
,
666 fetch_texel_3d_signed_rgb_16
,
667 store_texel_signed_rgb_16
670 MESA_FORMAT_SIGNED_RGBA_16
,
671 fetch_texel_1d_signed_rgba_16
,
672 fetch_texel_2d_signed_rgba_16
,
673 fetch_texel_3d_signed_rgba_16
,
674 store_texel_signed_rgba_16
678 fetch_texel_1d_rgba_16
,
679 fetch_texel_2d_rgba_16
,
680 fetch_texel_3d_rgba_16
,
686 static FetchTexelFuncF
687 _mesa_get_texel_fetch_func(gl_format format
, GLuint dims
)
690 /* check that the table entries are sorted by format name */
692 for (fmt
= 0; fmt
< MESA_FORMAT_COUNT
; fmt
++) {
693 assert(texfetch_funcs
[fmt
].Name
== fmt
);
697 assert(Elements(texfetch_funcs
) == MESA_FORMAT_COUNT
);
698 assert(format
< MESA_FORMAT_COUNT
);
702 return texfetch_funcs
[format
].Fetch1D
;
704 return texfetch_funcs
[format
].Fetch2D
;
706 return texfetch_funcs
[format
].Fetch3D
;
708 assert(0 && "bad dims in _mesa_get_texel_fetch_func");
715 _mesa_get_texel_store_func(gl_format format
)
717 assert(format
< MESA_FORMAT_COUNT
);
718 return texfetch_funcs
[format
].StoreTexel
;
723 * Adaptor for fetching a GLchan texel from a float-valued texture.
726 fetch_texel_float_to_chan(const struct gl_texture_image
*texImage
,
727 GLint i
, GLint j
, GLint k
, GLchan
*texelOut
)
730 GLenum baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
732 ASSERT(texImage
->FetchTexelf
);
733 texImage
->FetchTexelf(texImage
, i
, j
, k
, temp
);
734 if (baseFormat
== GL_DEPTH_COMPONENT
||
735 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
736 /* just one channel */
737 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[0], temp
[0]);
741 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[0], temp
[0]);
742 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[1], temp
[1]);
743 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[2], temp
[2]);
744 UNCLAMPED_FLOAT_TO_CHAN(texelOut
[3], temp
[3]);
751 * Adaptor for fetching a float texel from a GLchan-valued texture.
754 fetch_texel_chan_to_float(const struct gl_texture_image
*texImage
,
755 GLint i
, GLint j
, GLint k
, GLfloat
*texelOut
)
758 GLenum baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
760 ASSERT(texImage
->FetchTexelc
);
761 texImage
->FetchTexelc(texImage
, i
, j
, k
, temp
);
762 if (baseFormat
== GL_DEPTH_COMPONENT
||
763 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
764 /* just one channel */
765 texelOut
[0] = CHAN_TO_FLOAT(temp
[0]);
769 texelOut
[0] = CHAN_TO_FLOAT(temp
[0]);
770 texelOut
[1] = CHAN_TO_FLOAT(temp
[1]);
771 texelOut
[2] = CHAN_TO_FLOAT(temp
[2]);
772 texelOut
[3] = CHAN_TO_FLOAT(temp
[3]);
779 * Initialize the texture image's FetchTexelc and FetchTexelf methods.
782 _mesa_set_fetch_functions(struct gl_texture_image
*texImage
, GLuint dims
)
784 ASSERT(dims
== 1 || dims
== 2 || dims
== 3);
785 ASSERT(texImage
->TexFormat
);
787 if (!texImage
->FetchTexelf
) {
788 texImage
->FetchTexelf
=
789 _mesa_get_texel_fetch_func(texImage
->TexFormat
, dims
);
792 /* now check if we need to use a float/chan adaptor */
793 if (!texImage
->FetchTexelc
) {
794 texImage
->FetchTexelc
= fetch_texel_float_to_chan
;
797 ASSERT(texImage
->FetchTexelc
);
798 ASSERT(texImage
->FetchTexelf
);