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