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