0067d3eeb73658d4fd02d7ffac99cbce75ec7404
[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 break;
466 default:
467 _mesa_problem(NULL, "bad wrapMode in clamp_rect_coord_linear");
468 i0 = i1 = 0;
469 fcol = 0.0F;
470 }
471 *i0out = i0;
472 *i1out = i1;
473 *weight = FRAC(fcol);
474 }
475
476
477 /**
478 * Compute nearest integer texcoords for given texobj and coordinate.
479 */
480 static INLINE void
481 nearest_texcoord(const struct gl_texture_object *texObj,
482 const GLfloat texcoord[4],
483 GLint *i, GLint *j, GLint *k)
484 {
485 const GLint baseLevel = texObj->BaseLevel;
486 const struct gl_texture_image *img = texObj->Image[0][baseLevel];
487 const GLint width = img->Width;
488 const GLint height = img->Height;
489 const GLint depth = img->Depth;
490
491 switch (texObj->Target) {
492 case GL_TEXTURE_RECTANGLE_ARB:
493 *i = clamp_rect_coord_nearest(texObj->WrapS, texcoord[0], width);
494 *j = clamp_rect_coord_nearest(texObj->WrapT, texcoord[1], height);
495 *k = 0;
496 break;
497 case GL_TEXTURE_1D:
498 *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
499 *j = 0;
500 *k = 0;
501 break;
502 case GL_TEXTURE_2D:
503 *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
504 *j = nearest_texel_location(texObj->WrapT, img, height, texcoord[1]);
505 *k = 0;
506 break;
507 case GL_TEXTURE_1D_ARRAY_EXT:
508 *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
509 *j = clamp_rect_coord_nearest(texObj->WrapT, texcoord[1], height);
510 *k = 0;
511 break;
512 case GL_TEXTURE_2D_ARRAY_EXT:
513 *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
514 *j = nearest_texel_location(texObj->WrapT, img, height, texcoord[1]);
515 *k = clamp_rect_coord_nearest(texObj->WrapR, texcoord[2], depth);
516 break;
517 default:
518 *i = *j = *k = 0;
519 }
520 }
521
522
523 /**
524 * Compute linear integer texcoords for given texobj and coordinate.
525 */
526 static INLINE void
527 linear_texcoord(const struct gl_texture_object *texObj,
528 const GLfloat texcoord[4],
529 GLint *i0, GLint *i1, GLint *j0, GLint *j1, GLint *slice,
530 GLfloat *wi, GLfloat *wj)
531 {
532 const GLint baseLevel = texObj->BaseLevel;
533 const struct gl_texture_image *img = texObj->Image[0][baseLevel];
534 const GLint width = img->Width;
535 const GLint height = img->Height;
536 const GLint depth = img->Depth;
537
538 switch (texObj->Target) {
539 case GL_TEXTURE_RECTANGLE_ARB:
540 clamp_rect_coord_linear(texObj->WrapS, texcoord[0],
541 width, i0, i1, wi);
542 clamp_rect_coord_linear(texObj->WrapT, texcoord[1],
543 height, j0, j1, wj);
544 *slice = 0;
545 break;
546
547 case GL_TEXTURE_1D:
548 case GL_TEXTURE_2D:
549 linear_texel_locations(texObj->WrapS, img, width,
550 texcoord[0], i0, i1, wi);
551 linear_texel_locations(texObj->WrapT, img, height,
552 texcoord[1], j0, j1, wj);
553 *slice = 0;
554 break;
555
556 case GL_TEXTURE_1D_ARRAY_EXT:
557 linear_texel_locations(texObj->WrapS, img, width,
558 texcoord[0], i0, i1, wi);
559 *j0 = clamp_rect_coord_nearest(texObj->WrapT, texcoord[1], height);
560 *j1 = *j0;
561 *slice = 0;
562 break;
563
564 case GL_TEXTURE_2D_ARRAY_EXT:
565 linear_texel_locations(texObj->WrapS, img, width,
566 texcoord[0], i0, i1, wi);
567 linear_texel_locations(texObj->WrapT, img, height,
568 texcoord[1], j0, j1, wj);
569 *slice = clamp_rect_coord_nearest(texObj->WrapR, texcoord[2], depth);
570 break;
571
572 default:
573 *slice = 0;
574 }
575 }
576
577
578
579 /**
580 * For linear interpolation between mipmap levels N and N+1, this function
581 * computes N.
582 */
583 static INLINE GLint
584 linear_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
585 {
586 if (lambda < 0.0F)
587 return tObj->BaseLevel;
588 else if (lambda > tObj->_MaxLambda)
589 return (GLint) (tObj->BaseLevel + tObj->_MaxLambda);
590 else
591 return (GLint) (tObj->BaseLevel + lambda);
592 }
593
594
595 /**
596 * Compute the nearest mipmap level to take texels from.
597 */
598 static INLINE GLint
599 nearest_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
600 {
601 GLfloat l;
602 GLint level;
603 if (lambda <= 0.5F)
604 l = 0.0F;
605 else if (lambda > tObj->_MaxLambda + 0.4999F)
606 l = tObj->_MaxLambda + 0.4999F;
607 else
608 l = lambda;
609 level = (GLint) (tObj->BaseLevel + l + 0.5F);
610 if (level > tObj->_MaxLevel)
611 level = tObj->_MaxLevel;
612 return level;
613 }
614
615
616
617 /*
618 * Bitflags for texture border color sampling.
619 */
620 #define I0BIT 1
621 #define I1BIT 2
622 #define J0BIT 4
623 #define J1BIT 8
624 #define K0BIT 16
625 #define K1BIT 32
626
627
628
629 /**
630 * The lambda[] array values are always monotonic. Either the whole span
631 * will be minified, magnified, or split between the two. This function
632 * determines the subranges in [0, n-1] that are to be minified or magnified.
633 */
634 static INLINE void
635 compute_min_mag_ranges(const struct gl_texture_object *tObj,
636 GLuint n, const GLfloat lambda[],
637 GLuint *minStart, GLuint *minEnd,
638 GLuint *magStart, GLuint *magEnd)
639 {
640 GLfloat minMagThresh;
641
642 /* we shouldn't be here if minfilter == magfilter */
643 ASSERT(tObj->MinFilter != tObj->MagFilter);
644
645 /* This bit comes from the OpenGL spec: */
646 if (tObj->MagFilter == GL_LINEAR
647 && (tObj->MinFilter == GL_NEAREST_MIPMAP_NEAREST ||
648 tObj->MinFilter == GL_NEAREST_MIPMAP_LINEAR)) {
649 minMagThresh = 0.5F;
650 }
651 else {
652 minMagThresh = 0.0F;
653 }
654
655 #if 0
656 /* DEBUG CODE: Verify that lambda[] is monotonic.
657 * We can't really use this because the inaccuracy in the LOG2 function
658 * causes this test to fail, yet the resulting texturing is correct.
659 */
660 if (n > 1) {
661 GLuint i;
662 printf("lambda delta = %g\n", lambda[0] - lambda[n-1]);
663 if (lambda[0] >= lambda[n-1]) { /* decreasing */
664 for (i = 0; i < n - 1; i++) {
665 ASSERT((GLint) (lambda[i] * 10) >= (GLint) (lambda[i+1] * 10));
666 }
667 }
668 else { /* increasing */
669 for (i = 0; i < n - 1; i++) {
670 ASSERT((GLint) (lambda[i] * 10) <= (GLint) (lambda[i+1] * 10));
671 }
672 }
673 }
674 #endif /* DEBUG */
675
676 if (lambda[0] <= minMagThresh && (n <= 1 || lambda[n-1] <= minMagThresh)) {
677 /* magnification for whole span */
678 *magStart = 0;
679 *magEnd = n;
680 *minStart = *minEnd = 0;
681 }
682 else if (lambda[0] > minMagThresh && (n <=1 || lambda[n-1] > minMagThresh)) {
683 /* minification for whole span */
684 *minStart = 0;
685 *minEnd = n;
686 *magStart = *magEnd = 0;
687 }
688 else {
689 /* a mix of minification and magnification */
690 GLuint i;
691 if (lambda[0] > minMagThresh) {
692 /* start with minification */
693 for (i = 1; i < n; i++) {
694 if (lambda[i] <= minMagThresh)
695 break;
696 }
697 *minStart = 0;
698 *minEnd = i;
699 *magStart = i;
700 *magEnd = n;
701 }
702 else {
703 /* start with magnification */
704 for (i = 1; i < n; i++) {
705 if (lambda[i] > minMagThresh)
706 break;
707 }
708 *magStart = 0;
709 *magEnd = i;
710 *minStart = i;
711 *minEnd = n;
712 }
713 }
714
715 #if 0
716 /* Verify the min/mag Start/End values
717 * We don't use this either (see above)
718 */
719 {
720 GLint i;
721 for (i = 0; i < n; i++) {
722 if (lambda[i] > minMagThresh) {
723 /* minification */
724 ASSERT(i >= *minStart);
725 ASSERT(i < *minEnd);
726 }
727 else {
728 /* magnification */
729 ASSERT(i >= *magStart);
730 ASSERT(i < *magEnd);
731 }
732 }
733 }
734 #endif
735 }
736
737
738 /**
739 * When we sample the border color, it must be interpreted according to
740 * the base texture format. Ex: if the texture base format it GL_ALPHA,
741 * we return (0,0,0,BorderAlpha).
742 */
743 static INLINE void
744 get_border_color(const struct gl_texture_object *tObj,
745 const struct gl_texture_image *img,
746 GLfloat rgba[4])
747 {
748 switch (img->TexFormat->BaseFormat) {
749 case GL_RGB:
750 rgba[0] = tObj->BorderColor[0];
751 rgba[1] = tObj->BorderColor[1];
752 rgba[2] = tObj->BorderColor[2];
753 rgba[3] = 1.0F;
754 break;
755 case GL_ALPHA:
756 rgba[0] = rgba[1] = rgba[2] = 0.0;
757 rgba[3] = tObj->BorderColor[3];
758 break;
759 case GL_LUMINANCE:
760 rgba[0] = rgba[1] = rgba[2] = tObj->BorderColor[0];
761 rgba[3] = 1.0;
762 break;
763 case GL_LUMINANCE_ALPHA:
764 rgba[0] = rgba[1] = rgba[2] = tObj->BorderColor[0];
765 rgba[3] = tObj->BorderColor[3];
766 break;
767 case GL_INTENSITY:
768 rgba[0] = rgba[1] = rgba[2] = rgba[3] = tObj->BorderColor[0];
769 break;
770 default:
771 COPY_4V(rgba, tObj->BorderColor);
772 }
773 }
774
775
776 /**********************************************************************/
777 /* 1-D Texture Sampling Functions */
778 /**********************************************************************/
779
780 /**
781 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
782 */
783 static INLINE void
784 sample_1d_nearest(GLcontext *ctx,
785 const struct gl_texture_object *tObj,
786 const struct gl_texture_image *img,
787 const GLfloat texcoord[4], GLfloat rgba[4])
788 {
789 const GLint width = img->Width2; /* without border, power of two */
790 GLint i;
791 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
792 /* skip over the border, if any */
793 i += img->Border;
794 if (i < 0 || i >= (GLint) img->Width) {
795 /* Need this test for GL_CLAMP_TO_BORDER mode */
796 get_border_color(tObj, img, rgba);
797 }
798 else {
799 img->FetchTexelf(img, i, 0, 0, rgba);
800 }
801 }
802
803
804 /**
805 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
806 */
807 static INLINE void
808 sample_1d_linear(GLcontext *ctx,
809 const struct gl_texture_object *tObj,
810 const struct gl_texture_image *img,
811 const GLfloat texcoord[4], GLfloat rgba[4])
812 {
813 const GLint width = img->Width2;
814 GLint i0, i1;
815 GLbitfield useBorderColor = 0x0;
816 GLfloat a;
817 GLfloat t0[4], t1[4]; /* texels */
818
819 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
820
821 if (img->Border) {
822 i0 += img->Border;
823 i1 += img->Border;
824 }
825 else {
826 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
827 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
828 }
829
830 /* fetch texel colors */
831 if (useBorderColor & I0BIT) {
832 get_border_color(tObj, img, t0);
833 }
834 else {
835 img->FetchTexelf(img, i0, 0, 0, t0);
836 }
837 if (useBorderColor & I1BIT) {
838 get_border_color(tObj, img, t1);
839 }
840 else {
841 img->FetchTexelf(img, i1, 0, 0, t1);
842 }
843
844 lerp_rgba(rgba, a, t0, t1);
845 }
846
847
848 static void
849 sample_1d_nearest_mipmap_nearest(GLcontext *ctx,
850 const struct gl_texture_object *tObj,
851 GLuint n, const GLfloat texcoord[][4],
852 const GLfloat lambda[], GLfloat rgba[][4])
853 {
854 GLuint i;
855 ASSERT(lambda != NULL);
856 for (i = 0; i < n; i++) {
857 GLint level = nearest_mipmap_level(tObj, lambda[i]);
858 sample_1d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
859 }
860 }
861
862
863 static void
864 sample_1d_linear_mipmap_nearest(GLcontext *ctx,
865 const struct gl_texture_object *tObj,
866 GLuint n, const GLfloat texcoord[][4],
867 const GLfloat lambda[], GLfloat rgba[][4])
868 {
869 GLuint i;
870 ASSERT(lambda != NULL);
871 for (i = 0; i < n; i++) {
872 GLint level = nearest_mipmap_level(tObj, lambda[i]);
873 sample_1d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
874 }
875 }
876
877
878 static void
879 sample_1d_nearest_mipmap_linear(GLcontext *ctx,
880 const struct gl_texture_object *tObj,
881 GLuint n, const GLfloat texcoord[][4],
882 const GLfloat lambda[], GLfloat rgba[][4])
883 {
884 GLuint i;
885 ASSERT(lambda != NULL);
886 for (i = 0; i < n; i++) {
887 GLint level = linear_mipmap_level(tObj, lambda[i]);
888 if (level >= tObj->_MaxLevel) {
889 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
890 texcoord[i], rgba[i]);
891 }
892 else {
893 GLfloat t0[4], t1[4];
894 const GLfloat f = FRAC(lambda[i]);
895 sample_1d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
896 sample_1d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
897 lerp_rgba(rgba[i], f, t0, t1);
898 }
899 }
900 }
901
902
903 static void
904 sample_1d_linear_mipmap_linear(GLcontext *ctx,
905 const struct gl_texture_object *tObj,
906 GLuint n, const GLfloat texcoord[][4],
907 const GLfloat lambda[], GLfloat rgba[][4])
908 {
909 GLuint i;
910 ASSERT(lambda != NULL);
911 for (i = 0; i < n; i++) {
912 GLint level = linear_mipmap_level(tObj, lambda[i]);
913 if (level >= tObj->_MaxLevel) {
914 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
915 texcoord[i], rgba[i]);
916 }
917 else {
918 GLfloat t0[4], t1[4];
919 const GLfloat f = FRAC(lambda[i]);
920 sample_1d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
921 sample_1d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
922 lerp_rgba(rgba[i], f, t0, t1);
923 }
924 }
925 }
926
927
928 /** Sample 1D texture, nearest filtering for both min/magnification */
929 static void
930 sample_nearest_1d( GLcontext *ctx,
931 const struct gl_texture_object *tObj, GLuint n,
932 const GLfloat texcoords[][4], const GLfloat lambda[],
933 GLfloat rgba[][4] )
934 {
935 GLuint i;
936 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
937 (void) lambda;
938 for (i = 0; i < n; i++) {
939 sample_1d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
940 }
941 }
942
943
944 /** Sample 1D texture, linear filtering for both min/magnification */
945 static void
946 sample_linear_1d( GLcontext *ctx,
947 const struct gl_texture_object *tObj, GLuint n,
948 const GLfloat texcoords[][4], const GLfloat lambda[],
949 GLfloat rgba[][4] )
950 {
951 GLuint i;
952 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
953 (void) lambda;
954 for (i = 0; i < n; i++) {
955 sample_1d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
956 }
957 }
958
959
960 /** Sample 1D texture, using lambda to choose between min/magnification */
961 static void
962 sample_lambda_1d( GLcontext *ctx,
963 const struct gl_texture_object *tObj, GLuint n,
964 const GLfloat texcoords[][4],
965 const GLfloat lambda[], GLfloat rgba[][4] )
966 {
967 GLuint minStart, minEnd; /* texels with minification */
968 GLuint magStart, magEnd; /* texels with magnification */
969 GLuint i;
970
971 ASSERT(lambda != NULL);
972 compute_min_mag_ranges(tObj, n, lambda,
973 &minStart, &minEnd, &magStart, &magEnd);
974
975 if (minStart < minEnd) {
976 /* do the minified texels */
977 const GLuint m = minEnd - minStart;
978 switch (tObj->MinFilter) {
979 case GL_NEAREST:
980 for (i = minStart; i < minEnd; i++)
981 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
982 texcoords[i], rgba[i]);
983 break;
984 case GL_LINEAR:
985 for (i = minStart; i < minEnd; i++)
986 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
987 texcoords[i], rgba[i]);
988 break;
989 case GL_NEAREST_MIPMAP_NEAREST:
990 sample_1d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
991 lambda + minStart, rgba + minStart);
992 break;
993 case GL_LINEAR_MIPMAP_NEAREST:
994 sample_1d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
995 lambda + minStart, rgba + minStart);
996 break;
997 case GL_NEAREST_MIPMAP_LINEAR:
998 sample_1d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
999 lambda + minStart, rgba + minStart);
1000 break;
1001 case GL_LINEAR_MIPMAP_LINEAR:
1002 sample_1d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1003 lambda + minStart, rgba + minStart);
1004 break;
1005 default:
1006 _mesa_problem(ctx, "Bad min filter in sample_1d_texture");
1007 return;
1008 }
1009 }
1010
1011 if (magStart < magEnd) {
1012 /* do the magnified texels */
1013 switch (tObj->MagFilter) {
1014 case GL_NEAREST:
1015 for (i = magStart; i < magEnd; i++)
1016 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1017 texcoords[i], rgba[i]);
1018 break;
1019 case GL_LINEAR:
1020 for (i = magStart; i < magEnd; i++)
1021 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1022 texcoords[i], rgba[i]);
1023 break;
1024 default:
1025 _mesa_problem(ctx, "Bad mag filter in sample_1d_texture");
1026 return;
1027 }
1028 }
1029 }
1030
1031
1032 /**********************************************************************/
1033 /* 2-D Texture Sampling Functions */
1034 /**********************************************************************/
1035
1036
1037 /**
1038 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1039 */
1040 static INLINE void
1041 sample_2d_nearest(GLcontext *ctx,
1042 const struct gl_texture_object *tObj,
1043 const struct gl_texture_image *img,
1044 const GLfloat texcoord[4],
1045 GLfloat rgba[])
1046 {
1047 const GLint width = img->Width2; /* without border, power of two */
1048 const GLint height = img->Height2; /* without border, power of two */
1049 GLint i, j;
1050 (void) ctx;
1051
1052 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
1053 j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
1054
1055 /* skip over the border, if any */
1056 i += img->Border;
1057 j += img->Border;
1058
1059 if (i < 0 || i >= (GLint) img->Width || j < 0 || j >= (GLint) img->Height) {
1060 /* Need this test for GL_CLAMP_TO_BORDER mode */
1061 get_border_color(tObj, img, rgba);
1062 }
1063 else {
1064 img->FetchTexelf(img, i, j, 0, rgba);
1065 }
1066 }
1067
1068
1069 /**
1070 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1071 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1072 */
1073 static INLINE void
1074 sample_2d_linear(GLcontext *ctx,
1075 const struct gl_texture_object *tObj,
1076 const struct gl_texture_image *img,
1077 const GLfloat texcoord[4],
1078 GLfloat rgba[])
1079 {
1080 const GLint width = img->Width2;
1081 const GLint height = img->Height2;
1082 GLint i0, j0, i1, j1;
1083 GLbitfield useBorderColor = 0x0;
1084 GLfloat a, b;
1085 GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
1086
1087 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
1088 linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
1089
1090 if (img->Border) {
1091 i0 += img->Border;
1092 i1 += img->Border;
1093 j0 += img->Border;
1094 j1 += img->Border;
1095 }
1096 else {
1097 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
1098 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
1099 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
1100 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
1101 }
1102
1103 /* fetch four texel colors */
1104 if (useBorderColor & (I0BIT | J0BIT)) {
1105 get_border_color(tObj, img, t00);
1106 }
1107 else {
1108 img->FetchTexelf(img, i0, j0, 0, t00);
1109 }
1110 if (useBorderColor & (I1BIT | J0BIT)) {
1111 get_border_color(tObj, img, t10);
1112 }
1113 else {
1114 img->FetchTexelf(img, i1, j0, 0, t10);
1115 }
1116 if (useBorderColor & (I0BIT | J1BIT)) {
1117 get_border_color(tObj, img, t01);
1118 }
1119 else {
1120 img->FetchTexelf(img, i0, j1, 0, t01);
1121 }
1122 if (useBorderColor & (I1BIT | J1BIT)) {
1123 get_border_color(tObj, img, t11);
1124 }
1125 else {
1126 img->FetchTexelf(img, i1, j1, 0, t11);
1127 }
1128
1129 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
1130 }
1131
1132
1133 /**
1134 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1135 * We don't have to worry about the texture border.
1136 */
1137 static INLINE void
1138 sample_2d_linear_repeat(GLcontext *ctx,
1139 const struct gl_texture_object *tObj,
1140 const struct gl_texture_image *img,
1141 const GLfloat texcoord[4],
1142 GLfloat rgba[])
1143 {
1144 const GLint width = img->Width2;
1145 const GLint height = img->Height2;
1146 GLint i0, j0, i1, j1;
1147 GLfloat wi, wj;
1148 GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
1149
1150 (void) ctx;
1151
1152 ASSERT(tObj->WrapS == GL_REPEAT);
1153 ASSERT(tObj->WrapT == GL_REPEAT);
1154 ASSERT(img->Border == 0);
1155 ASSERT(img->TexFormat->BaseFormat != GL_COLOR_INDEX);
1156 ASSERT(img->_IsPowerOfTwo);
1157
1158 linear_repeat_texel_location(width, texcoord[0], &i0, &i1, &wi);
1159 linear_repeat_texel_location(height, texcoord[1], &j0, &j1, &wj);
1160
1161 img->FetchTexelf(img, i0, j0, 0, t00);
1162 img->FetchTexelf(img, i1, j0, 0, t10);
1163 img->FetchTexelf(img, i0, j1, 0, t01);
1164 img->FetchTexelf(img, i1, j1, 0, t11);
1165
1166 lerp_rgba_2d(rgba, wi, wj, t00, t10, t01, t11);
1167 }
1168
1169
1170 static void
1171 sample_2d_nearest_mipmap_nearest(GLcontext *ctx,
1172 const struct gl_texture_object *tObj,
1173 GLuint n, const GLfloat texcoord[][4],
1174 const GLfloat lambda[], GLfloat rgba[][4])
1175 {
1176 GLuint i;
1177 for (i = 0; i < n; i++) {
1178 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1179 sample_2d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1180 }
1181 }
1182
1183
1184 static void
1185 sample_2d_linear_mipmap_nearest(GLcontext *ctx,
1186 const struct gl_texture_object *tObj,
1187 GLuint n, const GLfloat texcoord[][4],
1188 const GLfloat lambda[], GLfloat rgba[][4])
1189 {
1190 GLuint i;
1191 ASSERT(lambda != NULL);
1192 for (i = 0; i < n; i++) {
1193 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1194 sample_2d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1195 }
1196 }
1197
1198
1199 static void
1200 sample_2d_nearest_mipmap_linear(GLcontext *ctx,
1201 const struct gl_texture_object *tObj,
1202 GLuint n, const GLfloat texcoord[][4],
1203 const GLfloat lambda[], GLfloat rgba[][4])
1204 {
1205 GLuint i;
1206 ASSERT(lambda != NULL);
1207 for (i = 0; i < n; i++) {
1208 GLint level = linear_mipmap_level(tObj, lambda[i]);
1209 if (level >= tObj->_MaxLevel) {
1210 sample_2d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1211 texcoord[i], rgba[i]);
1212 }
1213 else {
1214 GLfloat t0[4], t1[4]; /* texels */
1215 const GLfloat f = FRAC(lambda[i]);
1216 sample_2d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1217 sample_2d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1218 lerp_rgba(rgba[i], f, t0, t1);
1219 }
1220 }
1221 }
1222
1223
1224 static void
1225 sample_2d_linear_mipmap_linear( GLcontext *ctx,
1226 const struct gl_texture_object *tObj,
1227 GLuint n, const GLfloat texcoord[][4],
1228 const GLfloat lambda[], GLfloat rgba[][4] )
1229 {
1230 GLuint i;
1231 ASSERT(lambda != NULL);
1232 for (i = 0; i < n; i++) {
1233 GLint level = linear_mipmap_level(tObj, lambda[i]);
1234 if (level >= tObj->_MaxLevel) {
1235 sample_2d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1236 texcoord[i], rgba[i]);
1237 }
1238 else {
1239 GLfloat t0[4], t1[4]; /* texels */
1240 const GLfloat f = FRAC(lambda[i]);
1241 sample_2d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1242 sample_2d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1243 lerp_rgba(rgba[i], f, t0, t1);
1244 }
1245 }
1246 }
1247
1248
1249 static void
1250 sample_2d_linear_mipmap_linear_repeat(GLcontext *ctx,
1251 const struct gl_texture_object *tObj,
1252 GLuint n, const GLfloat texcoord[][4],
1253 const GLfloat lambda[], GLfloat rgba[][4])
1254 {
1255 GLuint i;
1256 ASSERT(lambda != NULL);
1257 ASSERT(tObj->WrapS == GL_REPEAT);
1258 ASSERT(tObj->WrapT == GL_REPEAT);
1259 for (i = 0; i < n; i++) {
1260 GLint level = linear_mipmap_level(tObj, lambda[i]);
1261 if (level >= tObj->_MaxLevel) {
1262 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1263 texcoord[i], rgba[i]);
1264 }
1265 else {
1266 GLfloat t0[4], t1[4]; /* texels */
1267 const GLfloat f = FRAC(lambda[i]);
1268 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level ],
1269 texcoord[i], t0);
1270 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level+1],
1271 texcoord[i], t1);
1272 lerp_rgba(rgba[i], f, t0, t1);
1273 }
1274 }
1275 }
1276
1277
1278 /** Sample 2D texture, nearest filtering for both min/magnification */
1279 static void
1280 sample_nearest_2d(GLcontext *ctx,
1281 const struct gl_texture_object *tObj, GLuint n,
1282 const GLfloat texcoords[][4],
1283 const GLfloat lambda[], GLfloat rgba[][4])
1284 {
1285 GLuint i;
1286 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1287 (void) lambda;
1288 for (i = 0; i < n; i++) {
1289 sample_2d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
1290 }
1291 }
1292
1293
1294 /** Sample 2D texture, linear filtering for both min/magnification */
1295 static void
1296 sample_linear_2d(GLcontext *ctx,
1297 const struct gl_texture_object *tObj, GLuint n,
1298 const GLfloat texcoords[][4],
1299 const GLfloat lambda[], GLfloat rgba[][4])
1300 {
1301 GLuint i;
1302 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1303 (void) lambda;
1304 if (tObj->WrapS == GL_REPEAT &&
1305 tObj->WrapT == GL_REPEAT &&
1306 image->_IsPowerOfTwo &&
1307 image->Border == 0) {
1308 for (i = 0; i < n; i++) {
1309 sample_2d_linear_repeat(ctx, tObj, image, texcoords[i], rgba[i]);
1310 }
1311 }
1312 else {
1313 for (i = 0; i < n; i++) {
1314 sample_2d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
1315 }
1316 }
1317 }
1318
1319
1320 /**
1321 * Optimized 2-D texture sampling:
1322 * S and T wrap mode == GL_REPEAT
1323 * GL_NEAREST min/mag filter
1324 * No border,
1325 * RowStride == Width,
1326 * Format = GL_RGB
1327 */
1328 static void
1329 opt_sample_rgb_2d(GLcontext *ctx,
1330 const struct gl_texture_object *tObj,
1331 GLuint n, const GLfloat texcoords[][4],
1332 const GLfloat lambda[], GLfloat rgba[][4])
1333 {
1334 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1335 const GLfloat width = (GLfloat) img->Width;
1336 const GLfloat height = (GLfloat) img->Height;
1337 const GLint colMask = img->Width - 1;
1338 const GLint rowMask = img->Height - 1;
1339 const GLint shift = img->WidthLog2;
1340 GLuint k;
1341 (void) ctx;
1342 (void) lambda;
1343 ASSERT(tObj->WrapS==GL_REPEAT);
1344 ASSERT(tObj->WrapT==GL_REPEAT);
1345 ASSERT(img->Border==0);
1346 ASSERT(img->TexFormat->MesaFormat==MESA_FORMAT_RGB);
1347 ASSERT(img->_IsPowerOfTwo);
1348
1349 for (k=0; k<n; k++) {
1350 GLint i = IFLOOR(texcoords[k][0] * width) & colMask;
1351 GLint j = IFLOOR(texcoords[k][1] * height) & rowMask;
1352 GLint pos = (j << shift) | i;
1353 GLchan *texel = ((GLchan *) img->Data) + 3*pos;
1354 rgba[k][RCOMP] = CHAN_TO_FLOAT(texel[0]);
1355 rgba[k][GCOMP] = CHAN_TO_FLOAT(texel[1]);
1356 rgba[k][BCOMP] = CHAN_TO_FLOAT(texel[2]);
1357 }
1358 }
1359
1360
1361 /**
1362 * Optimized 2-D texture sampling:
1363 * S and T wrap mode == GL_REPEAT
1364 * GL_NEAREST min/mag filter
1365 * No border
1366 * RowStride == Width,
1367 * Format = GL_RGBA
1368 */
1369 static void
1370 opt_sample_rgba_2d(GLcontext *ctx,
1371 const struct gl_texture_object *tObj,
1372 GLuint n, const GLfloat texcoords[][4],
1373 const GLfloat lambda[], GLfloat rgba[][4])
1374 {
1375 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1376 const GLfloat width = (GLfloat) img->Width;
1377 const GLfloat height = (GLfloat) img->Height;
1378 const GLint colMask = img->Width - 1;
1379 const GLint rowMask = img->Height - 1;
1380 const GLint shift = img->WidthLog2;
1381 GLuint i;
1382 (void) ctx;
1383 (void) lambda;
1384 ASSERT(tObj->WrapS==GL_REPEAT);
1385 ASSERT(tObj->WrapT==GL_REPEAT);
1386 ASSERT(img->Border==0);
1387 ASSERT(img->TexFormat->MesaFormat==MESA_FORMAT_RGBA);
1388 ASSERT(img->_IsPowerOfTwo);
1389
1390 for (i = 0; i < n; i++) {
1391 const GLint col = IFLOOR(texcoords[i][0] * width) & colMask;
1392 const GLint row = IFLOOR(texcoords[i][1] * height) & rowMask;
1393 const GLint pos = (row << shift) | col;
1394 const GLchan *texel = ((GLchan *) img->Data) + (pos << 2); /* pos*4 */
1395 rgba[i][RCOMP] = CHAN_TO_FLOAT(texel[0]);
1396 rgba[i][GCOMP] = CHAN_TO_FLOAT(texel[1]);
1397 rgba[i][BCOMP] = CHAN_TO_FLOAT(texel[2]);
1398 rgba[i][ACOMP] = CHAN_TO_FLOAT(texel[3]);
1399 }
1400 }
1401
1402
1403 /** Sample 2D texture, using lambda to choose between min/magnification */
1404 static void
1405 sample_lambda_2d(GLcontext *ctx,
1406 const struct gl_texture_object *tObj,
1407 GLuint n, const GLfloat texcoords[][4],
1408 const GLfloat lambda[], GLfloat rgba[][4])
1409 {
1410 const struct gl_texture_image *tImg = tObj->Image[0][tObj->BaseLevel];
1411 GLuint minStart, minEnd; /* texels with minification */
1412 GLuint magStart, magEnd; /* texels with magnification */
1413
1414 const GLboolean repeatNoBorderPOT = (tObj->WrapS == GL_REPEAT)
1415 && (tObj->WrapT == GL_REPEAT)
1416 && (tImg->Border == 0 && (tImg->Width == tImg->RowStride))
1417 && (tImg->TexFormat->BaseFormat != GL_COLOR_INDEX)
1418 && tImg->_IsPowerOfTwo;
1419
1420 ASSERT(lambda != NULL);
1421 compute_min_mag_ranges(tObj, n, lambda,
1422 &minStart, &minEnd, &magStart, &magEnd);
1423
1424 if (minStart < minEnd) {
1425 /* do the minified texels */
1426 const GLuint m = minEnd - minStart;
1427 switch (tObj->MinFilter) {
1428 case GL_NEAREST:
1429 if (repeatNoBorderPOT) {
1430 switch (tImg->TexFormat->MesaFormat) {
1431 case MESA_FORMAT_RGB:
1432 opt_sample_rgb_2d(ctx, tObj, m, texcoords + minStart,
1433 NULL, rgba + minStart);
1434 break;
1435 case MESA_FORMAT_RGBA:
1436 opt_sample_rgba_2d(ctx, tObj, m, texcoords + minStart,
1437 NULL, rgba + minStart);
1438 break;
1439 default:
1440 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1441 NULL, rgba + minStart );
1442 }
1443 }
1444 else {
1445 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1446 NULL, rgba + minStart);
1447 }
1448 break;
1449 case GL_LINEAR:
1450 sample_linear_2d(ctx, tObj, m, texcoords + minStart,
1451 NULL, rgba + minStart);
1452 break;
1453 case GL_NEAREST_MIPMAP_NEAREST:
1454 sample_2d_nearest_mipmap_nearest(ctx, tObj, m,
1455 texcoords + minStart,
1456 lambda + minStart, rgba + minStart);
1457 break;
1458 case GL_LINEAR_MIPMAP_NEAREST:
1459 sample_2d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1460 lambda + minStart, rgba + minStart);
1461 break;
1462 case GL_NEAREST_MIPMAP_LINEAR:
1463 sample_2d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1464 lambda + minStart, rgba + minStart);
1465 break;
1466 case GL_LINEAR_MIPMAP_LINEAR:
1467 if (repeatNoBorderPOT)
1468 sample_2d_linear_mipmap_linear_repeat(ctx, tObj, m,
1469 texcoords + minStart, lambda + minStart, rgba + minStart);
1470 else
1471 sample_2d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1472 lambda + minStart, rgba + minStart);
1473 break;
1474 default:
1475 _mesa_problem(ctx, "Bad min filter in sample_2d_texture");
1476 return;
1477 }
1478 }
1479
1480 if (magStart < magEnd) {
1481 /* do the magnified texels */
1482 const GLuint m = magEnd - magStart;
1483
1484 switch (tObj->MagFilter) {
1485 case GL_NEAREST:
1486 if (repeatNoBorderPOT) {
1487 switch (tImg->TexFormat->MesaFormat) {
1488 case MESA_FORMAT_RGB:
1489 opt_sample_rgb_2d(ctx, tObj, m, texcoords + magStart,
1490 NULL, rgba + magStart);
1491 break;
1492 case MESA_FORMAT_RGBA:
1493 opt_sample_rgba_2d(ctx, tObj, m, texcoords + magStart,
1494 NULL, rgba + magStart);
1495 break;
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 const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
3183 ASSERT(t->MinFilter == GL_NEAREST);
3184 if (t->WrapS == GL_REPEAT &&
3185 t->WrapT == GL_REPEAT &&
3186 img->_IsPowerOfTwo &&
3187 img->Border == 0 &&
3188 img->TexFormat->MesaFormat == MESA_FORMAT_RGB) {
3189 return &opt_sample_rgb_2d;
3190 }
3191 else if (t->WrapS == GL_REPEAT &&
3192 t->WrapT == GL_REPEAT &&
3193 img->_IsPowerOfTwo &&
3194 img->Border == 0 &&
3195 img->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
3196 return &opt_sample_rgba_2d;
3197 }
3198 else {
3199 return &sample_nearest_2d;
3200 }
3201 }
3202 case GL_TEXTURE_3D:
3203 if (needLambda) {
3204 return &sample_lambda_3d;
3205 }
3206 else if (t->MinFilter == GL_LINEAR) {
3207 return &sample_linear_3d;
3208 }
3209 else {
3210 ASSERT(t->MinFilter == GL_NEAREST);
3211 return &sample_nearest_3d;
3212 }
3213 case GL_TEXTURE_CUBE_MAP:
3214 if (needLambda) {
3215 return &sample_lambda_cube;
3216 }
3217 else if (t->MinFilter == GL_LINEAR) {
3218 return &sample_linear_cube;
3219 }
3220 else {
3221 ASSERT(t->MinFilter == GL_NEAREST);
3222 return &sample_nearest_cube;
3223 }
3224 case GL_TEXTURE_RECTANGLE_NV:
3225 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3226 return &sample_depth_texture;
3227 }
3228 else if (needLambda) {
3229 return &sample_lambda_rect;
3230 }
3231 else if (t->MinFilter == GL_LINEAR) {
3232 return &sample_linear_rect;
3233 }
3234 else {
3235 ASSERT(t->MinFilter == GL_NEAREST);
3236 return &sample_nearest_rect;
3237 }
3238 case GL_TEXTURE_1D_ARRAY_EXT:
3239 if (needLambda) {
3240 return &sample_lambda_1d_array;
3241 }
3242 else if (t->MinFilter == GL_LINEAR) {
3243 return &sample_linear_1d_array;
3244 }
3245 else {
3246 ASSERT(t->MinFilter == GL_NEAREST);
3247 return &sample_nearest_1d_array;
3248 }
3249 case GL_TEXTURE_2D_ARRAY_EXT:
3250 if (needLambda) {
3251 return &sample_lambda_2d_array;
3252 }
3253 else if (t->MinFilter == GL_LINEAR) {
3254 return &sample_linear_2d_array;
3255 }
3256 else {
3257 ASSERT(t->MinFilter == GL_NEAREST);
3258 return &sample_nearest_2d_array;
3259 }
3260 default:
3261 _mesa_problem(ctx,
3262 "invalid target in _swrast_choose_texture_sample_func");
3263 return &null_sample_func;
3264 }
3265 }
3266 }