2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 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.
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/colormac.h"
29 #include "main/imports.h"
31 #include "s_context.h"
32 #include "s_texfilter.h"
36 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
37 * see 1-pixel bands of improperly weighted linear-filtered textures.
38 * The tests/texwrap.c demo is a good test.
39 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
40 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
42 #define FRAC(f) ((f) - IFLOOR(f))
47 * Linear interpolation macro
49 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
53 * Do 2D/biliner interpolation of float values.
54 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
55 * a and b are the horizontal and vertical interpolants.
56 * It's important that this function is inlined when compiled with
57 * optimization! If we find that's not true on some systems, convert
61 lerp_2d(GLfloat a
, GLfloat b
,
62 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
64 const GLfloat temp0
= LERP(a
, v00
, v10
);
65 const GLfloat temp1
= LERP(a
, v01
, v11
);
66 return LERP(b
, temp0
, temp1
);
71 * Do 3D/trilinear interpolation of float values.
75 lerp_3d(GLfloat a
, GLfloat b
, GLfloat c
,
76 GLfloat v000
, GLfloat v100
, GLfloat v010
, GLfloat v110
,
77 GLfloat v001
, GLfloat v101
, GLfloat v011
, GLfloat v111
)
79 const GLfloat temp00
= LERP(a
, v000
, v100
);
80 const GLfloat temp10
= LERP(a
, v010
, v110
);
81 const GLfloat temp01
= LERP(a
, v001
, v101
);
82 const GLfloat temp11
= LERP(a
, v011
, v111
);
83 const GLfloat temp0
= LERP(b
, temp00
, temp10
);
84 const GLfloat temp1
= LERP(b
, temp01
, temp11
);
85 return LERP(c
, temp0
, temp1
);
90 * Do linear interpolation of colors.
93 lerp_rgba(GLfloat result
[4], GLfloat t
, const GLfloat a
[4], const GLfloat b
[4])
95 result
[0] = LERP(t
, a
[0], b
[0]);
96 result
[1] = LERP(t
, a
[1], b
[1]);
97 result
[2] = LERP(t
, a
[2], b
[2]);
98 result
[3] = LERP(t
, a
[3], b
[3]);
103 * Do bilinear interpolation of colors.
106 lerp_rgba_2d(GLfloat result
[4], GLfloat a
, GLfloat b
,
107 const GLfloat t00
[4], const GLfloat t10
[4],
108 const GLfloat t01
[4], const GLfloat t11
[4])
110 result
[0] = lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]);
111 result
[1] = lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]);
112 result
[2] = lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]);
113 result
[3] = lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]);
118 * Do trilinear interpolation of colors.
121 lerp_rgba_3d(GLfloat result
[4], GLfloat a
, GLfloat b
, GLfloat c
,
122 const GLfloat t000
[4], const GLfloat t100
[4],
123 const GLfloat t010
[4], const GLfloat t110
[4],
124 const GLfloat t001
[4], const GLfloat t101
[4],
125 const GLfloat t011
[4], const GLfloat t111
[4])
128 /* compiler should unroll these short loops */
129 for (k
= 0; k
< 4; k
++) {
130 result
[k
] = lerp_3d(a
, b
, c
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
131 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
137 * Used for GL_REPEAT wrap mode. Using A % B doesn't produce the
138 * right results for A<0. Casting to A to be unsigned only works if B
139 * is a power of two. Adding a bias to A (which is a multiple of B)
140 * avoids the problems with A < 0 (for reasonable A) without using a
143 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
147 * Used to compute texel locations for linear sampling.
149 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
150 * s = texcoord in [0,1]
151 * size = width (or height or depth) of texture
153 * i0, i1 = returns two nearest texel indexes
154 * weight = returns blend factor between texels
157 linear_texel_locations(GLenum wrapMode
,
158 const struct gl_texture_image
*img
,
159 GLint size
, GLfloat s
,
160 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
162 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
167 if (swImg
->_IsPowerOfTwo
) {
168 *i0
= IFLOOR(u
) & (size
- 1);
169 *i1
= (*i0
+ 1) & (size
- 1);
172 *i0
= REMAINDER(IFLOOR(u
), size
);
173 *i1
= REMAINDER(*i0
+ 1, size
);
176 case GL_CLAMP_TO_EDGE
:
188 if (*i1
>= (GLint
) size
)
191 case GL_CLAMP_TO_BORDER
:
193 const GLfloat min
= -1.0F
/ (2.0F
* size
);
194 const GLfloat max
= 1.0F
- min
;
206 case GL_MIRRORED_REPEAT
:
208 const GLint flr
= IFLOOR(s
);
210 u
= 1.0F
- (s
- (GLfloat
) flr
);
212 u
= s
- (GLfloat
) flr
;
213 u
= (u
* size
) - 0.5F
;
218 if (*i1
>= (GLint
) size
)
222 case GL_MIRROR_CLAMP_EXT
:
232 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
243 if (*i1
>= (GLint
) size
)
246 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
248 const GLfloat min
= -1.0F
/ (2.0F
* size
);
249 const GLfloat max
= 1.0F
- min
;
274 _mesa_problem(NULL
, "Bad wrap mode");
282 * Used to compute texel location for nearest sampling.
285 nearest_texel_location(GLenum wrapMode
,
286 const struct gl_texture_image
*img
,
287 GLint size
, GLfloat s
)
289 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
294 /* s limited to [0,1) */
295 /* i limited to [0,size-1] */
296 i
= IFLOOR(s
* size
);
297 if (swImg
->_IsPowerOfTwo
)
300 i
= REMAINDER(i
, size
);
302 case GL_CLAMP_TO_EDGE
:
304 /* s limited to [min,max] */
305 /* i limited to [0, size-1] */
306 const GLfloat min
= 1.0F
/ (2.0F
* size
);
307 const GLfloat max
= 1.0F
- min
;
313 i
= IFLOOR(s
* size
);
316 case GL_CLAMP_TO_BORDER
:
318 /* s limited to [min,max] */
319 /* i limited to [-1, size] */
320 const GLfloat min
= -1.0F
/ (2.0F
* size
);
321 const GLfloat max
= 1.0F
- min
;
327 i
= IFLOOR(s
* size
);
330 case GL_MIRRORED_REPEAT
:
332 const GLfloat min
= 1.0F
/ (2.0F
* size
);
333 const GLfloat max
= 1.0F
- min
;
334 const GLint flr
= IFLOOR(s
);
337 u
= 1.0F
- (s
- (GLfloat
) flr
);
339 u
= s
- (GLfloat
) flr
;
345 i
= IFLOOR(u
* size
);
348 case GL_MIRROR_CLAMP_EXT
:
350 /* s limited to [0,1] */
351 /* i limited to [0,size-1] */
352 const GLfloat u
= FABSF(s
);
358 i
= IFLOOR(u
* size
);
361 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
363 /* s limited to [min,max] */
364 /* i limited to [0, size-1] */
365 const GLfloat min
= 1.0F
/ (2.0F
* size
);
366 const GLfloat max
= 1.0F
- min
;
367 const GLfloat u
= FABSF(s
);
373 i
= IFLOOR(u
* size
);
376 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
378 /* s limited to [min,max] */
379 /* i limited to [0, size-1] */
380 const GLfloat min
= -1.0F
/ (2.0F
* size
);
381 const GLfloat max
= 1.0F
- min
;
382 const GLfloat u
= FABSF(s
);
388 i
= IFLOOR(u
* size
);
392 /* s limited to [0,1] */
393 /* i limited to [0,size-1] */
399 i
= IFLOOR(s
* size
);
402 _mesa_problem(NULL
, "Bad wrap mode");
408 /* Power of two image sizes only */
410 linear_repeat_texel_location(GLuint size
, GLfloat s
,
411 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
413 GLfloat u
= s
* size
- 0.5F
;
414 *i0
= IFLOOR(u
) & (size
- 1);
415 *i1
= (*i0
+ 1) & (size
- 1);
421 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
424 clamp_rect_coord_nearest(GLenum wrapMode
, GLfloat coord
, GLint max
)
428 return IFLOOR( CLAMP(coord
, 0.0F
, max
- 1) );
429 case GL_CLAMP_TO_EDGE
:
430 return IFLOOR( CLAMP(coord
, 0.5F
, max
- 0.5F
) );
431 case GL_CLAMP_TO_BORDER
:
432 return IFLOOR( CLAMP(coord
, -0.5F
, max
+ 0.5F
) );
434 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_nearest");
441 * As above, but GL_LINEAR filtering.
444 clamp_rect_coord_linear(GLenum wrapMode
, GLfloat coord
, GLint max
,
445 GLint
*i0out
, GLint
*i1out
, GLfloat
*weight
)
451 /* Not exactly what the spec says, but it matches NVIDIA output */
452 fcol
= CLAMP(coord
- 0.5F
, 0.0F
, max
- 1);
456 case GL_CLAMP_TO_EDGE
:
457 fcol
= CLAMP(coord
, 0.5F
, max
- 0.5F
);
464 case GL_CLAMP_TO_BORDER
:
465 fcol
= CLAMP(coord
, -0.5F
, max
+ 0.5F
);
471 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_linear");
477 *weight
= FRAC(fcol
);
482 * Compute slice/image to use for 1D or 2D array texture.
485 tex_array_slice(GLfloat coord
, GLsizei size
)
487 GLint slice
= IFLOOR(coord
+ 0.5f
);
488 slice
= CLAMP(slice
, 0, size
- 1);
494 * Compute nearest integer texcoords for given texobj and coordinate.
495 * NOTE: only used for depth texture sampling.
498 nearest_texcoord(const struct gl_texture_object
*texObj
,
500 const GLfloat texcoord
[4],
501 GLint
*i
, GLint
*j
, GLint
*k
)
503 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
504 const GLint width
= img
->Width
;
505 const GLint height
= img
->Height
;
506 const GLint depth
= img
->Depth
;
508 switch (texObj
->Target
) {
509 case GL_TEXTURE_RECTANGLE_ARB
:
510 *i
= clamp_rect_coord_nearest(texObj
->Sampler
.WrapS
, texcoord
[0], width
);
511 *j
= clamp_rect_coord_nearest(texObj
->Sampler
.WrapT
, texcoord
[1], height
);
515 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
520 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
521 *j
= nearest_texel_location(texObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
524 case GL_TEXTURE_1D_ARRAY_EXT
:
525 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
526 *j
= tex_array_slice(texcoord
[1], height
);
529 case GL_TEXTURE_2D_ARRAY_EXT
:
530 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
531 *j
= nearest_texel_location(texObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
532 *k
= tex_array_slice(texcoord
[2], depth
);
541 * Compute linear integer texcoords for given texobj and coordinate.
542 * NOTE: only used for depth texture sampling.
545 linear_texcoord(const struct gl_texture_object
*texObj
,
547 const GLfloat texcoord
[4],
548 GLint
*i0
, GLint
*i1
, GLint
*j0
, GLint
*j1
, GLint
*slice
,
549 GLfloat
*wi
, GLfloat
*wj
)
551 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
552 const GLint width
= img
->Width
;
553 const GLint height
= img
->Height
;
554 const GLint depth
= img
->Depth
;
556 switch (texObj
->Target
) {
557 case GL_TEXTURE_RECTANGLE_ARB
:
558 clamp_rect_coord_linear(texObj
->Sampler
.WrapS
, texcoord
[0],
560 clamp_rect_coord_linear(texObj
->Sampler
.WrapT
, texcoord
[1],
567 linear_texel_locations(texObj
->Sampler
.WrapS
, img
, width
,
568 texcoord
[0], i0
, i1
, wi
);
569 linear_texel_locations(texObj
->Sampler
.WrapT
, img
, height
,
570 texcoord
[1], j0
, j1
, wj
);
574 case GL_TEXTURE_1D_ARRAY_EXT
:
575 linear_texel_locations(texObj
->Sampler
.WrapS
, img
, width
,
576 texcoord
[0], i0
, i1
, wi
);
577 *j0
= tex_array_slice(texcoord
[1], height
);
582 case GL_TEXTURE_2D_ARRAY_EXT
:
583 linear_texel_locations(texObj
->Sampler
.WrapS
, img
, width
,
584 texcoord
[0], i0
, i1
, wi
);
585 linear_texel_locations(texObj
->Sampler
.WrapT
, img
, height
,
586 texcoord
[1], j0
, j1
, wj
);
587 *slice
= tex_array_slice(texcoord
[2], depth
);
598 * For linear interpolation between mipmap levels N and N+1, this function
602 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
605 return tObj
->BaseLevel
;
606 else if (lambda
> tObj
->_MaxLambda
)
607 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
609 return (GLint
) (tObj
->BaseLevel
+ lambda
);
614 * Compute the nearest mipmap level to take texels from.
617 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
623 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
624 l
= tObj
->_MaxLambda
+ 0.4999F
;
627 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
628 if (level
> tObj
->_MaxLevel
)
629 level
= tObj
->_MaxLevel
;
636 * Bitflags for texture border color sampling.
648 * The lambda[] array values are always monotonic. Either the whole span
649 * will be minified, magnified, or split between the two. This function
650 * determines the subranges in [0, n-1] that are to be minified or magnified.
653 compute_min_mag_ranges(const struct gl_texture_object
*tObj
,
654 GLuint n
, const GLfloat lambda
[],
655 GLuint
*minStart
, GLuint
*minEnd
,
656 GLuint
*magStart
, GLuint
*magEnd
)
658 GLfloat minMagThresh
;
660 /* we shouldn't be here if minfilter == magfilter */
661 ASSERT(tObj
->Sampler
.MinFilter
!= tObj
->Sampler
.MagFilter
);
663 /* This bit comes from the OpenGL spec: */
664 if (tObj
->Sampler
.MagFilter
== GL_LINEAR
665 && (tObj
->Sampler
.MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
666 tObj
->Sampler
.MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
674 /* DEBUG CODE: Verify that lambda[] is monotonic.
675 * We can't really use this because the inaccuracy in the LOG2 function
676 * causes this test to fail, yet the resulting texturing is correct.
680 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
681 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
682 for (i
= 0; i
< n
- 1; i
++) {
683 ASSERT((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
686 else { /* increasing */
687 for (i
= 0; i
< n
- 1; i
++) {
688 ASSERT((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
694 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
695 /* magnification for whole span */
698 *minStart
= *minEnd
= 0;
700 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
701 /* minification for whole span */
704 *magStart
= *magEnd
= 0;
707 /* a mix of minification and magnification */
709 if (lambda
[0] > minMagThresh
) {
710 /* start with minification */
711 for (i
= 1; i
< n
; i
++) {
712 if (lambda
[i
] <= minMagThresh
)
721 /* start with magnification */
722 for (i
= 1; i
< n
; i
++) {
723 if (lambda
[i
] > minMagThresh
)
734 /* Verify the min/mag Start/End values
735 * We don't use this either (see above)
739 for (i
= 0; i
< n
; i
++) {
740 if (lambda
[i
] > minMagThresh
) {
742 ASSERT(i
>= *minStart
);
747 ASSERT(i
>= *magStart
);
757 * When we sample the border color, it must be interpreted according to
758 * the base texture format. Ex: if the texture base format it GL_ALPHA,
759 * we return (0,0,0,BorderAlpha).
762 get_border_color(const struct gl_texture_object
*tObj
,
763 const struct gl_texture_image
*img
,
766 switch (img
->_BaseFormat
) {
768 rgba
[0] = tObj
->Sampler
.BorderColor
.f
[0];
769 rgba
[1] = tObj
->Sampler
.BorderColor
.f
[1];
770 rgba
[2] = tObj
->Sampler
.BorderColor
.f
[2];
774 rgba
[0] = rgba
[1] = rgba
[2] = 0.0;
775 rgba
[3] = tObj
->Sampler
.BorderColor
.f
[3];
778 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->Sampler
.BorderColor
.f
[0];
781 case GL_LUMINANCE_ALPHA
:
782 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->Sampler
.BorderColor
.f
[0];
783 rgba
[3] = tObj
->Sampler
.BorderColor
.f
[3];
786 rgba
[0] = rgba
[1] = rgba
[2] = rgba
[3] = tObj
->Sampler
.BorderColor
.f
[0];
789 COPY_4V(rgba
, tObj
->Sampler
.BorderColor
.f
);
794 /**********************************************************************/
795 /* 1-D Texture Sampling Functions */
796 /**********************************************************************/
799 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
802 sample_1d_nearest(struct gl_context
*ctx
,
803 const struct gl_texture_object
*tObj
,
804 const struct gl_texture_image
*img
,
805 const GLfloat texcoord
[4], GLfloat rgba
[4])
807 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
808 const GLint width
= img
->Width2
; /* without border, power of two */
810 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
811 /* skip over the border, if any */
813 if (i
< 0 || i
>= (GLint
) img
->Width
) {
814 /* Need this test for GL_CLAMP_TO_BORDER mode */
815 get_border_color(tObj
, img
, rgba
);
818 swImg
->FetchTexelf(swImg
, i
, 0, 0, rgba
);
824 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
827 sample_1d_linear(struct gl_context
*ctx
,
828 const struct gl_texture_object
*tObj
,
829 const struct gl_texture_image
*img
,
830 const GLfloat texcoord
[4], GLfloat rgba
[4])
832 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
833 const GLint width
= img
->Width2
;
835 GLbitfield useBorderColor
= 0x0;
837 GLfloat t0
[4], t1
[4]; /* texels */
839 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
846 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
847 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
850 /* fetch texel colors */
851 if (useBorderColor
& I0BIT
) {
852 get_border_color(tObj
, img
, t0
);
855 swImg
->FetchTexelf(swImg
, i0
, 0, 0, t0
);
857 if (useBorderColor
& I1BIT
) {
858 get_border_color(tObj
, img
, t1
);
861 swImg
->FetchTexelf(swImg
, i1
, 0, 0, t1
);
864 lerp_rgba(rgba
, a
, t0
, t1
);
869 sample_1d_nearest_mipmap_nearest(struct gl_context
*ctx
,
870 const struct gl_texture_object
*tObj
,
871 GLuint n
, const GLfloat texcoord
[][4],
872 const GLfloat lambda
[], GLfloat rgba
[][4])
875 ASSERT(lambda
!= NULL
);
876 for (i
= 0; i
< n
; i
++) {
877 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
878 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
884 sample_1d_linear_mipmap_nearest(struct gl_context
*ctx
,
885 const struct gl_texture_object
*tObj
,
886 GLuint n
, const GLfloat texcoord
[][4],
887 const GLfloat lambda
[], GLfloat rgba
[][4])
890 ASSERT(lambda
!= NULL
);
891 for (i
= 0; i
< n
; i
++) {
892 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
893 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
899 sample_1d_nearest_mipmap_linear(struct gl_context
*ctx
,
900 const struct gl_texture_object
*tObj
,
901 GLuint n
, const GLfloat texcoord
[][4],
902 const GLfloat lambda
[], GLfloat rgba
[][4])
905 ASSERT(lambda
!= NULL
);
906 for (i
= 0; i
< n
; i
++) {
907 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
908 if (level
>= tObj
->_MaxLevel
) {
909 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
910 texcoord
[i
], rgba
[i
]);
913 GLfloat t0
[4], t1
[4];
914 const GLfloat f
= FRAC(lambda
[i
]);
915 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
916 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
917 lerp_rgba(rgba
[i
], f
, t0
, t1
);
924 sample_1d_linear_mipmap_linear(struct gl_context
*ctx
,
925 const struct gl_texture_object
*tObj
,
926 GLuint n
, const GLfloat texcoord
[][4],
927 const GLfloat lambda
[], GLfloat rgba
[][4])
930 ASSERT(lambda
!= NULL
);
931 for (i
= 0; i
< n
; i
++) {
932 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
933 if (level
>= tObj
->_MaxLevel
) {
934 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
935 texcoord
[i
], rgba
[i
]);
938 GLfloat t0
[4], t1
[4];
939 const GLfloat f
= FRAC(lambda
[i
]);
940 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
941 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
942 lerp_rgba(rgba
[i
], f
, t0
, t1
);
948 /** Sample 1D texture, nearest filtering for both min/magnification */
950 sample_nearest_1d( struct gl_context
*ctx
,
951 const struct gl_texture_object
*tObj
, GLuint n
,
952 const GLfloat texcoords
[][4], const GLfloat lambda
[],
956 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
958 for (i
= 0; i
< n
; i
++) {
959 sample_1d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
964 /** Sample 1D texture, linear filtering for both min/magnification */
966 sample_linear_1d( struct gl_context
*ctx
,
967 const struct gl_texture_object
*tObj
, GLuint n
,
968 const GLfloat texcoords
[][4], const GLfloat lambda
[],
972 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
974 for (i
= 0; i
< n
; i
++) {
975 sample_1d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
980 /** Sample 1D texture, using lambda to choose between min/magnification */
982 sample_lambda_1d( struct gl_context
*ctx
,
983 const struct gl_texture_object
*tObj
, GLuint n
,
984 const GLfloat texcoords
[][4],
985 const GLfloat lambda
[], GLfloat rgba
[][4] )
987 GLuint minStart
, minEnd
; /* texels with minification */
988 GLuint magStart
, magEnd
; /* texels with magnification */
991 ASSERT(lambda
!= NULL
);
992 compute_min_mag_ranges(tObj
, n
, lambda
,
993 &minStart
, &minEnd
, &magStart
, &magEnd
);
995 if (minStart
< minEnd
) {
996 /* do the minified texels */
997 const GLuint m
= minEnd
- minStart
;
998 switch (tObj
->Sampler
.MinFilter
) {
1000 for (i
= minStart
; i
< minEnd
; i
++)
1001 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1002 texcoords
[i
], rgba
[i
]);
1005 for (i
= minStart
; i
< minEnd
; i
++)
1006 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1007 texcoords
[i
], rgba
[i
]);
1009 case GL_NEAREST_MIPMAP_NEAREST
:
1010 sample_1d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1011 lambda
+ minStart
, rgba
+ minStart
);
1013 case GL_LINEAR_MIPMAP_NEAREST
:
1014 sample_1d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1015 lambda
+ minStart
, rgba
+ minStart
);
1017 case GL_NEAREST_MIPMAP_LINEAR
:
1018 sample_1d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1019 lambda
+ minStart
, rgba
+ minStart
);
1021 case GL_LINEAR_MIPMAP_LINEAR
:
1022 sample_1d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1023 lambda
+ minStart
, rgba
+ minStart
);
1026 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
1031 if (magStart
< magEnd
) {
1032 /* do the magnified texels */
1033 switch (tObj
->Sampler
.MagFilter
) {
1035 for (i
= magStart
; i
< magEnd
; i
++)
1036 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1037 texcoords
[i
], rgba
[i
]);
1040 for (i
= magStart
; i
< magEnd
; i
++)
1041 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1042 texcoords
[i
], rgba
[i
]);
1045 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
1052 /**********************************************************************/
1053 /* 2-D Texture Sampling Functions */
1054 /**********************************************************************/
1058 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1061 sample_2d_nearest(struct gl_context
*ctx
,
1062 const struct gl_texture_object
*tObj
,
1063 const struct gl_texture_image
*img
,
1064 const GLfloat texcoord
[4],
1067 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1068 const GLint width
= img
->Width2
; /* without border, power of two */
1069 const GLint height
= img
->Height2
; /* without border, power of two */
1073 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
1074 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
1076 /* skip over the border, if any */
1080 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
1081 /* Need this test for GL_CLAMP_TO_BORDER mode */
1082 get_border_color(tObj
, img
, rgba
);
1085 swImg
->FetchTexelf(swImg
, i
, j
, 0, rgba
);
1091 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1092 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1095 sample_2d_linear(struct gl_context
*ctx
,
1096 const struct gl_texture_object
*tObj
,
1097 const struct gl_texture_image
*img
,
1098 const GLfloat texcoord
[4],
1101 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1102 const GLint width
= img
->Width2
;
1103 const GLint height
= img
->Height2
;
1104 GLint i0
, j0
, i1
, j1
;
1105 GLbitfield useBorderColor
= 0x0;
1107 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1109 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1110 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1119 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1120 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1121 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1122 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1125 /* fetch four texel colors */
1126 if (useBorderColor
& (I0BIT
| J0BIT
)) {
1127 get_border_color(tObj
, img
, t00
);
1130 swImg
->FetchTexelf(swImg
, i0
, j0
, 0, t00
);
1132 if (useBorderColor
& (I1BIT
| J0BIT
)) {
1133 get_border_color(tObj
, img
, t10
);
1136 swImg
->FetchTexelf(swImg
, i1
, j0
, 0, t10
);
1138 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1139 get_border_color(tObj
, img
, t01
);
1142 swImg
->FetchTexelf(swImg
, i0
, j1
, 0, t01
);
1144 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1145 get_border_color(tObj
, img
, t11
);
1148 swImg
->FetchTexelf(swImg
, i1
, j1
, 0, t11
);
1151 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1156 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1157 * We don't have to worry about the texture border.
1160 sample_2d_linear_repeat(struct gl_context
*ctx
,
1161 const struct gl_texture_object
*tObj
,
1162 const struct gl_texture_image
*img
,
1163 const GLfloat texcoord
[4],
1166 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1167 const GLint width
= img
->Width2
;
1168 const GLint height
= img
->Height2
;
1169 GLint i0
, j0
, i1
, j1
;
1171 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1175 ASSERT(tObj
->Sampler
.WrapS
== GL_REPEAT
);
1176 ASSERT(tObj
->Sampler
.WrapT
== GL_REPEAT
);
1177 ASSERT(img
->Border
== 0);
1178 ASSERT(swImg
->_IsPowerOfTwo
);
1180 linear_repeat_texel_location(width
, texcoord
[0], &i0
, &i1
, &wi
);
1181 linear_repeat_texel_location(height
, texcoord
[1], &j0
, &j1
, &wj
);
1183 swImg
->FetchTexelf(swImg
, i0
, j0
, 0, t00
);
1184 swImg
->FetchTexelf(swImg
, i1
, j0
, 0, t10
);
1185 swImg
->FetchTexelf(swImg
, i0
, j1
, 0, t01
);
1186 swImg
->FetchTexelf(swImg
, i1
, j1
, 0, t11
);
1188 lerp_rgba_2d(rgba
, wi
, wj
, t00
, t10
, t01
, t11
);
1193 sample_2d_nearest_mipmap_nearest(struct gl_context
*ctx
,
1194 const struct gl_texture_object
*tObj
,
1195 GLuint n
, const GLfloat texcoord
[][4],
1196 const GLfloat lambda
[], GLfloat rgba
[][4])
1199 for (i
= 0; i
< n
; i
++) {
1200 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1201 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1207 sample_2d_linear_mipmap_nearest(struct gl_context
*ctx
,
1208 const struct gl_texture_object
*tObj
,
1209 GLuint n
, const GLfloat texcoord
[][4],
1210 const GLfloat lambda
[], GLfloat rgba
[][4])
1213 ASSERT(lambda
!= NULL
);
1214 for (i
= 0; i
< n
; i
++) {
1215 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1216 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1222 sample_2d_nearest_mipmap_linear(struct gl_context
*ctx
,
1223 const struct gl_texture_object
*tObj
,
1224 GLuint n
, const GLfloat texcoord
[][4],
1225 const GLfloat lambda
[], GLfloat rgba
[][4])
1228 ASSERT(lambda
!= NULL
);
1229 for (i
= 0; i
< n
; i
++) {
1230 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1231 if (level
>= tObj
->_MaxLevel
) {
1232 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1233 texcoord
[i
], rgba
[i
]);
1236 GLfloat t0
[4], t1
[4]; /* texels */
1237 const GLfloat f
= FRAC(lambda
[i
]);
1238 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1239 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1240 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1247 sample_2d_linear_mipmap_linear( struct gl_context
*ctx
,
1248 const struct gl_texture_object
*tObj
,
1249 GLuint n
, const GLfloat texcoord
[][4],
1250 const GLfloat lambda
[], GLfloat rgba
[][4] )
1253 ASSERT(lambda
!= NULL
);
1254 for (i
= 0; i
< n
; i
++) {
1255 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1256 if (level
>= tObj
->_MaxLevel
) {
1257 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1258 texcoord
[i
], rgba
[i
]);
1261 GLfloat t0
[4], t1
[4]; /* texels */
1262 const GLfloat f
= FRAC(lambda
[i
]);
1263 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1264 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1265 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1272 sample_2d_linear_mipmap_linear_repeat(struct gl_context
*ctx
,
1273 const struct gl_texture_object
*tObj
,
1274 GLuint n
, const GLfloat texcoord
[][4],
1275 const GLfloat lambda
[], GLfloat rgba
[][4])
1278 ASSERT(lambda
!= NULL
);
1279 ASSERT(tObj
->Sampler
.WrapS
== GL_REPEAT
);
1280 ASSERT(tObj
->Sampler
.WrapT
== GL_REPEAT
);
1281 for (i
= 0; i
< n
; i
++) {
1282 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1283 if (level
>= tObj
->_MaxLevel
) {
1284 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1285 texcoord
[i
], rgba
[i
]);
1288 GLfloat t0
[4], t1
[4]; /* texels */
1289 const GLfloat f
= FRAC(lambda
[i
]);
1290 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
],
1292 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
+1],
1294 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1300 /** Sample 2D texture, nearest filtering for both min/magnification */
1302 sample_nearest_2d(struct gl_context
*ctx
,
1303 const struct gl_texture_object
*tObj
, GLuint n
,
1304 const GLfloat texcoords
[][4],
1305 const GLfloat lambda
[], GLfloat rgba
[][4])
1308 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1310 for (i
= 0; i
< n
; i
++) {
1311 sample_2d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1316 /** Sample 2D texture, linear filtering for both min/magnification */
1318 sample_linear_2d(struct gl_context
*ctx
,
1319 const struct gl_texture_object
*tObj
, GLuint n
,
1320 const GLfloat texcoords
[][4],
1321 const GLfloat lambda
[], GLfloat rgba
[][4])
1324 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1325 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(image
);
1327 if (tObj
->Sampler
.WrapS
== GL_REPEAT
&&
1328 tObj
->Sampler
.WrapT
== GL_REPEAT
&&
1329 swImg
->_IsPowerOfTwo
&&
1330 image
->Border
== 0) {
1331 for (i
= 0; i
< n
; i
++) {
1332 sample_2d_linear_repeat(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1336 for (i
= 0; i
< n
; i
++) {
1337 sample_2d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1344 * Optimized 2-D texture sampling:
1345 * S and T wrap mode == GL_REPEAT
1346 * GL_NEAREST min/mag filter
1348 * RowStride == Width,
1352 opt_sample_rgb_2d(struct gl_context
*ctx
,
1353 const struct gl_texture_object
*tObj
,
1354 GLuint n
, const GLfloat texcoords
[][4],
1355 const GLfloat lambda
[], GLfloat rgba
[][4])
1357 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1358 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1359 const GLfloat width
= (GLfloat
) img
->Width
;
1360 const GLfloat height
= (GLfloat
) img
->Height
;
1361 const GLint colMask
= img
->Width
- 1;
1362 const GLint rowMask
= img
->Height
- 1;
1363 const GLint shift
= img
->WidthLog2
;
1367 ASSERT(tObj
->Sampler
.WrapS
==GL_REPEAT
);
1368 ASSERT(tObj
->Sampler
.WrapT
==GL_REPEAT
);
1369 ASSERT(img
->Border
==0);
1370 ASSERT(img
->TexFormat
== MESA_FORMAT_RGB888
);
1371 ASSERT(swImg
->_IsPowerOfTwo
);
1373 for (k
=0; k
<n
; k
++) {
1374 GLint i
= IFLOOR(texcoords
[k
][0] * width
) & colMask
;
1375 GLint j
= IFLOOR(texcoords
[k
][1] * height
) & rowMask
;
1376 GLint pos
= (j
<< shift
) | i
;
1377 GLubyte
*texel
= ((GLubyte
*) img
->Data
) + 3*pos
;
1378 rgba
[k
][RCOMP
] = UBYTE_TO_FLOAT(texel
[2]);
1379 rgba
[k
][GCOMP
] = UBYTE_TO_FLOAT(texel
[1]);
1380 rgba
[k
][BCOMP
] = UBYTE_TO_FLOAT(texel
[0]);
1381 rgba
[k
][ACOMP
] = 1.0F
;
1387 * Optimized 2-D texture sampling:
1388 * S and T wrap mode == GL_REPEAT
1389 * GL_NEAREST min/mag filter
1391 * RowStride == Width,
1395 opt_sample_rgba_2d(struct gl_context
*ctx
,
1396 const struct gl_texture_object
*tObj
,
1397 GLuint n
, const GLfloat texcoords
[][4],
1398 const GLfloat lambda
[], GLfloat rgba
[][4])
1400 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1401 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1402 const GLfloat width
= (GLfloat
) img
->Width
;
1403 const GLfloat height
= (GLfloat
) img
->Height
;
1404 const GLint colMask
= img
->Width
- 1;
1405 const GLint rowMask
= img
->Height
- 1;
1406 const GLint shift
= img
->WidthLog2
;
1410 ASSERT(tObj
->Sampler
.WrapS
==GL_REPEAT
);
1411 ASSERT(tObj
->Sampler
.WrapT
==GL_REPEAT
);
1412 ASSERT(img
->Border
==0);
1413 ASSERT(img
->TexFormat
== MESA_FORMAT_RGBA8888
);
1414 ASSERT(swImg
->_IsPowerOfTwo
);
1416 for (i
= 0; i
< n
; i
++) {
1417 const GLint col
= IFLOOR(texcoords
[i
][0] * width
) & colMask
;
1418 const GLint row
= IFLOOR(texcoords
[i
][1] * height
) & rowMask
;
1419 const GLint pos
= (row
<< shift
) | col
;
1420 const GLuint texel
= *((GLuint
*) img
->Data
+ pos
);
1421 rgba
[i
][RCOMP
] = UBYTE_TO_FLOAT( (texel
>> 24) );
1422 rgba
[i
][GCOMP
] = UBYTE_TO_FLOAT( (texel
>> 16) & 0xff );
1423 rgba
[i
][BCOMP
] = UBYTE_TO_FLOAT( (texel
>> 8) & 0xff );
1424 rgba
[i
][ACOMP
] = UBYTE_TO_FLOAT( (texel
) & 0xff );
1429 /** Sample 2D texture, using lambda to choose between min/magnification */
1431 sample_lambda_2d(struct gl_context
*ctx
,
1432 const struct gl_texture_object
*tObj
,
1433 GLuint n
, const GLfloat texcoords
[][4],
1434 const GLfloat lambda
[], GLfloat rgba
[][4])
1436 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1437 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(tImg
);
1438 GLuint minStart
, minEnd
; /* texels with minification */
1439 GLuint magStart
, magEnd
; /* texels with magnification */
1441 const GLboolean repeatNoBorderPOT
= (tObj
->Sampler
.WrapS
== GL_REPEAT
)
1442 && (tObj
->Sampler
.WrapT
== GL_REPEAT
)
1443 && (tImg
->Border
== 0 && (tImg
->Width
== tImg
->RowStride
))
1444 && swImg
->_IsPowerOfTwo
;
1446 ASSERT(lambda
!= NULL
);
1447 compute_min_mag_ranges(tObj
, n
, lambda
,
1448 &minStart
, &minEnd
, &magStart
, &magEnd
);
1450 if (minStart
< minEnd
) {
1451 /* do the minified texels */
1452 const GLuint m
= minEnd
- minStart
;
1453 switch (tObj
->Sampler
.MinFilter
) {
1455 if (repeatNoBorderPOT
) {
1456 switch (tImg
->TexFormat
) {
1457 case MESA_FORMAT_RGB888
:
1458 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1459 NULL
, rgba
+ minStart
);
1461 case MESA_FORMAT_RGBA8888
:
1462 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1463 NULL
, rgba
+ minStart
);
1466 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1467 NULL
, rgba
+ minStart
);
1471 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1472 NULL
, rgba
+ minStart
);
1476 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1477 NULL
, rgba
+ minStart
);
1479 case GL_NEAREST_MIPMAP_NEAREST
:
1480 sample_2d_nearest_mipmap_nearest(ctx
, tObj
, m
,
1481 texcoords
+ minStart
,
1482 lambda
+ minStart
, rgba
+ minStart
);
1484 case GL_LINEAR_MIPMAP_NEAREST
:
1485 sample_2d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1486 lambda
+ minStart
, rgba
+ minStart
);
1488 case GL_NEAREST_MIPMAP_LINEAR
:
1489 sample_2d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1490 lambda
+ minStart
, rgba
+ minStart
);
1492 case GL_LINEAR_MIPMAP_LINEAR
:
1493 if (repeatNoBorderPOT
)
1494 sample_2d_linear_mipmap_linear_repeat(ctx
, tObj
, m
,
1495 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1497 sample_2d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1498 lambda
+ minStart
, rgba
+ minStart
);
1501 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1506 if (magStart
< magEnd
) {
1507 /* do the magnified texels */
1508 const GLuint m
= magEnd
- magStart
;
1510 switch (tObj
->Sampler
.MagFilter
) {
1512 if (repeatNoBorderPOT
) {
1513 switch (tImg
->TexFormat
) {
1514 case MESA_FORMAT_RGB888
:
1515 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1516 NULL
, rgba
+ magStart
);
1518 case MESA_FORMAT_RGBA8888
:
1519 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1520 NULL
, rgba
+ magStart
);
1523 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1524 NULL
, rgba
+ magStart
);
1528 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1529 NULL
, rgba
+ magStart
);
1533 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1534 NULL
, rgba
+ magStart
);
1537 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1543 /* For anisotropic filtering */
1544 #define WEIGHT_LUT_SIZE 1024
1546 static GLfloat
*weightLut
= NULL
;
1549 * Creates the look-up table used to speed-up EWA sampling
1552 create_filter_table(void)
1556 weightLut
= (GLfloat
*) malloc(WEIGHT_LUT_SIZE
* sizeof(GLfloat
));
1558 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
1560 GLfloat r2
= (GLfloat
) i
/ (GLfloat
) (WEIGHT_LUT_SIZE
- 1);
1561 GLfloat weight
= (GLfloat
) exp(-alpha
* r2
);
1562 weightLut
[i
] = weight
;
1569 * Elliptical weighted average (EWA) filter for producing high quality
1570 * anisotropic filtered results.
1571 * Based on the Higher Quality Elliptical Weighted Avarage Filter
1572 * published by Paul S. Heckbert in his Master's Thesis
1573 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1576 sample_2d_ewa(struct gl_context
*ctx
,
1577 const struct gl_texture_object
*tObj
,
1578 const GLfloat texcoord
[4],
1579 const GLfloat dudx
, const GLfloat dvdx
,
1580 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1583 GLint level
= lod
> 0 ? lod
: 0;
1584 GLfloat scaling
= 1.0 / (1 << level
);
1585 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1586 const struct gl_texture_image
*mostDetailedImage
=
1587 tObj
->Image
[0][tObj
->BaseLevel
];
1588 GLfloat tex_u
=-0.5 + texcoord
[0] * mostDetailedImage
->WidthScale
* scaling
;
1589 GLfloat tex_v
=-0.5 + texcoord
[1] * mostDetailedImage
->HeightScale
* scaling
;
1591 GLfloat ux
= dudx
* scaling
;
1592 GLfloat vx
= dvdx
* scaling
;
1593 GLfloat uy
= dudy
* scaling
;
1594 GLfloat vy
= dvdy
* scaling
;
1596 /* compute ellipse coefficients to bound the region:
1597 * A*x*x + B*x*y + C*y*y = F.
1599 GLfloat A
= vx
*vx
+vy
*vy
+1;
1600 GLfloat B
= -2*(ux
*vx
+uy
*vy
);
1601 GLfloat C
= ux
*ux
+uy
*uy
+1;
1602 GLfloat F
= A
*C
-B
*B
/4.0;
1604 /* check if it is an ellipse */
1605 /* ASSERT(F > 0.0); */
1607 /* Compute the ellipse's (u,v) bounding box in texture space */
1608 GLfloat d
= -B
*B
+4.0*C
*A
;
1609 GLfloat box_u
= 2.0 / d
* sqrt(d
*C
*F
); /* box_u -> half of bbox with */
1610 GLfloat box_v
= 2.0 / d
* sqrt(A
*d
*F
); /* box_v -> half of bbox height */
1612 GLint u0
= floor(tex_u
- box_u
);
1613 GLint u1
= ceil (tex_u
+ box_u
);
1614 GLint v0
= floor(tex_v
- box_v
);
1615 GLint v1
= ceil (tex_v
+ box_v
);
1617 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1618 GLfloat newCoord
[2];
1621 GLfloat U
= u0
- tex_u
;
1624 /* Scale ellipse formula to directly index the Filter Lookup Table.
1625 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1627 double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
1631 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1633 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1634 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1635 * value, q, is less than F, we're inside the ellipse
1638 for (v
= v0
; v
<= v1
; ++v
) {
1639 GLfloat V
= v
- tex_v
;
1640 GLfloat dq
= A
* (2 * U
+ 1) + B
* V
;
1641 GLfloat q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
1644 for (u
= u0
; u
<= u1
; ++u
) {
1645 /* Note that the ellipse has been pre-scaled so F = WEIGHT_LUT_SIZE - 1 */
1646 if (q
< WEIGHT_LUT_SIZE
) {
1647 /* as a LUT is used, q must never be negative;
1648 * should not happen, though
1650 const GLint qClamped
= q
>= 0.0F
? q
: 0;
1651 GLfloat weight
= weightLut
[qClamped
];
1653 newCoord
[0] = u
/ ((GLfloat
) img
->Width2
);
1654 newCoord
[1] = v
/ ((GLfloat
) img
->Height2
);
1656 sample_2d_nearest(ctx
, tObj
, img
, newCoord
, rgba
);
1657 num
[0] += weight
* rgba
[0];
1658 num
[1] += weight
* rgba
[1];
1659 num
[2] += weight
* rgba
[2];
1660 num
[3] += weight
* rgba
[3];
1670 /* Reaching this place would mean
1671 * that no pixels intersected the ellipse.
1672 * This should never happen because
1673 * the filter we use always
1674 * intersects at least one pixel.
1681 /* not enough pixels in resampling, resort to direct interpolation */
1682 sample_2d_linear(ctx
, tObj
, img
, texcoord
, rgba
);
1686 rgba
[0] = num
[0] / den
;
1687 rgba
[1] = num
[1] / den
;
1688 rgba
[2] = num
[2] / den
;
1689 rgba
[3] = num
[3] / den
;
1694 * Anisotropic filtering using footprint assembly as outlined in the
1695 * EXT_texture_filter_anisotropic spec:
1696 * http://www.opengl.org/registry/specs/EXT/texture_filter_anisotropic.txt
1697 * Faster than EWA but has less quality (more aliasing effects)
1700 sample_2d_footprint(struct gl_context
*ctx
,
1701 const struct gl_texture_object
*tObj
,
1702 const GLfloat texcoord
[4],
1703 const GLfloat dudx
, const GLfloat dvdx
,
1704 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1707 GLint level
= lod
> 0 ? lod
: 0;
1708 GLfloat scaling
= 1.0F
/ (1 << level
);
1709 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1711 GLfloat ux
= dudx
* scaling
;
1712 GLfloat vx
= dvdx
* scaling
;
1713 GLfloat uy
= dudy
* scaling
;
1714 GLfloat vy
= dvdy
* scaling
;
1716 GLfloat Px2
= ux
* ux
+ vx
* vx
; /* squared length of dx */
1717 GLfloat Py2
= uy
* uy
+ vy
* vy
; /* squared length of dy */
1723 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1724 GLfloat newCoord
[2];
1727 /* Calculate the per anisotropic sample offsets in s,t space. */
1729 numSamples
= ceil(SQRTF(Px2
));
1730 ds
= ux
/ ((GLfloat
) img
->Width2
);
1731 dt
= vx
/ ((GLfloat
) img
->Height2
);
1734 numSamples
= ceil(SQRTF(Py2
));
1735 ds
= uy
/ ((GLfloat
) img
->Width2
);
1736 dt
= vy
/ ((GLfloat
) img
->Height2
);
1739 for (s
= 0; s
<numSamples
; s
++) {
1740 newCoord
[0] = texcoord
[0] + ds
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5);
1741 newCoord
[1] = texcoord
[1] + dt
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5);
1743 sample_2d_linear(ctx
, tObj
, img
, newCoord
, rgba
);
1750 rgba
[0] = num
[0] / numSamples
;
1751 rgba
[1] = num
[1] / numSamples
;
1752 rgba
[2] = num
[2] / numSamples
;
1753 rgba
[3] = num
[3] / numSamples
;
1758 * Returns the index of the specified texture object in the
1759 * gl_context texture unit array.
1761 static INLINE GLuint
1762 texture_unit_index(const struct gl_context
*ctx
,
1763 const struct gl_texture_object
*tObj
)
1765 const GLuint maxUnit
1766 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
1769 /* XXX CoordUnits vs. ImageUnits */
1770 for (u
= 0; u
< maxUnit
; u
++) {
1771 if (ctx
->Texture
.Unit
[u
]._Current
== tObj
)
1775 u
= 0; /* not found, use 1st one; should never happen */
1782 * Sample 2D texture using an anisotropic filter.
1783 * NOTE: the const GLfloat lambda_iso[] parameter does *NOT* contain
1784 * the lambda float array but a "hidden" SWspan struct which is required
1785 * by this function but is not available in the texture_sample_func signature.
1786 * See _swrast_texture_span( struct gl_context *ctx, SWspan *span ) on how
1787 * this function is called.
1790 sample_lambda_2d_aniso(struct gl_context
*ctx
,
1791 const struct gl_texture_object
*tObj
,
1792 GLuint n
, const GLfloat texcoords
[][4],
1793 const GLfloat lambda_iso
[], GLfloat rgba
[][4])
1795 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1796 const GLfloat maxEccentricity
=
1797 tObj
->Sampler
.MaxAnisotropy
* tObj
->Sampler
.MaxAnisotropy
;
1799 /* re-calculate the lambda values so that they are usable with anisotropic
1802 SWspan
*span
= (SWspan
*)lambda_iso
; /* access the "hidden" SWspan struct */
1804 /* based on interpolate_texcoords(struct gl_context *ctx, SWspan *span)
1805 * in swrast/s_span.c
1808 /* find the texture unit index by looking up the current texture object
1809 * from the context list of available texture objects.
1811 const GLuint u
= texture_unit_index(ctx
, tObj
);
1812 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
1815 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
1816 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
1817 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
1818 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
1819 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
1820 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
1821 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
1822 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
1823 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
1825 /* from swrast/s_texcombine.c _swrast_texture_span */
1826 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[u
];
1827 const GLboolean adjustLOD
=
1828 (texUnit
->LodBias
+ tObj
->Sampler
.LodBias
!= 0.0F
)
1829 || (tObj
->Sampler
.MinLod
!= -1000.0 || tObj
->Sampler
.MaxLod
!= 1000.0);
1833 /* on first access create the lookup table containing the filter weights. */
1835 create_filter_table();
1838 texW
= tImg
->WidthScale
;
1839 texH
= tImg
->HeightScale
;
1841 for (i
= 0; i
< n
; i
++) {
1842 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
1844 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
1845 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
1846 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
1847 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
1849 /* note: instead of working with Px and Py, we will use the
1850 * squared length instead, to avoid sqrt.
1852 GLfloat Px2
= dudx
* dudx
+ dvdx
* dvdx
;
1853 GLfloat Py2
= dudy
* dudy
+ dvdy
* dvdy
;
1873 /* if the eccentricity of the ellipse is too big, scale up the shorter
1874 * of the two vectors to limit the maximum amount of work per pixel
1877 if (e
> maxEccentricity
) {
1878 /* GLfloat s=e / maxEccentricity;
1882 Pmin2
= Pmax2
/ maxEccentricity
;
1885 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
1886 * this since 0.5*log(x) = log(sqrt(x))
1888 lod
= 0.5 * LOG2(Pmin2
);
1891 /* from swrast/s_texcombine.c _swrast_texture_span */
1892 if (texUnit
->LodBias
+ tObj
->Sampler
.LodBias
!= 0.0F
) {
1893 /* apply LOD bias, but don't clamp yet */
1894 const GLfloat bias
=
1895 CLAMP(texUnit
->LodBias
+ tObj
->Sampler
.LodBias
,
1896 -ctx
->Const
.MaxTextureLodBias
,
1897 ctx
->Const
.MaxTextureLodBias
);
1900 if (tObj
->Sampler
.MinLod
!= -1000.0 ||
1901 tObj
->Sampler
.MaxLod
!= 1000.0) {
1902 /* apply LOD clamping to lambda */
1903 lod
= CLAMP(lod
, tObj
->Sampler
.MinLod
, tObj
->Sampler
.MaxLod
);
1908 /* If the ellipse covers the whole image, we can
1909 * simply return the average of the whole image.
1911 if (lod
>= tObj
->_MaxLevel
) {
1912 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1913 texcoords
[i
], rgba
[i
]);
1916 /* don't bother interpolating between multiple LODs; it doesn't
1917 * seem to be worth the extra running time.
1919 sample_2d_ewa(ctx
, tObj
, texcoords
[i
],
1920 dudx
, dvdx
, dudy
, dvdy
, floor(lod
), rgba
[i
]);
1923 (void) sample_2d_footprint
;
1925 sample_2d_footprint(ctx, tObj, texcoords[i],
1926 dudx, dvdx, dudy, dvdy, floor(lod), rgba[i]);
1934 /**********************************************************************/
1935 /* 3-D Texture Sampling Functions */
1936 /**********************************************************************/
1939 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1942 sample_3d_nearest(struct gl_context
*ctx
,
1943 const struct gl_texture_object
*tObj
,
1944 const struct gl_texture_image
*img
,
1945 const GLfloat texcoord
[4],
1948 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1949 const GLint width
= img
->Width2
; /* without border, power of two */
1950 const GLint height
= img
->Height2
; /* without border, power of two */
1951 const GLint depth
= img
->Depth2
; /* without border, power of two */
1955 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
1956 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
1957 k
= nearest_texel_location(tObj
->Sampler
.WrapR
, img
, depth
, texcoord
[2]);
1959 if (i
< 0 || i
>= (GLint
) img
->Width
||
1960 j
< 0 || j
>= (GLint
) img
->Height
||
1961 k
< 0 || k
>= (GLint
) img
->Depth
) {
1962 /* Need this test for GL_CLAMP_TO_BORDER mode */
1963 get_border_color(tObj
, img
, rgba
);
1966 swImg
->FetchTexelf(swImg
, i
, j
, k
, rgba
);
1972 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1975 sample_3d_linear(struct gl_context
*ctx
,
1976 const struct gl_texture_object
*tObj
,
1977 const struct gl_texture_image
*img
,
1978 const GLfloat texcoord
[4],
1981 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1982 const GLint width
= img
->Width2
;
1983 const GLint height
= img
->Height2
;
1984 const GLint depth
= img
->Depth2
;
1985 GLint i0
, j0
, k0
, i1
, j1
, k1
;
1986 GLbitfield useBorderColor
= 0x0;
1988 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
1989 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
1991 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1992 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1993 linear_texel_locations(tObj
->Sampler
.WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
2004 /* check if sampling texture border color */
2005 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2006 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2007 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2008 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2009 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
2010 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
2014 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
2015 get_border_color(tObj
, img
, t000
);
2018 swImg
->FetchTexelf(swImg
, i0
, j0
, k0
, t000
);
2020 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
2021 get_border_color(tObj
, img
, t100
);
2024 swImg
->FetchTexelf(swImg
, i1
, j0
, k0
, t100
);
2026 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
2027 get_border_color(tObj
, img
, t010
);
2030 swImg
->FetchTexelf(swImg
, i0
, j1
, k0
, t010
);
2032 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
2033 get_border_color(tObj
, img
, t110
);
2036 swImg
->FetchTexelf(swImg
, i1
, j1
, k0
, t110
);
2039 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
2040 get_border_color(tObj
, img
, t001
);
2043 swImg
->FetchTexelf(swImg
, i0
, j0
, k1
, t001
);
2045 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
2046 get_border_color(tObj
, img
, t101
);
2049 swImg
->FetchTexelf(swImg
, i1
, j0
, k1
, t101
);
2051 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
2052 get_border_color(tObj
, img
, t011
);
2055 swImg
->FetchTexelf(swImg
, i0
, j1
, k1
, t011
);
2057 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
2058 get_border_color(tObj
, img
, t111
);
2061 swImg
->FetchTexelf(swImg
, i1
, j1
, k1
, t111
);
2064 /* trilinear interpolation of samples */
2065 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
2070 sample_3d_nearest_mipmap_nearest(struct gl_context
*ctx
,
2071 const struct gl_texture_object
*tObj
,
2072 GLuint n
, const GLfloat texcoord
[][4],
2073 const GLfloat lambda
[], GLfloat rgba
[][4] )
2076 for (i
= 0; i
< n
; i
++) {
2077 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2078 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2084 sample_3d_linear_mipmap_nearest(struct gl_context
*ctx
,
2085 const struct gl_texture_object
*tObj
,
2086 GLuint n
, const GLfloat texcoord
[][4],
2087 const GLfloat lambda
[], GLfloat rgba
[][4])
2090 ASSERT(lambda
!= NULL
);
2091 for (i
= 0; i
< n
; i
++) {
2092 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2093 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2099 sample_3d_nearest_mipmap_linear(struct gl_context
*ctx
,
2100 const struct gl_texture_object
*tObj
,
2101 GLuint n
, const GLfloat texcoord
[][4],
2102 const GLfloat lambda
[], GLfloat rgba
[][4])
2105 ASSERT(lambda
!= NULL
);
2106 for (i
= 0; i
< n
; i
++) {
2107 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2108 if (level
>= tObj
->_MaxLevel
) {
2109 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2110 texcoord
[i
], rgba
[i
]);
2113 GLfloat t0
[4], t1
[4]; /* texels */
2114 const GLfloat f
= FRAC(lambda
[i
]);
2115 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2116 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2117 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2124 sample_3d_linear_mipmap_linear(struct gl_context
*ctx
,
2125 const struct gl_texture_object
*tObj
,
2126 GLuint n
, const GLfloat texcoord
[][4],
2127 const GLfloat lambda
[], GLfloat rgba
[][4])
2130 ASSERT(lambda
!= NULL
);
2131 for (i
= 0; i
< n
; i
++) {
2132 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2133 if (level
>= tObj
->_MaxLevel
) {
2134 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2135 texcoord
[i
], rgba
[i
]);
2138 GLfloat t0
[4], t1
[4]; /* texels */
2139 const GLfloat f
= FRAC(lambda
[i
]);
2140 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2141 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2142 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2148 /** Sample 3D texture, nearest filtering for both min/magnification */
2150 sample_nearest_3d(struct gl_context
*ctx
,
2151 const struct gl_texture_object
*tObj
, GLuint n
,
2152 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2156 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2158 for (i
= 0; i
< n
; i
++) {
2159 sample_3d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2164 /** Sample 3D texture, linear filtering for both min/magnification */
2166 sample_linear_3d(struct gl_context
*ctx
,
2167 const struct gl_texture_object
*tObj
, GLuint n
,
2168 const GLfloat texcoords
[][4],
2169 const GLfloat lambda
[], GLfloat rgba
[][4])
2172 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2174 for (i
= 0; i
< n
; i
++) {
2175 sample_3d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2180 /** Sample 3D texture, using lambda to choose between min/magnification */
2182 sample_lambda_3d(struct gl_context
*ctx
,
2183 const struct gl_texture_object
*tObj
, GLuint n
,
2184 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2187 GLuint minStart
, minEnd
; /* texels with minification */
2188 GLuint magStart
, magEnd
; /* texels with magnification */
2191 ASSERT(lambda
!= NULL
);
2192 compute_min_mag_ranges(tObj
, n
, lambda
,
2193 &minStart
, &minEnd
, &magStart
, &magEnd
);
2195 if (minStart
< minEnd
) {
2196 /* do the minified texels */
2197 GLuint m
= minEnd
- minStart
;
2198 switch (tObj
->Sampler
.MinFilter
) {
2200 for (i
= minStart
; i
< minEnd
; i
++)
2201 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2202 texcoords
[i
], rgba
[i
]);
2205 for (i
= minStart
; i
< minEnd
; i
++)
2206 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2207 texcoords
[i
], rgba
[i
]);
2209 case GL_NEAREST_MIPMAP_NEAREST
:
2210 sample_3d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2211 lambda
+ minStart
, rgba
+ minStart
);
2213 case GL_LINEAR_MIPMAP_NEAREST
:
2214 sample_3d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2215 lambda
+ minStart
, rgba
+ minStart
);
2217 case GL_NEAREST_MIPMAP_LINEAR
:
2218 sample_3d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2219 lambda
+ minStart
, rgba
+ minStart
);
2221 case GL_LINEAR_MIPMAP_LINEAR
:
2222 sample_3d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2223 lambda
+ minStart
, rgba
+ minStart
);
2226 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
2231 if (magStart
< magEnd
) {
2232 /* do the magnified texels */
2233 switch (tObj
->Sampler
.MagFilter
) {
2235 for (i
= magStart
; i
< magEnd
; i
++)
2236 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2237 texcoords
[i
], rgba
[i
]);
2240 for (i
= magStart
; i
< magEnd
; i
++)
2241 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2242 texcoords
[i
], rgba
[i
]);
2245 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
2252 /**********************************************************************/
2253 /* Texture Cube Map Sampling Functions */
2254 /**********************************************************************/
2257 * Choose one of six sides of a texture cube map given the texture
2258 * coord (rx,ry,rz). Return pointer to corresponding array of texture
2261 static const struct gl_texture_image
**
2262 choose_cube_face(const struct gl_texture_object
*texObj
,
2263 const GLfloat texcoord
[4], GLfloat newCoord
[4])
2267 direction target sc tc ma
2268 ---------- ------------------------------- --- --- ---
2269 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2270 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2271 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2272 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2273 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2274 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2276 const GLfloat rx
= texcoord
[0];
2277 const GLfloat ry
= texcoord
[1];
2278 const GLfloat rz
= texcoord
[2];
2279 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
2283 if (arx
>= ary
&& arx
>= arz
) {
2297 else if (ary
>= arx
&& ary
>= arz
) {
2327 const float ima
= 1.0F
/ ma
;
2328 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
2329 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
2332 return (const struct gl_texture_image
**) texObj
->Image
[face
];
2337 sample_nearest_cube(struct gl_context
*ctx
,
2338 const struct gl_texture_object
*tObj
, GLuint n
,
2339 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2344 for (i
= 0; i
< n
; i
++) {
2345 const struct gl_texture_image
**images
;
2346 GLfloat newCoord
[4];
2347 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2348 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
2355 sample_linear_cube(struct gl_context
*ctx
,
2356 const struct gl_texture_object
*tObj
, GLuint n
,
2357 const GLfloat texcoords
[][4],
2358 const GLfloat lambda
[], GLfloat rgba
[][4])
2362 for (i
= 0; i
< n
; i
++) {
2363 const struct gl_texture_image
**images
;
2364 GLfloat newCoord
[4];
2365 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2366 sample_2d_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
2373 sample_cube_nearest_mipmap_nearest(struct gl_context
*ctx
,
2374 const struct gl_texture_object
*tObj
,
2375 GLuint n
, const GLfloat texcoord
[][4],
2376 const GLfloat lambda
[], GLfloat rgba
[][4])
2379 ASSERT(lambda
!= NULL
);
2380 for (i
= 0; i
< n
; i
++) {
2381 const struct gl_texture_image
**images
;
2382 GLfloat newCoord
[4];
2384 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2386 /* XXX we actually need to recompute lambda here based on the newCoords.
2387 * But we would need the texcoords of adjacent fragments to compute that
2388 * properly, and we don't have those here.
2389 * For now, do an approximation: subtracting 1 from the chosen mipmap
2390 * level seems to work in some test cases.
2391 * The same adjustment is done in the next few functions.
2393 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2394 level
= MAX2(level
- 1, 0);
2396 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2402 sample_cube_linear_mipmap_nearest(struct gl_context
*ctx
,
2403 const struct gl_texture_object
*tObj
,
2404 GLuint n
, const GLfloat texcoord
[][4],
2405 const GLfloat lambda
[], GLfloat rgba
[][4])
2408 ASSERT(lambda
!= NULL
);
2409 for (i
= 0; i
< n
; i
++) {
2410 const struct gl_texture_image
**images
;
2411 GLfloat newCoord
[4];
2412 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2413 level
= MAX2(level
- 1, 0); /* see comment above */
2414 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2415 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2421 sample_cube_nearest_mipmap_linear(struct gl_context
*ctx
,
2422 const struct gl_texture_object
*tObj
,
2423 GLuint n
, const GLfloat texcoord
[][4],
2424 const GLfloat lambda
[], GLfloat rgba
[][4])
2427 ASSERT(lambda
!= NULL
);
2428 for (i
= 0; i
< n
; i
++) {
2429 const struct gl_texture_image
**images
;
2430 GLfloat newCoord
[4];
2431 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2432 level
= MAX2(level
- 1, 0); /* see comment above */
2433 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2434 if (level
>= tObj
->_MaxLevel
) {
2435 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2439 GLfloat t0
[4], t1
[4]; /* texels */
2440 const GLfloat f
= FRAC(lambda
[i
]);
2441 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
2442 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2443 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2450 sample_cube_linear_mipmap_linear(struct gl_context
*ctx
,
2451 const struct gl_texture_object
*tObj
,
2452 GLuint n
, const GLfloat texcoord
[][4],
2453 const GLfloat lambda
[], GLfloat rgba
[][4])
2456 ASSERT(lambda
!= NULL
);
2457 for (i
= 0; i
< n
; i
++) {
2458 const struct gl_texture_image
**images
;
2459 GLfloat newCoord
[4];
2460 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2461 level
= MAX2(level
- 1, 0); /* see comment above */
2462 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2463 if (level
>= tObj
->_MaxLevel
) {
2464 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2468 GLfloat t0
[4], t1
[4];
2469 const GLfloat f
= FRAC(lambda
[i
]);
2470 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
2471 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2472 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2478 /** Sample cube texture, using lambda to choose between min/magnification */
2480 sample_lambda_cube(struct gl_context
*ctx
,
2481 const struct gl_texture_object
*tObj
, GLuint n
,
2482 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2485 GLuint minStart
, minEnd
; /* texels with minification */
2486 GLuint magStart
, magEnd
; /* texels with magnification */
2488 ASSERT(lambda
!= NULL
);
2489 compute_min_mag_ranges(tObj
, n
, lambda
,
2490 &minStart
, &minEnd
, &magStart
, &magEnd
);
2492 if (minStart
< minEnd
) {
2493 /* do the minified texels */
2494 const GLuint m
= minEnd
- minStart
;
2495 switch (tObj
->Sampler
.MinFilter
) {
2497 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2498 lambda
+ minStart
, rgba
+ minStart
);
2501 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2502 lambda
+ minStart
, rgba
+ minStart
);
2504 case GL_NEAREST_MIPMAP_NEAREST
:
2505 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
2506 texcoords
+ minStart
,
2507 lambda
+ minStart
, rgba
+ minStart
);
2509 case GL_LINEAR_MIPMAP_NEAREST
:
2510 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
2511 texcoords
+ minStart
,
2512 lambda
+ minStart
, rgba
+ minStart
);
2514 case GL_NEAREST_MIPMAP_LINEAR
:
2515 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
2516 texcoords
+ minStart
,
2517 lambda
+ minStart
, rgba
+ minStart
);
2519 case GL_LINEAR_MIPMAP_LINEAR
:
2520 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
2521 texcoords
+ minStart
,
2522 lambda
+ minStart
, rgba
+ minStart
);
2525 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2529 if (magStart
< magEnd
) {
2530 /* do the magnified texels */
2531 const GLuint m
= magEnd
- magStart
;
2532 switch (tObj
->Sampler
.MagFilter
) {
2534 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2535 lambda
+ magStart
, rgba
+ magStart
);
2538 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2539 lambda
+ magStart
, rgba
+ magStart
);
2542 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2548 /**********************************************************************/
2549 /* Texture Rectangle Sampling Functions */
2550 /**********************************************************************/
2554 sample_nearest_rect(struct gl_context
*ctx
,
2555 const struct gl_texture_object
*tObj
, GLuint n
,
2556 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2559 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2560 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2561 const GLint width
= img
->Width
;
2562 const GLint height
= img
->Height
;
2568 ASSERT(tObj
->Sampler
.WrapS
== GL_CLAMP
||
2569 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_EDGE
||
2570 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_BORDER
);
2571 ASSERT(tObj
->Sampler
.WrapT
== GL_CLAMP
||
2572 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_EDGE
||
2573 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_BORDER
);
2575 for (i
= 0; i
< n
; i
++) {
2577 col
= clamp_rect_coord_nearest(tObj
->Sampler
.WrapS
, texcoords
[i
][0], width
);
2578 row
= clamp_rect_coord_nearest(tObj
->Sampler
.WrapT
, texcoords
[i
][1], height
);
2579 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2580 get_border_color(tObj
, img
, rgba
[i
]);
2582 swImg
->FetchTexelf(swImg
, col
, row
, 0, rgba
[i
]);
2588 sample_linear_rect(struct gl_context
*ctx
,
2589 const struct gl_texture_object
*tObj
, GLuint n
,
2590 const GLfloat texcoords
[][4],
2591 const GLfloat lambda
[], GLfloat rgba
[][4])
2593 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2594 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2595 const GLint width
= img
->Width
;
2596 const GLint height
= img
->Height
;
2602 ASSERT(tObj
->Sampler
.WrapS
== GL_CLAMP
||
2603 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_EDGE
||
2604 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_BORDER
);
2605 ASSERT(tObj
->Sampler
.WrapT
== GL_CLAMP
||
2606 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_EDGE
||
2607 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_BORDER
);
2609 for (i
= 0; i
< n
; i
++) {
2610 GLint i0
, j0
, i1
, j1
;
2611 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2613 GLbitfield useBorderColor
= 0x0;
2615 clamp_rect_coord_linear(tObj
->Sampler
.WrapS
, texcoords
[i
][0], width
,
2617 clamp_rect_coord_linear(tObj
->Sampler
.WrapT
, texcoords
[i
][1], height
,
2620 /* compute integer rows/columns */
2621 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2622 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2623 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2624 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2626 /* get four texel samples */
2627 if (useBorderColor
& (I0BIT
| J0BIT
))
2628 get_border_color(tObj
, img
, t00
);
2630 swImg
->FetchTexelf(swImg
, i0
, j0
, 0, t00
);
2632 if (useBorderColor
& (I1BIT
| J0BIT
))
2633 get_border_color(tObj
, img
, t10
);
2635 swImg
->FetchTexelf(swImg
, i1
, j0
, 0, t10
);
2637 if (useBorderColor
& (I0BIT
| J1BIT
))
2638 get_border_color(tObj
, img
, t01
);
2640 swImg
->FetchTexelf(swImg
, i0
, j1
, 0, t01
);
2642 if (useBorderColor
& (I1BIT
| J1BIT
))
2643 get_border_color(tObj
, img
, t11
);
2645 swImg
->FetchTexelf(swImg
, i1
, j1
, 0, t11
);
2647 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2652 /** Sample Rect texture, using lambda to choose between min/magnification */
2654 sample_lambda_rect(struct gl_context
*ctx
,
2655 const struct gl_texture_object
*tObj
, GLuint n
,
2656 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2659 GLuint minStart
, minEnd
, magStart
, magEnd
;
2661 /* We only need lambda to decide between minification and magnification.
2662 * There is no mipmapping with rectangular textures.
2664 compute_min_mag_ranges(tObj
, n
, lambda
,
2665 &minStart
, &minEnd
, &magStart
, &magEnd
);
2667 if (minStart
< minEnd
) {
2668 if (tObj
->Sampler
.MinFilter
== GL_NEAREST
) {
2669 sample_nearest_rect(ctx
, tObj
, minEnd
- minStart
,
2670 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2673 sample_linear_rect(ctx
, tObj
, minEnd
- minStart
,
2674 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2677 if (magStart
< magEnd
) {
2678 if (tObj
->Sampler
.MagFilter
== GL_NEAREST
) {
2679 sample_nearest_rect(ctx
, tObj
, magEnd
- magStart
,
2680 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2683 sample_linear_rect(ctx
, tObj
, magEnd
- magStart
,
2684 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2690 /**********************************************************************/
2691 /* 2D Texture Array Sampling Functions */
2692 /**********************************************************************/
2695 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2698 sample_2d_array_nearest(struct gl_context
*ctx
,
2699 const struct gl_texture_object
*tObj
,
2700 const struct gl_texture_image
*img
,
2701 const GLfloat texcoord
[4],
2704 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2705 const GLint width
= img
->Width2
; /* without border, power of two */
2706 const GLint height
= img
->Height2
; /* without border, power of two */
2707 const GLint depth
= img
->Depth
;
2712 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
2713 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
2714 array
= tex_array_slice(texcoord
[2], depth
);
2716 if (i
< 0 || i
>= (GLint
) img
->Width
||
2717 j
< 0 || j
>= (GLint
) img
->Height
||
2718 array
< 0 || array
>= (GLint
) img
->Depth
) {
2719 /* Need this test for GL_CLAMP_TO_BORDER mode */
2720 get_border_color(tObj
, img
, rgba
);
2723 swImg
->FetchTexelf(swImg
, i
, j
, array
, rgba
);
2729 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2732 sample_2d_array_linear(struct gl_context
*ctx
,
2733 const struct gl_texture_object
*tObj
,
2734 const struct gl_texture_image
*img
,
2735 const GLfloat texcoord
[4],
2738 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2739 const GLint width
= img
->Width2
;
2740 const GLint height
= img
->Height2
;
2741 const GLint depth
= img
->Depth
;
2742 GLint i0
, j0
, i1
, j1
;
2744 GLbitfield useBorderColor
= 0x0;
2746 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2748 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2749 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2750 array
= tex_array_slice(texcoord
[2], depth
);
2752 if (array
< 0 || array
>= depth
) {
2753 COPY_4V(rgba
, tObj
->Sampler
.BorderColor
.f
);
2763 /* check if sampling texture border color */
2764 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2765 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2766 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2767 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2771 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2772 get_border_color(tObj
, img
, t00
);
2775 swImg
->FetchTexelf(swImg
, i0
, j0
, array
, t00
);
2777 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2778 get_border_color(tObj
, img
, t10
);
2781 swImg
->FetchTexelf(swImg
, i1
, j0
, array
, t10
);
2783 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2784 get_border_color(tObj
, img
, t01
);
2787 swImg
->FetchTexelf(swImg
, i0
, j1
, array
, t01
);
2789 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2790 get_border_color(tObj
, img
, t11
);
2793 swImg
->FetchTexelf(swImg
, i1
, j1
, array
, t11
);
2796 /* trilinear interpolation of samples */
2797 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2803 sample_2d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2804 const struct gl_texture_object
*tObj
,
2805 GLuint n
, const GLfloat texcoord
[][4],
2806 const GLfloat lambda
[], GLfloat rgba
[][4])
2809 for (i
= 0; i
< n
; i
++) {
2810 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2811 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2818 sample_2d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2819 const struct gl_texture_object
*tObj
,
2820 GLuint n
, const GLfloat texcoord
[][4],
2821 const GLfloat lambda
[], GLfloat rgba
[][4])
2824 ASSERT(lambda
!= NULL
);
2825 for (i
= 0; i
< n
; i
++) {
2826 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2827 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2828 texcoord
[i
], rgba
[i
]);
2834 sample_2d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2835 const struct gl_texture_object
*tObj
,
2836 GLuint n
, const GLfloat texcoord
[][4],
2837 const GLfloat lambda
[], GLfloat rgba
[][4])
2840 ASSERT(lambda
!= NULL
);
2841 for (i
= 0; i
< n
; i
++) {
2842 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2843 if (level
>= tObj
->_MaxLevel
) {
2844 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2845 texcoord
[i
], rgba
[i
]);
2848 GLfloat t0
[4], t1
[4]; /* texels */
2849 const GLfloat f
= FRAC(lambda
[i
]);
2850 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
],
2852 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1],
2854 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2861 sample_2d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2862 const struct gl_texture_object
*tObj
,
2863 GLuint n
, const GLfloat texcoord
[][4],
2864 const GLfloat lambda
[], GLfloat rgba
[][4])
2867 ASSERT(lambda
!= NULL
);
2868 for (i
= 0; i
< n
; i
++) {
2869 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2870 if (level
>= tObj
->_MaxLevel
) {
2871 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2872 texcoord
[i
], rgba
[i
]);
2875 GLfloat t0
[4], t1
[4]; /* texels */
2876 const GLfloat f
= FRAC(lambda
[i
]);
2877 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2879 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1],
2881 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2887 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2889 sample_nearest_2d_array(struct gl_context
*ctx
,
2890 const struct gl_texture_object
*tObj
, GLuint n
,
2891 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2895 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2897 for (i
= 0; i
< n
; i
++) {
2898 sample_2d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2904 /** Sample 2D Array texture, linear filtering for both min/magnification */
2906 sample_linear_2d_array(struct gl_context
*ctx
,
2907 const struct gl_texture_object
*tObj
, GLuint n
,
2908 const GLfloat texcoords
[][4],
2909 const GLfloat lambda
[], GLfloat rgba
[][4])
2912 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2914 for (i
= 0; i
< n
; i
++) {
2915 sample_2d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2920 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2922 sample_lambda_2d_array(struct gl_context
*ctx
,
2923 const struct gl_texture_object
*tObj
, GLuint n
,
2924 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2927 GLuint minStart
, minEnd
; /* texels with minification */
2928 GLuint magStart
, magEnd
; /* texels with magnification */
2931 ASSERT(lambda
!= NULL
);
2932 compute_min_mag_ranges(tObj
, n
, lambda
,
2933 &minStart
, &minEnd
, &magStart
, &magEnd
);
2935 if (minStart
< minEnd
) {
2936 /* do the minified texels */
2937 GLuint m
= minEnd
- minStart
;
2938 switch (tObj
->Sampler
.MinFilter
) {
2940 for (i
= minStart
; i
< minEnd
; i
++)
2941 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2942 texcoords
[i
], rgba
[i
]);
2945 for (i
= minStart
; i
< minEnd
; i
++)
2946 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2947 texcoords
[i
], rgba
[i
]);
2949 case GL_NEAREST_MIPMAP_NEAREST
:
2950 sample_2d_array_nearest_mipmap_nearest(ctx
, tObj
, m
,
2951 texcoords
+ minStart
,
2955 case GL_LINEAR_MIPMAP_NEAREST
:
2956 sample_2d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2957 texcoords
+ minStart
,
2961 case GL_NEAREST_MIPMAP_LINEAR
:
2962 sample_2d_array_nearest_mipmap_linear(ctx
, tObj
, m
,
2963 texcoords
+ minStart
,
2967 case GL_LINEAR_MIPMAP_LINEAR
:
2968 sample_2d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2969 texcoords
+ minStart
,
2974 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
2979 if (magStart
< magEnd
) {
2980 /* do the magnified texels */
2981 switch (tObj
->Sampler
.MagFilter
) {
2983 for (i
= magStart
; i
< magEnd
; i
++)
2984 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2985 texcoords
[i
], rgba
[i
]);
2988 for (i
= magStart
; i
< magEnd
; i
++)
2989 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2990 texcoords
[i
], rgba
[i
]);
2993 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
3002 /**********************************************************************/
3003 /* 1D Texture Array Sampling Functions */
3004 /**********************************************************************/
3007 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
3010 sample_1d_array_nearest(struct gl_context
*ctx
,
3011 const struct gl_texture_object
*tObj
,
3012 const struct gl_texture_image
*img
,
3013 const GLfloat texcoord
[4],
3016 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3017 const GLint width
= img
->Width2
; /* without border, power of two */
3018 const GLint height
= img
->Height
;
3023 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
3024 array
= tex_array_slice(texcoord
[1], height
);
3026 if (i
< 0 || i
>= (GLint
) img
->Width
||
3027 array
< 0 || array
>= (GLint
) img
->Height
) {
3028 /* Need this test for GL_CLAMP_TO_BORDER mode */
3029 get_border_color(tObj
, img
, rgba
);
3032 swImg
->FetchTexelf(swImg
, i
, array
, 0, rgba
);
3038 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
3041 sample_1d_array_linear(struct gl_context
*ctx
,
3042 const struct gl_texture_object
*tObj
,
3043 const struct gl_texture_image
*img
,
3044 const GLfloat texcoord
[4],
3047 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3048 const GLint width
= img
->Width2
;
3049 const GLint height
= img
->Height
;
3052 GLbitfield useBorderColor
= 0x0;
3054 GLfloat t0
[4], t1
[4];
3056 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
3057 array
= tex_array_slice(texcoord
[1], height
);
3064 /* check if sampling texture border color */
3065 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
3066 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
3069 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
3072 if (useBorderColor
& (I0BIT
| K0BIT
)) {
3073 get_border_color(tObj
, img
, t0
);
3076 swImg
->FetchTexelf(swImg
, i0
, array
, 0, t0
);
3078 if (useBorderColor
& (I1BIT
| K0BIT
)) {
3079 get_border_color(tObj
, img
, t1
);
3082 swImg
->FetchTexelf(swImg
, i1
, array
, 0, t1
);
3085 /* bilinear interpolation of samples */
3086 lerp_rgba(rgba
, a
, t0
, t1
);
3091 sample_1d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
3092 const struct gl_texture_object
*tObj
,
3093 GLuint n
, const GLfloat texcoord
[][4],
3094 const GLfloat lambda
[], GLfloat rgba
[][4])
3097 for (i
= 0; i
< n
; i
++) {
3098 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3099 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
3106 sample_1d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
3107 const struct gl_texture_object
*tObj
,
3108 GLuint n
, const GLfloat texcoord
[][4],
3109 const GLfloat lambda
[], GLfloat rgba
[][4])
3112 ASSERT(lambda
!= NULL
);
3113 for (i
= 0; i
< n
; i
++) {
3114 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3115 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
3116 texcoord
[i
], rgba
[i
]);
3122 sample_1d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
3123 const struct gl_texture_object
*tObj
,
3124 GLuint n
, const GLfloat texcoord
[][4],
3125 const GLfloat lambda
[], GLfloat rgba
[][4])
3128 ASSERT(lambda
!= NULL
);
3129 for (i
= 0; i
< n
; i
++) {
3130 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3131 if (level
>= tObj
->_MaxLevel
) {
3132 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
3133 texcoord
[i
], rgba
[i
]);
3136 GLfloat t0
[4], t1
[4]; /* texels */
3137 const GLfloat f
= FRAC(lambda
[i
]);
3138 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3139 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3140 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3147 sample_1d_array_linear_mipmap_linear(struct gl_context
*ctx
,
3148 const struct gl_texture_object
*tObj
,
3149 GLuint n
, const GLfloat texcoord
[][4],
3150 const GLfloat lambda
[], GLfloat rgba
[][4])
3153 ASSERT(lambda
!= NULL
);
3154 for (i
= 0; i
< n
; i
++) {
3155 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3156 if (level
>= tObj
->_MaxLevel
) {
3157 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
3158 texcoord
[i
], rgba
[i
]);
3161 GLfloat t0
[4], t1
[4]; /* texels */
3162 const GLfloat f
= FRAC(lambda
[i
]);
3163 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3164 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3165 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3171 /** Sample 1D Array texture, nearest filtering for both min/magnification */
3173 sample_nearest_1d_array(struct gl_context
*ctx
,
3174 const struct gl_texture_object
*tObj
, GLuint n
,
3175 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3179 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3181 for (i
= 0; i
< n
; i
++) {
3182 sample_1d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
3187 /** Sample 1D Array texture, linear filtering for both min/magnification */
3189 sample_linear_1d_array(struct gl_context
*ctx
,
3190 const struct gl_texture_object
*tObj
, GLuint n
,
3191 const GLfloat texcoords
[][4],
3192 const GLfloat lambda
[], GLfloat rgba
[][4])
3195 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3197 for (i
= 0; i
< n
; i
++) {
3198 sample_1d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
3203 /** Sample 1D Array texture, using lambda to choose between min/magnification */
3205 sample_lambda_1d_array(struct gl_context
*ctx
,
3206 const struct gl_texture_object
*tObj
, GLuint n
,
3207 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3210 GLuint minStart
, minEnd
; /* texels with minification */
3211 GLuint magStart
, magEnd
; /* texels with magnification */
3214 ASSERT(lambda
!= NULL
);
3215 compute_min_mag_ranges(tObj
, n
, lambda
,
3216 &minStart
, &minEnd
, &magStart
, &magEnd
);
3218 if (minStart
< minEnd
) {
3219 /* do the minified texels */
3220 GLuint m
= minEnd
- minStart
;
3221 switch (tObj
->Sampler
.MinFilter
) {
3223 for (i
= minStart
; i
< minEnd
; i
++)
3224 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3225 texcoords
[i
], rgba
[i
]);
3228 for (i
= minStart
; i
< minEnd
; i
++)
3229 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3230 texcoords
[i
], rgba
[i
]);
3232 case GL_NEAREST_MIPMAP_NEAREST
:
3233 sample_1d_array_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
3234 lambda
+ minStart
, rgba
+ minStart
);
3236 case GL_LINEAR_MIPMAP_NEAREST
:
3237 sample_1d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
3238 texcoords
+ minStart
,
3242 case GL_NEAREST_MIPMAP_LINEAR
:
3243 sample_1d_array_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
3244 lambda
+ minStart
, rgba
+ minStart
);
3246 case GL_LINEAR_MIPMAP_LINEAR
:
3247 sample_1d_array_linear_mipmap_linear(ctx
, tObj
, m
,
3248 texcoords
+ minStart
,
3253 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
3258 if (magStart
< magEnd
) {
3259 /* do the magnified texels */
3260 switch (tObj
->Sampler
.MagFilter
) {
3262 for (i
= magStart
; i
< magEnd
; i
++)
3263 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3264 texcoords
[i
], rgba
[i
]);
3267 for (i
= magStart
; i
< magEnd
; i
++)
3268 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3269 texcoords
[i
], rgba
[i
]);
3272 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
3280 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
3282 static INLINE GLfloat
3283 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
,
3288 return (coord
<= depthSample
) ? 1.0F
: ambient
;
3290 return (coord
>= depthSample
) ? 1.0F
: ambient
;
3292 return (coord
< depthSample
) ? 1.0F
: ambient
;
3294 return (coord
> depthSample
) ? 1.0F
: ambient
;
3296 return (coord
== depthSample
) ? 1.0F
: ambient
;
3298 return (coord
!= depthSample
) ? 1.0F
: ambient
;
3306 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
3313 * Compare texcoord against four depth samples.
3315 static INLINE GLfloat
3316 shadow_compare4(GLenum function
, GLfloat coord
,
3317 GLfloat depth00
, GLfloat depth01
,
3318 GLfloat depth10
, GLfloat depth11
,
3319 GLfloat ambient
, GLfloat wi
, GLfloat wj
)
3321 const GLfloat d
= (1.0F
- (GLfloat
) ambient
) * 0.25F
;
3322 GLfloat luminance
= 1.0F
;
3326 if (coord
> depth00
) luminance
-= d
;
3327 if (coord
> depth01
) luminance
-= d
;
3328 if (coord
> depth10
) luminance
-= d
;
3329 if (coord
> depth11
) luminance
-= d
;
3332 if (coord
< depth00
) luminance
-= d
;
3333 if (coord
< depth01
) luminance
-= d
;
3334 if (coord
< depth10
) luminance
-= d
;
3335 if (coord
< depth11
) luminance
-= d
;
3338 if (coord
>= depth00
) luminance
-= d
;
3339 if (coord
>= depth01
) luminance
-= d
;
3340 if (coord
>= depth10
) luminance
-= d
;
3341 if (coord
>= depth11
) luminance
-= d
;
3344 if (coord
<= depth00
) luminance
-= d
;
3345 if (coord
<= depth01
) luminance
-= d
;
3346 if (coord
<= depth10
) luminance
-= d
;
3347 if (coord
<= depth11
) luminance
-= d
;
3350 if (coord
!= depth00
) luminance
-= d
;
3351 if (coord
!= depth01
) luminance
-= d
;
3352 if (coord
!= depth10
) luminance
-= d
;
3353 if (coord
!= depth11
) luminance
-= d
;
3356 if (coord
== depth00
) luminance
-= d
;
3357 if (coord
== depth01
) luminance
-= d
;
3358 if (coord
== depth10
) luminance
-= d
;
3359 if (coord
== depth11
) luminance
-= d
;
3366 /* ordinary bilinear filtering */
3367 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
3369 _mesa_problem(NULL
, "Bad compare func in sample_compare4");
3376 * Choose the mipmap level to use when sampling from a depth texture.
3379 choose_depth_texture_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
3383 if (tObj
->Sampler
.MinFilter
== GL_NEAREST
|| tObj
->Sampler
.MinFilter
== GL_LINEAR
) {
3384 /* no mipmapping - use base level */
3385 level
= tObj
->BaseLevel
;
3388 /* choose mipmap level */
3389 lambda
= CLAMP(lambda
, tObj
->Sampler
.MinLod
, tObj
->Sampler
.MaxLod
);
3390 level
= (GLint
) lambda
;
3391 level
= CLAMP(level
, tObj
->BaseLevel
, tObj
->_MaxLevel
);
3399 * Sample a shadow/depth texture. This function is incomplete. It doesn't
3400 * check for minification vs. magnification, etc.
3403 sample_depth_texture( struct gl_context
*ctx
,
3404 const struct gl_texture_object
*tObj
, GLuint n
,
3405 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3406 GLfloat texel
[][4] )
3408 const GLint level
= choose_depth_texture_level(tObj
, lambda
[0]);
3409 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
3410 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3411 const GLint width
= img
->Width
;
3412 const GLint height
= img
->Height
;
3413 const GLint depth
= img
->Depth
;
3414 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
3420 ASSERT(img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
3421 img
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
3423 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
3424 tObj
->Target
== GL_TEXTURE_2D
||
3425 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
3426 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
3427 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
);
3429 ambient
= tObj
->Sampler
.CompareFailValue
;
3431 /* XXXX if tObj->Sampler.MinFilter != tObj->Sampler.MagFilter, we're ignoring lambda */
3433 function
= (tObj
->Sampler
.CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
3434 tObj
->Sampler
.CompareFunc
: GL_NONE
;
3436 if (tObj
->Sampler
.MagFilter
== GL_NEAREST
) {
3438 for (i
= 0; i
< n
; i
++) {
3439 GLfloat depthSample
, depthRef
;
3440 GLint col
, row
, slice
;
3442 nearest_texcoord(tObj
, level
, texcoords
[i
], &col
, &row
, &slice
);
3444 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3445 slice
>= 0 && slice
< depth
) {
3446 swImg
->FetchTexelf(swImg
, col
, row
, slice
, &depthSample
);
3449 depthSample
= tObj
->Sampler
.BorderColor
.f
[0];
3452 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3454 result
= shadow_compare(function
, depthRef
, depthSample
, ambient
);
3456 switch (tObj
->Sampler
.DepthMode
) {
3458 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3461 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3464 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3467 ASSIGN_4V(texel
[i
], result
, 0.0F
, 0.0F
, 1.0F
);
3470 _mesa_problem(ctx
, "Bad depth texture mode");
3476 ASSERT(tObj
->Sampler
.MagFilter
== GL_LINEAR
);
3477 for (i
= 0; i
< n
; i
++) {
3478 GLfloat depth00
, depth01
, depth10
, depth11
, depthRef
;
3479 GLint i0
, i1
, j0
, j1
;
3482 GLuint useBorderTexel
;
3484 linear_texcoord(tObj
, level
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3491 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3497 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3498 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3499 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3500 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3503 if (slice
< 0 || slice
>= (GLint
) depth
) {
3504 depth00
= tObj
->Sampler
.BorderColor
.f
[0];
3505 depth01
= tObj
->Sampler
.BorderColor
.f
[0];
3506 depth10
= tObj
->Sampler
.BorderColor
.f
[0];
3507 depth11
= tObj
->Sampler
.BorderColor
.f
[0];
3510 /* get four depth samples from the texture */
3511 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3512 depth00
= tObj
->Sampler
.BorderColor
.f
[0];
3515 swImg
->FetchTexelf(swImg
, i0
, j0
, slice
, &depth00
);
3517 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3518 depth10
= tObj
->Sampler
.BorderColor
.f
[0];
3521 swImg
->FetchTexelf(swImg
, i1
, j0
, slice
, &depth10
);
3524 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3525 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3526 depth01
= tObj
->Sampler
.BorderColor
.f
[0];
3529 swImg
->FetchTexelf(swImg
, i0
, j1
, slice
, &depth01
);
3531 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3532 depth11
= tObj
->Sampler
.BorderColor
.f
[0];
3535 swImg
->FetchTexelf(swImg
, i1
, j1
, slice
, &depth11
);
3544 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3546 result
= shadow_compare4(function
, depthRef
,
3547 depth00
, depth01
, depth10
, depth11
,
3550 switch (tObj
->Sampler
.DepthMode
) {
3552 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3555 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3558 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3561 _mesa_problem(ctx
, "Bad depth texture mode");
3570 * We use this function when a texture object is in an "incomplete" state.
3571 * When a fragment program attempts to sample an incomplete texture we
3572 * return black (see issue 23 in GL_ARB_fragment_program spec).
3573 * Note: fragment programs don't observe the texture enable/disable flags.
3576 null_sample_func( struct gl_context
*ctx
,
3577 const struct gl_texture_object
*tObj
, GLuint n
,
3578 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3586 for (i
= 0; i
< n
; i
++) {
3590 rgba
[i
][ACOMP
] = 1.0;
3596 * Choose the texture sampling function for the given texture object.
3599 _swrast_choose_texture_sample_func( struct gl_context
*ctx
,
3600 const struct gl_texture_object
*t
)
3602 if (!t
|| !t
->_Complete
) {
3603 return &null_sample_func
;
3606 const GLboolean needLambda
=
3607 (GLboolean
) (t
->Sampler
.MinFilter
!= t
->Sampler
.MagFilter
);
3608 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->_BaseFormat
;
3610 switch (t
->Target
) {
3612 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3613 return &sample_depth_texture
;
3615 else if (needLambda
) {
3616 return &sample_lambda_1d
;
3618 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3619 return &sample_linear_1d
;
3622 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3623 return &sample_nearest_1d
;
3626 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3627 return &sample_depth_texture
;
3629 else if (needLambda
) {
3630 /* Anisotropic filtering extension. Activated only if mipmaps are used */
3631 if (t
->Sampler
.MaxAnisotropy
> 1.0 &&
3632 t
->Sampler
.MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
3633 return &sample_lambda_2d_aniso
;
3635 return &sample_lambda_2d
;
3637 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3638 return &sample_linear_2d
;
3641 /* check for a few optimized cases */
3642 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3643 const struct swrast_texture_image
*swImg
=
3644 swrast_texture_image_const(img
);
3646 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3647 if (t
->Sampler
.WrapS
== GL_REPEAT
&&
3648 t
->Sampler
.WrapT
== GL_REPEAT
&&
3649 swImg
->_IsPowerOfTwo
&&
3651 img
->TexFormat
== MESA_FORMAT_RGB888
) {
3652 return &opt_sample_rgb_2d
;
3654 else if (t
->Sampler
.WrapS
== GL_REPEAT
&&
3655 t
->Sampler
.WrapT
== GL_REPEAT
&&
3656 swImg
->_IsPowerOfTwo
&&
3658 img
->TexFormat
== MESA_FORMAT_RGBA8888
) {
3659 return &opt_sample_rgba_2d
;
3662 return &sample_nearest_2d
;
3667 return &sample_lambda_3d
;
3669 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3670 return &sample_linear_3d
;
3673 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3674 return &sample_nearest_3d
;
3676 case GL_TEXTURE_CUBE_MAP
:
3678 return &sample_lambda_cube
;
3680 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3681 return &sample_linear_cube
;
3684 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3685 return &sample_nearest_cube
;
3687 case GL_TEXTURE_RECTANGLE_NV
:
3688 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3689 return &sample_depth_texture
;
3691 else if (needLambda
) {
3692 return &sample_lambda_rect
;
3694 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3695 return &sample_linear_rect
;
3698 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3699 return &sample_nearest_rect
;
3701 case GL_TEXTURE_1D_ARRAY_EXT
:
3703 return &sample_lambda_1d_array
;
3705 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3706 return &sample_linear_1d_array
;
3709 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3710 return &sample_nearest_1d_array
;
3712 case GL_TEXTURE_2D_ARRAY_EXT
:
3714 return &sample_lambda_2d_array
;
3716 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3717 return &sample_linear_2d_array
;
3720 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3721 return &sample_nearest_2d_array
;
3725 "invalid target in _swrast_choose_texture_sample_func");
3726 return &null_sample_func
;