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 s_texfetch.c
29 *
30 * Texel fetch/store functions
31 *
32 * \author Gareth Hughes
33 */
34
35
36#include "main/colormac.h"
37#include "main/macros.h"
38#include "main/texcompress.h"
39#include "main/texcompress_fxt1.h"
40#include "main/texcompress_s3tc.h"
41#include "main/texcompress_rgtc.h"
42#include "main/texcompress_etc.h"
43#include "main/teximage.h"
44#include "main/samplerobj.h"
45#include "s_context.h"
46#include "s_texfetch.h"
47#include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
48#include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
49
50
51/**
52 * Convert an 8-bit sRGB value from non-linear space to a
53 * linear RGB value in [0, 1].
54 * Implemented with a 256-entry lookup table.
55 */
56static inline GLfloat
57nonlinear_to_linear(GLubyte cs8)
58{
59   static GLfloat table[256];
60   static GLboolean tableReady = GL_FALSE;
61   if (!tableReady) {
62      /* compute lookup table now */
63      GLuint i;
64      for (i = 0; i < 256; i++) {
65         const GLfloat cs = UBYTE_TO_FLOAT(i);
66         if (cs <= 0.04045) {
67            table[i] = cs / 12.92f;
68         }
69         else {
70            table[i] = (GLfloat) pow((cs + 0.055) / 1.055, 2.4);
71         }
72      }
73      tableReady = GL_TRUE;
74   }
75   return table[cs8];
76}
77
78
79
80/* Texel fetch routines for all supported formats
81 */
82#define DIM 1
83#include "s_texfetch_tmp.h"
84
85#define DIM 2
86#include "s_texfetch_tmp.h"
87
88#define DIM 3
89#include "s_texfetch_tmp.h"
90
91/**
92 * Null texel fetch function.
93 *
94 * Have to have this so the FetchTexel function pointer is never NULL.
95 */
96static void fetch_null_texelf( const struct swrast_texture_image *texImage,
97                               GLint i, GLint j, GLint k, GLfloat *texel )
98{
99   (void) texImage; (void) i; (void) j; (void) k;
100   texel[RCOMP] = 0.0;
101   texel[GCOMP] = 0.0;
102   texel[BCOMP] = 0.0;
103   texel[ACOMP] = 0.0;
104   _mesa_warning(NULL, "fetch_null_texelf() called!");
105}
106
107
108/**
109 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
110 * XXX this is somewhat temporary.
111 */
112static struct {
113   gl_format Name;
114   FetchTexelFunc Fetch1D;
115   FetchTexelFunc Fetch2D;
116   FetchTexelFunc Fetch3D;
117}
118texfetch_funcs[MESA_FORMAT_COUNT] =
119{
120   {
121      MESA_FORMAT_NONE,
122      fetch_null_texelf,
123      fetch_null_texelf,
124      fetch_null_texelf
125   },
126
127   {
128      MESA_FORMAT_RGBA8888,
129      fetch_texel_1d_f_rgba8888,
130      fetch_texel_2d_f_rgba8888,
131      fetch_texel_3d_f_rgba8888
132   },
133   {
134      MESA_FORMAT_RGBA8888_REV,
135      fetch_texel_1d_f_rgba8888_rev,
136      fetch_texel_2d_f_rgba8888_rev,
137      fetch_texel_3d_f_rgba8888_rev
138   },
139   {
140      MESA_FORMAT_ARGB8888,
141      fetch_texel_1d_f_argb8888,
142      fetch_texel_2d_f_argb8888,
143      fetch_texel_3d_f_argb8888
144   },
145   {
146      MESA_FORMAT_ARGB8888_REV,
147      fetch_texel_1d_f_argb8888_rev,
148      fetch_texel_2d_f_argb8888_rev,
149      fetch_texel_3d_f_argb8888_rev
150   },
151   {
152      MESA_FORMAT_RGBX8888,
153      fetch_texel_1d_f_rgbx8888,
154      fetch_texel_2d_f_rgbx8888,
155      fetch_texel_3d_f_rgbx8888
156   },
157   {
158      MESA_FORMAT_RGBX8888_REV,
159      fetch_texel_1d_f_rgbx8888_rev,
160      fetch_texel_2d_f_rgbx8888_rev,
161      fetch_texel_3d_f_rgbx8888_rev
162   },
163   {
164      MESA_FORMAT_XRGB8888,
165      fetch_texel_1d_f_xrgb8888,
166      fetch_texel_2d_f_xrgb8888,
167      fetch_texel_3d_f_xrgb8888
168   },
169   {
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   },
175   {
176      MESA_FORMAT_RGB888,
177      fetch_texel_1d_f_rgb888,
178      fetch_texel_2d_f_rgb888,
179      fetch_texel_3d_f_rgb888
180   },
181   {
182      MESA_FORMAT_BGR888,
183      fetch_texel_1d_f_bgr888,
184      fetch_texel_2d_f_bgr888,
185      fetch_texel_3d_f_bgr888
186   },
187   {
188      MESA_FORMAT_RGB565,
189      fetch_texel_1d_f_rgb565,
190      fetch_texel_2d_f_rgb565,
191      fetch_texel_3d_f_rgb565
192   },
193   {
194      MESA_FORMAT_RGB565_REV,
195      fetch_texel_1d_f_rgb565_rev,
196      fetch_texel_2d_f_rgb565_rev,
197      fetch_texel_3d_f_rgb565_rev
198   },
199   {
200      MESA_FORMAT_ARGB4444,
201      fetch_texel_1d_f_argb4444,
202      fetch_texel_2d_f_argb4444,
203      fetch_texel_3d_f_argb4444
204   },
205   {
206      MESA_FORMAT_ARGB4444_REV,
207      fetch_texel_1d_f_argb4444_rev,
208      fetch_texel_2d_f_argb4444_rev,
209      fetch_texel_3d_f_argb4444_rev
210   },
211   {
212      MESA_FORMAT_RGBA5551,
213      fetch_texel_1d_f_rgba5551,
214      fetch_texel_2d_f_rgba5551,
215      fetch_texel_3d_f_rgba5551
216   },
217   {
218      MESA_FORMAT_ARGB1555,
219      fetch_texel_1d_f_argb1555,
220      fetch_texel_2d_f_argb1555,
221      fetch_texel_3d_f_argb1555
222   },
223   {
224      MESA_FORMAT_ARGB1555_REV,
225      fetch_texel_1d_f_argb1555_rev,
226      fetch_texel_2d_f_argb1555_rev,
227      fetch_texel_3d_f_argb1555_rev
228   },
229   {
230      MESA_FORMAT_AL44,
231      fetch_texel_1d_f_al44,
232      fetch_texel_2d_f_al44,
233      fetch_texel_3d_f_al44
234   },
235   {
236      MESA_FORMAT_AL88,
237      fetch_texel_1d_f_al88,
238      fetch_texel_2d_f_al88,
239      fetch_texel_3d_f_al88
240   },
241   {
242      MESA_FORMAT_AL88_REV,
243      fetch_texel_1d_f_al88_rev,
244      fetch_texel_2d_f_al88_rev,
245      fetch_texel_3d_f_al88_rev
246   },
247   {
248      MESA_FORMAT_AL1616,
249      fetch_texel_1d_f_al1616,
250      fetch_texel_2d_f_al1616,
251      fetch_texel_3d_f_al1616
252   },
253   {
254      MESA_FORMAT_AL1616_REV,
255      fetch_texel_1d_f_al1616_rev,
256      fetch_texel_2d_f_al1616_rev,
257      fetch_texel_3d_f_al1616_rev
258   },
259   {
260      MESA_FORMAT_RGB332,
261      fetch_texel_1d_f_rgb332,
262      fetch_texel_2d_f_rgb332,
263      fetch_texel_3d_f_rgb332
264   },
265   {
266      MESA_FORMAT_A8,
267      fetch_texel_1d_f_a8,
268      fetch_texel_2d_f_a8,
269      fetch_texel_3d_f_a8
270   },
271   {
272      MESA_FORMAT_A16,
273      fetch_texel_1d_f_a16,
274      fetch_texel_2d_f_a16,
275      fetch_texel_3d_f_a16
276   },
277   {
278      MESA_FORMAT_L8,
279      fetch_texel_1d_f_l8,
280      fetch_texel_2d_f_l8,
281      fetch_texel_3d_f_l8
282   },
283   {
284      MESA_FORMAT_L16,
285      fetch_texel_1d_f_l16,
286      fetch_texel_2d_f_l16,
287      fetch_texel_3d_f_l16
288   },
289   {
290      MESA_FORMAT_I8,
291      fetch_texel_1d_f_i8,
292      fetch_texel_2d_f_i8,
293      fetch_texel_3d_f_i8
294   },
295   {
296      MESA_FORMAT_I16,
297      fetch_texel_1d_f_i16,
298      fetch_texel_2d_f_i16,
299      fetch_texel_3d_f_i16
300   },
301   {
302      MESA_FORMAT_YCBCR,
303      fetch_texel_1d_f_ycbcr,
304      fetch_texel_2d_f_ycbcr,
305      fetch_texel_3d_f_ycbcr
306   },
307   {
308      MESA_FORMAT_YCBCR_REV,
309      fetch_texel_1d_f_ycbcr_rev,
310      fetch_texel_2d_f_ycbcr_rev,
311      fetch_texel_3d_f_ycbcr_rev
312   },
313   {
314      MESA_FORMAT_R8,
315      fetch_texel_1d_f_r8,
316      fetch_texel_2d_f_r8,
317      fetch_texel_3d_f_r8
318   },
319   {
320      MESA_FORMAT_GR88,
321      fetch_texel_1d_f_gr88,
322      fetch_texel_2d_f_gr88,
323      fetch_texel_3d_f_gr88
324   },
325   {
326      MESA_FORMAT_RG88,
327      fetch_texel_1d_f_rg88,
328      fetch_texel_2d_f_rg88,
329      fetch_texel_3d_f_rg88
330   },
331   {
332      MESA_FORMAT_R16,
333      fetch_texel_1d_f_r16,
334      fetch_texel_2d_f_r16,
335      fetch_texel_3d_f_r16
336   },
337   {
338      MESA_FORMAT_RG1616,
339      fetch_texel_1d_f_rg1616,
340      fetch_texel_2d_f_rg1616,
341      fetch_texel_3d_f_rg1616
342   },
343   {
344      MESA_FORMAT_RG1616_REV,
345      fetch_texel_1d_f_rg1616_rev,
346      fetch_texel_2d_f_rg1616_rev,
347      fetch_texel_3d_f_rg1616_rev
348   },
349   {
350      MESA_FORMAT_ARGB2101010,
351      fetch_texel_1d_f_argb2101010,
352      fetch_texel_2d_f_argb2101010,
353      fetch_texel_3d_f_argb2101010
354   },
355   {
356      MESA_FORMAT_Z24_S8,
357      fetch_texel_1d_f_z24_s8,
358      fetch_texel_2d_f_z24_s8,
359      fetch_texel_3d_f_z24_s8
360   },
361   {
362      MESA_FORMAT_S8_Z24,
363      fetch_texel_1d_f_s8_z24,
364      fetch_texel_2d_f_s8_z24,
365      fetch_texel_3d_f_s8_z24
366   },
367   {
368      MESA_FORMAT_Z16,
369      fetch_texel_1d_f_z16,
370      fetch_texel_2d_f_z16,
371      fetch_texel_3d_f_z16
372   },
373   {
374      MESA_FORMAT_X8_Z24,
375      fetch_texel_1d_f_s8_z24,
376      fetch_texel_2d_f_s8_z24,
377      fetch_texel_3d_f_s8_z24
378   },
379   {
380      MESA_FORMAT_Z24_X8,
381      fetch_texel_1d_f_z24_s8,
382      fetch_texel_2d_f_z24_s8,
383      fetch_texel_3d_f_z24_s8
384   },
385   {
386      MESA_FORMAT_Z32,
387      fetch_texel_1d_f_z32,
388      fetch_texel_2d_f_z32,
389      fetch_texel_3d_f_z32
390   },
391   {
392      MESA_FORMAT_S8,
393      NULL,
394      NULL,
395      NULL
396   },
397   {
398      MESA_FORMAT_SRGB8,
399      fetch_texel_1d_srgb8,
400      fetch_texel_2d_srgb8,
401      fetch_texel_3d_srgb8
402   },
403   {
404      MESA_FORMAT_SRGBA8,
405      fetch_texel_1d_srgba8,
406      fetch_texel_2d_srgba8,
407      fetch_texel_3d_srgba8
408   },
409   {
410      MESA_FORMAT_SARGB8,
411      fetch_texel_1d_sargb8,
412      fetch_texel_2d_sargb8,
413      fetch_texel_3d_sargb8
414   },
415   {
416      MESA_FORMAT_SL8,
417      fetch_texel_1d_sl8,
418      fetch_texel_2d_sl8,
419      fetch_texel_3d_sl8
420   },
421   {
422      MESA_FORMAT_SLA8,
423      fetch_texel_1d_sla8,
424      fetch_texel_2d_sla8,
425      fetch_texel_3d_sla8
426   },
427   {
428      MESA_FORMAT_SRGB_DXT1,
429      _mesa_fetch_texel_srgb_dxt1,
430      _mesa_fetch_texel_srgb_dxt1,
431      _mesa_fetch_texel_srgb_dxt1
432   },
433   {
434      MESA_FORMAT_SRGBA_DXT1,
435      _mesa_fetch_texel_srgba_dxt1,
436      _mesa_fetch_texel_srgba_dxt1,
437      _mesa_fetch_texel_srgba_dxt1
438   },
439   {
440      MESA_FORMAT_SRGBA_DXT3,
441      _mesa_fetch_texel_srgba_dxt3,
442      _mesa_fetch_texel_srgba_dxt3,
443      _mesa_fetch_texel_srgba_dxt3
444   },
445   {
446      MESA_FORMAT_SRGBA_DXT5,
447      _mesa_fetch_texel_srgba_dxt5,
448      _mesa_fetch_texel_srgba_dxt5,
449      _mesa_fetch_texel_srgba_dxt5
450   },
451
452   {
453      MESA_FORMAT_RGB_FXT1,
454      NULL,
455      _mesa_fetch_texel_2d_f_rgb_fxt1,
456      NULL
457   },
458   {
459      MESA_FORMAT_RGBA_FXT1,
460      NULL,
461      _mesa_fetch_texel_2d_f_rgba_fxt1,
462      NULL
463   },
464   {
465      MESA_FORMAT_RGB_DXT1,
466      _mesa_fetch_texel_rgb_dxt1,
467      _mesa_fetch_texel_rgb_dxt1,
468      _mesa_fetch_texel_rgb_dxt1
469   },
470   {
471      MESA_FORMAT_RGBA_DXT1,
472      _mesa_fetch_texel_rgba_dxt1,
473      _mesa_fetch_texel_rgba_dxt1,
474      _mesa_fetch_texel_rgba_dxt1
475   },
476   {
477      MESA_FORMAT_RGBA_DXT3,
478      _mesa_fetch_texel_rgba_dxt3,
479      _mesa_fetch_texel_rgba_dxt3,
480      _mesa_fetch_texel_rgba_dxt3
481   },
482   {
483      MESA_FORMAT_RGBA_DXT5,
484      _mesa_fetch_texel_rgba_dxt5,
485      _mesa_fetch_texel_rgba_dxt5,
486      _mesa_fetch_texel_rgba_dxt5
487   },
488   {
489      MESA_FORMAT_RGBA_FLOAT32,
490      fetch_texel_1d_f_rgba_f32,
491      fetch_texel_2d_f_rgba_f32,
492      fetch_texel_3d_f_rgba_f32
493   },
494   {
495      MESA_FORMAT_RGBA_FLOAT16,
496      fetch_texel_1d_f_rgba_f16,
497      fetch_texel_2d_f_rgba_f16,
498      fetch_texel_3d_f_rgba_f16
499   },
500   {
501      MESA_FORMAT_RGB_FLOAT32,
502      fetch_texel_1d_f_rgb_f32,
503      fetch_texel_2d_f_rgb_f32,
504      fetch_texel_3d_f_rgb_f32
505   },
506   {
507      MESA_FORMAT_RGB_FLOAT16,
508      fetch_texel_1d_f_rgb_f16,
509      fetch_texel_2d_f_rgb_f16,
510      fetch_texel_3d_f_rgb_f16
511   },
512   {
513      MESA_FORMAT_ALPHA_FLOAT32,
514      fetch_texel_1d_f_alpha_f32,
515      fetch_texel_2d_f_alpha_f32,
516      fetch_texel_3d_f_alpha_f32
517   },
518   {
519      MESA_FORMAT_ALPHA_FLOAT16,
520      fetch_texel_1d_f_alpha_f16,
521      fetch_texel_2d_f_alpha_f16,
522      fetch_texel_3d_f_alpha_f16
523   },
524   {
525      MESA_FORMAT_LUMINANCE_FLOAT32,
526      fetch_texel_1d_f_luminance_f32,
527      fetch_texel_2d_f_luminance_f32,
528      fetch_texel_3d_f_luminance_f32
529   },
530   {
531      MESA_FORMAT_LUMINANCE_FLOAT16,
532      fetch_texel_1d_f_luminance_f16,
533      fetch_texel_2d_f_luminance_f16,
534      fetch_texel_3d_f_luminance_f16
535   },
536   {
537      MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32,
538      fetch_texel_1d_f_luminance_alpha_f32,
539      fetch_texel_2d_f_luminance_alpha_f32,
540      fetch_texel_3d_f_luminance_alpha_f32
541   },
542   {
543      MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16,
544      fetch_texel_1d_f_luminance_alpha_f16,
545      fetch_texel_2d_f_luminance_alpha_f16,
546      fetch_texel_3d_f_luminance_alpha_f16
547   },
548   {
549      MESA_FORMAT_INTENSITY_FLOAT32,
550      fetch_texel_1d_f_intensity_f32,
551      fetch_texel_2d_f_intensity_f32,
552      fetch_texel_3d_f_intensity_f32
553   },
554   {
555      MESA_FORMAT_INTENSITY_FLOAT16,
556      fetch_texel_1d_f_intensity_f16,
557      fetch_texel_2d_f_intensity_f16,
558      fetch_texel_3d_f_intensity_f16
559   },
560   {
561      MESA_FORMAT_R_FLOAT32,
562      fetch_texel_1d_f_r_f32,
563      fetch_texel_2d_f_r_f32,
564      fetch_texel_3d_f_r_f32
565   },
566   {
567      MESA_FORMAT_R_FLOAT16,
568      fetch_texel_1d_f_r_f16,
569      fetch_texel_2d_f_r_f16,
570      fetch_texel_3d_f_r_f16
571   },
572   {
573      MESA_FORMAT_RG_FLOAT32,
574      fetch_texel_1d_f_rg_f32,
575      fetch_texel_2d_f_rg_f32,
576      fetch_texel_3d_f_rg_f32
577   },
578   {
579      MESA_FORMAT_RG_FLOAT16,
580      fetch_texel_1d_f_rg_f16,
581      fetch_texel_2d_f_rg_f16,
582      fetch_texel_3d_f_rg_f16
583   },
584
585   {
586      MESA_FORMAT_ALPHA_UINT8,
587      NULL,
588      NULL,
589      NULL
590   },
591
592   {
593      MESA_FORMAT_ALPHA_UINT16,
594      NULL,
595      NULL,
596      NULL
597   },
598
599   {
600      MESA_FORMAT_ALPHA_UINT32,
601      NULL,
602      NULL,
603      NULL
604   },
605
606   {
607      MESA_FORMAT_ALPHA_INT8,
608      NULL,
609      NULL,
610      NULL
611   },
612
613   {
614      MESA_FORMAT_ALPHA_INT16,
615      NULL,
616      NULL,
617      NULL
618   },
619
620   {
621      MESA_FORMAT_ALPHA_INT32,
622      NULL,
623      NULL,
624      NULL
625   },
626
627
628   {
629      MESA_FORMAT_INTENSITY_UINT8,
630      NULL,
631      NULL,
632      NULL
633   },
634
635   {
636      MESA_FORMAT_INTENSITY_UINT16,
637      NULL,
638      NULL,
639      NULL
640   },
641
642   {
643      MESA_FORMAT_INTENSITY_UINT32,
644      NULL,
645      NULL,
646      NULL
647   },
648
649   {
650      MESA_FORMAT_INTENSITY_INT8,
651      NULL,
652      NULL,
653      NULL
654   },
655
656   {
657      MESA_FORMAT_INTENSITY_INT16,
658      NULL,
659      NULL,
660      NULL
661   },
662
663   {
664      MESA_FORMAT_INTENSITY_INT32,
665      NULL,
666      NULL,
667      NULL
668   },
669
670
671   {
672      MESA_FORMAT_LUMINANCE_UINT8,
673      NULL,
674      NULL,
675      NULL
676   },
677
678   {
679      MESA_FORMAT_LUMINANCE_UINT16,
680      NULL,
681      NULL,
682      NULL
683   },
684
685   {
686      MESA_FORMAT_LUMINANCE_UINT32,
687      NULL,
688      NULL,
689      NULL
690   },
691
692   {
693      MESA_FORMAT_LUMINANCE_INT8,
694      NULL,
695      NULL,
696      NULL
697   },
698
699   {
700      MESA_FORMAT_LUMINANCE_INT16,
701      NULL,
702      NULL,
703      NULL
704   },
705
706   {
707      MESA_FORMAT_LUMINANCE_INT32,
708      NULL,
709      NULL,
710      NULL
711   },
712
713
714   {
715      MESA_FORMAT_LUMINANCE_ALPHA_UINT8,
716      NULL,
717      NULL,
718      NULL
719   },
720
721   {
722      MESA_FORMAT_LUMINANCE_ALPHA_UINT16,
723      NULL,
724      NULL,
725      NULL
726   },
727
728   {
729      MESA_FORMAT_LUMINANCE_ALPHA_UINT32,
730      NULL,
731      NULL,
732      NULL
733   },
734
735   {
736      MESA_FORMAT_LUMINANCE_ALPHA_INT8,
737      NULL,
738      NULL,
739      NULL
740   },
741
742   {
743      MESA_FORMAT_LUMINANCE_ALPHA_INT16,
744      NULL,
745      NULL,
746      NULL
747   },
748
749   {
750      MESA_FORMAT_LUMINANCE_ALPHA_INT32,
751      NULL,
752      NULL,
753      NULL
754   },
755
756
757   {
758      MESA_FORMAT_R_INT8,
759      NULL,
760      NULL,
761      NULL
762   },
763
764   {
765      MESA_FORMAT_RG_INT8,
766      NULL,
767      NULL,
768      NULL
769   },
770
771   {
772      MESA_FORMAT_RGB_INT8,
773      NULL,
774      NULL,
775      NULL
776   },
777
778   /* non-normalized, signed int */
779   {
780      MESA_FORMAT_RGBA_INT8,
781      fetch_texel_1d_rgba_int8,
782      fetch_texel_2d_rgba_int8,
783      fetch_texel_3d_rgba_int8
784   },
785   {
786      MESA_FORMAT_R_INT16,
787      NULL,
788      NULL,
789      NULL
790   },
791   {
792      MESA_FORMAT_RG_INT16,
793      NULL,
794      NULL,
795      NULL
796   },
797   {
798      MESA_FORMAT_RGB_INT16,
799      NULL,
800      NULL,
801      NULL
802   },
803   {
804      MESA_FORMAT_RGBA_INT16,
805      fetch_texel_1d_rgba_int16,
806      fetch_texel_2d_rgba_int16,
807      fetch_texel_3d_rgba_int16
808   },
809   {
810      MESA_FORMAT_R_INT32,
811      NULL,
812      NULL,
813      NULL
814   },
815   {
816      MESA_FORMAT_RG_INT32,
817      NULL,
818      NULL,
819      NULL
820   },
821   {
822      MESA_FORMAT_RGB_INT32,
823      NULL,
824      NULL,
825      NULL
826   },
827   {
828      MESA_FORMAT_RGBA_INT32,
829      fetch_texel_1d_rgba_int32,
830      fetch_texel_2d_rgba_int32,
831      fetch_texel_3d_rgba_int32
832   },
833
834   /* non-normalized, unsigned int */
835   {
836      MESA_FORMAT_R_UINT8,
837      NULL,
838      NULL,
839      NULL
840   },
841   {
842      MESA_FORMAT_RG_UINT8,
843      NULL,
844      NULL,
845      NULL
846   },
847   {
848      MESA_FORMAT_RGB_UINT8,
849      NULL,
850      NULL,
851      NULL
852   },
853   {
854      MESA_FORMAT_RGBA_UINT8,
855      fetch_texel_1d_rgba_uint8,
856      fetch_texel_2d_rgba_uint8,
857      fetch_texel_3d_rgba_uint8
858   },
859   {
860      MESA_FORMAT_R_UINT16,
861      NULL,
862      NULL,
863      NULL
864   },
865   {
866      MESA_FORMAT_RG_UINT16,
867      NULL,
868      NULL,
869      NULL
870   },
871   {
872      MESA_FORMAT_RGB_UINT16,
873      NULL,
874      NULL,
875      NULL
876   },
877   {
878      MESA_FORMAT_RGBA_UINT16,
879      fetch_texel_1d_rgba_uint16,
880      fetch_texel_2d_rgba_uint16,
881      fetch_texel_3d_rgba_uint16
882   },
883   {
884      MESA_FORMAT_R_UINT32,
885      NULL,
886      NULL,
887      NULL
888   },
889   {
890      MESA_FORMAT_RG_UINT32,
891      NULL,
892      NULL,
893      NULL
894   },
895   {
896      MESA_FORMAT_RGB_UINT32,
897      NULL,
898      NULL,
899      NULL
900   },
901   {
902      MESA_FORMAT_RGBA_UINT32,
903      fetch_texel_1d_rgba_uint32,
904      fetch_texel_2d_rgba_uint32,
905      fetch_texel_3d_rgba_uint32
906   },
907
908   /* dudv */
909   {
910      MESA_FORMAT_DUDV8,
911      fetch_texel_1d_dudv8,
912      fetch_texel_2d_dudv8,
913      fetch_texel_3d_dudv8
914   },
915
916   /* signed, normalized */
917   {
918      MESA_FORMAT_SIGNED_R8,
919      fetch_texel_1d_signed_r8,
920      fetch_texel_2d_signed_r8,
921      fetch_texel_3d_signed_r8
922   },
923   {
924      MESA_FORMAT_SIGNED_RG88_REV,
925      fetch_texel_1d_signed_rg88_rev,
926      fetch_texel_2d_signed_rg88_rev,
927      fetch_texel_3d_signed_rg88_rev
928   },
929   {
930      MESA_FORMAT_SIGNED_RGBX8888,
931      fetch_texel_1d_signed_rgbx8888,
932      fetch_texel_2d_signed_rgbx8888,
933      fetch_texel_3d_signed_rgbx8888
934   },
935   {
936      MESA_FORMAT_SIGNED_RGBA8888,
937      fetch_texel_1d_signed_rgba8888,
938      fetch_texel_2d_signed_rgba8888,
939      fetch_texel_3d_signed_rgba8888
940   },
941   {
942      MESA_FORMAT_SIGNED_RGBA8888_REV,
943      fetch_texel_1d_signed_rgba8888_rev,
944      fetch_texel_2d_signed_rgba8888_rev,
945      fetch_texel_3d_signed_rgba8888_rev
946   },
947   {
948      MESA_FORMAT_SIGNED_R16,
949      fetch_texel_1d_signed_r16,
950      fetch_texel_2d_signed_r16,
951      fetch_texel_3d_signed_r16
952   },
953   {
954      MESA_FORMAT_SIGNED_GR1616,
955      fetch_texel_1d_signed_rg1616,
956      fetch_texel_2d_signed_rg1616,
957      fetch_texel_3d_signed_rg1616
958   },
959   {
960      MESA_FORMAT_SIGNED_RGB_16,
961      fetch_texel_1d_signed_rgb_16,
962      fetch_texel_2d_signed_rgb_16,
963      fetch_texel_3d_signed_rgb_16
964   },
965   {
966      MESA_FORMAT_SIGNED_RGBA_16,
967      fetch_texel_1d_signed_rgba_16,
968      fetch_texel_2d_signed_rgba_16,
969      fetch_texel_3d_signed_rgba_16
970   },
971   {
972      MESA_FORMAT_RGBA_16,
973      fetch_texel_1d_rgba_16,
974      fetch_texel_2d_rgba_16,
975      fetch_texel_3d_rgba_16
976   },
977   {
978      MESA_FORMAT_RED_RGTC1,
979      _mesa_fetch_texel_red_rgtc1,
980      _mesa_fetch_texel_red_rgtc1,
981      _mesa_fetch_texel_red_rgtc1
982   },
983   {
984      MESA_FORMAT_SIGNED_RED_RGTC1,
985      _mesa_fetch_texel_signed_red_rgtc1,
986      _mesa_fetch_texel_signed_red_rgtc1,
987      _mesa_fetch_texel_signed_red_rgtc1
988   },
989   {
990      MESA_FORMAT_RG_RGTC2,
991      _mesa_fetch_texel_rg_rgtc2,
992      _mesa_fetch_texel_rg_rgtc2,
993      _mesa_fetch_texel_rg_rgtc2
994   },
995   {
996      MESA_FORMAT_SIGNED_RG_RGTC2,
997      _mesa_fetch_texel_signed_rg_rgtc2,
998      _mesa_fetch_texel_signed_rg_rgtc2,
999      _mesa_fetch_texel_signed_rg_rgtc2
1000   },
1001   {
1002      MESA_FORMAT_L_LATC1,
1003      _mesa_fetch_texel_l_latc1,
1004      _mesa_fetch_texel_l_latc1,
1005      _mesa_fetch_texel_l_latc1
1006   },
1007   {
1008      MESA_FORMAT_SIGNED_L_LATC1,
1009      _mesa_fetch_texel_signed_l_latc1,
1010      _mesa_fetch_texel_signed_l_latc1,
1011      _mesa_fetch_texel_signed_l_latc1
1012   },
1013   {
1014      MESA_FORMAT_LA_LATC2,
1015      _mesa_fetch_texel_la_latc2,
1016      _mesa_fetch_texel_la_latc2,
1017      _mesa_fetch_texel_la_latc2
1018   },
1019   {
1020      MESA_FORMAT_SIGNED_LA_LATC2,
1021      _mesa_fetch_texel_signed_la_latc2,
1022      _mesa_fetch_texel_signed_la_latc2,
1023      _mesa_fetch_texel_signed_la_latc2
1024   },
1025   {
1026      MESA_FORMAT_ETC1_RGB8,
1027      NULL,
1028      _mesa_fetch_texel_2d_f_etc1_rgb8,
1029      NULL
1030   },
1031   {
1032      MESA_FORMAT_SIGNED_A8,
1033      fetch_texel_1d_signed_a8,
1034      fetch_texel_2d_signed_a8,
1035      fetch_texel_3d_signed_a8
1036   },
1037   {
1038      MESA_FORMAT_SIGNED_L8,
1039      fetch_texel_1d_signed_l8,
1040      fetch_texel_2d_signed_l8,
1041      fetch_texel_3d_signed_l8
1042   },
1043   {
1044      MESA_FORMAT_SIGNED_AL88,
1045      fetch_texel_1d_signed_al88,
1046      fetch_texel_2d_signed_al88,
1047      fetch_texel_3d_signed_al88
1048   },
1049   {
1050      MESA_FORMAT_SIGNED_I8,
1051      fetch_texel_1d_signed_i8,
1052      fetch_texel_2d_signed_i8,
1053      fetch_texel_3d_signed_i8
1054   },
1055   {
1056      MESA_FORMAT_SIGNED_A16,
1057      fetch_texel_1d_signed_a16,
1058      fetch_texel_2d_signed_a16,
1059      fetch_texel_3d_signed_a16
1060   },
1061   {
1062      MESA_FORMAT_SIGNED_L16,
1063      fetch_texel_1d_signed_l16,
1064      fetch_texel_2d_signed_l16,
1065      fetch_texel_3d_signed_l16
1066   },
1067   {
1068      MESA_FORMAT_SIGNED_AL1616,
1069      fetch_texel_1d_signed_al1616,
1070      fetch_texel_2d_signed_al1616,
1071      fetch_texel_3d_signed_al1616
1072   },
1073   {
1074      MESA_FORMAT_SIGNED_I16,
1075      fetch_texel_1d_signed_i16,
1076      fetch_texel_2d_signed_i16,
1077      fetch_texel_3d_signed_i16
1078   },
1079   {
1080      MESA_FORMAT_RGB9_E5_FLOAT,
1081      fetch_texel_1d_rgb9_e5,
1082      fetch_texel_2d_rgb9_e5,
1083      fetch_texel_3d_rgb9_e5
1084   },
1085   {
1086      MESA_FORMAT_R11_G11_B10_FLOAT,
1087      fetch_texel_1d_r11_g11_b10f,
1088      fetch_texel_2d_r11_g11_b10f,
1089      fetch_texel_3d_r11_g11_b10f
1090   },
1091   {
1092      MESA_FORMAT_Z32_FLOAT,
1093      fetch_texel_1d_f_r_f32, /* Reuse the R32F functions. */
1094      fetch_texel_2d_f_r_f32,
1095      fetch_texel_3d_f_r_f32
1096   },
1097   {
1098      MESA_FORMAT_Z32_FLOAT_X24S8,
1099      fetch_texel_1d_z32f_x24s8,
1100      fetch_texel_2d_z32f_x24s8,
1101      fetch_texel_3d_z32f_x24s8
1102   },
1103   {
1104      MESA_FORMAT_ARGB2101010_UINT,
1105      NULL,
1106      NULL,
1107      NULL
1108   },
1109   {
1110      MESA_FORMAT_ABGR2101010_UINT,
1111      NULL,
1112      NULL,
1113      NULL
1114   },
1115};
1116
1117
1118FetchTexelFunc
1119_mesa_get_texel_fetch_func(gl_format format, GLuint dims)
1120{
1121#ifdef DEBUG
1122   /* check that the table entries are sorted by format name */
1123   gl_format fmt;
1124   for (fmt = 0; fmt < MESA_FORMAT_COUNT; fmt++) {
1125      assert(texfetch_funcs[fmt].Name == fmt);
1126   }
1127#endif
1128
1129   STATIC_ASSERT(Elements(texfetch_funcs) == MESA_FORMAT_COUNT);
1130
1131   assert(format < MESA_FORMAT_COUNT);
1132
1133   switch (dims) {
1134   case 1:
1135      return texfetch_funcs[format].Fetch1D;
1136   case 2:
1137      return texfetch_funcs[format].Fetch2D;
1138   case 3:
1139      return texfetch_funcs[format].Fetch3D;
1140   default:
1141      assert(0 && "bad dims in _mesa_get_texel_fetch_func");
1142      return NULL;
1143   }
1144}
1145
1146
1147/**
1148 * Initialize the texture image's FetchTexel methods.
1149 */
1150static void
1151set_fetch_functions(struct gl_sampler_object *samp,
1152                    struct swrast_texture_image *texImage, GLuint dims)
1153{
1154   gl_format format = texImage->Base.TexFormat;
1155
1156   ASSERT(dims == 1 || dims == 2 || dims == 3);
1157
1158   if (samp->sRGBDecode == GL_SKIP_DECODE_EXT &&
1159       _mesa_get_format_color_encoding(format) == GL_SRGB) {
1160      format = _mesa_get_srgb_format_linear(format);
1161   }
1162
1163   texImage->FetchTexel = _mesa_get_texel_fetch_func(format, dims);
1164   ASSERT(texImage->FetchTexel);
1165}
1166
1167void
1168_mesa_update_fetch_functions(struct gl_context *ctx, GLuint unit)
1169{
1170   struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
1171   struct gl_sampler_object *samp;
1172   GLuint face, i;
1173   GLuint dims;
1174
1175   if (!texObj)
1176      return;
1177
1178   samp = _mesa_get_samplerobj(ctx, unit);
1179
1180   dims = _mesa_get_texture_dimensions(texObj->Target);
1181
1182   for (face = 0; face < 6; face++) {
1183      for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
1184         if (texObj->Image[face][i]) {
1185	    set_fetch_functions(samp,
1186                                swrast_texture_image(texObj->Image[face][i]),
1187                                dims);
1188         }
1189      }
1190   }
1191}
1192