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