2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include "texformat.h"
32 #include "s_context.h"
33 #include "s_texfilter.h"
37 * Constants for integer linear interpolation.
39 #define ILERP_SCALE 65536.0F
40 #define ILERP_SHIFT 16
44 * Linear interpolation macros
46 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
47 #define ILERP(IT, A, B) ( (A) + (((IT) * ((B) - (A))) >> ILERP_SHIFT) )
51 * Do 2D/biliner interpolation of float values.
52 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
53 * a and b are the horizontal and vertical interpolants.
54 * It's important that this function is inlined when compiled with
55 * optimization! If we find that's not true on some systems, convert
59 lerp_2d(GLfloat a
, GLfloat b
,
60 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
62 const GLfloat temp0
= LERP(a
, v00
, v10
);
63 const GLfloat temp1
= LERP(a
, v01
, v11
);
64 return LERP(b
, temp0
, temp1
);
69 * Do 2D/biliner interpolation of integer values.
73 ilerp_2d(GLint ia
, GLint ib
,
74 GLint v00
, GLint v10
, GLint v01
, GLint v11
)
76 /* fixed point interpolants in [0, ILERP_SCALE] */
77 const GLint temp0
= ILERP(ia
, v00
, v10
);
78 const GLint temp1
= ILERP(ia
, v01
, v11
);
79 return ILERP(ib
, temp0
, temp1
);
84 * Do 3D/trilinear interpolation of float values.
88 lerp_3d(GLfloat a
, GLfloat b
, GLfloat c
,
89 GLfloat v000
, GLfloat v100
, GLfloat v010
, GLfloat v110
,
90 GLfloat v001
, GLfloat v101
, GLfloat v011
, GLfloat v111
)
92 const GLfloat temp00
= LERP(a
, v000
, v100
);
93 const GLfloat temp10
= LERP(a
, v010
, v110
);
94 const GLfloat temp01
= LERP(a
, v001
, v101
);
95 const GLfloat temp11
= LERP(a
, v011
, v111
);
96 const GLfloat temp0
= LERP(b
, temp00
, temp10
);
97 const GLfloat temp1
= LERP(b
, temp01
, temp11
);
98 return LERP(c
, temp0
, temp1
);
103 * Do 3D/trilinear interpolation of integer values.
107 ilerp_3d(GLint ia
, GLint ib
, GLint ic
,
108 GLint v000
, GLint v100
, GLint v010
, GLint v110
,
109 GLint v001
, GLint v101
, GLint v011
, GLint v111
)
111 /* fixed point interpolants in [0, ILERP_SCALE] */
112 const GLint temp00
= ILERP(ia
, v000
, v100
);
113 const GLint temp10
= ILERP(ia
, v010
, v110
);
114 const GLint temp01
= ILERP(ia
, v001
, v101
);
115 const GLint temp11
= ILERP(ia
, v011
, v111
);
116 const GLint temp0
= ILERP(ib
, temp00
, temp10
);
117 const GLint temp1
= ILERP(ib
, temp01
, temp11
);
118 return ILERP(ic
, temp0
, temp1
);
123 * Do linear interpolation of colors.
126 lerp_rgba(GLchan result
[4], GLfloat t
, const GLchan a
[4], const GLchan b
[4])
128 #if CHAN_TYPE == GL_FLOAT
129 result
[0] = LERP(t
, a
[0], b
[0]);
130 result
[1] = LERP(t
, a
[1], b
[1]);
131 result
[2] = LERP(t
, a
[2], b
[2]);
132 result
[3] = LERP(t
, a
[3], b
[3]);
133 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
134 result
[0] = (GLchan
) (LERP(t
, a
[0], b
[0]) + 0.5);
135 result
[1] = (GLchan
) (LERP(t
, a
[1], b
[1]) + 0.5);
136 result
[2] = (GLchan
) (LERP(t
, a
[2], b
[2]) + 0.5);
137 result
[3] = (GLchan
) (LERP(t
, a
[3], b
[3]) + 0.5);
139 /* fixed point interpolants in [0, ILERP_SCALE] */
140 const GLint it
= IROUND_POS(t
* ILERP_SCALE
);
141 ASSERT(CHAN_TYPE
== GL_UNSIGNED_BYTE
);
142 result
[0] = ILERP(it
, a
[0], b
[0]);
143 result
[1] = ILERP(it
, a
[1], b
[1]);
144 result
[2] = ILERP(it
, a
[2], b
[2]);
145 result
[3] = ILERP(it
, a
[3], b
[3]);
151 * Do bilinear interpolation of colors.
154 lerp_rgba_2d(GLchan result
[4], GLfloat a
, GLfloat b
,
155 const GLchan t00
[4], const GLchan t10
[4],
156 const GLchan t01
[4], const GLchan t11
[4])
158 #if CHAN_TYPE == GL_FLOAT
159 result
[0] = lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]);
160 result
[1] = lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]);
161 result
[2] = lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]);
162 result
[3] = lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]);
163 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
164 result
[0] = (GLchan
) (lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]) + 0.5);
165 result
[1] = (GLchan
) (lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]) + 0.5);
166 result
[2] = (GLchan
) (lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]) + 0.5);
167 result
[3] = (GLchan
) (lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]) + 0.5);
169 const GLint ia
= IROUND_POS(a
* ILERP_SCALE
);
170 const GLint ib
= IROUND_POS(b
* ILERP_SCALE
);
171 ASSERT(CHAN_TYPE
== GL_UNSIGNED_BYTE
);
172 result
[0] = ilerp_2d(ia
, ib
, t00
[0], t10
[0], t01
[0], t11
[0]);
173 result
[1] = ilerp_2d(ia
, ib
, t00
[1], t10
[1], t01
[1], t11
[1]);
174 result
[2] = ilerp_2d(ia
, ib
, t00
[2], t10
[2], t01
[2], t11
[2]);
175 result
[3] = ilerp_2d(ia
, ib
, t00
[3], t10
[3], t01
[3], t11
[3]);
181 * Do trilinear interpolation of colors.
184 lerp_rgba_3d(GLchan result
[4], GLfloat a
, GLfloat b
, GLfloat c
,
185 const GLchan t000
[4], const GLchan t100
[4],
186 const GLchan t010
[4], const GLchan t110
[4],
187 const GLchan t001
[4], const GLchan t101
[4],
188 const GLchan t011
[4], const GLchan t111
[4])
191 /* compiler should unroll these short loops */
192 #if CHAN_TYPE == GL_FLOAT
193 for (k
= 0; k
< 4; k
++) {
194 result
[k
] = lerp_3d(a
, b
, c
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
195 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
197 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
198 for (k
= 0; k
< 4; k
++) {
199 result
[k
] = (GLchan
)(lerp_3d(a
, b
, c
,
200 t000
[k
], t100
[k
], t010
[k
], t110
[k
],
201 t001
[k
], t101
[k
], t011
[k
], t111
[k
]) + 0.5F
);
204 GLint ia
= IROUND_POS(a
* ILERP_SCALE
);
205 GLint ib
= IROUND_POS(b
* ILERP_SCALE
);
206 GLint ic
= IROUND_POS(c
* ILERP_SCALE
);
207 for (k
= 0; k
< 4; k
++) {
208 result
[k
] = ilerp_3d(ia
, ib
, ic
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
209 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
216 * Compute the remainder of a divided by b, but be careful with
217 * negative values so that GL_REPEAT mode works right.
220 repeat_remainder(GLint a
, GLint b
)
225 return (a
+ 1) % b
+ b
- 1;
230 * Used to compute texel locations for linear sampling.
232 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
233 * S = texcoord in [0,1]
234 * SIZE = width (or height or depth) of texture
236 * U = texcoord in [0, width]
237 * I0, I1 = two nearest texel indexes
239 #define COMPUTE_LINEAR_TEXEL_LOCATIONS(wrapMode, S, U, SIZE, I0, I1) \
241 switch (wrapMode) { \
243 U = S * SIZE - 0.5F; \
244 if (img->_IsPowerOfTwo) { \
245 I0 = IFLOOR(U) & (SIZE - 1); \
246 I1 = (I0 + 1) & (SIZE - 1); \
249 I0 = repeat_remainder(IFLOOR(U), SIZE); \
250 I1 = repeat_remainder(I0 + 1, SIZE); \
253 case GL_CLAMP_TO_EDGE: \
256 else if (S >= 1.0F) \
257 U = (GLfloat) SIZE; \
265 if (I1 >= (GLint) SIZE) \
268 case GL_CLAMP_TO_BORDER: \
270 const GLfloat min = -1.0F / (2.0F * SIZE); \
271 const GLfloat max = 1.0F - min; \
283 case GL_MIRRORED_REPEAT: \
285 const GLint flr = IFLOOR(S); \
287 U = 1.0F - (S - (GLfloat) flr); /* flr is odd */ \
289 U = S - (GLfloat) flr; /* flr is even */ \
290 U = (U * SIZE) - 0.5F; \
295 if (I1 >= (GLint) SIZE) \
299 case GL_MIRROR_CLAMP_EXT: \
302 U = (GLfloat) SIZE; \
309 case GL_MIRROR_CLAMP_TO_EDGE_EXT: \
312 U = (GLfloat) SIZE; \
320 if (I1 >= (GLint) SIZE) \
323 case GL_MIRROR_CLAMP_TO_BORDER_EXT: \
325 const GLfloat min = -1.0F / (2.0F * SIZE); \
326 const GLfloat max = 1.0F - min; \
342 else if (S >= 1.0F) \
343 U = (GLfloat) SIZE; \
351 _mesa_problem(ctx, "Bad wrap mode"); \
357 * Used to compute texel location for nearest sampling.
359 #define COMPUTE_NEAREST_TEXEL_LOCATION(wrapMode, S, SIZE, I) \
361 switch (wrapMode) { \
363 /* s limited to [0,1) */ \
364 /* i limited to [0,size-1] */ \
365 I = IFLOOR(S * SIZE); \
366 if (img->_IsPowerOfTwo) \
369 I = repeat_remainder(I, SIZE); \
371 case GL_CLAMP_TO_EDGE: \
373 /* s limited to [min,max] */ \
374 /* i limited to [0, size-1] */ \
375 const GLfloat min = 1.0F / (2.0F * SIZE); \
376 const GLfloat max = 1.0F - min; \
382 I = IFLOOR(S * SIZE); \
385 case GL_CLAMP_TO_BORDER: \
387 /* s limited to [min,max] */ \
388 /* i limited to [-1, size] */ \
389 const GLfloat min = -1.0F / (2.0F * SIZE); \
390 const GLfloat max = 1.0F - min; \
396 I = IFLOOR(S * SIZE); \
399 case GL_MIRRORED_REPEAT: \
401 const GLfloat min = 1.0F / (2.0F * SIZE); \
402 const GLfloat max = 1.0F - min; \
403 const GLint flr = IFLOOR(S); \
406 u = 1.0F - (S - (GLfloat) flr); /* flr is odd */ \
408 u = S - (GLfloat) flr; /* flr is even */ \
414 I = IFLOOR(u * SIZE); \
417 case GL_MIRROR_CLAMP_EXT: \
419 /* s limited to [0,1] */ \
420 /* i limited to [0,size-1] */ \
421 const GLfloat u = FABSF(S); \
424 else if (u >= 1.0F) \
427 I = IFLOOR(u * SIZE); \
430 case GL_MIRROR_CLAMP_TO_EDGE_EXT: \
432 /* s limited to [min,max] */ \
433 /* i limited to [0, size-1] */ \
434 const GLfloat min = 1.0F / (2.0F * SIZE); \
435 const GLfloat max = 1.0F - min; \
436 const GLfloat u = FABSF(S); \
442 I = IFLOOR(u * SIZE); \
445 case GL_MIRROR_CLAMP_TO_BORDER_EXT: \
447 /* s limited to [min,max] */ \
448 /* i limited to [0, size-1] */ \
449 const GLfloat min = -1.0F / (2.0F * SIZE); \
450 const GLfloat max = 1.0F - min; \
451 const GLfloat u = FABSF(S); \
457 I = IFLOOR(u * SIZE); \
461 /* s limited to [0,1] */ \
462 /* i limited to [0,size-1] */ \
465 else if (S >= 1.0F) \
468 I = IFLOOR(S * SIZE); \
471 _mesa_problem(ctx, "Bad wrap mode"); \
476 /* Power of two image sizes only */
477 #define COMPUTE_LINEAR_REPEAT_TEXEL_LOCATION(S, U, SIZE, I0, I1) \
479 U = S * SIZE - 0.5F; \
480 I0 = IFLOOR(U) & (SIZE - 1); \
481 I1 = (I0 + 1) & (SIZE - 1); \
486 * For linear interpolation between mipmap levels N and N+1, this function
490 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
493 return tObj
->BaseLevel
;
494 else if (lambda
> tObj
->_MaxLambda
)
495 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
497 return (GLint
) (tObj
->BaseLevel
+ lambda
);
502 * Compute the nearest mipmap level to take texels from.
505 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
511 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
512 l
= tObj
->_MaxLambda
+ 0.4999F
;
515 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
516 if (level
> tObj
->_MaxLevel
)
517 level
= tObj
->_MaxLevel
;
524 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
525 * see 1-pixel bands of improperly weighted linear-filtered textures.
526 * The tests/texwrap.c demo is a good test.
527 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
528 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
530 #define FRAC(f) ((f) - IFLOOR(f))
535 * Bitflags for texture border color sampling.
547 * The lambda[] array values are always monotonic. Either the whole span
548 * will be minified, magnified, or split between the two. This function
549 * determines the subranges in [0, n-1] that are to be minified or magnified.
552 compute_min_mag_ranges(const struct gl_texture_object
*tObj
,
553 GLuint n
, const GLfloat lambda
[],
554 GLuint
*minStart
, GLuint
*minEnd
,
555 GLuint
*magStart
, GLuint
*magEnd
)
557 GLfloat minMagThresh
;
559 /* we shouldn't be here if minfilter == magfilter */
560 ASSERT(tObj
->MinFilter
!= tObj
->MagFilter
);
562 /* This bit comes from the OpenGL spec: */
563 if (tObj
->MagFilter
== GL_LINEAR
564 && (tObj
->MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
565 tObj
->MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
573 /* DEBUG CODE: Verify that lambda[] is monotonic.
574 * We can't really use this because the inaccuracy in the LOG2 function
575 * causes this test to fail, yet the resulting texturing is correct.
579 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
580 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
581 for (i
= 0; i
< n
- 1; i
++) {
582 ASSERT((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
585 else { /* increasing */
586 for (i
= 0; i
< n
- 1; i
++) {
587 ASSERT((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
593 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
594 /* magnification for whole span */
597 *minStart
= *minEnd
= 0;
599 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
600 /* minification for whole span */
603 *magStart
= *magEnd
= 0;
606 /* a mix of minification and magnification */
608 if (lambda
[0] > minMagThresh
) {
609 /* start with minification */
610 for (i
= 1; i
< n
; i
++) {
611 if (lambda
[i
] <= minMagThresh
)
620 /* start with magnification */
621 for (i
= 1; i
< n
; i
++) {
622 if (lambda
[i
] > minMagThresh
)
633 /* Verify the min/mag Start/End values
634 * We don't use this either (see above)
638 for (i
= 0; i
< n
; i
++) {
639 if (lambda
[i
] > minMagThresh
) {
641 ASSERT(i
>= *minStart
);
646 ASSERT(i
>= *magStart
);
655 /**********************************************************************/
656 /* 1-D Texture Sampling Functions */
657 /**********************************************************************/
660 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
663 sample_1d_nearest(GLcontext
*ctx
,
664 const struct gl_texture_object
*tObj
,
665 const struct gl_texture_image
*img
,
666 const GLfloat texcoord
[4], GLchan rgba
[4])
668 const GLint width
= img
->Width2
; /* without border, power of two */
670 COMPUTE_NEAREST_TEXEL_LOCATION(tObj
->WrapS
, texcoord
[0], width
, i
);
671 /* skip over the border, if any */
673 if (i
< 0 || i
>= (GLint
) img
->Width
) {
674 /* Need this test for GL_CLAMP_TO_BORDER mode */
675 COPY_CHAN4(rgba
, tObj
->_BorderChan
);
678 img
->FetchTexelc(img
, i
, 0, 0, rgba
);
684 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
687 sample_1d_linear(GLcontext
*ctx
,
688 const struct gl_texture_object
*tObj
,
689 const struct gl_texture_image
*img
,
690 const GLfloat texcoord
[4], GLchan rgba
[4])
692 const GLint width
= img
->Width2
;
695 GLbitfield useBorderColor
= 0x0;
697 GLchan t0
[4], t1
[4]; /* texels */
699 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj
->WrapS
, texcoord
[0], u
, width
, i0
, i1
);
706 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
707 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
710 /* fetch texel colors */
711 if (useBorderColor
& I0BIT
) {
712 COPY_CHAN4(t0
, tObj
->_BorderChan
);
715 img
->FetchTexelc(img
, i0
, 0, 0, t0
);
717 if (useBorderColor
& I1BIT
) {
718 COPY_CHAN4(t1
, tObj
->_BorderChan
);
721 img
->FetchTexelc(img
, i1
, 0, 0, t1
);
725 lerp_rgba(rgba
, a
, t0
, t1
);
730 sample_1d_nearest_mipmap_nearest(GLcontext
*ctx
,
731 const struct gl_texture_object
*tObj
,
732 GLuint n
, const GLfloat texcoord
[][4],
733 const GLfloat lambda
[], GLchan rgba
[][4])
736 ASSERT(lambda
!= NULL
);
737 for (i
= 0; i
< n
; i
++) {
738 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
739 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
745 sample_1d_linear_mipmap_nearest(GLcontext
*ctx
,
746 const struct gl_texture_object
*tObj
,
747 GLuint n
, const GLfloat texcoord
[][4],
748 const GLfloat lambda
[], GLchan rgba
[][4])
751 ASSERT(lambda
!= NULL
);
752 for (i
= 0; i
< n
; i
++) {
753 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
754 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
760 sample_1d_nearest_mipmap_linear(GLcontext
*ctx
,
761 const struct gl_texture_object
*tObj
,
762 GLuint n
, const GLfloat texcoord
[][4],
763 const GLfloat lambda
[], GLchan rgba
[][4])
766 ASSERT(lambda
!= NULL
);
767 for (i
= 0; i
< n
; i
++) {
768 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
769 if (level
>= tObj
->_MaxLevel
) {
770 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
771 texcoord
[i
], rgba
[i
]);
775 const GLfloat f
= FRAC(lambda
[i
]);
776 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
777 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
778 lerp_rgba(rgba
[i
], f
, t0
, t1
);
786 sample_1d_linear_mipmap_linear(GLcontext
*ctx
,
787 const struct gl_texture_object
*tObj
,
788 GLuint n
, const GLfloat texcoord
[][4],
789 const GLfloat lambda
[], GLchan rgba
[][4])
792 ASSERT(lambda
!= NULL
);
793 for (i
= 0; i
< n
; i
++) {
794 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
795 if (level
>= tObj
->_MaxLevel
) {
796 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
797 texcoord
[i
], rgba
[i
]);
801 const GLfloat f
= FRAC(lambda
[i
]);
802 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
803 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
804 lerp_rgba(rgba
[i
], f
, t0
, t1
);
812 sample_nearest_1d( GLcontext
*ctx
,
813 const struct gl_texture_object
*tObj
, GLuint n
,
814 const GLfloat texcoords
[][4], const GLfloat lambda
[],
818 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
821 sample_1d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
828 sample_linear_1d( GLcontext
*ctx
,
829 const struct gl_texture_object
*tObj
, GLuint n
,
830 const GLfloat texcoords
[][4], const GLfloat lambda
[],
834 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
837 sample_1d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
843 * Given an (s) texture coordinate and lambda (level of detail) value,
844 * return a texture sample.
848 sample_lambda_1d( GLcontext
*ctx
,
849 const struct gl_texture_object
*tObj
, GLuint n
,
850 const GLfloat texcoords
[][4],
851 const GLfloat lambda
[], GLchan rgba
[][4] )
853 GLuint minStart
, minEnd
; /* texels with minification */
854 GLuint magStart
, magEnd
; /* texels with magnification */
857 ASSERT(lambda
!= NULL
);
858 compute_min_mag_ranges(tObj
, n
, lambda
,
859 &minStart
, &minEnd
, &magStart
, &magEnd
);
861 if (minStart
< minEnd
) {
862 /* do the minified texels */
863 const GLuint m
= minEnd
- minStart
;
864 switch (tObj
->MinFilter
) {
866 for (i
= minStart
; i
< minEnd
; i
++)
867 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
868 texcoords
[i
], rgba
[i
]);
871 for (i
= minStart
; i
< minEnd
; i
++)
872 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
873 texcoords
[i
], rgba
[i
]);
875 case GL_NEAREST_MIPMAP_NEAREST
:
876 sample_1d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
877 lambda
+ minStart
, rgba
+ minStart
);
879 case GL_LINEAR_MIPMAP_NEAREST
:
880 sample_1d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
881 lambda
+ minStart
, rgba
+ minStart
);
883 case GL_NEAREST_MIPMAP_LINEAR
:
884 sample_1d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
885 lambda
+ minStart
, rgba
+ minStart
);
887 case GL_LINEAR_MIPMAP_LINEAR
:
888 sample_1d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
889 lambda
+ minStart
, rgba
+ minStart
);
892 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
897 if (magStart
< magEnd
) {
898 /* do the magnified texels */
899 switch (tObj
->MagFilter
) {
901 for (i
= magStart
; i
< magEnd
; i
++)
902 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
903 texcoords
[i
], rgba
[i
]);
906 for (i
= magStart
; i
< magEnd
; i
++)
907 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
908 texcoords
[i
], rgba
[i
]);
911 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
918 /**********************************************************************/
919 /* 2-D Texture Sampling Functions */
920 /**********************************************************************/
924 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
927 sample_2d_nearest(GLcontext
*ctx
,
928 const struct gl_texture_object
*tObj
,
929 const struct gl_texture_image
*img
,
930 const GLfloat texcoord
[4],
933 const GLint width
= img
->Width2
; /* without border, power of two */
934 const GLint height
= img
->Height2
; /* without border, power of two */
938 COMPUTE_NEAREST_TEXEL_LOCATION(tObj
->WrapS
, texcoord
[0], width
, i
);
939 COMPUTE_NEAREST_TEXEL_LOCATION(tObj
->WrapT
, texcoord
[1], height
, j
);
941 /* skip over the border, if any */
945 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
946 /* Need this test for GL_CLAMP_TO_BORDER mode */
947 COPY_CHAN4(rgba
, tObj
->_BorderChan
);
950 img
->FetchTexelc(img
, i
, j
, 0, rgba
);
957 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
958 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
961 sample_2d_linear(GLcontext
*ctx
,
962 const struct gl_texture_object
*tObj
,
963 const struct gl_texture_image
*img
,
964 const GLfloat texcoord
[4],
967 const GLint width
= img
->Width2
;
968 const GLint height
= img
->Height2
;
969 GLint i0
, j0
, i1
, j1
;
970 GLbitfield useBorderColor
= 0x0;
973 GLchan t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
975 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj
->WrapS
, texcoord
[0], u
, width
, i0
, i1
);
976 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj
->WrapT
, texcoord
[1], v
, height
, j0
, j1
);
985 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
986 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
987 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
988 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
991 /* fetch four texel colors */
992 if (useBorderColor
& (I0BIT
| J0BIT
)) {
993 COPY_CHAN4(t00
, tObj
->_BorderChan
);
996 img
->FetchTexelc(img
, i0
, j0
, 0, t00
);
998 if (useBorderColor
& (I1BIT
| J0BIT
)) {
999 COPY_CHAN4(t10
, tObj
->_BorderChan
);
1002 img
->FetchTexelc(img
, i1
, j0
, 0, t10
);
1004 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1005 COPY_CHAN4(t01
, tObj
->_BorderChan
);
1008 img
->FetchTexelc(img
, i0
, j1
, 0, t01
);
1010 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1011 COPY_CHAN4(t11
, tObj
->_BorderChan
);
1014 img
->FetchTexelc(img
, i1
, j1
, 0, t11
);
1019 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1024 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1025 * We don't have to worry about the texture border.
1028 sample_2d_linear_repeat(GLcontext
*ctx
,
1029 const struct gl_texture_object
*tObj
,
1030 const struct gl_texture_image
*img
,
1031 const GLfloat texcoord
[4],
1034 const GLint width
= img
->Width2
;
1035 const GLint height
= img
->Height2
;
1036 GLint i0
, j0
, i1
, j1
;
1039 GLchan t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1043 ASSERT(tObj
->WrapS
== GL_REPEAT
);
1044 ASSERT(tObj
->WrapT
== GL_REPEAT
);
1045 ASSERT(img
->Border
== 0);
1046 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
1047 ASSERT(img
->_IsPowerOfTwo
);
1049 COMPUTE_LINEAR_REPEAT_TEXEL_LOCATION(texcoord
[0], u
, width
, i0
, i1
);
1050 COMPUTE_LINEAR_REPEAT_TEXEL_LOCATION(texcoord
[1], v
, height
, j0
, j1
);
1052 img
->FetchTexelc(img
, i0
, j0
, 0, t00
);
1053 img
->FetchTexelc(img
, i1
, j0
, 0, t10
);
1054 img
->FetchTexelc(img
, i0
, j1
, 0, t01
);
1055 img
->FetchTexelc(img
, i1
, j1
, 0, t11
);
1059 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1065 sample_2d_nearest_mipmap_nearest(GLcontext
*ctx
,
1066 const struct gl_texture_object
*tObj
,
1067 GLuint n
, const GLfloat texcoord
[][4],
1068 const GLfloat lambda
[], GLchan rgba
[][4])
1071 for (i
= 0; i
< n
; i
++) {
1072 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1073 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1080 sample_2d_linear_mipmap_nearest(GLcontext
*ctx
,
1081 const struct gl_texture_object
*tObj
,
1082 GLuint n
, const GLfloat texcoord
[][4],
1083 const GLfloat lambda
[], GLchan rgba
[][4])
1086 ASSERT(lambda
!= NULL
);
1087 for (i
= 0; i
< n
; i
++) {
1088 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1089 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1096 sample_2d_nearest_mipmap_linear(GLcontext
*ctx
,
1097 const struct gl_texture_object
*tObj
,
1098 GLuint n
, const GLfloat texcoord
[][4],
1099 const GLfloat lambda
[], GLchan rgba
[][4])
1102 ASSERT(lambda
!= NULL
);
1103 for (i
= 0; i
< n
; i
++) {
1104 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1105 if (level
>= tObj
->_MaxLevel
) {
1106 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1107 texcoord
[i
], rgba
[i
]);
1110 GLchan t0
[4], t1
[4]; /* texels */
1111 const GLfloat f
= FRAC(lambda
[i
]);
1112 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1113 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1114 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1121 /* Trilinear filtering */
1123 sample_2d_linear_mipmap_linear( GLcontext
*ctx
,
1124 const struct gl_texture_object
*tObj
,
1125 GLuint n
, const GLfloat texcoord
[][4],
1126 const GLfloat lambda
[], GLchan rgba
[][4] )
1129 ASSERT(lambda
!= NULL
);
1130 for (i
= 0; i
< n
; i
++) {
1131 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1132 if (level
>= tObj
->_MaxLevel
) {
1133 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1134 texcoord
[i
], rgba
[i
]);
1137 GLchan t0
[4], t1
[4]; /* texels */
1138 const GLfloat f
= FRAC(lambda
[i
]);
1139 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1140 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1141 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1148 sample_2d_linear_mipmap_linear_repeat( GLcontext
*ctx
,
1149 const struct gl_texture_object
*tObj
,
1150 GLuint n
, const GLfloat texcoord
[][4],
1151 const GLfloat lambda
[], GLchan rgba
[][4] )
1154 ASSERT(lambda
!= NULL
);
1155 ASSERT(tObj
->WrapS
== GL_REPEAT
);
1156 ASSERT(tObj
->WrapT
== GL_REPEAT
);
1157 for (i
= 0; i
< n
; i
++) {
1158 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1159 if (level
>= tObj
->_MaxLevel
) {
1160 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1161 texcoord
[i
], rgba
[i
]);
1164 GLchan t0
[4], t1
[4]; /* texels */
1165 const GLfloat f
= FRAC(lambda
[i
]);
1166 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1167 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1168 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1175 sample_nearest_2d( GLcontext
*ctx
,
1176 const struct gl_texture_object
*tObj
, GLuint n
,
1177 const GLfloat texcoords
[][4],
1178 const GLfloat lambda
[], GLchan rgba
[][4] )
1181 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1184 sample_2d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1191 sample_linear_2d( GLcontext
*ctx
,
1192 const struct gl_texture_object
*tObj
, GLuint n
,
1193 const GLfloat texcoords
[][4],
1194 const GLfloat lambda
[], GLchan rgba
[][4] )
1197 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1199 if (tObj
->WrapS
== GL_REPEAT
&& tObj
->WrapT
== GL_REPEAT
) {
1201 sample_2d_linear_repeat(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1206 sample_2d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1213 * Optimized 2-D texture sampling:
1214 * S and T wrap mode == GL_REPEAT
1215 * GL_NEAREST min/mag filter
1217 * RowStride == Width,
1221 opt_sample_rgb_2d( GLcontext
*ctx
,
1222 const struct gl_texture_object
*tObj
,
1223 GLuint n
, const GLfloat texcoords
[][4],
1224 const GLfloat lambda
[], GLchan rgba
[][4] )
1226 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1227 const GLfloat width
= (GLfloat
) img
->Width
;
1228 const GLfloat height
= (GLfloat
) img
->Height
;
1229 const GLint colMask
= img
->Width
- 1;
1230 const GLint rowMask
= img
->Height
- 1;
1231 const GLint shift
= img
->WidthLog2
;
1235 ASSERT(tObj
->WrapS
==GL_REPEAT
);
1236 ASSERT(tObj
->WrapT
==GL_REPEAT
);
1237 ASSERT(img
->Border
==0);
1238 ASSERT(img
->_BaseFormat
==GL_RGB
);
1239 ASSERT(img
->_IsPowerOfTwo
);
1241 for (k
=0; k
<n
; k
++) {
1242 GLint i
= IFLOOR(texcoords
[k
][0] * width
) & colMask
;
1243 GLint j
= IFLOOR(texcoords
[k
][1] * height
) & rowMask
;
1244 GLint pos
= (j
<< shift
) | i
;
1245 GLchan
*texel
= ((GLchan
*) img
->Data
) + 3*pos
;
1246 rgba
[k
][RCOMP
] = texel
[0];
1247 rgba
[k
][GCOMP
] = texel
[1];
1248 rgba
[k
][BCOMP
] = texel
[2];
1254 * Optimized 2-D texture sampling:
1255 * S and T wrap mode == GL_REPEAT
1256 * GL_NEAREST min/mag filter
1258 * RowStride == Width,
1262 opt_sample_rgba_2d( GLcontext
*ctx
,
1263 const struct gl_texture_object
*tObj
,
1264 GLuint n
, const GLfloat texcoords
[][4],
1265 const GLfloat lambda
[], GLchan rgba
[][4] )
1267 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1268 const GLfloat width
= (GLfloat
) img
->Width
;
1269 const GLfloat height
= (GLfloat
) img
->Height
;
1270 const GLint colMask
= img
->Width
- 1;
1271 const GLint rowMask
= img
->Height
- 1;
1272 const GLint shift
= img
->WidthLog2
;
1276 ASSERT(tObj
->WrapS
==GL_REPEAT
);
1277 ASSERT(tObj
->WrapT
==GL_REPEAT
);
1278 ASSERT(img
->Border
==0);
1279 ASSERT(img
->_BaseFormat
==GL_RGBA
);
1280 ASSERT(img
->_IsPowerOfTwo
);
1282 for (i
= 0; i
< n
; i
++) {
1283 const GLint col
= IFLOOR(texcoords
[i
][0] * width
) & colMask
;
1284 const GLint row
= IFLOOR(texcoords
[i
][1] * height
) & rowMask
;
1285 const GLint pos
= (row
<< shift
) | col
;
1286 const GLchan
*texel
= ((GLchan
*) img
->Data
) + (pos
<< 2); /* pos*4 */
1287 COPY_CHAN4(rgba
[i
], texel
);
1293 * Given an array of texture coordinate and lambda (level of detail)
1294 * values, return an array of texture sample.
1297 sample_lambda_2d( GLcontext
*ctx
,
1298 const struct gl_texture_object
*tObj
,
1299 GLuint n
, const GLfloat texcoords
[][4],
1300 const GLfloat lambda
[], GLchan rgba
[][4] )
1302 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1303 GLuint minStart
, minEnd
; /* texels with minification */
1304 GLuint magStart
, magEnd
; /* texels with magnification */
1306 const GLboolean repeatNoBorderPOT
= (tObj
->WrapS
== GL_REPEAT
)
1307 && (tObj
->WrapT
== GL_REPEAT
)
1308 && (tImg
->Border
== 0 && (tImg
->Width
== tImg
->RowStride
))
1309 && (tImg
->_BaseFormat
!= GL_COLOR_INDEX
)
1310 && tImg
->_IsPowerOfTwo
;
1312 ASSERT(lambda
!= NULL
);
1313 compute_min_mag_ranges(tObj
, n
, lambda
,
1314 &minStart
, &minEnd
, &magStart
, &magEnd
);
1316 if (minStart
< minEnd
) {
1317 /* do the minified texels */
1318 const GLuint m
= minEnd
- minStart
;
1319 switch (tObj
->MinFilter
) {
1321 if (repeatNoBorderPOT
) {
1322 switch (tImg
->TexFormat
->MesaFormat
) {
1323 case MESA_FORMAT_RGB
:
1324 case MESA_FORMAT_RGB888
:
1325 /*case MESA_FORMAT_BGR888:*/
1326 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1327 NULL
, rgba
+ minStart
);
1329 case MESA_FORMAT_RGBA
:
1330 case MESA_FORMAT_RGBA8888
:
1331 case MESA_FORMAT_ARGB8888
:
1332 /*case MESA_FORMAT_ABGR8888:*/
1333 /*case MESA_FORMAT_BGRA8888:*/
1334 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1335 NULL
, rgba
+ minStart
);
1338 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1339 NULL
, rgba
+ minStart
);
1343 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1344 NULL
, rgba
+ minStart
);
1348 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1349 NULL
, rgba
+ minStart
);
1351 case GL_NEAREST_MIPMAP_NEAREST
:
1352 sample_2d_nearest_mipmap_nearest(ctx
, tObj
, m
,
1353 texcoords
+ minStart
,
1354 lambda
+ minStart
, rgba
+ minStart
);
1356 case GL_LINEAR_MIPMAP_NEAREST
:
1357 sample_2d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1358 lambda
+ minStart
, rgba
+ minStart
);
1360 case GL_NEAREST_MIPMAP_LINEAR
:
1361 sample_2d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1362 lambda
+ minStart
, rgba
+ minStart
);
1364 case GL_LINEAR_MIPMAP_LINEAR
:
1365 if (repeatNoBorderPOT
)
1366 sample_2d_linear_mipmap_linear_repeat(ctx
, tObj
, m
,
1367 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1369 sample_2d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1370 lambda
+ minStart
, rgba
+ minStart
);
1373 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1378 if (magStart
< magEnd
) {
1379 /* do the magnified texels */
1380 const GLuint m
= magEnd
- magStart
;
1382 switch (tObj
->MagFilter
) {
1384 if (repeatNoBorderPOT
) {
1385 switch (tImg
->TexFormat
->MesaFormat
) {
1386 case MESA_FORMAT_RGB
:
1387 case MESA_FORMAT_RGB888
:
1388 /*case MESA_FORMAT_BGR888:*/
1389 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1390 NULL
, rgba
+ magStart
);
1392 case MESA_FORMAT_RGBA
:
1393 case MESA_FORMAT_RGBA8888
:
1394 case MESA_FORMAT_ARGB8888
:
1395 /*case MESA_FORMAT_ABGR8888:*/
1396 /*case MESA_FORMAT_BGRA8888:*/
1397 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1398 NULL
, rgba
+ magStart
);
1401 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1402 NULL
, rgba
+ magStart
);
1406 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1407 NULL
, rgba
+ magStart
);
1411 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1412 NULL
, rgba
+ magStart
);
1415 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1422 /**********************************************************************/
1423 /* 3-D Texture Sampling Functions */
1424 /**********************************************************************/
1427 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1430 sample_3d_nearest(GLcontext
*ctx
,
1431 const struct gl_texture_object
*tObj
,
1432 const struct gl_texture_image
*img
,
1433 const GLfloat texcoord
[4],
1436 const GLint width
= img
->Width2
; /* without border, power of two */
1437 const GLint height
= img
->Height2
; /* without border, power of two */
1438 const GLint depth
= img
->Depth2
; /* without border, power of two */
1442 COMPUTE_NEAREST_TEXEL_LOCATION(tObj
->WrapS
, texcoord
[0], width
, i
);
1443 COMPUTE_NEAREST_TEXEL_LOCATION(tObj
->WrapT
, texcoord
[1], height
, j
);
1444 COMPUTE_NEAREST_TEXEL_LOCATION(tObj
->WrapR
, texcoord
[2], depth
, k
);
1446 if (i
< 0 || i
>= (GLint
) img
->Width
||
1447 j
< 0 || j
>= (GLint
) img
->Height
||
1448 k
< 0 || k
>= (GLint
) img
->Depth
) {
1449 /* Need this test for GL_CLAMP_TO_BORDER mode */
1450 COPY_CHAN4(rgba
, tObj
->_BorderChan
);
1453 img
->FetchTexelc(img
, i
, j
, k
, rgba
);
1460 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1463 sample_3d_linear(GLcontext
*ctx
,
1464 const struct gl_texture_object
*tObj
,
1465 const struct gl_texture_image
*img
,
1466 const GLfloat texcoord
[4],
1469 const GLint width
= img
->Width2
;
1470 const GLint height
= img
->Height2
;
1471 const GLint depth
= img
->Depth2
;
1472 GLint i0
, j0
, k0
, i1
, j1
, k1
;
1473 GLbitfield useBorderColor
= 0x0;
1476 GLchan t000
[4], t010
[4], t001
[4], t011
[4];
1477 GLchan t100
[4], t110
[4], t101
[4], t111
[4];
1479 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj
->WrapS
, texcoord
[0], u
, width
, i0
, i1
);
1480 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj
->WrapT
, texcoord
[1], v
, height
, j0
, j1
);
1481 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj
->WrapR
, texcoord
[2], w
, depth
, k0
, k1
);
1492 /* check if sampling texture border color */
1493 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1494 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1495 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1496 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1497 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
1498 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
1502 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
1503 COPY_CHAN4(t000
, tObj
->_BorderChan
);
1506 img
->FetchTexelc(img
, i0
, j0
, k0
, t000
);
1508 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
1509 COPY_CHAN4(t100
, tObj
->_BorderChan
);
1512 img
->FetchTexelc(img
, i1
, j0
, k0
, t100
);
1514 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
1515 COPY_CHAN4(t010
, tObj
->_BorderChan
);
1518 img
->FetchTexelc(img
, i0
, j1
, k0
, t010
);
1520 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
1521 COPY_CHAN4(t110
, tObj
->_BorderChan
);
1524 img
->FetchTexelc(img
, i1
, j1
, k0
, t110
);
1527 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
1528 COPY_CHAN4(t001
, tObj
->_BorderChan
);
1531 img
->FetchTexelc(img
, i0
, j0
, k1
, t001
);
1533 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
1534 COPY_CHAN4(t101
, tObj
->_BorderChan
);
1537 img
->FetchTexelc(img
, i1
, j0
, k1
, t101
);
1539 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
1540 COPY_CHAN4(t011
, tObj
->_BorderChan
);
1543 img
->FetchTexelc(img
, i0
, j1
, k1
, t011
);
1545 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
1546 COPY_CHAN4(t111
, tObj
->_BorderChan
);
1549 img
->FetchTexelc(img
, i1
, j1
, k1
, t111
);
1552 /* trilinear interpolation of samples */
1556 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
1562 sample_3d_nearest_mipmap_nearest(GLcontext
*ctx
,
1563 const struct gl_texture_object
*tObj
,
1564 GLuint n
, const GLfloat texcoord
[][4],
1565 const GLfloat lambda
[], GLchan rgba
[][4] )
1568 for (i
= 0; i
< n
; i
++) {
1569 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1570 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1576 sample_3d_linear_mipmap_nearest(GLcontext
*ctx
,
1577 const struct gl_texture_object
*tObj
,
1578 GLuint n
, const GLfloat texcoord
[][4],
1579 const GLfloat lambda
[], GLchan rgba
[][4])
1582 ASSERT(lambda
!= NULL
);
1583 for (i
= 0; i
< n
; i
++) {
1584 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1585 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1591 sample_3d_nearest_mipmap_linear(GLcontext
*ctx
,
1592 const struct gl_texture_object
*tObj
,
1593 GLuint n
, const GLfloat texcoord
[][4],
1594 const GLfloat lambda
[], GLchan rgba
[][4])
1597 ASSERT(lambda
!= NULL
);
1598 for (i
= 0; i
< n
; i
++) {
1599 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1600 if (level
>= tObj
->_MaxLevel
) {
1601 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1602 texcoord
[i
], rgba
[i
]);
1605 GLchan t0
[4], t1
[4]; /* texels */
1606 const GLfloat f
= FRAC(lambda
[i
]);
1607 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1608 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1609 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1616 sample_3d_linear_mipmap_linear(GLcontext
*ctx
,
1617 const struct gl_texture_object
*tObj
,
1618 GLuint n
, const GLfloat texcoord
[][4],
1619 const GLfloat lambda
[], GLchan rgba
[][4])
1622 ASSERT(lambda
!= NULL
);
1623 for (i
= 0; i
< n
; i
++) {
1624 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1625 if (level
>= tObj
->_MaxLevel
) {
1626 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1627 texcoord
[i
], rgba
[i
]);
1630 GLchan t0
[4], t1
[4]; /* texels */
1631 const GLfloat f
= FRAC(lambda
[i
]);
1632 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1633 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1634 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1641 sample_nearest_3d(GLcontext
*ctx
,
1642 const struct gl_texture_object
*tObj
, GLuint n
,
1643 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1647 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1650 sample_3d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1657 sample_linear_3d( GLcontext
*ctx
,
1658 const struct gl_texture_object
*tObj
, GLuint n
,
1659 const GLfloat texcoords
[][4],
1660 const GLfloat lambda
[], GLchan rgba
[][4] )
1663 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1666 sample_3d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1672 * Given an (s,t,r) texture coordinate and lambda (level of detail) value,
1673 * return a texture sample.
1676 sample_lambda_3d( GLcontext
*ctx
,
1677 const struct gl_texture_object
*tObj
, GLuint n
,
1678 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1681 GLuint minStart
, minEnd
; /* texels with minification */
1682 GLuint magStart
, magEnd
; /* texels with magnification */
1685 ASSERT(lambda
!= NULL
);
1686 compute_min_mag_ranges(tObj
, n
, lambda
,
1687 &minStart
, &minEnd
, &magStart
, &magEnd
);
1689 if (minStart
< minEnd
) {
1690 /* do the minified texels */
1691 GLuint m
= minEnd
- minStart
;
1692 switch (tObj
->MinFilter
) {
1694 for (i
= minStart
; i
< minEnd
; i
++)
1695 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1696 texcoords
[i
], rgba
[i
]);
1699 for (i
= minStart
; i
< minEnd
; i
++)
1700 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1701 texcoords
[i
], rgba
[i
]);
1703 case GL_NEAREST_MIPMAP_NEAREST
:
1704 sample_3d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1705 lambda
+ minStart
, rgba
+ minStart
);
1707 case GL_LINEAR_MIPMAP_NEAREST
:
1708 sample_3d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1709 lambda
+ minStart
, rgba
+ minStart
);
1711 case GL_NEAREST_MIPMAP_LINEAR
:
1712 sample_3d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1713 lambda
+ minStart
, rgba
+ minStart
);
1715 case GL_LINEAR_MIPMAP_LINEAR
:
1716 sample_3d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1717 lambda
+ minStart
, rgba
+ minStart
);
1720 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
1725 if (magStart
< magEnd
) {
1726 /* do the magnified texels */
1727 switch (tObj
->MagFilter
) {
1729 for (i
= magStart
; i
< magEnd
; i
++)
1730 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1731 texcoords
[i
], rgba
[i
]);
1734 for (i
= magStart
; i
< magEnd
; i
++)
1735 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1736 texcoords
[i
], rgba
[i
]);
1739 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
1746 /**********************************************************************/
1747 /* Texture Cube Map Sampling Functions */
1748 /**********************************************************************/
1751 * Choose one of six sides of a texture cube map given the texture
1752 * coord (rx,ry,rz). Return pointer to corresponding array of texture
1755 static const struct gl_texture_image
**
1756 choose_cube_face(const struct gl_texture_object
*texObj
,
1757 const GLfloat texcoord
[4], GLfloat newCoord
[4])
1761 direction target sc tc ma
1762 ---------- ------------------------------- --- --- ---
1763 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1764 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1765 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1766 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1767 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1768 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1770 const GLfloat rx
= texcoord
[0];
1771 const GLfloat ry
= texcoord
[1];
1772 const GLfloat rz
= texcoord
[2];
1773 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
1777 if (arx
> ary
&& arx
> arz
) {
1791 else if (ary
> arx
&& ary
> arz
) {
1820 newCoord
[0] = ( sc
/ ma
+ 1.0F
) * 0.5F
;
1821 newCoord
[1] = ( tc
/ ma
+ 1.0F
) * 0.5F
;
1822 return (const struct gl_texture_image
**) texObj
->Image
[face
];
1827 sample_nearest_cube(GLcontext
*ctx
,
1828 const struct gl_texture_object
*tObj
, GLuint n
,
1829 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1834 for (i
= 0; i
< n
; i
++) {
1835 const struct gl_texture_image
**images
;
1836 GLfloat newCoord
[4];
1837 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1838 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
1845 sample_linear_cube(GLcontext
*ctx
,
1846 const struct gl_texture_object
*tObj
, GLuint n
,
1847 const GLfloat texcoords
[][4],
1848 const GLfloat lambda
[], GLchan rgba
[][4])
1852 for (i
= 0; i
< n
; i
++) {
1853 const struct gl_texture_image
**images
;
1854 GLfloat newCoord
[4];
1855 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1856 sample_2d_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
1863 sample_cube_nearest_mipmap_nearest(GLcontext
*ctx
,
1864 const struct gl_texture_object
*tObj
,
1865 GLuint n
, const GLfloat texcoord
[][4],
1866 const GLfloat lambda
[], GLchan rgba
[][4])
1869 ASSERT(lambda
!= NULL
);
1870 for (i
= 0; i
< n
; i
++) {
1871 const struct gl_texture_image
**images
;
1872 GLfloat newCoord
[4];
1873 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1874 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1875 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
1881 sample_cube_linear_mipmap_nearest(GLcontext
*ctx
,
1882 const struct gl_texture_object
*tObj
,
1883 GLuint n
, const GLfloat texcoord
[][4],
1884 const GLfloat lambda
[], GLchan rgba
[][4])
1887 ASSERT(lambda
!= NULL
);
1888 for (i
= 0; i
< n
; i
++) {
1889 const struct gl_texture_image
**images
;
1890 GLfloat newCoord
[4];
1891 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1892 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1893 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
1899 sample_cube_nearest_mipmap_linear(GLcontext
*ctx
,
1900 const struct gl_texture_object
*tObj
,
1901 GLuint n
, const GLfloat texcoord
[][4],
1902 const GLfloat lambda
[], GLchan rgba
[][4])
1905 ASSERT(lambda
!= NULL
);
1906 for (i
= 0; i
< n
; i
++) {
1907 const struct gl_texture_image
**images
;
1908 GLfloat newCoord
[4];
1909 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1910 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1911 if (level
>= tObj
->_MaxLevel
) {
1912 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
1916 GLchan t0
[4], t1
[4]; /* texels */
1917 const GLfloat f
= FRAC(lambda
[i
]);
1918 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
1919 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
1920 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1927 sample_cube_linear_mipmap_linear(GLcontext
*ctx
,
1928 const struct gl_texture_object
*tObj
,
1929 GLuint n
, const GLfloat texcoord
[][4],
1930 const GLfloat lambda
[], GLchan rgba
[][4])
1933 ASSERT(lambda
!= NULL
);
1934 for (i
= 0; i
< n
; i
++) {
1935 const struct gl_texture_image
**images
;
1936 GLfloat newCoord
[4];
1937 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1938 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1939 if (level
>= tObj
->_MaxLevel
) {
1940 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
1944 GLchan t0
[4], t1
[4];
1945 const GLfloat f
= FRAC(lambda
[i
]);
1946 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
1947 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
1948 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1955 sample_lambda_cube( GLcontext
*ctx
,
1956 const struct gl_texture_object
*tObj
, GLuint n
,
1957 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1960 GLuint minStart
, minEnd
; /* texels with minification */
1961 GLuint magStart
, magEnd
; /* texels with magnification */
1963 ASSERT(lambda
!= NULL
);
1964 compute_min_mag_ranges(tObj
, n
, lambda
,
1965 &minStart
, &minEnd
, &magStart
, &magEnd
);
1967 if (minStart
< minEnd
) {
1968 /* do the minified texels */
1969 const GLuint m
= minEnd
- minStart
;
1970 switch (tObj
->MinFilter
) {
1972 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
1973 lambda
+ minStart
, rgba
+ minStart
);
1976 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
1977 lambda
+ minStart
, rgba
+ minStart
);
1979 case GL_NEAREST_MIPMAP_NEAREST
:
1980 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
1981 texcoords
+ minStart
,
1982 lambda
+ minStart
, rgba
+ minStart
);
1984 case GL_LINEAR_MIPMAP_NEAREST
:
1985 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
1986 texcoords
+ minStart
,
1987 lambda
+ minStart
, rgba
+ minStart
);
1989 case GL_NEAREST_MIPMAP_LINEAR
:
1990 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
1991 texcoords
+ minStart
,
1992 lambda
+ minStart
, rgba
+ minStart
);
1994 case GL_LINEAR_MIPMAP_LINEAR
:
1995 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
1996 texcoords
+ minStart
,
1997 lambda
+ minStart
, rgba
+ minStart
);
2000 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2004 if (magStart
< magEnd
) {
2005 /* do the magnified texels */
2006 const GLuint m
= magEnd
- magStart
;
2007 switch (tObj
->MagFilter
) {
2009 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2010 lambda
+ magStart
, rgba
+ magStart
);
2013 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2014 lambda
+ magStart
, rgba
+ magStart
);
2017 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2023 /**********************************************************************/
2024 /* Texture Rectangle Sampling Functions */
2025 /**********************************************************************/
2028 sample_nearest_rect(GLcontext
*ctx
,
2029 const struct gl_texture_object
*tObj
, GLuint n
,
2030 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2033 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2034 const GLfloat width
= (GLfloat
) img
->Width
;
2035 const GLfloat height
= (GLfloat
) img
->Height
;
2036 const GLint width_minus_1
= img
->Width
- 1;
2037 const GLint height_minus_1
= img
->Height
- 1;
2043 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2044 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2045 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2046 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2047 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2048 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2049 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
2051 /* XXX move Wrap mode tests outside of loops for common cases */
2052 for (i
= 0; i
< n
; i
++) {
2054 /* NOTE: we DO NOT use [0, 1] texture coordinates! */
2055 if (tObj
->WrapS
== GL_CLAMP
) {
2056 col
= IFLOOR( CLAMP(texcoords
[i
][0], 0.0F
, width
- 1) );
2058 else if (tObj
->WrapS
== GL_CLAMP_TO_EDGE
) {
2059 col
= IFLOOR( CLAMP(texcoords
[i
][0], 0.5F
, width
- 0.5F
) );
2062 col
= IFLOOR( CLAMP(texcoords
[i
][0], -0.5F
, width
+ 0.5F
) );
2064 if (tObj
->WrapT
== GL_CLAMP
) {
2065 row
= IFLOOR( CLAMP(texcoords
[i
][1], 0.0F
, height
- 1) );
2067 else if (tObj
->WrapT
== GL_CLAMP_TO_EDGE
) {
2068 row
= IFLOOR( CLAMP(texcoords
[i
][1], 0.5F
, height
- 0.5F
) );
2071 row
= IFLOOR( CLAMP(texcoords
[i
][1], -0.5F
, height
+ 0.5F
) );
2074 if (col
< 0 || col
> width_minus_1
|| row
< 0 || row
> height_minus_1
)
2075 COPY_CHAN4(rgba
[i
], tObj
->_BorderChan
);
2077 img
->FetchTexelc(img
, col
, row
, 0, rgba
[i
]);
2083 sample_linear_rect(GLcontext
*ctx
,
2084 const struct gl_texture_object
*tObj
, GLuint n
,
2085 const GLfloat texcoords
[][4],
2086 const GLfloat lambda
[], GLchan rgba
[][4])
2088 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2089 const GLfloat width
= (GLfloat
) img
->Width
;
2090 const GLfloat height
= (GLfloat
) img
->Height
;
2091 const GLint width_minus_1
= img
->Width
- 1;
2092 const GLint height_minus_1
= img
->Height
- 1;
2098 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2099 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2100 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2101 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2102 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2103 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2104 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
2106 /* XXX lots of opportunity for optimization in this loop */
2107 for (i
= 0; i
< n
; i
++) {
2109 GLint i0
, j0
, i1
, j1
;
2110 GLchan t00
[4], t01
[4], t10
[4], t11
[4];
2112 GLbitfield useBorderColor
= 0x0;
2114 /* NOTE: we DO NOT use [0, 1] texture coordinates! */
2115 if (tObj
->WrapS
== GL_CLAMP
) {
2116 /* Not exactly what the spec says, but it matches NVIDIA output */
2117 fcol
= CLAMP(texcoords
[i
][0] - 0.5F
, 0.0, width_minus_1
);
2121 else if (tObj
->WrapS
== GL_CLAMP_TO_EDGE
) {
2122 fcol
= CLAMP(texcoords
[i
][0], 0.5F
, width
- 0.5F
);
2126 if (i1
> width_minus_1
)
2130 ASSERT(tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2131 fcol
= CLAMP(texcoords
[i
][0], -0.5F
, width
+ 0.5F
);
2137 if (tObj
->WrapT
== GL_CLAMP
) {
2138 /* Not exactly what the spec says, but it matches NVIDIA output */
2139 frow
= CLAMP(texcoords
[i
][1] - 0.5F
, 0.0, width_minus_1
);
2143 else if (tObj
->WrapT
== GL_CLAMP_TO_EDGE
) {
2144 frow
= CLAMP(texcoords
[i
][1], 0.5F
, height
- 0.5F
);
2148 if (j1
> height_minus_1
)
2149 j1
= height_minus_1
;
2152 ASSERT(tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2153 frow
= CLAMP(texcoords
[i
][1], -0.5F
, height
+ 0.5F
);
2159 /* compute integer rows/columns */
2160 if (i0
< 0 || i0
> width_minus_1
) useBorderColor
|= I0BIT
;
2161 if (i1
< 0 || i1
> width_minus_1
) useBorderColor
|= I1BIT
;
2162 if (j0
< 0 || j0
> height_minus_1
) useBorderColor
|= J0BIT
;
2163 if (j1
< 0 || j1
> height_minus_1
) useBorderColor
|= J1BIT
;
2165 /* get four texel samples */
2166 if (useBorderColor
& (I0BIT
| J0BIT
))
2167 COPY_CHAN4(t00
, tObj
->_BorderChan
);
2169 img
->FetchTexelc(img
, i0
, j0
, 0, t00
);
2171 if (useBorderColor
& (I1BIT
| J0BIT
))
2172 COPY_CHAN4(t10
, tObj
->_BorderChan
);
2174 img
->FetchTexelc(img
, i1
, j0
, 0, t10
);
2176 if (useBorderColor
& (I0BIT
| J1BIT
))
2177 COPY_CHAN4(t01
, tObj
->_BorderChan
);
2179 img
->FetchTexelc(img
, i0
, j1
, 0, t01
);
2181 if (useBorderColor
& (I1BIT
| J1BIT
))
2182 COPY_CHAN4(t11
, tObj
->_BorderChan
);
2184 img
->FetchTexelc(img
, i1
, j1
, 0, t11
);
2186 /* compute interpolants */
2190 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2196 sample_lambda_rect( GLcontext
*ctx
,
2197 const struct gl_texture_object
*tObj
, GLuint n
,
2198 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2201 GLuint minStart
, minEnd
, magStart
, magEnd
;
2203 /* We only need lambda to decide between minification and magnification.
2204 * There is no mipmapping with rectangular textures.
2206 compute_min_mag_ranges(tObj
, n
, lambda
,
2207 &minStart
, &minEnd
, &magStart
, &magEnd
);
2209 if (minStart
< minEnd
) {
2210 if (tObj
->MinFilter
== GL_NEAREST
) {
2211 sample_nearest_rect( ctx
, tObj
, minEnd
- minStart
,
2212 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2215 sample_linear_rect( ctx
, tObj
, minEnd
- minStart
,
2216 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2219 if (magStart
< magEnd
) {
2220 if (tObj
->MagFilter
== GL_NEAREST
) {
2221 sample_nearest_rect( ctx
, tObj
, magEnd
- magStart
,
2222 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2225 sample_linear_rect( ctx
, tObj
, magEnd
- magStart
,
2226 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2234 * Sample a shadow/depth texture.
2237 sample_depth_texture( GLcontext
*ctx
,
2238 const struct gl_texture_object
*tObj
, GLuint n
,
2239 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2242 const GLint baseLevel
= tObj
->BaseLevel
;
2243 const struct gl_texture_image
*img
= tObj
->Image
[0][baseLevel
];
2244 const GLint width
= img
->Width
;
2245 const GLint height
= img
->Height
;
2252 ASSERT(tObj
->Image
[0][tObj
->BaseLevel
]->_BaseFormat
== GL_DEPTH_COMPONENT
||
2253 tObj
->Image
[0][tObj
->BaseLevel
]->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
2255 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
2256 tObj
->Target
== GL_TEXTURE_2D
||
2257 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
);
2259 UNCLAMPED_FLOAT_TO_CHAN(ambient
, tObj
->ShadowAmbient
);
2261 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
2263 /* XXX this could be precomputed and saved in the texture object */
2264 if (tObj
->CompareFlag
) {
2265 /* GL_SGIX_shadow */
2266 if (tObj
->CompareOperator
== GL_TEXTURE_LEQUAL_R_SGIX
) {
2267 function
= GL_LEQUAL
;
2270 ASSERT(tObj
->CompareOperator
== GL_TEXTURE_GEQUAL_R_SGIX
);
2271 function
= GL_GEQUAL
;
2274 else if (tObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) {
2276 function
= tObj
->CompareFunc
;
2279 function
= GL_NONE
; /* pass depth through as grayscale */
2282 if (tObj
->MagFilter
== GL_NEAREST
) {
2284 for (i
= 0; i
< n
; i
++) {
2285 GLfloat depthSample
;
2287 /* XXX fix for texture rectangle! */
2288 COMPUTE_NEAREST_TEXEL_LOCATION(tObj
->WrapS
, texcoords
[i
][0], width
, col
);
2289 COMPUTE_NEAREST_TEXEL_LOCATION(tObj
->WrapT
, texcoords
[i
][1], height
, row
);
2290 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
) {
2291 img
->FetchTexelf(img
, col
, row
, 0, &depthSample
);
2294 depthSample
= tObj
->BorderColor
[0];
2299 result
= (texcoords
[i
][2] <= depthSample
) ? CHAN_MAX
: ambient
;
2302 result
= (texcoords
[i
][2] >= depthSample
) ? CHAN_MAX
: ambient
;
2305 result
= (texcoords
[i
][2] < depthSample
) ? CHAN_MAX
: ambient
;
2308 result
= (texcoords
[i
][2] > depthSample
) ? CHAN_MAX
: ambient
;
2311 result
= (texcoords
[i
][2] == depthSample
) ? CHAN_MAX
: ambient
;
2314 result
= (texcoords
[i
][2] != depthSample
) ? CHAN_MAX
: ambient
;
2323 CLAMPED_FLOAT_TO_CHAN(result
, depthSample
);
2326 _mesa_problem(ctx
, "Bad compare func in sample_depth_texture");
2330 switch (tObj
->DepthMode
) {
2332 texel
[i
][RCOMP
] = result
;
2333 texel
[i
][GCOMP
] = result
;
2334 texel
[i
][BCOMP
] = result
;
2335 texel
[i
][ACOMP
] = CHAN_MAX
;
2338 texel
[i
][RCOMP
] = result
;
2339 texel
[i
][GCOMP
] = result
;
2340 texel
[i
][BCOMP
] = result
;
2341 texel
[i
][ACOMP
] = result
;
2344 texel
[i
][RCOMP
] = 0;
2345 texel
[i
][GCOMP
] = 0;
2346 texel
[i
][BCOMP
] = 0;
2347 texel
[i
][ACOMP
] = result
;
2350 _mesa_problem(ctx
, "Bad depth texture mode");
2356 ASSERT(tObj
->MagFilter
== GL_LINEAR
);
2357 for (i
= 0; i
< n
; i
++) {
2358 GLfloat depth00
, depth01
, depth10
, depth11
;
2359 GLint i0
, i1
, j0
, j1
;
2361 GLuint useBorderTexel
;
2363 /* XXX fix for texture rectangle! */
2364 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj
->WrapS
, texcoords
[i
][0], u
, width
, i0
, i1
);
2365 COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj
->WrapT
, texcoords
[i
][1], v
, height
,j0
, j1
);
2375 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
2376 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
2377 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
2378 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
2381 /* get four depth samples from the texture */
2382 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
2383 depth00
= tObj
->BorderColor
[0];
2386 img
->FetchTexelf(img
, i0
, j0
, 0, &depth00
);
2388 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
2389 depth10
= tObj
->BorderColor
[0];
2392 img
->FetchTexelf(img
, i1
, j0
, 0, &depth10
);
2394 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
2395 depth01
= tObj
->BorderColor
[0];
2398 img
->FetchTexelf(img
, i0
, j1
, 0, &depth01
);
2400 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
2401 depth11
= tObj
->BorderColor
[0];
2404 img
->FetchTexelf(img
, i1
, j1
, 0, &depth11
);
2408 /* compute a single weighted depth sample and do one comparison */
2409 const GLfloat a
= FRAC(u
+ 1.0F
);
2410 const GLfloat b
= FRAC(v
+ 1.0F
);
2411 const GLfloat depthSample
2412 = lerp_2d(a
, b
, depth00
, depth10
, depth01
, depth11
);
2413 if ((depthSample
<= texcoords
[i
][2] && function
== GL_LEQUAL
) ||
2414 (depthSample
>= texcoords
[i
][2] && function
== GL_GEQUAL
)) {
2422 /* Do four depth/R comparisons and compute a weighted result.
2423 * If this touches on somebody's I.P., I'll remove this code
2426 const GLfloat d
= (CHAN_MAXF
- (GLfloat
) ambient
) * 0.25F
;
2427 GLfloat luminance
= CHAN_MAXF
;
2431 if (depth00
<= texcoords
[i
][2]) luminance
-= d
;
2432 if (depth01
<= texcoords
[i
][2]) luminance
-= d
;
2433 if (depth10
<= texcoords
[i
][2]) luminance
-= d
;
2434 if (depth11
<= texcoords
[i
][2]) luminance
-= d
;
2435 result
= (GLchan
) luminance
;
2438 if (depth00
>= texcoords
[i
][2]) luminance
-= d
;
2439 if (depth01
>= texcoords
[i
][2]) luminance
-= d
;
2440 if (depth10
>= texcoords
[i
][2]) luminance
-= d
;
2441 if (depth11
>= texcoords
[i
][2]) luminance
-= d
;
2442 result
= (GLchan
) luminance
;
2445 if (depth00
< texcoords
[i
][2]) luminance
-= d
;
2446 if (depth01
< texcoords
[i
][2]) luminance
-= d
;
2447 if (depth10
< texcoords
[i
][2]) luminance
-= d
;
2448 if (depth11
< texcoords
[i
][2]) luminance
-= d
;
2449 result
= (GLchan
) luminance
;
2452 if (depth00
> texcoords
[i
][2]) luminance
-= d
;
2453 if (depth01
> texcoords
[i
][2]) luminance
-= d
;
2454 if (depth10
> texcoords
[i
][2]) luminance
-= d
;
2455 if (depth11
> texcoords
[i
][2]) luminance
-= d
;
2456 result
= (GLchan
) luminance
;
2459 if (depth00
== texcoords
[i
][2]) luminance
-= d
;
2460 if (depth01
== texcoords
[i
][2]) luminance
-= d
;
2461 if (depth10
== texcoords
[i
][2]) luminance
-= d
;
2462 if (depth11
== texcoords
[i
][2]) luminance
-= d
;
2463 result
= (GLchan
) luminance
;
2466 if (depth00
!= texcoords
[i
][2]) luminance
-= d
;
2467 if (depth01
!= texcoords
[i
][2]) luminance
-= d
;
2468 if (depth10
!= texcoords
[i
][2]) luminance
-= d
;
2469 if (depth11
!= texcoords
[i
][2]) luminance
-= d
;
2470 result
= (GLchan
) luminance
;
2479 /* ordinary bilinear filtering */
2481 const GLfloat a
= FRAC(u
+ 1.0F
);
2482 const GLfloat b
= FRAC(v
+ 1.0F
);
2483 const GLfloat depthSample
2484 = lerp_2d(a
, b
, depth00
, depth10
, depth01
, depth11
);
2485 CLAMPED_FLOAT_TO_CHAN(result
, depthSample
);
2489 _mesa_problem(ctx
, "Bad compare func in sample_depth_texture");
2494 switch (tObj
->DepthMode
) {
2496 texel
[i
][RCOMP
] = result
;
2497 texel
[i
][GCOMP
] = result
;
2498 texel
[i
][BCOMP
] = result
;
2499 texel
[i
][ACOMP
] = CHAN_MAX
;
2502 texel
[i
][RCOMP
] = result
;
2503 texel
[i
][GCOMP
] = result
;
2504 texel
[i
][BCOMP
] = result
;
2505 texel
[i
][ACOMP
] = result
;
2508 texel
[i
][RCOMP
] = 0;
2509 texel
[i
][GCOMP
] = 0;
2510 texel
[i
][BCOMP
] = 0;
2511 texel
[i
][ACOMP
] = result
;
2514 _mesa_problem(ctx
, "Bad depth texture mode");
2523 * Experimental depth texture sampling function.
2526 sample_depth_texture2(const GLcontext
*ctx
,
2527 const struct gl_texture_unit
*texUnit
,
2528 GLuint n
, const GLfloat texcoords
[][4],
2531 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
2532 const GLint baseLevel
= texObj
->BaseLevel
;
2533 const struct gl_texture_image
*texImage
= texObj
->Image
[0][baseLevel
];
2534 const GLuint width
= texImage
->Width
;
2535 const GLuint height
= texImage
->Height
;
2537 GLboolean lequal
, gequal
;
2539 if (texObj
->Target
!= GL_TEXTURE_2D
) {
2540 _mesa_problem(ctx
, "only 2-D depth textures supported at this time");
2544 if (texObj
->MinFilter
!= texObj
->MagFilter
) {
2545 _mesa_problem(ctx
, "mipmapped depth textures not supported at this time");
2549 /* XXX the GL_SGIX_shadow extension spec doesn't say what to do if
2550 * GL_TEXTURE_COMPARE_SGIX == GL_TRUE but the current texture object
2551 * isn't a depth texture.
2553 if (texImage
->_BaseFormat
!= GL_DEPTH_COMPONENT
) {
2554 _mesa_problem(ctx
,"GL_TEXTURE_COMPARE_SGIX enabled with non-depth texture");
2558 UNCLAMPED_FLOAT_TO_CHAN(ambient
, tObj
->ShadowAmbient
);
2560 if (texObj
->CompareOperator
== GL_TEXTURE_LEQUAL_R_SGIX
) {
2571 for (i
= 0; i
< n
; i
++) {
2573 GLint col
, row
, ii
, jj
, imin
, imax
, jmin
, jmax
, samples
, count
;
2576 COMPUTE_NEAREST_TEXEL_LOCATION(texObj
->WrapS
, texcoords
[i
][0],
2578 COMPUTE_NEAREST_TEXEL_LOCATION(texObj
->WrapT
, texcoords
[i
][1],
2586 if (imin
< 0) imin
= 0;
2587 if (imax
>= width
) imax
= width
- 1;
2588 if (jmin
< 0) jmin
= 0;
2589 if (jmax
>= height
) jmax
= height
- 1;
2591 samples
= (imax
- imin
+ 1) * (jmax
- jmin
+ 1);
2593 for (jj
= jmin
; jj
<= jmax
; jj
++) {
2594 for (ii
= imin
; ii
<= imax
; ii
++) {
2595 GLfloat depthSample
;
2596 texImage
->FetchTexelf(texImage
, ii
, jj
, 0, &depthSample
);
2597 if ((depthSample
<= r
[i
] && lequal
) ||
2598 (depthSample
>= r
[i
] && gequal
)) {
2604 w
= (GLfloat
) count
/ (GLfloat
) samples
;
2605 w
= CHAN_MAXF
- w
* (CHAN_MAXF
- (GLfloat
) ambient
);
2608 texel
[i
][RCOMP
] = lum
;
2609 texel
[i
][GCOMP
] = lum
;
2610 texel
[i
][BCOMP
] = lum
;
2611 texel
[i
][ACOMP
] = CHAN_MAX
;
2619 * We use this function when a texture object is in an "incomplete" state.
2620 * When a fragment program attempts to sample an incomplete texture we
2621 * return black (see issue 23 in GL_ARB_fragment_program spec).
2622 * Note: fragment programs don't observe the texture enable/disable flags.
2625 null_sample_func( GLcontext
*ctx
,
2626 const struct gl_texture_object
*tObj
, GLuint n
,
2627 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2635 for (i
= 0; i
< n
; i
++) {
2639 rgba
[i
][ACOMP
] = CHAN_MAX
;
2645 * Choose the texture sampling function for the given texture object.
2648 _swrast_choose_texture_sample_func( GLcontext
*ctx
,
2649 const struct gl_texture_object
*t
)
2651 if (!t
|| !t
->Complete
) {
2652 return &null_sample_func
;
2655 const GLboolean needLambda
= (GLboolean
) (t
->MinFilter
!= t
->MagFilter
);
2656 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->_BaseFormat
;
2658 switch (t
->Target
) {
2660 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
2661 return &sample_depth_texture
;
2663 else if (needLambda
) {
2664 return &sample_lambda_1d
;
2666 else if (t
->MinFilter
== GL_LINEAR
) {
2667 return &sample_linear_1d
;
2670 ASSERT(t
->MinFilter
== GL_NEAREST
);
2671 return &sample_nearest_1d
;
2674 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
2675 return &sample_depth_texture
;
2677 else if (needLambda
) {
2678 return &sample_lambda_2d
;
2680 else if (t
->MinFilter
== GL_LINEAR
) {
2681 return &sample_linear_2d
;
2684 /* check for a few optimized cases */
2685 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
2686 ASSERT(t
->MinFilter
== GL_NEAREST
);
2687 if (t
->WrapS
== GL_REPEAT
&&
2688 t
->WrapT
== GL_REPEAT
&&
2689 img
->_IsPowerOfTwo
&&
2691 img
->TexFormat
->MesaFormat
== MESA_FORMAT_RGB
) {
2692 return &opt_sample_rgb_2d
;
2694 else if (t
->WrapS
== GL_REPEAT
&&
2695 t
->WrapT
== GL_REPEAT
&&
2696 img
->_IsPowerOfTwo
&&
2698 img
->TexFormat
->MesaFormat
== MESA_FORMAT_RGBA
) {
2699 return &opt_sample_rgba_2d
;
2702 return &sample_nearest_2d
;
2707 return &sample_lambda_3d
;
2709 else if (t
->MinFilter
== GL_LINEAR
) {
2710 return &sample_linear_3d
;
2713 ASSERT(t
->MinFilter
== GL_NEAREST
);
2714 return &sample_nearest_3d
;
2716 case GL_TEXTURE_CUBE_MAP
:
2718 return &sample_lambda_cube
;
2720 else if (t
->MinFilter
== GL_LINEAR
) {
2721 return &sample_linear_cube
;
2724 ASSERT(t
->MinFilter
== GL_NEAREST
);
2725 return &sample_nearest_cube
;
2727 case GL_TEXTURE_RECTANGLE_NV
:
2729 return &sample_lambda_rect
;
2731 else if (t
->MinFilter
== GL_LINEAR
) {
2732 return &sample_linear_rect
;
2735 ASSERT(t
->MinFilter
== GL_NEAREST
);
2736 return &sample_nearest_rect
;
2740 "invalid target in _swrast_choose_texture_sample_func");
2741 return &null_sample_func
;