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