2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "main/glheader.h"
28 #include "main/context.h"
30 #include "main/macros.h"
31 #include "main/samplerobj.h"
32 #include "main/teximage.h"
33 #include "main/texobj.h"
35 #include "s_context.h"
36 #include "s_texfilter.h"
40 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
41 * see 1-pixel bands of improperly weighted linear-filtered textures.
42 * The tests/texwrap.c demo is a good test.
43 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
44 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
46 #define FRAC(f) ((f) - util_ifloor(f))
51 * Linear interpolation macro
53 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
57 * Do 2D/biliner interpolation of float values.
58 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
59 * a and b are the horizontal and vertical interpolants.
60 * It's important that this function is inlined when compiled with
61 * optimization! If we find that's not true on some systems, convert
65 lerp_2d(GLfloat a
, GLfloat b
,
66 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
68 const GLfloat temp0
= LERP(a
, v00
, v10
);
69 const GLfloat temp1
= LERP(a
, v01
, v11
);
70 return LERP(b
, temp0
, temp1
);
75 * Do 3D/trilinear interpolation of float values.
79 lerp_3d(GLfloat a
, GLfloat b
, GLfloat c
,
80 GLfloat v000
, GLfloat v100
, GLfloat v010
, GLfloat v110
,
81 GLfloat v001
, GLfloat v101
, GLfloat v011
, GLfloat v111
)
83 const GLfloat temp00
= LERP(a
, v000
, v100
);
84 const GLfloat temp10
= LERP(a
, v010
, v110
);
85 const GLfloat temp01
= LERP(a
, v001
, v101
);
86 const GLfloat temp11
= LERP(a
, v011
, v111
);
87 const GLfloat temp0
= LERP(b
, temp00
, temp10
);
88 const GLfloat temp1
= LERP(b
, temp01
, temp11
);
89 return LERP(c
, temp0
, temp1
);
94 * Do linear interpolation of colors.
97 lerp_rgba(GLfloat result
[4], GLfloat t
, const GLfloat a
[4], const GLfloat b
[4])
99 result
[0] = LERP(t
, a
[0], b
[0]);
100 result
[1] = LERP(t
, a
[1], b
[1]);
101 result
[2] = LERP(t
, a
[2], b
[2]);
102 result
[3] = LERP(t
, a
[3], b
[3]);
107 * Do bilinear interpolation of colors.
110 lerp_rgba_2d(GLfloat result
[4], GLfloat a
, GLfloat b
,
111 const GLfloat t00
[4], const GLfloat t10
[4],
112 const GLfloat t01
[4], const GLfloat t11
[4])
114 result
[0] = lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]);
115 result
[1] = lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]);
116 result
[2] = lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]);
117 result
[3] = lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]);
122 * Do trilinear interpolation of colors.
125 lerp_rgba_3d(GLfloat result
[4], GLfloat a
, GLfloat b
, GLfloat c
,
126 const GLfloat t000
[4], const GLfloat t100
[4],
127 const GLfloat t010
[4], const GLfloat t110
[4],
128 const GLfloat t001
[4], const GLfloat t101
[4],
129 const GLfloat t011
[4], const GLfloat t111
[4])
132 /* compiler should unroll these short loops */
133 for (k
= 0; k
< 4; k
++) {
134 result
[k
] = lerp_3d(a
, b
, c
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
135 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
141 * Used for GL_REPEAT wrap mode. Using A % B doesn't produce the
142 * right results for A<0. Casting to A to be unsigned only works if B
143 * is a power of two. Adding a bias to A (which is a multiple of B)
144 * avoids the problems with A < 0 (for reasonable A) without using a
147 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
151 * Used to compute texel locations for linear sampling.
153 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
154 * s = texcoord in [0,1]
155 * size = width (or height or depth) of texture
157 * i0, i1 = returns two nearest texel indexes
158 * weight = returns blend factor between texels
161 linear_texel_locations(GLenum wrapMode
,
162 const struct gl_texture_image
*img
,
163 GLint size
, GLfloat s
,
164 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
166 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
171 if (swImg
->_IsPowerOfTwo
) {
172 *i0
= util_ifloor(u
) & (size
- 1);
173 *i1
= (*i0
+ 1) & (size
- 1);
176 *i0
= REMAINDER(util_ifloor(u
), size
);
177 *i1
= REMAINDER(*i0
+ 1, size
);
180 case GL_CLAMP_TO_EDGE
:
188 *i0
= util_ifloor(u
);
192 if (*i1
>= (GLint
) size
)
195 case GL_CLAMP_TO_BORDER
:
197 const GLfloat min
= -1.0F
/ (2.0F
* size
);
198 const GLfloat max
= 1.0F
- min
;
206 *i0
= util_ifloor(u
);
210 case GL_MIRRORED_REPEAT
:
212 const GLint flr
= util_ifloor(s
);
214 u
= 1.0F
- (s
- (GLfloat
) flr
);
216 u
= s
- (GLfloat
) flr
;
217 u
= (u
* size
) - 0.5F
;
218 *i0
= util_ifloor(u
);
222 if (*i1
>= (GLint
) size
)
226 case GL_MIRROR_CLAMP_EXT
:
233 *i0
= util_ifloor(u
);
236 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
243 *i0
= util_ifloor(u
);
247 if (*i1
>= (GLint
) size
)
250 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
252 const GLfloat min
= -1.0F
/ (2.0F
* size
);
253 const GLfloat max
= 1.0F
- min
;
262 *i0
= util_ifloor(u
);
274 *i0
= util_ifloor(u
);
278 _mesa_problem(NULL
, "Bad wrap mode");
288 * Used to compute texel location for nearest sampling.
291 nearest_texel_location(GLenum wrapMode
,
292 const struct gl_texture_image
*img
,
293 GLint size
, GLfloat s
)
295 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
300 /* s limited to [0,1) */
301 /* i limited to [0,size-1] */
302 i
= util_ifloor(s
* size
);
303 if (swImg
->_IsPowerOfTwo
)
306 i
= REMAINDER(i
, size
);
308 case GL_CLAMP_TO_EDGE
:
310 /* s limited to [min,max] */
311 /* i limited to [0, size-1] */
312 const GLfloat min
= 1.0F
/ (2.0F
* size
);
313 const GLfloat max
= 1.0F
- min
;
319 i
= util_ifloor(s
* size
);
322 case GL_CLAMP_TO_BORDER
:
324 /* s limited to [min,max] */
325 /* i limited to [-1, size] */
326 const GLfloat min
= -1.0F
/ (2.0F
* size
);
327 const GLfloat max
= 1.0F
- min
;
333 i
= util_ifloor(s
* size
);
336 case GL_MIRRORED_REPEAT
:
338 const GLfloat min
= 1.0F
/ (2.0F
* size
);
339 const GLfloat max
= 1.0F
- min
;
340 const GLint flr
= util_ifloor(s
);
343 u
= 1.0F
- (s
- (GLfloat
) flr
);
345 u
= s
- (GLfloat
) flr
;
351 i
= util_ifloor(u
* size
);
354 case GL_MIRROR_CLAMP_EXT
:
356 /* s limited to [0,1] */
357 /* i limited to [0,size-1] */
358 const GLfloat u
= fabsf(s
);
364 i
= util_ifloor(u
* size
);
367 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
369 /* s limited to [min,max] */
370 /* i limited to [0, size-1] */
371 const GLfloat min
= 1.0F
/ (2.0F
* size
);
372 const GLfloat max
= 1.0F
- min
;
373 const GLfloat u
= fabsf(s
);
379 i
= util_ifloor(u
* size
);
382 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
384 /* s limited to [min,max] */
385 /* i limited to [0, size-1] */
386 const GLfloat min
= -1.0F
/ (2.0F
* size
);
387 const GLfloat max
= 1.0F
- min
;
388 const GLfloat u
= fabsf(s
);
394 i
= util_ifloor(u
* size
);
398 /* s limited to [0,1] */
399 /* i limited to [0,size-1] */
405 i
= util_ifloor(s
* size
);
408 _mesa_problem(NULL
, "Bad wrap mode");
414 /* Power of two image sizes only */
416 linear_repeat_texel_location(GLuint size
, GLfloat s
,
417 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
419 GLfloat u
= s
* size
- 0.5F
;
420 *i0
= util_ifloor(u
) & (size
- 1);
421 *i1
= (*i0
+ 1) & (size
- 1);
427 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
430 clamp_rect_coord_nearest(GLenum wrapMode
, GLfloat coord
, GLint max
)
434 return util_ifloor( CLAMP(coord
, 0.0F
, max
- 1) );
435 case GL_CLAMP_TO_EDGE
:
436 return util_ifloor( CLAMP(coord
, 0.5F
, max
- 0.5F
) );
437 case GL_CLAMP_TO_BORDER
:
438 return util_ifloor( CLAMP(coord
, -0.5F
, max
+ 0.5F
) );
440 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_nearest");
447 * As above, but GL_LINEAR filtering.
450 clamp_rect_coord_linear(GLenum wrapMode
, GLfloat coord
, GLint max
,
451 GLint
*i0out
, GLint
*i1out
, GLfloat
*weight
)
457 /* Not exactly what the spec says, but it matches NVIDIA output */
458 fcol
= CLAMP(coord
- 0.5F
, 0.0F
, max
- 1);
459 i0
= util_ifloor(fcol
);
462 case GL_CLAMP_TO_EDGE
:
463 fcol
= CLAMP(coord
, 0.5F
, max
- 0.5F
);
465 i0
= util_ifloor(fcol
);
470 case GL_CLAMP_TO_BORDER
:
471 fcol
= CLAMP(coord
, -0.5F
, max
+ 0.5F
);
473 i0
= util_ifloor(fcol
);
477 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_linear");
484 *weight
= FRAC(fcol
);
489 * Compute slice/image to use for 1D or 2D array texture.
492 tex_array_slice(GLfloat coord
, GLsizei size
)
494 GLint slice
= util_ifloor(coord
+ 0.5f
);
495 slice
= CLAMP(slice
, 0, size
- 1);
501 * Compute nearest integer texcoords for given texobj and coordinate.
502 * NOTE: only used for depth texture sampling.
505 nearest_texcoord(const struct gl_sampler_object
*samp
,
506 const struct gl_texture_object
*texObj
,
508 const GLfloat texcoord
[4],
509 GLint
*i
, GLint
*j
, GLint
*k
)
511 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
512 const GLint width
= img
->Width
;
513 const GLint height
= img
->Height
;
514 const GLint depth
= img
->Depth
;
516 switch (texObj
->Target
) {
517 case GL_TEXTURE_RECTANGLE_ARB
:
518 *i
= clamp_rect_coord_nearest(samp
->WrapS
, texcoord
[0], width
);
519 *j
= clamp_rect_coord_nearest(samp
->WrapT
, texcoord
[1], height
);
523 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
528 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
529 *j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
532 case GL_TEXTURE_1D_ARRAY_EXT
:
533 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
534 *j
= tex_array_slice(texcoord
[1], height
);
537 case GL_TEXTURE_2D_ARRAY_EXT
:
538 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
539 *j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
540 *k
= tex_array_slice(texcoord
[2], depth
);
550 * Compute linear integer texcoords for given texobj and coordinate.
551 * NOTE: only used for depth texture sampling.
554 linear_texcoord(const struct gl_sampler_object
*samp
,
555 const struct gl_texture_object
*texObj
,
557 const GLfloat texcoord
[4],
558 GLint
*i0
, GLint
*i1
, GLint
*j0
, GLint
*j1
, GLint
*slice
,
559 GLfloat
*wi
, GLfloat
*wj
)
561 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
562 const GLint width
= img
->Width
;
563 const GLint height
= img
->Height
;
564 const GLint depth
= img
->Depth
;
566 switch (texObj
->Target
) {
567 case GL_TEXTURE_RECTANGLE_ARB
:
568 clamp_rect_coord_linear(samp
->WrapS
, texcoord
[0],
570 clamp_rect_coord_linear(samp
->WrapT
, texcoord
[1],
577 linear_texel_locations(samp
->WrapS
, img
, width
,
578 texcoord
[0], i0
, i1
, wi
);
579 linear_texel_locations(samp
->WrapT
, img
, height
,
580 texcoord
[1], j0
, j1
, wj
);
584 case GL_TEXTURE_1D_ARRAY_EXT
:
585 linear_texel_locations(samp
->WrapS
, img
, width
,
586 texcoord
[0], i0
, i1
, wi
);
587 *j0
= tex_array_slice(texcoord
[1], height
);
592 case GL_TEXTURE_2D_ARRAY_EXT
:
593 linear_texel_locations(samp
->WrapS
, img
, width
,
594 texcoord
[0], i0
, i1
, wi
);
595 linear_texel_locations(samp
->WrapT
, img
, height
,
596 texcoord
[1], j0
, j1
, wj
);
597 *slice
= tex_array_slice(texcoord
[2], depth
);
609 * For linear interpolation between mipmap levels N and N+1, this function
613 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
616 return tObj
->BaseLevel
;
617 else if (lambda
> tObj
->_MaxLambda
)
618 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
620 return (GLint
) (tObj
->BaseLevel
+ lambda
);
625 * Compute the nearest mipmap level to take texels from.
628 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
634 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
635 l
= tObj
->_MaxLambda
+ 0.4999F
;
638 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
639 if (level
> tObj
->_MaxLevel
)
640 level
= tObj
->_MaxLevel
;
647 * Bitflags for texture border color sampling.
659 * The lambda[] array values are always monotonic. Either the whole span
660 * will be minified, magnified, or split between the two. This function
661 * determines the subranges in [0, n-1] that are to be minified or magnified.
664 compute_min_mag_ranges(const struct gl_sampler_object
*samp
,
665 GLuint n
, const GLfloat lambda
[],
666 GLuint
*minStart
, GLuint
*minEnd
,
667 GLuint
*magStart
, GLuint
*magEnd
)
669 GLfloat minMagThresh
;
671 /* we shouldn't be here if minfilter == magfilter */
672 assert(samp
->MinFilter
!= samp
->MagFilter
);
674 /* This bit comes from the OpenGL spec: */
675 if (samp
->MagFilter
== GL_LINEAR
676 && (samp
->MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
677 samp
->MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
685 /* DEBUG CODE: Verify that lambda[] is monotonic.
686 * We can't really use this because the inaccuracy in the LOG2 function
687 * causes this test to fail, yet the resulting texturing is correct.
691 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
692 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
693 for (i
= 0; i
< n
- 1; i
++) {
694 assert((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
697 else { /* increasing */
698 for (i
= 0; i
< n
- 1; i
++) {
699 assert((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
705 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
706 /* magnification for whole span */
709 *minStart
= *minEnd
= 0;
711 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
712 /* minification for whole span */
715 *magStart
= *magEnd
= 0;
718 /* a mix of minification and magnification */
720 if (lambda
[0] > minMagThresh
) {
721 /* start with minification */
722 for (i
= 1; i
< n
; i
++) {
723 if (lambda
[i
] <= minMagThresh
)
732 /* start with magnification */
733 for (i
= 1; i
< n
; i
++) {
734 if (lambda
[i
] > minMagThresh
)
745 /* Verify the min/mag Start/End values
746 * We don't use this either (see above)
750 for (i
= 0; i
< n
; i
++) {
751 if (lambda
[i
] > minMagThresh
) {
753 assert(i
>= *minStart
);
758 assert(i
>= *magStart
);
768 * When we sample the border color, it must be interpreted according to
769 * the base texture format. Ex: if the texture base format it GL_ALPHA,
770 * we return (0,0,0,BorderAlpha).
773 get_border_color(const struct gl_sampler_object
*samp
,
774 const struct gl_texture_image
*img
,
777 switch (img
->_BaseFormat
) {
779 rgba
[0] = samp
->BorderColor
.f
[0];
780 rgba
[1] = samp
->BorderColor
.f
[1];
781 rgba
[2] = samp
->BorderColor
.f
[2];
785 rgba
[0] = rgba
[1] = rgba
[2] = 0.0;
786 rgba
[3] = samp
->BorderColor
.f
[3];
789 rgba
[0] = rgba
[1] = rgba
[2] = samp
->BorderColor
.f
[0];
792 case GL_LUMINANCE_ALPHA
:
793 rgba
[0] = rgba
[1] = rgba
[2] = samp
->BorderColor
.f
[0];
794 rgba
[3] = samp
->BorderColor
.f
[3];
797 rgba
[0] = rgba
[1] = rgba
[2] = rgba
[3] = samp
->BorderColor
.f
[0];
800 COPY_4V(rgba
, samp
->BorderColor
.f
);
807 * Put z into texel according to GL_DEPTH_MODE.
810 apply_depth_mode(GLenum depthMode
, GLfloat z
, GLfloat texel
[4])
814 ASSIGN_4V(texel
, z
, z
, z
, 1.0F
);
817 ASSIGN_4V(texel
, z
, z
, z
, z
);
820 ASSIGN_4V(texel
, 0.0F
, 0.0F
, 0.0F
, z
);
823 ASSIGN_4V(texel
, z
, 0.0F
, 0.0F
, 1.0F
);
826 _mesa_problem(NULL
, "Bad depth texture mode");
832 * Is the given texture a depth (or depth/stencil) texture?
835 is_depth_texture(const struct gl_texture_object
*tObj
)
837 GLenum format
= _mesa_texture_base_format(tObj
);
838 return format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
;
842 /**********************************************************************/
843 /* 1-D Texture Sampling Functions */
844 /**********************************************************************/
847 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
850 sample_1d_nearest(struct gl_context
*ctx
,
851 const struct gl_sampler_object
*samp
,
852 const struct gl_texture_image
*img
,
853 const GLfloat texcoord
[4], GLfloat rgba
[4])
855 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
856 const GLint width
= img
->Width2
; /* without border, power of two */
858 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
859 /* skip over the border, if any */
861 if (i
< 0 || i
>= (GLint
) img
->Width
) {
862 /* Need this test for GL_CLAMP_TO_BORDER mode */
863 get_border_color(samp
, img
, rgba
);
866 swImg
->FetchTexel(swImg
, i
, 0, 0, rgba
);
872 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
875 sample_1d_linear(struct gl_context
*ctx
,
876 const struct gl_sampler_object
*samp
,
877 const struct gl_texture_image
*img
,
878 const GLfloat texcoord
[4], GLfloat rgba
[4])
880 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
881 const GLint width
= img
->Width2
;
883 GLbitfield useBorderColor
= 0x0;
885 GLfloat t0
[4], t1
[4]; /* texels */
887 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
894 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
895 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
898 /* fetch texel colors */
899 if (useBorderColor
& I0BIT
) {
900 get_border_color(samp
, img
, t0
);
903 swImg
->FetchTexel(swImg
, i0
, 0, 0, t0
);
905 if (useBorderColor
& I1BIT
) {
906 get_border_color(samp
, img
, t1
);
909 swImg
->FetchTexel(swImg
, i1
, 0, 0, t1
);
912 lerp_rgba(rgba
, a
, t0
, t1
);
917 sample_1d_nearest_mipmap_nearest(struct gl_context
*ctx
,
918 const struct gl_sampler_object
*samp
,
919 const struct gl_texture_object
*tObj
,
920 GLuint n
, const GLfloat texcoord
[][4],
921 const GLfloat lambda
[], GLfloat rgba
[][4])
924 assert(lambda
!= NULL
);
925 for (i
= 0; i
< n
; i
++) {
926 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
927 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
933 sample_1d_linear_mipmap_nearest(struct gl_context
*ctx
,
934 const struct gl_sampler_object
*samp
,
935 const struct gl_texture_object
*tObj
,
936 GLuint n
, const GLfloat texcoord
[][4],
937 const GLfloat lambda
[], GLfloat rgba
[][4])
940 assert(lambda
!= NULL
);
941 for (i
= 0; i
< n
; i
++) {
942 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
943 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
949 sample_1d_nearest_mipmap_linear(struct gl_context
*ctx
,
950 const struct gl_sampler_object
*samp
,
951 const struct gl_texture_object
*tObj
,
952 GLuint n
, const GLfloat texcoord
[][4],
953 const GLfloat lambda
[], GLfloat rgba
[][4])
956 assert(lambda
!= NULL
);
957 for (i
= 0; i
< n
; i
++) {
958 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
959 if (level
>= tObj
->_MaxLevel
) {
960 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
961 texcoord
[i
], rgba
[i
]);
964 GLfloat t0
[4], t1
[4];
965 const GLfloat f
= FRAC(lambda
[i
]);
966 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
967 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
968 lerp_rgba(rgba
[i
], f
, t0
, t1
);
975 sample_1d_linear_mipmap_linear(struct gl_context
*ctx
,
976 const struct gl_sampler_object
*samp
,
977 const struct gl_texture_object
*tObj
,
978 GLuint n
, const GLfloat texcoord
[][4],
979 const GLfloat lambda
[], GLfloat rgba
[][4])
982 assert(lambda
!= NULL
);
983 for (i
= 0; i
< n
; i
++) {
984 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
985 if (level
>= tObj
->_MaxLevel
) {
986 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
987 texcoord
[i
], rgba
[i
]);
990 GLfloat t0
[4], t1
[4];
991 const GLfloat f
= FRAC(lambda
[i
]);
992 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
993 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
994 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1000 /** Sample 1D texture, nearest filtering for both min/magnification */
1002 sample_nearest_1d( struct gl_context
*ctx
,
1003 const struct gl_sampler_object
*samp
,
1004 const struct gl_texture_object
*tObj
, GLuint n
,
1005 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1009 const struct gl_texture_image
*image
= _mesa_base_tex_image(tObj
);
1011 for (i
= 0; i
< n
; i
++) {
1012 sample_1d_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1017 /** Sample 1D texture, linear filtering for both min/magnification */
1019 sample_linear_1d( struct gl_context
*ctx
,
1020 const struct gl_sampler_object
*samp
,
1021 const struct gl_texture_object
*tObj
, GLuint n
,
1022 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1026 const struct gl_texture_image
*image
= _mesa_base_tex_image(tObj
);
1028 for (i
= 0; i
< n
; i
++) {
1029 sample_1d_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1034 /** Sample 1D texture, using lambda to choose between min/magnification */
1036 sample_lambda_1d( struct gl_context
*ctx
,
1037 const struct gl_sampler_object
*samp
,
1038 const struct gl_texture_object
*tObj
, GLuint n
,
1039 const GLfloat texcoords
[][4],
1040 const GLfloat lambda
[], GLfloat rgba
[][4] )
1042 GLuint minStart
, minEnd
; /* texels with minification */
1043 GLuint magStart
, magEnd
; /* texels with magnification */
1046 assert(lambda
!= NULL
);
1047 compute_min_mag_ranges(samp
, n
, lambda
,
1048 &minStart
, &minEnd
, &magStart
, &magEnd
);
1050 if (minStart
< minEnd
) {
1051 /* do the minified texels */
1052 const GLuint m
= minEnd
- minStart
;
1053 switch (samp
->MinFilter
) {
1055 for (i
= minStart
; i
< minEnd
; i
++)
1056 sample_1d_nearest(ctx
, samp
, _mesa_base_tex_image(tObj
),
1057 texcoords
[i
], rgba
[i
]);
1060 for (i
= minStart
; i
< minEnd
; i
++)
1061 sample_1d_linear(ctx
, samp
, _mesa_base_tex_image(tObj
),
1062 texcoords
[i
], rgba
[i
]);
1064 case GL_NEAREST_MIPMAP_NEAREST
:
1065 sample_1d_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1066 lambda
+ minStart
, rgba
+ minStart
);
1068 case GL_LINEAR_MIPMAP_NEAREST
:
1069 sample_1d_linear_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1070 lambda
+ minStart
, rgba
+ minStart
);
1072 case GL_NEAREST_MIPMAP_LINEAR
:
1073 sample_1d_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1074 lambda
+ minStart
, rgba
+ minStart
);
1076 case GL_LINEAR_MIPMAP_LINEAR
:
1077 sample_1d_linear_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1078 lambda
+ minStart
, rgba
+ minStart
);
1081 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
1086 if (magStart
< magEnd
) {
1087 /* do the magnified texels */
1088 switch (samp
->MagFilter
) {
1090 for (i
= magStart
; i
< magEnd
; i
++)
1091 sample_1d_nearest(ctx
, samp
, _mesa_base_tex_image(tObj
),
1092 texcoords
[i
], rgba
[i
]);
1095 for (i
= magStart
; i
< magEnd
; i
++)
1096 sample_1d_linear(ctx
, samp
, _mesa_base_tex_image(tObj
),
1097 texcoords
[i
], rgba
[i
]);
1100 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
1107 /**********************************************************************/
1108 /* 2-D Texture Sampling Functions */
1109 /**********************************************************************/
1113 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1116 sample_2d_nearest(struct gl_context
*ctx
,
1117 const struct gl_sampler_object
*samp
,
1118 const struct gl_texture_image
*img
,
1119 const GLfloat texcoord
[4],
1122 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1123 const GLint width
= img
->Width2
; /* without border, power of two */
1124 const GLint height
= img
->Height2
; /* without border, power of two */
1128 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
1129 j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
1131 /* skip over the border, if any */
1135 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
1136 /* Need this test for GL_CLAMP_TO_BORDER mode */
1137 get_border_color(samp
, img
, rgba
);
1140 swImg
->FetchTexel(swImg
, i
, j
, 0, rgba
);
1146 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1147 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1150 sample_2d_linear(struct gl_context
*ctx
,
1151 const struct gl_sampler_object
*samp
,
1152 const struct gl_texture_image
*img
,
1153 const GLfloat texcoord
[4],
1156 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1157 const GLint width
= img
->Width2
;
1158 const GLint height
= img
->Height2
;
1159 GLint i0
, j0
, i1
, j1
;
1160 GLbitfield useBorderColor
= 0x0;
1162 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1164 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1165 linear_texel_locations(samp
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1174 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1175 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1176 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1177 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1180 /* fetch four texel colors */
1181 if (useBorderColor
& (I0BIT
| J0BIT
)) {
1182 get_border_color(samp
, img
, t00
);
1185 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
1187 if (useBorderColor
& (I1BIT
| J0BIT
)) {
1188 get_border_color(samp
, img
, t10
);
1191 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
1193 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1194 get_border_color(samp
, img
, t01
);
1197 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
1199 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1200 get_border_color(samp
, img
, t11
);
1203 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
1206 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1211 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1212 * We don't have to worry about the texture border.
1215 sample_2d_linear_repeat(struct gl_context
*ctx
,
1216 const struct gl_sampler_object
*samp
,
1217 const struct gl_texture_image
*img
,
1218 const GLfloat texcoord
[4],
1221 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1222 const GLint width
= img
->Width2
;
1223 const GLint height
= img
->Height2
;
1224 GLint i0
, j0
, i1
, j1
;
1226 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1230 assert(samp
->WrapS
== GL_REPEAT
);
1231 assert(samp
->WrapT
== GL_REPEAT
);
1232 assert(img
->Border
== 0);
1233 assert(swImg
->_IsPowerOfTwo
);
1235 linear_repeat_texel_location(width
, texcoord
[0], &i0
, &i1
, &wi
);
1236 linear_repeat_texel_location(height
, texcoord
[1], &j0
, &j1
, &wj
);
1238 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
1239 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
1240 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
1241 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
1243 lerp_rgba_2d(rgba
, wi
, wj
, t00
, t10
, t01
, t11
);
1248 sample_2d_nearest_mipmap_nearest(struct gl_context
*ctx
,
1249 const struct gl_sampler_object
*samp
,
1250 const struct gl_texture_object
*tObj
,
1251 GLuint n
, const GLfloat texcoord
[][4],
1252 const GLfloat lambda
[], GLfloat rgba
[][4])
1255 for (i
= 0; i
< n
; i
++) {
1256 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1257 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1263 sample_2d_linear_mipmap_nearest(struct gl_context
*ctx
,
1264 const struct gl_sampler_object
*samp
,
1265 const struct gl_texture_object
*tObj
,
1266 GLuint n
, const GLfloat texcoord
[][4],
1267 const GLfloat lambda
[], GLfloat rgba
[][4])
1270 assert(lambda
!= NULL
);
1271 for (i
= 0; i
< n
; i
++) {
1272 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1273 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1279 sample_2d_nearest_mipmap_linear(struct gl_context
*ctx
,
1280 const struct gl_sampler_object
*samp
,
1281 const struct gl_texture_object
*tObj
,
1282 GLuint n
, const GLfloat texcoord
[][4],
1283 const GLfloat lambda
[], GLfloat rgba
[][4])
1286 assert(lambda
!= NULL
);
1287 for (i
= 0; i
< n
; i
++) {
1288 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1289 if (level
>= tObj
->_MaxLevel
) {
1290 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1291 texcoord
[i
], rgba
[i
]);
1294 GLfloat t0
[4], t1
[4]; /* texels */
1295 const GLfloat f
= FRAC(lambda
[i
]);
1296 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1297 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1298 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1305 sample_2d_linear_mipmap_linear( struct gl_context
*ctx
,
1306 const struct gl_sampler_object
*samp
,
1307 const struct gl_texture_object
*tObj
,
1308 GLuint n
, const GLfloat texcoord
[][4],
1309 const GLfloat lambda
[], GLfloat rgba
[][4] )
1312 assert(lambda
!= NULL
);
1313 for (i
= 0; i
< n
; i
++) {
1314 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1315 if (level
>= tObj
->_MaxLevel
) {
1316 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1317 texcoord
[i
], rgba
[i
]);
1320 GLfloat t0
[4], t1
[4]; /* texels */
1321 const GLfloat f
= FRAC(lambda
[i
]);
1322 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1323 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1324 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1331 sample_2d_linear_mipmap_linear_repeat(struct gl_context
*ctx
,
1332 const struct gl_sampler_object
*samp
,
1333 const struct gl_texture_object
*tObj
,
1334 GLuint n
, const GLfloat texcoord
[][4],
1335 const GLfloat lambda
[], GLfloat rgba
[][4])
1338 assert(lambda
!= NULL
);
1339 assert(samp
->WrapS
== GL_REPEAT
);
1340 assert(samp
->WrapT
== GL_REPEAT
);
1341 for (i
= 0; i
< n
; i
++) {
1342 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1343 if (level
>= tObj
->_MaxLevel
) {
1344 sample_2d_linear_repeat(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1345 texcoord
[i
], rgba
[i
]);
1348 GLfloat t0
[4], t1
[4]; /* texels */
1349 const GLfloat f
= FRAC(lambda
[i
]);
1350 sample_2d_linear_repeat(ctx
, samp
, tObj
->Image
[0][level
],
1352 sample_2d_linear_repeat(ctx
, samp
, tObj
->Image
[0][level
+1],
1354 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1360 /** Sample 2D texture, nearest filtering for both min/magnification */
1362 sample_nearest_2d(struct gl_context
*ctx
,
1363 const struct gl_sampler_object
*samp
,
1364 const struct gl_texture_object
*tObj
, GLuint n
,
1365 const GLfloat texcoords
[][4],
1366 const GLfloat lambda
[], GLfloat rgba
[][4])
1369 const struct gl_texture_image
*image
= _mesa_base_tex_image(tObj
);
1371 for (i
= 0; i
< n
; i
++) {
1372 sample_2d_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1377 /** Sample 2D texture, linear filtering for both min/magnification */
1379 sample_linear_2d(struct gl_context
*ctx
,
1380 const struct gl_sampler_object
*samp
,
1381 const struct gl_texture_object
*tObj
, GLuint n
,
1382 const GLfloat texcoords
[][4],
1383 const GLfloat lambda
[], GLfloat rgba
[][4])
1386 const struct gl_texture_image
*image
= _mesa_base_tex_image(tObj
);
1387 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(image
);
1389 if (samp
->WrapS
== GL_REPEAT
&&
1390 samp
->WrapT
== GL_REPEAT
&&
1391 swImg
->_IsPowerOfTwo
&&
1392 image
->Border
== 0) {
1393 for (i
= 0; i
< n
; i
++) {
1394 sample_2d_linear_repeat(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1398 for (i
= 0; i
< n
; i
++) {
1399 sample_2d_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1406 * Optimized 2-D texture sampling:
1407 * S and T wrap mode == GL_REPEAT
1408 * GL_NEAREST min/mag filter
1410 * RowStride == Width,
1414 opt_sample_rgb_2d(struct gl_context
*ctx
,
1415 const struct gl_sampler_object
*samp
,
1416 const struct gl_texture_object
*tObj
,
1417 GLuint n
, const GLfloat texcoords
[][4],
1418 const GLfloat lambda
[], GLfloat rgba
[][4])
1420 const struct gl_texture_image
*img
= _mesa_base_tex_image(tObj
);
1421 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1422 const GLfloat width
= (GLfloat
) img
->Width
;
1423 const GLfloat height
= (GLfloat
) img
->Height
;
1424 const GLint colMask
= img
->Width
- 1;
1425 const GLint rowMask
= img
->Height
- 1;
1426 const GLint shift
= img
->WidthLog2
;
1430 assert(samp
->WrapS
==GL_REPEAT
);
1431 assert(samp
->WrapT
==GL_REPEAT
);
1432 assert(img
->Border
==0);
1433 assert(img
->TexFormat
== MESA_FORMAT_BGR_UNORM8
);
1434 assert(swImg
->_IsPowerOfTwo
);
1437 for (k
=0; k
<n
; k
++) {
1438 GLint i
= util_ifloor(texcoords
[k
][0] * width
) & colMask
;
1439 GLint j
= util_ifloor(texcoords
[k
][1] * height
) & rowMask
;
1440 GLint pos
= (j
<< shift
) | i
;
1441 GLubyte
*texel
= (GLubyte
*) swImg
->ImageSlices
[0] + 3 * pos
;
1442 rgba
[k
][RCOMP
] = UBYTE_TO_FLOAT(texel
[2]);
1443 rgba
[k
][GCOMP
] = UBYTE_TO_FLOAT(texel
[1]);
1444 rgba
[k
][BCOMP
] = UBYTE_TO_FLOAT(texel
[0]);
1445 rgba
[k
][ACOMP
] = 1.0F
;
1451 * Optimized 2-D texture sampling:
1452 * S and T wrap mode == GL_REPEAT
1453 * GL_NEAREST min/mag filter
1455 * RowStride == Width,
1459 opt_sample_rgba_2d(struct gl_context
*ctx
,
1460 const struct gl_sampler_object
*samp
,
1461 const struct gl_texture_object
*tObj
,
1462 GLuint n
, const GLfloat texcoords
[][4],
1463 const GLfloat lambda
[], GLfloat rgba
[][4])
1465 const struct gl_texture_image
*img
= _mesa_base_tex_image(tObj
);
1466 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1467 const GLfloat width
= (GLfloat
) img
->Width
;
1468 const GLfloat height
= (GLfloat
) img
->Height
;
1469 const GLint colMask
= img
->Width
- 1;
1470 const GLint rowMask
= img
->Height
- 1;
1471 const GLint shift
= img
->WidthLog2
;
1475 assert(samp
->WrapS
==GL_REPEAT
);
1476 assert(samp
->WrapT
==GL_REPEAT
);
1477 assert(img
->Border
==0);
1478 assert(img
->TexFormat
== MESA_FORMAT_A8B8G8R8_UNORM
);
1479 assert(swImg
->_IsPowerOfTwo
);
1482 for (i
= 0; i
< n
; i
++) {
1483 const GLint col
= util_ifloor(texcoords
[i
][0] * width
) & colMask
;
1484 const GLint row
= util_ifloor(texcoords
[i
][1] * height
) & rowMask
;
1485 const GLint pos
= (row
<< shift
) | col
;
1486 const GLuint texel
= *((GLuint
*) swImg
->ImageSlices
[0] + pos
);
1487 rgba
[i
][RCOMP
] = UBYTE_TO_FLOAT( (texel
>> 24) );
1488 rgba
[i
][GCOMP
] = UBYTE_TO_FLOAT( (texel
>> 16) & 0xff );
1489 rgba
[i
][BCOMP
] = UBYTE_TO_FLOAT( (texel
>> 8) & 0xff );
1490 rgba
[i
][ACOMP
] = UBYTE_TO_FLOAT( (texel
) & 0xff );
1495 /** Sample 2D texture, using lambda to choose between min/magnification */
1497 sample_lambda_2d(struct gl_context
*ctx
,
1498 const struct gl_sampler_object
*samp
,
1499 const struct gl_texture_object
*tObj
,
1500 GLuint n
, const GLfloat texcoords
[][4],
1501 const GLfloat lambda
[], GLfloat rgba
[][4])
1503 const struct gl_texture_image
*tImg
= _mesa_base_tex_image(tObj
);
1504 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(tImg
);
1505 GLuint minStart
, minEnd
; /* texels with minification */
1506 GLuint magStart
, magEnd
; /* texels with magnification */
1508 const GLboolean repeatNoBorderPOT
= (samp
->WrapS
== GL_REPEAT
)
1509 && (samp
->WrapT
== GL_REPEAT
)
1510 && (tImg
->Border
== 0)
1511 && (_mesa_format_row_stride(tImg
->TexFormat
, tImg
->Width
) ==
1513 && swImg
->_IsPowerOfTwo
;
1515 assert(lambda
!= NULL
);
1516 compute_min_mag_ranges(samp
, n
, lambda
,
1517 &minStart
, &minEnd
, &magStart
, &magEnd
);
1519 if (minStart
< minEnd
) {
1520 /* do the minified texels */
1521 const GLuint m
= minEnd
- minStart
;
1522 switch (samp
->MinFilter
) {
1524 if (repeatNoBorderPOT
) {
1525 switch (tImg
->TexFormat
) {
1526 case MESA_FORMAT_BGR_UNORM8
:
1527 opt_sample_rgb_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1528 NULL
, rgba
+ minStart
);
1530 case MESA_FORMAT_A8B8G8R8_UNORM
:
1531 opt_sample_rgba_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1532 NULL
, rgba
+ minStart
);
1535 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1536 NULL
, rgba
+ minStart
);
1540 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1541 NULL
, rgba
+ minStart
);
1545 sample_linear_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1546 NULL
, rgba
+ minStart
);
1548 case GL_NEAREST_MIPMAP_NEAREST
:
1549 sample_2d_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
,
1550 texcoords
+ minStart
,
1551 lambda
+ minStart
, rgba
+ minStart
);
1553 case GL_LINEAR_MIPMAP_NEAREST
:
1554 sample_2d_linear_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1555 lambda
+ minStart
, rgba
+ minStart
);
1557 case GL_NEAREST_MIPMAP_LINEAR
:
1558 sample_2d_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1559 lambda
+ minStart
, rgba
+ minStart
);
1561 case GL_LINEAR_MIPMAP_LINEAR
:
1562 if (repeatNoBorderPOT
)
1563 sample_2d_linear_mipmap_linear_repeat(ctx
, samp
, tObj
, m
,
1564 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1566 sample_2d_linear_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1567 lambda
+ minStart
, rgba
+ minStart
);
1570 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1575 if (magStart
< magEnd
) {
1576 /* do the magnified texels */
1577 const GLuint m
= magEnd
- magStart
;
1579 switch (samp
->MagFilter
) {
1581 if (repeatNoBorderPOT
) {
1582 switch (tImg
->TexFormat
) {
1583 case MESA_FORMAT_BGR_UNORM8
:
1584 opt_sample_rgb_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1585 NULL
, rgba
+ magStart
);
1587 case MESA_FORMAT_A8B8G8R8_UNORM
:
1588 opt_sample_rgba_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1589 NULL
, rgba
+ magStart
);
1592 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1593 NULL
, rgba
+ magStart
);
1597 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1598 NULL
, rgba
+ magStart
);
1602 sample_linear_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1603 NULL
, rgba
+ magStart
);
1606 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1613 /* For anisotropic filtering */
1614 #define WEIGHT_LUT_SIZE 1024
1616 static GLfloat
*weightLut
= NULL
;
1619 * Creates the look-up table used to speed-up EWA sampling
1622 create_filter_table(void)
1626 weightLut
= malloc(WEIGHT_LUT_SIZE
* sizeof(GLfloat
));
1628 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
1630 GLfloat r2
= (GLfloat
) i
/ (GLfloat
) (WEIGHT_LUT_SIZE
- 1);
1631 GLfloat weight
= expf(-alpha
* r2
);
1632 weightLut
[i
] = weight
;
1639 * Elliptical weighted average (EWA) filter for producing high quality
1640 * anisotropic filtered results.
1641 * Based on the Higher Quality Elliptical Weighted Avarage Filter
1642 * published by Paul S. Heckbert in his Master's Thesis
1643 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1646 sample_2d_ewa(struct gl_context
*ctx
,
1647 const struct gl_sampler_object
*samp
,
1648 const struct gl_texture_object
*tObj
,
1649 const GLfloat texcoord
[4],
1650 const GLfloat dudx
, const GLfloat dvdx
,
1651 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1654 GLint level
= lod
> 0 ? lod
: 0;
1655 GLfloat scaling
= 1.0f
/ (1 << level
);
1656 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1657 const struct gl_texture_image
*mostDetailedImage
=
1658 _mesa_base_tex_image(tObj
);
1659 const struct swrast_texture_image
*swImg
=
1660 swrast_texture_image_const(mostDetailedImage
);
1661 GLfloat tex_u
= -0.5f
+ texcoord
[0] * swImg
->WidthScale
* scaling
;
1662 GLfloat tex_v
= -0.5f
+ texcoord
[1] * swImg
->HeightScale
* scaling
;
1664 GLfloat ux
= dudx
* scaling
;
1665 GLfloat vx
= dvdx
* scaling
;
1666 GLfloat uy
= dudy
* scaling
;
1667 GLfloat vy
= dvdy
* scaling
;
1669 /* compute ellipse coefficients to bound the region:
1670 * A*x*x + B*x*y + C*y*y = F.
1672 GLfloat A
= vx
*vx
+vy
*vy
+1;
1673 GLfloat B
= -2*(ux
*vx
+uy
*vy
);
1674 GLfloat C
= ux
*ux
+uy
*uy
+1;
1675 GLfloat F
= A
*C
-B
*B
/4.0f
;
1677 /* check if it is an ellipse */
1678 /* assert(F > 0.0); */
1680 /* Compute the ellipse's (u,v) bounding box in texture space */
1681 GLfloat d
= -B
*B
+4.0f
*C
*A
;
1682 GLfloat box_u
= 2.0f
/ d
* sqrtf(d
*C
*F
); /* box_u -> half of bbox with */
1683 GLfloat box_v
= 2.0f
/ d
* sqrtf(A
*d
*F
); /* box_v -> half of bbox height */
1685 GLint u0
= (GLint
) floorf(tex_u
- box_u
);
1686 GLint u1
= (GLint
) ceilf (tex_u
+ box_u
);
1687 GLint v0
= (GLint
) floorf(tex_v
- box_v
);
1688 GLint v1
= (GLint
) ceilf (tex_v
+ box_v
);
1690 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1691 GLfloat newCoord
[2];
1694 GLfloat U
= u0
- tex_u
;
1697 /* Scale ellipse formula to directly index the Filter Lookup Table.
1698 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1700 GLfloat formScale
= (GLfloat
) (WEIGHT_LUT_SIZE
- 1) / F
;
1704 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1706 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1707 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1708 * value, q, is less than F, we're inside the ellipse
1711 for (v
= v0
; v
<= v1
; ++v
) {
1712 GLfloat V
= v
- tex_v
;
1713 GLfloat dq
= A
* (2 * U
+ 1) + B
* V
;
1714 GLfloat q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
1717 for (u
= u0
; u
<= u1
; ++u
) {
1718 /* Note that the ellipse has been pre-scaled so F = WEIGHT_LUT_SIZE - 1 */
1719 if (q
< WEIGHT_LUT_SIZE
) {
1720 /* as a LUT is used, q must never be negative;
1721 * should not happen, though
1723 const GLint qClamped
= q
>= 0.0F
? (GLint
) q
: 0;
1724 GLfloat weight
= weightLut
[qClamped
];
1726 newCoord
[0] = u
/ ((GLfloat
) img
->Width2
);
1727 newCoord
[1] = v
/ ((GLfloat
) img
->Height2
);
1729 sample_2d_nearest(ctx
, samp
, img
, newCoord
, rgba
);
1730 num
[0] += weight
* rgba
[0];
1731 num
[1] += weight
* rgba
[1];
1732 num
[2] += weight
* rgba
[2];
1733 num
[3] += weight
* rgba
[3];
1743 /* Reaching this place would mean
1744 * that no pixels intersected the ellipse.
1745 * This should never happen because
1746 * the filter we use always
1747 * intersects at least one pixel.
1754 /* not enough pixels in resampling, resort to direct interpolation */
1755 sample_2d_linear(ctx
, samp
, img
, texcoord
, rgba
);
1759 rgba
[0] = num
[0] / den
;
1760 rgba
[1] = num
[1] / den
;
1761 rgba
[2] = num
[2] / den
;
1762 rgba
[3] = num
[3] / den
;
1767 * Anisotropic filtering using footprint assembly as outlined in the
1768 * EXT_texture_filter_anisotropic spec:
1769 * http://www.opengl.org/registry/specs/EXT/texture_filter_anisotropic.txt
1770 * Faster than EWA but has less quality (more aliasing effects)
1773 sample_2d_footprint(struct gl_context
*ctx
,
1774 const struct gl_sampler_object
*samp
,
1775 const struct gl_texture_object
*tObj
,
1776 const GLfloat texcoord
[4],
1777 const GLfloat dudx
, const GLfloat dvdx
,
1778 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1781 GLint level
= lod
> 0 ? lod
: 0;
1782 GLfloat scaling
= 1.0F
/ (1 << level
);
1783 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1785 GLfloat ux
= dudx
* scaling
;
1786 GLfloat vx
= dvdx
* scaling
;
1787 GLfloat uy
= dudy
* scaling
;
1788 GLfloat vy
= dvdy
* scaling
;
1790 GLfloat Px2
= ux
* ux
+ vx
* vx
; /* squared length of dx */
1791 GLfloat Py2
= uy
* uy
+ vy
* vy
; /* squared length of dy */
1797 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1798 GLfloat newCoord
[2];
1801 /* Calculate the per anisotropic sample offsets in s,t space. */
1803 numSamples
= (GLint
) ceilf(sqrtf(Px2
));
1804 ds
= ux
/ ((GLfloat
) img
->Width2
);
1805 dt
= vx
/ ((GLfloat
) img
->Height2
);
1808 numSamples
= (GLint
) ceilf(sqrtf(Py2
));
1809 ds
= uy
/ ((GLfloat
) img
->Width2
);
1810 dt
= vy
/ ((GLfloat
) img
->Height2
);
1813 for (s
= 0; s
<numSamples
; s
++) {
1814 newCoord
[0] = texcoord
[0] + ds
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5f
);
1815 newCoord
[1] = texcoord
[1] + dt
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5f
);
1817 sample_2d_linear(ctx
, samp
, img
, newCoord
, rgba
);
1824 rgba
[0] = num
[0] / numSamples
;
1825 rgba
[1] = num
[1] / numSamples
;
1826 rgba
[2] = num
[2] / numSamples
;
1827 rgba
[3] = num
[3] / numSamples
;
1832 * Returns the index of the specified texture object in the
1833 * gl_context texture unit array.
1836 texture_unit_index(const struct gl_context
*ctx
,
1837 const struct gl_texture_object
*tObj
)
1839 const GLuint maxUnit
1840 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
1843 /* XXX CoordUnits vs. ImageUnits */
1844 for (u
= 0; u
< maxUnit
; u
++) {
1845 if (ctx
->Texture
.Unit
[u
]._Current
== tObj
)
1849 u
= 0; /* not found, use 1st one; should never happen */
1856 * Sample 2D texture using an anisotropic filter.
1857 * NOTE: the const GLfloat lambda_iso[] parameter does *NOT* contain
1858 * the lambda float array but a "hidden" SWspan struct which is required
1859 * by this function but is not available in the texture_sample_func signature.
1860 * See _swrast_texture_span( struct gl_context *ctx, SWspan *span ) on how
1861 * this function is called.
1864 sample_lambda_2d_aniso(struct gl_context
*ctx
,
1865 const struct gl_sampler_object
*samp
,
1866 const struct gl_texture_object
*tObj
,
1867 GLuint n
, const GLfloat texcoords
[][4],
1868 const GLfloat lambda_iso
[], GLfloat rgba
[][4])
1870 const struct gl_texture_image
*tImg
= _mesa_base_tex_image(tObj
);
1871 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(tImg
);
1872 const GLfloat maxEccentricity
=
1873 samp
->MaxAnisotropy
* samp
->MaxAnisotropy
;
1875 /* re-calculate the lambda values so that they are usable with anisotropic
1878 SWspan
*span
= (SWspan
*)lambda_iso
; /* access the "hidden" SWspan struct */
1880 /* based on interpolate_texcoords(struct gl_context *ctx, SWspan *span)
1881 * in swrast/s_span.c
1884 /* find the texture unit index by looking up the current texture object
1885 * from the context list of available texture objects.
1887 const GLuint u
= texture_unit_index(ctx
, tObj
);
1888 const GLuint attr
= VARYING_SLOT_TEX0
+ u
;
1891 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
1892 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
1893 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
1894 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
1895 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
1896 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
1897 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
1898 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
1899 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
1901 /* from swrast/s_texcombine.c _swrast_texture_span */
1902 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[u
];
1903 const GLboolean adjustLOD
=
1904 (texUnit
->LodBias
+ samp
->LodBias
!= 0.0F
)
1905 || (samp
->MinLod
!= -1000.0F
|| samp
->MaxLod
!= 1000.0F
);
1909 /* on first access create the lookup table containing the filter weights. */
1911 create_filter_table();
1914 texW
= swImg
->WidthScale
;
1915 texH
= swImg
->HeightScale
;
1917 for (i
= 0; i
< n
; i
++) {
1918 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
1920 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
1921 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
1922 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
1923 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
1925 /* note: instead of working with Px and Py, we will use the
1926 * squared length instead, to avoid sqrt.
1928 GLfloat Px2
= dudx
* dudx
+ dvdx
* dvdx
;
1929 GLfloat Py2
= dudy
* dudy
+ dvdy
* dvdy
;
1949 /* if the eccentricity of the ellipse is too big, scale up the shorter
1950 * of the two vectors to limit the maximum amount of work per pixel
1953 if (e
> maxEccentricity
) {
1954 /* GLfloat s=e / maxEccentricity;
1958 Pmin2
= Pmax2
/ maxEccentricity
;
1961 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
1962 * this since 0.5*log(x) = log(sqrt(x))
1964 lod
= 0.5f
* util_fast_log2(Pmin2
);
1967 /* from swrast/s_texcombine.c _swrast_texture_span */
1968 if (texUnit
->LodBias
+ samp
->LodBias
!= 0.0F
) {
1969 /* apply LOD bias, but don't clamp yet */
1970 const GLfloat bias
=
1971 CLAMP(texUnit
->LodBias
+ samp
->LodBias
,
1972 -ctx
->Const
.MaxTextureLodBias
,
1973 ctx
->Const
.MaxTextureLodBias
);
1976 if (samp
->MinLod
!= -1000.0F
||
1977 samp
->MaxLod
!= 1000.0F
) {
1978 /* apply LOD clamping to lambda */
1979 lod
= CLAMP(lod
, samp
->MinLod
, samp
->MaxLod
);
1984 /* If the ellipse covers the whole image, we can
1985 * simply return the average of the whole image.
1987 if (lod
>= tObj
->_MaxLevel
) {
1988 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1989 texcoords
[i
], rgba
[i
]);
1992 /* don't bother interpolating between multiple LODs; it doesn't
1993 * seem to be worth the extra running time.
1995 sample_2d_ewa(ctx
, samp
, tObj
, texcoords
[i
],
1996 dudx
, dvdx
, dudy
, dvdy
, (GLint
) floorf(lod
), rgba
[i
]);
1999 (void) sample_2d_footprint
;
2001 sample_2d_footprint(ctx, tObj, texcoords[i],
2002 dudx, dvdx, dudy, dvdy, floor(lod), rgba[i]);
2010 /**********************************************************************/
2011 /* 3-D Texture Sampling Functions */
2012 /**********************************************************************/
2015 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2018 sample_3d_nearest(struct gl_context
*ctx
,
2019 const struct gl_sampler_object
*samp
,
2020 const struct gl_texture_image
*img
,
2021 const GLfloat texcoord
[4],
2024 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2025 const GLint width
= img
->Width2
; /* without border, power of two */
2026 const GLint height
= img
->Height2
; /* without border, power of two */
2027 const GLint depth
= img
->Depth2
; /* without border, power of two */
2031 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
2032 j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
2033 k
= nearest_texel_location(samp
->WrapR
, img
, depth
, texcoord
[2]);
2035 if (i
< 0 || i
>= (GLint
) img
->Width
||
2036 j
< 0 || j
>= (GLint
) img
->Height
||
2037 k
< 0 || k
>= (GLint
) img
->Depth
) {
2038 /* Need this test for GL_CLAMP_TO_BORDER mode */
2039 get_border_color(samp
, img
, rgba
);
2042 swImg
->FetchTexel(swImg
, i
, j
, k
, rgba
);
2048 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2051 sample_3d_linear(struct gl_context
*ctx
,
2052 const struct gl_sampler_object
*samp
,
2053 const struct gl_texture_image
*img
,
2054 const GLfloat texcoord
[4],
2057 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2058 const GLint width
= img
->Width2
;
2059 const GLint height
= img
->Height2
;
2060 const GLint depth
= img
->Depth2
;
2061 GLint i0
, j0
, k0
, i1
, j1
, k1
;
2062 GLbitfield useBorderColor
= 0x0;
2064 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
2065 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
2067 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2068 linear_texel_locations(samp
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2069 linear_texel_locations(samp
->WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
2080 /* check if sampling texture border color */
2081 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2082 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2083 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2084 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2085 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
2086 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
2090 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
2091 get_border_color(samp
, img
, t000
);
2094 swImg
->FetchTexel(swImg
, i0
, j0
, k0
, t000
);
2096 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
2097 get_border_color(samp
, img
, t100
);
2100 swImg
->FetchTexel(swImg
, i1
, j0
, k0
, t100
);
2102 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
2103 get_border_color(samp
, img
, t010
);
2106 swImg
->FetchTexel(swImg
, i0
, j1
, k0
, t010
);
2108 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
2109 get_border_color(samp
, img
, t110
);
2112 swImg
->FetchTexel(swImg
, i1
, j1
, k0
, t110
);
2115 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
2116 get_border_color(samp
, img
, t001
);
2119 swImg
->FetchTexel(swImg
, i0
, j0
, k1
, t001
);
2121 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
2122 get_border_color(samp
, img
, t101
);
2125 swImg
->FetchTexel(swImg
, i1
, j0
, k1
, t101
);
2127 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
2128 get_border_color(samp
, img
, t011
);
2131 swImg
->FetchTexel(swImg
, i0
, j1
, k1
, t011
);
2133 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
2134 get_border_color(samp
, img
, t111
);
2137 swImg
->FetchTexel(swImg
, i1
, j1
, k1
, t111
);
2140 /* trilinear interpolation of samples */
2141 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
2146 sample_3d_nearest_mipmap_nearest(struct gl_context
*ctx
,
2147 const struct gl_sampler_object
*samp
,
2148 const struct gl_texture_object
*tObj
,
2149 GLuint n
, const GLfloat texcoord
[][4],
2150 const GLfloat lambda
[], GLfloat rgba
[][4] )
2153 for (i
= 0; i
< n
; i
++) {
2154 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2155 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2161 sample_3d_linear_mipmap_nearest(struct gl_context
*ctx
,
2162 const struct gl_sampler_object
*samp
,
2163 const struct gl_texture_object
*tObj
,
2164 GLuint n
, const GLfloat texcoord
[][4],
2165 const GLfloat lambda
[], GLfloat rgba
[][4])
2168 assert(lambda
!= NULL
);
2169 for (i
= 0; i
< n
; i
++) {
2170 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2171 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2177 sample_3d_nearest_mipmap_linear(struct gl_context
*ctx
,
2178 const struct gl_sampler_object
*samp
,
2179 const struct gl_texture_object
*tObj
,
2180 GLuint n
, const GLfloat texcoord
[][4],
2181 const GLfloat lambda
[], GLfloat rgba
[][4])
2184 assert(lambda
!= NULL
);
2185 for (i
= 0; i
< n
; i
++) {
2186 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2187 if (level
>= tObj
->_MaxLevel
) {
2188 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2189 texcoord
[i
], rgba
[i
]);
2192 GLfloat t0
[4], t1
[4]; /* texels */
2193 const GLfloat f
= FRAC(lambda
[i
]);
2194 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2195 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2196 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2203 sample_3d_linear_mipmap_linear(struct gl_context
*ctx
,
2204 const struct gl_sampler_object
*samp
,
2205 const struct gl_texture_object
*tObj
,
2206 GLuint n
, const GLfloat texcoord
[][4],
2207 const GLfloat lambda
[], GLfloat rgba
[][4])
2210 assert(lambda
!= NULL
);
2211 for (i
= 0; i
< n
; i
++) {
2212 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2213 if (level
>= tObj
->_MaxLevel
) {
2214 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2215 texcoord
[i
], rgba
[i
]);
2218 GLfloat t0
[4], t1
[4]; /* texels */
2219 const GLfloat f
= FRAC(lambda
[i
]);
2220 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2221 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2222 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2228 /** Sample 3D texture, nearest filtering for both min/magnification */
2230 sample_nearest_3d(struct gl_context
*ctx
,
2231 const struct gl_sampler_object
*samp
,
2232 const struct gl_texture_object
*tObj
, GLuint n
,
2233 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2237 const struct gl_texture_image
*image
= _mesa_base_tex_image(tObj
);
2239 for (i
= 0; i
< n
; i
++) {
2240 sample_3d_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
2245 /** Sample 3D texture, linear filtering for both min/magnification */
2247 sample_linear_3d(struct gl_context
*ctx
,
2248 const struct gl_sampler_object
*samp
,
2249 const struct gl_texture_object
*tObj
, GLuint n
,
2250 const GLfloat texcoords
[][4],
2251 const GLfloat lambda
[], GLfloat rgba
[][4])
2254 const struct gl_texture_image
*image
= _mesa_base_tex_image(tObj
);
2256 for (i
= 0; i
< n
; i
++) {
2257 sample_3d_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
2262 /** Sample 3D texture, using lambda to choose between min/magnification */
2264 sample_lambda_3d(struct gl_context
*ctx
,
2265 const struct gl_sampler_object
*samp
,
2266 const struct gl_texture_object
*tObj
, GLuint n
,
2267 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2270 GLuint minStart
, minEnd
; /* texels with minification */
2271 GLuint magStart
, magEnd
; /* texels with magnification */
2274 assert(lambda
!= NULL
);
2275 compute_min_mag_ranges(samp
, n
, lambda
,
2276 &minStart
, &minEnd
, &magStart
, &magEnd
);
2278 if (minStart
< minEnd
) {
2279 /* do the minified texels */
2280 GLuint m
= minEnd
- minStart
;
2281 switch (samp
->MinFilter
) {
2283 for (i
= minStart
; i
< minEnd
; i
++)
2284 sample_3d_nearest(ctx
, samp
, _mesa_base_tex_image(tObj
),
2285 texcoords
[i
], rgba
[i
]);
2288 for (i
= minStart
; i
< minEnd
; i
++)
2289 sample_3d_linear(ctx
, samp
, _mesa_base_tex_image(tObj
),
2290 texcoords
[i
], rgba
[i
]);
2292 case GL_NEAREST_MIPMAP_NEAREST
:
2293 sample_3d_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2294 lambda
+ minStart
, rgba
+ minStart
);
2296 case GL_LINEAR_MIPMAP_NEAREST
:
2297 sample_3d_linear_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2298 lambda
+ minStart
, rgba
+ minStart
);
2300 case GL_NEAREST_MIPMAP_LINEAR
:
2301 sample_3d_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2302 lambda
+ minStart
, rgba
+ minStart
);
2304 case GL_LINEAR_MIPMAP_LINEAR
:
2305 sample_3d_linear_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2306 lambda
+ minStart
, rgba
+ minStart
);
2309 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
2314 if (magStart
< magEnd
) {
2315 /* do the magnified texels */
2316 switch (samp
->MagFilter
) {
2318 for (i
= magStart
; i
< magEnd
; i
++)
2319 sample_3d_nearest(ctx
, samp
, _mesa_base_tex_image(tObj
),
2320 texcoords
[i
], rgba
[i
]);
2323 for (i
= magStart
; i
< magEnd
; i
++)
2324 sample_3d_linear(ctx
, samp
, _mesa_base_tex_image(tObj
),
2325 texcoords
[i
], rgba
[i
]);
2328 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
2335 /**********************************************************************/
2336 /* Texture Cube Map Sampling Functions */
2337 /**********************************************************************/
2340 * Choose one of six sides of a texture cube map given the texture
2341 * coord (rx,ry,rz). Return pointer to corresponding array of texture
2344 static const struct gl_texture_image
**
2345 choose_cube_face(const struct gl_texture_object
*texObj
,
2346 const GLfloat texcoord
[4], GLfloat newCoord
[4])
2350 direction target sc tc ma
2351 ---------- ------------------------------- --- --- ---
2352 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2353 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2354 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2355 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2356 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2357 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2359 const GLfloat rx
= texcoord
[0];
2360 const GLfloat ry
= texcoord
[1];
2361 const GLfloat rz
= texcoord
[2];
2362 const GLfloat arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
2366 if (arx
>= ary
&& arx
>= arz
) {
2380 else if (ary
>= arx
&& ary
>= arz
) {
2410 const float ima
= 1.0F
/ ma
;
2411 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
2412 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
2415 return (const struct gl_texture_image
**) texObj
->Image
[face
];
2420 sample_nearest_cube(struct gl_context
*ctx
,
2421 const struct gl_sampler_object
*samp
,
2422 const struct gl_texture_object
*tObj
, GLuint n
,
2423 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2428 for (i
= 0; i
< n
; i
++) {
2429 const struct gl_texture_image
**images
;
2430 GLfloat newCoord
[4];
2431 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2432 sample_2d_nearest(ctx
, samp
, images
[tObj
->BaseLevel
],
2435 if (is_depth_texture(tObj
)) {
2436 for (i
= 0; i
< n
; i
++) {
2437 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2444 sample_linear_cube(struct gl_context
*ctx
,
2445 const struct gl_sampler_object
*samp
,
2446 const struct gl_texture_object
*tObj
, GLuint n
,
2447 const GLfloat texcoords
[][4],
2448 const GLfloat lambda
[], GLfloat rgba
[][4])
2452 for (i
= 0; i
< n
; i
++) {
2453 const struct gl_texture_image
**images
;
2454 GLfloat newCoord
[4];
2455 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2456 sample_2d_linear(ctx
, samp
, images
[tObj
->BaseLevel
],
2459 if (is_depth_texture(tObj
)) {
2460 for (i
= 0; i
< n
; i
++) {
2461 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2468 sample_cube_nearest_mipmap_nearest(struct gl_context
*ctx
,
2469 const struct gl_sampler_object
*samp
,
2470 const struct gl_texture_object
*tObj
,
2471 GLuint n
, const GLfloat texcoord
[][4],
2472 const GLfloat lambda
[], GLfloat rgba
[][4])
2475 assert(lambda
!= NULL
);
2476 for (i
= 0; i
< n
; i
++) {
2477 const struct gl_texture_image
**images
;
2478 GLfloat newCoord
[4];
2480 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2482 /* XXX we actually need to recompute lambda here based on the newCoords.
2483 * But we would need the texcoords of adjacent fragments to compute that
2484 * properly, and we don't have those here.
2485 * For now, do an approximation: subtracting 1 from the chosen mipmap
2486 * level seems to work in some test cases.
2487 * The same adjustment is done in the next few functions.
2489 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2490 level
= MAX2(level
- 1, 0);
2492 sample_2d_nearest(ctx
, samp
, images
[level
], newCoord
, rgba
[i
]);
2494 if (is_depth_texture(tObj
)) {
2495 for (i
= 0; i
< n
; i
++) {
2496 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2503 sample_cube_linear_mipmap_nearest(struct gl_context
*ctx
,
2504 const struct gl_sampler_object
*samp
,
2505 const struct gl_texture_object
*tObj
,
2506 GLuint n
, const GLfloat texcoord
[][4],
2507 const GLfloat lambda
[], GLfloat rgba
[][4])
2510 assert(lambda
!= NULL
);
2511 for (i
= 0; i
< n
; i
++) {
2512 const struct gl_texture_image
**images
;
2513 GLfloat newCoord
[4];
2514 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2515 level
= MAX2(level
- 1, 0); /* see comment above */
2516 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2517 sample_2d_linear(ctx
, samp
, images
[level
], newCoord
, rgba
[i
]);
2519 if (is_depth_texture(tObj
)) {
2520 for (i
= 0; i
< n
; i
++) {
2521 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2528 sample_cube_nearest_mipmap_linear(struct gl_context
*ctx
,
2529 const struct gl_sampler_object
*samp
,
2530 const struct gl_texture_object
*tObj
,
2531 GLuint n
, const GLfloat texcoord
[][4],
2532 const GLfloat lambda
[], GLfloat rgba
[][4])
2535 assert(lambda
!= NULL
);
2536 for (i
= 0; i
< n
; i
++) {
2537 const struct gl_texture_image
**images
;
2538 GLfloat newCoord
[4];
2539 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2540 level
= MAX2(level
- 1, 0); /* see comment above */
2541 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2542 if (level
>= tObj
->_MaxLevel
) {
2543 sample_2d_nearest(ctx
, samp
, images
[tObj
->_MaxLevel
],
2547 GLfloat t0
[4], t1
[4]; /* texels */
2548 const GLfloat f
= FRAC(lambda
[i
]);
2549 sample_2d_nearest(ctx
, samp
, images
[level
], newCoord
, t0
);
2550 sample_2d_nearest(ctx
, samp
, images
[level
+1], newCoord
, t1
);
2551 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2554 if (is_depth_texture(tObj
)) {
2555 for (i
= 0; i
< n
; i
++) {
2556 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2563 sample_cube_linear_mipmap_linear(struct gl_context
*ctx
,
2564 const struct gl_sampler_object
*samp
,
2565 const struct gl_texture_object
*tObj
,
2566 GLuint n
, const GLfloat texcoord
[][4],
2567 const GLfloat lambda
[], GLfloat rgba
[][4])
2570 assert(lambda
!= NULL
);
2571 for (i
= 0; i
< n
; i
++) {
2572 const struct gl_texture_image
**images
;
2573 GLfloat newCoord
[4];
2574 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2575 level
= MAX2(level
- 1, 0); /* see comment above */
2576 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2577 if (level
>= tObj
->_MaxLevel
) {
2578 sample_2d_linear(ctx
, samp
, images
[tObj
->_MaxLevel
],
2582 GLfloat t0
[4], t1
[4];
2583 const GLfloat f
= FRAC(lambda
[i
]);
2584 sample_2d_linear(ctx
, samp
, images
[level
], newCoord
, t0
);
2585 sample_2d_linear(ctx
, samp
, images
[level
+1], newCoord
, t1
);
2586 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2589 if (is_depth_texture(tObj
)) {
2590 for (i
= 0; i
< n
; i
++) {
2591 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2597 /** Sample cube texture, using lambda to choose between min/magnification */
2599 sample_lambda_cube(struct gl_context
*ctx
,
2600 const struct gl_sampler_object
*samp
,
2601 const struct gl_texture_object
*tObj
, GLuint n
,
2602 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2605 GLuint minStart
, minEnd
; /* texels with minification */
2606 GLuint magStart
, magEnd
; /* texels with magnification */
2608 assert(lambda
!= NULL
);
2609 compute_min_mag_ranges(samp
, n
, lambda
,
2610 &minStart
, &minEnd
, &magStart
, &magEnd
);
2612 if (minStart
< minEnd
) {
2613 /* do the minified texels */
2614 const GLuint m
= minEnd
- minStart
;
2615 switch (samp
->MinFilter
) {
2617 sample_nearest_cube(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2618 lambda
+ minStart
, rgba
+ minStart
);
2621 sample_linear_cube(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2622 lambda
+ minStart
, rgba
+ minStart
);
2624 case GL_NEAREST_MIPMAP_NEAREST
:
2625 sample_cube_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
,
2626 texcoords
+ minStart
,
2627 lambda
+ minStart
, rgba
+ minStart
);
2629 case GL_LINEAR_MIPMAP_NEAREST
:
2630 sample_cube_linear_mipmap_nearest(ctx
, samp
, tObj
, m
,
2631 texcoords
+ minStart
,
2632 lambda
+ minStart
, rgba
+ minStart
);
2634 case GL_NEAREST_MIPMAP_LINEAR
:
2635 sample_cube_nearest_mipmap_linear(ctx
, samp
, tObj
, m
,
2636 texcoords
+ minStart
,
2637 lambda
+ minStart
, rgba
+ minStart
);
2639 case GL_LINEAR_MIPMAP_LINEAR
:
2640 sample_cube_linear_mipmap_linear(ctx
, samp
, tObj
, m
,
2641 texcoords
+ minStart
,
2642 lambda
+ minStart
, rgba
+ minStart
);
2645 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2650 if (magStart
< magEnd
) {
2651 /* do the magnified texels */
2652 const GLuint m
= magEnd
- magStart
;
2653 switch (samp
->MagFilter
) {
2655 sample_nearest_cube(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
2656 lambda
+ magStart
, rgba
+ magStart
);
2659 sample_linear_cube(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
2660 lambda
+ magStart
, rgba
+ magStart
);
2663 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2670 /**********************************************************************/
2671 /* Texture Rectangle Sampling Functions */
2672 /**********************************************************************/
2676 sample_nearest_rect(struct gl_context
*ctx
,
2677 const struct gl_sampler_object
*samp
,
2678 const struct gl_texture_object
*tObj
, GLuint n
,
2679 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2682 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2683 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2684 const GLint width
= img
->Width
;
2685 const GLint height
= img
->Height
;
2691 assert(samp
->WrapS
== GL_CLAMP
||
2692 samp
->WrapS
== GL_CLAMP_TO_EDGE
||
2693 samp
->WrapS
== GL_CLAMP_TO_BORDER
);
2694 assert(samp
->WrapT
== GL_CLAMP
||
2695 samp
->WrapT
== GL_CLAMP_TO_EDGE
||
2696 samp
->WrapT
== GL_CLAMP_TO_BORDER
);
2698 for (i
= 0; i
< n
; i
++) {
2700 col
= clamp_rect_coord_nearest(samp
->WrapS
, texcoords
[i
][0], width
);
2701 row
= clamp_rect_coord_nearest(samp
->WrapT
, texcoords
[i
][1], height
);
2702 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2703 get_border_color(samp
, img
, rgba
[i
]);
2705 swImg
->FetchTexel(swImg
, col
, row
, 0, rgba
[i
]);
2711 sample_linear_rect(struct gl_context
*ctx
,
2712 const struct gl_sampler_object
*samp
,
2713 const struct gl_texture_object
*tObj
, GLuint n
,
2714 const GLfloat texcoords
[][4],
2715 const GLfloat lambda
[], GLfloat rgba
[][4])
2717 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2718 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2719 const GLint width
= img
->Width
;
2720 const GLint height
= img
->Height
;
2726 assert(samp
->WrapS
== GL_CLAMP
||
2727 samp
->WrapS
== GL_CLAMP_TO_EDGE
||
2728 samp
->WrapS
== GL_CLAMP_TO_BORDER
);
2729 assert(samp
->WrapT
== GL_CLAMP
||
2730 samp
->WrapT
== GL_CLAMP_TO_EDGE
||
2731 samp
->WrapT
== GL_CLAMP_TO_BORDER
);
2733 for (i
= 0; i
< n
; i
++) {
2734 GLint i0
, j0
, i1
, j1
;
2735 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2737 GLbitfield useBorderColor
= 0x0;
2739 clamp_rect_coord_linear(samp
->WrapS
, texcoords
[i
][0], width
,
2741 clamp_rect_coord_linear(samp
->WrapT
, texcoords
[i
][1], height
,
2744 /* compute integer rows/columns */
2745 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2746 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2747 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2748 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2750 /* get four texel samples */
2751 if (useBorderColor
& (I0BIT
| J0BIT
))
2752 get_border_color(samp
, img
, t00
);
2754 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
2756 if (useBorderColor
& (I1BIT
| J0BIT
))
2757 get_border_color(samp
, img
, t10
);
2759 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
2761 if (useBorderColor
& (I0BIT
| J1BIT
))
2762 get_border_color(samp
, img
, t01
);
2764 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
2766 if (useBorderColor
& (I1BIT
| J1BIT
))
2767 get_border_color(samp
, img
, t11
);
2769 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
2771 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2776 /** Sample Rect texture, using lambda to choose between min/magnification */
2778 sample_lambda_rect(struct gl_context
*ctx
,
2779 const struct gl_sampler_object
*samp
,
2780 const struct gl_texture_object
*tObj
, GLuint n
,
2781 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2784 GLuint minStart
, minEnd
, magStart
, magEnd
;
2786 /* We only need lambda to decide between minification and magnification.
2787 * There is no mipmapping with rectangular textures.
2789 compute_min_mag_ranges(samp
, n
, lambda
,
2790 &minStart
, &minEnd
, &magStart
, &magEnd
);
2792 if (minStart
< minEnd
) {
2793 if (samp
->MinFilter
== GL_NEAREST
) {
2794 sample_nearest_rect(ctx
, samp
, tObj
, minEnd
- minStart
,
2795 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2798 sample_linear_rect(ctx
, samp
, tObj
, minEnd
- minStart
,
2799 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2802 if (magStart
< magEnd
) {
2803 if (samp
->MagFilter
== GL_NEAREST
) {
2804 sample_nearest_rect(ctx
, samp
, tObj
, magEnd
- magStart
,
2805 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2808 sample_linear_rect(ctx
, samp
, tObj
, magEnd
- magStart
,
2809 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2815 /**********************************************************************/
2816 /* 2D Texture Array Sampling Functions */
2817 /**********************************************************************/
2820 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2823 sample_2d_array_nearest(struct gl_context
*ctx
,
2824 const struct gl_sampler_object
*samp
,
2825 const struct gl_texture_image
*img
,
2826 const GLfloat texcoord
[4],
2829 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2830 const GLint width
= img
->Width2
; /* without border, power of two */
2831 const GLint height
= img
->Height2
; /* without border, power of two */
2832 const GLint depth
= img
->Depth
;
2837 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
2838 j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
2839 array
= tex_array_slice(texcoord
[2], depth
);
2841 if (i
< 0 || i
>= (GLint
) img
->Width
||
2842 j
< 0 || j
>= (GLint
) img
->Height
||
2843 array
< 0 || array
>= (GLint
) img
->Depth
) {
2844 /* Need this test for GL_CLAMP_TO_BORDER mode */
2845 get_border_color(samp
, img
, rgba
);
2848 swImg
->FetchTexel(swImg
, i
, j
, array
, rgba
);
2854 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2857 sample_2d_array_linear(struct gl_context
*ctx
,
2858 const struct gl_sampler_object
*samp
,
2859 const struct gl_texture_image
*img
,
2860 const GLfloat texcoord
[4],
2863 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2864 const GLint width
= img
->Width2
;
2865 const GLint height
= img
->Height2
;
2866 const GLint depth
= img
->Depth
;
2867 GLint i0
, j0
, i1
, j1
;
2869 GLbitfield useBorderColor
= 0x0;
2871 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2873 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2874 linear_texel_locations(samp
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2875 array
= tex_array_slice(texcoord
[2], depth
);
2877 if (array
< 0 || array
>= depth
) {
2878 COPY_4V(rgba
, samp
->BorderColor
.f
);
2888 /* check if sampling texture border color */
2889 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2890 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2891 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2892 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2896 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2897 get_border_color(samp
, img
, t00
);
2900 swImg
->FetchTexel(swImg
, i0
, j0
, array
, t00
);
2902 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2903 get_border_color(samp
, img
, t10
);
2906 swImg
->FetchTexel(swImg
, i1
, j0
, array
, t10
);
2908 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2909 get_border_color(samp
, img
, t01
);
2912 swImg
->FetchTexel(swImg
, i0
, j1
, array
, t01
);
2914 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2915 get_border_color(samp
, img
, t11
);
2918 swImg
->FetchTexel(swImg
, i1
, j1
, array
, t11
);
2921 /* trilinear interpolation of samples */
2922 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2928 sample_2d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2929 const struct gl_sampler_object
*samp
,
2930 const struct gl_texture_object
*tObj
,
2931 GLuint n
, const GLfloat texcoord
[][4],
2932 const GLfloat lambda
[], GLfloat rgba
[][4])
2935 for (i
= 0; i
< n
; i
++) {
2936 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2937 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
],
2944 sample_2d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2945 const struct gl_sampler_object
*samp
,
2946 const struct gl_texture_object
*tObj
,
2947 GLuint n
, const GLfloat texcoord
[][4],
2948 const GLfloat lambda
[], GLfloat rgba
[][4])
2951 assert(lambda
!= NULL
);
2952 for (i
= 0; i
< n
; i
++) {
2953 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2954 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][level
],
2955 texcoord
[i
], rgba
[i
]);
2961 sample_2d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2962 const struct gl_sampler_object
*samp
,
2963 const struct gl_texture_object
*tObj
,
2964 GLuint n
, const GLfloat texcoord
[][4],
2965 const GLfloat lambda
[], GLfloat rgba
[][4])
2968 assert(lambda
!= NULL
);
2969 for (i
= 0; i
< n
; i
++) {
2970 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2971 if (level
>= tObj
->_MaxLevel
) {
2972 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2973 texcoord
[i
], rgba
[i
]);
2976 GLfloat t0
[4], t1
[4]; /* texels */
2977 const GLfloat f
= FRAC(lambda
[i
]);
2978 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
],
2980 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
+1],
2982 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2989 sample_2d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2990 const struct gl_sampler_object
*samp
,
2991 const struct gl_texture_object
*tObj
,
2992 GLuint n
, const GLfloat texcoord
[][4],
2993 const GLfloat lambda
[], GLfloat rgba
[][4])
2996 assert(lambda
!= NULL
);
2997 for (i
= 0; i
< n
; i
++) {
2998 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2999 if (level
>= tObj
->_MaxLevel
) {
3000 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
3001 texcoord
[i
], rgba
[i
]);
3004 GLfloat t0
[4], t1
[4]; /* texels */
3005 const GLfloat f
= FRAC(lambda
[i
]);
3006 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][level
],
3008 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][level
+1],
3010 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3016 /** Sample 2D Array texture, nearest filtering for both min/magnification */
3018 sample_nearest_2d_array(struct gl_context
*ctx
,
3019 const struct gl_sampler_object
*samp
,
3020 const struct gl_texture_object
*tObj
, GLuint n
,
3021 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3025 const struct gl_texture_image
*image
= _mesa_base_tex_image(tObj
);
3027 for (i
= 0; i
< n
; i
++) {
3028 sample_2d_array_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3034 /** Sample 2D Array texture, linear filtering for both min/magnification */
3036 sample_linear_2d_array(struct gl_context
*ctx
,
3037 const struct gl_sampler_object
*samp
,
3038 const struct gl_texture_object
*tObj
, GLuint n
,
3039 const GLfloat texcoords
[][4],
3040 const GLfloat lambda
[], GLfloat rgba
[][4])
3043 const struct gl_texture_image
*image
= _mesa_base_tex_image(tObj
);
3045 for (i
= 0; i
< n
; i
++) {
3046 sample_2d_array_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3051 /** Sample 2D Array texture, using lambda to choose between min/magnification */
3053 sample_lambda_2d_array(struct gl_context
*ctx
,
3054 const struct gl_sampler_object
*samp
,
3055 const struct gl_texture_object
*tObj
, GLuint n
,
3056 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3059 GLuint minStart
, minEnd
; /* texels with minification */
3060 GLuint magStart
, magEnd
; /* texels with magnification */
3063 assert(lambda
!= NULL
);
3064 compute_min_mag_ranges(samp
, n
, lambda
,
3065 &minStart
, &minEnd
, &magStart
, &magEnd
);
3067 if (minStart
< minEnd
) {
3068 /* do the minified texels */
3069 GLuint m
= minEnd
- minStart
;
3070 switch (samp
->MinFilter
) {
3072 for (i
= minStart
; i
< minEnd
; i
++)
3073 sample_2d_array_nearest(ctx
, samp
, _mesa_base_tex_image(tObj
),
3074 texcoords
[i
], rgba
[i
]);
3077 for (i
= minStart
; i
< minEnd
; i
++)
3078 sample_2d_array_linear(ctx
, samp
, _mesa_base_tex_image(tObj
),
3079 texcoords
[i
], rgba
[i
]);
3081 case GL_NEAREST_MIPMAP_NEAREST
:
3082 sample_2d_array_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
,
3083 texcoords
+ minStart
,
3087 case GL_LINEAR_MIPMAP_NEAREST
:
3088 sample_2d_array_linear_mipmap_nearest(ctx
, samp
, tObj
, m
,
3089 texcoords
+ minStart
,
3093 case GL_NEAREST_MIPMAP_LINEAR
:
3094 sample_2d_array_nearest_mipmap_linear(ctx
, samp
, tObj
, m
,
3095 texcoords
+ minStart
,
3099 case GL_LINEAR_MIPMAP_LINEAR
:
3100 sample_2d_array_linear_mipmap_linear(ctx
, samp
, tObj
, m
,
3101 texcoords
+ minStart
,
3106 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
3111 if (magStart
< magEnd
) {
3112 /* do the magnified texels */
3113 switch (samp
->MagFilter
) {
3115 for (i
= magStart
; i
< magEnd
; i
++)
3116 sample_2d_array_nearest(ctx
, samp
, _mesa_base_tex_image(tObj
),
3117 texcoords
[i
], rgba
[i
]);
3120 for (i
= magStart
; i
< magEnd
; i
++)
3121 sample_2d_array_linear(ctx
, samp
, _mesa_base_tex_image(tObj
),
3122 texcoords
[i
], rgba
[i
]);
3125 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
3134 /**********************************************************************/
3135 /* 1D Texture Array Sampling Functions */
3136 /**********************************************************************/
3139 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
3142 sample_1d_array_nearest(struct gl_context
*ctx
,
3143 const struct gl_sampler_object
*samp
,
3144 const struct gl_texture_image
*img
,
3145 const GLfloat texcoord
[4],
3148 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3149 const GLint width
= img
->Width2
; /* without border, power of two */
3150 const GLint height
= img
->Height
;
3155 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
3156 array
= tex_array_slice(texcoord
[1], height
);
3158 if (i
< 0 || i
>= (GLint
) img
->Width
||
3159 array
< 0 || array
>= (GLint
) img
->Height
) {
3160 /* Need this test for GL_CLAMP_TO_BORDER mode */
3161 get_border_color(samp
, img
, rgba
);
3164 swImg
->FetchTexel(swImg
, i
, array
, 0, rgba
);
3170 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
3173 sample_1d_array_linear(struct gl_context
*ctx
,
3174 const struct gl_sampler_object
*samp
,
3175 const struct gl_texture_image
*img
,
3176 const GLfloat texcoord
[4],
3179 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3180 const GLint width
= img
->Width2
;
3181 const GLint height
= img
->Height
;
3184 GLbitfield useBorderColor
= 0x0;
3186 GLfloat t0
[4], t1
[4];
3188 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
3189 array
= tex_array_slice(texcoord
[1], height
);
3196 /* check if sampling texture border color */
3197 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
3198 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
3201 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
3204 if (useBorderColor
& (I0BIT
| K0BIT
)) {
3205 get_border_color(samp
, img
, t0
);
3208 swImg
->FetchTexel(swImg
, i0
, array
, 0, t0
);
3210 if (useBorderColor
& (I1BIT
| K0BIT
)) {
3211 get_border_color(samp
, img
, t1
);
3214 swImg
->FetchTexel(swImg
, i1
, array
, 0, t1
);
3217 /* bilinear interpolation of samples */
3218 lerp_rgba(rgba
, a
, t0
, t1
);
3223 sample_1d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
3224 const struct gl_sampler_object
*samp
,
3225 const struct gl_texture_object
*tObj
,
3226 GLuint n
, const GLfloat texcoord
[][4],
3227 const GLfloat lambda
[], GLfloat rgba
[][4])
3230 for (i
= 0; i
< n
; i
++) {
3231 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3232 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
],
3239 sample_1d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
3240 const struct gl_sampler_object
*samp
,
3241 const struct gl_texture_object
*tObj
,
3242 GLuint n
, const GLfloat texcoord
[][4],
3243 const GLfloat lambda
[], GLfloat rgba
[][4])
3246 assert(lambda
!= NULL
);
3247 for (i
= 0; i
< n
; i
++) {
3248 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3249 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][level
],
3250 texcoord
[i
], rgba
[i
]);
3256 sample_1d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
3257 const struct gl_sampler_object
*samp
,
3258 const struct gl_texture_object
*tObj
,
3259 GLuint n
, const GLfloat texcoord
[][4],
3260 const GLfloat lambda
[], GLfloat rgba
[][4])
3263 assert(lambda
!= NULL
);
3264 for (i
= 0; i
< n
; i
++) {
3265 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3266 if (level
>= tObj
->_MaxLevel
) {
3267 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
3268 texcoord
[i
], rgba
[i
]);
3271 GLfloat t0
[4], t1
[4]; /* texels */
3272 const GLfloat f
= FRAC(lambda
[i
]);
3273 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3274 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3275 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3282 sample_1d_array_linear_mipmap_linear(struct gl_context
*ctx
,
3283 const struct gl_sampler_object
*samp
,
3284 const struct gl_texture_object
*tObj
,
3285 GLuint n
, const GLfloat texcoord
[][4],
3286 const GLfloat lambda
[], GLfloat rgba
[][4])
3289 assert(lambda
!= NULL
);
3290 for (i
= 0; i
< n
; i
++) {
3291 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3292 if (level
>= tObj
->_MaxLevel
) {
3293 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
3294 texcoord
[i
], rgba
[i
]);
3297 GLfloat t0
[4], t1
[4]; /* texels */
3298 const GLfloat f
= FRAC(lambda
[i
]);
3299 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3300 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3301 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3307 /** Sample 1D Array texture, nearest filtering for both min/magnification */
3309 sample_nearest_1d_array(struct gl_context
*ctx
,
3310 const struct gl_sampler_object
*samp
,
3311 const struct gl_texture_object
*tObj
, GLuint n
,
3312 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3316 const struct gl_texture_image
*image
= _mesa_base_tex_image(tObj
);
3318 for (i
= 0; i
< n
; i
++) {
3319 sample_1d_array_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3324 /** Sample 1D Array texture, linear filtering for both min/magnification */
3326 sample_linear_1d_array(struct gl_context
*ctx
,
3327 const struct gl_sampler_object
*samp
,
3328 const struct gl_texture_object
*tObj
, GLuint n
,
3329 const GLfloat texcoords
[][4],
3330 const GLfloat lambda
[], GLfloat rgba
[][4])
3333 const struct gl_texture_image
*image
= _mesa_base_tex_image(tObj
);
3335 for (i
= 0; i
< n
; i
++) {
3336 sample_1d_array_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3341 /** Sample 1D Array texture, using lambda to choose between min/magnification */
3343 sample_lambda_1d_array(struct gl_context
*ctx
,
3344 const struct gl_sampler_object
*samp
,
3345 const struct gl_texture_object
*tObj
, GLuint n
,
3346 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3349 GLuint minStart
, minEnd
; /* texels with minification */
3350 GLuint magStart
, magEnd
; /* texels with magnification */
3353 assert(lambda
!= NULL
);
3354 compute_min_mag_ranges(samp
, n
, lambda
,
3355 &minStart
, &minEnd
, &magStart
, &magEnd
);
3357 if (minStart
< minEnd
) {
3358 /* do the minified texels */
3359 GLuint m
= minEnd
- minStart
;
3360 switch (samp
->MinFilter
) {
3362 for (i
= minStart
; i
< minEnd
; i
++)
3363 sample_1d_array_nearest(ctx
, samp
, _mesa_base_tex_image(tObj
),
3364 texcoords
[i
], rgba
[i
]);
3367 for (i
= minStart
; i
< minEnd
; i
++)
3368 sample_1d_array_linear(ctx
, samp
, _mesa_base_tex_image(tObj
),
3369 texcoords
[i
], rgba
[i
]);
3371 case GL_NEAREST_MIPMAP_NEAREST
:
3372 sample_1d_array_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
3373 lambda
+ minStart
, rgba
+ minStart
);
3375 case GL_LINEAR_MIPMAP_NEAREST
:
3376 sample_1d_array_linear_mipmap_nearest(ctx
, samp
, tObj
, m
,
3377 texcoords
+ minStart
,
3381 case GL_NEAREST_MIPMAP_LINEAR
:
3382 sample_1d_array_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
3383 lambda
+ minStart
, rgba
+ minStart
);
3385 case GL_LINEAR_MIPMAP_LINEAR
:
3386 sample_1d_array_linear_mipmap_linear(ctx
, samp
, tObj
, m
,
3387 texcoords
+ minStart
,
3392 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
3397 if (magStart
< magEnd
) {
3398 /* do the magnified texels */
3399 switch (samp
->MagFilter
) {
3401 for (i
= magStart
; i
< magEnd
; i
++)
3402 sample_1d_array_nearest(ctx
, samp
, _mesa_base_tex_image(tObj
),
3403 texcoords
[i
], rgba
[i
]);
3406 for (i
= magStart
; i
< magEnd
; i
++)
3407 sample_1d_array_linear(ctx
, samp
, _mesa_base_tex_image(tObj
),
3408 texcoords
[i
], rgba
[i
]);
3411 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
3419 * Compare texcoord against depth sample. Return 1.0 or 0.0 value.
3422 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
)
3426 return (coord
<= depthSample
) ? 1.0F
: 0.0F
;
3428 return (coord
>= depthSample
) ? 1.0F
: 0.0F
;
3430 return (coord
< depthSample
) ? 1.0F
: 0.0F
;
3432 return (coord
> depthSample
) ? 1.0F
: 0.0F
;
3434 return (coord
== depthSample
) ? 1.0F
: 0.0F
;
3436 return (coord
!= depthSample
) ? 1.0F
: 0.0F
;
3444 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
3451 * Compare texcoord against four depth samples.
3454 shadow_compare4(GLenum function
, GLfloat coord
,
3455 GLfloat depth00
, GLfloat depth01
,
3456 GLfloat depth10
, GLfloat depth11
,
3457 GLfloat wi
, GLfloat wj
)
3459 const GLfloat d
= 0.25F
;
3460 GLfloat luminance
= 1.0F
;
3464 if (coord
> depth00
) luminance
-= d
;
3465 if (coord
> depth01
) luminance
-= d
;
3466 if (coord
> depth10
) luminance
-= d
;
3467 if (coord
> depth11
) luminance
-= d
;
3470 if (coord
< depth00
) luminance
-= d
;
3471 if (coord
< depth01
) luminance
-= d
;
3472 if (coord
< depth10
) luminance
-= d
;
3473 if (coord
< depth11
) luminance
-= d
;
3476 if (coord
>= depth00
) luminance
-= d
;
3477 if (coord
>= depth01
) luminance
-= d
;
3478 if (coord
>= depth10
) luminance
-= d
;
3479 if (coord
>= depth11
) luminance
-= d
;
3482 if (coord
<= depth00
) luminance
-= d
;
3483 if (coord
<= depth01
) luminance
-= d
;
3484 if (coord
<= depth10
) luminance
-= d
;
3485 if (coord
<= depth11
) luminance
-= d
;
3488 if (coord
!= depth00
) luminance
-= d
;
3489 if (coord
!= depth01
) luminance
-= d
;
3490 if (coord
!= depth10
) luminance
-= d
;
3491 if (coord
!= depth11
) luminance
-= d
;
3494 if (coord
== depth00
) luminance
-= d
;
3495 if (coord
== depth01
) luminance
-= d
;
3496 if (coord
== depth10
) luminance
-= d
;
3497 if (coord
== depth11
) luminance
-= d
;
3504 /* ordinary bilinear filtering */
3505 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
3507 _mesa_problem(NULL
, "Bad compare func in sample_compare4");
3514 * Choose the mipmap level to use when sampling from a depth texture.
3517 choose_depth_texture_level(const struct gl_sampler_object
*samp
,
3518 const struct gl_texture_object
*tObj
, GLfloat lambda
)
3522 if (samp
->MinFilter
== GL_NEAREST
|| samp
->MinFilter
== GL_LINEAR
) {
3523 /* no mipmapping - use base level */
3524 level
= tObj
->BaseLevel
;
3527 /* choose mipmap level */
3528 lambda
= CLAMP(lambda
, samp
->MinLod
, samp
->MaxLod
);
3529 level
= (GLint
) lambda
;
3530 level
= CLAMP(level
, tObj
->BaseLevel
, tObj
->_MaxLevel
);
3538 * Sample a shadow/depth texture. This function is incomplete. It doesn't
3539 * check for minification vs. magnification, etc.
3542 sample_depth_texture( struct gl_context
*ctx
,
3543 const struct gl_sampler_object
*samp
,
3544 const struct gl_texture_object
*tObj
, GLuint n
,
3545 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3546 GLfloat texel
[][4] )
3548 const GLint level
= choose_depth_texture_level(samp
, tObj
, lambda
[0]);
3549 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
3550 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3551 const GLint width
= img
->Width
;
3552 const GLint height
= img
->Height
;
3553 const GLint depth
= img
->Depth
;
3554 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
3559 assert(img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
3560 img
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
3562 assert(tObj
->Target
== GL_TEXTURE_1D
||
3563 tObj
->Target
== GL_TEXTURE_2D
||
3564 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
3565 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
3566 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
||
3567 tObj
->Target
== GL_TEXTURE_CUBE_MAP
);
3569 /* XXXX if samp->MinFilter != samp->MagFilter, we're ignoring lambda */
3571 function
= (samp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
3572 samp
->CompareFunc
: GL_NONE
;
3574 if (samp
->MagFilter
== GL_NEAREST
) {
3576 for (i
= 0; i
< n
; i
++) {
3577 GLfloat depthSample
, depthRef
;
3578 GLint col
, row
, slice
;
3580 nearest_texcoord(samp
, tObj
, level
, texcoords
[i
], &col
, &row
, &slice
);
3582 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3583 slice
>= 0 && slice
< depth
) {
3584 swImg
->FetchTexel(swImg
, col
, row
, slice
, &depthSample
);
3587 depthSample
= samp
->BorderColor
.f
[0];
3590 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3592 result
= shadow_compare(function
, depthRef
, depthSample
);
3594 apply_depth_mode(tObj
->DepthMode
, result
, texel
[i
]);
3599 assert(samp
->MagFilter
== GL_LINEAR
);
3600 for (i
= 0; i
< n
; i
++) {
3601 GLfloat depth00
, depth01
, depth10
, depth11
, depthRef
;
3602 GLint i0
, i1
, j0
, j1
;
3605 GLuint useBorderTexel
;
3607 linear_texcoord(samp
, tObj
, level
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3614 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3620 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3621 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3622 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3623 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3626 if (slice
< 0 || slice
>= (GLint
) depth
) {
3627 depth00
= samp
->BorderColor
.f
[0];
3628 depth01
= samp
->BorderColor
.f
[0];
3629 depth10
= samp
->BorderColor
.f
[0];
3630 depth11
= samp
->BorderColor
.f
[0];
3633 /* get four depth samples from the texture */
3634 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3635 depth00
= samp
->BorderColor
.f
[0];
3638 swImg
->FetchTexel(swImg
, i0
, j0
, slice
, &depth00
);
3640 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3641 depth10
= samp
->BorderColor
.f
[0];
3644 swImg
->FetchTexel(swImg
, i1
, j0
, slice
, &depth10
);
3647 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3648 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3649 depth01
= samp
->BorderColor
.f
[0];
3652 swImg
->FetchTexel(swImg
, i0
, j1
, slice
, &depth01
);
3654 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3655 depth11
= samp
->BorderColor
.f
[0];
3658 swImg
->FetchTexel(swImg
, i1
, j1
, slice
, &depth11
);
3667 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3669 result
= shadow_compare4(function
, depthRef
,
3670 depth00
, depth01
, depth10
, depth11
,
3673 apply_depth_mode(tObj
->DepthMode
, result
, texel
[i
]);
3680 * We use this function when a texture object is in an "incomplete" state.
3681 * When a fragment program attempts to sample an incomplete texture we
3682 * return black (see issue 23 in GL_ARB_fragment_program spec).
3683 * Note: fragment programs don't observe the texture enable/disable flags.
3686 null_sample_func( struct gl_context
*ctx
,
3687 const struct gl_sampler_object
*samp
,
3688 const struct gl_texture_object
*tObj
, GLuint n
,
3689 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3698 for (i
= 0; i
< n
; i
++) {
3702 rgba
[i
][ACOMP
] = 1.0;
3708 * Choose the texture sampling function for the given texture object.
3711 _swrast_choose_texture_sample_func( struct gl_context
*ctx
,
3712 const struct gl_texture_object
*t
,
3713 const struct gl_sampler_object
*sampler
)
3715 if (!t
|| !_mesa_is_texture_complete(t
, sampler
,
3716 ctx
->Const
.ForceIntegerTexNearest
)) {
3717 return null_sample_func
;
3720 const GLboolean needLambda
=
3721 (GLboolean
) (sampler
->MinFilter
!= sampler
->MagFilter
);
3723 switch (t
->Target
) {
3725 if (is_depth_texture(t
)) {
3726 return sample_depth_texture
;
3728 else if (needLambda
) {
3729 return sample_lambda_1d
;
3731 else if (sampler
->MinFilter
== GL_LINEAR
) {
3732 return sample_linear_1d
;
3735 assert(sampler
->MinFilter
== GL_NEAREST
);
3736 return sample_nearest_1d
;
3739 if (is_depth_texture(t
)) {
3740 return sample_depth_texture
;
3742 else if (needLambda
) {
3743 /* Anisotropic filtering extension. Activated only if mipmaps are used */
3744 if (sampler
->MaxAnisotropy
> 1.0F
&&
3745 sampler
->MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
3746 return sample_lambda_2d_aniso
;
3748 return sample_lambda_2d
;
3750 else if (sampler
->MinFilter
== GL_LINEAR
) {
3751 return sample_linear_2d
;
3754 /* check for a few optimized cases */
3755 const struct gl_texture_image
*img
= _mesa_base_tex_image(t
);
3756 const struct swrast_texture_image
*swImg
=
3757 swrast_texture_image_const(img
);
3758 texture_sample_func func
;
3760 assert(sampler
->MinFilter
== GL_NEAREST
);
3761 func
= &sample_nearest_2d
;
3762 if (sampler
->WrapS
== GL_REPEAT
&&
3763 sampler
->WrapT
== GL_REPEAT
&&
3764 swImg
->_IsPowerOfTwo
&&
3766 if (img
->TexFormat
== MESA_FORMAT_BGR_UNORM8
)
3767 func
= &opt_sample_rgb_2d
;
3768 else if (img
->TexFormat
== MESA_FORMAT_A8B8G8R8_UNORM
)
3769 func
= &opt_sample_rgba_2d
;
3776 return sample_lambda_3d
;
3778 else if (sampler
->MinFilter
== GL_LINEAR
) {
3779 return sample_linear_3d
;
3782 assert(sampler
->MinFilter
== GL_NEAREST
);
3783 return sample_nearest_3d
;
3785 case GL_TEXTURE_CUBE_MAP
:
3787 return sample_lambda_cube
;
3789 else if (sampler
->MinFilter
== GL_LINEAR
) {
3790 return sample_linear_cube
;
3793 assert(sampler
->MinFilter
== GL_NEAREST
);
3794 return sample_nearest_cube
;
3796 case GL_TEXTURE_RECTANGLE_NV
:
3797 if (is_depth_texture(t
)) {
3798 return sample_depth_texture
;
3800 else if (needLambda
) {
3801 return sample_lambda_rect
;
3803 else if (sampler
->MinFilter
== GL_LINEAR
) {
3804 return sample_linear_rect
;
3807 assert(sampler
->MinFilter
== GL_NEAREST
);
3808 return sample_nearest_rect
;
3810 case GL_TEXTURE_1D_ARRAY_EXT
:
3811 if (is_depth_texture(t
)) {
3812 return sample_depth_texture
;
3814 else if (needLambda
) {
3815 return sample_lambda_1d_array
;
3817 else if (sampler
->MinFilter
== GL_LINEAR
) {
3818 return sample_linear_1d_array
;
3821 assert(sampler
->MinFilter
== GL_NEAREST
);
3822 return sample_nearest_1d_array
;
3824 case GL_TEXTURE_2D_ARRAY_EXT
:
3825 if (is_depth_texture(t
)) {
3826 return sample_depth_texture
;
3828 else if (needLambda
) {
3829 return sample_lambda_2d_array
;
3831 else if (sampler
->MinFilter
== GL_LINEAR
) {
3832 return sample_linear_2d_array
;
3835 assert(sampler
->MinFilter
== GL_NEAREST
);
3836 return sample_nearest_2d_array
;
3840 "invalid target in _swrast_choose_texture_sample_func");
3841 return null_sample_func
;