2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/colormac.h"
29 #include "main/imports.h"
31 #include "s_context.h"
32 #include "s_texfilter.h"
36 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
37 * see 1-pixel bands of improperly weighted linear-filtered textures.
38 * The tests/texwrap.c demo is a good test.
39 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
40 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
42 #define FRAC(f) ((f) - IFLOOR(f))
47 * Linear interpolation macro
49 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
53 * Do 2D/biliner interpolation of float values.
54 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
55 * a and b are the horizontal and vertical interpolants.
56 * It's important that this function is inlined when compiled with
57 * optimization! If we find that's not true on some systems, convert
61 lerp_2d(GLfloat a
, GLfloat b
,
62 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
64 const GLfloat temp0
= LERP(a
, v00
, v10
);
65 const GLfloat temp1
= LERP(a
, v01
, v11
);
66 return LERP(b
, temp0
, temp1
);
71 * Do 3D/trilinear interpolation of float values.
75 lerp_3d(GLfloat a
, GLfloat b
, GLfloat c
,
76 GLfloat v000
, GLfloat v100
, GLfloat v010
, GLfloat v110
,
77 GLfloat v001
, GLfloat v101
, GLfloat v011
, GLfloat v111
)
79 const GLfloat temp00
= LERP(a
, v000
, v100
);
80 const GLfloat temp10
= LERP(a
, v010
, v110
);
81 const GLfloat temp01
= LERP(a
, v001
, v101
);
82 const GLfloat temp11
= LERP(a
, v011
, v111
);
83 const GLfloat temp0
= LERP(b
, temp00
, temp10
);
84 const GLfloat temp1
= LERP(b
, temp01
, temp11
);
85 return LERP(c
, temp0
, temp1
);
90 * Do linear interpolation of colors.
93 lerp_rgba(GLfloat result
[4], GLfloat t
, const GLfloat a
[4], const GLfloat b
[4])
95 result
[0] = LERP(t
, a
[0], b
[0]);
96 result
[1] = LERP(t
, a
[1], b
[1]);
97 result
[2] = LERP(t
, a
[2], b
[2]);
98 result
[3] = LERP(t
, a
[3], b
[3]);
103 * Do bilinear interpolation of colors.
106 lerp_rgba_2d(GLfloat result
[4], GLfloat a
, GLfloat b
,
107 const GLfloat t00
[4], const GLfloat t10
[4],
108 const GLfloat t01
[4], const GLfloat t11
[4])
110 result
[0] = lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]);
111 result
[1] = lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]);
112 result
[2] = lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]);
113 result
[3] = lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]);
118 * Do trilinear interpolation of colors.
121 lerp_rgba_3d(GLfloat result
[4], GLfloat a
, GLfloat b
, GLfloat c
,
122 const GLfloat t000
[4], const GLfloat t100
[4],
123 const GLfloat t010
[4], const GLfloat t110
[4],
124 const GLfloat t001
[4], const GLfloat t101
[4],
125 const GLfloat t011
[4], const GLfloat t111
[4])
128 /* compiler should unroll these short loops */
129 for (k
= 0; k
< 4; k
++) {
130 result
[k
] = lerp_3d(a
, b
, c
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
131 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
137 * Used for GL_REPEAT wrap mode. Using A % B doesn't produce the
138 * right results for A<0. Casting to A to be unsigned only works if B
139 * is a power of two. Adding a bias to A (which is a multiple of B)
140 * avoids the problems with A < 0 (for reasonable A) without using a
143 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
147 * Used to compute texel locations for linear sampling.
149 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
150 * s = texcoord in [0,1]
151 * size = width (or height or depth) of texture
153 * i0, i1 = returns two nearest texel indexes
154 * weight = returns blend factor between texels
157 linear_texel_locations(GLenum wrapMode
,
158 const struct gl_texture_image
*img
,
159 GLint size
, GLfloat s
,
160 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
166 if (img
->_IsPowerOfTwo
) {
167 *i0
= IFLOOR(u
) & (size
- 1);
168 *i1
= (*i0
+ 1) & (size
- 1);
171 *i0
= REMAINDER(IFLOOR(u
), size
);
172 *i1
= REMAINDER(*i0
+ 1, size
);
175 case GL_CLAMP_TO_EDGE
:
187 if (*i1
>= (GLint
) size
)
190 case GL_CLAMP_TO_BORDER
:
192 const GLfloat min
= -1.0F
/ (2.0F
* size
);
193 const GLfloat max
= 1.0F
- min
;
205 case GL_MIRRORED_REPEAT
:
207 const GLint flr
= IFLOOR(s
);
209 u
= 1.0F
- (s
- (GLfloat
) flr
);
211 u
= s
- (GLfloat
) flr
;
212 u
= (u
* size
) - 0.5F
;
217 if (*i1
>= (GLint
) size
)
221 case GL_MIRROR_CLAMP_EXT
:
231 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
242 if (*i1
>= (GLint
) size
)
245 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
247 const GLfloat min
= -1.0F
/ (2.0F
* size
);
248 const GLfloat max
= 1.0F
- min
;
273 _mesa_problem(NULL
, "Bad wrap mode");
281 * Used to compute texel location for nearest sampling.
284 nearest_texel_location(GLenum wrapMode
,
285 const struct gl_texture_image
*img
,
286 GLint size
, GLfloat s
)
292 /* s limited to [0,1) */
293 /* i limited to [0,size-1] */
294 i
= IFLOOR(s
* size
);
295 if (img
->_IsPowerOfTwo
)
298 i
= REMAINDER(i
, size
);
300 case GL_CLAMP_TO_EDGE
:
302 /* s limited to [min,max] */
303 /* i limited to [0, size-1] */
304 const GLfloat min
= 1.0F
/ (2.0F
* size
);
305 const GLfloat max
= 1.0F
- min
;
311 i
= IFLOOR(s
* size
);
314 case GL_CLAMP_TO_BORDER
:
316 /* s limited to [min,max] */
317 /* i limited to [-1, size] */
318 const GLfloat min
= -1.0F
/ (2.0F
* size
);
319 const GLfloat max
= 1.0F
- min
;
325 i
= IFLOOR(s
* size
);
328 case GL_MIRRORED_REPEAT
:
330 const GLfloat min
= 1.0F
/ (2.0F
* size
);
331 const GLfloat max
= 1.0F
- min
;
332 const GLint flr
= IFLOOR(s
);
335 u
= 1.0F
- (s
- (GLfloat
) flr
);
337 u
= s
- (GLfloat
) flr
;
343 i
= IFLOOR(u
* size
);
346 case GL_MIRROR_CLAMP_EXT
:
348 /* s limited to [0,1] */
349 /* i limited to [0,size-1] */
350 const GLfloat u
= FABSF(s
);
356 i
= IFLOOR(u
* size
);
359 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
361 /* s limited to [min,max] */
362 /* i limited to [0, size-1] */
363 const GLfloat min
= 1.0F
/ (2.0F
* size
);
364 const GLfloat max
= 1.0F
- min
;
365 const GLfloat u
= FABSF(s
);
371 i
= IFLOOR(u
* size
);
374 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
376 /* s limited to [min,max] */
377 /* i limited to [0, size-1] */
378 const GLfloat min
= -1.0F
/ (2.0F
* size
);
379 const GLfloat max
= 1.0F
- min
;
380 const GLfloat u
= FABSF(s
);
386 i
= IFLOOR(u
* size
);
390 /* s limited to [0,1] */
391 /* i limited to [0,size-1] */
397 i
= IFLOOR(s
* size
);
400 _mesa_problem(NULL
, "Bad wrap mode");
406 /* Power of two image sizes only */
408 linear_repeat_texel_location(GLuint size
, GLfloat s
,
409 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
411 GLfloat u
= s
* size
- 0.5F
;
412 *i0
= IFLOOR(u
) & (size
- 1);
413 *i1
= (*i0
+ 1) & (size
- 1);
419 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
422 clamp_rect_coord_nearest(GLenum wrapMode
, GLfloat coord
, GLint max
)
426 return IFLOOR( CLAMP(coord
, 0.0F
, max
- 1) );
427 case GL_CLAMP_TO_EDGE
:
428 return IFLOOR( CLAMP(coord
, 0.5F
, max
- 0.5F
) );
429 case GL_CLAMP_TO_BORDER
:
430 return IFLOOR( CLAMP(coord
, -0.5F
, max
+ 0.5F
) );
432 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_nearest");
439 * As above, but GL_LINEAR filtering.
442 clamp_rect_coord_linear(GLenum wrapMode
, GLfloat coord
, GLint max
,
443 GLint
*i0out
, GLint
*i1out
, GLfloat
*weight
)
449 /* Not exactly what the spec says, but it matches NVIDIA output */
450 fcol
= CLAMP(coord
- 0.5F
, 0.0F
, max
- 1);
454 case GL_CLAMP_TO_EDGE
:
455 fcol
= CLAMP(coord
, 0.5F
, max
- 0.5F
);
462 case GL_CLAMP_TO_BORDER
:
463 fcol
= CLAMP(coord
, -0.5F
, max
+ 0.5F
);
469 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_linear");
475 *weight
= FRAC(fcol
);
480 * Compute slice/image to use for 1D or 2D array texture.
483 tex_array_slice(GLfloat coord
, GLsizei size
)
485 GLint slice
= IFLOOR(coord
+ 0.5f
);
486 slice
= CLAMP(slice
, 0, size
- 1);
492 * Compute nearest integer texcoords for given texobj and coordinate.
493 * NOTE: only used for depth texture sampling.
496 nearest_texcoord(const struct gl_texture_object
*texObj
,
498 const GLfloat texcoord
[4],
499 GLint
*i
, GLint
*j
, GLint
*k
)
501 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
502 const GLint width
= img
->Width
;
503 const GLint height
= img
->Height
;
504 const GLint depth
= img
->Depth
;
506 switch (texObj
->Target
) {
507 case GL_TEXTURE_RECTANGLE_ARB
:
508 *i
= clamp_rect_coord_nearest(texObj
->WrapS
, texcoord
[0], width
);
509 *j
= clamp_rect_coord_nearest(texObj
->WrapT
, texcoord
[1], height
);
513 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
518 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
519 *j
= nearest_texel_location(texObj
->WrapT
, img
, height
, texcoord
[1]);
522 case GL_TEXTURE_1D_ARRAY_EXT
:
523 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
524 *j
= tex_array_slice(texcoord
[1], height
);
527 case GL_TEXTURE_2D_ARRAY_EXT
:
528 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
529 *j
= nearest_texel_location(texObj
->WrapT
, img
, height
, texcoord
[1]);
530 *k
= tex_array_slice(texcoord
[2], depth
);
539 * Compute linear integer texcoords for given texobj and coordinate.
540 * NOTE: only used for depth texture sampling.
543 linear_texcoord(const struct gl_texture_object
*texObj
,
545 const GLfloat texcoord
[4],
546 GLint
*i0
, GLint
*i1
, GLint
*j0
, GLint
*j1
, GLint
*slice
,
547 GLfloat
*wi
, GLfloat
*wj
)
549 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
550 const GLint width
= img
->Width
;
551 const GLint height
= img
->Height
;
552 const GLint depth
= img
->Depth
;
554 switch (texObj
->Target
) {
555 case GL_TEXTURE_RECTANGLE_ARB
:
556 clamp_rect_coord_linear(texObj
->WrapS
, texcoord
[0],
558 clamp_rect_coord_linear(texObj
->WrapT
, texcoord
[1],
565 linear_texel_locations(texObj
->WrapS
, img
, width
,
566 texcoord
[0], i0
, i1
, wi
);
567 linear_texel_locations(texObj
->WrapT
, img
, height
,
568 texcoord
[1], j0
, j1
, wj
);
572 case GL_TEXTURE_1D_ARRAY_EXT
:
573 linear_texel_locations(texObj
->WrapS
, img
, width
,
574 texcoord
[0], i0
, i1
, wi
);
575 *j0
= tex_array_slice(texcoord
[1], height
);
580 case GL_TEXTURE_2D_ARRAY_EXT
:
581 linear_texel_locations(texObj
->WrapS
, img
, width
,
582 texcoord
[0], i0
, i1
, wi
);
583 linear_texel_locations(texObj
->WrapT
, img
, height
,
584 texcoord
[1], j0
, j1
, wj
);
585 *slice
= tex_array_slice(texcoord
[2], depth
);
596 * For linear interpolation between mipmap levels N and N+1, this function
600 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
603 return tObj
->BaseLevel
;
604 else if (lambda
> tObj
->_MaxLambda
)
605 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
607 return (GLint
) (tObj
->BaseLevel
+ lambda
);
612 * Compute the nearest mipmap level to take texels from.
615 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
621 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
622 l
= tObj
->_MaxLambda
+ 0.4999F
;
625 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
626 if (level
> tObj
->_MaxLevel
)
627 level
= tObj
->_MaxLevel
;
634 * Bitflags for texture border color sampling.
646 * The lambda[] array values are always monotonic. Either the whole span
647 * will be minified, magnified, or split between the two. This function
648 * determines the subranges in [0, n-1] that are to be minified or magnified.
651 compute_min_mag_ranges(const struct gl_texture_object
*tObj
,
652 GLuint n
, const GLfloat lambda
[],
653 GLuint
*minStart
, GLuint
*minEnd
,
654 GLuint
*magStart
, GLuint
*magEnd
)
656 GLfloat minMagThresh
;
658 /* we shouldn't be here if minfilter == magfilter */
659 ASSERT(tObj
->MinFilter
!= tObj
->MagFilter
);
661 /* This bit comes from the OpenGL spec: */
662 if (tObj
->MagFilter
== GL_LINEAR
663 && (tObj
->MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
664 tObj
->MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
672 /* DEBUG CODE: Verify that lambda[] is monotonic.
673 * We can't really use this because the inaccuracy in the LOG2 function
674 * causes this test to fail, yet the resulting texturing is correct.
678 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
679 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
680 for (i
= 0; i
< n
- 1; i
++) {
681 ASSERT((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
684 else { /* increasing */
685 for (i
= 0; i
< n
- 1; i
++) {
686 ASSERT((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
692 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
693 /* magnification for whole span */
696 *minStart
= *minEnd
= 0;
698 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
699 /* minification for whole span */
702 *magStart
= *magEnd
= 0;
705 /* a mix of minification and magnification */
707 if (lambda
[0] > minMagThresh
) {
708 /* start with minification */
709 for (i
= 1; i
< n
; i
++) {
710 if (lambda
[i
] <= minMagThresh
)
719 /* start with magnification */
720 for (i
= 1; i
< n
; i
++) {
721 if (lambda
[i
] > minMagThresh
)
732 /* Verify the min/mag Start/End values
733 * We don't use this either (see above)
737 for (i
= 0; i
< n
; i
++) {
738 if (lambda
[i
] > minMagThresh
) {
740 ASSERT(i
>= *minStart
);
745 ASSERT(i
>= *magStart
);
755 * When we sample the border color, it must be interpreted according to
756 * the base texture format. Ex: if the texture base format it GL_ALPHA,
757 * we return (0,0,0,BorderAlpha).
760 get_border_color(const struct gl_texture_object
*tObj
,
761 const struct gl_texture_image
*img
,
764 switch (img
->_BaseFormat
) {
766 rgba
[0] = tObj
->BorderColor
.f
[0];
767 rgba
[1] = tObj
->BorderColor
.f
[1];
768 rgba
[2] = tObj
->BorderColor
.f
[2];
772 rgba
[0] = rgba
[1] = rgba
[2] = 0.0;
773 rgba
[3] = tObj
->BorderColor
.f
[3];
776 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->BorderColor
.f
[0];
779 case GL_LUMINANCE_ALPHA
:
780 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->BorderColor
.f
[0];
781 rgba
[3] = tObj
->BorderColor
.f
[3];
784 rgba
[0] = rgba
[1] = rgba
[2] = rgba
[3] = tObj
->BorderColor
.f
[0];
787 COPY_4V(rgba
, tObj
->BorderColor
.f
);
792 /**********************************************************************/
793 /* 1-D Texture Sampling Functions */
794 /**********************************************************************/
797 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
800 sample_1d_nearest(struct gl_context
*ctx
,
801 const struct gl_texture_object
*tObj
,
802 const struct gl_texture_image
*img
,
803 const GLfloat texcoord
[4], GLfloat rgba
[4])
805 const GLint width
= img
->Width2
; /* without border, power of two */
807 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
808 /* skip over the border, if any */
810 if (i
< 0 || i
>= (GLint
) img
->Width
) {
811 /* Need this test for GL_CLAMP_TO_BORDER mode */
812 get_border_color(tObj
, img
, rgba
);
815 img
->FetchTexelf(img
, i
, 0, 0, rgba
);
821 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
824 sample_1d_linear(struct gl_context
*ctx
,
825 const struct gl_texture_object
*tObj
,
826 const struct gl_texture_image
*img
,
827 const GLfloat texcoord
[4], GLfloat rgba
[4])
829 const GLint width
= img
->Width2
;
831 GLbitfield useBorderColor
= 0x0;
833 GLfloat t0
[4], t1
[4]; /* texels */
835 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
842 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
843 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
846 /* fetch texel colors */
847 if (useBorderColor
& I0BIT
) {
848 get_border_color(tObj
, img
, t0
);
851 img
->FetchTexelf(img
, i0
, 0, 0, t0
);
853 if (useBorderColor
& I1BIT
) {
854 get_border_color(tObj
, img
, t1
);
857 img
->FetchTexelf(img
, i1
, 0, 0, t1
);
860 lerp_rgba(rgba
, a
, t0
, t1
);
865 sample_1d_nearest_mipmap_nearest(struct gl_context
*ctx
,
866 const struct gl_texture_object
*tObj
,
867 GLuint n
, const GLfloat texcoord
[][4],
868 const GLfloat lambda
[], GLfloat rgba
[][4])
871 ASSERT(lambda
!= NULL
);
872 for (i
= 0; i
< n
; i
++) {
873 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
874 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
880 sample_1d_linear_mipmap_nearest(struct gl_context
*ctx
,
881 const struct gl_texture_object
*tObj
,
882 GLuint n
, const GLfloat texcoord
[][4],
883 const GLfloat lambda
[], GLfloat rgba
[][4])
886 ASSERT(lambda
!= NULL
);
887 for (i
= 0; i
< n
; i
++) {
888 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
889 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
895 sample_1d_nearest_mipmap_linear(struct gl_context
*ctx
,
896 const struct gl_texture_object
*tObj
,
897 GLuint n
, const GLfloat texcoord
[][4],
898 const GLfloat lambda
[], GLfloat rgba
[][4])
901 ASSERT(lambda
!= NULL
);
902 for (i
= 0; i
< n
; i
++) {
903 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
904 if (level
>= tObj
->_MaxLevel
) {
905 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
906 texcoord
[i
], rgba
[i
]);
909 GLfloat t0
[4], t1
[4];
910 const GLfloat f
= FRAC(lambda
[i
]);
911 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
912 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
913 lerp_rgba(rgba
[i
], f
, t0
, t1
);
920 sample_1d_linear_mipmap_linear(struct gl_context
*ctx
,
921 const struct gl_texture_object
*tObj
,
922 GLuint n
, const GLfloat texcoord
[][4],
923 const GLfloat lambda
[], GLfloat rgba
[][4])
926 ASSERT(lambda
!= NULL
);
927 for (i
= 0; i
< n
; i
++) {
928 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
929 if (level
>= tObj
->_MaxLevel
) {
930 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
931 texcoord
[i
], rgba
[i
]);
934 GLfloat t0
[4], t1
[4];
935 const GLfloat f
= FRAC(lambda
[i
]);
936 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
937 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
938 lerp_rgba(rgba
[i
], f
, t0
, t1
);
944 /** Sample 1D texture, nearest filtering for both min/magnification */
946 sample_nearest_1d( struct gl_context
*ctx
,
947 const struct gl_texture_object
*tObj
, GLuint n
,
948 const GLfloat texcoords
[][4], const GLfloat lambda
[],
952 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
954 for (i
= 0; i
< n
; i
++) {
955 sample_1d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
960 /** Sample 1D texture, linear filtering for both min/magnification */
962 sample_linear_1d( struct gl_context
*ctx
,
963 const struct gl_texture_object
*tObj
, GLuint n
,
964 const GLfloat texcoords
[][4], const GLfloat lambda
[],
968 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
970 for (i
= 0; i
< n
; i
++) {
971 sample_1d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
976 /** Sample 1D texture, using lambda to choose between min/magnification */
978 sample_lambda_1d( struct gl_context
*ctx
,
979 const struct gl_texture_object
*tObj
, GLuint n
,
980 const GLfloat texcoords
[][4],
981 const GLfloat lambda
[], GLfloat rgba
[][4] )
983 GLuint minStart
, minEnd
; /* texels with minification */
984 GLuint magStart
, magEnd
; /* texels with magnification */
987 ASSERT(lambda
!= NULL
);
988 compute_min_mag_ranges(tObj
, n
, lambda
,
989 &minStart
, &minEnd
, &magStart
, &magEnd
);
991 if (minStart
< minEnd
) {
992 /* do the minified texels */
993 const GLuint m
= minEnd
- minStart
;
994 switch (tObj
->MinFilter
) {
996 for (i
= minStart
; i
< minEnd
; i
++)
997 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
998 texcoords
[i
], rgba
[i
]);
1001 for (i
= minStart
; i
< minEnd
; i
++)
1002 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1003 texcoords
[i
], rgba
[i
]);
1005 case GL_NEAREST_MIPMAP_NEAREST
:
1006 sample_1d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1007 lambda
+ minStart
, rgba
+ minStart
);
1009 case GL_LINEAR_MIPMAP_NEAREST
:
1010 sample_1d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1011 lambda
+ minStart
, rgba
+ minStart
);
1013 case GL_NEAREST_MIPMAP_LINEAR
:
1014 sample_1d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1015 lambda
+ minStart
, rgba
+ minStart
);
1017 case GL_LINEAR_MIPMAP_LINEAR
:
1018 sample_1d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1019 lambda
+ minStart
, rgba
+ minStart
);
1022 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
1027 if (magStart
< magEnd
) {
1028 /* do the magnified texels */
1029 switch (tObj
->MagFilter
) {
1031 for (i
= magStart
; i
< magEnd
; i
++)
1032 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1033 texcoords
[i
], rgba
[i
]);
1036 for (i
= magStart
; i
< magEnd
; i
++)
1037 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1038 texcoords
[i
], rgba
[i
]);
1041 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
1048 /**********************************************************************/
1049 /* 2-D Texture Sampling Functions */
1050 /**********************************************************************/
1054 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1057 sample_2d_nearest(struct gl_context
*ctx
,
1058 const struct gl_texture_object
*tObj
,
1059 const struct gl_texture_image
*img
,
1060 const GLfloat texcoord
[4],
1063 const GLint width
= img
->Width2
; /* without border, power of two */
1064 const GLint height
= img
->Height2
; /* without border, power of two */
1068 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
1069 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
1071 /* skip over the border, if any */
1075 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
1076 /* Need this test for GL_CLAMP_TO_BORDER mode */
1077 get_border_color(tObj
, img
, rgba
);
1080 img
->FetchTexelf(img
, i
, j
, 0, rgba
);
1086 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1087 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1090 sample_2d_linear(struct gl_context
*ctx
,
1091 const struct gl_texture_object
*tObj
,
1092 const struct gl_texture_image
*img
,
1093 const GLfloat texcoord
[4],
1096 const GLint width
= img
->Width2
;
1097 const GLint height
= img
->Height2
;
1098 GLint i0
, j0
, i1
, j1
;
1099 GLbitfield useBorderColor
= 0x0;
1101 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1103 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1104 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1113 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1114 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1115 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1116 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1119 /* fetch four texel colors */
1120 if (useBorderColor
& (I0BIT
| J0BIT
)) {
1121 get_border_color(tObj
, img
, t00
);
1124 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
1126 if (useBorderColor
& (I1BIT
| J0BIT
)) {
1127 get_border_color(tObj
, img
, t10
);
1130 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
1132 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1133 get_border_color(tObj
, img
, t01
);
1136 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
1138 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1139 get_border_color(tObj
, img
, t11
);
1142 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
1145 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1150 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1151 * We don't have to worry about the texture border.
1154 sample_2d_linear_repeat(struct gl_context
*ctx
,
1155 const struct gl_texture_object
*tObj
,
1156 const struct gl_texture_image
*img
,
1157 const GLfloat texcoord
[4],
1160 const GLint width
= img
->Width2
;
1161 const GLint height
= img
->Height2
;
1162 GLint i0
, j0
, i1
, j1
;
1164 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1168 ASSERT(tObj
->WrapS
== GL_REPEAT
);
1169 ASSERT(tObj
->WrapT
== GL_REPEAT
);
1170 ASSERT(img
->Border
== 0);
1171 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
1172 ASSERT(img
->_IsPowerOfTwo
);
1174 linear_repeat_texel_location(width
, texcoord
[0], &i0
, &i1
, &wi
);
1175 linear_repeat_texel_location(height
, texcoord
[1], &j0
, &j1
, &wj
);
1177 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
1178 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
1179 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
1180 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
1182 lerp_rgba_2d(rgba
, wi
, wj
, t00
, t10
, t01
, t11
);
1187 sample_2d_nearest_mipmap_nearest(struct gl_context
*ctx
,
1188 const struct gl_texture_object
*tObj
,
1189 GLuint n
, const GLfloat texcoord
[][4],
1190 const GLfloat lambda
[], GLfloat rgba
[][4])
1193 for (i
= 0; i
< n
; i
++) {
1194 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1195 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1201 sample_2d_linear_mipmap_nearest(struct gl_context
*ctx
,
1202 const struct gl_texture_object
*tObj
,
1203 GLuint n
, const GLfloat texcoord
[][4],
1204 const GLfloat lambda
[], GLfloat rgba
[][4])
1207 ASSERT(lambda
!= NULL
);
1208 for (i
= 0; i
< n
; i
++) {
1209 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1210 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1216 sample_2d_nearest_mipmap_linear(struct gl_context
*ctx
,
1217 const struct gl_texture_object
*tObj
,
1218 GLuint n
, const GLfloat texcoord
[][4],
1219 const GLfloat lambda
[], GLfloat rgba
[][4])
1222 ASSERT(lambda
!= NULL
);
1223 for (i
= 0; i
< n
; i
++) {
1224 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1225 if (level
>= tObj
->_MaxLevel
) {
1226 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1227 texcoord
[i
], rgba
[i
]);
1230 GLfloat t0
[4], t1
[4]; /* texels */
1231 const GLfloat f
= FRAC(lambda
[i
]);
1232 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1233 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1234 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1241 sample_2d_linear_mipmap_linear( struct gl_context
*ctx
,
1242 const struct gl_texture_object
*tObj
,
1243 GLuint n
, const GLfloat texcoord
[][4],
1244 const GLfloat lambda
[], GLfloat rgba
[][4] )
1247 ASSERT(lambda
!= NULL
);
1248 for (i
= 0; i
< n
; i
++) {
1249 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1250 if (level
>= tObj
->_MaxLevel
) {
1251 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1252 texcoord
[i
], rgba
[i
]);
1255 GLfloat t0
[4], t1
[4]; /* texels */
1256 const GLfloat f
= FRAC(lambda
[i
]);
1257 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1258 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1259 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1266 sample_2d_linear_mipmap_linear_repeat(struct gl_context
*ctx
,
1267 const struct gl_texture_object
*tObj
,
1268 GLuint n
, const GLfloat texcoord
[][4],
1269 const GLfloat lambda
[], GLfloat rgba
[][4])
1272 ASSERT(lambda
!= NULL
);
1273 ASSERT(tObj
->WrapS
== GL_REPEAT
);
1274 ASSERT(tObj
->WrapT
== GL_REPEAT
);
1275 for (i
= 0; i
< n
; i
++) {
1276 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1277 if (level
>= tObj
->_MaxLevel
) {
1278 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1279 texcoord
[i
], rgba
[i
]);
1282 GLfloat t0
[4], t1
[4]; /* texels */
1283 const GLfloat f
= FRAC(lambda
[i
]);
1284 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
],
1286 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
+1],
1288 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1294 /** Sample 2D texture, nearest filtering for both min/magnification */
1296 sample_nearest_2d(struct gl_context
*ctx
,
1297 const struct gl_texture_object
*tObj
, GLuint n
,
1298 const GLfloat texcoords
[][4],
1299 const GLfloat lambda
[], GLfloat rgba
[][4])
1302 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1304 for (i
= 0; i
< n
; i
++) {
1305 sample_2d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1310 /** Sample 2D texture, linear filtering for both min/magnification */
1312 sample_linear_2d(struct gl_context
*ctx
,
1313 const struct gl_texture_object
*tObj
, GLuint n
,
1314 const GLfloat texcoords
[][4],
1315 const GLfloat lambda
[], GLfloat rgba
[][4])
1318 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1320 if (tObj
->WrapS
== GL_REPEAT
&&
1321 tObj
->WrapT
== GL_REPEAT
&&
1322 image
->_IsPowerOfTwo
&&
1323 image
->Border
== 0) {
1324 for (i
= 0; i
< n
; i
++) {
1325 sample_2d_linear_repeat(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1329 for (i
= 0; i
< n
; i
++) {
1330 sample_2d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1337 * Optimized 2-D texture sampling:
1338 * S and T wrap mode == GL_REPEAT
1339 * GL_NEAREST min/mag filter
1341 * RowStride == Width,
1345 opt_sample_rgb_2d(struct gl_context
*ctx
,
1346 const struct gl_texture_object
*tObj
,
1347 GLuint n
, const GLfloat texcoords
[][4],
1348 const GLfloat lambda
[], GLfloat rgba
[][4])
1350 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1351 const GLfloat width
= (GLfloat
) img
->Width
;
1352 const GLfloat height
= (GLfloat
) img
->Height
;
1353 const GLint colMask
= img
->Width
- 1;
1354 const GLint rowMask
= img
->Height
- 1;
1355 const GLint shift
= img
->WidthLog2
;
1359 ASSERT(tObj
->WrapS
==GL_REPEAT
);
1360 ASSERT(tObj
->WrapT
==GL_REPEAT
);
1361 ASSERT(img
->Border
==0);
1362 ASSERT(img
->TexFormat
== MESA_FORMAT_RGB888
);
1363 ASSERT(img
->_IsPowerOfTwo
);
1365 for (k
=0; k
<n
; k
++) {
1366 GLint i
= IFLOOR(texcoords
[k
][0] * width
) & colMask
;
1367 GLint j
= IFLOOR(texcoords
[k
][1] * height
) & rowMask
;
1368 GLint pos
= (j
<< shift
) | i
;
1369 GLubyte
*texel
= ((GLubyte
*) img
->Data
) + 3*pos
;
1370 rgba
[k
][RCOMP
] = UBYTE_TO_FLOAT(texel
[2]);
1371 rgba
[k
][GCOMP
] = UBYTE_TO_FLOAT(texel
[1]);
1372 rgba
[k
][BCOMP
] = UBYTE_TO_FLOAT(texel
[0]);
1373 rgba
[k
][ACOMP
] = 1.0F
;
1379 * Optimized 2-D texture sampling:
1380 * S and T wrap mode == GL_REPEAT
1381 * GL_NEAREST min/mag filter
1383 * RowStride == Width,
1387 opt_sample_rgba_2d(struct gl_context
*ctx
,
1388 const struct gl_texture_object
*tObj
,
1389 GLuint n
, const GLfloat texcoords
[][4],
1390 const GLfloat lambda
[], GLfloat rgba
[][4])
1392 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1393 const GLfloat width
= (GLfloat
) img
->Width
;
1394 const GLfloat height
= (GLfloat
) img
->Height
;
1395 const GLint colMask
= img
->Width
- 1;
1396 const GLint rowMask
= img
->Height
- 1;
1397 const GLint shift
= img
->WidthLog2
;
1401 ASSERT(tObj
->WrapS
==GL_REPEAT
);
1402 ASSERT(tObj
->WrapT
==GL_REPEAT
);
1403 ASSERT(img
->Border
==0);
1404 ASSERT(img
->TexFormat
== MESA_FORMAT_RGBA8888
);
1405 ASSERT(img
->_IsPowerOfTwo
);
1407 for (i
= 0; i
< n
; i
++) {
1408 const GLint col
= IFLOOR(texcoords
[i
][0] * width
) & colMask
;
1409 const GLint row
= IFLOOR(texcoords
[i
][1] * height
) & rowMask
;
1410 const GLint pos
= (row
<< shift
) | col
;
1411 const GLuint texel
= *((GLuint
*) img
->Data
+ pos
);
1412 rgba
[i
][RCOMP
] = UBYTE_TO_FLOAT( (texel
>> 24) );
1413 rgba
[i
][GCOMP
] = UBYTE_TO_FLOAT( (texel
>> 16) & 0xff );
1414 rgba
[i
][BCOMP
] = UBYTE_TO_FLOAT( (texel
>> 8) & 0xff );
1415 rgba
[i
][ACOMP
] = UBYTE_TO_FLOAT( (texel
) & 0xff );
1420 /** Sample 2D texture, using lambda to choose between min/magnification */
1422 sample_lambda_2d(struct gl_context
*ctx
,
1423 const struct gl_texture_object
*tObj
,
1424 GLuint n
, const GLfloat texcoords
[][4],
1425 const GLfloat lambda
[], GLfloat rgba
[][4])
1427 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1428 GLuint minStart
, minEnd
; /* texels with minification */
1429 GLuint magStart
, magEnd
; /* texels with magnification */
1431 const GLboolean repeatNoBorderPOT
= (tObj
->WrapS
== GL_REPEAT
)
1432 && (tObj
->WrapT
== GL_REPEAT
)
1433 && (tImg
->Border
== 0 && (tImg
->Width
== tImg
->RowStride
))
1434 && (tImg
->_BaseFormat
!= GL_COLOR_INDEX
)
1435 && tImg
->_IsPowerOfTwo
;
1437 ASSERT(lambda
!= NULL
);
1438 compute_min_mag_ranges(tObj
, n
, lambda
,
1439 &minStart
, &minEnd
, &magStart
, &magEnd
);
1441 if (minStart
< minEnd
) {
1442 /* do the minified texels */
1443 const GLuint m
= minEnd
- minStart
;
1444 switch (tObj
->MinFilter
) {
1446 if (repeatNoBorderPOT
) {
1447 switch (tImg
->TexFormat
) {
1448 case MESA_FORMAT_RGB888
:
1449 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1450 NULL
, rgba
+ minStart
);
1452 case MESA_FORMAT_RGBA8888
:
1453 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1454 NULL
, rgba
+ minStart
);
1457 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1458 NULL
, rgba
+ minStart
);
1462 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1463 NULL
, rgba
+ minStart
);
1467 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1468 NULL
, rgba
+ minStart
);
1470 case GL_NEAREST_MIPMAP_NEAREST
:
1471 sample_2d_nearest_mipmap_nearest(ctx
, tObj
, m
,
1472 texcoords
+ minStart
,
1473 lambda
+ minStart
, rgba
+ minStart
);
1475 case GL_LINEAR_MIPMAP_NEAREST
:
1476 sample_2d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1477 lambda
+ minStart
, rgba
+ minStart
);
1479 case GL_NEAREST_MIPMAP_LINEAR
:
1480 sample_2d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1481 lambda
+ minStart
, rgba
+ minStart
);
1483 case GL_LINEAR_MIPMAP_LINEAR
:
1484 if (repeatNoBorderPOT
)
1485 sample_2d_linear_mipmap_linear_repeat(ctx
, tObj
, m
,
1486 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1488 sample_2d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1489 lambda
+ minStart
, rgba
+ minStart
);
1492 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1497 if (magStart
< magEnd
) {
1498 /* do the magnified texels */
1499 const GLuint m
= magEnd
- magStart
;
1501 switch (tObj
->MagFilter
) {
1503 if (repeatNoBorderPOT
) {
1504 switch (tImg
->TexFormat
) {
1505 case MESA_FORMAT_RGB888
:
1506 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1507 NULL
, rgba
+ magStart
);
1509 case MESA_FORMAT_RGBA8888
:
1510 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1511 NULL
, rgba
+ magStart
);
1514 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1515 NULL
, rgba
+ magStart
);
1519 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1520 NULL
, rgba
+ magStart
);
1524 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1525 NULL
, rgba
+ magStart
);
1528 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1535 /**********************************************************************/
1536 /* 3-D Texture Sampling Functions */
1537 /**********************************************************************/
1540 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1543 sample_3d_nearest(struct gl_context
*ctx
,
1544 const struct gl_texture_object
*tObj
,
1545 const struct gl_texture_image
*img
,
1546 const GLfloat texcoord
[4],
1549 const GLint width
= img
->Width2
; /* without border, power of two */
1550 const GLint height
= img
->Height2
; /* without border, power of two */
1551 const GLint depth
= img
->Depth2
; /* without border, power of two */
1555 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
1556 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
1557 k
= nearest_texel_location(tObj
->WrapR
, img
, depth
, texcoord
[2]);
1559 if (i
< 0 || i
>= (GLint
) img
->Width
||
1560 j
< 0 || j
>= (GLint
) img
->Height
||
1561 k
< 0 || k
>= (GLint
) img
->Depth
) {
1562 /* Need this test for GL_CLAMP_TO_BORDER mode */
1563 get_border_color(tObj
, img
, rgba
);
1566 img
->FetchTexelf(img
, i
, j
, k
, rgba
);
1572 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1575 sample_3d_linear(struct gl_context
*ctx
,
1576 const struct gl_texture_object
*tObj
,
1577 const struct gl_texture_image
*img
,
1578 const GLfloat texcoord
[4],
1581 const GLint width
= img
->Width2
;
1582 const GLint height
= img
->Height2
;
1583 const GLint depth
= img
->Depth2
;
1584 GLint i0
, j0
, k0
, i1
, j1
, k1
;
1585 GLbitfield useBorderColor
= 0x0;
1587 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
1588 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
1590 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1591 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1592 linear_texel_locations(tObj
->WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
1603 /* check if sampling texture border color */
1604 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1605 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1606 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1607 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1608 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
1609 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
1613 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
1614 get_border_color(tObj
, img
, t000
);
1617 img
->FetchTexelf(img
, i0
, j0
, k0
, t000
);
1619 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
1620 get_border_color(tObj
, img
, t100
);
1623 img
->FetchTexelf(img
, i1
, j0
, k0
, t100
);
1625 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
1626 get_border_color(tObj
, img
, t010
);
1629 img
->FetchTexelf(img
, i0
, j1
, k0
, t010
);
1631 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
1632 get_border_color(tObj
, img
, t110
);
1635 img
->FetchTexelf(img
, i1
, j1
, k0
, t110
);
1638 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
1639 get_border_color(tObj
, img
, t001
);
1642 img
->FetchTexelf(img
, i0
, j0
, k1
, t001
);
1644 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
1645 get_border_color(tObj
, img
, t101
);
1648 img
->FetchTexelf(img
, i1
, j0
, k1
, t101
);
1650 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
1651 get_border_color(tObj
, img
, t011
);
1654 img
->FetchTexelf(img
, i0
, j1
, k1
, t011
);
1656 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
1657 get_border_color(tObj
, img
, t111
);
1660 img
->FetchTexelf(img
, i1
, j1
, k1
, t111
);
1663 /* trilinear interpolation of samples */
1664 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
1669 sample_3d_nearest_mipmap_nearest(struct gl_context
*ctx
,
1670 const struct gl_texture_object
*tObj
,
1671 GLuint n
, const GLfloat texcoord
[][4],
1672 const GLfloat lambda
[], GLfloat rgba
[][4] )
1675 for (i
= 0; i
< n
; i
++) {
1676 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1677 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1683 sample_3d_linear_mipmap_nearest(struct gl_context
*ctx
,
1684 const struct gl_texture_object
*tObj
,
1685 GLuint n
, const GLfloat texcoord
[][4],
1686 const GLfloat lambda
[], GLfloat rgba
[][4])
1689 ASSERT(lambda
!= NULL
);
1690 for (i
= 0; i
< n
; i
++) {
1691 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1692 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1698 sample_3d_nearest_mipmap_linear(struct gl_context
*ctx
,
1699 const struct gl_texture_object
*tObj
,
1700 GLuint n
, const GLfloat texcoord
[][4],
1701 const GLfloat lambda
[], GLfloat rgba
[][4])
1704 ASSERT(lambda
!= NULL
);
1705 for (i
= 0; i
< n
; i
++) {
1706 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1707 if (level
>= tObj
->_MaxLevel
) {
1708 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1709 texcoord
[i
], rgba
[i
]);
1712 GLfloat t0
[4], t1
[4]; /* texels */
1713 const GLfloat f
= FRAC(lambda
[i
]);
1714 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1715 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1716 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1723 sample_3d_linear_mipmap_linear(struct gl_context
*ctx
,
1724 const struct gl_texture_object
*tObj
,
1725 GLuint n
, const GLfloat texcoord
[][4],
1726 const GLfloat lambda
[], GLfloat rgba
[][4])
1729 ASSERT(lambda
!= NULL
);
1730 for (i
= 0; i
< n
; i
++) {
1731 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1732 if (level
>= tObj
->_MaxLevel
) {
1733 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1734 texcoord
[i
], rgba
[i
]);
1737 GLfloat t0
[4], t1
[4]; /* texels */
1738 const GLfloat f
= FRAC(lambda
[i
]);
1739 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1740 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1741 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1747 /** Sample 3D texture, nearest filtering for both min/magnification */
1749 sample_nearest_3d(struct gl_context
*ctx
,
1750 const struct gl_texture_object
*tObj
, GLuint n
,
1751 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1755 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1757 for (i
= 0; i
< n
; i
++) {
1758 sample_3d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1763 /** Sample 3D texture, linear filtering for both min/magnification */
1765 sample_linear_3d(struct gl_context
*ctx
,
1766 const struct gl_texture_object
*tObj
, GLuint n
,
1767 const GLfloat texcoords
[][4],
1768 const GLfloat lambda
[], GLfloat rgba
[][4])
1771 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1773 for (i
= 0; i
< n
; i
++) {
1774 sample_3d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1779 /** Sample 3D texture, using lambda to choose between min/magnification */
1781 sample_lambda_3d(struct gl_context
*ctx
,
1782 const struct gl_texture_object
*tObj
, GLuint n
,
1783 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1786 GLuint minStart
, minEnd
; /* texels with minification */
1787 GLuint magStart
, magEnd
; /* texels with magnification */
1790 ASSERT(lambda
!= NULL
);
1791 compute_min_mag_ranges(tObj
, n
, lambda
,
1792 &minStart
, &minEnd
, &magStart
, &magEnd
);
1794 if (minStart
< minEnd
) {
1795 /* do the minified texels */
1796 GLuint m
= minEnd
- minStart
;
1797 switch (tObj
->MinFilter
) {
1799 for (i
= minStart
; i
< minEnd
; i
++)
1800 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1801 texcoords
[i
], rgba
[i
]);
1804 for (i
= minStart
; i
< minEnd
; i
++)
1805 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1806 texcoords
[i
], rgba
[i
]);
1808 case GL_NEAREST_MIPMAP_NEAREST
:
1809 sample_3d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1810 lambda
+ minStart
, rgba
+ minStart
);
1812 case GL_LINEAR_MIPMAP_NEAREST
:
1813 sample_3d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1814 lambda
+ minStart
, rgba
+ minStart
);
1816 case GL_NEAREST_MIPMAP_LINEAR
:
1817 sample_3d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1818 lambda
+ minStart
, rgba
+ minStart
);
1820 case GL_LINEAR_MIPMAP_LINEAR
:
1821 sample_3d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1822 lambda
+ minStart
, rgba
+ minStart
);
1825 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
1830 if (magStart
< magEnd
) {
1831 /* do the magnified texels */
1832 switch (tObj
->MagFilter
) {
1834 for (i
= magStart
; i
< magEnd
; i
++)
1835 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1836 texcoords
[i
], rgba
[i
]);
1839 for (i
= magStart
; i
< magEnd
; i
++)
1840 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1841 texcoords
[i
], rgba
[i
]);
1844 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
1851 /**********************************************************************/
1852 /* Texture Cube Map Sampling Functions */
1853 /**********************************************************************/
1856 * Choose one of six sides of a texture cube map given the texture
1857 * coord (rx,ry,rz). Return pointer to corresponding array of texture
1860 static const struct gl_texture_image
**
1861 choose_cube_face(const struct gl_texture_object
*texObj
,
1862 const GLfloat texcoord
[4], GLfloat newCoord
[4])
1866 direction target sc tc ma
1867 ---------- ------------------------------- --- --- ---
1868 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1869 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1870 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1871 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1872 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1873 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1875 const GLfloat rx
= texcoord
[0];
1876 const GLfloat ry
= texcoord
[1];
1877 const GLfloat rz
= texcoord
[2];
1878 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
1882 if (arx
>= ary
&& arx
>= arz
) {
1896 else if (ary
>= arx
&& ary
>= arz
) {
1926 const float ima
= 1.0F
/ ma
;
1927 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
1928 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
1931 return (const struct gl_texture_image
**) texObj
->Image
[face
];
1936 sample_nearest_cube(struct gl_context
*ctx
,
1937 const struct gl_texture_object
*tObj
, GLuint n
,
1938 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1943 for (i
= 0; i
< n
; i
++) {
1944 const struct gl_texture_image
**images
;
1945 GLfloat newCoord
[4];
1946 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1947 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
1954 sample_linear_cube(struct gl_context
*ctx
,
1955 const struct gl_texture_object
*tObj
, GLuint n
,
1956 const GLfloat texcoords
[][4],
1957 const GLfloat lambda
[], GLfloat rgba
[][4])
1961 for (i
= 0; i
< n
; i
++) {
1962 const struct gl_texture_image
**images
;
1963 GLfloat newCoord
[4];
1964 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1965 sample_2d_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
1972 sample_cube_nearest_mipmap_nearest(struct gl_context
*ctx
,
1973 const struct gl_texture_object
*tObj
,
1974 GLuint n
, const GLfloat texcoord
[][4],
1975 const GLfloat lambda
[], GLfloat rgba
[][4])
1978 ASSERT(lambda
!= NULL
);
1979 for (i
= 0; i
< n
; i
++) {
1980 const struct gl_texture_image
**images
;
1981 GLfloat newCoord
[4];
1983 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1985 /* XXX we actually need to recompute lambda here based on the newCoords.
1986 * But we would need the texcoords of adjacent fragments to compute that
1987 * properly, and we don't have those here.
1988 * For now, do an approximation: subtracting 1 from the chosen mipmap
1989 * level seems to work in some test cases.
1990 * The same adjustment is done in the next few functions.
1992 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1993 level
= MAX2(level
- 1, 0);
1995 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2001 sample_cube_linear_mipmap_nearest(struct gl_context
*ctx
,
2002 const struct gl_texture_object
*tObj
,
2003 GLuint n
, const GLfloat texcoord
[][4],
2004 const GLfloat lambda
[], GLfloat rgba
[][4])
2007 ASSERT(lambda
!= NULL
);
2008 for (i
= 0; i
< n
; i
++) {
2009 const struct gl_texture_image
**images
;
2010 GLfloat newCoord
[4];
2011 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2012 level
= MAX2(level
- 1, 0); /* see comment above */
2013 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2014 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2020 sample_cube_nearest_mipmap_linear(struct gl_context
*ctx
,
2021 const struct gl_texture_object
*tObj
,
2022 GLuint n
, const GLfloat texcoord
[][4],
2023 const GLfloat lambda
[], GLfloat rgba
[][4])
2026 ASSERT(lambda
!= NULL
);
2027 for (i
= 0; i
< n
; i
++) {
2028 const struct gl_texture_image
**images
;
2029 GLfloat newCoord
[4];
2030 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2031 level
= MAX2(level
- 1, 0); /* see comment above */
2032 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2033 if (level
>= tObj
->_MaxLevel
) {
2034 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2038 GLfloat t0
[4], t1
[4]; /* texels */
2039 const GLfloat f
= FRAC(lambda
[i
]);
2040 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
2041 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2042 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2049 sample_cube_linear_mipmap_linear(struct gl_context
*ctx
,
2050 const struct gl_texture_object
*tObj
,
2051 GLuint n
, const GLfloat texcoord
[][4],
2052 const GLfloat lambda
[], GLfloat rgba
[][4])
2055 ASSERT(lambda
!= NULL
);
2056 for (i
= 0; i
< n
; i
++) {
2057 const struct gl_texture_image
**images
;
2058 GLfloat newCoord
[4];
2059 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2060 level
= MAX2(level
- 1, 0); /* see comment above */
2061 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2062 if (level
>= tObj
->_MaxLevel
) {
2063 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2067 GLfloat t0
[4], t1
[4];
2068 const GLfloat f
= FRAC(lambda
[i
]);
2069 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
2070 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2071 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2077 /** Sample cube texture, using lambda to choose between min/magnification */
2079 sample_lambda_cube(struct gl_context
*ctx
,
2080 const struct gl_texture_object
*tObj
, GLuint n
,
2081 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2084 GLuint minStart
, minEnd
; /* texels with minification */
2085 GLuint magStart
, magEnd
; /* texels with magnification */
2087 ASSERT(lambda
!= NULL
);
2088 compute_min_mag_ranges(tObj
, n
, lambda
,
2089 &minStart
, &minEnd
, &magStart
, &magEnd
);
2091 if (minStart
< minEnd
) {
2092 /* do the minified texels */
2093 const GLuint m
= minEnd
- minStart
;
2094 switch (tObj
->MinFilter
) {
2096 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2097 lambda
+ minStart
, rgba
+ minStart
);
2100 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2101 lambda
+ minStart
, rgba
+ minStart
);
2103 case GL_NEAREST_MIPMAP_NEAREST
:
2104 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
2105 texcoords
+ minStart
,
2106 lambda
+ minStart
, rgba
+ minStart
);
2108 case GL_LINEAR_MIPMAP_NEAREST
:
2109 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
2110 texcoords
+ minStart
,
2111 lambda
+ minStart
, rgba
+ minStart
);
2113 case GL_NEAREST_MIPMAP_LINEAR
:
2114 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
2115 texcoords
+ minStart
,
2116 lambda
+ minStart
, rgba
+ minStart
);
2118 case GL_LINEAR_MIPMAP_LINEAR
:
2119 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
2120 texcoords
+ minStart
,
2121 lambda
+ minStart
, rgba
+ minStart
);
2124 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2128 if (magStart
< magEnd
) {
2129 /* do the magnified texels */
2130 const GLuint m
= magEnd
- magStart
;
2131 switch (tObj
->MagFilter
) {
2133 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2134 lambda
+ magStart
, rgba
+ magStart
);
2137 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2138 lambda
+ magStart
, rgba
+ magStart
);
2141 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2147 /**********************************************************************/
2148 /* Texture Rectangle Sampling Functions */
2149 /**********************************************************************/
2153 sample_nearest_rect(struct gl_context
*ctx
,
2154 const struct gl_texture_object
*tObj
, GLuint n
,
2155 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2158 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2159 const GLint width
= img
->Width
;
2160 const GLint height
= img
->Height
;
2166 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2167 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2168 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2169 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2170 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2171 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2172 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
2174 for (i
= 0; i
< n
; i
++) {
2176 col
= clamp_rect_coord_nearest(tObj
->WrapS
, texcoords
[i
][0], width
);
2177 row
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoords
[i
][1], height
);
2178 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2179 get_border_color(tObj
, img
, rgba
[i
]);
2181 img
->FetchTexelf(img
, col
, row
, 0, rgba
[i
]);
2187 sample_linear_rect(struct gl_context
*ctx
,
2188 const struct gl_texture_object
*tObj
, GLuint n
,
2189 const GLfloat texcoords
[][4],
2190 const GLfloat lambda
[], GLfloat rgba
[][4])
2192 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2193 const GLint width
= img
->Width
;
2194 const GLint height
= img
->Height
;
2200 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2201 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2202 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2203 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2204 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2205 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2206 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
2208 for (i
= 0; i
< n
; i
++) {
2209 GLint i0
, j0
, i1
, j1
;
2210 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2212 GLbitfield useBorderColor
= 0x0;
2214 clamp_rect_coord_linear(tObj
->WrapS
, texcoords
[i
][0], width
,
2216 clamp_rect_coord_linear(tObj
->WrapT
, texcoords
[i
][1], height
,
2219 /* compute integer rows/columns */
2220 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2221 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2222 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2223 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2225 /* get four texel samples */
2226 if (useBorderColor
& (I0BIT
| J0BIT
))
2227 get_border_color(tObj
, img
, t00
);
2229 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
2231 if (useBorderColor
& (I1BIT
| J0BIT
))
2232 get_border_color(tObj
, img
, t10
);
2234 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
2236 if (useBorderColor
& (I0BIT
| J1BIT
))
2237 get_border_color(tObj
, img
, t01
);
2239 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
2241 if (useBorderColor
& (I1BIT
| J1BIT
))
2242 get_border_color(tObj
, img
, t11
);
2244 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
2246 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2251 /** Sample Rect texture, using lambda to choose between min/magnification */
2253 sample_lambda_rect(struct gl_context
*ctx
,
2254 const struct gl_texture_object
*tObj
, GLuint n
,
2255 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2258 GLuint minStart
, minEnd
, magStart
, magEnd
;
2260 /* We only need lambda to decide between minification and magnification.
2261 * There is no mipmapping with rectangular textures.
2263 compute_min_mag_ranges(tObj
, n
, lambda
,
2264 &minStart
, &minEnd
, &magStart
, &magEnd
);
2266 if (minStart
< minEnd
) {
2267 if (tObj
->MinFilter
== GL_NEAREST
) {
2268 sample_nearest_rect(ctx
, tObj
, minEnd
- minStart
,
2269 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2272 sample_linear_rect(ctx
, tObj
, minEnd
- minStart
,
2273 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2276 if (magStart
< magEnd
) {
2277 if (tObj
->MagFilter
== GL_NEAREST
) {
2278 sample_nearest_rect(ctx
, tObj
, magEnd
- magStart
,
2279 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2282 sample_linear_rect(ctx
, tObj
, magEnd
- magStart
,
2283 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2289 /**********************************************************************/
2290 /* 2D Texture Array Sampling Functions */
2291 /**********************************************************************/
2294 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2297 sample_2d_array_nearest(struct gl_context
*ctx
,
2298 const struct gl_texture_object
*tObj
,
2299 const struct gl_texture_image
*img
,
2300 const GLfloat texcoord
[4],
2303 const GLint width
= img
->Width2
; /* without border, power of two */
2304 const GLint height
= img
->Height2
; /* without border, power of two */
2305 const GLint depth
= img
->Depth
;
2310 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2311 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
2312 array
= tex_array_slice(texcoord
[2], depth
);
2314 if (i
< 0 || i
>= (GLint
) img
->Width
||
2315 j
< 0 || j
>= (GLint
) img
->Height
||
2316 array
< 0 || array
>= (GLint
) img
->Depth
) {
2317 /* Need this test for GL_CLAMP_TO_BORDER mode */
2318 get_border_color(tObj
, img
, rgba
);
2321 img
->FetchTexelf(img
, i
, j
, array
, rgba
);
2327 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2330 sample_2d_array_linear(struct gl_context
*ctx
,
2331 const struct gl_texture_object
*tObj
,
2332 const struct gl_texture_image
*img
,
2333 const GLfloat texcoord
[4],
2336 const GLint width
= img
->Width2
;
2337 const GLint height
= img
->Height2
;
2338 const GLint depth
= img
->Depth
;
2339 GLint i0
, j0
, i1
, j1
;
2341 GLbitfield useBorderColor
= 0x0;
2343 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2345 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2346 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2347 array
= tex_array_slice(texcoord
[2], depth
);
2349 if (array
< 0 || array
>= depth
) {
2350 COPY_4V(rgba
, tObj
->BorderColor
.f
);
2360 /* check if sampling texture border color */
2361 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2362 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2363 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2364 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2368 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2369 get_border_color(tObj
, img
, t00
);
2372 img
->FetchTexelf(img
, i0
, j0
, array
, t00
);
2374 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2375 get_border_color(tObj
, img
, t10
);
2378 img
->FetchTexelf(img
, i1
, j0
, array
, t10
);
2380 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2381 get_border_color(tObj
, img
, t01
);
2384 img
->FetchTexelf(img
, i0
, j1
, array
, t01
);
2386 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2387 get_border_color(tObj
, img
, t11
);
2390 img
->FetchTexelf(img
, i1
, j1
, array
, t11
);
2393 /* trilinear interpolation of samples */
2394 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2400 sample_2d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2401 const struct gl_texture_object
*tObj
,
2402 GLuint n
, const GLfloat texcoord
[][4],
2403 const GLfloat lambda
[], GLfloat rgba
[][4])
2406 for (i
= 0; i
< n
; i
++) {
2407 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2408 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2415 sample_2d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2416 const struct gl_texture_object
*tObj
,
2417 GLuint n
, const GLfloat texcoord
[][4],
2418 const GLfloat lambda
[], GLfloat rgba
[][4])
2421 ASSERT(lambda
!= NULL
);
2422 for (i
= 0; i
< n
; i
++) {
2423 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2424 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2425 texcoord
[i
], rgba
[i
]);
2431 sample_2d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2432 const struct gl_texture_object
*tObj
,
2433 GLuint n
, const GLfloat texcoord
[][4],
2434 const GLfloat lambda
[], GLfloat rgba
[][4])
2437 ASSERT(lambda
!= NULL
);
2438 for (i
= 0; i
< n
; i
++) {
2439 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2440 if (level
>= tObj
->_MaxLevel
) {
2441 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2442 texcoord
[i
], rgba
[i
]);
2445 GLfloat t0
[4], t1
[4]; /* texels */
2446 const GLfloat f
= FRAC(lambda
[i
]);
2447 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
],
2449 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1],
2451 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2458 sample_2d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2459 const struct gl_texture_object
*tObj
,
2460 GLuint n
, const GLfloat texcoord
[][4],
2461 const GLfloat lambda
[], GLfloat rgba
[][4])
2464 ASSERT(lambda
!= NULL
);
2465 for (i
= 0; i
< n
; i
++) {
2466 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2467 if (level
>= tObj
->_MaxLevel
) {
2468 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2469 texcoord
[i
], rgba
[i
]);
2472 GLfloat t0
[4], t1
[4]; /* texels */
2473 const GLfloat f
= FRAC(lambda
[i
]);
2474 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2476 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1],
2478 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2484 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2486 sample_nearest_2d_array(struct gl_context
*ctx
,
2487 const struct gl_texture_object
*tObj
, GLuint n
,
2488 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2492 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2494 for (i
= 0; i
< n
; i
++) {
2495 sample_2d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2501 /** Sample 2D Array texture, linear filtering for both min/magnification */
2503 sample_linear_2d_array(struct gl_context
*ctx
,
2504 const struct gl_texture_object
*tObj
, GLuint n
,
2505 const GLfloat texcoords
[][4],
2506 const GLfloat lambda
[], GLfloat rgba
[][4])
2509 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2511 for (i
= 0; i
< n
; i
++) {
2512 sample_2d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2517 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2519 sample_lambda_2d_array(struct gl_context
*ctx
,
2520 const struct gl_texture_object
*tObj
, GLuint n
,
2521 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2524 GLuint minStart
, minEnd
; /* texels with minification */
2525 GLuint magStart
, magEnd
; /* texels with magnification */
2528 ASSERT(lambda
!= NULL
);
2529 compute_min_mag_ranges(tObj
, n
, lambda
,
2530 &minStart
, &minEnd
, &magStart
, &magEnd
);
2532 if (minStart
< minEnd
) {
2533 /* do the minified texels */
2534 GLuint m
= minEnd
- minStart
;
2535 switch (tObj
->MinFilter
) {
2537 for (i
= minStart
; i
< minEnd
; i
++)
2538 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2539 texcoords
[i
], rgba
[i
]);
2542 for (i
= minStart
; i
< minEnd
; i
++)
2543 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2544 texcoords
[i
], rgba
[i
]);
2546 case GL_NEAREST_MIPMAP_NEAREST
:
2547 sample_2d_array_nearest_mipmap_nearest(ctx
, tObj
, m
,
2548 texcoords
+ minStart
,
2552 case GL_LINEAR_MIPMAP_NEAREST
:
2553 sample_2d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2554 texcoords
+ minStart
,
2558 case GL_NEAREST_MIPMAP_LINEAR
:
2559 sample_2d_array_nearest_mipmap_linear(ctx
, tObj
, m
,
2560 texcoords
+ minStart
,
2564 case GL_LINEAR_MIPMAP_LINEAR
:
2565 sample_2d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2566 texcoords
+ minStart
,
2571 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
2576 if (magStart
< magEnd
) {
2577 /* do the magnified texels */
2578 switch (tObj
->MagFilter
) {
2580 for (i
= magStart
; i
< magEnd
; i
++)
2581 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2582 texcoords
[i
], rgba
[i
]);
2585 for (i
= magStart
; i
< magEnd
; i
++)
2586 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2587 texcoords
[i
], rgba
[i
]);
2590 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
2599 /**********************************************************************/
2600 /* 1D Texture Array Sampling Functions */
2601 /**********************************************************************/
2604 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2607 sample_1d_array_nearest(struct gl_context
*ctx
,
2608 const struct gl_texture_object
*tObj
,
2609 const struct gl_texture_image
*img
,
2610 const GLfloat texcoord
[4],
2613 const GLint width
= img
->Width2
; /* without border, power of two */
2614 const GLint height
= img
->Height
;
2619 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2620 array
= tex_array_slice(texcoord
[1], height
);
2622 if (i
< 0 || i
>= (GLint
) img
->Width
||
2623 array
< 0 || array
>= (GLint
) img
->Height
) {
2624 /* Need this test for GL_CLAMP_TO_BORDER mode */
2625 get_border_color(tObj
, img
, rgba
);
2628 img
->FetchTexelf(img
, i
, array
, 0, rgba
);
2634 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2637 sample_1d_array_linear(struct gl_context
*ctx
,
2638 const struct gl_texture_object
*tObj
,
2639 const struct gl_texture_image
*img
,
2640 const GLfloat texcoord
[4],
2643 const GLint width
= img
->Width2
;
2644 const GLint height
= img
->Height
;
2647 GLbitfield useBorderColor
= 0x0;
2649 GLfloat t0
[4], t1
[4];
2651 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2652 array
= tex_array_slice(texcoord
[1], height
);
2659 /* check if sampling texture border color */
2660 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2661 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2664 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
2667 if (useBorderColor
& (I0BIT
| K0BIT
)) {
2668 get_border_color(tObj
, img
, t0
);
2671 img
->FetchTexelf(img
, i0
, array
, 0, t0
);
2673 if (useBorderColor
& (I1BIT
| K0BIT
)) {
2674 get_border_color(tObj
, img
, t1
);
2677 img
->FetchTexelf(img
, i1
, array
, 0, t1
);
2680 /* bilinear interpolation of samples */
2681 lerp_rgba(rgba
, a
, t0
, t1
);
2686 sample_1d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2687 const struct gl_texture_object
*tObj
,
2688 GLuint n
, const GLfloat texcoord
[][4],
2689 const GLfloat lambda
[], GLfloat rgba
[][4])
2692 for (i
= 0; i
< n
; i
++) {
2693 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2694 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2701 sample_1d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2702 const struct gl_texture_object
*tObj
,
2703 GLuint n
, const GLfloat texcoord
[][4],
2704 const GLfloat lambda
[], GLfloat rgba
[][4])
2707 ASSERT(lambda
!= NULL
);
2708 for (i
= 0; i
< n
; i
++) {
2709 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2710 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2711 texcoord
[i
], rgba
[i
]);
2717 sample_1d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2718 const struct gl_texture_object
*tObj
,
2719 GLuint n
, const GLfloat texcoord
[][4],
2720 const GLfloat lambda
[], GLfloat rgba
[][4])
2723 ASSERT(lambda
!= NULL
);
2724 for (i
= 0; i
< n
; i
++) {
2725 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2726 if (level
>= tObj
->_MaxLevel
) {
2727 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2728 texcoord
[i
], rgba
[i
]);
2731 GLfloat t0
[4], t1
[4]; /* texels */
2732 const GLfloat f
= FRAC(lambda
[i
]);
2733 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2734 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2735 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2742 sample_1d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2743 const struct gl_texture_object
*tObj
,
2744 GLuint n
, const GLfloat texcoord
[][4],
2745 const GLfloat lambda
[], GLfloat rgba
[][4])
2748 ASSERT(lambda
!= NULL
);
2749 for (i
= 0; i
< n
; i
++) {
2750 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2751 if (level
>= tObj
->_MaxLevel
) {
2752 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2753 texcoord
[i
], rgba
[i
]);
2756 GLfloat t0
[4], t1
[4]; /* texels */
2757 const GLfloat f
= FRAC(lambda
[i
]);
2758 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2759 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2760 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2766 /** Sample 1D Array texture, nearest filtering for both min/magnification */
2768 sample_nearest_1d_array(struct gl_context
*ctx
,
2769 const struct gl_texture_object
*tObj
, GLuint n
,
2770 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2774 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2776 for (i
= 0; i
< n
; i
++) {
2777 sample_1d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2782 /** Sample 1D Array texture, linear filtering for both min/magnification */
2784 sample_linear_1d_array(struct gl_context
*ctx
,
2785 const struct gl_texture_object
*tObj
, GLuint n
,
2786 const GLfloat texcoords
[][4],
2787 const GLfloat lambda
[], GLfloat rgba
[][4])
2790 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2792 for (i
= 0; i
< n
; i
++) {
2793 sample_1d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2798 /** Sample 1D Array texture, using lambda to choose between min/magnification */
2800 sample_lambda_1d_array(struct gl_context
*ctx
,
2801 const struct gl_texture_object
*tObj
, GLuint n
,
2802 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2805 GLuint minStart
, minEnd
; /* texels with minification */
2806 GLuint magStart
, magEnd
; /* texels with magnification */
2809 ASSERT(lambda
!= NULL
);
2810 compute_min_mag_ranges(tObj
, n
, lambda
,
2811 &minStart
, &minEnd
, &magStart
, &magEnd
);
2813 if (minStart
< minEnd
) {
2814 /* do the minified texels */
2815 GLuint m
= minEnd
- minStart
;
2816 switch (tObj
->MinFilter
) {
2818 for (i
= minStart
; i
< minEnd
; i
++)
2819 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2820 texcoords
[i
], rgba
[i
]);
2823 for (i
= minStart
; i
< minEnd
; i
++)
2824 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2825 texcoords
[i
], rgba
[i
]);
2827 case GL_NEAREST_MIPMAP_NEAREST
:
2828 sample_1d_array_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2829 lambda
+ minStart
, rgba
+ minStart
);
2831 case GL_LINEAR_MIPMAP_NEAREST
:
2832 sample_1d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2833 texcoords
+ minStart
,
2837 case GL_NEAREST_MIPMAP_LINEAR
:
2838 sample_1d_array_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2839 lambda
+ minStart
, rgba
+ minStart
);
2841 case GL_LINEAR_MIPMAP_LINEAR
:
2842 sample_1d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2843 texcoords
+ minStart
,
2848 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
2853 if (magStart
< magEnd
) {
2854 /* do the magnified texels */
2855 switch (tObj
->MagFilter
) {
2857 for (i
= magStart
; i
< magEnd
; i
++)
2858 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2859 texcoords
[i
], rgba
[i
]);
2862 for (i
= magStart
; i
< magEnd
; i
++)
2863 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2864 texcoords
[i
], rgba
[i
]);
2867 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
2875 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
2877 static INLINE GLfloat
2878 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
,
2883 return (coord
<= depthSample
) ? 1.0F
: ambient
;
2885 return (coord
>= depthSample
) ? 1.0F
: ambient
;
2887 return (coord
< depthSample
) ? 1.0F
: ambient
;
2889 return (coord
> depthSample
) ? 1.0F
: ambient
;
2891 return (coord
== depthSample
) ? 1.0F
: ambient
;
2893 return (coord
!= depthSample
) ? 1.0F
: ambient
;
2901 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
2908 * Compare texcoord against four depth samples.
2910 static INLINE GLfloat
2911 shadow_compare4(GLenum function
, GLfloat coord
,
2912 GLfloat depth00
, GLfloat depth01
,
2913 GLfloat depth10
, GLfloat depth11
,
2914 GLfloat ambient
, GLfloat wi
, GLfloat wj
)
2916 const GLfloat d
= (1.0F
- (GLfloat
) ambient
) * 0.25F
;
2917 GLfloat luminance
= 1.0F
;
2921 if (coord
> depth00
) luminance
-= d
;
2922 if (coord
> depth01
) luminance
-= d
;
2923 if (coord
> depth10
) luminance
-= d
;
2924 if (coord
> depth11
) luminance
-= d
;
2927 if (coord
< depth00
) luminance
-= d
;
2928 if (coord
< depth01
) luminance
-= d
;
2929 if (coord
< depth10
) luminance
-= d
;
2930 if (coord
< depth11
) luminance
-= d
;
2933 if (coord
>= depth00
) luminance
-= d
;
2934 if (coord
>= depth01
) luminance
-= d
;
2935 if (coord
>= depth10
) luminance
-= d
;
2936 if (coord
>= depth11
) luminance
-= d
;
2939 if (coord
<= depth00
) luminance
-= d
;
2940 if (coord
<= depth01
) luminance
-= d
;
2941 if (coord
<= depth10
) luminance
-= d
;
2942 if (coord
<= depth11
) luminance
-= d
;
2945 if (coord
!= depth00
) luminance
-= d
;
2946 if (coord
!= depth01
) luminance
-= d
;
2947 if (coord
!= depth10
) luminance
-= d
;
2948 if (coord
!= depth11
) luminance
-= d
;
2951 if (coord
== depth00
) luminance
-= d
;
2952 if (coord
== depth01
) luminance
-= d
;
2953 if (coord
== depth10
) luminance
-= d
;
2954 if (coord
== depth11
) luminance
-= d
;
2961 /* ordinary bilinear filtering */
2962 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
2964 _mesa_problem(NULL
, "Bad compare func in sample_compare4");
2971 * Choose the mipmap level to use when sampling from a depth texture.
2974 choose_depth_texture_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
2978 if (tObj
->MinFilter
== GL_NEAREST
|| tObj
->MinFilter
== GL_LINEAR
) {
2979 /* no mipmapping - use base level */
2980 level
= tObj
->BaseLevel
;
2983 /* choose mipmap level */
2984 lambda
= CLAMP(lambda
, tObj
->MinLod
, tObj
->MaxLod
);
2985 level
= (GLint
) lambda
;
2986 level
= CLAMP(level
, tObj
->BaseLevel
, tObj
->_MaxLevel
);
2994 * Sample a shadow/depth texture. This function is incomplete. It doesn't
2995 * check for minification vs. magnification, etc.
2998 sample_depth_texture( struct gl_context
*ctx
,
2999 const struct gl_texture_object
*tObj
, GLuint n
,
3000 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3001 GLfloat texel
[][4] )
3003 const GLint level
= choose_depth_texture_level(tObj
, lambda
[0]);
3004 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
3005 const GLint width
= img
->Width
;
3006 const GLint height
= img
->Height
;
3007 const GLint depth
= img
->Depth
;
3008 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
3014 ASSERT(img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
3015 img
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
3017 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
3018 tObj
->Target
== GL_TEXTURE_2D
||
3019 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
3020 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
3021 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
);
3023 ambient
= tObj
->CompareFailValue
;
3025 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
3027 function
= (tObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
3028 tObj
->CompareFunc
: GL_NONE
;
3030 if (tObj
->MagFilter
== GL_NEAREST
) {
3032 for (i
= 0; i
< n
; i
++) {
3033 GLfloat depthSample
, depthRef
;
3034 GLint col
, row
, slice
;
3036 nearest_texcoord(tObj
, level
, texcoords
[i
], &col
, &row
, &slice
);
3038 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3039 slice
>= 0 && slice
< depth
) {
3040 img
->FetchTexelf(img
, col
, row
, slice
, &depthSample
);
3043 depthSample
= tObj
->BorderColor
.f
[0];
3046 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3048 result
= shadow_compare(function
, depthRef
, depthSample
, ambient
);
3050 switch (tObj
->DepthMode
) {
3052 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3055 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3058 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3061 ASSIGN_4V(texel
[i
], result
, 0.0F
, 0.0F
, 1.0F
);
3064 _mesa_problem(ctx
, "Bad depth texture mode");
3070 ASSERT(tObj
->MagFilter
== GL_LINEAR
);
3071 for (i
= 0; i
< n
; i
++) {
3072 GLfloat depth00
, depth01
, depth10
, depth11
, depthRef
;
3073 GLint i0
, i1
, j0
, j1
;
3076 GLuint useBorderTexel
;
3078 linear_texcoord(tObj
, level
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3085 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3091 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3092 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3093 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3094 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3097 if (slice
< 0 || slice
>= (GLint
) depth
) {
3098 depth00
= tObj
->BorderColor
.f
[0];
3099 depth01
= tObj
->BorderColor
.f
[0];
3100 depth10
= tObj
->BorderColor
.f
[0];
3101 depth11
= tObj
->BorderColor
.f
[0];
3104 /* get four depth samples from the texture */
3105 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3106 depth00
= tObj
->BorderColor
.f
[0];
3109 img
->FetchTexelf(img
, i0
, j0
, slice
, &depth00
);
3111 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3112 depth10
= tObj
->BorderColor
.f
[0];
3115 img
->FetchTexelf(img
, i1
, j0
, slice
, &depth10
);
3118 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3119 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3120 depth01
= tObj
->BorderColor
.f
[0];
3123 img
->FetchTexelf(img
, i0
, j1
, slice
, &depth01
);
3125 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3126 depth11
= tObj
->BorderColor
.f
[0];
3129 img
->FetchTexelf(img
, i1
, j1
, slice
, &depth11
);
3138 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3140 result
= shadow_compare4(function
, depthRef
,
3141 depth00
, depth01
, depth10
, depth11
,
3144 switch (tObj
->DepthMode
) {
3146 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3149 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3152 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3155 _mesa_problem(ctx
, "Bad depth texture mode");
3164 * We use this function when a texture object is in an "incomplete" state.
3165 * When a fragment program attempts to sample an incomplete texture we
3166 * return black (see issue 23 in GL_ARB_fragment_program spec).
3167 * Note: fragment programs don't observe the texture enable/disable flags.
3170 null_sample_func( struct gl_context
*ctx
,
3171 const struct gl_texture_object
*tObj
, GLuint n
,
3172 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3180 for (i
= 0; i
< n
; i
++) {
3184 rgba
[i
][ACOMP
] = 1.0;
3190 * Choose the texture sampling function for the given texture object.
3193 _swrast_choose_texture_sample_func( struct gl_context
*ctx
,
3194 const struct gl_texture_object
*t
)
3196 if (!t
|| !t
->_Complete
) {
3197 return &null_sample_func
;
3200 const GLboolean needLambda
= (GLboolean
) (t
->MinFilter
!= t
->MagFilter
);
3201 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->_BaseFormat
;
3203 switch (t
->Target
) {
3205 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3206 return &sample_depth_texture
;
3208 else if (needLambda
) {
3209 return &sample_lambda_1d
;
3211 else if (t
->MinFilter
== GL_LINEAR
) {
3212 return &sample_linear_1d
;
3215 ASSERT(t
->MinFilter
== GL_NEAREST
);
3216 return &sample_nearest_1d
;
3219 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3220 return &sample_depth_texture
;
3222 else if (needLambda
) {
3223 return &sample_lambda_2d
;
3225 else if (t
->MinFilter
== GL_LINEAR
) {
3226 return &sample_linear_2d
;
3229 /* check for a few optimized cases */
3230 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3231 ASSERT(t
->MinFilter
== GL_NEAREST
);
3232 if (t
->WrapS
== GL_REPEAT
&&
3233 t
->WrapT
== GL_REPEAT
&&
3234 img
->_IsPowerOfTwo
&&
3236 img
->TexFormat
== MESA_FORMAT_RGB888
) {
3237 return &opt_sample_rgb_2d
;
3239 else if (t
->WrapS
== GL_REPEAT
&&
3240 t
->WrapT
== GL_REPEAT
&&
3241 img
->_IsPowerOfTwo
&&
3243 img
->TexFormat
== MESA_FORMAT_RGBA8888
) {
3244 return &opt_sample_rgba_2d
;
3247 return &sample_nearest_2d
;
3252 return &sample_lambda_3d
;
3254 else if (t
->MinFilter
== GL_LINEAR
) {
3255 return &sample_linear_3d
;
3258 ASSERT(t
->MinFilter
== GL_NEAREST
);
3259 return &sample_nearest_3d
;
3261 case GL_TEXTURE_CUBE_MAP
:
3263 return &sample_lambda_cube
;
3265 else if (t
->MinFilter
== GL_LINEAR
) {
3266 return &sample_linear_cube
;
3269 ASSERT(t
->MinFilter
== GL_NEAREST
);
3270 return &sample_nearest_cube
;
3272 case GL_TEXTURE_RECTANGLE_NV
:
3273 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3274 return &sample_depth_texture
;
3276 else if (needLambda
) {
3277 return &sample_lambda_rect
;
3279 else if (t
->MinFilter
== GL_LINEAR
) {
3280 return &sample_linear_rect
;
3283 ASSERT(t
->MinFilter
== GL_NEAREST
);
3284 return &sample_nearest_rect
;
3286 case GL_TEXTURE_1D_ARRAY_EXT
:
3288 return &sample_lambda_1d_array
;
3290 else if (t
->MinFilter
== GL_LINEAR
) {
3291 return &sample_linear_1d_array
;
3294 ASSERT(t
->MinFilter
== GL_NEAREST
);
3295 return &sample_nearest_1d_array
;
3297 case GL_TEXTURE_2D_ARRAY_EXT
:
3299 return &sample_lambda_2d_array
;
3301 else if (t
->MinFilter
== GL_LINEAR
) {
3302 return &sample_linear_2d_array
;
3305 ASSERT(t
->MinFilter
== GL_NEAREST
);
3306 return &sample_nearest_2d_array
;
3310 "invalid target in _swrast_choose_texture_sample_func");
3311 return &null_sample_func
;