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