Merge commit 'origin/master' into radeon-rewrite
[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[], GLchan 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] = texel[0];
1355 rgba[k][GCOMP] = texel[1];
1356 rgba[k][BCOMP] = 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[], GLchan 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 COPY_4V(rgba[i], texel);
1396 }
1397 }
1398
1399
1400 /** Sample 2D texture, using lambda to choose between min/magnification */
1401 static void
1402 sample_lambda_2d(GLcontext *ctx,
1403 const struct gl_texture_object *tObj,
1404 GLuint n, const GLfloat texcoords[][4],
1405 const GLfloat lambda[], GLfloat rgba[][4])
1406 {
1407 const struct gl_texture_image *tImg = tObj->Image[0][tObj->BaseLevel];
1408 GLuint minStart, minEnd; /* texels with minification */
1409 GLuint magStart, magEnd; /* texels with magnification */
1410
1411 const GLboolean repeatNoBorderPOT = (tObj->WrapS == GL_REPEAT)
1412 && (tObj->WrapT == GL_REPEAT)
1413 && (tImg->Border == 0 && (tImg->Width == tImg->RowStride))
1414 && (tImg->TexFormat->BaseFormat != GL_COLOR_INDEX)
1415 && tImg->_IsPowerOfTwo;
1416
1417 ASSERT(lambda != NULL);
1418 compute_min_mag_ranges(tObj, n, lambda,
1419 &minStart, &minEnd, &magStart, &magEnd);
1420
1421 if (minStart < minEnd) {
1422 /* do the minified texels */
1423 const GLuint m = minEnd - minStart;
1424 switch (tObj->MinFilter) {
1425 case GL_NEAREST:
1426 if (repeatNoBorderPOT) {
1427 switch (tImg->TexFormat->MesaFormat) {
1428 #if 0
1429 case MESA_FORMAT_RGB:
1430 opt_sample_rgb_2d(ctx, tObj, m, texcoords + minStart,
1431 NULL, rgba + minStart);
1432 break;
1433 case MESA_FORMAT_RGBA:
1434 opt_sample_rgba_2d(ctx, tObj, m, texcoords + minStart,
1435 NULL, rgba + minStart);
1436 break;
1437 #endif
1438 default:
1439 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1440 NULL, rgba + minStart );
1441 }
1442 }
1443 else {
1444 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1445 NULL, rgba + minStart);
1446 }
1447 break;
1448 case GL_LINEAR:
1449 sample_linear_2d(ctx, tObj, m, texcoords + minStart,
1450 NULL, rgba + minStart);
1451 break;
1452 case GL_NEAREST_MIPMAP_NEAREST:
1453 sample_2d_nearest_mipmap_nearest(ctx, tObj, m,
1454 texcoords + minStart,
1455 lambda + minStart, rgba + minStart);
1456 break;
1457 case GL_LINEAR_MIPMAP_NEAREST:
1458 sample_2d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1459 lambda + minStart, rgba + minStart);
1460 break;
1461 case GL_NEAREST_MIPMAP_LINEAR:
1462 sample_2d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1463 lambda + minStart, rgba + minStart);
1464 break;
1465 case GL_LINEAR_MIPMAP_LINEAR:
1466 if (repeatNoBorderPOT)
1467 sample_2d_linear_mipmap_linear_repeat(ctx, tObj, m,
1468 texcoords + minStart, lambda + minStart, rgba + minStart);
1469 else
1470 sample_2d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1471 lambda + minStart, rgba + minStart);
1472 break;
1473 default:
1474 _mesa_problem(ctx, "Bad min filter in sample_2d_texture");
1475 return;
1476 }
1477 }
1478
1479 if (magStart < magEnd) {
1480 /* do the magnified texels */
1481 const GLuint m = magEnd - magStart;
1482
1483 switch (tObj->MagFilter) {
1484 case GL_NEAREST:
1485 if (repeatNoBorderPOT) {
1486 switch (tImg->TexFormat->MesaFormat) {
1487 #if 0
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 #endif
1497 default:
1498 sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
1499 NULL, rgba + magStart );
1500 }
1501 }
1502 else {
1503 sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
1504 NULL, rgba + magStart);
1505 }
1506 break;
1507 case GL_LINEAR:
1508 sample_linear_2d(ctx, tObj, m, texcoords + magStart,
1509 NULL, rgba + magStart);
1510 break;
1511 default:
1512 _mesa_problem(ctx, "Bad mag filter in sample_lambda_2d");
1513 }
1514 }
1515 }
1516
1517
1518
1519 /**********************************************************************/
1520 /* 3-D Texture Sampling Functions */
1521 /**********************************************************************/
1522
1523 /**
1524 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1525 */
1526 static INLINE void
1527 sample_3d_nearest(GLcontext *ctx,
1528 const struct gl_texture_object *tObj,
1529 const struct gl_texture_image *img,
1530 const GLfloat texcoord[4],
1531 GLfloat rgba[4])
1532 {
1533 const GLint width = img->Width2; /* without border, power of two */
1534 const GLint height = img->Height2; /* without border, power of two */
1535 const GLint depth = img->Depth2; /* without border, power of two */
1536 GLint i, j, k;
1537 (void) ctx;
1538
1539 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
1540 j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
1541 k = nearest_texel_location(tObj->WrapR, img, depth, texcoord[2]);
1542
1543 if (i < 0 || i >= (GLint) img->Width ||
1544 j < 0 || j >= (GLint) img->Height ||
1545 k < 0 || k >= (GLint) img->Depth) {
1546 /* Need this test for GL_CLAMP_TO_BORDER mode */
1547 get_border_color(tObj, img, rgba);
1548 }
1549 else {
1550 img->FetchTexelf(img, i, j, k, rgba);
1551 }
1552 }
1553
1554
1555 /**
1556 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1557 */
1558 static void
1559 sample_3d_linear(GLcontext *ctx,
1560 const struct gl_texture_object *tObj,
1561 const struct gl_texture_image *img,
1562 const GLfloat texcoord[4],
1563 GLfloat rgba[4])
1564 {
1565 const GLint width = img->Width2;
1566 const GLint height = img->Height2;
1567 const GLint depth = img->Depth2;
1568 GLint i0, j0, k0, i1, j1, k1;
1569 GLbitfield useBorderColor = 0x0;
1570 GLfloat a, b, c;
1571 GLfloat t000[4], t010[4], t001[4], t011[4];
1572 GLfloat t100[4], t110[4], t101[4], t111[4];
1573
1574 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
1575 linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
1576 linear_texel_locations(tObj->WrapR, img, depth, texcoord[2], &k0, &k1, &c);
1577
1578 if (img->Border) {
1579 i0 += img->Border;
1580 i1 += img->Border;
1581 j0 += img->Border;
1582 j1 += img->Border;
1583 k0 += img->Border;
1584 k1 += img->Border;
1585 }
1586 else {
1587 /* check if sampling texture border color */
1588 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
1589 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
1590 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
1591 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
1592 if (k0 < 0 || k0 >= depth) useBorderColor |= K0BIT;
1593 if (k1 < 0 || k1 >= depth) useBorderColor |= K1BIT;
1594 }
1595
1596 /* Fetch texels */
1597 if (useBorderColor & (I0BIT | J0BIT | K0BIT)) {
1598 get_border_color(tObj, img, t000);
1599 }
1600 else {
1601 img->FetchTexelf(img, i0, j0, k0, t000);
1602 }
1603 if (useBorderColor & (I1BIT | J0BIT | K0BIT)) {
1604 get_border_color(tObj, img, t100);
1605 }
1606 else {
1607 img->FetchTexelf(img, i1, j0, k0, t100);
1608 }
1609 if (useBorderColor & (I0BIT | J1BIT | K0BIT)) {
1610 get_border_color(tObj, img, t010);
1611 }
1612 else {
1613 img->FetchTexelf(img, i0, j1, k0, t010);
1614 }
1615 if (useBorderColor & (I1BIT | J1BIT | K0BIT)) {
1616 get_border_color(tObj, img, t110);
1617 }
1618 else {
1619 img->FetchTexelf(img, i1, j1, k0, t110);
1620 }
1621
1622 if (useBorderColor & (I0BIT | J0BIT | K1BIT)) {
1623 get_border_color(tObj, img, t001);
1624 }
1625 else {
1626 img->FetchTexelf(img, i0, j0, k1, t001);
1627 }
1628 if (useBorderColor & (I1BIT | J0BIT | K1BIT)) {
1629 get_border_color(tObj, img, t101);
1630 }
1631 else {
1632 img->FetchTexelf(img, i1, j0, k1, t101);
1633 }
1634 if (useBorderColor & (I0BIT | J1BIT | K1BIT)) {
1635 get_border_color(tObj, img, t011);
1636 }
1637 else {
1638 img->FetchTexelf(img, i0, j1, k1, t011);
1639 }
1640 if (useBorderColor & (I1BIT | J1BIT | K1BIT)) {
1641 get_border_color(tObj, img, t111);
1642 }
1643 else {
1644 img->FetchTexelf(img, i1, j1, k1, t111);
1645 }
1646
1647 /* trilinear interpolation of samples */
1648 lerp_rgba_3d(rgba, a, b, c, t000, t100, t010, t110, t001, t101, t011, t111);
1649 }
1650
1651
1652 static void
1653 sample_3d_nearest_mipmap_nearest(GLcontext *ctx,
1654 const struct gl_texture_object *tObj,
1655 GLuint n, const GLfloat texcoord[][4],
1656 const GLfloat lambda[], GLfloat rgba[][4] )
1657 {
1658 GLuint i;
1659 for (i = 0; i < n; i++) {
1660 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1661 sample_3d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1662 }
1663 }
1664
1665
1666 static void
1667 sample_3d_linear_mipmap_nearest(GLcontext *ctx,
1668 const struct gl_texture_object *tObj,
1669 GLuint n, const GLfloat texcoord[][4],
1670 const GLfloat lambda[], GLfloat rgba[][4])
1671 {
1672 GLuint i;
1673 ASSERT(lambda != NULL);
1674 for (i = 0; i < n; i++) {
1675 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1676 sample_3d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1677 }
1678 }
1679
1680
1681 static void
1682 sample_3d_nearest_mipmap_linear(GLcontext *ctx,
1683 const struct gl_texture_object *tObj,
1684 GLuint n, const GLfloat texcoord[][4],
1685 const GLfloat lambda[], GLfloat rgba[][4])
1686 {
1687 GLuint i;
1688 ASSERT(lambda != NULL);
1689 for (i = 0; i < n; i++) {
1690 GLint level = linear_mipmap_level(tObj, lambda[i]);
1691 if (level >= tObj->_MaxLevel) {
1692 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1693 texcoord[i], rgba[i]);
1694 }
1695 else {
1696 GLfloat t0[4], t1[4]; /* texels */
1697 const GLfloat f = FRAC(lambda[i]);
1698 sample_3d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1699 sample_3d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1700 lerp_rgba(rgba[i], f, t0, t1);
1701 }
1702 }
1703 }
1704
1705
1706 static void
1707 sample_3d_linear_mipmap_linear(GLcontext *ctx,
1708 const struct gl_texture_object *tObj,
1709 GLuint n, const GLfloat texcoord[][4],
1710 const GLfloat lambda[], GLfloat rgba[][4])
1711 {
1712 GLuint i;
1713 ASSERT(lambda != NULL);
1714 for (i = 0; i < n; i++) {
1715 GLint level = linear_mipmap_level(tObj, lambda[i]);
1716 if (level >= tObj->_MaxLevel) {
1717 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1718 texcoord[i], rgba[i]);
1719 }
1720 else {
1721 GLfloat t0[4], t1[4]; /* texels */
1722 const GLfloat f = FRAC(lambda[i]);
1723 sample_3d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1724 sample_3d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1725 lerp_rgba(rgba[i], f, t0, t1);
1726 }
1727 }
1728 }
1729
1730
1731 /** Sample 3D texture, nearest filtering for both min/magnification */
1732 static void
1733 sample_nearest_3d(GLcontext *ctx,
1734 const struct gl_texture_object *tObj, GLuint n,
1735 const GLfloat texcoords[][4], const GLfloat lambda[],
1736 GLfloat rgba[][4])
1737 {
1738 GLuint i;
1739 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1740 (void) lambda;
1741 for (i = 0; i < n; i++) {
1742 sample_3d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
1743 }
1744 }
1745
1746
1747 /** Sample 3D texture, linear filtering for both min/magnification */
1748 static void
1749 sample_linear_3d(GLcontext *ctx,
1750 const struct gl_texture_object *tObj, GLuint n,
1751 const GLfloat texcoords[][4],
1752 const GLfloat lambda[], GLfloat rgba[][4])
1753 {
1754 GLuint i;
1755 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1756 (void) lambda;
1757 for (i = 0; i < n; i++) {
1758 sample_3d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
1759 }
1760 }
1761
1762
1763 /** Sample 3D texture, using lambda to choose between min/magnification */
1764 static void
1765 sample_lambda_3d(GLcontext *ctx,
1766 const struct gl_texture_object *tObj, GLuint n,
1767 const GLfloat texcoords[][4], const GLfloat lambda[],
1768 GLfloat rgba[][4])
1769 {
1770 GLuint minStart, minEnd; /* texels with minification */
1771 GLuint magStart, magEnd; /* texels with magnification */
1772 GLuint i;
1773
1774 ASSERT(lambda != NULL);
1775 compute_min_mag_ranges(tObj, n, lambda,
1776 &minStart, &minEnd, &magStart, &magEnd);
1777
1778 if (minStart < minEnd) {
1779 /* do the minified texels */
1780 GLuint m = minEnd - minStart;
1781 switch (tObj->MinFilter) {
1782 case GL_NEAREST:
1783 for (i = minStart; i < minEnd; i++)
1784 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1785 texcoords[i], rgba[i]);
1786 break;
1787 case GL_LINEAR:
1788 for (i = minStart; i < minEnd; i++)
1789 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1790 texcoords[i], rgba[i]);
1791 break;
1792 case GL_NEAREST_MIPMAP_NEAREST:
1793 sample_3d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1794 lambda + minStart, rgba + minStart);
1795 break;
1796 case GL_LINEAR_MIPMAP_NEAREST:
1797 sample_3d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1798 lambda + minStart, rgba + minStart);
1799 break;
1800 case GL_NEAREST_MIPMAP_LINEAR:
1801 sample_3d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1802 lambda + minStart, rgba + minStart);
1803 break;
1804 case GL_LINEAR_MIPMAP_LINEAR:
1805 sample_3d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1806 lambda + minStart, rgba + minStart);
1807 break;
1808 default:
1809 _mesa_problem(ctx, "Bad min filter in sample_3d_texture");
1810 return;
1811 }
1812 }
1813
1814 if (magStart < magEnd) {
1815 /* do the magnified texels */
1816 switch (tObj->MagFilter) {
1817 case GL_NEAREST:
1818 for (i = magStart; i < magEnd; i++)
1819 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1820 texcoords[i], rgba[i]);
1821 break;
1822 case GL_LINEAR:
1823 for (i = magStart; i < magEnd; i++)
1824 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1825 texcoords[i], rgba[i]);
1826 break;
1827 default:
1828 _mesa_problem(ctx, "Bad mag filter in sample_3d_texture");
1829 return;
1830 }
1831 }
1832 }
1833
1834
1835 /**********************************************************************/
1836 /* Texture Cube Map Sampling Functions */
1837 /**********************************************************************/
1838
1839 /**
1840 * Choose one of six sides of a texture cube map given the texture
1841 * coord (rx,ry,rz). Return pointer to corresponding array of texture
1842 * images.
1843 */
1844 static const struct gl_texture_image **
1845 choose_cube_face(const struct gl_texture_object *texObj,
1846 const GLfloat texcoord[4], GLfloat newCoord[4])
1847 {
1848 /*
1849 major axis
1850 direction target sc tc ma
1851 ---------- ------------------------------- --- --- ---
1852 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1853 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1854 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1855 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1856 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1857 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1858 */
1859 const GLfloat rx = texcoord[0];
1860 const GLfloat ry = texcoord[1];
1861 const GLfloat rz = texcoord[2];
1862 const GLfloat arx = FABSF(rx), ary = FABSF(ry), arz = FABSF(rz);
1863 GLuint face;
1864 GLfloat sc, tc, ma;
1865
1866 if (arx > ary && arx > arz) {
1867 if (rx >= 0.0F) {
1868 face = FACE_POS_X;
1869 sc = -rz;
1870 tc = -ry;
1871 ma = arx;
1872 }
1873 else {
1874 face = FACE_NEG_X;
1875 sc = rz;
1876 tc = -ry;
1877 ma = arx;
1878 }
1879 }
1880 else if (ary > arx && ary > arz) {
1881 if (ry >= 0.0F) {
1882 face = FACE_POS_Y;
1883 sc = rx;
1884 tc = rz;
1885 ma = ary;
1886 }
1887 else {
1888 face = FACE_NEG_Y;
1889 sc = rx;
1890 tc = -rz;
1891 ma = ary;
1892 }
1893 }
1894 else {
1895 if (rz > 0.0F) {
1896 face = FACE_POS_Z;
1897 sc = rx;
1898 tc = -ry;
1899 ma = arz;
1900 }
1901 else {
1902 face = FACE_NEG_Z;
1903 sc = -rx;
1904 tc = -ry;
1905 ma = arz;
1906 }
1907 }
1908
1909 newCoord[0] = ( sc / ma + 1.0F ) * 0.5F;
1910 newCoord[1] = ( tc / ma + 1.0F ) * 0.5F;
1911 return (const struct gl_texture_image **) texObj->Image[face];
1912 }
1913
1914
1915 static void
1916 sample_nearest_cube(GLcontext *ctx,
1917 const struct gl_texture_object *tObj, GLuint n,
1918 const GLfloat texcoords[][4], const GLfloat lambda[],
1919 GLfloat rgba[][4])
1920 {
1921 GLuint i;
1922 (void) lambda;
1923 for (i = 0; i < n; i++) {
1924 const struct gl_texture_image **images;
1925 GLfloat newCoord[4];
1926 images = choose_cube_face(tObj, texcoords[i], newCoord);
1927 sample_2d_nearest(ctx, tObj, images[tObj->BaseLevel],
1928 newCoord, rgba[i]);
1929 }
1930 }
1931
1932
1933 static void
1934 sample_linear_cube(GLcontext *ctx,
1935 const struct gl_texture_object *tObj, GLuint n,
1936 const GLfloat texcoords[][4],
1937 const GLfloat lambda[], GLfloat rgba[][4])
1938 {
1939 GLuint i;
1940 (void) lambda;
1941 for (i = 0; i < n; i++) {
1942 const struct gl_texture_image **images;
1943 GLfloat newCoord[4];
1944 images = choose_cube_face(tObj, texcoords[i], newCoord);
1945 sample_2d_linear(ctx, tObj, images[tObj->BaseLevel],
1946 newCoord, rgba[i]);
1947 }
1948 }
1949
1950
1951 static void
1952 sample_cube_nearest_mipmap_nearest(GLcontext *ctx,
1953 const struct gl_texture_object *tObj,
1954 GLuint n, const GLfloat texcoord[][4],
1955 const GLfloat lambda[], GLfloat rgba[][4])
1956 {
1957 GLuint i;
1958 ASSERT(lambda != NULL);
1959 for (i = 0; i < n; i++) {
1960 const struct gl_texture_image **images;
1961 GLfloat newCoord[4];
1962 GLint level;
1963 images = choose_cube_face(tObj, texcoord[i], newCoord);
1964
1965 /* XXX we actually need to recompute lambda here based on the newCoords.
1966 * But we would need the texcoords of adjacent fragments to compute that
1967 * properly, and we don't have those here.
1968 * For now, do an approximation: subtracting 1 from the chosen mipmap
1969 * level seems to work in some test cases.
1970 * The same adjustment is done in the next few functions.
1971 */
1972 level = nearest_mipmap_level(tObj, lambda[i]);
1973 level = MAX2(level - 1, 0);
1974
1975 sample_2d_nearest(ctx, tObj, images[level], newCoord, rgba[i]);
1976 }
1977 }
1978
1979
1980 static void
1981 sample_cube_linear_mipmap_nearest(GLcontext *ctx,
1982 const struct gl_texture_object *tObj,
1983 GLuint n, const GLfloat texcoord[][4],
1984 const GLfloat lambda[], GLfloat rgba[][4])
1985 {
1986 GLuint i;
1987 ASSERT(lambda != NULL);
1988 for (i = 0; i < n; i++) {
1989 const struct gl_texture_image **images;
1990 GLfloat newCoord[4];
1991 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1992 level = MAX2(level - 1, 0); /* see comment above */
1993 images = choose_cube_face(tObj, texcoord[i], newCoord);
1994 sample_2d_linear(ctx, tObj, images[level], newCoord, rgba[i]);
1995 }
1996 }
1997
1998
1999 static void
2000 sample_cube_nearest_mipmap_linear(GLcontext *ctx,
2001 const struct gl_texture_object *tObj,
2002 GLuint n, const GLfloat texcoord[][4],
2003 const GLfloat lambda[], GLfloat rgba[][4])
2004 {
2005 GLuint i;
2006 ASSERT(lambda != NULL);
2007 for (i = 0; i < n; i++) {
2008 const struct gl_texture_image **images;
2009 GLfloat newCoord[4];
2010 GLint level = linear_mipmap_level(tObj, lambda[i]);
2011 level = MAX2(level - 1, 0); /* see comment above */
2012 images = choose_cube_face(tObj, texcoord[i], newCoord);
2013 if (level >= tObj->_MaxLevel) {
2014 sample_2d_nearest(ctx, tObj, images[tObj->_MaxLevel],
2015 newCoord, rgba[i]);
2016 }
2017 else {
2018 GLfloat t0[4], t1[4]; /* texels */
2019 const GLfloat f = FRAC(lambda[i]);
2020 sample_2d_nearest(ctx, tObj, images[level ], newCoord, t0);
2021 sample_2d_nearest(ctx, tObj, images[level+1], newCoord, t1);
2022 lerp_rgba(rgba[i], f, t0, t1);
2023 }
2024 }
2025 }
2026
2027
2028 static void
2029 sample_cube_linear_mipmap_linear(GLcontext *ctx,
2030 const struct gl_texture_object *tObj,
2031 GLuint n, const GLfloat texcoord[][4],
2032 const GLfloat lambda[], GLfloat rgba[][4])
2033 {
2034 GLuint i;
2035 ASSERT(lambda != NULL);
2036 for (i = 0; i < n; i++) {
2037 const struct gl_texture_image **images;
2038 GLfloat newCoord[4];
2039 GLint level = linear_mipmap_level(tObj, lambda[i]);
2040 level = MAX2(level - 1, 0); /* see comment above */
2041 images = choose_cube_face(tObj, texcoord[i], newCoord);
2042 if (level >= tObj->_MaxLevel) {
2043 sample_2d_linear(ctx, tObj, images[tObj->_MaxLevel],
2044 newCoord, rgba[i]);
2045 }
2046 else {
2047 GLfloat t0[4], t1[4];
2048 const GLfloat f = FRAC(lambda[i]);
2049 sample_2d_linear(ctx, tObj, images[level ], newCoord, t0);
2050 sample_2d_linear(ctx, tObj, images[level+1], newCoord, t1);
2051 lerp_rgba(rgba[i], f, t0, t1);
2052 }
2053 }
2054 }
2055
2056
2057 /** Sample cube texture, using lambda to choose between min/magnification */
2058 static void
2059 sample_lambda_cube(GLcontext *ctx,
2060 const struct gl_texture_object *tObj, GLuint n,
2061 const GLfloat texcoords[][4], const GLfloat lambda[],
2062 GLfloat rgba[][4])
2063 {
2064 GLuint minStart, minEnd; /* texels with minification */
2065 GLuint magStart, magEnd; /* texels with magnification */
2066
2067 ASSERT(lambda != NULL);
2068 compute_min_mag_ranges(tObj, n, lambda,
2069 &minStart, &minEnd, &magStart, &magEnd);
2070
2071 if (minStart < minEnd) {
2072 /* do the minified texels */
2073 const GLuint m = minEnd - minStart;
2074 switch (tObj->MinFilter) {
2075 case GL_NEAREST:
2076 sample_nearest_cube(ctx, tObj, m, texcoords + minStart,
2077 lambda + minStart, rgba + minStart);
2078 break;
2079 case GL_LINEAR:
2080 sample_linear_cube(ctx, tObj, m, texcoords + minStart,
2081 lambda + minStart, rgba + minStart);
2082 break;
2083 case GL_NEAREST_MIPMAP_NEAREST:
2084 sample_cube_nearest_mipmap_nearest(ctx, tObj, m,
2085 texcoords + minStart,
2086 lambda + minStart, rgba + minStart);
2087 break;
2088 case GL_LINEAR_MIPMAP_NEAREST:
2089 sample_cube_linear_mipmap_nearest(ctx, tObj, m,
2090 texcoords + minStart,
2091 lambda + minStart, rgba + minStart);
2092 break;
2093 case GL_NEAREST_MIPMAP_LINEAR:
2094 sample_cube_nearest_mipmap_linear(ctx, tObj, m,
2095 texcoords + minStart,
2096 lambda + minStart, rgba + minStart);
2097 break;
2098 case GL_LINEAR_MIPMAP_LINEAR:
2099 sample_cube_linear_mipmap_linear(ctx, tObj, m,
2100 texcoords + minStart,
2101 lambda + minStart, rgba + minStart);
2102 break;
2103 default:
2104 _mesa_problem(ctx, "Bad min filter in sample_lambda_cube");
2105 }
2106 }
2107
2108 if (magStart < magEnd) {
2109 /* do the magnified texels */
2110 const GLuint m = magEnd - magStart;
2111 switch (tObj->MagFilter) {
2112 case GL_NEAREST:
2113 sample_nearest_cube(ctx, tObj, m, texcoords + magStart,
2114 lambda + magStart, rgba + magStart);
2115 break;
2116 case GL_LINEAR:
2117 sample_linear_cube(ctx, tObj, m, texcoords + magStart,
2118 lambda + magStart, rgba + magStart);
2119 break;
2120 default:
2121 _mesa_problem(ctx, "Bad mag filter in sample_lambda_cube");
2122 }
2123 }
2124 }
2125
2126
2127 /**********************************************************************/
2128 /* Texture Rectangle Sampling Functions */
2129 /**********************************************************************/
2130
2131
2132 static void
2133 sample_nearest_rect(GLcontext *ctx,
2134 const struct gl_texture_object *tObj, GLuint n,
2135 const GLfloat texcoords[][4], const GLfloat lambda[],
2136 GLfloat rgba[][4])
2137 {
2138 const struct gl_texture_image *img = tObj->Image[0][0];
2139 const GLint width = img->Width;
2140 const GLint height = img->Height;
2141 GLuint i;
2142
2143 (void) ctx;
2144 (void) lambda;
2145
2146 ASSERT(tObj->WrapS == GL_CLAMP ||
2147 tObj->WrapS == GL_CLAMP_TO_EDGE ||
2148 tObj->WrapS == GL_CLAMP_TO_BORDER);
2149 ASSERT(tObj->WrapT == GL_CLAMP ||
2150 tObj->WrapT == GL_CLAMP_TO_EDGE ||
2151 tObj->WrapT == GL_CLAMP_TO_BORDER);
2152 ASSERT(img->TexFormat->BaseFormat != GL_COLOR_INDEX);
2153
2154 for (i = 0; i < n; i++) {
2155 GLint row, col;
2156 col = clamp_rect_coord_nearest(tObj->WrapS, texcoords[i][0], width);
2157 row = clamp_rect_coord_nearest(tObj->WrapT, texcoords[i][1], height);
2158 if (col < 0 || col >= width || row < 0 || row >= height)
2159 get_border_color(tObj, img, rgba[i]);
2160 else
2161 img->FetchTexelf(img, col, row, 0, rgba[i]);
2162 }
2163 }
2164
2165
2166 static void
2167 sample_linear_rect(GLcontext *ctx,
2168 const struct gl_texture_object *tObj, GLuint n,
2169 const GLfloat texcoords[][4],
2170 const GLfloat lambda[], GLfloat rgba[][4])
2171 {
2172 const struct gl_texture_image *img = tObj->Image[0][0];
2173 const GLint width = img->Width;
2174 const GLint height = img->Height;
2175 GLuint i;
2176
2177 (void) ctx;
2178 (void) lambda;
2179
2180 ASSERT(tObj->WrapS == GL_CLAMP ||
2181 tObj->WrapS == GL_CLAMP_TO_EDGE ||
2182 tObj->WrapS == GL_CLAMP_TO_BORDER);
2183 ASSERT(tObj->WrapT == GL_CLAMP ||
2184 tObj->WrapT == GL_CLAMP_TO_EDGE ||
2185 tObj->WrapT == GL_CLAMP_TO_BORDER);
2186 ASSERT(img->TexFormat->BaseFormat != GL_COLOR_INDEX);
2187
2188 for (i = 0; i < n; i++) {
2189 GLint i0, j0, i1, j1;
2190 GLfloat t00[4], t01[4], t10[4], t11[4];
2191 GLfloat a, b;
2192 GLbitfield useBorderColor = 0x0;
2193
2194 clamp_rect_coord_linear(tObj->WrapS, texcoords[i][0], width,
2195 &i0, &i1, &a);
2196 clamp_rect_coord_linear(tObj->WrapT, texcoords[i][1], height,
2197 &j0, &j1, &b);
2198
2199 /* compute integer rows/columns */
2200 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2201 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2202 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
2203 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
2204
2205 /* get four texel samples */
2206 if (useBorderColor & (I0BIT | J0BIT))
2207 get_border_color(tObj, img, t00);
2208 else
2209 img->FetchTexelf(img, i0, j0, 0, t00);
2210
2211 if (useBorderColor & (I1BIT | J0BIT))
2212 get_border_color(tObj, img, t10);
2213 else
2214 img->FetchTexelf(img, i1, j0, 0, t10);
2215
2216 if (useBorderColor & (I0BIT | J1BIT))
2217 get_border_color(tObj, img, t01);
2218 else
2219 img->FetchTexelf(img, i0, j1, 0, t01);
2220
2221 if (useBorderColor & (I1BIT | J1BIT))
2222 get_border_color(tObj, img, t11);
2223 else
2224 img->FetchTexelf(img, i1, j1, 0, t11);
2225
2226 lerp_rgba_2d(rgba[i], a, b, t00, t10, t01, t11);
2227 }
2228 }
2229
2230
2231 /** Sample Rect texture, using lambda to choose between min/magnification */
2232 static void
2233 sample_lambda_rect(GLcontext *ctx,
2234 const struct gl_texture_object *tObj, GLuint n,
2235 const GLfloat texcoords[][4], const GLfloat lambda[],
2236 GLfloat rgba[][4])
2237 {
2238 GLuint minStart, minEnd, magStart, magEnd;
2239
2240 /* We only need lambda to decide between minification and magnification.
2241 * There is no mipmapping with rectangular textures.
2242 */
2243 compute_min_mag_ranges(tObj, n, lambda,
2244 &minStart, &minEnd, &magStart, &magEnd);
2245
2246 if (minStart < minEnd) {
2247 if (tObj->MinFilter == GL_NEAREST) {
2248 sample_nearest_rect(ctx, tObj, minEnd - minStart,
2249 texcoords + minStart, NULL, rgba + minStart);
2250 }
2251 else {
2252 sample_linear_rect(ctx, tObj, minEnd - minStart,
2253 texcoords + minStart, NULL, rgba + minStart);
2254 }
2255 }
2256 if (magStart < magEnd) {
2257 if (tObj->MagFilter == GL_NEAREST) {
2258 sample_nearest_rect(ctx, tObj, magEnd - magStart,
2259 texcoords + magStart, NULL, rgba + magStart);
2260 }
2261 else {
2262 sample_linear_rect(ctx, tObj, magEnd - magStart,
2263 texcoords + magStart, NULL, rgba + magStart);
2264 }
2265 }
2266 }
2267
2268
2269
2270 /**********************************************************************/
2271 /* 2D Texture Array Sampling Functions */
2272 /**********************************************************************/
2273
2274 /**
2275 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2276 */
2277 static void
2278 sample_2d_array_nearest(GLcontext *ctx,
2279 const struct gl_texture_object *tObj,
2280 const struct gl_texture_image *img,
2281 const GLfloat texcoord[4],
2282 GLfloat rgba[4])
2283 {
2284 const GLint width = img->Width2; /* without border, power of two */
2285 const GLint height = img->Height2; /* without border, power of two */
2286 const GLint depth = img->Depth;
2287 GLint i, j;
2288 GLint array;
2289 (void) ctx;
2290
2291 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
2292 j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
2293 array = clamp_rect_coord_nearest(tObj->WrapR, texcoord[2], depth);
2294
2295 if (i < 0 || i >= (GLint) img->Width ||
2296 j < 0 || j >= (GLint) img->Height ||
2297 array < 0 || array >= (GLint) img->Depth) {
2298 /* Need this test for GL_CLAMP_TO_BORDER mode */
2299 get_border_color(tObj, img, rgba);
2300 }
2301 else {
2302 img->FetchTexelf(img, i, j, array, rgba);
2303 }
2304 }
2305
2306
2307 /**
2308 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2309 */
2310 static void
2311 sample_2d_array_linear(GLcontext *ctx,
2312 const struct gl_texture_object *tObj,
2313 const struct gl_texture_image *img,
2314 const GLfloat texcoord[4],
2315 GLfloat rgba[4])
2316 {
2317 const GLint width = img->Width2;
2318 const GLint height = img->Height2;
2319 const GLint depth = img->Depth;
2320 GLint i0, j0, i1, j1;
2321 GLint array;
2322 GLbitfield useBorderColor = 0x0;
2323 GLfloat a, b;
2324 GLfloat t00[4], t01[4], t10[4], t11[4];
2325
2326 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
2327 linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
2328 array = clamp_rect_coord_nearest(tObj->WrapR, texcoord[2], depth);
2329
2330 if (array < 0 || array >= depth) {
2331 COPY_4V(rgba, tObj->BorderColor);
2332 }
2333 else {
2334 if (img->Border) {
2335 i0 += img->Border;
2336 i1 += img->Border;
2337 j0 += img->Border;
2338 j1 += img->Border;
2339 }
2340 else {
2341 /* check if sampling texture border color */
2342 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2343 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2344 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
2345 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
2346 }
2347
2348 /* Fetch texels */
2349 if (useBorderColor & (I0BIT | J0BIT)) {
2350 get_border_color(tObj, img, t00);
2351 }
2352 else {
2353 img->FetchTexelf(img, i0, j0, array, t00);
2354 }
2355 if (useBorderColor & (I1BIT | J0BIT)) {
2356 get_border_color(tObj, img, t10);
2357 }
2358 else {
2359 img->FetchTexelf(img, i1, j0, array, t10);
2360 }
2361 if (useBorderColor & (I0BIT | J1BIT)) {
2362 get_border_color(tObj, img, t01);
2363 }
2364 else {
2365 img->FetchTexelf(img, i0, j1, array, t01);
2366 }
2367 if (useBorderColor & (I1BIT | J1BIT)) {
2368 get_border_color(tObj, img, t11);
2369 }
2370 else {
2371 img->FetchTexelf(img, i1, j1, array, t11);
2372 }
2373
2374 /* trilinear interpolation of samples */
2375 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
2376 }
2377 }
2378
2379
2380 static void
2381 sample_2d_array_nearest_mipmap_nearest(GLcontext *ctx,
2382 const struct gl_texture_object *tObj,
2383 GLuint n, const GLfloat texcoord[][4],
2384 const GLfloat lambda[], GLfloat rgba[][4])
2385 {
2386 GLuint i;
2387 for (i = 0; i < n; i++) {
2388 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2389 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i],
2390 rgba[i]);
2391 }
2392 }
2393
2394
2395 static void
2396 sample_2d_array_linear_mipmap_nearest(GLcontext *ctx,
2397 const struct gl_texture_object *tObj,
2398 GLuint n, const GLfloat texcoord[][4],
2399 const GLfloat lambda[], GLfloat rgba[][4])
2400 {
2401 GLuint i;
2402 ASSERT(lambda != NULL);
2403 for (i = 0; i < n; i++) {
2404 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2405 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level],
2406 texcoord[i], rgba[i]);
2407 }
2408 }
2409
2410
2411 static void
2412 sample_2d_array_nearest_mipmap_linear(GLcontext *ctx,
2413 const struct gl_texture_object *tObj,
2414 GLuint n, const GLfloat texcoord[][4],
2415 const GLfloat lambda[], GLfloat rgba[][4])
2416 {
2417 GLuint i;
2418 ASSERT(lambda != NULL);
2419 for (i = 0; i < n; i++) {
2420 GLint level = linear_mipmap_level(tObj, lambda[i]);
2421 if (level >= tObj->_MaxLevel) {
2422 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2423 texcoord[i], rgba[i]);
2424 }
2425 else {
2426 GLfloat t0[4], t1[4]; /* texels */
2427 const GLfloat f = FRAC(lambda[i]);
2428 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level ],
2429 texcoord[i], t0);
2430 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level+1],
2431 texcoord[i], t1);
2432 lerp_rgba(rgba[i], f, t0, t1);
2433 }
2434 }
2435 }
2436
2437
2438 static void
2439 sample_2d_array_linear_mipmap_linear(GLcontext *ctx,
2440 const struct gl_texture_object *tObj,
2441 GLuint n, const GLfloat texcoord[][4],
2442 const GLfloat lambda[], GLfloat rgba[][4])
2443 {
2444 GLuint i;
2445 ASSERT(lambda != NULL);
2446 for (i = 0; i < n; i++) {
2447 GLint level = linear_mipmap_level(tObj, lambda[i]);
2448 if (level >= tObj->_MaxLevel) {
2449 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2450 texcoord[i], rgba[i]);
2451 }
2452 else {
2453 GLfloat t0[4], t1[4]; /* texels */
2454 const GLfloat f = FRAC(lambda[i]);
2455 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level ],
2456 texcoord[i], t0);
2457 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level+1],
2458 texcoord[i], t1);
2459 lerp_rgba(rgba[i], f, t0, t1);
2460 }
2461 }
2462 }
2463
2464
2465 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2466 static void
2467 sample_nearest_2d_array(GLcontext *ctx,
2468 const struct gl_texture_object *tObj, GLuint n,
2469 const GLfloat texcoords[][4], const GLfloat lambda[],
2470 GLfloat rgba[][4])
2471 {
2472 GLuint i;
2473 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2474 (void) lambda;
2475 for (i = 0; i < n; i++) {
2476 sample_2d_array_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
2477 }
2478 }
2479
2480
2481
2482 /** Sample 2D Array texture, linear filtering for both min/magnification */
2483 static void
2484 sample_linear_2d_array(GLcontext *ctx,
2485 const struct gl_texture_object *tObj, GLuint n,
2486 const GLfloat texcoords[][4],
2487 const GLfloat lambda[], GLfloat rgba[][4])
2488 {
2489 GLuint i;
2490 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2491 (void) lambda;
2492 for (i = 0; i < n; i++) {
2493 sample_2d_array_linear(ctx, tObj, image, texcoords[i], rgba[i]);
2494 }
2495 }
2496
2497
2498 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2499 static void
2500 sample_lambda_2d_array(GLcontext *ctx,
2501 const struct gl_texture_object *tObj, GLuint n,
2502 const GLfloat texcoords[][4], const GLfloat lambda[],
2503 GLfloat rgba[][4])
2504 {
2505 GLuint minStart, minEnd; /* texels with minification */
2506 GLuint magStart, magEnd; /* texels with magnification */
2507 GLuint i;
2508
2509 ASSERT(lambda != NULL);
2510 compute_min_mag_ranges(tObj, n, lambda,
2511 &minStart, &minEnd, &magStart, &magEnd);
2512
2513 if (minStart < minEnd) {
2514 /* do the minified texels */
2515 GLuint m = minEnd - minStart;
2516 switch (tObj->MinFilter) {
2517 case GL_NEAREST:
2518 for (i = minStart; i < minEnd; i++)
2519 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2520 texcoords[i], rgba[i]);
2521 break;
2522 case GL_LINEAR:
2523 for (i = minStart; i < minEnd; i++)
2524 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2525 texcoords[i], rgba[i]);
2526 break;
2527 case GL_NEAREST_MIPMAP_NEAREST:
2528 sample_2d_array_nearest_mipmap_nearest(ctx, tObj, m,
2529 texcoords + minStart,
2530 lambda + minStart,
2531 rgba + minStart);
2532 break;
2533 case GL_LINEAR_MIPMAP_NEAREST:
2534 sample_2d_array_linear_mipmap_nearest(ctx, tObj, m,
2535 texcoords + minStart,
2536 lambda + minStart,
2537 rgba + minStart);
2538 break;
2539 case GL_NEAREST_MIPMAP_LINEAR:
2540 sample_2d_array_nearest_mipmap_linear(ctx, tObj, m,
2541 texcoords + minStart,
2542 lambda + minStart,
2543 rgba + minStart);
2544 break;
2545 case GL_LINEAR_MIPMAP_LINEAR:
2546 sample_2d_array_linear_mipmap_linear(ctx, tObj, m,
2547 texcoords + minStart,
2548 lambda + minStart,
2549 rgba + minStart);
2550 break;
2551 default:
2552 _mesa_problem(ctx, "Bad min filter in sample_2d_array_texture");
2553 return;
2554 }
2555 }
2556
2557 if (magStart < magEnd) {
2558 /* do the magnified texels */
2559 switch (tObj->MagFilter) {
2560 case GL_NEAREST:
2561 for (i = magStart; i < magEnd; i++)
2562 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2563 texcoords[i], rgba[i]);
2564 break;
2565 case GL_LINEAR:
2566 for (i = magStart; i < magEnd; i++)
2567 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2568 texcoords[i], rgba[i]);
2569 break;
2570 default:
2571 _mesa_problem(ctx, "Bad mag filter in sample_2d_array_texture");
2572 return;
2573 }
2574 }
2575 }
2576
2577
2578
2579
2580 /**********************************************************************/
2581 /* 1D Texture Array Sampling Functions */
2582 /**********************************************************************/
2583
2584 /**
2585 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2586 */
2587 static void
2588 sample_1d_array_nearest(GLcontext *ctx,
2589 const struct gl_texture_object *tObj,
2590 const struct gl_texture_image *img,
2591 const GLfloat texcoord[4],
2592 GLfloat rgba[4])
2593 {
2594 const GLint width = img->Width2; /* without border, power of two */
2595 const GLint height = img->Height;
2596 GLint i;
2597 GLint array;
2598 (void) ctx;
2599
2600 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
2601 array = clamp_rect_coord_nearest(tObj->WrapT, texcoord[1], height);
2602
2603 if (i < 0 || i >= (GLint) img->Width ||
2604 array < 0 || array >= (GLint) img->Height) {
2605 /* Need this test for GL_CLAMP_TO_BORDER mode */
2606 get_border_color(tObj, img, rgba);
2607 }
2608 else {
2609 img->FetchTexelf(img, i, array, 0, rgba);
2610 }
2611 }
2612
2613
2614 /**
2615 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2616 */
2617 static void
2618 sample_1d_array_linear(GLcontext *ctx,
2619 const struct gl_texture_object *tObj,
2620 const struct gl_texture_image *img,
2621 const GLfloat texcoord[4],
2622 GLfloat rgba[4])
2623 {
2624 const GLint width = img->Width2;
2625 const GLint height = img->Height;
2626 GLint i0, i1;
2627 GLint array;
2628 GLbitfield useBorderColor = 0x0;
2629 GLfloat a;
2630 GLfloat t0[4], t1[4];
2631
2632 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
2633 array = clamp_rect_coord_nearest(tObj->WrapT, texcoord[1], height);
2634
2635 if (img->Border) {
2636 i0 += img->Border;
2637 i1 += img->Border;
2638 }
2639 else {
2640 /* check if sampling texture border color */
2641 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2642 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2643 }
2644
2645 if (array < 0 || array >= height) useBorderColor |= K0BIT;
2646
2647 /* Fetch texels */
2648 if (useBorderColor & (I0BIT | K0BIT)) {
2649 get_border_color(tObj, img, t0);
2650 }
2651 else {
2652 img->FetchTexelf(img, i0, array, 0, t0);
2653 }
2654 if (useBorderColor & (I1BIT | K0BIT)) {
2655 get_border_color(tObj, img, t1);
2656 }
2657 else {
2658 img->FetchTexelf(img, i1, array, 0, t1);
2659 }
2660
2661 /* bilinear interpolation of samples */
2662 lerp_rgba(rgba, a, t0, t1);
2663 }
2664
2665
2666 static void
2667 sample_1d_array_nearest_mipmap_nearest(GLcontext *ctx,
2668 const struct gl_texture_object *tObj,
2669 GLuint n, const GLfloat texcoord[][4],
2670 const GLfloat lambda[], GLfloat rgba[][4])
2671 {
2672 GLuint i;
2673 for (i = 0; i < n; i++) {
2674 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2675 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i],
2676 rgba[i]);
2677 }
2678 }
2679
2680
2681 static void
2682 sample_1d_array_linear_mipmap_nearest(GLcontext *ctx,
2683 const struct gl_texture_object *tObj,
2684 GLuint n, const GLfloat texcoord[][4],
2685 const GLfloat lambda[], GLfloat rgba[][4])
2686 {
2687 GLuint i;
2688 ASSERT(lambda != NULL);
2689 for (i = 0; i < n; i++) {
2690 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2691 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level],
2692 texcoord[i], rgba[i]);
2693 }
2694 }
2695
2696
2697 static void
2698 sample_1d_array_nearest_mipmap_linear(GLcontext *ctx,
2699 const struct gl_texture_object *tObj,
2700 GLuint n, const GLfloat texcoord[][4],
2701 const GLfloat lambda[], GLfloat rgba[][4])
2702 {
2703 GLuint i;
2704 ASSERT(lambda != NULL);
2705 for (i = 0; i < n; i++) {
2706 GLint level = linear_mipmap_level(tObj, lambda[i]);
2707 if (level >= tObj->_MaxLevel) {
2708 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2709 texcoord[i], rgba[i]);
2710 }
2711 else {
2712 GLfloat t0[4], t1[4]; /* texels */
2713 const GLfloat f = FRAC(lambda[i]);
2714 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
2715 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
2716 lerp_rgba(rgba[i], f, t0, t1);
2717 }
2718 }
2719 }
2720
2721
2722 static void
2723 sample_1d_array_linear_mipmap_linear(GLcontext *ctx,
2724 const struct gl_texture_object *tObj,
2725 GLuint n, const GLfloat texcoord[][4],
2726 const GLfloat lambda[], GLfloat rgba[][4])
2727 {
2728 GLuint i;
2729 ASSERT(lambda != NULL);
2730 for (i = 0; i < n; i++) {
2731 GLint level = linear_mipmap_level(tObj, lambda[i]);
2732 if (level >= tObj->_MaxLevel) {
2733 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2734 texcoord[i], rgba[i]);
2735 }
2736 else {
2737 GLfloat t0[4], t1[4]; /* texels */
2738 const GLfloat f = FRAC(lambda[i]);
2739 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
2740 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
2741 lerp_rgba(rgba[i], f, t0, t1);
2742 }
2743 }
2744 }
2745
2746
2747 /** Sample 1D Array texture, nearest filtering for both min/magnification */
2748 static void
2749 sample_nearest_1d_array(GLcontext *ctx,
2750 const struct gl_texture_object *tObj, GLuint n,
2751 const GLfloat texcoords[][4], const GLfloat lambda[],
2752 GLfloat rgba[][4])
2753 {
2754 GLuint i;
2755 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2756 (void) lambda;
2757 for (i = 0; i < n; i++) {
2758 sample_1d_array_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
2759 }
2760 }
2761
2762
2763 /** Sample 1D Array texture, linear filtering for both min/magnification */
2764 static void
2765 sample_linear_1d_array(GLcontext *ctx,
2766 const struct gl_texture_object *tObj, GLuint n,
2767 const GLfloat texcoords[][4],
2768 const GLfloat lambda[], GLfloat rgba[][4])
2769 {
2770 GLuint i;
2771 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2772 (void) lambda;
2773 for (i = 0; i < n; i++) {
2774 sample_1d_array_linear(ctx, tObj, image, texcoords[i], rgba[i]);
2775 }
2776 }
2777
2778
2779 /** Sample 1D Array texture, using lambda to choose between min/magnification */
2780 static void
2781 sample_lambda_1d_array(GLcontext *ctx,
2782 const struct gl_texture_object *tObj, GLuint n,
2783 const GLfloat texcoords[][4], const GLfloat lambda[],
2784 GLfloat rgba[][4])
2785 {
2786 GLuint minStart, minEnd; /* texels with minification */
2787 GLuint magStart, magEnd; /* texels with magnification */
2788 GLuint i;
2789
2790 ASSERT(lambda != NULL);
2791 compute_min_mag_ranges(tObj, n, lambda,
2792 &minStart, &minEnd, &magStart, &magEnd);
2793
2794 if (minStart < minEnd) {
2795 /* do the minified texels */
2796 GLuint m = minEnd - minStart;
2797 switch (tObj->MinFilter) {
2798 case GL_NEAREST:
2799 for (i = minStart; i < minEnd; i++)
2800 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2801 texcoords[i], rgba[i]);
2802 break;
2803 case GL_LINEAR:
2804 for (i = minStart; i < minEnd; i++)
2805 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2806 texcoords[i], rgba[i]);
2807 break;
2808 case GL_NEAREST_MIPMAP_NEAREST:
2809 sample_1d_array_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
2810 lambda + minStart, rgba + minStart);
2811 break;
2812 case GL_LINEAR_MIPMAP_NEAREST:
2813 sample_1d_array_linear_mipmap_nearest(ctx, tObj, m,
2814 texcoords + minStart,
2815 lambda + minStart,
2816 rgba + minStart);
2817 break;
2818 case GL_NEAREST_MIPMAP_LINEAR:
2819 sample_1d_array_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
2820 lambda + minStart, rgba + minStart);
2821 break;
2822 case GL_LINEAR_MIPMAP_LINEAR:
2823 sample_1d_array_linear_mipmap_linear(ctx, tObj, m,
2824 texcoords + minStart,
2825 lambda + minStart,
2826 rgba + minStart);
2827 break;
2828 default:
2829 _mesa_problem(ctx, "Bad min filter in sample_1d_array_texture");
2830 return;
2831 }
2832 }
2833
2834 if (magStart < magEnd) {
2835 /* do the magnified texels */
2836 switch (tObj->MagFilter) {
2837 case GL_NEAREST:
2838 for (i = magStart; i < magEnd; i++)
2839 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2840 texcoords[i], rgba[i]);
2841 break;
2842 case GL_LINEAR:
2843 for (i = magStart; i < magEnd; i++)
2844 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2845 texcoords[i], rgba[i]);
2846 break;
2847 default:
2848 _mesa_problem(ctx, "Bad mag filter in sample_1d_array_texture");
2849 return;
2850 }
2851 }
2852 }
2853
2854
2855 /**
2856 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
2857 */
2858 static INLINE GLfloat
2859 shadow_compare(GLenum function, GLfloat coord, GLfloat depthSample,
2860 GLfloat ambient)
2861 {
2862 switch (function) {
2863 case GL_LEQUAL:
2864 return (coord <= depthSample) ? 1.0F : ambient;
2865 case GL_GEQUAL:
2866 return (coord >= depthSample) ? 1.0F : ambient;
2867 case GL_LESS:
2868 return (coord < depthSample) ? 1.0F : ambient;
2869 case GL_GREATER:
2870 return (coord > depthSample) ? 1.0F : ambient;
2871 case GL_EQUAL:
2872 return (coord == depthSample) ? 1.0F : ambient;
2873 case GL_NOTEQUAL:
2874 return (coord != depthSample) ? 1.0F : ambient;
2875 case GL_ALWAYS:
2876 return 1.0F;
2877 case GL_NEVER:
2878 return ambient;
2879 case GL_NONE:
2880 return depthSample;
2881 default:
2882 _mesa_problem(NULL, "Bad compare func in shadow_compare");
2883 return ambient;
2884 }
2885 }
2886
2887
2888 /**
2889 * Compare texcoord against four depth samples.
2890 */
2891 static INLINE GLfloat
2892 shadow_compare4(GLenum function, GLfloat coord,
2893 GLfloat depth00, GLfloat depth01,
2894 GLfloat depth10, GLfloat depth11,
2895 GLfloat ambient, GLfloat wi, GLfloat wj)
2896 {
2897 const GLfloat d = (1.0F - (GLfloat) ambient) * 0.25F;
2898 GLfloat luminance = 1.0F;
2899
2900 switch (function) {
2901 case GL_LEQUAL:
2902 if (depth00 <= coord) luminance -= d;
2903 if (depth01 <= coord) luminance -= d;
2904 if (depth10 <= coord) luminance -= d;
2905 if (depth11 <= coord) luminance -= d;
2906 return luminance;
2907 case GL_GEQUAL:
2908 if (depth00 >= coord) luminance -= d;
2909 if (depth01 >= coord) luminance -= d;
2910 if (depth10 >= coord) luminance -= d;
2911 if (depth11 >= coord) luminance -= d;
2912 return luminance;
2913 case GL_LESS:
2914 if (depth00 < coord) luminance -= d;
2915 if (depth01 < coord) luminance -= d;
2916 if (depth10 < coord) luminance -= d;
2917 if (depth11 < coord) luminance -= d;
2918 return luminance;
2919 case GL_GREATER:
2920 if (depth00 > coord) luminance -= d;
2921 if (depth01 > coord) luminance -= d;
2922 if (depth10 > coord) luminance -= d;
2923 if (depth11 > coord) luminance -= d;
2924 return luminance;
2925 case GL_EQUAL:
2926 if (depth00 == coord) luminance -= d;
2927 if (depth01 == coord) luminance -= d;
2928 if (depth10 == coord) luminance -= d;
2929 if (depth11 == coord) luminance -= d;
2930 return luminance;
2931 case GL_NOTEQUAL:
2932 if (depth00 != coord) luminance -= d;
2933 if (depth01 != coord) luminance -= d;
2934 if (depth10 != coord) luminance -= d;
2935 if (depth11 != coord) luminance -= d;
2936 return luminance;
2937 case GL_ALWAYS:
2938 return 0.0;
2939 case GL_NEVER:
2940 return ambient;
2941 case GL_NONE:
2942 /* ordinary bilinear filtering */
2943 return lerp_2d(wi, wj, depth00, depth10, depth01, depth11);
2944 default:
2945 _mesa_problem(NULL, "Bad compare func in sample_depth_texture");
2946 return 0.0F;
2947 }
2948 }
2949
2950
2951 /**
2952 * Sample a shadow/depth texture.
2953 */
2954 static void
2955 sample_depth_texture( GLcontext *ctx,
2956 const struct gl_texture_object *tObj, GLuint n,
2957 const GLfloat texcoords[][4], const GLfloat lambda[],
2958 GLfloat texel[][4] )
2959 {
2960 const GLint baseLevel = tObj->BaseLevel;
2961 const struct gl_texture_image *img = tObj->Image[0][baseLevel];
2962 const GLint width = img->Width;
2963 const GLint height = img->Height;
2964 const GLint depth = img->Depth;
2965 const GLuint compare_coord = (tObj->Target == GL_TEXTURE_2D_ARRAY_EXT)
2966 ? 3 : 2;
2967 GLfloat ambient;
2968 GLenum function;
2969 GLfloat result;
2970
2971 (void) lambda;
2972
2973 ASSERT(img->TexFormat->BaseFormat == GL_DEPTH_COMPONENT ||
2974 img->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT);
2975
2976 ASSERT(tObj->Target == GL_TEXTURE_1D ||
2977 tObj->Target == GL_TEXTURE_2D ||
2978 tObj->Target == GL_TEXTURE_RECTANGLE_NV ||
2979 tObj->Target == GL_TEXTURE_1D_ARRAY_EXT ||
2980 tObj->Target == GL_TEXTURE_2D_ARRAY_EXT);
2981
2982 ambient = tObj->CompareFailValue;
2983
2984 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
2985
2986 function = (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) ?
2987 tObj->CompareFunc : GL_NONE;
2988
2989 if (tObj->MagFilter == GL_NEAREST) {
2990 GLuint i;
2991 for (i = 0; i < n; i++) {
2992 GLfloat depthSample;
2993 GLint col, row, slice;
2994
2995 nearest_texcoord(tObj, texcoords[i], &col, &row, &slice);
2996
2997 if (col >= 0 && row >= 0 && col < width && row < height &&
2998 slice >= 0 && slice < depth) {
2999 img->FetchTexelf(img, col, row, slice, &depthSample);
3000 }
3001 else {
3002 depthSample = tObj->BorderColor[0];
3003 }
3004
3005 result = shadow_compare(function, texcoords[i][compare_coord],
3006 depthSample, ambient);
3007
3008 switch (tObj->DepthMode) {
3009 case GL_LUMINANCE:
3010 ASSIGN_4V(texel[i], result, result, result, 1.0F);
3011 break;
3012 case GL_INTENSITY:
3013 ASSIGN_4V(texel[i], result, result, result, result);
3014 break;
3015 case GL_ALPHA:
3016 ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
3017 break;
3018 default:
3019 _mesa_problem(ctx, "Bad depth texture mode");
3020 }
3021 }
3022 }
3023 else {
3024 GLuint i;
3025 ASSERT(tObj->MagFilter == GL_LINEAR);
3026 for (i = 0; i < n; i++) {
3027 GLfloat depth00, depth01, depth10, depth11;
3028 GLint i0, i1, j0, j1;
3029 GLint slice;
3030 GLfloat wi, wj;
3031 GLuint useBorderTexel;
3032
3033 linear_texcoord(tObj, texcoords[i], &i0, &i1, &j0, &j1, &slice,
3034 &wi, &wj);
3035
3036 useBorderTexel = 0;
3037 if (img->Border) {
3038 i0 += img->Border;
3039 i1 += img->Border;
3040 if (tObj->Target != GL_TEXTURE_1D_ARRAY_EXT) {
3041 j0 += img->Border;
3042 j1 += img->Border;
3043 }
3044 }
3045 else {
3046 if (i0 < 0 || i0 >= (GLint) width) useBorderTexel |= I0BIT;
3047 if (i1 < 0 || i1 >= (GLint) width) useBorderTexel |= I1BIT;
3048 if (j0 < 0 || j0 >= (GLint) height) useBorderTexel |= J0BIT;
3049 if (j1 < 0 || j1 >= (GLint) height) useBorderTexel |= J1BIT;
3050 }
3051
3052 if (slice < 0 || slice >= (GLint) depth) {
3053 depth00 = tObj->BorderColor[0];
3054 depth01 = tObj->BorderColor[0];
3055 depth10 = tObj->BorderColor[0];
3056 depth11 = tObj->BorderColor[0];
3057 }
3058 else {
3059 /* get four depth samples from the texture */
3060 if (useBorderTexel & (I0BIT | J0BIT)) {
3061 depth00 = tObj->BorderColor[0];
3062 }
3063 else {
3064 img->FetchTexelf(img, i0, j0, slice, &depth00);
3065 }
3066 if (useBorderTexel & (I1BIT | J0BIT)) {
3067 depth10 = tObj->BorderColor[0];
3068 }
3069 else {
3070 img->FetchTexelf(img, i1, j0, slice, &depth10);
3071 }
3072
3073 if (tObj->Target != GL_TEXTURE_1D_ARRAY_EXT) {
3074 if (useBorderTexel & (I0BIT | J1BIT)) {
3075 depth01 = tObj->BorderColor[0];
3076 }
3077 else {
3078 img->FetchTexelf(img, i0, j1, slice, &depth01);
3079 }
3080 if (useBorderTexel & (I1BIT | J1BIT)) {
3081 depth11 = tObj->BorderColor[0];
3082 }
3083 else {
3084 img->FetchTexelf(img, i1, j1, slice, &depth11);
3085 }
3086 }
3087 else {
3088 depth01 = depth00;
3089 depth11 = depth10;
3090 }
3091 }
3092
3093 result = shadow_compare4(function, texcoords[i][compare_coord],
3094 depth00, depth01, depth10, depth11,
3095 ambient, wi, wj);
3096
3097 switch (tObj->DepthMode) {
3098 case GL_LUMINANCE:
3099 ASSIGN_4V(texel[i], result, result, result, 1.0F);
3100 break;
3101 case GL_INTENSITY:
3102 ASSIGN_4V(texel[i], result, result, result, result);
3103 break;
3104 case GL_ALPHA:
3105 ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
3106 break;
3107 default:
3108 _mesa_problem(ctx, "Bad depth texture mode");
3109 }
3110
3111 } /* for */
3112 } /* if filter */
3113 }
3114
3115
3116 /**
3117 * We use this function when a texture object is in an "incomplete" state.
3118 * When a fragment program attempts to sample an incomplete texture we
3119 * return black (see issue 23 in GL_ARB_fragment_program spec).
3120 * Note: fragment programs don't observe the texture enable/disable flags.
3121 */
3122 static void
3123 null_sample_func( GLcontext *ctx,
3124 const struct gl_texture_object *tObj, GLuint n,
3125 const GLfloat texcoords[][4], const GLfloat lambda[],
3126 GLfloat rgba[][4])
3127 {
3128 GLuint i;
3129 (void) ctx;
3130 (void) tObj;
3131 (void) texcoords;
3132 (void) lambda;
3133 for (i = 0; i < n; i++) {
3134 rgba[i][RCOMP] = 0;
3135 rgba[i][GCOMP] = 0;
3136 rgba[i][BCOMP] = 0;
3137 rgba[i][ACOMP] = CHAN_MAX;
3138 }
3139 }
3140
3141
3142 /**
3143 * Choose the texture sampling function for the given texture object.
3144 */
3145 texture_sample_func
3146 _swrast_choose_texture_sample_func( GLcontext *ctx,
3147 const struct gl_texture_object *t )
3148 {
3149 if (!t || !t->_Complete) {
3150 return &null_sample_func;
3151 }
3152 else {
3153 const GLboolean needLambda = (GLboolean) (t->MinFilter != t->MagFilter);
3154 const GLenum format = t->Image[0][t->BaseLevel]->TexFormat->BaseFormat;
3155
3156 switch (t->Target) {
3157 case GL_TEXTURE_1D:
3158 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3159 return &sample_depth_texture;
3160 }
3161 else if (needLambda) {
3162 return &sample_lambda_1d;
3163 }
3164 else if (t->MinFilter == GL_LINEAR) {
3165 return &sample_linear_1d;
3166 }
3167 else {
3168 ASSERT(t->MinFilter == GL_NEAREST);
3169 return &sample_nearest_1d;
3170 }
3171 case GL_TEXTURE_2D:
3172 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3173 return &sample_depth_texture;
3174 }
3175 else if (needLambda) {
3176 return &sample_lambda_2d;
3177 }
3178 else if (t->MinFilter == GL_LINEAR) {
3179 return &sample_linear_2d;
3180 }
3181 else {
3182 /* check for a few optimized cases */
3183 #if 0
3184 const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
3185 ASSERT(t->MinFilter == GL_NEAREST);
3186 if (t->WrapS == GL_REPEAT &&
3187 t->WrapT == GL_REPEAT &&
3188 img->_IsPowerOfTwo &&
3189 img->Border == 0 &&
3190 img->TexFormat->MesaFormat == MESA_FORMAT_RGB) {
3191 return &opt_sample_rgb_2d;
3192 }
3193 else if (t->WrapS == GL_REPEAT &&
3194 t->WrapT == GL_REPEAT &&
3195 img->_IsPowerOfTwo &&
3196 img->Border == 0 &&
3197 img->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
3198 return &opt_sample_rgba_2d;
3199 }
3200 #else
3201 if (0)
3202 ;
3203 #endif
3204 else {
3205 return &sample_nearest_2d;
3206 }
3207 }
3208 case GL_TEXTURE_3D:
3209 if (needLambda) {
3210 return &sample_lambda_3d;
3211 }
3212 else if (t->MinFilter == GL_LINEAR) {
3213 return &sample_linear_3d;
3214 }
3215 else {
3216 ASSERT(t->MinFilter == GL_NEAREST);
3217 return &sample_nearest_3d;
3218 }
3219 case GL_TEXTURE_CUBE_MAP:
3220 if (needLambda) {
3221 return &sample_lambda_cube;
3222 }
3223 else if (t->MinFilter == GL_LINEAR) {
3224 return &sample_linear_cube;
3225 }
3226 else {
3227 ASSERT(t->MinFilter == GL_NEAREST);
3228 return &sample_nearest_cube;
3229 }
3230 case GL_TEXTURE_RECTANGLE_NV:
3231 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3232 return &sample_depth_texture;
3233 }
3234 else if (needLambda) {
3235 return &sample_lambda_rect;
3236 }
3237 else if (t->MinFilter == GL_LINEAR) {
3238 return &sample_linear_rect;
3239 }
3240 else {
3241 ASSERT(t->MinFilter == GL_NEAREST);
3242 return &sample_nearest_rect;
3243 }
3244 case GL_TEXTURE_1D_ARRAY_EXT:
3245 if (needLambda) {
3246 return &sample_lambda_1d_array;
3247 }
3248 else if (t->MinFilter == GL_LINEAR) {
3249 return &sample_linear_1d_array;
3250 }
3251 else {
3252 ASSERT(t->MinFilter == GL_NEAREST);
3253 return &sample_nearest_1d_array;
3254 }
3255 case GL_TEXTURE_2D_ARRAY_EXT:
3256 if (needLambda) {
3257 return &sample_lambda_2d_array;
3258 }
3259 else if (t->MinFilter == GL_LINEAR) {
3260 return &sample_linear_2d_array;
3261 }
3262 else {
3263 ASSERT(t->MinFilter == GL_NEAREST);
3264 return &sample_nearest_2d_array;
3265 }
3266 default:
3267 _mesa_problem(ctx,
3268 "invalid target in _swrast_choose_texture_sample_func");
3269 return &null_sample_func;
3270 }
3271 }
3272 }