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