2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/colormac.h"
29 #include "main/imports.h"
31 #include "s_context.h"
32 #include "s_texfilter.h"
36 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
37 * see 1-pixel bands of improperly weighted linear-filtered textures.
38 * The tests/texwrap.c demo is a good test.
39 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
40 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
42 #define FRAC(f) ((f) - IFLOOR(f))
47 * Linear interpolation macro
49 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
53 * Do 2D/biliner interpolation of float values.
54 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
55 * a and b are the horizontal and vertical interpolants.
56 * It's important that this function is inlined when compiled with
57 * optimization! If we find that's not true on some systems, convert
61 lerp_2d(GLfloat a
, GLfloat b
,
62 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
64 const GLfloat temp0
= LERP(a
, v00
, v10
);
65 const GLfloat temp1
= LERP(a
, v01
, v11
);
66 return LERP(b
, temp0
, temp1
);
71 * Do 3D/trilinear interpolation of float values.
75 lerp_3d(GLfloat a
, GLfloat b
, GLfloat c
,
76 GLfloat v000
, GLfloat v100
, GLfloat v010
, GLfloat v110
,
77 GLfloat v001
, GLfloat v101
, GLfloat v011
, GLfloat v111
)
79 const GLfloat temp00
= LERP(a
, v000
, v100
);
80 const GLfloat temp10
= LERP(a
, v010
, v110
);
81 const GLfloat temp01
= LERP(a
, v001
, v101
);
82 const GLfloat temp11
= LERP(a
, v011
, v111
);
83 const GLfloat temp0
= LERP(b
, temp00
, temp10
);
84 const GLfloat temp1
= LERP(b
, temp01
, temp11
);
85 return LERP(c
, temp0
, temp1
);
90 * Do linear interpolation of colors.
93 lerp_rgba(GLfloat result
[4], GLfloat t
, const GLfloat a
[4], const GLfloat b
[4])
95 result
[0] = LERP(t
, a
[0], b
[0]);
96 result
[1] = LERP(t
, a
[1], b
[1]);
97 result
[2] = LERP(t
, a
[2], b
[2]);
98 result
[3] = LERP(t
, a
[3], b
[3]);
103 * Do bilinear interpolation of colors.
106 lerp_rgba_2d(GLfloat result
[4], GLfloat a
, GLfloat b
,
107 const GLfloat t00
[4], const GLfloat t10
[4],
108 const GLfloat t01
[4], const GLfloat t11
[4])
110 result
[0] = lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]);
111 result
[1] = lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]);
112 result
[2] = lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]);
113 result
[3] = lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]);
118 * Do trilinear interpolation of colors.
121 lerp_rgba_3d(GLfloat result
[4], GLfloat a
, GLfloat b
, GLfloat c
,
122 const GLfloat t000
[4], const GLfloat t100
[4],
123 const GLfloat t010
[4], const GLfloat t110
[4],
124 const GLfloat t001
[4], const GLfloat t101
[4],
125 const GLfloat t011
[4], const GLfloat t111
[4])
128 /* compiler should unroll these short loops */
129 for (k
= 0; k
< 4; k
++) {
130 result
[k
] = lerp_3d(a
, b
, c
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
131 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
137 * Used for GL_REPEAT wrap mode. Using A % B doesn't produce the
138 * right results for A<0. Casting to A to be unsigned only works if B
139 * is a power of two. Adding a bias to A (which is a multiple of B)
140 * avoids the problems with A < 0 (for reasonable A) without using a
143 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
147 * Used to compute texel locations for linear sampling.
149 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
150 * s = texcoord in [0,1]
151 * size = width (or height or depth) of texture
153 * i0, i1 = returns two nearest texel indexes
154 * weight = returns blend factor between texels
157 linear_texel_locations(GLenum wrapMode
,
158 const struct gl_texture_image
*img
,
159 GLint size
, GLfloat s
,
160 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
166 if (img
->_IsPowerOfTwo
) {
167 *i0
= IFLOOR(u
) & (size
- 1);
168 *i1
= (*i0
+ 1) & (size
- 1);
171 *i0
= REMAINDER(IFLOOR(u
), size
);
172 *i1
= REMAINDER(*i0
+ 1, size
);
175 case GL_CLAMP_TO_EDGE
:
187 if (*i1
>= (GLint
) size
)
190 case GL_CLAMP_TO_BORDER
:
192 const GLfloat min
= -1.0F
/ (2.0F
* size
);
193 const GLfloat max
= 1.0F
- min
;
205 case GL_MIRRORED_REPEAT
:
207 const GLint flr
= IFLOOR(s
);
209 u
= 1.0F
- (s
- (GLfloat
) flr
);
211 u
= s
- (GLfloat
) flr
;
212 u
= (u
* size
) - 0.5F
;
217 if (*i1
>= (GLint
) size
)
221 case GL_MIRROR_CLAMP_EXT
:
231 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
242 if (*i1
>= (GLint
) size
)
245 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
247 const GLfloat min
= -1.0F
/ (2.0F
* size
);
248 const GLfloat max
= 1.0F
- min
;
273 _mesa_problem(NULL
, "Bad wrap mode");
281 * Used to compute texel location for nearest sampling.
284 nearest_texel_location(GLenum wrapMode
,
285 const struct gl_texture_image
*img
,
286 GLint size
, GLfloat s
)
292 /* s limited to [0,1) */
293 /* i limited to [0,size-1] */
294 i
= IFLOOR(s
* size
);
295 if (img
->_IsPowerOfTwo
)
298 i
= REMAINDER(i
, size
);
300 case GL_CLAMP_TO_EDGE
:
302 /* s limited to [min,max] */
303 /* i limited to [0, size-1] */
304 const GLfloat min
= 1.0F
/ (2.0F
* size
);
305 const GLfloat max
= 1.0F
- min
;
311 i
= IFLOOR(s
* size
);
314 case GL_CLAMP_TO_BORDER
:
316 /* s limited to [min,max] */
317 /* i limited to [-1, size] */
318 const GLfloat min
= -1.0F
/ (2.0F
* size
);
319 const GLfloat max
= 1.0F
- min
;
325 i
= IFLOOR(s
* size
);
328 case GL_MIRRORED_REPEAT
:
330 const GLfloat min
= 1.0F
/ (2.0F
* size
);
331 const GLfloat max
= 1.0F
- min
;
332 const GLint flr
= IFLOOR(s
);
335 u
= 1.0F
- (s
- (GLfloat
) flr
);
337 u
= s
- (GLfloat
) flr
;
343 i
= IFLOOR(u
* size
);
346 case GL_MIRROR_CLAMP_EXT
:
348 /* s limited to [0,1] */
349 /* i limited to [0,size-1] */
350 const GLfloat u
= FABSF(s
);
356 i
= IFLOOR(u
* size
);
359 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
361 /* s limited to [min,max] */
362 /* i limited to [0, size-1] */
363 const GLfloat min
= 1.0F
/ (2.0F
* size
);
364 const GLfloat max
= 1.0F
- min
;
365 const GLfloat u
= FABSF(s
);
371 i
= IFLOOR(u
* size
);
374 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
376 /* s limited to [min,max] */
377 /* i limited to [0, size-1] */
378 const GLfloat min
= -1.0F
/ (2.0F
* size
);
379 const GLfloat max
= 1.0F
- min
;
380 const GLfloat u
= FABSF(s
);
386 i
= IFLOOR(u
* size
);
390 /* s limited to [0,1] */
391 /* i limited to [0,size-1] */
397 i
= IFLOOR(s
* size
);
400 _mesa_problem(NULL
, "Bad wrap mode");
406 /* Power of two image sizes only */
408 linear_repeat_texel_location(GLuint size
, GLfloat s
,
409 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
411 GLfloat u
= s
* size
- 0.5F
;
412 *i0
= IFLOOR(u
) & (size
- 1);
413 *i1
= (*i0
+ 1) & (size
- 1);
419 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
422 clamp_rect_coord_nearest(GLenum wrapMode
, GLfloat coord
, GLint max
)
426 return IFLOOR( CLAMP(coord
, 0.0F
, max
- 1) );
427 case GL_CLAMP_TO_EDGE
:
428 return IFLOOR( CLAMP(coord
, 0.5F
, max
- 0.5F
) );
429 case GL_CLAMP_TO_BORDER
:
430 return IFLOOR( CLAMP(coord
, -0.5F
, max
+ 0.5F
) );
432 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_nearest");
439 * As above, but GL_LINEAR filtering.
442 clamp_rect_coord_linear(GLenum wrapMode
, GLfloat coord
, GLint max
,
443 GLint
*i0out
, GLint
*i1out
, GLfloat
*weight
)
449 /* Not exactly what the spec says, but it matches NVIDIA output */
450 fcol
= CLAMP(coord
- 0.5F
, 0.0F
, max
- 1);
454 case GL_CLAMP_TO_EDGE
:
455 fcol
= CLAMP(coord
, 0.5F
, max
- 0.5F
);
462 case GL_CLAMP_TO_BORDER
:
463 fcol
= CLAMP(coord
, -0.5F
, max
+ 0.5F
);
469 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_linear");
475 *weight
= FRAC(fcol
);
480 * Compute slice/image to use for 1D or 2D array texture.
483 tex_array_slice(GLfloat coord
, GLsizei size
)
485 GLint slice
= IFLOOR(coord
+ 0.5f
);
486 slice
= CLAMP(slice
, 0, size
- 1);
492 * Compute nearest integer texcoords for given texobj and coordinate.
493 * NOTE: only used for depth texture sampling.
496 nearest_texcoord(const struct gl_texture_object
*texObj
,
498 const GLfloat texcoord
[4],
499 GLint
*i
, GLint
*j
, GLint
*k
)
501 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
502 const GLint width
= img
->Width
;
503 const GLint height
= img
->Height
;
504 const GLint depth
= img
->Depth
;
506 switch (texObj
->Target
) {
507 case GL_TEXTURE_RECTANGLE_ARB
:
508 *i
= clamp_rect_coord_nearest(texObj
->Sampler
.WrapS
, texcoord
[0], width
);
509 *j
= clamp_rect_coord_nearest(texObj
->Sampler
.WrapT
, texcoord
[1], height
);
513 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
518 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
519 *j
= nearest_texel_location(texObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
522 case GL_TEXTURE_1D_ARRAY_EXT
:
523 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
524 *j
= tex_array_slice(texcoord
[1], height
);
527 case GL_TEXTURE_2D_ARRAY_EXT
:
528 *i
= nearest_texel_location(texObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
529 *j
= nearest_texel_location(texObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
530 *k
= tex_array_slice(texcoord
[2], depth
);
539 * Compute linear integer texcoords for given texobj and coordinate.
540 * NOTE: only used for depth texture sampling.
543 linear_texcoord(const struct gl_texture_object
*texObj
,
545 const GLfloat texcoord
[4],
546 GLint
*i0
, GLint
*i1
, GLint
*j0
, GLint
*j1
, GLint
*slice
,
547 GLfloat
*wi
, GLfloat
*wj
)
549 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
550 const GLint width
= img
->Width
;
551 const GLint height
= img
->Height
;
552 const GLint depth
= img
->Depth
;
554 switch (texObj
->Target
) {
555 case GL_TEXTURE_RECTANGLE_ARB
:
556 clamp_rect_coord_linear(texObj
->Sampler
.WrapS
, texcoord
[0],
558 clamp_rect_coord_linear(texObj
->Sampler
.WrapT
, texcoord
[1],
565 linear_texel_locations(texObj
->Sampler
.WrapS
, img
, width
,
566 texcoord
[0], i0
, i1
, wi
);
567 linear_texel_locations(texObj
->Sampler
.WrapT
, img
, height
,
568 texcoord
[1], j0
, j1
, wj
);
572 case GL_TEXTURE_1D_ARRAY_EXT
:
573 linear_texel_locations(texObj
->Sampler
.WrapS
, img
, width
,
574 texcoord
[0], i0
, i1
, wi
);
575 *j0
= tex_array_slice(texcoord
[1], height
);
580 case GL_TEXTURE_2D_ARRAY_EXT
:
581 linear_texel_locations(texObj
->Sampler
.WrapS
, img
, width
,
582 texcoord
[0], i0
, i1
, wi
);
583 linear_texel_locations(texObj
->Sampler
.WrapT
, img
, height
,
584 texcoord
[1], j0
, j1
, wj
);
585 *slice
= tex_array_slice(texcoord
[2], depth
);
596 * For linear interpolation between mipmap levels N and N+1, this function
600 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
603 return tObj
->BaseLevel
;
604 else if (lambda
> tObj
->_MaxLambda
)
605 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
607 return (GLint
) (tObj
->BaseLevel
+ lambda
);
612 * Compute the nearest mipmap level to take texels from.
615 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
621 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
622 l
= tObj
->_MaxLambda
+ 0.4999F
;
625 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
626 if (level
> tObj
->_MaxLevel
)
627 level
= tObj
->_MaxLevel
;
634 * Bitflags for texture border color sampling.
646 * The lambda[] array values are always monotonic. Either the whole span
647 * will be minified, magnified, or split between the two. This function
648 * determines the subranges in [0, n-1] that are to be minified or magnified.
651 compute_min_mag_ranges(const struct gl_texture_object
*tObj
,
652 GLuint n
, const GLfloat lambda
[],
653 GLuint
*minStart
, GLuint
*minEnd
,
654 GLuint
*magStart
, GLuint
*magEnd
)
656 GLfloat minMagThresh
;
658 /* we shouldn't be here if minfilter == magfilter */
659 ASSERT(tObj
->Sampler
.MinFilter
!= tObj
->Sampler
.MagFilter
);
661 /* This bit comes from the OpenGL spec: */
662 if (tObj
->Sampler
.MagFilter
== GL_LINEAR
663 && (tObj
->Sampler
.MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
664 tObj
->Sampler
.MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
672 /* DEBUG CODE: Verify that lambda[] is monotonic.
673 * We can't really use this because the inaccuracy in the LOG2 function
674 * causes this test to fail, yet the resulting texturing is correct.
678 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
679 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
680 for (i
= 0; i
< n
- 1; i
++) {
681 ASSERT((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
684 else { /* increasing */
685 for (i
= 0; i
< n
- 1; i
++) {
686 ASSERT((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
692 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
693 /* magnification for whole span */
696 *minStart
= *minEnd
= 0;
698 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
699 /* minification for whole span */
702 *magStart
= *magEnd
= 0;
705 /* a mix of minification and magnification */
707 if (lambda
[0] > minMagThresh
) {
708 /* start with minification */
709 for (i
= 1; i
< n
; i
++) {
710 if (lambda
[i
] <= minMagThresh
)
719 /* start with magnification */
720 for (i
= 1; i
< n
; i
++) {
721 if (lambda
[i
] > minMagThresh
)
732 /* Verify the min/mag Start/End values
733 * We don't use this either (see above)
737 for (i
= 0; i
< n
; i
++) {
738 if (lambda
[i
] > minMagThresh
) {
740 ASSERT(i
>= *minStart
);
745 ASSERT(i
>= *magStart
);
755 * When we sample the border color, it must be interpreted according to
756 * the base texture format. Ex: if the texture base format it GL_ALPHA,
757 * we return (0,0,0,BorderAlpha).
760 get_border_color(const struct gl_texture_object
*tObj
,
761 const struct gl_texture_image
*img
,
764 switch (img
->_BaseFormat
) {
766 rgba
[0] = tObj
->Sampler
.BorderColor
.f
[0];
767 rgba
[1] = tObj
->Sampler
.BorderColor
.f
[1];
768 rgba
[2] = tObj
->Sampler
.BorderColor
.f
[2];
772 rgba
[0] = rgba
[1] = rgba
[2] = 0.0;
773 rgba
[3] = tObj
->Sampler
.BorderColor
.f
[3];
776 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->Sampler
.BorderColor
.f
[0];
779 case GL_LUMINANCE_ALPHA
:
780 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->Sampler
.BorderColor
.f
[0];
781 rgba
[3] = tObj
->Sampler
.BorderColor
.f
[3];
784 rgba
[0] = rgba
[1] = rgba
[2] = rgba
[3] = tObj
->Sampler
.BorderColor
.f
[0];
787 COPY_4V(rgba
, tObj
->Sampler
.BorderColor
.f
);
792 /**********************************************************************/
793 /* 1-D Texture Sampling Functions */
794 /**********************************************************************/
797 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
800 sample_1d_nearest(struct gl_context
*ctx
,
801 const struct gl_texture_object
*tObj
,
802 const struct gl_texture_image
*img
,
803 const GLfloat texcoord
[4], GLfloat rgba
[4])
805 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
806 const GLint width
= img
->Width2
; /* without border, power of two */
808 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
809 /* skip over the border, if any */
811 if (i
< 0 || i
>= (GLint
) img
->Width
) {
812 /* Need this test for GL_CLAMP_TO_BORDER mode */
813 get_border_color(tObj
, img
, rgba
);
816 swImg
->FetchTexelf(swImg
, i
, 0, 0, rgba
);
822 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
825 sample_1d_linear(struct gl_context
*ctx
,
826 const struct gl_texture_object
*tObj
,
827 const struct gl_texture_image
*img
,
828 const GLfloat texcoord
[4], GLfloat rgba
[4])
830 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
831 const GLint width
= img
->Width2
;
833 GLbitfield useBorderColor
= 0x0;
835 GLfloat t0
[4], t1
[4]; /* texels */
837 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
844 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
845 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
848 /* fetch texel colors */
849 if (useBorderColor
& I0BIT
) {
850 get_border_color(tObj
, img
, t0
);
853 swImg
->FetchTexelf(swImg
, i0
, 0, 0, t0
);
855 if (useBorderColor
& I1BIT
) {
856 get_border_color(tObj
, img
, t1
);
859 swImg
->FetchTexelf(swImg
, i1
, 0, 0, t1
);
862 lerp_rgba(rgba
, a
, t0
, t1
);
867 sample_1d_nearest_mipmap_nearest(struct gl_context
*ctx
,
868 const struct gl_texture_object
*tObj
,
869 GLuint n
, const GLfloat texcoord
[][4],
870 const GLfloat lambda
[], GLfloat rgba
[][4])
873 ASSERT(lambda
!= NULL
);
874 for (i
= 0; i
< n
; i
++) {
875 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
876 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
882 sample_1d_linear_mipmap_nearest(struct gl_context
*ctx
,
883 const struct gl_texture_object
*tObj
,
884 GLuint n
, const GLfloat texcoord
[][4],
885 const GLfloat lambda
[], GLfloat rgba
[][4])
888 ASSERT(lambda
!= NULL
);
889 for (i
= 0; i
< n
; i
++) {
890 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
891 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
897 sample_1d_nearest_mipmap_linear(struct gl_context
*ctx
,
898 const struct gl_texture_object
*tObj
,
899 GLuint n
, const GLfloat texcoord
[][4],
900 const GLfloat lambda
[], GLfloat rgba
[][4])
903 ASSERT(lambda
!= NULL
);
904 for (i
= 0; i
< n
; i
++) {
905 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
906 if (level
>= tObj
->_MaxLevel
) {
907 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
908 texcoord
[i
], rgba
[i
]);
911 GLfloat t0
[4], t1
[4];
912 const GLfloat f
= FRAC(lambda
[i
]);
913 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
914 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
915 lerp_rgba(rgba
[i
], f
, t0
, t1
);
922 sample_1d_linear_mipmap_linear(struct gl_context
*ctx
,
923 const struct gl_texture_object
*tObj
,
924 GLuint n
, const GLfloat texcoord
[][4],
925 const GLfloat lambda
[], GLfloat rgba
[][4])
928 ASSERT(lambda
!= NULL
);
929 for (i
= 0; i
< n
; i
++) {
930 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
931 if (level
>= tObj
->_MaxLevel
) {
932 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
933 texcoord
[i
], rgba
[i
]);
936 GLfloat t0
[4], t1
[4];
937 const GLfloat f
= FRAC(lambda
[i
]);
938 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
939 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
940 lerp_rgba(rgba
[i
], f
, t0
, t1
);
946 /** Sample 1D texture, nearest filtering for both min/magnification */
948 sample_nearest_1d( struct gl_context
*ctx
,
949 const struct gl_texture_object
*tObj
, GLuint n
,
950 const GLfloat texcoords
[][4], const GLfloat lambda
[],
954 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
956 for (i
= 0; i
< n
; i
++) {
957 sample_1d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
962 /** Sample 1D texture, linear filtering for both min/magnification */
964 sample_linear_1d( struct gl_context
*ctx
,
965 const struct gl_texture_object
*tObj
, GLuint n
,
966 const GLfloat texcoords
[][4], const GLfloat lambda
[],
970 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
972 for (i
= 0; i
< n
; i
++) {
973 sample_1d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
978 /** Sample 1D texture, using lambda to choose between min/magnification */
980 sample_lambda_1d( struct gl_context
*ctx
,
981 const struct gl_texture_object
*tObj
, GLuint n
,
982 const GLfloat texcoords
[][4],
983 const GLfloat lambda
[], GLfloat rgba
[][4] )
985 GLuint minStart
, minEnd
; /* texels with minification */
986 GLuint magStart
, magEnd
; /* texels with magnification */
989 ASSERT(lambda
!= NULL
);
990 compute_min_mag_ranges(tObj
, n
, lambda
,
991 &minStart
, &minEnd
, &magStart
, &magEnd
);
993 if (minStart
< minEnd
) {
994 /* do the minified texels */
995 const GLuint m
= minEnd
- minStart
;
996 switch (tObj
->Sampler
.MinFilter
) {
998 for (i
= minStart
; i
< minEnd
; i
++)
999 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1000 texcoords
[i
], rgba
[i
]);
1003 for (i
= minStart
; i
< minEnd
; i
++)
1004 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1005 texcoords
[i
], rgba
[i
]);
1007 case GL_NEAREST_MIPMAP_NEAREST
:
1008 sample_1d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1009 lambda
+ minStart
, rgba
+ minStart
);
1011 case GL_LINEAR_MIPMAP_NEAREST
:
1012 sample_1d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1013 lambda
+ minStart
, rgba
+ minStart
);
1015 case GL_NEAREST_MIPMAP_LINEAR
:
1016 sample_1d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1017 lambda
+ minStart
, rgba
+ minStart
);
1019 case GL_LINEAR_MIPMAP_LINEAR
:
1020 sample_1d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1021 lambda
+ minStart
, rgba
+ minStart
);
1024 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
1029 if (magStart
< magEnd
) {
1030 /* do the magnified texels */
1031 switch (tObj
->Sampler
.MagFilter
) {
1033 for (i
= magStart
; i
< magEnd
; i
++)
1034 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1035 texcoords
[i
], rgba
[i
]);
1038 for (i
= magStart
; i
< magEnd
; i
++)
1039 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1040 texcoords
[i
], rgba
[i
]);
1043 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
1050 /**********************************************************************/
1051 /* 2-D Texture Sampling Functions */
1052 /**********************************************************************/
1056 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1059 sample_2d_nearest(struct gl_context
*ctx
,
1060 const struct gl_texture_object
*tObj
,
1061 const struct gl_texture_image
*img
,
1062 const GLfloat texcoord
[4],
1065 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1066 const GLint width
= img
->Width2
; /* without border, power of two */
1067 const GLint height
= img
->Height2
; /* without border, power of two */
1071 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
1072 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
1074 /* skip over the border, if any */
1078 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
1079 /* Need this test for GL_CLAMP_TO_BORDER mode */
1080 get_border_color(tObj
, img
, rgba
);
1083 swImg
->FetchTexelf(swImg
, i
, j
, 0, rgba
);
1089 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1090 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1093 sample_2d_linear(struct gl_context
*ctx
,
1094 const struct gl_texture_object
*tObj
,
1095 const struct gl_texture_image
*img
,
1096 const GLfloat texcoord
[4],
1099 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1100 const GLint width
= img
->Width2
;
1101 const GLint height
= img
->Height2
;
1102 GLint i0
, j0
, i1
, j1
;
1103 GLbitfield useBorderColor
= 0x0;
1105 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1107 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1108 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1117 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1118 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1119 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1120 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1123 /* fetch four texel colors */
1124 if (useBorderColor
& (I0BIT
| J0BIT
)) {
1125 get_border_color(tObj
, img
, t00
);
1128 swImg
->FetchTexelf(swImg
, i0
, j0
, 0, t00
);
1130 if (useBorderColor
& (I1BIT
| J0BIT
)) {
1131 get_border_color(tObj
, img
, t10
);
1134 swImg
->FetchTexelf(swImg
, i1
, j0
, 0, t10
);
1136 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1137 get_border_color(tObj
, img
, t01
);
1140 swImg
->FetchTexelf(swImg
, i0
, j1
, 0, t01
);
1142 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1143 get_border_color(tObj
, img
, t11
);
1146 swImg
->FetchTexelf(swImg
, i1
, j1
, 0, t11
);
1149 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1154 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1155 * We don't have to worry about the texture border.
1158 sample_2d_linear_repeat(struct gl_context
*ctx
,
1159 const struct gl_texture_object
*tObj
,
1160 const struct gl_texture_image
*img
,
1161 const GLfloat texcoord
[4],
1164 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1165 const GLint width
= img
->Width2
;
1166 const GLint height
= img
->Height2
;
1167 GLint i0
, j0
, i1
, j1
;
1169 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1173 ASSERT(tObj
->Sampler
.WrapS
== GL_REPEAT
);
1174 ASSERT(tObj
->Sampler
.WrapT
== GL_REPEAT
);
1175 ASSERT(img
->Border
== 0);
1176 ASSERT(img
->_IsPowerOfTwo
);
1178 linear_repeat_texel_location(width
, texcoord
[0], &i0
, &i1
, &wi
);
1179 linear_repeat_texel_location(height
, texcoord
[1], &j0
, &j1
, &wj
);
1181 swImg
->FetchTexelf(swImg
, i0
, j0
, 0, t00
);
1182 swImg
->FetchTexelf(swImg
, i1
, j0
, 0, t10
);
1183 swImg
->FetchTexelf(swImg
, i0
, j1
, 0, t01
);
1184 swImg
->FetchTexelf(swImg
, i1
, j1
, 0, t11
);
1186 lerp_rgba_2d(rgba
, wi
, wj
, t00
, t10
, t01
, t11
);
1191 sample_2d_nearest_mipmap_nearest(struct gl_context
*ctx
,
1192 const struct gl_texture_object
*tObj
,
1193 GLuint n
, const GLfloat texcoord
[][4],
1194 const GLfloat lambda
[], GLfloat rgba
[][4])
1197 for (i
= 0; i
< n
; i
++) {
1198 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1199 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1205 sample_2d_linear_mipmap_nearest(struct gl_context
*ctx
,
1206 const struct gl_texture_object
*tObj
,
1207 GLuint n
, const GLfloat texcoord
[][4],
1208 const GLfloat lambda
[], GLfloat rgba
[][4])
1211 ASSERT(lambda
!= NULL
);
1212 for (i
= 0; i
< n
; i
++) {
1213 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1214 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1220 sample_2d_nearest_mipmap_linear(struct gl_context
*ctx
,
1221 const struct gl_texture_object
*tObj
,
1222 GLuint n
, const GLfloat texcoord
[][4],
1223 const GLfloat lambda
[], GLfloat rgba
[][4])
1226 ASSERT(lambda
!= NULL
);
1227 for (i
= 0; i
< n
; i
++) {
1228 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1229 if (level
>= tObj
->_MaxLevel
) {
1230 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1231 texcoord
[i
], rgba
[i
]);
1234 GLfloat t0
[4], t1
[4]; /* texels */
1235 const GLfloat f
= FRAC(lambda
[i
]);
1236 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1237 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1238 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1245 sample_2d_linear_mipmap_linear( struct gl_context
*ctx
,
1246 const struct gl_texture_object
*tObj
,
1247 GLuint n
, const GLfloat texcoord
[][4],
1248 const GLfloat lambda
[], GLfloat rgba
[][4] )
1251 ASSERT(lambda
!= NULL
);
1252 for (i
= 0; i
< n
; i
++) {
1253 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1254 if (level
>= tObj
->_MaxLevel
) {
1255 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1256 texcoord
[i
], rgba
[i
]);
1259 GLfloat t0
[4], t1
[4]; /* texels */
1260 const GLfloat f
= FRAC(lambda
[i
]);
1261 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1262 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1263 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1270 sample_2d_linear_mipmap_linear_repeat(struct gl_context
*ctx
,
1271 const struct gl_texture_object
*tObj
,
1272 GLuint n
, const GLfloat texcoord
[][4],
1273 const GLfloat lambda
[], GLfloat rgba
[][4])
1276 ASSERT(lambda
!= NULL
);
1277 ASSERT(tObj
->Sampler
.WrapS
== GL_REPEAT
);
1278 ASSERT(tObj
->Sampler
.WrapT
== GL_REPEAT
);
1279 for (i
= 0; i
< n
; i
++) {
1280 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1281 if (level
>= tObj
->_MaxLevel
) {
1282 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1283 texcoord
[i
], rgba
[i
]);
1286 GLfloat t0
[4], t1
[4]; /* texels */
1287 const GLfloat f
= FRAC(lambda
[i
]);
1288 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
],
1290 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
+1],
1292 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1298 /** Sample 2D texture, nearest filtering for both min/magnification */
1300 sample_nearest_2d(struct gl_context
*ctx
,
1301 const struct gl_texture_object
*tObj
, GLuint n
,
1302 const GLfloat texcoords
[][4],
1303 const GLfloat lambda
[], GLfloat rgba
[][4])
1306 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1308 for (i
= 0; i
< n
; i
++) {
1309 sample_2d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1314 /** Sample 2D texture, linear filtering for both min/magnification */
1316 sample_linear_2d(struct gl_context
*ctx
,
1317 const struct gl_texture_object
*tObj
, GLuint n
,
1318 const GLfloat texcoords
[][4],
1319 const GLfloat lambda
[], GLfloat rgba
[][4])
1322 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1324 if (tObj
->Sampler
.WrapS
== GL_REPEAT
&&
1325 tObj
->Sampler
.WrapT
== GL_REPEAT
&&
1326 image
->_IsPowerOfTwo
&&
1327 image
->Border
== 0) {
1328 for (i
= 0; i
< n
; i
++) {
1329 sample_2d_linear_repeat(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1333 for (i
= 0; i
< n
; i
++) {
1334 sample_2d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1341 * Optimized 2-D texture sampling:
1342 * S and T wrap mode == GL_REPEAT
1343 * GL_NEAREST min/mag filter
1345 * RowStride == Width,
1349 opt_sample_rgb_2d(struct gl_context
*ctx
,
1350 const struct gl_texture_object
*tObj
,
1351 GLuint n
, const GLfloat texcoords
[][4],
1352 const GLfloat lambda
[], GLfloat rgba
[][4])
1354 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1355 const GLfloat width
= (GLfloat
) img
->Width
;
1356 const GLfloat height
= (GLfloat
) img
->Height
;
1357 const GLint colMask
= img
->Width
- 1;
1358 const GLint rowMask
= img
->Height
- 1;
1359 const GLint shift
= img
->WidthLog2
;
1363 ASSERT(tObj
->Sampler
.WrapS
==GL_REPEAT
);
1364 ASSERT(tObj
->Sampler
.WrapT
==GL_REPEAT
);
1365 ASSERT(img
->Border
==0);
1366 ASSERT(img
->TexFormat
== MESA_FORMAT_RGB888
);
1367 ASSERT(img
->_IsPowerOfTwo
);
1369 for (k
=0; k
<n
; k
++) {
1370 GLint i
= IFLOOR(texcoords
[k
][0] * width
) & colMask
;
1371 GLint j
= IFLOOR(texcoords
[k
][1] * height
) & rowMask
;
1372 GLint pos
= (j
<< shift
) | i
;
1373 GLubyte
*texel
= ((GLubyte
*) img
->Data
) + 3*pos
;
1374 rgba
[k
][RCOMP
] = UBYTE_TO_FLOAT(texel
[2]);
1375 rgba
[k
][GCOMP
] = UBYTE_TO_FLOAT(texel
[1]);
1376 rgba
[k
][BCOMP
] = UBYTE_TO_FLOAT(texel
[0]);
1377 rgba
[k
][ACOMP
] = 1.0F
;
1383 * Optimized 2-D texture sampling:
1384 * S and T wrap mode == GL_REPEAT
1385 * GL_NEAREST min/mag filter
1387 * RowStride == Width,
1391 opt_sample_rgba_2d(struct gl_context
*ctx
,
1392 const struct gl_texture_object
*tObj
,
1393 GLuint n
, const GLfloat texcoords
[][4],
1394 const GLfloat lambda
[], GLfloat rgba
[][4])
1396 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1397 const GLfloat width
= (GLfloat
) img
->Width
;
1398 const GLfloat height
= (GLfloat
) img
->Height
;
1399 const GLint colMask
= img
->Width
- 1;
1400 const GLint rowMask
= img
->Height
- 1;
1401 const GLint shift
= img
->WidthLog2
;
1405 ASSERT(tObj
->Sampler
.WrapS
==GL_REPEAT
);
1406 ASSERT(tObj
->Sampler
.WrapT
==GL_REPEAT
);
1407 ASSERT(img
->Border
==0);
1408 ASSERT(img
->TexFormat
== MESA_FORMAT_RGBA8888
);
1409 ASSERT(img
->_IsPowerOfTwo
);
1411 for (i
= 0; i
< n
; i
++) {
1412 const GLint col
= IFLOOR(texcoords
[i
][0] * width
) & colMask
;
1413 const GLint row
= IFLOOR(texcoords
[i
][1] * height
) & rowMask
;
1414 const GLint pos
= (row
<< shift
) | col
;
1415 const GLuint texel
= *((GLuint
*) img
->Data
+ pos
);
1416 rgba
[i
][RCOMP
] = UBYTE_TO_FLOAT( (texel
>> 24) );
1417 rgba
[i
][GCOMP
] = UBYTE_TO_FLOAT( (texel
>> 16) & 0xff );
1418 rgba
[i
][BCOMP
] = UBYTE_TO_FLOAT( (texel
>> 8) & 0xff );
1419 rgba
[i
][ACOMP
] = UBYTE_TO_FLOAT( (texel
) & 0xff );
1424 /** Sample 2D texture, using lambda to choose between min/magnification */
1426 sample_lambda_2d(struct gl_context
*ctx
,
1427 const struct gl_texture_object
*tObj
,
1428 GLuint n
, const GLfloat texcoords
[][4],
1429 const GLfloat lambda
[], GLfloat rgba
[][4])
1431 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1432 GLuint minStart
, minEnd
; /* texels with minification */
1433 GLuint magStart
, magEnd
; /* texels with magnification */
1435 const GLboolean repeatNoBorderPOT
= (tObj
->Sampler
.WrapS
== GL_REPEAT
)
1436 && (tObj
->Sampler
.WrapT
== GL_REPEAT
)
1437 && (tImg
->Border
== 0 && (tImg
->Width
== tImg
->RowStride
))
1438 && tImg
->_IsPowerOfTwo
;
1440 ASSERT(lambda
!= NULL
);
1441 compute_min_mag_ranges(tObj
, n
, lambda
,
1442 &minStart
, &minEnd
, &magStart
, &magEnd
);
1444 if (minStart
< minEnd
) {
1445 /* do the minified texels */
1446 const GLuint m
= minEnd
- minStart
;
1447 switch (tObj
->Sampler
.MinFilter
) {
1449 if (repeatNoBorderPOT
) {
1450 switch (tImg
->TexFormat
) {
1451 case MESA_FORMAT_RGB888
:
1452 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1453 NULL
, rgba
+ minStart
);
1455 case MESA_FORMAT_RGBA8888
:
1456 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1457 NULL
, rgba
+ minStart
);
1460 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1461 NULL
, rgba
+ minStart
);
1465 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1466 NULL
, rgba
+ minStart
);
1470 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1471 NULL
, rgba
+ minStart
);
1473 case GL_NEAREST_MIPMAP_NEAREST
:
1474 sample_2d_nearest_mipmap_nearest(ctx
, tObj
, m
,
1475 texcoords
+ minStart
,
1476 lambda
+ minStart
, rgba
+ minStart
);
1478 case GL_LINEAR_MIPMAP_NEAREST
:
1479 sample_2d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1480 lambda
+ minStart
, rgba
+ minStart
);
1482 case GL_NEAREST_MIPMAP_LINEAR
:
1483 sample_2d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1484 lambda
+ minStart
, rgba
+ minStart
);
1486 case GL_LINEAR_MIPMAP_LINEAR
:
1487 if (repeatNoBorderPOT
)
1488 sample_2d_linear_mipmap_linear_repeat(ctx
, tObj
, m
,
1489 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1491 sample_2d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1492 lambda
+ minStart
, rgba
+ minStart
);
1495 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1500 if (magStart
< magEnd
) {
1501 /* do the magnified texels */
1502 const GLuint m
= magEnd
- magStart
;
1504 switch (tObj
->Sampler
.MagFilter
) {
1506 if (repeatNoBorderPOT
) {
1507 switch (tImg
->TexFormat
) {
1508 case MESA_FORMAT_RGB888
:
1509 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1510 NULL
, rgba
+ magStart
);
1512 case MESA_FORMAT_RGBA8888
:
1513 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1514 NULL
, rgba
+ magStart
);
1517 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1518 NULL
, rgba
+ magStart
);
1522 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1523 NULL
, rgba
+ magStart
);
1527 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1528 NULL
, rgba
+ magStart
);
1531 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1537 /* For anisotropic filtering */
1538 #define WEIGHT_LUT_SIZE 1024
1540 static GLfloat
*weightLut
= NULL
;
1543 * Creates the look-up table used to speed-up EWA sampling
1546 create_filter_table(void)
1550 weightLut
= (GLfloat
*) malloc(WEIGHT_LUT_SIZE
* sizeof(GLfloat
));
1552 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
1554 GLfloat r2
= (GLfloat
) i
/ (GLfloat
) (WEIGHT_LUT_SIZE
- 1);
1555 GLfloat weight
= (GLfloat
) exp(-alpha
* r2
);
1556 weightLut
[i
] = weight
;
1563 * Elliptical weighted average (EWA) filter for producing high quality
1564 * anisotropic filtered results.
1565 * Based on the Higher Quality Elliptical Weighted Avarage Filter
1566 * published by Paul S. Heckbert in his Master's Thesis
1567 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1570 sample_2d_ewa(struct gl_context
*ctx
,
1571 const struct gl_texture_object
*tObj
,
1572 const GLfloat texcoord
[4],
1573 const GLfloat dudx
, const GLfloat dvdx
,
1574 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1577 GLint level
= lod
> 0 ? lod
: 0;
1578 GLfloat scaling
= 1.0 / (1 << level
);
1579 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1580 const struct gl_texture_image
*mostDetailedImage
=
1581 tObj
->Image
[0][tObj
->BaseLevel
];
1582 GLfloat tex_u
=-0.5 + texcoord
[0] * mostDetailedImage
->WidthScale
* scaling
;
1583 GLfloat tex_v
=-0.5 + texcoord
[1] * mostDetailedImage
->HeightScale
* scaling
;
1585 GLfloat ux
= dudx
* scaling
;
1586 GLfloat vx
= dvdx
* scaling
;
1587 GLfloat uy
= dudy
* scaling
;
1588 GLfloat vy
= dvdy
* scaling
;
1590 /* compute ellipse coefficients to bound the region:
1591 * A*x*x + B*x*y + C*y*y = F.
1593 GLfloat A
= vx
*vx
+vy
*vy
+1;
1594 GLfloat B
= -2*(ux
*vx
+uy
*vy
);
1595 GLfloat C
= ux
*ux
+uy
*uy
+1;
1596 GLfloat F
= A
*C
-B
*B
/4.0;
1598 /* check if it is an ellipse */
1599 /* ASSERT(F > 0.0); */
1601 /* Compute the ellipse's (u,v) bounding box in texture space */
1602 GLfloat d
= -B
*B
+4.0*C
*A
;
1603 GLfloat box_u
= 2.0 / d
* sqrt(d
*C
*F
); /* box_u -> half of bbox with */
1604 GLfloat box_v
= 2.0 / d
* sqrt(A
*d
*F
); /* box_v -> half of bbox height */
1606 GLint u0
= floor(tex_u
- box_u
);
1607 GLint u1
= ceil (tex_u
+ box_u
);
1608 GLint v0
= floor(tex_v
- box_v
);
1609 GLint v1
= ceil (tex_v
+ box_v
);
1611 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1612 GLfloat newCoord
[2];
1615 GLfloat U
= u0
- tex_u
;
1618 /* Scale ellipse formula to directly index the Filter Lookup Table.
1619 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1621 double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
1625 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1627 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1628 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1629 * value, q, is less than F, we're inside the ellipse
1632 for (v
= v0
; v
<= v1
; ++v
) {
1633 GLfloat V
= v
- tex_v
;
1634 GLfloat dq
= A
* (2 * U
+ 1) + B
* V
;
1635 GLfloat q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
1638 for (u
= u0
; u
<= u1
; ++u
) {
1639 /* Note that the ellipse has been pre-scaled so F = WEIGHT_LUT_SIZE - 1 */
1640 if (q
< WEIGHT_LUT_SIZE
) {
1641 /* as a LUT is used, q must never be negative;
1642 * should not happen, though
1644 const GLint qClamped
= q
>= 0.0F
? q
: 0;
1645 GLfloat weight
= weightLut
[qClamped
];
1647 newCoord
[0] = u
/ ((GLfloat
) img
->Width2
);
1648 newCoord
[1] = v
/ ((GLfloat
) img
->Height2
);
1650 sample_2d_nearest(ctx
, tObj
, img
, newCoord
, rgba
);
1651 num
[0] += weight
* rgba
[0];
1652 num
[1] += weight
* rgba
[1];
1653 num
[2] += weight
* rgba
[2];
1654 num
[3] += weight
* rgba
[3];
1664 /* Reaching this place would mean
1665 * that no pixels intersected the ellipse.
1666 * This should never happen because
1667 * the filter we use always
1668 * intersects at least one pixel.
1675 /* not enough pixels in resampling, resort to direct interpolation */
1676 sample_2d_linear(ctx
, tObj
, img
, texcoord
, rgba
);
1680 rgba
[0] = num
[0] / den
;
1681 rgba
[1] = num
[1] / den
;
1682 rgba
[2] = num
[2] / den
;
1683 rgba
[3] = num
[3] / den
;
1688 * Anisotropic filtering using footprint assembly as outlined in the
1689 * EXT_texture_filter_anisotropic spec:
1690 * http://www.opengl.org/registry/specs/EXT/texture_filter_anisotropic.txt
1691 * Faster than EWA but has less quality (more aliasing effects)
1694 sample_2d_footprint(struct gl_context
*ctx
,
1695 const struct gl_texture_object
*tObj
,
1696 const GLfloat texcoord
[4],
1697 const GLfloat dudx
, const GLfloat dvdx
,
1698 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1701 GLint level
= lod
> 0 ? lod
: 0;
1702 GLfloat scaling
= 1.0F
/ (1 << level
);
1703 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1705 GLfloat ux
= dudx
* scaling
;
1706 GLfloat vx
= dvdx
* scaling
;
1707 GLfloat uy
= dudy
* scaling
;
1708 GLfloat vy
= dvdy
* scaling
;
1710 GLfloat Px2
= ux
* ux
+ vx
* vx
; /* squared length of dx */
1711 GLfloat Py2
= uy
* uy
+ vy
* vy
; /* squared length of dy */
1717 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1718 GLfloat newCoord
[2];
1721 /* Calculate the per anisotropic sample offsets in s,t space. */
1723 numSamples
= ceil(SQRTF(Px2
));
1724 ds
= ux
/ ((GLfloat
) img
->Width2
);
1725 dt
= vx
/ ((GLfloat
) img
->Height2
);
1728 numSamples
= ceil(SQRTF(Py2
));
1729 ds
= uy
/ ((GLfloat
) img
->Width2
);
1730 dt
= vy
/ ((GLfloat
) img
->Height2
);
1733 for (s
= 0; s
<numSamples
; s
++) {
1734 newCoord
[0] = texcoord
[0] + ds
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5);
1735 newCoord
[1] = texcoord
[1] + dt
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5);
1737 sample_2d_linear(ctx
, tObj
, img
, newCoord
, rgba
);
1744 rgba
[0] = num
[0] / numSamples
;
1745 rgba
[1] = num
[1] / numSamples
;
1746 rgba
[2] = num
[2] / numSamples
;
1747 rgba
[3] = num
[3] / numSamples
;
1752 * Returns the index of the specified texture object in the
1753 * gl_context texture unit array.
1755 static INLINE GLuint
1756 texture_unit_index(const struct gl_context
*ctx
,
1757 const struct gl_texture_object
*tObj
)
1759 const GLuint maxUnit
1760 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
1763 /* XXX CoordUnits vs. ImageUnits */
1764 for (u
= 0; u
< maxUnit
; u
++) {
1765 if (ctx
->Texture
.Unit
[u
]._Current
== tObj
)
1769 u
= 0; /* not found, use 1st one; should never happen */
1776 * Sample 2D texture using an anisotropic filter.
1777 * NOTE: the const GLfloat lambda_iso[] parameter does *NOT* contain
1778 * the lambda float array but a "hidden" SWspan struct which is required
1779 * by this function but is not available in the texture_sample_func signature.
1780 * See _swrast_texture_span( struct gl_context *ctx, SWspan *span ) on how
1781 * this function is called.
1784 sample_lambda_2d_aniso(struct gl_context
*ctx
,
1785 const struct gl_texture_object
*tObj
,
1786 GLuint n
, const GLfloat texcoords
[][4],
1787 const GLfloat lambda_iso
[], GLfloat rgba
[][4])
1789 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1790 const GLfloat maxEccentricity
=
1791 tObj
->Sampler
.MaxAnisotropy
* tObj
->Sampler
.MaxAnisotropy
;
1793 /* re-calculate the lambda values so that they are usable with anisotropic
1796 SWspan
*span
= (SWspan
*)lambda_iso
; /* access the "hidden" SWspan struct */
1798 /* based on interpolate_texcoords(struct gl_context *ctx, SWspan *span)
1799 * in swrast/s_span.c
1802 /* find the texture unit index by looking up the current texture object
1803 * from the context list of available texture objects.
1805 const GLuint u
= texture_unit_index(ctx
, tObj
);
1806 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
1809 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
1810 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
1811 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
1812 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
1813 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
1814 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
1815 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
1816 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
1817 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
1819 /* from swrast/s_texcombine.c _swrast_texture_span */
1820 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[u
];
1821 const GLboolean adjustLOD
=
1822 (texUnit
->LodBias
+ tObj
->Sampler
.LodBias
!= 0.0F
)
1823 || (tObj
->Sampler
.MinLod
!= -1000.0 || tObj
->Sampler
.MaxLod
!= 1000.0);
1827 /* on first access create the lookup table containing the filter weights. */
1829 create_filter_table();
1832 texW
= tImg
->WidthScale
;
1833 texH
= tImg
->HeightScale
;
1835 for (i
= 0; i
< n
; i
++) {
1836 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
1838 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
1839 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
1840 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
1841 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
1843 /* note: instead of working with Px and Py, we will use the
1844 * squared length instead, to avoid sqrt.
1846 GLfloat Px2
= dudx
* dudx
+ dvdx
* dvdx
;
1847 GLfloat Py2
= dudy
* dudy
+ dvdy
* dvdy
;
1867 /* if the eccentricity of the ellipse is too big, scale up the shorter
1868 * of the two vectors to limit the maximum amount of work per pixel
1871 if (e
> maxEccentricity
) {
1872 /* GLfloat s=e / maxEccentricity;
1876 Pmin2
= Pmax2
/ maxEccentricity
;
1879 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
1880 * this since 0.5*log(x) = log(sqrt(x))
1882 lod
= 0.5 * LOG2(Pmin2
);
1885 /* from swrast/s_texcombine.c _swrast_texture_span */
1886 if (texUnit
->LodBias
+ tObj
->Sampler
.LodBias
!= 0.0F
) {
1887 /* apply LOD bias, but don't clamp yet */
1888 const GLfloat bias
=
1889 CLAMP(texUnit
->LodBias
+ tObj
->Sampler
.LodBias
,
1890 -ctx
->Const
.MaxTextureLodBias
,
1891 ctx
->Const
.MaxTextureLodBias
);
1894 if (tObj
->Sampler
.MinLod
!= -1000.0 ||
1895 tObj
->Sampler
.MaxLod
!= 1000.0) {
1896 /* apply LOD clamping to lambda */
1897 lod
= CLAMP(lod
, tObj
->Sampler
.MinLod
, tObj
->Sampler
.MaxLod
);
1902 /* If the ellipse covers the whole image, we can
1903 * simply return the average of the whole image.
1905 if (lod
>= tObj
->_MaxLevel
) {
1906 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1907 texcoords
[i
], rgba
[i
]);
1910 /* don't bother interpolating between multiple LODs; it doesn't
1911 * seem to be worth the extra running time.
1913 sample_2d_ewa(ctx
, tObj
, texcoords
[i
],
1914 dudx
, dvdx
, dudy
, dvdy
, floor(lod
), rgba
[i
]);
1917 (void) sample_2d_footprint
;
1919 sample_2d_footprint(ctx, tObj, texcoords[i],
1920 dudx, dvdx, dudy, dvdy, floor(lod), rgba[i]);
1928 /**********************************************************************/
1929 /* 3-D Texture Sampling Functions */
1930 /**********************************************************************/
1933 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1936 sample_3d_nearest(struct gl_context
*ctx
,
1937 const struct gl_texture_object
*tObj
,
1938 const struct gl_texture_image
*img
,
1939 const GLfloat texcoord
[4],
1942 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1943 const GLint width
= img
->Width2
; /* without border, power of two */
1944 const GLint height
= img
->Height2
; /* without border, power of two */
1945 const GLint depth
= img
->Depth2
; /* without border, power of two */
1949 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
1950 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
1951 k
= nearest_texel_location(tObj
->Sampler
.WrapR
, img
, depth
, texcoord
[2]);
1953 if (i
< 0 || i
>= (GLint
) img
->Width
||
1954 j
< 0 || j
>= (GLint
) img
->Height
||
1955 k
< 0 || k
>= (GLint
) img
->Depth
) {
1956 /* Need this test for GL_CLAMP_TO_BORDER mode */
1957 get_border_color(tObj
, img
, rgba
);
1960 swImg
->FetchTexelf(swImg
, i
, j
, k
, rgba
);
1966 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1969 sample_3d_linear(struct gl_context
*ctx
,
1970 const struct gl_texture_object
*tObj
,
1971 const struct gl_texture_image
*img
,
1972 const GLfloat texcoord
[4],
1975 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1976 const GLint width
= img
->Width2
;
1977 const GLint height
= img
->Height2
;
1978 const GLint depth
= img
->Depth2
;
1979 GLint i0
, j0
, k0
, i1
, j1
, k1
;
1980 GLbitfield useBorderColor
= 0x0;
1982 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
1983 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
1985 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1986 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1987 linear_texel_locations(tObj
->Sampler
.WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
1998 /* check if sampling texture border color */
1999 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2000 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2001 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2002 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2003 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
2004 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
2008 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
2009 get_border_color(tObj
, img
, t000
);
2012 swImg
->FetchTexelf(swImg
, i0
, j0
, k0
, t000
);
2014 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
2015 get_border_color(tObj
, img
, t100
);
2018 swImg
->FetchTexelf(swImg
, i1
, j0
, k0
, t100
);
2020 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
2021 get_border_color(tObj
, img
, t010
);
2024 swImg
->FetchTexelf(swImg
, i0
, j1
, k0
, t010
);
2026 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
2027 get_border_color(tObj
, img
, t110
);
2030 swImg
->FetchTexelf(swImg
, i1
, j1
, k0
, t110
);
2033 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
2034 get_border_color(tObj
, img
, t001
);
2037 swImg
->FetchTexelf(swImg
, i0
, j0
, k1
, t001
);
2039 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
2040 get_border_color(tObj
, img
, t101
);
2043 swImg
->FetchTexelf(swImg
, i1
, j0
, k1
, t101
);
2045 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
2046 get_border_color(tObj
, img
, t011
);
2049 swImg
->FetchTexelf(swImg
, i0
, j1
, k1
, t011
);
2051 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
2052 get_border_color(tObj
, img
, t111
);
2055 swImg
->FetchTexelf(swImg
, i1
, j1
, k1
, t111
);
2058 /* trilinear interpolation of samples */
2059 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
2064 sample_3d_nearest_mipmap_nearest(struct gl_context
*ctx
,
2065 const struct gl_texture_object
*tObj
,
2066 GLuint n
, const GLfloat texcoord
[][4],
2067 const GLfloat lambda
[], GLfloat rgba
[][4] )
2070 for (i
= 0; i
< n
; i
++) {
2071 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2072 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2078 sample_3d_linear_mipmap_nearest(struct gl_context
*ctx
,
2079 const struct gl_texture_object
*tObj
,
2080 GLuint n
, const GLfloat texcoord
[][4],
2081 const GLfloat lambda
[], GLfloat rgba
[][4])
2084 ASSERT(lambda
!= NULL
);
2085 for (i
= 0; i
< n
; i
++) {
2086 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2087 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2093 sample_3d_nearest_mipmap_linear(struct gl_context
*ctx
,
2094 const struct gl_texture_object
*tObj
,
2095 GLuint n
, const GLfloat texcoord
[][4],
2096 const GLfloat lambda
[], GLfloat rgba
[][4])
2099 ASSERT(lambda
!= NULL
);
2100 for (i
= 0; i
< n
; i
++) {
2101 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2102 if (level
>= tObj
->_MaxLevel
) {
2103 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2104 texcoord
[i
], rgba
[i
]);
2107 GLfloat t0
[4], t1
[4]; /* texels */
2108 const GLfloat f
= FRAC(lambda
[i
]);
2109 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2110 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2111 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2118 sample_3d_linear_mipmap_linear(struct gl_context
*ctx
,
2119 const struct gl_texture_object
*tObj
,
2120 GLuint n
, const GLfloat texcoord
[][4],
2121 const GLfloat lambda
[], GLfloat rgba
[][4])
2124 ASSERT(lambda
!= NULL
);
2125 for (i
= 0; i
< n
; i
++) {
2126 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2127 if (level
>= tObj
->_MaxLevel
) {
2128 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2129 texcoord
[i
], rgba
[i
]);
2132 GLfloat t0
[4], t1
[4]; /* texels */
2133 const GLfloat f
= FRAC(lambda
[i
]);
2134 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2135 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2136 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2142 /** Sample 3D texture, nearest filtering for both min/magnification */
2144 sample_nearest_3d(struct gl_context
*ctx
,
2145 const struct gl_texture_object
*tObj
, GLuint n
,
2146 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2150 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2152 for (i
= 0; i
< n
; i
++) {
2153 sample_3d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2158 /** Sample 3D texture, linear filtering for both min/magnification */
2160 sample_linear_3d(struct gl_context
*ctx
,
2161 const struct gl_texture_object
*tObj
, GLuint n
,
2162 const GLfloat texcoords
[][4],
2163 const GLfloat lambda
[], GLfloat rgba
[][4])
2166 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2168 for (i
= 0; i
< n
; i
++) {
2169 sample_3d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2174 /** Sample 3D texture, using lambda to choose between min/magnification */
2176 sample_lambda_3d(struct gl_context
*ctx
,
2177 const struct gl_texture_object
*tObj
, GLuint n
,
2178 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2181 GLuint minStart
, minEnd
; /* texels with minification */
2182 GLuint magStart
, magEnd
; /* texels with magnification */
2185 ASSERT(lambda
!= NULL
);
2186 compute_min_mag_ranges(tObj
, n
, lambda
,
2187 &minStart
, &minEnd
, &magStart
, &magEnd
);
2189 if (minStart
< minEnd
) {
2190 /* do the minified texels */
2191 GLuint m
= minEnd
- minStart
;
2192 switch (tObj
->Sampler
.MinFilter
) {
2194 for (i
= minStart
; i
< minEnd
; i
++)
2195 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2196 texcoords
[i
], rgba
[i
]);
2199 for (i
= minStart
; i
< minEnd
; i
++)
2200 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2201 texcoords
[i
], rgba
[i
]);
2203 case GL_NEAREST_MIPMAP_NEAREST
:
2204 sample_3d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2205 lambda
+ minStart
, rgba
+ minStart
);
2207 case GL_LINEAR_MIPMAP_NEAREST
:
2208 sample_3d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2209 lambda
+ minStart
, rgba
+ minStart
);
2211 case GL_NEAREST_MIPMAP_LINEAR
:
2212 sample_3d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2213 lambda
+ minStart
, rgba
+ minStart
);
2215 case GL_LINEAR_MIPMAP_LINEAR
:
2216 sample_3d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2217 lambda
+ minStart
, rgba
+ minStart
);
2220 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
2225 if (magStart
< magEnd
) {
2226 /* do the magnified texels */
2227 switch (tObj
->Sampler
.MagFilter
) {
2229 for (i
= magStart
; i
< magEnd
; i
++)
2230 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2231 texcoords
[i
], rgba
[i
]);
2234 for (i
= magStart
; i
< magEnd
; i
++)
2235 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2236 texcoords
[i
], rgba
[i
]);
2239 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
2246 /**********************************************************************/
2247 /* Texture Cube Map Sampling Functions */
2248 /**********************************************************************/
2251 * Choose one of six sides of a texture cube map given the texture
2252 * coord (rx,ry,rz). Return pointer to corresponding array of texture
2255 static const struct gl_texture_image
**
2256 choose_cube_face(const struct gl_texture_object
*texObj
,
2257 const GLfloat texcoord
[4], GLfloat newCoord
[4])
2261 direction target sc tc ma
2262 ---------- ------------------------------- --- --- ---
2263 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2264 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2265 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2266 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2267 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2268 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2270 const GLfloat rx
= texcoord
[0];
2271 const GLfloat ry
= texcoord
[1];
2272 const GLfloat rz
= texcoord
[2];
2273 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
2277 if (arx
>= ary
&& arx
>= arz
) {
2291 else if (ary
>= arx
&& ary
>= arz
) {
2321 const float ima
= 1.0F
/ ma
;
2322 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
2323 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
2326 return (const struct gl_texture_image
**) texObj
->Image
[face
];
2331 sample_nearest_cube(struct gl_context
*ctx
,
2332 const struct gl_texture_object
*tObj
, GLuint n
,
2333 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2338 for (i
= 0; i
< n
; i
++) {
2339 const struct gl_texture_image
**images
;
2340 GLfloat newCoord
[4];
2341 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2342 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
2349 sample_linear_cube(struct gl_context
*ctx
,
2350 const struct gl_texture_object
*tObj
, GLuint n
,
2351 const GLfloat texcoords
[][4],
2352 const GLfloat lambda
[], GLfloat rgba
[][4])
2356 for (i
= 0; i
< n
; i
++) {
2357 const struct gl_texture_image
**images
;
2358 GLfloat newCoord
[4];
2359 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2360 sample_2d_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
2367 sample_cube_nearest_mipmap_nearest(struct gl_context
*ctx
,
2368 const struct gl_texture_object
*tObj
,
2369 GLuint n
, const GLfloat texcoord
[][4],
2370 const GLfloat lambda
[], GLfloat rgba
[][4])
2373 ASSERT(lambda
!= NULL
);
2374 for (i
= 0; i
< n
; i
++) {
2375 const struct gl_texture_image
**images
;
2376 GLfloat newCoord
[4];
2378 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2380 /* XXX we actually need to recompute lambda here based on the newCoords.
2381 * But we would need the texcoords of adjacent fragments to compute that
2382 * properly, and we don't have those here.
2383 * For now, do an approximation: subtracting 1 from the chosen mipmap
2384 * level seems to work in some test cases.
2385 * The same adjustment is done in the next few functions.
2387 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2388 level
= MAX2(level
- 1, 0);
2390 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2396 sample_cube_linear_mipmap_nearest(struct gl_context
*ctx
,
2397 const struct gl_texture_object
*tObj
,
2398 GLuint n
, const GLfloat texcoord
[][4],
2399 const GLfloat lambda
[], GLfloat rgba
[][4])
2402 ASSERT(lambda
!= NULL
);
2403 for (i
= 0; i
< n
; i
++) {
2404 const struct gl_texture_image
**images
;
2405 GLfloat newCoord
[4];
2406 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2407 level
= MAX2(level
- 1, 0); /* see comment above */
2408 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2409 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2415 sample_cube_nearest_mipmap_linear(struct gl_context
*ctx
,
2416 const struct gl_texture_object
*tObj
,
2417 GLuint n
, const GLfloat texcoord
[][4],
2418 const GLfloat lambda
[], GLfloat rgba
[][4])
2421 ASSERT(lambda
!= NULL
);
2422 for (i
= 0; i
< n
; i
++) {
2423 const struct gl_texture_image
**images
;
2424 GLfloat newCoord
[4];
2425 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2426 level
= MAX2(level
- 1, 0); /* see comment above */
2427 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2428 if (level
>= tObj
->_MaxLevel
) {
2429 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2433 GLfloat t0
[4], t1
[4]; /* texels */
2434 const GLfloat f
= FRAC(lambda
[i
]);
2435 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
2436 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2437 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2444 sample_cube_linear_mipmap_linear(struct gl_context
*ctx
,
2445 const struct gl_texture_object
*tObj
,
2446 GLuint n
, const GLfloat texcoord
[][4],
2447 const GLfloat lambda
[], GLfloat rgba
[][4])
2450 ASSERT(lambda
!= NULL
);
2451 for (i
= 0; i
< n
; i
++) {
2452 const struct gl_texture_image
**images
;
2453 GLfloat newCoord
[4];
2454 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2455 level
= MAX2(level
- 1, 0); /* see comment above */
2456 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2457 if (level
>= tObj
->_MaxLevel
) {
2458 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2462 GLfloat t0
[4], t1
[4];
2463 const GLfloat f
= FRAC(lambda
[i
]);
2464 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
2465 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2466 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2472 /** Sample cube texture, using lambda to choose between min/magnification */
2474 sample_lambda_cube(struct gl_context
*ctx
,
2475 const struct gl_texture_object
*tObj
, GLuint n
,
2476 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2479 GLuint minStart
, minEnd
; /* texels with minification */
2480 GLuint magStart
, magEnd
; /* texels with magnification */
2482 ASSERT(lambda
!= NULL
);
2483 compute_min_mag_ranges(tObj
, n
, lambda
,
2484 &minStart
, &minEnd
, &magStart
, &magEnd
);
2486 if (minStart
< minEnd
) {
2487 /* do the minified texels */
2488 const GLuint m
= minEnd
- minStart
;
2489 switch (tObj
->Sampler
.MinFilter
) {
2491 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2492 lambda
+ minStart
, rgba
+ minStart
);
2495 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2496 lambda
+ minStart
, rgba
+ minStart
);
2498 case GL_NEAREST_MIPMAP_NEAREST
:
2499 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
2500 texcoords
+ minStart
,
2501 lambda
+ minStart
, rgba
+ minStart
);
2503 case GL_LINEAR_MIPMAP_NEAREST
:
2504 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
2505 texcoords
+ minStart
,
2506 lambda
+ minStart
, rgba
+ minStart
);
2508 case GL_NEAREST_MIPMAP_LINEAR
:
2509 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
2510 texcoords
+ minStart
,
2511 lambda
+ minStart
, rgba
+ minStart
);
2513 case GL_LINEAR_MIPMAP_LINEAR
:
2514 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
2515 texcoords
+ minStart
,
2516 lambda
+ minStart
, rgba
+ minStart
);
2519 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2523 if (magStart
< magEnd
) {
2524 /* do the magnified texels */
2525 const GLuint m
= magEnd
- magStart
;
2526 switch (tObj
->Sampler
.MagFilter
) {
2528 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2529 lambda
+ magStart
, rgba
+ magStart
);
2532 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2533 lambda
+ magStart
, rgba
+ magStart
);
2536 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2542 /**********************************************************************/
2543 /* Texture Rectangle Sampling Functions */
2544 /**********************************************************************/
2548 sample_nearest_rect(struct gl_context
*ctx
,
2549 const struct gl_texture_object
*tObj
, GLuint n
,
2550 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2553 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2554 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2555 const GLint width
= img
->Width
;
2556 const GLint height
= img
->Height
;
2562 ASSERT(tObj
->Sampler
.WrapS
== GL_CLAMP
||
2563 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_EDGE
||
2564 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_BORDER
);
2565 ASSERT(tObj
->Sampler
.WrapT
== GL_CLAMP
||
2566 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_EDGE
||
2567 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_BORDER
);
2569 for (i
= 0; i
< n
; i
++) {
2571 col
= clamp_rect_coord_nearest(tObj
->Sampler
.WrapS
, texcoords
[i
][0], width
);
2572 row
= clamp_rect_coord_nearest(tObj
->Sampler
.WrapT
, texcoords
[i
][1], height
);
2573 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2574 get_border_color(tObj
, img
, rgba
[i
]);
2576 swImg
->FetchTexelf(swImg
, col
, row
, 0, rgba
[i
]);
2582 sample_linear_rect(struct gl_context
*ctx
,
2583 const struct gl_texture_object
*tObj
, GLuint n
,
2584 const GLfloat texcoords
[][4],
2585 const GLfloat lambda
[], GLfloat rgba
[][4])
2587 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2588 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2589 const GLint width
= img
->Width
;
2590 const GLint height
= img
->Height
;
2596 ASSERT(tObj
->Sampler
.WrapS
== GL_CLAMP
||
2597 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_EDGE
||
2598 tObj
->Sampler
.WrapS
== GL_CLAMP_TO_BORDER
);
2599 ASSERT(tObj
->Sampler
.WrapT
== GL_CLAMP
||
2600 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_EDGE
||
2601 tObj
->Sampler
.WrapT
== GL_CLAMP_TO_BORDER
);
2603 for (i
= 0; i
< n
; i
++) {
2604 GLint i0
, j0
, i1
, j1
;
2605 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2607 GLbitfield useBorderColor
= 0x0;
2609 clamp_rect_coord_linear(tObj
->Sampler
.WrapS
, texcoords
[i
][0], width
,
2611 clamp_rect_coord_linear(tObj
->Sampler
.WrapT
, texcoords
[i
][1], height
,
2614 /* compute integer rows/columns */
2615 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2616 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2617 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2618 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2620 /* get four texel samples */
2621 if (useBorderColor
& (I0BIT
| J0BIT
))
2622 get_border_color(tObj
, img
, t00
);
2624 swImg
->FetchTexelf(swImg
, i0
, j0
, 0, t00
);
2626 if (useBorderColor
& (I1BIT
| J0BIT
))
2627 get_border_color(tObj
, img
, t10
);
2629 swImg
->FetchTexelf(swImg
, i1
, j0
, 0, t10
);
2631 if (useBorderColor
& (I0BIT
| J1BIT
))
2632 get_border_color(tObj
, img
, t01
);
2634 swImg
->FetchTexelf(swImg
, i0
, j1
, 0, t01
);
2636 if (useBorderColor
& (I1BIT
| J1BIT
))
2637 get_border_color(tObj
, img
, t11
);
2639 swImg
->FetchTexelf(swImg
, i1
, j1
, 0, t11
);
2641 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2646 /** Sample Rect texture, using lambda to choose between min/magnification */
2648 sample_lambda_rect(struct gl_context
*ctx
,
2649 const struct gl_texture_object
*tObj
, GLuint n
,
2650 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2653 GLuint minStart
, minEnd
, magStart
, magEnd
;
2655 /* We only need lambda to decide between minification and magnification.
2656 * There is no mipmapping with rectangular textures.
2658 compute_min_mag_ranges(tObj
, n
, lambda
,
2659 &minStart
, &minEnd
, &magStart
, &magEnd
);
2661 if (minStart
< minEnd
) {
2662 if (tObj
->Sampler
.MinFilter
== GL_NEAREST
) {
2663 sample_nearest_rect(ctx
, tObj
, minEnd
- minStart
,
2664 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2667 sample_linear_rect(ctx
, tObj
, minEnd
- minStart
,
2668 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2671 if (magStart
< magEnd
) {
2672 if (tObj
->Sampler
.MagFilter
== GL_NEAREST
) {
2673 sample_nearest_rect(ctx
, tObj
, magEnd
- magStart
,
2674 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2677 sample_linear_rect(ctx
, tObj
, magEnd
- magStart
,
2678 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2684 /**********************************************************************/
2685 /* 2D Texture Array Sampling Functions */
2686 /**********************************************************************/
2689 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2692 sample_2d_array_nearest(struct gl_context
*ctx
,
2693 const struct gl_texture_object
*tObj
,
2694 const struct gl_texture_image
*img
,
2695 const GLfloat texcoord
[4],
2698 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2699 const GLint width
= img
->Width2
; /* without border, power of two */
2700 const GLint height
= img
->Height2
; /* without border, power of two */
2701 const GLint depth
= img
->Depth
;
2706 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
2707 j
= nearest_texel_location(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1]);
2708 array
= tex_array_slice(texcoord
[2], depth
);
2710 if (i
< 0 || i
>= (GLint
) img
->Width
||
2711 j
< 0 || j
>= (GLint
) img
->Height
||
2712 array
< 0 || array
>= (GLint
) img
->Depth
) {
2713 /* Need this test for GL_CLAMP_TO_BORDER mode */
2714 get_border_color(tObj
, img
, rgba
);
2717 swImg
->FetchTexelf(swImg
, i
, j
, array
, rgba
);
2723 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2726 sample_2d_array_linear(struct gl_context
*ctx
,
2727 const struct gl_texture_object
*tObj
,
2728 const struct gl_texture_image
*img
,
2729 const GLfloat texcoord
[4],
2732 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2733 const GLint width
= img
->Width2
;
2734 const GLint height
= img
->Height2
;
2735 const GLint depth
= img
->Depth
;
2736 GLint i0
, j0
, i1
, j1
;
2738 GLbitfield useBorderColor
= 0x0;
2740 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2742 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2743 linear_texel_locations(tObj
->Sampler
.WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2744 array
= tex_array_slice(texcoord
[2], depth
);
2746 if (array
< 0 || array
>= depth
) {
2747 COPY_4V(rgba
, tObj
->Sampler
.BorderColor
.f
);
2757 /* check if sampling texture border color */
2758 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2759 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2760 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2761 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2765 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2766 get_border_color(tObj
, img
, t00
);
2769 swImg
->FetchTexelf(swImg
, i0
, j0
, array
, t00
);
2771 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2772 get_border_color(tObj
, img
, t10
);
2775 swImg
->FetchTexelf(swImg
, i1
, j0
, array
, t10
);
2777 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2778 get_border_color(tObj
, img
, t01
);
2781 swImg
->FetchTexelf(swImg
, i0
, j1
, array
, t01
);
2783 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2784 get_border_color(tObj
, img
, t11
);
2787 swImg
->FetchTexelf(swImg
, i1
, j1
, array
, t11
);
2790 /* trilinear interpolation of samples */
2791 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2797 sample_2d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2798 const struct gl_texture_object
*tObj
,
2799 GLuint n
, const GLfloat texcoord
[][4],
2800 const GLfloat lambda
[], GLfloat rgba
[][4])
2803 for (i
= 0; i
< n
; i
++) {
2804 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2805 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2812 sample_2d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2813 const struct gl_texture_object
*tObj
,
2814 GLuint n
, const GLfloat texcoord
[][4],
2815 const GLfloat lambda
[], GLfloat rgba
[][4])
2818 ASSERT(lambda
!= NULL
);
2819 for (i
= 0; i
< n
; i
++) {
2820 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2821 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2822 texcoord
[i
], rgba
[i
]);
2828 sample_2d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2829 const struct gl_texture_object
*tObj
,
2830 GLuint n
, const GLfloat texcoord
[][4],
2831 const GLfloat lambda
[], GLfloat rgba
[][4])
2834 ASSERT(lambda
!= NULL
);
2835 for (i
= 0; i
< n
; i
++) {
2836 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2837 if (level
>= tObj
->_MaxLevel
) {
2838 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2839 texcoord
[i
], rgba
[i
]);
2842 GLfloat t0
[4], t1
[4]; /* texels */
2843 const GLfloat f
= FRAC(lambda
[i
]);
2844 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
],
2846 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1],
2848 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2855 sample_2d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2856 const struct gl_texture_object
*tObj
,
2857 GLuint n
, const GLfloat texcoord
[][4],
2858 const GLfloat lambda
[], GLfloat rgba
[][4])
2861 ASSERT(lambda
!= NULL
);
2862 for (i
= 0; i
< n
; i
++) {
2863 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2864 if (level
>= tObj
->_MaxLevel
) {
2865 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2866 texcoord
[i
], rgba
[i
]);
2869 GLfloat t0
[4], t1
[4]; /* texels */
2870 const GLfloat f
= FRAC(lambda
[i
]);
2871 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2873 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1],
2875 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2881 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2883 sample_nearest_2d_array(struct gl_context
*ctx
,
2884 const struct gl_texture_object
*tObj
, GLuint n
,
2885 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2889 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2891 for (i
= 0; i
< n
; i
++) {
2892 sample_2d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2898 /** Sample 2D Array texture, linear filtering for both min/magnification */
2900 sample_linear_2d_array(struct gl_context
*ctx
,
2901 const struct gl_texture_object
*tObj
, GLuint n
,
2902 const GLfloat texcoords
[][4],
2903 const GLfloat lambda
[], GLfloat rgba
[][4])
2906 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2908 for (i
= 0; i
< n
; i
++) {
2909 sample_2d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2914 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2916 sample_lambda_2d_array(struct gl_context
*ctx
,
2917 const struct gl_texture_object
*tObj
, GLuint n
,
2918 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2921 GLuint minStart
, minEnd
; /* texels with minification */
2922 GLuint magStart
, magEnd
; /* texels with magnification */
2925 ASSERT(lambda
!= NULL
);
2926 compute_min_mag_ranges(tObj
, n
, lambda
,
2927 &minStart
, &minEnd
, &magStart
, &magEnd
);
2929 if (minStart
< minEnd
) {
2930 /* do the minified texels */
2931 GLuint m
= minEnd
- minStart
;
2932 switch (tObj
->Sampler
.MinFilter
) {
2934 for (i
= minStart
; i
< minEnd
; i
++)
2935 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2936 texcoords
[i
], rgba
[i
]);
2939 for (i
= minStart
; i
< minEnd
; i
++)
2940 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2941 texcoords
[i
], rgba
[i
]);
2943 case GL_NEAREST_MIPMAP_NEAREST
:
2944 sample_2d_array_nearest_mipmap_nearest(ctx
, tObj
, m
,
2945 texcoords
+ minStart
,
2949 case GL_LINEAR_MIPMAP_NEAREST
:
2950 sample_2d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2951 texcoords
+ minStart
,
2955 case GL_NEAREST_MIPMAP_LINEAR
:
2956 sample_2d_array_nearest_mipmap_linear(ctx
, tObj
, m
,
2957 texcoords
+ minStart
,
2961 case GL_LINEAR_MIPMAP_LINEAR
:
2962 sample_2d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2963 texcoords
+ minStart
,
2968 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
2973 if (magStart
< magEnd
) {
2974 /* do the magnified texels */
2975 switch (tObj
->Sampler
.MagFilter
) {
2977 for (i
= magStart
; i
< magEnd
; i
++)
2978 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2979 texcoords
[i
], rgba
[i
]);
2982 for (i
= magStart
; i
< magEnd
; i
++)
2983 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2984 texcoords
[i
], rgba
[i
]);
2987 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
2996 /**********************************************************************/
2997 /* 1D Texture Array Sampling Functions */
2998 /**********************************************************************/
3001 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
3004 sample_1d_array_nearest(struct gl_context
*ctx
,
3005 const struct gl_texture_object
*tObj
,
3006 const struct gl_texture_image
*img
,
3007 const GLfloat texcoord
[4],
3010 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3011 const GLint width
= img
->Width2
; /* without border, power of two */
3012 const GLint height
= img
->Height
;
3017 i
= nearest_texel_location(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0]);
3018 array
= tex_array_slice(texcoord
[1], height
);
3020 if (i
< 0 || i
>= (GLint
) img
->Width
||
3021 array
< 0 || array
>= (GLint
) img
->Height
) {
3022 /* Need this test for GL_CLAMP_TO_BORDER mode */
3023 get_border_color(tObj
, img
, rgba
);
3026 swImg
->FetchTexelf(swImg
, i
, array
, 0, rgba
);
3032 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
3035 sample_1d_array_linear(struct gl_context
*ctx
,
3036 const struct gl_texture_object
*tObj
,
3037 const struct gl_texture_image
*img
,
3038 const GLfloat texcoord
[4],
3041 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3042 const GLint width
= img
->Width2
;
3043 const GLint height
= img
->Height
;
3046 GLbitfield useBorderColor
= 0x0;
3048 GLfloat t0
[4], t1
[4];
3050 linear_texel_locations(tObj
->Sampler
.WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
3051 array
= tex_array_slice(texcoord
[1], height
);
3058 /* check if sampling texture border color */
3059 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
3060 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
3063 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
3066 if (useBorderColor
& (I0BIT
| K0BIT
)) {
3067 get_border_color(tObj
, img
, t0
);
3070 swImg
->FetchTexelf(swImg
, i0
, array
, 0, t0
);
3072 if (useBorderColor
& (I1BIT
| K0BIT
)) {
3073 get_border_color(tObj
, img
, t1
);
3076 swImg
->FetchTexelf(swImg
, i1
, array
, 0, t1
);
3079 /* bilinear interpolation of samples */
3080 lerp_rgba(rgba
, a
, t0
, t1
);
3085 sample_1d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
3086 const struct gl_texture_object
*tObj
,
3087 GLuint n
, const GLfloat texcoord
[][4],
3088 const GLfloat lambda
[], GLfloat rgba
[][4])
3091 for (i
= 0; i
< n
; i
++) {
3092 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3093 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
3100 sample_1d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
3101 const struct gl_texture_object
*tObj
,
3102 GLuint n
, const GLfloat texcoord
[][4],
3103 const GLfloat lambda
[], GLfloat rgba
[][4])
3106 ASSERT(lambda
!= NULL
);
3107 for (i
= 0; i
< n
; i
++) {
3108 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3109 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
3110 texcoord
[i
], rgba
[i
]);
3116 sample_1d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
3117 const struct gl_texture_object
*tObj
,
3118 GLuint n
, const GLfloat texcoord
[][4],
3119 const GLfloat lambda
[], GLfloat rgba
[][4])
3122 ASSERT(lambda
!= NULL
);
3123 for (i
= 0; i
< n
; i
++) {
3124 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3125 if (level
>= tObj
->_MaxLevel
) {
3126 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
3127 texcoord
[i
], rgba
[i
]);
3130 GLfloat t0
[4], t1
[4]; /* texels */
3131 const GLfloat f
= FRAC(lambda
[i
]);
3132 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3133 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3134 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3141 sample_1d_array_linear_mipmap_linear(struct gl_context
*ctx
,
3142 const struct gl_texture_object
*tObj
,
3143 GLuint n
, const GLfloat texcoord
[][4],
3144 const GLfloat lambda
[], GLfloat rgba
[][4])
3147 ASSERT(lambda
!= NULL
);
3148 for (i
= 0; i
< n
; i
++) {
3149 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3150 if (level
>= tObj
->_MaxLevel
) {
3151 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
3152 texcoord
[i
], rgba
[i
]);
3155 GLfloat t0
[4], t1
[4]; /* texels */
3156 const GLfloat f
= FRAC(lambda
[i
]);
3157 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3158 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3159 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3165 /** Sample 1D Array texture, nearest filtering for both min/magnification */
3167 sample_nearest_1d_array(struct gl_context
*ctx
,
3168 const struct gl_texture_object
*tObj
, GLuint n
,
3169 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3173 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3175 for (i
= 0; i
< n
; i
++) {
3176 sample_1d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
3181 /** Sample 1D Array texture, linear filtering for both min/magnification */
3183 sample_linear_1d_array(struct gl_context
*ctx
,
3184 const struct gl_texture_object
*tObj
, GLuint n
,
3185 const GLfloat texcoords
[][4],
3186 const GLfloat lambda
[], GLfloat rgba
[][4])
3189 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3191 for (i
= 0; i
< n
; i
++) {
3192 sample_1d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
3197 /** Sample 1D Array texture, using lambda to choose between min/magnification */
3199 sample_lambda_1d_array(struct gl_context
*ctx
,
3200 const struct gl_texture_object
*tObj
, GLuint n
,
3201 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3204 GLuint minStart
, minEnd
; /* texels with minification */
3205 GLuint magStart
, magEnd
; /* texels with magnification */
3208 ASSERT(lambda
!= NULL
);
3209 compute_min_mag_ranges(tObj
, n
, lambda
,
3210 &minStart
, &minEnd
, &magStart
, &magEnd
);
3212 if (minStart
< minEnd
) {
3213 /* do the minified texels */
3214 GLuint m
= minEnd
- minStart
;
3215 switch (tObj
->Sampler
.MinFilter
) {
3217 for (i
= minStart
; i
< minEnd
; i
++)
3218 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3219 texcoords
[i
], rgba
[i
]);
3222 for (i
= minStart
; i
< minEnd
; i
++)
3223 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3224 texcoords
[i
], rgba
[i
]);
3226 case GL_NEAREST_MIPMAP_NEAREST
:
3227 sample_1d_array_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
3228 lambda
+ minStart
, rgba
+ minStart
);
3230 case GL_LINEAR_MIPMAP_NEAREST
:
3231 sample_1d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
3232 texcoords
+ minStart
,
3236 case GL_NEAREST_MIPMAP_LINEAR
:
3237 sample_1d_array_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
3238 lambda
+ minStart
, rgba
+ minStart
);
3240 case GL_LINEAR_MIPMAP_LINEAR
:
3241 sample_1d_array_linear_mipmap_linear(ctx
, tObj
, m
,
3242 texcoords
+ minStart
,
3247 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
3252 if (magStart
< magEnd
) {
3253 /* do the magnified texels */
3254 switch (tObj
->Sampler
.MagFilter
) {
3256 for (i
= magStart
; i
< magEnd
; i
++)
3257 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3258 texcoords
[i
], rgba
[i
]);
3261 for (i
= magStart
; i
< magEnd
; i
++)
3262 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
3263 texcoords
[i
], rgba
[i
]);
3266 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
3274 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
3276 static INLINE GLfloat
3277 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
,
3282 return (coord
<= depthSample
) ? 1.0F
: ambient
;
3284 return (coord
>= depthSample
) ? 1.0F
: ambient
;
3286 return (coord
< depthSample
) ? 1.0F
: ambient
;
3288 return (coord
> depthSample
) ? 1.0F
: ambient
;
3290 return (coord
== depthSample
) ? 1.0F
: ambient
;
3292 return (coord
!= depthSample
) ? 1.0F
: ambient
;
3300 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
3307 * Compare texcoord against four depth samples.
3309 static INLINE GLfloat
3310 shadow_compare4(GLenum function
, GLfloat coord
,
3311 GLfloat depth00
, GLfloat depth01
,
3312 GLfloat depth10
, GLfloat depth11
,
3313 GLfloat ambient
, GLfloat wi
, GLfloat wj
)
3315 const GLfloat d
= (1.0F
- (GLfloat
) ambient
) * 0.25F
;
3316 GLfloat luminance
= 1.0F
;
3320 if (coord
> depth00
) luminance
-= d
;
3321 if (coord
> depth01
) luminance
-= d
;
3322 if (coord
> depth10
) luminance
-= d
;
3323 if (coord
> depth11
) luminance
-= d
;
3326 if (coord
< depth00
) luminance
-= d
;
3327 if (coord
< depth01
) luminance
-= d
;
3328 if (coord
< depth10
) luminance
-= d
;
3329 if (coord
< depth11
) luminance
-= d
;
3332 if (coord
>= depth00
) luminance
-= d
;
3333 if (coord
>= depth01
) luminance
-= d
;
3334 if (coord
>= depth10
) luminance
-= d
;
3335 if (coord
>= depth11
) luminance
-= d
;
3338 if (coord
<= depth00
) luminance
-= d
;
3339 if (coord
<= depth01
) luminance
-= d
;
3340 if (coord
<= depth10
) luminance
-= d
;
3341 if (coord
<= depth11
) luminance
-= d
;
3344 if (coord
!= depth00
) luminance
-= d
;
3345 if (coord
!= depth01
) luminance
-= d
;
3346 if (coord
!= depth10
) luminance
-= d
;
3347 if (coord
!= depth11
) luminance
-= d
;
3350 if (coord
== depth00
) luminance
-= d
;
3351 if (coord
== depth01
) luminance
-= d
;
3352 if (coord
== depth10
) luminance
-= d
;
3353 if (coord
== depth11
) luminance
-= d
;
3360 /* ordinary bilinear filtering */
3361 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
3363 _mesa_problem(NULL
, "Bad compare func in sample_compare4");
3370 * Choose the mipmap level to use when sampling from a depth texture.
3373 choose_depth_texture_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
3377 if (tObj
->Sampler
.MinFilter
== GL_NEAREST
|| tObj
->Sampler
.MinFilter
== GL_LINEAR
) {
3378 /* no mipmapping - use base level */
3379 level
= tObj
->BaseLevel
;
3382 /* choose mipmap level */
3383 lambda
= CLAMP(lambda
, tObj
->Sampler
.MinLod
, tObj
->Sampler
.MaxLod
);
3384 level
= (GLint
) lambda
;
3385 level
= CLAMP(level
, tObj
->BaseLevel
, tObj
->_MaxLevel
);
3393 * Sample a shadow/depth texture. This function is incomplete. It doesn't
3394 * check for minification vs. magnification, etc.
3397 sample_depth_texture( struct gl_context
*ctx
,
3398 const struct gl_texture_object
*tObj
, GLuint n
,
3399 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3400 GLfloat texel
[][4] )
3402 const GLint level
= choose_depth_texture_level(tObj
, lambda
[0]);
3403 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
3404 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3405 const GLint width
= img
->Width
;
3406 const GLint height
= img
->Height
;
3407 const GLint depth
= img
->Depth
;
3408 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
3414 ASSERT(img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
3415 img
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
3417 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
3418 tObj
->Target
== GL_TEXTURE_2D
||
3419 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
3420 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
3421 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
);
3423 ambient
= tObj
->Sampler
.CompareFailValue
;
3425 /* XXXX if tObj->Sampler.MinFilter != tObj->Sampler.MagFilter, we're ignoring lambda */
3427 function
= (tObj
->Sampler
.CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
3428 tObj
->Sampler
.CompareFunc
: GL_NONE
;
3430 if (tObj
->Sampler
.MagFilter
== GL_NEAREST
) {
3432 for (i
= 0; i
< n
; i
++) {
3433 GLfloat depthSample
, depthRef
;
3434 GLint col
, row
, slice
;
3436 nearest_texcoord(tObj
, level
, texcoords
[i
], &col
, &row
, &slice
);
3438 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3439 slice
>= 0 && slice
< depth
) {
3440 swImg
->FetchTexelf(swImg
, col
, row
, slice
, &depthSample
);
3443 depthSample
= tObj
->Sampler
.BorderColor
.f
[0];
3446 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3448 result
= shadow_compare(function
, depthRef
, depthSample
, ambient
);
3450 switch (tObj
->Sampler
.DepthMode
) {
3452 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3455 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3458 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3461 ASSIGN_4V(texel
[i
], result
, 0.0F
, 0.0F
, 1.0F
);
3464 _mesa_problem(ctx
, "Bad depth texture mode");
3470 ASSERT(tObj
->Sampler
.MagFilter
== GL_LINEAR
);
3471 for (i
= 0; i
< n
; i
++) {
3472 GLfloat depth00
, depth01
, depth10
, depth11
, depthRef
;
3473 GLint i0
, i1
, j0
, j1
;
3476 GLuint useBorderTexel
;
3478 linear_texcoord(tObj
, level
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3485 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3491 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3492 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3493 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3494 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3497 if (slice
< 0 || slice
>= (GLint
) depth
) {
3498 depth00
= tObj
->Sampler
.BorderColor
.f
[0];
3499 depth01
= tObj
->Sampler
.BorderColor
.f
[0];
3500 depth10
= tObj
->Sampler
.BorderColor
.f
[0];
3501 depth11
= tObj
->Sampler
.BorderColor
.f
[0];
3504 /* get four depth samples from the texture */
3505 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3506 depth00
= tObj
->Sampler
.BorderColor
.f
[0];
3509 swImg
->FetchTexelf(swImg
, i0
, j0
, slice
, &depth00
);
3511 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3512 depth10
= tObj
->Sampler
.BorderColor
.f
[0];
3515 swImg
->FetchTexelf(swImg
, i1
, j0
, slice
, &depth10
);
3518 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3519 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3520 depth01
= tObj
->Sampler
.BorderColor
.f
[0];
3523 swImg
->FetchTexelf(swImg
, i0
, j1
, slice
, &depth01
);
3525 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3526 depth11
= tObj
->Sampler
.BorderColor
.f
[0];
3529 swImg
->FetchTexelf(swImg
, i1
, j1
, slice
, &depth11
);
3538 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3540 result
= shadow_compare4(function
, depthRef
,
3541 depth00
, depth01
, depth10
, depth11
,
3544 switch (tObj
->Sampler
.DepthMode
) {
3546 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3549 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3552 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3555 _mesa_problem(ctx
, "Bad depth texture mode");
3564 * We use this function when a texture object is in an "incomplete" state.
3565 * When a fragment program attempts to sample an incomplete texture we
3566 * return black (see issue 23 in GL_ARB_fragment_program spec).
3567 * Note: fragment programs don't observe the texture enable/disable flags.
3570 null_sample_func( struct gl_context
*ctx
,
3571 const struct gl_texture_object
*tObj
, GLuint n
,
3572 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3580 for (i
= 0; i
< n
; i
++) {
3584 rgba
[i
][ACOMP
] = 1.0;
3590 * Choose the texture sampling function for the given texture object.
3593 _swrast_choose_texture_sample_func( struct gl_context
*ctx
,
3594 const struct gl_texture_object
*t
)
3596 if (!t
|| !t
->_Complete
) {
3597 return &null_sample_func
;
3600 const GLboolean needLambda
=
3601 (GLboolean
) (t
->Sampler
.MinFilter
!= t
->Sampler
.MagFilter
);
3602 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->_BaseFormat
;
3604 switch (t
->Target
) {
3606 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3607 return &sample_depth_texture
;
3609 else if (needLambda
) {
3610 return &sample_lambda_1d
;
3612 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3613 return &sample_linear_1d
;
3616 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3617 return &sample_nearest_1d
;
3620 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3621 return &sample_depth_texture
;
3623 else if (needLambda
) {
3624 /* Anisotropic filtering extension. Activated only if mipmaps are used */
3625 if (t
->Sampler
.MaxAnisotropy
> 1.0 &&
3626 t
->Sampler
.MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
3627 return &sample_lambda_2d_aniso
;
3629 return &sample_lambda_2d
;
3631 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3632 return &sample_linear_2d
;
3635 /* check for a few optimized cases */
3636 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3637 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3638 if (t
->Sampler
.WrapS
== GL_REPEAT
&&
3639 t
->Sampler
.WrapT
== GL_REPEAT
&&
3640 img
->_IsPowerOfTwo
&&
3642 img
->TexFormat
== MESA_FORMAT_RGB888
) {
3643 return &opt_sample_rgb_2d
;
3645 else if (t
->Sampler
.WrapS
== GL_REPEAT
&&
3646 t
->Sampler
.WrapT
== GL_REPEAT
&&
3647 img
->_IsPowerOfTwo
&&
3649 img
->TexFormat
== MESA_FORMAT_RGBA8888
) {
3650 return &opt_sample_rgba_2d
;
3653 return &sample_nearest_2d
;
3658 return &sample_lambda_3d
;
3660 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3661 return &sample_linear_3d
;
3664 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3665 return &sample_nearest_3d
;
3667 case GL_TEXTURE_CUBE_MAP
:
3669 return &sample_lambda_cube
;
3671 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3672 return &sample_linear_cube
;
3675 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3676 return &sample_nearest_cube
;
3678 case GL_TEXTURE_RECTANGLE_NV
:
3679 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3680 return &sample_depth_texture
;
3682 else if (needLambda
) {
3683 return &sample_lambda_rect
;
3685 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3686 return &sample_linear_rect
;
3689 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3690 return &sample_nearest_rect
;
3692 case GL_TEXTURE_1D_ARRAY_EXT
:
3694 return &sample_lambda_1d_array
;
3696 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3697 return &sample_linear_1d_array
;
3700 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3701 return &sample_nearest_1d_array
;
3703 case GL_TEXTURE_2D_ARRAY_EXT
:
3705 return &sample_lambda_2d_array
;
3707 else if (t
->Sampler
.MinFilter
== GL_LINEAR
) {
3708 return &sample_linear_2d_array
;
3711 ASSERT(t
->Sampler
.MinFilter
== GL_NEAREST
);
3712 return &sample_nearest_2d_array
;
3716 "invalid target in _swrast_choose_texture_sample_func");
3717 return &null_sample_func
;