Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / mesa / swrast / s_texfilter.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.3
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/colormac.h"
29 #include "main/imports.h"
30
31 #include "s_context.h"
32 #include "s_texfilter.h"
33
34
35 /*
36 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
37 * see 1-pixel bands of improperly weighted linear-filtered textures.
38 * The tests/texwrap.c demo is a good test.
39 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
40 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
41 */
42 #define FRAC(f) ((f) - IFLOOR(f))
43
44
45
46 /**
47 * Linear interpolation macro
48 */
49 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
50
51
52 /**
53 * Do 2D/biliner interpolation of float values.
54 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
55 * a and b are the horizontal and vertical interpolants.
56 * It's important that this function is inlined when compiled with
57 * optimization! If we find that's not true on some systems, convert
58 * to a macro.
59 */
60 static INLINE GLfloat
61 lerp_2d(GLfloat a, GLfloat b,
62 GLfloat v00, GLfloat v10, GLfloat v01, GLfloat v11)
63 {
64 const GLfloat temp0 = LERP(a, v00, v10);
65 const GLfloat temp1 = LERP(a, v01, v11);
66 return LERP(b, temp0, temp1);
67 }
68
69
70 /**
71 * Do 3D/trilinear interpolation of float values.
72 * \sa lerp_2d
73 */
74 static INLINE GLfloat
75 lerp_3d(GLfloat a, GLfloat b, GLfloat c,
76 GLfloat v000, GLfloat v100, GLfloat v010, GLfloat v110,
77 GLfloat v001, GLfloat v101, GLfloat v011, GLfloat v111)
78 {
79 const GLfloat temp00 = LERP(a, v000, v100);
80 const GLfloat temp10 = LERP(a, v010, v110);
81 const GLfloat temp01 = LERP(a, v001, v101);
82 const GLfloat temp11 = LERP(a, v011, v111);
83 const GLfloat temp0 = LERP(b, temp00, temp10);
84 const GLfloat temp1 = LERP(b, temp01, temp11);
85 return LERP(c, temp0, temp1);
86 }
87
88
89 /**
90 * Do linear interpolation of colors.
91 */
92 static INLINE void
93 lerp_rgba(GLfloat result[4], GLfloat t, const GLfloat a[4], const GLfloat b[4])
94 {
95 result[0] = LERP(t, a[0], b[0]);
96 result[1] = LERP(t, a[1], b[1]);
97 result[2] = LERP(t, a[2], b[2]);
98 result[3] = LERP(t, a[3], b[3]);
99 }
100
101
102 /**
103 * Do bilinear interpolation of colors.
104 */
105 static INLINE void
106 lerp_rgba_2d(GLfloat result[4], GLfloat a, GLfloat b,
107 const GLfloat t00[4], const GLfloat t10[4],
108 const GLfloat t01[4], const GLfloat t11[4])
109 {
110 result[0] = lerp_2d(a, b, t00[0], t10[0], t01[0], t11[0]);
111 result[1] = lerp_2d(a, b, t00[1], t10[1], t01[1], t11[1]);
112 result[2] = lerp_2d(a, b, t00[2], t10[2], t01[2], t11[2]);
113 result[3] = lerp_2d(a, b, t00[3], t10[3], t01[3], t11[3]);
114 }
115
116
117 /**
118 * Do trilinear interpolation of colors.
119 */
120 static INLINE void
121 lerp_rgba_3d(GLfloat result[4], GLfloat a, GLfloat b, GLfloat c,
122 const GLfloat t000[4], const GLfloat t100[4],
123 const GLfloat t010[4], const GLfloat t110[4],
124 const GLfloat t001[4], const GLfloat t101[4],
125 const GLfloat t011[4], const GLfloat t111[4])
126 {
127 GLuint k;
128 /* compiler should unroll these short loops */
129 for (k = 0; k < 4; k++) {
130 result[k] = lerp_3d(a, b, c, t000[k], t100[k], t010[k], t110[k],
131 t001[k], t101[k], t011[k], t111[k]);
132 }
133 }
134
135
136 /**
137 * Used for GL_REPEAT wrap mode. Using A % B doesn't produce the
138 * right results for A<0. Casting to A to be unsigned only works if B
139 * is a power of two. Adding a bias to A (which is a multiple of B)
140 * avoids the problems with A < 0 (for reasonable A) without using a
141 * conditional.
142 */
143 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
144
145
146 /**
147 * Used to compute texel locations for linear sampling.
148 * Input:
149 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
150 * s = texcoord in [0,1]
151 * size = width (or height or depth) of texture
152 * Output:
153 * i0, i1 = returns two nearest texel indexes
154 * weight = returns blend factor between texels
155 */
156 static INLINE void
157 linear_texel_locations(GLenum wrapMode,
158 const struct gl_texture_image *img,
159 GLint size, GLfloat s,
160 GLint *i0, GLint *i1, GLfloat *weight)
161 {
162 GLfloat u;
163 switch (wrapMode) {
164 case GL_REPEAT:
165 u = s * size - 0.5F;
166 if (img->_IsPowerOfTwo) {
167 *i0 = IFLOOR(u) & (size - 1);
168 *i1 = (*i0 + 1) & (size - 1);
169 }
170 else {
171 *i0 = REMAINDER(IFLOOR(u), size);
172 *i1 = REMAINDER(*i0 + 1, size);
173 }
174 break;
175 case GL_CLAMP_TO_EDGE:
176 if (s <= 0.0F)
177 u = 0.0F;
178 else if (s >= 1.0F)
179 u = (GLfloat) size;
180 else
181 u = s * size;
182 u -= 0.5F;
183 *i0 = IFLOOR(u);
184 *i1 = *i0 + 1;
185 if (*i0 < 0)
186 *i0 = 0;
187 if (*i1 >= (GLint) size)
188 *i1 = size - 1;
189 break;
190 case GL_CLAMP_TO_BORDER:
191 {
192 const GLfloat min = -1.0F / (2.0F * size);
193 const GLfloat max = 1.0F - min;
194 if (s <= min)
195 u = min * size;
196 else if (s >= max)
197 u = max * size;
198 else
199 u = s * size;
200 u -= 0.5F;
201 *i0 = IFLOOR(u);
202 *i1 = *i0 + 1;
203 }
204 break;
205 case GL_MIRRORED_REPEAT:
206 {
207 const GLint flr = IFLOOR(s);
208 if (flr & 1)
209 u = 1.0F - (s - (GLfloat) flr);
210 else
211 u = s - (GLfloat) flr;
212 u = (u * size) - 0.5F;
213 *i0 = IFLOOR(u);
214 *i1 = *i0 + 1;
215 if (*i0 < 0)
216 *i0 = 0;
217 if (*i1 >= (GLint) size)
218 *i1 = size - 1;
219 }
220 break;
221 case GL_MIRROR_CLAMP_EXT:
222 u = FABSF(s);
223 if (u >= 1.0F)
224 u = (GLfloat) size;
225 else
226 u *= size;
227 u -= 0.5F;
228 *i0 = IFLOOR(u);
229 *i1 = *i0 + 1;
230 break;
231 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
232 u = FABSF(s);
233 if (u >= 1.0F)
234 u = (GLfloat) size;
235 else
236 u *= size;
237 u -= 0.5F;
238 *i0 = IFLOOR(u);
239 *i1 = *i0 + 1;
240 if (*i0 < 0)
241 *i0 = 0;
242 if (*i1 >= (GLint) size)
243 *i1 = size - 1;
244 break;
245 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
246 {
247 const GLfloat min = -1.0F / (2.0F * size);
248 const GLfloat max = 1.0F - min;
249 u = FABSF(s);
250 if (u <= min)
251 u = min * size;
252 else if (u >= max)
253 u = max * size;
254 else
255 u *= size;
256 u -= 0.5F;
257 *i0 = IFLOOR(u);
258 *i1 = *i0 + 1;
259 }
260 break;
261 case GL_CLAMP:
262 if (s <= 0.0F)
263 u = 0.0F;
264 else if (s >= 1.0F)
265 u = (GLfloat) size;
266 else
267 u = s * size;
268 u -= 0.5F;
269 *i0 = IFLOOR(u);
270 *i1 = *i0 + 1;
271 break;
272 default:
273 _mesa_problem(NULL, "Bad wrap mode");
274 u = 0.0F;
275 }
276 *weight = FRAC(u);
277 }
278
279
280 /**
281 * Used to compute texel location for nearest sampling.
282 */
283 static INLINE GLint
284 nearest_texel_location(GLenum wrapMode,
285 const struct gl_texture_image *img,
286 GLint size, GLfloat s)
287 {
288 GLint i;
289
290 switch (wrapMode) {
291 case GL_REPEAT:
292 /* s limited to [0,1) */
293 /* i limited to [0,size-1] */
294 i = IFLOOR(s * size);
295 if (img->_IsPowerOfTwo)
296 i &= (size - 1);
297 else
298 i = REMAINDER(i, size);
299 return i;
300 case GL_CLAMP_TO_EDGE:
301 {
302 /* s limited to [min,max] */
303 /* i limited to [0, size-1] */
304 const GLfloat min = 1.0F / (2.0F * size);
305 const GLfloat max = 1.0F - min;
306 if (s < min)
307 i = 0;
308 else if (s > max)
309 i = size - 1;
310 else
311 i = IFLOOR(s * size);
312 }
313 return i;
314 case GL_CLAMP_TO_BORDER:
315 {
316 /* s limited to [min,max] */
317 /* i limited to [-1, size] */
318 const GLfloat min = -1.0F / (2.0F * size);
319 const GLfloat max = 1.0F - min;
320 if (s <= min)
321 i = -1;
322 else if (s >= max)
323 i = size;
324 else
325 i = IFLOOR(s * size);
326 }
327 return i;
328 case GL_MIRRORED_REPEAT:
329 {
330 const GLfloat min = 1.0F / (2.0F * size);
331 const GLfloat max = 1.0F - min;
332 const GLint flr = IFLOOR(s);
333 GLfloat u;
334 if (flr & 1)
335 u = 1.0F - (s - (GLfloat) flr);
336 else
337 u = s - (GLfloat) flr;
338 if (u < min)
339 i = 0;
340 else if (u > max)
341 i = size - 1;
342 else
343 i = IFLOOR(u * size);
344 }
345 return i;
346 case GL_MIRROR_CLAMP_EXT:
347 {
348 /* s limited to [0,1] */
349 /* i limited to [0,size-1] */
350 const GLfloat u = FABSF(s);
351 if (u <= 0.0F)
352 i = 0;
353 else if (u >= 1.0F)
354 i = size - 1;
355 else
356 i = IFLOOR(u * size);
357 }
358 return i;
359 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
360 {
361 /* s limited to [min,max] */
362 /* i limited to [0, size-1] */
363 const GLfloat min = 1.0F / (2.0F * size);
364 const GLfloat max = 1.0F - min;
365 const GLfloat u = FABSF(s);
366 if (u < min)
367 i = 0;
368 else if (u > max)
369 i = size - 1;
370 else
371 i = IFLOOR(u * size);
372 }
373 return i;
374 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
375 {
376 /* s limited to [min,max] */
377 /* i limited to [0, size-1] */
378 const GLfloat min = -1.0F / (2.0F * size);
379 const GLfloat max = 1.0F - min;
380 const GLfloat u = FABSF(s);
381 if (u < min)
382 i = -1;
383 else if (u > max)
384 i = size;
385 else
386 i = IFLOOR(u * size);
387 }
388 return i;
389 case GL_CLAMP:
390 /* s limited to [0,1] */
391 /* i limited to [0,size-1] */
392 if (s <= 0.0F)
393 i = 0;
394 else if (s >= 1.0F)
395 i = size - 1;
396 else
397 i = IFLOOR(s * size);
398 return i;
399 default:
400 _mesa_problem(NULL, "Bad wrap mode");
401 return 0;
402 }
403 }
404
405
406 /* Power of two image sizes only */
407 static INLINE void
408 linear_repeat_texel_location(GLuint size, GLfloat s,
409 GLint *i0, GLint *i1, GLfloat *weight)
410 {
411 GLfloat u = s * size - 0.5F;
412 *i0 = IFLOOR(u) & (size - 1);
413 *i1 = (*i0 + 1) & (size - 1);
414 *weight = FRAC(u);
415 }
416
417
418 /**
419 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
420 */
421 static INLINE GLint
422 clamp_rect_coord_nearest(GLenum wrapMode, GLfloat coord, GLint max)
423 {
424 switch (wrapMode) {
425 case GL_CLAMP:
426 return IFLOOR( CLAMP(coord, 0.0F, max - 1) );
427 case GL_CLAMP_TO_EDGE:
428 return IFLOOR( CLAMP(coord, 0.5F, max - 0.5F) );
429 case GL_CLAMP_TO_BORDER:
430 return IFLOOR( CLAMP(coord, -0.5F, max + 0.5F) );
431 default:
432 _mesa_problem(NULL, "bad wrapMode in clamp_rect_coord_nearest");
433 return 0;
434 }
435 }
436
437
438 /**
439 * As above, but GL_LINEAR filtering.
440 */
441 static INLINE void
442 clamp_rect_coord_linear(GLenum wrapMode, GLfloat coord, GLint max,
443 GLint *i0out, GLint *i1out, GLfloat *weight)
444 {
445 GLfloat fcol;
446 GLint i0, i1;
447 switch (wrapMode) {
448 case GL_CLAMP:
449 /* Not exactly what the spec says, but it matches NVIDIA output */
450 fcol = CLAMP(coord - 0.5F, 0.0F, max - 1);
451 i0 = IFLOOR(fcol);
452 i1 = i0 + 1;
453 break;
454 case GL_CLAMP_TO_EDGE:
455 fcol = CLAMP(coord, 0.5F, max - 0.5F);
456 fcol -= 0.5F;
457 i0 = IFLOOR(fcol);
458 i1 = i0 + 1;
459 if (i1 > max - 1)
460 i1 = max - 1;
461 break;
462 case GL_CLAMP_TO_BORDER:
463 fcol = CLAMP(coord, -0.5F, max + 0.5F);
464 fcol -= 0.5F;
465 i0 = IFLOOR(fcol);
466 i1 = i0 + 1;
467 break;
468 default:
469 _mesa_problem(NULL, "bad wrapMode in clamp_rect_coord_linear");
470 i0 = i1 = 0;
471 fcol = 0.0F;
472 }
473 *i0out = i0;
474 *i1out = i1;
475 *weight = FRAC(fcol);
476 }
477
478
479 /**
480 * Compute slice/image to use for 1D or 2D array texture.
481 */
482 static INLINE GLint
483 tex_array_slice(GLfloat coord, GLsizei size)
484 {
485 GLint slice = IFLOOR(coord + 0.5f);
486 slice = CLAMP(slice, 0, size - 1);
487 return slice;
488 }
489
490
491 /**
492 * Compute nearest integer texcoords for given texobj and coordinate.
493 * NOTE: only used for depth texture sampling.
494 */
495 static INLINE void
496 nearest_texcoord(const struct gl_texture_object *texObj,
497 GLuint level,
498 const GLfloat texcoord[4],
499 GLint *i, GLint *j, GLint *k)
500 {
501 const struct gl_texture_image *img = texObj->Image[0][level];
502 const GLint width = img->Width;
503 const GLint height = img->Height;
504 const GLint depth = img->Depth;
505
506 switch (texObj->Target) {
507 case GL_TEXTURE_RECTANGLE_ARB:
508 *i = clamp_rect_coord_nearest(texObj->WrapS, texcoord[0], width);
509 *j = clamp_rect_coord_nearest(texObj->WrapT, texcoord[1], height);
510 *k = 0;
511 break;
512 case GL_TEXTURE_1D:
513 *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
514 *j = 0;
515 *k = 0;
516 break;
517 case GL_TEXTURE_2D:
518 *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
519 *j = nearest_texel_location(texObj->WrapT, img, height, texcoord[1]);
520 *k = 0;
521 break;
522 case GL_TEXTURE_1D_ARRAY_EXT:
523 *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
524 *j = tex_array_slice(texcoord[1], height);
525 *k = 0;
526 break;
527 case GL_TEXTURE_2D_ARRAY_EXT:
528 *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
529 *j = nearest_texel_location(texObj->WrapT, img, height, texcoord[1]);
530 *k = tex_array_slice(texcoord[2], depth);
531 break;
532 default:
533 *i = *j = *k = 0;
534 }
535 }
536
537
538 /**
539 * Compute linear integer texcoords for given texobj and coordinate.
540 * NOTE: only used for depth texture sampling.
541 */
542 static INLINE void
543 linear_texcoord(const struct gl_texture_object *texObj,
544 GLuint level,
545 const GLfloat texcoord[4],
546 GLint *i0, GLint *i1, GLint *j0, GLint *j1, GLint *slice,
547 GLfloat *wi, GLfloat *wj)
548 {
549 const struct gl_texture_image *img = texObj->Image[0][level];
550 const GLint width = img->Width;
551 const GLint height = img->Height;
552 const GLint depth = img->Depth;
553
554 switch (texObj->Target) {
555 case GL_TEXTURE_RECTANGLE_ARB:
556 clamp_rect_coord_linear(texObj->WrapS, texcoord[0],
557 width, i0, i1, wi);
558 clamp_rect_coord_linear(texObj->WrapT, texcoord[1],
559 height, j0, j1, wj);
560 *slice = 0;
561 break;
562
563 case GL_TEXTURE_1D:
564 case GL_TEXTURE_2D:
565 linear_texel_locations(texObj->WrapS, img, width,
566 texcoord[0], i0, i1, wi);
567 linear_texel_locations(texObj->WrapT, img, height,
568 texcoord[1], j0, j1, wj);
569 *slice = 0;
570 break;
571
572 case GL_TEXTURE_1D_ARRAY_EXT:
573 linear_texel_locations(texObj->WrapS, img, width,
574 texcoord[0], i0, i1, wi);
575 *j0 = tex_array_slice(texcoord[1], height);
576 *j1 = *j0;
577 *slice = 0;
578 break;
579
580 case GL_TEXTURE_2D_ARRAY_EXT:
581 linear_texel_locations(texObj->WrapS, img, width,
582 texcoord[0], i0, i1, wi);
583 linear_texel_locations(texObj->WrapT, img, height,
584 texcoord[1], j0, j1, wj);
585 *slice = tex_array_slice(texcoord[2], depth);
586 break;
587
588 default:
589 *slice = 0;
590 }
591 }
592
593
594
595 /**
596 * For linear interpolation between mipmap levels N and N+1, this function
597 * computes N.
598 */
599 static INLINE GLint
600 linear_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
601 {
602 if (lambda < 0.0F)
603 return tObj->BaseLevel;
604 else if (lambda > tObj->_MaxLambda)
605 return (GLint) (tObj->BaseLevel + tObj->_MaxLambda);
606 else
607 return (GLint) (tObj->BaseLevel + lambda);
608 }
609
610
611 /**
612 * Compute the nearest mipmap level to take texels from.
613 */
614 static INLINE GLint
615 nearest_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
616 {
617 GLfloat l;
618 GLint level;
619 if (lambda <= 0.5F)
620 l = 0.0F;
621 else if (lambda > tObj->_MaxLambda + 0.4999F)
622 l = tObj->_MaxLambda + 0.4999F;
623 else
624 l = lambda;
625 level = (GLint) (tObj->BaseLevel + l + 0.5F);
626 if (level > tObj->_MaxLevel)
627 level = tObj->_MaxLevel;
628 return level;
629 }
630
631
632
633 /*
634 * Bitflags for texture border color sampling.
635 */
636 #define I0BIT 1
637 #define I1BIT 2
638 #define J0BIT 4
639 #define J1BIT 8
640 #define K0BIT 16
641 #define K1BIT 32
642
643
644
645 /**
646 * The lambda[] array values are always monotonic. Either the whole span
647 * will be minified, magnified, or split between the two. This function
648 * determines the subranges in [0, n-1] that are to be minified or magnified.
649 */
650 static INLINE void
651 compute_min_mag_ranges(const struct gl_texture_object *tObj,
652 GLuint n, const GLfloat lambda[],
653 GLuint *minStart, GLuint *minEnd,
654 GLuint *magStart, GLuint *magEnd)
655 {
656 GLfloat minMagThresh;
657
658 /* we shouldn't be here if minfilter == magfilter */
659 ASSERT(tObj->MinFilter != tObj->MagFilter);
660
661 /* This bit comes from the OpenGL spec: */
662 if (tObj->MagFilter == GL_LINEAR
663 && (tObj->MinFilter == GL_NEAREST_MIPMAP_NEAREST ||
664 tObj->MinFilter == GL_NEAREST_MIPMAP_LINEAR)) {
665 minMagThresh = 0.5F;
666 }
667 else {
668 minMagThresh = 0.0F;
669 }
670
671 #if 0
672 /* DEBUG CODE: Verify that lambda[] is monotonic.
673 * We can't really use this because the inaccuracy in the LOG2 function
674 * causes this test to fail, yet the resulting texturing is correct.
675 */
676 if (n > 1) {
677 GLuint i;
678 printf("lambda delta = %g\n", lambda[0] - lambda[n-1]);
679 if (lambda[0] >= lambda[n-1]) { /* decreasing */
680 for (i = 0; i < n - 1; i++) {
681 ASSERT((GLint) (lambda[i] * 10) >= (GLint) (lambda[i+1] * 10));
682 }
683 }
684 else { /* increasing */
685 for (i = 0; i < n - 1; i++) {
686 ASSERT((GLint) (lambda[i] * 10) <= (GLint) (lambda[i+1] * 10));
687 }
688 }
689 }
690 #endif /* DEBUG */
691
692 if (lambda[0] <= minMagThresh && (n <= 1 || lambda[n-1] <= minMagThresh)) {
693 /* magnification for whole span */
694 *magStart = 0;
695 *magEnd = n;
696 *minStart = *minEnd = 0;
697 }
698 else if (lambda[0] > minMagThresh && (n <=1 || lambda[n-1] > minMagThresh)) {
699 /* minification for whole span */
700 *minStart = 0;
701 *minEnd = n;
702 *magStart = *magEnd = 0;
703 }
704 else {
705 /* a mix of minification and magnification */
706 GLuint i;
707 if (lambda[0] > minMagThresh) {
708 /* start with minification */
709 for (i = 1; i < n; i++) {
710 if (lambda[i] <= minMagThresh)
711 break;
712 }
713 *minStart = 0;
714 *minEnd = i;
715 *magStart = i;
716 *magEnd = n;
717 }
718 else {
719 /* start with magnification */
720 for (i = 1; i < n; i++) {
721 if (lambda[i] > minMagThresh)
722 break;
723 }
724 *magStart = 0;
725 *magEnd = i;
726 *minStart = i;
727 *minEnd = n;
728 }
729 }
730
731 #if 0
732 /* Verify the min/mag Start/End values
733 * We don't use this either (see above)
734 */
735 {
736 GLint i;
737 for (i = 0; i < n; i++) {
738 if (lambda[i] > minMagThresh) {
739 /* minification */
740 ASSERT(i >= *minStart);
741 ASSERT(i < *minEnd);
742 }
743 else {
744 /* magnification */
745 ASSERT(i >= *magStart);
746 ASSERT(i < *magEnd);
747 }
748 }
749 }
750 #endif
751 }
752
753
754 /**
755 * When we sample the border color, it must be interpreted according to
756 * the base texture format. Ex: if the texture base format it GL_ALPHA,
757 * we return (0,0,0,BorderAlpha).
758 */
759 static INLINE void
760 get_border_color(const struct gl_texture_object *tObj,
761 const struct gl_texture_image *img,
762 GLfloat rgba[4])
763 {
764 switch (img->_BaseFormat) {
765 case GL_RGB:
766 rgba[0] = tObj->BorderColor.f[0];
767 rgba[1] = tObj->BorderColor.f[1];
768 rgba[2] = tObj->BorderColor.f[2];
769 rgba[3] = 1.0F;
770 break;
771 case GL_ALPHA:
772 rgba[0] = rgba[1] = rgba[2] = 0.0;
773 rgba[3] = tObj->BorderColor.f[3];
774 break;
775 case GL_LUMINANCE:
776 rgba[0] = rgba[1] = rgba[2] = tObj->BorderColor.f[0];
777 rgba[3] = 1.0;
778 break;
779 case GL_LUMINANCE_ALPHA:
780 rgba[0] = rgba[1] = rgba[2] = tObj->BorderColor.f[0];
781 rgba[3] = tObj->BorderColor.f[3];
782 break;
783 case GL_INTENSITY:
784 rgba[0] = rgba[1] = rgba[2] = rgba[3] = tObj->BorderColor.f[0];
785 break;
786 default:
787 COPY_4V(rgba, tObj->BorderColor.f);
788 }
789 }
790
791
792 /**********************************************************************/
793 /* 1-D Texture Sampling Functions */
794 /**********************************************************************/
795
796 /**
797 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
798 */
799 static INLINE void
800 sample_1d_nearest(struct gl_context *ctx,
801 const struct gl_texture_object *tObj,
802 const struct gl_texture_image *img,
803 const GLfloat texcoord[4], GLfloat rgba[4])
804 {
805 const GLint width = img->Width2; /* without border, power of two */
806 GLint i;
807 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
808 /* skip over the border, if any */
809 i += img->Border;
810 if (i < 0 || i >= (GLint) img->Width) {
811 /* Need this test for GL_CLAMP_TO_BORDER mode */
812 get_border_color(tObj, img, rgba);
813 }
814 else {
815 img->FetchTexelf(img, i, 0, 0, rgba);
816 }
817 }
818
819
820 /**
821 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
822 */
823 static INLINE void
824 sample_1d_linear(struct gl_context *ctx,
825 const struct gl_texture_object *tObj,
826 const struct gl_texture_image *img,
827 const GLfloat texcoord[4], GLfloat rgba[4])
828 {
829 const GLint width = img->Width2;
830 GLint i0, i1;
831 GLbitfield useBorderColor = 0x0;
832 GLfloat a;
833 GLfloat t0[4], t1[4]; /* texels */
834
835 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
836
837 if (img->Border) {
838 i0 += img->Border;
839 i1 += img->Border;
840 }
841 else {
842 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
843 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
844 }
845
846 /* fetch texel colors */
847 if (useBorderColor & I0BIT) {
848 get_border_color(tObj, img, t0);
849 }
850 else {
851 img->FetchTexelf(img, i0, 0, 0, t0);
852 }
853 if (useBorderColor & I1BIT) {
854 get_border_color(tObj, img, t1);
855 }
856 else {
857 img->FetchTexelf(img, i1, 0, 0, t1);
858 }
859
860 lerp_rgba(rgba, a, t0, t1);
861 }
862
863
864 static void
865 sample_1d_nearest_mipmap_nearest(struct gl_context *ctx,
866 const struct gl_texture_object *tObj,
867 GLuint n, const GLfloat texcoord[][4],
868 const GLfloat lambda[], GLfloat rgba[][4])
869 {
870 GLuint i;
871 ASSERT(lambda != NULL);
872 for (i = 0; i < n; i++) {
873 GLint level = nearest_mipmap_level(tObj, lambda[i]);
874 sample_1d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
875 }
876 }
877
878
879 static void
880 sample_1d_linear_mipmap_nearest(struct gl_context *ctx,
881 const struct gl_texture_object *tObj,
882 GLuint n, const GLfloat texcoord[][4],
883 const GLfloat lambda[], GLfloat rgba[][4])
884 {
885 GLuint i;
886 ASSERT(lambda != NULL);
887 for (i = 0; i < n; i++) {
888 GLint level = nearest_mipmap_level(tObj, lambda[i]);
889 sample_1d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
890 }
891 }
892
893
894 static void
895 sample_1d_nearest_mipmap_linear(struct gl_context *ctx,
896 const struct gl_texture_object *tObj,
897 GLuint n, const GLfloat texcoord[][4],
898 const GLfloat lambda[], GLfloat rgba[][4])
899 {
900 GLuint i;
901 ASSERT(lambda != NULL);
902 for (i = 0; i < n; i++) {
903 GLint level = linear_mipmap_level(tObj, lambda[i]);
904 if (level >= tObj->_MaxLevel) {
905 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
906 texcoord[i], rgba[i]);
907 }
908 else {
909 GLfloat t0[4], t1[4];
910 const GLfloat f = FRAC(lambda[i]);
911 sample_1d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
912 sample_1d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
913 lerp_rgba(rgba[i], f, t0, t1);
914 }
915 }
916 }
917
918
919 static void
920 sample_1d_linear_mipmap_linear(struct gl_context *ctx,
921 const struct gl_texture_object *tObj,
922 GLuint n, const GLfloat texcoord[][4],
923 const GLfloat lambda[], GLfloat rgba[][4])
924 {
925 GLuint i;
926 ASSERT(lambda != NULL);
927 for (i = 0; i < n; i++) {
928 GLint level = linear_mipmap_level(tObj, lambda[i]);
929 if (level >= tObj->_MaxLevel) {
930 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
931 texcoord[i], rgba[i]);
932 }
933 else {
934 GLfloat t0[4], t1[4];
935 const GLfloat f = FRAC(lambda[i]);
936 sample_1d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
937 sample_1d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
938 lerp_rgba(rgba[i], f, t0, t1);
939 }
940 }
941 }
942
943
944 /** Sample 1D texture, nearest filtering for both min/magnification */
945 static void
946 sample_nearest_1d( struct gl_context *ctx,
947 const struct gl_texture_object *tObj, GLuint n,
948 const GLfloat texcoords[][4], const GLfloat lambda[],
949 GLfloat rgba[][4] )
950 {
951 GLuint i;
952 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
953 (void) lambda;
954 for (i = 0; i < n; i++) {
955 sample_1d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
956 }
957 }
958
959
960 /** Sample 1D texture, linear filtering for both min/magnification */
961 static void
962 sample_linear_1d( struct gl_context *ctx,
963 const struct gl_texture_object *tObj, GLuint n,
964 const GLfloat texcoords[][4], const GLfloat lambda[],
965 GLfloat rgba[][4] )
966 {
967 GLuint i;
968 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
969 (void) lambda;
970 for (i = 0; i < n; i++) {
971 sample_1d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
972 }
973 }
974
975
976 /** Sample 1D texture, using lambda to choose between min/magnification */
977 static void
978 sample_lambda_1d( struct gl_context *ctx,
979 const struct gl_texture_object *tObj, GLuint n,
980 const GLfloat texcoords[][4],
981 const GLfloat lambda[], GLfloat rgba[][4] )
982 {
983 GLuint minStart, minEnd; /* texels with minification */
984 GLuint magStart, magEnd; /* texels with magnification */
985 GLuint i;
986
987 ASSERT(lambda != NULL);
988 compute_min_mag_ranges(tObj, n, lambda,
989 &minStart, &minEnd, &magStart, &magEnd);
990
991 if (minStart < minEnd) {
992 /* do the minified texels */
993 const GLuint m = minEnd - minStart;
994 switch (tObj->MinFilter) {
995 case GL_NEAREST:
996 for (i = minStart; i < minEnd; i++)
997 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
998 texcoords[i], rgba[i]);
999 break;
1000 case GL_LINEAR:
1001 for (i = minStart; i < minEnd; i++)
1002 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1003 texcoords[i], rgba[i]);
1004 break;
1005 case GL_NEAREST_MIPMAP_NEAREST:
1006 sample_1d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1007 lambda + minStart, rgba + minStart);
1008 break;
1009 case GL_LINEAR_MIPMAP_NEAREST:
1010 sample_1d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1011 lambda + minStart, rgba + minStart);
1012 break;
1013 case GL_NEAREST_MIPMAP_LINEAR:
1014 sample_1d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1015 lambda + minStart, rgba + minStart);
1016 break;
1017 case GL_LINEAR_MIPMAP_LINEAR:
1018 sample_1d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1019 lambda + minStart, rgba + minStart);
1020 break;
1021 default:
1022 _mesa_problem(ctx, "Bad min filter in sample_1d_texture");
1023 return;
1024 }
1025 }
1026
1027 if (magStart < magEnd) {
1028 /* do the magnified texels */
1029 switch (tObj->MagFilter) {
1030 case GL_NEAREST:
1031 for (i = magStart; i < magEnd; i++)
1032 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1033 texcoords[i], rgba[i]);
1034 break;
1035 case GL_LINEAR:
1036 for (i = magStart; i < magEnd; i++)
1037 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1038 texcoords[i], rgba[i]);
1039 break;
1040 default:
1041 _mesa_problem(ctx, "Bad mag filter in sample_1d_texture");
1042 return;
1043 }
1044 }
1045 }
1046
1047
1048 /**********************************************************************/
1049 /* 2-D Texture Sampling Functions */
1050 /**********************************************************************/
1051
1052
1053 /**
1054 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1055 */
1056 static INLINE void
1057 sample_2d_nearest(struct gl_context *ctx,
1058 const struct gl_texture_object *tObj,
1059 const struct gl_texture_image *img,
1060 const GLfloat texcoord[4],
1061 GLfloat rgba[])
1062 {
1063 const GLint width = img->Width2; /* without border, power of two */
1064 const GLint height = img->Height2; /* without border, power of two */
1065 GLint i, j;
1066 (void) ctx;
1067
1068 i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
1069 j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
1070
1071 /* skip over the border, if any */
1072 i += img->Border;
1073 j += img->Border;
1074
1075 if (i < 0 || i >= (GLint) img->Width || j < 0 || j >= (GLint) img->Height) {
1076 /* Need this test for GL_CLAMP_TO_BORDER mode */
1077 get_border_color(tObj, img, rgba);
1078 }
1079 else {
1080 img->FetchTexelf(img, i, j, 0, rgba);
1081 }
1082 }
1083
1084
1085 /**
1086 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1087 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1088 */
1089 static INLINE void
1090 sample_2d_linear(struct gl_context *ctx,
1091 const struct gl_texture_object *tObj,
1092 const struct gl_texture_image *img,
1093 const GLfloat texcoord[4],
1094 GLfloat rgba[])
1095 {
1096 const GLint width = img->Width2;
1097 const GLint height = img->Height2;
1098 GLint i0, j0, i1, j1;
1099 GLbitfield useBorderColor = 0x0;
1100 GLfloat a, b;
1101 GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
1102
1103 linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
1104 linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
1105
1106 if (img->Border) {
1107 i0 += img->Border;
1108 i1 += img->Border;
1109 j0 += img->Border;
1110 j1 += img->Border;
1111 }
1112 else {
1113 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
1114 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
1115 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
1116 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
1117 }
1118
1119 /* fetch four texel colors */
1120 if (useBorderColor & (I0BIT | J0BIT)) {
1121 get_border_color(tObj, img, t00);
1122 }
1123 else {
1124 img->FetchTexelf(img, i0, j0, 0, t00);
1125 }
1126 if (useBorderColor & (I1BIT | J0BIT)) {
1127 get_border_color(tObj, img, t10);
1128 }
1129 else {
1130 img->FetchTexelf(img, i1, j0, 0, t10);
1131 }
1132 if (useBorderColor & (I0BIT | J1BIT)) {
1133 get_border_color(tObj, img, t01);
1134 }
1135 else {
1136 img->FetchTexelf(img, i0, j1, 0, t01);
1137 }
1138 if (useBorderColor & (I1BIT | J1BIT)) {
1139 get_border_color(tObj, img, t11);
1140 }
1141 else {
1142 img->FetchTexelf(img, i1, j1, 0, t11);
1143 }
1144
1145 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
1146 }
1147
1148
1149 /**
1150 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1151 * We don't have to worry about the texture border.
1152 */
1153 static INLINE void
1154 sample_2d_linear_repeat(struct gl_context *ctx,
1155 const struct gl_texture_object *tObj,
1156 const struct gl_texture_image *img,
1157 const GLfloat texcoord[4],
1158 GLfloat rgba[])
1159 {
1160 const GLint width = img->Width2;
1161 const GLint height = img->Height2;
1162 GLint i0, j0, i1, j1;
1163 GLfloat wi, wj;
1164 GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
1165
1166 (void) ctx;
1167
1168 ASSERT(tObj->WrapS == GL_REPEAT);
1169 ASSERT(tObj->WrapT == GL_REPEAT);
1170 ASSERT(img->Border == 0);
1171 ASSERT(img->_BaseFormat != GL_COLOR_INDEX);
1172 ASSERT(img->_IsPowerOfTwo);
1173
1174 linear_repeat_texel_location(width, texcoord[0], &i0, &i1, &wi);
1175 linear_repeat_texel_location(height, texcoord[1], &j0, &j1, &wj);
1176
1177 img->FetchTexelf(img, i0, j0, 0, t00);
1178 img->FetchTexelf(img, i1, j0, 0, t10);
1179 img->FetchTexelf(img, i0, j1, 0, t01);
1180 img->FetchTexelf(img, i1, j1, 0, t11);
1181
1182 lerp_rgba_2d(rgba, wi, wj, t00, t10, t01, t11);
1183 }
1184
1185
1186 static void
1187 sample_2d_nearest_mipmap_nearest(struct gl_context *ctx,
1188 const struct gl_texture_object *tObj,
1189 GLuint n, const GLfloat texcoord[][4],
1190 const GLfloat lambda[], GLfloat rgba[][4])
1191 {
1192 GLuint i;
1193 for (i = 0; i < n; i++) {
1194 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1195 sample_2d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1196 }
1197 }
1198
1199
1200 static void
1201 sample_2d_linear_mipmap_nearest(struct gl_context *ctx,
1202 const struct gl_texture_object *tObj,
1203 GLuint n, const GLfloat texcoord[][4],
1204 const GLfloat lambda[], GLfloat rgba[][4])
1205 {
1206 GLuint i;
1207 ASSERT(lambda != NULL);
1208 for (i = 0; i < n; i++) {
1209 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1210 sample_2d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1211 }
1212 }
1213
1214
1215 static void
1216 sample_2d_nearest_mipmap_linear(struct gl_context *ctx,
1217 const struct gl_texture_object *tObj,
1218 GLuint n, const GLfloat texcoord[][4],
1219 const GLfloat lambda[], GLfloat rgba[][4])
1220 {
1221 GLuint i;
1222 ASSERT(lambda != NULL);
1223 for (i = 0; i < n; i++) {
1224 GLint level = linear_mipmap_level(tObj, lambda[i]);
1225 if (level >= tObj->_MaxLevel) {
1226 sample_2d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1227 texcoord[i], rgba[i]);
1228 }
1229 else {
1230 GLfloat t0[4], t1[4]; /* texels */
1231 const GLfloat f = FRAC(lambda[i]);
1232 sample_2d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1233 sample_2d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1234 lerp_rgba(rgba[i], f, t0, t1);
1235 }
1236 }
1237 }
1238
1239
1240 static void
1241 sample_2d_linear_mipmap_linear( struct gl_context *ctx,
1242 const struct gl_texture_object *tObj,
1243 GLuint n, const GLfloat texcoord[][4],
1244 const GLfloat lambda[], GLfloat rgba[][4] )
1245 {
1246 GLuint i;
1247 ASSERT(lambda != NULL);
1248 for (i = 0; i < n; i++) {
1249 GLint level = linear_mipmap_level(tObj, lambda[i]);
1250 if (level >= tObj->_MaxLevel) {
1251 sample_2d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1252 texcoord[i], rgba[i]);
1253 }
1254 else {
1255 GLfloat t0[4], t1[4]; /* texels */
1256 const GLfloat f = FRAC(lambda[i]);
1257 sample_2d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1258 sample_2d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1259 lerp_rgba(rgba[i], f, t0, t1);
1260 }
1261 }
1262 }
1263
1264
1265 static void
1266 sample_2d_linear_mipmap_linear_repeat(struct gl_context *ctx,
1267 const struct gl_texture_object *tObj,
1268 GLuint n, const GLfloat texcoord[][4],
1269 const GLfloat lambda[], GLfloat rgba[][4])
1270 {
1271 GLuint i;
1272 ASSERT(lambda != NULL);
1273 ASSERT(tObj->WrapS == GL_REPEAT);
1274 ASSERT(tObj->WrapT == GL_REPEAT);
1275 for (i = 0; i < n; i++) {
1276 GLint level = linear_mipmap_level(tObj, lambda[i]);
1277 if (level >= tObj->_MaxLevel) {
1278 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1279 texcoord[i], rgba[i]);
1280 }
1281 else {
1282 GLfloat t0[4], t1[4]; /* texels */
1283 const GLfloat f = FRAC(lambda[i]);
1284 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level ],
1285 texcoord[i], t0);
1286 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level+1],
1287 texcoord[i], t1);
1288 lerp_rgba(rgba[i], f, t0, t1);
1289 }
1290 }
1291 }
1292
1293
1294 /** Sample 2D texture, nearest filtering for both min/magnification */
1295 static void
1296 sample_nearest_2d(struct gl_context *ctx,
1297 const struct gl_texture_object *tObj, GLuint n,
1298 const GLfloat texcoords[][4],
1299 const GLfloat lambda[], GLfloat rgba[][4])
1300 {
1301 GLuint i;
1302 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1303 (void) lambda;
1304 for (i = 0; i < n; i++) {
1305 sample_2d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
1306 }
1307 }
1308
1309
1310 /** Sample 2D texture, linear filtering for both min/magnification */
1311 static void
1312 sample_linear_2d(struct gl_context *ctx,
1313 const struct gl_texture_object *tObj, GLuint n,
1314 const GLfloat texcoords[][4],
1315 const GLfloat lambda[], GLfloat rgba[][4])
1316 {
1317 GLuint i;
1318 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1319 (void) lambda;
1320 if (tObj->WrapS == GL_REPEAT &&
1321 tObj->WrapT == GL_REPEAT &&
1322 image->_IsPowerOfTwo &&
1323 image->Border == 0) {
1324 for (i = 0; i < n; i++) {
1325 sample_2d_linear_repeat(ctx, tObj, image, texcoords[i], rgba[i]);
1326 }
1327 }
1328 else {
1329 for (i = 0; i < n; i++) {
1330 sample_2d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
1331 }
1332 }
1333 }
1334
1335
1336 /**
1337 * Optimized 2-D texture sampling:
1338 * S and T wrap mode == GL_REPEAT
1339 * GL_NEAREST min/mag filter
1340 * No border,
1341 * RowStride == Width,
1342 * Format = GL_RGB
1343 */
1344 static void
1345 opt_sample_rgb_2d(struct gl_context *ctx,
1346 const struct gl_texture_object *tObj,
1347 GLuint n, const GLfloat texcoords[][4],
1348 const GLfloat lambda[], GLfloat rgba[][4])
1349 {
1350 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1351 const GLfloat width = (GLfloat) img->Width;
1352 const GLfloat height = (GLfloat) img->Height;
1353 const GLint colMask = img->Width - 1;
1354 const GLint rowMask = img->Height - 1;
1355 const GLint shift = img->WidthLog2;
1356 GLuint k;
1357 (void) ctx;
1358 (void) lambda;
1359 ASSERT(tObj->WrapS==GL_REPEAT);
1360 ASSERT(tObj->WrapT==GL_REPEAT);
1361 ASSERT(img->Border==0);
1362 ASSERT(img->TexFormat == MESA_FORMAT_RGB888);
1363 ASSERT(img->_IsPowerOfTwo);
1364
1365 for (k=0; k<n; k++) {
1366 GLint i = IFLOOR(texcoords[k][0] * width) & colMask;
1367 GLint j = IFLOOR(texcoords[k][1] * height) & rowMask;
1368 GLint pos = (j << shift) | i;
1369 GLubyte *texel = ((GLubyte *) img->Data) + 3*pos;
1370 rgba[k][RCOMP] = UBYTE_TO_FLOAT(texel[2]);
1371 rgba[k][GCOMP] = UBYTE_TO_FLOAT(texel[1]);
1372 rgba[k][BCOMP] = UBYTE_TO_FLOAT(texel[0]);
1373 rgba[k][ACOMP] = 1.0F;
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 (coord > depth00) luminance -= d;
2922 if (coord > depth01) luminance -= d;
2923 if (coord > depth10) luminance -= d;
2924 if (coord > depth11) luminance -= d;
2925 return luminance;
2926 case GL_GEQUAL:
2927 if (coord < depth00) luminance -= d;
2928 if (coord < depth01) luminance -= d;
2929 if (coord < depth10) luminance -= d;
2930 if (coord < depth11) luminance -= d;
2931 return luminance;
2932 case GL_LESS:
2933 if (coord >= depth00) luminance -= d;
2934 if (coord >= depth01) luminance -= d;
2935 if (coord >= depth10) luminance -= d;
2936 if (coord >= depth11) luminance -= d;
2937 return luminance;
2938 case GL_GREATER:
2939 if (coord <= depth00) luminance -= d;
2940 if (coord <= depth01) luminance -= d;
2941 if (coord <= depth10) luminance -= d;
2942 if (coord <= depth11) luminance -= d;
2943 return luminance;
2944 case GL_EQUAL:
2945 if (coord != depth00) luminance -= d;
2946 if (coord != depth01) luminance -= d;
2947 if (coord != depth10) luminance -= d;
2948 if (coord != depth11) luminance -= d;
2949 return luminance;
2950 case GL_NOTEQUAL:
2951 if (coord == depth00) luminance -= d;
2952 if (coord == depth01) luminance -= d;
2953 if (coord == depth10) luminance -= d;
2954 if (coord == depth11) luminance -= d;
2955 return luminance;
2956 case GL_ALWAYS:
2957 return 1.0F;
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_compare4");
2965 return ambient;
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, depthRef;
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 depthRef = CLAMP(texcoords[i][compare_coord], 0.0F, 1.0F);
3047
3048 result = shadow_compare(function, depthRef, depthSample, ambient);
3049
3050 switch (tObj->DepthMode) {
3051 case GL_LUMINANCE:
3052 ASSIGN_4V(texel[i], result, result, result, 1.0F);
3053 break;
3054 case GL_INTENSITY:
3055 ASSIGN_4V(texel[i], result, result, result, result);
3056 break;
3057 case GL_ALPHA:
3058 ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
3059 break;
3060 case GL_RED:
3061 ASSIGN_4V(texel[i], result, 0.0F, 0.0F, 1.0F);
3062 break;
3063 default:
3064 _mesa_problem(ctx, "Bad depth texture mode");
3065 }
3066 }
3067 }
3068 else {
3069 GLuint i;
3070 ASSERT(tObj->MagFilter == GL_LINEAR);
3071 for (i = 0; i < n; i++) {
3072 GLfloat depth00, depth01, depth10, depth11, depthRef;
3073 GLint i0, i1, j0, j1;
3074 GLint slice;
3075 GLfloat wi, wj;
3076 GLuint useBorderTexel;
3077
3078 linear_texcoord(tObj, level, texcoords[i], &i0, &i1, &j0, &j1, &slice,
3079 &wi, &wj);
3080
3081 useBorderTexel = 0;
3082 if (img->Border) {
3083 i0 += img->Border;
3084 i1 += img->Border;
3085 if (tObj->Target != GL_TEXTURE_1D_ARRAY_EXT) {
3086 j0 += img->Border;
3087 j1 += img->Border;
3088 }
3089 }
3090 else {
3091 if (i0 < 0 || i0 >= (GLint) width) useBorderTexel |= I0BIT;
3092 if (i1 < 0 || i1 >= (GLint) width) useBorderTexel |= I1BIT;
3093 if (j0 < 0 || j0 >= (GLint) height) useBorderTexel |= J0BIT;
3094 if (j1 < 0 || j1 >= (GLint) height) useBorderTexel |= J1BIT;
3095 }
3096
3097 if (slice < 0 || slice >= (GLint) depth) {
3098 depth00 = tObj->BorderColor.f[0];
3099 depth01 = tObj->BorderColor.f[0];
3100 depth10 = tObj->BorderColor.f[0];
3101 depth11 = tObj->BorderColor.f[0];
3102 }
3103 else {
3104 /* get four depth samples from the texture */
3105 if (useBorderTexel & (I0BIT | J0BIT)) {
3106 depth00 = tObj->BorderColor.f[0];
3107 }
3108 else {
3109 img->FetchTexelf(img, i0, j0, slice, &depth00);
3110 }
3111 if (useBorderTexel & (I1BIT | J0BIT)) {
3112 depth10 = tObj->BorderColor.f[0];
3113 }
3114 else {
3115 img->FetchTexelf(img, i1, j0, slice, &depth10);
3116 }
3117
3118 if (tObj->Target != GL_TEXTURE_1D_ARRAY_EXT) {
3119 if (useBorderTexel & (I0BIT | J1BIT)) {
3120 depth01 = tObj->BorderColor.f[0];
3121 }
3122 else {
3123 img->FetchTexelf(img, i0, j1, slice, &depth01);
3124 }
3125 if (useBorderTexel & (I1BIT | J1BIT)) {
3126 depth11 = tObj->BorderColor.f[0];
3127 }
3128 else {
3129 img->FetchTexelf(img, i1, j1, slice, &depth11);
3130 }
3131 }
3132 else {
3133 depth01 = depth00;
3134 depth11 = depth10;
3135 }
3136 }
3137
3138 depthRef = CLAMP(texcoords[i][compare_coord], 0.0F, 1.0F);
3139
3140 result = shadow_compare4(function, depthRef,
3141 depth00, depth01, depth10, depth11,
3142 ambient, wi, wj);
3143
3144 switch (tObj->DepthMode) {
3145 case GL_LUMINANCE:
3146 ASSIGN_4V(texel[i], result, result, result, 1.0F);
3147 break;
3148 case GL_INTENSITY:
3149 ASSIGN_4V(texel[i], result, result, result, result);
3150 break;
3151 case GL_ALPHA:
3152 ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
3153 break;
3154 default:
3155 _mesa_problem(ctx, "Bad depth texture mode");
3156 }
3157
3158 } /* for */
3159 } /* if filter */
3160 }
3161
3162
3163 /**
3164 * We use this function when a texture object is in an "incomplete" state.
3165 * When a fragment program attempts to sample an incomplete texture we
3166 * return black (see issue 23 in GL_ARB_fragment_program spec).
3167 * Note: fragment programs don't observe the texture enable/disable flags.
3168 */
3169 static void
3170 null_sample_func( struct gl_context *ctx,
3171 const struct gl_texture_object *tObj, GLuint n,
3172 const GLfloat texcoords[][4], const GLfloat lambda[],
3173 GLfloat rgba[][4])
3174 {
3175 GLuint i;
3176 (void) ctx;
3177 (void) tObj;
3178 (void) texcoords;
3179 (void) lambda;
3180 for (i = 0; i < n; i++) {
3181 rgba[i][RCOMP] = 0;
3182 rgba[i][GCOMP] = 0;
3183 rgba[i][BCOMP] = 0;
3184 rgba[i][ACOMP] = 1.0;
3185 }
3186 }
3187
3188
3189 /**
3190 * Choose the texture sampling function for the given texture object.
3191 */
3192 texture_sample_func
3193 _swrast_choose_texture_sample_func( struct gl_context *ctx,
3194 const struct gl_texture_object *t )
3195 {
3196 if (!t || !t->_Complete) {
3197 return &null_sample_func;
3198 }
3199 else {
3200 const GLboolean needLambda = (GLboolean) (t->MinFilter != t->MagFilter);
3201 const GLenum format = t->Image[0][t->BaseLevel]->_BaseFormat;
3202
3203 switch (t->Target) {
3204 case GL_TEXTURE_1D:
3205 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3206 return &sample_depth_texture;
3207 }
3208 else if (needLambda) {
3209 return &sample_lambda_1d;
3210 }
3211 else if (t->MinFilter == GL_LINEAR) {
3212 return &sample_linear_1d;
3213 }
3214 else {
3215 ASSERT(t->MinFilter == GL_NEAREST);
3216 return &sample_nearest_1d;
3217 }
3218 case GL_TEXTURE_2D:
3219 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3220 return &sample_depth_texture;
3221 }
3222 else if (needLambda) {
3223 return &sample_lambda_2d;
3224 }
3225 else if (t->MinFilter == GL_LINEAR) {
3226 return &sample_linear_2d;
3227 }
3228 else {
3229 /* check for a few optimized cases */
3230 const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
3231 ASSERT(t->MinFilter == GL_NEAREST);
3232 if (t->WrapS == GL_REPEAT &&
3233 t->WrapT == GL_REPEAT &&
3234 img->_IsPowerOfTwo &&
3235 img->Border == 0 &&
3236 img->TexFormat == MESA_FORMAT_RGB888) {
3237 return &opt_sample_rgb_2d;
3238 }
3239 else if (t->WrapS == GL_REPEAT &&
3240 t->WrapT == GL_REPEAT &&
3241 img->_IsPowerOfTwo &&
3242 img->Border == 0 &&
3243 img->TexFormat == MESA_FORMAT_RGBA8888) {
3244 return &opt_sample_rgba_2d;
3245 }
3246 else {
3247 return &sample_nearest_2d;
3248 }
3249 }
3250 case GL_TEXTURE_3D:
3251 if (needLambda) {
3252 return &sample_lambda_3d;
3253 }
3254 else if (t->MinFilter == GL_LINEAR) {
3255 return &sample_linear_3d;
3256 }
3257 else {
3258 ASSERT(t->MinFilter == GL_NEAREST);
3259 return &sample_nearest_3d;
3260 }
3261 case GL_TEXTURE_CUBE_MAP:
3262 if (needLambda) {
3263 return &sample_lambda_cube;
3264 }
3265 else if (t->MinFilter == GL_LINEAR) {
3266 return &sample_linear_cube;
3267 }
3268 else {
3269 ASSERT(t->MinFilter == GL_NEAREST);
3270 return &sample_nearest_cube;
3271 }
3272 case GL_TEXTURE_RECTANGLE_NV:
3273 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3274 return &sample_depth_texture;
3275 }
3276 else if (needLambda) {
3277 return &sample_lambda_rect;
3278 }
3279 else if (t->MinFilter == GL_LINEAR) {
3280 return &sample_linear_rect;
3281 }
3282 else {
3283 ASSERT(t->MinFilter == GL_NEAREST);
3284 return &sample_nearest_rect;
3285 }
3286 case GL_TEXTURE_1D_ARRAY_EXT:
3287 if (needLambda) {
3288 return &sample_lambda_1d_array;
3289 }
3290 else if (t->MinFilter == GL_LINEAR) {
3291 return &sample_linear_1d_array;
3292 }
3293 else {
3294 ASSERT(t->MinFilter == GL_NEAREST);
3295 return &sample_nearest_1d_array;
3296 }
3297 case GL_TEXTURE_2D_ARRAY_EXT:
3298 if (needLambda) {
3299 return &sample_lambda_2d_array;
3300 }
3301 else if (t->MinFilter == GL_LINEAR) {
3302 return &sample_linear_2d_array;
3303 }
3304 else {
3305 ASSERT(t->MinFilter == GL_NEAREST);
3306 return &sample_nearest_2d_array;
3307 }
3308 default:
3309 _mesa_problem(ctx,
3310 "invalid target in _swrast_choose_texture_sample_func");
3311 return &null_sample_func;
3312 }
3313 }
3314 }