swrast: do texture sampling/combining in floating point
[mesa.git] / src / mesa / swrast / s_texfilter.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.3
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/colormac.h"
29 #include "main/imports.h"
30 #include "main/texformat.h"
31
32 #include "s_context.h"
33 #include "s_texfilter.h"
34
35
36 /*
37 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
38 * see 1-pixel bands of improperly weighted linear-filtered textures.
39 * The tests/texwrap.c demo is a good test.
40 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
41 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
42 */
43 #define FRAC(f) ((f) - IFLOOR(f))
44
45
46 /**
47 * Constants for integer linear interpolation.
48 */
49 #define ILERP_SCALE 65536.0F
50 #define ILERP_SHIFT 16
51
52
53 /**
54 * Linear interpolation macros
55 */
56 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
57 #define ILERP(IT, A, B) ( (A) + (((IT) * ((B) - (A))) >> ILERP_SHIFT) )
58
59
60 /**
61 * Do 2D/biliner interpolation of float values.
62 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
63 * a and b are the horizontal and vertical interpolants.
64 * It's important that this function is inlined when compiled with
65 * optimization! If we find that's not true on some systems, convert
66 * to a macro.
67 */
68 static INLINE GLfloat
69 lerp_2d(GLfloat a, GLfloat b,
70 GLfloat v00, GLfloat v10, GLfloat v01, GLfloat v11)
71 {
72 const GLfloat temp0 = LERP(a, v00, v10);
73 const GLfloat temp1 = LERP(a, v01, v11);
74 return LERP(b, temp0, temp1);
75 }
76
77
78 /**
79 * Do 2D/biliner interpolation of integer values.
80 * \sa lerp_2d
81 */
82 static INLINE GLint
83 ilerp_2d(GLint ia, GLint ib,
84 GLint v00, GLint v10, GLint v01, GLint v11)
85 {
86 /* fixed point interpolants in [0, ILERP_SCALE] */
87 const GLint temp0 = ILERP(ia, v00, v10);
88 const GLint temp1 = ILERP(ia, v01, v11);
89 return ILERP(ib, temp0, temp1);
90 }
91
92
93 /**
94 * Do 3D/trilinear interpolation of float values.
95 * \sa lerp_2d
96 */
97 static INLINE GLfloat
98 lerp_3d(GLfloat a, GLfloat b, GLfloat c,
99 GLfloat v000, GLfloat v100, GLfloat v010, GLfloat v110,
100 GLfloat v001, GLfloat v101, GLfloat v011, GLfloat v111)
101 {
102 const GLfloat temp00 = LERP(a, v000, v100);
103 const GLfloat temp10 = LERP(a, v010, v110);
104 const GLfloat temp01 = LERP(a, v001, v101);
105 const GLfloat temp11 = LERP(a, v011, v111);
106 const GLfloat temp0 = LERP(b, temp00, temp10);
107 const GLfloat temp1 = LERP(b, temp01, temp11);
108 return LERP(c, temp0, temp1);
109 }
110
111
112 /**
113 * Do 3D/trilinear interpolation of integer values.
114 * \sa lerp_2d
115 */
116 static INLINE GLint
117 ilerp_3d(GLint ia, GLint ib, GLint ic,
118 GLint v000, GLint v100, GLint v010, GLint v110,
119 GLint v001, GLint v101, GLint v011, GLint v111)
120 {
121 /* fixed point interpolants in [0, ILERP_SCALE] */
122 const GLint temp00 = ILERP(ia, v000, v100);
123 const GLint temp10 = ILERP(ia, v010, v110);
124 const GLint temp01 = ILERP(ia, v001, v101);
125 const GLint temp11 = ILERP(ia, v011, v111);
126 const GLint temp0 = ILERP(ib, temp00, temp10);
127 const GLint temp1 = ILERP(ib, temp01, temp11);
128 return ILERP(ic, temp0, temp1);
129 }
130
131
132 /**
133 * Do linear interpolation of colors.
134 */
135 static INLINE void
136 lerp_rgba(GLfloat result[4], GLfloat t, const GLfloat a[4], const GLfloat b[4])
137 {
138 result[0] = LERP(t, a[0], b[0]);
139 result[1] = LERP(t, a[1], b[1]);
140 result[2] = LERP(t, a[2], b[2]);
141 result[3] = LERP(t, a[3], b[3]);
142 }
143
144
145 /**
146 * Do bilinear interpolation of colors.
147 */
148 static INLINE void
149 lerp_rgba_2d(GLfloat result[4], GLfloat a, GLfloat b,
150 const GLfloat t00[4], const GLfloat t10[4],
151 const GLfloat t01[4], const GLfloat t11[4])
152 {
153 result[0] = lerp_2d(a, b, t00[0], t10[0], t01[0], t11[0]);
154 result[1] = lerp_2d(a, b, t00[1], t10[1], t01[1], t11[1]);
155 result[2] = lerp_2d(a, b, t00[2], t10[2], t01[2], t11[2]);
156 result[3] = lerp_2d(a, b, t00[3], t10[3], t01[3], t11[3]);
157 }
158
159
160 /**
161 * Do trilinear interpolation of colors.
162 */
163 static INLINE void
164 lerp_rgba_3d(GLfloat result[4], GLfloat a, GLfloat b, GLfloat c,
165 const GLfloat t000[4], const GLfloat t100[4],
166 const GLfloat t010[4], const GLfloat t110[4],
167 const GLfloat t001[4], const GLfloat t101[4],
168 const GLfloat t011[4], const GLfloat t111[4])
169 {
170 GLuint k;
171 /* compiler should unroll these short loops */
172 for (k = 0; k < 4; k++) {
173 result[k] = lerp_3d(a, b, c, t000[k], t100[k], t010[k], t110[k],
174 t001[k], t101[k], t011[k], t111[k]);
175 }
176 }
177
178
179 /**
180 * If A is a signed integer, A % B doesn't give the right value for A < 0
181 * (in terms of texture repeat). Just casting to unsigned fixes that.
182 */
183 #define REMAINDER(A, B) ((unsigned) (A) % (unsigned) (B))
184
185
186 /**
187 * Used to compute texel locations for linear sampling.
188 * Input:
189 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
190 * s = texcoord in [0,1]
191 * size = width (or height or depth) of texture
192 * Output:
193 * i0, i1 = returns two nearest texel indexes
194 * weight = returns blend factor between texels
195 */
196 static INLINE void
197 linear_texel_locations(GLenum wrapMode,
198 const struct gl_texture_image *img,
199 GLint size, GLfloat s,
200 GLint *i0, GLint *i1, GLfloat *weight)
201 {
202 GLfloat u;
203 switch (wrapMode) {
204 case GL_REPEAT:
205 u = s * size - 0.5F;
206 if (img->_IsPowerOfTwo) {
207 *i0 = IFLOOR(u) & (size - 1);
208 *i1 = (*i0 + 1) & (size - 1);
209 }
210 else {
211 *i0 = REMAINDER(IFLOOR(u), size);
212 *i1 = REMAINDER(*i0 + 1, size);
213 }
214 break;
215 case GL_CLAMP_TO_EDGE:
216 if (s <= 0.0F)
217 u = 0.0F;
218 else if (s >= 1.0F)
219 u = (GLfloat) size;
220 else
221 u = s * size;
222 u -= 0.5F;
223 *i0 = IFLOOR(u);
224 *i1 = *i0 + 1;
225 if (*i0 < 0)
226 *i0 = 0;
227 if (*i1 >= (GLint) size)
228 *i1 = size - 1;
229 break;
230 case GL_CLAMP_TO_BORDER:
231 {
232 const GLfloat min = -1.0F / (2.0F * size);
233 const GLfloat max = 1.0F - min;
234 if (s <= min)
235 u = min * size;
236 else if (s >= max)
237 u = max * size;
238 else
239 u = s * size;
240 u -= 0.5F;
241 *i0 = IFLOOR(u);
242 *i1 = *i0 + 1;
243 }
244 break;
245 case GL_MIRRORED_REPEAT:
246 {
247 const GLint flr = IFLOOR(s);
248 if (flr & 1)
249 u = 1.0F - (s - (GLfloat) flr);
250 else
251 u = s - (GLfloat) flr;
252 u = (u * size) - 0.5F;
253 *i0 = IFLOOR(u);
254 *i1 = *i0 + 1;
255 if (*i0 < 0)
256 *i0 = 0;
257 if (*i1 >= (GLint) size)
258 *i1 = size - 1;
259 }
260 break;
261 case GL_MIRROR_CLAMP_EXT:
262 u = FABSF(s);
263 if (u >= 1.0F)
264 u = (GLfloat) size;
265 else
266 u *= size;
267 u -= 0.5F;
268 *i0 = IFLOOR(u);
269 *i1 = *i0 + 1;
270 break;
271 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
272 u = FABSF(s);
273 if (u >= 1.0F)
274 u = (GLfloat) size;
275 else
276 u *= size;
277 u -= 0.5F;
278 *i0 = IFLOOR(u);
279 *i1 = *i0 + 1;
280 if (*i0 < 0)
281 *i0 = 0;
282 if (*i1 >= (GLint) size)
283 *i1 = size - 1;
284 break;
285 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
286 {
287 const GLfloat min = -1.0F / (2.0F * size);
288 const GLfloat max = 1.0F - min;
289 u = FABSF(s);
290 if (u <= min)
291 u = min * size;
292 else if (u >= max)
293 u = max * size;
294 else
295 u *= size;
296 u -= 0.5F;
297 *i0 = IFLOOR(u);
298 *i1 = *i0 + 1;
299 }
300 break;
301 case GL_CLAMP:
302 if (s <= 0.0F)
303 u = 0.0F;
304 else if (s >= 1.0F)
305 u = (GLfloat) size;
306 else
307 u = s * size;
308 u -= 0.5F;
309 *i0 = IFLOOR(u);
310 *i1 = *i0 + 1;
311 break;
312 default:
313 _mesa_problem(NULL, "Bad wrap mode");
314 u = 0.0F;
315 }
316 *weight = FRAC(u);
317 }
318
319
320 /**
321 * Used to compute texel location for nearest sampling.
322 */
323 static INLINE GLint
324 nearest_texel_location(GLenum wrapMode,
325 const struct gl_texture_image *img,
326 GLint size, GLfloat s)
327 {
328 GLint i;
329
330 switch (wrapMode) {
331 case GL_REPEAT:
332 /* s limited to [0,1) */
333 /* i limited to [0,size-1] */
334 i = IFLOOR(s * size);
335 if (img->_IsPowerOfTwo)
336 i &= (size - 1);
337 else
338 i = REMAINDER(i, size);
339 return i;
340 case GL_CLAMP_TO_EDGE:
341 {
342 /* s limited to [min,max] */
343 /* i limited to [0, size-1] */
344 const GLfloat min = 1.0F / (2.0F * size);
345 const GLfloat max = 1.0F - min;
346 if (s < min)
347 i = 0;
348 else if (s > max)
349 i = size - 1;
350 else
351 i = IFLOOR(s * size);
352 }
353 return i;
354 case GL_CLAMP_TO_BORDER:
355 {
356 /* s limited to [min,max] */
357 /* i limited to [-1, size] */
358 const GLfloat min = -1.0F / (2.0F * size);
359 const GLfloat max = 1.0F - min;
360 if (s <= min)
361 i = -1;
362 else if (s >= max)
363 i = size;
364 else
365 i = IFLOOR(s * size);
366 }
367 return i;
368 case GL_MIRRORED_REPEAT:
369 {
370 const GLfloat min = 1.0F / (2.0F * size);
371 const GLfloat max = 1.0F - min;
372 const GLint flr = IFLOOR(s);
373 GLfloat u;
374 if (flr & 1)
375 u = 1.0F - (s - (GLfloat) flr);
376 else
377 u = s - (GLfloat) flr;
378 if (u < min)
379 i = 0;
380 else if (u > max)
381 i = size - 1;
382 else
383 i = IFLOOR(u * size);
384 }
385 return i;
386 case GL_MIRROR_CLAMP_EXT:
387 {
388 /* s limited to [0,1] */
389 /* i limited to [0,size-1] */
390 const GLfloat u = FABSF(s);
391 if (u <= 0.0F)
392 i = 0;
393 else if (u >= 1.0F)
394 i = size - 1;
395 else
396 i = IFLOOR(u * size);
397 }
398 return i;
399 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
400 {
401 /* s limited to [min,max] */
402 /* i limited to [0, size-1] */
403 const GLfloat min = 1.0F / (2.0F * size);
404 const GLfloat max = 1.0F - min;
405 const GLfloat u = FABSF(s);
406 if (u < min)
407 i = 0;
408 else if (u > max)
409 i = size - 1;
410 else
411 i = IFLOOR(u * size);
412 }
413 return i;
414 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
415 {
416 /* s limited to [min,max] */
417 /* i limited to [0, size-1] */
418 const GLfloat min = -1.0F / (2.0F * size);
419 const GLfloat max = 1.0F - min;
420 const GLfloat u = FABSF(s);
421 if (u < min)
422 i = -1;
423 else if (u > max)
424 i = size;
425 else
426 i = IFLOOR(u * size);
427 }
428 return i;
429 case GL_CLAMP:
430 /* s limited to [0,1] */
431 /* i limited to [0,size-1] */
432 if (s <= 0.0F)
433 i = 0;
434 else if (s >= 1.0F)
435 i = size - 1;
436 else
437 i = IFLOOR(s * size);
438 return i;
439 default:
440 _mesa_problem(NULL, "Bad wrap mode");
441 return 0;
442 }
443 }
444
445
446 /* Power of two image sizes only */
447 static INLINE void
448 linear_repeat_texel_location(GLuint size, GLfloat s,
449 GLint *i0, GLint *i1, GLfloat *weight)
450 {
451 GLfloat u = s * size - 0.5F;
452 *i0 = IFLOOR(u) & (size - 1);
453 *i1 = (*i0 + 1) & (size - 1);
454 *weight = FRAC(u);
455 }
456
457
458 /**
459 * For linear interpolation between mipmap levels N and N+1, this function
460 * computes N.
461 */
462 static INLINE GLint
463 linear_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
464 {
465 if (lambda < 0.0F)
466 return tObj->BaseLevel;
467 else if (lambda > tObj->_MaxLambda)
468 return (GLint) (tObj->BaseLevel + tObj->_MaxLambda);
469 else
470 return (GLint) (tObj->BaseLevel + lambda);
471 }
472
473
474 /**
475 * Compute the nearest mipmap level to take texels from.
476 */
477 static INLINE GLint
478 nearest_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
479 {
480 GLfloat l;
481 GLint level;
482 if (lambda <= 0.5F)
483 l = 0.0F;
484 else if (lambda > tObj->_MaxLambda + 0.4999F)
485 l = tObj->_MaxLambda + 0.4999F;
486 else
487 l = lambda;
488 level = (GLint) (tObj->BaseLevel + l + 0.5F);
489 if (level > tObj->_MaxLevel)
490 level = tObj->_MaxLevel;
491 return level;
492 }
493
494
495
496 /*
497 * Bitflags for texture border color sampling.
498 */
499 #define I0BIT 1
500 #define I1BIT 2
501 #define J0BIT 4
502 #define J1BIT 8
503 #define K0BIT 16
504 #define K1BIT 32
505
506
507
508 /**
509 * The lambda[] array values are always monotonic. Either the whole span
510 * will be minified, magnified, or split between the two. This function
511 * determines the subranges in [0, n-1] that are to be minified or magnified.
512 */
513 static INLINE void
514 compute_min_mag_ranges(const struct gl_texture_object *tObj,
515 GLuint n, const GLfloat lambda[],
516 GLuint *minStart, GLuint *minEnd,
517 GLuint *magStart, GLuint *magEnd)
518 {
519 GLfloat minMagThresh;
520
521 /* we shouldn't be here if minfilter == magfilter */
522 ASSERT(tObj->MinFilter != tObj->MagFilter);
523
524 /* This bit comes from the OpenGL spec: */
525 if (tObj->MagFilter == GL_LINEAR
526 && (tObj->MinFilter == GL_NEAREST_MIPMAP_NEAREST ||
527 tObj->MinFilter == GL_NEAREST_MIPMAP_LINEAR)) {
528 minMagThresh = 0.5F;
529 }
530 else {
531 minMagThresh = 0.0F;
532 }
533
534 #if 0
535 /* DEBUG CODE: Verify that lambda[] is monotonic.
536 * We can't really use this because the inaccuracy in the LOG2 function
537 * causes this test to fail, yet the resulting texturing is correct.
538 */
539 if (n > 1) {
540 GLuint i;
541 printf("lambda delta = %g\n", lambda[0] - lambda[n-1]);
542 if (lambda[0] >= lambda[n-1]) { /* decreasing */
543 for (i = 0; i < n - 1; i++) {
544 ASSERT((GLint) (lambda[i] * 10) >= (GLint) (lambda[i+1] * 10));
545 }
546 }
547 else { /* increasing */
548 for (i = 0; i < n - 1; i++) {
549 ASSERT((GLint) (lambda[i] * 10) <= (GLint) (lambda[i+1] * 10));
550 }
551 }
552 }
553 #endif /* DEBUG */
554
555 if (lambda[0] <= minMagThresh && (n <= 1 || lambda[n-1] <= minMagThresh)) {
556 /* magnification for whole span */
557 *magStart = 0;
558 *magEnd = n;
559 *minStart = *minEnd = 0;
560 }
561 else if (lambda[0] > minMagThresh && (n <=1 || lambda[n-1] > minMagThresh)) {
562 /* minification for whole span */
563 *minStart = 0;
564 *minEnd = n;
565 *magStart = *magEnd = 0;
566 }
567 else {
568 /* a mix of minification and magnification */
569 GLuint i;
570 if (lambda[0] > minMagThresh) {
571 /* start with minification */
572 for (i = 1; i < n; i++) {
573 if (lambda[i] <= minMagThresh)
574 break;
575 }
576 *minStart = 0;
577 *minEnd = i;
578 *magStart = i;
579 *magEnd = n;
580 }
581 else {
582 /* start with magnification */
583 for (i = 1; i < n; i++) {
584 if (lambda[i] > minMagThresh)
585 break;
586 }
587 *magStart = 0;
588 *magEnd = i;
589 *minStart = i;
590 *minEnd = n;
591 }
592 }
593
594 #if 0
595 /* Verify the min/mag Start/End values
596 * We don't use this either (see above)
597 */
598 {
599 GLint i;
600 for (i = 0; i < n; i++) {
601 if (lambda[i] > minMagThresh) {
602 /* minification */
603 ASSERT(i >= *minStart);
604 ASSERT(i < *minEnd);
605 }
606 else {
607 /* magnification */
608 ASSERT(i >= *magStart);
609 ASSERT(i < *magEnd);
610 }
611 }
612 }
613 #endif
614 }
615
616
617 /**********************************************************************/
618 /* 1-D Texture Sampling Functions */
619 /**********************************************************************/
620
621 /**
622 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
623 */
624 static INLINE void
625 sample_1d_nearest(GLcontext *ctx,
626 const struct gl_texture_object *tObj,
627 const struct gl_texture_image *img,
628 const GLfloat texcoord[4], GLfloat rgba[4])
629 {
630 const GLint width = img->Width2; /* without border, power of two */
631 GLint i;
632 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
633 /* skip over the border, if any */
634 i += img->Border;
635 if (i < 0 || i >= (GLint) img->Width) {
636 /* Need this test for GL_CLAMP_TO_BORDER mode */
637 COPY_4V(rgba, tObj->BorderColor);
638 }
639 else {
640 img->FetchTexelf(img, i, 0, 0, rgba);
641 }
642 }
643
644
645 /**
646 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
647 */
648 static INLINE void
649 sample_1d_linear(GLcontext *ctx,
650 const struct gl_texture_object *tObj,
651 const struct gl_texture_image *img,
652 const GLfloat texcoord[4], GLfloat rgba[4])
653 {
654 const GLint width = img->Width2;
655 GLint i0, i1;
656 GLbitfield useBorderColor = 0x0;
657 GLfloat a;
658 GLfloat t0[4], t1[4]; /* texels */
659
660 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
661
662 if (img->Border) {
663 i0 += img->Border;
664 i1 += img->Border;
665 }
666 else {
667 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
668 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
669 }
670
671 /* fetch texel colors */
672 if (useBorderColor & I0BIT) {
673 COPY_4V(t0, tObj->BorderColor);
674 }
675 else {
676 img->FetchTexelf(img, i0, 0, 0, t0);
677 }
678 if (useBorderColor & I1BIT) {
679 COPY_4V(t1, tObj->BorderColor);
680 }
681 else {
682 img->FetchTexelf(img, i1, 0, 0, t1);
683 }
684
685 lerp_rgba(rgba, a, t0, t1);
686 }
687
688
689 static void
690 sample_1d_nearest_mipmap_nearest(GLcontext *ctx,
691 const struct gl_texture_object *tObj,
692 GLuint n, const GLfloat texcoord[][4],
693 const GLfloat lambda[], GLfloat rgba[][4])
694 {
695 GLuint i;
696 ASSERT(lambda != NULL);
697 for (i = 0; i < n; i++) {
698 GLint level = nearest_mipmap_level(tObj, lambda[i]);
699 sample_1d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
700 }
701 }
702
703
704 static void
705 sample_1d_linear_mipmap_nearest(GLcontext *ctx,
706 const struct gl_texture_object *tObj,
707 GLuint n, const GLfloat texcoord[][4],
708 const GLfloat lambda[], GLfloat rgba[][4])
709 {
710 GLuint i;
711 ASSERT(lambda != NULL);
712 for (i = 0; i < n; i++) {
713 GLint level = nearest_mipmap_level(tObj, lambda[i]);
714 sample_1d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
715 }
716 }
717
718
719 static void
720 sample_1d_nearest_mipmap_linear(GLcontext *ctx,
721 const struct gl_texture_object *tObj,
722 GLuint n, const GLfloat texcoord[][4],
723 const GLfloat lambda[], GLfloat rgba[][4])
724 {
725 GLuint i;
726 ASSERT(lambda != NULL);
727 for (i = 0; i < n; i++) {
728 GLint level = linear_mipmap_level(tObj, lambda[i]);
729 if (level >= tObj->_MaxLevel) {
730 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
731 texcoord[i], rgba[i]);
732 }
733 else {
734 GLfloat t0[4], t1[4];
735 const GLfloat f = FRAC(lambda[i]);
736 sample_1d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
737 sample_1d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
738 lerp_rgba(rgba[i], f, t0, t1);
739 }
740 }
741 }
742
743
744 static void
745 sample_1d_linear_mipmap_linear(GLcontext *ctx,
746 const struct gl_texture_object *tObj,
747 GLuint n, const GLfloat texcoord[][4],
748 const GLfloat lambda[], GLfloat rgba[][4])
749 {
750 GLuint i;
751 ASSERT(lambda != NULL);
752 for (i = 0; i < n; i++) {
753 GLint level = linear_mipmap_level(tObj, lambda[i]);
754 if (level >= tObj->_MaxLevel) {
755 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
756 texcoord[i], rgba[i]);
757 }
758 else {
759 GLfloat t0[4], t1[4];
760 const GLfloat f = FRAC(lambda[i]);
761 sample_1d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
762 sample_1d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
763 lerp_rgba(rgba[i], f, t0, t1);
764 }
765 }
766 }
767
768
769 /** Sample 1D texture, nearest filtering for both min/magnification */
770 static void
771 sample_nearest_1d( GLcontext *ctx,
772 const struct gl_texture_object *tObj, GLuint n,
773 const GLfloat texcoords[][4], const GLfloat lambda[],
774 GLfloat rgba[][4] )
775 {
776 GLuint i;
777 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
778 (void) lambda;
779 for (i = 0; i < n; i++) {
780 sample_1d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
781 }
782 }
783
784
785 /** Sample 1D texture, linear filtering for both min/magnification */
786 static void
787 sample_linear_1d( GLcontext *ctx,
788 const struct gl_texture_object *tObj, GLuint n,
789 const GLfloat texcoords[][4], const GLfloat lambda[],
790 GLfloat rgba[][4] )
791 {
792 GLuint i;
793 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
794 (void) lambda;
795 for (i = 0; i < n; i++) {
796 sample_1d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
797 }
798 }
799
800
801 /** Sample 1D texture, using lambda to choose between min/magnification */
802 static void
803 sample_lambda_1d( GLcontext *ctx,
804 const struct gl_texture_object *tObj, GLuint n,
805 const GLfloat texcoords[][4],
806 const GLfloat lambda[], GLfloat rgba[][4] )
807 {
808 GLuint minStart, minEnd; /* texels with minification */
809 GLuint magStart, magEnd; /* texels with magnification */
810 GLuint i;
811
812 ASSERT(lambda != NULL);
813 compute_min_mag_ranges(tObj, n, lambda,
814 &minStart, &minEnd, &magStart, &magEnd);
815
816 if (minStart < minEnd) {
817 /* do the minified texels */
818 const GLuint m = minEnd - minStart;
819 switch (tObj->MinFilter) {
820 case GL_NEAREST:
821 for (i = minStart; i < minEnd; i++)
822 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
823 texcoords[i], rgba[i]);
824 break;
825 case GL_LINEAR:
826 for (i = minStart; i < minEnd; i++)
827 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
828 texcoords[i], rgba[i]);
829 break;
830 case GL_NEAREST_MIPMAP_NEAREST:
831 sample_1d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
832 lambda + minStart, rgba + minStart);
833 break;
834 case GL_LINEAR_MIPMAP_NEAREST:
835 sample_1d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
836 lambda + minStart, rgba + minStart);
837 break;
838 case GL_NEAREST_MIPMAP_LINEAR:
839 sample_1d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
840 lambda + minStart, rgba + minStart);
841 break;
842 case GL_LINEAR_MIPMAP_LINEAR:
843 sample_1d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
844 lambda + minStart, rgba + minStart);
845 break;
846 default:
847 _mesa_problem(ctx, "Bad min filter in sample_1d_texture");
848 return;
849 }
850 }
851
852 if (magStart < magEnd) {
853 /* do the magnified texels */
854 switch (tObj->MagFilter) {
855 case GL_NEAREST:
856 for (i = magStart; i < magEnd; i++)
857 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
858 texcoords[i], rgba[i]);
859 break;
860 case GL_LINEAR:
861 for (i = magStart; i < magEnd; i++)
862 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
863 texcoords[i], rgba[i]);
864 break;
865 default:
866 _mesa_problem(ctx, "Bad mag filter in sample_1d_texture");
867 return;
868 }
869 }
870 }
871
872
873 /**********************************************************************/
874 /* 2-D Texture Sampling Functions */
875 /**********************************************************************/
876
877
878 /**
879 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
880 */
881 static INLINE void
882 sample_2d_nearest(GLcontext *ctx,
883 const struct gl_texture_object *tObj,
884 const struct gl_texture_image *img,
885 const GLfloat texcoord[4],
886 GLfloat rgba[])
887 {
888 const GLint width = img->Width2; /* without border, power of two */
889 const GLint height = img->Height2; /* without border, power of two */
890 GLint i, j;
891 (void) ctx;
892
893 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
894 j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
895
896 /* skip over the border, if any */
897 i += img->Border;
898 j += img->Border;
899
900 if (i < 0 || i >= (GLint) img->Width || j < 0 || j >= (GLint) img->Height) {
901 /* Need this test for GL_CLAMP_TO_BORDER mode */
902 COPY_4V(rgba, tObj->BorderColor);
903 }
904 else {
905 img->FetchTexelf(img, i, j, 0, rgba);
906 }
907 }
908
909
910 /**
911 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
912 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
913 */
914 static INLINE void
915 sample_2d_linear(GLcontext *ctx,
916 const struct gl_texture_object *tObj,
917 const struct gl_texture_image *img,
918 const GLfloat texcoord[4],
919 GLfloat rgba[])
920 {
921 const GLint width = img->Width2;
922 const GLint height = img->Height2;
923 GLint i0, j0, i1, j1;
924 GLbitfield useBorderColor = 0x0;
925 GLfloat a, b;
926 GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
927
928 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
929 linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
930
931 if (img->Border) {
932 i0 += img->Border;
933 i1 += img->Border;
934 j0 += img->Border;
935 j1 += img->Border;
936 }
937 else {
938 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
939 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
940 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
941 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
942 }
943
944 /* fetch four texel colors */
945 if (useBorderColor & (I0BIT | J0BIT)) {
946 COPY_4V(t00, tObj->BorderColor);
947 }
948 else {
949 img->FetchTexelf(img, i0, j0, 0, t00);
950 }
951 if (useBorderColor & (I1BIT | J0BIT)) {
952 COPY_4V(t10, tObj->BorderColor);
953 }
954 else {
955 img->FetchTexelf(img, i1, j0, 0, t10);
956 }
957 if (useBorderColor & (I0BIT | J1BIT)) {
958 COPY_4V(t01, tObj->BorderColor);
959 }
960 else {
961 img->FetchTexelf(img, i0, j1, 0, t01);
962 }
963 if (useBorderColor & (I1BIT | J1BIT)) {
964 COPY_4V(t11, tObj->BorderColor);
965 }
966 else {
967 img->FetchTexelf(img, i1, j1, 0, t11);
968 }
969
970 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
971 }
972
973
974 /**
975 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
976 * We don't have to worry about the texture border.
977 */
978 static INLINE void
979 sample_2d_linear_repeat(GLcontext *ctx,
980 const struct gl_texture_object *tObj,
981 const struct gl_texture_image *img,
982 const GLfloat texcoord[4],
983 GLfloat rgba[])
984 {
985 const GLint width = img->Width2;
986 const GLint height = img->Height2;
987 GLint i0, j0, i1, j1;
988 GLfloat wi, wj;
989 GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
990
991 (void) ctx;
992
993 ASSERT(tObj->WrapS == GL_REPEAT);
994 ASSERT(tObj->WrapT == GL_REPEAT);
995 ASSERT(img->Border == 0);
996 ASSERT(img->TexFormat->BaseFormat != GL_COLOR_INDEX);
997 ASSERT(img->_IsPowerOfTwo);
998
999 linear_repeat_texel_location(width, texcoord[0], &i0, &i1, &wi);
1000 linear_repeat_texel_location(height, texcoord[1], &j0, &j1, &wj);
1001
1002 img->FetchTexelf(img, i0, j0, 0, t00);
1003 img->FetchTexelf(img, i1, j0, 0, t10);
1004 img->FetchTexelf(img, i0, j1, 0, t01);
1005 img->FetchTexelf(img, i1, j1, 0, t11);
1006
1007 lerp_rgba_2d(rgba, wi, wj, t00, t10, t01, t11);
1008 }
1009
1010
1011 static void
1012 sample_2d_nearest_mipmap_nearest(GLcontext *ctx,
1013 const struct gl_texture_object *tObj,
1014 GLuint n, const GLfloat texcoord[][4],
1015 const GLfloat lambda[], GLfloat rgba[][4])
1016 {
1017 GLuint i;
1018 for (i = 0; i < n; i++) {
1019 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1020 sample_2d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1021 }
1022 }
1023
1024
1025 static void
1026 sample_2d_linear_mipmap_nearest(GLcontext *ctx,
1027 const struct gl_texture_object *tObj,
1028 GLuint n, const GLfloat texcoord[][4],
1029 const GLfloat lambda[], GLfloat rgba[][4])
1030 {
1031 GLuint i;
1032 ASSERT(lambda != NULL);
1033 for (i = 0; i < n; i++) {
1034 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1035 sample_2d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1036 }
1037 }
1038
1039
1040 static void
1041 sample_2d_nearest_mipmap_linear(GLcontext *ctx,
1042 const struct gl_texture_object *tObj,
1043 GLuint n, const GLfloat texcoord[][4],
1044 const GLfloat lambda[], GLfloat rgba[][4])
1045 {
1046 GLuint i;
1047 ASSERT(lambda != NULL);
1048 for (i = 0; i < n; i++) {
1049 GLint level = linear_mipmap_level(tObj, lambda[i]);
1050 if (level >= tObj->_MaxLevel) {
1051 sample_2d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1052 texcoord[i], rgba[i]);
1053 }
1054 else {
1055 GLfloat t0[4], t1[4]; /* texels */
1056 const GLfloat f = FRAC(lambda[i]);
1057 sample_2d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1058 sample_2d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1059 lerp_rgba(rgba[i], f, t0, t1);
1060 }
1061 }
1062 }
1063
1064
1065 static void
1066 sample_2d_linear_mipmap_linear( GLcontext *ctx,
1067 const struct gl_texture_object *tObj,
1068 GLuint n, const GLfloat texcoord[][4],
1069 const GLfloat lambda[], GLfloat rgba[][4] )
1070 {
1071 GLuint i;
1072 ASSERT(lambda != NULL);
1073 for (i = 0; i < n; i++) {
1074 GLint level = linear_mipmap_level(tObj, lambda[i]);
1075 if (level >= tObj->_MaxLevel) {
1076 sample_2d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1077 texcoord[i], rgba[i]);
1078 }
1079 else {
1080 GLfloat t0[4], t1[4]; /* texels */
1081 const GLfloat f = FRAC(lambda[i]);
1082 sample_2d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1083 sample_2d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1084 lerp_rgba(rgba[i], f, t0, t1);
1085 }
1086 }
1087 }
1088
1089
1090 static void
1091 sample_2d_linear_mipmap_linear_repeat(GLcontext *ctx,
1092 const struct gl_texture_object *tObj,
1093 GLuint n, const GLfloat texcoord[][4],
1094 const GLfloat lambda[], GLfloat rgba[][4])
1095 {
1096 GLuint i;
1097 ASSERT(lambda != NULL);
1098 ASSERT(tObj->WrapS == GL_REPEAT);
1099 ASSERT(tObj->WrapT == GL_REPEAT);
1100 for (i = 0; i < n; i++) {
1101 GLint level = linear_mipmap_level(tObj, lambda[i]);
1102 if (level >= tObj->_MaxLevel) {
1103 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1104 texcoord[i], rgba[i]);
1105 }
1106 else {
1107 GLfloat t0[4], t1[4]; /* texels */
1108 const GLfloat f = FRAC(lambda[i]);
1109 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level ],
1110 texcoord[i], t0);
1111 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level+1],
1112 texcoord[i], t1);
1113 lerp_rgba(rgba[i], f, t0, t1);
1114 }
1115 }
1116 }
1117
1118
1119 /** Sample 2D texture, nearest filtering for both min/magnification */
1120 static void
1121 sample_nearest_2d(GLcontext *ctx,
1122 const struct gl_texture_object *tObj, GLuint n,
1123 const GLfloat texcoords[][4],
1124 const GLfloat lambda[], GLfloat rgba[][4])
1125 {
1126 GLuint i;
1127 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1128 (void) lambda;
1129 for (i = 0; i < n; i++) {
1130 sample_2d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
1131 }
1132 }
1133
1134
1135 /** Sample 2D texture, linear filtering for both min/magnification */
1136 static void
1137 sample_linear_2d(GLcontext *ctx,
1138 const struct gl_texture_object *tObj, GLuint n,
1139 const GLfloat texcoords[][4],
1140 const GLfloat lambda[], GLfloat rgba[][4])
1141 {
1142 GLuint i;
1143 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1144 (void) lambda;
1145 if (tObj->WrapS == GL_REPEAT &&
1146 tObj->WrapT == GL_REPEAT &&
1147 image->_IsPowerOfTwo &&
1148 image->Border == 0) {
1149 for (i = 0; i < n; i++) {
1150 sample_2d_linear_repeat(ctx, tObj, image, texcoords[i], rgba[i]);
1151 }
1152 }
1153 else {
1154 for (i = 0; i < n; i++) {
1155 sample_2d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
1156 }
1157 }
1158 }
1159
1160
1161 /**
1162 * Optimized 2-D texture sampling:
1163 * S and T wrap mode == GL_REPEAT
1164 * GL_NEAREST min/mag filter
1165 * No border,
1166 * RowStride == Width,
1167 * Format = GL_RGB
1168 */
1169 static void
1170 opt_sample_rgb_2d(GLcontext *ctx,
1171 const struct gl_texture_object *tObj,
1172 GLuint n, const GLfloat texcoords[][4],
1173 const GLfloat lambda[], GLchan rgba[][4])
1174 {
1175 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1176 const GLfloat width = (GLfloat) img->Width;
1177 const GLfloat height = (GLfloat) img->Height;
1178 const GLint colMask = img->Width - 1;
1179 const GLint rowMask = img->Height - 1;
1180 const GLint shift = img->WidthLog2;
1181 GLuint k;
1182 (void) ctx;
1183 (void) lambda;
1184 ASSERT(tObj->WrapS==GL_REPEAT);
1185 ASSERT(tObj->WrapT==GL_REPEAT);
1186 ASSERT(img->Border==0);
1187 ASSERT(img->TexFormat->MesaFormat==MESA_FORMAT_RGB);
1188 ASSERT(img->_IsPowerOfTwo);
1189
1190 for (k=0; k<n; k++) {
1191 GLint i = IFLOOR(texcoords[k][0] * width) & colMask;
1192 GLint j = IFLOOR(texcoords[k][1] * height) & rowMask;
1193 GLint pos = (j << shift) | i;
1194 GLchan *texel = ((GLchan *) img->Data) + 3*pos;
1195 rgba[k][RCOMP] = texel[0];
1196 rgba[k][GCOMP] = texel[1];
1197 rgba[k][BCOMP] = texel[2];
1198 }
1199 }
1200
1201
1202 /**
1203 * Optimized 2-D texture sampling:
1204 * S and T wrap mode == GL_REPEAT
1205 * GL_NEAREST min/mag filter
1206 * No border
1207 * RowStride == Width,
1208 * Format = GL_RGBA
1209 */
1210 static void
1211 opt_sample_rgba_2d(GLcontext *ctx,
1212 const struct gl_texture_object *tObj,
1213 GLuint n, const GLfloat texcoords[][4],
1214 const GLfloat lambda[], GLchan rgba[][4])
1215 {
1216 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1217 const GLfloat width = (GLfloat) img->Width;
1218 const GLfloat height = (GLfloat) img->Height;
1219 const GLint colMask = img->Width - 1;
1220 const GLint rowMask = img->Height - 1;
1221 const GLint shift = img->WidthLog2;
1222 GLuint i;
1223 (void) ctx;
1224 (void) lambda;
1225 ASSERT(tObj->WrapS==GL_REPEAT);
1226 ASSERT(tObj->WrapT==GL_REPEAT);
1227 ASSERT(img->Border==0);
1228 ASSERT(img->TexFormat->MesaFormat==MESA_FORMAT_RGBA);
1229 ASSERT(img->_IsPowerOfTwo);
1230
1231 for (i = 0; i < n; i++) {
1232 const GLint col = IFLOOR(texcoords[i][0] * width) & colMask;
1233 const GLint row = IFLOOR(texcoords[i][1] * height) & rowMask;
1234 const GLint pos = (row << shift) | col;
1235 const GLchan *texel = ((GLchan *) img->Data) + (pos << 2); /* pos*4 */
1236 COPY_4V(rgba[i], texel);
1237 }
1238 }
1239
1240
1241 /** Sample 2D texture, using lambda to choose between min/magnification */
1242 static void
1243 sample_lambda_2d(GLcontext *ctx,
1244 const struct gl_texture_object *tObj,
1245 GLuint n, const GLfloat texcoords[][4],
1246 const GLfloat lambda[], GLfloat rgba[][4])
1247 {
1248 const struct gl_texture_image *tImg = tObj->Image[0][tObj->BaseLevel];
1249 GLuint minStart, minEnd; /* texels with minification */
1250 GLuint magStart, magEnd; /* texels with magnification */
1251
1252 const GLboolean repeatNoBorderPOT = (tObj->WrapS == GL_REPEAT)
1253 && (tObj->WrapT == GL_REPEAT)
1254 && (tImg->Border == 0 && (tImg->Width == tImg->RowStride))
1255 && (tImg->TexFormat->BaseFormat != GL_COLOR_INDEX)
1256 && tImg->_IsPowerOfTwo;
1257
1258 ASSERT(lambda != NULL);
1259 compute_min_mag_ranges(tObj, n, lambda,
1260 &minStart, &minEnd, &magStart, &magEnd);
1261
1262 if (minStart < minEnd) {
1263 /* do the minified texels */
1264 const GLuint m = minEnd - minStart;
1265 switch (tObj->MinFilter) {
1266 case GL_NEAREST:
1267 if (repeatNoBorderPOT) {
1268 switch (tImg->TexFormat->MesaFormat) {
1269 #if 0
1270 case MESA_FORMAT_RGB:
1271 opt_sample_rgb_2d(ctx, tObj, m, texcoords + minStart,
1272 NULL, rgba + minStart);
1273 break;
1274 case MESA_FORMAT_RGBA:
1275 opt_sample_rgba_2d(ctx, tObj, m, texcoords + minStart,
1276 NULL, rgba + minStart);
1277 break;
1278 #endif
1279 default:
1280 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1281 NULL, rgba + minStart );
1282 }
1283 }
1284 else {
1285 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1286 NULL, rgba + minStart);
1287 }
1288 break;
1289 case GL_LINEAR:
1290 sample_linear_2d(ctx, tObj, m, texcoords + minStart,
1291 NULL, rgba + minStart);
1292 break;
1293 case GL_NEAREST_MIPMAP_NEAREST:
1294 sample_2d_nearest_mipmap_nearest(ctx, tObj, m,
1295 texcoords + minStart,
1296 lambda + minStart, rgba + minStart);
1297 break;
1298 case GL_LINEAR_MIPMAP_NEAREST:
1299 sample_2d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1300 lambda + minStart, rgba + minStart);
1301 break;
1302 case GL_NEAREST_MIPMAP_LINEAR:
1303 sample_2d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1304 lambda + minStart, rgba + minStart);
1305 break;
1306 case GL_LINEAR_MIPMAP_LINEAR:
1307 if (repeatNoBorderPOT)
1308 sample_2d_linear_mipmap_linear_repeat(ctx, tObj, m,
1309 texcoords + minStart, lambda + minStart, rgba + minStart);
1310 else
1311 sample_2d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1312 lambda + minStart, rgba + minStart);
1313 break;
1314 default:
1315 _mesa_problem(ctx, "Bad min filter in sample_2d_texture");
1316 return;
1317 }
1318 }
1319
1320 if (magStart < magEnd) {
1321 /* do the magnified texels */
1322 const GLuint m = magEnd - magStart;
1323
1324 switch (tObj->MagFilter) {
1325 case GL_NEAREST:
1326 if (repeatNoBorderPOT) {
1327 switch (tImg->TexFormat->MesaFormat) {
1328 #if 0
1329 case MESA_FORMAT_RGB:
1330 opt_sample_rgb_2d(ctx, tObj, m, texcoords + magStart,
1331 NULL, rgba + magStart);
1332 break;
1333 case MESA_FORMAT_RGBA:
1334 opt_sample_rgba_2d(ctx, tObj, m, texcoords + magStart,
1335 NULL, rgba + magStart);
1336 break;
1337 #endif
1338 default:
1339 sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
1340 NULL, rgba + magStart );
1341 }
1342 }
1343 else {
1344 sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
1345 NULL, rgba + magStart);
1346 }
1347 break;
1348 case GL_LINEAR:
1349 sample_linear_2d(ctx, tObj, m, texcoords + magStart,
1350 NULL, rgba + magStart);
1351 break;
1352 default:
1353 _mesa_problem(ctx, "Bad mag filter in sample_lambda_2d");
1354 }
1355 }
1356 }
1357
1358
1359
1360 /**********************************************************************/
1361 /* 3-D Texture Sampling Functions */
1362 /**********************************************************************/
1363
1364 /**
1365 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1366 */
1367 static INLINE void
1368 sample_3d_nearest(GLcontext *ctx,
1369 const struct gl_texture_object *tObj,
1370 const struct gl_texture_image *img,
1371 const GLfloat texcoord[4],
1372 GLfloat rgba[4])
1373 {
1374 const GLint width = img->Width2; /* without border, power of two */
1375 const GLint height = img->Height2; /* without border, power of two */
1376 const GLint depth = img->Depth2; /* without border, power of two */
1377 GLint i, j, k;
1378 (void) ctx;
1379
1380 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
1381 j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
1382 k = nearest_texel_location(tObj->WrapR, img, depth, texcoord[2]);
1383
1384 if (i < 0 || i >= (GLint) img->Width ||
1385 j < 0 || j >= (GLint) img->Height ||
1386 k < 0 || k >= (GLint) img->Depth) {
1387 /* Need this test for GL_CLAMP_TO_BORDER mode */
1388 COPY_4V(rgba, tObj->BorderColor);
1389 }
1390 else {
1391 img->FetchTexelf(img, i, j, k, rgba);
1392 }
1393 }
1394
1395
1396 /**
1397 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1398 */
1399 static void
1400 sample_3d_linear(GLcontext *ctx,
1401 const struct gl_texture_object *tObj,
1402 const struct gl_texture_image *img,
1403 const GLfloat texcoord[4],
1404 GLfloat rgba[4])
1405 {
1406 const GLint width = img->Width2;
1407 const GLint height = img->Height2;
1408 const GLint depth = img->Depth2;
1409 GLint i0, j0, k0, i1, j1, k1;
1410 GLbitfield useBorderColor = 0x0;
1411 GLfloat a, b, c;
1412 GLfloat t000[4], t010[4], t001[4], t011[4];
1413 GLfloat t100[4], t110[4], t101[4], t111[4];
1414
1415 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
1416 linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
1417 linear_texel_locations(tObj->WrapR, img, depth, texcoord[2], &k0, &k1, &c);
1418
1419 if (img->Border) {
1420 i0 += img->Border;
1421 i1 += img->Border;
1422 j0 += img->Border;
1423 j1 += img->Border;
1424 k0 += img->Border;
1425 k1 += img->Border;
1426 }
1427 else {
1428 /* check if sampling texture border color */
1429 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
1430 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
1431 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
1432 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
1433 if (k0 < 0 || k0 >= depth) useBorderColor |= K0BIT;
1434 if (k1 < 0 || k1 >= depth) useBorderColor |= K1BIT;
1435 }
1436
1437 /* Fetch texels */
1438 if (useBorderColor & (I0BIT | J0BIT | K0BIT)) {
1439 COPY_4V(t000, tObj->BorderColor);
1440 }
1441 else {
1442 img->FetchTexelf(img, i0, j0, k0, t000);
1443 }
1444 if (useBorderColor & (I1BIT | J0BIT | K0BIT)) {
1445 COPY_4V(t100, tObj->BorderColor);
1446 }
1447 else {
1448 img->FetchTexelf(img, i1, j0, k0, t100);
1449 }
1450 if (useBorderColor & (I0BIT | J1BIT | K0BIT)) {
1451 COPY_4V(t010, tObj->BorderColor);
1452 }
1453 else {
1454 img->FetchTexelf(img, i0, j1, k0, t010);
1455 }
1456 if (useBorderColor & (I1BIT | J1BIT | K0BIT)) {
1457 COPY_4V(t110, tObj->BorderColor);
1458 }
1459 else {
1460 img->FetchTexelf(img, i1, j1, k0, t110);
1461 }
1462
1463 if (useBorderColor & (I0BIT | J0BIT | K1BIT)) {
1464 COPY_4V(t001, tObj->BorderColor);
1465 }
1466 else {
1467 img->FetchTexelf(img, i0, j0, k1, t001);
1468 }
1469 if (useBorderColor & (I1BIT | J0BIT | K1BIT)) {
1470 COPY_4V(t101, tObj->BorderColor);
1471 }
1472 else {
1473 img->FetchTexelf(img, i1, j0, k1, t101);
1474 }
1475 if (useBorderColor & (I0BIT | J1BIT | K1BIT)) {
1476 COPY_4V(t011, tObj->BorderColor);
1477 }
1478 else {
1479 img->FetchTexelf(img, i0, j1, k1, t011);
1480 }
1481 if (useBorderColor & (I1BIT | J1BIT | K1BIT)) {
1482 COPY_4V(t111, tObj->BorderColor);
1483 }
1484 else {
1485 img->FetchTexelf(img, i1, j1, k1, t111);
1486 }
1487
1488 /* trilinear interpolation of samples */
1489 lerp_rgba_3d(rgba, a, b, c, t000, t100, t010, t110, t001, t101, t011, t111);
1490 }
1491
1492
1493 static void
1494 sample_3d_nearest_mipmap_nearest(GLcontext *ctx,
1495 const struct gl_texture_object *tObj,
1496 GLuint n, const GLfloat texcoord[][4],
1497 const GLfloat lambda[], GLfloat rgba[][4] )
1498 {
1499 GLuint i;
1500 for (i = 0; i < n; i++) {
1501 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1502 sample_3d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1503 }
1504 }
1505
1506
1507 static void
1508 sample_3d_linear_mipmap_nearest(GLcontext *ctx,
1509 const struct gl_texture_object *tObj,
1510 GLuint n, const GLfloat texcoord[][4],
1511 const GLfloat lambda[], GLfloat rgba[][4])
1512 {
1513 GLuint i;
1514 ASSERT(lambda != NULL);
1515 for (i = 0; i < n; i++) {
1516 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1517 sample_3d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1518 }
1519 }
1520
1521
1522 static void
1523 sample_3d_nearest_mipmap_linear(GLcontext *ctx,
1524 const struct gl_texture_object *tObj,
1525 GLuint n, const GLfloat texcoord[][4],
1526 const GLfloat lambda[], GLfloat rgba[][4])
1527 {
1528 GLuint i;
1529 ASSERT(lambda != NULL);
1530 for (i = 0; i < n; i++) {
1531 GLint level = linear_mipmap_level(tObj, lambda[i]);
1532 if (level >= tObj->_MaxLevel) {
1533 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1534 texcoord[i], rgba[i]);
1535 }
1536 else {
1537 GLfloat t0[4], t1[4]; /* texels */
1538 const GLfloat f = FRAC(lambda[i]);
1539 sample_3d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1540 sample_3d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1541 lerp_rgba(rgba[i], f, t0, t1);
1542 }
1543 }
1544 }
1545
1546
1547 static void
1548 sample_3d_linear_mipmap_linear(GLcontext *ctx,
1549 const struct gl_texture_object *tObj,
1550 GLuint n, const GLfloat texcoord[][4],
1551 const GLfloat lambda[], GLfloat rgba[][4])
1552 {
1553 GLuint i;
1554 ASSERT(lambda != NULL);
1555 for (i = 0; i < n; i++) {
1556 GLint level = linear_mipmap_level(tObj, lambda[i]);
1557 if (level >= tObj->_MaxLevel) {
1558 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1559 texcoord[i], rgba[i]);
1560 }
1561 else {
1562 GLfloat t0[4], t1[4]; /* texels */
1563 const GLfloat f = FRAC(lambda[i]);
1564 sample_3d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1565 sample_3d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1566 lerp_rgba(rgba[i], f, t0, t1);
1567 }
1568 }
1569 }
1570
1571
1572 /** Sample 3D texture, nearest filtering for both min/magnification */
1573 static void
1574 sample_nearest_3d(GLcontext *ctx,
1575 const struct gl_texture_object *tObj, GLuint n,
1576 const GLfloat texcoords[][4], const GLfloat lambda[],
1577 GLfloat rgba[][4])
1578 {
1579 GLuint i;
1580 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1581 (void) lambda;
1582 for (i = 0; i < n; i++) {
1583 sample_3d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
1584 }
1585 }
1586
1587
1588 /** Sample 3D texture, linear filtering for both min/magnification */
1589 static void
1590 sample_linear_3d(GLcontext *ctx,
1591 const struct gl_texture_object *tObj, GLuint n,
1592 const GLfloat texcoords[][4],
1593 const GLfloat lambda[], GLfloat rgba[][4])
1594 {
1595 GLuint i;
1596 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1597 (void) lambda;
1598 for (i = 0; i < n; i++) {
1599 sample_3d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
1600 }
1601 }
1602
1603
1604 /** Sample 3D texture, using lambda to choose between min/magnification */
1605 static void
1606 sample_lambda_3d(GLcontext *ctx,
1607 const struct gl_texture_object *tObj, GLuint n,
1608 const GLfloat texcoords[][4], const GLfloat lambda[],
1609 GLfloat rgba[][4])
1610 {
1611 GLuint minStart, minEnd; /* texels with minification */
1612 GLuint magStart, magEnd; /* texels with magnification */
1613 GLuint i;
1614
1615 ASSERT(lambda != NULL);
1616 compute_min_mag_ranges(tObj, n, lambda,
1617 &minStart, &minEnd, &magStart, &magEnd);
1618
1619 if (minStart < minEnd) {
1620 /* do the minified texels */
1621 GLuint m = minEnd - minStart;
1622 switch (tObj->MinFilter) {
1623 case GL_NEAREST:
1624 for (i = minStart; i < minEnd; i++)
1625 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1626 texcoords[i], rgba[i]);
1627 break;
1628 case GL_LINEAR:
1629 for (i = minStart; i < minEnd; i++)
1630 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1631 texcoords[i], rgba[i]);
1632 break;
1633 case GL_NEAREST_MIPMAP_NEAREST:
1634 sample_3d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1635 lambda + minStart, rgba + minStart);
1636 break;
1637 case GL_LINEAR_MIPMAP_NEAREST:
1638 sample_3d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1639 lambda + minStart, rgba + minStart);
1640 break;
1641 case GL_NEAREST_MIPMAP_LINEAR:
1642 sample_3d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1643 lambda + minStart, rgba + minStart);
1644 break;
1645 case GL_LINEAR_MIPMAP_LINEAR:
1646 sample_3d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1647 lambda + minStart, rgba + minStart);
1648 break;
1649 default:
1650 _mesa_problem(ctx, "Bad min filter in sample_3d_texture");
1651 return;
1652 }
1653 }
1654
1655 if (magStart < magEnd) {
1656 /* do the magnified texels */
1657 switch (tObj->MagFilter) {
1658 case GL_NEAREST:
1659 for (i = magStart; i < magEnd; i++)
1660 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1661 texcoords[i], rgba[i]);
1662 break;
1663 case GL_LINEAR:
1664 for (i = magStart; i < magEnd; i++)
1665 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1666 texcoords[i], rgba[i]);
1667 break;
1668 default:
1669 _mesa_problem(ctx, "Bad mag filter in sample_3d_texture");
1670 return;
1671 }
1672 }
1673 }
1674
1675
1676 /**********************************************************************/
1677 /* Texture Cube Map Sampling Functions */
1678 /**********************************************************************/
1679
1680 /**
1681 * Choose one of six sides of a texture cube map given the texture
1682 * coord (rx,ry,rz). Return pointer to corresponding array of texture
1683 * images.
1684 */
1685 static const struct gl_texture_image **
1686 choose_cube_face(const struct gl_texture_object *texObj,
1687 const GLfloat texcoord[4], GLfloat newCoord[4])
1688 {
1689 /*
1690 major axis
1691 direction target sc tc ma
1692 ---------- ------------------------------- --- --- ---
1693 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1694 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1695 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1696 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1697 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1698 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1699 */
1700 const GLfloat rx = texcoord[0];
1701 const GLfloat ry = texcoord[1];
1702 const GLfloat rz = texcoord[2];
1703 const GLfloat arx = FABSF(rx), ary = FABSF(ry), arz = FABSF(rz);
1704 GLuint face;
1705 GLfloat sc, tc, ma;
1706
1707 if (arx > ary && arx > arz) {
1708 if (rx >= 0.0F) {
1709 face = FACE_POS_X;
1710 sc = -rz;
1711 tc = -ry;
1712 ma = arx;
1713 }
1714 else {
1715 face = FACE_NEG_X;
1716 sc = rz;
1717 tc = -ry;
1718 ma = arx;
1719 }
1720 }
1721 else if (ary > arx && ary > arz) {
1722 if (ry >= 0.0F) {
1723 face = FACE_POS_Y;
1724 sc = rx;
1725 tc = rz;
1726 ma = ary;
1727 }
1728 else {
1729 face = FACE_NEG_Y;
1730 sc = rx;
1731 tc = -rz;
1732 ma = ary;
1733 }
1734 }
1735 else {
1736 if (rz > 0.0F) {
1737 face = FACE_POS_Z;
1738 sc = rx;
1739 tc = -ry;
1740 ma = arz;
1741 }
1742 else {
1743 face = FACE_NEG_Z;
1744 sc = -rx;
1745 tc = -ry;
1746 ma = arz;
1747 }
1748 }
1749
1750 newCoord[0] = ( sc / ma + 1.0F ) * 0.5F;
1751 newCoord[1] = ( tc / ma + 1.0F ) * 0.5F;
1752 return (const struct gl_texture_image **) texObj->Image[face];
1753 }
1754
1755
1756 static void
1757 sample_nearest_cube(GLcontext *ctx,
1758 const struct gl_texture_object *tObj, GLuint n,
1759 const GLfloat texcoords[][4], const GLfloat lambda[],
1760 GLfloat rgba[][4])
1761 {
1762 GLuint i;
1763 (void) lambda;
1764 for (i = 0; i < n; i++) {
1765 const struct gl_texture_image **images;
1766 GLfloat newCoord[4];
1767 images = choose_cube_face(tObj, texcoords[i], newCoord);
1768 sample_2d_nearest(ctx, tObj, images[tObj->BaseLevel],
1769 newCoord, rgba[i]);
1770 }
1771 }
1772
1773
1774 static void
1775 sample_linear_cube(GLcontext *ctx,
1776 const struct gl_texture_object *tObj, GLuint n,
1777 const GLfloat texcoords[][4],
1778 const GLfloat lambda[], GLfloat rgba[][4])
1779 {
1780 GLuint i;
1781 (void) lambda;
1782 for (i = 0; i < n; i++) {
1783 const struct gl_texture_image **images;
1784 GLfloat newCoord[4];
1785 images = choose_cube_face(tObj, texcoords[i], newCoord);
1786 sample_2d_linear(ctx, tObj, images[tObj->BaseLevel],
1787 newCoord, rgba[i]);
1788 }
1789 }
1790
1791
1792 static void
1793 sample_cube_nearest_mipmap_nearest(GLcontext *ctx,
1794 const struct gl_texture_object *tObj,
1795 GLuint n, const GLfloat texcoord[][4],
1796 const GLfloat lambda[], GLfloat rgba[][4])
1797 {
1798 GLuint i;
1799 ASSERT(lambda != NULL);
1800 for (i = 0; i < n; i++) {
1801 const struct gl_texture_image **images;
1802 GLfloat newCoord[4];
1803 GLint level;
1804 images = choose_cube_face(tObj, texcoord[i], newCoord);
1805
1806 /* XXX we actually need to recompute lambda here based on the newCoords.
1807 * But we would need the texcoords of adjacent fragments to compute that
1808 * properly, and we don't have those here.
1809 * For now, do an approximation: subtracting 1 from the chosen mipmap
1810 * level seems to work in some test cases.
1811 * The same adjustment is done in the next few functions.
1812 */
1813 level = nearest_mipmap_level(tObj, lambda[i]);
1814 level = MAX2(level - 1, 0);
1815
1816 sample_2d_nearest(ctx, tObj, images[level], newCoord, rgba[i]);
1817 }
1818 }
1819
1820
1821 static void
1822 sample_cube_linear_mipmap_nearest(GLcontext *ctx,
1823 const struct gl_texture_object *tObj,
1824 GLuint n, const GLfloat texcoord[][4],
1825 const GLfloat lambda[], GLfloat rgba[][4])
1826 {
1827 GLuint i;
1828 ASSERT(lambda != NULL);
1829 for (i = 0; i < n; i++) {
1830 const struct gl_texture_image **images;
1831 GLfloat newCoord[4];
1832 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1833 level = MAX2(level - 1, 0); /* see comment above */
1834 images = choose_cube_face(tObj, texcoord[i], newCoord);
1835 sample_2d_linear(ctx, tObj, images[level], newCoord, rgba[i]);
1836 }
1837 }
1838
1839
1840 static void
1841 sample_cube_nearest_mipmap_linear(GLcontext *ctx,
1842 const struct gl_texture_object *tObj,
1843 GLuint n, const GLfloat texcoord[][4],
1844 const GLfloat lambda[], GLfloat rgba[][4])
1845 {
1846 GLuint i;
1847 ASSERT(lambda != NULL);
1848 for (i = 0; i < n; i++) {
1849 const struct gl_texture_image **images;
1850 GLfloat newCoord[4];
1851 GLint level = linear_mipmap_level(tObj, lambda[i]);
1852 level = MAX2(level - 1, 0); /* see comment above */
1853 images = choose_cube_face(tObj, texcoord[i], newCoord);
1854 if (level >= tObj->_MaxLevel) {
1855 sample_2d_nearest(ctx, tObj, images[tObj->_MaxLevel],
1856 newCoord, rgba[i]);
1857 }
1858 else {
1859 GLfloat t0[4], t1[4]; /* texels */
1860 const GLfloat f = FRAC(lambda[i]);
1861 sample_2d_nearest(ctx, tObj, images[level ], newCoord, t0);
1862 sample_2d_nearest(ctx, tObj, images[level+1], newCoord, t1);
1863 lerp_rgba(rgba[i], f, t0, t1);
1864 }
1865 }
1866 }
1867
1868
1869 static void
1870 sample_cube_linear_mipmap_linear(GLcontext *ctx,
1871 const struct gl_texture_object *tObj,
1872 GLuint n, const GLfloat texcoord[][4],
1873 const GLfloat lambda[], GLfloat rgba[][4])
1874 {
1875 GLuint i;
1876 ASSERT(lambda != NULL);
1877 for (i = 0; i < n; i++) {
1878 const struct gl_texture_image **images;
1879 GLfloat newCoord[4];
1880 GLint level = linear_mipmap_level(tObj, lambda[i]);
1881 level = MAX2(level - 1, 0); /* see comment above */
1882 images = choose_cube_face(tObj, texcoord[i], newCoord);
1883 if (level >= tObj->_MaxLevel) {
1884 sample_2d_linear(ctx, tObj, images[tObj->_MaxLevel],
1885 newCoord, rgba[i]);
1886 }
1887 else {
1888 GLfloat t0[4], t1[4];
1889 const GLfloat f = FRAC(lambda[i]);
1890 sample_2d_linear(ctx, tObj, images[level ], newCoord, t0);
1891 sample_2d_linear(ctx, tObj, images[level+1], newCoord, t1);
1892 lerp_rgba(rgba[i], f, t0, t1);
1893 }
1894 }
1895 }
1896
1897
1898 /** Sample cube texture, using lambda to choose between min/magnification */
1899 static void
1900 sample_lambda_cube(GLcontext *ctx,
1901 const struct gl_texture_object *tObj, GLuint n,
1902 const GLfloat texcoords[][4], const GLfloat lambda[],
1903 GLfloat rgba[][4])
1904 {
1905 GLuint minStart, minEnd; /* texels with minification */
1906 GLuint magStart, magEnd; /* texels with magnification */
1907
1908 ASSERT(lambda != NULL);
1909 compute_min_mag_ranges(tObj, n, lambda,
1910 &minStart, &minEnd, &magStart, &magEnd);
1911
1912 if (minStart < minEnd) {
1913 /* do the minified texels */
1914 const GLuint m = minEnd - minStart;
1915 switch (tObj->MinFilter) {
1916 case GL_NEAREST:
1917 sample_nearest_cube(ctx, tObj, m, texcoords + minStart,
1918 lambda + minStart, rgba + minStart);
1919 break;
1920 case GL_LINEAR:
1921 sample_linear_cube(ctx, tObj, m, texcoords + minStart,
1922 lambda + minStart, rgba + minStart);
1923 break;
1924 case GL_NEAREST_MIPMAP_NEAREST:
1925 sample_cube_nearest_mipmap_nearest(ctx, tObj, m,
1926 texcoords + minStart,
1927 lambda + minStart, rgba + minStart);
1928 break;
1929 case GL_LINEAR_MIPMAP_NEAREST:
1930 sample_cube_linear_mipmap_nearest(ctx, tObj, m,
1931 texcoords + minStart,
1932 lambda + minStart, rgba + minStart);
1933 break;
1934 case GL_NEAREST_MIPMAP_LINEAR:
1935 sample_cube_nearest_mipmap_linear(ctx, tObj, m,
1936 texcoords + minStart,
1937 lambda + minStart, rgba + minStart);
1938 break;
1939 case GL_LINEAR_MIPMAP_LINEAR:
1940 sample_cube_linear_mipmap_linear(ctx, tObj, m,
1941 texcoords + minStart,
1942 lambda + minStart, rgba + minStart);
1943 break;
1944 default:
1945 _mesa_problem(ctx, "Bad min filter in sample_lambda_cube");
1946 }
1947 }
1948
1949 if (magStart < magEnd) {
1950 /* do the magnified texels */
1951 const GLuint m = magEnd - magStart;
1952 switch (tObj->MagFilter) {
1953 case GL_NEAREST:
1954 sample_nearest_cube(ctx, tObj, m, texcoords + magStart,
1955 lambda + magStart, rgba + magStart);
1956 break;
1957 case GL_LINEAR:
1958 sample_linear_cube(ctx, tObj, m, texcoords + magStart,
1959 lambda + magStart, rgba + magStart);
1960 break;
1961 default:
1962 _mesa_problem(ctx, "Bad mag filter in sample_lambda_cube");
1963 }
1964 }
1965 }
1966
1967
1968 /**********************************************************************/
1969 /* Texture Rectangle Sampling Functions */
1970 /**********************************************************************/
1971
1972
1973 /**
1974 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
1975 */
1976 static INLINE GLint
1977 clamp_rect_coord_nearest(GLenum wrapMode, GLfloat coord, GLint max)
1978 {
1979 switch (wrapMode) {
1980 case GL_CLAMP:
1981 return IFLOOR( CLAMP(coord, 0.0F, max - 1) );
1982 case GL_CLAMP_TO_EDGE:
1983 return IFLOOR( CLAMP(coord, 0.5F, max - 0.5F) );
1984 case GL_CLAMP_TO_BORDER:
1985 return IFLOOR( CLAMP(coord, -0.5F, max + 0.5F) );
1986 default:
1987 _mesa_problem(NULL, "bad wrapMode in clamp_rect_coord_nearest");
1988 return 0;
1989 }
1990 }
1991
1992
1993 /**
1994 * As above, but GL_LINEAR filtering.
1995 */
1996 static INLINE void
1997 clamp_rect_coord_linear(GLenum wrapMode, GLfloat coord, GLint max,
1998 GLint *i0out, GLint *i1out, GLfloat *weight)
1999 {
2000 GLfloat fcol;
2001 GLint i0, i1;
2002 switch (wrapMode) {
2003 case GL_CLAMP:
2004 /* Not exactly what the spec says, but it matches NVIDIA output */
2005 fcol = CLAMP(coord - 0.5F, 0.0, max-1);
2006 i0 = IFLOOR(fcol);
2007 i1 = i0 + 1;
2008 break;
2009 case GL_CLAMP_TO_EDGE:
2010 fcol = CLAMP(coord, 0.5F, max - 0.5F);
2011 fcol -= 0.5F;
2012 i0 = IFLOOR(fcol);
2013 i1 = i0 + 1;
2014 if (i1 > max - 1)
2015 i1 = max - 1;
2016 break;
2017 case GL_CLAMP_TO_BORDER:
2018 fcol = CLAMP(coord, -0.5F, max + 0.5F);
2019 fcol -= 0.5F;
2020 i0 = IFLOOR(fcol);
2021 i1 = i0 + 1;
2022 default:
2023 _mesa_problem(NULL, "bad wrapMode in clamp_rect_coord_linear");
2024 i0 = i1 = 0;
2025 fcol = 0.0F;
2026 }
2027 *i0out = i0;
2028 *i1out = i1;
2029 *weight = FRAC(fcol);
2030 }
2031
2032
2033 static void
2034 sample_nearest_rect(GLcontext *ctx,
2035 const struct gl_texture_object *tObj, GLuint n,
2036 const GLfloat texcoords[][4], const GLfloat lambda[],
2037 GLfloat rgba[][4])
2038 {
2039 const struct gl_texture_image *img = tObj->Image[0][0];
2040 const GLint width = img->Width;
2041 const GLint height = img->Height;
2042 GLuint i;
2043
2044 (void) ctx;
2045 (void) lambda;
2046
2047 ASSERT(tObj->WrapS == GL_CLAMP ||
2048 tObj->WrapS == GL_CLAMP_TO_EDGE ||
2049 tObj->WrapS == GL_CLAMP_TO_BORDER);
2050 ASSERT(tObj->WrapT == GL_CLAMP ||
2051 tObj->WrapT == GL_CLAMP_TO_EDGE ||
2052 tObj->WrapT == GL_CLAMP_TO_BORDER);
2053 ASSERT(img->TexFormat->BaseFormat != GL_COLOR_INDEX);
2054
2055 for (i = 0; i < n; i++) {
2056 GLint row, col;
2057 col = clamp_rect_coord_nearest(tObj->WrapS, texcoords[i][0], width);
2058 row = clamp_rect_coord_nearest(tObj->WrapT, texcoords[i][1], height);
2059 if (col < 0 || col >= width || row < 0 || row >= height)
2060 COPY_4V(rgba[i], tObj->BorderColor);
2061 else
2062 img->FetchTexelf(img, col, row, 0, rgba[i]);
2063 }
2064 }
2065
2066
2067 static void
2068 sample_linear_rect(GLcontext *ctx,
2069 const struct gl_texture_object *tObj, GLuint n,
2070 const GLfloat texcoords[][4],
2071 const GLfloat lambda[], GLfloat rgba[][4])
2072 {
2073 const struct gl_texture_image *img = tObj->Image[0][0];
2074 const GLint width = img->Width;
2075 const GLint height = img->Height;
2076 GLuint i;
2077
2078 (void) ctx;
2079 (void) lambda;
2080
2081 ASSERT(tObj->WrapS == GL_CLAMP ||
2082 tObj->WrapS == GL_CLAMP_TO_EDGE ||
2083 tObj->WrapS == GL_CLAMP_TO_BORDER);
2084 ASSERT(tObj->WrapT == GL_CLAMP ||
2085 tObj->WrapT == GL_CLAMP_TO_EDGE ||
2086 tObj->WrapT == GL_CLAMP_TO_BORDER);
2087 ASSERT(img->TexFormat->BaseFormat != GL_COLOR_INDEX);
2088
2089 for (i = 0; i < n; i++) {
2090 GLint i0, j0, i1, j1;
2091 GLfloat t00[4], t01[4], t10[4], t11[4];
2092 GLfloat a, b;
2093 GLbitfield useBorderColor = 0x0;
2094
2095 clamp_rect_coord_linear(tObj->WrapS, texcoords[i][0], width,
2096 &i0, &i1, &a);
2097 clamp_rect_coord_linear(tObj->WrapT, texcoords[i][1], height,
2098 &j0, &j1, &b);
2099
2100 /* compute integer rows/columns */
2101 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2102 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2103 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
2104 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
2105
2106 /* get four texel samples */
2107 if (useBorderColor & (I0BIT | J0BIT))
2108 COPY_4V(t00, tObj->BorderColor);
2109 else
2110 img->FetchTexelf(img, i0, j0, 0, t00);
2111
2112 if (useBorderColor & (I1BIT | J0BIT))
2113 COPY_4V(t10, tObj->BorderColor);
2114 else
2115 img->FetchTexelf(img, i1, j0, 0, t10);
2116
2117 if (useBorderColor & (I0BIT | J1BIT))
2118 COPY_4V(t01, tObj->BorderColor);
2119 else
2120 img->FetchTexelf(img, i0, j1, 0, t01);
2121
2122 if (useBorderColor & (I1BIT | J1BIT))
2123 COPY_4V(t11, tObj->BorderColor);
2124 else
2125 img->FetchTexelf(img, i1, j1, 0, t11);
2126
2127 lerp_rgba_2d(rgba[i], a, b, t00, t10, t01, t11);
2128 }
2129 }
2130
2131
2132 /** Sample Rect texture, using lambda to choose between min/magnification */
2133 static void
2134 sample_lambda_rect(GLcontext *ctx,
2135 const struct gl_texture_object *tObj, GLuint n,
2136 const GLfloat texcoords[][4], const GLfloat lambda[],
2137 GLfloat rgba[][4])
2138 {
2139 GLuint minStart, minEnd, magStart, magEnd;
2140
2141 /* We only need lambda to decide between minification and magnification.
2142 * There is no mipmapping with rectangular textures.
2143 */
2144 compute_min_mag_ranges(tObj, n, lambda,
2145 &minStart, &minEnd, &magStart, &magEnd);
2146
2147 if (minStart < minEnd) {
2148 if (tObj->MinFilter == GL_NEAREST) {
2149 sample_nearest_rect(ctx, tObj, minEnd - minStart,
2150 texcoords + minStart, NULL, rgba + minStart);
2151 }
2152 else {
2153 sample_linear_rect(ctx, tObj, minEnd - minStart,
2154 texcoords + minStart, NULL, rgba + minStart);
2155 }
2156 }
2157 if (magStart < magEnd) {
2158 if (tObj->MagFilter == GL_NEAREST) {
2159 sample_nearest_rect(ctx, tObj, magEnd - magStart,
2160 texcoords + magStart, NULL, rgba + magStart);
2161 }
2162 else {
2163 sample_linear_rect(ctx, tObj, magEnd - magStart,
2164 texcoords + magStart, NULL, rgba + magStart);
2165 }
2166 }
2167 }
2168
2169
2170
2171 /**********************************************************************/
2172 /* 2D Texture Array Sampling Functions */
2173 /**********************************************************************/
2174
2175 /**
2176 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2177 */
2178 static void
2179 sample_2d_array_nearest(GLcontext *ctx,
2180 const struct gl_texture_object *tObj,
2181 const struct gl_texture_image *img,
2182 const GLfloat texcoord[4],
2183 GLfloat rgba[4])
2184 {
2185 const GLint width = img->Width2; /* without border, power of two */
2186 const GLint height = img->Height2; /* without border, power of two */
2187 const GLint depth = img->Depth;
2188 GLint i, j;
2189 GLint array;
2190 (void) ctx;
2191
2192 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
2193 j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
2194 array = clamp_rect_coord_nearest(tObj->WrapR, texcoord[2], depth);
2195
2196 if (i < 0 || i >= (GLint) img->Width ||
2197 j < 0 || j >= (GLint) img->Height ||
2198 array < 0 || array >= (GLint) img->Depth) {
2199 /* Need this test for GL_CLAMP_TO_BORDER mode */
2200 COPY_4V(rgba, tObj->BorderColor);
2201 }
2202 else {
2203 img->FetchTexelf(img, i, j, array, rgba);
2204 }
2205 }
2206
2207
2208 /**
2209 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2210 */
2211 static void
2212 sample_2d_array_linear(GLcontext *ctx,
2213 const struct gl_texture_object *tObj,
2214 const struct gl_texture_image *img,
2215 const GLfloat texcoord[4],
2216 GLfloat rgba[4])
2217 {
2218 const GLint width = img->Width2;
2219 const GLint height = img->Height2;
2220 const GLint depth = img->Depth;
2221 GLint i0, j0, i1, j1;
2222 GLint array;
2223 GLbitfield useBorderColor = 0x0;
2224 GLfloat a, b;
2225 GLfloat t00[4], t01[4], t10[4], t11[4];
2226
2227 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
2228 linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
2229 array = clamp_rect_coord_nearest(tObj->WrapR, texcoord[2], depth);
2230
2231 if (array < 0 || array >= depth) {
2232 COPY_4V(rgba, tObj->BorderColor);
2233 }
2234 else {
2235 if (img->Border) {
2236 i0 += img->Border;
2237 i1 += img->Border;
2238 j0 += img->Border;
2239 j1 += img->Border;
2240 }
2241 else {
2242 /* check if sampling texture border color */
2243 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2244 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2245 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
2246 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
2247 }
2248
2249 /* Fetch texels */
2250 if (useBorderColor & (I0BIT | J0BIT)) {
2251 COPY_4V(t00, tObj->BorderColor);
2252 }
2253 else {
2254 img->FetchTexelf(img, i0, j0, array, t00);
2255 }
2256 if (useBorderColor & (I1BIT | J0BIT)) {
2257 COPY_4V(t10, tObj->BorderColor);
2258 }
2259 else {
2260 img->FetchTexelf(img, i1, j0, array, t10);
2261 }
2262 if (useBorderColor & (I0BIT | J1BIT)) {
2263 COPY_4V(t01, tObj->BorderColor);
2264 }
2265 else {
2266 img->FetchTexelf(img, i0, j1, array, t01);
2267 }
2268 if (useBorderColor & (I1BIT | J1BIT)) {
2269 COPY_4V(t11, tObj->BorderColor);
2270 }
2271 else {
2272 img->FetchTexelf(img, i1, j1, array, t11);
2273 }
2274
2275 /* trilinear interpolation of samples */
2276 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
2277 }
2278 }
2279
2280
2281 static void
2282 sample_2d_array_nearest_mipmap_nearest(GLcontext *ctx,
2283 const struct gl_texture_object *tObj,
2284 GLuint n, const GLfloat texcoord[][4],
2285 const GLfloat lambda[], GLfloat rgba[][4])
2286 {
2287 GLuint i;
2288 for (i = 0; i < n; i++) {
2289 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2290 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i],
2291 rgba[i]);
2292 }
2293 }
2294
2295
2296 static void
2297 sample_2d_array_linear_mipmap_nearest(GLcontext *ctx,
2298 const struct gl_texture_object *tObj,
2299 GLuint n, const GLfloat texcoord[][4],
2300 const GLfloat lambda[], GLfloat rgba[][4])
2301 {
2302 GLuint i;
2303 ASSERT(lambda != NULL);
2304 for (i = 0; i < n; i++) {
2305 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2306 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level],
2307 texcoord[i], rgba[i]);
2308 }
2309 }
2310
2311
2312 static void
2313 sample_2d_array_nearest_mipmap_linear(GLcontext *ctx,
2314 const struct gl_texture_object *tObj,
2315 GLuint n, const GLfloat texcoord[][4],
2316 const GLfloat lambda[], GLfloat rgba[][4])
2317 {
2318 GLuint i;
2319 ASSERT(lambda != NULL);
2320 for (i = 0; i < n; i++) {
2321 GLint level = linear_mipmap_level(tObj, lambda[i]);
2322 if (level >= tObj->_MaxLevel) {
2323 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2324 texcoord[i], rgba[i]);
2325 }
2326 else {
2327 GLfloat t0[4], t1[4]; /* texels */
2328 const GLfloat f = FRAC(lambda[i]);
2329 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level ],
2330 texcoord[i], t0);
2331 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level+1],
2332 texcoord[i], t1);
2333 lerp_rgba(rgba[i], f, t0, t1);
2334 }
2335 }
2336 }
2337
2338
2339 static void
2340 sample_2d_array_linear_mipmap_linear(GLcontext *ctx,
2341 const struct gl_texture_object *tObj,
2342 GLuint n, const GLfloat texcoord[][4],
2343 const GLfloat lambda[], GLfloat rgba[][4])
2344 {
2345 GLuint i;
2346 ASSERT(lambda != NULL);
2347 for (i = 0; i < n; i++) {
2348 GLint level = linear_mipmap_level(tObj, lambda[i]);
2349 if (level >= tObj->_MaxLevel) {
2350 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2351 texcoord[i], rgba[i]);
2352 }
2353 else {
2354 GLfloat t0[4], t1[4]; /* texels */
2355 const GLfloat f = FRAC(lambda[i]);
2356 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level ],
2357 texcoord[i], t0);
2358 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level+1],
2359 texcoord[i], t1);
2360 lerp_rgba(rgba[i], f, t0, t1);
2361 }
2362 }
2363 }
2364
2365
2366 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2367 static void
2368 sample_nearest_2d_array(GLcontext *ctx,
2369 const struct gl_texture_object *tObj, GLuint n,
2370 const GLfloat texcoords[][4], const GLfloat lambda[],
2371 GLfloat rgba[][4])
2372 {
2373 GLuint i;
2374 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2375 (void) lambda;
2376 for (i = 0; i < n; i++) {
2377 sample_2d_array_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
2378 }
2379 }
2380
2381
2382
2383 /** Sample 2D Array texture, linear filtering for both min/magnification */
2384 static void
2385 sample_linear_2d_array(GLcontext *ctx,
2386 const struct gl_texture_object *tObj, GLuint n,
2387 const GLfloat texcoords[][4],
2388 const GLfloat lambda[], GLfloat rgba[][4])
2389 {
2390 GLuint i;
2391 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2392 (void) lambda;
2393 for (i = 0; i < n; i++) {
2394 sample_2d_array_linear(ctx, tObj, image, texcoords[i], rgba[i]);
2395 }
2396 }
2397
2398
2399 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2400 static void
2401 sample_lambda_2d_array(GLcontext *ctx,
2402 const struct gl_texture_object *tObj, GLuint n,
2403 const GLfloat texcoords[][4], const GLfloat lambda[],
2404 GLfloat rgba[][4])
2405 {
2406 GLuint minStart, minEnd; /* texels with minification */
2407 GLuint magStart, magEnd; /* texels with magnification */
2408 GLuint i;
2409
2410 ASSERT(lambda != NULL);
2411 compute_min_mag_ranges(tObj, n, lambda,
2412 &minStart, &minEnd, &magStart, &magEnd);
2413
2414 if (minStart < minEnd) {
2415 /* do the minified texels */
2416 GLuint m = minEnd - minStart;
2417 switch (tObj->MinFilter) {
2418 case GL_NEAREST:
2419 for (i = minStart; i < minEnd; i++)
2420 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2421 texcoords[i], rgba[i]);
2422 break;
2423 case GL_LINEAR:
2424 for (i = minStart; i < minEnd; i++)
2425 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2426 texcoords[i], rgba[i]);
2427 break;
2428 case GL_NEAREST_MIPMAP_NEAREST:
2429 sample_2d_array_nearest_mipmap_nearest(ctx, tObj, m,
2430 texcoords + minStart,
2431 lambda + minStart,
2432 rgba + minStart);
2433 break;
2434 case GL_LINEAR_MIPMAP_NEAREST:
2435 sample_2d_array_linear_mipmap_nearest(ctx, tObj, m,
2436 texcoords + minStart,
2437 lambda + minStart,
2438 rgba + minStart);
2439 break;
2440 case GL_NEAREST_MIPMAP_LINEAR:
2441 sample_2d_array_nearest_mipmap_linear(ctx, tObj, m,
2442 texcoords + minStart,
2443 lambda + minStart,
2444 rgba + minStart);
2445 break;
2446 case GL_LINEAR_MIPMAP_LINEAR:
2447 sample_2d_array_linear_mipmap_linear(ctx, tObj, m,
2448 texcoords + minStart,
2449 lambda + minStart,
2450 rgba + minStart);
2451 break;
2452 default:
2453 _mesa_problem(ctx, "Bad min filter in sample_2d_array_texture");
2454 return;
2455 }
2456 }
2457
2458 if (magStart < magEnd) {
2459 /* do the magnified texels */
2460 switch (tObj->MagFilter) {
2461 case GL_NEAREST:
2462 for (i = magStart; i < magEnd; i++)
2463 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2464 texcoords[i], rgba[i]);
2465 break;
2466 case GL_LINEAR:
2467 for (i = magStart; i < magEnd; i++)
2468 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2469 texcoords[i], rgba[i]);
2470 break;
2471 default:
2472 _mesa_problem(ctx, "Bad mag filter in sample_2d_array_texture");
2473 return;
2474 }
2475 }
2476 }
2477
2478
2479
2480
2481 /**********************************************************************/
2482 /* 1D Texture Array Sampling Functions */
2483 /**********************************************************************/
2484
2485 /**
2486 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2487 */
2488 static void
2489 sample_1d_array_nearest(GLcontext *ctx,
2490 const struct gl_texture_object *tObj,
2491 const struct gl_texture_image *img,
2492 const GLfloat texcoord[4],
2493 GLfloat rgba[4])
2494 {
2495 const GLint width = img->Width2; /* without border, power of two */
2496 const GLint height = img->Height;
2497 GLint i;
2498 GLint array;
2499 (void) ctx;
2500
2501 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
2502 array = clamp_rect_coord_nearest(tObj->WrapT, texcoord[1], height);
2503
2504 if (i < 0 || i >= (GLint) img->Width ||
2505 array < 0 || array >= (GLint) img->Height) {
2506 /* Need this test for GL_CLAMP_TO_BORDER mode */
2507 COPY_4V(rgba, tObj->BorderColor);
2508 }
2509 else {
2510 img->FetchTexelf(img, i, array, 0, rgba);
2511 }
2512 }
2513
2514
2515 /**
2516 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2517 */
2518 static void
2519 sample_1d_array_linear(GLcontext *ctx,
2520 const struct gl_texture_object *tObj,
2521 const struct gl_texture_image *img,
2522 const GLfloat texcoord[4],
2523 GLfloat rgba[4])
2524 {
2525 const GLint width = img->Width2;
2526 const GLint height = img->Height;
2527 GLint i0, i1;
2528 GLint array;
2529 GLbitfield useBorderColor = 0x0;
2530 GLfloat a;
2531 GLfloat t0[4], t1[4];
2532
2533 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
2534 array = clamp_rect_coord_nearest(tObj->WrapT, texcoord[1], height);
2535
2536 if (img->Border) {
2537 i0 += img->Border;
2538 i1 += img->Border;
2539 }
2540 else {
2541 /* check if sampling texture border color */
2542 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2543 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2544 }
2545
2546 if (array < 0 || array >= height) useBorderColor |= K0BIT;
2547
2548 /* Fetch texels */
2549 if (useBorderColor & (I0BIT | K0BIT)) {
2550 COPY_4V(t0, tObj->BorderColor);
2551 }
2552 else {
2553 img->FetchTexelf(img, i0, array, 0, t0);
2554 }
2555 if (useBorderColor & (I1BIT | K0BIT)) {
2556 COPY_4V(t1, tObj->BorderColor);
2557 }
2558 else {
2559 img->FetchTexelf(img, i1, array, 0, t1);
2560 }
2561
2562 /* bilinear interpolation of samples */
2563 lerp_rgba(rgba, a, t0, t1);
2564 }
2565
2566
2567 static void
2568 sample_1d_array_nearest_mipmap_nearest(GLcontext *ctx,
2569 const struct gl_texture_object *tObj,
2570 GLuint n, const GLfloat texcoord[][4],
2571 const GLfloat lambda[], GLfloat rgba[][4])
2572 {
2573 GLuint i;
2574 for (i = 0; i < n; i++) {
2575 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2576 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i],
2577 rgba[i]);
2578 }
2579 }
2580
2581
2582 static void
2583 sample_1d_array_linear_mipmap_nearest(GLcontext *ctx,
2584 const struct gl_texture_object *tObj,
2585 GLuint n, const GLfloat texcoord[][4],
2586 const GLfloat lambda[], GLfloat rgba[][4])
2587 {
2588 GLuint i;
2589 ASSERT(lambda != NULL);
2590 for (i = 0; i < n; i++) {
2591 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2592 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level],
2593 texcoord[i], rgba[i]);
2594 }
2595 }
2596
2597
2598 static void
2599 sample_1d_array_nearest_mipmap_linear(GLcontext *ctx,
2600 const struct gl_texture_object *tObj,
2601 GLuint n, const GLfloat texcoord[][4],
2602 const GLfloat lambda[], GLfloat rgba[][4])
2603 {
2604 GLuint i;
2605 ASSERT(lambda != NULL);
2606 for (i = 0; i < n; i++) {
2607 GLint level = linear_mipmap_level(tObj, lambda[i]);
2608 if (level >= tObj->_MaxLevel) {
2609 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2610 texcoord[i], rgba[i]);
2611 }
2612 else {
2613 GLfloat t0[4], t1[4]; /* texels */
2614 const GLfloat f = FRAC(lambda[i]);
2615 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
2616 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
2617 lerp_rgba(rgba[i], f, t0, t1);
2618 }
2619 }
2620 }
2621
2622
2623 static void
2624 sample_1d_array_linear_mipmap_linear(GLcontext *ctx,
2625 const struct gl_texture_object *tObj,
2626 GLuint n, const GLfloat texcoord[][4],
2627 const GLfloat lambda[], GLfloat rgba[][4])
2628 {
2629 GLuint i;
2630 ASSERT(lambda != NULL);
2631 for (i = 0; i < n; i++) {
2632 GLint level = linear_mipmap_level(tObj, lambda[i]);
2633 if (level >= tObj->_MaxLevel) {
2634 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2635 texcoord[i], rgba[i]);
2636 }
2637 else {
2638 GLfloat t0[4], t1[4]; /* texels */
2639 const GLfloat f = FRAC(lambda[i]);
2640 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
2641 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
2642 lerp_rgba(rgba[i], f, t0, t1);
2643 }
2644 }
2645 }
2646
2647
2648 /** Sample 1D Array texture, nearest filtering for both min/magnification */
2649 static void
2650 sample_nearest_1d_array(GLcontext *ctx,
2651 const struct gl_texture_object *tObj, GLuint n,
2652 const GLfloat texcoords[][4], const GLfloat lambda[],
2653 GLfloat rgba[][4])
2654 {
2655 GLuint i;
2656 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2657 (void) lambda;
2658 for (i = 0; i < n; i++) {
2659 sample_1d_array_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
2660 }
2661 }
2662
2663
2664 /** Sample 1D Array texture, linear filtering for both min/magnification */
2665 static void
2666 sample_linear_1d_array(GLcontext *ctx,
2667 const struct gl_texture_object *tObj, GLuint n,
2668 const GLfloat texcoords[][4],
2669 const GLfloat lambda[], GLfloat rgba[][4])
2670 {
2671 GLuint i;
2672 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2673 (void) lambda;
2674 for (i = 0; i < n; i++) {
2675 sample_1d_array_linear(ctx, tObj, image, texcoords[i], rgba[i]);
2676 }
2677 }
2678
2679
2680 /** Sample 1D Array texture, using lambda to choose between min/magnification */
2681 static void
2682 sample_lambda_1d_array(GLcontext *ctx,
2683 const struct gl_texture_object *tObj, GLuint n,
2684 const GLfloat texcoords[][4], const GLfloat lambda[],
2685 GLfloat rgba[][4])
2686 {
2687 GLuint minStart, minEnd; /* texels with minification */
2688 GLuint magStart, magEnd; /* texels with magnification */
2689 GLuint i;
2690
2691 ASSERT(lambda != NULL);
2692 compute_min_mag_ranges(tObj, n, lambda,
2693 &minStart, &minEnd, &magStart, &magEnd);
2694
2695 if (minStart < minEnd) {
2696 /* do the minified texels */
2697 GLuint m = minEnd - minStart;
2698 switch (tObj->MinFilter) {
2699 case GL_NEAREST:
2700 for (i = minStart; i < minEnd; i++)
2701 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2702 texcoords[i], rgba[i]);
2703 break;
2704 case GL_LINEAR:
2705 for (i = minStart; i < minEnd; i++)
2706 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2707 texcoords[i], rgba[i]);
2708 break;
2709 case GL_NEAREST_MIPMAP_NEAREST:
2710 sample_1d_array_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
2711 lambda + minStart, rgba + minStart);
2712 break;
2713 case GL_LINEAR_MIPMAP_NEAREST:
2714 sample_1d_array_linear_mipmap_nearest(ctx, tObj, m,
2715 texcoords + minStart,
2716 lambda + minStart,
2717 rgba + minStart);
2718 break;
2719 case GL_NEAREST_MIPMAP_LINEAR:
2720 sample_1d_array_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
2721 lambda + minStart, rgba + minStart);
2722 break;
2723 case GL_LINEAR_MIPMAP_LINEAR:
2724 sample_1d_array_linear_mipmap_linear(ctx, tObj, m,
2725 texcoords + minStart,
2726 lambda + minStart,
2727 rgba + minStart);
2728 break;
2729 default:
2730 _mesa_problem(ctx, "Bad min filter in sample_1d_array_texture");
2731 return;
2732 }
2733 }
2734
2735 if (magStart < magEnd) {
2736 /* do the magnified texels */
2737 switch (tObj->MagFilter) {
2738 case GL_NEAREST:
2739 for (i = magStart; i < magEnd; i++)
2740 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2741 texcoords[i], rgba[i]);
2742 break;
2743 case GL_LINEAR:
2744 for (i = magStart; i < magEnd; i++)
2745 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2746 texcoords[i], rgba[i]);
2747 break;
2748 default:
2749 _mesa_problem(ctx, "Bad mag filter in sample_1d_array_texture");
2750 return;
2751 }
2752 }
2753 }
2754
2755
2756 /**
2757 * Sample a shadow/depth texture.
2758 */
2759 static void
2760 sample_depth_texture( GLcontext *ctx,
2761 const struct gl_texture_object *tObj, GLuint n,
2762 const GLfloat texcoords[][4], const GLfloat lambda[],
2763 GLfloat texel[][4] )
2764 {
2765 const GLint baseLevel = tObj->BaseLevel;
2766 const struct gl_texture_image *img = tObj->Image[0][baseLevel];
2767 const GLint width = img->Width;
2768 const GLint height = img->Height;
2769 const GLint depth = img->Depth;
2770 const GLuint compare_coord = (tObj->Target == GL_TEXTURE_2D_ARRAY_EXT)
2771 ? 3 : 2;
2772 GLfloat ambient;
2773 GLenum function;
2774 GLfloat result;
2775
2776 (void) lambda;
2777
2778 ASSERT(img->TexFormat->BaseFormat == GL_DEPTH_COMPONENT ||
2779 img->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT);
2780
2781 ASSERT(tObj->Target == GL_TEXTURE_1D ||
2782 tObj->Target == GL_TEXTURE_2D ||
2783 tObj->Target == GL_TEXTURE_RECTANGLE_NV ||
2784 tObj->Target == GL_TEXTURE_1D_ARRAY_EXT ||
2785 tObj->Target == GL_TEXTURE_2D_ARRAY_EXT);
2786
2787 ambient = tObj->CompareFailValue;
2788
2789 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
2790
2791 function = (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) ?
2792 tObj->CompareFunc : GL_NONE;
2793
2794 if (tObj->MagFilter == GL_NEAREST) {
2795 GLuint i;
2796 for (i = 0; i < n; i++) {
2797 GLfloat depthSample;
2798 GLint col, row, slice;
2799
2800 switch (tObj->Target) {
2801 case GL_TEXTURE_RECTANGLE_ARB:
2802 col = clamp_rect_coord_nearest(tObj->WrapS, texcoords[i][0], width);
2803 row = clamp_rect_coord_nearest(tObj->WrapT, texcoords[i][1], height);
2804 slice = 0;
2805 break;
2806
2807 case GL_TEXTURE_1D:
2808 col = nearest_texel_location(tObj->WrapS, img, width,
2809 texcoords[i][0]);
2810 row = 0;
2811 slice = 0;
2812 break;
2813
2814 case GL_TEXTURE_2D:
2815 col = nearest_texel_location(tObj->WrapS, img, width,
2816 texcoords[i][0]);
2817 row = nearest_texel_location(tObj->WrapT, img, height,
2818 texcoords[i][1]);
2819 slice = 0;
2820 break;
2821
2822 case GL_TEXTURE_1D_ARRAY_EXT:
2823 col = nearest_texel_location(tObj->WrapS, img, width,
2824 texcoords[i][0]);
2825 row = clamp_rect_coord_nearest(tObj->WrapT, texcoords[i][1], height);
2826 slice = 0;
2827 break;
2828
2829 case GL_TEXTURE_2D_ARRAY_EXT:
2830 col = nearest_texel_location(tObj->WrapS, img, width,
2831 texcoords[i][0]);
2832 row = nearest_texel_location(tObj->WrapT, img, height,
2833 texcoords[i][1]);
2834 slice = clamp_rect_coord_nearest(tObj->WrapR, texcoords[i][2], depth);
2835 break;
2836 default:
2837 col = row = slice = 0;
2838 }
2839
2840 if (col >= 0 && row >= 0 && col < width && row < height &&
2841 slice >= 0 && slice < depth) {
2842 img->FetchTexelf(img, col, row, slice, &depthSample);
2843 }
2844 else {
2845 depthSample = tObj->BorderColor[0];
2846 }
2847
2848 switch (function) {
2849 case GL_LEQUAL:
2850 result = (texcoords[i][compare_coord] <= depthSample) ? 1.0F : ambient;
2851 break;
2852 case GL_GEQUAL:
2853 result = (texcoords[i][compare_coord] >= depthSample) ? 1.0F : ambient;
2854 break;
2855 case GL_LESS:
2856 result = (texcoords[i][compare_coord] < depthSample) ? 1.0F : ambient;
2857 break;
2858 case GL_GREATER:
2859 result = (texcoords[i][compare_coord] > depthSample) ? 1.0F : ambient;
2860 break;
2861 case GL_EQUAL:
2862 result = (texcoords[i][compare_coord] == depthSample) ? 1.0F : ambient;
2863 break;
2864 case GL_NOTEQUAL:
2865 result = (texcoords[i][compare_coord] != depthSample) ? 1.0F : ambient;
2866 break;
2867 case GL_ALWAYS:
2868 result = 1.0F;
2869 break;
2870 case GL_NEVER:
2871 result = ambient;
2872 break;
2873 case GL_NONE:
2874 result = depthSample;
2875 break;
2876 default:
2877 _mesa_problem(ctx, "Bad compare func in sample_depth_texture");
2878 return;
2879 }
2880
2881 switch (tObj->DepthMode) {
2882 case GL_LUMINANCE:
2883 ASSIGN_4V(texel[i], result, result, result, 1.0F);
2884 break;
2885 case GL_INTENSITY:
2886 ASSIGN_4V(texel[i], result, result, result, result);
2887 break;
2888 case GL_ALPHA:
2889 ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
2890 break;
2891 default:
2892 _mesa_problem(ctx, "Bad depth texture mode");
2893 }
2894 }
2895 }
2896 else {
2897 GLuint i;
2898 ASSERT(tObj->MagFilter == GL_LINEAR);
2899 for (i = 0; i < n; i++) {
2900 GLfloat depth00, depth01, depth10, depth11;
2901 GLint i0, i1, j0, j1;
2902 GLint slice;
2903 GLfloat a, b;
2904 GLuint useBorderTexel;
2905
2906 switch (tObj->Target) {
2907 case GL_TEXTURE_RECTANGLE_ARB:
2908 clamp_rect_coord_linear(tObj->WrapS, texcoords[i][0],
2909 width, &i0, &i1, &a);
2910 clamp_rect_coord_linear(tObj->WrapT, texcoords[i][1],
2911 height, &j0, &j1, &b);
2912 slice = 0;
2913 break;
2914
2915 case GL_TEXTURE_1D:
2916 case GL_TEXTURE_2D:
2917 linear_texel_locations(tObj->WrapS, img, width,
2918 texcoords[i][0], &i0, &i1, &a);
2919 linear_texel_locations(tObj->WrapT, img, height,
2920 texcoords[i][1], &j0, &j1, &b);
2921 slice = 0;
2922 break;
2923
2924 case GL_TEXTURE_1D_ARRAY_EXT:
2925 linear_texel_locations(tObj->WrapS, img, width,
2926 texcoords[i][0], &i0, &i1, &a);
2927 j0 = clamp_rect_coord_nearest(tObj->WrapT, texcoords[i][1], height);
2928 j1 = j0;
2929 slice = 0;
2930 break;
2931
2932 case GL_TEXTURE_2D_ARRAY_EXT:
2933 linear_texel_locations(tObj->WrapS, img, width,
2934 texcoords[i][0], &i0, &i1, &a);
2935 linear_texel_locations(tObj->WrapT, img, height,
2936 texcoords[i][1], &j0, &j1, &b);
2937 slice = clamp_rect_coord_nearest(tObj->WrapR, texcoords[i][2], depth);
2938 break;
2939 default:
2940 slice = 0;
2941 }
2942
2943 useBorderTexel = 0;
2944 if (img->Border) {
2945 i0 += img->Border;
2946 i1 += img->Border;
2947 if (tObj->Target != GL_TEXTURE_1D_ARRAY_EXT) {
2948 j0 += img->Border;
2949 j1 += img->Border;
2950 }
2951 }
2952 else {
2953 if (i0 < 0 || i0 >= (GLint) width) useBorderTexel |= I0BIT;
2954 if (i1 < 0 || i1 >= (GLint) width) useBorderTexel |= I1BIT;
2955 if (j0 < 0 || j0 >= (GLint) height) useBorderTexel |= J0BIT;
2956 if (j1 < 0 || j1 >= (GLint) height) useBorderTexel |= J1BIT;
2957 }
2958
2959 if (slice < 0 || slice >= (GLint) depth) {
2960 depth00 = tObj->BorderColor[0];
2961 depth01 = tObj->BorderColor[0];
2962 depth10 = tObj->BorderColor[0];
2963 depth11 = tObj->BorderColor[0];
2964 }
2965 else {
2966 /* get four depth samples from the texture */
2967 if (useBorderTexel & (I0BIT | J0BIT)) {
2968 depth00 = tObj->BorderColor[0];
2969 }
2970 else {
2971 img->FetchTexelf(img, i0, j0, slice, &depth00);
2972 }
2973 if (useBorderTexel & (I1BIT | J0BIT)) {
2974 depth10 = tObj->BorderColor[0];
2975 }
2976 else {
2977 img->FetchTexelf(img, i1, j0, slice, &depth10);
2978 }
2979
2980 if (tObj->Target != GL_TEXTURE_1D_ARRAY_EXT) {
2981 if (useBorderTexel & (I0BIT | J1BIT)) {
2982 depth01 = tObj->BorderColor[0];
2983 }
2984 else {
2985 img->FetchTexelf(img, i0, j1, slice, &depth01);
2986 }
2987 if (useBorderTexel & (I1BIT | J1BIT)) {
2988 depth11 = tObj->BorderColor[0];
2989 }
2990 else {
2991 img->FetchTexelf(img, i1, j1, slice, &depth11);
2992 }
2993 }
2994 else {
2995 depth01 = depth00;
2996 depth11 = depth10;
2997 }
2998 }
2999
3000 if (0) {
3001 /* compute a single weighted depth sample and do one comparison */
3002 const GLfloat depthSample
3003 = lerp_2d(a, b, depth00, depth10, depth01, depth11);
3004 if ((depthSample <= texcoords[i][compare_coord] && function == GL_LEQUAL) ||
3005 (depthSample >= texcoords[i][compare_coord] && function == GL_GEQUAL)) {
3006 result = ambient;
3007 }
3008 else {
3009 result = CHAN_MAX;
3010 }
3011 }
3012 else {
3013 /* Do four depth/R comparisons and compute a weighted result.
3014 * If this touches on somebody's I.P., I'll remove this code
3015 * upon request.
3016 */
3017 const GLfloat d = (CHAN_MAXF - (GLfloat) ambient) * 0.25F;
3018 GLfloat luminance = CHAN_MAXF;
3019
3020 switch (function) {
3021 case GL_LEQUAL:
3022 if (depth00 <= texcoords[i][compare_coord]) luminance -= d;
3023 if (depth01 <= texcoords[i][compare_coord]) luminance -= d;
3024 if (depth10 <= texcoords[i][compare_coord]) luminance -= d;
3025 if (depth11 <= texcoords[i][compare_coord]) luminance -= d;
3026 result = (GLfloat) luminance;
3027 break;
3028 case GL_GEQUAL:
3029 if (depth00 >= texcoords[i][compare_coord]) luminance -= d;
3030 if (depth01 >= texcoords[i][compare_coord]) luminance -= d;
3031 if (depth10 >= texcoords[i][compare_coord]) luminance -= d;
3032 if (depth11 >= texcoords[i][compare_coord]) luminance -= d;
3033 result = (GLfloat) luminance;
3034 break;
3035 case GL_LESS:
3036 if (depth00 < texcoords[i][compare_coord]) luminance -= d;
3037 if (depth01 < texcoords[i][compare_coord]) luminance -= d;
3038 if (depth10 < texcoords[i][compare_coord]) luminance -= d;
3039 if (depth11 < texcoords[i][compare_coord]) luminance -= d;
3040 result = (GLfloat) luminance;
3041 break;
3042 case GL_GREATER:
3043 if (depth00 > texcoords[i][compare_coord]) luminance -= d;
3044 if (depth01 > texcoords[i][compare_coord]) luminance -= d;
3045 if (depth10 > texcoords[i][compare_coord]) luminance -= d;
3046 if (depth11 > texcoords[i][compare_coord]) luminance -= d;
3047 result = (GLfloat) luminance;
3048 break;
3049 case GL_EQUAL:
3050 if (depth00 == texcoords[i][compare_coord]) luminance -= d;
3051 if (depth01 == texcoords[i][compare_coord]) luminance -= d;
3052 if (depth10 == texcoords[i][compare_coord]) luminance -= d;
3053 if (depth11 == texcoords[i][compare_coord]) luminance -= d;
3054 result = (GLfloat) luminance;
3055 break;
3056 case GL_NOTEQUAL:
3057 if (depth00 != texcoords[i][compare_coord]) luminance -= d;
3058 if (depth01 != texcoords[i][compare_coord]) luminance -= d;
3059 if (depth10 != texcoords[i][compare_coord]) luminance -= d;
3060 if (depth11 != texcoords[i][compare_coord]) luminance -= d;
3061 result = (GLfloat) luminance;
3062 break;
3063 case GL_ALWAYS:
3064 result = 0;
3065 break;
3066 case GL_NEVER:
3067 result = CHAN_MAX;
3068 break;
3069 case GL_NONE:
3070 /* ordinary bilinear filtering */
3071 {
3072 const GLfloat depthSample
3073 = lerp_2d(a, b, depth00, depth10, depth01, depth11);
3074 CLAMPED_FLOAT_TO_CHAN(result, depthSample);
3075 }
3076 break;
3077 default:
3078 _mesa_problem(ctx, "Bad compare func in sample_depth_texture");
3079 return;
3080 }
3081 }
3082
3083 switch (tObj->DepthMode) {
3084 case GL_LUMINANCE:
3085 texel[i][RCOMP] = result;
3086 texel[i][GCOMP] = result;
3087 texel[i][BCOMP] = result;
3088 texel[i][ACOMP] = CHAN_MAX;
3089 break;
3090 case GL_INTENSITY:
3091 texel[i][RCOMP] = result;
3092 texel[i][GCOMP] = result;
3093 texel[i][BCOMP] = result;
3094 texel[i][ACOMP] = result;
3095 break;
3096 case GL_ALPHA:
3097 texel[i][RCOMP] = 0;
3098 texel[i][GCOMP] = 0;
3099 texel[i][BCOMP] = 0;
3100 texel[i][ACOMP] = result;
3101 break;
3102 default:
3103 _mesa_problem(ctx, "Bad depth texture mode");
3104 }
3105 } /* for */
3106 } /* if filter */
3107 }
3108
3109
3110 /**
3111 * We use this function when a texture object is in an "incomplete" state.
3112 * When a fragment program attempts to sample an incomplete texture we
3113 * return black (see issue 23 in GL_ARB_fragment_program spec).
3114 * Note: fragment programs don't observe the texture enable/disable flags.
3115 */
3116 static void
3117 null_sample_func( GLcontext *ctx,
3118 const struct gl_texture_object *tObj, GLuint n,
3119 const GLfloat texcoords[][4], const GLfloat lambda[],
3120 GLfloat rgba[][4])
3121 {
3122 GLuint i;
3123 (void) ctx;
3124 (void) tObj;
3125 (void) texcoords;
3126 (void) lambda;
3127 for (i = 0; i < n; i++) {
3128 rgba[i][RCOMP] = 0;
3129 rgba[i][GCOMP] = 0;
3130 rgba[i][BCOMP] = 0;
3131 rgba[i][ACOMP] = CHAN_MAX;
3132 }
3133 }
3134
3135
3136 /**
3137 * Choose the texture sampling function for the given texture object.
3138 */
3139 texture_sample_func
3140 _swrast_choose_texture_sample_func( GLcontext *ctx,
3141 const struct gl_texture_object *t )
3142 {
3143 if (!t || !t->_Complete) {
3144 return &null_sample_func;
3145 }
3146 else {
3147 const GLboolean needLambda = (GLboolean) (t->MinFilter != t->MagFilter);
3148 const GLenum format = t->Image[0][t->BaseLevel]->TexFormat->BaseFormat;
3149
3150 switch (t->Target) {
3151 case GL_TEXTURE_1D:
3152 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3153 return &sample_depth_texture;
3154 }
3155 else if (needLambda) {
3156 return &sample_lambda_1d;
3157 }
3158 else if (t->MinFilter == GL_LINEAR) {
3159 return &sample_linear_1d;
3160 }
3161 else {
3162 ASSERT(t->MinFilter == GL_NEAREST);
3163 return &sample_nearest_1d;
3164 }
3165 case GL_TEXTURE_2D:
3166 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3167 return &sample_depth_texture;
3168 }
3169 else if (needLambda) {
3170 return &sample_lambda_2d;
3171 }
3172 else if (t->MinFilter == GL_LINEAR) {
3173 return &sample_linear_2d;
3174 }
3175 else {
3176 /* check for a few optimized cases */
3177 #if 0
3178 const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
3179 ASSERT(t->MinFilter == GL_NEAREST);
3180 if (t->WrapS == GL_REPEAT &&
3181 t->WrapT == GL_REPEAT &&
3182 img->_IsPowerOfTwo &&
3183 img->Border == 0 &&
3184 img->TexFormat->MesaFormat == MESA_FORMAT_RGB) {
3185 return &opt_sample_rgb_2d;
3186 }
3187 else if (t->WrapS == GL_REPEAT &&
3188 t->WrapT == GL_REPEAT &&
3189 img->_IsPowerOfTwo &&
3190 img->Border == 0 &&
3191 img->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
3192 return &opt_sample_rgba_2d;
3193 }
3194 #else
3195 if (0)
3196 ;
3197 #endif
3198 else {
3199 return &sample_nearest_2d;
3200 }
3201 }
3202 case GL_TEXTURE_3D:
3203 if (needLambda) {
3204 return &sample_lambda_3d;
3205 }
3206 else if (t->MinFilter == GL_LINEAR) {
3207 return &sample_linear_3d;
3208 }
3209 else {
3210 ASSERT(t->MinFilter == GL_NEAREST);
3211 return &sample_nearest_3d;
3212 }
3213 case GL_TEXTURE_CUBE_MAP:
3214 if (needLambda) {
3215 return &sample_lambda_cube;
3216 }
3217 else if (t->MinFilter == GL_LINEAR) {
3218 return &sample_linear_cube;
3219 }
3220 else {
3221 ASSERT(t->MinFilter == GL_NEAREST);
3222 return &sample_nearest_cube;
3223 }
3224 case GL_TEXTURE_RECTANGLE_NV:
3225 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3226 return &sample_depth_texture;
3227 }
3228 else if (needLambda) {
3229 return &sample_lambda_rect;
3230 }
3231 else if (t->MinFilter == GL_LINEAR) {
3232 return &sample_linear_rect;
3233 }
3234 else {
3235 ASSERT(t->MinFilter == GL_NEAREST);
3236 return &sample_nearest_rect;
3237 }
3238 case GL_TEXTURE_1D_ARRAY_EXT:
3239 if (needLambda) {
3240 return &sample_lambda_1d_array;
3241 }
3242 else if (t->MinFilter == GL_LINEAR) {
3243 return &sample_linear_1d_array;
3244 }
3245 else {
3246 ASSERT(t->MinFilter == GL_NEAREST);
3247 return &sample_nearest_1d_array;
3248 }
3249 case GL_TEXTURE_2D_ARRAY_EXT:
3250 if (needLambda) {
3251 return &sample_lambda_2d_array;
3252 }
3253 else if (t->MinFilter == GL_LINEAR) {
3254 return &sample_linear_2d_array;
3255 }
3256 else {
3257 ASSERT(t->MinFilter == GL_NEAREST);
3258 return &sample_nearest_2d_array;
3259 }
3260 default:
3261 _mesa_problem(ctx,
3262 "invalid target in _swrast_choose_texture_sample_func");
3263 return &null_sample_func;
3264 }
3265 }
3266 }