Dead code elimination and fix bufmgr_fake_wait_idle.
[mesa.git] / src / mesa / swrast / s_texfilter.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 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 "glheader.h"
27 #include "context.h"
28 #include "colormac.h"
29 #include "imports.h"
30 #include "texformat.h"
31
32 #include "s_context.h"
33 #include "s_texfilter.h"
34
35
36 /**
37 * Constants for integer linear interpolation.
38 */
39 #define ILERP_SCALE 65536.0F
40 #define ILERP_SHIFT 16
41
42
43 /**
44 * Linear interpolation macros
45 */
46 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
47 #define ILERP(IT, A, B) ( (A) + (((IT) * ((B) - (A))) >> ILERP_SHIFT) )
48
49
50 /**
51 * Do 2D/biliner interpolation of float values.
52 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
53 * a and b are the horizontal and vertical interpolants.
54 * It's important that this function is inlined when compiled with
55 * optimization! If we find that's not true on some systems, convert
56 * to a macro.
57 */
58 static INLINE GLfloat
59 lerp_2d(GLfloat a, GLfloat b,
60 GLfloat v00, GLfloat v10, GLfloat v01, GLfloat v11)
61 {
62 const GLfloat temp0 = LERP(a, v00, v10);
63 const GLfloat temp1 = LERP(a, v01, v11);
64 return LERP(b, temp0, temp1);
65 }
66
67
68 /**
69 * Do 2D/biliner interpolation of integer values.
70 * \sa lerp_2d
71 */
72 static INLINE GLint
73 ilerp_2d(GLint ia, GLint ib,
74 GLint v00, GLint v10, GLint v01, GLint v11)
75 {
76 /* fixed point interpolants in [0, ILERP_SCALE] */
77 const GLint temp0 = ILERP(ia, v00, v10);
78 const GLint temp1 = ILERP(ia, v01, v11);
79 return ILERP(ib, temp0, temp1);
80 }
81
82
83 /**
84 * Do 3D/trilinear interpolation of float values.
85 * \sa lerp_2d
86 */
87 static INLINE GLfloat
88 lerp_3d(GLfloat a, GLfloat b, GLfloat c,
89 GLfloat v000, GLfloat v100, GLfloat v010, GLfloat v110,
90 GLfloat v001, GLfloat v101, GLfloat v011, GLfloat v111)
91 {
92 const GLfloat temp00 = LERP(a, v000, v100);
93 const GLfloat temp10 = LERP(a, v010, v110);
94 const GLfloat temp01 = LERP(a, v001, v101);
95 const GLfloat temp11 = LERP(a, v011, v111);
96 const GLfloat temp0 = LERP(b, temp00, temp10);
97 const GLfloat temp1 = LERP(b, temp01, temp11);
98 return LERP(c, temp0, temp1);
99 }
100
101
102 /**
103 * Do 3D/trilinear interpolation of integer values.
104 * \sa lerp_2d
105 */
106 static INLINE GLint
107 ilerp_3d(GLint ia, GLint ib, GLint ic,
108 GLint v000, GLint v100, GLint v010, GLint v110,
109 GLint v001, GLint v101, GLint v011, GLint v111)
110 {
111 /* fixed point interpolants in [0, ILERP_SCALE] */
112 const GLint temp00 = ILERP(ia, v000, v100);
113 const GLint temp10 = ILERP(ia, v010, v110);
114 const GLint temp01 = ILERP(ia, v001, v101);
115 const GLint temp11 = ILERP(ia, v011, v111);
116 const GLint temp0 = ILERP(ib, temp00, temp10);
117 const GLint temp1 = ILERP(ib, temp01, temp11);
118 return ILERP(ic, temp0, temp1);
119 }
120
121
122 /**
123 * Do linear interpolation of colors.
124 */
125 static INLINE void
126 lerp_rgba(GLchan result[4], GLfloat t, const GLchan a[4], const GLchan b[4])
127 {
128 #if CHAN_TYPE == GL_FLOAT
129 result[0] = LERP(t, a[0], b[0]);
130 result[1] = LERP(t, a[1], b[1]);
131 result[2] = LERP(t, a[2], b[2]);
132 result[3] = LERP(t, a[3], b[3]);
133 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
134 result[0] = (GLchan) (LERP(t, a[0], b[0]) + 0.5);
135 result[1] = (GLchan) (LERP(t, a[1], b[1]) + 0.5);
136 result[2] = (GLchan) (LERP(t, a[2], b[2]) + 0.5);
137 result[3] = (GLchan) (LERP(t, a[3], b[3]) + 0.5);
138 #else
139 /* fixed point interpolants in [0, ILERP_SCALE] */
140 const GLint it = IROUND_POS(t * ILERP_SCALE);
141 ASSERT(CHAN_TYPE == GL_UNSIGNED_BYTE);
142 result[0] = ILERP(it, a[0], b[0]);
143 result[1] = ILERP(it, a[1], b[1]);
144 result[2] = ILERP(it, a[2], b[2]);
145 result[3] = ILERP(it, a[3], b[3]);
146 #endif
147 }
148
149
150 /**
151 * Do bilinear interpolation of colors.
152 */
153 static INLINE void
154 lerp_rgba_2d(GLchan result[4], GLfloat a, GLfloat b,
155 const GLchan t00[4], const GLchan t10[4],
156 const GLchan t01[4], const GLchan t11[4])
157 {
158 #if CHAN_TYPE == GL_FLOAT
159 result[0] = lerp_2d(a, b, t00[0], t10[0], t01[0], t11[0]);
160 result[1] = lerp_2d(a, b, t00[1], t10[1], t01[1], t11[1]);
161 result[2] = lerp_2d(a, b, t00[2], t10[2], t01[2], t11[2]);
162 result[3] = lerp_2d(a, b, t00[3], t10[3], t01[3], t11[3]);
163 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
164 result[0] = (GLchan) (lerp_2d(a, b, t00[0], t10[0], t01[0], t11[0]) + 0.5);
165 result[1] = (GLchan) (lerp_2d(a, b, t00[1], t10[1], t01[1], t11[1]) + 0.5);
166 result[2] = (GLchan) (lerp_2d(a, b, t00[2], t10[2], t01[2], t11[2]) + 0.5);
167 result[3] = (GLchan) (lerp_2d(a, b, t00[3], t10[3], t01[3], t11[3]) + 0.5);
168 #else
169 const GLint ia = IROUND_POS(a * ILERP_SCALE);
170 const GLint ib = IROUND_POS(b * ILERP_SCALE);
171 ASSERT(CHAN_TYPE == GL_UNSIGNED_BYTE);
172 result[0] = ilerp_2d(ia, ib, t00[0], t10[0], t01[0], t11[0]);
173 result[1] = ilerp_2d(ia, ib, t00[1], t10[1], t01[1], t11[1]);
174 result[2] = ilerp_2d(ia, ib, t00[2], t10[2], t01[2], t11[2]);
175 result[3] = ilerp_2d(ia, ib, t00[3], t10[3], t01[3], t11[3]);
176 #endif
177 }
178
179
180 /**
181 * Do trilinear interpolation of colors.
182 */
183 static INLINE void
184 lerp_rgba_3d(GLchan result[4], GLfloat a, GLfloat b, GLfloat c,
185 const GLchan t000[4], const GLchan t100[4],
186 const GLchan t010[4], const GLchan t110[4],
187 const GLchan t001[4], const GLchan t101[4],
188 const GLchan t011[4], const GLchan t111[4])
189 {
190 GLuint k;
191 /* compiler should unroll these short loops */
192 #if CHAN_TYPE == GL_FLOAT
193 for (k = 0; k < 4; k++) {
194 result[k] = lerp_3d(a, b, c, t000[k], t100[k], t010[k], t110[k],
195 t001[k], t101[k], t011[k], t111[k]);
196 }
197 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
198 for (k = 0; k < 4; k++) {
199 result[k] = (GLchan)(lerp_3d(a, b, c,
200 t000[k], t100[k], t010[k], t110[k],
201 t001[k], t101[k], t011[k], t111[k]) + 0.5F);
202 }
203 #else
204 GLint ia = IROUND_POS(a * ILERP_SCALE);
205 GLint ib = IROUND_POS(b * ILERP_SCALE);
206 GLint ic = IROUND_POS(c * ILERP_SCALE);
207 for (k = 0; k < 4; k++) {
208 result[k] = ilerp_3d(ia, ib, ic, t000[k], t100[k], t010[k], t110[k],
209 t001[k], t101[k], t011[k], t111[k]);
210 }
211 #endif
212 }
213
214
215 /**
216 * Compute the remainder of a divided by b, but be careful with
217 * negative values so that GL_REPEAT mode works right.
218 */
219 static INLINE GLint
220 repeat_remainder(GLint a, GLint b)
221 {
222 if (a >= 0)
223 return a % b;
224 else
225 return (a + 1) % b + b - 1;
226 }
227
228
229 /**
230 * Used to compute texel locations for linear sampling.
231 * Input:
232 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
233 * S = texcoord in [0,1]
234 * SIZE = width (or height or depth) of texture
235 * Output:
236 * U = texcoord in [0, width]
237 * I0, I1 = two nearest texel indexes
238 */
239 #define COMPUTE_LINEAR_TEXEL_LOCATIONS(wrapMode, S, U, SIZE, I0, I1) \
240 { \
241 switch (wrapMode) { \
242 case GL_REPEAT: \
243 U = S * SIZE - 0.5F; \
244 if (img->_IsPowerOfTwo) { \
245 I0 = IFLOOR(U) & (SIZE - 1); \
246 I1 = (I0 + 1) & (SIZE - 1); \
247 } \
248 else { \
249 I0 = repeat_remainder(IFLOOR(U), SIZE); \
250 I1 = repeat_remainder(I0 + 1, SIZE); \
251 } \
252 break; \
253 case GL_CLAMP_TO_EDGE: \
254 if (S <= 0.0F) \
255 U = 0.0F; \
256 else if (S >= 1.0F) \
257 U = (GLfloat) SIZE; \
258 else \
259 U = S * SIZE; \
260 U -= 0.5F; \
261 I0 = IFLOOR(U); \
262 I1 = I0 + 1; \
263 if (I0 < 0) \
264 I0 = 0; \
265 if (I1 >= (GLint) SIZE) \
266 I1 = SIZE - 1; \
267 break; \
268 case GL_CLAMP_TO_BORDER: \
269 { \
270 const GLfloat min = -1.0F / (2.0F * SIZE); \
271 const GLfloat max = 1.0F - min; \
272 if (S <= min) \
273 U = min * SIZE; \
274 else if (S >= max) \
275 U = max * SIZE; \
276 else \
277 U = S * SIZE; \
278 U -= 0.5F; \
279 I0 = IFLOOR(U); \
280 I1 = I0 + 1; \
281 } \
282 break; \
283 case GL_MIRRORED_REPEAT: \
284 { \
285 const GLint flr = IFLOOR(S); \
286 if (flr & 1) \
287 U = 1.0F - (S - (GLfloat) flr); /* flr is odd */ \
288 else \
289 U = S - (GLfloat) flr; /* flr is even */ \
290 U = (U * SIZE) - 0.5F; \
291 I0 = IFLOOR(U); \
292 I1 = I0 + 1; \
293 if (I0 < 0) \
294 I0 = 0; \
295 if (I1 >= (GLint) SIZE) \
296 I1 = SIZE - 1; \
297 } \
298 break; \
299 case GL_MIRROR_CLAMP_EXT: \
300 U = FABSF(S); \
301 if (U >= 1.0F) \
302 U = (GLfloat) SIZE; \
303 else \
304 U *= SIZE; \
305 U -= 0.5F; \
306 I0 = IFLOOR(U); \
307 I1 = I0 + 1; \
308 break; \
309 case GL_MIRROR_CLAMP_TO_EDGE_EXT: \
310 U = FABSF(S); \
311 if (U >= 1.0F) \
312 U = (GLfloat) SIZE; \
313 else \
314 U *= SIZE; \
315 U -= 0.5F; \
316 I0 = IFLOOR(U); \
317 I1 = I0 + 1; \
318 if (I0 < 0) \
319 I0 = 0; \
320 if (I1 >= (GLint) SIZE) \
321 I1 = SIZE - 1; \
322 break; \
323 case GL_MIRROR_CLAMP_TO_BORDER_EXT: \
324 { \
325 const GLfloat min = -1.0F / (2.0F * SIZE); \
326 const GLfloat max = 1.0F - min; \
327 U = FABSF(S); \
328 if (U <= min) \
329 U = min * SIZE; \
330 else if (U >= max) \
331 U = max * SIZE; \
332 else \
333 U *= SIZE; \
334 U -= 0.5F; \
335 I0 = IFLOOR(U); \
336 I1 = I0 + 1; \
337 } \
338 break; \
339 case GL_CLAMP: \
340 if (S <= 0.0F) \
341 U = 0.0F; \
342 else if (S >= 1.0F) \
343 U = (GLfloat) SIZE; \
344 else \
345 U = S * SIZE; \
346 U -= 0.5F; \
347 I0 = IFLOOR(U); \
348 I1 = I0 + 1; \
349 break; \
350 default: \
351 _mesa_problem(ctx, "Bad wrap mode"); \
352 } \
353 }
354
355
356 /**
357 * Used to compute texel location for nearest sampling.
358 */
359 #define COMPUTE_NEAREST_TEXEL_LOCATION(wrapMode, S, SIZE, I) \
360 { \
361 switch (wrapMode) { \
362 case GL_REPEAT: \
363 /* s limited to [0,1) */ \
364 /* i limited to [0,size-1] */ \
365 I = IFLOOR(S * SIZE); \
366 if (img->_IsPowerOfTwo) \
367 I &= (SIZE - 1); \
368 else \
369 I = repeat_remainder(I, SIZE); \
370 break; \
371 case GL_CLAMP_TO_EDGE: \
372 { \
373 /* s limited to [min,max] */ \
374 /* i limited to [0, size-1] */ \
375 const GLfloat min = 1.0F / (2.0F * SIZE); \
376 const GLfloat max = 1.0F - min; \
377 if (S < min) \
378 I = 0; \
379 else if (S > max) \
380 I = SIZE - 1; \
381 else \
382 I = IFLOOR(S * SIZE); \
383 } \
384 break; \
385 case GL_CLAMP_TO_BORDER: \
386 { \
387 /* s limited to [min,max] */ \
388 /* i limited to [-1, size] */ \
389 const GLfloat min = -1.0F / (2.0F * SIZE); \
390 const GLfloat max = 1.0F - min; \
391 if (S <= min) \
392 I = -1; \
393 else if (S >= max) \
394 I = SIZE; \
395 else \
396 I = IFLOOR(S * SIZE); \
397 } \
398 break; \
399 case GL_MIRRORED_REPEAT: \
400 { \
401 const GLfloat min = 1.0F / (2.0F * SIZE); \
402 const GLfloat max = 1.0F - min; \
403 const GLint flr = IFLOOR(S); \
404 GLfloat u; \
405 if (flr & 1) \
406 u = 1.0F - (S - (GLfloat) flr); /* flr is odd */ \
407 else \
408 u = S - (GLfloat) flr; /* flr is even */ \
409 if (u < min) \
410 I = 0; \
411 else if (u > max) \
412 I = SIZE - 1; \
413 else \
414 I = IFLOOR(u * SIZE); \
415 } \
416 break; \
417 case GL_MIRROR_CLAMP_EXT: \
418 { \
419 /* s limited to [0,1] */ \
420 /* i limited to [0,size-1] */ \
421 const GLfloat u = FABSF(S); \
422 if (u <= 0.0F) \
423 I = 0; \
424 else if (u >= 1.0F) \
425 I = SIZE - 1; \
426 else \
427 I = IFLOOR(u * SIZE); \
428 } \
429 break; \
430 case GL_MIRROR_CLAMP_TO_EDGE_EXT: \
431 { \
432 /* s limited to [min,max] */ \
433 /* i limited to [0, size-1] */ \
434 const GLfloat min = 1.0F / (2.0F * SIZE); \
435 const GLfloat max = 1.0F - min; \
436 const GLfloat u = FABSF(S); \
437 if (u < min) \
438 I = 0; \
439 else if (u > max) \
440 I = SIZE - 1; \
441 else \
442 I = IFLOOR(u * SIZE); \
443 } \
444 break; \
445 case GL_MIRROR_CLAMP_TO_BORDER_EXT: \
446 { \
447 /* s limited to [min,max] */ \
448 /* i limited to [0, size-1] */ \
449 const GLfloat min = -1.0F / (2.0F * SIZE); \
450 const GLfloat max = 1.0F - min; \
451 const GLfloat u = FABSF(S); \
452 if (u < min) \
453 I = -1; \
454 else if (u > max) \
455 I = SIZE; \
456 else \
457 I = IFLOOR(u * SIZE); \
458 } \
459 break; \
460 case GL_CLAMP: \
461 /* s limited to [0,1] */ \
462 /* i limited to [0,size-1] */ \
463 if (S <= 0.0F) \
464 I = 0; \
465 else if (S >= 1.0F) \
466 I = SIZE - 1; \
467 else \
468 I = IFLOOR(S * SIZE); \
469 break; \
470 default: \
471 _mesa_problem(ctx, "Bad wrap mode"); \
472 } \
473 }
474
475
476 /* Power of two image sizes only */
477 #define COMPUTE_LINEAR_REPEAT_TEXEL_LOCATION(S, U, SIZE, I0, I1) \
478 { \
479 U = S * SIZE - 0.5F; \
480 I0 = IFLOOR(U) & (SIZE - 1); \
481 I1 = (I0 + 1) & (SIZE - 1); \
482 }
483
484
485 /**
486 * For linear interpolation between mipmap levels N and N+1, this function
487 * computes N.
488 */
489 static INLINE GLint
490 linear_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
491 {
492 if (lambda < 0.0F)
493 return tObj->BaseLevel;
494 else if (lambda > tObj->_MaxLambda)
495 return (GLint) (tObj->BaseLevel + tObj->_MaxLambda);
496 else
497 return (GLint) (tObj->BaseLevel + lambda);
498 }
499
500
501 /**
502 * Compute the nearest mipmap level to take texels from.
503 */
504 static INLINE GLint
505 nearest_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
506 {
507 GLfloat l;
508 GLint level;
509 if (lambda <= 0.5F)
510 l = 0.0F;
511 else if (lambda > tObj->_MaxLambda + 0.4999F)
512 l = tObj->_MaxLambda + 0.4999F;
513 else
514 l = lambda;
515 level = (GLint) (tObj->BaseLevel + l + 0.5F);
516 if (level > tObj->_MaxLevel)
517 level = tObj->_MaxLevel;
518 return level;
519 }
520
521
522
523 /*
524 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
525 * see 1-pixel bands of improperly weighted linear-filtered textures.
526 * The tests/texwrap.c demo is a good test.
527 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
528 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
529 */
530 #define FRAC(f) ((f) - IFLOOR(f))
531
532
533
534 /*
535 * Bitflags for texture border color sampling.
536 */
537 #define I0BIT 1
538 #define I1BIT 2
539 #define J0BIT 4
540 #define J1BIT 8
541 #define K0BIT 16
542 #define K1BIT 32
543
544
545
546 /*
547 * The lambda[] array values are always monotonic. Either the whole span
548 * will be minified, magnified, or split between the two. This function
549 * determines the subranges in [0, n-1] that are to be minified or magnified.
550 */
551 static INLINE void
552 compute_min_mag_ranges(const struct gl_texture_object *tObj,
553 GLuint n, const GLfloat lambda[],
554 GLuint *minStart, GLuint *minEnd,
555 GLuint *magStart, GLuint *magEnd)
556 {
557 GLfloat minMagThresh;
558
559 /* we shouldn't be here if minfilter == magfilter */
560 ASSERT(tObj->MinFilter != tObj->MagFilter);
561
562 /* This bit comes from the OpenGL spec: */
563 if (tObj->MagFilter == GL_LINEAR
564 && (tObj->MinFilter == GL_NEAREST_MIPMAP_NEAREST ||
565 tObj->MinFilter == GL_NEAREST_MIPMAP_LINEAR)) {
566 minMagThresh = 0.5F;
567 }
568 else {
569 minMagThresh = 0.0F;
570 }
571
572 #if 0
573 /* DEBUG CODE: Verify that lambda[] is monotonic.
574 * We can't really use this because the inaccuracy in the LOG2 function
575 * causes this test to fail, yet the resulting texturing is correct.
576 */
577 if (n > 1) {
578 GLuint i;
579 printf("lambda delta = %g\n", lambda[0] - lambda[n-1]);
580 if (lambda[0] >= lambda[n-1]) { /* decreasing */
581 for (i = 0; i < n - 1; i++) {
582 ASSERT((GLint) (lambda[i] * 10) >= (GLint) (lambda[i+1] * 10));
583 }
584 }
585 else { /* increasing */
586 for (i = 0; i < n - 1; i++) {
587 ASSERT((GLint) (lambda[i] * 10) <= (GLint) (lambda[i+1] * 10));
588 }
589 }
590 }
591 #endif /* DEBUG */
592
593 if (lambda[0] <= minMagThresh && (n <= 1 || lambda[n-1] <= minMagThresh)) {
594 /* magnification for whole span */
595 *magStart = 0;
596 *magEnd = n;
597 *minStart = *minEnd = 0;
598 }
599 else if (lambda[0] > minMagThresh && (n <=1 || lambda[n-1] > minMagThresh)) {
600 /* minification for whole span */
601 *minStart = 0;
602 *minEnd = n;
603 *magStart = *magEnd = 0;
604 }
605 else {
606 /* a mix of minification and magnification */
607 GLuint i;
608 if (lambda[0] > minMagThresh) {
609 /* start with minification */
610 for (i = 1; i < n; i++) {
611 if (lambda[i] <= minMagThresh)
612 break;
613 }
614 *minStart = 0;
615 *minEnd = i;
616 *magStart = i;
617 *magEnd = n;
618 }
619 else {
620 /* start with magnification */
621 for (i = 1; i < n; i++) {
622 if (lambda[i] > minMagThresh)
623 break;
624 }
625 *magStart = 0;
626 *magEnd = i;
627 *minStart = i;
628 *minEnd = n;
629 }
630 }
631
632 #if 0
633 /* Verify the min/mag Start/End values
634 * We don't use this either (see above)
635 */
636 {
637 GLint i;
638 for (i = 0; i < n; i++) {
639 if (lambda[i] > minMagThresh) {
640 /* minification */
641 ASSERT(i >= *minStart);
642 ASSERT(i < *minEnd);
643 }
644 else {
645 /* magnification */
646 ASSERT(i >= *magStart);
647 ASSERT(i < *magEnd);
648 }
649 }
650 }
651 #endif
652 }
653
654
655 /**********************************************************************/
656 /* 1-D Texture Sampling Functions */
657 /**********************************************************************/
658
659 /*
660 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
661 */
662 static void
663 sample_1d_nearest(GLcontext *ctx,
664 const struct gl_texture_object *tObj,
665 const struct gl_texture_image *img,
666 const GLfloat texcoord[4], GLchan rgba[4])
667 {
668 const GLint width = img->Width2; /* without border, power of two */
669 GLint i;
670 COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoord[0], width, i);
671 /* skip over the border, if any */
672 i += img->Border;
673 if (i < 0 || i >= (GLint) img->Width) {
674 /* Need this test for GL_CLAMP_TO_BORDER mode */
675 COPY_CHAN4(rgba, tObj->_BorderChan);
676 }
677 else {
678 img->FetchTexelc(img, i, 0, 0, rgba);
679 }
680 }
681
682
683 /*
684 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
685 */
686 static void
687 sample_1d_linear(GLcontext *ctx,
688 const struct gl_texture_object *tObj,
689 const struct gl_texture_image *img,
690 const GLfloat texcoord[4], GLchan rgba[4])
691 {
692 const GLint width = img->Width2;
693 GLint i0, i1;
694 GLfloat u;
695 GLbitfield useBorderColor = 0x0;
696 GLfloat a;
697 GLchan t0[4], t1[4]; /* texels */
698
699 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoord[0], u, width, i0, i1);
700
701 if (img->Border) {
702 i0 += img->Border;
703 i1 += img->Border;
704 }
705 else {
706 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
707 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
708 }
709
710 /* fetch texel colors */
711 if (useBorderColor & I0BIT) {
712 COPY_CHAN4(t0, tObj->_BorderChan);
713 }
714 else {
715 img->FetchTexelc(img, i0, 0, 0, t0);
716 }
717 if (useBorderColor & I1BIT) {
718 COPY_CHAN4(t1, tObj->_BorderChan);
719 }
720 else {
721 img->FetchTexelc(img, i1, 0, 0, t1);
722 }
723
724 a = FRAC(u);
725 lerp_rgba(rgba, a, t0, t1);
726 }
727
728
729 static void
730 sample_1d_nearest_mipmap_nearest(GLcontext *ctx,
731 const struct gl_texture_object *tObj,
732 GLuint n, const GLfloat texcoord[][4],
733 const GLfloat lambda[], GLchan rgba[][4])
734 {
735 GLuint i;
736 ASSERT(lambda != NULL);
737 for (i = 0; i < n; i++) {
738 GLint level = nearest_mipmap_level(tObj, lambda[i]);
739 sample_1d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
740 }
741 }
742
743
744 static void
745 sample_1d_linear_mipmap_nearest(GLcontext *ctx,
746 const struct gl_texture_object *tObj,
747 GLuint n, const GLfloat texcoord[][4],
748 const GLfloat lambda[], GLchan rgba[][4])
749 {
750 GLuint i;
751 ASSERT(lambda != NULL);
752 for (i = 0; i < n; i++) {
753 GLint level = nearest_mipmap_level(tObj, lambda[i]);
754 sample_1d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
755 }
756 }
757
758
759 static void
760 sample_1d_nearest_mipmap_linear(GLcontext *ctx,
761 const struct gl_texture_object *tObj,
762 GLuint n, const GLfloat texcoord[][4],
763 const GLfloat lambda[], GLchan rgba[][4])
764 {
765 GLuint i;
766 ASSERT(lambda != NULL);
767 for (i = 0; i < n; i++) {
768 GLint level = linear_mipmap_level(tObj, lambda[i]);
769 if (level >= tObj->_MaxLevel) {
770 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
771 texcoord[i], rgba[i]);
772 }
773 else {
774 GLchan t0[4], t1[4];
775 const GLfloat f = FRAC(lambda[i]);
776 sample_1d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
777 sample_1d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
778 lerp_rgba(rgba[i], f, t0, t1);
779 }
780 }
781 }
782
783
784
785 static void
786 sample_1d_linear_mipmap_linear(GLcontext *ctx,
787 const struct gl_texture_object *tObj,
788 GLuint n, const GLfloat texcoord[][4],
789 const GLfloat lambda[], GLchan rgba[][4])
790 {
791 GLuint i;
792 ASSERT(lambda != NULL);
793 for (i = 0; i < n; i++) {
794 GLint level = linear_mipmap_level(tObj, lambda[i]);
795 if (level >= tObj->_MaxLevel) {
796 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
797 texcoord[i], rgba[i]);
798 }
799 else {
800 GLchan t0[4], t1[4];
801 const GLfloat f = FRAC(lambda[i]);
802 sample_1d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
803 sample_1d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
804 lerp_rgba(rgba[i], f, t0, t1);
805 }
806 }
807 }
808
809
810
811 static void
812 sample_nearest_1d( GLcontext *ctx,
813 const struct gl_texture_object *tObj, GLuint n,
814 const GLfloat texcoords[][4], const GLfloat lambda[],
815 GLchan rgba[][4] )
816 {
817 GLuint i;
818 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
819 (void) lambda;
820 for (i=0;i<n;i++) {
821 sample_1d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
822 }
823 }
824
825
826
827 static void
828 sample_linear_1d( GLcontext *ctx,
829 const struct gl_texture_object *tObj, GLuint n,
830 const GLfloat texcoords[][4], const GLfloat lambda[],
831 GLchan rgba[][4] )
832 {
833 GLuint i;
834 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
835 (void) lambda;
836 for (i=0;i<n;i++) {
837 sample_1d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
838 }
839 }
840
841
842 /*
843 * Given an (s) texture coordinate and lambda (level of detail) value,
844 * return a texture sample.
845 *
846 */
847 static void
848 sample_lambda_1d( GLcontext *ctx,
849 const struct gl_texture_object *tObj, GLuint n,
850 const GLfloat texcoords[][4],
851 const GLfloat lambda[], GLchan rgba[][4] )
852 {
853 GLuint minStart, minEnd; /* texels with minification */
854 GLuint magStart, magEnd; /* texels with magnification */
855 GLuint i;
856
857 ASSERT(lambda != NULL);
858 compute_min_mag_ranges(tObj, n, lambda,
859 &minStart, &minEnd, &magStart, &magEnd);
860
861 if (minStart < minEnd) {
862 /* do the minified texels */
863 const GLuint m = minEnd - minStart;
864 switch (tObj->MinFilter) {
865 case GL_NEAREST:
866 for (i = minStart; i < minEnd; i++)
867 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
868 texcoords[i], rgba[i]);
869 break;
870 case GL_LINEAR:
871 for (i = minStart; i < minEnd; i++)
872 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
873 texcoords[i], rgba[i]);
874 break;
875 case GL_NEAREST_MIPMAP_NEAREST:
876 sample_1d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
877 lambda + minStart, rgba + minStart);
878 break;
879 case GL_LINEAR_MIPMAP_NEAREST:
880 sample_1d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
881 lambda + minStart, rgba + minStart);
882 break;
883 case GL_NEAREST_MIPMAP_LINEAR:
884 sample_1d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
885 lambda + minStart, rgba + minStart);
886 break;
887 case GL_LINEAR_MIPMAP_LINEAR:
888 sample_1d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
889 lambda + minStart, rgba + minStart);
890 break;
891 default:
892 _mesa_problem(ctx, "Bad min filter in sample_1d_texture");
893 return;
894 }
895 }
896
897 if (magStart < magEnd) {
898 /* do the magnified texels */
899 switch (tObj->MagFilter) {
900 case GL_NEAREST:
901 for (i = magStart; i < magEnd; i++)
902 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
903 texcoords[i], rgba[i]);
904 break;
905 case GL_LINEAR:
906 for (i = magStart; i < magEnd; i++)
907 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
908 texcoords[i], rgba[i]);
909 break;
910 default:
911 _mesa_problem(ctx, "Bad mag filter in sample_1d_texture");
912 return;
913 }
914 }
915 }
916
917
918 /**********************************************************************/
919 /* 2-D Texture Sampling Functions */
920 /**********************************************************************/
921
922
923 /*
924 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
925 */
926 static INLINE void
927 sample_2d_nearest(GLcontext *ctx,
928 const struct gl_texture_object *tObj,
929 const struct gl_texture_image *img,
930 const GLfloat texcoord[4],
931 GLchan rgba[])
932 {
933 const GLint width = img->Width2; /* without border, power of two */
934 const GLint height = img->Height2; /* without border, power of two */
935 GLint i, j;
936 (void) ctx;
937
938 COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoord[0], width, i);
939 COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapT, texcoord[1], height, j);
940
941 /* skip over the border, if any */
942 i += img->Border;
943 j += img->Border;
944
945 if (i < 0 || i >= (GLint) img->Width || j < 0 || j >= (GLint) img->Height) {
946 /* Need this test for GL_CLAMP_TO_BORDER mode */
947 COPY_CHAN4(rgba, tObj->_BorderChan);
948 }
949 else {
950 img->FetchTexelc(img, i, j, 0, rgba);
951 }
952 }
953
954
955
956 /**
957 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
958 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
959 */
960 static INLINE void
961 sample_2d_linear(GLcontext *ctx,
962 const struct gl_texture_object *tObj,
963 const struct gl_texture_image *img,
964 const GLfloat texcoord[4],
965 GLchan rgba[])
966 {
967 const GLint width = img->Width2;
968 const GLint height = img->Height2;
969 GLint i0, j0, i1, j1;
970 GLbitfield useBorderColor = 0x0;
971 GLfloat u, v;
972 GLfloat a, b;
973 GLchan t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
974
975 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoord[0], u, width, i0, i1);
976 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapT, texcoord[1], v, height, j0, j1);
977
978 if (img->Border) {
979 i0 += img->Border;
980 i1 += img->Border;
981 j0 += img->Border;
982 j1 += img->Border;
983 }
984 else {
985 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
986 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
987 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
988 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
989 }
990
991 /* fetch four texel colors */
992 if (useBorderColor & (I0BIT | J0BIT)) {
993 COPY_CHAN4(t00, tObj->_BorderChan);
994 }
995 else {
996 img->FetchTexelc(img, i0, j0, 0, t00);
997 }
998 if (useBorderColor & (I1BIT | J0BIT)) {
999 COPY_CHAN4(t10, tObj->_BorderChan);
1000 }
1001 else {
1002 img->FetchTexelc(img, i1, j0, 0, t10);
1003 }
1004 if (useBorderColor & (I0BIT | J1BIT)) {
1005 COPY_CHAN4(t01, tObj->_BorderChan);
1006 }
1007 else {
1008 img->FetchTexelc(img, i0, j1, 0, t01);
1009 }
1010 if (useBorderColor & (I1BIT | J1BIT)) {
1011 COPY_CHAN4(t11, tObj->_BorderChan);
1012 }
1013 else {
1014 img->FetchTexelc(img, i1, j1, 0, t11);
1015 }
1016
1017 a = FRAC(u);
1018 b = FRAC(v);
1019 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
1020 }
1021
1022
1023 /*
1024 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1025 * We don't have to worry about the texture border.
1026 */
1027 static INLINE void
1028 sample_2d_linear_repeat(GLcontext *ctx,
1029 const struct gl_texture_object *tObj,
1030 const struct gl_texture_image *img,
1031 const GLfloat texcoord[4],
1032 GLchan rgba[])
1033 {
1034 const GLint width = img->Width2;
1035 const GLint height = img->Height2;
1036 GLint i0, j0, i1, j1;
1037 GLfloat u, v;
1038 GLfloat a, b;
1039 GLchan t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
1040
1041 (void) ctx;
1042
1043 ASSERT(tObj->WrapS == GL_REPEAT);
1044 ASSERT(tObj->WrapT == GL_REPEAT);
1045 ASSERT(img->Border == 0);
1046 ASSERT(img->_BaseFormat != GL_COLOR_INDEX);
1047 ASSERT(img->_IsPowerOfTwo);
1048
1049 COMPUTE_LINEAR_REPEAT_TEXEL_LOCATION(texcoord[0], u, width, i0, i1);
1050 COMPUTE_LINEAR_REPEAT_TEXEL_LOCATION(texcoord[1], v, height, j0, j1);
1051
1052 img->FetchTexelc(img, i0, j0, 0, t00);
1053 img->FetchTexelc(img, i1, j0, 0, t10);
1054 img->FetchTexelc(img, i0, j1, 0, t01);
1055 img->FetchTexelc(img, i1, j1, 0, t11);
1056
1057 a = FRAC(u);
1058 b = FRAC(v);
1059 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
1060 }
1061
1062
1063
1064 static void
1065 sample_2d_nearest_mipmap_nearest(GLcontext *ctx,
1066 const struct gl_texture_object *tObj,
1067 GLuint n, const GLfloat texcoord[][4],
1068 const GLfloat lambda[], GLchan rgba[][4])
1069 {
1070 GLuint i;
1071 for (i = 0; i < n; i++) {
1072 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1073 sample_2d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1074 }
1075 }
1076
1077
1078
1079 static void
1080 sample_2d_linear_mipmap_nearest(GLcontext *ctx,
1081 const struct gl_texture_object *tObj,
1082 GLuint n, const GLfloat texcoord[][4],
1083 const GLfloat lambda[], GLchan rgba[][4])
1084 {
1085 GLuint i;
1086 ASSERT(lambda != NULL);
1087 for (i = 0; i < n; i++) {
1088 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1089 sample_2d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1090 }
1091 }
1092
1093
1094
1095 static void
1096 sample_2d_nearest_mipmap_linear(GLcontext *ctx,
1097 const struct gl_texture_object *tObj,
1098 GLuint n, const GLfloat texcoord[][4],
1099 const GLfloat lambda[], GLchan rgba[][4])
1100 {
1101 GLuint i;
1102 ASSERT(lambda != NULL);
1103 for (i = 0; i < n; i++) {
1104 GLint level = linear_mipmap_level(tObj, lambda[i]);
1105 if (level >= tObj->_MaxLevel) {
1106 sample_2d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1107 texcoord[i], rgba[i]);
1108 }
1109 else {
1110 GLchan t0[4], t1[4]; /* texels */
1111 const GLfloat f = FRAC(lambda[i]);
1112 sample_2d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1113 sample_2d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1114 lerp_rgba(rgba[i], f, t0, t1);
1115 }
1116 }
1117 }
1118
1119
1120
1121 /* Trilinear filtering */
1122 static void
1123 sample_2d_linear_mipmap_linear( GLcontext *ctx,
1124 const struct gl_texture_object *tObj,
1125 GLuint n, const GLfloat texcoord[][4],
1126 const GLfloat lambda[], GLchan rgba[][4] )
1127 {
1128 GLuint i;
1129 ASSERT(lambda != NULL);
1130 for (i = 0; i < n; i++) {
1131 GLint level = linear_mipmap_level(tObj, lambda[i]);
1132 if (level >= tObj->_MaxLevel) {
1133 sample_2d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1134 texcoord[i], rgba[i]);
1135 }
1136 else {
1137 GLchan t0[4], t1[4]; /* texels */
1138 const GLfloat f = FRAC(lambda[i]);
1139 sample_2d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1140 sample_2d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1141 lerp_rgba(rgba[i], f, t0, t1);
1142 }
1143 }
1144 }
1145
1146
1147 static void
1148 sample_2d_linear_mipmap_linear_repeat( GLcontext *ctx,
1149 const struct gl_texture_object *tObj,
1150 GLuint n, const GLfloat texcoord[][4],
1151 const GLfloat lambda[], GLchan rgba[][4] )
1152 {
1153 GLuint i;
1154 ASSERT(lambda != NULL);
1155 ASSERT(tObj->WrapS == GL_REPEAT);
1156 ASSERT(tObj->WrapT == GL_REPEAT);
1157 for (i = 0; i < n; i++) {
1158 GLint level = linear_mipmap_level(tObj, lambda[i]);
1159 if (level >= tObj->_MaxLevel) {
1160 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1161 texcoord[i], rgba[i]);
1162 }
1163 else {
1164 GLchan t0[4], t1[4]; /* texels */
1165 const GLfloat f = FRAC(lambda[i]);
1166 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1167 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1168 lerp_rgba(rgba[i], f, t0, t1);
1169 }
1170 }
1171 }
1172
1173
1174 static void
1175 sample_nearest_2d( GLcontext *ctx,
1176 const struct gl_texture_object *tObj, GLuint n,
1177 const GLfloat texcoords[][4],
1178 const GLfloat lambda[], GLchan rgba[][4] )
1179 {
1180 GLuint i;
1181 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1182 (void) lambda;
1183 for (i=0;i<n;i++) {
1184 sample_2d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
1185 }
1186 }
1187
1188
1189
1190 static void
1191 sample_linear_2d( GLcontext *ctx,
1192 const struct gl_texture_object *tObj, GLuint n,
1193 const GLfloat texcoords[][4],
1194 const GLfloat lambda[], GLchan rgba[][4] )
1195 {
1196 GLuint i;
1197 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1198 (void) lambda;
1199 if (tObj->WrapS == GL_REPEAT &&
1200 tObj->WrapT == GL_REPEAT &&
1201 image->_IsPowerOfTwo) {
1202 for (i=0;i<n;i++) {
1203 sample_2d_linear_repeat(ctx, tObj, image, texcoords[i], rgba[i]);
1204 }
1205 }
1206 else {
1207 for (i=0;i<n;i++) {
1208 sample_2d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
1209 }
1210 }
1211 }
1212
1213
1214 /*
1215 * Optimized 2-D texture sampling:
1216 * S and T wrap mode == GL_REPEAT
1217 * GL_NEAREST min/mag filter
1218 * No border,
1219 * RowStride == Width,
1220 * Format = GL_RGB
1221 */
1222 static void
1223 opt_sample_rgb_2d( GLcontext *ctx,
1224 const struct gl_texture_object *tObj,
1225 GLuint n, const GLfloat texcoords[][4],
1226 const GLfloat lambda[], GLchan rgba[][4] )
1227 {
1228 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1229 const GLfloat width = (GLfloat) img->Width;
1230 const GLfloat height = (GLfloat) img->Height;
1231 const GLint colMask = img->Width - 1;
1232 const GLint rowMask = img->Height - 1;
1233 const GLint shift = img->WidthLog2;
1234 GLuint k;
1235 (void) ctx;
1236 (void) lambda;
1237 ASSERT(tObj->WrapS==GL_REPEAT);
1238 ASSERT(tObj->WrapT==GL_REPEAT);
1239 ASSERT(img->Border==0);
1240 ASSERT(img->_BaseFormat==GL_RGB);
1241 ASSERT(img->_IsPowerOfTwo);
1242
1243 for (k=0; k<n; k++) {
1244 GLint i = IFLOOR(texcoords[k][0] * width) & colMask;
1245 GLint j = IFLOOR(texcoords[k][1] * height) & rowMask;
1246 GLint pos = (j << shift) | i;
1247 GLchan *texel = ((GLchan *) img->Data) + 3*pos;
1248 rgba[k][RCOMP] = texel[0];
1249 rgba[k][GCOMP] = texel[1];
1250 rgba[k][BCOMP] = texel[2];
1251 }
1252 }
1253
1254
1255 /*
1256 * Optimized 2-D texture sampling:
1257 * S and T wrap mode == GL_REPEAT
1258 * GL_NEAREST min/mag filter
1259 * No border
1260 * RowStride == Width,
1261 * Format = GL_RGBA
1262 */
1263 static void
1264 opt_sample_rgba_2d( GLcontext *ctx,
1265 const struct gl_texture_object *tObj,
1266 GLuint n, const GLfloat texcoords[][4],
1267 const GLfloat lambda[], GLchan rgba[][4] )
1268 {
1269 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1270 const GLfloat width = (GLfloat) img->Width;
1271 const GLfloat height = (GLfloat) img->Height;
1272 const GLint colMask = img->Width - 1;
1273 const GLint rowMask = img->Height - 1;
1274 const GLint shift = img->WidthLog2;
1275 GLuint i;
1276 (void) ctx;
1277 (void) lambda;
1278 ASSERT(tObj->WrapS==GL_REPEAT);
1279 ASSERT(tObj->WrapT==GL_REPEAT);
1280 ASSERT(img->Border==0);
1281 ASSERT(img->_BaseFormat==GL_RGBA);
1282 ASSERT(img->_IsPowerOfTwo);
1283
1284 for (i = 0; i < n; i++) {
1285 const GLint col = IFLOOR(texcoords[i][0] * width) & colMask;
1286 const GLint row = IFLOOR(texcoords[i][1] * height) & rowMask;
1287 const GLint pos = (row << shift) | col;
1288 const GLchan *texel = ((GLchan *) img->Data) + (pos << 2); /* pos*4 */
1289 COPY_CHAN4(rgba[i], texel);
1290 }
1291 }
1292
1293
1294 /*
1295 * Given an array of texture coordinate and lambda (level of detail)
1296 * values, return an array of texture sample.
1297 */
1298 static void
1299 sample_lambda_2d( GLcontext *ctx,
1300 const struct gl_texture_object *tObj,
1301 GLuint n, const GLfloat texcoords[][4],
1302 const GLfloat lambda[], GLchan rgba[][4] )
1303 {
1304 const struct gl_texture_image *tImg = tObj->Image[0][tObj->BaseLevel];
1305 GLuint minStart, minEnd; /* texels with minification */
1306 GLuint magStart, magEnd; /* texels with magnification */
1307
1308 const GLboolean repeatNoBorderPOT = (tObj->WrapS == GL_REPEAT)
1309 && (tObj->WrapT == GL_REPEAT)
1310 && (tImg->Border == 0 && (tImg->Width == tImg->RowStride))
1311 && (tImg->_BaseFormat != GL_COLOR_INDEX)
1312 && tImg->_IsPowerOfTwo;
1313
1314 ASSERT(lambda != NULL);
1315 compute_min_mag_ranges(tObj, n, lambda,
1316 &minStart, &minEnd, &magStart, &magEnd);
1317
1318 if (minStart < minEnd) {
1319 /* do the minified texels */
1320 const GLuint m = minEnd - minStart;
1321 switch (tObj->MinFilter) {
1322 case GL_NEAREST:
1323 if (repeatNoBorderPOT) {
1324 switch (tImg->TexFormat->MesaFormat) {
1325 case MESA_FORMAT_RGB:
1326 case MESA_FORMAT_RGB888:
1327 /*case MESA_FORMAT_BGR888:*/
1328 opt_sample_rgb_2d(ctx, tObj, m, texcoords + minStart,
1329 NULL, rgba + minStart);
1330 break;
1331 case MESA_FORMAT_RGBA:
1332 case MESA_FORMAT_RGBA8888:
1333 case MESA_FORMAT_ARGB8888:
1334 /*case MESA_FORMAT_ABGR8888:*/
1335 /*case MESA_FORMAT_BGRA8888:*/
1336 opt_sample_rgba_2d(ctx, tObj, m, texcoords + minStart,
1337 NULL, rgba + minStart);
1338 break;
1339 default:
1340 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1341 NULL, rgba + minStart );
1342 }
1343 }
1344 else {
1345 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1346 NULL, rgba + minStart);
1347 }
1348 break;
1349 case GL_LINEAR:
1350 sample_linear_2d(ctx, tObj, m, texcoords + minStart,
1351 NULL, rgba + minStart);
1352 break;
1353 case GL_NEAREST_MIPMAP_NEAREST:
1354 sample_2d_nearest_mipmap_nearest(ctx, tObj, m,
1355 texcoords + minStart,
1356 lambda + minStart, rgba + minStart);
1357 break;
1358 case GL_LINEAR_MIPMAP_NEAREST:
1359 sample_2d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1360 lambda + minStart, rgba + minStart);
1361 break;
1362 case GL_NEAREST_MIPMAP_LINEAR:
1363 sample_2d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1364 lambda + minStart, rgba + minStart);
1365 break;
1366 case GL_LINEAR_MIPMAP_LINEAR:
1367 if (repeatNoBorderPOT)
1368 sample_2d_linear_mipmap_linear_repeat(ctx, tObj, m,
1369 texcoords + minStart, lambda + minStart, rgba + minStart);
1370 else
1371 sample_2d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1372 lambda + minStart, rgba + minStart);
1373 break;
1374 default:
1375 _mesa_problem(ctx, "Bad min filter in sample_2d_texture");
1376 return;
1377 }
1378 }
1379
1380 if (magStart < magEnd) {
1381 /* do the magnified texels */
1382 const GLuint m = magEnd - magStart;
1383
1384 switch (tObj->MagFilter) {
1385 case GL_NEAREST:
1386 if (repeatNoBorderPOT) {
1387 switch (tImg->TexFormat->MesaFormat) {
1388 case MESA_FORMAT_RGB:
1389 case MESA_FORMAT_RGB888:
1390 /*case MESA_FORMAT_BGR888:*/
1391 opt_sample_rgb_2d(ctx, tObj, m, texcoords + magStart,
1392 NULL, rgba + magStart);
1393 break;
1394 case MESA_FORMAT_RGBA:
1395 case MESA_FORMAT_RGBA8888:
1396 case MESA_FORMAT_ARGB8888:
1397 /*case MESA_FORMAT_ABGR8888:*/
1398 /*case MESA_FORMAT_BGRA8888:*/
1399 opt_sample_rgba_2d(ctx, tObj, m, texcoords + magStart,
1400 NULL, rgba + magStart);
1401 break;
1402 default:
1403 sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
1404 NULL, rgba + magStart );
1405 }
1406 }
1407 else {
1408 sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
1409 NULL, rgba + magStart);
1410 }
1411 break;
1412 case GL_LINEAR:
1413 sample_linear_2d(ctx, tObj, m, texcoords + magStart,
1414 NULL, rgba + magStart);
1415 break;
1416 default:
1417 _mesa_problem(ctx, "Bad mag filter in sample_lambda_2d");
1418 }
1419 }
1420 }
1421
1422
1423
1424 /**********************************************************************/
1425 /* 3-D Texture Sampling Functions */
1426 /**********************************************************************/
1427
1428 /*
1429 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1430 */
1431 static void
1432 sample_3d_nearest(GLcontext *ctx,
1433 const struct gl_texture_object *tObj,
1434 const struct gl_texture_image *img,
1435 const GLfloat texcoord[4],
1436 GLchan rgba[4])
1437 {
1438 const GLint width = img->Width2; /* without border, power of two */
1439 const GLint height = img->Height2; /* without border, power of two */
1440 const GLint depth = img->Depth2; /* without border, power of two */
1441 GLint i, j, k;
1442 (void) ctx;
1443
1444 COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoord[0], width, i);
1445 COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapT, texcoord[1], height, j);
1446 COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapR, texcoord[2], depth, k);
1447
1448 if (i < 0 || i >= (GLint) img->Width ||
1449 j < 0 || j >= (GLint) img->Height ||
1450 k < 0 || k >= (GLint) img->Depth) {
1451 /* Need this test for GL_CLAMP_TO_BORDER mode */
1452 COPY_CHAN4(rgba, tObj->_BorderChan);
1453 }
1454 else {
1455 img->FetchTexelc(img, i, j, k, rgba);
1456 }
1457 }
1458
1459
1460
1461 /*
1462 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1463 */
1464 static void
1465 sample_3d_linear(GLcontext *ctx,
1466 const struct gl_texture_object *tObj,
1467 const struct gl_texture_image *img,
1468 const GLfloat texcoord[4],
1469 GLchan rgba[4])
1470 {
1471 const GLint width = img->Width2;
1472 const GLint height = img->Height2;
1473 const GLint depth = img->Depth2;
1474 GLint i0, j0, k0, i1, j1, k1;
1475 GLbitfield useBorderColor = 0x0;
1476 GLfloat u, v, w;
1477 GLfloat a, b, c;
1478 GLchan t000[4], t010[4], t001[4], t011[4];
1479 GLchan t100[4], t110[4], t101[4], t111[4];
1480
1481 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoord[0], u, width, i0, i1);
1482 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapT, texcoord[1], v, height, j0, j1);
1483 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapR, texcoord[2], w, depth, k0, k1);
1484
1485 if (img->Border) {
1486 i0 += img->Border;
1487 i1 += img->Border;
1488 j0 += img->Border;
1489 j1 += img->Border;
1490 k0 += img->Border;
1491 k1 += img->Border;
1492 }
1493 else {
1494 /* check if sampling texture border color */
1495 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
1496 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
1497 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
1498 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
1499 if (k0 < 0 || k0 >= depth) useBorderColor |= K0BIT;
1500 if (k1 < 0 || k1 >= depth) useBorderColor |= K1BIT;
1501 }
1502
1503 /* Fetch texels */
1504 if (useBorderColor & (I0BIT | J0BIT | K0BIT)) {
1505 COPY_CHAN4(t000, tObj->_BorderChan);
1506 }
1507 else {
1508 img->FetchTexelc(img, i0, j0, k0, t000);
1509 }
1510 if (useBorderColor & (I1BIT | J0BIT | K0BIT)) {
1511 COPY_CHAN4(t100, tObj->_BorderChan);
1512 }
1513 else {
1514 img->FetchTexelc(img, i1, j0, k0, t100);
1515 }
1516 if (useBorderColor & (I0BIT | J1BIT | K0BIT)) {
1517 COPY_CHAN4(t010, tObj->_BorderChan);
1518 }
1519 else {
1520 img->FetchTexelc(img, i0, j1, k0, t010);
1521 }
1522 if (useBorderColor & (I1BIT | J1BIT | K0BIT)) {
1523 COPY_CHAN4(t110, tObj->_BorderChan);
1524 }
1525 else {
1526 img->FetchTexelc(img, i1, j1, k0, t110);
1527 }
1528
1529 if (useBorderColor & (I0BIT | J0BIT | K1BIT)) {
1530 COPY_CHAN4(t001, tObj->_BorderChan);
1531 }
1532 else {
1533 img->FetchTexelc(img, i0, j0, k1, t001);
1534 }
1535 if (useBorderColor & (I1BIT | J0BIT | K1BIT)) {
1536 COPY_CHAN4(t101, tObj->_BorderChan);
1537 }
1538 else {
1539 img->FetchTexelc(img, i1, j0, k1, t101);
1540 }
1541 if (useBorderColor & (I0BIT | J1BIT | K1BIT)) {
1542 COPY_CHAN4(t011, tObj->_BorderChan);
1543 }
1544 else {
1545 img->FetchTexelc(img, i0, j1, k1, t011);
1546 }
1547 if (useBorderColor & (I1BIT | J1BIT | K1BIT)) {
1548 COPY_CHAN4(t111, tObj->_BorderChan);
1549 }
1550 else {
1551 img->FetchTexelc(img, i1, j1, k1, t111);
1552 }
1553
1554 /* trilinear interpolation of samples */
1555 a = FRAC(u);
1556 b = FRAC(v);
1557 c = FRAC(w);
1558 lerp_rgba_3d(rgba, a, b, c, t000, t100, t010, t110, t001, t101, t011, t111);
1559 }
1560
1561
1562
1563 static void
1564 sample_3d_nearest_mipmap_nearest(GLcontext *ctx,
1565 const struct gl_texture_object *tObj,
1566 GLuint n, const GLfloat texcoord[][4],
1567 const GLfloat lambda[], GLchan rgba[][4] )
1568 {
1569 GLuint i;
1570 for (i = 0; i < n; i++) {
1571 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1572 sample_3d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1573 }
1574 }
1575
1576
1577 static void
1578 sample_3d_linear_mipmap_nearest(GLcontext *ctx,
1579 const struct gl_texture_object *tObj,
1580 GLuint n, const GLfloat texcoord[][4],
1581 const GLfloat lambda[], GLchan rgba[][4])
1582 {
1583 GLuint i;
1584 ASSERT(lambda != NULL);
1585 for (i = 0; i < n; i++) {
1586 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1587 sample_3d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1588 }
1589 }
1590
1591
1592 static void
1593 sample_3d_nearest_mipmap_linear(GLcontext *ctx,
1594 const struct gl_texture_object *tObj,
1595 GLuint n, const GLfloat texcoord[][4],
1596 const GLfloat lambda[], GLchan rgba[][4])
1597 {
1598 GLuint i;
1599 ASSERT(lambda != NULL);
1600 for (i = 0; i < n; i++) {
1601 GLint level = linear_mipmap_level(tObj, lambda[i]);
1602 if (level >= tObj->_MaxLevel) {
1603 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1604 texcoord[i], rgba[i]);
1605 }
1606 else {
1607 GLchan t0[4], t1[4]; /* texels */
1608 const GLfloat f = FRAC(lambda[i]);
1609 sample_3d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1610 sample_3d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1611 lerp_rgba(rgba[i], f, t0, t1);
1612 }
1613 }
1614 }
1615
1616
1617 static void
1618 sample_3d_linear_mipmap_linear(GLcontext *ctx,
1619 const struct gl_texture_object *tObj,
1620 GLuint n, const GLfloat texcoord[][4],
1621 const GLfloat lambda[], GLchan rgba[][4])
1622 {
1623 GLuint i;
1624 ASSERT(lambda != NULL);
1625 for (i = 0; i < n; i++) {
1626 GLint level = linear_mipmap_level(tObj, lambda[i]);
1627 if (level >= tObj->_MaxLevel) {
1628 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1629 texcoord[i], rgba[i]);
1630 }
1631 else {
1632 GLchan t0[4], t1[4]; /* texels */
1633 const GLfloat f = FRAC(lambda[i]);
1634 sample_3d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1635 sample_3d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1636 lerp_rgba(rgba[i], f, t0, t1);
1637 }
1638 }
1639 }
1640
1641
1642 static void
1643 sample_nearest_3d(GLcontext *ctx,
1644 const struct gl_texture_object *tObj, GLuint n,
1645 const GLfloat texcoords[][4], const GLfloat lambda[],
1646 GLchan rgba[][4])
1647 {
1648 GLuint i;
1649 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1650 (void) lambda;
1651 for (i=0;i<n;i++) {
1652 sample_3d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
1653 }
1654 }
1655
1656
1657
1658 static void
1659 sample_linear_3d( GLcontext *ctx,
1660 const struct gl_texture_object *tObj, GLuint n,
1661 const GLfloat texcoords[][4],
1662 const GLfloat lambda[], GLchan rgba[][4] )
1663 {
1664 GLuint i;
1665 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1666 (void) lambda;
1667 for (i=0;i<n;i++) {
1668 sample_3d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
1669 }
1670 }
1671
1672
1673 /*
1674 * Given an (s,t,r) texture coordinate and lambda (level of detail) value,
1675 * return a texture sample.
1676 */
1677 static void
1678 sample_lambda_3d( GLcontext *ctx,
1679 const struct gl_texture_object *tObj, GLuint n,
1680 const GLfloat texcoords[][4], const GLfloat lambda[],
1681 GLchan rgba[][4] )
1682 {
1683 GLuint minStart, minEnd; /* texels with minification */
1684 GLuint magStart, magEnd; /* texels with magnification */
1685 GLuint i;
1686
1687 ASSERT(lambda != NULL);
1688 compute_min_mag_ranges(tObj, n, lambda,
1689 &minStart, &minEnd, &magStart, &magEnd);
1690
1691 if (minStart < minEnd) {
1692 /* do the minified texels */
1693 GLuint m = minEnd - minStart;
1694 switch (tObj->MinFilter) {
1695 case GL_NEAREST:
1696 for (i = minStart; i < minEnd; i++)
1697 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1698 texcoords[i], rgba[i]);
1699 break;
1700 case GL_LINEAR:
1701 for (i = minStart; i < minEnd; i++)
1702 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1703 texcoords[i], rgba[i]);
1704 break;
1705 case GL_NEAREST_MIPMAP_NEAREST:
1706 sample_3d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1707 lambda + minStart, rgba + minStart);
1708 break;
1709 case GL_LINEAR_MIPMAP_NEAREST:
1710 sample_3d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1711 lambda + minStart, rgba + minStart);
1712 break;
1713 case GL_NEAREST_MIPMAP_LINEAR:
1714 sample_3d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1715 lambda + minStart, rgba + minStart);
1716 break;
1717 case GL_LINEAR_MIPMAP_LINEAR:
1718 sample_3d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1719 lambda + minStart, rgba + minStart);
1720 break;
1721 default:
1722 _mesa_problem(ctx, "Bad min filter in sample_3d_texture");
1723 return;
1724 }
1725 }
1726
1727 if (magStart < magEnd) {
1728 /* do the magnified texels */
1729 switch (tObj->MagFilter) {
1730 case GL_NEAREST:
1731 for (i = magStart; i < magEnd; i++)
1732 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1733 texcoords[i], rgba[i]);
1734 break;
1735 case GL_LINEAR:
1736 for (i = magStart; i < magEnd; i++)
1737 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1738 texcoords[i], rgba[i]);
1739 break;
1740 default:
1741 _mesa_problem(ctx, "Bad mag filter in sample_3d_texture");
1742 return;
1743 }
1744 }
1745 }
1746
1747
1748 /**********************************************************************/
1749 /* Texture Cube Map Sampling Functions */
1750 /**********************************************************************/
1751
1752 /**
1753 * Choose one of six sides of a texture cube map given the texture
1754 * coord (rx,ry,rz). Return pointer to corresponding array of texture
1755 * images.
1756 */
1757 static const struct gl_texture_image **
1758 choose_cube_face(const struct gl_texture_object *texObj,
1759 const GLfloat texcoord[4], GLfloat newCoord[4])
1760 {
1761 /*
1762 major axis
1763 direction target sc tc ma
1764 ---------- ------------------------------- --- --- ---
1765 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1766 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1767 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1768 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1769 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1770 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1771 */
1772 const GLfloat rx = texcoord[0];
1773 const GLfloat ry = texcoord[1];
1774 const GLfloat rz = texcoord[2];
1775 const GLfloat arx = FABSF(rx), ary = FABSF(ry), arz = FABSF(rz);
1776 GLuint face;
1777 GLfloat sc, tc, ma;
1778
1779 if (arx > ary && arx > arz) {
1780 if (rx >= 0.0F) {
1781 face = FACE_POS_X;
1782 sc = -rz;
1783 tc = -ry;
1784 ma = arx;
1785 }
1786 else {
1787 face = FACE_NEG_X;
1788 sc = rz;
1789 tc = -ry;
1790 ma = arx;
1791 }
1792 }
1793 else if (ary > arx && ary > arz) {
1794 if (ry >= 0.0F) {
1795 face = FACE_POS_Y;
1796 sc = rx;
1797 tc = rz;
1798 ma = ary;
1799 }
1800 else {
1801 face = FACE_NEG_Y;
1802 sc = rx;
1803 tc = -rz;
1804 ma = ary;
1805 }
1806 }
1807 else {
1808 if (rz > 0.0F) {
1809 face = FACE_POS_Z;
1810 sc = rx;
1811 tc = -ry;
1812 ma = arz;
1813 }
1814 else {
1815 face = FACE_NEG_Z;
1816 sc = -rx;
1817 tc = -ry;
1818 ma = arz;
1819 }
1820 }
1821
1822 newCoord[0] = ( sc / ma + 1.0F ) * 0.5F;
1823 newCoord[1] = ( tc / ma + 1.0F ) * 0.5F;
1824 return (const struct gl_texture_image **) texObj->Image[face];
1825 }
1826
1827
1828 static void
1829 sample_nearest_cube(GLcontext *ctx,
1830 const struct gl_texture_object *tObj, GLuint n,
1831 const GLfloat texcoords[][4], const GLfloat lambda[],
1832 GLchan rgba[][4])
1833 {
1834 GLuint i;
1835 (void) lambda;
1836 for (i = 0; i < n; i++) {
1837 const struct gl_texture_image **images;
1838 GLfloat newCoord[4];
1839 images = choose_cube_face(tObj, texcoords[i], newCoord);
1840 sample_2d_nearest(ctx, tObj, images[tObj->BaseLevel],
1841 newCoord, rgba[i]);
1842 }
1843 }
1844
1845
1846 static void
1847 sample_linear_cube(GLcontext *ctx,
1848 const struct gl_texture_object *tObj, GLuint n,
1849 const GLfloat texcoords[][4],
1850 const GLfloat lambda[], GLchan rgba[][4])
1851 {
1852 GLuint i;
1853 (void) lambda;
1854 for (i = 0; i < n; i++) {
1855 const struct gl_texture_image **images;
1856 GLfloat newCoord[4];
1857 images = choose_cube_face(tObj, texcoords[i], newCoord);
1858 sample_2d_linear(ctx, tObj, images[tObj->BaseLevel],
1859 newCoord, rgba[i]);
1860 }
1861 }
1862
1863
1864 static void
1865 sample_cube_nearest_mipmap_nearest(GLcontext *ctx,
1866 const struct gl_texture_object *tObj,
1867 GLuint n, const GLfloat texcoord[][4],
1868 const GLfloat lambda[], GLchan rgba[][4])
1869 {
1870 GLuint i;
1871 ASSERT(lambda != NULL);
1872 for (i = 0; i < n; i++) {
1873 const struct gl_texture_image **images;
1874 GLfloat newCoord[4];
1875 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1876 images = choose_cube_face(tObj, texcoord[i], newCoord);
1877 sample_2d_nearest(ctx, tObj, images[level], newCoord, rgba[i]);
1878 }
1879 }
1880
1881
1882 static void
1883 sample_cube_linear_mipmap_nearest(GLcontext *ctx,
1884 const struct gl_texture_object *tObj,
1885 GLuint n, const GLfloat texcoord[][4],
1886 const GLfloat lambda[], GLchan rgba[][4])
1887 {
1888 GLuint i;
1889 ASSERT(lambda != NULL);
1890 for (i = 0; i < n; i++) {
1891 const struct gl_texture_image **images;
1892 GLfloat newCoord[4];
1893 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1894 images = choose_cube_face(tObj, texcoord[i], newCoord);
1895 sample_2d_linear(ctx, tObj, images[level], newCoord, rgba[i]);
1896 }
1897 }
1898
1899
1900 static void
1901 sample_cube_nearest_mipmap_linear(GLcontext *ctx,
1902 const struct gl_texture_object *tObj,
1903 GLuint n, const GLfloat texcoord[][4],
1904 const GLfloat lambda[], GLchan rgba[][4])
1905 {
1906 GLuint i;
1907 ASSERT(lambda != NULL);
1908 for (i = 0; i < n; i++) {
1909 const struct gl_texture_image **images;
1910 GLfloat newCoord[4];
1911 GLint level = linear_mipmap_level(tObj, lambda[i]);
1912 images = choose_cube_face(tObj, texcoord[i], newCoord);
1913 if (level >= tObj->_MaxLevel) {
1914 sample_2d_nearest(ctx, tObj, images[tObj->_MaxLevel],
1915 newCoord, rgba[i]);
1916 }
1917 else {
1918 GLchan t0[4], t1[4]; /* texels */
1919 const GLfloat f = FRAC(lambda[i]);
1920 sample_2d_nearest(ctx, tObj, images[level ], newCoord, t0);
1921 sample_2d_nearest(ctx, tObj, images[level+1], newCoord, t1);
1922 lerp_rgba(rgba[i], f, t0, t1);
1923 }
1924 }
1925 }
1926
1927
1928 static void
1929 sample_cube_linear_mipmap_linear(GLcontext *ctx,
1930 const struct gl_texture_object *tObj,
1931 GLuint n, const GLfloat texcoord[][4],
1932 const GLfloat lambda[], GLchan rgba[][4])
1933 {
1934 GLuint i;
1935 ASSERT(lambda != NULL);
1936 for (i = 0; i < n; i++) {
1937 const struct gl_texture_image **images;
1938 GLfloat newCoord[4];
1939 GLint level = linear_mipmap_level(tObj, lambda[i]);
1940 images = choose_cube_face(tObj, texcoord[i], newCoord);
1941 if (level >= tObj->_MaxLevel) {
1942 sample_2d_linear(ctx, tObj, images[tObj->_MaxLevel],
1943 newCoord, rgba[i]);
1944 }
1945 else {
1946 GLchan t0[4], t1[4];
1947 const GLfloat f = FRAC(lambda[i]);
1948 sample_2d_linear(ctx, tObj, images[level ], newCoord, t0);
1949 sample_2d_linear(ctx, tObj, images[level+1], newCoord, t1);
1950 lerp_rgba(rgba[i], f, t0, t1);
1951 }
1952 }
1953 }
1954
1955
1956 static void
1957 sample_lambda_cube( GLcontext *ctx,
1958 const struct gl_texture_object *tObj, GLuint n,
1959 const GLfloat texcoords[][4], const GLfloat lambda[],
1960 GLchan rgba[][4])
1961 {
1962 GLuint minStart, minEnd; /* texels with minification */
1963 GLuint magStart, magEnd; /* texels with magnification */
1964
1965 ASSERT(lambda != NULL);
1966 compute_min_mag_ranges(tObj, n, lambda,
1967 &minStart, &minEnd, &magStart, &magEnd);
1968
1969 if (minStart < minEnd) {
1970 /* do the minified texels */
1971 const GLuint m = minEnd - minStart;
1972 switch (tObj->MinFilter) {
1973 case GL_NEAREST:
1974 sample_nearest_cube(ctx, tObj, m, texcoords + minStart,
1975 lambda + minStart, rgba + minStart);
1976 break;
1977 case GL_LINEAR:
1978 sample_linear_cube(ctx, tObj, m, texcoords + minStart,
1979 lambda + minStart, rgba + minStart);
1980 break;
1981 case GL_NEAREST_MIPMAP_NEAREST:
1982 sample_cube_nearest_mipmap_nearest(ctx, tObj, m,
1983 texcoords + minStart,
1984 lambda + minStart, rgba + minStart);
1985 break;
1986 case GL_LINEAR_MIPMAP_NEAREST:
1987 sample_cube_linear_mipmap_nearest(ctx, tObj, m,
1988 texcoords + minStart,
1989 lambda + minStart, rgba + minStart);
1990 break;
1991 case GL_NEAREST_MIPMAP_LINEAR:
1992 sample_cube_nearest_mipmap_linear(ctx, tObj, m,
1993 texcoords + minStart,
1994 lambda + minStart, rgba + minStart);
1995 break;
1996 case GL_LINEAR_MIPMAP_LINEAR:
1997 sample_cube_linear_mipmap_linear(ctx, tObj, m,
1998 texcoords + minStart,
1999 lambda + minStart, rgba + minStart);
2000 break;
2001 default:
2002 _mesa_problem(ctx, "Bad min filter in sample_lambda_cube");
2003 }
2004 }
2005
2006 if (magStart < magEnd) {
2007 /* do the magnified texels */
2008 const GLuint m = magEnd - magStart;
2009 switch (tObj->MagFilter) {
2010 case GL_NEAREST:
2011 sample_nearest_cube(ctx, tObj, m, texcoords + magStart,
2012 lambda + magStart, rgba + magStart);
2013 break;
2014 case GL_LINEAR:
2015 sample_linear_cube(ctx, tObj, m, texcoords + magStart,
2016 lambda + magStart, rgba + magStart);
2017 break;
2018 default:
2019 _mesa_problem(ctx, "Bad mag filter in sample_lambda_cube");
2020 }
2021 }
2022 }
2023
2024
2025 /**********************************************************************/
2026 /* Texture Rectangle Sampling Functions */
2027 /**********************************************************************/
2028
2029 static void
2030 sample_nearest_rect(GLcontext *ctx,
2031 const struct gl_texture_object *tObj, GLuint n,
2032 const GLfloat texcoords[][4], const GLfloat lambda[],
2033 GLchan rgba[][4])
2034 {
2035 const struct gl_texture_image *img = tObj->Image[0][0];
2036 const GLfloat width = (GLfloat) img->Width;
2037 const GLfloat height = (GLfloat) img->Height;
2038 const GLint width_minus_1 = img->Width - 1;
2039 const GLint height_minus_1 = img->Height - 1;
2040 GLuint i;
2041
2042 (void) ctx;
2043 (void) lambda;
2044
2045 ASSERT(tObj->WrapS == GL_CLAMP ||
2046 tObj->WrapS == GL_CLAMP_TO_EDGE ||
2047 tObj->WrapS == GL_CLAMP_TO_BORDER);
2048 ASSERT(tObj->WrapT == GL_CLAMP ||
2049 tObj->WrapT == GL_CLAMP_TO_EDGE ||
2050 tObj->WrapT == GL_CLAMP_TO_BORDER);
2051 ASSERT(img->_BaseFormat != GL_COLOR_INDEX);
2052
2053 /* XXX move Wrap mode tests outside of loops for common cases */
2054 for (i = 0; i < n; i++) {
2055 GLint row, col;
2056 /* NOTE: we DO NOT use [0, 1] texture coordinates! */
2057 if (tObj->WrapS == GL_CLAMP) {
2058 col = IFLOOR( CLAMP(texcoords[i][0], 0.0F, width - 1) );
2059 }
2060 else if (tObj->WrapS == GL_CLAMP_TO_EDGE) {
2061 col = IFLOOR( CLAMP(texcoords[i][0], 0.5F, width - 0.5F) );
2062 }
2063 else {
2064 col = IFLOOR( CLAMP(texcoords[i][0], -0.5F, width + 0.5F) );
2065 }
2066 if (tObj->WrapT == GL_CLAMP) {
2067 row = IFLOOR( CLAMP(texcoords[i][1], 0.0F, height - 1) );
2068 }
2069 else if (tObj->WrapT == GL_CLAMP_TO_EDGE) {
2070 row = IFLOOR( CLAMP(texcoords[i][1], 0.5F, height - 0.5F) );
2071 }
2072 else {
2073 row = IFLOOR( CLAMP(texcoords[i][1], -0.5F, height + 0.5F) );
2074 }
2075
2076 if (col < 0 || col > width_minus_1 || row < 0 || row > height_minus_1)
2077 COPY_CHAN4(rgba[i], tObj->_BorderChan);
2078 else
2079 img->FetchTexelc(img, col, row, 0, rgba[i]);
2080 }
2081 }
2082
2083
2084 static void
2085 sample_linear_rect(GLcontext *ctx,
2086 const struct gl_texture_object *tObj, GLuint n,
2087 const GLfloat texcoords[][4],
2088 const GLfloat lambda[], GLchan rgba[][4])
2089 {
2090 const struct gl_texture_image *img = tObj->Image[0][0];
2091 const GLfloat width = (GLfloat) img->Width;
2092 const GLfloat height = (GLfloat) img->Height;
2093 const GLint width_minus_1 = img->Width - 1;
2094 const GLint height_minus_1 = img->Height - 1;
2095 GLuint i;
2096
2097 (void) ctx;
2098 (void) lambda;
2099
2100 ASSERT(tObj->WrapS == GL_CLAMP ||
2101 tObj->WrapS == GL_CLAMP_TO_EDGE ||
2102 tObj->WrapS == GL_CLAMP_TO_BORDER);
2103 ASSERT(tObj->WrapT == GL_CLAMP ||
2104 tObj->WrapT == GL_CLAMP_TO_EDGE ||
2105 tObj->WrapT == GL_CLAMP_TO_BORDER);
2106 ASSERT(img->_BaseFormat != GL_COLOR_INDEX);
2107
2108 /* XXX lots of opportunity for optimization in this loop */
2109 for (i = 0; i < n; i++) {
2110 GLfloat frow, fcol;
2111 GLint i0, j0, i1, j1;
2112 GLchan t00[4], t01[4], t10[4], t11[4];
2113 GLfloat a, b;
2114 GLbitfield useBorderColor = 0x0;
2115
2116 /* NOTE: we DO NOT use [0, 1] texture coordinates! */
2117 if (tObj->WrapS == GL_CLAMP) {
2118 /* Not exactly what the spec says, but it matches NVIDIA output */
2119 fcol = CLAMP(texcoords[i][0] - 0.5F, 0.0, width_minus_1);
2120 i0 = IFLOOR(fcol);
2121 i1 = i0 + 1;
2122 }
2123 else if (tObj->WrapS == GL_CLAMP_TO_EDGE) {
2124 fcol = CLAMP(texcoords[i][0], 0.5F, width - 0.5F);
2125 fcol -= 0.5F;
2126 i0 = IFLOOR(fcol);
2127 i1 = i0 + 1;
2128 if (i1 > width_minus_1)
2129 i1 = width_minus_1;
2130 }
2131 else {
2132 ASSERT(tObj->WrapS == GL_CLAMP_TO_BORDER);
2133 fcol = CLAMP(texcoords[i][0], -0.5F, width + 0.5F);
2134 fcol -= 0.5F;
2135 i0 = IFLOOR(fcol);
2136 i1 = i0 + 1;
2137 }
2138
2139 if (tObj->WrapT == GL_CLAMP) {
2140 /* Not exactly what the spec says, but it matches NVIDIA output */
2141 frow = CLAMP(texcoords[i][1] - 0.5F, 0.0, width_minus_1);
2142 j0 = IFLOOR(frow);
2143 j1 = j0 + 1;
2144 }
2145 else if (tObj->WrapT == GL_CLAMP_TO_EDGE) {
2146 frow = CLAMP(texcoords[i][1], 0.5F, height - 0.5F);
2147 frow -= 0.5F;
2148 j0 = IFLOOR(frow);
2149 j1 = j0 + 1;
2150 if (j1 > height_minus_1)
2151 j1 = height_minus_1;
2152 }
2153 else {
2154 ASSERT(tObj->WrapT == GL_CLAMP_TO_BORDER);
2155 frow = CLAMP(texcoords[i][1], -0.5F, height + 0.5F);
2156 frow -= 0.5F;
2157 j0 = IFLOOR(frow);
2158 j1 = j0 + 1;
2159 }
2160
2161 /* compute integer rows/columns */
2162 if (i0 < 0 || i0 > width_minus_1) useBorderColor |= I0BIT;
2163 if (i1 < 0 || i1 > width_minus_1) useBorderColor |= I1BIT;
2164 if (j0 < 0 || j0 > height_minus_1) useBorderColor |= J0BIT;
2165 if (j1 < 0 || j1 > height_minus_1) useBorderColor |= J1BIT;
2166
2167 /* get four texel samples */
2168 if (useBorderColor & (I0BIT | J0BIT))
2169 COPY_CHAN4(t00, tObj->_BorderChan);
2170 else
2171 img->FetchTexelc(img, i0, j0, 0, t00);
2172
2173 if (useBorderColor & (I1BIT | J0BIT))
2174 COPY_CHAN4(t10, tObj->_BorderChan);
2175 else
2176 img->FetchTexelc(img, i1, j0, 0, t10);
2177
2178 if (useBorderColor & (I0BIT | J1BIT))
2179 COPY_CHAN4(t01, tObj->_BorderChan);
2180 else
2181 img->FetchTexelc(img, i0, j1, 0, t01);
2182
2183 if (useBorderColor & (I1BIT | J1BIT))
2184 COPY_CHAN4(t11, tObj->_BorderChan);
2185 else
2186 img->FetchTexelc(img, i1, j1, 0, t11);
2187
2188 /* compute interpolants */
2189 a = FRAC(fcol);
2190 b = FRAC(frow);
2191
2192 lerp_rgba_2d(rgba[i], a, b, t00, t10, t01, t11);
2193 }
2194 }
2195
2196
2197 static void
2198 sample_lambda_rect( GLcontext *ctx,
2199 const struct gl_texture_object *tObj, GLuint n,
2200 const GLfloat texcoords[][4], const GLfloat lambda[],
2201 GLchan rgba[][4])
2202 {
2203 GLuint minStart, minEnd, magStart, magEnd;
2204
2205 /* We only need lambda to decide between minification and magnification.
2206 * There is no mipmapping with rectangular textures.
2207 */
2208 compute_min_mag_ranges(tObj, n, lambda,
2209 &minStart, &minEnd, &magStart, &magEnd);
2210
2211 if (minStart < minEnd) {
2212 if (tObj->MinFilter == GL_NEAREST) {
2213 sample_nearest_rect( ctx, tObj, minEnd - minStart,
2214 texcoords + minStart, NULL, rgba + minStart);
2215 }
2216 else {
2217 sample_linear_rect( ctx, tObj, minEnd - minStart,
2218 texcoords + minStart, NULL, rgba + minStart);
2219 }
2220 }
2221 if (magStart < magEnd) {
2222 if (tObj->MagFilter == GL_NEAREST) {
2223 sample_nearest_rect( ctx, tObj, magEnd - magStart,
2224 texcoords + magStart, NULL, rgba + magStart);
2225 }
2226 else {
2227 sample_linear_rect( ctx, tObj, magEnd - magStart,
2228 texcoords + magStart, NULL, rgba + magStart);
2229 }
2230 }
2231 }
2232
2233
2234
2235 /*
2236 * Sample a shadow/depth texture.
2237 */
2238 static void
2239 sample_depth_texture( GLcontext *ctx,
2240 const struct gl_texture_object *tObj, GLuint n,
2241 const GLfloat texcoords[][4], const GLfloat lambda[],
2242 GLchan texel[][4] )
2243 {
2244 const GLint baseLevel = tObj->BaseLevel;
2245 const struct gl_texture_image *img = tObj->Image[0][baseLevel];
2246 const GLint width = img->Width;
2247 const GLint height = img->Height;
2248 GLchan ambient;
2249 GLenum function;
2250 GLchan result;
2251
2252 (void) lambda;
2253
2254 ASSERT(tObj->Image[0][tObj->BaseLevel]->_BaseFormat == GL_DEPTH_COMPONENT ||
2255 tObj->Image[0][tObj->BaseLevel]->_BaseFormat == GL_DEPTH_STENCIL_EXT);
2256
2257 ASSERT(tObj->Target == GL_TEXTURE_1D ||
2258 tObj->Target == GL_TEXTURE_2D ||
2259 tObj->Target == GL_TEXTURE_RECTANGLE_NV);
2260
2261 UNCLAMPED_FLOAT_TO_CHAN(ambient, tObj->ShadowAmbient);
2262
2263 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
2264
2265 /* XXX this could be precomputed and saved in the texture object */
2266 if (tObj->CompareFlag) {
2267 /* GL_SGIX_shadow */
2268 if (tObj->CompareOperator == GL_TEXTURE_LEQUAL_R_SGIX) {
2269 function = GL_LEQUAL;
2270 }
2271 else {
2272 ASSERT(tObj->CompareOperator == GL_TEXTURE_GEQUAL_R_SGIX);
2273 function = GL_GEQUAL;
2274 }
2275 }
2276 else if (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
2277 /* GL_ARB_shadow */
2278 function = tObj->CompareFunc;
2279 }
2280 else {
2281 function = GL_NONE; /* pass depth through as grayscale */
2282 }
2283
2284 if (tObj->MagFilter == GL_NEAREST) {
2285 GLuint i;
2286 for (i = 0; i < n; i++) {
2287 GLfloat depthSample;
2288 GLint col, row;
2289 /* XXX fix for texture rectangle! */
2290 COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoords[i][0], width, col);
2291 COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapT, texcoords[i][1], height, row);
2292 if (col >= 0 && row >= 0 && col < width && row < height) {
2293 img->FetchTexelf(img, col, row, 0, &depthSample);
2294 }
2295 else {
2296 depthSample = tObj->BorderColor[0];
2297 }
2298
2299 switch (function) {
2300 case GL_LEQUAL:
2301 result = (texcoords[i][2] <= depthSample) ? CHAN_MAX : ambient;
2302 break;
2303 case GL_GEQUAL:
2304 result = (texcoords[i][2] >= depthSample) ? CHAN_MAX : ambient;
2305 break;
2306 case GL_LESS:
2307 result = (texcoords[i][2] < depthSample) ? CHAN_MAX : ambient;
2308 break;
2309 case GL_GREATER:
2310 result = (texcoords[i][2] > depthSample) ? CHAN_MAX : ambient;
2311 break;
2312 case GL_EQUAL:
2313 result = (texcoords[i][2] == depthSample) ? CHAN_MAX : ambient;
2314 break;
2315 case GL_NOTEQUAL:
2316 result = (texcoords[i][2] != depthSample) ? CHAN_MAX : ambient;
2317 break;
2318 case GL_ALWAYS:
2319 result = CHAN_MAX;
2320 break;
2321 case GL_NEVER:
2322 result = ambient;
2323 break;
2324 case GL_NONE:
2325 CLAMPED_FLOAT_TO_CHAN(result, depthSample);
2326 break;
2327 default:
2328 _mesa_problem(ctx, "Bad compare func in sample_depth_texture");
2329 return;
2330 }
2331
2332 switch (tObj->DepthMode) {
2333 case GL_LUMINANCE:
2334 texel[i][RCOMP] = result;
2335 texel[i][GCOMP] = result;
2336 texel[i][BCOMP] = result;
2337 texel[i][ACOMP] = CHAN_MAX;
2338 break;
2339 case GL_INTENSITY:
2340 texel[i][RCOMP] = result;
2341 texel[i][GCOMP] = result;
2342 texel[i][BCOMP] = result;
2343 texel[i][ACOMP] = result;
2344 break;
2345 case GL_ALPHA:
2346 texel[i][RCOMP] = 0;
2347 texel[i][GCOMP] = 0;
2348 texel[i][BCOMP] = 0;
2349 texel[i][ACOMP] = result;
2350 break;
2351 default:
2352 _mesa_problem(ctx, "Bad depth texture mode");
2353 }
2354 }
2355 }
2356 else {
2357 GLuint i;
2358 ASSERT(tObj->MagFilter == GL_LINEAR);
2359 for (i = 0; i < n; i++) {
2360 GLfloat depth00, depth01, depth10, depth11;
2361 GLint i0, i1, j0, j1;
2362 GLfloat u, v;
2363 GLuint useBorderTexel;
2364
2365 /* XXX fix for texture rectangle! */
2366 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoords[i][0], u, width, i0, i1);
2367 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapT, texcoords[i][1], v, height,j0, j1);
2368
2369 useBorderTexel = 0;
2370 if (img->Border) {
2371 i0 += img->Border;
2372 i1 += img->Border;
2373 j0 += img->Border;
2374 j1 += img->Border;
2375 }
2376 else {
2377 if (i0 < 0 || i0 >= (GLint) width) useBorderTexel |= I0BIT;
2378 if (i1 < 0 || i1 >= (GLint) width) useBorderTexel |= I1BIT;
2379 if (j0 < 0 || j0 >= (GLint) height) useBorderTexel |= J0BIT;
2380 if (j1 < 0 || j1 >= (GLint) height) useBorderTexel |= J1BIT;
2381 }
2382
2383 /* get four depth samples from the texture */
2384 if (useBorderTexel & (I0BIT | J0BIT)) {
2385 depth00 = tObj->BorderColor[0];
2386 }
2387 else {
2388 img->FetchTexelf(img, i0, j0, 0, &depth00);
2389 }
2390 if (useBorderTexel & (I1BIT | J0BIT)) {
2391 depth10 = tObj->BorderColor[0];
2392 }
2393 else {
2394 img->FetchTexelf(img, i1, j0, 0, &depth10);
2395 }
2396 if (useBorderTexel & (I0BIT | J1BIT)) {
2397 depth01 = tObj->BorderColor[0];
2398 }
2399 else {
2400 img->FetchTexelf(img, i0, j1, 0, &depth01);
2401 }
2402 if (useBorderTexel & (I1BIT | J1BIT)) {
2403 depth11 = tObj->BorderColor[0];
2404 }
2405 else {
2406 img->FetchTexelf(img, i1, j1, 0, &depth11);
2407 }
2408
2409 if (0) {
2410 /* compute a single weighted depth sample and do one comparison */
2411 const GLfloat a = FRAC(u + 1.0F);
2412 const GLfloat b = FRAC(v + 1.0F);
2413 const GLfloat depthSample
2414 = lerp_2d(a, b, depth00, depth10, depth01, depth11);
2415 if ((depthSample <= texcoords[i][2] && function == GL_LEQUAL) ||
2416 (depthSample >= texcoords[i][2] && function == GL_GEQUAL)) {
2417 result = ambient;
2418 }
2419 else {
2420 result = CHAN_MAX;
2421 }
2422 }
2423 else {
2424 /* Do four depth/R comparisons and compute a weighted result.
2425 * If this touches on somebody's I.P., I'll remove this code
2426 * upon request.
2427 */
2428 const GLfloat d = (CHAN_MAXF - (GLfloat) ambient) * 0.25F;
2429 GLfloat luminance = CHAN_MAXF;
2430
2431 switch (function) {
2432 case GL_LEQUAL:
2433 if (depth00 <= texcoords[i][2]) luminance -= d;
2434 if (depth01 <= texcoords[i][2]) luminance -= d;
2435 if (depth10 <= texcoords[i][2]) luminance -= d;
2436 if (depth11 <= texcoords[i][2]) luminance -= d;
2437 result = (GLchan) luminance;
2438 break;
2439 case GL_GEQUAL:
2440 if (depth00 >= texcoords[i][2]) luminance -= d;
2441 if (depth01 >= texcoords[i][2]) luminance -= d;
2442 if (depth10 >= texcoords[i][2]) luminance -= d;
2443 if (depth11 >= texcoords[i][2]) luminance -= d;
2444 result = (GLchan) luminance;
2445 break;
2446 case GL_LESS:
2447 if (depth00 < texcoords[i][2]) luminance -= d;
2448 if (depth01 < texcoords[i][2]) luminance -= d;
2449 if (depth10 < texcoords[i][2]) luminance -= d;
2450 if (depth11 < texcoords[i][2]) luminance -= d;
2451 result = (GLchan) luminance;
2452 break;
2453 case GL_GREATER:
2454 if (depth00 > texcoords[i][2]) luminance -= d;
2455 if (depth01 > texcoords[i][2]) luminance -= d;
2456 if (depth10 > texcoords[i][2]) luminance -= d;
2457 if (depth11 > texcoords[i][2]) luminance -= d;
2458 result = (GLchan) luminance;
2459 break;
2460 case GL_EQUAL:
2461 if (depth00 == texcoords[i][2]) luminance -= d;
2462 if (depth01 == texcoords[i][2]) luminance -= d;
2463 if (depth10 == texcoords[i][2]) luminance -= d;
2464 if (depth11 == texcoords[i][2]) luminance -= d;
2465 result = (GLchan) luminance;
2466 break;
2467 case GL_NOTEQUAL:
2468 if (depth00 != texcoords[i][2]) luminance -= d;
2469 if (depth01 != texcoords[i][2]) luminance -= d;
2470 if (depth10 != texcoords[i][2]) luminance -= d;
2471 if (depth11 != texcoords[i][2]) luminance -= d;
2472 result = (GLchan) luminance;
2473 break;
2474 case GL_ALWAYS:
2475 result = 0;
2476 break;
2477 case GL_NEVER:
2478 result = CHAN_MAX;
2479 break;
2480 case GL_NONE:
2481 /* ordinary bilinear filtering */
2482 {
2483 const GLfloat a = FRAC(u + 1.0F);
2484 const GLfloat b = FRAC(v + 1.0F);
2485 const GLfloat depthSample
2486 = lerp_2d(a, b, depth00, depth10, depth01, depth11);
2487 CLAMPED_FLOAT_TO_CHAN(result, depthSample);
2488 }
2489 break;
2490 default:
2491 _mesa_problem(ctx, "Bad compare func in sample_depth_texture");
2492 return;
2493 }
2494 }
2495
2496 switch (tObj->DepthMode) {
2497 case GL_LUMINANCE:
2498 texel[i][RCOMP] = result;
2499 texel[i][GCOMP] = result;
2500 texel[i][BCOMP] = result;
2501 texel[i][ACOMP] = CHAN_MAX;
2502 break;
2503 case GL_INTENSITY:
2504 texel[i][RCOMP] = result;
2505 texel[i][GCOMP] = result;
2506 texel[i][BCOMP] = result;
2507 texel[i][ACOMP] = result;
2508 break;
2509 case GL_ALPHA:
2510 texel[i][RCOMP] = 0;
2511 texel[i][GCOMP] = 0;
2512 texel[i][BCOMP] = 0;
2513 texel[i][ACOMP] = result;
2514 break;
2515 default:
2516 _mesa_problem(ctx, "Bad depth texture mode");
2517 }
2518 } /* for */
2519 } /* if filter */
2520 }
2521
2522
2523 #if 0
2524 /*
2525 * Experimental depth texture sampling function.
2526 */
2527 static void
2528 sample_depth_texture2(const GLcontext *ctx,
2529 const struct gl_texture_unit *texUnit,
2530 GLuint n, const GLfloat texcoords[][4],
2531 GLchan texel[][4])
2532 {
2533 const struct gl_texture_object *texObj = texUnit->_Current;
2534 const GLint baseLevel = texObj->BaseLevel;
2535 const struct gl_texture_image *texImage = texObj->Image[0][baseLevel];
2536 const GLuint width = texImage->Width;
2537 const GLuint height = texImage->Height;
2538 GLchan ambient;
2539 GLboolean lequal, gequal;
2540
2541 if (texObj->Target != GL_TEXTURE_2D) {
2542 _mesa_problem(ctx, "only 2-D depth textures supported at this time");
2543 return;
2544 }
2545
2546 if (texObj->MinFilter != texObj->MagFilter) {
2547 _mesa_problem(ctx, "mipmapped depth textures not supported at this time");
2548 return;
2549 }
2550
2551 /* XXX the GL_SGIX_shadow extension spec doesn't say what to do if
2552 * GL_TEXTURE_COMPARE_SGIX == GL_TRUE but the current texture object
2553 * isn't a depth texture.
2554 */
2555 if (texImage->_BaseFormat != GL_DEPTH_COMPONENT) {
2556 _mesa_problem(ctx,"GL_TEXTURE_COMPARE_SGIX enabled with non-depth texture");
2557 return;
2558 }
2559
2560 UNCLAMPED_FLOAT_TO_CHAN(ambient, tObj->ShadowAmbient);
2561
2562 if (texObj->CompareOperator == GL_TEXTURE_LEQUAL_R_SGIX) {
2563 lequal = GL_TRUE;
2564 gequal = GL_FALSE;
2565 }
2566 else {
2567 lequal = GL_FALSE;
2568 gequal = GL_TRUE;
2569 }
2570
2571 {
2572 GLuint i;
2573 for (i = 0; i < n; i++) {
2574 const GLint K = 3;
2575 GLint col, row, ii, jj, imin, imax, jmin, jmax, samples, count;
2576 GLfloat w;
2577 GLchan lum;
2578 COMPUTE_NEAREST_TEXEL_LOCATION(texObj->WrapS, texcoords[i][0],
2579 width, col);
2580 COMPUTE_NEAREST_TEXEL_LOCATION(texObj->WrapT, texcoords[i][1],
2581 height, row);
2582
2583 imin = col - K;
2584 imax = col + K;
2585 jmin = row - K;
2586 jmax = row + K;
2587
2588 if (imin < 0) imin = 0;
2589 if (imax >= width) imax = width - 1;
2590 if (jmin < 0) jmin = 0;
2591 if (jmax >= height) jmax = height - 1;
2592
2593 samples = (imax - imin + 1) * (jmax - jmin + 1);
2594 count = 0;
2595 for (jj = jmin; jj <= jmax; jj++) {
2596 for (ii = imin; ii <= imax; ii++) {
2597 GLfloat depthSample;
2598 texImage->FetchTexelf(texImage, ii, jj, 0, &depthSample);
2599 if ((depthSample <= r[i] && lequal) ||
2600 (depthSample >= r[i] && gequal)) {
2601 count++;
2602 }
2603 }
2604 }
2605
2606 w = (GLfloat) count / (GLfloat) samples;
2607 w = CHAN_MAXF - w * (CHAN_MAXF - (GLfloat) ambient);
2608 lum = (GLint) w;
2609
2610 texel[i][RCOMP] = lum;
2611 texel[i][GCOMP] = lum;
2612 texel[i][BCOMP] = lum;
2613 texel[i][ACOMP] = CHAN_MAX;
2614 }
2615 }
2616 }
2617 #endif
2618
2619
2620 /**
2621 * We use this function when a texture object is in an "incomplete" state.
2622 * When a fragment program attempts to sample an incomplete texture we
2623 * return black (see issue 23 in GL_ARB_fragment_program spec).
2624 * Note: fragment programs don't observe the texture enable/disable flags.
2625 */
2626 static void
2627 null_sample_func( GLcontext *ctx,
2628 const struct gl_texture_object *tObj, GLuint n,
2629 const GLfloat texcoords[][4], const GLfloat lambda[],
2630 GLchan rgba[][4])
2631 {
2632 GLuint i;
2633 (void) ctx;
2634 (void) tObj;
2635 (void) texcoords;
2636 (void) lambda;
2637 for (i = 0; i < n; i++) {
2638 rgba[i][RCOMP] = 0;
2639 rgba[i][GCOMP] = 0;
2640 rgba[i][BCOMP] = 0;
2641 rgba[i][ACOMP] = CHAN_MAX;
2642 }
2643 }
2644
2645
2646 /**
2647 * Choose the texture sampling function for the given texture object.
2648 */
2649 texture_sample_func
2650 _swrast_choose_texture_sample_func( GLcontext *ctx,
2651 const struct gl_texture_object *t )
2652 {
2653 if (!t || !t->Complete) {
2654 return &null_sample_func;
2655 }
2656 else {
2657 const GLboolean needLambda = (GLboolean) (t->MinFilter != t->MagFilter);
2658 const GLenum format = t->Image[0][t->BaseLevel]->_BaseFormat;
2659
2660 switch (t->Target) {
2661 case GL_TEXTURE_1D:
2662 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
2663 return &sample_depth_texture;
2664 }
2665 else if (needLambda) {
2666 return &sample_lambda_1d;
2667 }
2668 else if (t->MinFilter == GL_LINEAR) {
2669 return &sample_linear_1d;
2670 }
2671 else {
2672 ASSERT(t->MinFilter == GL_NEAREST);
2673 return &sample_nearest_1d;
2674 }
2675 case GL_TEXTURE_2D:
2676 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
2677 return &sample_depth_texture;
2678 }
2679 else if (needLambda) {
2680 return &sample_lambda_2d;
2681 }
2682 else if (t->MinFilter == GL_LINEAR) {
2683 return &sample_linear_2d;
2684 }
2685 else {
2686 /* check for a few optimized cases */
2687 const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
2688 ASSERT(t->MinFilter == GL_NEAREST);
2689 if (t->WrapS == GL_REPEAT &&
2690 t->WrapT == GL_REPEAT &&
2691 img->_IsPowerOfTwo &&
2692 img->Border == 0 &&
2693 img->TexFormat->MesaFormat == MESA_FORMAT_RGB) {
2694 return &opt_sample_rgb_2d;
2695 }
2696 else if (t->WrapS == GL_REPEAT &&
2697 t->WrapT == GL_REPEAT &&
2698 img->_IsPowerOfTwo &&
2699 img->Border == 0 &&
2700 img->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
2701 return &opt_sample_rgba_2d;
2702 }
2703 else {
2704 return &sample_nearest_2d;
2705 }
2706 }
2707 case GL_TEXTURE_3D:
2708 if (needLambda) {
2709 return &sample_lambda_3d;
2710 }
2711 else if (t->MinFilter == GL_LINEAR) {
2712 return &sample_linear_3d;
2713 }
2714 else {
2715 ASSERT(t->MinFilter == GL_NEAREST);
2716 return &sample_nearest_3d;
2717 }
2718 case GL_TEXTURE_CUBE_MAP:
2719 if (needLambda) {
2720 return &sample_lambda_cube;
2721 }
2722 else if (t->MinFilter == GL_LINEAR) {
2723 return &sample_linear_cube;
2724 }
2725 else {
2726 ASSERT(t->MinFilter == GL_NEAREST);
2727 return &sample_nearest_cube;
2728 }
2729 case GL_TEXTURE_RECTANGLE_NV:
2730 if (needLambda) {
2731 return &sample_lambda_rect;
2732 }
2733 else if (t->MinFilter == GL_LINEAR) {
2734 return &sample_linear_rect;
2735 }
2736 else {
2737 ASSERT(t->MinFilter == GL_NEAREST);
2738 return &sample_nearest_rect;
2739 }
2740 default:
2741 _mesa_problem(ctx,
2742 "invalid target in _swrast_choose_texture_sample_func");
2743 return &null_sample_func;
2744 }
2745 }
2746 }