8aa1e4970d5feb88c8e49b512adad55b557f73dc
[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 "texfetch.h"
42 #include "teximage.h"
43
44
45 /**
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.
49 */
50 static INLINE GLfloat
51 nonlinear_to_linear(GLubyte cs8)
52 {
53 static GLfloat table[256];
54 static GLboolean tableReady = GL_FALSE;
55 if (!tableReady) {
56 /* compute lookup table now */
57 GLuint i;
58 for (i = 0; i < 256; i++) {
59 const GLfloat cs = UBYTE_TO_FLOAT(i);
60 if (cs <= 0.04045) {
61 table[i] = cs / 12.92f;
62 }
63 else {
64 table[i] = (GLfloat) pow((cs + 0.055) / 1.055, 2.4);
65 }
66 }
67 tableReady = GL_TRUE;
68 }
69 return table[cs8];
70 }
71
72
73
74 /* Texel fetch routines for all supported formats
75 */
76 #define DIM 1
77 #include "texfetch_tmp.h"
78
79 #define DIM 2
80 #include "texfetch_tmp.h"
81
82 #define DIM 3
83 #include "texfetch_tmp.h"
84
85 /**
86 * Null texel fetch function.
87 *
88 * Have to have this so the FetchTexel function pointer is never NULL.
89 */
90 static void fetch_null_texelf( const struct gl_texture_image *texImage,
91 GLint i, GLint j, GLint k, GLfloat *texel )
92 {
93 (void) texImage; (void) i; (void) j; (void) k;
94 texel[RCOMP] = 0.0;
95 texel[GCOMP] = 0.0;
96 texel[BCOMP] = 0.0;
97 texel[ACOMP] = 0.0;
98 _mesa_warning(NULL, "fetch_null_texelf() called!");
99 }
100
101 static void store_null_texel(struct gl_texture_image *texImage,
102 GLint i, GLint j, GLint k, const void *texel)
103 {
104 (void) texImage;
105 (void) i;
106 (void) j;
107 (void) k;
108 (void) texel;
109 /* no-op */
110 }
111
112
113
114 /**
115 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
116 * XXX this is somewhat temporary.
117 */
118 static struct {
119 gl_format Name;
120 FetchTexelFuncF Fetch1D;
121 FetchTexelFuncF Fetch2D;
122 FetchTexelFuncF Fetch3D;
123 StoreTexelFunc StoreTexel;
124 }
125 texfetch_funcs[MESA_FORMAT_COUNT] =
126 {
127 {
128 MESA_FORMAT_NONE,
129 fetch_null_texelf,
130 fetch_null_texelf,
131 fetch_null_texelf,
132 store_null_texel
133 },
134
135 {
136 MESA_FORMAT_RGBA8888,
137 fetch_texel_1d_f_rgba8888,
138 fetch_texel_2d_f_rgba8888,
139 fetch_texel_3d_f_rgba8888,
140 store_texel_rgba8888
141 },
142 {
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
148 },
149 {
150 MESA_FORMAT_ARGB8888,
151 fetch_texel_1d_f_argb8888,
152 fetch_texel_2d_f_argb8888,
153 fetch_texel_3d_f_argb8888,
154 store_texel_argb8888
155 },
156 {
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
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 store_texel_xrgb8888
169 },
170 {
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,
176 },
177 {
178 MESA_FORMAT_RGB888,
179 fetch_texel_1d_f_rgb888,
180 fetch_texel_2d_f_rgb888,
181 fetch_texel_3d_f_rgb888,
182 store_texel_rgb888
183 },
184 {
185 MESA_FORMAT_BGR888,
186 fetch_texel_1d_f_bgr888,
187 fetch_texel_2d_f_bgr888,
188 fetch_texel_3d_f_bgr888,
189 store_texel_bgr888
190 },
191 {
192 MESA_FORMAT_RGB565,
193 fetch_texel_1d_f_rgb565,
194 fetch_texel_2d_f_rgb565,
195 fetch_texel_3d_f_rgb565,
196 store_texel_rgb565
197 },
198 {
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
204 },
205 {
206 MESA_FORMAT_ARGB4444,
207 fetch_texel_1d_f_argb4444,
208 fetch_texel_2d_f_argb4444,
209 fetch_texel_3d_f_argb4444,
210 store_texel_argb4444
211 },
212 {
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
218 },
219 {
220 MESA_FORMAT_RGBA5551,
221 fetch_texel_1d_f_rgba5551,
222 fetch_texel_2d_f_rgba5551,
223 fetch_texel_3d_f_rgba5551,
224 store_texel_rgba5551
225 },
226 {
227 MESA_FORMAT_ARGB1555,
228 fetch_texel_1d_f_argb1555,
229 fetch_texel_2d_f_argb1555,
230 fetch_texel_3d_f_argb1555,
231 store_texel_argb1555
232 },
233 {
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
239 },
240 {
241 MESA_FORMAT_AL44,
242 fetch_texel_1d_f_al44,
243 fetch_texel_2d_f_al44,
244 fetch_texel_3d_f_al44,
245 store_texel_al44
246 },
247 {
248 MESA_FORMAT_AL88,
249 fetch_texel_1d_f_al88,
250 fetch_texel_2d_f_al88,
251 fetch_texel_3d_f_al88,
252 store_texel_al88
253 },
254 {
255 MESA_FORMAT_AL88_REV,
256 fetch_texel_1d_f_al88_rev,
257 fetch_texel_2d_f_al88_rev,
258 fetch_texel_3d_f_al88_rev,
259 store_texel_al88_rev
260 },
261 {
262 MESA_FORMAT_AL1616,
263 fetch_texel_1d_f_al1616,
264 fetch_texel_2d_f_al1616,
265 fetch_texel_3d_f_al1616,
266 store_texel_al1616
267 },
268 {
269 MESA_FORMAT_AL1616_REV,
270 fetch_texel_1d_f_al1616_rev,
271 fetch_texel_2d_f_al1616_rev,
272 fetch_texel_3d_f_al1616_rev,
273 store_texel_al1616_rev
274 },
275 {
276 MESA_FORMAT_RGB332,
277 fetch_texel_1d_f_rgb332,
278 fetch_texel_2d_f_rgb332,
279 fetch_texel_3d_f_rgb332,
280 store_texel_rgb332
281 },
282 {
283 MESA_FORMAT_A8,
284 fetch_texel_1d_f_a8,
285 fetch_texel_2d_f_a8,
286 fetch_texel_3d_f_a8,
287 store_texel_a8
288 },
289 {
290 MESA_FORMAT_A16,
291 fetch_texel_1d_f_a16,
292 fetch_texel_2d_f_a16,
293 fetch_texel_3d_f_a16,
294 store_texel_a16
295 },
296 {
297 MESA_FORMAT_L8,
298 fetch_texel_1d_f_l8,
299 fetch_texel_2d_f_l8,
300 fetch_texel_3d_f_l8,
301 store_texel_l8
302 },
303 {
304 MESA_FORMAT_L16,
305 fetch_texel_1d_f_l16,
306 fetch_texel_2d_f_l16,
307 fetch_texel_3d_f_l16,
308 store_texel_l16
309 },
310 {
311 MESA_FORMAT_I8,
312 fetch_texel_1d_f_i8,
313 fetch_texel_2d_f_i8,
314 fetch_texel_3d_f_i8,
315 store_texel_i8
316 },
317 {
318 MESA_FORMAT_I16,
319 fetch_texel_1d_f_i16,
320 fetch_texel_2d_f_i16,
321 fetch_texel_3d_f_i16,
322 store_texel_i16
323 },
324 {
325 MESA_FORMAT_CI8,
326 fetch_texel_1d_f_ci8,
327 fetch_texel_2d_f_ci8,
328 fetch_texel_3d_f_ci8,
329 store_texel_ci8
330 },
331 {
332 MESA_FORMAT_YCBCR,
333 fetch_texel_1d_f_ycbcr,
334 fetch_texel_2d_f_ycbcr,
335 fetch_texel_3d_f_ycbcr,
336 store_texel_ycbcr
337 },
338 {
339 MESA_FORMAT_YCBCR_REV,
340 fetch_texel_1d_f_ycbcr_rev,
341 fetch_texel_2d_f_ycbcr_rev,
342 fetch_texel_3d_f_ycbcr_rev,
343 store_texel_ycbcr_rev
344 },
345 {
346 MESA_FORMAT_R8,
347 fetch_texel_1d_f_r8,
348 fetch_texel_2d_f_r8,
349 fetch_texel_3d_f_r8,
350 store_texel_r8,
351 },
352 {
353 MESA_FORMAT_RG88,
354 fetch_texel_1d_f_rg88,
355 fetch_texel_2d_f_rg88,
356 fetch_texel_3d_f_rg88,
357 store_texel_rg88,
358 },
359 {
360 MESA_FORMAT_RG88_REV,
361 fetch_texel_1d_f_rg88_rev,
362 fetch_texel_2d_f_rg88_rev,
363 fetch_texel_3d_f_rg88_rev,
364 store_texel_rg88_rev,
365 },
366 {
367 MESA_FORMAT_R16,
368 fetch_texel_1d_f_r16,
369 fetch_texel_2d_f_r16,
370 fetch_texel_3d_f_r16,
371 store_texel_r16,
372 },
373 {
374 MESA_FORMAT_RG1616,
375 fetch_texel_1d_f_rg1616,
376 fetch_texel_2d_f_rg1616,
377 fetch_texel_3d_f_rg1616,
378 store_texel_rg1616,
379 },
380 {
381 MESA_FORMAT_RG1616_REV,
382 fetch_texel_1d_f_rg1616_rev,
383 fetch_texel_2d_f_rg1616_rev,
384 fetch_texel_3d_f_rg1616_rev,
385 store_texel_rg1616_rev,
386 },
387 {
388 MESA_FORMAT_ARGB2101010,
389 fetch_texel_1d_f_argb2101010,
390 fetch_texel_2d_f_argb2101010,
391 fetch_texel_3d_f_argb2101010,
392 store_texel_argb2101010
393 },
394 {
395 MESA_FORMAT_Z24_S8,
396 fetch_texel_1d_f_z24_s8,
397 fetch_texel_2d_f_z24_s8,
398 fetch_texel_3d_f_z24_s8,
399 store_texel_z24_s8
400 },
401 {
402 MESA_FORMAT_S8_Z24,
403 fetch_texel_1d_f_s8_z24,
404 fetch_texel_2d_f_s8_z24,
405 fetch_texel_3d_f_s8_z24,
406 store_texel_s8_z24
407 },
408 {
409 MESA_FORMAT_Z16,
410 fetch_texel_1d_f_z16,
411 fetch_texel_2d_f_z16,
412 fetch_texel_3d_f_z16,
413 store_texel_z16
414 },
415 {
416 MESA_FORMAT_X8_Z24,
417 fetch_texel_1d_f_s8_z24,
418 fetch_texel_2d_f_s8_z24,
419 fetch_texel_3d_f_s8_z24,
420 store_texel_s8_z24
421 },
422 {
423 MESA_FORMAT_Z24_X8,
424 fetch_texel_1d_f_z24_s8,
425 fetch_texel_2d_f_z24_s8,
426 fetch_texel_3d_f_z24_s8,
427 store_texel_z24_s8
428 },
429 {
430 MESA_FORMAT_Z32,
431 fetch_texel_1d_f_z32,
432 fetch_texel_2d_f_z32,
433 fetch_texel_3d_f_z32,
434 store_texel_z32
435 },
436 {
437 MESA_FORMAT_S8,
438 NULL,
439 NULL,
440 NULL,
441 NULL
442 },
443 {
444 MESA_FORMAT_SRGB8,
445 fetch_texel_1d_srgb8,
446 fetch_texel_2d_srgb8,
447 fetch_texel_3d_srgb8,
448 store_texel_srgb8
449 },
450 {
451 MESA_FORMAT_SRGBA8,
452 fetch_texel_1d_srgba8,
453 fetch_texel_2d_srgba8,
454 fetch_texel_3d_srgba8,
455 store_texel_srgba8
456 },
457 {
458 MESA_FORMAT_SARGB8,
459 fetch_texel_1d_sargb8,
460 fetch_texel_2d_sargb8,
461 fetch_texel_3d_sargb8,
462 store_texel_sargb8
463 },
464 {
465 MESA_FORMAT_SL8,
466 fetch_texel_1d_sl8,
467 fetch_texel_2d_sl8,
468 fetch_texel_3d_sl8,
469 store_texel_sl8
470 },
471 {
472 MESA_FORMAT_SLA8,
473 fetch_texel_1d_sla8,
474 fetch_texel_2d_sla8,
475 fetch_texel_3d_sla8,
476 store_texel_sla8
477 },
478 {
479 MESA_FORMAT_SRGB_DXT1,
480 NULL,
481 _mesa_fetch_texel_2d_f_srgb_dxt1,
482 NULL,
483 NULL
484 },
485 {
486 MESA_FORMAT_SRGBA_DXT1,
487 NULL,
488 _mesa_fetch_texel_2d_f_srgba_dxt1,
489 NULL,
490 NULL
491 },
492 {
493 MESA_FORMAT_SRGBA_DXT3,
494 NULL,
495 _mesa_fetch_texel_2d_f_srgba_dxt3,
496 NULL,
497 NULL
498 },
499 {
500 MESA_FORMAT_SRGBA_DXT5,
501 NULL,
502 _mesa_fetch_texel_2d_f_srgba_dxt5,
503 NULL,
504 NULL
505 },
506
507 {
508 MESA_FORMAT_RGB_FXT1,
509 NULL,
510 _mesa_fetch_texel_2d_f_rgb_fxt1,
511 NULL,
512 NULL
513 },
514 {
515 MESA_FORMAT_RGBA_FXT1,
516 NULL,
517 _mesa_fetch_texel_2d_f_rgba_fxt1,
518 NULL,
519 NULL
520 },
521 {
522 MESA_FORMAT_RGB_DXT1,
523 NULL,
524 _mesa_fetch_texel_2d_f_rgb_dxt1,
525 NULL,
526 NULL
527 },
528 {
529 MESA_FORMAT_RGBA_DXT1,
530 NULL,
531 _mesa_fetch_texel_2d_f_rgba_dxt1,
532 NULL,
533 NULL
534 },
535 {
536 MESA_FORMAT_RGBA_DXT3,
537 NULL,
538 _mesa_fetch_texel_2d_f_rgba_dxt3,
539 NULL,
540 NULL
541 },
542 {
543 MESA_FORMAT_RGBA_DXT5,
544 NULL,
545 _mesa_fetch_texel_2d_f_rgba_dxt5,
546 NULL,
547 NULL
548 },
549 {
550 MESA_FORMAT_RGBA_FLOAT32,
551 fetch_texel_1d_f_rgba_f32,
552 fetch_texel_2d_f_rgba_f32,
553 fetch_texel_3d_f_rgba_f32,
554 store_texel_rgba_f32
555 },
556 {
557 MESA_FORMAT_RGBA_FLOAT16,
558 fetch_texel_1d_f_rgba_f16,
559 fetch_texel_2d_f_rgba_f16,
560 fetch_texel_3d_f_rgba_f16,
561 store_texel_rgba_f16
562 },
563 {
564 MESA_FORMAT_RGB_FLOAT32,
565 fetch_texel_1d_f_rgb_f32,
566 fetch_texel_2d_f_rgb_f32,
567 fetch_texel_3d_f_rgb_f32,
568 store_texel_rgb_f32
569 },
570 {
571 MESA_FORMAT_RGB_FLOAT16,
572 fetch_texel_1d_f_rgb_f16,
573 fetch_texel_2d_f_rgb_f16,
574 fetch_texel_3d_f_rgb_f16,
575 store_texel_rgb_f16
576 },
577 {
578 MESA_FORMAT_ALPHA_FLOAT32,
579 fetch_texel_1d_f_alpha_f32,
580 fetch_texel_2d_f_alpha_f32,
581 fetch_texel_3d_f_alpha_f32,
582 store_texel_alpha_f32
583 },
584 {
585 MESA_FORMAT_ALPHA_FLOAT16,
586 fetch_texel_1d_f_alpha_f16,
587 fetch_texel_2d_f_alpha_f16,
588 fetch_texel_3d_f_alpha_f16,
589 store_texel_alpha_f16
590 },
591 {
592 MESA_FORMAT_LUMINANCE_FLOAT32,
593 fetch_texel_1d_f_luminance_f32,
594 fetch_texel_2d_f_luminance_f32,
595 fetch_texel_3d_f_luminance_f32,
596 store_texel_luminance_f32
597 },
598 {
599 MESA_FORMAT_LUMINANCE_FLOAT16,
600 fetch_texel_1d_f_luminance_f16,
601 fetch_texel_2d_f_luminance_f16,
602 fetch_texel_3d_f_luminance_f16,
603 store_texel_luminance_f16
604 },
605 {
606 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32,
607 fetch_texel_1d_f_luminance_alpha_f32,
608 fetch_texel_2d_f_luminance_alpha_f32,
609 fetch_texel_3d_f_luminance_alpha_f32,
610 store_texel_luminance_alpha_f32
611 },
612 {
613 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16,
614 fetch_texel_1d_f_luminance_alpha_f16,
615 fetch_texel_2d_f_luminance_alpha_f16,
616 fetch_texel_3d_f_luminance_alpha_f16,
617 store_texel_luminance_alpha_f16
618 },
619 {
620 MESA_FORMAT_INTENSITY_FLOAT32,
621 fetch_texel_1d_f_intensity_f32,
622 fetch_texel_2d_f_intensity_f32,
623 fetch_texel_3d_f_intensity_f32,
624 store_texel_intensity_f32
625 },
626 {
627 MESA_FORMAT_INTENSITY_FLOAT16,
628 fetch_texel_1d_f_intensity_f16,
629 fetch_texel_2d_f_intensity_f16,
630 fetch_texel_3d_f_intensity_f16,
631 store_texel_intensity_f16
632 },
633
634 /* non-normalized, signed int */
635 {
636 MESA_FORMAT_RGBA_INT8,
637 fetch_texel_1d_rgba_int8,
638 fetch_texel_2d_rgba_int8,
639 fetch_texel_3d_rgba_int8,
640 store_texel_rgba_int8
641 },
642 {
643 MESA_FORMAT_RGBA_INT16,
644 fetch_texel_1d_rgba_int16,
645 fetch_texel_2d_rgba_int16,
646 fetch_texel_3d_rgba_int16,
647 store_texel_rgba_int16
648 },
649 {
650 MESA_FORMAT_RGBA_INT32,
651 fetch_texel_1d_rgba_int32,
652 fetch_texel_2d_rgba_int32,
653 fetch_texel_3d_rgba_int32,
654 store_texel_rgba_int32
655 },
656
657 /* non-normalized, unsigned int */
658 {
659 MESA_FORMAT_RGBA_UINT8,
660 fetch_texel_1d_rgba_uint8,
661 fetch_texel_2d_rgba_uint8,
662 fetch_texel_3d_rgba_uint8,
663 store_texel_rgba_uint8
664 },
665 {
666 MESA_FORMAT_RGBA_UINT16,
667 fetch_texel_1d_rgba_uint16,
668 fetch_texel_2d_rgba_uint16,
669 fetch_texel_3d_rgba_uint16,
670 store_texel_rgba_uint16
671 },
672 {
673 MESA_FORMAT_RGBA_UINT32,
674 fetch_texel_1d_rgba_uint32,
675 fetch_texel_2d_rgba_uint32,
676 fetch_texel_3d_rgba_uint32,
677 store_texel_rgba_uint32
678 },
679
680 /* dudv */
681 {
682 MESA_FORMAT_DUDV8,
683 fetch_texel_1d_dudv8,
684 fetch_texel_2d_dudv8,
685 fetch_texel_3d_dudv8,
686 NULL
687 },
688
689 /* signed, normalized */
690 {
691 MESA_FORMAT_SIGNED_R8,
692 fetch_texel_1d_signed_r8,
693 fetch_texel_2d_signed_r8,
694 fetch_texel_3d_signed_r8,
695 store_texel_signed_r8
696 },
697 {
698 MESA_FORMAT_SIGNED_RG88,
699 fetch_texel_1d_signed_rg88,
700 fetch_texel_2d_signed_rg88,
701 fetch_texel_3d_signed_rg88,
702 store_texel_signed_rg88
703 },
704 {
705 MESA_FORMAT_SIGNED_RGBX8888,
706 fetch_texel_1d_signed_rgbx8888,
707 fetch_texel_2d_signed_rgbx8888,
708 fetch_texel_3d_signed_rgbx8888,
709 store_texel_signed_rgbx8888
710 },
711 {
712 MESA_FORMAT_SIGNED_RGBA8888,
713 fetch_texel_1d_signed_rgba8888,
714 fetch_texel_2d_signed_rgba8888,
715 fetch_texel_3d_signed_rgba8888,
716 store_texel_signed_rgba8888
717 },
718 {
719 MESA_FORMAT_SIGNED_RGBA8888_REV,
720 fetch_texel_1d_signed_rgba8888_rev,
721 fetch_texel_2d_signed_rgba8888_rev,
722 fetch_texel_3d_signed_rgba8888_rev,
723 store_texel_signed_rgba8888_rev
724 },
725 {
726 MESA_FORMAT_SIGNED_R_16,
727 fetch_texel_1d_signed_r_16,
728 fetch_texel_2d_signed_r_16,
729 fetch_texel_3d_signed_r_16,
730 store_texel_signed_r_16
731 },
732 {
733 MESA_FORMAT_SIGNED_RG_16,
734 fetch_texel_1d_signed_rg_16,
735 fetch_texel_2d_signed_rg_16,
736 fetch_texel_3d_signed_rg_16,
737 store_texel_signed_rg_16
738 },
739 {
740 MESA_FORMAT_SIGNED_RGB_16,
741 fetch_texel_1d_signed_rgb_16,
742 fetch_texel_2d_signed_rgb_16,
743 fetch_texel_3d_signed_rgb_16,
744 store_texel_signed_rgb_16
745 },
746 {
747 MESA_FORMAT_SIGNED_RGBA_16,
748 fetch_texel_1d_signed_rgba_16,
749 fetch_texel_2d_signed_rgba_16,
750 fetch_texel_3d_signed_rgba_16,
751 store_texel_signed_rgba_16
752 },
753 {
754 MESA_FORMAT_RGBA_16,
755 fetch_texel_1d_rgba_16,
756 fetch_texel_2d_rgba_16,
757 fetch_texel_3d_rgba_16,
758 store_texel_rgba_16
759 }
760 };
761
762
763 FetchTexelFuncF
764 _mesa_get_texel_fetch_func(gl_format format, GLuint dims)
765 {
766 #ifdef DEBUG
767 /* check that the table entries are sorted by format name */
768 gl_format fmt;
769 for (fmt = 0; fmt < MESA_FORMAT_COUNT; fmt++) {
770 assert(texfetch_funcs[fmt].Name == fmt);
771 }
772 #endif
773
774 assert(Elements(texfetch_funcs) == MESA_FORMAT_COUNT);
775 assert(format < MESA_FORMAT_COUNT);
776
777 switch (dims) {
778 case 1:
779 return texfetch_funcs[format].Fetch1D;
780 case 2:
781 return texfetch_funcs[format].Fetch2D;
782 case 3:
783 return texfetch_funcs[format].Fetch3D;
784 default:
785 assert(0 && "bad dims in _mesa_get_texel_fetch_func");
786 return NULL;
787 }
788 }
789
790
791 StoreTexelFunc
792 _mesa_get_texel_store_func(gl_format format)
793 {
794 assert(format < MESA_FORMAT_COUNT);
795 return texfetch_funcs[format].StoreTexel;
796 }
797
798
799 /**
800 * Adaptor for fetching a GLchan texel from a float-valued texture.
801 */
802 static void
803 fetch_texel_float_to_chan(const struct gl_texture_image *texImage,
804 GLint i, GLint j, GLint k, GLchan *texelOut)
805 {
806 GLfloat temp[4];
807 GLenum baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
808
809 ASSERT(texImage->FetchTexelf);
810 texImage->FetchTexelf(texImage, i, j, k, temp);
811 if (baseFormat == GL_DEPTH_COMPONENT ||
812 baseFormat == GL_DEPTH_STENCIL_EXT) {
813 /* just one channel */
814 UNCLAMPED_FLOAT_TO_CHAN(texelOut[0], temp[0]);
815 }
816 else {
817 /* four channels */
818 UNCLAMPED_FLOAT_TO_CHAN(texelOut[0], temp[0]);
819 UNCLAMPED_FLOAT_TO_CHAN(texelOut[1], temp[1]);
820 UNCLAMPED_FLOAT_TO_CHAN(texelOut[2], temp[2]);
821 UNCLAMPED_FLOAT_TO_CHAN(texelOut[3], temp[3]);
822 }
823 }
824
825
826 #if 0
827 /**
828 * Adaptor for fetching a float texel from a GLchan-valued texture.
829 */
830 static void
831 fetch_texel_chan_to_float(const struct gl_texture_image *texImage,
832 GLint i, GLint j, GLint k, GLfloat *texelOut)
833 {
834 GLchan temp[4];
835 GLenum baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
836
837 ASSERT(texImage->FetchTexelc);
838 texImage->FetchTexelc(texImage, i, j, k, temp);
839 if (baseFormat == GL_DEPTH_COMPONENT ||
840 baseFormat == GL_DEPTH_STENCIL_EXT) {
841 /* just one channel */
842 texelOut[0] = CHAN_TO_FLOAT(temp[0]);
843 }
844 else {
845 /* four channels */
846 texelOut[0] = CHAN_TO_FLOAT(temp[0]);
847 texelOut[1] = CHAN_TO_FLOAT(temp[1]);
848 texelOut[2] = CHAN_TO_FLOAT(temp[2]);
849 texelOut[3] = CHAN_TO_FLOAT(temp[3]);
850 }
851 }
852 #endif
853
854
855 /**
856 * Initialize the texture image's FetchTexelc and FetchTexelf methods.
857 */
858 void
859 _mesa_set_fetch_functions(struct gl_texture_image *texImage, GLuint dims)
860 {
861 gl_format format = texImage->TexFormat;
862
863 ASSERT(dims == 1 || dims == 2 || dims == 3);
864
865 if (texImage->TexObject->sRGBDecode == GL_SKIP_DECODE_EXT &&
866 _mesa_get_format_color_encoding(format) == GL_SRGB) {
867 format = _mesa_get_srgb_format_linear(format);
868 }
869
870 texImage->FetchTexelf = _mesa_get_texel_fetch_func(format, dims);
871
872 texImage->FetchTexelc = fetch_texel_float_to_chan;
873
874 ASSERT(texImage->FetchTexelc);
875 ASSERT(texImage->FetchTexelf);
876 }
877
878 void
879 _mesa_update_fetch_functions(struct gl_texture_object *texObj)
880 {
881 GLuint face, i;
882 GLuint dims;
883
884 dims = _mesa_get_texture_dimensions(texObj->Target);
885
886 for (face = 0; face < 6; face++) {
887 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
888 if (texObj->Image[face][i]) {
889 _mesa_set_fetch_functions(texObj->Image[face][i], dims);
890 }
891 }
892 }
893 }