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
36 #include "main/colormac.h"
37 #include "main/macros.h"
38 #include "main/teximage.h"
39 #include "s_context.h"
40 #include "s_texfetch.h"
44 * Convert an 8-bit sRGB value from non-linear space to a
45 * linear RGB value in [0, 1].
46 * Implemented with a 256-entry lookup table.
49 nonlinear_to_linear(GLubyte cs8
)
51 static GLfloat table
[256];
52 static GLboolean tableReady
= GL_FALSE
;
54 /* compute lookup table now */
56 for (i
= 0; i
< 256; i
++) {
57 const GLfloat cs
= UBYTE_TO_FLOAT(i
);
59 table
[i
] = cs
/ 12.92f
;
62 table
[i
] = (GLfloat
) pow((cs
+ 0.055) / 1.055, 2.4);
72 /* Texel fetch routines for all supported formats
75 #include "s_texfetch_tmp.h"
78 #include "s_texfetch_tmp.h"
81 #include "s_texfetch_tmp.h"
84 * Null texel fetch function.
86 * Have to have this so the FetchTexel function pointer is never NULL.
88 static void fetch_null_texelf( const struct swrast_texture_image
*texImage
,
89 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
91 (void) texImage
; (void) i
; (void) j
; (void) k
;
96 _mesa_warning(NULL
, "fetch_null_texelf() called!");
101 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
102 * XXX this is somewhat temporary.
106 FetchTexelFunc Fetch1D
;
107 FetchTexelFunc Fetch2D
;
108 FetchTexelFunc Fetch3D
;
110 texfetch_funcs
[MESA_FORMAT_COUNT
] =
120 MESA_FORMAT_RGBA8888
,
121 fetch_texel_1d_f_rgba8888
,
122 fetch_texel_2d_f_rgba8888
,
123 fetch_texel_3d_f_rgba8888
126 MESA_FORMAT_RGBA8888_REV
,
127 fetch_texel_1d_f_rgba8888_rev
,
128 fetch_texel_2d_f_rgba8888_rev
,
129 fetch_texel_3d_f_rgba8888_rev
132 MESA_FORMAT_ARGB8888
,
133 fetch_texel_1d_f_argb8888
,
134 fetch_texel_2d_f_argb8888
,
135 fetch_texel_3d_f_argb8888
138 MESA_FORMAT_ARGB8888_REV
,
139 fetch_texel_1d_f_argb8888_rev
,
140 fetch_texel_2d_f_argb8888_rev
,
141 fetch_texel_3d_f_argb8888_rev
144 MESA_FORMAT_RGBX8888
,
145 fetch_texel_1d_f_rgbx8888
,
146 fetch_texel_2d_f_rgbx8888
,
147 fetch_texel_3d_f_rgbx8888
150 MESA_FORMAT_RGBX8888_REV
,
151 fetch_texel_1d_f_rgbx8888_rev
,
152 fetch_texel_2d_f_rgbx8888_rev
,
153 fetch_texel_3d_f_rgbx8888_rev
156 MESA_FORMAT_XRGB8888
,
157 fetch_texel_1d_f_xrgb8888
,
158 fetch_texel_2d_f_xrgb8888
,
159 fetch_texel_3d_f_xrgb8888
162 MESA_FORMAT_XRGB8888_REV
,
163 fetch_texel_1d_f_xrgb8888_rev
,
164 fetch_texel_2d_f_xrgb8888_rev
,
165 fetch_texel_3d_f_xrgb8888_rev
169 fetch_texel_1d_f_rgb888
,
170 fetch_texel_2d_f_rgb888
,
171 fetch_texel_3d_f_rgb888
175 fetch_texel_1d_f_bgr888
,
176 fetch_texel_2d_f_bgr888
,
177 fetch_texel_3d_f_bgr888
181 fetch_texel_1d_f_rgb565
,
182 fetch_texel_2d_f_rgb565
,
183 fetch_texel_3d_f_rgb565
186 MESA_FORMAT_RGB565_REV
,
187 fetch_texel_1d_f_rgb565_rev
,
188 fetch_texel_2d_f_rgb565_rev
,
189 fetch_texel_3d_f_rgb565_rev
192 MESA_FORMAT_ARGB4444
,
193 fetch_texel_1d_f_argb4444
,
194 fetch_texel_2d_f_argb4444
,
195 fetch_texel_3d_f_argb4444
198 MESA_FORMAT_ARGB4444_REV
,
199 fetch_texel_1d_f_argb4444_rev
,
200 fetch_texel_2d_f_argb4444_rev
,
201 fetch_texel_3d_f_argb4444_rev
204 MESA_FORMAT_RGBA5551
,
205 fetch_texel_1d_f_rgba5551
,
206 fetch_texel_2d_f_rgba5551
,
207 fetch_texel_3d_f_rgba5551
210 MESA_FORMAT_ARGB1555
,
211 fetch_texel_1d_f_argb1555
,
212 fetch_texel_2d_f_argb1555
,
213 fetch_texel_3d_f_argb1555
216 MESA_FORMAT_ARGB1555_REV
,
217 fetch_texel_1d_f_argb1555_rev
,
218 fetch_texel_2d_f_argb1555_rev
,
219 fetch_texel_3d_f_argb1555_rev
223 fetch_texel_1d_f_al44
,
224 fetch_texel_2d_f_al44
,
225 fetch_texel_3d_f_al44
229 fetch_texel_1d_f_al88
,
230 fetch_texel_2d_f_al88
,
231 fetch_texel_3d_f_al88
234 MESA_FORMAT_AL88_REV
,
235 fetch_texel_1d_f_al88_rev
,
236 fetch_texel_2d_f_al88_rev
,
237 fetch_texel_3d_f_al88_rev
241 fetch_texel_1d_f_al1616
,
242 fetch_texel_2d_f_al1616
,
243 fetch_texel_3d_f_al1616
246 MESA_FORMAT_AL1616_REV
,
247 fetch_texel_1d_f_al1616_rev
,
248 fetch_texel_2d_f_al1616_rev
,
249 fetch_texel_3d_f_al1616_rev
253 fetch_texel_1d_f_rgb332
,
254 fetch_texel_2d_f_rgb332
,
255 fetch_texel_3d_f_rgb332
265 fetch_texel_1d_f_a16
,
266 fetch_texel_2d_f_a16
,
277 fetch_texel_1d_f_l16
,
278 fetch_texel_2d_f_l16
,
289 fetch_texel_1d_f_i16
,
290 fetch_texel_2d_f_i16
,
295 fetch_texel_1d_f_ycbcr
,
296 fetch_texel_2d_f_ycbcr
,
297 fetch_texel_3d_f_ycbcr
300 MESA_FORMAT_YCBCR_REV
,
301 fetch_texel_1d_f_ycbcr_rev
,
302 fetch_texel_2d_f_ycbcr_rev
,
303 fetch_texel_3d_f_ycbcr_rev
307 fetch_texel_1d_f_z24_s8
,
308 fetch_texel_2d_f_z24_s8
,
309 fetch_texel_3d_f_z24_s8
313 fetch_texel_1d_f_s8_z24
,
314 fetch_texel_2d_f_s8_z24
,
315 fetch_texel_3d_f_s8_z24
319 fetch_texel_1d_f_z16
,
320 fetch_texel_2d_f_z16
,
325 fetch_texel_1d_f_s8_z24
,
326 fetch_texel_2d_f_s8_z24
,
327 fetch_texel_3d_f_s8_z24
331 fetch_texel_1d_f_z24_s8
,
332 fetch_texel_2d_f_z24_s8
,
333 fetch_texel_3d_f_z24_s8
337 fetch_texel_1d_f_z32
,
338 fetch_texel_2d_f_z32
,
349 fetch_texel_1d_srgb8
,
350 fetch_texel_2d_srgb8
,
355 fetch_texel_1d_srgba8
,
356 fetch_texel_2d_srgba8
,
357 fetch_texel_3d_srgba8
361 fetch_texel_1d_sargb8
,
362 fetch_texel_2d_sargb8
,
363 fetch_texel_3d_sargb8
378 MESA_FORMAT_RGBA_FLOAT32
,
379 fetch_texel_1d_f_rgba_f32
,
380 fetch_texel_2d_f_rgba_f32
,
381 fetch_texel_3d_f_rgba_f32
384 MESA_FORMAT_RGBA_FLOAT16
,
385 fetch_texel_1d_f_rgba_f16
,
386 fetch_texel_2d_f_rgba_f16
,
387 fetch_texel_3d_f_rgba_f16
390 MESA_FORMAT_RGB_FLOAT32
,
391 fetch_texel_1d_f_rgb_f32
,
392 fetch_texel_2d_f_rgb_f32
,
393 fetch_texel_3d_f_rgb_f32
396 MESA_FORMAT_RGB_FLOAT16
,
397 fetch_texel_1d_f_rgb_f16
,
398 fetch_texel_2d_f_rgb_f16
,
399 fetch_texel_3d_f_rgb_f16
402 MESA_FORMAT_ALPHA_FLOAT32
,
403 fetch_texel_1d_f_alpha_f32
,
404 fetch_texel_2d_f_alpha_f32
,
405 fetch_texel_3d_f_alpha_f32
408 MESA_FORMAT_ALPHA_FLOAT16
,
409 fetch_texel_1d_f_alpha_f16
,
410 fetch_texel_2d_f_alpha_f16
,
411 fetch_texel_3d_f_alpha_f16
414 MESA_FORMAT_LUMINANCE_FLOAT32
,
415 fetch_texel_1d_f_luminance_f32
,
416 fetch_texel_2d_f_luminance_f32
,
417 fetch_texel_3d_f_luminance_f32
420 MESA_FORMAT_LUMINANCE_FLOAT16
,
421 fetch_texel_1d_f_luminance_f16
,
422 fetch_texel_2d_f_luminance_f16
,
423 fetch_texel_3d_f_luminance_f16
426 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
,
427 fetch_texel_1d_f_luminance_alpha_f32
,
428 fetch_texel_2d_f_luminance_alpha_f32
,
429 fetch_texel_3d_f_luminance_alpha_f32
432 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
,
433 fetch_texel_1d_f_luminance_alpha_f16
,
434 fetch_texel_2d_f_luminance_alpha_f16
,
435 fetch_texel_3d_f_luminance_alpha_f16
438 MESA_FORMAT_INTENSITY_FLOAT32
,
439 fetch_texel_1d_f_intensity_f32
,
440 fetch_texel_2d_f_intensity_f32
,
441 fetch_texel_3d_f_intensity_f32
444 MESA_FORMAT_INTENSITY_FLOAT16
,
445 fetch_texel_1d_f_intensity_f16
,
446 fetch_texel_2d_f_intensity_f16
,
447 fetch_texel_3d_f_intensity_f16
451 MESA_FORMAT_ALPHA_UINT8
,
458 MESA_FORMAT_ALPHA_UINT16
,
465 MESA_FORMAT_ALPHA_UINT32
,
472 MESA_FORMAT_ALPHA_INT8
,
479 MESA_FORMAT_ALPHA_INT16
,
486 MESA_FORMAT_ALPHA_INT32
,
494 MESA_FORMAT_INTENSITY_UINT8
,
501 MESA_FORMAT_INTENSITY_UINT16
,
508 MESA_FORMAT_INTENSITY_UINT32
,
515 MESA_FORMAT_INTENSITY_INT8
,
522 MESA_FORMAT_INTENSITY_INT16
,
529 MESA_FORMAT_INTENSITY_INT32
,
537 MESA_FORMAT_LUMINANCE_UINT8
,
544 MESA_FORMAT_LUMINANCE_UINT16
,
551 MESA_FORMAT_LUMINANCE_UINT32
,
558 MESA_FORMAT_LUMINANCE_INT8
,
565 MESA_FORMAT_LUMINANCE_INT16
,
572 MESA_FORMAT_LUMINANCE_INT32
,
580 MESA_FORMAT_LUMINANCE_ALPHA_UINT8
,
587 MESA_FORMAT_LUMINANCE_ALPHA_UINT16
,
594 MESA_FORMAT_LUMINANCE_ALPHA_UINT32
,
601 MESA_FORMAT_LUMINANCE_ALPHA_INT8
,
608 MESA_FORMAT_LUMINANCE_ALPHA_INT16
,
615 MESA_FORMAT_LUMINANCE_ALPHA_INT32
,
622 MESA_FORMAT_RGB_INT8
,
628 /* non-normalized, signed int */
630 MESA_FORMAT_RGBA_INT8
,
631 fetch_texel_1d_rgba_int8
,
632 fetch_texel_2d_rgba_int8
,
633 fetch_texel_3d_rgba_int8
636 MESA_FORMAT_RGB_INT16
,
642 MESA_FORMAT_RGBA_INT16
,
643 fetch_texel_1d_rgba_int16
,
644 fetch_texel_2d_rgba_int16
,
645 fetch_texel_3d_rgba_int16
648 MESA_FORMAT_RGB_INT32
,
654 MESA_FORMAT_RGBA_INT32
,
655 fetch_texel_1d_rgba_int32
,
656 fetch_texel_2d_rgba_int32
,
657 fetch_texel_3d_rgba_int32
660 /* non-normalized, unsigned int */
662 MESA_FORMAT_RGB_UINT8
,
668 MESA_FORMAT_RGBA_UINT8
,
669 fetch_texel_1d_rgba_uint8
,
670 fetch_texel_2d_rgba_uint8
,
671 fetch_texel_3d_rgba_uint8
674 MESA_FORMAT_RGB_UINT16
,
680 MESA_FORMAT_RGBA_UINT16
,
681 fetch_texel_1d_rgba_uint16
,
682 fetch_texel_2d_rgba_uint16
,
683 fetch_texel_3d_rgba_uint16
686 MESA_FORMAT_RGB_UINT32
,
692 MESA_FORMAT_RGBA_UINT32
,
693 fetch_texel_1d_rgba_uint32
,
694 fetch_texel_2d_rgba_uint32
,
695 fetch_texel_3d_rgba_uint32
701 fetch_texel_1d_dudv8
,
702 fetch_texel_2d_dudv8
,
706 /* signed, normalized */
708 MESA_FORMAT_SIGNED_RGBA_16
,
709 fetch_texel_1d_signed_rgba_16
,
710 fetch_texel_2d_signed_rgba_16
,
711 fetch_texel_3d_signed_rgba_16
715 fetch_texel_1d_rgba_16
,
716 fetch_texel_2d_rgba_16
,
717 fetch_texel_3d_rgba_16
720 MESA_FORMAT_Z32_FLOAT
,
721 fetch_texel_1d_f_r_f32
, /* Reuse the R32F functions. */
722 fetch_texel_2d_f_r_f32
,
723 fetch_texel_3d_f_r_f32
726 MESA_FORMAT_Z32_FLOAT_X24S8
,
727 fetch_texel_1d_z32f_x24s8
,
728 fetch_texel_2d_z32f_x24s8
,
729 fetch_texel_3d_z32f_x24s8
735 _mesa_get_texel_fetch_func(gl_format format
, GLuint dims
)
738 /* check that the table entries are sorted by format name */
740 for (fmt
= 0; fmt
< MESA_FORMAT_COUNT
; fmt
++) {
741 assert(texfetch_funcs
[fmt
].Name
== fmt
);
745 STATIC_ASSERT(Elements(texfetch_funcs
) == MESA_FORMAT_COUNT
);
747 assert(format
< MESA_FORMAT_COUNT
);
751 return texfetch_funcs
[format
].Fetch1D
;
753 return texfetch_funcs
[format
].Fetch2D
;
755 return texfetch_funcs
[format
].Fetch3D
;
757 assert(0 && "bad dims in _mesa_get_texel_fetch_func");
764 * Initialize the texture image's FetchTexel methods.
767 set_fetch_functions(struct swrast_texture_image
*texImage
, GLuint dims
)
769 gl_format format
= texImage
->Base
.TexFormat
;
771 ASSERT(dims
== 1 || dims
== 2 || dims
== 3);
773 if (_mesa_get_format_color_encoding(format
) == GL_SRGB
) {
774 format
= _mesa_get_srgb_format_linear(format
);
777 texImage
->FetchTexel
= _mesa_get_texel_fetch_func(format
, dims
);
778 ASSERT(texImage
->FetchTexel
);
782 _mesa_update_fetch_functions(struct gl_texture_object
*texObj
)
787 dims
= _mesa_get_texture_dimensions(texObj
->Target
);
789 for (face
= 0; face
< 6; face
++) {
790 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
791 if (texObj
->Image
[face
][i
]) {
792 set_fetch_functions(swrast_texture_image(texObj
->Image
[face
][i
]),