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 const struct swrast_texture_image
*swImg
=
1589 swrast_texture_image_const(mostDetailedImage
);
1590 GLfloat tex_u
=-0.5 + texcoord
[0] * swImg
->WidthScale
* scaling
;
1591 GLfloat tex_v
=-0.5 + texcoord
[1] * swImg
->HeightScale
* scaling
;
1593 GLfloat ux
= dudx
* scaling
;
1594 GLfloat vx
= dvdx
* scaling
;
1595 GLfloat uy
= dudy
* scaling
;
1596 GLfloat vy
= dvdy
* scaling
;
1598 /* compute ellipse coefficients to bound the region:
1599 * A*x*x + B*x*y + C*y*y = F.
1601 GLfloat A
= vx
*vx
+vy
*vy
+1;
1602 GLfloat B
= -2*(ux
*vx
+uy
*vy
);
1603 GLfloat C
= ux
*ux
+uy
*uy
+1;
1604 GLfloat F
= A
*C
-B
*B
/4.0;
1606 /* check if it is an ellipse */
1607 /* ASSERT(F > 0.0); */
1609 /* Compute the ellipse's (u,v) bounding box in texture space */
1610 GLfloat d
= -B
*B
+4.0*C
*A
;
1611 GLfloat box_u
= 2.0 / d
* sqrt(d
*C
*F
); /* box_u -> half of bbox with */
1612 GLfloat box_v
= 2.0 / d
* sqrt(A
*d
*F
); /* box_v -> half of bbox height */
1614 GLint u0
= floor(tex_u
- box_u
);
1615 GLint u1
= ceil (tex_u
+ box_u
);
1616 GLint v0
= floor(tex_v
- box_v
);
1617 GLint v1
= ceil (tex_v
+ box_v
);
1619 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1620 GLfloat newCoord
[2];
1623 GLfloat U
= u0
- tex_u
;
1626 /* Scale ellipse formula to directly index the Filter Lookup Table.
1627 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1629 double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
1633 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1635 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1636 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1637 * value, q, is less than F, we're inside the ellipse
1640 for (v
= v0
; v
<= v1
; ++v
) {
1641 GLfloat V
= v
- tex_v
;
1642 GLfloat dq
= A
* (2 * U
+ 1) + B
* V
;
1643 GLfloat q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
1646 for (u
= u0
; u
<= u1
; ++u
) {
1647 /* Note that the ellipse has been pre-scaled so F = WEIGHT_LUT_SIZE - 1 */
1648 if (q
< WEIGHT_LUT_SIZE
) {
1649 /* as a LUT is used, q must never be negative;
1650 * should not happen, though
1652 const GLint qClamped
= q
>= 0.0F
? q
: 0;
1653 GLfloat weight
= weightLut
[qClamped
];
1655 newCoord
[0] = u
/ ((GLfloat
) img
->Width2
);
1656 newCoord
[1] = v
/ ((GLfloat
) img
->Height2
);
1658 sample_2d_nearest(ctx
, tObj
, img
, newCoord
, rgba
);
1659 num
[0] += weight
* rgba
[0];
1660 num
[1] += weight
* rgba
[1];
1661 num
[2] += weight
* rgba
[2];
1662 num
[3] += weight
* rgba
[3];
1672 /* Reaching this place would mean
1673 * that no pixels intersected the ellipse.
1674 * This should never happen because
1675 * the filter we use always
1676 * intersects at least one pixel.
1683 /* not enough pixels in resampling, resort to direct interpolation */
1684 sample_2d_linear(ctx
, tObj
, img
, texcoord
, rgba
);
1688 rgba
[0] = num
[0] / den
;
1689 rgba
[1] = num
[1] / den
;
1690 rgba
[2] = num
[2] / den
;
1691 rgba
[3] = num
[3] / den
;
1696 * Anisotropic filtering using footprint assembly as outlined in the
1697 * EXT_texture_filter_anisotropic spec:
1698 * http://www.opengl.org/registry/specs/EXT/texture_filter_anisotropic.txt
1699 * Faster than EWA but has less quality (more aliasing effects)
1702 sample_2d_footprint(struct gl_context
*ctx
,
1703 const struct gl_texture_object
*tObj
,
1704 const GLfloat texcoord
[4],
1705 const GLfloat dudx
, const GLfloat dvdx
,
1706 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1709 GLint level
= lod
> 0 ? lod
: 0;
1710 GLfloat scaling
= 1.0F
/ (1 << level
);
1711 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1713 GLfloat ux
= dudx
* scaling
;
1714 GLfloat vx
= dvdx
* scaling
;
1715 GLfloat uy
= dudy
* scaling
;
1716 GLfloat vy
= dvdy
* scaling
;
1718 GLfloat Px2
= ux
* ux
+ vx
* vx
; /* squared length of dx */
1719 GLfloat Py2
= uy
* uy
+ vy
* vy
; /* squared length of dy */
1725 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1726 GLfloat newCoord
[2];
1729 /* Calculate the per anisotropic sample offsets in s,t space. */
1731 numSamples
= ceil(SQRTF(Px2
));
1732 ds
= ux
/ ((GLfloat
) img
->Width2
);
1733 dt
= vx
/ ((GLfloat
) img
->Height2
);
1736 numSamples
= ceil(SQRTF(Py2
));
1737 ds
= uy
/ ((GLfloat
) img
->Width2
);
1738 dt
= vy
/ ((GLfloat
) img
->Height2
);
1741 for (s
= 0; s
<numSamples
; s
++) {
1742 newCoord
[0] = texcoord
[0] + ds
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5);
1743 newCoord
[1] = texcoord
[1] + dt
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5);
1745 sample_2d_linear(ctx
, tObj
, img
, newCoord
, rgba
);
1752 rgba
[0] = num
[0] / numSamples
;
1753 rgba
[1] = num
[1] / numSamples
;
1754 rgba
[2] = num
[2] / numSamples
;
1755 rgba
[3] = num
[3] / numSamples
;
1760 * Returns the index of the specified texture object in the
1761 * gl_context texture unit array.
1763 static INLINE GLuint
1764 texture_unit_index(const struct gl_context
*ctx
,
1765 const struct gl_texture_object
*tObj
)
1767 const GLuint maxUnit
1768 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
1771 /* XXX CoordUnits vs. ImageUnits */
1772 for (u
= 0; u
< maxUnit
; u
++) {
1773 if (ctx
->Texture
.Unit
[u
]._Current
== tObj
)
1777 u
= 0; /* not found, use 1st one; should never happen */
1784 * Sample 2D texture using an anisotropic filter.
1785 * NOTE: the const GLfloat lambda_iso[] parameter does *NOT* contain
1786 * the lambda float array but a "hidden" SWspan struct which is required
1787 * by this function but is not available in the texture_sample_func signature.
1788 * See _swrast_texture_span( struct gl_context *ctx, SWspan *span ) on how
1789 * this function is called.
1792 sample_lambda_2d_aniso(struct gl_context
*ctx
,
1793 const struct gl_texture_object
*tObj
,
1794 GLuint n
, const GLfloat texcoords
[][4],
1795 const GLfloat lambda_iso
[], GLfloat rgba
[][4])
1797 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1798 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(tImg
);
1799 const GLfloat maxEccentricity
=
1800 tObj
->Sampler
.MaxAnisotropy
* tObj
->Sampler
.MaxAnisotropy
;
1802 /* re-calculate the lambda values so that they are usable with anisotropic
1805 SWspan
*span
= (SWspan
*)lambda_iso
; /* access the "hidden" SWspan struct */
1807 /* based on interpolate_texcoords(struct gl_context *ctx, SWspan *span)
1808 * in swrast/s_span.c
1811 /* find the texture unit index by looking up the current texture object
1812 * from the context list of available texture objects.
1814 const GLuint u
= texture_unit_index(ctx
, tObj
);
1815 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
1818 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
1819 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
1820 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
1821 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
1822 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
1823 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
1824 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
1825 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
1826 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
1828 /* from swrast/s_texcombine.c _swrast_texture_span */
1829 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[u
];
1830 const GLboolean adjustLOD
=
1831 (texUnit
->LodBias
+ tObj
->Sampler
.LodBias
!= 0.0F
)
1832 || (tObj
->Sampler
.MinLod
!= -1000.0 || tObj
->Sampler
.MaxLod
!= 1000.0);
1836 /* on first access create the lookup table containing the filter weights. */
1838 create_filter_table();
1841 texW
= swImg
->WidthScale
;
1842 texH
= swImg
->HeightScale
;
1844 for (i
= 0; i
< n
; i
++) {
1845 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
1847 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
1848 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
1849 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
1850 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
1852 /* note: instead of working with Px and Py, we will use the
1853 * squared length instead, to avoid sqrt.
1855 GLfloat Px2
= dudx
* dudx
+ dvdx
* dvdx
;
1856 GLfloat Py2
= dudy
* dudy
+ dvdy
* dvdy
;
1876 /* if the eccentricity of the ellipse is too big, scale up the shorter
1877 * of the two vectors to limit the maximum amount of work per pixel
1880 if (e
> maxEccentricity
) {
1881 /* GLfloat s=e / maxEccentricity;
1885 Pmin2
= Pmax2
/ maxEccentricity
;
1888 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
1889 * this since 0.5*log(x) = log(sqrt(x))
1891 lod
= 0.5 * LOG2(Pmin2
);
1894 /* from swrast/s_texcombine.c _swrast_texture_span */
1895 if (texUnit
->LodBias
+ tObj
->Sampler
.LodBias
!= 0.0F
) {
1896 /* apply LOD bias, but don't clamp yet */
1897 const GLfloat bias
=
1898 CLAMP(texUnit
->LodBias
+ tObj
->Sampler
.LodBias
,
1899 -ctx
->Const
.MaxTextureLodBias
,
1900 ctx
->Const
.MaxTextureLodBias
);
1903 if (tObj
->Sampler
.MinLod
!= -1000.0 ||
1904 tObj
->Sampler
.MaxLod
!= 1000.0) {
1905 /* apply LOD clamping to lambda */
1906 lod
= CLAMP(lod
, tObj
->Sampler
.MinLod
, tObj
->Sampler
.MaxLod
);
1911 /* If the ellipse covers the whole image, we can
1912 * simply return the average of the whole image.
1914 if (lod
>= tObj
->_MaxLevel
) {
1915 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1916 texcoords
[i
], rgba
[i
]);
1919 /* don't bother interpolating between multiple LODs; it doesn't
1920 * seem to be worth the extra running time.
1922 sample_2d_ewa(ctx
, tObj
, texcoords
[i
],
1923 dudx
, dvdx
, dudy
, dvdy
, floor(lod
), rgba
[i
]);
1926 (void) sample_2d_footprint
;
1928 sample_2d_footprint(ctx, tObj, texcoords[i],
1929 dudx, dvdx, dudy, dvdy, floor(lod), rgba[i]);
1937 /**********************************************************************/
1938 /* 3-D Texture Sampling Functions */
1939 /**********************************************************************/
1942 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1945 sample_3d_nearest(struct gl_context
*ctx
,
1946 const struct gl_texture_object
*tObj
,
1947 const struct gl_texture_image
*img
,
1948 const GLfloat texcoord
[4],
1951 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1952 const GLint width
= img
->Width2
; /* without border, power of two */
1953 const GLint height
= img
->Height2
; /* without border, power of two */
1954 const GLint depth
= img
->Depth2
; /* without border, power of two */
1958 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
1959 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
1960 k
= nearest_texel_location(tObj
->Sampler
.WrapR
, img
, depth
, texcoord
[2]);
1962 if (i
< 0 || i
>= (GLint
) img
->Width
||
1963 j
< 0 || j
>= (GLint
) img
->Height
||
1964 k
< 0 || k
>= (GLint
) img
->Depth
) {
1965 /* Need this test for GL_CLAMP_TO_BORDER mode */
1966 get_border_color(tObj
, img
, rgba
);
1969 swImg
->FetchTexelf(swImg
, i
, j
, k
, rgba
);
1975 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1978 sample_3d_linear(struct gl_context
*ctx
,
1979 const struct gl_texture_object
*tObj
,
1980 const struct gl_texture_image
*img
,
1981 const GLfloat texcoord
[4],
1984 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1985 const GLint width
= img
->Width2
;
1986 const GLint height
= img
->Height2
;
1987 const GLint depth
= img
->Depth2
;
1988 GLint i0
, j0
, k0
, i1
, j1
, k1
;
1989 GLbitfield useBorderColor
= 0x0;
1991 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
1992 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
1994 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1995 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1996 linear_texel_locations(tObj
->Sampler
.WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
2007 /* check if sampling texture border color */
2008 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2009 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2010 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2011 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2012 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
2013 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
2017 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
2018 get_border_color(tObj
, img
, t000
);
2021 swImg
->FetchTexelf(swImg
, i0
, j0
, k0
, t000
);
2023 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
2024 get_border_color(tObj
, img
, t100
);
2027 swImg
->FetchTexelf(swImg
, i1
, j0
, k0
, t100
);
2029 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
2030 get_border_color(tObj
, img
, t010
);
2033 swImg
->FetchTexelf(swImg
, i0
, j1
, k0
, t010
);
2035 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
2036 get_border_color(tObj
, img
, t110
);
2039 swImg
->FetchTexelf(swImg
, i1
, j1
, k0
, t110
);
2042 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
2043 get_border_color(tObj
, img
, t001
);
2046 swImg
->FetchTexelf(swImg
, i0
, j0
, k1
, t001
);
2048 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
2049 get_border_color(tObj
, img
, t101
);
2052 swImg
->FetchTexelf(swImg
, i1
, j0
, k1
, t101
);
2054 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
2055 get_border_color(tObj
, img
, t011
);
2058 swImg
->FetchTexelf(swImg
, i0
, j1
, k1
, t011
);
2060 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
2061 get_border_color(tObj
, img
, t111
);
2064 swImg
->FetchTexelf(swImg
, i1
, j1
, k1
, t111
);
2067 /* trilinear interpolation of samples */
2068 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
2073 sample_3d_nearest_mipmap_nearest(struct gl_context
*ctx
,
2074 const struct gl_texture_object
*tObj
,
2075 GLuint n
, const GLfloat texcoord
[][4],
2076 const GLfloat lambda
[], GLfloat rgba
[][4] )
2079 for (i
= 0; i
< n
; i
++) {
2080 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2081 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2087 sample_3d_linear_mipmap_nearest(struct gl_context
*ctx
,
2088 const struct gl_texture_object
*tObj
,
2089 GLuint n
, const GLfloat texcoord
[][4],
2090 const GLfloat lambda
[], GLfloat rgba
[][4])
2093 ASSERT(lambda
!= NULL
);
2094 for (i
= 0; i
< n
; i
++) {
2095 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2096 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2102 sample_3d_nearest_mipmap_linear(struct gl_context
*ctx
,
2103 const struct gl_texture_object
*tObj
,
2104 GLuint n
, const GLfloat texcoord
[][4],
2105 const GLfloat lambda
[], GLfloat rgba
[][4])
2108 ASSERT(lambda
!= NULL
);
2109 for (i
= 0; i
< n
; i
++) {
2110 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2111 if (level
>= tObj
->_MaxLevel
) {
2112 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2113 texcoord
[i
], rgba
[i
]);
2116 GLfloat t0
[4], t1
[4]; /* texels */
2117 const GLfloat f
= FRAC(lambda
[i
]);
2118 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2119 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2120 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2127 sample_3d_linear_mipmap_linear(struct gl_context
*ctx
,
2128 const struct gl_texture_object
*tObj
,
2129 GLuint n
, const GLfloat texcoord
[][4],
2130 const GLfloat lambda
[], GLfloat rgba
[][4])
2133 ASSERT(lambda
!= NULL
);
2134 for (i
= 0; i
< n
; i
++) {
2135 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2136 if (level
>= tObj
->_MaxLevel
) {
2137 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2138 texcoord
[i
], rgba
[i
]);
2141 GLfloat t0
[4], t1
[4]; /* texels */
2142 const GLfloat f
= FRAC(lambda
[i
]);
2143 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2144 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2145 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2151 /** Sample 3D texture, nearest filtering for both min/magnification */
2153 sample_nearest_3d(struct gl_context
*ctx
,
2154 const struct gl_texture_object
*tObj
, GLuint n
,
2155 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2159 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2161 for (i
= 0; i
< n
; i
++) {
2162 sample_3d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2167 /** Sample 3D texture, linear filtering for both min/magnification */
2169 sample_linear_3d(struct gl_context
*ctx
,
2170 const struct gl_texture_object
*tObj
, GLuint n
,
2171 const GLfloat texcoords
[][4],
2172 const GLfloat lambda
[], GLfloat rgba
[][4])
2175 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2177 for (i
= 0; i
< n
; i
++) {
2178 sample_3d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2183 /** Sample 3D texture, using lambda to choose between min/magnification */
2185 sample_lambda_3d(struct gl_context
*ctx
,
2186 const struct gl_texture_object
*tObj
, GLuint n
,
2187 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2190 GLuint minStart
, minEnd
; /* texels with minification */
2191 GLuint magStart
, magEnd
; /* texels with magnification */
2194 ASSERT(lambda
!= NULL
);
2195 compute_min_mag_ranges(tObj
, n
, lambda
,
2196 &minStart
, &minEnd
, &magStart
, &magEnd
);
2198 if (minStart
< minEnd
) {
2199 /* do the minified texels */
2200 GLuint m
= minEnd
- minStart
;
2201 switch (tObj
->Sampler
.MinFilter
) {
2203 for (i
= minStart
; i
< minEnd
; i
++)
2204 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2205 texcoords
[i
], rgba
[i
]);
2208 for (i
= minStart
; i
< minEnd
; i
++)
2209 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2210 texcoords
[i
], rgba
[i
]);
2212 case GL_NEAREST_MIPMAP_NEAREST
:
2213 sample_3d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2214 lambda
+ minStart
, rgba
+ minStart
);
2216 case GL_LINEAR_MIPMAP_NEAREST
:
2217 sample_3d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2218 lambda
+ minStart
, rgba
+ minStart
);
2220 case GL_NEAREST_MIPMAP_LINEAR
:
2221 sample_3d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2222 lambda
+ minStart
, rgba
+ minStart
);
2224 case GL_LINEAR_MIPMAP_LINEAR
:
2225 sample_3d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2226 lambda
+ minStart
, rgba
+ minStart
);
2229 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
2234 if (magStart
< magEnd
) {
2235 /* do the magnified texels */
2236 switch (tObj
->Sampler
.MagFilter
) {
2238 for (i
= magStart
; i
< magEnd
; i
++)
2239 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2240 texcoords
[i
], rgba
[i
]);
2243 for (i
= magStart
; i
< magEnd
; i
++)
2244 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2245 texcoords
[i
], rgba
[i
]);
2248 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
2255 /**********************************************************************/
2256 /* Texture Cube Map Sampling Functions */
2257 /**********************************************************************/
2260 * Choose one of six sides of a texture cube map given the texture
2261 * coord (rx,ry,rz). Return pointer to corresponding array of texture
2264 static const struct gl_texture_image
**
2265 choose_cube_face(const struct gl_texture_object
*texObj
,
2266 const GLfloat texcoord
[4], GLfloat newCoord
[4])
2270 direction target sc tc ma
2271 ---------- ------------------------------- --- --- ---
2272 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2273 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2274 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2275 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2276 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2277 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2279 const GLfloat rx
= texcoord
[0];
2280 const GLfloat ry
= texcoord
[1];
2281 const GLfloat rz
= texcoord
[2];
2282 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
2286 if (arx
>= ary
&& arx
>= arz
) {
2300 else if (ary
>= arx
&& ary
>= arz
) {
2330 const float ima
= 1.0F
/ ma
;
2331 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
2332 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
2335 return (const struct gl_texture_image
**) texObj
->Image
[face
];
2340 sample_nearest_cube(struct gl_context
*ctx
,
2341 const struct gl_texture_object
*tObj
, GLuint n
,
2342 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2347 for (i
= 0; i
< n
; i
++) {
2348 const struct gl_texture_image
**images
;
2349 GLfloat newCoord
[4];
2350 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2351 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
2358 sample_linear_cube(struct gl_context
*ctx
,
2359 const struct gl_texture_object
*tObj
, GLuint n
,
2360 const GLfloat texcoords
[][4],
2361 const GLfloat lambda
[], GLfloat rgba
[][4])
2365 for (i
= 0; i
< n
; i
++) {
2366 const struct gl_texture_image
**images
;
2367 GLfloat newCoord
[4];
2368 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2369 sample_2d_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
2376 sample_cube_nearest_mipmap_nearest(struct gl_context
*ctx
,
2377 const struct gl_texture_object
*tObj
,
2378 GLuint n
, const GLfloat texcoord
[][4],
2379 const GLfloat lambda
[], GLfloat rgba
[][4])
2382 ASSERT(lambda
!= NULL
);
2383 for (i
= 0; i
< n
; i
++) {
2384 const struct gl_texture_image
**images
;
2385 GLfloat newCoord
[4];
2387 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2389 /* XXX we actually need to recompute lambda here based on the newCoords.
2390 * But we would need the texcoords of adjacent fragments to compute that
2391 * properly, and we don't have those here.
2392 * For now, do an approximation: subtracting 1 from the chosen mipmap
2393 * level seems to work in some test cases.
2394 * The same adjustment is done in the next few functions.
2396 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2397 level
= MAX2(level
- 1, 0);
2399 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2405 sample_cube_linear_mipmap_nearest(struct gl_context
*ctx
,
2406 const struct gl_texture_object
*tObj
,
2407 GLuint n
, const GLfloat texcoord
[][4],
2408 const GLfloat lambda
[], GLfloat rgba
[][4])
2411 ASSERT(lambda
!= NULL
);
2412 for (i
= 0; i
< n
; i
++) {
2413 const struct gl_texture_image
**images
;
2414 GLfloat newCoord
[4];
2415 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2416 level
= MAX2(level
- 1, 0); /* see comment above */
2417 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2418 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2424 sample_cube_nearest_mipmap_linear(struct gl_context
*ctx
,
2425 const struct gl_texture_object
*tObj
,
2426 GLuint n
, const GLfloat texcoord
[][4],
2427 const GLfloat lambda
[], GLfloat rgba
[][4])
2430 ASSERT(lambda
!= NULL
);
2431 for (i
= 0; i
< n
; i
++) {
2432 const struct gl_texture_image
**images
;
2433 GLfloat newCoord
[4];
2434 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2435 level
= MAX2(level
- 1, 0); /* see comment above */
2436 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2437 if (level
>= tObj
->_MaxLevel
) {
2438 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2442 GLfloat t0
[4], t1
[4]; /* texels */
2443 const GLfloat f
= FRAC(lambda
[i
]);
2444 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
2445 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2446 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2453 sample_cube_linear_mipmap_linear(struct gl_context
*ctx
,
2454 const struct gl_texture_object
*tObj
,
2455 GLuint n
, const GLfloat texcoord
[][4],
2456 const GLfloat lambda
[], GLfloat rgba
[][4])
2459 ASSERT(lambda
!= NULL
);
2460 for (i
= 0; i
< n
; i
++) {
2461 const struct gl_texture_image
**images
;
2462 GLfloat newCoord
[4];
2463 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2464 level
= MAX2(level
- 1, 0); /* see comment above */
2465 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2466 if (level
>= tObj
->_MaxLevel
) {
2467 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2471 GLfloat t0
[4], t1
[4];
2472 const GLfloat f
= FRAC(lambda
[i
]);
2473 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
2474 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2475 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2481 /** Sample cube texture, using lambda to choose between min/magnification */
2483 sample_lambda_cube(struct gl_context
*ctx
,
2484 const struct gl_texture_object
*tObj
, GLuint n
,
2485 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2488 GLuint minStart
, minEnd
; /* texels with minification */
2489 GLuint magStart
, magEnd
; /* texels with magnification */
2491 ASSERT(lambda
!= NULL
);
2492 compute_min_mag_ranges(tObj
, n
, lambda
,
2493 &minStart
, &minEnd
, &magStart
, &magEnd
);
2495 if (minStart
< minEnd
) {
2496 /* do the minified texels */
2497 const GLuint m
= minEnd
- minStart
;
2498 switch (tObj
->Sampler
.MinFilter
) {
2500 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2501 lambda
+ minStart
, rgba
+ minStart
);
2504 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2505 lambda
+ minStart
, rgba
+ minStart
);
2507 case GL_NEAREST_MIPMAP_NEAREST
:
2508 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
2509 texcoords
+ minStart
,
2510 lambda
+ minStart
, rgba
+ minStart
);
2512 case GL_LINEAR_MIPMAP_NEAREST
:
2513 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
2514 texcoords
+ minStart
,
2515 lambda
+ minStart
, rgba
+ minStart
);
2517 case GL_NEAREST_MIPMAP_LINEAR
:
2518 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
2519 texcoords
+ minStart
,
2520 lambda
+ minStart
, rgba
+ minStart
);
2522 case GL_LINEAR_MIPMAP_LINEAR
:
2523 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
2524 texcoords
+ minStart
,
2525 lambda
+ minStart
, rgba
+ minStart
);
2528 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2532 if (magStart
< magEnd
) {
2533 /* do the magnified texels */
2534 const GLuint m
= magEnd
- magStart
;
2535 switch (tObj
->Sampler
.MagFilter
) {
2537 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2538 lambda
+ magStart
, rgba
+ magStart
);
2541 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2542 lambda
+ magStart
, rgba
+ magStart
);
2545 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2551 /**********************************************************************/
2552 /* Texture Rectangle Sampling Functions */
2553 /**********************************************************************/
2557 sample_nearest_rect(struct gl_context
*ctx
,
2558 const struct gl_texture_object
*tObj
, GLuint n
,
2559 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2562 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2563 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2564 const GLint width
= img
->Width
;
2565 const GLint height
= img
->Height
;
2571 ASSERT(tObj
->Sampler
.WrapS
== GL_CLAMP
||
2572 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_EDGE
||
2573 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_BORDER
);
2574 ASSERT(tObj
->Sampler
.WrapT
== GL_CLAMP
||
2575 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_EDGE
||
2576 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_BORDER
);
2578 for (i
= 0; i
< n
; i
++) {
2580 col
= clamp_rect_coord_nearest(tObj
->Sampler
.WrapS
, texcoords
[i
][0], width
);
2581 row
= clamp_rect_coord_nearest(tObj
->Sampler
.WrapT
, texcoords
[i
][1], height
);
2582 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2583 get_border_color(tObj
, img
, rgba
[i
]);
2585 swImg
->FetchTexelf(swImg
, col
, row
, 0, rgba
[i
]);
2591 sample_linear_rect(struct gl_context
*ctx
,
2592 const struct gl_texture_object
*tObj
, GLuint n
,
2593 const GLfloat texcoords
[][4],
2594 const GLfloat lambda
[], GLfloat rgba
[][4])
2596 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2597 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2598 const GLint width
= img
->Width
;
2599 const GLint height
= img
->Height
;
2605 ASSERT(tObj
->Sampler
.WrapS
== GL_CLAMP
||
2606 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_EDGE
||
2607 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_BORDER
);
2608 ASSERT(tObj
->Sampler
.WrapT
== GL_CLAMP
||
2609 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_EDGE
||
2610 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_BORDER
);
2612 for (i
= 0; i
< n
; i
++) {
2613 GLint i0
, j0
, i1
, j1
;
2614 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2616 GLbitfield useBorderColor
= 0x0;
2618 clamp_rect_coord_linear(tObj
->Sampler
.WrapS
, texcoords
[i
][0], width
,
2620 clamp_rect_coord_linear(tObj
->Sampler
.WrapT
, texcoords
[i
][1], height
,
2623 /* compute integer rows/columns */
2624 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2625 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2626 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2627 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2629 /* get four texel samples */
2630 if (useBorderColor
& (I0BIT
| J0BIT
))
2631 get_border_color(tObj
, img
, t00
);
2633 swImg
->FetchTexelf(swImg
, i0
, j0
, 0, t00
);
2635 if (useBorderColor
& (I1BIT
| J0BIT
))
2636 get_border_color(tObj
, img
, t10
);
2638 swImg
->FetchTexelf(swImg
, i1
, j0
, 0, t10
);
2640 if (useBorderColor
& (I0BIT
| J1BIT
))
2641 get_border_color(tObj
, img
, t01
);
2643 swImg
->FetchTexelf(swImg
, i0
, j1
, 0, t01
);
2645 if (useBorderColor
& (I1BIT
| J1BIT
))
2646 get_border_color(tObj
, img
, t11
);
2648 swImg
->FetchTexelf(swImg
, i1
, j1
, 0, t11
);
2650 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2655 /** Sample Rect texture, using lambda to choose between min/magnification */
2657 sample_lambda_rect(struct gl_context
*ctx
,
2658 const struct gl_texture_object
*tObj
, GLuint n
,
2659 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2662 GLuint minStart
, minEnd
, magStart
, magEnd
;
2664 /* We only need lambda to decide between minification and magnification.
2665 * There is no mipmapping with rectangular textures.
2667 compute_min_mag_ranges(tObj
, n
, lambda
,
2668 &minStart
, &minEnd
, &magStart
, &magEnd
);
2670 if (minStart
< minEnd
) {
2671 if (tObj
->Sampler
.MinFilter
== GL_NEAREST
) {
2672 sample_nearest_rect(ctx
, tObj
, minEnd
- minStart
,
2673 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2676 sample_linear_rect(ctx
, tObj
, minEnd
- minStart
,
2677 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2680 if (magStart
< magEnd
) {
2681 if (tObj
->Sampler
.MagFilter
== GL_NEAREST
) {
2682 sample_nearest_rect(ctx
, tObj
, magEnd
- magStart
,
2683 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2686 sample_linear_rect(ctx
, tObj
, magEnd
- magStart
,
2687 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2693 /**********************************************************************/
2694 /* 2D Texture Array Sampling Functions */
2695 /**********************************************************************/
2698 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2701 sample_2d_array_nearest(struct gl_context
*ctx
,
2702 const struct gl_texture_object
*tObj
,
2703 const struct gl_texture_image
*img
,
2704 const GLfloat texcoord
[4],
2707 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2708 const GLint width
= img
->Width2
; /* without border, power of two */
2709 const GLint height
= img
->Height2
; /* without border, power of two */
2710 const GLint depth
= img
->Depth
;
2715 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
2716 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
2717 array
= tex_array_slice(texcoord
[2], depth
);
2719 if (i
< 0 || i
>= (GLint
) img
->Width
||
2720 j
< 0 || j
>= (GLint
) img
->Height
||
2721 array
< 0 || array
>= (GLint
) img
->Depth
) {
2722 /* Need this test for GL_CLAMP_TO_BORDER mode */
2723 get_border_color(tObj
, img
, rgba
);
2726 swImg
->FetchTexelf(swImg
, i
, j
, array
, rgba
);
2732 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2735 sample_2d_array_linear(struct gl_context
*ctx
,
2736 const struct gl_texture_object
*tObj
,
2737 const struct gl_texture_image
*img
,
2738 const GLfloat texcoord
[4],
2741 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2742 const GLint width
= img
->Width2
;
2743 const GLint height
= img
->Height2
;
2744 const GLint depth
= img
->Depth
;
2745 GLint i0
, j0
, i1
, j1
;
2747 GLbitfield useBorderColor
= 0x0;
2749 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2751 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2752 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2753 array
= tex_array_slice(texcoord
[2], depth
);
2755 if (array
< 0 || array
>= depth
) {
2756 COPY_4V(rgba
, tObj
->Sampler
.BorderColor
.f
);
2766 /* check if sampling texture border color */
2767 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2768 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2769 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2770 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2774 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2775 get_border_color(tObj
, img
, t00
);
2778 swImg
->FetchTexelf(swImg
, i0
, j0
, array
, t00
);
2780 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2781 get_border_color(tObj
, img
, t10
);
2784 swImg
->FetchTexelf(swImg
, i1
, j0
, array
, t10
);
2786 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2787 get_border_color(tObj
, img
, t01
);
2790 swImg
->FetchTexelf(swImg
, i0
, j1
, array
, t01
);
2792 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2793 get_border_color(tObj
, img
, t11
);
2796 swImg
->FetchTexelf(swImg
, i1
, j1
, array
, t11
);
2799 /* trilinear interpolation of samples */
2800 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2806 sample_2d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2807 const struct gl_texture_object
*tObj
,
2808 GLuint n
, const GLfloat texcoord
[][4],
2809 const GLfloat lambda
[], GLfloat rgba
[][4])
2812 for (i
= 0; i
< n
; i
++) {
2813 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2814 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2821 sample_2d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2822 const struct gl_texture_object
*tObj
,
2823 GLuint n
, const GLfloat texcoord
[][4],
2824 const GLfloat lambda
[], GLfloat rgba
[][4])
2827 ASSERT(lambda
!= NULL
);
2828 for (i
= 0; i
< n
; i
++) {
2829 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2830 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2831 texcoord
[i
], rgba
[i
]);
2837 sample_2d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2838 const struct gl_texture_object
*tObj
,
2839 GLuint n
, const GLfloat texcoord
[][4],
2840 const GLfloat lambda
[], GLfloat rgba
[][4])
2843 ASSERT(lambda
!= NULL
);
2844 for (i
= 0; i
< n
; i
++) {
2845 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2846 if (level
>= tObj
->_MaxLevel
) {
2847 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2848 texcoord
[i
], rgba
[i
]);
2851 GLfloat t0
[4], t1
[4]; /* texels */
2852 const GLfloat f
= FRAC(lambda
[i
]);
2853 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
],
2855 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1],
2857 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2864 sample_2d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2865 const struct gl_texture_object
*tObj
,
2866 GLuint n
, const GLfloat texcoord
[][4],
2867 const GLfloat lambda
[], GLfloat rgba
[][4])
2870 ASSERT(lambda
!= NULL
);
2871 for (i
= 0; i
< n
; i
++) {
2872 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2873 if (level
>= tObj
->_MaxLevel
) {
2874 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2875 texcoord
[i
], rgba
[i
]);
2878 GLfloat t0
[4], t1
[4]; /* texels */
2879 const GLfloat f
= FRAC(lambda
[i
]);
2880 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2882 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1],
2884 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2890 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2892 sample_nearest_2d_array(struct gl_context
*ctx
,
2893 const struct gl_texture_object
*tObj
, GLuint n
,
2894 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2898 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2900 for (i
= 0; i
< n
; i
++) {
2901 sample_2d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2907 /** Sample 2D Array texture, linear filtering for both min/magnification */
2909 sample_linear_2d_array(struct gl_context
*ctx
,
2910 const struct gl_texture_object
*tObj
, GLuint n
,
2911 const GLfloat texcoords
[][4],
2912 const GLfloat lambda
[], GLfloat rgba
[][4])
2915 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2917 for (i
= 0; i
< n
; i
++) {
2918 sample_2d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2923 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2925 sample_lambda_2d_array(struct gl_context
*ctx
,
2926 const struct gl_texture_object
*tObj
, GLuint n
,
2927 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2930 GLuint minStart
, minEnd
; /* texels with minification */
2931 GLuint magStart
, magEnd
; /* texels with magnification */
2934 ASSERT(lambda
!= NULL
);
2935 compute_min_mag_ranges(tObj
, n
, lambda
,
2936 &minStart
, &minEnd
, &magStart
, &magEnd
);
2938 if (minStart
< minEnd
) {
2939 /* do the minified texels */
2940 GLuint m
= minEnd
- minStart
;
2941 switch (tObj
->Sampler
.MinFilter
) {
2943 for (i
= minStart
; i
< minEnd
; i
++)
2944 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2945 texcoords
[i
], rgba
[i
]);
2948 for (i
= minStart
; i
< minEnd
; i
++)
2949 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2950 texcoords
[i
], rgba
[i
]);
2952 case GL_NEAREST_MIPMAP_NEAREST
:
2953 sample_2d_array_nearest_mipmap_nearest(ctx
, tObj
, m
,
2954 texcoords
+ minStart
,
2958 case GL_LINEAR_MIPMAP_NEAREST
:
2959 sample_2d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2960 texcoords
+ minStart
,
2964 case GL_NEAREST_MIPMAP_LINEAR
:
2965 sample_2d_array_nearest_mipmap_linear(ctx
, tObj
, m
,
2966 texcoords
+ minStart
,
2970 case GL_LINEAR_MIPMAP_LINEAR
:
2971 sample_2d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2972 texcoords
+ minStart
,
2977 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
2982 if (magStart
< magEnd
) {
2983 /* do the magnified texels */
2984 switch (tObj
->Sampler
.MagFilter
) {
2986 for (i
= magStart
; i
< magEnd
; i
++)
2987 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2988 texcoords
[i
], rgba
[i
]);
2991 for (i
= magStart
; i
< magEnd
; i
++)
2992 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2993 texcoords
[i
], rgba
[i
]);
2996 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
3005 /**********************************************************************/
3006 /* 1D Texture Array Sampling Functions */
3007 /**********************************************************************/
3010 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
3013 sample_1d_array_nearest(struct gl_context
*ctx
,
3014 const struct gl_texture_object
*tObj
,
3015 const struct gl_texture_image
*img
,
3016 const GLfloat texcoord
[4],
3019 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3020 const GLint width
= img
->Width2
; /* without border, power of two */
3021 const GLint height
= img
->Height
;
3026 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
3027 array
= tex_array_slice(texcoord
[1], height
);
3029 if (i
< 0 || i
>= (GLint
) img
->Width
||
3030 array
< 0 || array
>= (GLint
) img
->Height
) {
3031 /* Need this test for GL_CLAMP_TO_BORDER mode */
3032 get_border_color(tObj
, img
, rgba
);
3035 swImg
->FetchTexelf(swImg
, i
, array
, 0, rgba
);
3041 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
3044 sample_1d_array_linear(struct gl_context
*ctx
,
3045 const struct gl_texture_object
*tObj
,
3046 const struct gl_texture_image
*img
,
3047 const GLfloat texcoord
[4],
3050 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3051 const GLint width
= img
->Width2
;
3052 const GLint height
= img
->Height
;
3055 GLbitfield useBorderColor
= 0x0;
3057 GLfloat t0
[4], t1
[4];
3059 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
3060 array
= tex_array_slice(texcoord
[1], height
);
3067 /* check if sampling texture border color */
3068 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
3069 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
3072 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
3075 if (useBorderColor
& (I0BIT
| K0BIT
)) {
3076 get_border_color(tObj
, img
, t0
);
3079 swImg
->FetchTexelf(swImg
, i0
, array
, 0, t0
);
3081 if (useBorderColor
& (I1BIT
| K0BIT
)) {
3082 get_border_color(tObj
, img
, t1
);
3085 swImg
->FetchTexelf(swImg
, i1
, array
, 0, t1
);
3088 /* bilinear interpolation of samples */
3089 lerp_rgba(rgba
, a
, t0
, t1
);
3094 sample_1d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
3095 const struct gl_texture_object
*tObj
,
3096 GLuint n
, const GLfloat texcoord
[][4],
3097 const GLfloat lambda
[], GLfloat rgba
[][4])
3100 for (i
= 0; i
< n
; i
++) {
3101 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3102 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
3109 sample_1d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
3110 const struct gl_texture_object
*tObj
,
3111 GLuint n
, const GLfloat texcoord
[][4],
3112 const GLfloat lambda
[], GLfloat rgba
[][4])
3115 ASSERT(lambda
!= NULL
);
3116 for (i
= 0; i
< n
; i
++) {
3117 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3118 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
3119 texcoord
[i
], rgba
[i
]);
3125 sample_1d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
3126 const struct gl_texture_object
*tObj
,
3127 GLuint n
, const GLfloat texcoord
[][4],
3128 const GLfloat lambda
[], GLfloat rgba
[][4])
3131 ASSERT(lambda
!= NULL
);
3132 for (i
= 0; i
< n
; i
++) {
3133 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3134 if (level
>= tObj
->_MaxLevel
) {
3135 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
3136 texcoord
[i
], rgba
[i
]);
3139 GLfloat t0
[4], t1
[4]; /* texels */
3140 const GLfloat f
= FRAC(lambda
[i
]);
3141 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3142 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3143 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3150 sample_1d_array_linear_mipmap_linear(struct gl_context
*ctx
,
3151 const struct gl_texture_object
*tObj
,
3152 GLuint n
, const GLfloat texcoord
[][4],
3153 const GLfloat lambda
[], GLfloat rgba
[][4])
3156 ASSERT(lambda
!= NULL
);
3157 for (i
= 0; i
< n
; i
++) {
3158 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3159 if (level
>= tObj
->_MaxLevel
) {
3160 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
3161 texcoord
[i
], rgba
[i
]);
3164 GLfloat t0
[4], t1
[4]; /* texels */
3165 const GLfloat f
= FRAC(lambda
[i
]);
3166 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3167 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3168 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3174 /** Sample 1D Array texture, nearest filtering for both min/magnification */
3176 sample_nearest_1d_array(struct gl_context
*ctx
,
3177 const struct gl_texture_object
*tObj
, GLuint n
,
3178 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3182 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3184 for (i
= 0; i
< n
; i
++) {
3185 sample_1d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
3190 /** Sample 1D Array texture, linear filtering for both min/magnification */
3192 sample_linear_1d_array(struct gl_context
*ctx
,
3193 const struct gl_texture_object
*tObj
, GLuint n
,
3194 const GLfloat texcoords
[][4],
3195 const GLfloat lambda
[], GLfloat rgba
[][4])
3198 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3200 for (i
= 0; i
< n
; i
++) {
3201 sample_1d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
3206 /** Sample 1D Array texture, using lambda to choose between min/magnification */
3208 sample_lambda_1d_array(struct gl_context
*ctx
,
3209 const struct gl_texture_object
*tObj
, GLuint n
,
3210 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3213 GLuint minStart
, minEnd
; /* texels with minification */
3214 GLuint magStart
, magEnd
; /* texels with magnification */
3217 ASSERT(lambda
!= NULL
);
3218 compute_min_mag_ranges(tObj
, n
, lambda
,
3219 &minStart
, &minEnd
, &magStart
, &magEnd
);
3221 if (minStart
< minEnd
) {
3222 /* do the minified texels */
3223 GLuint m
= minEnd
- minStart
;
3224 switch (tObj
->Sampler
.MinFilter
) {
3226 for (i
= minStart
; i
< minEnd
; i
++)
3227 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3228 texcoords
[i
], rgba
[i
]);
3231 for (i
= minStart
; i
< minEnd
; i
++)
3232 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3233 texcoords
[i
], rgba
[i
]);
3235 case GL_NEAREST_MIPMAP_NEAREST
:
3236 sample_1d_array_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
3237 lambda
+ minStart
, rgba
+ minStart
);
3239 case GL_LINEAR_MIPMAP_NEAREST
:
3240 sample_1d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
3241 texcoords
+ minStart
,
3245 case GL_NEAREST_MIPMAP_LINEAR
:
3246 sample_1d_array_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
3247 lambda
+ minStart
, rgba
+ minStart
);
3249 case GL_LINEAR_MIPMAP_LINEAR
:
3250 sample_1d_array_linear_mipmap_linear(ctx
, tObj
, m
,
3251 texcoords
+ minStart
,
3256 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
3261 if (magStart
< magEnd
) {
3262 /* do the magnified texels */
3263 switch (tObj
->Sampler
.MagFilter
) {
3265 for (i
= magStart
; i
< magEnd
; i
++)
3266 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3267 texcoords
[i
], rgba
[i
]);
3270 for (i
= magStart
; i
< magEnd
; i
++)
3271 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3272 texcoords
[i
], rgba
[i
]);
3275 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
3283 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
3285 static INLINE GLfloat
3286 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
,
3291 return (coord
<= depthSample
) ? 1.0F
: ambient
;
3293 return (coord
>= depthSample
) ? 1.0F
: ambient
;
3295 return (coord
< depthSample
) ? 1.0F
: ambient
;
3297 return (coord
> depthSample
) ? 1.0F
: ambient
;
3299 return (coord
== depthSample
) ? 1.0F
: ambient
;
3301 return (coord
!= depthSample
) ? 1.0F
: ambient
;
3309 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
3316 * Compare texcoord against four depth samples.
3318 static INLINE GLfloat
3319 shadow_compare4(GLenum function
, GLfloat coord
,
3320 GLfloat depth00
, GLfloat depth01
,
3321 GLfloat depth10
, GLfloat depth11
,
3322 GLfloat ambient
, GLfloat wi
, GLfloat wj
)
3324 const GLfloat d
= (1.0F
- (GLfloat
) ambient
) * 0.25F
;
3325 GLfloat luminance
= 1.0F
;
3329 if (coord
> depth00
) luminance
-= d
;
3330 if (coord
> depth01
) luminance
-= d
;
3331 if (coord
> depth10
) luminance
-= d
;
3332 if (coord
> depth11
) luminance
-= d
;
3335 if (coord
< depth00
) luminance
-= d
;
3336 if (coord
< depth01
) luminance
-= d
;
3337 if (coord
< depth10
) luminance
-= d
;
3338 if (coord
< depth11
) luminance
-= d
;
3341 if (coord
>= depth00
) luminance
-= d
;
3342 if (coord
>= depth01
) luminance
-= d
;
3343 if (coord
>= depth10
) luminance
-= d
;
3344 if (coord
>= depth11
) luminance
-= d
;
3347 if (coord
<= depth00
) luminance
-= d
;
3348 if (coord
<= depth01
) luminance
-= d
;
3349 if (coord
<= depth10
) luminance
-= d
;
3350 if (coord
<= depth11
) luminance
-= d
;
3353 if (coord
!= depth00
) luminance
-= d
;
3354 if (coord
!= depth01
) luminance
-= d
;
3355 if (coord
!= depth10
) luminance
-= d
;
3356 if (coord
!= depth11
) luminance
-= d
;
3359 if (coord
== depth00
) luminance
-= d
;
3360 if (coord
== depth01
) luminance
-= d
;
3361 if (coord
== depth10
) luminance
-= d
;
3362 if (coord
== depth11
) luminance
-= d
;
3369 /* ordinary bilinear filtering */
3370 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
3372 _mesa_problem(NULL
, "Bad compare func in sample_compare4");
3379 * Choose the mipmap level to use when sampling from a depth texture.
3382 choose_depth_texture_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
3386 if (tObj
->Sampler
.MinFilter
== GL_NEAREST
|| tObj
->Sampler
.MinFilter
== GL_LINEAR
) {
3387 /* no mipmapping - use base level */
3388 level
= tObj
->BaseLevel
;
3391 /* choose mipmap level */
3392 lambda
= CLAMP(lambda
, tObj
->Sampler
.MinLod
, tObj
->Sampler
.MaxLod
);
3393 level
= (GLint
) lambda
;
3394 level
= CLAMP(level
, tObj
->BaseLevel
, tObj
->_MaxLevel
);
3402 * Sample a shadow/depth texture. This function is incomplete. It doesn't
3403 * check for minification vs. magnification, etc.
3406 sample_depth_texture( struct gl_context
*ctx
,
3407 const struct gl_texture_object
*tObj
, GLuint n
,
3408 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3409 GLfloat texel
[][4] )
3411 const GLint level
= choose_depth_texture_level(tObj
, lambda
[0]);
3412 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
3413 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3414 const GLint width
= img
->Width
;
3415 const GLint height
= img
->Height
;
3416 const GLint depth
= img
->Depth
;
3417 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
3423 ASSERT(img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
3424 img
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
3426 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
3427 tObj
->Target
== GL_TEXTURE_2D
||
3428 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
3429 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
3430 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
);
3432 ambient
= tObj
->Sampler
.CompareFailValue
;
3434 /* XXXX if tObj->Sampler.MinFilter != tObj->Sampler.MagFilter, we're ignoring lambda */
3436 function
= (tObj
->Sampler
.CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
3437 tObj
->Sampler
.CompareFunc
: GL_NONE
;
3439 if (tObj
->Sampler
.MagFilter
== GL_NEAREST
) {
3441 for (i
= 0; i
< n
; i
++) {
3442 GLfloat depthSample
, depthRef
;
3443 GLint col
, row
, slice
;
3445 nearest_texcoord(tObj
, level
, texcoords
[i
], &col
, &row
, &slice
);
3447 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3448 slice
>= 0 && slice
< depth
) {
3449 swImg
->FetchTexelf(swImg
, col
, row
, slice
, &depthSample
);
3452 depthSample
= tObj
->Sampler
.BorderColor
.f
[0];
3455 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3457 result
= shadow_compare(function
, depthRef
, depthSample
, ambient
);
3459 switch (tObj
->Sampler
.DepthMode
) {
3461 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3464 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3467 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3470 ASSIGN_4V(texel
[i
], result
, 0.0F
, 0.0F
, 1.0F
);
3473 _mesa_problem(ctx
, "Bad depth texture mode");
3479 ASSERT(tObj
->Sampler
.MagFilter
== GL_LINEAR
);
3480 for (i
= 0; i
< n
; i
++) {
3481 GLfloat depth00
, depth01
, depth10
, depth11
, depthRef
;
3482 GLint i0
, i1
, j0
, j1
;
3485 GLuint useBorderTexel
;
3487 linear_texcoord(tObj
, level
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3494 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3500 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3501 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3502 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3503 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3506 if (slice
< 0 || slice
>= (GLint
) depth
) {
3507 depth00
= tObj
->Sampler
.BorderColor
.f
[0];
3508 depth01
= tObj
->Sampler
.BorderColor
.f
[0];
3509 depth10
= tObj
->Sampler
.BorderColor
.f
[0];
3510 depth11
= tObj
->Sampler
.BorderColor
.f
[0];
3513 /* get four depth samples from the texture */
3514 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3515 depth00
= tObj
->Sampler
.BorderColor
.f
[0];
3518 swImg
->FetchTexelf(swImg
, i0
, j0
, slice
, &depth00
);
3520 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3521 depth10
= tObj
->Sampler
.BorderColor
.f
[0];
3524 swImg
->FetchTexelf(swImg
, i1
, j0
, slice
, &depth10
);
3527 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3528 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3529 depth01
= tObj
->Sampler
.BorderColor
.f
[0];
3532 swImg
->FetchTexelf(swImg
, i0
, j1
, slice
, &depth01
);
3534 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3535 depth11
= tObj
->Sampler
.BorderColor
.f
[0];
3538 swImg
->FetchTexelf(swImg
, i1
, j1
, slice
, &depth11
);
3547 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3549 result
= shadow_compare4(function
, depthRef
,
3550 depth00
, depth01
, depth10
, depth11
,
3553 switch (tObj
->Sampler
.DepthMode
) {
3555 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3558 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3561 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3564 _mesa_problem(ctx
, "Bad depth texture mode");
3573 * We use this function when a texture object is in an "incomplete" state.
3574 * When a fragment program attempts to sample an incomplete texture we
3575 * return black (see issue 23 in GL_ARB_fragment_program spec).
3576 * Note: fragment programs don't observe the texture enable/disable flags.
3579 null_sample_func( struct gl_context
*ctx
,
3580 const struct gl_texture_object
*tObj
, GLuint n
,
3581 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3589 for (i
= 0; i
< n
; i
++) {
3593 rgba
[i
][ACOMP
] = 1.0;
3599 * Choose the texture sampling function for the given texture object.
3602 _swrast_choose_texture_sample_func( struct gl_context
*ctx
,
3603 const struct gl_texture_object
*t
)
3605 if (!t
|| !t
->_Complete
) {
3606 return &null_sample_func
;
3609 const GLboolean needLambda
=
3610 (GLboolean
) (t
->Sampler
.MinFilter
!= t
->Sampler
.MagFilter
);
3611 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->_BaseFormat
;
3613 switch (t
->Target
) {
3615 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3616 return &sample_depth_texture
;
3618 else if (needLambda
) {
3619 return &sample_lambda_1d
;
3621 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3622 return &sample_linear_1d
;
3625 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3626 return &sample_nearest_1d
;
3629 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3630 return &sample_depth_texture
;
3632 else if (needLambda
) {
3633 /* Anisotropic filtering extension. Activated only if mipmaps are used */
3634 if (t
->Sampler
.MaxAnisotropy
> 1.0 &&
3635 t
->Sampler
.MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
3636 return &sample_lambda_2d_aniso
;
3638 return &sample_lambda_2d
;
3640 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3641 return &sample_linear_2d
;
3644 /* check for a few optimized cases */
3645 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3646 const struct swrast_texture_image
*swImg
=
3647 swrast_texture_image_const(img
);
3649 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3650 if (t
->Sampler
.WrapS
== GL_REPEAT
&&
3651 t
->Sampler
.WrapT
== GL_REPEAT
&&
3652 swImg
->_IsPowerOfTwo
&&
3654 img
->TexFormat
== MESA_FORMAT_RGB888
) {
3655 return &opt_sample_rgb_2d
;
3657 else if (t
->Sampler
.WrapS
== GL_REPEAT
&&
3658 t
->Sampler
.WrapT
== GL_REPEAT
&&
3659 swImg
->_IsPowerOfTwo
&&
3661 img
->TexFormat
== MESA_FORMAT_RGBA8888
) {
3662 return &opt_sample_rgba_2d
;
3665 return &sample_nearest_2d
;
3670 return &sample_lambda_3d
;
3672 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3673 return &sample_linear_3d
;
3676 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3677 return &sample_nearest_3d
;
3679 case GL_TEXTURE_CUBE_MAP
:
3681 return &sample_lambda_cube
;
3683 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3684 return &sample_linear_cube
;
3687 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3688 return &sample_nearest_cube
;
3690 case GL_TEXTURE_RECTANGLE_NV
:
3691 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3692 return &sample_depth_texture
;
3694 else if (needLambda
) {
3695 return &sample_lambda_rect
;
3697 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3698 return &sample_linear_rect
;
3701 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3702 return &sample_nearest_rect
;
3704 case GL_TEXTURE_1D_ARRAY_EXT
:
3706 return &sample_lambda_1d_array
;
3708 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3709 return &sample_linear_1d_array
;
3712 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3713 return &sample_nearest_1d_array
;
3715 case GL_TEXTURE_2D_ARRAY_EXT
:
3717 return &sample_lambda_2d_array
;
3719 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3720 return &sample_linear_2d_array
;
3723 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3724 return &sample_nearest_2d_array
;
3728 "invalid target in _swrast_choose_texture_sample_func");
3729 return &null_sample_func
;