20ee2527f5fde5644a7ef35428a6e7e7ee6a8763
[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 "context.h"
38 #include "texcompress.h"
39 #include "texcompress_fxt1.h"
40 #include "texcompress_s3tc.h"
41 #include "texfetch.h"
42
43
44 #if FEATURE_EXT_texture_sRGB
45
46 /**
47 * Convert an 8-bit sRGB value from non-linear space to a
48 * linear RGB value in [0, 1].
49 * Implemented with a 256-entry lookup table.
50 */
51 static INLINE GLfloat
52 nonlinear_to_linear(GLubyte cs8)
53 {
54 static GLfloat table[256];
55 static GLboolean tableReady = GL_FALSE;
56 if (!tableReady) {
57 /* compute lookup table now */
58 GLuint i;
59 for (i = 0; i < 256; i++) {
60 const GLfloat cs = UBYTE_TO_FLOAT(i);
61 if (cs <= 0.04045) {
62 table[i] = cs / 12.92f;
63 }
64 else {
65 table[i] = (GLfloat) _mesa_pow((cs + 0.055) / 1.055, 2.4);
66 }
67 }
68 tableReady = GL_TRUE;
69 }
70 return table[cs8];
71 }
72
73
74 #endif /* FEATURE_EXT_texture_sRGB */
75
76
77 /* Texel fetch routines for all supported formats
78 */
79 #define DIM 1
80 #include "texformat_tmp.h"
81
82 #define DIM 2
83 #include "texformat_tmp.h"
84
85 #define DIM 3
86 #include "texformat_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 GLuint 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_RGBA,
132 fetch_texel_1d_f_rgba,
133 fetch_texel_2d_f_rgba,
134 fetch_texel_3d_f_rgba,
135 store_texel_rgba
136 },
137 {
138 MESA_FORMAT_RGB,
139 fetch_texel_1d_f_rgb,
140 fetch_texel_2d_f_rgb,
141 fetch_texel_3d_f_rgb,
142 store_texel_rgb
143 },
144 {
145 MESA_FORMAT_ALPHA,
146 fetch_texel_1d_f_alpha,
147 fetch_texel_2d_f_alpha,
148 fetch_texel_3d_f_alpha,
149 store_texel_alpha
150 },
151 {
152 MESA_FORMAT_LUMINANCE,
153 fetch_texel_1d_f_luminance,
154 fetch_texel_2d_f_luminance,
155 fetch_texel_3d_f_luminance,
156 store_texel_luminance
157 },
158 {
159 MESA_FORMAT_LUMINANCE_ALPHA,
160 fetch_texel_1d_f_luminance_alpha,
161 fetch_texel_2d_f_luminance_alpha,
162 fetch_texel_3d_f_luminance_alpha,
163 store_texel_luminance_alpha
164 },
165 {
166 MESA_FORMAT_INTENSITY,
167 fetch_texel_1d_f_intensity,
168 fetch_texel_2d_f_intensity,
169 fetch_texel_3d_f_intensity,
170 store_texel_intensity
171 },
172 {
173 MESA_FORMAT_SRGB8,
174 fetch_texel_1d_srgb8,
175 fetch_texel_2d_srgb8,
176 fetch_texel_3d_srgb8,
177 store_texel_srgb8
178 },
179 {
180 MESA_FORMAT_SRGBA8,
181 fetch_texel_1d_srgba8,
182 fetch_texel_2d_srgba8,
183 fetch_texel_3d_srgba8,
184 store_texel_srgba8
185 },
186 {
187 MESA_FORMAT_SARGB8,
188 fetch_texel_1d_sargb8,
189 fetch_texel_2d_sargb8,
190 fetch_texel_3d_sargb8,
191 store_texel_sargb8
192 },
193 {
194 MESA_FORMAT_SL8,
195 fetch_texel_1d_sl8,
196 fetch_texel_2d_sl8,
197 fetch_texel_3d_sl8,
198 store_texel_sl8
199 },
200 {
201 MESA_FORMAT_SLA8,
202 fetch_texel_1d_sla8,
203 fetch_texel_2d_sla8,
204 fetch_texel_3d_sla8,
205 store_texel_sla8
206 },
207 {
208 MESA_FORMAT_RGB_FXT1,
209 NULL,
210 _mesa_fetch_texel_2d_f_rgb_fxt1,
211 NULL,
212 NULL
213 },
214 {
215 MESA_FORMAT_RGBA_FXT1,
216 NULL,
217 _mesa_fetch_texel_2d_f_rgba_fxt1,
218 NULL,
219 NULL
220 },
221 {
222 MESA_FORMAT_RGB_DXT1,
223 NULL,
224 _mesa_fetch_texel_2d_f_rgb_dxt1,
225 NULL,
226 NULL
227 },
228 {
229 MESA_FORMAT_RGBA_DXT1,
230 NULL,
231 _mesa_fetch_texel_2d_f_rgba_dxt1,
232 NULL,
233 NULL
234 },
235 {
236 MESA_FORMAT_RGBA_DXT3,
237 NULL,
238 _mesa_fetch_texel_2d_f_rgba_dxt3,
239 NULL,
240 NULL
241 },
242 {
243 MESA_FORMAT_RGBA_DXT5,
244 NULL,
245 _mesa_fetch_texel_2d_f_rgba_dxt5,
246 NULL,
247 NULL
248 },
249 {
250 MESA_FORMAT_SRGB_DXT1,
251 NULL,
252 _mesa_fetch_texel_2d_f_srgb_dxt1,
253 NULL,
254 NULL
255 },
256 {
257 MESA_FORMAT_SRGBA_DXT1,
258 NULL,
259 _mesa_fetch_texel_2d_f_srgba_dxt1,
260 NULL,
261 NULL
262 },
263 {
264 MESA_FORMAT_SRGBA_DXT3,
265 NULL,
266 _mesa_fetch_texel_2d_f_srgba_dxt3,
267 NULL,
268 NULL
269 },
270 {
271 MESA_FORMAT_SRGBA_DXT5,
272 NULL,
273 _mesa_fetch_texel_2d_f_srgba_dxt5,
274 NULL,
275 NULL
276 },
277 {
278 MESA_FORMAT_RGBA_FLOAT32,
279 fetch_texel_1d_f_rgba_f32,
280 fetch_texel_2d_f_rgba_f32,
281 fetch_texel_3d_f_rgba_f32,
282 store_texel_rgba_f32
283 },
284 {
285 MESA_FORMAT_RGBA_FLOAT16,
286 fetch_texel_1d_f_rgba_f16,
287 fetch_texel_2d_f_rgba_f16,
288 fetch_texel_3d_f_rgba_f16,
289 store_texel_rgba_f16
290 },
291 {
292 MESA_FORMAT_RGB_FLOAT32,
293 fetch_texel_1d_f_rgb_f32,
294 fetch_texel_2d_f_rgb_f32,
295 fetch_texel_3d_f_rgb_f32,
296 store_texel_rgb_f32
297 },
298 {
299 MESA_FORMAT_RGB_FLOAT16,
300 fetch_texel_1d_f_rgb_f16,
301 fetch_texel_2d_f_rgb_f16,
302 fetch_texel_3d_f_rgb_f16,
303 store_texel_rgb_f16
304 },
305 {
306 MESA_FORMAT_ALPHA_FLOAT32,
307 fetch_texel_1d_f_alpha_f32,
308 fetch_texel_2d_f_alpha_f32,
309 fetch_texel_3d_f_alpha_f32,
310 store_texel_alpha_f32
311 },
312 {
313 MESA_FORMAT_ALPHA_FLOAT16,
314 fetch_texel_1d_f_alpha_f16,
315 fetch_texel_2d_f_alpha_f16,
316 fetch_texel_3d_f_alpha_f16,
317 store_texel_alpha_f16
318 },
319 {
320 MESA_FORMAT_LUMINANCE_FLOAT32,
321 fetch_texel_1d_f_luminance_f32,
322 fetch_texel_2d_f_luminance_f32,
323 fetch_texel_3d_f_luminance_f32,
324 store_texel_luminance_f32
325 },
326 {
327 MESA_FORMAT_LUMINANCE_FLOAT16,
328 fetch_texel_1d_f_luminance_f16,
329 fetch_texel_2d_f_luminance_f16,
330 fetch_texel_3d_f_luminance_f16,
331 store_texel_luminance_f16
332 },
333 {
334 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32,
335 fetch_texel_1d_f_luminance_alpha_f32,
336 fetch_texel_2d_f_luminance_alpha_f32,
337 fetch_texel_3d_f_luminance_alpha_f32,
338 store_texel_luminance_alpha_f32
339 },
340 {
341 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16,
342 fetch_texel_1d_f_luminance_alpha_f16,
343 fetch_texel_2d_f_luminance_alpha_f16,
344 fetch_texel_3d_f_luminance_alpha_f16,
345 store_texel_luminance_alpha_f16
346 },
347 {
348 MESA_FORMAT_INTENSITY_FLOAT32,
349 fetch_texel_1d_f_intensity_f32,
350 fetch_texel_2d_f_intensity_f32,
351 fetch_texel_3d_f_intensity_f32,
352 store_texel_intensity_f32
353 },
354 {
355 MESA_FORMAT_INTENSITY_FLOAT16,
356 fetch_texel_1d_f_intensity_f16,
357 fetch_texel_2d_f_intensity_f16,
358 fetch_texel_3d_f_intensity_f16,
359 store_texel_intensity_f16
360 },
361 {
362 MESA_FORMAT_DUDV8,
363 fetch_texel_1d_dudv8,
364 fetch_texel_2d_dudv8,
365 fetch_texel_3d_dudv8,
366 NULL
367 },
368 {
369 MESA_FORMAT_SIGNED_RGBA8888,
370 fetch_texel_1d_signed_rgba8888,
371 fetch_texel_2d_signed_rgba8888,
372 fetch_texel_3d_signed_rgba8888,
373 store_texel_signed_rgba8888
374 },
375 {
376 MESA_FORMAT_SIGNED_RGBA8888_REV,
377 fetch_texel_1d_signed_rgba8888_rev,
378 fetch_texel_2d_signed_rgba8888_rev,
379 fetch_texel_3d_signed_rgba8888_rev,
380 store_texel_signed_rgba8888_rev
381 },
382 {
383 MESA_FORMAT_RGBA8888,
384 fetch_texel_1d_f_rgba8888,
385 fetch_texel_2d_f_rgba8888,
386 fetch_texel_3d_f_rgba8888,
387 store_texel_rgba8888
388 },
389 {
390 MESA_FORMAT_RGBA8888_REV,
391 fetch_texel_1d_f_rgba8888_rev,
392 fetch_texel_2d_f_rgba8888_rev,
393 fetch_texel_3d_f_rgba8888_rev,
394 store_texel_rgba8888_rev
395 },
396 {
397 MESA_FORMAT_ARGB8888,
398 fetch_texel_1d_f_argb8888,
399 fetch_texel_2d_f_argb8888,
400 fetch_texel_3d_f_argb8888,
401 store_texel_argb8888
402 },
403 {
404 MESA_FORMAT_ARGB8888_REV,
405 fetch_texel_1d_f_argb8888_rev,
406 fetch_texel_2d_f_argb8888_rev,
407 fetch_texel_3d_f_argb8888_rev,
408 store_texel_argb8888_rev
409 },
410 {
411 MESA_FORMAT_RGB888,
412 fetch_texel_1d_f_rgb888,
413 fetch_texel_2d_f_rgb888,
414 fetch_texel_3d_f_rgb888,
415 store_texel_rgb888
416 },
417 {
418 MESA_FORMAT_BGR888,
419 fetch_texel_1d_f_bgr888,
420 fetch_texel_2d_f_bgr888,
421 fetch_texel_3d_f_bgr888,
422 store_texel_bgr888
423 },
424 {
425 MESA_FORMAT_RGB565,
426 fetch_texel_1d_f_rgb565,
427 fetch_texel_2d_f_rgb565,
428 fetch_texel_3d_f_rgb565,
429 store_texel_rgb565
430 },
431 {
432 MESA_FORMAT_RGB565_REV,
433 fetch_texel_1d_f_rgb565_rev,
434 fetch_texel_2d_f_rgb565_rev,
435 fetch_texel_3d_f_rgb565_rev,
436 store_texel_rgb565_rev
437 },
438 {
439 MESA_FORMAT_RGBA4444,
440 fetch_texel_1d_f_rgba4444,
441 fetch_texel_2d_f_rgba4444,
442 fetch_texel_3d_f_rgba4444,
443 store_texel_rgba4444
444 },
445 {
446 MESA_FORMAT_ARGB4444,
447 fetch_texel_1d_f_argb4444,
448 fetch_texel_2d_f_argb4444,
449 fetch_texel_3d_f_argb4444,
450 store_texel_argb4444
451 },
452 {
453 MESA_FORMAT_ARGB4444_REV,
454 fetch_texel_1d_f_argb4444_rev,
455 fetch_texel_2d_f_argb4444_rev,
456 fetch_texel_3d_f_argb4444_rev,
457 store_texel_argb4444_rev
458 },
459 {
460 MESA_FORMAT_RGBA5551,
461 fetch_texel_1d_f_rgba5551,
462 fetch_texel_2d_f_rgba5551,
463 fetch_texel_3d_f_rgba5551,
464 store_texel_rgba5551
465 },
466 {
467 MESA_FORMAT_ARGB1555,
468 fetch_texel_1d_f_argb1555,
469 fetch_texel_2d_f_argb1555,
470 fetch_texel_3d_f_argb1555,
471 store_texel_argb1555
472 },
473 {
474 MESA_FORMAT_ARGB1555_REV,
475 fetch_texel_1d_f_argb1555_rev,
476 fetch_texel_2d_f_argb1555_rev,
477 fetch_texel_3d_f_argb1555_rev,
478 store_texel_argb1555_rev
479 },
480 {
481 MESA_FORMAT_AL88,
482 fetch_texel_1d_f_al88,
483 fetch_texel_2d_f_al88,
484 fetch_texel_3d_f_al88,
485 store_texel_al88
486 },
487 {
488 MESA_FORMAT_AL88_REV,
489 fetch_texel_1d_f_al88_rev,
490 fetch_texel_2d_f_al88_rev,
491 fetch_texel_3d_f_al88_rev,
492 store_texel_al88_rev
493 },
494 {
495 MESA_FORMAT_RGB332,
496 fetch_texel_1d_f_rgb332,
497 fetch_texel_2d_f_rgb332,
498 fetch_texel_3d_f_rgb332,
499 store_texel_rgb332
500 },
501 {
502 MESA_FORMAT_A8,
503 fetch_texel_1d_f_a8,
504 fetch_texel_2d_f_a8,
505 fetch_texel_3d_f_a8,
506 store_texel_a8
507 },
508 {
509 MESA_FORMAT_L8,
510 fetch_texel_1d_f_l8,
511 fetch_texel_2d_f_l8,
512 fetch_texel_3d_f_l8,
513 store_texel_l8
514 },
515 {
516 MESA_FORMAT_I8,
517 fetch_texel_1d_f_i8,
518 fetch_texel_2d_f_i8,
519 fetch_texel_3d_f_i8,
520 store_texel_i8
521 },
522 {
523 MESA_FORMAT_CI8,
524 fetch_texel_1d_f_ci8,
525 fetch_texel_2d_f_ci8,
526 fetch_texel_3d_f_ci8,
527 store_texel_ci8
528 },
529 {
530 MESA_FORMAT_YCBCR,
531 fetch_texel_1d_f_ycbcr,
532 fetch_texel_2d_f_ycbcr,
533 fetch_texel_3d_f_ycbcr,
534 store_texel_ycbcr
535 },
536 {
537 MESA_FORMAT_YCBCR_REV,
538 fetch_texel_1d_f_ycbcr_rev,
539 fetch_texel_2d_f_ycbcr_rev,
540 fetch_texel_3d_f_ycbcr_rev,
541 store_texel_ycbcr_rev
542 },
543 {
544 MESA_FORMAT_Z24_S8,
545 fetch_texel_1d_f_z24_s8,
546 fetch_texel_2d_f_z24_s8,
547 fetch_texel_3d_f_z24_s8,
548 store_texel_z24_s8
549 },
550 {
551 MESA_FORMAT_S8_Z24,
552 fetch_texel_1d_f_s8_z24,
553 fetch_texel_2d_f_s8_z24,
554 fetch_texel_3d_f_s8_z24,
555 store_texel_s8_z24
556 },
557 {
558 MESA_FORMAT_Z16,
559 fetch_texel_1d_f_z16,
560 fetch_texel_2d_f_z16,
561 fetch_texel_3d_f_z16,
562 store_texel_z16
563 },
564 {
565 MESA_FORMAT_Z32,
566 fetch_texel_1d_f_z32,
567 fetch_texel_2d_f_z32,
568 fetch_texel_3d_f_z32,
569 store_texel_z32
570 }
571 };
572
573
574 FetchTexelFuncF
575 _mesa_get_texel_fetch_func(gl_format format, GLuint dims)
576 {
577 FetchTexelFuncF f;
578 GLuint i;
579 /* XXX replace loop with direct table lookup */
580 for (i = 0; i < MESA_FORMAT_COUNT; i++) {
581 if (texfetch_funcs[i].Name == format) {
582 switch (dims) {
583 case 1:
584 f = texfetch_funcs[i].Fetch1D;
585 break;
586 case 2:
587 f = texfetch_funcs[i].Fetch2D;
588 break;
589 case 3:
590 f = texfetch_funcs[i].Fetch3D;
591 break;
592 }
593 if (!f)
594 f = fetch_null_texelf;
595 return f;
596 }
597 }
598 return NULL;
599 }
600
601
602 StoreTexelFunc
603 _mesa_get_texel_store_func(gl_format format)
604 {
605 GLuint i;
606 /* XXX replace loop with direct table lookup */
607 for (i = 0; i < MESA_FORMAT_COUNT; i++) {
608 if (texfetch_funcs[i].Name == format) {
609 if (texfetch_funcs[i].StoreTexel)
610 return texfetch_funcs[i].StoreTexel;
611 else
612 return store_null_texel;
613 }
614 }
615 return NULL;
616 }