Merge branch 'radeon-rewrite' of git+ssh://agd5f@git.freedesktop.org/git/mesa/mesa...
[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 * When we sample the border color, it must be interpreted according to
739 * the base texture format. Ex: if the texture base format it GL_ALPHA,
740 * we return (0,0,0,BorderAlpha).
741 */
742 static INLINE void
743 get_border_color(const struct gl_texture_object *tObj,
744 const struct gl_texture_image *img,
745 GLfloat rgba[4])
746 {
747 switch (img->TexFormat->BaseFormat) {
748 case GL_RGB:
749 rgba[0] = tObj->BorderColor[0];
750 rgba[1] = tObj->BorderColor[1];
751 rgba[2] = tObj->BorderColor[2];
752 rgba[3] = 1.0F;
753 break;
754 case GL_ALPHA:
755 rgba[0] = rgba[1] = rgba[2] = 0.0;
756 rgba[3] = tObj->BorderColor[3];
757 break;
758 case GL_LUMINANCE:
759 rgba[0] = rgba[1] = rgba[2] = tObj->BorderColor[0];
760 rgba[3] = 1.0;
761 break;
762 case GL_LUMINANCE_ALPHA:
763 rgba[0] = rgba[1] = rgba[2] = tObj->BorderColor[0];
764 rgba[3] = tObj->BorderColor[3];
765 break;
766 case GL_INTENSITY:
767 rgba[0] = rgba[1] = rgba[2] = rgba[3] = tObj->BorderColor[0];
768 break;
769 default:
770 COPY_4V(rgba, tObj->BorderColor);
771 }
772 }
773
774
775 /**********************************************************************/
776 /* 1-D Texture Sampling Functions */
777 /**********************************************************************/
778
779 /**
780 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
781 */
782 static INLINE void
783 sample_1d_nearest(GLcontext *ctx,
784 const struct gl_texture_object *tObj,
785 const struct gl_texture_image *img,
786 const GLfloat texcoord[4], GLfloat rgba[4])
787 {
788 const GLint width = img->Width2; /* without border, power of two */
789 GLint i;
790 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
791 /* skip over the border, if any */
792 i += img->Border;
793 if (i < 0 || i >= (GLint) img->Width) {
794 /* Need this test for GL_CLAMP_TO_BORDER mode */
795 get_border_color(tObj, img, rgba);
796 }
797 else {
798 img->FetchTexelf(img, i, 0, 0, rgba);
799 }
800 }
801
802
803 /**
804 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
805 */
806 static INLINE void
807 sample_1d_linear(GLcontext *ctx,
808 const struct gl_texture_object *tObj,
809 const struct gl_texture_image *img,
810 const GLfloat texcoord[4], GLfloat rgba[4])
811 {
812 const GLint width = img->Width2;
813 GLint i0, i1;
814 GLbitfield useBorderColor = 0x0;
815 GLfloat a;
816 GLfloat t0[4], t1[4]; /* texels */
817
818 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
819
820 if (img->Border) {
821 i0 += img->Border;
822 i1 += img->Border;
823 }
824 else {
825 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
826 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
827 }
828
829 /* fetch texel colors */
830 if (useBorderColor & I0BIT) {
831 get_border_color(tObj, img, t0);
832 }
833 else {
834 img->FetchTexelf(img, i0, 0, 0, t0);
835 }
836 if (useBorderColor & I1BIT) {
837 get_border_color(tObj, img, t1);
838 }
839 else {
840 img->FetchTexelf(img, i1, 0, 0, t1);
841 }
842
843 lerp_rgba(rgba, a, t0, t1);
844 }
845
846
847 static void
848 sample_1d_nearest_mipmap_nearest(GLcontext *ctx,
849 const struct gl_texture_object *tObj,
850 GLuint n, const GLfloat texcoord[][4],
851 const GLfloat lambda[], GLfloat rgba[][4])
852 {
853 GLuint i;
854 ASSERT(lambda != NULL);
855 for (i = 0; i < n; i++) {
856 GLint level = nearest_mipmap_level(tObj, lambda[i]);
857 sample_1d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
858 }
859 }
860
861
862 static void
863 sample_1d_linear_mipmap_nearest(GLcontext *ctx,
864 const struct gl_texture_object *tObj,
865 GLuint n, const GLfloat texcoord[][4],
866 const GLfloat lambda[], GLfloat rgba[][4])
867 {
868 GLuint i;
869 ASSERT(lambda != NULL);
870 for (i = 0; i < n; i++) {
871 GLint level = nearest_mipmap_level(tObj, lambda[i]);
872 sample_1d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
873 }
874 }
875
876
877 static void
878 sample_1d_nearest_mipmap_linear(GLcontext *ctx,
879 const struct gl_texture_object *tObj,
880 GLuint n, const GLfloat texcoord[][4],
881 const GLfloat lambda[], GLfloat rgba[][4])
882 {
883 GLuint i;
884 ASSERT(lambda != NULL);
885 for (i = 0; i < n; i++) {
886 GLint level = linear_mipmap_level(tObj, lambda[i]);
887 if (level >= tObj->_MaxLevel) {
888 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
889 texcoord[i], rgba[i]);
890 }
891 else {
892 GLfloat t0[4], t1[4];
893 const GLfloat f = FRAC(lambda[i]);
894 sample_1d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
895 sample_1d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
896 lerp_rgba(rgba[i], f, t0, t1);
897 }
898 }
899 }
900
901
902 static void
903 sample_1d_linear_mipmap_linear(GLcontext *ctx,
904 const struct gl_texture_object *tObj,
905 GLuint n, const GLfloat texcoord[][4],
906 const GLfloat lambda[], GLfloat rgba[][4])
907 {
908 GLuint i;
909 ASSERT(lambda != NULL);
910 for (i = 0; i < n; i++) {
911 GLint level = linear_mipmap_level(tObj, lambda[i]);
912 if (level >= tObj->_MaxLevel) {
913 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
914 texcoord[i], rgba[i]);
915 }
916 else {
917 GLfloat t0[4], t1[4];
918 const GLfloat f = FRAC(lambda[i]);
919 sample_1d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
920 sample_1d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
921 lerp_rgba(rgba[i], f, t0, t1);
922 }
923 }
924 }
925
926
927 /** Sample 1D texture, nearest filtering for both min/magnification */
928 static void
929 sample_nearest_1d( GLcontext *ctx,
930 const struct gl_texture_object *tObj, GLuint n,
931 const GLfloat texcoords[][4], const GLfloat lambda[],
932 GLfloat rgba[][4] )
933 {
934 GLuint i;
935 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
936 (void) lambda;
937 for (i = 0; i < n; i++) {
938 sample_1d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
939 }
940 }
941
942
943 /** Sample 1D texture, linear filtering for both min/magnification */
944 static void
945 sample_linear_1d( GLcontext *ctx,
946 const struct gl_texture_object *tObj, GLuint n,
947 const GLfloat texcoords[][4], const GLfloat lambda[],
948 GLfloat rgba[][4] )
949 {
950 GLuint i;
951 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
952 (void) lambda;
953 for (i = 0; i < n; i++) {
954 sample_1d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
955 }
956 }
957
958
959 /** Sample 1D texture, using lambda to choose between min/magnification */
960 static void
961 sample_lambda_1d( GLcontext *ctx,
962 const struct gl_texture_object *tObj, GLuint n,
963 const GLfloat texcoords[][4],
964 const GLfloat lambda[], GLfloat rgba[][4] )
965 {
966 GLuint minStart, minEnd; /* texels with minification */
967 GLuint magStart, magEnd; /* texels with magnification */
968 GLuint i;
969
970 ASSERT(lambda != NULL);
971 compute_min_mag_ranges(tObj, n, lambda,
972 &minStart, &minEnd, &magStart, &magEnd);
973
974 if (minStart < minEnd) {
975 /* do the minified texels */
976 const GLuint m = minEnd - minStart;
977 switch (tObj->MinFilter) {
978 case GL_NEAREST:
979 for (i = minStart; i < minEnd; i++)
980 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
981 texcoords[i], rgba[i]);
982 break;
983 case GL_LINEAR:
984 for (i = minStart; i < minEnd; i++)
985 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
986 texcoords[i], rgba[i]);
987 break;
988 case GL_NEAREST_MIPMAP_NEAREST:
989 sample_1d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
990 lambda + minStart, rgba + minStart);
991 break;
992 case GL_LINEAR_MIPMAP_NEAREST:
993 sample_1d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
994 lambda + minStart, rgba + minStart);
995 break;
996 case GL_NEAREST_MIPMAP_LINEAR:
997 sample_1d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
998 lambda + minStart, rgba + minStart);
999 break;
1000 case GL_LINEAR_MIPMAP_LINEAR:
1001 sample_1d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1002 lambda + minStart, rgba + minStart);
1003 break;
1004 default:
1005 _mesa_problem(ctx, "Bad min filter in sample_1d_texture");
1006 return;
1007 }
1008 }
1009
1010 if (magStart < magEnd) {
1011 /* do the magnified texels */
1012 switch (tObj->MagFilter) {
1013 case GL_NEAREST:
1014 for (i = magStart; i < magEnd; i++)
1015 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1016 texcoords[i], rgba[i]);
1017 break;
1018 case GL_LINEAR:
1019 for (i = magStart; i < magEnd; i++)
1020 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1021 texcoords[i], rgba[i]);
1022 break;
1023 default:
1024 _mesa_problem(ctx, "Bad mag filter in sample_1d_texture");
1025 return;
1026 }
1027 }
1028 }
1029
1030
1031 /**********************************************************************/
1032 /* 2-D Texture Sampling Functions */
1033 /**********************************************************************/
1034
1035
1036 /**
1037 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1038 */
1039 static INLINE void
1040 sample_2d_nearest(GLcontext *ctx,
1041 const struct gl_texture_object *tObj,
1042 const struct gl_texture_image *img,
1043 const GLfloat texcoord[4],
1044 GLfloat rgba[])
1045 {
1046 const GLint width = img->Width2; /* without border, power of two */
1047 const GLint height = img->Height2; /* without border, power of two */
1048 GLint i, j;
1049 (void) ctx;
1050
1051 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
1052 j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
1053
1054 /* skip over the border, if any */
1055 i += img->Border;
1056 j += img->Border;
1057
1058 if (i < 0 || i >= (GLint) img->Width || j < 0 || j >= (GLint) img->Height) {
1059 /* Need this test for GL_CLAMP_TO_BORDER mode */
1060 get_border_color(tObj, img, rgba);
1061 }
1062 else {
1063 img->FetchTexelf(img, i, j, 0, rgba);
1064 }
1065 }
1066
1067
1068 /**
1069 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1070 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1071 */
1072 static INLINE void
1073 sample_2d_linear(GLcontext *ctx,
1074 const struct gl_texture_object *tObj,
1075 const struct gl_texture_image *img,
1076 const GLfloat texcoord[4],
1077 GLfloat rgba[])
1078 {
1079 const GLint width = img->Width2;
1080 const GLint height = img->Height2;
1081 GLint i0, j0, i1, j1;
1082 GLbitfield useBorderColor = 0x0;
1083 GLfloat a, b;
1084 GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
1085
1086 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
1087 linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
1088
1089 if (img->Border) {
1090 i0 += img->Border;
1091 i1 += img->Border;
1092 j0 += img->Border;
1093 j1 += img->Border;
1094 }
1095 else {
1096 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
1097 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
1098 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
1099 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
1100 }
1101
1102 /* fetch four texel colors */
1103 if (useBorderColor & (I0BIT | J0BIT)) {
1104 get_border_color(tObj, img, t00);
1105 }
1106 else {
1107 img->FetchTexelf(img, i0, j0, 0, t00);
1108 }
1109 if (useBorderColor & (I1BIT | J0BIT)) {
1110 get_border_color(tObj, img, t10);
1111 }
1112 else {
1113 img->FetchTexelf(img, i1, j0, 0, t10);
1114 }
1115 if (useBorderColor & (I0BIT | J1BIT)) {
1116 get_border_color(tObj, img, t01);
1117 }
1118 else {
1119 img->FetchTexelf(img, i0, j1, 0, t01);
1120 }
1121 if (useBorderColor & (I1BIT | J1BIT)) {
1122 get_border_color(tObj, img, t11);
1123 }
1124 else {
1125 img->FetchTexelf(img, i1, j1, 0, t11);
1126 }
1127
1128 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
1129 }
1130
1131
1132 /**
1133 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1134 * We don't have to worry about the texture border.
1135 */
1136 static INLINE void
1137 sample_2d_linear_repeat(GLcontext *ctx,
1138 const struct gl_texture_object *tObj,
1139 const struct gl_texture_image *img,
1140 const GLfloat texcoord[4],
1141 GLfloat rgba[])
1142 {
1143 const GLint width = img->Width2;
1144 const GLint height = img->Height2;
1145 GLint i0, j0, i1, j1;
1146 GLfloat wi, wj;
1147 GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
1148
1149 (void) ctx;
1150
1151 ASSERT(tObj->WrapS == GL_REPEAT);
1152 ASSERT(tObj->WrapT == GL_REPEAT);
1153 ASSERT(img->Border == 0);
1154 ASSERT(img->TexFormat->BaseFormat != GL_COLOR_INDEX);
1155 ASSERT(img->_IsPowerOfTwo);
1156
1157 linear_repeat_texel_location(width, texcoord[0], &i0, &i1, &wi);
1158 linear_repeat_texel_location(height, texcoord[1], &j0, &j1, &wj);
1159
1160 img->FetchTexelf(img, i0, j0, 0, t00);
1161 img->FetchTexelf(img, i1, j0, 0, t10);
1162 img->FetchTexelf(img, i0, j1, 0, t01);
1163 img->FetchTexelf(img, i1, j1, 0, t11);
1164
1165 lerp_rgba_2d(rgba, wi, wj, t00, t10, t01, t11);
1166 }
1167
1168
1169 static void
1170 sample_2d_nearest_mipmap_nearest(GLcontext *ctx,
1171 const struct gl_texture_object *tObj,
1172 GLuint n, const GLfloat texcoord[][4],
1173 const GLfloat lambda[], GLfloat rgba[][4])
1174 {
1175 GLuint i;
1176 for (i = 0; i < n; i++) {
1177 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1178 sample_2d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1179 }
1180 }
1181
1182
1183 static void
1184 sample_2d_linear_mipmap_nearest(GLcontext *ctx,
1185 const struct gl_texture_object *tObj,
1186 GLuint n, const GLfloat texcoord[][4],
1187 const GLfloat lambda[], GLfloat rgba[][4])
1188 {
1189 GLuint i;
1190 ASSERT(lambda != NULL);
1191 for (i = 0; i < n; i++) {
1192 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1193 sample_2d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1194 }
1195 }
1196
1197
1198 static void
1199 sample_2d_nearest_mipmap_linear(GLcontext *ctx,
1200 const struct gl_texture_object *tObj,
1201 GLuint n, const GLfloat texcoord[][4],
1202 const GLfloat lambda[], GLfloat rgba[][4])
1203 {
1204 GLuint i;
1205 ASSERT(lambda != NULL);
1206 for (i = 0; i < n; i++) {
1207 GLint level = linear_mipmap_level(tObj, lambda[i]);
1208 if (level >= tObj->_MaxLevel) {
1209 sample_2d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1210 texcoord[i], rgba[i]);
1211 }
1212 else {
1213 GLfloat t0[4], t1[4]; /* texels */
1214 const GLfloat f = FRAC(lambda[i]);
1215 sample_2d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1216 sample_2d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1217 lerp_rgba(rgba[i], f, t0, t1);
1218 }
1219 }
1220 }
1221
1222
1223 static void
1224 sample_2d_linear_mipmap_linear( GLcontext *ctx,
1225 const struct gl_texture_object *tObj,
1226 GLuint n, const GLfloat texcoord[][4],
1227 const GLfloat lambda[], GLfloat rgba[][4] )
1228 {
1229 GLuint i;
1230 ASSERT(lambda != NULL);
1231 for (i = 0; i < n; i++) {
1232 GLint level = linear_mipmap_level(tObj, lambda[i]);
1233 if (level >= tObj->_MaxLevel) {
1234 sample_2d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1235 texcoord[i], rgba[i]);
1236 }
1237 else {
1238 GLfloat t0[4], t1[4]; /* texels */
1239 const GLfloat f = FRAC(lambda[i]);
1240 sample_2d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1241 sample_2d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1242 lerp_rgba(rgba[i], f, t0, t1);
1243 }
1244 }
1245 }
1246
1247
1248 static void
1249 sample_2d_linear_mipmap_linear_repeat(GLcontext *ctx,
1250 const struct gl_texture_object *tObj,
1251 GLuint n, const GLfloat texcoord[][4],
1252 const GLfloat lambda[], GLfloat rgba[][4])
1253 {
1254 GLuint i;
1255 ASSERT(lambda != NULL);
1256 ASSERT(tObj->WrapS == GL_REPEAT);
1257 ASSERT(tObj->WrapT == GL_REPEAT);
1258 for (i = 0; i < n; i++) {
1259 GLint level = linear_mipmap_level(tObj, lambda[i]);
1260 if (level >= tObj->_MaxLevel) {
1261 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1262 texcoord[i], rgba[i]);
1263 }
1264 else {
1265 GLfloat t0[4], t1[4]; /* texels */
1266 const GLfloat f = FRAC(lambda[i]);
1267 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level ],
1268 texcoord[i], t0);
1269 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level+1],
1270 texcoord[i], t1);
1271 lerp_rgba(rgba[i], f, t0, t1);
1272 }
1273 }
1274 }
1275
1276
1277 /** Sample 2D texture, nearest filtering for both min/magnification */
1278 static void
1279 sample_nearest_2d(GLcontext *ctx,
1280 const struct gl_texture_object *tObj, GLuint n,
1281 const GLfloat texcoords[][4],
1282 const GLfloat lambda[], GLfloat rgba[][4])
1283 {
1284 GLuint i;
1285 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1286 (void) lambda;
1287 for (i = 0; i < n; i++) {
1288 sample_2d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
1289 }
1290 }
1291
1292
1293 /** Sample 2D texture, linear filtering for both min/magnification */
1294 static void
1295 sample_linear_2d(GLcontext *ctx,
1296 const struct gl_texture_object *tObj, GLuint n,
1297 const GLfloat texcoords[][4],
1298 const GLfloat lambda[], GLfloat rgba[][4])
1299 {
1300 GLuint i;
1301 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1302 (void) lambda;
1303 if (tObj->WrapS == GL_REPEAT &&
1304 tObj->WrapT == GL_REPEAT &&
1305 image->_IsPowerOfTwo &&
1306 image->Border == 0) {
1307 for (i = 0; i < n; i++) {
1308 sample_2d_linear_repeat(ctx, tObj, image, texcoords[i], rgba[i]);
1309 }
1310 }
1311 else {
1312 for (i = 0; i < n; i++) {
1313 sample_2d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
1314 }
1315 }
1316 }
1317
1318
1319 /**
1320 * Optimized 2-D texture sampling:
1321 * S and T wrap mode == GL_REPEAT
1322 * GL_NEAREST min/mag filter
1323 * No border,
1324 * RowStride == Width,
1325 * Format = GL_RGB
1326 */
1327 static void
1328 opt_sample_rgb_2d(GLcontext *ctx,
1329 const struct gl_texture_object *tObj,
1330 GLuint n, const GLfloat texcoords[][4],
1331 const GLfloat lambda[], GLchan rgba[][4])
1332 {
1333 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1334 const GLfloat width = (GLfloat) img->Width;
1335 const GLfloat height = (GLfloat) img->Height;
1336 const GLint colMask = img->Width - 1;
1337 const GLint rowMask = img->Height - 1;
1338 const GLint shift = img->WidthLog2;
1339 GLuint k;
1340 (void) ctx;
1341 (void) lambda;
1342 ASSERT(tObj->WrapS==GL_REPEAT);
1343 ASSERT(tObj->WrapT==GL_REPEAT);
1344 ASSERT(img->Border==0);
1345 ASSERT(img->TexFormat->MesaFormat==MESA_FORMAT_RGB);
1346 ASSERT(img->_IsPowerOfTwo);
1347
1348 for (k=0; k<n; k++) {
1349 GLint i = IFLOOR(texcoords[k][0] * width) & colMask;
1350 GLint j = IFLOOR(texcoords[k][1] * height) & rowMask;
1351 GLint pos = (j << shift) | i;
1352 GLchan *texel = ((GLchan *) img->Data) + 3*pos;
1353 rgba[k][RCOMP] = texel[0];
1354 rgba[k][GCOMP] = texel[1];
1355 rgba[k][BCOMP] = texel[2];
1356 }
1357 }
1358
1359
1360 /**
1361 * Optimized 2-D texture sampling:
1362 * S and T wrap mode == GL_REPEAT
1363 * GL_NEAREST min/mag filter
1364 * No border
1365 * RowStride == Width,
1366 * Format = GL_RGBA
1367 */
1368 static void
1369 opt_sample_rgba_2d(GLcontext *ctx,
1370 const struct gl_texture_object *tObj,
1371 GLuint n, const GLfloat texcoords[][4],
1372 const GLfloat lambda[], GLchan rgba[][4])
1373 {
1374 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1375 const GLfloat width = (GLfloat) img->Width;
1376 const GLfloat height = (GLfloat) img->Height;
1377 const GLint colMask = img->Width - 1;
1378 const GLint rowMask = img->Height - 1;
1379 const GLint shift = img->WidthLog2;
1380 GLuint i;
1381 (void) ctx;
1382 (void) lambda;
1383 ASSERT(tObj->WrapS==GL_REPEAT);
1384 ASSERT(tObj->WrapT==GL_REPEAT);
1385 ASSERT(img->Border==0);
1386 ASSERT(img->TexFormat->MesaFormat==MESA_FORMAT_RGBA);
1387 ASSERT(img->_IsPowerOfTwo);
1388
1389 for (i = 0; i < n; i++) {
1390 const GLint col = IFLOOR(texcoords[i][0] * width) & colMask;
1391 const GLint row = IFLOOR(texcoords[i][1] * height) & rowMask;
1392 const GLint pos = (row << shift) | col;
1393 const GLchan *texel = ((GLchan *) img->Data) + (pos << 2); /* pos*4 */
1394 COPY_4V(rgba[i], texel);
1395 }
1396 }
1397
1398
1399 /** Sample 2D texture, using lambda to choose between min/magnification */
1400 static void
1401 sample_lambda_2d(GLcontext *ctx,
1402 const struct gl_texture_object *tObj,
1403 GLuint n, const GLfloat texcoords[][4],
1404 const GLfloat lambda[], GLfloat rgba[][4])
1405 {
1406 const struct gl_texture_image *tImg = tObj->Image[0][tObj->BaseLevel];
1407 GLuint minStart, minEnd; /* texels with minification */
1408 GLuint magStart, magEnd; /* texels with magnification */
1409
1410 const GLboolean repeatNoBorderPOT = (tObj->WrapS == GL_REPEAT)
1411 && (tObj->WrapT == GL_REPEAT)
1412 && (tImg->Border == 0 && (tImg->Width == tImg->RowStride))
1413 && (tImg->TexFormat->BaseFormat != GL_COLOR_INDEX)
1414 && tImg->_IsPowerOfTwo;
1415
1416 ASSERT(lambda != NULL);
1417 compute_min_mag_ranges(tObj, n, lambda,
1418 &minStart, &minEnd, &magStart, &magEnd);
1419
1420 if (minStart < minEnd) {
1421 /* do the minified texels */
1422 const GLuint m = minEnd - minStart;
1423 switch (tObj->MinFilter) {
1424 case GL_NEAREST:
1425 if (repeatNoBorderPOT) {
1426 switch (tImg->TexFormat->MesaFormat) {
1427 #if 0
1428 case MESA_FORMAT_RGB:
1429 opt_sample_rgb_2d(ctx, tObj, m, texcoords + minStart,
1430 NULL, rgba + minStart);
1431 break;
1432 case MESA_FORMAT_RGBA:
1433 opt_sample_rgba_2d(ctx, tObj, m, texcoords + minStart,
1434 NULL, rgba + minStart);
1435 break;
1436 #endif
1437 default:
1438 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1439 NULL, rgba + minStart );
1440 }
1441 }
1442 else {
1443 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1444 NULL, rgba + minStart);
1445 }
1446 break;
1447 case GL_LINEAR:
1448 sample_linear_2d(ctx, tObj, m, texcoords + minStart,
1449 NULL, rgba + minStart);
1450 break;
1451 case GL_NEAREST_MIPMAP_NEAREST:
1452 sample_2d_nearest_mipmap_nearest(ctx, tObj, m,
1453 texcoords + minStart,
1454 lambda + minStart, rgba + minStart);
1455 break;
1456 case GL_LINEAR_MIPMAP_NEAREST:
1457 sample_2d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1458 lambda + minStart, rgba + minStart);
1459 break;
1460 case GL_NEAREST_MIPMAP_LINEAR:
1461 sample_2d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1462 lambda + minStart, rgba + minStart);
1463 break;
1464 case GL_LINEAR_MIPMAP_LINEAR:
1465 if (repeatNoBorderPOT)
1466 sample_2d_linear_mipmap_linear_repeat(ctx, tObj, m,
1467 texcoords + minStart, lambda + minStart, rgba + minStart);
1468 else
1469 sample_2d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1470 lambda + minStart, rgba + minStart);
1471 break;
1472 default:
1473 _mesa_problem(ctx, "Bad min filter in sample_2d_texture");
1474 return;
1475 }
1476 }
1477
1478 if (magStart < magEnd) {
1479 /* do the magnified texels */
1480 const GLuint m = magEnd - magStart;
1481
1482 switch (tObj->MagFilter) {
1483 case GL_NEAREST:
1484 if (repeatNoBorderPOT) {
1485 switch (tImg->TexFormat->MesaFormat) {
1486 #if 0
1487 case MESA_FORMAT_RGB:
1488 opt_sample_rgb_2d(ctx, tObj, m, texcoords + magStart,
1489 NULL, rgba + magStart);
1490 break;
1491 case MESA_FORMAT_RGBA:
1492 opt_sample_rgba_2d(ctx, tObj, m, texcoords + magStart,
1493 NULL, rgba + magStart);
1494 break;
1495 #endif
1496 default:
1497 sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
1498 NULL, rgba + magStart );
1499 }
1500 }
1501 else {
1502 sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
1503 NULL, rgba + magStart);
1504 }
1505 break;
1506 case GL_LINEAR:
1507 sample_linear_2d(ctx, tObj, m, texcoords + magStart,
1508 NULL, rgba + magStart);
1509 break;
1510 default:
1511 _mesa_problem(ctx, "Bad mag filter in sample_lambda_2d");
1512 }
1513 }
1514 }
1515
1516
1517
1518 /**********************************************************************/
1519 /* 3-D Texture Sampling Functions */
1520 /**********************************************************************/
1521
1522 /**
1523 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1524 */
1525 static INLINE void
1526 sample_3d_nearest(GLcontext *ctx,
1527 const struct gl_texture_object *tObj,
1528 const struct gl_texture_image *img,
1529 const GLfloat texcoord[4],
1530 GLfloat rgba[4])
1531 {
1532 const GLint width = img->Width2; /* without border, power of two */
1533 const GLint height = img->Height2; /* without border, power of two */
1534 const GLint depth = img->Depth2; /* without border, power of two */
1535 GLint i, j, k;
1536 (void) ctx;
1537
1538 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
1539 j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
1540 k = nearest_texel_location(tObj->WrapR, img, depth, texcoord[2]);
1541
1542 if (i < 0 || i >= (GLint) img->Width ||
1543 j < 0 || j >= (GLint) img->Height ||
1544 k < 0 || k >= (GLint) img->Depth) {
1545 /* Need this test for GL_CLAMP_TO_BORDER mode */
1546 get_border_color(tObj, img, rgba);
1547 }
1548 else {
1549 img->FetchTexelf(img, i, j, k, rgba);
1550 }
1551 }
1552
1553
1554 /**
1555 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1556 */
1557 static void
1558 sample_3d_linear(GLcontext *ctx,
1559 const struct gl_texture_object *tObj,
1560 const struct gl_texture_image *img,
1561 const GLfloat texcoord[4],
1562 GLfloat rgba[4])
1563 {
1564 const GLint width = img->Width2;
1565 const GLint height = img->Height2;
1566 const GLint depth = img->Depth2;
1567 GLint i0, j0, k0, i1, j1, k1;
1568 GLbitfield useBorderColor = 0x0;
1569 GLfloat a, b, c;
1570 GLfloat t000[4], t010[4], t001[4], t011[4];
1571 GLfloat t100[4], t110[4], t101[4], t111[4];
1572
1573 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
1574 linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
1575 linear_texel_locations(tObj->WrapR, img, depth, texcoord[2], &k0, &k1, &c);
1576
1577 if (img->Border) {
1578 i0 += img->Border;
1579 i1 += img->Border;
1580 j0 += img->Border;
1581 j1 += img->Border;
1582 k0 += img->Border;
1583 k1 += img->Border;
1584 }
1585 else {
1586 /* check if sampling texture border color */
1587 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
1588 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
1589 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
1590 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
1591 if (k0 < 0 || k0 >= depth) useBorderColor |= K0BIT;
1592 if (k1 < 0 || k1 >= depth) useBorderColor |= K1BIT;
1593 }
1594
1595 /* Fetch texels */
1596 if (useBorderColor & (I0BIT | J0BIT | K0BIT)) {
1597 get_border_color(tObj, img, t000);
1598 }
1599 else {
1600 img->FetchTexelf(img, i0, j0, k0, t000);
1601 }
1602 if (useBorderColor & (I1BIT | J0BIT | K0BIT)) {
1603 get_border_color(tObj, img, t100);
1604 }
1605 else {
1606 img->FetchTexelf(img, i1, j0, k0, t100);
1607 }
1608 if (useBorderColor & (I0BIT | J1BIT | K0BIT)) {
1609 get_border_color(tObj, img, t010);
1610 }
1611 else {
1612 img->FetchTexelf(img, i0, j1, k0, t010);
1613 }
1614 if (useBorderColor & (I1BIT | J1BIT | K0BIT)) {
1615 get_border_color(tObj, img, t110);
1616 }
1617 else {
1618 img->FetchTexelf(img, i1, j1, k0, t110);
1619 }
1620
1621 if (useBorderColor & (I0BIT | J0BIT | K1BIT)) {
1622 get_border_color(tObj, img, t001);
1623 }
1624 else {
1625 img->FetchTexelf(img, i0, j0, k1, t001);
1626 }
1627 if (useBorderColor & (I1BIT | J0BIT | K1BIT)) {
1628 get_border_color(tObj, img, t101);
1629 }
1630 else {
1631 img->FetchTexelf(img, i1, j0, k1, t101);
1632 }
1633 if (useBorderColor & (I0BIT | J1BIT | K1BIT)) {
1634 get_border_color(tObj, img, t011);
1635 }
1636 else {
1637 img->FetchTexelf(img, i0, j1, k1, t011);
1638 }
1639 if (useBorderColor & (I1BIT | J1BIT | K1BIT)) {
1640 get_border_color(tObj, img, t111);
1641 }
1642 else {
1643 img->FetchTexelf(img, i1, j1, k1, t111);
1644 }
1645
1646 /* trilinear interpolation of samples */
1647 lerp_rgba_3d(rgba, a, b, c, t000, t100, t010, t110, t001, t101, t011, t111);
1648 }
1649
1650
1651 static void
1652 sample_3d_nearest_mipmap_nearest(GLcontext *ctx,
1653 const struct gl_texture_object *tObj,
1654 GLuint n, const GLfloat texcoord[][4],
1655 const GLfloat lambda[], GLfloat rgba[][4] )
1656 {
1657 GLuint i;
1658 for (i = 0; i < n; i++) {
1659 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1660 sample_3d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1661 }
1662 }
1663
1664
1665 static void
1666 sample_3d_linear_mipmap_nearest(GLcontext *ctx,
1667 const struct gl_texture_object *tObj,
1668 GLuint n, const GLfloat texcoord[][4],
1669 const GLfloat lambda[], GLfloat rgba[][4])
1670 {
1671 GLuint i;
1672 ASSERT(lambda != NULL);
1673 for (i = 0; i < n; i++) {
1674 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1675 sample_3d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1676 }
1677 }
1678
1679
1680 static void
1681 sample_3d_nearest_mipmap_linear(GLcontext *ctx,
1682 const struct gl_texture_object *tObj,
1683 GLuint n, const GLfloat texcoord[][4],
1684 const GLfloat lambda[], GLfloat rgba[][4])
1685 {
1686 GLuint i;
1687 ASSERT(lambda != NULL);
1688 for (i = 0; i < n; i++) {
1689 GLint level = linear_mipmap_level(tObj, lambda[i]);
1690 if (level >= tObj->_MaxLevel) {
1691 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1692 texcoord[i], rgba[i]);
1693 }
1694 else {
1695 GLfloat t0[4], t1[4]; /* texels */
1696 const GLfloat f = FRAC(lambda[i]);
1697 sample_3d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1698 sample_3d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1699 lerp_rgba(rgba[i], f, t0, t1);
1700 }
1701 }
1702 }
1703
1704
1705 static void
1706 sample_3d_linear_mipmap_linear(GLcontext *ctx,
1707 const struct gl_texture_object *tObj,
1708 GLuint n, const GLfloat texcoord[][4],
1709 const GLfloat lambda[], GLfloat rgba[][4])
1710 {
1711 GLuint i;
1712 ASSERT(lambda != NULL);
1713 for (i = 0; i < n; i++) {
1714 GLint level = linear_mipmap_level(tObj, lambda[i]);
1715 if (level >= tObj->_MaxLevel) {
1716 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1717 texcoord[i], rgba[i]);
1718 }
1719 else {
1720 GLfloat t0[4], t1[4]; /* texels */
1721 const GLfloat f = FRAC(lambda[i]);
1722 sample_3d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1723 sample_3d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1724 lerp_rgba(rgba[i], f, t0, t1);
1725 }
1726 }
1727 }
1728
1729
1730 /** Sample 3D texture, nearest filtering for both min/magnification */
1731 static void
1732 sample_nearest_3d(GLcontext *ctx,
1733 const struct gl_texture_object *tObj, GLuint n,
1734 const GLfloat texcoords[][4], const GLfloat lambda[],
1735 GLfloat rgba[][4])
1736 {
1737 GLuint i;
1738 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1739 (void) lambda;
1740 for (i = 0; i < n; i++) {
1741 sample_3d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
1742 }
1743 }
1744
1745
1746 /** Sample 3D texture, linear filtering for both min/magnification */
1747 static void
1748 sample_linear_3d(GLcontext *ctx,
1749 const struct gl_texture_object *tObj, GLuint n,
1750 const GLfloat texcoords[][4],
1751 const GLfloat lambda[], GLfloat rgba[][4])
1752 {
1753 GLuint i;
1754 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1755 (void) lambda;
1756 for (i = 0; i < n; i++) {
1757 sample_3d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
1758 }
1759 }
1760
1761
1762 /** Sample 3D texture, using lambda to choose between min/magnification */
1763 static void
1764 sample_lambda_3d(GLcontext *ctx,
1765 const struct gl_texture_object *tObj, GLuint n,
1766 const GLfloat texcoords[][4], const GLfloat lambda[],
1767 GLfloat rgba[][4])
1768 {
1769 GLuint minStart, minEnd; /* texels with minification */
1770 GLuint magStart, magEnd; /* texels with magnification */
1771 GLuint i;
1772
1773 ASSERT(lambda != NULL);
1774 compute_min_mag_ranges(tObj, n, lambda,
1775 &minStart, &minEnd, &magStart, &magEnd);
1776
1777 if (minStart < minEnd) {
1778 /* do the minified texels */
1779 GLuint m = minEnd - minStart;
1780 switch (tObj->MinFilter) {
1781 case GL_NEAREST:
1782 for (i = minStart; i < minEnd; i++)
1783 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1784 texcoords[i], rgba[i]);
1785 break;
1786 case GL_LINEAR:
1787 for (i = minStart; i < minEnd; i++)
1788 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1789 texcoords[i], rgba[i]);
1790 break;
1791 case GL_NEAREST_MIPMAP_NEAREST:
1792 sample_3d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1793 lambda + minStart, rgba + minStart);
1794 break;
1795 case GL_LINEAR_MIPMAP_NEAREST:
1796 sample_3d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1797 lambda + minStart, rgba + minStart);
1798 break;
1799 case GL_NEAREST_MIPMAP_LINEAR:
1800 sample_3d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1801 lambda + minStart, rgba + minStart);
1802 break;
1803 case GL_LINEAR_MIPMAP_LINEAR:
1804 sample_3d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1805 lambda + minStart, rgba + minStart);
1806 break;
1807 default:
1808 _mesa_problem(ctx, "Bad min filter in sample_3d_texture");
1809 return;
1810 }
1811 }
1812
1813 if (magStart < magEnd) {
1814 /* do the magnified texels */
1815 switch (tObj->MagFilter) {
1816 case GL_NEAREST:
1817 for (i = magStart; i < magEnd; i++)
1818 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1819 texcoords[i], rgba[i]);
1820 break;
1821 case GL_LINEAR:
1822 for (i = magStart; i < magEnd; i++)
1823 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1824 texcoords[i], rgba[i]);
1825 break;
1826 default:
1827 _mesa_problem(ctx, "Bad mag filter in sample_3d_texture");
1828 return;
1829 }
1830 }
1831 }
1832
1833
1834 /**********************************************************************/
1835 /* Texture Cube Map Sampling Functions */
1836 /**********************************************************************/
1837
1838 /**
1839 * Choose one of six sides of a texture cube map given the texture
1840 * coord (rx,ry,rz). Return pointer to corresponding array of texture
1841 * images.
1842 */
1843 static const struct gl_texture_image **
1844 choose_cube_face(const struct gl_texture_object *texObj,
1845 const GLfloat texcoord[4], GLfloat newCoord[4])
1846 {
1847 /*
1848 major axis
1849 direction target sc tc ma
1850 ---------- ------------------------------- --- --- ---
1851 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1852 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1853 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1854 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1855 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1856 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1857 */
1858 const GLfloat rx = texcoord[0];
1859 const GLfloat ry = texcoord[1];
1860 const GLfloat rz = texcoord[2];
1861 const GLfloat arx = FABSF(rx), ary = FABSF(ry), arz = FABSF(rz);
1862 GLuint face;
1863 GLfloat sc, tc, ma;
1864
1865 if (arx > ary && arx > arz) {
1866 if (rx >= 0.0F) {
1867 face = FACE_POS_X;
1868 sc = -rz;
1869 tc = -ry;
1870 ma = arx;
1871 }
1872 else {
1873 face = FACE_NEG_X;
1874 sc = rz;
1875 tc = -ry;
1876 ma = arx;
1877 }
1878 }
1879 else if (ary > arx && ary > arz) {
1880 if (ry >= 0.0F) {
1881 face = FACE_POS_Y;
1882 sc = rx;
1883 tc = rz;
1884 ma = ary;
1885 }
1886 else {
1887 face = FACE_NEG_Y;
1888 sc = rx;
1889 tc = -rz;
1890 ma = ary;
1891 }
1892 }
1893 else {
1894 if (rz > 0.0F) {
1895 face = FACE_POS_Z;
1896 sc = rx;
1897 tc = -ry;
1898 ma = arz;
1899 }
1900 else {
1901 face = FACE_NEG_Z;
1902 sc = -rx;
1903 tc = -ry;
1904 ma = arz;
1905 }
1906 }
1907
1908 newCoord[0] = ( sc / ma + 1.0F ) * 0.5F;
1909 newCoord[1] = ( tc / ma + 1.0F ) * 0.5F;
1910 return (const struct gl_texture_image **) texObj->Image[face];
1911 }
1912
1913
1914 static void
1915 sample_nearest_cube(GLcontext *ctx,
1916 const struct gl_texture_object *tObj, GLuint n,
1917 const GLfloat texcoords[][4], const GLfloat lambda[],
1918 GLfloat rgba[][4])
1919 {
1920 GLuint i;
1921 (void) lambda;
1922 for (i = 0; i < n; i++) {
1923 const struct gl_texture_image **images;
1924 GLfloat newCoord[4];
1925 images = choose_cube_face(tObj, texcoords[i], newCoord);
1926 sample_2d_nearest(ctx, tObj, images[tObj->BaseLevel],
1927 newCoord, rgba[i]);
1928 }
1929 }
1930
1931
1932 static void
1933 sample_linear_cube(GLcontext *ctx,
1934 const struct gl_texture_object *tObj, GLuint n,
1935 const GLfloat texcoords[][4],
1936 const GLfloat lambda[], GLfloat rgba[][4])
1937 {
1938 GLuint i;
1939 (void) lambda;
1940 for (i = 0; i < n; i++) {
1941 const struct gl_texture_image **images;
1942 GLfloat newCoord[4];
1943 images = choose_cube_face(tObj, texcoords[i], newCoord);
1944 sample_2d_linear(ctx, tObj, images[tObj->BaseLevel],
1945 newCoord, rgba[i]);
1946 }
1947 }
1948
1949
1950 static void
1951 sample_cube_nearest_mipmap_nearest(GLcontext *ctx,
1952 const struct gl_texture_object *tObj,
1953 GLuint n, const GLfloat texcoord[][4],
1954 const GLfloat lambda[], GLfloat rgba[][4])
1955 {
1956 GLuint i;
1957 ASSERT(lambda != NULL);
1958 for (i = 0; i < n; i++) {
1959 const struct gl_texture_image **images;
1960 GLfloat newCoord[4];
1961 GLint level;
1962 images = choose_cube_face(tObj, texcoord[i], newCoord);
1963
1964 /* XXX we actually need to recompute lambda here based on the newCoords.
1965 * But we would need the texcoords of adjacent fragments to compute that
1966 * properly, and we don't have those here.
1967 * For now, do an approximation: subtracting 1 from the chosen mipmap
1968 * level seems to work in some test cases.
1969 * The same adjustment is done in the next few functions.
1970 */
1971 level = nearest_mipmap_level(tObj, lambda[i]);
1972 level = MAX2(level - 1, 0);
1973
1974 sample_2d_nearest(ctx, tObj, images[level], newCoord, rgba[i]);
1975 }
1976 }
1977
1978
1979 static void
1980 sample_cube_linear_mipmap_nearest(GLcontext *ctx,
1981 const struct gl_texture_object *tObj,
1982 GLuint n, const GLfloat texcoord[][4],
1983 const GLfloat lambda[], GLfloat rgba[][4])
1984 {
1985 GLuint i;
1986 ASSERT(lambda != NULL);
1987 for (i = 0; i < n; i++) {
1988 const struct gl_texture_image **images;
1989 GLfloat newCoord[4];
1990 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1991 level = MAX2(level - 1, 0); /* see comment above */
1992 images = choose_cube_face(tObj, texcoord[i], newCoord);
1993 sample_2d_linear(ctx, tObj, images[level], newCoord, rgba[i]);
1994 }
1995 }
1996
1997
1998 static void
1999 sample_cube_nearest_mipmap_linear(GLcontext *ctx,
2000 const struct gl_texture_object *tObj,
2001 GLuint n, const GLfloat texcoord[][4],
2002 const GLfloat lambda[], GLfloat rgba[][4])
2003 {
2004 GLuint i;
2005 ASSERT(lambda != NULL);
2006 for (i = 0; i < n; i++) {
2007 const struct gl_texture_image **images;
2008 GLfloat newCoord[4];
2009 GLint level = linear_mipmap_level(tObj, lambda[i]);
2010 level = MAX2(level - 1, 0); /* see comment above */
2011 images = choose_cube_face(tObj, texcoord[i], newCoord);
2012 if (level >= tObj->_MaxLevel) {
2013 sample_2d_nearest(ctx, tObj, images[tObj->_MaxLevel],
2014 newCoord, rgba[i]);
2015 }
2016 else {
2017 GLfloat t0[4], t1[4]; /* texels */
2018 const GLfloat f = FRAC(lambda[i]);
2019 sample_2d_nearest(ctx, tObj, images[level ], newCoord, t0);
2020 sample_2d_nearest(ctx, tObj, images[level+1], newCoord, t1);
2021 lerp_rgba(rgba[i], f, t0, t1);
2022 }
2023 }
2024 }
2025
2026
2027 static void
2028 sample_cube_linear_mipmap_linear(GLcontext *ctx,
2029 const struct gl_texture_object *tObj,
2030 GLuint n, const GLfloat texcoord[][4],
2031 const GLfloat lambda[], GLfloat rgba[][4])
2032 {
2033 GLuint i;
2034 ASSERT(lambda != NULL);
2035 for (i = 0; i < n; i++) {
2036 const struct gl_texture_image **images;
2037 GLfloat newCoord[4];
2038 GLint level = linear_mipmap_level(tObj, lambda[i]);
2039 level = MAX2(level - 1, 0); /* see comment above */
2040 images = choose_cube_face(tObj, texcoord[i], newCoord);
2041 if (level >= tObj->_MaxLevel) {
2042 sample_2d_linear(ctx, tObj, images[tObj->_MaxLevel],
2043 newCoord, rgba[i]);
2044 }
2045 else {
2046 GLfloat t0[4], t1[4];
2047 const GLfloat f = FRAC(lambda[i]);
2048 sample_2d_linear(ctx, tObj, images[level ], newCoord, t0);
2049 sample_2d_linear(ctx, tObj, images[level+1], newCoord, t1);
2050 lerp_rgba(rgba[i], f, t0, t1);
2051 }
2052 }
2053 }
2054
2055
2056 /** Sample cube texture, using lambda to choose between min/magnification */
2057 static void
2058 sample_lambda_cube(GLcontext *ctx,
2059 const struct gl_texture_object *tObj, GLuint n,
2060 const GLfloat texcoords[][4], const GLfloat lambda[],
2061 GLfloat rgba[][4])
2062 {
2063 GLuint minStart, minEnd; /* texels with minification */
2064 GLuint magStart, magEnd; /* texels with magnification */
2065
2066 ASSERT(lambda != NULL);
2067 compute_min_mag_ranges(tObj, n, lambda,
2068 &minStart, &minEnd, &magStart, &magEnd);
2069
2070 if (minStart < minEnd) {
2071 /* do the minified texels */
2072 const GLuint m = minEnd - minStart;
2073 switch (tObj->MinFilter) {
2074 case GL_NEAREST:
2075 sample_nearest_cube(ctx, tObj, m, texcoords + minStart,
2076 lambda + minStart, rgba + minStart);
2077 break;
2078 case GL_LINEAR:
2079 sample_linear_cube(ctx, tObj, m, texcoords + minStart,
2080 lambda + minStart, rgba + minStart);
2081 break;
2082 case GL_NEAREST_MIPMAP_NEAREST:
2083 sample_cube_nearest_mipmap_nearest(ctx, tObj, m,
2084 texcoords + minStart,
2085 lambda + minStart, rgba + minStart);
2086 break;
2087 case GL_LINEAR_MIPMAP_NEAREST:
2088 sample_cube_linear_mipmap_nearest(ctx, tObj, m,
2089 texcoords + minStart,
2090 lambda + minStart, rgba + minStart);
2091 break;
2092 case GL_NEAREST_MIPMAP_LINEAR:
2093 sample_cube_nearest_mipmap_linear(ctx, tObj, m,
2094 texcoords + minStart,
2095 lambda + minStart, rgba + minStart);
2096 break;
2097 case GL_LINEAR_MIPMAP_LINEAR:
2098 sample_cube_linear_mipmap_linear(ctx, tObj, m,
2099 texcoords + minStart,
2100 lambda + minStart, rgba + minStart);
2101 break;
2102 default:
2103 _mesa_problem(ctx, "Bad min filter in sample_lambda_cube");
2104 }
2105 }
2106
2107 if (magStart < magEnd) {
2108 /* do the magnified texels */
2109 const GLuint m = magEnd - magStart;
2110 switch (tObj->MagFilter) {
2111 case GL_NEAREST:
2112 sample_nearest_cube(ctx, tObj, m, texcoords + magStart,
2113 lambda + magStart, rgba + magStart);
2114 break;
2115 case GL_LINEAR:
2116 sample_linear_cube(ctx, tObj, m, texcoords + magStart,
2117 lambda + magStart, rgba + magStart);
2118 break;
2119 default:
2120 _mesa_problem(ctx, "Bad mag filter in sample_lambda_cube");
2121 }
2122 }
2123 }
2124
2125
2126 /**********************************************************************/
2127 /* Texture Rectangle Sampling Functions */
2128 /**********************************************************************/
2129
2130
2131 static void
2132 sample_nearest_rect(GLcontext *ctx,
2133 const struct gl_texture_object *tObj, GLuint n,
2134 const GLfloat texcoords[][4], const GLfloat lambda[],
2135 GLfloat rgba[][4])
2136 {
2137 const struct gl_texture_image *img = tObj->Image[0][0];
2138 const GLint width = img->Width;
2139 const GLint height = img->Height;
2140 GLuint i;
2141
2142 (void) ctx;
2143 (void) lambda;
2144
2145 ASSERT(tObj->WrapS == GL_CLAMP ||
2146 tObj->WrapS == GL_CLAMP_TO_EDGE ||
2147 tObj->WrapS == GL_CLAMP_TO_BORDER);
2148 ASSERT(tObj->WrapT == GL_CLAMP ||
2149 tObj->WrapT == GL_CLAMP_TO_EDGE ||
2150 tObj->WrapT == GL_CLAMP_TO_BORDER);
2151 ASSERT(img->TexFormat->BaseFormat != GL_COLOR_INDEX);
2152
2153 for (i = 0; i < n; i++) {
2154 GLint row, col;
2155 col = clamp_rect_coord_nearest(tObj->WrapS, texcoords[i][0], width);
2156 row = clamp_rect_coord_nearest(tObj->WrapT, texcoords[i][1], height);
2157 if (col < 0 || col >= width || row < 0 || row >= height)
2158 get_border_color(tObj, img, rgba[i]);
2159 else
2160 img->FetchTexelf(img, col, row, 0, rgba[i]);
2161 }
2162 }
2163
2164
2165 static void
2166 sample_linear_rect(GLcontext *ctx,
2167 const struct gl_texture_object *tObj, GLuint n,
2168 const GLfloat texcoords[][4],
2169 const GLfloat lambda[], GLfloat rgba[][4])
2170 {
2171 const struct gl_texture_image *img = tObj->Image[0][0];
2172 const GLint width = img->Width;
2173 const GLint height = img->Height;
2174 GLuint i;
2175
2176 (void) ctx;
2177 (void) lambda;
2178
2179 ASSERT(tObj->WrapS == GL_CLAMP ||
2180 tObj->WrapS == GL_CLAMP_TO_EDGE ||
2181 tObj->WrapS == GL_CLAMP_TO_BORDER);
2182 ASSERT(tObj->WrapT == GL_CLAMP ||
2183 tObj->WrapT == GL_CLAMP_TO_EDGE ||
2184 tObj->WrapT == GL_CLAMP_TO_BORDER);
2185 ASSERT(img->TexFormat->BaseFormat != GL_COLOR_INDEX);
2186
2187 for (i = 0; i < n; i++) {
2188 GLint i0, j0, i1, j1;
2189 GLfloat t00[4], t01[4], t10[4], t11[4];
2190 GLfloat a, b;
2191 GLbitfield useBorderColor = 0x0;
2192
2193 clamp_rect_coord_linear(tObj->WrapS, texcoords[i][0], width,
2194 &i0, &i1, &a);
2195 clamp_rect_coord_linear(tObj->WrapT, texcoords[i][1], height,
2196 &j0, &j1, &b);
2197
2198 /* compute integer rows/columns */
2199 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2200 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2201 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
2202 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
2203
2204 /* get four texel samples */
2205 if (useBorderColor & (I0BIT | J0BIT))
2206 get_border_color(tObj, img, t00);
2207 else
2208 img->FetchTexelf(img, i0, j0, 0, t00);
2209
2210 if (useBorderColor & (I1BIT | J0BIT))
2211 get_border_color(tObj, img, t10);
2212 else
2213 img->FetchTexelf(img, i1, j0, 0, t10);
2214
2215 if (useBorderColor & (I0BIT | J1BIT))
2216 get_border_color(tObj, img, t01);
2217 else
2218 img->FetchTexelf(img, i0, j1, 0, t01);
2219
2220 if (useBorderColor & (I1BIT | J1BIT))
2221 get_border_color(tObj, img, t11);
2222 else
2223 img->FetchTexelf(img, i1, j1, 0, t11);
2224
2225 lerp_rgba_2d(rgba[i], a, b, t00, t10, t01, t11);
2226 }
2227 }
2228
2229
2230 /** Sample Rect texture, using lambda to choose between min/magnification */
2231 static void
2232 sample_lambda_rect(GLcontext *ctx,
2233 const struct gl_texture_object *tObj, GLuint n,
2234 const GLfloat texcoords[][4], const GLfloat lambda[],
2235 GLfloat rgba[][4])
2236 {
2237 GLuint minStart, minEnd, magStart, magEnd;
2238
2239 /* We only need lambda to decide between minification and magnification.
2240 * There is no mipmapping with rectangular textures.
2241 */
2242 compute_min_mag_ranges(tObj, n, lambda,
2243 &minStart, &minEnd, &magStart, &magEnd);
2244
2245 if (minStart < minEnd) {
2246 if (tObj->MinFilter == GL_NEAREST) {
2247 sample_nearest_rect(ctx, tObj, minEnd - minStart,
2248 texcoords + minStart, NULL, rgba + minStart);
2249 }
2250 else {
2251 sample_linear_rect(ctx, tObj, minEnd - minStart,
2252 texcoords + minStart, NULL, rgba + minStart);
2253 }
2254 }
2255 if (magStart < magEnd) {
2256 if (tObj->MagFilter == GL_NEAREST) {
2257 sample_nearest_rect(ctx, tObj, magEnd - magStart,
2258 texcoords + magStart, NULL, rgba + magStart);
2259 }
2260 else {
2261 sample_linear_rect(ctx, tObj, magEnd - magStart,
2262 texcoords + magStart, NULL, rgba + magStart);
2263 }
2264 }
2265 }
2266
2267
2268
2269 /**********************************************************************/
2270 /* 2D Texture Array Sampling Functions */
2271 /**********************************************************************/
2272
2273 /**
2274 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2275 */
2276 static void
2277 sample_2d_array_nearest(GLcontext *ctx,
2278 const struct gl_texture_object *tObj,
2279 const struct gl_texture_image *img,
2280 const GLfloat texcoord[4],
2281 GLfloat rgba[4])
2282 {
2283 const GLint width = img->Width2; /* without border, power of two */
2284 const GLint height = img->Height2; /* without border, power of two */
2285 const GLint depth = img->Depth;
2286 GLint i, j;
2287 GLint array;
2288 (void) ctx;
2289
2290 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
2291 j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
2292 array = clamp_rect_coord_nearest(tObj->WrapR, texcoord[2], depth);
2293
2294 if (i < 0 || i >= (GLint) img->Width ||
2295 j < 0 || j >= (GLint) img->Height ||
2296 array < 0 || array >= (GLint) img->Depth) {
2297 /* Need this test for GL_CLAMP_TO_BORDER mode */
2298 get_border_color(tObj, img, rgba);
2299 }
2300 else {
2301 img->FetchTexelf(img, i, j, array, rgba);
2302 }
2303 }
2304
2305
2306 /**
2307 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2308 */
2309 static void
2310 sample_2d_array_linear(GLcontext *ctx,
2311 const struct gl_texture_object *tObj,
2312 const struct gl_texture_image *img,
2313 const GLfloat texcoord[4],
2314 GLfloat rgba[4])
2315 {
2316 const GLint width = img->Width2;
2317 const GLint height = img->Height2;
2318 const GLint depth = img->Depth;
2319 GLint i0, j0, i1, j1;
2320 GLint array;
2321 GLbitfield useBorderColor = 0x0;
2322 GLfloat a, b;
2323 GLfloat t00[4], t01[4], t10[4], t11[4];
2324
2325 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
2326 linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
2327 array = clamp_rect_coord_nearest(tObj->WrapR, texcoord[2], depth);
2328
2329 if (array < 0 || array >= depth) {
2330 COPY_4V(rgba, tObj->BorderColor);
2331 }
2332 else {
2333 if (img->Border) {
2334 i0 += img->Border;
2335 i1 += img->Border;
2336 j0 += img->Border;
2337 j1 += img->Border;
2338 }
2339 else {
2340 /* check if sampling texture border color */
2341 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2342 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2343 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
2344 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
2345 }
2346
2347 /* Fetch texels */
2348 if (useBorderColor & (I0BIT | J0BIT)) {
2349 get_border_color(tObj, img, t00);
2350 }
2351 else {
2352 img->FetchTexelf(img, i0, j0, array, t00);
2353 }
2354 if (useBorderColor & (I1BIT | J0BIT)) {
2355 get_border_color(tObj, img, t10);
2356 }
2357 else {
2358 img->FetchTexelf(img, i1, j0, array, t10);
2359 }
2360 if (useBorderColor & (I0BIT | J1BIT)) {
2361 get_border_color(tObj, img, t01);
2362 }
2363 else {
2364 img->FetchTexelf(img, i0, j1, array, t01);
2365 }
2366 if (useBorderColor & (I1BIT | J1BIT)) {
2367 get_border_color(tObj, img, t11);
2368 }
2369 else {
2370 img->FetchTexelf(img, i1, j1, array, t11);
2371 }
2372
2373 /* trilinear interpolation of samples */
2374 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
2375 }
2376 }
2377
2378
2379 static void
2380 sample_2d_array_nearest_mipmap_nearest(GLcontext *ctx,
2381 const struct gl_texture_object *tObj,
2382 GLuint n, const GLfloat texcoord[][4],
2383 const GLfloat lambda[], GLfloat rgba[][4])
2384 {
2385 GLuint i;
2386 for (i = 0; i < n; i++) {
2387 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2388 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i],
2389 rgba[i]);
2390 }
2391 }
2392
2393
2394 static void
2395 sample_2d_array_linear_mipmap_nearest(GLcontext *ctx,
2396 const struct gl_texture_object *tObj,
2397 GLuint n, const GLfloat texcoord[][4],
2398 const GLfloat lambda[], GLfloat rgba[][4])
2399 {
2400 GLuint i;
2401 ASSERT(lambda != NULL);
2402 for (i = 0; i < n; i++) {
2403 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2404 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level],
2405 texcoord[i], rgba[i]);
2406 }
2407 }
2408
2409
2410 static void
2411 sample_2d_array_nearest_mipmap_linear(GLcontext *ctx,
2412 const struct gl_texture_object *tObj,
2413 GLuint n, const GLfloat texcoord[][4],
2414 const GLfloat lambda[], GLfloat rgba[][4])
2415 {
2416 GLuint i;
2417 ASSERT(lambda != NULL);
2418 for (i = 0; i < n; i++) {
2419 GLint level = linear_mipmap_level(tObj, lambda[i]);
2420 if (level >= tObj->_MaxLevel) {
2421 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2422 texcoord[i], rgba[i]);
2423 }
2424 else {
2425 GLfloat t0[4], t1[4]; /* texels */
2426 const GLfloat f = FRAC(lambda[i]);
2427 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level ],
2428 texcoord[i], t0);
2429 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level+1],
2430 texcoord[i], t1);
2431 lerp_rgba(rgba[i], f, t0, t1);
2432 }
2433 }
2434 }
2435
2436
2437 static void
2438 sample_2d_array_linear_mipmap_linear(GLcontext *ctx,
2439 const struct gl_texture_object *tObj,
2440 GLuint n, const GLfloat texcoord[][4],
2441 const GLfloat lambda[], GLfloat rgba[][4])
2442 {
2443 GLuint i;
2444 ASSERT(lambda != NULL);
2445 for (i = 0; i < n; i++) {
2446 GLint level = linear_mipmap_level(tObj, lambda[i]);
2447 if (level >= tObj->_MaxLevel) {
2448 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2449 texcoord[i], rgba[i]);
2450 }
2451 else {
2452 GLfloat t0[4], t1[4]; /* texels */
2453 const GLfloat f = FRAC(lambda[i]);
2454 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level ],
2455 texcoord[i], t0);
2456 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level+1],
2457 texcoord[i], t1);
2458 lerp_rgba(rgba[i], f, t0, t1);
2459 }
2460 }
2461 }
2462
2463
2464 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2465 static void
2466 sample_nearest_2d_array(GLcontext *ctx,
2467 const struct gl_texture_object *tObj, GLuint n,
2468 const GLfloat texcoords[][4], const GLfloat lambda[],
2469 GLfloat rgba[][4])
2470 {
2471 GLuint i;
2472 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2473 (void) lambda;
2474 for (i = 0; i < n; i++) {
2475 sample_2d_array_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
2476 }
2477 }
2478
2479
2480
2481 /** Sample 2D Array texture, linear filtering for both min/magnification */
2482 static void
2483 sample_linear_2d_array(GLcontext *ctx,
2484 const struct gl_texture_object *tObj, GLuint n,
2485 const GLfloat texcoords[][4],
2486 const GLfloat lambda[], GLfloat rgba[][4])
2487 {
2488 GLuint i;
2489 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2490 (void) lambda;
2491 for (i = 0; i < n; i++) {
2492 sample_2d_array_linear(ctx, tObj, image, texcoords[i], rgba[i]);
2493 }
2494 }
2495
2496
2497 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2498 static void
2499 sample_lambda_2d_array(GLcontext *ctx,
2500 const struct gl_texture_object *tObj, GLuint n,
2501 const GLfloat texcoords[][4], const GLfloat lambda[],
2502 GLfloat rgba[][4])
2503 {
2504 GLuint minStart, minEnd; /* texels with minification */
2505 GLuint magStart, magEnd; /* texels with magnification */
2506 GLuint i;
2507
2508 ASSERT(lambda != NULL);
2509 compute_min_mag_ranges(tObj, n, lambda,
2510 &minStart, &minEnd, &magStart, &magEnd);
2511
2512 if (minStart < minEnd) {
2513 /* do the minified texels */
2514 GLuint m = minEnd - minStart;
2515 switch (tObj->MinFilter) {
2516 case GL_NEAREST:
2517 for (i = minStart; i < minEnd; i++)
2518 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2519 texcoords[i], rgba[i]);
2520 break;
2521 case GL_LINEAR:
2522 for (i = minStart; i < minEnd; i++)
2523 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2524 texcoords[i], rgba[i]);
2525 break;
2526 case GL_NEAREST_MIPMAP_NEAREST:
2527 sample_2d_array_nearest_mipmap_nearest(ctx, tObj, m,
2528 texcoords + minStart,
2529 lambda + minStart,
2530 rgba + minStart);
2531 break;
2532 case GL_LINEAR_MIPMAP_NEAREST:
2533 sample_2d_array_linear_mipmap_nearest(ctx, tObj, m,
2534 texcoords + minStart,
2535 lambda + minStart,
2536 rgba + minStart);
2537 break;
2538 case GL_NEAREST_MIPMAP_LINEAR:
2539 sample_2d_array_nearest_mipmap_linear(ctx, tObj, m,
2540 texcoords + minStart,
2541 lambda + minStart,
2542 rgba + minStart);
2543 break;
2544 case GL_LINEAR_MIPMAP_LINEAR:
2545 sample_2d_array_linear_mipmap_linear(ctx, tObj, m,
2546 texcoords + minStart,
2547 lambda + minStart,
2548 rgba + minStart);
2549 break;
2550 default:
2551 _mesa_problem(ctx, "Bad min filter in sample_2d_array_texture");
2552 return;
2553 }
2554 }
2555
2556 if (magStart < magEnd) {
2557 /* do the magnified texels */
2558 switch (tObj->MagFilter) {
2559 case GL_NEAREST:
2560 for (i = magStart; i < magEnd; i++)
2561 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2562 texcoords[i], rgba[i]);
2563 break;
2564 case GL_LINEAR:
2565 for (i = magStart; i < magEnd; i++)
2566 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2567 texcoords[i], rgba[i]);
2568 break;
2569 default:
2570 _mesa_problem(ctx, "Bad mag filter in sample_2d_array_texture");
2571 return;
2572 }
2573 }
2574 }
2575
2576
2577
2578
2579 /**********************************************************************/
2580 /* 1D Texture Array Sampling Functions */
2581 /**********************************************************************/
2582
2583 /**
2584 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2585 */
2586 static void
2587 sample_1d_array_nearest(GLcontext *ctx,
2588 const struct gl_texture_object *tObj,
2589 const struct gl_texture_image *img,
2590 const GLfloat texcoord[4],
2591 GLfloat rgba[4])
2592 {
2593 const GLint width = img->Width2; /* without border, power of two */
2594 const GLint height = img->Height;
2595 GLint i;
2596 GLint array;
2597 (void) ctx;
2598
2599 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
2600 array = clamp_rect_coord_nearest(tObj->WrapT, texcoord[1], height);
2601
2602 if (i < 0 || i >= (GLint) img->Width ||
2603 array < 0 || array >= (GLint) img->Height) {
2604 /* Need this test for GL_CLAMP_TO_BORDER mode */
2605 get_border_color(tObj, img, rgba);
2606 }
2607 else {
2608 img->FetchTexelf(img, i, array, 0, rgba);
2609 }
2610 }
2611
2612
2613 /**
2614 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2615 */
2616 static void
2617 sample_1d_array_linear(GLcontext *ctx,
2618 const struct gl_texture_object *tObj,
2619 const struct gl_texture_image *img,
2620 const GLfloat texcoord[4],
2621 GLfloat rgba[4])
2622 {
2623 const GLint width = img->Width2;
2624 const GLint height = img->Height;
2625 GLint i0, i1;
2626 GLint array;
2627 GLbitfield useBorderColor = 0x0;
2628 GLfloat a;
2629 GLfloat t0[4], t1[4];
2630
2631 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
2632 array = clamp_rect_coord_nearest(tObj->WrapT, texcoord[1], height);
2633
2634 if (img->Border) {
2635 i0 += img->Border;
2636 i1 += img->Border;
2637 }
2638 else {
2639 /* check if sampling texture border color */
2640 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2641 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2642 }
2643
2644 if (array < 0 || array >= height) useBorderColor |= K0BIT;
2645
2646 /* Fetch texels */
2647 if (useBorderColor & (I0BIT | K0BIT)) {
2648 get_border_color(tObj, img, t0);
2649 }
2650 else {
2651 img->FetchTexelf(img, i0, array, 0, t0);
2652 }
2653 if (useBorderColor & (I1BIT | K0BIT)) {
2654 get_border_color(tObj, img, t1);
2655 }
2656 else {
2657 img->FetchTexelf(img, i1, array, 0, t1);
2658 }
2659
2660 /* bilinear interpolation of samples */
2661 lerp_rgba(rgba, a, t0, t1);
2662 }
2663
2664
2665 static void
2666 sample_1d_array_nearest_mipmap_nearest(GLcontext *ctx,
2667 const struct gl_texture_object *tObj,
2668 GLuint n, const GLfloat texcoord[][4],
2669 const GLfloat lambda[], GLfloat rgba[][4])
2670 {
2671 GLuint i;
2672 for (i = 0; i < n; i++) {
2673 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2674 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i],
2675 rgba[i]);
2676 }
2677 }
2678
2679
2680 static void
2681 sample_1d_array_linear_mipmap_nearest(GLcontext *ctx,
2682 const struct gl_texture_object *tObj,
2683 GLuint n, const GLfloat texcoord[][4],
2684 const GLfloat lambda[], GLfloat rgba[][4])
2685 {
2686 GLuint i;
2687 ASSERT(lambda != NULL);
2688 for (i = 0; i < n; i++) {
2689 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2690 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level],
2691 texcoord[i], rgba[i]);
2692 }
2693 }
2694
2695
2696 static void
2697 sample_1d_array_nearest_mipmap_linear(GLcontext *ctx,
2698 const struct gl_texture_object *tObj,
2699 GLuint n, const GLfloat texcoord[][4],
2700 const GLfloat lambda[], GLfloat rgba[][4])
2701 {
2702 GLuint i;
2703 ASSERT(lambda != NULL);
2704 for (i = 0; i < n; i++) {
2705 GLint level = linear_mipmap_level(tObj, lambda[i]);
2706 if (level >= tObj->_MaxLevel) {
2707 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2708 texcoord[i], rgba[i]);
2709 }
2710 else {
2711 GLfloat t0[4], t1[4]; /* texels */
2712 const GLfloat f = FRAC(lambda[i]);
2713 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
2714 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
2715 lerp_rgba(rgba[i], f, t0, t1);
2716 }
2717 }
2718 }
2719
2720
2721 static void
2722 sample_1d_array_linear_mipmap_linear(GLcontext *ctx,
2723 const struct gl_texture_object *tObj,
2724 GLuint n, const GLfloat texcoord[][4],
2725 const GLfloat lambda[], GLfloat rgba[][4])
2726 {
2727 GLuint i;
2728 ASSERT(lambda != NULL);
2729 for (i = 0; i < n; i++) {
2730 GLint level = linear_mipmap_level(tObj, lambda[i]);
2731 if (level >= tObj->_MaxLevel) {
2732 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2733 texcoord[i], rgba[i]);
2734 }
2735 else {
2736 GLfloat t0[4], t1[4]; /* texels */
2737 const GLfloat f = FRAC(lambda[i]);
2738 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
2739 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
2740 lerp_rgba(rgba[i], f, t0, t1);
2741 }
2742 }
2743 }
2744
2745
2746 /** Sample 1D Array texture, nearest filtering for both min/magnification */
2747 static void
2748 sample_nearest_1d_array(GLcontext *ctx,
2749 const struct gl_texture_object *tObj, GLuint n,
2750 const GLfloat texcoords[][4], const GLfloat lambda[],
2751 GLfloat rgba[][4])
2752 {
2753 GLuint i;
2754 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2755 (void) lambda;
2756 for (i = 0; i < n; i++) {
2757 sample_1d_array_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
2758 }
2759 }
2760
2761
2762 /** Sample 1D Array texture, linear filtering for both min/magnification */
2763 static void
2764 sample_linear_1d_array(GLcontext *ctx,
2765 const struct gl_texture_object *tObj, GLuint n,
2766 const GLfloat texcoords[][4],
2767 const GLfloat lambda[], GLfloat rgba[][4])
2768 {
2769 GLuint i;
2770 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2771 (void) lambda;
2772 for (i = 0; i < n; i++) {
2773 sample_1d_array_linear(ctx, tObj, image, texcoords[i], rgba[i]);
2774 }
2775 }
2776
2777
2778 /** Sample 1D Array texture, using lambda to choose between min/magnification */
2779 static void
2780 sample_lambda_1d_array(GLcontext *ctx,
2781 const struct gl_texture_object *tObj, GLuint n,
2782 const GLfloat texcoords[][4], const GLfloat lambda[],
2783 GLfloat rgba[][4])
2784 {
2785 GLuint minStart, minEnd; /* texels with minification */
2786 GLuint magStart, magEnd; /* texels with magnification */
2787 GLuint i;
2788
2789 ASSERT(lambda != NULL);
2790 compute_min_mag_ranges(tObj, n, lambda,
2791 &minStart, &minEnd, &magStart, &magEnd);
2792
2793 if (minStart < minEnd) {
2794 /* do the minified texels */
2795 GLuint m = minEnd - minStart;
2796 switch (tObj->MinFilter) {
2797 case GL_NEAREST:
2798 for (i = minStart; i < minEnd; i++)
2799 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2800 texcoords[i], rgba[i]);
2801 break;
2802 case GL_LINEAR:
2803 for (i = minStart; i < minEnd; i++)
2804 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2805 texcoords[i], rgba[i]);
2806 break;
2807 case GL_NEAREST_MIPMAP_NEAREST:
2808 sample_1d_array_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
2809 lambda + minStart, rgba + minStart);
2810 break;
2811 case GL_LINEAR_MIPMAP_NEAREST:
2812 sample_1d_array_linear_mipmap_nearest(ctx, tObj, m,
2813 texcoords + minStart,
2814 lambda + minStart,
2815 rgba + minStart);
2816 break;
2817 case GL_NEAREST_MIPMAP_LINEAR:
2818 sample_1d_array_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
2819 lambda + minStart, rgba + minStart);
2820 break;
2821 case GL_LINEAR_MIPMAP_LINEAR:
2822 sample_1d_array_linear_mipmap_linear(ctx, tObj, m,
2823 texcoords + minStart,
2824 lambda + minStart,
2825 rgba + minStart);
2826 break;
2827 default:
2828 _mesa_problem(ctx, "Bad min filter in sample_1d_array_texture");
2829 return;
2830 }
2831 }
2832
2833 if (magStart < magEnd) {
2834 /* do the magnified texels */
2835 switch (tObj->MagFilter) {
2836 case GL_NEAREST:
2837 for (i = magStart; i < magEnd; i++)
2838 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2839 texcoords[i], rgba[i]);
2840 break;
2841 case GL_LINEAR:
2842 for (i = magStart; i < magEnd; i++)
2843 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2844 texcoords[i], rgba[i]);
2845 break;
2846 default:
2847 _mesa_problem(ctx, "Bad mag filter in sample_1d_array_texture");
2848 return;
2849 }
2850 }
2851 }
2852
2853
2854 /**
2855 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
2856 */
2857 static INLINE GLfloat
2858 shadow_compare(GLenum function, GLfloat coord, GLfloat depthSample,
2859 GLfloat ambient)
2860 {
2861 switch (function) {
2862 case GL_LEQUAL:
2863 return (coord <= depthSample) ? 1.0F : ambient;
2864 case GL_GEQUAL:
2865 return (coord >= depthSample) ? 1.0F : ambient;
2866 case GL_LESS:
2867 return (coord < depthSample) ? 1.0F : ambient;
2868 case GL_GREATER:
2869 return (coord > depthSample) ? 1.0F : ambient;
2870 case GL_EQUAL:
2871 return (coord == depthSample) ? 1.0F : ambient;
2872 case GL_NOTEQUAL:
2873 return (coord != depthSample) ? 1.0F : ambient;
2874 case GL_ALWAYS:
2875 return 1.0F;
2876 case GL_NEVER:
2877 return ambient;
2878 case GL_NONE:
2879 return depthSample;
2880 default:
2881 _mesa_problem(NULL, "Bad compare func in shadow_compare");
2882 return ambient;
2883 }
2884 }
2885
2886
2887 /**
2888 * Compare texcoord against four depth samples.
2889 */
2890 static INLINE GLfloat
2891 shadow_compare4(GLenum function, GLfloat coord,
2892 GLfloat depth00, GLfloat depth01,
2893 GLfloat depth10, GLfloat depth11,
2894 GLfloat ambient, GLfloat wi, GLfloat wj)
2895 {
2896 const GLfloat d = (1.0F - (GLfloat) ambient) * 0.25F;
2897 GLfloat luminance = 1.0F;
2898
2899 switch (function) {
2900 case GL_LEQUAL:
2901 if (depth00 <= coord) luminance -= d;
2902 if (depth01 <= coord) luminance -= d;
2903 if (depth10 <= coord) luminance -= d;
2904 if (depth11 <= coord) luminance -= d;
2905 return luminance;
2906 case GL_GEQUAL:
2907 if (depth00 >= coord) luminance -= d;
2908 if (depth01 >= coord) luminance -= d;
2909 if (depth10 >= coord) luminance -= d;
2910 if (depth11 >= coord) luminance -= d;
2911 return luminance;
2912 case GL_LESS:
2913 if (depth00 < coord) luminance -= d;
2914 if (depth01 < coord) luminance -= d;
2915 if (depth10 < coord) luminance -= d;
2916 if (depth11 < coord) luminance -= d;
2917 return luminance;
2918 case GL_GREATER:
2919 if (depth00 > coord) luminance -= d;
2920 if (depth01 > coord) luminance -= d;
2921 if (depth10 > coord) luminance -= d;
2922 if (depth11 > coord) luminance -= d;
2923 return luminance;
2924 case GL_EQUAL:
2925 if (depth00 == coord) luminance -= d;
2926 if (depth01 == coord) luminance -= d;
2927 if (depth10 == coord) luminance -= d;
2928 if (depth11 == coord) luminance -= d;
2929 return luminance;
2930 case GL_NOTEQUAL:
2931 if (depth00 != coord) luminance -= d;
2932 if (depth01 != coord) luminance -= d;
2933 if (depth10 != coord) luminance -= d;
2934 if (depth11 != coord) luminance -= d;
2935 return luminance;
2936 case GL_ALWAYS:
2937 return 0.0;
2938 case GL_NEVER:
2939 return ambient;
2940 case GL_NONE:
2941 /* ordinary bilinear filtering */
2942 return lerp_2d(wi, wj, depth00, depth10, depth01, depth11);
2943 default:
2944 _mesa_problem(NULL, "Bad compare func in sample_depth_texture");
2945 return 0.0F;
2946 }
2947 }
2948
2949
2950 /**
2951 * Sample a shadow/depth texture.
2952 */
2953 static void
2954 sample_depth_texture( GLcontext *ctx,
2955 const struct gl_texture_object *tObj, GLuint n,
2956 const GLfloat texcoords[][4], const GLfloat lambda[],
2957 GLfloat texel[][4] )
2958 {
2959 const GLint baseLevel = tObj->BaseLevel;
2960 const struct gl_texture_image *img = tObj->Image[0][baseLevel];
2961 const GLint width = img->Width;
2962 const GLint height = img->Height;
2963 const GLint depth = img->Depth;
2964 const GLuint compare_coord = (tObj->Target == GL_TEXTURE_2D_ARRAY_EXT)
2965 ? 3 : 2;
2966 GLfloat ambient;
2967 GLenum function;
2968 GLfloat result;
2969
2970 (void) lambda;
2971
2972 ASSERT(img->TexFormat->BaseFormat == GL_DEPTH_COMPONENT ||
2973 img->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT);
2974
2975 ASSERT(tObj->Target == GL_TEXTURE_1D ||
2976 tObj->Target == GL_TEXTURE_2D ||
2977 tObj->Target == GL_TEXTURE_RECTANGLE_NV ||
2978 tObj->Target == GL_TEXTURE_1D_ARRAY_EXT ||
2979 tObj->Target == GL_TEXTURE_2D_ARRAY_EXT);
2980
2981 ambient = tObj->CompareFailValue;
2982
2983 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
2984
2985 function = (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) ?
2986 tObj->CompareFunc : GL_NONE;
2987
2988 if (tObj->MagFilter == GL_NEAREST) {
2989 GLuint i;
2990 for (i = 0; i < n; i++) {
2991 GLfloat depthSample;
2992 GLint col, row, slice;
2993
2994 nearest_texcoord(tObj, texcoords[i], &col, &row, &slice);
2995
2996 if (col >= 0 && row >= 0 && col < width && row < height &&
2997 slice >= 0 && slice < depth) {
2998 img->FetchTexelf(img, col, row, slice, &depthSample);
2999 }
3000 else {
3001 depthSample = tObj->BorderColor[0];
3002 }
3003
3004 result = shadow_compare(function, texcoords[i][compare_coord],
3005 depthSample, ambient);
3006
3007 switch (tObj->DepthMode) {
3008 case GL_LUMINANCE:
3009 ASSIGN_4V(texel[i], result, result, result, 1.0F);
3010 break;
3011 case GL_INTENSITY:
3012 ASSIGN_4V(texel[i], result, result, result, result);
3013 break;
3014 case GL_ALPHA:
3015 ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
3016 break;
3017 default:
3018 _mesa_problem(ctx, "Bad depth texture mode");
3019 }
3020 }
3021 }
3022 else {
3023 GLuint i;
3024 ASSERT(tObj->MagFilter == GL_LINEAR);
3025 for (i = 0; i < n; i++) {
3026 GLfloat depth00, depth01, depth10, depth11;
3027 GLint i0, i1, j0, j1;
3028 GLint slice;
3029 GLfloat wi, wj;
3030 GLuint useBorderTexel;
3031
3032 linear_texcoord(tObj, texcoords[i], &i0, &i1, &j0, &j1, &slice,
3033 &wi, &wj);
3034
3035 useBorderTexel = 0;
3036 if (img->Border) {
3037 i0 += img->Border;
3038 i1 += img->Border;
3039 if (tObj->Target != GL_TEXTURE_1D_ARRAY_EXT) {
3040 j0 += img->Border;
3041 j1 += img->Border;
3042 }
3043 }
3044 else {
3045 if (i0 < 0 || i0 >= (GLint) width) useBorderTexel |= I0BIT;
3046 if (i1 < 0 || i1 >= (GLint) width) useBorderTexel |= I1BIT;
3047 if (j0 < 0 || j0 >= (GLint) height) useBorderTexel |= J0BIT;
3048 if (j1 < 0 || j1 >= (GLint) height) useBorderTexel |= J1BIT;
3049 }
3050
3051 if (slice < 0 || slice >= (GLint) depth) {
3052 depth00 = tObj->BorderColor[0];
3053 depth01 = tObj->BorderColor[0];
3054 depth10 = tObj->BorderColor[0];
3055 depth11 = tObj->BorderColor[0];
3056 }
3057 else {
3058 /* get four depth samples from the texture */
3059 if (useBorderTexel & (I0BIT | J0BIT)) {
3060 depth00 = tObj->BorderColor[0];
3061 }
3062 else {
3063 img->FetchTexelf(img, i0, j0, slice, &depth00);
3064 }
3065 if (useBorderTexel & (I1BIT | J0BIT)) {
3066 depth10 = tObj->BorderColor[0];
3067 }
3068 else {
3069 img->FetchTexelf(img, i1, j0, slice, &depth10);
3070 }
3071
3072 if (tObj->Target != GL_TEXTURE_1D_ARRAY_EXT) {
3073 if (useBorderTexel & (I0BIT | J1BIT)) {
3074 depth01 = tObj->BorderColor[0];
3075 }
3076 else {
3077 img->FetchTexelf(img, i0, j1, slice, &depth01);
3078 }
3079 if (useBorderTexel & (I1BIT | J1BIT)) {
3080 depth11 = tObj->BorderColor[0];
3081 }
3082 else {
3083 img->FetchTexelf(img, i1, j1, slice, &depth11);
3084 }
3085 }
3086 else {
3087 depth01 = depth00;
3088 depth11 = depth10;
3089 }
3090 }
3091
3092 result = shadow_compare4(function, texcoords[i][compare_coord],
3093 depth00, depth01, depth10, depth11,
3094 ambient, wi, wj);
3095
3096 switch (tObj->DepthMode) {
3097 case GL_LUMINANCE:
3098 ASSIGN_4V(texel[i], result, result, result, 1.0F);
3099 break;
3100 case GL_INTENSITY:
3101 ASSIGN_4V(texel[i], result, result, result, result);
3102 break;
3103 case GL_ALPHA:
3104 ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
3105 break;
3106 default:
3107 _mesa_problem(ctx, "Bad depth texture mode");
3108 }
3109
3110 } /* for */
3111 } /* if filter */
3112 }
3113
3114
3115 /**
3116 * We use this function when a texture object is in an "incomplete" state.
3117 * When a fragment program attempts to sample an incomplete texture we
3118 * return black (see issue 23 in GL_ARB_fragment_program spec).
3119 * Note: fragment programs don't observe the texture enable/disable flags.
3120 */
3121 static void
3122 null_sample_func( GLcontext *ctx,
3123 const struct gl_texture_object *tObj, GLuint n,
3124 const GLfloat texcoords[][4], const GLfloat lambda[],
3125 GLfloat rgba[][4])
3126 {
3127 GLuint i;
3128 (void) ctx;
3129 (void) tObj;
3130 (void) texcoords;
3131 (void) lambda;
3132 for (i = 0; i < n; i++) {
3133 rgba[i][RCOMP] = 0;
3134 rgba[i][GCOMP] = 0;
3135 rgba[i][BCOMP] = 0;
3136 rgba[i][ACOMP] = CHAN_MAX;
3137 }
3138 }
3139
3140
3141 /**
3142 * Choose the texture sampling function for the given texture object.
3143 */
3144 texture_sample_func
3145 _swrast_choose_texture_sample_func( GLcontext *ctx,
3146 const struct gl_texture_object *t )
3147 {
3148 if (!t || !t->_Complete) {
3149 return &null_sample_func;
3150 }
3151 else {
3152 const GLboolean needLambda = (GLboolean) (t->MinFilter != t->MagFilter);
3153 const GLenum format = t->Image[0][t->BaseLevel]->TexFormat->BaseFormat;
3154
3155 switch (t->Target) {
3156 case GL_TEXTURE_1D:
3157 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3158 return &sample_depth_texture;
3159 }
3160 else if (needLambda) {
3161 return &sample_lambda_1d;
3162 }
3163 else if (t->MinFilter == GL_LINEAR) {
3164 return &sample_linear_1d;
3165 }
3166 else {
3167 ASSERT(t->MinFilter == GL_NEAREST);
3168 return &sample_nearest_1d;
3169 }
3170 case GL_TEXTURE_2D:
3171 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3172 return &sample_depth_texture;
3173 }
3174 else if (needLambda) {
3175 return &sample_lambda_2d;
3176 }
3177 else if (t->MinFilter == GL_LINEAR) {
3178 return &sample_linear_2d;
3179 }
3180 else {
3181 /* check for a few optimized cases */
3182 #if 0
3183 const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
3184 ASSERT(t->MinFilter == GL_NEAREST);
3185 if (t->WrapS == GL_REPEAT &&
3186 t->WrapT == GL_REPEAT &&
3187 img->_IsPowerOfTwo &&
3188 img->Border == 0 &&
3189 img->TexFormat->MesaFormat == MESA_FORMAT_RGB) {
3190 return &opt_sample_rgb_2d;
3191 }
3192 else if (t->WrapS == GL_REPEAT &&
3193 t->WrapT == GL_REPEAT &&
3194 img->_IsPowerOfTwo &&
3195 img->Border == 0 &&
3196 img->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
3197 return &opt_sample_rgba_2d;
3198 }
3199 #else
3200 if (0)
3201 ;
3202 #endif
3203 else {
3204 return &sample_nearest_2d;
3205 }
3206 }
3207 case GL_TEXTURE_3D:
3208 if (needLambda) {
3209 return &sample_lambda_3d;
3210 }
3211 else if (t->MinFilter == GL_LINEAR) {
3212 return &sample_linear_3d;
3213 }
3214 else {
3215 ASSERT(t->MinFilter == GL_NEAREST);
3216 return &sample_nearest_3d;
3217 }
3218 case GL_TEXTURE_CUBE_MAP:
3219 if (needLambda) {
3220 return &sample_lambda_cube;
3221 }
3222 else if (t->MinFilter == GL_LINEAR) {
3223 return &sample_linear_cube;
3224 }
3225 else {
3226 ASSERT(t->MinFilter == GL_NEAREST);
3227 return &sample_nearest_cube;
3228 }
3229 case GL_TEXTURE_RECTANGLE_NV:
3230 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3231 return &sample_depth_texture;
3232 }
3233 else if (needLambda) {
3234 return &sample_lambda_rect;
3235 }
3236 else if (t->MinFilter == GL_LINEAR) {
3237 return &sample_linear_rect;
3238 }
3239 else {
3240 ASSERT(t->MinFilter == GL_NEAREST);
3241 return &sample_nearest_rect;
3242 }
3243 case GL_TEXTURE_1D_ARRAY_EXT:
3244 if (needLambda) {
3245 return &sample_lambda_1d_array;
3246 }
3247 else if (t->MinFilter == GL_LINEAR) {
3248 return &sample_linear_1d_array;
3249 }
3250 else {
3251 ASSERT(t->MinFilter == GL_NEAREST);
3252 return &sample_nearest_1d_array;
3253 }
3254 case GL_TEXTURE_2D_ARRAY_EXT:
3255 if (needLambda) {
3256 return &sample_lambda_2d_array;
3257 }
3258 else if (t->MinFilter == GL_LINEAR) {
3259 return &sample_linear_2d_array;
3260 }
3261 else {
3262 ASSERT(t->MinFilter == GL_NEAREST);
3263 return &sample_nearest_2d_array;
3264 }
3265 default:
3266 _mesa_problem(ctx,
3267 "invalid target in _swrast_choose_texture_sample_func");
3268 return &null_sample_func;
3269 }
3270 }
3271 }