[MESA]
[reactos.git] / reactos / dll / opengl / mesa / 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/teximage.h"
39 #include "s_context.h"
40 #include "s_texfetch.h"
41
42
43 /**
44 * Convert an 8-bit sRGB value from non-linear space to a
45 * linear RGB value in [0, 1].
46 * Implemented with a 256-entry lookup table.
47 */
48 static inline GLfloat
49 nonlinear_to_linear(GLubyte cs8)
50 {
51 static GLfloat table[256];
52 static GLboolean tableReady = GL_FALSE;
53 if (!tableReady) {
54 /* compute lookup table now */
55 GLuint i;
56 for (i = 0; i < 256; i++) {
57 const GLfloat cs = UBYTE_TO_FLOAT(i);
58 if (cs <= 0.04045) {
59 table[i] = cs / 12.92f;
60 }
61 else {
62 table[i] = (GLfloat) pow((cs + 0.055) / 1.055, 2.4);
63 }
64 }
65 tableReady = GL_TRUE;
66 }
67 return table[cs8];
68 }
69
70
71
72 /* Texel fetch routines for all supported formats
73 */
74 #define DIM 1
75 #include "s_texfetch_tmp.h"
76
77 #define DIM 2
78 #include "s_texfetch_tmp.h"
79
80 #define DIM 3
81 #include "s_texfetch_tmp.h"
82
83 /**
84 * Null texel fetch function.
85 *
86 * Have to have this so the FetchTexel function pointer is never NULL.
87 */
88 static void fetch_null_texelf( const struct swrast_texture_image *texImage,
89 GLint i, GLint j, GLint k, GLfloat *texel )
90 {
91 (void) texImage; (void) i; (void) j; (void) k;
92 texel[RCOMP] = 0.0;
93 texel[GCOMP] = 0.0;
94 texel[BCOMP] = 0.0;
95 texel[ACOMP] = 0.0;
96 _mesa_warning(NULL, "fetch_null_texelf() called!");
97 }
98
99
100 /**
101 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
102 * XXX this is somewhat temporary.
103 */
104 static struct {
105 gl_format Name;
106 FetchTexelFunc Fetch1D;
107 FetchTexelFunc Fetch2D;
108 FetchTexelFunc Fetch3D;
109 }
110 texfetch_funcs[MESA_FORMAT_COUNT] =
111 {
112 {
113 MESA_FORMAT_NONE,
114 fetch_null_texelf,
115 fetch_null_texelf,
116 fetch_null_texelf
117 },
118
119 {
120 MESA_FORMAT_RGBA8888,
121 fetch_texel_1d_f_rgba8888,
122 fetch_texel_2d_f_rgba8888,
123 fetch_texel_3d_f_rgba8888
124 },
125 {
126 MESA_FORMAT_RGBA8888_REV,
127 fetch_texel_1d_f_rgba8888_rev,
128 fetch_texel_2d_f_rgba8888_rev,
129 fetch_texel_3d_f_rgba8888_rev
130 },
131 {
132 MESA_FORMAT_ARGB8888,
133 fetch_texel_1d_f_argb8888,
134 fetch_texel_2d_f_argb8888,
135 fetch_texel_3d_f_argb8888
136 },
137 {
138 MESA_FORMAT_ARGB8888_REV,
139 fetch_texel_1d_f_argb8888_rev,
140 fetch_texel_2d_f_argb8888_rev,
141 fetch_texel_3d_f_argb8888_rev
142 },
143 {
144 MESA_FORMAT_RGBX8888,
145 fetch_texel_1d_f_rgbx8888,
146 fetch_texel_2d_f_rgbx8888,
147 fetch_texel_3d_f_rgbx8888
148 },
149 {
150 MESA_FORMAT_RGBX8888_REV,
151 fetch_texel_1d_f_rgbx8888_rev,
152 fetch_texel_2d_f_rgbx8888_rev,
153 fetch_texel_3d_f_rgbx8888_rev
154 },
155 {
156 MESA_FORMAT_XRGB8888,
157 fetch_texel_1d_f_xrgb8888,
158 fetch_texel_2d_f_xrgb8888,
159 fetch_texel_3d_f_xrgb8888
160 },
161 {
162 MESA_FORMAT_XRGB8888_REV,
163 fetch_texel_1d_f_xrgb8888_rev,
164 fetch_texel_2d_f_xrgb8888_rev,
165 fetch_texel_3d_f_xrgb8888_rev
166 },
167 {
168 MESA_FORMAT_RGB888,
169 fetch_texel_1d_f_rgb888,
170 fetch_texel_2d_f_rgb888,
171 fetch_texel_3d_f_rgb888
172 },
173 {
174 MESA_FORMAT_BGR888,
175 fetch_texel_1d_f_bgr888,
176 fetch_texel_2d_f_bgr888,
177 fetch_texel_3d_f_bgr888
178 },
179 {
180 MESA_FORMAT_RGB565,
181 fetch_texel_1d_f_rgb565,
182 fetch_texel_2d_f_rgb565,
183 fetch_texel_3d_f_rgb565
184 },
185 {
186 MESA_FORMAT_RGB565_REV,
187 fetch_texel_1d_f_rgb565_rev,
188 fetch_texel_2d_f_rgb565_rev,
189 fetch_texel_3d_f_rgb565_rev
190 },
191 {
192 MESA_FORMAT_ARGB4444,
193 fetch_texel_1d_f_argb4444,
194 fetch_texel_2d_f_argb4444,
195 fetch_texel_3d_f_argb4444
196 },
197 {
198 MESA_FORMAT_ARGB4444_REV,
199 fetch_texel_1d_f_argb4444_rev,
200 fetch_texel_2d_f_argb4444_rev,
201 fetch_texel_3d_f_argb4444_rev
202 },
203 {
204 MESA_FORMAT_RGBA5551,
205 fetch_texel_1d_f_rgba5551,
206 fetch_texel_2d_f_rgba5551,
207 fetch_texel_3d_f_rgba5551
208 },
209 {
210 MESA_FORMAT_ARGB1555,
211 fetch_texel_1d_f_argb1555,
212 fetch_texel_2d_f_argb1555,
213 fetch_texel_3d_f_argb1555
214 },
215 {
216 MESA_FORMAT_ARGB1555_REV,
217 fetch_texel_1d_f_argb1555_rev,
218 fetch_texel_2d_f_argb1555_rev,
219 fetch_texel_3d_f_argb1555_rev
220 },
221 {
222 MESA_FORMAT_AL44,
223 fetch_texel_1d_f_al44,
224 fetch_texel_2d_f_al44,
225 fetch_texel_3d_f_al44
226 },
227 {
228 MESA_FORMAT_AL88,
229 fetch_texel_1d_f_al88,
230 fetch_texel_2d_f_al88,
231 fetch_texel_3d_f_al88
232 },
233 {
234 MESA_FORMAT_AL88_REV,
235 fetch_texel_1d_f_al88_rev,
236 fetch_texel_2d_f_al88_rev,
237 fetch_texel_3d_f_al88_rev
238 },
239 {
240 MESA_FORMAT_AL1616,
241 fetch_texel_1d_f_al1616,
242 fetch_texel_2d_f_al1616,
243 fetch_texel_3d_f_al1616
244 },
245 {
246 MESA_FORMAT_AL1616_REV,
247 fetch_texel_1d_f_al1616_rev,
248 fetch_texel_2d_f_al1616_rev,
249 fetch_texel_3d_f_al1616_rev
250 },
251 {
252 MESA_FORMAT_RGB332,
253 fetch_texel_1d_f_rgb332,
254 fetch_texel_2d_f_rgb332,
255 fetch_texel_3d_f_rgb332
256 },
257 {
258 MESA_FORMAT_A8,
259 fetch_texel_1d_f_a8,
260 fetch_texel_2d_f_a8,
261 fetch_texel_3d_f_a8
262 },
263 {
264 MESA_FORMAT_A16,
265 fetch_texel_1d_f_a16,
266 fetch_texel_2d_f_a16,
267 fetch_texel_3d_f_a16
268 },
269 {
270 MESA_FORMAT_L8,
271 fetch_texel_1d_f_l8,
272 fetch_texel_2d_f_l8,
273 fetch_texel_3d_f_l8
274 },
275 {
276 MESA_FORMAT_L16,
277 fetch_texel_1d_f_l16,
278 fetch_texel_2d_f_l16,
279 fetch_texel_3d_f_l16
280 },
281 {
282 MESA_FORMAT_I8,
283 fetch_texel_1d_f_i8,
284 fetch_texel_2d_f_i8,
285 fetch_texel_3d_f_i8
286 },
287 {
288 MESA_FORMAT_I16,
289 fetch_texel_1d_f_i16,
290 fetch_texel_2d_f_i16,
291 fetch_texel_3d_f_i16
292 },
293 {
294 MESA_FORMAT_YCBCR,
295 fetch_texel_1d_f_ycbcr,
296 fetch_texel_2d_f_ycbcr,
297 fetch_texel_3d_f_ycbcr
298 },
299 {
300 MESA_FORMAT_YCBCR_REV,
301 fetch_texel_1d_f_ycbcr_rev,
302 fetch_texel_2d_f_ycbcr_rev,
303 fetch_texel_3d_f_ycbcr_rev
304 },
305 {
306 MESA_FORMAT_Z16,
307 fetch_texel_1d_f_z16,
308 fetch_texel_2d_f_z16,
309 fetch_texel_3d_f_z16
310 },
311 {
312 MESA_FORMAT_X8_Z24,
313 fetch_texel_1d_f_s8_z24,
314 fetch_texel_2d_f_s8_z24,
315 fetch_texel_3d_f_s8_z24
316 },
317 {
318 MESA_FORMAT_Z24_X8,
319 fetch_texel_1d_f_z24_s8,
320 fetch_texel_2d_f_z24_s8,
321 fetch_texel_3d_f_z24_s8
322 },
323 {
324 MESA_FORMAT_Z32,
325 fetch_texel_1d_f_z32,
326 fetch_texel_2d_f_z32,
327 fetch_texel_3d_f_z32
328 },
329 {
330 MESA_FORMAT_S8,
331 NULL,
332 NULL,
333 NULL
334 },
335 {
336 MESA_FORMAT_RGBA_FLOAT32,
337 fetch_texel_1d_f_rgba_f32,
338 fetch_texel_2d_f_rgba_f32,
339 fetch_texel_3d_f_rgba_f32
340 },
341 {
342 MESA_FORMAT_RGBA_FLOAT16,
343 fetch_texel_1d_f_rgba_f16,
344 fetch_texel_2d_f_rgba_f16,
345 fetch_texel_3d_f_rgba_f16
346 },
347 {
348 MESA_FORMAT_RGB_FLOAT32,
349 fetch_texel_1d_f_rgb_f32,
350 fetch_texel_2d_f_rgb_f32,
351 fetch_texel_3d_f_rgb_f32
352 },
353 {
354 MESA_FORMAT_RGB_FLOAT16,
355 fetch_texel_1d_f_rgb_f16,
356 fetch_texel_2d_f_rgb_f16,
357 fetch_texel_3d_f_rgb_f16
358 },
359 {
360 MESA_FORMAT_ALPHA_FLOAT32,
361 fetch_texel_1d_f_alpha_f32,
362 fetch_texel_2d_f_alpha_f32,
363 fetch_texel_3d_f_alpha_f32
364 },
365 {
366 MESA_FORMAT_ALPHA_FLOAT16,
367 fetch_texel_1d_f_alpha_f16,
368 fetch_texel_2d_f_alpha_f16,
369 fetch_texel_3d_f_alpha_f16
370 },
371 {
372 MESA_FORMAT_LUMINANCE_FLOAT32,
373 fetch_texel_1d_f_luminance_f32,
374 fetch_texel_2d_f_luminance_f32,
375 fetch_texel_3d_f_luminance_f32
376 },
377 {
378 MESA_FORMAT_LUMINANCE_FLOAT16,
379 fetch_texel_1d_f_luminance_f16,
380 fetch_texel_2d_f_luminance_f16,
381 fetch_texel_3d_f_luminance_f16
382 },
383 {
384 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32,
385 fetch_texel_1d_f_luminance_alpha_f32,
386 fetch_texel_2d_f_luminance_alpha_f32,
387 fetch_texel_3d_f_luminance_alpha_f32
388 },
389 {
390 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16,
391 fetch_texel_1d_f_luminance_alpha_f16,
392 fetch_texel_2d_f_luminance_alpha_f16,
393 fetch_texel_3d_f_luminance_alpha_f16
394 },
395 {
396 MESA_FORMAT_INTENSITY_FLOAT32,
397 fetch_texel_1d_f_intensity_f32,
398 fetch_texel_2d_f_intensity_f32,
399 fetch_texel_3d_f_intensity_f32
400 },
401 {
402 MESA_FORMAT_INTENSITY_FLOAT16,
403 fetch_texel_1d_f_intensity_f16,
404 fetch_texel_2d_f_intensity_f16,
405 fetch_texel_3d_f_intensity_f16
406 },
407
408 {
409 MESA_FORMAT_ALPHA_UINT8,
410 NULL,
411 NULL,
412 NULL
413 },
414
415 {
416 MESA_FORMAT_ALPHA_UINT16,
417 NULL,
418 NULL,
419 NULL
420 },
421
422 {
423 MESA_FORMAT_ALPHA_UINT32,
424 NULL,
425 NULL,
426 NULL
427 },
428
429 {
430 MESA_FORMAT_ALPHA_INT8,
431 NULL,
432 NULL,
433 NULL
434 },
435
436 {
437 MESA_FORMAT_ALPHA_INT16,
438 NULL,
439 NULL,
440 NULL
441 },
442
443 {
444 MESA_FORMAT_ALPHA_INT32,
445 NULL,
446 NULL,
447 NULL
448 },
449
450
451 {
452 MESA_FORMAT_INTENSITY_UINT8,
453 NULL,
454 NULL,
455 NULL
456 },
457
458 {
459 MESA_FORMAT_INTENSITY_UINT16,
460 NULL,
461 NULL,
462 NULL
463 },
464
465 {
466 MESA_FORMAT_INTENSITY_UINT32,
467 NULL,
468 NULL,
469 NULL
470 },
471
472 {
473 MESA_FORMAT_INTENSITY_INT8,
474 NULL,
475 NULL,
476 NULL
477 },
478
479 {
480 MESA_FORMAT_INTENSITY_INT16,
481 NULL,
482 NULL,
483 NULL
484 },
485
486 {
487 MESA_FORMAT_INTENSITY_INT32,
488 NULL,
489 NULL,
490 NULL
491 },
492
493
494 {
495 MESA_FORMAT_LUMINANCE_UINT8,
496 NULL,
497 NULL,
498 NULL
499 },
500
501 {
502 MESA_FORMAT_LUMINANCE_UINT16,
503 NULL,
504 NULL,
505 NULL
506 },
507
508 {
509 MESA_FORMAT_LUMINANCE_UINT32,
510 NULL,
511 NULL,
512 NULL
513 },
514
515 {
516 MESA_FORMAT_LUMINANCE_INT8,
517 NULL,
518 NULL,
519 NULL
520 },
521
522 {
523 MESA_FORMAT_LUMINANCE_INT16,
524 NULL,
525 NULL,
526 NULL
527 },
528
529 {
530 MESA_FORMAT_LUMINANCE_INT32,
531 NULL,
532 NULL,
533 NULL
534 },
535
536
537 {
538 MESA_FORMAT_LUMINANCE_ALPHA_UINT8,
539 NULL,
540 NULL,
541 NULL
542 },
543
544 {
545 MESA_FORMAT_LUMINANCE_ALPHA_UINT16,
546 NULL,
547 NULL,
548 NULL
549 },
550
551 {
552 MESA_FORMAT_LUMINANCE_ALPHA_UINT32,
553 NULL,
554 NULL,
555 NULL
556 },
557
558 {
559 MESA_FORMAT_LUMINANCE_ALPHA_INT8,
560 NULL,
561 NULL,
562 NULL
563 },
564
565 {
566 MESA_FORMAT_LUMINANCE_ALPHA_INT16,
567 NULL,
568 NULL,
569 NULL
570 },
571
572 {
573 MESA_FORMAT_LUMINANCE_ALPHA_INT32,
574 NULL,
575 NULL,
576 NULL
577 },
578
579 {
580 MESA_FORMAT_RGB_INT8,
581 NULL,
582 NULL,
583 NULL
584 },
585
586 /* non-normalized, signed int */
587 {
588 MESA_FORMAT_RGBA_INT8,
589 fetch_texel_1d_rgba_int8,
590 fetch_texel_2d_rgba_int8,
591 fetch_texel_3d_rgba_int8
592 },
593 {
594 MESA_FORMAT_RGB_INT16,
595 NULL,
596 NULL,
597 NULL
598 },
599 {
600 MESA_FORMAT_RGBA_INT16,
601 fetch_texel_1d_rgba_int16,
602 fetch_texel_2d_rgba_int16,
603 fetch_texel_3d_rgba_int16
604 },
605 {
606 MESA_FORMAT_RGB_INT32,
607 NULL,
608 NULL,
609 NULL
610 },
611 {
612 MESA_FORMAT_RGBA_INT32,
613 fetch_texel_1d_rgba_int32,
614 fetch_texel_2d_rgba_int32,
615 fetch_texel_3d_rgba_int32
616 },
617
618 /* non-normalized, unsigned int */
619 {
620 MESA_FORMAT_RGB_UINT8,
621 NULL,
622 NULL,
623 NULL
624 },
625 {
626 MESA_FORMAT_RGBA_UINT8,
627 fetch_texel_1d_rgba_uint8,
628 fetch_texel_2d_rgba_uint8,
629 fetch_texel_3d_rgba_uint8
630 },
631 {
632 MESA_FORMAT_RGB_UINT16,
633 NULL,
634 NULL,
635 NULL
636 },
637 {
638 MESA_FORMAT_RGBA_UINT16,
639 fetch_texel_1d_rgba_uint16,
640 fetch_texel_2d_rgba_uint16,
641 fetch_texel_3d_rgba_uint16
642 },
643 {
644 MESA_FORMAT_RGB_UINT32,
645 NULL,
646 NULL,
647 NULL
648 },
649 {
650 MESA_FORMAT_RGBA_UINT32,
651 fetch_texel_1d_rgba_uint32,
652 fetch_texel_2d_rgba_uint32,
653 fetch_texel_3d_rgba_uint32
654 },
655
656 /* signed, normalized */
657 {
658 MESA_FORMAT_SIGNED_RGBA_16,
659 fetch_texel_1d_signed_rgba_16,
660 fetch_texel_2d_signed_rgba_16,
661 fetch_texel_3d_signed_rgba_16
662 },
663 {
664 MESA_FORMAT_RGBA_16,
665 fetch_texel_1d_rgba_16,
666 fetch_texel_2d_rgba_16,
667 fetch_texel_3d_rgba_16
668 }
669 };
670
671
672 FetchTexelFunc
673 _mesa_get_texel_fetch_func(gl_format format, GLuint dims)
674 {
675 #ifdef DEBUG
676 /* check that the table entries are sorted by format name */
677 gl_format fmt;
678 for (fmt = 0; fmt < MESA_FORMAT_COUNT; fmt++) {
679 assert(texfetch_funcs[fmt].Name == fmt);
680 }
681 #endif
682
683 STATIC_ASSERT(Elements(texfetch_funcs) == MESA_FORMAT_COUNT);
684
685 assert(format < MESA_FORMAT_COUNT);
686
687 switch (dims) {
688 case 1:
689 return texfetch_funcs[format].Fetch1D;
690 case 2:
691 return texfetch_funcs[format].Fetch2D;
692 case 3:
693 return texfetch_funcs[format].Fetch3D;
694 default:
695 assert(0 && "bad dims in _mesa_get_texel_fetch_func");
696 return NULL;
697 }
698 }
699
700
701 /**
702 * Initialize the texture image's FetchTexel methods.
703 */
704 static void
705 set_fetch_functions(struct swrast_texture_image *texImage, GLuint dims)
706 {
707 gl_format format = texImage->Base.TexFormat;
708
709 ASSERT(dims == 1 || dims == 2 || dims == 3);
710
711 texImage->FetchTexel = _mesa_get_texel_fetch_func(format, dims);
712 ASSERT(texImage->FetchTexel);
713 }
714
715 void
716 _mesa_update_fetch_functions(struct gl_texture_object *texObj)
717 {
718 GLuint face, i;
719 GLuint dims;
720
721 dims = _mesa_get_texture_dimensions(texObj->Target);
722
723 for (face = 0; face < 6; face++) {
724 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
725 if (texObj->Image[face][i]) {
726 set_fetch_functions(swrast_texture_image(texObj->Image[face][i]),
727 dims);
728 }
729 }
730 }
731 }