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