2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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"
31 #include "main/samplerobj.h"
33 #include "s_context.h"
34 #include "s_texfilter.h"
38 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
39 * see 1-pixel bands of improperly weighted linear-filtered textures.
40 * The tests/texwrap.c demo is a good test.
41 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
42 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
44 #define FRAC(f) ((f) - IFLOOR(f))
49 * Linear interpolation macro
51 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
55 * Do 2D/biliner interpolation of float values.
56 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
57 * a and b are the horizontal and vertical interpolants.
58 * It's important that this function is inlined when compiled with
59 * optimization! If we find that's not true on some systems, convert
63 lerp_2d(GLfloat a
, GLfloat b
,
64 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
66 const GLfloat temp0
= LERP(a
, v00
, v10
);
67 const GLfloat temp1
= LERP(a
, v01
, v11
);
68 return LERP(b
, temp0
, temp1
);
73 * Do 3D/trilinear interpolation of float values.
77 lerp_3d(GLfloat a
, GLfloat b
, GLfloat c
,
78 GLfloat v000
, GLfloat v100
, GLfloat v010
, GLfloat v110
,
79 GLfloat v001
, GLfloat v101
, GLfloat v011
, GLfloat v111
)
81 const GLfloat temp00
= LERP(a
, v000
, v100
);
82 const GLfloat temp10
= LERP(a
, v010
, v110
);
83 const GLfloat temp01
= LERP(a
, v001
, v101
);
84 const GLfloat temp11
= LERP(a
, v011
, v111
);
85 const GLfloat temp0
= LERP(b
, temp00
, temp10
);
86 const GLfloat temp1
= LERP(b
, temp01
, temp11
);
87 return LERP(c
, temp0
, temp1
);
92 * Do linear interpolation of colors.
95 lerp_rgba(GLfloat result
[4], GLfloat t
, const GLfloat a
[4], const GLfloat b
[4])
97 result
[0] = LERP(t
, a
[0], b
[0]);
98 result
[1] = LERP(t
, a
[1], b
[1]);
99 result
[2] = LERP(t
, a
[2], b
[2]);
100 result
[3] = LERP(t
, a
[3], b
[3]);
105 * Do bilinear interpolation of colors.
108 lerp_rgba_2d(GLfloat result
[4], GLfloat a
, GLfloat b
,
109 const GLfloat t00
[4], const GLfloat t10
[4],
110 const GLfloat t01
[4], const GLfloat t11
[4])
112 result
[0] = lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]);
113 result
[1] = lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]);
114 result
[2] = lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]);
115 result
[3] = lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]);
120 * Do trilinear interpolation of colors.
123 lerp_rgba_3d(GLfloat result
[4], GLfloat a
, GLfloat b
, GLfloat c
,
124 const GLfloat t000
[4], const GLfloat t100
[4],
125 const GLfloat t010
[4], const GLfloat t110
[4],
126 const GLfloat t001
[4], const GLfloat t101
[4],
127 const GLfloat t011
[4], const GLfloat t111
[4])
130 /* compiler should unroll these short loops */
131 for (k
= 0; k
< 4; k
++) {
132 result
[k
] = lerp_3d(a
, b
, c
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
133 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
139 * Used for GL_REPEAT wrap mode. Using A % B doesn't produce the
140 * right results for A<0. Casting to A to be unsigned only works if B
141 * is a power of two. Adding a bias to A (which is a multiple of B)
142 * avoids the problems with A < 0 (for reasonable A) without using a
145 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
149 * Used to compute texel locations for linear sampling.
151 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
152 * s = texcoord in [0,1]
153 * size = width (or height or depth) of texture
155 * i0, i1 = returns two nearest texel indexes
156 * weight = returns blend factor between texels
159 linear_texel_locations(GLenum wrapMode
,
160 const struct gl_texture_image
*img
,
161 GLint size
, GLfloat s
,
162 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
164 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
169 if (swImg
->_IsPowerOfTwo
) {
170 *i0
= IFLOOR(u
) & (size
- 1);
171 *i1
= (*i0
+ 1) & (size
- 1);
174 *i0
= REMAINDER(IFLOOR(u
), size
);
175 *i1
= REMAINDER(*i0
+ 1, size
);
178 case GL_CLAMP_TO_EDGE
:
190 if (*i1
>= (GLint
) size
)
193 case GL_CLAMP_TO_BORDER
:
195 const GLfloat min
= -1.0F
/ (2.0F
* size
);
196 const GLfloat max
= 1.0F
- min
;
208 case GL_MIRRORED_REPEAT
:
210 const GLint flr
= IFLOOR(s
);
212 u
= 1.0F
- (s
- (GLfloat
) flr
);
214 u
= s
- (GLfloat
) flr
;
215 u
= (u
* size
) - 0.5F
;
220 if (*i1
>= (GLint
) size
)
224 case GL_MIRROR_CLAMP_EXT
:
234 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
245 if (*i1
>= (GLint
) size
)
248 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
250 const GLfloat min
= -1.0F
/ (2.0F
* size
);
251 const GLfloat max
= 1.0F
- min
;
276 _mesa_problem(NULL
, "Bad wrap mode");
286 * Used to compute texel location for nearest sampling.
289 nearest_texel_location(GLenum wrapMode
,
290 const struct gl_texture_image
*img
,
291 GLint size
, GLfloat s
)
293 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
298 /* s limited to [0,1) */
299 /* i limited to [0,size-1] */
300 i
= IFLOOR(s
* size
);
301 if (swImg
->_IsPowerOfTwo
)
304 i
= REMAINDER(i
, size
);
306 case GL_CLAMP_TO_EDGE
:
308 /* s limited to [min,max] */
309 /* i limited to [0, size-1] */
310 const GLfloat min
= 1.0F
/ (2.0F
* size
);
311 const GLfloat max
= 1.0F
- min
;
317 i
= IFLOOR(s
* size
);
320 case GL_CLAMP_TO_BORDER
:
322 /* s limited to [min,max] */
323 /* i limited to [-1, size] */
324 const GLfloat min
= -1.0F
/ (2.0F
* size
);
325 const GLfloat max
= 1.0F
- min
;
331 i
= IFLOOR(s
* size
);
334 case GL_MIRRORED_REPEAT
:
336 const GLfloat min
= 1.0F
/ (2.0F
* size
);
337 const GLfloat max
= 1.0F
- min
;
338 const GLint flr
= IFLOOR(s
);
341 u
= 1.0F
- (s
- (GLfloat
) flr
);
343 u
= s
- (GLfloat
) flr
;
349 i
= IFLOOR(u
* size
);
352 case GL_MIRROR_CLAMP_EXT
:
354 /* s limited to [0,1] */
355 /* i limited to [0,size-1] */
356 const GLfloat u
= FABSF(s
);
362 i
= IFLOOR(u
* size
);
365 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
367 /* s limited to [min,max] */
368 /* i limited to [0, size-1] */
369 const GLfloat min
= 1.0F
/ (2.0F
* size
);
370 const GLfloat max
= 1.0F
- min
;
371 const GLfloat u
= FABSF(s
);
377 i
= IFLOOR(u
* size
);
380 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
382 /* s limited to [min,max] */
383 /* i limited to [0, size-1] */
384 const GLfloat min
= -1.0F
/ (2.0F
* size
);
385 const GLfloat max
= 1.0F
- min
;
386 const GLfloat u
= FABSF(s
);
392 i
= IFLOOR(u
* size
);
396 /* s limited to [0,1] */
397 /* i limited to [0,size-1] */
403 i
= IFLOOR(s
* size
);
406 _mesa_problem(NULL
, "Bad wrap mode");
412 /* Power of two image sizes only */
414 linear_repeat_texel_location(GLuint size
, GLfloat s
,
415 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
417 GLfloat u
= s
* size
- 0.5F
;
418 *i0
= IFLOOR(u
) & (size
- 1);
419 *i1
= (*i0
+ 1) & (size
- 1);
425 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
428 clamp_rect_coord_nearest(GLenum wrapMode
, GLfloat coord
, GLint max
)
432 return IFLOOR( CLAMP(coord
, 0.0F
, max
- 1) );
433 case GL_CLAMP_TO_EDGE
:
434 return IFLOOR( CLAMP(coord
, 0.5F
, max
- 0.5F
) );
435 case GL_CLAMP_TO_BORDER
:
436 return IFLOOR( CLAMP(coord
, -0.5F
, max
+ 0.5F
) );
438 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_nearest");
445 * As above, but GL_LINEAR filtering.
448 clamp_rect_coord_linear(GLenum wrapMode
, GLfloat coord
, GLint max
,
449 GLint
*i0out
, GLint
*i1out
, GLfloat
*weight
)
455 /* Not exactly what the spec says, but it matches NVIDIA output */
456 fcol
= CLAMP(coord
- 0.5F
, 0.0F
, max
- 1);
460 case GL_CLAMP_TO_EDGE
:
461 fcol
= CLAMP(coord
, 0.5F
, max
- 0.5F
);
468 case GL_CLAMP_TO_BORDER
:
469 fcol
= CLAMP(coord
, -0.5F
, max
+ 0.5F
);
475 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_linear");
482 *weight
= FRAC(fcol
);
487 * Compute slice/image to use for 1D or 2D array texture.
490 tex_array_slice(GLfloat coord
, GLsizei size
)
492 GLint slice
= IFLOOR(coord
+ 0.5f
);
493 slice
= CLAMP(slice
, 0, size
- 1);
499 * Compute nearest integer texcoords for given texobj and coordinate.
500 * NOTE: only used for depth texture sampling.
503 nearest_texcoord(const struct gl_sampler_object
*samp
,
504 const struct gl_texture_object
*texObj
,
506 const GLfloat texcoord
[4],
507 GLint
*i
, GLint
*j
, GLint
*k
)
509 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
510 const GLint width
= img
->Width
;
511 const GLint height
= img
->Height
;
512 const GLint depth
= img
->Depth
;
514 switch (texObj
->Target
) {
515 case GL_TEXTURE_RECTANGLE_ARB
:
516 *i
= clamp_rect_coord_nearest(samp
->WrapS
, texcoord
[0], width
);
517 *j
= clamp_rect_coord_nearest(samp
->WrapT
, texcoord
[1], height
);
521 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
526 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
527 *j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
530 case GL_TEXTURE_1D_ARRAY_EXT
:
531 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
532 *j
= tex_array_slice(texcoord
[1], height
);
535 case GL_TEXTURE_2D_ARRAY_EXT
:
536 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
537 *j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
538 *k
= tex_array_slice(texcoord
[2], depth
);
548 * Compute linear integer texcoords for given texobj and coordinate.
549 * NOTE: only used for depth texture sampling.
552 linear_texcoord(const struct gl_sampler_object
*samp
,
553 const struct gl_texture_object
*texObj
,
555 const GLfloat texcoord
[4],
556 GLint
*i0
, GLint
*i1
, GLint
*j0
, GLint
*j1
, GLint
*slice
,
557 GLfloat
*wi
, GLfloat
*wj
)
559 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
560 const GLint width
= img
->Width
;
561 const GLint height
= img
->Height
;
562 const GLint depth
= img
->Depth
;
564 switch (texObj
->Target
) {
565 case GL_TEXTURE_RECTANGLE_ARB
:
566 clamp_rect_coord_linear(samp
->WrapS
, texcoord
[0],
568 clamp_rect_coord_linear(samp
->WrapT
, texcoord
[1],
575 linear_texel_locations(samp
->WrapS
, img
, width
,
576 texcoord
[0], i0
, i1
, wi
);
577 linear_texel_locations(samp
->WrapT
, img
, height
,
578 texcoord
[1], j0
, j1
, wj
);
582 case GL_TEXTURE_1D_ARRAY_EXT
:
583 linear_texel_locations(samp
->WrapS
, img
, width
,
584 texcoord
[0], i0
, i1
, wi
);
585 *j0
= tex_array_slice(texcoord
[1], height
);
590 case GL_TEXTURE_2D_ARRAY_EXT
:
591 linear_texel_locations(samp
->WrapS
, img
, width
,
592 texcoord
[0], i0
, i1
, wi
);
593 linear_texel_locations(samp
->WrapT
, img
, height
,
594 texcoord
[1], j0
, j1
, wj
);
595 *slice
= tex_array_slice(texcoord
[2], depth
);
607 * For linear interpolation between mipmap levels N and N+1, this function
611 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
614 return tObj
->BaseLevel
;
615 else if (lambda
> tObj
->_MaxLambda
)
616 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
618 return (GLint
) (tObj
->BaseLevel
+ lambda
);
623 * Compute the nearest mipmap level to take texels from.
626 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
632 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
633 l
= tObj
->_MaxLambda
+ 0.4999F
;
636 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
637 if (level
> tObj
->_MaxLevel
)
638 level
= tObj
->_MaxLevel
;
645 * Bitflags for texture border color sampling.
657 * The lambda[] array values are always monotonic. Either the whole span
658 * will be minified, magnified, or split between the two. This function
659 * determines the subranges in [0, n-1] that are to be minified or magnified.
662 compute_min_mag_ranges(const struct gl_sampler_object
*samp
,
663 GLuint n
, const GLfloat lambda
[],
664 GLuint
*minStart
, GLuint
*minEnd
,
665 GLuint
*magStart
, GLuint
*magEnd
)
667 GLfloat minMagThresh
;
669 /* we shouldn't be here if minfilter == magfilter */
670 ASSERT(samp
->MinFilter
!= samp
->MagFilter
);
672 /* This bit comes from the OpenGL spec: */
673 if (samp
->MagFilter
== GL_LINEAR
674 && (samp
->MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
675 samp
->MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
683 /* DEBUG CODE: Verify that lambda[] is monotonic.
684 * We can't really use this because the inaccuracy in the LOG2 function
685 * causes this test to fail, yet the resulting texturing is correct.
689 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
690 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
691 for (i
= 0; i
< n
- 1; i
++) {
692 ASSERT((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
695 else { /* increasing */
696 for (i
= 0; i
< n
- 1; i
++) {
697 ASSERT((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
703 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
704 /* magnification for whole span */
707 *minStart
= *minEnd
= 0;
709 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
710 /* minification for whole span */
713 *magStart
= *magEnd
= 0;
716 /* a mix of minification and magnification */
718 if (lambda
[0] > minMagThresh
) {
719 /* start with minification */
720 for (i
= 1; i
< n
; i
++) {
721 if (lambda
[i
] <= minMagThresh
)
730 /* start with magnification */
731 for (i
= 1; i
< n
; i
++) {
732 if (lambda
[i
] > minMagThresh
)
743 /* Verify the min/mag Start/End values
744 * We don't use this either (see above)
748 for (i
= 0; i
< n
; i
++) {
749 if (lambda
[i
] > minMagThresh
) {
751 ASSERT(i
>= *minStart
);
756 ASSERT(i
>= *magStart
);
766 * When we sample the border color, it must be interpreted according to
767 * the base texture format. Ex: if the texture base format it GL_ALPHA,
768 * we return (0,0,0,BorderAlpha).
771 get_border_color(const struct gl_sampler_object
*samp
,
772 const struct gl_texture_image
*img
,
775 switch (img
->_BaseFormat
) {
777 rgba
[0] = samp
->BorderColor
.f
[0];
778 rgba
[1] = samp
->BorderColor
.f
[1];
779 rgba
[2] = samp
->BorderColor
.f
[2];
783 rgba
[0] = rgba
[1] = rgba
[2] = 0.0;
784 rgba
[3] = samp
->BorderColor
.f
[3];
787 rgba
[0] = rgba
[1] = rgba
[2] = samp
->BorderColor
.f
[0];
790 case GL_LUMINANCE_ALPHA
:
791 rgba
[0] = rgba
[1] = rgba
[2] = samp
->BorderColor
.f
[0];
792 rgba
[3] = samp
->BorderColor
.f
[3];
795 rgba
[0] = rgba
[1] = rgba
[2] = rgba
[3] = samp
->BorderColor
.f
[0];
798 COPY_4V(rgba
, samp
->BorderColor
.f
);
805 * Put z into texel according to GL_DEPTH_MODE.
808 apply_depth_mode(GLenum depthMode
, GLfloat z
, GLfloat texel
[4])
812 ASSIGN_4V(texel
, z
, z
, z
, 1.0F
);
815 ASSIGN_4V(texel
, z
, z
, z
, z
);
818 ASSIGN_4V(texel
, 0.0F
, 0.0F
, 0.0F
, z
);
821 ASSIGN_4V(texel
, z
, 0.0F
, 0.0F
, 1.0F
);
824 _mesa_problem(NULL
, "Bad depth texture mode");
830 * Is the given texture a depth (or depth/stencil) texture?
833 is_depth_texture(const struct gl_texture_object
*tObj
)
835 GLenum format
= tObj
->Image
[0][tObj
->BaseLevel
]->_BaseFormat
;
836 return format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
;
840 /**********************************************************************/
841 /* 1-D Texture Sampling Functions */
842 /**********************************************************************/
845 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
848 sample_1d_nearest(struct gl_context
*ctx
,
849 const struct gl_sampler_object
*samp
,
850 const struct gl_texture_image
*img
,
851 const GLfloat texcoord
[4], GLfloat rgba
[4])
853 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
854 const GLint width
= img
->Width2
; /* without border, power of two */
856 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
857 /* skip over the border, if any */
859 if (i
< 0 || i
>= (GLint
) img
->Width
) {
860 /* Need this test for GL_CLAMP_TO_BORDER mode */
861 get_border_color(samp
, img
, rgba
);
864 swImg
->FetchTexel(swImg
, i
, 0, 0, rgba
);
870 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
873 sample_1d_linear(struct gl_context
*ctx
,
874 const struct gl_sampler_object
*samp
,
875 const struct gl_texture_image
*img
,
876 const GLfloat texcoord
[4], GLfloat rgba
[4])
878 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
879 const GLint width
= img
->Width2
;
881 GLbitfield useBorderColor
= 0x0;
883 GLfloat t0
[4], t1
[4]; /* texels */
885 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
892 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
893 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
896 /* fetch texel colors */
897 if (useBorderColor
& I0BIT
) {
898 get_border_color(samp
, img
, t0
);
901 swImg
->FetchTexel(swImg
, i0
, 0, 0, t0
);
903 if (useBorderColor
& I1BIT
) {
904 get_border_color(samp
, img
, t1
);
907 swImg
->FetchTexel(swImg
, i1
, 0, 0, t1
);
910 lerp_rgba(rgba
, a
, t0
, t1
);
915 sample_1d_nearest_mipmap_nearest(struct gl_context
*ctx
,
916 const struct gl_sampler_object
*samp
,
917 const struct gl_texture_object
*tObj
,
918 GLuint n
, const GLfloat texcoord
[][4],
919 const GLfloat lambda
[], GLfloat rgba
[][4])
922 ASSERT(lambda
!= NULL
);
923 for (i
= 0; i
< n
; i
++) {
924 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
925 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
931 sample_1d_linear_mipmap_nearest(struct gl_context
*ctx
,
932 const struct gl_sampler_object
*samp
,
933 const struct gl_texture_object
*tObj
,
934 GLuint n
, const GLfloat texcoord
[][4],
935 const GLfloat lambda
[], GLfloat rgba
[][4])
938 ASSERT(lambda
!= NULL
);
939 for (i
= 0; i
< n
; i
++) {
940 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
941 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
947 sample_1d_nearest_mipmap_linear(struct gl_context
*ctx
,
948 const struct gl_sampler_object
*samp
,
949 const struct gl_texture_object
*tObj
,
950 GLuint n
, const GLfloat texcoord
[][4],
951 const GLfloat lambda
[], GLfloat rgba
[][4])
954 ASSERT(lambda
!= NULL
);
955 for (i
= 0; i
< n
; i
++) {
956 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
957 if (level
>= tObj
->_MaxLevel
) {
958 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
959 texcoord
[i
], rgba
[i
]);
962 GLfloat t0
[4], t1
[4];
963 const GLfloat f
= FRAC(lambda
[i
]);
964 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
965 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
966 lerp_rgba(rgba
[i
], f
, t0
, t1
);
973 sample_1d_linear_mipmap_linear(struct gl_context
*ctx
,
974 const struct gl_sampler_object
*samp
,
975 const struct gl_texture_object
*tObj
,
976 GLuint n
, const GLfloat texcoord
[][4],
977 const GLfloat lambda
[], GLfloat rgba
[][4])
980 ASSERT(lambda
!= NULL
);
981 for (i
= 0; i
< n
; i
++) {
982 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
983 if (level
>= tObj
->_MaxLevel
) {
984 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
985 texcoord
[i
], rgba
[i
]);
988 GLfloat t0
[4], t1
[4];
989 const GLfloat f
= FRAC(lambda
[i
]);
990 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
991 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
992 lerp_rgba(rgba
[i
], f
, t0
, t1
);
998 /** Sample 1D texture, nearest filtering for both min/magnification */
1000 sample_nearest_1d( struct gl_context
*ctx
,
1001 const struct gl_sampler_object
*samp
,
1002 const struct gl_texture_object
*tObj
, GLuint n
,
1003 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1007 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1009 for (i
= 0; i
< n
; i
++) {
1010 sample_1d_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1015 /** Sample 1D texture, linear filtering for both min/magnification */
1017 sample_linear_1d( struct gl_context
*ctx
,
1018 const struct gl_sampler_object
*samp
,
1019 const struct gl_texture_object
*tObj
, GLuint n
,
1020 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1024 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1026 for (i
= 0; i
< n
; i
++) {
1027 sample_1d_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1032 /** Sample 1D texture, using lambda to choose between min/magnification */
1034 sample_lambda_1d( struct gl_context
*ctx
,
1035 const struct gl_sampler_object
*samp
,
1036 const struct gl_texture_object
*tObj
, GLuint n
,
1037 const GLfloat texcoords
[][4],
1038 const GLfloat lambda
[], GLfloat rgba
[][4] )
1040 GLuint minStart
, minEnd
; /* texels with minification */
1041 GLuint magStart
, magEnd
; /* texels with magnification */
1044 ASSERT(lambda
!= NULL
);
1045 compute_min_mag_ranges(samp
, n
, lambda
,
1046 &minStart
, &minEnd
, &magStart
, &magEnd
);
1048 if (minStart
< minEnd
) {
1049 /* do the minified texels */
1050 const GLuint m
= minEnd
- minStart
;
1051 switch (samp
->MinFilter
) {
1053 for (i
= minStart
; i
< minEnd
; i
++)
1054 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
1055 texcoords
[i
], rgba
[i
]);
1058 for (i
= minStart
; i
< minEnd
; i
++)
1059 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
1060 texcoords
[i
], rgba
[i
]);
1062 case GL_NEAREST_MIPMAP_NEAREST
:
1063 sample_1d_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1064 lambda
+ minStart
, rgba
+ minStart
);
1066 case GL_LINEAR_MIPMAP_NEAREST
:
1067 sample_1d_linear_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1068 lambda
+ minStart
, rgba
+ minStart
);
1070 case GL_NEAREST_MIPMAP_LINEAR
:
1071 sample_1d_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1072 lambda
+ minStart
, rgba
+ minStart
);
1074 case GL_LINEAR_MIPMAP_LINEAR
:
1075 sample_1d_linear_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1076 lambda
+ minStart
, rgba
+ minStart
);
1079 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
1084 if (magStart
< magEnd
) {
1085 /* do the magnified texels */
1086 switch (samp
->MagFilter
) {
1088 for (i
= magStart
; i
< magEnd
; i
++)
1089 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
1090 texcoords
[i
], rgba
[i
]);
1093 for (i
= magStart
; i
< magEnd
; i
++)
1094 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
1095 texcoords
[i
], rgba
[i
]);
1098 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
1105 /**********************************************************************/
1106 /* 2-D Texture Sampling Functions */
1107 /**********************************************************************/
1111 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1114 sample_2d_nearest(struct gl_context
*ctx
,
1115 const struct gl_sampler_object
*samp
,
1116 const struct gl_texture_image
*img
,
1117 const GLfloat texcoord
[4],
1120 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1121 const GLint width
= img
->Width2
; /* without border, power of two */
1122 const GLint height
= img
->Height2
; /* without border, power of two */
1126 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
1127 j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
1129 /* skip over the border, if any */
1133 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
1134 /* Need this test for GL_CLAMP_TO_BORDER mode */
1135 get_border_color(samp
, img
, rgba
);
1138 swImg
->FetchTexel(swImg
, i
, j
, 0, rgba
);
1144 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1145 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1148 sample_2d_linear(struct gl_context
*ctx
,
1149 const struct gl_sampler_object
*samp
,
1150 const struct gl_texture_image
*img
,
1151 const GLfloat texcoord
[4],
1154 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1155 const GLint width
= img
->Width2
;
1156 const GLint height
= img
->Height2
;
1157 GLint i0
, j0
, i1
, j1
;
1158 GLbitfield useBorderColor
= 0x0;
1160 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1162 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1163 linear_texel_locations(samp
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1172 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1173 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1174 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1175 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1178 /* fetch four texel colors */
1179 if (useBorderColor
& (I0BIT
| J0BIT
)) {
1180 get_border_color(samp
, img
, t00
);
1183 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
1185 if (useBorderColor
& (I1BIT
| J0BIT
)) {
1186 get_border_color(samp
, img
, t10
);
1189 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
1191 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1192 get_border_color(samp
, img
, t01
);
1195 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
1197 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1198 get_border_color(samp
, img
, t11
);
1201 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
1204 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1209 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1210 * We don't have to worry about the texture border.
1213 sample_2d_linear_repeat(struct gl_context
*ctx
,
1214 const struct gl_sampler_object
*samp
,
1215 const struct gl_texture_image
*img
,
1216 const GLfloat texcoord
[4],
1219 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1220 const GLint width
= img
->Width2
;
1221 const GLint height
= img
->Height2
;
1222 GLint i0
, j0
, i1
, j1
;
1224 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1228 ASSERT(samp
->WrapS
== GL_REPEAT
);
1229 ASSERT(samp
->WrapT
== GL_REPEAT
);
1230 ASSERT(img
->Border
== 0);
1231 ASSERT(swImg
->_IsPowerOfTwo
);
1233 linear_repeat_texel_location(width
, texcoord
[0], &i0
, &i1
, &wi
);
1234 linear_repeat_texel_location(height
, texcoord
[1], &j0
, &j1
, &wj
);
1236 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
1237 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
1238 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
1239 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
1241 lerp_rgba_2d(rgba
, wi
, wj
, t00
, t10
, t01
, t11
);
1246 sample_2d_nearest_mipmap_nearest(struct gl_context
*ctx
,
1247 const struct gl_sampler_object
*samp
,
1248 const struct gl_texture_object
*tObj
,
1249 GLuint n
, const GLfloat texcoord
[][4],
1250 const GLfloat lambda
[], GLfloat rgba
[][4])
1253 for (i
= 0; i
< n
; i
++) {
1254 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1255 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1261 sample_2d_linear_mipmap_nearest(struct gl_context
*ctx
,
1262 const struct gl_sampler_object
*samp
,
1263 const struct gl_texture_object
*tObj
,
1264 GLuint n
, const GLfloat texcoord
[][4],
1265 const GLfloat lambda
[], GLfloat rgba
[][4])
1268 ASSERT(lambda
!= NULL
);
1269 for (i
= 0; i
< n
; i
++) {
1270 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1271 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1277 sample_2d_nearest_mipmap_linear(struct gl_context
*ctx
,
1278 const struct gl_sampler_object
*samp
,
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 for (i
= 0; i
< n
; i
++) {
1286 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1287 if (level
>= tObj
->_MaxLevel
) {
1288 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1289 texcoord
[i
], rgba
[i
]);
1292 GLfloat t0
[4], t1
[4]; /* texels */
1293 const GLfloat f
= FRAC(lambda
[i
]);
1294 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1295 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1296 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1303 sample_2d_linear_mipmap_linear( struct gl_context
*ctx
,
1304 const struct gl_sampler_object
*samp
,
1305 const struct gl_texture_object
*tObj
,
1306 GLuint n
, const GLfloat texcoord
[][4],
1307 const GLfloat lambda
[], GLfloat rgba
[][4] )
1310 ASSERT(lambda
!= NULL
);
1311 for (i
= 0; i
< n
; i
++) {
1312 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1313 if (level
>= tObj
->_MaxLevel
) {
1314 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1315 texcoord
[i
], rgba
[i
]);
1318 GLfloat t0
[4], t1
[4]; /* texels */
1319 const GLfloat f
= FRAC(lambda
[i
]);
1320 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1321 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1322 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1329 sample_2d_linear_mipmap_linear_repeat(struct gl_context
*ctx
,
1330 const struct gl_sampler_object
*samp
,
1331 const struct gl_texture_object
*tObj
,
1332 GLuint n
, const GLfloat texcoord
[][4],
1333 const GLfloat lambda
[], GLfloat rgba
[][4])
1336 ASSERT(lambda
!= NULL
);
1337 ASSERT(samp
->WrapS
== GL_REPEAT
);
1338 ASSERT(samp
->WrapT
== GL_REPEAT
);
1339 for (i
= 0; i
< n
; i
++) {
1340 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1341 if (level
>= tObj
->_MaxLevel
) {
1342 sample_2d_linear_repeat(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1343 texcoord
[i
], rgba
[i
]);
1346 GLfloat t0
[4], t1
[4]; /* texels */
1347 const GLfloat f
= FRAC(lambda
[i
]);
1348 sample_2d_linear_repeat(ctx
, samp
, tObj
->Image
[0][level
],
1350 sample_2d_linear_repeat(ctx
, samp
, tObj
->Image
[0][level
+1],
1352 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1358 /** Sample 2D texture, nearest filtering for both min/magnification */
1360 sample_nearest_2d(struct gl_context
*ctx
,
1361 const struct gl_sampler_object
*samp
,
1362 const struct gl_texture_object
*tObj
, GLuint n
,
1363 const GLfloat texcoords
[][4],
1364 const GLfloat lambda
[], GLfloat rgba
[][4])
1367 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1369 for (i
= 0; i
< n
; i
++) {
1370 sample_2d_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1375 /** Sample 2D texture, linear filtering for both min/magnification */
1377 sample_linear_2d(struct gl_context
*ctx
,
1378 const struct gl_sampler_object
*samp
,
1379 const struct gl_texture_object
*tObj
, GLuint n
,
1380 const GLfloat texcoords
[][4],
1381 const GLfloat lambda
[], GLfloat rgba
[][4])
1384 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1385 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(image
);
1387 if (samp
->WrapS
== GL_REPEAT
&&
1388 samp
->WrapT
== GL_REPEAT
&&
1389 swImg
->_IsPowerOfTwo
&&
1390 image
->Border
== 0) {
1391 for (i
= 0; i
< n
; i
++) {
1392 sample_2d_linear_repeat(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1396 for (i
= 0; i
< n
; i
++) {
1397 sample_2d_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1404 * Optimized 2-D texture sampling:
1405 * S and T wrap mode == GL_REPEAT
1406 * GL_NEAREST min/mag filter
1408 * RowStride == Width,
1412 opt_sample_rgb_2d(struct gl_context
*ctx
,
1413 const struct gl_sampler_object
*samp
,
1414 const struct gl_texture_object
*tObj
,
1415 GLuint n
, const GLfloat texcoords
[][4],
1416 const GLfloat lambda
[], GLfloat rgba
[][4])
1418 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1419 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1420 const GLfloat width
= (GLfloat
) img
->Width
;
1421 const GLfloat height
= (GLfloat
) img
->Height
;
1422 const GLint colMask
= img
->Width
- 1;
1423 const GLint rowMask
= img
->Height
- 1;
1424 const GLint shift
= img
->WidthLog2
;
1428 ASSERT(samp
->WrapS
==GL_REPEAT
);
1429 ASSERT(samp
->WrapT
==GL_REPEAT
);
1430 ASSERT(img
->Border
==0);
1431 ASSERT(img
->TexFormat
== MESA_FORMAT_BGR_UNORM8
);
1432 ASSERT(swImg
->_IsPowerOfTwo
);
1435 for (k
=0; k
<n
; k
++) {
1436 GLint i
= IFLOOR(texcoords
[k
][0] * width
) & colMask
;
1437 GLint j
= IFLOOR(texcoords
[k
][1] * height
) & rowMask
;
1438 GLint pos
= (j
<< shift
) | i
;
1439 GLubyte
*texel
= (GLubyte
*) swImg
->ImageSlices
[0] + 3 * pos
;
1440 rgba
[k
][RCOMP
] = UBYTE_TO_FLOAT(texel
[2]);
1441 rgba
[k
][GCOMP
] = UBYTE_TO_FLOAT(texel
[1]);
1442 rgba
[k
][BCOMP
] = UBYTE_TO_FLOAT(texel
[0]);
1443 rgba
[k
][ACOMP
] = 1.0F
;
1449 * Optimized 2-D texture sampling:
1450 * S and T wrap mode == GL_REPEAT
1451 * GL_NEAREST min/mag filter
1453 * RowStride == Width,
1457 opt_sample_rgba_2d(struct gl_context
*ctx
,
1458 const struct gl_sampler_object
*samp
,
1459 const struct gl_texture_object
*tObj
,
1460 GLuint n
, const GLfloat texcoords
[][4],
1461 const GLfloat lambda
[], GLfloat rgba
[][4])
1463 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1464 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1465 const GLfloat width
= (GLfloat
) img
->Width
;
1466 const GLfloat height
= (GLfloat
) img
->Height
;
1467 const GLint colMask
= img
->Width
- 1;
1468 const GLint rowMask
= img
->Height
- 1;
1469 const GLint shift
= img
->WidthLog2
;
1473 ASSERT(samp
->WrapS
==GL_REPEAT
);
1474 ASSERT(samp
->WrapT
==GL_REPEAT
);
1475 ASSERT(img
->Border
==0);
1476 ASSERT(img
->TexFormat
== MESA_FORMAT_A8B8G8R8_UNORM
);
1477 ASSERT(swImg
->_IsPowerOfTwo
);
1480 for (i
= 0; i
< n
; i
++) {
1481 const GLint col
= IFLOOR(texcoords
[i
][0] * width
) & colMask
;
1482 const GLint row
= IFLOOR(texcoords
[i
][1] * height
) & rowMask
;
1483 const GLint pos
= (row
<< shift
) | col
;
1484 const GLuint texel
= *((GLuint
*) swImg
->ImageSlices
[0] + pos
);
1485 rgba
[i
][RCOMP
] = UBYTE_TO_FLOAT( (texel
>> 24) );
1486 rgba
[i
][GCOMP
] = UBYTE_TO_FLOAT( (texel
>> 16) & 0xff );
1487 rgba
[i
][BCOMP
] = UBYTE_TO_FLOAT( (texel
>> 8) & 0xff );
1488 rgba
[i
][ACOMP
] = UBYTE_TO_FLOAT( (texel
) & 0xff );
1493 /** Sample 2D texture, using lambda to choose between min/magnification */
1495 sample_lambda_2d(struct gl_context
*ctx
,
1496 const struct gl_sampler_object
*samp
,
1497 const struct gl_texture_object
*tObj
,
1498 GLuint n
, const GLfloat texcoords
[][4],
1499 const GLfloat lambda
[], GLfloat rgba
[][4])
1501 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1502 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(tImg
);
1503 GLuint minStart
, minEnd
; /* texels with minification */
1504 GLuint magStart
, magEnd
; /* texels with magnification */
1506 const GLboolean repeatNoBorderPOT
= (samp
->WrapS
== GL_REPEAT
)
1507 && (samp
->WrapT
== GL_REPEAT
)
1508 && (tImg
->Border
== 0)
1509 && (_mesa_format_row_stride(tImg
->TexFormat
, tImg
->Width
) ==
1511 && swImg
->_IsPowerOfTwo
;
1513 ASSERT(lambda
!= NULL
);
1514 compute_min_mag_ranges(samp
, n
, lambda
,
1515 &minStart
, &minEnd
, &magStart
, &magEnd
);
1517 if (minStart
< minEnd
) {
1518 /* do the minified texels */
1519 const GLuint m
= minEnd
- minStart
;
1520 switch (samp
->MinFilter
) {
1522 if (repeatNoBorderPOT
) {
1523 switch (tImg
->TexFormat
) {
1524 case MESA_FORMAT_BGR_UNORM8
:
1525 opt_sample_rgb_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1526 NULL
, rgba
+ minStart
);
1528 case MESA_FORMAT_A8B8G8R8_UNORM
:
1529 opt_sample_rgba_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1530 NULL
, rgba
+ minStart
);
1533 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1534 NULL
, rgba
+ minStart
);
1538 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1539 NULL
, rgba
+ minStart
);
1543 sample_linear_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1544 NULL
, rgba
+ minStart
);
1546 case GL_NEAREST_MIPMAP_NEAREST
:
1547 sample_2d_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
,
1548 texcoords
+ minStart
,
1549 lambda
+ minStart
, rgba
+ minStart
);
1551 case GL_LINEAR_MIPMAP_NEAREST
:
1552 sample_2d_linear_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1553 lambda
+ minStart
, rgba
+ minStart
);
1555 case GL_NEAREST_MIPMAP_LINEAR
:
1556 sample_2d_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1557 lambda
+ minStart
, rgba
+ minStart
);
1559 case GL_LINEAR_MIPMAP_LINEAR
:
1560 if (repeatNoBorderPOT
)
1561 sample_2d_linear_mipmap_linear_repeat(ctx
, samp
, tObj
, m
,
1562 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1564 sample_2d_linear_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1565 lambda
+ minStart
, rgba
+ minStart
);
1568 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1573 if (magStart
< magEnd
) {
1574 /* do the magnified texels */
1575 const GLuint m
= magEnd
- magStart
;
1577 switch (samp
->MagFilter
) {
1579 if (repeatNoBorderPOT
) {
1580 switch (tImg
->TexFormat
) {
1581 case MESA_FORMAT_BGR_UNORM8
:
1582 opt_sample_rgb_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1583 NULL
, rgba
+ magStart
);
1585 case MESA_FORMAT_A8B8G8R8_UNORM
:
1586 opt_sample_rgba_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1587 NULL
, rgba
+ magStart
);
1590 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1591 NULL
, rgba
+ magStart
);
1595 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1596 NULL
, rgba
+ magStart
);
1600 sample_linear_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1601 NULL
, rgba
+ magStart
);
1604 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1611 /* For anisotropic filtering */
1612 #define WEIGHT_LUT_SIZE 1024
1614 static GLfloat
*weightLut
= NULL
;
1617 * Creates the look-up table used to speed-up EWA sampling
1620 create_filter_table(void)
1624 weightLut
= malloc(WEIGHT_LUT_SIZE
* sizeof(GLfloat
));
1626 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
1628 GLfloat r2
= (GLfloat
) i
/ (GLfloat
) (WEIGHT_LUT_SIZE
- 1);
1629 GLfloat weight
= (GLfloat
) exp(-alpha
* r2
);
1630 weightLut
[i
] = weight
;
1637 * Elliptical weighted average (EWA) filter for producing high quality
1638 * anisotropic filtered results.
1639 * Based on the Higher Quality Elliptical Weighted Avarage Filter
1640 * published by Paul S. Heckbert in his Master's Thesis
1641 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1644 sample_2d_ewa(struct gl_context
*ctx
,
1645 const struct gl_sampler_object
*samp
,
1646 const struct gl_texture_object
*tObj
,
1647 const GLfloat texcoord
[4],
1648 const GLfloat dudx
, const GLfloat dvdx
,
1649 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1652 GLint level
= lod
> 0 ? lod
: 0;
1653 GLfloat scaling
= 1.0f
/ (1 << level
);
1654 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1655 const struct gl_texture_image
*mostDetailedImage
=
1656 tObj
->Image
[0][tObj
->BaseLevel
];
1657 const struct swrast_texture_image
*swImg
=
1658 swrast_texture_image_const(mostDetailedImage
);
1659 GLfloat tex_u
= -0.5f
+ texcoord
[0] * swImg
->WidthScale
* scaling
;
1660 GLfloat tex_v
= -0.5f
+ texcoord
[1] * swImg
->HeightScale
* scaling
;
1662 GLfloat ux
= dudx
* scaling
;
1663 GLfloat vx
= dvdx
* scaling
;
1664 GLfloat uy
= dudy
* scaling
;
1665 GLfloat vy
= dvdy
* scaling
;
1667 /* compute ellipse coefficients to bound the region:
1668 * A*x*x + B*x*y + C*y*y = F.
1670 GLfloat A
= vx
*vx
+vy
*vy
+1;
1671 GLfloat B
= -2*(ux
*vx
+uy
*vy
);
1672 GLfloat C
= ux
*ux
+uy
*uy
+1;
1673 GLfloat F
= A
*C
-B
*B
/4.0f
;
1675 /* check if it is an ellipse */
1676 /* ASSERT(F > 0.0); */
1678 /* Compute the ellipse's (u,v) bounding box in texture space */
1679 GLfloat d
= -B
*B
+4.0f
*C
*A
;
1680 GLfloat box_u
= 2.0f
/ d
* sqrtf(d
*C
*F
); /* box_u -> half of bbox with */
1681 GLfloat box_v
= 2.0f
/ d
* sqrtf(A
*d
*F
); /* box_v -> half of bbox height */
1683 GLint u0
= (GLint
) floorf(tex_u
- box_u
);
1684 GLint u1
= (GLint
) ceilf (tex_u
+ box_u
);
1685 GLint v0
= (GLint
) floorf(tex_v
- box_v
);
1686 GLint v1
= (GLint
) ceilf (tex_v
+ box_v
);
1688 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1689 GLfloat newCoord
[2];
1692 GLfloat U
= u0
- tex_u
;
1695 /* Scale ellipse formula to directly index the Filter Lookup Table.
1696 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1698 GLfloat formScale
= (GLfloat
) (WEIGHT_LUT_SIZE
- 1) / F
;
1702 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1704 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1705 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1706 * value, q, is less than F, we're inside the ellipse
1709 for (v
= v0
; v
<= v1
; ++v
) {
1710 GLfloat V
= v
- tex_v
;
1711 GLfloat dq
= A
* (2 * U
+ 1) + B
* V
;
1712 GLfloat q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
1715 for (u
= u0
; u
<= u1
; ++u
) {
1716 /* Note that the ellipse has been pre-scaled so F = WEIGHT_LUT_SIZE - 1 */
1717 if (q
< WEIGHT_LUT_SIZE
) {
1718 /* as a LUT is used, q must never be negative;
1719 * should not happen, though
1721 const GLint qClamped
= q
>= 0.0F
? (GLint
) q
: 0;
1722 GLfloat weight
= weightLut
[qClamped
];
1724 newCoord
[0] = u
/ ((GLfloat
) img
->Width2
);
1725 newCoord
[1] = v
/ ((GLfloat
) img
->Height2
);
1727 sample_2d_nearest(ctx
, samp
, img
, newCoord
, rgba
);
1728 num
[0] += weight
* rgba
[0];
1729 num
[1] += weight
* rgba
[1];
1730 num
[2] += weight
* rgba
[2];
1731 num
[3] += weight
* rgba
[3];
1741 /* Reaching this place would mean
1742 * that no pixels intersected the ellipse.
1743 * This should never happen because
1744 * the filter we use always
1745 * intersects at least one pixel.
1752 /* not enough pixels in resampling, resort to direct interpolation */
1753 sample_2d_linear(ctx
, samp
, img
, texcoord
, rgba
);
1757 rgba
[0] = num
[0] / den
;
1758 rgba
[1] = num
[1] / den
;
1759 rgba
[2] = num
[2] / den
;
1760 rgba
[3] = num
[3] / den
;
1765 * Anisotropic filtering using footprint assembly as outlined in the
1766 * EXT_texture_filter_anisotropic spec:
1767 * http://www.opengl.org/registry/specs/EXT/texture_filter_anisotropic.txt
1768 * Faster than EWA but has less quality (more aliasing effects)
1771 sample_2d_footprint(struct gl_context
*ctx
,
1772 const struct gl_sampler_object
*samp
,
1773 const struct gl_texture_object
*tObj
,
1774 const GLfloat texcoord
[4],
1775 const GLfloat dudx
, const GLfloat dvdx
,
1776 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1779 GLint level
= lod
> 0 ? lod
: 0;
1780 GLfloat scaling
= 1.0F
/ (1 << level
);
1781 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1783 GLfloat ux
= dudx
* scaling
;
1784 GLfloat vx
= dvdx
* scaling
;
1785 GLfloat uy
= dudy
* scaling
;
1786 GLfloat vy
= dvdy
* scaling
;
1788 GLfloat Px2
= ux
* ux
+ vx
* vx
; /* squared length of dx */
1789 GLfloat Py2
= uy
* uy
+ vy
* vy
; /* squared length of dy */
1795 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1796 GLfloat newCoord
[2];
1799 /* Calculate the per anisotropic sample offsets in s,t space. */
1801 numSamples
= (GLint
) ceilf(sqrtf(Px2
));
1802 ds
= ux
/ ((GLfloat
) img
->Width2
);
1803 dt
= vx
/ ((GLfloat
) img
->Height2
);
1806 numSamples
= (GLint
) ceilf(sqrtf(Py2
));
1807 ds
= uy
/ ((GLfloat
) img
->Width2
);
1808 dt
= vy
/ ((GLfloat
) img
->Height2
);
1811 for (s
= 0; s
<numSamples
; s
++) {
1812 newCoord
[0] = texcoord
[0] + ds
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5f
);
1813 newCoord
[1] = texcoord
[1] + dt
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5f
);
1815 sample_2d_linear(ctx
, samp
, img
, newCoord
, rgba
);
1822 rgba
[0] = num
[0] / numSamples
;
1823 rgba
[1] = num
[1] / numSamples
;
1824 rgba
[2] = num
[2] / numSamples
;
1825 rgba
[3] = num
[3] / numSamples
;
1830 * Returns the index of the specified texture object in the
1831 * gl_context texture unit array.
1833 static inline GLuint
1834 texture_unit_index(const struct gl_context
*ctx
,
1835 const struct gl_texture_object
*tObj
)
1837 const GLuint maxUnit
1838 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
1841 /* XXX CoordUnits vs. ImageUnits */
1842 for (u
= 0; u
< maxUnit
; u
++) {
1843 if (ctx
->Texture
.Unit
[u
]._Current
== tObj
)
1847 u
= 0; /* not found, use 1st one; should never happen */
1854 * Sample 2D texture using an anisotropic filter.
1855 * NOTE: the const GLfloat lambda_iso[] parameter does *NOT* contain
1856 * the lambda float array but a "hidden" SWspan struct which is required
1857 * by this function but is not available in the texture_sample_func signature.
1858 * See _swrast_texture_span( struct gl_context *ctx, SWspan *span ) on how
1859 * this function is called.
1862 sample_lambda_2d_aniso(struct gl_context
*ctx
,
1863 const struct gl_sampler_object
*samp
,
1864 const struct gl_texture_object
*tObj
,
1865 GLuint n
, const GLfloat texcoords
[][4],
1866 const GLfloat lambda_iso
[], GLfloat rgba
[][4])
1868 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1869 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(tImg
);
1870 const GLfloat maxEccentricity
=
1871 samp
->MaxAnisotropy
* samp
->MaxAnisotropy
;
1873 /* re-calculate the lambda values so that they are usable with anisotropic
1876 SWspan
*span
= (SWspan
*)lambda_iso
; /* access the "hidden" SWspan struct */
1878 /* based on interpolate_texcoords(struct gl_context *ctx, SWspan *span)
1879 * in swrast/s_span.c
1882 /* find the texture unit index by looking up the current texture object
1883 * from the context list of available texture objects.
1885 const GLuint u
= texture_unit_index(ctx
, tObj
);
1886 const GLuint attr
= VARYING_SLOT_TEX0
+ u
;
1889 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
1890 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
1891 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
1892 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
1893 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
1894 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
1895 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
1896 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
1897 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
1899 /* from swrast/s_texcombine.c _swrast_texture_span */
1900 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[u
];
1901 const GLboolean adjustLOD
=
1902 (texUnit
->LodBias
+ samp
->LodBias
!= 0.0F
)
1903 || (samp
->MinLod
!= -1000.0 || samp
->MaxLod
!= 1000.0);
1907 /* on first access create the lookup table containing the filter weights. */
1909 create_filter_table();
1912 texW
= swImg
->WidthScale
;
1913 texH
= swImg
->HeightScale
;
1915 for (i
= 0; i
< n
; i
++) {
1916 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
1918 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
1919 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
1920 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
1921 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
1923 /* note: instead of working with Px and Py, we will use the
1924 * squared length instead, to avoid sqrt.
1926 GLfloat Px2
= dudx
* dudx
+ dvdx
* dvdx
;
1927 GLfloat Py2
= dudy
* dudy
+ dvdy
* dvdy
;
1947 /* if the eccentricity of the ellipse is too big, scale up the shorter
1948 * of the two vectors to limit the maximum amount of work per pixel
1951 if (e
> maxEccentricity
) {
1952 /* GLfloat s=e / maxEccentricity;
1956 Pmin2
= Pmax2
/ maxEccentricity
;
1959 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
1960 * this since 0.5*log(x) = log(sqrt(x))
1962 lod
= 0.5f
* LOG2(Pmin2
);
1965 /* from swrast/s_texcombine.c _swrast_texture_span */
1966 if (texUnit
->LodBias
+ samp
->LodBias
!= 0.0F
) {
1967 /* apply LOD bias, but don't clamp yet */
1968 const GLfloat bias
=
1969 CLAMP(texUnit
->LodBias
+ samp
->LodBias
,
1970 -ctx
->Const
.MaxTextureLodBias
,
1971 ctx
->Const
.MaxTextureLodBias
);
1974 if (samp
->MinLod
!= -1000.0 ||
1975 samp
->MaxLod
!= 1000.0) {
1976 /* apply LOD clamping to lambda */
1977 lod
= CLAMP(lod
, samp
->MinLod
, samp
->MaxLod
);
1982 /* If the ellipse covers the whole image, we can
1983 * simply return the average of the whole image.
1985 if (lod
>= tObj
->_MaxLevel
) {
1986 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1987 texcoords
[i
], rgba
[i
]);
1990 /* don't bother interpolating between multiple LODs; it doesn't
1991 * seem to be worth the extra running time.
1993 sample_2d_ewa(ctx
, samp
, tObj
, texcoords
[i
],
1994 dudx
, dvdx
, dudy
, dvdy
, (GLint
) floorf(lod
), rgba
[i
]);
1997 (void) sample_2d_footprint
;
1999 sample_2d_footprint(ctx, tObj, texcoords[i],
2000 dudx, dvdx, dudy, dvdy, floor(lod), rgba[i]);
2008 /**********************************************************************/
2009 /* 3-D Texture Sampling Functions */
2010 /**********************************************************************/
2013 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2016 sample_3d_nearest(struct gl_context
*ctx
,
2017 const struct gl_sampler_object
*samp
,
2018 const struct gl_texture_image
*img
,
2019 const GLfloat texcoord
[4],
2022 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2023 const GLint width
= img
->Width2
; /* without border, power of two */
2024 const GLint height
= img
->Height2
; /* without border, power of two */
2025 const GLint depth
= img
->Depth2
; /* without border, power of two */
2029 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
2030 j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
2031 k
= nearest_texel_location(samp
->WrapR
, img
, depth
, texcoord
[2]);
2033 if (i
< 0 || i
>= (GLint
) img
->Width
||
2034 j
< 0 || j
>= (GLint
) img
->Height
||
2035 k
< 0 || k
>= (GLint
) img
->Depth
) {
2036 /* Need this test for GL_CLAMP_TO_BORDER mode */
2037 get_border_color(samp
, img
, rgba
);
2040 swImg
->FetchTexel(swImg
, i
, j
, k
, rgba
);
2046 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2049 sample_3d_linear(struct gl_context
*ctx
,
2050 const struct gl_sampler_object
*samp
,
2051 const struct gl_texture_image
*img
,
2052 const GLfloat texcoord
[4],
2055 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2056 const GLint width
= img
->Width2
;
2057 const GLint height
= img
->Height2
;
2058 const GLint depth
= img
->Depth2
;
2059 GLint i0
, j0
, k0
, i1
, j1
, k1
;
2060 GLbitfield useBorderColor
= 0x0;
2062 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
2063 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
2065 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2066 linear_texel_locations(samp
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2067 linear_texel_locations(samp
->WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
2078 /* check if sampling texture border color */
2079 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2080 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2081 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2082 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2083 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
2084 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
2088 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
2089 get_border_color(samp
, img
, t000
);
2092 swImg
->FetchTexel(swImg
, i0
, j0
, k0
, t000
);
2094 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
2095 get_border_color(samp
, img
, t100
);
2098 swImg
->FetchTexel(swImg
, i1
, j0
, k0
, t100
);
2100 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
2101 get_border_color(samp
, img
, t010
);
2104 swImg
->FetchTexel(swImg
, i0
, j1
, k0
, t010
);
2106 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
2107 get_border_color(samp
, img
, t110
);
2110 swImg
->FetchTexel(swImg
, i1
, j1
, k0
, t110
);
2113 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
2114 get_border_color(samp
, img
, t001
);
2117 swImg
->FetchTexel(swImg
, i0
, j0
, k1
, t001
);
2119 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
2120 get_border_color(samp
, img
, t101
);
2123 swImg
->FetchTexel(swImg
, i1
, j0
, k1
, t101
);
2125 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
2126 get_border_color(samp
, img
, t011
);
2129 swImg
->FetchTexel(swImg
, i0
, j1
, k1
, t011
);
2131 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
2132 get_border_color(samp
, img
, t111
);
2135 swImg
->FetchTexel(swImg
, i1
, j1
, k1
, t111
);
2138 /* trilinear interpolation of samples */
2139 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
2144 sample_3d_nearest_mipmap_nearest(struct gl_context
*ctx
,
2145 const struct gl_sampler_object
*samp
,
2146 const struct gl_texture_object
*tObj
,
2147 GLuint n
, const GLfloat texcoord
[][4],
2148 const GLfloat lambda
[], GLfloat rgba
[][4] )
2151 for (i
= 0; i
< n
; i
++) {
2152 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2153 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2159 sample_3d_linear_mipmap_nearest(struct gl_context
*ctx
,
2160 const struct gl_sampler_object
*samp
,
2161 const struct gl_texture_object
*tObj
,
2162 GLuint n
, const GLfloat texcoord
[][4],
2163 const GLfloat lambda
[], GLfloat rgba
[][4])
2166 ASSERT(lambda
!= NULL
);
2167 for (i
= 0; i
< n
; i
++) {
2168 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2169 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2175 sample_3d_nearest_mipmap_linear(struct gl_context
*ctx
,
2176 const struct gl_sampler_object
*samp
,
2177 const struct gl_texture_object
*tObj
,
2178 GLuint n
, const GLfloat texcoord
[][4],
2179 const GLfloat lambda
[], GLfloat rgba
[][4])
2182 ASSERT(lambda
!= NULL
);
2183 for (i
= 0; i
< n
; i
++) {
2184 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2185 if (level
>= tObj
->_MaxLevel
) {
2186 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2187 texcoord
[i
], rgba
[i
]);
2190 GLfloat t0
[4], t1
[4]; /* texels */
2191 const GLfloat f
= FRAC(lambda
[i
]);
2192 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2193 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2194 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2201 sample_3d_linear_mipmap_linear(struct gl_context
*ctx
,
2202 const struct gl_sampler_object
*samp
,
2203 const struct gl_texture_object
*tObj
,
2204 GLuint n
, const GLfloat texcoord
[][4],
2205 const GLfloat lambda
[], GLfloat rgba
[][4])
2208 ASSERT(lambda
!= NULL
);
2209 for (i
= 0; i
< n
; i
++) {
2210 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2211 if (level
>= tObj
->_MaxLevel
) {
2212 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2213 texcoord
[i
], rgba
[i
]);
2216 GLfloat t0
[4], t1
[4]; /* texels */
2217 const GLfloat f
= FRAC(lambda
[i
]);
2218 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2219 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2220 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2226 /** Sample 3D texture, nearest filtering for both min/magnification */
2228 sample_nearest_3d(struct gl_context
*ctx
,
2229 const struct gl_sampler_object
*samp
,
2230 const struct gl_texture_object
*tObj
, GLuint n
,
2231 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2235 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2237 for (i
= 0; i
< n
; i
++) {
2238 sample_3d_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
2243 /** Sample 3D texture, linear filtering for both min/magnification */
2245 sample_linear_3d(struct gl_context
*ctx
,
2246 const struct gl_sampler_object
*samp
,
2247 const struct gl_texture_object
*tObj
, GLuint n
,
2248 const GLfloat texcoords
[][4],
2249 const GLfloat lambda
[], GLfloat rgba
[][4])
2252 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2254 for (i
= 0; i
< n
; i
++) {
2255 sample_3d_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
2260 /** Sample 3D texture, using lambda to choose between min/magnification */
2262 sample_lambda_3d(struct gl_context
*ctx
,
2263 const struct gl_sampler_object
*samp
,
2264 const struct gl_texture_object
*tObj
, GLuint n
,
2265 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2268 GLuint minStart
, minEnd
; /* texels with minification */
2269 GLuint magStart
, magEnd
; /* texels with magnification */
2272 ASSERT(lambda
!= NULL
);
2273 compute_min_mag_ranges(samp
, n
, lambda
,
2274 &minStart
, &minEnd
, &magStart
, &magEnd
);
2276 if (minStart
< minEnd
) {
2277 /* do the minified texels */
2278 GLuint m
= minEnd
- minStart
;
2279 switch (samp
->MinFilter
) {
2281 for (i
= minStart
; i
< minEnd
; i
++)
2282 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
2283 texcoords
[i
], rgba
[i
]);
2286 for (i
= minStart
; i
< minEnd
; i
++)
2287 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
2288 texcoords
[i
], rgba
[i
]);
2290 case GL_NEAREST_MIPMAP_NEAREST
:
2291 sample_3d_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2292 lambda
+ minStart
, rgba
+ minStart
);
2294 case GL_LINEAR_MIPMAP_NEAREST
:
2295 sample_3d_linear_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2296 lambda
+ minStart
, rgba
+ minStart
);
2298 case GL_NEAREST_MIPMAP_LINEAR
:
2299 sample_3d_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2300 lambda
+ minStart
, rgba
+ minStart
);
2302 case GL_LINEAR_MIPMAP_LINEAR
:
2303 sample_3d_linear_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2304 lambda
+ minStart
, rgba
+ minStart
);
2307 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
2312 if (magStart
< magEnd
) {
2313 /* do the magnified texels */
2314 switch (samp
->MagFilter
) {
2316 for (i
= magStart
; i
< magEnd
; i
++)
2317 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
2318 texcoords
[i
], rgba
[i
]);
2321 for (i
= magStart
; i
< magEnd
; i
++)
2322 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
2323 texcoords
[i
], rgba
[i
]);
2326 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
2333 /**********************************************************************/
2334 /* Texture Cube Map Sampling Functions */
2335 /**********************************************************************/
2338 * Choose one of six sides of a texture cube map given the texture
2339 * coord (rx,ry,rz). Return pointer to corresponding array of texture
2342 static const struct gl_texture_image
**
2343 choose_cube_face(const struct gl_texture_object
*texObj
,
2344 const GLfloat texcoord
[4], GLfloat newCoord
[4])
2348 direction target sc tc ma
2349 ---------- ------------------------------- --- --- ---
2350 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2351 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2352 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2353 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2354 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2355 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2357 const GLfloat rx
= texcoord
[0];
2358 const GLfloat ry
= texcoord
[1];
2359 const GLfloat rz
= texcoord
[2];
2360 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
2364 if (arx
>= ary
&& arx
>= arz
) {
2378 else if (ary
>= arx
&& ary
>= arz
) {
2408 const float ima
= 1.0F
/ ma
;
2409 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
2410 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
2413 return (const struct gl_texture_image
**) texObj
->Image
[face
];
2418 sample_nearest_cube(struct gl_context
*ctx
,
2419 const struct gl_sampler_object
*samp
,
2420 const struct gl_texture_object
*tObj
, GLuint n
,
2421 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2426 for (i
= 0; i
< n
; i
++) {
2427 const struct gl_texture_image
**images
;
2428 GLfloat newCoord
[4];
2429 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2430 sample_2d_nearest(ctx
, samp
, images
[tObj
->BaseLevel
],
2433 if (is_depth_texture(tObj
)) {
2434 for (i
= 0; i
< n
; i
++) {
2435 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2442 sample_linear_cube(struct gl_context
*ctx
,
2443 const struct gl_sampler_object
*samp
,
2444 const struct gl_texture_object
*tObj
, GLuint n
,
2445 const GLfloat texcoords
[][4],
2446 const GLfloat lambda
[], GLfloat rgba
[][4])
2450 for (i
= 0; i
< n
; i
++) {
2451 const struct gl_texture_image
**images
;
2452 GLfloat newCoord
[4];
2453 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2454 sample_2d_linear(ctx
, samp
, images
[tObj
->BaseLevel
],
2457 if (is_depth_texture(tObj
)) {
2458 for (i
= 0; i
< n
; i
++) {
2459 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2466 sample_cube_nearest_mipmap_nearest(struct gl_context
*ctx
,
2467 const struct gl_sampler_object
*samp
,
2468 const struct gl_texture_object
*tObj
,
2469 GLuint n
, const GLfloat texcoord
[][4],
2470 const GLfloat lambda
[], GLfloat rgba
[][4])
2473 ASSERT(lambda
!= NULL
);
2474 for (i
= 0; i
< n
; i
++) {
2475 const struct gl_texture_image
**images
;
2476 GLfloat newCoord
[4];
2478 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2480 /* XXX we actually need to recompute lambda here based on the newCoords.
2481 * But we would need the texcoords of adjacent fragments to compute that
2482 * properly, and we don't have those here.
2483 * For now, do an approximation: subtracting 1 from the chosen mipmap
2484 * level seems to work in some test cases.
2485 * The same adjustment is done in the next few functions.
2487 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2488 level
= MAX2(level
- 1, 0);
2490 sample_2d_nearest(ctx
, samp
, images
[level
], newCoord
, rgba
[i
]);
2492 if (is_depth_texture(tObj
)) {
2493 for (i
= 0; i
< n
; i
++) {
2494 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2501 sample_cube_linear_mipmap_nearest(struct gl_context
*ctx
,
2502 const struct gl_sampler_object
*samp
,
2503 const struct gl_texture_object
*tObj
,
2504 GLuint n
, const GLfloat texcoord
[][4],
2505 const GLfloat lambda
[], GLfloat rgba
[][4])
2508 ASSERT(lambda
!= NULL
);
2509 for (i
= 0; i
< n
; i
++) {
2510 const struct gl_texture_image
**images
;
2511 GLfloat newCoord
[4];
2512 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2513 level
= MAX2(level
- 1, 0); /* see comment above */
2514 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2515 sample_2d_linear(ctx
, samp
, images
[level
], newCoord
, rgba
[i
]);
2517 if (is_depth_texture(tObj
)) {
2518 for (i
= 0; i
< n
; i
++) {
2519 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2526 sample_cube_nearest_mipmap_linear(struct gl_context
*ctx
,
2527 const struct gl_sampler_object
*samp
,
2528 const struct gl_texture_object
*tObj
,
2529 GLuint n
, const GLfloat texcoord
[][4],
2530 const GLfloat lambda
[], GLfloat rgba
[][4])
2533 ASSERT(lambda
!= NULL
);
2534 for (i
= 0; i
< n
; i
++) {
2535 const struct gl_texture_image
**images
;
2536 GLfloat newCoord
[4];
2537 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2538 level
= MAX2(level
- 1, 0); /* see comment above */
2539 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2540 if (level
>= tObj
->_MaxLevel
) {
2541 sample_2d_nearest(ctx
, samp
, images
[tObj
->_MaxLevel
],
2545 GLfloat t0
[4], t1
[4]; /* texels */
2546 const GLfloat f
= FRAC(lambda
[i
]);
2547 sample_2d_nearest(ctx
, samp
, images
[level
], newCoord
, t0
);
2548 sample_2d_nearest(ctx
, samp
, images
[level
+1], newCoord
, t1
);
2549 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2552 if (is_depth_texture(tObj
)) {
2553 for (i
= 0; i
< n
; i
++) {
2554 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2561 sample_cube_linear_mipmap_linear(struct gl_context
*ctx
,
2562 const struct gl_sampler_object
*samp
,
2563 const struct gl_texture_object
*tObj
,
2564 GLuint n
, const GLfloat texcoord
[][4],
2565 const GLfloat lambda
[], GLfloat rgba
[][4])
2568 ASSERT(lambda
!= NULL
);
2569 for (i
= 0; i
< n
; i
++) {
2570 const struct gl_texture_image
**images
;
2571 GLfloat newCoord
[4];
2572 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2573 level
= MAX2(level
- 1, 0); /* see comment above */
2574 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2575 if (level
>= tObj
->_MaxLevel
) {
2576 sample_2d_linear(ctx
, samp
, images
[tObj
->_MaxLevel
],
2580 GLfloat t0
[4], t1
[4];
2581 const GLfloat f
= FRAC(lambda
[i
]);
2582 sample_2d_linear(ctx
, samp
, images
[level
], newCoord
, t0
);
2583 sample_2d_linear(ctx
, samp
, images
[level
+1], newCoord
, t1
);
2584 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2587 if (is_depth_texture(tObj
)) {
2588 for (i
= 0; i
< n
; i
++) {
2589 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2595 /** Sample cube texture, using lambda to choose between min/magnification */
2597 sample_lambda_cube(struct gl_context
*ctx
,
2598 const struct gl_sampler_object
*samp
,
2599 const struct gl_texture_object
*tObj
, GLuint n
,
2600 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2603 GLuint minStart
, minEnd
; /* texels with minification */
2604 GLuint magStart
, magEnd
; /* texels with magnification */
2606 ASSERT(lambda
!= NULL
);
2607 compute_min_mag_ranges(samp
, n
, lambda
,
2608 &minStart
, &minEnd
, &magStart
, &magEnd
);
2610 if (minStart
< minEnd
) {
2611 /* do the minified texels */
2612 const GLuint m
= minEnd
- minStart
;
2613 switch (samp
->MinFilter
) {
2615 sample_nearest_cube(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2616 lambda
+ minStart
, rgba
+ minStart
);
2619 sample_linear_cube(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2620 lambda
+ minStart
, rgba
+ minStart
);
2622 case GL_NEAREST_MIPMAP_NEAREST
:
2623 sample_cube_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
,
2624 texcoords
+ minStart
,
2625 lambda
+ minStart
, rgba
+ minStart
);
2627 case GL_LINEAR_MIPMAP_NEAREST
:
2628 sample_cube_linear_mipmap_nearest(ctx
, samp
, tObj
, m
,
2629 texcoords
+ minStart
,
2630 lambda
+ minStart
, rgba
+ minStart
);
2632 case GL_NEAREST_MIPMAP_LINEAR
:
2633 sample_cube_nearest_mipmap_linear(ctx
, samp
, tObj
, m
,
2634 texcoords
+ minStart
,
2635 lambda
+ minStart
, rgba
+ minStart
);
2637 case GL_LINEAR_MIPMAP_LINEAR
:
2638 sample_cube_linear_mipmap_linear(ctx
, samp
, tObj
, m
,
2639 texcoords
+ minStart
,
2640 lambda
+ minStart
, rgba
+ minStart
);
2643 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2648 if (magStart
< magEnd
) {
2649 /* do the magnified texels */
2650 const GLuint m
= magEnd
- magStart
;
2651 switch (samp
->MagFilter
) {
2653 sample_nearest_cube(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
2654 lambda
+ magStart
, rgba
+ magStart
);
2657 sample_linear_cube(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
2658 lambda
+ magStart
, rgba
+ magStart
);
2661 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2668 /**********************************************************************/
2669 /* Texture Rectangle Sampling Functions */
2670 /**********************************************************************/
2674 sample_nearest_rect(struct gl_context
*ctx
,
2675 const struct gl_sampler_object
*samp
,
2676 const struct gl_texture_object
*tObj
, GLuint n
,
2677 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2680 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2681 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2682 const GLint width
= img
->Width
;
2683 const GLint height
= img
->Height
;
2689 ASSERT(samp
->WrapS
== GL_CLAMP
||
2690 samp
->WrapS
== GL_CLAMP_TO_EDGE
||
2691 samp
->WrapS
== GL_CLAMP_TO_BORDER
);
2692 ASSERT(samp
->WrapT
== GL_CLAMP
||
2693 samp
->WrapT
== GL_CLAMP_TO_EDGE
||
2694 samp
->WrapT
== GL_CLAMP_TO_BORDER
);
2696 for (i
= 0; i
< n
; i
++) {
2698 col
= clamp_rect_coord_nearest(samp
->WrapS
, texcoords
[i
][0], width
);
2699 row
= clamp_rect_coord_nearest(samp
->WrapT
, texcoords
[i
][1], height
);
2700 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2701 get_border_color(samp
, img
, rgba
[i
]);
2703 swImg
->FetchTexel(swImg
, col
, row
, 0, rgba
[i
]);
2709 sample_linear_rect(struct gl_context
*ctx
,
2710 const struct gl_sampler_object
*samp
,
2711 const struct gl_texture_object
*tObj
, GLuint n
,
2712 const GLfloat texcoords
[][4],
2713 const GLfloat lambda
[], GLfloat rgba
[][4])
2715 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2716 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2717 const GLint width
= img
->Width
;
2718 const GLint height
= img
->Height
;
2724 ASSERT(samp
->WrapS
== GL_CLAMP
||
2725 samp
->WrapS
== GL_CLAMP_TO_EDGE
||
2726 samp
->WrapS
== GL_CLAMP_TO_BORDER
);
2727 ASSERT(samp
->WrapT
== GL_CLAMP
||
2728 samp
->WrapT
== GL_CLAMP_TO_EDGE
||
2729 samp
->WrapT
== GL_CLAMP_TO_BORDER
);
2731 for (i
= 0; i
< n
; i
++) {
2732 GLint i0
, j0
, i1
, j1
;
2733 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2735 GLbitfield useBorderColor
= 0x0;
2737 clamp_rect_coord_linear(samp
->WrapS
, texcoords
[i
][0], width
,
2739 clamp_rect_coord_linear(samp
->WrapT
, texcoords
[i
][1], height
,
2742 /* compute integer rows/columns */
2743 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2744 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2745 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2746 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2748 /* get four texel samples */
2749 if (useBorderColor
& (I0BIT
| J0BIT
))
2750 get_border_color(samp
, img
, t00
);
2752 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
2754 if (useBorderColor
& (I1BIT
| J0BIT
))
2755 get_border_color(samp
, img
, t10
);
2757 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
2759 if (useBorderColor
& (I0BIT
| J1BIT
))
2760 get_border_color(samp
, img
, t01
);
2762 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
2764 if (useBorderColor
& (I1BIT
| J1BIT
))
2765 get_border_color(samp
, img
, t11
);
2767 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
2769 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2774 /** Sample Rect texture, using lambda to choose between min/magnification */
2776 sample_lambda_rect(struct gl_context
*ctx
,
2777 const struct gl_sampler_object
*samp
,
2778 const struct gl_texture_object
*tObj
, GLuint n
,
2779 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2782 GLuint minStart
, minEnd
, magStart
, magEnd
;
2784 /* We only need lambda to decide between minification and magnification.
2785 * There is no mipmapping with rectangular textures.
2787 compute_min_mag_ranges(samp
, n
, lambda
,
2788 &minStart
, &minEnd
, &magStart
, &magEnd
);
2790 if (minStart
< minEnd
) {
2791 if (samp
->MinFilter
== GL_NEAREST
) {
2792 sample_nearest_rect(ctx
, samp
, tObj
, minEnd
- minStart
,
2793 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2796 sample_linear_rect(ctx
, samp
, tObj
, minEnd
- minStart
,
2797 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2800 if (magStart
< magEnd
) {
2801 if (samp
->MagFilter
== GL_NEAREST
) {
2802 sample_nearest_rect(ctx
, samp
, tObj
, magEnd
- magStart
,
2803 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2806 sample_linear_rect(ctx
, samp
, tObj
, magEnd
- magStart
,
2807 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2813 /**********************************************************************/
2814 /* 2D Texture Array Sampling Functions */
2815 /**********************************************************************/
2818 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2821 sample_2d_array_nearest(struct gl_context
*ctx
,
2822 const struct gl_sampler_object
*samp
,
2823 const struct gl_texture_image
*img
,
2824 const GLfloat texcoord
[4],
2827 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2828 const GLint width
= img
->Width2
; /* without border, power of two */
2829 const GLint height
= img
->Height2
; /* without border, power of two */
2830 const GLint depth
= img
->Depth
;
2835 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
2836 j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
2837 array
= tex_array_slice(texcoord
[2], depth
);
2839 if (i
< 0 || i
>= (GLint
) img
->Width
||
2840 j
< 0 || j
>= (GLint
) img
->Height
||
2841 array
< 0 || array
>= (GLint
) img
->Depth
) {
2842 /* Need this test for GL_CLAMP_TO_BORDER mode */
2843 get_border_color(samp
, img
, rgba
);
2846 swImg
->FetchTexel(swImg
, i
, j
, array
, rgba
);
2852 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2855 sample_2d_array_linear(struct gl_context
*ctx
,
2856 const struct gl_sampler_object
*samp
,
2857 const struct gl_texture_image
*img
,
2858 const GLfloat texcoord
[4],
2861 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2862 const GLint width
= img
->Width2
;
2863 const GLint height
= img
->Height2
;
2864 const GLint depth
= img
->Depth
;
2865 GLint i0
, j0
, i1
, j1
;
2867 GLbitfield useBorderColor
= 0x0;
2869 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2871 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2872 linear_texel_locations(samp
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2873 array
= tex_array_slice(texcoord
[2], depth
);
2875 if (array
< 0 || array
>= depth
) {
2876 COPY_4V(rgba
, samp
->BorderColor
.f
);
2886 /* check if sampling texture border color */
2887 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2888 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2889 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2890 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2894 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2895 get_border_color(samp
, img
, t00
);
2898 swImg
->FetchTexel(swImg
, i0
, j0
, array
, t00
);
2900 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2901 get_border_color(samp
, img
, t10
);
2904 swImg
->FetchTexel(swImg
, i1
, j0
, array
, t10
);
2906 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2907 get_border_color(samp
, img
, t01
);
2910 swImg
->FetchTexel(swImg
, i0
, j1
, array
, t01
);
2912 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2913 get_border_color(samp
, img
, t11
);
2916 swImg
->FetchTexel(swImg
, i1
, j1
, array
, t11
);
2919 /* trilinear interpolation of samples */
2920 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2926 sample_2d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2927 const struct gl_sampler_object
*samp
,
2928 const struct gl_texture_object
*tObj
,
2929 GLuint n
, const GLfloat texcoord
[][4],
2930 const GLfloat lambda
[], GLfloat rgba
[][4])
2933 for (i
= 0; i
< n
; i
++) {
2934 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2935 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
],
2942 sample_2d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2943 const struct gl_sampler_object
*samp
,
2944 const struct gl_texture_object
*tObj
,
2945 GLuint n
, const GLfloat texcoord
[][4],
2946 const GLfloat lambda
[], GLfloat rgba
[][4])
2949 ASSERT(lambda
!= NULL
);
2950 for (i
= 0; i
< n
; i
++) {
2951 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2952 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][level
],
2953 texcoord
[i
], rgba
[i
]);
2959 sample_2d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2960 const struct gl_sampler_object
*samp
,
2961 const struct gl_texture_object
*tObj
,
2962 GLuint n
, const GLfloat texcoord
[][4],
2963 const GLfloat lambda
[], GLfloat rgba
[][4])
2966 ASSERT(lambda
!= NULL
);
2967 for (i
= 0; i
< n
; i
++) {
2968 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2969 if (level
>= tObj
->_MaxLevel
) {
2970 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2971 texcoord
[i
], rgba
[i
]);
2974 GLfloat t0
[4], t1
[4]; /* texels */
2975 const GLfloat f
= FRAC(lambda
[i
]);
2976 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
],
2978 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
+1],
2980 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2987 sample_2d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2988 const struct gl_sampler_object
*samp
,
2989 const struct gl_texture_object
*tObj
,
2990 GLuint n
, const GLfloat texcoord
[][4],
2991 const GLfloat lambda
[], GLfloat rgba
[][4])
2994 ASSERT(lambda
!= NULL
);
2995 for (i
= 0; i
< n
; i
++) {
2996 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2997 if (level
>= tObj
->_MaxLevel
) {
2998 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2999 texcoord
[i
], rgba
[i
]);
3002 GLfloat t0
[4], t1
[4]; /* texels */
3003 const GLfloat f
= FRAC(lambda
[i
]);
3004 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][level
],
3006 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][level
+1],
3008 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3014 /** Sample 2D Array texture, nearest filtering for both min/magnification */
3016 sample_nearest_2d_array(struct gl_context
*ctx
,
3017 const struct gl_sampler_object
*samp
,
3018 const struct gl_texture_object
*tObj
, GLuint n
,
3019 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3023 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3025 for (i
= 0; i
< n
; i
++) {
3026 sample_2d_array_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3032 /** Sample 2D Array texture, linear filtering for both min/magnification */
3034 sample_linear_2d_array(struct gl_context
*ctx
,
3035 const struct gl_sampler_object
*samp
,
3036 const struct gl_texture_object
*tObj
, GLuint n
,
3037 const GLfloat texcoords
[][4],
3038 const GLfloat lambda
[], GLfloat rgba
[][4])
3041 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3043 for (i
= 0; i
< n
; i
++) {
3044 sample_2d_array_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3049 /** Sample 2D Array texture, using lambda to choose between min/magnification */
3051 sample_lambda_2d_array(struct gl_context
*ctx
,
3052 const struct gl_sampler_object
*samp
,
3053 const struct gl_texture_object
*tObj
, GLuint n
,
3054 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3057 GLuint minStart
, minEnd
; /* texels with minification */
3058 GLuint magStart
, magEnd
; /* texels with magnification */
3061 ASSERT(lambda
!= NULL
);
3062 compute_min_mag_ranges(samp
, n
, lambda
,
3063 &minStart
, &minEnd
, &magStart
, &magEnd
);
3065 if (minStart
< minEnd
) {
3066 /* do the minified texels */
3067 GLuint m
= minEnd
- minStart
;
3068 switch (samp
->MinFilter
) {
3070 for (i
= minStart
; i
< minEnd
; i
++)
3071 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3072 texcoords
[i
], rgba
[i
]);
3075 for (i
= minStart
; i
< minEnd
; i
++)
3076 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3077 texcoords
[i
], rgba
[i
]);
3079 case GL_NEAREST_MIPMAP_NEAREST
:
3080 sample_2d_array_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
,
3081 texcoords
+ minStart
,
3085 case GL_LINEAR_MIPMAP_NEAREST
:
3086 sample_2d_array_linear_mipmap_nearest(ctx
, samp
, tObj
, m
,
3087 texcoords
+ minStart
,
3091 case GL_NEAREST_MIPMAP_LINEAR
:
3092 sample_2d_array_nearest_mipmap_linear(ctx
, samp
, tObj
, m
,
3093 texcoords
+ minStart
,
3097 case GL_LINEAR_MIPMAP_LINEAR
:
3098 sample_2d_array_linear_mipmap_linear(ctx
, samp
, tObj
, m
,
3099 texcoords
+ minStart
,
3104 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
3109 if (magStart
< magEnd
) {
3110 /* do the magnified texels */
3111 switch (samp
->MagFilter
) {
3113 for (i
= magStart
; i
< magEnd
; i
++)
3114 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3115 texcoords
[i
], rgba
[i
]);
3118 for (i
= magStart
; i
< magEnd
; i
++)
3119 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3120 texcoords
[i
], rgba
[i
]);
3123 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
3132 /**********************************************************************/
3133 /* 1D Texture Array Sampling Functions */
3134 /**********************************************************************/
3137 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
3140 sample_1d_array_nearest(struct gl_context
*ctx
,
3141 const struct gl_sampler_object
*samp
,
3142 const struct gl_texture_image
*img
,
3143 const GLfloat texcoord
[4],
3146 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3147 const GLint width
= img
->Width2
; /* without border, power of two */
3148 const GLint height
= img
->Height
;
3153 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
3154 array
= tex_array_slice(texcoord
[1], height
);
3156 if (i
< 0 || i
>= (GLint
) img
->Width
||
3157 array
< 0 || array
>= (GLint
) img
->Height
) {
3158 /* Need this test for GL_CLAMP_TO_BORDER mode */
3159 get_border_color(samp
, img
, rgba
);
3162 swImg
->FetchTexel(swImg
, i
, array
, 0, rgba
);
3168 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
3171 sample_1d_array_linear(struct gl_context
*ctx
,
3172 const struct gl_sampler_object
*samp
,
3173 const struct gl_texture_image
*img
,
3174 const GLfloat texcoord
[4],
3177 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3178 const GLint width
= img
->Width2
;
3179 const GLint height
= img
->Height
;
3182 GLbitfield useBorderColor
= 0x0;
3184 GLfloat t0
[4], t1
[4];
3186 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
3187 array
= tex_array_slice(texcoord
[1], height
);
3194 /* check if sampling texture border color */
3195 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
3196 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
3199 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
3202 if (useBorderColor
& (I0BIT
| K0BIT
)) {
3203 get_border_color(samp
, img
, t0
);
3206 swImg
->FetchTexel(swImg
, i0
, array
, 0, t0
);
3208 if (useBorderColor
& (I1BIT
| K0BIT
)) {
3209 get_border_color(samp
, img
, t1
);
3212 swImg
->FetchTexel(swImg
, i1
, array
, 0, t1
);
3215 /* bilinear interpolation of samples */
3216 lerp_rgba(rgba
, a
, t0
, t1
);
3221 sample_1d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
3222 const struct gl_sampler_object
*samp
,
3223 const struct gl_texture_object
*tObj
,
3224 GLuint n
, const GLfloat texcoord
[][4],
3225 const GLfloat lambda
[], GLfloat rgba
[][4])
3228 for (i
= 0; i
< n
; i
++) {
3229 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3230 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
],
3237 sample_1d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
3238 const struct gl_sampler_object
*samp
,
3239 const struct gl_texture_object
*tObj
,
3240 GLuint n
, const GLfloat texcoord
[][4],
3241 const GLfloat lambda
[], GLfloat rgba
[][4])
3244 ASSERT(lambda
!= NULL
);
3245 for (i
= 0; i
< n
; i
++) {
3246 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3247 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][level
],
3248 texcoord
[i
], rgba
[i
]);
3254 sample_1d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
3255 const struct gl_sampler_object
*samp
,
3256 const struct gl_texture_object
*tObj
,
3257 GLuint n
, const GLfloat texcoord
[][4],
3258 const GLfloat lambda
[], GLfloat rgba
[][4])
3261 ASSERT(lambda
!= NULL
);
3262 for (i
= 0; i
< n
; i
++) {
3263 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3264 if (level
>= tObj
->_MaxLevel
) {
3265 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
3266 texcoord
[i
], rgba
[i
]);
3269 GLfloat t0
[4], t1
[4]; /* texels */
3270 const GLfloat f
= FRAC(lambda
[i
]);
3271 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3272 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3273 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3280 sample_1d_array_linear_mipmap_linear(struct gl_context
*ctx
,
3281 const struct gl_sampler_object
*samp
,
3282 const struct gl_texture_object
*tObj
,
3283 GLuint n
, const GLfloat texcoord
[][4],
3284 const GLfloat lambda
[], GLfloat rgba
[][4])
3287 ASSERT(lambda
!= NULL
);
3288 for (i
= 0; i
< n
; i
++) {
3289 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3290 if (level
>= tObj
->_MaxLevel
) {
3291 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
3292 texcoord
[i
], rgba
[i
]);
3295 GLfloat t0
[4], t1
[4]; /* texels */
3296 const GLfloat f
= FRAC(lambda
[i
]);
3297 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3298 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3299 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3305 /** Sample 1D Array texture, nearest filtering for both min/magnification */
3307 sample_nearest_1d_array(struct gl_context
*ctx
,
3308 const struct gl_sampler_object
*samp
,
3309 const struct gl_texture_object
*tObj
, GLuint n
,
3310 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3314 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3316 for (i
= 0; i
< n
; i
++) {
3317 sample_1d_array_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3322 /** Sample 1D Array texture, linear filtering for both min/magnification */
3324 sample_linear_1d_array(struct gl_context
*ctx
,
3325 const struct gl_sampler_object
*samp
,
3326 const struct gl_texture_object
*tObj
, GLuint n
,
3327 const GLfloat texcoords
[][4],
3328 const GLfloat lambda
[], GLfloat rgba
[][4])
3331 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3333 for (i
= 0; i
< n
; i
++) {
3334 sample_1d_array_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3339 /** Sample 1D Array texture, using lambda to choose between min/magnification */
3341 sample_lambda_1d_array(struct gl_context
*ctx
,
3342 const struct gl_sampler_object
*samp
,
3343 const struct gl_texture_object
*tObj
, GLuint n
,
3344 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3347 GLuint minStart
, minEnd
; /* texels with minification */
3348 GLuint magStart
, magEnd
; /* texels with magnification */
3351 ASSERT(lambda
!= NULL
);
3352 compute_min_mag_ranges(samp
, n
, lambda
,
3353 &minStart
, &minEnd
, &magStart
, &magEnd
);
3355 if (minStart
< minEnd
) {
3356 /* do the minified texels */
3357 GLuint m
= minEnd
- minStart
;
3358 switch (samp
->MinFilter
) {
3360 for (i
= minStart
; i
< minEnd
; i
++)
3361 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3362 texcoords
[i
], rgba
[i
]);
3365 for (i
= minStart
; i
< minEnd
; i
++)
3366 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3367 texcoords
[i
], rgba
[i
]);
3369 case GL_NEAREST_MIPMAP_NEAREST
:
3370 sample_1d_array_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
3371 lambda
+ minStart
, rgba
+ minStart
);
3373 case GL_LINEAR_MIPMAP_NEAREST
:
3374 sample_1d_array_linear_mipmap_nearest(ctx
, samp
, tObj
, m
,
3375 texcoords
+ minStart
,
3379 case GL_NEAREST_MIPMAP_LINEAR
:
3380 sample_1d_array_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
3381 lambda
+ minStart
, rgba
+ minStart
);
3383 case GL_LINEAR_MIPMAP_LINEAR
:
3384 sample_1d_array_linear_mipmap_linear(ctx
, samp
, tObj
, m
,
3385 texcoords
+ minStart
,
3390 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
3395 if (magStart
< magEnd
) {
3396 /* do the magnified texels */
3397 switch (samp
->MagFilter
) {
3399 for (i
= magStart
; i
< magEnd
; i
++)
3400 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3401 texcoords
[i
], rgba
[i
]);
3404 for (i
= magStart
; i
< magEnd
; i
++)
3405 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3406 texcoords
[i
], rgba
[i
]);
3409 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
3417 * Compare texcoord against depth sample. Return 1.0 or 0.0 value.
3419 static inline GLfloat
3420 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
)
3424 return (coord
<= depthSample
) ? 1.0F
: 0.0F
;
3426 return (coord
>= depthSample
) ? 1.0F
: 0.0F
;
3428 return (coord
< depthSample
) ? 1.0F
: 0.0F
;
3430 return (coord
> depthSample
) ? 1.0F
: 0.0F
;
3432 return (coord
== depthSample
) ? 1.0F
: 0.0F
;
3434 return (coord
!= depthSample
) ? 1.0F
: 0.0F
;
3442 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
3449 * Compare texcoord against four depth samples.
3451 static inline GLfloat
3452 shadow_compare4(GLenum function
, GLfloat coord
,
3453 GLfloat depth00
, GLfloat depth01
,
3454 GLfloat depth10
, GLfloat depth11
,
3455 GLfloat wi
, GLfloat wj
)
3457 const GLfloat d
= 0.25F
;
3458 GLfloat luminance
= 1.0F
;
3462 if (coord
> depth00
) luminance
-= d
;
3463 if (coord
> depth01
) luminance
-= d
;
3464 if (coord
> depth10
) luminance
-= d
;
3465 if (coord
> depth11
) luminance
-= d
;
3468 if (coord
< depth00
) luminance
-= d
;
3469 if (coord
< depth01
) luminance
-= d
;
3470 if (coord
< depth10
) luminance
-= d
;
3471 if (coord
< depth11
) luminance
-= d
;
3474 if (coord
>= depth00
) luminance
-= d
;
3475 if (coord
>= depth01
) luminance
-= d
;
3476 if (coord
>= depth10
) luminance
-= d
;
3477 if (coord
>= depth11
) luminance
-= d
;
3480 if (coord
<= depth00
) luminance
-= d
;
3481 if (coord
<= depth01
) luminance
-= d
;
3482 if (coord
<= depth10
) luminance
-= d
;
3483 if (coord
<= depth11
) luminance
-= d
;
3486 if (coord
!= depth00
) luminance
-= d
;
3487 if (coord
!= depth01
) luminance
-= d
;
3488 if (coord
!= depth10
) luminance
-= d
;
3489 if (coord
!= depth11
) luminance
-= d
;
3492 if (coord
== depth00
) luminance
-= d
;
3493 if (coord
== depth01
) luminance
-= d
;
3494 if (coord
== depth10
) luminance
-= d
;
3495 if (coord
== depth11
) luminance
-= d
;
3502 /* ordinary bilinear filtering */
3503 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
3505 _mesa_problem(NULL
, "Bad compare func in sample_compare4");
3512 * Choose the mipmap level to use when sampling from a depth texture.
3515 choose_depth_texture_level(const struct gl_sampler_object
*samp
,
3516 const struct gl_texture_object
*tObj
, GLfloat lambda
)
3520 if (samp
->MinFilter
== GL_NEAREST
|| samp
->MinFilter
== GL_LINEAR
) {
3521 /* no mipmapping - use base level */
3522 level
= tObj
->BaseLevel
;
3525 /* choose mipmap level */
3526 lambda
= CLAMP(lambda
, samp
->MinLod
, samp
->MaxLod
);
3527 level
= (GLint
) lambda
;
3528 level
= CLAMP(level
, tObj
->BaseLevel
, tObj
->_MaxLevel
);
3536 * Sample a shadow/depth texture. This function is incomplete. It doesn't
3537 * check for minification vs. magnification, etc.
3540 sample_depth_texture( struct gl_context
*ctx
,
3541 const struct gl_sampler_object
*samp
,
3542 const struct gl_texture_object
*tObj
, GLuint n
,
3543 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3544 GLfloat texel
[][4] )
3546 const GLint level
= choose_depth_texture_level(samp
, tObj
, lambda
[0]);
3547 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
3548 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3549 const GLint width
= img
->Width
;
3550 const GLint height
= img
->Height
;
3551 const GLint depth
= img
->Depth
;
3552 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
3557 ASSERT(img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
3558 img
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
3560 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
3561 tObj
->Target
== GL_TEXTURE_2D
||
3562 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
3563 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
3564 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
||
3565 tObj
->Target
== GL_TEXTURE_CUBE_MAP
);
3567 /* XXXX if samp->MinFilter != samp->MagFilter, we're ignoring lambda */
3569 function
= (samp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
3570 samp
->CompareFunc
: GL_NONE
;
3572 if (samp
->MagFilter
== GL_NEAREST
) {
3574 for (i
= 0; i
< n
; i
++) {
3575 GLfloat depthSample
, depthRef
;
3576 GLint col
, row
, slice
;
3578 nearest_texcoord(samp
, tObj
, level
, texcoords
[i
], &col
, &row
, &slice
);
3580 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3581 slice
>= 0 && slice
< depth
) {
3582 swImg
->FetchTexel(swImg
, col
, row
, slice
, &depthSample
);
3585 depthSample
= samp
->BorderColor
.f
[0];
3588 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3590 result
= shadow_compare(function
, depthRef
, depthSample
);
3592 apply_depth_mode(tObj
->DepthMode
, result
, texel
[i
]);
3597 ASSERT(samp
->MagFilter
== GL_LINEAR
);
3598 for (i
= 0; i
< n
; i
++) {
3599 GLfloat depth00
, depth01
, depth10
, depth11
, depthRef
;
3600 GLint i0
, i1
, j0
, j1
;
3603 GLuint useBorderTexel
;
3605 linear_texcoord(samp
, tObj
, level
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3612 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3618 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3619 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3620 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3621 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3624 if (slice
< 0 || slice
>= (GLint
) depth
) {
3625 depth00
= samp
->BorderColor
.f
[0];
3626 depth01
= samp
->BorderColor
.f
[0];
3627 depth10
= samp
->BorderColor
.f
[0];
3628 depth11
= samp
->BorderColor
.f
[0];
3631 /* get four depth samples from the texture */
3632 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3633 depth00
= samp
->BorderColor
.f
[0];
3636 swImg
->FetchTexel(swImg
, i0
, j0
, slice
, &depth00
);
3638 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3639 depth10
= samp
->BorderColor
.f
[0];
3642 swImg
->FetchTexel(swImg
, i1
, j0
, slice
, &depth10
);
3645 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3646 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3647 depth01
= samp
->BorderColor
.f
[0];
3650 swImg
->FetchTexel(swImg
, i0
, j1
, slice
, &depth01
);
3652 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3653 depth11
= samp
->BorderColor
.f
[0];
3656 swImg
->FetchTexel(swImg
, i1
, j1
, slice
, &depth11
);
3665 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3667 result
= shadow_compare4(function
, depthRef
,
3668 depth00
, depth01
, depth10
, depth11
,
3671 apply_depth_mode(tObj
->DepthMode
, result
, texel
[i
]);
3678 * We use this function when a texture object is in an "incomplete" state.
3679 * When a fragment program attempts to sample an incomplete texture we
3680 * return black (see issue 23 in GL_ARB_fragment_program spec).
3681 * Note: fragment programs don't observe the texture enable/disable flags.
3684 null_sample_func( struct gl_context
*ctx
,
3685 const struct gl_sampler_object
*samp
,
3686 const struct gl_texture_object
*tObj
, GLuint n
,
3687 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3696 for (i
= 0; i
< n
; i
++) {
3700 rgba
[i
][ACOMP
] = 1.0;
3706 * Choose the texture sampling function for the given texture object.
3709 _swrast_choose_texture_sample_func( struct gl_context
*ctx
,
3710 const struct gl_texture_object
*t
,
3711 const struct gl_sampler_object
*sampler
)
3713 if (!t
|| !_mesa_is_texture_complete(t
, sampler
)) {
3714 return &null_sample_func
;
3717 const GLboolean needLambda
=
3718 (GLboolean
) (sampler
->MinFilter
!= sampler
->MagFilter
);
3720 switch (t
->Target
) {
3722 if (is_depth_texture(t
)) {
3723 return &sample_depth_texture
;
3725 else if (needLambda
) {
3726 return &sample_lambda_1d
;
3728 else if (sampler
->MinFilter
== GL_LINEAR
) {
3729 return &sample_linear_1d
;
3732 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3733 return &sample_nearest_1d
;
3736 if (is_depth_texture(t
)) {
3737 return &sample_depth_texture
;
3739 else if (needLambda
) {
3740 /* Anisotropic filtering extension. Activated only if mipmaps are used */
3741 if (sampler
->MaxAnisotropy
> 1.0 &&
3742 sampler
->MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
3743 return &sample_lambda_2d_aniso
;
3745 return &sample_lambda_2d
;
3747 else if (sampler
->MinFilter
== GL_LINEAR
) {
3748 return &sample_linear_2d
;
3751 /* check for a few optimized cases */
3752 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3753 const struct swrast_texture_image
*swImg
=
3754 swrast_texture_image_const(img
);
3755 texture_sample_func func
;
3757 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3758 func
= &sample_nearest_2d
;
3759 if (sampler
->WrapS
== GL_REPEAT
&&
3760 sampler
->WrapT
== GL_REPEAT
&&
3761 swImg
->_IsPowerOfTwo
&&
3763 if (img
->TexFormat
== MESA_FORMAT_BGR_UNORM8
)
3764 func
= &opt_sample_rgb_2d
;
3765 else if (img
->TexFormat
== MESA_FORMAT_A8B8G8R8_UNORM
)
3766 func
= &opt_sample_rgba_2d
;
3773 return &sample_lambda_3d
;
3775 else if (sampler
->MinFilter
== GL_LINEAR
) {
3776 return &sample_linear_3d
;
3779 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3780 return &sample_nearest_3d
;
3782 case GL_TEXTURE_CUBE_MAP
:
3784 return &sample_lambda_cube
;
3786 else if (sampler
->MinFilter
== GL_LINEAR
) {
3787 return &sample_linear_cube
;
3790 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3791 return &sample_nearest_cube
;
3793 case GL_TEXTURE_RECTANGLE_NV
:
3794 if (is_depth_texture(t
)) {
3795 return &sample_depth_texture
;
3797 else if (needLambda
) {
3798 return &sample_lambda_rect
;
3800 else if (sampler
->MinFilter
== GL_LINEAR
) {
3801 return &sample_linear_rect
;
3804 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3805 return &sample_nearest_rect
;
3807 case GL_TEXTURE_1D_ARRAY_EXT
:
3808 if (is_depth_texture(t
)) {
3809 return &sample_depth_texture
;
3811 else if (needLambda
) {
3812 return &sample_lambda_1d_array
;
3814 else if (sampler
->MinFilter
== GL_LINEAR
) {
3815 return &sample_linear_1d_array
;
3818 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3819 return &sample_nearest_1d_array
;
3821 case GL_TEXTURE_2D_ARRAY_EXT
:
3822 if (is_depth_texture(t
)) {
3823 return &sample_depth_texture
;
3825 else if (needLambda
) {
3826 return &sample_lambda_2d_array
;
3828 else if (sampler
->MinFilter
== GL_LINEAR
) {
3829 return &sample_linear_2d_array
;
3832 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3833 return &sample_nearest_2d_array
;
3837 "invalid target in _swrast_choose_texture_sample_func");
3838 return &null_sample_func
;