mesa, util: move RGB9E5 conversion functions to gallium/util
[mesa.git] / src / mesa / main / texfetch.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.7
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (c) 2009 VMware, Inc.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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.
24 */
25
26
27 /**
28 * \file texfetch.c
29 *
30 * Texel fetch/store functions
31 *
32 * \author Gareth Hughes
33 */
34
35
36 #include "colormac.h"
37 #include "macros.h"
38 #include "texcompress.h"
39 #include "texcompress_fxt1.h"
40 #include "texcompress_s3tc.h"
41 #include "texcompress_rgtc.h"
42 #include "texfetch.h"
43 #include "teximage.h"
44 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
45
46
47 /**
48 * Convert an 8-bit sRGB value from non-linear space to a
49 * linear RGB value in [0, 1].
50 * Implemented with a 256-entry lookup table.
51 */
52 static INLINE GLfloat
53 nonlinear_to_linear(GLubyte cs8)
54 {
55 static GLfloat table[256];
56 static GLboolean tableReady = GL_FALSE;
57 if (!tableReady) {
58 /* compute lookup table now */
59 GLuint i;
60 for (i = 0; i < 256; i++) {
61 const GLfloat cs = UBYTE_TO_FLOAT(i);
62 if (cs <= 0.04045) {
63 table[i] = cs / 12.92f;
64 }
65 else {
66 table[i] = (GLfloat) pow((cs + 0.055) / 1.055, 2.4);
67 }
68 }
69 tableReady = GL_TRUE;
70 }
71 return table[cs8];
72 }
73
74
75
76 /* Texel fetch routines for all supported formats
77 */
78 #define DIM 1
79 #include "texfetch_tmp.h"
80
81 #define DIM 2
82 #include "texfetch_tmp.h"
83
84 #define DIM 3
85 #include "texfetch_tmp.h"
86
87 /**
88 * Null texel fetch function.
89 *
90 * Have to have this so the FetchTexel function pointer is never NULL.
91 */
92 static void fetch_null_texelf( const struct gl_texture_image *texImage,
93 GLint i, GLint j, GLint k, GLfloat *texel )
94 {
95 (void) texImage; (void) i; (void) j; (void) k;
96 texel[RCOMP] = 0.0;
97 texel[GCOMP] = 0.0;
98 texel[BCOMP] = 0.0;
99 texel[ACOMP] = 0.0;
100 _mesa_warning(NULL, "fetch_null_texelf() called!");
101 }
102
103 static void store_null_texel(struct gl_texture_image *texImage,
104 GLint i, GLint j, GLint k, const void *texel)
105 {
106 (void) texImage;
107 (void) i;
108 (void) j;
109 (void) k;
110 (void) texel;
111 /* no-op */
112 }
113
114
115
116 /**
117 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
118 * XXX this is somewhat temporary.
119 */
120 static struct {
121 gl_format Name;
122 FetchTexelFuncF Fetch1D;
123 FetchTexelFuncF Fetch2D;
124 FetchTexelFuncF Fetch3D;
125 StoreTexelFunc StoreTexel;
126 }
127 texfetch_funcs[MESA_FORMAT_COUNT] =
128 {
129 {
130 MESA_FORMAT_NONE,
131 fetch_null_texelf,
132 fetch_null_texelf,
133 fetch_null_texelf,
134 store_null_texel
135 },
136
137 {
138 MESA_FORMAT_RGBA8888,
139 fetch_texel_1d_f_rgba8888,
140 fetch_texel_2d_f_rgba8888,
141 fetch_texel_3d_f_rgba8888,
142 store_texel_rgba8888
143 },
144 {
145 MESA_FORMAT_RGBA8888_REV,
146 fetch_texel_1d_f_rgba8888_rev,
147 fetch_texel_2d_f_rgba8888_rev,
148 fetch_texel_3d_f_rgba8888_rev,
149 store_texel_rgba8888_rev
150 },
151 {
152 MESA_FORMAT_ARGB8888,
153 fetch_texel_1d_f_argb8888,
154 fetch_texel_2d_f_argb8888,
155 fetch_texel_3d_f_argb8888,
156 store_texel_argb8888
157 },
158 {
159 MESA_FORMAT_ARGB8888_REV,
160 fetch_texel_1d_f_argb8888_rev,
161 fetch_texel_2d_f_argb8888_rev,
162 fetch_texel_3d_f_argb8888_rev,
163 store_texel_argb8888_rev
164 },
165 {
166 MESA_FORMAT_XRGB8888,
167 fetch_texel_1d_f_xrgb8888,
168 fetch_texel_2d_f_xrgb8888,
169 fetch_texel_3d_f_xrgb8888,
170 store_texel_xrgb8888
171 },
172 {
173 MESA_FORMAT_XRGB8888_REV,
174 fetch_texel_1d_f_xrgb8888_rev,
175 fetch_texel_2d_f_xrgb8888_rev,
176 fetch_texel_3d_f_xrgb8888_rev,
177 store_texel_xrgb8888_rev,
178 },
179 {
180 MESA_FORMAT_RGB888,
181 fetch_texel_1d_f_rgb888,
182 fetch_texel_2d_f_rgb888,
183 fetch_texel_3d_f_rgb888,
184 store_texel_rgb888
185 },
186 {
187 MESA_FORMAT_BGR888,
188 fetch_texel_1d_f_bgr888,
189 fetch_texel_2d_f_bgr888,
190 fetch_texel_3d_f_bgr888,
191 store_texel_bgr888
192 },
193 {
194 MESA_FORMAT_RGB565,
195 fetch_texel_1d_f_rgb565,
196 fetch_texel_2d_f_rgb565,
197 fetch_texel_3d_f_rgb565,
198 store_texel_rgb565
199 },
200 {
201 MESA_FORMAT_RGB565_REV,
202 fetch_texel_1d_f_rgb565_rev,
203 fetch_texel_2d_f_rgb565_rev,
204 fetch_texel_3d_f_rgb565_rev,
205 store_texel_rgb565_rev
206 },
207 {
208 MESA_FORMAT_ARGB4444,
209 fetch_texel_1d_f_argb4444,
210 fetch_texel_2d_f_argb4444,
211 fetch_texel_3d_f_argb4444,
212 store_texel_argb4444
213 },
214 {
215 MESA_FORMAT_ARGB4444_REV,
216 fetch_texel_1d_f_argb4444_rev,
217 fetch_texel_2d_f_argb4444_rev,
218 fetch_texel_3d_f_argb4444_rev,
219 store_texel_argb4444_rev
220 },
221 {
222 MESA_FORMAT_RGBA5551,
223 fetch_texel_1d_f_rgba5551,
224 fetch_texel_2d_f_rgba5551,
225 fetch_texel_3d_f_rgba5551,
226 store_texel_rgba5551
227 },
228 {
229 MESA_FORMAT_ARGB1555,
230 fetch_texel_1d_f_argb1555,
231 fetch_texel_2d_f_argb1555,
232 fetch_texel_3d_f_argb1555,
233 store_texel_argb1555
234 },
235 {
236 MESA_FORMAT_ARGB1555_REV,
237 fetch_texel_1d_f_argb1555_rev,
238 fetch_texel_2d_f_argb1555_rev,
239 fetch_texel_3d_f_argb1555_rev,
240 store_texel_argb1555_rev
241 },
242 {
243 MESA_FORMAT_AL44,
244 fetch_texel_1d_f_al44,
245 fetch_texel_2d_f_al44,
246 fetch_texel_3d_f_al44,
247 store_texel_al44
248 },
249 {
250 MESA_FORMAT_AL88,
251 fetch_texel_1d_f_al88,
252 fetch_texel_2d_f_al88,
253 fetch_texel_3d_f_al88,
254 store_texel_al88
255 },
256 {
257 MESA_FORMAT_AL88_REV,
258 fetch_texel_1d_f_al88_rev,
259 fetch_texel_2d_f_al88_rev,
260 fetch_texel_3d_f_al88_rev,
261 store_texel_al88_rev
262 },
263 {
264 MESA_FORMAT_AL1616,
265 fetch_texel_1d_f_al1616,
266 fetch_texel_2d_f_al1616,
267 fetch_texel_3d_f_al1616,
268 store_texel_al1616
269 },
270 {
271 MESA_FORMAT_AL1616_REV,
272 fetch_texel_1d_f_al1616_rev,
273 fetch_texel_2d_f_al1616_rev,
274 fetch_texel_3d_f_al1616_rev,
275 store_texel_al1616_rev
276 },
277 {
278 MESA_FORMAT_RGB332,
279 fetch_texel_1d_f_rgb332,
280 fetch_texel_2d_f_rgb332,
281 fetch_texel_3d_f_rgb332,
282 store_texel_rgb332
283 },
284 {
285 MESA_FORMAT_A8,
286 fetch_texel_1d_f_a8,
287 fetch_texel_2d_f_a8,
288 fetch_texel_3d_f_a8,
289 store_texel_a8
290 },
291 {
292 MESA_FORMAT_A16,
293 fetch_texel_1d_f_a16,
294 fetch_texel_2d_f_a16,
295 fetch_texel_3d_f_a16,
296 store_texel_a16
297 },
298 {
299 MESA_FORMAT_L8,
300 fetch_texel_1d_f_l8,
301 fetch_texel_2d_f_l8,
302 fetch_texel_3d_f_l8,
303 store_texel_l8
304 },
305 {
306 MESA_FORMAT_L16,
307 fetch_texel_1d_f_l16,
308 fetch_texel_2d_f_l16,
309 fetch_texel_3d_f_l16,
310 store_texel_l16
311 },
312 {
313 MESA_FORMAT_I8,
314 fetch_texel_1d_f_i8,
315 fetch_texel_2d_f_i8,
316 fetch_texel_3d_f_i8,
317 store_texel_i8
318 },
319 {
320 MESA_FORMAT_I16,
321 fetch_texel_1d_f_i16,
322 fetch_texel_2d_f_i16,
323 fetch_texel_3d_f_i16,
324 store_texel_i16
325 },
326 {
327 MESA_FORMAT_CI8,
328 fetch_texel_1d_f_ci8,
329 fetch_texel_2d_f_ci8,
330 fetch_texel_3d_f_ci8,
331 store_texel_ci8
332 },
333 {
334 MESA_FORMAT_YCBCR,
335 fetch_texel_1d_f_ycbcr,
336 fetch_texel_2d_f_ycbcr,
337 fetch_texel_3d_f_ycbcr,
338 store_texel_ycbcr
339 },
340 {
341 MESA_FORMAT_YCBCR_REV,
342 fetch_texel_1d_f_ycbcr_rev,
343 fetch_texel_2d_f_ycbcr_rev,
344 fetch_texel_3d_f_ycbcr_rev,
345 store_texel_ycbcr_rev
346 },
347 {
348 MESA_FORMAT_R8,
349 fetch_texel_1d_f_r8,
350 fetch_texel_2d_f_r8,
351 fetch_texel_3d_f_r8,
352 store_texel_r8,
353 },
354 {
355 MESA_FORMAT_RG88,
356 fetch_texel_1d_f_rg88,
357 fetch_texel_2d_f_rg88,
358 fetch_texel_3d_f_rg88,
359 store_texel_rg88,
360 },
361 {
362 MESA_FORMAT_RG88_REV,
363 fetch_texel_1d_f_rg88_rev,
364 fetch_texel_2d_f_rg88_rev,
365 fetch_texel_3d_f_rg88_rev,
366 store_texel_rg88_rev,
367 },
368 {
369 MESA_FORMAT_R16,
370 fetch_texel_1d_f_r16,
371 fetch_texel_2d_f_r16,
372 fetch_texel_3d_f_r16,
373 store_texel_r16,
374 },
375 {
376 MESA_FORMAT_RG1616,
377 fetch_texel_1d_f_rg1616,
378 fetch_texel_2d_f_rg1616,
379 fetch_texel_3d_f_rg1616,
380 store_texel_rg1616,
381 },
382 {
383 MESA_FORMAT_RG1616_REV,
384 fetch_texel_1d_f_rg1616_rev,
385 fetch_texel_2d_f_rg1616_rev,
386 fetch_texel_3d_f_rg1616_rev,
387 store_texel_rg1616_rev,
388 },
389 {
390 MESA_FORMAT_ARGB2101010,
391 fetch_texel_1d_f_argb2101010,
392 fetch_texel_2d_f_argb2101010,
393 fetch_texel_3d_f_argb2101010,
394 store_texel_argb2101010
395 },
396 {
397 MESA_FORMAT_Z24_S8,
398 fetch_texel_1d_f_z24_s8,
399 fetch_texel_2d_f_z24_s8,
400 fetch_texel_3d_f_z24_s8,
401 store_texel_z24_s8
402 },
403 {
404 MESA_FORMAT_S8_Z24,
405 fetch_texel_1d_f_s8_z24,
406 fetch_texel_2d_f_s8_z24,
407 fetch_texel_3d_f_s8_z24,
408 store_texel_s8_z24
409 },
410 {
411 MESA_FORMAT_Z16,
412 fetch_texel_1d_f_z16,
413 fetch_texel_2d_f_z16,
414 fetch_texel_3d_f_z16,
415 store_texel_z16
416 },
417 {
418 MESA_FORMAT_X8_Z24,
419 fetch_texel_1d_f_s8_z24,
420 fetch_texel_2d_f_s8_z24,
421 fetch_texel_3d_f_s8_z24,
422 store_texel_s8_z24
423 },
424 {
425 MESA_FORMAT_Z24_X8,
426 fetch_texel_1d_f_z24_s8,
427 fetch_texel_2d_f_z24_s8,
428 fetch_texel_3d_f_z24_s8,
429 store_texel_z24_s8
430 },
431 {
432 MESA_FORMAT_Z32,
433 fetch_texel_1d_f_z32,
434 fetch_texel_2d_f_z32,
435 fetch_texel_3d_f_z32,
436 store_texel_z32
437 },
438 {
439 MESA_FORMAT_S8,
440 NULL,
441 NULL,
442 NULL,
443 NULL
444 },
445 {
446 MESA_FORMAT_SRGB8,
447 fetch_texel_1d_srgb8,
448 fetch_texel_2d_srgb8,
449 fetch_texel_3d_srgb8,
450 store_texel_srgb8
451 },
452 {
453 MESA_FORMAT_SRGBA8,
454 fetch_texel_1d_srgba8,
455 fetch_texel_2d_srgba8,
456 fetch_texel_3d_srgba8,
457 store_texel_srgba8
458 },
459 {
460 MESA_FORMAT_SARGB8,
461 fetch_texel_1d_sargb8,
462 fetch_texel_2d_sargb8,
463 fetch_texel_3d_sargb8,
464 store_texel_sargb8
465 },
466 {
467 MESA_FORMAT_SL8,
468 fetch_texel_1d_sl8,
469 fetch_texel_2d_sl8,
470 fetch_texel_3d_sl8,
471 store_texel_sl8
472 },
473 {
474 MESA_FORMAT_SLA8,
475 fetch_texel_1d_sla8,
476 fetch_texel_2d_sla8,
477 fetch_texel_3d_sla8,
478 store_texel_sla8
479 },
480 {
481 MESA_FORMAT_SRGB_DXT1,
482 NULL,
483 _mesa_fetch_texel_2d_f_srgb_dxt1,
484 NULL,
485 NULL
486 },
487 {
488 MESA_FORMAT_SRGBA_DXT1,
489 NULL,
490 _mesa_fetch_texel_2d_f_srgba_dxt1,
491 NULL,
492 NULL
493 },
494 {
495 MESA_FORMAT_SRGBA_DXT3,
496 NULL,
497 _mesa_fetch_texel_2d_f_srgba_dxt3,
498 NULL,
499 NULL
500 },
501 {
502 MESA_FORMAT_SRGBA_DXT5,
503 NULL,
504 _mesa_fetch_texel_2d_f_srgba_dxt5,
505 NULL,
506 NULL
507 },
508
509 {
510 MESA_FORMAT_RGB_FXT1,
511 NULL,
512 _mesa_fetch_texel_2d_f_rgb_fxt1,
513 NULL,
514 NULL
515 },
516 {
517 MESA_FORMAT_RGBA_FXT1,
518 NULL,
519 _mesa_fetch_texel_2d_f_rgba_fxt1,
520 NULL,
521 NULL
522 },
523 {
524 MESA_FORMAT_RGB_DXT1,
525 NULL,
526 _mesa_fetch_texel_2d_f_rgb_dxt1,
527 NULL,
528 NULL
529 },
530 {
531 MESA_FORMAT_RGBA_DXT1,
532 NULL,
533 _mesa_fetch_texel_2d_f_rgba_dxt1,
534 NULL,
535 NULL
536 },
537 {
538 MESA_FORMAT_RGBA_DXT3,
539 NULL,
540 _mesa_fetch_texel_2d_f_rgba_dxt3,
541 NULL,
542 NULL
543 },
544 {
545 MESA_FORMAT_RGBA_DXT5,
546 NULL,
547 _mesa_fetch_texel_2d_f_rgba_dxt5,
548 NULL,
549 NULL
550 },
551 {
552 MESA_FORMAT_RGBA_FLOAT32,
553 fetch_texel_1d_f_rgba_f32,
554 fetch_texel_2d_f_rgba_f32,
555 fetch_texel_3d_f_rgba_f32,
556 store_texel_rgba_f32
557 },
558 {
559 MESA_FORMAT_RGBA_FLOAT16,
560 fetch_texel_1d_f_rgba_f16,
561 fetch_texel_2d_f_rgba_f16,
562 fetch_texel_3d_f_rgba_f16,
563 store_texel_rgba_f16
564 },
565 {
566 MESA_FORMAT_RGB_FLOAT32,
567 fetch_texel_1d_f_rgb_f32,
568 fetch_texel_2d_f_rgb_f32,
569 fetch_texel_3d_f_rgb_f32,
570 store_texel_rgb_f32
571 },
572 {
573 MESA_FORMAT_RGB_FLOAT16,
574 fetch_texel_1d_f_rgb_f16,
575 fetch_texel_2d_f_rgb_f16,
576 fetch_texel_3d_f_rgb_f16,
577 store_texel_rgb_f16
578 },
579 {
580 MESA_FORMAT_ALPHA_FLOAT32,
581 fetch_texel_1d_f_alpha_f32,
582 fetch_texel_2d_f_alpha_f32,
583 fetch_texel_3d_f_alpha_f32,
584 store_texel_alpha_f32
585 },
586 {
587 MESA_FORMAT_ALPHA_FLOAT16,
588 fetch_texel_1d_f_alpha_f16,
589 fetch_texel_2d_f_alpha_f16,
590 fetch_texel_3d_f_alpha_f16,
591 store_texel_alpha_f16
592 },
593 {
594 MESA_FORMAT_LUMINANCE_FLOAT32,
595 fetch_texel_1d_f_luminance_f32,
596 fetch_texel_2d_f_luminance_f32,
597 fetch_texel_3d_f_luminance_f32,
598 store_texel_luminance_f32
599 },
600 {
601 MESA_FORMAT_LUMINANCE_FLOAT16,
602 fetch_texel_1d_f_luminance_f16,
603 fetch_texel_2d_f_luminance_f16,
604 fetch_texel_3d_f_luminance_f16,
605 store_texel_luminance_f16
606 },
607 {
608 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32,
609 fetch_texel_1d_f_luminance_alpha_f32,
610 fetch_texel_2d_f_luminance_alpha_f32,
611 fetch_texel_3d_f_luminance_alpha_f32,
612 store_texel_luminance_alpha_f32
613 },
614 {
615 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16,
616 fetch_texel_1d_f_luminance_alpha_f16,
617 fetch_texel_2d_f_luminance_alpha_f16,
618 fetch_texel_3d_f_luminance_alpha_f16,
619 store_texel_luminance_alpha_f16
620 },
621 {
622 MESA_FORMAT_INTENSITY_FLOAT32,
623 fetch_texel_1d_f_intensity_f32,
624 fetch_texel_2d_f_intensity_f32,
625 fetch_texel_3d_f_intensity_f32,
626 store_texel_intensity_f32
627 },
628 {
629 MESA_FORMAT_INTENSITY_FLOAT16,
630 fetch_texel_1d_f_intensity_f16,
631 fetch_texel_2d_f_intensity_f16,
632 fetch_texel_3d_f_intensity_f16,
633 store_texel_intensity_f16
634 },
635 {
636 MESA_FORMAT_R_FLOAT32,
637 fetch_texel_1d_f_r_f32,
638 fetch_texel_2d_f_r_f32,
639 fetch_texel_3d_f_r_f32,
640 store_texel_r_f32
641 },
642 {
643 MESA_FORMAT_R_FLOAT16,
644 fetch_texel_1d_f_r_f16,
645 fetch_texel_2d_f_r_f16,
646 fetch_texel_3d_f_r_f16,
647 store_texel_r_f16
648 },
649 {
650 MESA_FORMAT_RG_FLOAT32,
651 fetch_texel_1d_f_rg_f32,
652 fetch_texel_2d_f_rg_f32,
653 fetch_texel_3d_f_rg_f32,
654 store_texel_rg_f32
655 },
656 {
657 MESA_FORMAT_RG_FLOAT16,
658 fetch_texel_1d_f_rg_f16,
659 fetch_texel_2d_f_rg_f16,
660 fetch_texel_3d_f_rg_f16,
661 store_texel_rg_f16
662 },
663
664 /* non-normalized, signed int */
665 {
666 MESA_FORMAT_RGBA_INT8,
667 fetch_texel_1d_rgba_int8,
668 fetch_texel_2d_rgba_int8,
669 fetch_texel_3d_rgba_int8,
670 store_texel_rgba_int8
671 },
672 {
673 MESA_FORMAT_RGBA_INT16,
674 fetch_texel_1d_rgba_int16,
675 fetch_texel_2d_rgba_int16,
676 fetch_texel_3d_rgba_int16,
677 store_texel_rgba_int16
678 },
679 {
680 MESA_FORMAT_RGBA_INT32,
681 fetch_texel_1d_rgba_int32,
682 fetch_texel_2d_rgba_int32,
683 fetch_texel_3d_rgba_int32,
684 store_texel_rgba_int32
685 },
686
687 /* non-normalized, unsigned int */
688 {
689 MESA_FORMAT_RGBA_UINT8,
690 fetch_texel_1d_rgba_uint8,
691 fetch_texel_2d_rgba_uint8,
692 fetch_texel_3d_rgba_uint8,
693 store_texel_rgba_uint8
694 },
695 {
696 MESA_FORMAT_RGBA_UINT16,
697 fetch_texel_1d_rgba_uint16,
698 fetch_texel_2d_rgba_uint16,
699 fetch_texel_3d_rgba_uint16,
700 store_texel_rgba_uint16
701 },
702 {
703 MESA_FORMAT_RGBA_UINT32,
704 fetch_texel_1d_rgba_uint32,
705 fetch_texel_2d_rgba_uint32,
706 fetch_texel_3d_rgba_uint32,
707 store_texel_rgba_uint32
708 },
709
710 /* dudv */
711 {
712 MESA_FORMAT_DUDV8,
713 fetch_texel_1d_dudv8,
714 fetch_texel_2d_dudv8,
715 fetch_texel_3d_dudv8,
716 NULL
717 },
718
719 /* signed, normalized */
720 {
721 MESA_FORMAT_SIGNED_R8,
722 fetch_texel_1d_signed_r8,
723 fetch_texel_2d_signed_r8,
724 fetch_texel_3d_signed_r8,
725 store_texel_signed_r8
726 },
727 {
728 MESA_FORMAT_SIGNED_RG88_REV,
729 fetch_texel_1d_signed_rg88_rev,
730 fetch_texel_2d_signed_rg88_rev,
731 fetch_texel_3d_signed_rg88_rev,
732 store_texel_signed_rg88_rev
733 },
734 {
735 MESA_FORMAT_SIGNED_RGBX8888,
736 fetch_texel_1d_signed_rgbx8888,
737 fetch_texel_2d_signed_rgbx8888,
738 fetch_texel_3d_signed_rgbx8888,
739 store_texel_signed_rgbx8888
740 },
741 {
742 MESA_FORMAT_SIGNED_RGBA8888,
743 fetch_texel_1d_signed_rgba8888,
744 fetch_texel_2d_signed_rgba8888,
745 fetch_texel_3d_signed_rgba8888,
746 store_texel_signed_rgba8888
747 },
748 {
749 MESA_FORMAT_SIGNED_RGBA8888_REV,
750 fetch_texel_1d_signed_rgba8888_rev,
751 fetch_texel_2d_signed_rgba8888_rev,
752 fetch_texel_3d_signed_rgba8888_rev,
753 store_texel_signed_rgba8888_rev
754 },
755 {
756 MESA_FORMAT_SIGNED_R16,
757 fetch_texel_1d_signed_r16,
758 fetch_texel_2d_signed_r16,
759 fetch_texel_3d_signed_r16,
760 store_texel_signed_r16
761 },
762 {
763 MESA_FORMAT_SIGNED_GR1616,
764 fetch_texel_1d_signed_rg1616,
765 fetch_texel_2d_signed_rg1616,
766 fetch_texel_3d_signed_rg1616,
767 store_texel_signed_rg1616
768 },
769 {
770 MESA_FORMAT_SIGNED_RGB_16,
771 fetch_texel_1d_signed_rgb_16,
772 fetch_texel_2d_signed_rgb_16,
773 fetch_texel_3d_signed_rgb_16,
774 store_texel_signed_rgb_16
775 },
776 {
777 MESA_FORMAT_SIGNED_RGBA_16,
778 fetch_texel_1d_signed_rgba_16,
779 fetch_texel_2d_signed_rgba_16,
780 fetch_texel_3d_signed_rgba_16,
781 store_texel_signed_rgba_16
782 },
783 {
784 MESA_FORMAT_RGBA_16,
785 fetch_texel_1d_rgba_16,
786 fetch_texel_2d_rgba_16,
787 fetch_texel_3d_rgba_16,
788 store_texel_rgba_16
789 },
790 {
791 MESA_FORMAT_RED_RGTC1,
792 NULL,
793 _mesa_fetch_texel_2d_f_red_rgtc1,
794 NULL,
795 NULL
796 },
797 {
798 MESA_FORMAT_SIGNED_RED_RGTC1,
799 NULL,
800 _mesa_fetch_texel_2d_f_signed_red_rgtc1,
801 NULL,
802 NULL
803 },
804 {
805 MESA_FORMAT_RG_RGTC2,
806 NULL,
807 _mesa_fetch_texel_2d_f_rg_rgtc2,
808 NULL,
809 NULL
810 },
811 {
812 MESA_FORMAT_SIGNED_RG_RGTC2,
813 NULL,
814 _mesa_fetch_texel_2d_f_signed_rg_rgtc2,
815 NULL,
816 NULL
817 },
818 {
819 MESA_FORMAT_L_LATC1,
820 NULL,
821 _mesa_fetch_texel_2d_f_l_latc1,
822 NULL,
823 NULL
824 },
825 {
826 MESA_FORMAT_SIGNED_L_LATC1,
827 NULL,
828 _mesa_fetch_texel_2d_f_signed_l_latc1,
829 NULL,
830 NULL
831 },
832 {
833 MESA_FORMAT_LA_LATC2,
834 NULL,
835 _mesa_fetch_texel_2d_f_la_latc2,
836 NULL,
837 NULL
838 },
839 {
840 MESA_FORMAT_SIGNED_LA_LATC2,
841 NULL,
842 _mesa_fetch_texel_2d_f_signed_la_latc2,
843 NULL,
844 NULL
845 },
846 {
847 MESA_FORMAT_SIGNED_A8,
848 fetch_texel_1d_signed_a8,
849 fetch_texel_2d_signed_a8,
850 fetch_texel_3d_signed_a8,
851 store_texel_signed_a8
852 },
853 {
854 MESA_FORMAT_SIGNED_L8,
855 fetch_texel_1d_signed_l8,
856 fetch_texel_2d_signed_l8,
857 fetch_texel_3d_signed_l8,
858 store_texel_signed_l8
859 },
860 {
861 MESA_FORMAT_SIGNED_AL88,
862 fetch_texel_1d_signed_al88,
863 fetch_texel_2d_signed_al88,
864 fetch_texel_3d_signed_al88,
865 store_texel_signed_al88
866 },
867 {
868 MESA_FORMAT_SIGNED_I8,
869 fetch_texel_1d_signed_i8,
870 fetch_texel_2d_signed_i8,
871 fetch_texel_3d_signed_i8,
872 store_texel_signed_i8
873 },
874 {
875 MESA_FORMAT_SIGNED_A16,
876 fetch_texel_1d_signed_a16,
877 fetch_texel_2d_signed_a16,
878 fetch_texel_3d_signed_a16,
879 store_texel_signed_a16
880 },
881 {
882 MESA_FORMAT_SIGNED_L16,
883 fetch_texel_1d_signed_l16,
884 fetch_texel_2d_signed_l16,
885 fetch_texel_3d_signed_l16,
886 store_texel_signed_l16
887 },
888 {
889 MESA_FORMAT_SIGNED_AL1616,
890 fetch_texel_1d_signed_al1616,
891 fetch_texel_2d_signed_al1616,
892 fetch_texel_3d_signed_al1616,
893 store_texel_signed_al1616
894 },
895 {
896 MESA_FORMAT_SIGNED_I16,
897 fetch_texel_1d_signed_i16,
898 fetch_texel_2d_signed_i16,
899 fetch_texel_3d_signed_i16,
900 store_texel_signed_i16
901 },
902 {
903 MESA_FORMAT_RGB9_E5_FLOAT,
904 fetch_texel_1d_rgb9_e5,
905 fetch_texel_2d_rgb9_e5,
906 fetch_texel_3d_rgb9_e5,
907 store_texel_rgb9_e5
908 },
909 };
910
911
912 FetchTexelFuncF
913 _mesa_get_texel_fetch_func(gl_format format, GLuint dims)
914 {
915 #ifdef DEBUG
916 /* check that the table entries are sorted by format name */
917 gl_format fmt;
918 for (fmt = 0; fmt < MESA_FORMAT_COUNT; fmt++) {
919 assert(texfetch_funcs[fmt].Name == fmt);
920 }
921 #endif
922
923 assert(Elements(texfetch_funcs) == MESA_FORMAT_COUNT);
924 assert(format < MESA_FORMAT_COUNT);
925
926 switch (dims) {
927 case 1:
928 return texfetch_funcs[format].Fetch1D;
929 case 2:
930 return texfetch_funcs[format].Fetch2D;
931 case 3:
932 return texfetch_funcs[format].Fetch3D;
933 default:
934 assert(0 && "bad dims in _mesa_get_texel_fetch_func");
935 return NULL;
936 }
937 }
938
939
940 StoreTexelFunc
941 _mesa_get_texel_store_func(gl_format format)
942 {
943 assert(format < MESA_FORMAT_COUNT);
944 return texfetch_funcs[format].StoreTexel;
945 }
946
947
948 /**
949 * Adaptor for fetching a GLchan texel from a float-valued texture.
950 */
951 static void
952 fetch_texel_float_to_chan(const struct gl_texture_image *texImage,
953 GLint i, GLint j, GLint k, GLchan *texelOut)
954 {
955 GLfloat temp[4];
956 GLenum baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
957
958 ASSERT(texImage->FetchTexelf);
959 texImage->FetchTexelf(texImage, i, j, k, temp);
960 if (baseFormat == GL_DEPTH_COMPONENT ||
961 baseFormat == GL_DEPTH_STENCIL_EXT) {
962 /* just one channel */
963 UNCLAMPED_FLOAT_TO_CHAN(texelOut[0], temp[0]);
964 }
965 else {
966 /* four channels */
967 UNCLAMPED_FLOAT_TO_CHAN(texelOut[0], temp[0]);
968 UNCLAMPED_FLOAT_TO_CHAN(texelOut[1], temp[1]);
969 UNCLAMPED_FLOAT_TO_CHAN(texelOut[2], temp[2]);
970 UNCLAMPED_FLOAT_TO_CHAN(texelOut[3], temp[3]);
971 }
972 }
973
974
975 #if 0
976 /**
977 * Adaptor for fetching a float texel from a GLchan-valued texture.
978 */
979 static void
980 fetch_texel_chan_to_float(const struct gl_texture_image *texImage,
981 GLint i, GLint j, GLint k, GLfloat *texelOut)
982 {
983 GLchan temp[4];
984 GLenum baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
985
986 ASSERT(texImage->FetchTexelc);
987 texImage->FetchTexelc(texImage, i, j, k, temp);
988 if (baseFormat == GL_DEPTH_COMPONENT ||
989 baseFormat == GL_DEPTH_STENCIL_EXT) {
990 /* just one channel */
991 texelOut[0] = CHAN_TO_FLOAT(temp[0]);
992 }
993 else {
994 /* four channels */
995 texelOut[0] = CHAN_TO_FLOAT(temp[0]);
996 texelOut[1] = CHAN_TO_FLOAT(temp[1]);
997 texelOut[2] = CHAN_TO_FLOAT(temp[2]);
998 texelOut[3] = CHAN_TO_FLOAT(temp[3]);
999 }
1000 }
1001 #endif
1002
1003
1004 /**
1005 * Initialize the texture image's FetchTexelc and FetchTexelf methods.
1006 */
1007 void
1008 _mesa_set_fetch_functions(struct gl_texture_image *texImage, GLuint dims)
1009 {
1010 gl_format format = texImage->TexFormat;
1011
1012 ASSERT(dims == 1 || dims == 2 || dims == 3);
1013
1014 if (texImage->TexObject->Sampler.sRGBDecode == GL_SKIP_DECODE_EXT &&
1015 _mesa_get_format_color_encoding(format) == GL_SRGB) {
1016 format = _mesa_get_srgb_format_linear(format);
1017 }
1018
1019 texImage->FetchTexelf = _mesa_get_texel_fetch_func(format, dims);
1020
1021 texImage->FetchTexelc = fetch_texel_float_to_chan;
1022
1023 ASSERT(texImage->FetchTexelc);
1024 ASSERT(texImage->FetchTexelf);
1025 }
1026
1027 void
1028 _mesa_update_fetch_functions(struct gl_texture_object *texObj)
1029 {
1030 GLuint face, i;
1031 GLuint dims;
1032
1033 dims = _mesa_get_texture_dimensions(texObj->Target);
1034
1035 for (face = 0; face < 6; face++) {
1036 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
1037 if (texObj->Image[face][i]) {
1038 _mesa_set_fetch_functions(texObj->Image[face][i], dims);
1039 }
1040 }
1041 }
1042 }