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"
30 #include "main/texobj.h"
32 #include "s_context.h"
33 #include "s_texfilter.h"
37 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
38 * see 1-pixel bands of improperly weighted linear-filtered textures.
39 * The tests/texwrap.c demo is a good test.
40 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
41 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
43 #define FRAC(f) ((f) - IFLOOR(f))
48 * Linear interpolation macro
50 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
54 * Do 2D/biliner interpolation of float values.
55 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
56 * a and b are the horizontal and vertical interpolants.
57 * It's important that this function is inlined when compiled with
58 * optimization! If we find that's not true on some systems, convert
62 lerp_2d(GLfloat a
, GLfloat b
,
63 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
65 const GLfloat temp0
= LERP(a
, v00
, v10
);
66 const GLfloat temp1
= LERP(a
, v01
, v11
);
67 return LERP(b
, temp0
, temp1
);
72 * Do 3D/trilinear interpolation of float values.
76 lerp_3d(GLfloat a
, GLfloat b
, GLfloat c
,
77 GLfloat v000
, GLfloat v100
, GLfloat v010
, GLfloat v110
,
78 GLfloat v001
, GLfloat v101
, GLfloat v011
, GLfloat v111
)
80 const GLfloat temp00
= LERP(a
, v000
, v100
);
81 const GLfloat temp10
= LERP(a
, v010
, v110
);
82 const GLfloat temp01
= LERP(a
, v001
, v101
);
83 const GLfloat temp11
= LERP(a
, v011
, v111
);
84 const GLfloat temp0
= LERP(b
, temp00
, temp10
);
85 const GLfloat temp1
= LERP(b
, temp01
, temp11
);
86 return LERP(c
, temp0
, temp1
);
91 * Do linear interpolation of colors.
94 lerp_rgba(GLfloat result
[4], GLfloat t
, const GLfloat a
[4], const GLfloat b
[4])
96 result
[0] = LERP(t
, a
[0], b
[0]);
97 result
[1] = LERP(t
, a
[1], b
[1]);
98 result
[2] = LERP(t
, a
[2], b
[2]);
99 result
[3] = LERP(t
, a
[3], b
[3]);
104 * Do bilinear interpolation of colors.
107 lerp_rgba_2d(GLfloat result
[4], GLfloat a
, GLfloat b
,
108 const GLfloat t00
[4], const GLfloat t10
[4],
109 const GLfloat t01
[4], const GLfloat t11
[4])
111 result
[0] = lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]);
112 result
[1] = lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]);
113 result
[2] = lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]);
114 result
[3] = lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]);
119 * Do trilinear interpolation of colors.
122 lerp_rgba_3d(GLfloat result
[4], GLfloat a
, GLfloat b
, GLfloat c
,
123 const GLfloat t000
[4], const GLfloat t100
[4],
124 const GLfloat t010
[4], const GLfloat t110
[4],
125 const GLfloat t001
[4], const GLfloat t101
[4],
126 const GLfloat t011
[4], const GLfloat t111
[4])
129 /* compiler should unroll these short loops */
130 for (k
= 0; k
< 4; k
++) {
131 result
[k
] = lerp_3d(a
, b
, c
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
132 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
138 * Used for GL_REPEAT wrap mode. Using A % B doesn't produce the
139 * right results for A<0. Casting to A to be unsigned only works if B
140 * is a power of two. Adding a bias to A (which is a multiple of B)
141 * avoids the problems with A < 0 (for reasonable A) without using a
144 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
148 * Used to compute texel locations for linear sampling.
150 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
151 * s = texcoord in [0,1]
152 * size = width (or height or depth) of texture
154 * i0, i1 = returns two nearest texel indexes
155 * weight = returns blend factor between texels
158 linear_texel_locations(GLenum wrapMode
,
159 const struct gl_texture_image
*img
,
160 GLint size
, GLfloat s
,
161 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
163 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
168 if (swImg
->_IsPowerOfTwo
) {
169 *i0
= IFLOOR(u
) & (size
- 1);
170 *i1
= (*i0
+ 1) & (size
- 1);
173 *i0
= REMAINDER(IFLOOR(u
), size
);
174 *i1
= REMAINDER(*i0
+ 1, size
);
177 case GL_CLAMP_TO_EDGE
:
189 if (*i1
>= (GLint
) size
)
192 case GL_CLAMP_TO_BORDER
:
194 const GLfloat min
= -1.0F
/ (2.0F
* size
);
195 const GLfloat max
= 1.0F
- min
;
207 case GL_MIRRORED_REPEAT
:
209 const GLint flr
= IFLOOR(s
);
211 u
= 1.0F
- (s
- (GLfloat
) flr
);
213 u
= s
- (GLfloat
) flr
;
214 u
= (u
* size
) - 0.5F
;
219 if (*i1
>= (GLint
) size
)
223 case GL_MIRROR_CLAMP_EXT
:
233 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
244 if (*i1
>= (GLint
) size
)
247 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
249 const GLfloat min
= -1.0F
/ (2.0F
* size
);
250 const GLfloat max
= 1.0F
- min
;
275 _mesa_problem(NULL
, "Bad wrap mode");
284 * Used to compute texel location for nearest sampling.
287 nearest_texel_location(GLenum wrapMode
,
288 const struct gl_texture_image
*img
,
289 GLint size
, GLfloat s
)
291 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
296 /* s limited to [0,1) */
297 /* i limited to [0,size-1] */
298 i
= IFLOOR(s
* size
);
299 if (swImg
->_IsPowerOfTwo
)
302 i
= REMAINDER(i
, size
);
304 case GL_CLAMP_TO_EDGE
:
306 /* s limited to [min,max] */
307 /* i limited to [0, size-1] */
308 const GLfloat min
= 1.0F
/ (2.0F
* size
);
309 const GLfloat max
= 1.0F
- min
;
315 i
= IFLOOR(s
* size
);
318 case GL_CLAMP_TO_BORDER
:
320 /* s limited to [min,max] */
321 /* i limited to [-1, size] */
322 const GLfloat min
= -1.0F
/ (2.0F
* size
);
323 const GLfloat max
= 1.0F
- min
;
329 i
= IFLOOR(s
* size
);
332 case GL_MIRRORED_REPEAT
:
334 const GLfloat min
= 1.0F
/ (2.0F
* size
);
335 const GLfloat max
= 1.0F
- min
;
336 const GLint flr
= IFLOOR(s
);
339 u
= 1.0F
- (s
- (GLfloat
) flr
);
341 u
= s
- (GLfloat
) flr
;
347 i
= IFLOOR(u
* size
);
350 case GL_MIRROR_CLAMP_EXT
:
352 /* s limited to [0,1] */
353 /* i limited to [0,size-1] */
354 const GLfloat u
= FABSF(s
);
360 i
= IFLOOR(u
* size
);
363 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
365 /* s limited to [min,max] */
366 /* i limited to [0, size-1] */
367 const GLfloat min
= 1.0F
/ (2.0F
* size
);
368 const GLfloat max
= 1.0F
- min
;
369 const GLfloat u
= FABSF(s
);
375 i
= IFLOOR(u
* size
);
378 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
380 /* s limited to [min,max] */
381 /* i limited to [0, size-1] */
382 const GLfloat min
= -1.0F
/ (2.0F
* size
);
383 const GLfloat max
= 1.0F
- min
;
384 const GLfloat u
= FABSF(s
);
390 i
= IFLOOR(u
* size
);
394 /* s limited to [0,1] */
395 /* i limited to [0,size-1] */
401 i
= IFLOOR(s
* size
);
404 _mesa_problem(NULL
, "Bad wrap mode");
410 /* Power of two image sizes only */
412 linear_repeat_texel_location(GLuint size
, GLfloat s
,
413 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
415 GLfloat u
= s
* size
- 0.5F
;
416 *i0
= IFLOOR(u
) & (size
- 1);
417 *i1
= (*i0
+ 1) & (size
- 1);
423 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
426 clamp_rect_coord_nearest(GLenum wrapMode
, GLfloat coord
, GLint max
)
430 return IFLOOR( CLAMP(coord
, 0.0F
, max
- 1) );
431 case GL_CLAMP_TO_EDGE
:
432 return IFLOOR( CLAMP(coord
, 0.5F
, max
- 0.5F
) );
433 case GL_CLAMP_TO_BORDER
:
434 return IFLOOR( CLAMP(coord
, -0.5F
, max
+ 0.5F
) );
436 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_nearest");
443 * As above, but GL_LINEAR filtering.
446 clamp_rect_coord_linear(GLenum wrapMode
, GLfloat coord
, GLint max
,
447 GLint
*i0out
, GLint
*i1out
, GLfloat
*weight
)
453 /* Not exactly what the spec says, but it matches NVIDIA output */
454 fcol
= CLAMP(coord
- 0.5F
, 0.0F
, max
- 1);
458 case GL_CLAMP_TO_EDGE
:
459 fcol
= CLAMP(coord
, 0.5F
, max
- 0.5F
);
466 case GL_CLAMP_TO_BORDER
:
467 fcol
= CLAMP(coord
, -0.5F
, max
+ 0.5F
);
473 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_linear");
480 *weight
= FRAC(fcol
);
485 * Compute slice/image to use for 1D or 2D array texture.
488 tex_array_slice(GLfloat coord
, GLsizei size
)
490 GLint slice
= IFLOOR(coord
+ 0.5f
);
491 slice
= CLAMP(slice
, 0, size
- 1);
497 * Compute nearest integer texcoords for given texobj and coordinate.
498 * NOTE: only used for depth texture sampling.
501 nearest_texcoord(const struct gl_texture_object
*texObj
,
503 const GLfloat texcoord
[4],
504 GLint
*i
, GLint
*j
, GLint
*k
)
506 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
507 const GLint width
= img
->Width
;
508 const GLint height
= img
->Height
;
509 const GLint depth
= img
->Depth
;
511 switch (texObj
->Target
) {
512 case GL_TEXTURE_RECTANGLE_ARB
:
513 *i
= clamp_rect_coord_nearest(texObj
->Sampler
.WrapS
, texcoord
[0], width
);
514 *j
= clamp_rect_coord_nearest(texObj
->Sampler
.WrapT
, texcoord
[1], height
);
518 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
523 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
524 *j
= nearest_texel_location(texObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
527 case GL_TEXTURE_1D_ARRAY_EXT
:
528 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
529 *j
= tex_array_slice(texcoord
[1], height
);
532 case GL_TEXTURE_2D_ARRAY_EXT
:
533 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
534 *j
= nearest_texel_location(texObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
535 *k
= tex_array_slice(texcoord
[2], depth
);
545 * Compute linear integer texcoords for given texobj and coordinate.
546 * NOTE: only used for depth texture sampling.
549 linear_texcoord(const struct gl_texture_object
*texObj
,
551 const GLfloat texcoord
[4],
552 GLint
*i0
, GLint
*i1
, GLint
*j0
, GLint
*j1
, GLint
*slice
,
553 GLfloat
*wi
, GLfloat
*wj
)
555 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
556 const GLint width
= img
->Width
;
557 const GLint height
= img
->Height
;
558 const GLint depth
= img
->Depth
;
560 switch (texObj
->Target
) {
561 case GL_TEXTURE_RECTANGLE_ARB
:
562 clamp_rect_coord_linear(texObj
->Sampler
.WrapS
, texcoord
[0],
564 clamp_rect_coord_linear(texObj
->Sampler
.WrapT
, texcoord
[1],
571 linear_texel_locations(texObj
->Sampler
.WrapS
, img
, width
,
572 texcoord
[0], i0
, i1
, wi
);
573 linear_texel_locations(texObj
->Sampler
.WrapT
, img
, height
,
574 texcoord
[1], j0
, j1
, wj
);
578 case GL_TEXTURE_1D_ARRAY_EXT
:
579 linear_texel_locations(texObj
->Sampler
.WrapS
, img
, width
,
580 texcoord
[0], i0
, i1
, wi
);
581 *j0
= tex_array_slice(texcoord
[1], height
);
586 case GL_TEXTURE_2D_ARRAY_EXT
:
587 linear_texel_locations(texObj
->Sampler
.WrapS
, img
, width
,
588 texcoord
[0], i0
, i1
, wi
);
589 linear_texel_locations(texObj
->Sampler
.WrapT
, img
, height
,
590 texcoord
[1], j0
, j1
, wj
);
591 *slice
= tex_array_slice(texcoord
[2], depth
);
603 * For linear interpolation between mipmap levels N and N+1, this function
607 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
610 return tObj
->BaseLevel
;
611 else if (lambda
> tObj
->_MaxLambda
)
612 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
614 return (GLint
) (tObj
->BaseLevel
+ lambda
);
619 * Compute the nearest mipmap level to take texels from.
622 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
628 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
629 l
= tObj
->_MaxLambda
+ 0.4999F
;
632 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
633 if (level
> tObj
->_MaxLevel
)
634 level
= tObj
->_MaxLevel
;
641 * Bitflags for texture border color sampling.
653 * The lambda[] array values are always monotonic. Either the whole span
654 * will be minified, magnified, or split between the two. This function
655 * determines the subranges in [0, n-1] that are to be minified or magnified.
658 compute_min_mag_ranges(const struct gl_texture_object
*tObj
,
659 GLuint n
, const GLfloat lambda
[],
660 GLuint
*minStart
, GLuint
*minEnd
,
661 GLuint
*magStart
, GLuint
*magEnd
)
663 GLfloat minMagThresh
;
665 /* we shouldn't be here if minfilter == magfilter */
666 ASSERT(tObj
->Sampler
.MinFilter
!= tObj
->Sampler
.MagFilter
);
668 /* This bit comes from the OpenGL spec: */
669 if (tObj
->Sampler
.MagFilter
== GL_LINEAR
670 && (tObj
->Sampler
.MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
671 tObj
->Sampler
.MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
679 /* DEBUG CODE: Verify that lambda[] is monotonic.
680 * We can't really use this because the inaccuracy in the LOG2 function
681 * causes this test to fail, yet the resulting texturing is correct.
685 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
686 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
687 for (i
= 0; i
< n
- 1; i
++) {
688 ASSERT((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
691 else { /* increasing */
692 for (i
= 0; i
< n
- 1; i
++) {
693 ASSERT((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
699 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
700 /* magnification for whole span */
703 *minStart
= *minEnd
= 0;
705 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
706 /* minification for whole span */
709 *magStart
= *magEnd
= 0;
712 /* a mix of minification and magnification */
714 if (lambda
[0] > minMagThresh
) {
715 /* start with minification */
716 for (i
= 1; i
< n
; i
++) {
717 if (lambda
[i
] <= minMagThresh
)
726 /* start with magnification */
727 for (i
= 1; i
< n
; i
++) {
728 if (lambda
[i
] > minMagThresh
)
739 /* Verify the min/mag Start/End values
740 * We don't use this either (see above)
744 for (i
= 0; i
< n
; i
++) {
745 if (lambda
[i
] > minMagThresh
) {
747 ASSERT(i
>= *minStart
);
752 ASSERT(i
>= *magStart
);
762 * When we sample the border color, it must be interpreted according to
763 * the base texture format. Ex: if the texture base format it GL_ALPHA,
764 * we return (0,0,0,BorderAlpha).
767 get_border_color(const struct gl_texture_object
*tObj
,
768 const struct gl_texture_image
*img
,
771 switch (img
->_BaseFormat
) {
773 rgba
[0] = tObj
->Sampler
.BorderColor
.f
[0];
774 rgba
[1] = tObj
->Sampler
.BorderColor
.f
[1];
775 rgba
[2] = tObj
->Sampler
.BorderColor
.f
[2];
779 rgba
[0] = rgba
[1] = rgba
[2] = 0.0;
780 rgba
[3] = tObj
->Sampler
.BorderColor
.f
[3];
783 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->Sampler
.BorderColor
.f
[0];
786 case GL_LUMINANCE_ALPHA
:
787 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->Sampler
.BorderColor
.f
[0];
788 rgba
[3] = tObj
->Sampler
.BorderColor
.f
[3];
791 rgba
[0] = rgba
[1] = rgba
[2] = rgba
[3] = tObj
->Sampler
.BorderColor
.f
[0];
794 COPY_4V(rgba
, tObj
->Sampler
.BorderColor
.f
);
800 /**********************************************************************/
801 /* 1-D Texture Sampling Functions */
802 /**********************************************************************/
805 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
808 sample_1d_nearest(struct gl_context
*ctx
,
809 const struct gl_texture_object
*tObj
,
810 const struct gl_texture_image
*img
,
811 const GLfloat texcoord
[4], GLfloat rgba
[4])
813 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
814 const GLint width
= img
->Width2
; /* without border, power of two */
816 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
817 /* skip over the border, if any */
819 if (i
< 0 || i
>= (GLint
) img
->Width
) {
820 /* Need this test for GL_CLAMP_TO_BORDER mode */
821 get_border_color(tObj
, img
, rgba
);
824 swImg
->FetchTexel(swImg
, i
, 0, 0, rgba
);
830 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
833 sample_1d_linear(struct gl_context
*ctx
,
834 const struct gl_texture_object
*tObj
,
835 const struct gl_texture_image
*img
,
836 const GLfloat texcoord
[4], GLfloat rgba
[4])
838 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
839 const GLint width
= img
->Width2
;
841 GLbitfield useBorderColor
= 0x0;
843 GLfloat t0
[4], t1
[4]; /* texels */
845 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
852 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
853 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
856 /* fetch texel colors */
857 if (useBorderColor
& I0BIT
) {
858 get_border_color(tObj
, img
, t0
);
861 swImg
->FetchTexel(swImg
, i0
, 0, 0, t0
);
863 if (useBorderColor
& I1BIT
) {
864 get_border_color(tObj
, img
, t1
);
867 swImg
->FetchTexel(swImg
, i1
, 0, 0, t1
);
870 lerp_rgba(rgba
, a
, t0
, t1
);
875 sample_1d_nearest_mipmap_nearest(struct gl_context
*ctx
,
876 const struct gl_texture_object
*tObj
,
877 GLuint n
, const GLfloat texcoord
[][4],
878 const GLfloat lambda
[], GLfloat rgba
[][4])
881 ASSERT(lambda
!= NULL
);
882 for (i
= 0; i
< n
; i
++) {
883 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
884 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
890 sample_1d_linear_mipmap_nearest(struct gl_context
*ctx
,
891 const struct gl_texture_object
*tObj
,
892 GLuint n
, const GLfloat texcoord
[][4],
893 const GLfloat lambda
[], GLfloat rgba
[][4])
896 ASSERT(lambda
!= NULL
);
897 for (i
= 0; i
< n
; i
++) {
898 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
899 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
905 sample_1d_nearest_mipmap_linear(struct gl_context
*ctx
,
906 const struct gl_texture_object
*tObj
,
907 GLuint n
, const GLfloat texcoord
[][4],
908 const GLfloat lambda
[], GLfloat rgba
[][4])
911 ASSERT(lambda
!= NULL
);
912 for (i
= 0; i
< n
; i
++) {
913 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
914 if (level
>= tObj
->_MaxLevel
) {
915 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
916 texcoord
[i
], rgba
[i
]);
919 GLfloat t0
[4], t1
[4];
920 const GLfloat f
= FRAC(lambda
[i
]);
921 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
922 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
923 lerp_rgba(rgba
[i
], f
, t0
, t1
);
930 sample_1d_linear_mipmap_linear(struct gl_context
*ctx
,
931 const struct gl_texture_object
*tObj
,
932 GLuint n
, const GLfloat texcoord
[][4],
933 const GLfloat lambda
[], GLfloat rgba
[][4])
936 ASSERT(lambda
!= NULL
);
937 for (i
= 0; i
< n
; i
++) {
938 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
939 if (level
>= tObj
->_MaxLevel
) {
940 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
941 texcoord
[i
], rgba
[i
]);
944 GLfloat t0
[4], t1
[4];
945 const GLfloat f
= FRAC(lambda
[i
]);
946 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
947 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
948 lerp_rgba(rgba
[i
], f
, t0
, t1
);
954 /** Sample 1D texture, nearest filtering for both min/magnification */
956 sample_nearest_1d( struct gl_context
*ctx
,
957 const struct gl_texture_object
*tObj
, GLuint n
,
958 const GLfloat texcoords
[][4], const GLfloat lambda
[],
962 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
964 for (i
= 0; i
< n
; i
++) {
965 sample_1d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
970 /** Sample 1D texture, linear filtering for both min/magnification */
972 sample_linear_1d( struct gl_context
*ctx
,
973 const struct gl_texture_object
*tObj
, GLuint n
,
974 const GLfloat texcoords
[][4], const GLfloat lambda
[],
978 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
980 for (i
= 0; i
< n
; i
++) {
981 sample_1d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
986 /** Sample 1D texture, using lambda to choose between min/magnification */
988 sample_lambda_1d( struct gl_context
*ctx
,
989 const struct gl_texture_object
*tObj
, GLuint n
,
990 const GLfloat texcoords
[][4],
991 const GLfloat lambda
[], GLfloat rgba
[][4] )
993 GLuint minStart
, minEnd
; /* texels with minification */
994 GLuint magStart
, magEnd
; /* texels with magnification */
997 ASSERT(lambda
!= NULL
);
998 compute_min_mag_ranges(tObj
, n
, lambda
,
999 &minStart
, &minEnd
, &magStart
, &magEnd
);
1001 if (minStart
< minEnd
) {
1002 /* do the minified texels */
1003 const GLuint m
= minEnd
- minStart
;
1004 switch (tObj
->Sampler
.MinFilter
) {
1006 for (i
= minStart
; i
< minEnd
; i
++)
1007 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1008 texcoords
[i
], rgba
[i
]);
1011 for (i
= minStart
; i
< minEnd
; i
++)
1012 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1013 texcoords
[i
], rgba
[i
]);
1015 case GL_NEAREST_MIPMAP_NEAREST
:
1016 sample_1d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1017 lambda
+ minStart
, rgba
+ minStart
);
1019 case GL_LINEAR_MIPMAP_NEAREST
:
1020 sample_1d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1021 lambda
+ minStart
, rgba
+ minStart
);
1023 case GL_NEAREST_MIPMAP_LINEAR
:
1024 sample_1d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1025 lambda
+ minStart
, rgba
+ minStart
);
1027 case GL_LINEAR_MIPMAP_LINEAR
:
1028 sample_1d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1029 lambda
+ minStart
, rgba
+ minStart
);
1032 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
1037 if (magStart
< magEnd
) {
1038 /* do the magnified texels */
1039 switch (tObj
->Sampler
.MagFilter
) {
1041 for (i
= magStart
; i
< magEnd
; i
++)
1042 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1043 texcoords
[i
], rgba
[i
]);
1046 for (i
= magStart
; i
< magEnd
; i
++)
1047 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1048 texcoords
[i
], rgba
[i
]);
1051 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
1058 /**********************************************************************/
1059 /* 2-D Texture Sampling Functions */
1060 /**********************************************************************/
1064 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1067 sample_2d_nearest(struct gl_context
*ctx
,
1068 const struct gl_texture_object
*tObj
,
1069 const struct gl_texture_image
*img
,
1070 const GLfloat texcoord
[4],
1073 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1074 const GLint width
= img
->Width2
; /* without border, power of two */
1075 const GLint height
= img
->Height2
; /* without border, power of two */
1079 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
1080 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
1082 /* skip over the border, if any */
1086 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
1087 /* Need this test for GL_CLAMP_TO_BORDER mode */
1088 get_border_color(tObj
, img
, rgba
);
1091 swImg
->FetchTexel(swImg
, i
, j
, 0, rgba
);
1097 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1098 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1101 sample_2d_linear(struct gl_context
*ctx
,
1102 const struct gl_texture_object
*tObj
,
1103 const struct gl_texture_image
*img
,
1104 const GLfloat texcoord
[4],
1107 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1108 const GLint width
= img
->Width2
;
1109 const GLint height
= img
->Height2
;
1110 GLint i0
, j0
, i1
, j1
;
1111 GLbitfield useBorderColor
= 0x0;
1113 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1115 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1116 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1125 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1126 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1127 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1128 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1131 /* fetch four texel colors */
1132 if (useBorderColor
& (I0BIT
| J0BIT
)) {
1133 get_border_color(tObj
, img
, t00
);
1136 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
1138 if (useBorderColor
& (I1BIT
| J0BIT
)) {
1139 get_border_color(tObj
, img
, t10
);
1142 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
1144 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1145 get_border_color(tObj
, img
, t01
);
1148 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
1150 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1151 get_border_color(tObj
, img
, t11
);
1154 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
1157 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1162 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1163 * We don't have to worry about the texture border.
1166 sample_2d_linear_repeat(struct gl_context
*ctx
,
1167 const struct gl_texture_object
*tObj
,
1168 const struct gl_texture_image
*img
,
1169 const GLfloat texcoord
[4],
1172 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1173 const GLint width
= img
->Width2
;
1174 const GLint height
= img
->Height2
;
1175 GLint i0
, j0
, i1
, j1
;
1177 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1181 ASSERT(tObj
->Sampler
.WrapS
== GL_REPEAT
);
1182 ASSERT(tObj
->Sampler
.WrapT
== GL_REPEAT
);
1183 ASSERT(img
->Border
== 0);
1184 ASSERT(swImg
->_IsPowerOfTwo
);
1186 linear_repeat_texel_location(width
, texcoord
[0], &i0
, &i1
, &wi
);
1187 linear_repeat_texel_location(height
, texcoord
[1], &j0
, &j1
, &wj
);
1189 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
1190 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
1191 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
1192 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
1194 lerp_rgba_2d(rgba
, wi
, wj
, t00
, t10
, t01
, t11
);
1199 sample_2d_nearest_mipmap_nearest(struct gl_context
*ctx
,
1200 const struct gl_texture_object
*tObj
,
1201 GLuint n
, const GLfloat texcoord
[][4],
1202 const GLfloat lambda
[], GLfloat rgba
[][4])
1205 for (i
= 0; i
< n
; i
++) {
1206 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1207 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1213 sample_2d_linear_mipmap_nearest(struct gl_context
*ctx
,
1214 const struct gl_texture_object
*tObj
,
1215 GLuint n
, const GLfloat texcoord
[][4],
1216 const GLfloat lambda
[], GLfloat rgba
[][4])
1219 ASSERT(lambda
!= NULL
);
1220 for (i
= 0; i
< n
; i
++) {
1221 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1222 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1228 sample_2d_nearest_mipmap_linear(struct gl_context
*ctx
,
1229 const struct gl_texture_object
*tObj
,
1230 GLuint n
, const GLfloat texcoord
[][4],
1231 const GLfloat lambda
[], GLfloat rgba
[][4])
1234 ASSERT(lambda
!= NULL
);
1235 for (i
= 0; i
< n
; i
++) {
1236 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1237 if (level
>= tObj
->_MaxLevel
) {
1238 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1239 texcoord
[i
], rgba
[i
]);
1242 GLfloat t0
[4], t1
[4]; /* texels */
1243 const GLfloat f
= FRAC(lambda
[i
]);
1244 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1245 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1246 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1253 sample_2d_linear_mipmap_linear( struct gl_context
*ctx
,
1254 const struct gl_texture_object
*tObj
,
1255 GLuint n
, const GLfloat texcoord
[][4],
1256 const GLfloat lambda
[], GLfloat rgba
[][4] )
1259 ASSERT(lambda
!= NULL
);
1260 for (i
= 0; i
< n
; i
++) {
1261 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1262 if (level
>= tObj
->_MaxLevel
) {
1263 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1264 texcoord
[i
], rgba
[i
]);
1267 GLfloat t0
[4], t1
[4]; /* texels */
1268 const GLfloat f
= FRAC(lambda
[i
]);
1269 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1270 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1271 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1278 sample_2d_linear_mipmap_linear_repeat(struct gl_context
*ctx
,
1279 const struct gl_texture_object
*tObj
,
1280 GLuint n
, const GLfloat texcoord
[][4],
1281 const GLfloat lambda
[], GLfloat rgba
[][4])
1284 ASSERT(lambda
!= NULL
);
1285 ASSERT(tObj
->Sampler
.WrapS
== GL_REPEAT
);
1286 ASSERT(tObj
->Sampler
.WrapT
== GL_REPEAT
);
1287 for (i
= 0; i
< n
; i
++) {
1288 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1289 if (level
>= tObj
->_MaxLevel
) {
1290 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1291 texcoord
[i
], rgba
[i
]);
1294 GLfloat t0
[4], t1
[4]; /* texels */
1295 const GLfloat f
= FRAC(lambda
[i
]);
1296 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
],
1298 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
+1],
1300 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1306 /** Sample 2D texture, nearest filtering for both min/magnification */
1308 sample_nearest_2d(struct gl_context
*ctx
,
1309 const struct gl_texture_object
*tObj
, GLuint n
,
1310 const GLfloat texcoords
[][4],
1311 const GLfloat lambda
[], GLfloat rgba
[][4])
1314 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1316 for (i
= 0; i
< n
; i
++) {
1317 sample_2d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1322 /** Sample 2D texture, linear filtering for both min/magnification */
1324 sample_linear_2d(struct gl_context
*ctx
,
1325 const struct gl_texture_object
*tObj
, GLuint n
,
1326 const GLfloat texcoords
[][4],
1327 const GLfloat lambda
[], GLfloat rgba
[][4])
1330 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1331 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(image
);
1333 if (tObj
->Sampler
.WrapS
== GL_REPEAT
&&
1334 tObj
->Sampler
.WrapT
== GL_REPEAT
&&
1335 swImg
->_IsPowerOfTwo
&&
1336 image
->Border
== 0) {
1337 for (i
= 0; i
< n
; i
++) {
1338 sample_2d_linear_repeat(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1342 for (i
= 0; i
< n
; i
++) {
1343 sample_2d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1350 * Optimized 2-D texture sampling:
1351 * S and T wrap mode == GL_REPEAT
1352 * GL_NEAREST min/mag filter
1354 * RowStride == Width,
1358 opt_sample_rgb_2d(struct gl_context
*ctx
,
1359 const struct gl_texture_object
*tObj
,
1360 GLuint n
, const GLfloat texcoords
[][4],
1361 const GLfloat lambda
[], GLfloat rgba
[][4])
1363 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1364 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1365 const GLfloat width
= (GLfloat
) img
->Width
;
1366 const GLfloat height
= (GLfloat
) img
->Height
;
1367 const GLint colMask
= img
->Width
- 1;
1368 const GLint rowMask
= img
->Height
- 1;
1369 const GLint shift
= img
->WidthLog2
;
1373 ASSERT(tObj
->Sampler
.WrapS
==GL_REPEAT
);
1374 ASSERT(tObj
->Sampler
.WrapT
==GL_REPEAT
);
1375 ASSERT(img
->Border
==0);
1376 ASSERT(img
->TexFormat
== MESA_FORMAT_RGB888
);
1377 ASSERT(swImg
->_IsPowerOfTwo
);
1380 for (k
=0; k
<n
; k
++) {
1381 GLint i
= IFLOOR(texcoords
[k
][0] * width
) & colMask
;
1382 GLint j
= IFLOOR(texcoords
[k
][1] * height
) & rowMask
;
1383 GLint pos
= (j
<< shift
) | i
;
1384 GLubyte
*texel
= swImg
->Map
+ 3 * pos
;
1385 rgba
[k
][RCOMP
] = UBYTE_TO_FLOAT(texel
[2]);
1386 rgba
[k
][GCOMP
] = UBYTE_TO_FLOAT(texel
[1]);
1387 rgba
[k
][BCOMP
] = UBYTE_TO_FLOAT(texel
[0]);
1388 rgba
[k
][ACOMP
] = 1.0F
;
1394 * Optimized 2-D texture sampling:
1395 * S and T wrap mode == GL_REPEAT
1396 * GL_NEAREST min/mag filter
1398 * RowStride == Width,
1402 opt_sample_rgba_2d(struct gl_context
*ctx
,
1403 const struct gl_texture_object
*tObj
,
1404 GLuint n
, const GLfloat texcoords
[][4],
1405 const GLfloat lambda
[], GLfloat rgba
[][4])
1407 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1408 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1409 const GLfloat width
= (GLfloat
) img
->Width
;
1410 const GLfloat height
= (GLfloat
) img
->Height
;
1411 const GLint colMask
= img
->Width
- 1;
1412 const GLint rowMask
= img
->Height
- 1;
1413 const GLint shift
= img
->WidthLog2
;
1417 ASSERT(tObj
->Sampler
.WrapS
==GL_REPEAT
);
1418 ASSERT(tObj
->Sampler
.WrapT
==GL_REPEAT
);
1419 ASSERT(img
->Border
==0);
1420 ASSERT(img
->TexFormat
== MESA_FORMAT_RGBA8888
);
1421 ASSERT(swImg
->_IsPowerOfTwo
);
1424 for (i
= 0; i
< n
; i
++) {
1425 const GLint col
= IFLOOR(texcoords
[i
][0] * width
) & colMask
;
1426 const GLint row
= IFLOOR(texcoords
[i
][1] * height
) & rowMask
;
1427 const GLint pos
= (row
<< shift
) | col
;
1428 const GLuint texel
= *((GLuint
*) swImg
->Map
+ pos
);
1429 rgba
[i
][RCOMP
] = UBYTE_TO_FLOAT( (texel
>> 24) );
1430 rgba
[i
][GCOMP
] = UBYTE_TO_FLOAT( (texel
>> 16) & 0xff );
1431 rgba
[i
][BCOMP
] = UBYTE_TO_FLOAT( (texel
>> 8) & 0xff );
1432 rgba
[i
][ACOMP
] = UBYTE_TO_FLOAT( (texel
) & 0xff );
1437 /** Sample 2D texture, using lambda to choose between min/magnification */
1439 sample_lambda_2d(struct gl_context
*ctx
,
1440 const struct gl_texture_object
*tObj
,
1441 GLuint n
, const GLfloat texcoords
[][4],
1442 const GLfloat lambda
[], GLfloat rgba
[][4])
1444 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1445 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(tImg
);
1446 GLuint minStart
, minEnd
; /* texels with minification */
1447 GLuint magStart
, magEnd
; /* texels with magnification */
1449 const GLboolean repeatNoBorderPOT
= (tObj
->Sampler
.WrapS
== GL_REPEAT
)
1450 && (tObj
->Sampler
.WrapT
== GL_REPEAT
)
1451 && (tImg
->Border
== 0 && (tImg
->Width
== swImg
->RowStride
))
1452 && swImg
->_IsPowerOfTwo
;
1454 ASSERT(lambda
!= NULL
);
1455 compute_min_mag_ranges(tObj
, n
, lambda
,
1456 &minStart
, &minEnd
, &magStart
, &magEnd
);
1458 if (minStart
< minEnd
) {
1459 /* do the minified texels */
1460 const GLuint m
= minEnd
- minStart
;
1461 switch (tObj
->Sampler
.MinFilter
) {
1463 if (repeatNoBorderPOT
) {
1464 switch (tImg
->TexFormat
) {
1465 case MESA_FORMAT_RGB888
:
1466 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1467 NULL
, rgba
+ minStart
);
1469 case MESA_FORMAT_RGBA8888
:
1470 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1471 NULL
, rgba
+ minStart
);
1474 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1475 NULL
, rgba
+ minStart
);
1479 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1480 NULL
, rgba
+ minStart
);
1484 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1485 NULL
, rgba
+ minStart
);
1487 case GL_NEAREST_MIPMAP_NEAREST
:
1488 sample_2d_nearest_mipmap_nearest(ctx
, tObj
, m
,
1489 texcoords
+ minStart
,
1490 lambda
+ minStart
, rgba
+ minStart
);
1492 case GL_LINEAR_MIPMAP_NEAREST
:
1493 sample_2d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1494 lambda
+ minStart
, rgba
+ minStart
);
1496 case GL_NEAREST_MIPMAP_LINEAR
:
1497 sample_2d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1498 lambda
+ minStart
, rgba
+ minStart
);
1500 case GL_LINEAR_MIPMAP_LINEAR
:
1501 if (repeatNoBorderPOT
)
1502 sample_2d_linear_mipmap_linear_repeat(ctx
, tObj
, m
,
1503 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1505 sample_2d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1506 lambda
+ minStart
, rgba
+ minStart
);
1509 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1514 if (magStart
< magEnd
) {
1515 /* do the magnified texels */
1516 const GLuint m
= magEnd
- magStart
;
1518 switch (tObj
->Sampler
.MagFilter
) {
1520 if (repeatNoBorderPOT
) {
1521 switch (tImg
->TexFormat
) {
1522 case MESA_FORMAT_RGB888
:
1523 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1524 NULL
, rgba
+ magStart
);
1526 case MESA_FORMAT_RGBA8888
:
1527 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1528 NULL
, rgba
+ magStart
);
1531 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1532 NULL
, rgba
+ magStart
);
1536 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1537 NULL
, rgba
+ magStart
);
1541 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1542 NULL
, rgba
+ magStart
);
1545 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1552 /* For anisotropic filtering */
1553 #define WEIGHT_LUT_SIZE 1024
1555 static GLfloat
*weightLut
= NULL
;
1558 * Creates the look-up table used to speed-up EWA sampling
1561 create_filter_table(void)
1565 weightLut
= (GLfloat
*) malloc(WEIGHT_LUT_SIZE
* sizeof(GLfloat
));
1567 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
1569 GLfloat r2
= (GLfloat
) i
/ (GLfloat
) (WEIGHT_LUT_SIZE
- 1);
1570 GLfloat weight
= (GLfloat
) exp(-alpha
* r2
);
1571 weightLut
[i
] = weight
;
1578 * Elliptical weighted average (EWA) filter for producing high quality
1579 * anisotropic filtered results.
1580 * Based on the Higher Quality Elliptical Weighted Avarage Filter
1581 * published by Paul S. Heckbert in his Master's Thesis
1582 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1585 sample_2d_ewa(struct gl_context
*ctx
,
1586 const struct gl_texture_object
*tObj
,
1587 const GLfloat texcoord
[4],
1588 const GLfloat dudx
, const GLfloat dvdx
,
1589 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1592 GLint level
= lod
> 0 ? lod
: 0;
1593 GLfloat scaling
= 1.0 / (1 << level
);
1594 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1595 const struct gl_texture_image
*mostDetailedImage
=
1596 tObj
->Image
[0][tObj
->BaseLevel
];
1597 const struct swrast_texture_image
*swImg
=
1598 swrast_texture_image_const(mostDetailedImage
);
1599 GLfloat tex_u
=-0.5 + texcoord
[0] * swImg
->WidthScale
* scaling
;
1600 GLfloat tex_v
=-0.5 + texcoord
[1] * swImg
->HeightScale
* scaling
;
1602 GLfloat ux
= dudx
* scaling
;
1603 GLfloat vx
= dvdx
* scaling
;
1604 GLfloat uy
= dudy
* scaling
;
1605 GLfloat vy
= dvdy
* scaling
;
1607 /* compute ellipse coefficients to bound the region:
1608 * A*x*x + B*x*y + C*y*y = F.
1610 GLfloat A
= vx
*vx
+vy
*vy
+1;
1611 GLfloat B
= -2*(ux
*vx
+uy
*vy
);
1612 GLfloat C
= ux
*ux
+uy
*uy
+1;
1613 GLfloat F
= A
*C
-B
*B
/4.0;
1615 /* check if it is an ellipse */
1616 /* ASSERT(F > 0.0); */
1618 /* Compute the ellipse's (u,v) bounding box in texture space */
1619 GLfloat d
= -B
*B
+4.0*C
*A
;
1620 GLfloat box_u
= 2.0 / d
* sqrt(d
*C
*F
); /* box_u -> half of bbox with */
1621 GLfloat box_v
= 2.0 / d
* sqrt(A
*d
*F
); /* box_v -> half of bbox height */
1623 GLint u0
= floor(tex_u
- box_u
);
1624 GLint u1
= ceil (tex_u
+ box_u
);
1625 GLint v0
= floor(tex_v
- box_v
);
1626 GLint v1
= ceil (tex_v
+ box_v
);
1628 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1629 GLfloat newCoord
[2];
1632 GLfloat U
= u0
- tex_u
;
1635 /* Scale ellipse formula to directly index the Filter Lookup Table.
1636 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1638 double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
1642 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1644 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1645 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1646 * value, q, is less than F, we're inside the ellipse
1649 for (v
= v0
; v
<= v1
; ++v
) {
1650 GLfloat V
= v
- tex_v
;
1651 GLfloat dq
= A
* (2 * U
+ 1) + B
* V
;
1652 GLfloat q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
1655 for (u
= u0
; u
<= u1
; ++u
) {
1656 /* Note that the ellipse has been pre-scaled so F = WEIGHT_LUT_SIZE - 1 */
1657 if (q
< WEIGHT_LUT_SIZE
) {
1658 /* as a LUT is used, q must never be negative;
1659 * should not happen, though
1661 const GLint qClamped
= q
>= 0.0F
? q
: 0;
1662 GLfloat weight
= weightLut
[qClamped
];
1664 newCoord
[0] = u
/ ((GLfloat
) img
->Width2
);
1665 newCoord
[1] = v
/ ((GLfloat
) img
->Height2
);
1667 sample_2d_nearest(ctx
, tObj
, img
, newCoord
, rgba
);
1668 num
[0] += weight
* rgba
[0];
1669 num
[1] += weight
* rgba
[1];
1670 num
[2] += weight
* rgba
[2];
1671 num
[3] += weight
* rgba
[3];
1681 /* Reaching this place would mean
1682 * that no pixels intersected the ellipse.
1683 * This should never happen because
1684 * the filter we use always
1685 * intersects at least one pixel.
1692 /* not enough pixels in resampling, resort to direct interpolation */
1693 sample_2d_linear(ctx
, tObj
, img
, texcoord
, rgba
);
1697 rgba
[0] = num
[0] / den
;
1698 rgba
[1] = num
[1] / den
;
1699 rgba
[2] = num
[2] / den
;
1700 rgba
[3] = num
[3] / den
;
1705 * Anisotropic filtering using footprint assembly as outlined in the
1706 * EXT_texture_filter_anisotropic spec:
1707 * http://www.opengl.org/registry/specs/EXT/texture_filter_anisotropic.txt
1708 * Faster than EWA but has less quality (more aliasing effects)
1711 sample_2d_footprint(struct gl_context
*ctx
,
1712 const struct gl_texture_object
*tObj
,
1713 const GLfloat texcoord
[4],
1714 const GLfloat dudx
, const GLfloat dvdx
,
1715 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1718 GLint level
= lod
> 0 ? lod
: 0;
1719 GLfloat scaling
= 1.0F
/ (1 << level
);
1720 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1722 GLfloat ux
= dudx
* scaling
;
1723 GLfloat vx
= dvdx
* scaling
;
1724 GLfloat uy
= dudy
* scaling
;
1725 GLfloat vy
= dvdy
* scaling
;
1727 GLfloat Px2
= ux
* ux
+ vx
* vx
; /* squared length of dx */
1728 GLfloat Py2
= uy
* uy
+ vy
* vy
; /* squared length of dy */
1734 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1735 GLfloat newCoord
[2];
1738 /* Calculate the per anisotropic sample offsets in s,t space. */
1740 numSamples
= ceil(SQRTF(Px2
));
1741 ds
= ux
/ ((GLfloat
) img
->Width2
);
1742 dt
= vx
/ ((GLfloat
) img
->Height2
);
1745 numSamples
= ceil(SQRTF(Py2
));
1746 ds
= uy
/ ((GLfloat
) img
->Width2
);
1747 dt
= vy
/ ((GLfloat
) img
->Height2
);
1750 for (s
= 0; s
<numSamples
; s
++) {
1751 newCoord
[0] = texcoord
[0] + ds
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5);
1752 newCoord
[1] = texcoord
[1] + dt
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5);
1754 sample_2d_linear(ctx
, tObj
, img
, newCoord
, rgba
);
1761 rgba
[0] = num
[0] / numSamples
;
1762 rgba
[1] = num
[1] / numSamples
;
1763 rgba
[2] = num
[2] / numSamples
;
1764 rgba
[3] = num
[3] / numSamples
;
1769 * Returns the index of the specified texture object in the
1770 * gl_context texture unit array.
1772 static inline GLuint
1773 texture_unit_index(const struct gl_context
*ctx
,
1774 const struct gl_texture_object
*tObj
)
1776 const GLuint maxUnit
1777 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
1780 /* XXX CoordUnits vs. ImageUnits */
1781 for (u
= 0; u
< maxUnit
; u
++) {
1782 if (ctx
->Texture
.Unit
[u
]._Current
== tObj
)
1786 u
= 0; /* not found, use 1st one; should never happen */
1793 * Sample 2D texture using an anisotropic filter.
1794 * NOTE: the const GLfloat lambda_iso[] parameter does *NOT* contain
1795 * the lambda float array but a "hidden" SWspan struct which is required
1796 * by this function but is not available in the texture_sample_func signature.
1797 * See _swrast_texture_span( struct gl_context *ctx, SWspan *span ) on how
1798 * this function is called.
1801 sample_lambda_2d_aniso(struct gl_context
*ctx
,
1802 const struct gl_texture_object
*tObj
,
1803 GLuint n
, const GLfloat texcoords
[][4],
1804 const GLfloat lambda_iso
[], GLfloat rgba
[][4])
1806 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1807 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(tImg
);
1808 const GLfloat maxEccentricity
=
1809 tObj
->Sampler
.MaxAnisotropy
* tObj
->Sampler
.MaxAnisotropy
;
1811 /* re-calculate the lambda values so that they are usable with anisotropic
1814 SWspan
*span
= (SWspan
*)lambda_iso
; /* access the "hidden" SWspan struct */
1816 /* based on interpolate_texcoords(struct gl_context *ctx, SWspan *span)
1817 * in swrast/s_span.c
1820 /* find the texture unit index by looking up the current texture object
1821 * from the context list of available texture objects.
1823 const GLuint u
= texture_unit_index(ctx
, tObj
);
1824 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
1827 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
1828 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
1829 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
1830 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
1831 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
1832 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
1833 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
1834 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
1835 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
1837 /* from swrast/s_texcombine.c _swrast_texture_span */
1838 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[u
];
1839 const GLboolean adjustLOD
=
1840 (texUnit
->LodBias
+ tObj
->Sampler
.LodBias
!= 0.0F
)
1841 || (tObj
->Sampler
.MinLod
!= -1000.0 || tObj
->Sampler
.MaxLod
!= 1000.0);
1845 /* on first access create the lookup table containing the filter weights. */
1847 create_filter_table();
1850 texW
= swImg
->WidthScale
;
1851 texH
= swImg
->HeightScale
;
1853 for (i
= 0; i
< n
; i
++) {
1854 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
1856 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
1857 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
1858 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
1859 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
1861 /* note: instead of working with Px and Py, we will use the
1862 * squared length instead, to avoid sqrt.
1864 GLfloat Px2
= dudx
* dudx
+ dvdx
* dvdx
;
1865 GLfloat Py2
= dudy
* dudy
+ dvdy
* dvdy
;
1885 /* if the eccentricity of the ellipse is too big, scale up the shorter
1886 * of the two vectors to limit the maximum amount of work per pixel
1889 if (e
> maxEccentricity
) {
1890 /* GLfloat s=e / maxEccentricity;
1894 Pmin2
= Pmax2
/ maxEccentricity
;
1897 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
1898 * this since 0.5*log(x) = log(sqrt(x))
1900 lod
= 0.5 * LOG2(Pmin2
);
1903 /* from swrast/s_texcombine.c _swrast_texture_span */
1904 if (texUnit
->LodBias
+ tObj
->Sampler
.LodBias
!= 0.0F
) {
1905 /* apply LOD bias, but don't clamp yet */
1906 const GLfloat bias
=
1907 CLAMP(texUnit
->LodBias
+ tObj
->Sampler
.LodBias
,
1908 -ctx
->Const
.MaxTextureLodBias
,
1909 ctx
->Const
.MaxTextureLodBias
);
1912 if (tObj
->Sampler
.MinLod
!= -1000.0 ||
1913 tObj
->Sampler
.MaxLod
!= 1000.0) {
1914 /* apply LOD clamping to lambda */
1915 lod
= CLAMP(lod
, tObj
->Sampler
.MinLod
, tObj
->Sampler
.MaxLod
);
1920 /* If the ellipse covers the whole image, we can
1921 * simply return the average of the whole image.
1923 if (lod
>= tObj
->_MaxLevel
) {
1924 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1925 texcoords
[i
], rgba
[i
]);
1928 /* don't bother interpolating between multiple LODs; it doesn't
1929 * seem to be worth the extra running time.
1931 sample_2d_ewa(ctx
, tObj
, texcoords
[i
],
1932 dudx
, dvdx
, dudy
, dvdy
, floor(lod
), rgba
[i
]);
1935 (void) sample_2d_footprint
;
1937 sample_2d_footprint(ctx, tObj, texcoords[i],
1938 dudx, dvdx, dudy, dvdy, floor(lod), rgba[i]);
1946 /**********************************************************************/
1947 /* 3-D Texture Sampling Functions */
1948 /**********************************************************************/
1951 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1954 sample_3d_nearest(struct gl_context
*ctx
,
1955 const struct gl_texture_object
*tObj
,
1956 const struct gl_texture_image
*img
,
1957 const GLfloat texcoord
[4],
1960 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1961 const GLint width
= img
->Width2
; /* without border, power of two */
1962 const GLint height
= img
->Height2
; /* without border, power of two */
1963 const GLint depth
= img
->Depth2
; /* without border, power of two */
1967 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
1968 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
1969 k
= nearest_texel_location(tObj
->Sampler
.WrapR
, img
, depth
, texcoord
[2]);
1971 if (i
< 0 || i
>= (GLint
) img
->Width
||
1972 j
< 0 || j
>= (GLint
) img
->Height
||
1973 k
< 0 || k
>= (GLint
) img
->Depth
) {
1974 /* Need this test for GL_CLAMP_TO_BORDER mode */
1975 get_border_color(tObj
, img
, rgba
);
1978 swImg
->FetchTexel(swImg
, i
, j
, k
, rgba
);
1984 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1987 sample_3d_linear(struct gl_context
*ctx
,
1988 const struct gl_texture_object
*tObj
,
1989 const struct gl_texture_image
*img
,
1990 const GLfloat texcoord
[4],
1993 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1994 const GLint width
= img
->Width2
;
1995 const GLint height
= img
->Height2
;
1996 const GLint depth
= img
->Depth2
;
1997 GLint i0
, j0
, k0
, i1
, j1
, k1
;
1998 GLbitfield useBorderColor
= 0x0;
2000 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
2001 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
2003 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2004 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2005 linear_texel_locations(tObj
->Sampler
.WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
2016 /* check if sampling texture border color */
2017 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2018 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2019 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2020 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2021 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
2022 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
2026 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
2027 get_border_color(tObj
, img
, t000
);
2030 swImg
->FetchTexel(swImg
, i0
, j0
, k0
, t000
);
2032 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
2033 get_border_color(tObj
, img
, t100
);
2036 swImg
->FetchTexel(swImg
, i1
, j0
, k0
, t100
);
2038 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
2039 get_border_color(tObj
, img
, t010
);
2042 swImg
->FetchTexel(swImg
, i0
, j1
, k0
, t010
);
2044 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
2045 get_border_color(tObj
, img
, t110
);
2048 swImg
->FetchTexel(swImg
, i1
, j1
, k0
, t110
);
2051 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
2052 get_border_color(tObj
, img
, t001
);
2055 swImg
->FetchTexel(swImg
, i0
, j0
, k1
, t001
);
2057 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
2058 get_border_color(tObj
, img
, t101
);
2061 swImg
->FetchTexel(swImg
, i1
, j0
, k1
, t101
);
2063 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
2064 get_border_color(tObj
, img
, t011
);
2067 swImg
->FetchTexel(swImg
, i0
, j1
, k1
, t011
);
2069 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
2070 get_border_color(tObj
, img
, t111
);
2073 swImg
->FetchTexel(swImg
, i1
, j1
, k1
, t111
);
2076 /* trilinear interpolation of samples */
2077 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
2082 sample_3d_nearest_mipmap_nearest(struct gl_context
*ctx
,
2083 const struct gl_texture_object
*tObj
,
2084 GLuint n
, const GLfloat texcoord
[][4],
2085 const GLfloat lambda
[], GLfloat rgba
[][4] )
2088 for (i
= 0; i
< n
; i
++) {
2089 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2090 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2096 sample_3d_linear_mipmap_nearest(struct gl_context
*ctx
,
2097 const struct gl_texture_object
*tObj
,
2098 GLuint n
, const GLfloat texcoord
[][4],
2099 const GLfloat lambda
[], GLfloat rgba
[][4])
2102 ASSERT(lambda
!= NULL
);
2103 for (i
= 0; i
< n
; i
++) {
2104 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2105 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2111 sample_3d_nearest_mipmap_linear(struct gl_context
*ctx
,
2112 const struct gl_texture_object
*tObj
,
2113 GLuint n
, const GLfloat texcoord
[][4],
2114 const GLfloat lambda
[], GLfloat rgba
[][4])
2117 ASSERT(lambda
!= NULL
);
2118 for (i
= 0; i
< n
; i
++) {
2119 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2120 if (level
>= tObj
->_MaxLevel
) {
2121 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2122 texcoord
[i
], rgba
[i
]);
2125 GLfloat t0
[4], t1
[4]; /* texels */
2126 const GLfloat f
= FRAC(lambda
[i
]);
2127 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2128 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2129 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2136 sample_3d_linear_mipmap_linear(struct gl_context
*ctx
,
2137 const struct gl_texture_object
*tObj
,
2138 GLuint n
, const GLfloat texcoord
[][4],
2139 const GLfloat lambda
[], GLfloat rgba
[][4])
2142 ASSERT(lambda
!= NULL
);
2143 for (i
= 0; i
< n
; i
++) {
2144 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2145 if (level
>= tObj
->_MaxLevel
) {
2146 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2147 texcoord
[i
], rgba
[i
]);
2150 GLfloat t0
[4], t1
[4]; /* texels */
2151 const GLfloat f
= FRAC(lambda
[i
]);
2152 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2153 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2154 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2160 /** Sample 3D texture, nearest filtering for both min/magnification */
2162 sample_nearest_3d(struct gl_context
*ctx
,
2163 const struct gl_texture_object
*tObj
, GLuint n
,
2164 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2168 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2170 for (i
= 0; i
< n
; i
++) {
2171 sample_3d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2176 /** Sample 3D texture, linear filtering for both min/magnification */
2178 sample_linear_3d(struct gl_context
*ctx
,
2179 const struct gl_texture_object
*tObj
, GLuint n
,
2180 const GLfloat texcoords
[][4],
2181 const GLfloat lambda
[], GLfloat rgba
[][4])
2184 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2186 for (i
= 0; i
< n
; i
++) {
2187 sample_3d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2192 /** Sample 3D texture, using lambda to choose between min/magnification */
2194 sample_lambda_3d(struct gl_context
*ctx
,
2195 const struct gl_texture_object
*tObj
, GLuint n
,
2196 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2199 GLuint minStart
, minEnd
; /* texels with minification */
2200 GLuint magStart
, magEnd
; /* texels with magnification */
2203 ASSERT(lambda
!= NULL
);
2204 compute_min_mag_ranges(tObj
, n
, lambda
,
2205 &minStart
, &minEnd
, &magStart
, &magEnd
);
2207 if (minStart
< minEnd
) {
2208 /* do the minified texels */
2209 GLuint m
= minEnd
- minStart
;
2210 switch (tObj
->Sampler
.MinFilter
) {
2212 for (i
= minStart
; i
< minEnd
; i
++)
2213 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2214 texcoords
[i
], rgba
[i
]);
2217 for (i
= minStart
; i
< minEnd
; i
++)
2218 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2219 texcoords
[i
], rgba
[i
]);
2221 case GL_NEAREST_MIPMAP_NEAREST
:
2222 sample_3d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2223 lambda
+ minStart
, rgba
+ minStart
);
2225 case GL_LINEAR_MIPMAP_NEAREST
:
2226 sample_3d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2227 lambda
+ minStart
, rgba
+ minStart
);
2229 case GL_NEAREST_MIPMAP_LINEAR
:
2230 sample_3d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2231 lambda
+ minStart
, rgba
+ minStart
);
2233 case GL_LINEAR_MIPMAP_LINEAR
:
2234 sample_3d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2235 lambda
+ minStart
, rgba
+ minStart
);
2238 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
2243 if (magStart
< magEnd
) {
2244 /* do the magnified texels */
2245 switch (tObj
->Sampler
.MagFilter
) {
2247 for (i
= magStart
; i
< magEnd
; i
++)
2248 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2249 texcoords
[i
], rgba
[i
]);
2252 for (i
= magStart
; i
< magEnd
; i
++)
2253 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2254 texcoords
[i
], rgba
[i
]);
2257 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
2264 /**********************************************************************/
2265 /* Texture Cube Map Sampling Functions */
2266 /**********************************************************************/
2269 * Choose one of six sides of a texture cube map given the texture
2270 * coord (rx,ry,rz). Return pointer to corresponding array of texture
2273 static const struct gl_texture_image
**
2274 choose_cube_face(const struct gl_texture_object
*texObj
,
2275 const GLfloat texcoord
[4], GLfloat newCoord
[4])
2279 direction target sc tc ma
2280 ---------- ------------------------------- --- --- ---
2281 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2282 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2283 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2284 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2285 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2286 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2288 const GLfloat rx
= texcoord
[0];
2289 const GLfloat ry
= texcoord
[1];
2290 const GLfloat rz
= texcoord
[2];
2291 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
2295 if (arx
>= ary
&& arx
>= arz
) {
2309 else if (ary
>= arx
&& ary
>= arz
) {
2339 const float ima
= 1.0F
/ ma
;
2340 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
2341 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
2344 return (const struct gl_texture_image
**) texObj
->Image
[face
];
2349 sample_nearest_cube(struct gl_context
*ctx
,
2350 const struct gl_texture_object
*tObj
, GLuint n
,
2351 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2356 for (i
= 0; i
< n
; i
++) {
2357 const struct gl_texture_image
**images
;
2358 GLfloat newCoord
[4];
2359 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2360 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
2367 sample_linear_cube(struct gl_context
*ctx
,
2368 const struct gl_texture_object
*tObj
, GLuint n
,
2369 const GLfloat texcoords
[][4],
2370 const GLfloat lambda
[], GLfloat rgba
[][4])
2374 for (i
= 0; i
< n
; i
++) {
2375 const struct gl_texture_image
**images
;
2376 GLfloat newCoord
[4];
2377 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2378 sample_2d_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
2385 sample_cube_nearest_mipmap_nearest(struct gl_context
*ctx
,
2386 const struct gl_texture_object
*tObj
,
2387 GLuint n
, const GLfloat texcoord
[][4],
2388 const GLfloat lambda
[], GLfloat rgba
[][4])
2391 ASSERT(lambda
!= NULL
);
2392 for (i
= 0; i
< n
; i
++) {
2393 const struct gl_texture_image
**images
;
2394 GLfloat newCoord
[4];
2396 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2398 /* XXX we actually need to recompute lambda here based on the newCoords.
2399 * But we would need the texcoords of adjacent fragments to compute that
2400 * properly, and we don't have those here.
2401 * For now, do an approximation: subtracting 1 from the chosen mipmap
2402 * level seems to work in some test cases.
2403 * The same adjustment is done in the next few functions.
2405 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2406 level
= MAX2(level
- 1, 0);
2408 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2414 sample_cube_linear_mipmap_nearest(struct gl_context
*ctx
,
2415 const struct gl_texture_object
*tObj
,
2416 GLuint n
, const GLfloat texcoord
[][4],
2417 const GLfloat lambda
[], GLfloat rgba
[][4])
2420 ASSERT(lambda
!= NULL
);
2421 for (i
= 0; i
< n
; i
++) {
2422 const struct gl_texture_image
**images
;
2423 GLfloat newCoord
[4];
2424 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2425 level
= MAX2(level
- 1, 0); /* see comment above */
2426 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2427 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2433 sample_cube_nearest_mipmap_linear(struct gl_context
*ctx
,
2434 const struct gl_texture_object
*tObj
,
2435 GLuint n
, const GLfloat texcoord
[][4],
2436 const GLfloat lambda
[], GLfloat rgba
[][4])
2439 ASSERT(lambda
!= NULL
);
2440 for (i
= 0; i
< n
; i
++) {
2441 const struct gl_texture_image
**images
;
2442 GLfloat newCoord
[4];
2443 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2444 level
= MAX2(level
- 1, 0); /* see comment above */
2445 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2446 if (level
>= tObj
->_MaxLevel
) {
2447 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2451 GLfloat t0
[4], t1
[4]; /* texels */
2452 const GLfloat f
= FRAC(lambda
[i
]);
2453 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
2454 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2455 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2462 sample_cube_linear_mipmap_linear(struct gl_context
*ctx
,
2463 const struct gl_texture_object
*tObj
,
2464 GLuint n
, const GLfloat texcoord
[][4],
2465 const GLfloat lambda
[], GLfloat rgba
[][4])
2468 ASSERT(lambda
!= NULL
);
2469 for (i
= 0; i
< n
; i
++) {
2470 const struct gl_texture_image
**images
;
2471 GLfloat newCoord
[4];
2472 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2473 level
= MAX2(level
- 1, 0); /* see comment above */
2474 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2475 if (level
>= tObj
->_MaxLevel
) {
2476 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2480 GLfloat t0
[4], t1
[4];
2481 const GLfloat f
= FRAC(lambda
[i
]);
2482 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
2483 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2484 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2490 /** Sample cube texture, using lambda to choose between min/magnification */
2492 sample_lambda_cube(struct gl_context
*ctx
,
2493 const struct gl_texture_object
*tObj
, GLuint n
,
2494 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2497 GLuint minStart
, minEnd
; /* texels with minification */
2498 GLuint magStart
, magEnd
; /* texels with magnification */
2500 ASSERT(lambda
!= NULL
);
2501 compute_min_mag_ranges(tObj
, n
, lambda
,
2502 &minStart
, &minEnd
, &magStart
, &magEnd
);
2504 if (minStart
< minEnd
) {
2505 /* do the minified texels */
2506 const GLuint m
= minEnd
- minStart
;
2507 switch (tObj
->Sampler
.MinFilter
) {
2509 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2510 lambda
+ minStart
, rgba
+ minStart
);
2513 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2514 lambda
+ minStart
, rgba
+ minStart
);
2516 case GL_NEAREST_MIPMAP_NEAREST
:
2517 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
2518 texcoords
+ minStart
,
2519 lambda
+ minStart
, rgba
+ minStart
);
2521 case GL_LINEAR_MIPMAP_NEAREST
:
2522 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
2523 texcoords
+ minStart
,
2524 lambda
+ minStart
, rgba
+ minStart
);
2526 case GL_NEAREST_MIPMAP_LINEAR
:
2527 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
2528 texcoords
+ minStart
,
2529 lambda
+ minStart
, rgba
+ minStart
);
2531 case GL_LINEAR_MIPMAP_LINEAR
:
2532 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
2533 texcoords
+ minStart
,
2534 lambda
+ minStart
, rgba
+ minStart
);
2537 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2542 if (magStart
< magEnd
) {
2543 /* do the magnified texels */
2544 const GLuint m
= magEnd
- magStart
;
2545 switch (tObj
->Sampler
.MagFilter
) {
2547 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2548 lambda
+ magStart
, rgba
+ magStart
);
2551 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2552 lambda
+ magStart
, rgba
+ magStart
);
2555 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2562 /**********************************************************************/
2563 /* Texture Rectangle Sampling Functions */
2564 /**********************************************************************/
2568 sample_nearest_rect(struct gl_context
*ctx
,
2569 const struct gl_texture_object
*tObj
, GLuint n
,
2570 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2573 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2574 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2575 const GLint width
= img
->Width
;
2576 const GLint height
= img
->Height
;
2582 ASSERT(tObj
->Sampler
.WrapS
== GL_CLAMP
||
2583 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_EDGE
||
2584 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_BORDER
);
2585 ASSERT(tObj
->Sampler
.WrapT
== GL_CLAMP
||
2586 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_EDGE
||
2587 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_BORDER
);
2589 for (i
= 0; i
< n
; i
++) {
2591 col
= clamp_rect_coord_nearest(tObj
->Sampler
.WrapS
, texcoords
[i
][0], width
);
2592 row
= clamp_rect_coord_nearest(tObj
->Sampler
.WrapT
, texcoords
[i
][1], height
);
2593 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2594 get_border_color(tObj
, img
, rgba
[i
]);
2596 swImg
->FetchTexel(swImg
, col
, row
, 0, rgba
[i
]);
2602 sample_linear_rect(struct gl_context
*ctx
,
2603 const struct gl_texture_object
*tObj
, GLuint n
,
2604 const GLfloat texcoords
[][4],
2605 const GLfloat lambda
[], GLfloat rgba
[][4])
2607 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2608 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2609 const GLint width
= img
->Width
;
2610 const GLint height
= img
->Height
;
2616 ASSERT(tObj
->Sampler
.WrapS
== GL_CLAMP
||
2617 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_EDGE
||
2618 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_BORDER
);
2619 ASSERT(tObj
->Sampler
.WrapT
== GL_CLAMP
||
2620 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_EDGE
||
2621 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_BORDER
);
2623 for (i
= 0; i
< n
; i
++) {
2624 GLint i0
, j0
, i1
, j1
;
2625 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2627 GLbitfield useBorderColor
= 0x0;
2629 clamp_rect_coord_linear(tObj
->Sampler
.WrapS
, texcoords
[i
][0], width
,
2631 clamp_rect_coord_linear(tObj
->Sampler
.WrapT
, texcoords
[i
][1], height
,
2634 /* compute integer rows/columns */
2635 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2636 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2637 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2638 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2640 /* get four texel samples */
2641 if (useBorderColor
& (I0BIT
| J0BIT
))
2642 get_border_color(tObj
, img
, t00
);
2644 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
2646 if (useBorderColor
& (I1BIT
| J0BIT
))
2647 get_border_color(tObj
, img
, t10
);
2649 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
2651 if (useBorderColor
& (I0BIT
| J1BIT
))
2652 get_border_color(tObj
, img
, t01
);
2654 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
2656 if (useBorderColor
& (I1BIT
| J1BIT
))
2657 get_border_color(tObj
, img
, t11
);
2659 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
2661 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2666 /** Sample Rect texture, using lambda to choose between min/magnification */
2668 sample_lambda_rect(struct gl_context
*ctx
,
2669 const struct gl_texture_object
*tObj
, GLuint n
,
2670 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2673 GLuint minStart
, minEnd
, magStart
, magEnd
;
2675 /* We only need lambda to decide between minification and magnification.
2676 * There is no mipmapping with rectangular textures.
2678 compute_min_mag_ranges(tObj
, n
, lambda
,
2679 &minStart
, &minEnd
, &magStart
, &magEnd
);
2681 if (minStart
< minEnd
) {
2682 if (tObj
->Sampler
.MinFilter
== GL_NEAREST
) {
2683 sample_nearest_rect(ctx
, tObj
, minEnd
- minStart
,
2684 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2687 sample_linear_rect(ctx
, tObj
, minEnd
- minStart
,
2688 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2691 if (magStart
< magEnd
) {
2692 if (tObj
->Sampler
.MagFilter
== GL_NEAREST
) {
2693 sample_nearest_rect(ctx
, tObj
, magEnd
- magStart
,
2694 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2697 sample_linear_rect(ctx
, tObj
, magEnd
- magStart
,
2698 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2704 /**********************************************************************/
2705 /* 2D Texture Array Sampling Functions */
2706 /**********************************************************************/
2709 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2712 sample_2d_array_nearest(struct gl_context
*ctx
,
2713 const struct gl_texture_object
*tObj
,
2714 const struct gl_texture_image
*img
,
2715 const GLfloat texcoord
[4],
2718 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2719 const GLint width
= img
->Width2
; /* without border, power of two */
2720 const GLint height
= img
->Height2
; /* without border, power of two */
2721 const GLint depth
= img
->Depth
;
2726 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
2727 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
2728 array
= tex_array_slice(texcoord
[2], depth
);
2730 if (i
< 0 || i
>= (GLint
) img
->Width
||
2731 j
< 0 || j
>= (GLint
) img
->Height
||
2732 array
< 0 || array
>= (GLint
) img
->Depth
) {
2733 /* Need this test for GL_CLAMP_TO_BORDER mode */
2734 get_border_color(tObj
, img
, rgba
);
2737 swImg
->FetchTexel(swImg
, i
, j
, array
, rgba
);
2743 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2746 sample_2d_array_linear(struct gl_context
*ctx
,
2747 const struct gl_texture_object
*tObj
,
2748 const struct gl_texture_image
*img
,
2749 const GLfloat texcoord
[4],
2752 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2753 const GLint width
= img
->Width2
;
2754 const GLint height
= img
->Height2
;
2755 const GLint depth
= img
->Depth
;
2756 GLint i0
, j0
, i1
, j1
;
2758 GLbitfield useBorderColor
= 0x0;
2760 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2762 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2763 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2764 array
= tex_array_slice(texcoord
[2], depth
);
2766 if (array
< 0 || array
>= depth
) {
2767 COPY_4V(rgba
, tObj
->Sampler
.BorderColor
.f
);
2777 /* check if sampling texture border color */
2778 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2779 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2780 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2781 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2785 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2786 get_border_color(tObj
, img
, t00
);
2789 swImg
->FetchTexel(swImg
, i0
, j0
, array
, t00
);
2791 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2792 get_border_color(tObj
, img
, t10
);
2795 swImg
->FetchTexel(swImg
, i1
, j0
, array
, t10
);
2797 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2798 get_border_color(tObj
, img
, t01
);
2801 swImg
->FetchTexel(swImg
, i0
, j1
, array
, t01
);
2803 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2804 get_border_color(tObj
, img
, t11
);
2807 swImg
->FetchTexel(swImg
, i1
, j1
, array
, t11
);
2810 /* trilinear interpolation of samples */
2811 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2817 sample_2d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2818 const struct gl_texture_object
*tObj
,
2819 GLuint n
, const GLfloat texcoord
[][4],
2820 const GLfloat lambda
[], GLfloat rgba
[][4])
2823 for (i
= 0; i
< n
; i
++) {
2824 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2825 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2832 sample_2d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2833 const struct gl_texture_object
*tObj
,
2834 GLuint n
, const GLfloat texcoord
[][4],
2835 const GLfloat lambda
[], GLfloat rgba
[][4])
2838 ASSERT(lambda
!= NULL
);
2839 for (i
= 0; i
< n
; i
++) {
2840 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2841 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2842 texcoord
[i
], rgba
[i
]);
2848 sample_2d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2849 const struct gl_texture_object
*tObj
,
2850 GLuint n
, const GLfloat texcoord
[][4],
2851 const GLfloat lambda
[], GLfloat rgba
[][4])
2854 ASSERT(lambda
!= NULL
);
2855 for (i
= 0; i
< n
; i
++) {
2856 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2857 if (level
>= tObj
->_MaxLevel
) {
2858 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2859 texcoord
[i
], rgba
[i
]);
2862 GLfloat t0
[4], t1
[4]; /* texels */
2863 const GLfloat f
= FRAC(lambda
[i
]);
2864 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
],
2866 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1],
2868 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2875 sample_2d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2876 const struct gl_texture_object
*tObj
,
2877 GLuint n
, const GLfloat texcoord
[][4],
2878 const GLfloat lambda
[], GLfloat rgba
[][4])
2881 ASSERT(lambda
!= NULL
);
2882 for (i
= 0; i
< n
; i
++) {
2883 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2884 if (level
>= tObj
->_MaxLevel
) {
2885 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2886 texcoord
[i
], rgba
[i
]);
2889 GLfloat t0
[4], t1
[4]; /* texels */
2890 const GLfloat f
= FRAC(lambda
[i
]);
2891 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2893 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1],
2895 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2901 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2903 sample_nearest_2d_array(struct gl_context
*ctx
,
2904 const struct gl_texture_object
*tObj
, GLuint n
,
2905 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2909 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2911 for (i
= 0; i
< n
; i
++) {
2912 sample_2d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2918 /** Sample 2D Array texture, linear filtering for both min/magnification */
2920 sample_linear_2d_array(struct gl_context
*ctx
,
2921 const struct gl_texture_object
*tObj
, GLuint n
,
2922 const GLfloat texcoords
[][4],
2923 const GLfloat lambda
[], GLfloat rgba
[][4])
2926 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2928 for (i
= 0; i
< n
; i
++) {
2929 sample_2d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2934 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2936 sample_lambda_2d_array(struct gl_context
*ctx
,
2937 const struct gl_texture_object
*tObj
, GLuint n
,
2938 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2941 GLuint minStart
, minEnd
; /* texels with minification */
2942 GLuint magStart
, magEnd
; /* texels with magnification */
2945 ASSERT(lambda
!= NULL
);
2946 compute_min_mag_ranges(tObj
, n
, lambda
,
2947 &minStart
, &minEnd
, &magStart
, &magEnd
);
2949 if (minStart
< minEnd
) {
2950 /* do the minified texels */
2951 GLuint m
= minEnd
- minStart
;
2952 switch (tObj
->Sampler
.MinFilter
) {
2954 for (i
= minStart
; i
< minEnd
; i
++)
2955 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2956 texcoords
[i
], rgba
[i
]);
2959 for (i
= minStart
; i
< minEnd
; i
++)
2960 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2961 texcoords
[i
], rgba
[i
]);
2963 case GL_NEAREST_MIPMAP_NEAREST
:
2964 sample_2d_array_nearest_mipmap_nearest(ctx
, tObj
, m
,
2965 texcoords
+ minStart
,
2969 case GL_LINEAR_MIPMAP_NEAREST
:
2970 sample_2d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2971 texcoords
+ minStart
,
2975 case GL_NEAREST_MIPMAP_LINEAR
:
2976 sample_2d_array_nearest_mipmap_linear(ctx
, tObj
, m
,
2977 texcoords
+ minStart
,
2981 case GL_LINEAR_MIPMAP_LINEAR
:
2982 sample_2d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2983 texcoords
+ minStart
,
2988 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
2993 if (magStart
< magEnd
) {
2994 /* do the magnified texels */
2995 switch (tObj
->Sampler
.MagFilter
) {
2997 for (i
= magStart
; i
< magEnd
; i
++)
2998 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2999 texcoords
[i
], rgba
[i
]);
3002 for (i
= magStart
; i
< magEnd
; i
++)
3003 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3004 texcoords
[i
], rgba
[i
]);
3007 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
3016 /**********************************************************************/
3017 /* 1D Texture Array Sampling Functions */
3018 /**********************************************************************/
3021 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
3024 sample_1d_array_nearest(struct gl_context
*ctx
,
3025 const struct gl_texture_object
*tObj
,
3026 const struct gl_texture_image
*img
,
3027 const GLfloat texcoord
[4],
3030 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3031 const GLint width
= img
->Width2
; /* without border, power of two */
3032 const GLint height
= img
->Height
;
3037 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
3038 array
= tex_array_slice(texcoord
[1], height
);
3040 if (i
< 0 || i
>= (GLint
) img
->Width
||
3041 array
< 0 || array
>= (GLint
) img
->Height
) {
3042 /* Need this test for GL_CLAMP_TO_BORDER mode */
3043 get_border_color(tObj
, img
, rgba
);
3046 swImg
->FetchTexel(swImg
, i
, array
, 0, rgba
);
3052 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
3055 sample_1d_array_linear(struct gl_context
*ctx
,
3056 const struct gl_texture_object
*tObj
,
3057 const struct gl_texture_image
*img
,
3058 const GLfloat texcoord
[4],
3061 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3062 const GLint width
= img
->Width2
;
3063 const GLint height
= img
->Height
;
3066 GLbitfield useBorderColor
= 0x0;
3068 GLfloat t0
[4], t1
[4];
3070 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
3071 array
= tex_array_slice(texcoord
[1], height
);
3078 /* check if sampling texture border color */
3079 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
3080 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
3083 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
3086 if (useBorderColor
& (I0BIT
| K0BIT
)) {
3087 get_border_color(tObj
, img
, t0
);
3090 swImg
->FetchTexel(swImg
, i0
, array
, 0, t0
);
3092 if (useBorderColor
& (I1BIT
| K0BIT
)) {
3093 get_border_color(tObj
, img
, t1
);
3096 swImg
->FetchTexel(swImg
, i1
, array
, 0, t1
);
3099 /* bilinear interpolation of samples */
3100 lerp_rgba(rgba
, a
, t0
, t1
);
3105 sample_1d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
3106 const struct gl_texture_object
*tObj
,
3107 GLuint n
, const GLfloat texcoord
[][4],
3108 const GLfloat lambda
[], GLfloat rgba
[][4])
3111 for (i
= 0; i
< n
; i
++) {
3112 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3113 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
3120 sample_1d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
3121 const struct gl_texture_object
*tObj
,
3122 GLuint n
, const GLfloat texcoord
[][4],
3123 const GLfloat lambda
[], GLfloat rgba
[][4])
3126 ASSERT(lambda
!= NULL
);
3127 for (i
= 0; i
< n
; i
++) {
3128 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3129 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
3130 texcoord
[i
], rgba
[i
]);
3136 sample_1d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
3137 const struct gl_texture_object
*tObj
,
3138 GLuint n
, const GLfloat texcoord
[][4],
3139 const GLfloat lambda
[], GLfloat rgba
[][4])
3142 ASSERT(lambda
!= NULL
);
3143 for (i
= 0; i
< n
; i
++) {
3144 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3145 if (level
>= tObj
->_MaxLevel
) {
3146 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
3147 texcoord
[i
], rgba
[i
]);
3150 GLfloat t0
[4], t1
[4]; /* texels */
3151 const GLfloat f
= FRAC(lambda
[i
]);
3152 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3153 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3154 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3161 sample_1d_array_linear_mipmap_linear(struct gl_context
*ctx
,
3162 const struct gl_texture_object
*tObj
,
3163 GLuint n
, const GLfloat texcoord
[][4],
3164 const GLfloat lambda
[], GLfloat rgba
[][4])
3167 ASSERT(lambda
!= NULL
);
3168 for (i
= 0; i
< n
; i
++) {
3169 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3170 if (level
>= tObj
->_MaxLevel
) {
3171 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
3172 texcoord
[i
], rgba
[i
]);
3175 GLfloat t0
[4], t1
[4]; /* texels */
3176 const GLfloat f
= FRAC(lambda
[i
]);
3177 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3178 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3179 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3185 /** Sample 1D Array texture, nearest filtering for both min/magnification */
3187 sample_nearest_1d_array(struct gl_context
*ctx
,
3188 const struct gl_texture_object
*tObj
, GLuint n
,
3189 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3193 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3195 for (i
= 0; i
< n
; i
++) {
3196 sample_1d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
3201 /** Sample 1D Array texture, linear filtering for both min/magnification */
3203 sample_linear_1d_array(struct gl_context
*ctx
,
3204 const struct gl_texture_object
*tObj
, GLuint n
,
3205 const GLfloat texcoords
[][4],
3206 const GLfloat lambda
[], GLfloat rgba
[][4])
3209 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3211 for (i
= 0; i
< n
; i
++) {
3212 sample_1d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
3217 /** Sample 1D Array texture, using lambda to choose between min/magnification */
3219 sample_lambda_1d_array(struct gl_context
*ctx
,
3220 const struct gl_texture_object
*tObj
, GLuint n
,
3221 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3224 GLuint minStart
, minEnd
; /* texels with minification */
3225 GLuint magStart
, magEnd
; /* texels with magnification */
3228 ASSERT(lambda
!= NULL
);
3229 compute_min_mag_ranges(tObj
, n
, lambda
,
3230 &minStart
, &minEnd
, &magStart
, &magEnd
);
3232 if (minStart
< minEnd
) {
3233 /* do the minified texels */
3234 GLuint m
= minEnd
- minStart
;
3235 switch (tObj
->Sampler
.MinFilter
) {
3237 for (i
= minStart
; i
< minEnd
; i
++)
3238 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3239 texcoords
[i
], rgba
[i
]);
3242 for (i
= minStart
; i
< minEnd
; i
++)
3243 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3244 texcoords
[i
], rgba
[i
]);
3246 case GL_NEAREST_MIPMAP_NEAREST
:
3247 sample_1d_array_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
3248 lambda
+ minStart
, rgba
+ minStart
);
3250 case GL_LINEAR_MIPMAP_NEAREST
:
3251 sample_1d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
3252 texcoords
+ minStart
,
3256 case GL_NEAREST_MIPMAP_LINEAR
:
3257 sample_1d_array_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
3258 lambda
+ minStart
, rgba
+ minStart
);
3260 case GL_LINEAR_MIPMAP_LINEAR
:
3261 sample_1d_array_linear_mipmap_linear(ctx
, tObj
, m
,
3262 texcoords
+ minStart
,
3267 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
3272 if (magStart
< magEnd
) {
3273 /* do the magnified texels */
3274 switch (tObj
->Sampler
.MagFilter
) {
3276 for (i
= magStart
; i
< magEnd
; i
++)
3277 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3278 texcoords
[i
], rgba
[i
]);
3281 for (i
= magStart
; i
< magEnd
; i
++)
3282 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3283 texcoords
[i
], rgba
[i
]);
3286 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
3294 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
3296 static inline GLfloat
3297 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
,
3302 return (coord
<= depthSample
) ? 1.0F
: ambient
;
3304 return (coord
>= depthSample
) ? 1.0F
: ambient
;
3306 return (coord
< depthSample
) ? 1.0F
: ambient
;
3308 return (coord
> depthSample
) ? 1.0F
: ambient
;
3310 return (coord
== depthSample
) ? 1.0F
: ambient
;
3312 return (coord
!= depthSample
) ? 1.0F
: ambient
;
3320 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
3327 * Compare texcoord against four depth samples.
3329 static inline GLfloat
3330 shadow_compare4(GLenum function
, GLfloat coord
,
3331 GLfloat depth00
, GLfloat depth01
,
3332 GLfloat depth10
, GLfloat depth11
,
3333 GLfloat ambient
, GLfloat wi
, GLfloat wj
)
3335 const GLfloat d
= (1.0F
- (GLfloat
) ambient
) * 0.25F
;
3336 GLfloat luminance
= 1.0F
;
3340 if (coord
> depth00
) luminance
-= d
;
3341 if (coord
> depth01
) luminance
-= d
;
3342 if (coord
> depth10
) luminance
-= d
;
3343 if (coord
> depth11
) luminance
-= d
;
3346 if (coord
< depth00
) luminance
-= d
;
3347 if (coord
< depth01
) luminance
-= d
;
3348 if (coord
< depth10
) luminance
-= d
;
3349 if (coord
< depth11
) luminance
-= d
;
3352 if (coord
>= depth00
) luminance
-= d
;
3353 if (coord
>= depth01
) luminance
-= d
;
3354 if (coord
>= depth10
) luminance
-= d
;
3355 if (coord
>= depth11
) luminance
-= d
;
3358 if (coord
<= depth00
) luminance
-= d
;
3359 if (coord
<= depth01
) luminance
-= d
;
3360 if (coord
<= depth10
) luminance
-= d
;
3361 if (coord
<= depth11
) luminance
-= d
;
3364 if (coord
!= depth00
) luminance
-= d
;
3365 if (coord
!= depth01
) luminance
-= d
;
3366 if (coord
!= depth10
) luminance
-= d
;
3367 if (coord
!= depth11
) luminance
-= d
;
3370 if (coord
== depth00
) luminance
-= d
;
3371 if (coord
== depth01
) luminance
-= d
;
3372 if (coord
== depth10
) luminance
-= d
;
3373 if (coord
== depth11
) luminance
-= d
;
3380 /* ordinary bilinear filtering */
3381 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
3383 _mesa_problem(NULL
, "Bad compare func in sample_compare4");
3390 * Choose the mipmap level to use when sampling from a depth texture.
3393 choose_depth_texture_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
3397 if (tObj
->Sampler
.MinFilter
== GL_NEAREST
|| tObj
->Sampler
.MinFilter
== GL_LINEAR
) {
3398 /* no mipmapping - use base level */
3399 level
= tObj
->BaseLevel
;
3402 /* choose mipmap level */
3403 lambda
= CLAMP(lambda
, tObj
->Sampler
.MinLod
, tObj
->Sampler
.MaxLod
);
3404 level
= (GLint
) lambda
;
3405 level
= CLAMP(level
, tObj
->BaseLevel
, tObj
->_MaxLevel
);
3413 * Sample a shadow/depth texture. This function is incomplete. It doesn't
3414 * check for minification vs. magnification, etc.
3417 sample_depth_texture( struct gl_context
*ctx
,
3418 const struct gl_texture_object
*tObj
, GLuint n
,
3419 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3420 GLfloat texel
[][4] )
3422 const GLint level
= choose_depth_texture_level(tObj
, lambda
[0]);
3423 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
3424 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3425 const GLint width
= img
->Width
;
3426 const GLint height
= img
->Height
;
3427 const GLint depth
= img
->Depth
;
3428 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
3434 ASSERT(img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
3435 img
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
3437 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
3438 tObj
->Target
== GL_TEXTURE_2D
||
3439 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
3440 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
3441 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
||
3442 tObj
->Target
== GL_TEXTURE_CUBE_MAP
);
3444 ambient
= tObj
->Sampler
.CompareFailValue
;
3446 /* XXXX if tObj->Sampler.MinFilter != tObj->Sampler.MagFilter, we're ignoring lambda */
3448 function
= (tObj
->Sampler
.CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
3449 tObj
->Sampler
.CompareFunc
: GL_NONE
;
3451 if (tObj
->Sampler
.MagFilter
== GL_NEAREST
) {
3453 for (i
= 0; i
< n
; i
++) {
3454 GLfloat depthSample
, depthRef
;
3455 GLint col
, row
, slice
;
3457 nearest_texcoord(tObj
, level
, texcoords
[i
], &col
, &row
, &slice
);
3459 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3460 slice
>= 0 && slice
< depth
) {
3461 swImg
->FetchTexel(swImg
, col
, row
, slice
, &depthSample
);
3464 depthSample
= tObj
->Sampler
.BorderColor
.f
[0];
3467 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3469 result
= shadow_compare(function
, depthRef
, depthSample
, ambient
);
3471 switch (tObj
->Sampler
.DepthMode
) {
3473 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3476 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3479 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3482 ASSIGN_4V(texel
[i
], result
, 0.0F
, 0.0F
, 1.0F
);
3485 _mesa_problem(ctx
, "Bad depth texture mode");
3492 ASSERT(tObj
->Sampler
.MagFilter
== GL_LINEAR
);
3493 for (i
= 0; i
< n
; i
++) {
3494 GLfloat depth00
, depth01
, depth10
, depth11
, depthRef
;
3495 GLint i0
, i1
, j0
, j1
;
3498 GLuint useBorderTexel
;
3500 linear_texcoord(tObj
, level
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3507 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3513 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3514 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3515 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3516 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3519 if (slice
< 0 || slice
>= (GLint
) depth
) {
3520 depth00
= tObj
->Sampler
.BorderColor
.f
[0];
3521 depth01
= tObj
->Sampler
.BorderColor
.f
[0];
3522 depth10
= tObj
->Sampler
.BorderColor
.f
[0];
3523 depth11
= tObj
->Sampler
.BorderColor
.f
[0];
3526 /* get four depth samples from the texture */
3527 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3528 depth00
= tObj
->Sampler
.BorderColor
.f
[0];
3531 swImg
->FetchTexel(swImg
, i0
, j0
, slice
, &depth00
);
3533 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3534 depth10
= tObj
->Sampler
.BorderColor
.f
[0];
3537 swImg
->FetchTexel(swImg
, i1
, j0
, slice
, &depth10
);
3540 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3541 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3542 depth01
= tObj
->Sampler
.BorderColor
.f
[0];
3545 swImg
->FetchTexel(swImg
, i0
, j1
, slice
, &depth01
);
3547 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3548 depth11
= tObj
->Sampler
.BorderColor
.f
[0];
3551 swImg
->FetchTexel(swImg
, i1
, j1
, slice
, &depth11
);
3560 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3562 result
= shadow_compare4(function
, depthRef
,
3563 depth00
, depth01
, depth10
, depth11
,
3566 switch (tObj
->Sampler
.DepthMode
) {
3568 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3571 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3574 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3577 _mesa_problem(ctx
, "Bad depth texture mode");
3586 * We use this function when a texture object is in an "incomplete" state.
3587 * When a fragment program attempts to sample an incomplete texture we
3588 * return black (see issue 23 in GL_ARB_fragment_program spec).
3589 * Note: fragment programs don't observe the texture enable/disable flags.
3592 null_sample_func( struct gl_context
*ctx
,
3593 const struct gl_texture_object
*tObj
, GLuint n
,
3594 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3602 for (i
= 0; i
< n
; i
++) {
3606 rgba
[i
][ACOMP
] = 1.0;
3612 * Choose the texture sampling function for the given texture object.
3615 _swrast_choose_texture_sample_func( struct gl_context
*ctx
,
3616 const struct gl_texture_object
*t
,
3617 const struct gl_sampler_object
*sampler
)
3619 if (!t
|| !_mesa_is_texture_complete(t
, sampler
)) {
3620 return &null_sample_func
;
3623 const GLboolean needLambda
=
3624 (GLboolean
) (t
->Sampler
.MinFilter
!= t
->Sampler
.MagFilter
);
3625 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->_BaseFormat
;
3627 switch (t
->Target
) {
3629 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3630 return &sample_depth_texture
;
3632 else if (needLambda
) {
3633 return &sample_lambda_1d
;
3635 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3636 return &sample_linear_1d
;
3639 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3640 return &sample_nearest_1d
;
3643 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3644 return &sample_depth_texture
;
3646 else if (needLambda
) {
3647 /* Anisotropic filtering extension. Activated only if mipmaps are used */
3648 if (t
->Sampler
.MaxAnisotropy
> 1.0 &&
3649 t
->Sampler
.MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
3650 return &sample_lambda_2d_aniso
;
3652 return &sample_lambda_2d
;
3654 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3655 return &sample_linear_2d
;
3658 /* check for a few optimized cases */
3659 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3660 const struct swrast_texture_image
*swImg
=
3661 swrast_texture_image_const(img
);
3662 texture_sample_func func
;
3664 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3665 func
= &sample_nearest_2d
;
3666 if (t
->Sampler
.WrapS
== GL_REPEAT
&&
3667 t
->Sampler
.WrapT
== GL_REPEAT
&&
3668 swImg
->_IsPowerOfTwo
&&
3670 if (img
->TexFormat
== MESA_FORMAT_RGB888
)
3671 func
= &opt_sample_rgb_2d
;
3672 else if (img
->TexFormat
== MESA_FORMAT_RGBA8888
)
3673 func
= &opt_sample_rgba_2d
;
3680 return &sample_lambda_3d
;
3682 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3683 return &sample_linear_3d
;
3686 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3687 return &sample_nearest_3d
;
3689 case GL_TEXTURE_CUBE_MAP
:
3690 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3691 return &sample_depth_texture
;
3693 else if (needLambda
) {
3694 return &sample_lambda_cube
;
3696 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3697 return &sample_linear_cube
;
3700 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3701 return &sample_nearest_cube
;
3703 case GL_TEXTURE_RECTANGLE_NV
:
3704 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3705 return &sample_depth_texture
;
3707 else if (needLambda
) {
3708 return &sample_lambda_rect
;
3710 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3711 return &sample_linear_rect
;
3714 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3715 return &sample_nearest_rect
;
3717 case GL_TEXTURE_1D_ARRAY_EXT
:
3718 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3719 return &sample_depth_texture
;
3721 else if (needLambda
) {
3722 return &sample_lambda_1d_array
;
3724 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3725 return &sample_linear_1d_array
;
3728 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3729 return &sample_nearest_1d_array
;
3731 case GL_TEXTURE_2D_ARRAY_EXT
:
3732 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3733 return &sample_depth_texture
;
3735 else if (needLambda
) {
3736 return &sample_lambda_2d_array
;
3738 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3739 return &sample_linear_2d_array
;
3742 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3743 return &sample_nearest_2d_array
;
3747 "invalid target in _swrast_choose_texture_sample_func");
3748 return &null_sample_func
;