2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/colormac.h"
29 #include "main/imports.h"
30 #include "main/texformat.h"
32 #include "s_context.h"
33 #include "s_texfilter.h"
37 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
38 * see 1-pixel bands of improperly weighted linear-filtered textures.
39 * The tests/texwrap.c demo is a good test.
40 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
41 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
43 #define FRAC(f) ((f) - IFLOOR(f))
48 * Linear interpolation macro
50 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
54 * Do 2D/biliner interpolation of float values.
55 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
56 * a and b are the horizontal and vertical interpolants.
57 * It's important that this function is inlined when compiled with
58 * optimization! If we find that's not true on some systems, convert
62 lerp_2d(GLfloat a
, GLfloat b
,
63 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
65 const GLfloat temp0
= LERP(a
, v00
, v10
);
66 const GLfloat temp1
= LERP(a
, v01
, v11
);
67 return LERP(b
, temp0
, temp1
);
72 * Do 3D/trilinear interpolation of float values.
76 lerp_3d(GLfloat a
, GLfloat b
, GLfloat c
,
77 GLfloat v000
, GLfloat v100
, GLfloat v010
, GLfloat v110
,
78 GLfloat v001
, GLfloat v101
, GLfloat v011
, GLfloat v111
)
80 const GLfloat temp00
= LERP(a
, v000
, v100
);
81 const GLfloat temp10
= LERP(a
, v010
, v110
);
82 const GLfloat temp01
= LERP(a
, v001
, v101
);
83 const GLfloat temp11
= LERP(a
, v011
, v111
);
84 const GLfloat temp0
= LERP(b
, temp00
, temp10
);
85 const GLfloat temp1
= LERP(b
, temp01
, temp11
);
86 return LERP(c
, temp0
, temp1
);
91 * Do linear interpolation of colors.
94 lerp_rgba(GLfloat result
[4], GLfloat t
, const GLfloat a
[4], const GLfloat b
[4])
96 result
[0] = LERP(t
, a
[0], b
[0]);
97 result
[1] = LERP(t
, a
[1], b
[1]);
98 result
[2] = LERP(t
, a
[2], b
[2]);
99 result
[3] = LERP(t
, a
[3], b
[3]);
104 * Do bilinear interpolation of colors.
107 lerp_rgba_2d(GLfloat result
[4], GLfloat a
, GLfloat b
,
108 const GLfloat t00
[4], const GLfloat t10
[4],
109 const GLfloat t01
[4], const GLfloat t11
[4])
111 result
[0] = lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]);
112 result
[1] = lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]);
113 result
[2] = lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]);
114 result
[3] = lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]);
119 * Do trilinear interpolation of colors.
122 lerp_rgba_3d(GLfloat result
[4], GLfloat a
, GLfloat b
, GLfloat c
,
123 const GLfloat t000
[4], const GLfloat t100
[4],
124 const GLfloat t010
[4], const GLfloat t110
[4],
125 const GLfloat t001
[4], const GLfloat t101
[4],
126 const GLfloat t011
[4], const GLfloat t111
[4])
129 /* compiler should unroll these short loops */
130 for (k
= 0; k
< 4; k
++) {
131 result
[k
] = lerp_3d(a
, b
, c
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
132 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
138 * Used for GL_REPEAT wrap mode. Using A % B doesn't produce the
139 * right results for A<0. Casting to A to be unsigned only works if B
140 * is a power of two. Adding a bias to A (which is a multiple of B)
141 * avoids the problems with A < 0 (for reasonable A) without using a
144 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
148 * Used to compute texel locations for linear sampling.
150 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
151 * s = texcoord in [0,1]
152 * size = width (or height or depth) of texture
154 * i0, i1 = returns two nearest texel indexes
155 * weight = returns blend factor between texels
158 linear_texel_locations(GLenum wrapMode
,
159 const struct gl_texture_image
*img
,
160 GLint size
, GLfloat s
,
161 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
167 if (img
->_IsPowerOfTwo
) {
168 *i0
= IFLOOR(u
) & (size
- 1);
169 *i1
= (*i0
+ 1) & (size
- 1);
172 *i0
= REMAINDER(IFLOOR(u
), size
);
173 *i1
= REMAINDER(*i0
+ 1, size
);
176 case GL_CLAMP_TO_EDGE
:
188 if (*i1
>= (GLint
) size
)
191 case GL_CLAMP_TO_BORDER
:
193 const GLfloat min
= -1.0F
/ (2.0F
* size
);
194 const GLfloat max
= 1.0F
- min
;
206 case GL_MIRRORED_REPEAT
:
208 const GLint flr
= IFLOOR(s
);
210 u
= 1.0F
- (s
- (GLfloat
) flr
);
212 u
= s
- (GLfloat
) flr
;
213 u
= (u
* size
) - 0.5F
;
218 if (*i1
>= (GLint
) size
)
222 case GL_MIRROR_CLAMP_EXT
:
232 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
243 if (*i1
>= (GLint
) size
)
246 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
248 const GLfloat min
= -1.0F
/ (2.0F
* size
);
249 const GLfloat max
= 1.0F
- min
;
274 _mesa_problem(NULL
, "Bad wrap mode");
282 * Used to compute texel location for nearest sampling.
285 nearest_texel_location(GLenum wrapMode
,
286 const struct gl_texture_image
*img
,
287 GLint size
, GLfloat s
)
293 /* s limited to [0,1) */
294 /* i limited to [0,size-1] */
295 i
= IFLOOR(s
* size
);
296 if (img
->_IsPowerOfTwo
)
299 i
= REMAINDER(i
, size
);
301 case GL_CLAMP_TO_EDGE
:
303 /* s limited to [min,max] */
304 /* i limited to [0, size-1] */
305 const GLfloat min
= 1.0F
/ (2.0F
* size
);
306 const GLfloat max
= 1.0F
- min
;
312 i
= IFLOOR(s
* size
);
315 case GL_CLAMP_TO_BORDER
:
317 /* s limited to [min,max] */
318 /* i limited to [-1, size] */
319 const GLfloat min
= -1.0F
/ (2.0F
* size
);
320 const GLfloat max
= 1.0F
- min
;
326 i
= IFLOOR(s
* size
);
329 case GL_MIRRORED_REPEAT
:
331 const GLfloat min
= 1.0F
/ (2.0F
* size
);
332 const GLfloat max
= 1.0F
- min
;
333 const GLint flr
= IFLOOR(s
);
336 u
= 1.0F
- (s
- (GLfloat
) flr
);
338 u
= s
- (GLfloat
) flr
;
344 i
= IFLOOR(u
* size
);
347 case GL_MIRROR_CLAMP_EXT
:
349 /* s limited to [0,1] */
350 /* i limited to [0,size-1] */
351 const GLfloat u
= FABSF(s
);
357 i
= IFLOOR(u
* size
);
360 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
362 /* s limited to [min,max] */
363 /* i limited to [0, size-1] */
364 const GLfloat min
= 1.0F
/ (2.0F
* size
);
365 const GLfloat max
= 1.0F
- min
;
366 const GLfloat u
= FABSF(s
);
372 i
= IFLOOR(u
* size
);
375 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
377 /* s limited to [min,max] */
378 /* i limited to [0, size-1] */
379 const GLfloat min
= -1.0F
/ (2.0F
* size
);
380 const GLfloat max
= 1.0F
- min
;
381 const GLfloat u
= FABSF(s
);
387 i
= IFLOOR(u
* size
);
391 /* s limited to [0,1] */
392 /* i limited to [0,size-1] */
398 i
= IFLOOR(s
* size
);
401 _mesa_problem(NULL
, "Bad wrap mode");
407 /* Power of two image sizes only */
409 linear_repeat_texel_location(GLuint size
, GLfloat s
,
410 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
412 GLfloat u
= s
* size
- 0.5F
;
413 *i0
= IFLOOR(u
) & (size
- 1);
414 *i1
= (*i0
+ 1) & (size
- 1);
420 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
423 clamp_rect_coord_nearest(GLenum wrapMode
, GLfloat coord
, GLint max
)
427 return IFLOOR( CLAMP(coord
, 0.0F
, max
- 1) );
428 case GL_CLAMP_TO_EDGE
:
429 return IFLOOR( CLAMP(coord
, 0.5F
, max
- 0.5F
) );
430 case GL_CLAMP_TO_BORDER
:
431 return IFLOOR( CLAMP(coord
, -0.5F
, max
+ 0.5F
) );
433 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_nearest");
440 * As above, but GL_LINEAR filtering.
443 clamp_rect_coord_linear(GLenum wrapMode
, GLfloat coord
, GLint max
,
444 GLint
*i0out
, GLint
*i1out
, GLfloat
*weight
)
450 /* Not exactly what the spec says, but it matches NVIDIA output */
451 fcol
= CLAMP(coord
- 0.5F
, 0.0F
, max
- 1);
455 case GL_CLAMP_TO_EDGE
:
456 fcol
= CLAMP(coord
, 0.5F
, max
- 0.5F
);
463 case GL_CLAMP_TO_BORDER
:
464 fcol
= CLAMP(coord
, -0.5F
, max
+ 0.5F
);
470 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_linear");
476 *weight
= FRAC(fcol
);
481 * Compute slice/image to use for 1D or 2D array texture.
484 tex_array_slice(GLfloat coord
, GLsizei size
)
486 GLint slice
= IFLOOR(coord
+ 0.5f
);
487 slice
= CLAMP(slice
, 0, size
- 1);
493 * Compute nearest integer texcoords for given texobj and coordinate.
494 * NOTE: only used for depth texture sampling.
497 nearest_texcoord(const struct gl_texture_object
*texObj
,
499 const GLfloat texcoord
[4],
500 GLint
*i
, GLint
*j
, GLint
*k
)
502 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
503 const GLint width
= img
->Width
;
504 const GLint height
= img
->Height
;
505 const GLint depth
= img
->Depth
;
507 switch (texObj
->Target
) {
508 case GL_TEXTURE_RECTANGLE_ARB
:
509 *i
= clamp_rect_coord_nearest(texObj
->WrapS
, texcoord
[0], width
);
510 *j
= clamp_rect_coord_nearest(texObj
->WrapT
, texcoord
[1], height
);
514 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
519 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
520 *j
= nearest_texel_location(texObj
->WrapT
, img
, height
, texcoord
[1]);
523 case GL_TEXTURE_1D_ARRAY_EXT
:
524 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
525 *j
= tex_array_slice(texcoord
[1], height
);
528 case GL_TEXTURE_2D_ARRAY_EXT
:
529 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
530 *j
= nearest_texel_location(texObj
->WrapT
, img
, height
, texcoord
[1]);
531 *k
= tex_array_slice(texcoord
[2], depth
);
540 * Compute linear integer texcoords for given texobj and coordinate.
541 * NOTE: only used for depth texture sampling.
544 linear_texcoord(const struct gl_texture_object
*texObj
,
546 const GLfloat texcoord
[4],
547 GLint
*i0
, GLint
*i1
, GLint
*j0
, GLint
*j1
, GLint
*slice
,
548 GLfloat
*wi
, GLfloat
*wj
)
550 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
551 const GLint width
= img
->Width
;
552 const GLint height
= img
->Height
;
553 const GLint depth
= img
->Depth
;
555 switch (texObj
->Target
) {
556 case GL_TEXTURE_RECTANGLE_ARB
:
557 clamp_rect_coord_linear(texObj
->WrapS
, texcoord
[0],
559 clamp_rect_coord_linear(texObj
->WrapT
, texcoord
[1],
566 linear_texel_locations(texObj
->WrapS
, img
, width
,
567 texcoord
[0], i0
, i1
, wi
);
568 linear_texel_locations(texObj
->WrapT
, img
, height
,
569 texcoord
[1], j0
, j1
, wj
);
573 case GL_TEXTURE_1D_ARRAY_EXT
:
574 linear_texel_locations(texObj
->WrapS
, img
, width
,
575 texcoord
[0], i0
, i1
, wi
);
576 *j0
= tex_array_slice(texcoord
[1], height
);
581 case GL_TEXTURE_2D_ARRAY_EXT
:
582 linear_texel_locations(texObj
->WrapS
, img
, width
,
583 texcoord
[0], i0
, i1
, wi
);
584 linear_texel_locations(texObj
->WrapT
, img
, height
,
585 texcoord
[1], j0
, j1
, wj
);
586 *slice
= tex_array_slice(texcoord
[2], depth
);
597 * For linear interpolation between mipmap levels N and N+1, this function
601 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
604 return tObj
->BaseLevel
;
605 else if (lambda
> tObj
->_MaxLambda
)
606 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
608 return (GLint
) (tObj
->BaseLevel
+ lambda
);
613 * Compute the nearest mipmap level to take texels from.
616 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
622 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
623 l
= tObj
->_MaxLambda
+ 0.4999F
;
626 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
627 if (level
> tObj
->_MaxLevel
)
628 level
= tObj
->_MaxLevel
;
635 * Bitflags for texture border color sampling.
647 * The lambda[] array values are always monotonic. Either the whole span
648 * will be minified, magnified, or split between the two. This function
649 * determines the subranges in [0, n-1] that are to be minified or magnified.
652 compute_min_mag_ranges(const struct gl_texture_object
*tObj
,
653 GLuint n
, const GLfloat lambda
[],
654 GLuint
*minStart
, GLuint
*minEnd
,
655 GLuint
*magStart
, GLuint
*magEnd
)
657 GLfloat minMagThresh
;
659 /* we shouldn't be here if minfilter == magfilter */
660 ASSERT(tObj
->MinFilter
!= tObj
->MagFilter
);
662 /* This bit comes from the OpenGL spec: */
663 if (tObj
->MagFilter
== GL_LINEAR
664 && (tObj
->MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
665 tObj
->MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
673 /* DEBUG CODE: Verify that lambda[] is monotonic.
674 * We can't really use this because the inaccuracy in the LOG2 function
675 * causes this test to fail, yet the resulting texturing is correct.
679 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
680 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
681 for (i
= 0; i
< n
- 1; i
++) {
682 ASSERT((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
685 else { /* increasing */
686 for (i
= 0; i
< n
- 1; i
++) {
687 ASSERT((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
693 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
694 /* magnification for whole span */
697 *minStart
= *minEnd
= 0;
699 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
700 /* minification for whole span */
703 *magStart
= *magEnd
= 0;
706 /* a mix of minification and magnification */
708 if (lambda
[0] > minMagThresh
) {
709 /* start with minification */
710 for (i
= 1; i
< n
; i
++) {
711 if (lambda
[i
] <= minMagThresh
)
720 /* start with magnification */
721 for (i
= 1; i
< n
; i
++) {
722 if (lambda
[i
] > minMagThresh
)
733 /* Verify the min/mag Start/End values
734 * We don't use this either (see above)
738 for (i
= 0; i
< n
; i
++) {
739 if (lambda
[i
] > minMagThresh
) {
741 ASSERT(i
>= *minStart
);
746 ASSERT(i
>= *magStart
);
756 * When we sample the border color, it must be interpreted according to
757 * the base texture format. Ex: if the texture base format it GL_ALPHA,
758 * we return (0,0,0,BorderAlpha).
761 get_border_color(const struct gl_texture_object
*tObj
,
762 const struct gl_texture_image
*img
,
765 switch (img
->_BaseFormat
) {
767 rgba
[0] = tObj
->BorderColor
.f
[0];
768 rgba
[1] = tObj
->BorderColor
.f
[1];
769 rgba
[2] = tObj
->BorderColor
.f
[2];
773 rgba
[0] = rgba
[1] = rgba
[2] = 0.0;
774 rgba
[3] = tObj
->BorderColor
.f
[3];
777 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->BorderColor
.f
[0];
780 case GL_LUMINANCE_ALPHA
:
781 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->BorderColor
.f
[0];
782 rgba
[3] = tObj
->BorderColor
.f
[3];
785 rgba
[0] = rgba
[1] = rgba
[2] = rgba
[3] = tObj
->BorderColor
.f
[0];
788 COPY_4V(rgba
, tObj
->BorderColor
.f
);
793 /**********************************************************************/
794 /* 1-D Texture Sampling Functions */
795 /**********************************************************************/
798 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
801 sample_1d_nearest(struct gl_context
*ctx
,
802 const struct gl_texture_object
*tObj
,
803 const struct gl_texture_image
*img
,
804 const GLfloat texcoord
[4], GLfloat rgba
[4])
806 const GLint width
= img
->Width2
; /* without border, power of two */
808 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
809 /* skip over the border, if any */
811 if (i
< 0 || i
>= (GLint
) img
->Width
) {
812 /* Need this test for GL_CLAMP_TO_BORDER mode */
813 get_border_color(tObj
, img
, rgba
);
816 img
->FetchTexelf(img
, i
, 0, 0, rgba
);
822 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
825 sample_1d_linear(struct gl_context
*ctx
,
826 const struct gl_texture_object
*tObj
,
827 const struct gl_texture_image
*img
,
828 const GLfloat texcoord
[4], GLfloat rgba
[4])
830 const GLint width
= img
->Width2
;
832 GLbitfield useBorderColor
= 0x0;
834 GLfloat t0
[4], t1
[4]; /* texels */
836 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
843 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
844 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
847 /* fetch texel colors */
848 if (useBorderColor
& I0BIT
) {
849 get_border_color(tObj
, img
, t0
);
852 img
->FetchTexelf(img
, i0
, 0, 0, t0
);
854 if (useBorderColor
& I1BIT
) {
855 get_border_color(tObj
, img
, t1
);
858 img
->FetchTexelf(img
, i1
, 0, 0, t1
);
861 lerp_rgba(rgba
, a
, t0
, t1
);
866 sample_1d_nearest_mipmap_nearest(struct gl_context
*ctx
,
867 const struct gl_texture_object
*tObj
,
868 GLuint n
, const GLfloat texcoord
[][4],
869 const GLfloat lambda
[], GLfloat rgba
[][4])
872 ASSERT(lambda
!= NULL
);
873 for (i
= 0; i
< n
; i
++) {
874 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
875 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
881 sample_1d_linear_mipmap_nearest(struct gl_context
*ctx
,
882 const struct gl_texture_object
*tObj
,
883 GLuint n
, const GLfloat texcoord
[][4],
884 const GLfloat lambda
[], GLfloat rgba
[][4])
887 ASSERT(lambda
!= NULL
);
888 for (i
= 0; i
< n
; i
++) {
889 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
890 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
896 sample_1d_nearest_mipmap_linear(struct gl_context
*ctx
,
897 const struct gl_texture_object
*tObj
,
898 GLuint n
, const GLfloat texcoord
[][4],
899 const GLfloat lambda
[], GLfloat rgba
[][4])
902 ASSERT(lambda
!= NULL
);
903 for (i
= 0; i
< n
; i
++) {
904 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
905 if (level
>= tObj
->_MaxLevel
) {
906 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
907 texcoord
[i
], rgba
[i
]);
910 GLfloat t0
[4], t1
[4];
911 const GLfloat f
= FRAC(lambda
[i
]);
912 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
913 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
914 lerp_rgba(rgba
[i
], f
, t0
, t1
);
921 sample_1d_linear_mipmap_linear(struct gl_context
*ctx
,
922 const struct gl_texture_object
*tObj
,
923 GLuint n
, const GLfloat texcoord
[][4],
924 const GLfloat lambda
[], GLfloat rgba
[][4])
927 ASSERT(lambda
!= NULL
);
928 for (i
= 0; i
< n
; i
++) {
929 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
930 if (level
>= tObj
->_MaxLevel
) {
931 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
932 texcoord
[i
], rgba
[i
]);
935 GLfloat t0
[4], t1
[4];
936 const GLfloat f
= FRAC(lambda
[i
]);
937 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
938 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
939 lerp_rgba(rgba
[i
], f
, t0
, t1
);
945 /** Sample 1D texture, nearest filtering for both min/magnification */
947 sample_nearest_1d( struct gl_context
*ctx
,
948 const struct gl_texture_object
*tObj
, GLuint n
,
949 const GLfloat texcoords
[][4], const GLfloat lambda
[],
953 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
955 for (i
= 0; i
< n
; i
++) {
956 sample_1d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
961 /** Sample 1D texture, linear filtering for both min/magnification */
963 sample_linear_1d( struct gl_context
*ctx
,
964 const struct gl_texture_object
*tObj
, GLuint n
,
965 const GLfloat texcoords
[][4], const GLfloat lambda
[],
969 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
971 for (i
= 0; i
< n
; i
++) {
972 sample_1d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
977 /** Sample 1D texture, using lambda to choose between min/magnification */
979 sample_lambda_1d( struct gl_context
*ctx
,
980 const struct gl_texture_object
*tObj
, GLuint n
,
981 const GLfloat texcoords
[][4],
982 const GLfloat lambda
[], GLfloat rgba
[][4] )
984 GLuint minStart
, minEnd
; /* texels with minification */
985 GLuint magStart
, magEnd
; /* texels with magnification */
988 ASSERT(lambda
!= NULL
);
989 compute_min_mag_ranges(tObj
, n
, lambda
,
990 &minStart
, &minEnd
, &magStart
, &magEnd
);
992 if (minStart
< minEnd
) {
993 /* do the minified texels */
994 const GLuint m
= minEnd
- minStart
;
995 switch (tObj
->MinFilter
) {
997 for (i
= minStart
; i
< minEnd
; i
++)
998 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
999 texcoords
[i
], rgba
[i
]);
1002 for (i
= minStart
; i
< minEnd
; i
++)
1003 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1004 texcoords
[i
], rgba
[i
]);
1006 case GL_NEAREST_MIPMAP_NEAREST
:
1007 sample_1d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1008 lambda
+ minStart
, rgba
+ minStart
);
1010 case GL_LINEAR_MIPMAP_NEAREST
:
1011 sample_1d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1012 lambda
+ minStart
, rgba
+ minStart
);
1014 case GL_NEAREST_MIPMAP_LINEAR
:
1015 sample_1d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1016 lambda
+ minStart
, rgba
+ minStart
);
1018 case GL_LINEAR_MIPMAP_LINEAR
:
1019 sample_1d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1020 lambda
+ minStart
, rgba
+ minStart
);
1023 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
1028 if (magStart
< magEnd
) {
1029 /* do the magnified texels */
1030 switch (tObj
->MagFilter
) {
1032 for (i
= magStart
; i
< magEnd
; i
++)
1033 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1034 texcoords
[i
], rgba
[i
]);
1037 for (i
= magStart
; i
< magEnd
; i
++)
1038 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1039 texcoords
[i
], rgba
[i
]);
1042 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
1049 /**********************************************************************/
1050 /* 2-D Texture Sampling Functions */
1051 /**********************************************************************/
1055 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1058 sample_2d_nearest(struct gl_context
*ctx
,
1059 const struct gl_texture_object
*tObj
,
1060 const struct gl_texture_image
*img
,
1061 const GLfloat texcoord
[4],
1064 const GLint width
= img
->Width2
; /* without border, power of two */
1065 const GLint height
= img
->Height2
; /* without border, power of two */
1069 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
1070 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
1072 /* skip over the border, if any */
1076 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
1077 /* Need this test for GL_CLAMP_TO_BORDER mode */
1078 get_border_color(tObj
, img
, rgba
);
1081 img
->FetchTexelf(img
, i
, j
, 0, rgba
);
1087 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1088 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1091 sample_2d_linear(struct gl_context
*ctx
,
1092 const struct gl_texture_object
*tObj
,
1093 const struct gl_texture_image
*img
,
1094 const GLfloat texcoord
[4],
1097 const GLint width
= img
->Width2
;
1098 const GLint height
= img
->Height2
;
1099 GLint i0
, j0
, i1
, j1
;
1100 GLbitfield useBorderColor
= 0x0;
1102 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1104 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1105 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1114 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1115 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1116 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1117 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1120 /* fetch four texel colors */
1121 if (useBorderColor
& (I0BIT
| J0BIT
)) {
1122 get_border_color(tObj
, img
, t00
);
1125 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
1127 if (useBorderColor
& (I1BIT
| J0BIT
)) {
1128 get_border_color(tObj
, img
, t10
);
1131 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
1133 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1134 get_border_color(tObj
, img
, t01
);
1137 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
1139 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1140 get_border_color(tObj
, img
, t11
);
1143 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
1146 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1151 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1152 * We don't have to worry about the texture border.
1155 sample_2d_linear_repeat(struct gl_context
*ctx
,
1156 const struct gl_texture_object
*tObj
,
1157 const struct gl_texture_image
*img
,
1158 const GLfloat texcoord
[4],
1161 const GLint width
= img
->Width2
;
1162 const GLint height
= img
->Height2
;
1163 GLint i0
, j0
, i1
, j1
;
1165 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1169 ASSERT(tObj
->WrapS
== GL_REPEAT
);
1170 ASSERT(tObj
->WrapT
== GL_REPEAT
);
1171 ASSERT(img
->Border
== 0);
1172 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
1173 ASSERT(img
->_IsPowerOfTwo
);
1175 linear_repeat_texel_location(width
, texcoord
[0], &i0
, &i1
, &wi
);
1176 linear_repeat_texel_location(height
, texcoord
[1], &j0
, &j1
, &wj
);
1178 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
1179 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
1180 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
1181 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
1183 lerp_rgba_2d(rgba
, wi
, wj
, t00
, t10
, t01
, t11
);
1188 sample_2d_nearest_mipmap_nearest(struct gl_context
*ctx
,
1189 const struct gl_texture_object
*tObj
,
1190 GLuint n
, const GLfloat texcoord
[][4],
1191 const GLfloat lambda
[], GLfloat rgba
[][4])
1194 for (i
= 0; i
< n
; i
++) {
1195 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1196 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1202 sample_2d_linear_mipmap_nearest(struct gl_context
*ctx
,
1203 const struct gl_texture_object
*tObj
,
1204 GLuint n
, const GLfloat texcoord
[][4],
1205 const GLfloat lambda
[], GLfloat rgba
[][4])
1208 ASSERT(lambda
!= NULL
);
1209 for (i
= 0; i
< n
; i
++) {
1210 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1211 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1217 sample_2d_nearest_mipmap_linear(struct gl_context
*ctx
,
1218 const struct gl_texture_object
*tObj
,
1219 GLuint n
, const GLfloat texcoord
[][4],
1220 const GLfloat lambda
[], GLfloat rgba
[][4])
1223 ASSERT(lambda
!= NULL
);
1224 for (i
= 0; i
< n
; i
++) {
1225 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1226 if (level
>= tObj
->_MaxLevel
) {
1227 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1228 texcoord
[i
], rgba
[i
]);
1231 GLfloat t0
[4], t1
[4]; /* texels */
1232 const GLfloat f
= FRAC(lambda
[i
]);
1233 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1234 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1235 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1242 sample_2d_linear_mipmap_linear( struct gl_context
*ctx
,
1243 const struct gl_texture_object
*tObj
,
1244 GLuint n
, const GLfloat texcoord
[][4],
1245 const GLfloat lambda
[], GLfloat rgba
[][4] )
1248 ASSERT(lambda
!= NULL
);
1249 for (i
= 0; i
< n
; i
++) {
1250 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1251 if (level
>= tObj
->_MaxLevel
) {
1252 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1253 texcoord
[i
], rgba
[i
]);
1256 GLfloat t0
[4], t1
[4]; /* texels */
1257 const GLfloat f
= FRAC(lambda
[i
]);
1258 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1259 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1260 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1267 sample_2d_linear_mipmap_linear_repeat(struct gl_context
*ctx
,
1268 const struct gl_texture_object
*tObj
,
1269 GLuint n
, const GLfloat texcoord
[][4],
1270 const GLfloat lambda
[], GLfloat rgba
[][4])
1273 ASSERT(lambda
!= NULL
);
1274 ASSERT(tObj
->WrapS
== GL_REPEAT
);
1275 ASSERT(tObj
->WrapT
== GL_REPEAT
);
1276 for (i
= 0; i
< n
; i
++) {
1277 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1278 if (level
>= tObj
->_MaxLevel
) {
1279 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1280 texcoord
[i
], rgba
[i
]);
1283 GLfloat t0
[4], t1
[4]; /* texels */
1284 const GLfloat f
= FRAC(lambda
[i
]);
1285 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
],
1287 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
+1],
1289 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1295 /** Sample 2D texture, nearest filtering for both min/magnification */
1297 sample_nearest_2d(struct gl_context
*ctx
,
1298 const struct gl_texture_object
*tObj
, GLuint n
,
1299 const GLfloat texcoords
[][4],
1300 const GLfloat lambda
[], GLfloat rgba
[][4])
1303 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1305 for (i
= 0; i
< n
; i
++) {
1306 sample_2d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1311 /** Sample 2D texture, linear filtering for both min/magnification */
1313 sample_linear_2d(struct gl_context
*ctx
,
1314 const struct gl_texture_object
*tObj
, GLuint n
,
1315 const GLfloat texcoords
[][4],
1316 const GLfloat lambda
[], GLfloat rgba
[][4])
1319 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1321 if (tObj
->WrapS
== GL_REPEAT
&&
1322 tObj
->WrapT
== GL_REPEAT
&&
1323 image
->_IsPowerOfTwo
&&
1324 image
->Border
== 0) {
1325 for (i
= 0; i
< n
; i
++) {
1326 sample_2d_linear_repeat(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1330 for (i
= 0; i
< n
; i
++) {
1331 sample_2d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1338 * Optimized 2-D texture sampling:
1339 * S and T wrap mode == GL_REPEAT
1340 * GL_NEAREST min/mag filter
1342 * RowStride == Width,
1346 opt_sample_rgb_2d(struct gl_context
*ctx
,
1347 const struct gl_texture_object
*tObj
,
1348 GLuint n
, const GLfloat texcoords
[][4],
1349 const GLfloat lambda
[], GLfloat rgba
[][4])
1351 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1352 const GLfloat width
= (GLfloat
) img
->Width
;
1353 const GLfloat height
= (GLfloat
) img
->Height
;
1354 const GLint colMask
= img
->Width
- 1;
1355 const GLint rowMask
= img
->Height
- 1;
1356 const GLint shift
= img
->WidthLog2
;
1360 ASSERT(tObj
->WrapS
==GL_REPEAT
);
1361 ASSERT(tObj
->WrapT
==GL_REPEAT
);
1362 ASSERT(img
->Border
==0);
1363 ASSERT(img
->TexFormat
== MESA_FORMAT_RGB888
);
1364 ASSERT(img
->_IsPowerOfTwo
);
1366 for (k
=0; k
<n
; k
++) {
1367 GLint i
= IFLOOR(texcoords
[k
][0] * width
) & colMask
;
1368 GLint j
= IFLOOR(texcoords
[k
][1] * height
) & rowMask
;
1369 GLint pos
= (j
<< shift
) | i
;
1370 GLubyte
*texel
= ((GLubyte
*) img
->Data
) + 3*pos
;
1371 rgba
[k
][RCOMP
] = UBYTE_TO_FLOAT(texel
[2]);
1372 rgba
[k
][GCOMP
] = UBYTE_TO_FLOAT(texel
[1]);
1373 rgba
[k
][BCOMP
] = UBYTE_TO_FLOAT(texel
[0]);
1374 rgba
[k
][ACOMP
] = 1.0F
;
1380 * Optimized 2-D texture sampling:
1381 * S and T wrap mode == GL_REPEAT
1382 * GL_NEAREST min/mag filter
1384 * RowStride == Width,
1388 opt_sample_rgba_2d(struct gl_context
*ctx
,
1389 const struct gl_texture_object
*tObj
,
1390 GLuint n
, const GLfloat texcoords
[][4],
1391 const GLfloat lambda
[], GLfloat rgba
[][4])
1393 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1394 const GLfloat width
= (GLfloat
) img
->Width
;
1395 const GLfloat height
= (GLfloat
) img
->Height
;
1396 const GLint colMask
= img
->Width
- 1;
1397 const GLint rowMask
= img
->Height
- 1;
1398 const GLint shift
= img
->WidthLog2
;
1402 ASSERT(tObj
->WrapS
==GL_REPEAT
);
1403 ASSERT(tObj
->WrapT
==GL_REPEAT
);
1404 ASSERT(img
->Border
==0);
1405 ASSERT(img
->TexFormat
== MESA_FORMAT_RGBA8888
);
1406 ASSERT(img
->_IsPowerOfTwo
);
1408 for (i
= 0; i
< n
; i
++) {
1409 const GLint col
= IFLOOR(texcoords
[i
][0] * width
) & colMask
;
1410 const GLint row
= IFLOOR(texcoords
[i
][1] * height
) & rowMask
;
1411 const GLint pos
= (row
<< shift
) | col
;
1412 const GLuint texel
= *((GLuint
*) img
->Data
+ pos
);
1413 rgba
[i
][RCOMP
] = UBYTE_TO_FLOAT( (texel
>> 24) );
1414 rgba
[i
][GCOMP
] = UBYTE_TO_FLOAT( (texel
>> 16) & 0xff );
1415 rgba
[i
][BCOMP
] = UBYTE_TO_FLOAT( (texel
>> 8) & 0xff );
1416 rgba
[i
][ACOMP
] = UBYTE_TO_FLOAT( (texel
) & 0xff );
1421 /** Sample 2D texture, using lambda to choose between min/magnification */
1423 sample_lambda_2d(struct gl_context
*ctx
,
1424 const struct gl_texture_object
*tObj
,
1425 GLuint n
, const GLfloat texcoords
[][4],
1426 const GLfloat lambda
[], GLfloat rgba
[][4])
1428 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1429 GLuint minStart
, minEnd
; /* texels with minification */
1430 GLuint magStart
, magEnd
; /* texels with magnification */
1432 const GLboolean repeatNoBorderPOT
= (tObj
->WrapS
== GL_REPEAT
)
1433 && (tObj
->WrapT
== GL_REPEAT
)
1434 && (tImg
->Border
== 0 && (tImg
->Width
== tImg
->RowStride
))
1435 && (tImg
->_BaseFormat
!= GL_COLOR_INDEX
)
1436 && tImg
->_IsPowerOfTwo
;
1438 ASSERT(lambda
!= NULL
);
1439 compute_min_mag_ranges(tObj
, n
, lambda
,
1440 &minStart
, &minEnd
, &magStart
, &magEnd
);
1442 if (minStart
< minEnd
) {
1443 /* do the minified texels */
1444 const GLuint m
= minEnd
- minStart
;
1445 switch (tObj
->MinFilter
) {
1447 if (repeatNoBorderPOT
) {
1448 switch (tImg
->TexFormat
) {
1449 case MESA_FORMAT_RGB888
:
1450 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1451 NULL
, rgba
+ minStart
);
1453 case MESA_FORMAT_RGBA8888
:
1454 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1455 NULL
, rgba
+ minStart
);
1458 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1459 NULL
, rgba
+ minStart
);
1463 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1464 NULL
, rgba
+ minStart
);
1468 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1469 NULL
, rgba
+ minStart
);
1471 case GL_NEAREST_MIPMAP_NEAREST
:
1472 sample_2d_nearest_mipmap_nearest(ctx
, tObj
, m
,
1473 texcoords
+ minStart
,
1474 lambda
+ minStart
, rgba
+ minStart
);
1476 case GL_LINEAR_MIPMAP_NEAREST
:
1477 sample_2d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1478 lambda
+ minStart
, rgba
+ minStart
);
1480 case GL_NEAREST_MIPMAP_LINEAR
:
1481 sample_2d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1482 lambda
+ minStart
, rgba
+ minStart
);
1484 case GL_LINEAR_MIPMAP_LINEAR
:
1485 if (repeatNoBorderPOT
)
1486 sample_2d_linear_mipmap_linear_repeat(ctx
, tObj
, m
,
1487 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1489 sample_2d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1490 lambda
+ minStart
, rgba
+ minStart
);
1493 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1498 if (magStart
< magEnd
) {
1499 /* do the magnified texels */
1500 const GLuint m
= magEnd
- magStart
;
1502 switch (tObj
->MagFilter
) {
1504 if (repeatNoBorderPOT
) {
1505 switch (tImg
->TexFormat
) {
1506 case MESA_FORMAT_RGB888
:
1507 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1508 NULL
, rgba
+ magStart
);
1510 case MESA_FORMAT_RGBA8888
:
1511 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1512 NULL
, rgba
+ magStart
);
1515 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1516 NULL
, rgba
+ magStart
);
1520 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1521 NULL
, rgba
+ magStart
);
1525 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1526 NULL
, rgba
+ magStart
);
1529 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1536 /**********************************************************************/
1537 /* 3-D Texture Sampling Functions */
1538 /**********************************************************************/
1541 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1544 sample_3d_nearest(struct gl_context
*ctx
,
1545 const struct gl_texture_object
*tObj
,
1546 const struct gl_texture_image
*img
,
1547 const GLfloat texcoord
[4],
1550 const GLint width
= img
->Width2
; /* without border, power of two */
1551 const GLint height
= img
->Height2
; /* without border, power of two */
1552 const GLint depth
= img
->Depth2
; /* without border, power of two */
1556 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
1557 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
1558 k
= nearest_texel_location(tObj
->WrapR
, img
, depth
, texcoord
[2]);
1560 if (i
< 0 || i
>= (GLint
) img
->Width
||
1561 j
< 0 || j
>= (GLint
) img
->Height
||
1562 k
< 0 || k
>= (GLint
) img
->Depth
) {
1563 /* Need this test for GL_CLAMP_TO_BORDER mode */
1564 get_border_color(tObj
, img
, rgba
);
1567 img
->FetchTexelf(img
, i
, j
, k
, rgba
);
1573 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1576 sample_3d_linear(struct gl_context
*ctx
,
1577 const struct gl_texture_object
*tObj
,
1578 const struct gl_texture_image
*img
,
1579 const GLfloat texcoord
[4],
1582 const GLint width
= img
->Width2
;
1583 const GLint height
= img
->Height2
;
1584 const GLint depth
= img
->Depth2
;
1585 GLint i0
, j0
, k0
, i1
, j1
, k1
;
1586 GLbitfield useBorderColor
= 0x0;
1588 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
1589 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
1591 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1592 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1593 linear_texel_locations(tObj
->WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
1604 /* check if sampling texture border color */
1605 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1606 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1607 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1608 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1609 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
1610 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
1614 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
1615 get_border_color(tObj
, img
, t000
);
1618 img
->FetchTexelf(img
, i0
, j0
, k0
, t000
);
1620 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
1621 get_border_color(tObj
, img
, t100
);
1624 img
->FetchTexelf(img
, i1
, j0
, k0
, t100
);
1626 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
1627 get_border_color(tObj
, img
, t010
);
1630 img
->FetchTexelf(img
, i0
, j1
, k0
, t010
);
1632 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
1633 get_border_color(tObj
, img
, t110
);
1636 img
->FetchTexelf(img
, i1
, j1
, k0
, t110
);
1639 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
1640 get_border_color(tObj
, img
, t001
);
1643 img
->FetchTexelf(img
, i0
, j0
, k1
, t001
);
1645 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
1646 get_border_color(tObj
, img
, t101
);
1649 img
->FetchTexelf(img
, i1
, j0
, k1
, t101
);
1651 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
1652 get_border_color(tObj
, img
, t011
);
1655 img
->FetchTexelf(img
, i0
, j1
, k1
, t011
);
1657 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
1658 get_border_color(tObj
, img
, t111
);
1661 img
->FetchTexelf(img
, i1
, j1
, k1
, t111
);
1664 /* trilinear interpolation of samples */
1665 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
1670 sample_3d_nearest_mipmap_nearest(struct gl_context
*ctx
,
1671 const struct gl_texture_object
*tObj
,
1672 GLuint n
, const GLfloat texcoord
[][4],
1673 const GLfloat lambda
[], GLfloat rgba
[][4] )
1676 for (i
= 0; i
< n
; i
++) {
1677 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1678 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1684 sample_3d_linear_mipmap_nearest(struct gl_context
*ctx
,
1685 const struct gl_texture_object
*tObj
,
1686 GLuint n
, const GLfloat texcoord
[][4],
1687 const GLfloat lambda
[], GLfloat rgba
[][4])
1690 ASSERT(lambda
!= NULL
);
1691 for (i
= 0; i
< n
; i
++) {
1692 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1693 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1699 sample_3d_nearest_mipmap_linear(struct gl_context
*ctx
,
1700 const struct gl_texture_object
*tObj
,
1701 GLuint n
, const GLfloat texcoord
[][4],
1702 const GLfloat lambda
[], GLfloat rgba
[][4])
1705 ASSERT(lambda
!= NULL
);
1706 for (i
= 0; i
< n
; i
++) {
1707 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1708 if (level
>= tObj
->_MaxLevel
) {
1709 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1710 texcoord
[i
], rgba
[i
]);
1713 GLfloat t0
[4], t1
[4]; /* texels */
1714 const GLfloat f
= FRAC(lambda
[i
]);
1715 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1716 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1717 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1724 sample_3d_linear_mipmap_linear(struct gl_context
*ctx
,
1725 const struct gl_texture_object
*tObj
,
1726 GLuint n
, const GLfloat texcoord
[][4],
1727 const GLfloat lambda
[], GLfloat rgba
[][4])
1730 ASSERT(lambda
!= NULL
);
1731 for (i
= 0; i
< n
; i
++) {
1732 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1733 if (level
>= tObj
->_MaxLevel
) {
1734 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1735 texcoord
[i
], rgba
[i
]);
1738 GLfloat t0
[4], t1
[4]; /* texels */
1739 const GLfloat f
= FRAC(lambda
[i
]);
1740 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1741 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1742 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1748 /** Sample 3D texture, nearest filtering for both min/magnification */
1750 sample_nearest_3d(struct gl_context
*ctx
,
1751 const struct gl_texture_object
*tObj
, GLuint n
,
1752 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1756 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1758 for (i
= 0; i
< n
; i
++) {
1759 sample_3d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1764 /** Sample 3D texture, linear filtering for both min/magnification */
1766 sample_linear_3d(struct gl_context
*ctx
,
1767 const struct gl_texture_object
*tObj
, GLuint n
,
1768 const GLfloat texcoords
[][4],
1769 const GLfloat lambda
[], GLfloat rgba
[][4])
1772 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1774 for (i
= 0; i
< n
; i
++) {
1775 sample_3d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1780 /** Sample 3D texture, using lambda to choose between min/magnification */
1782 sample_lambda_3d(struct gl_context
*ctx
,
1783 const struct gl_texture_object
*tObj
, GLuint n
,
1784 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1787 GLuint minStart
, minEnd
; /* texels with minification */
1788 GLuint magStart
, magEnd
; /* texels with magnification */
1791 ASSERT(lambda
!= NULL
);
1792 compute_min_mag_ranges(tObj
, n
, lambda
,
1793 &minStart
, &minEnd
, &magStart
, &magEnd
);
1795 if (minStart
< minEnd
) {
1796 /* do the minified texels */
1797 GLuint m
= minEnd
- minStart
;
1798 switch (tObj
->MinFilter
) {
1800 for (i
= minStart
; i
< minEnd
; i
++)
1801 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1802 texcoords
[i
], rgba
[i
]);
1805 for (i
= minStart
; i
< minEnd
; i
++)
1806 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1807 texcoords
[i
], rgba
[i
]);
1809 case GL_NEAREST_MIPMAP_NEAREST
:
1810 sample_3d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1811 lambda
+ minStart
, rgba
+ minStart
);
1813 case GL_LINEAR_MIPMAP_NEAREST
:
1814 sample_3d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1815 lambda
+ minStart
, rgba
+ minStart
);
1817 case GL_NEAREST_MIPMAP_LINEAR
:
1818 sample_3d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1819 lambda
+ minStart
, rgba
+ minStart
);
1821 case GL_LINEAR_MIPMAP_LINEAR
:
1822 sample_3d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1823 lambda
+ minStart
, rgba
+ minStart
);
1826 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
1831 if (magStart
< magEnd
) {
1832 /* do the magnified texels */
1833 switch (tObj
->MagFilter
) {
1835 for (i
= magStart
; i
< magEnd
; i
++)
1836 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1837 texcoords
[i
], rgba
[i
]);
1840 for (i
= magStart
; i
< magEnd
; i
++)
1841 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1842 texcoords
[i
], rgba
[i
]);
1845 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
1852 /**********************************************************************/
1853 /* Texture Cube Map Sampling Functions */
1854 /**********************************************************************/
1857 * Choose one of six sides of a texture cube map given the texture
1858 * coord (rx,ry,rz). Return pointer to corresponding array of texture
1861 static const struct gl_texture_image
**
1862 choose_cube_face(const struct gl_texture_object
*texObj
,
1863 const GLfloat texcoord
[4], GLfloat newCoord
[4])
1867 direction target sc tc ma
1868 ---------- ------------------------------- --- --- ---
1869 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1870 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1871 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1872 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1873 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1874 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1876 const GLfloat rx
= texcoord
[0];
1877 const GLfloat ry
= texcoord
[1];
1878 const GLfloat rz
= texcoord
[2];
1879 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
1883 if (arx
>= ary
&& arx
>= arz
) {
1897 else if (ary
>= arx
&& ary
>= arz
) {
1927 const float ima
= 1.0F
/ ma
;
1928 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
1929 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
1932 return (const struct gl_texture_image
**) texObj
->Image
[face
];
1937 sample_nearest_cube(struct gl_context
*ctx
,
1938 const struct gl_texture_object
*tObj
, GLuint n
,
1939 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1944 for (i
= 0; i
< n
; i
++) {
1945 const struct gl_texture_image
**images
;
1946 GLfloat newCoord
[4];
1947 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1948 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
1955 sample_linear_cube(struct gl_context
*ctx
,
1956 const struct gl_texture_object
*tObj
, GLuint n
,
1957 const GLfloat texcoords
[][4],
1958 const GLfloat lambda
[], GLfloat rgba
[][4])
1962 for (i
= 0; i
< n
; i
++) {
1963 const struct gl_texture_image
**images
;
1964 GLfloat newCoord
[4];
1965 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1966 sample_2d_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
1973 sample_cube_nearest_mipmap_nearest(struct gl_context
*ctx
,
1974 const struct gl_texture_object
*tObj
,
1975 GLuint n
, const GLfloat texcoord
[][4],
1976 const GLfloat lambda
[], GLfloat rgba
[][4])
1979 ASSERT(lambda
!= NULL
);
1980 for (i
= 0; i
< n
; i
++) {
1981 const struct gl_texture_image
**images
;
1982 GLfloat newCoord
[4];
1984 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1986 /* XXX we actually need to recompute lambda here based on the newCoords.
1987 * But we would need the texcoords of adjacent fragments to compute that
1988 * properly, and we don't have those here.
1989 * For now, do an approximation: subtracting 1 from the chosen mipmap
1990 * level seems to work in some test cases.
1991 * The same adjustment is done in the next few functions.
1993 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1994 level
= MAX2(level
- 1, 0);
1996 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2002 sample_cube_linear_mipmap_nearest(struct gl_context
*ctx
,
2003 const struct gl_texture_object
*tObj
,
2004 GLuint n
, const GLfloat texcoord
[][4],
2005 const GLfloat lambda
[], GLfloat rgba
[][4])
2008 ASSERT(lambda
!= NULL
);
2009 for (i
= 0; i
< n
; i
++) {
2010 const struct gl_texture_image
**images
;
2011 GLfloat newCoord
[4];
2012 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2013 level
= MAX2(level
- 1, 0); /* see comment above */
2014 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2015 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2021 sample_cube_nearest_mipmap_linear(struct gl_context
*ctx
,
2022 const struct gl_texture_object
*tObj
,
2023 GLuint n
, const GLfloat texcoord
[][4],
2024 const GLfloat lambda
[], GLfloat rgba
[][4])
2027 ASSERT(lambda
!= NULL
);
2028 for (i
= 0; i
< n
; i
++) {
2029 const struct gl_texture_image
**images
;
2030 GLfloat newCoord
[4];
2031 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2032 level
= MAX2(level
- 1, 0); /* see comment above */
2033 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2034 if (level
>= tObj
->_MaxLevel
) {
2035 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2039 GLfloat t0
[4], t1
[4]; /* texels */
2040 const GLfloat f
= FRAC(lambda
[i
]);
2041 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
2042 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2043 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2050 sample_cube_linear_mipmap_linear(struct gl_context
*ctx
,
2051 const struct gl_texture_object
*tObj
,
2052 GLuint n
, const GLfloat texcoord
[][4],
2053 const GLfloat lambda
[], GLfloat rgba
[][4])
2056 ASSERT(lambda
!= NULL
);
2057 for (i
= 0; i
< n
; i
++) {
2058 const struct gl_texture_image
**images
;
2059 GLfloat newCoord
[4];
2060 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2061 level
= MAX2(level
- 1, 0); /* see comment above */
2062 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2063 if (level
>= tObj
->_MaxLevel
) {
2064 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2068 GLfloat t0
[4], t1
[4];
2069 const GLfloat f
= FRAC(lambda
[i
]);
2070 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
2071 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2072 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2078 /** Sample cube texture, using lambda to choose between min/magnification */
2080 sample_lambda_cube(struct gl_context
*ctx
,
2081 const struct gl_texture_object
*tObj
, GLuint n
,
2082 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2085 GLuint minStart
, minEnd
; /* texels with minification */
2086 GLuint magStart
, magEnd
; /* texels with magnification */
2088 ASSERT(lambda
!= NULL
);
2089 compute_min_mag_ranges(tObj
, n
, lambda
,
2090 &minStart
, &minEnd
, &magStart
, &magEnd
);
2092 if (minStart
< minEnd
) {
2093 /* do the minified texels */
2094 const GLuint m
= minEnd
- minStart
;
2095 switch (tObj
->MinFilter
) {
2097 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2098 lambda
+ minStart
, rgba
+ minStart
);
2101 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2102 lambda
+ minStart
, rgba
+ minStart
);
2104 case GL_NEAREST_MIPMAP_NEAREST
:
2105 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
2106 texcoords
+ minStart
,
2107 lambda
+ minStart
, rgba
+ minStart
);
2109 case GL_LINEAR_MIPMAP_NEAREST
:
2110 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
2111 texcoords
+ minStart
,
2112 lambda
+ minStart
, rgba
+ minStart
);
2114 case GL_NEAREST_MIPMAP_LINEAR
:
2115 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
2116 texcoords
+ minStart
,
2117 lambda
+ minStart
, rgba
+ minStart
);
2119 case GL_LINEAR_MIPMAP_LINEAR
:
2120 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
2121 texcoords
+ minStart
,
2122 lambda
+ minStart
, rgba
+ minStart
);
2125 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2129 if (magStart
< magEnd
) {
2130 /* do the magnified texels */
2131 const GLuint m
= magEnd
- magStart
;
2132 switch (tObj
->MagFilter
) {
2134 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2135 lambda
+ magStart
, rgba
+ magStart
);
2138 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2139 lambda
+ magStart
, rgba
+ magStart
);
2142 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2148 /**********************************************************************/
2149 /* Texture Rectangle Sampling Functions */
2150 /**********************************************************************/
2154 sample_nearest_rect(struct gl_context
*ctx
,
2155 const struct gl_texture_object
*tObj
, GLuint n
,
2156 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2159 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2160 const GLint width
= img
->Width
;
2161 const GLint height
= img
->Height
;
2167 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2168 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2169 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2170 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2171 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2172 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2173 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
2175 for (i
= 0; i
< n
; i
++) {
2177 col
= clamp_rect_coord_nearest(tObj
->WrapS
, texcoords
[i
][0], width
);
2178 row
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoords
[i
][1], height
);
2179 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2180 get_border_color(tObj
, img
, rgba
[i
]);
2182 img
->FetchTexelf(img
, col
, row
, 0, rgba
[i
]);
2188 sample_linear_rect(struct gl_context
*ctx
,
2189 const struct gl_texture_object
*tObj
, GLuint n
,
2190 const GLfloat texcoords
[][4],
2191 const GLfloat lambda
[], GLfloat rgba
[][4])
2193 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2194 const GLint width
= img
->Width
;
2195 const GLint height
= img
->Height
;
2201 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2202 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2203 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2204 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2205 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2206 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2207 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
2209 for (i
= 0; i
< n
; i
++) {
2210 GLint i0
, j0
, i1
, j1
;
2211 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2213 GLbitfield useBorderColor
= 0x0;
2215 clamp_rect_coord_linear(tObj
->WrapS
, texcoords
[i
][0], width
,
2217 clamp_rect_coord_linear(tObj
->WrapT
, texcoords
[i
][1], height
,
2220 /* compute integer rows/columns */
2221 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2222 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2223 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2224 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2226 /* get four texel samples */
2227 if (useBorderColor
& (I0BIT
| J0BIT
))
2228 get_border_color(tObj
, img
, t00
);
2230 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
2232 if (useBorderColor
& (I1BIT
| J0BIT
))
2233 get_border_color(tObj
, img
, t10
);
2235 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
2237 if (useBorderColor
& (I0BIT
| J1BIT
))
2238 get_border_color(tObj
, img
, t01
);
2240 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
2242 if (useBorderColor
& (I1BIT
| J1BIT
))
2243 get_border_color(tObj
, img
, t11
);
2245 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
2247 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2252 /** Sample Rect texture, using lambda to choose between min/magnification */
2254 sample_lambda_rect(struct gl_context
*ctx
,
2255 const struct gl_texture_object
*tObj
, GLuint n
,
2256 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2259 GLuint minStart
, minEnd
, magStart
, magEnd
;
2261 /* We only need lambda to decide between minification and magnification.
2262 * There is no mipmapping with rectangular textures.
2264 compute_min_mag_ranges(tObj
, n
, lambda
,
2265 &minStart
, &minEnd
, &magStart
, &magEnd
);
2267 if (minStart
< minEnd
) {
2268 if (tObj
->MinFilter
== GL_NEAREST
) {
2269 sample_nearest_rect(ctx
, tObj
, minEnd
- minStart
,
2270 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2273 sample_linear_rect(ctx
, tObj
, minEnd
- minStart
,
2274 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2277 if (magStart
< magEnd
) {
2278 if (tObj
->MagFilter
== GL_NEAREST
) {
2279 sample_nearest_rect(ctx
, tObj
, magEnd
- magStart
,
2280 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2283 sample_linear_rect(ctx
, tObj
, magEnd
- magStart
,
2284 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2290 /**********************************************************************/
2291 /* 2D Texture Array Sampling Functions */
2292 /**********************************************************************/
2295 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2298 sample_2d_array_nearest(struct gl_context
*ctx
,
2299 const struct gl_texture_object
*tObj
,
2300 const struct gl_texture_image
*img
,
2301 const GLfloat texcoord
[4],
2304 const GLint width
= img
->Width2
; /* without border, power of two */
2305 const GLint height
= img
->Height2
; /* without border, power of two */
2306 const GLint depth
= img
->Depth
;
2311 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2312 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
2313 array
= tex_array_slice(texcoord
[2], depth
);
2315 if (i
< 0 || i
>= (GLint
) img
->Width
||
2316 j
< 0 || j
>= (GLint
) img
->Height
||
2317 array
< 0 || array
>= (GLint
) img
->Depth
) {
2318 /* Need this test for GL_CLAMP_TO_BORDER mode */
2319 get_border_color(tObj
, img
, rgba
);
2322 img
->FetchTexelf(img
, i
, j
, array
, rgba
);
2328 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2331 sample_2d_array_linear(struct gl_context
*ctx
,
2332 const struct gl_texture_object
*tObj
,
2333 const struct gl_texture_image
*img
,
2334 const GLfloat texcoord
[4],
2337 const GLint width
= img
->Width2
;
2338 const GLint height
= img
->Height2
;
2339 const GLint depth
= img
->Depth
;
2340 GLint i0
, j0
, i1
, j1
;
2342 GLbitfield useBorderColor
= 0x0;
2344 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2346 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2347 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2348 array
= tex_array_slice(texcoord
[2], depth
);
2350 if (array
< 0 || array
>= depth
) {
2351 COPY_4V(rgba
, tObj
->BorderColor
.f
);
2361 /* check if sampling texture border color */
2362 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2363 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2364 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2365 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2369 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2370 get_border_color(tObj
, img
, t00
);
2373 img
->FetchTexelf(img
, i0
, j0
, array
, t00
);
2375 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2376 get_border_color(tObj
, img
, t10
);
2379 img
->FetchTexelf(img
, i1
, j0
, array
, t10
);
2381 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2382 get_border_color(tObj
, img
, t01
);
2385 img
->FetchTexelf(img
, i0
, j1
, array
, t01
);
2387 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2388 get_border_color(tObj
, img
, t11
);
2391 img
->FetchTexelf(img
, i1
, j1
, array
, t11
);
2394 /* trilinear interpolation of samples */
2395 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2401 sample_2d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2402 const struct gl_texture_object
*tObj
,
2403 GLuint n
, const GLfloat texcoord
[][4],
2404 const GLfloat lambda
[], GLfloat rgba
[][4])
2407 for (i
= 0; i
< n
; i
++) {
2408 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2409 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2416 sample_2d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2417 const struct gl_texture_object
*tObj
,
2418 GLuint n
, const GLfloat texcoord
[][4],
2419 const GLfloat lambda
[], GLfloat rgba
[][4])
2422 ASSERT(lambda
!= NULL
);
2423 for (i
= 0; i
< n
; i
++) {
2424 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2425 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2426 texcoord
[i
], rgba
[i
]);
2432 sample_2d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2433 const struct gl_texture_object
*tObj
,
2434 GLuint n
, const GLfloat texcoord
[][4],
2435 const GLfloat lambda
[], GLfloat rgba
[][4])
2438 ASSERT(lambda
!= NULL
);
2439 for (i
= 0; i
< n
; i
++) {
2440 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2441 if (level
>= tObj
->_MaxLevel
) {
2442 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2443 texcoord
[i
], rgba
[i
]);
2446 GLfloat t0
[4], t1
[4]; /* texels */
2447 const GLfloat f
= FRAC(lambda
[i
]);
2448 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
],
2450 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1],
2452 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2459 sample_2d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2460 const struct gl_texture_object
*tObj
,
2461 GLuint n
, const GLfloat texcoord
[][4],
2462 const GLfloat lambda
[], GLfloat rgba
[][4])
2465 ASSERT(lambda
!= NULL
);
2466 for (i
= 0; i
< n
; i
++) {
2467 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2468 if (level
>= tObj
->_MaxLevel
) {
2469 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2470 texcoord
[i
], rgba
[i
]);
2473 GLfloat t0
[4], t1
[4]; /* texels */
2474 const GLfloat f
= FRAC(lambda
[i
]);
2475 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2477 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1],
2479 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2485 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2487 sample_nearest_2d_array(struct gl_context
*ctx
,
2488 const struct gl_texture_object
*tObj
, GLuint n
,
2489 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2493 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2495 for (i
= 0; i
< n
; i
++) {
2496 sample_2d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2502 /** Sample 2D Array texture, linear filtering for both min/magnification */
2504 sample_linear_2d_array(struct gl_context
*ctx
,
2505 const struct gl_texture_object
*tObj
, GLuint n
,
2506 const GLfloat texcoords
[][4],
2507 const GLfloat lambda
[], GLfloat rgba
[][4])
2510 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2512 for (i
= 0; i
< n
; i
++) {
2513 sample_2d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2518 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2520 sample_lambda_2d_array(struct gl_context
*ctx
,
2521 const struct gl_texture_object
*tObj
, GLuint n
,
2522 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2525 GLuint minStart
, minEnd
; /* texels with minification */
2526 GLuint magStart
, magEnd
; /* texels with magnification */
2529 ASSERT(lambda
!= NULL
);
2530 compute_min_mag_ranges(tObj
, n
, lambda
,
2531 &minStart
, &minEnd
, &magStart
, &magEnd
);
2533 if (minStart
< minEnd
) {
2534 /* do the minified texels */
2535 GLuint m
= minEnd
- minStart
;
2536 switch (tObj
->MinFilter
) {
2538 for (i
= minStart
; i
< minEnd
; i
++)
2539 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2540 texcoords
[i
], rgba
[i
]);
2543 for (i
= minStart
; i
< minEnd
; i
++)
2544 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2545 texcoords
[i
], rgba
[i
]);
2547 case GL_NEAREST_MIPMAP_NEAREST
:
2548 sample_2d_array_nearest_mipmap_nearest(ctx
, tObj
, m
,
2549 texcoords
+ minStart
,
2553 case GL_LINEAR_MIPMAP_NEAREST
:
2554 sample_2d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2555 texcoords
+ minStart
,
2559 case GL_NEAREST_MIPMAP_LINEAR
:
2560 sample_2d_array_nearest_mipmap_linear(ctx
, tObj
, m
,
2561 texcoords
+ minStart
,
2565 case GL_LINEAR_MIPMAP_LINEAR
:
2566 sample_2d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2567 texcoords
+ minStart
,
2572 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
2577 if (magStart
< magEnd
) {
2578 /* do the magnified texels */
2579 switch (tObj
->MagFilter
) {
2581 for (i
= magStart
; i
< magEnd
; i
++)
2582 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2583 texcoords
[i
], rgba
[i
]);
2586 for (i
= magStart
; i
< magEnd
; i
++)
2587 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2588 texcoords
[i
], rgba
[i
]);
2591 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
2600 /**********************************************************************/
2601 /* 1D Texture Array Sampling Functions */
2602 /**********************************************************************/
2605 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2608 sample_1d_array_nearest(struct gl_context
*ctx
,
2609 const struct gl_texture_object
*tObj
,
2610 const struct gl_texture_image
*img
,
2611 const GLfloat texcoord
[4],
2614 const GLint width
= img
->Width2
; /* without border, power of two */
2615 const GLint height
= img
->Height
;
2620 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2621 array
= tex_array_slice(texcoord
[1], height
);
2623 if (i
< 0 || i
>= (GLint
) img
->Width
||
2624 array
< 0 || array
>= (GLint
) img
->Height
) {
2625 /* Need this test for GL_CLAMP_TO_BORDER mode */
2626 get_border_color(tObj
, img
, rgba
);
2629 img
->FetchTexelf(img
, i
, array
, 0, rgba
);
2635 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2638 sample_1d_array_linear(struct gl_context
*ctx
,
2639 const struct gl_texture_object
*tObj
,
2640 const struct gl_texture_image
*img
,
2641 const GLfloat texcoord
[4],
2644 const GLint width
= img
->Width2
;
2645 const GLint height
= img
->Height
;
2648 GLbitfield useBorderColor
= 0x0;
2650 GLfloat t0
[4], t1
[4];
2652 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2653 array
= tex_array_slice(texcoord
[1], height
);
2660 /* check if sampling texture border color */
2661 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2662 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2665 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
2668 if (useBorderColor
& (I0BIT
| K0BIT
)) {
2669 get_border_color(tObj
, img
, t0
);
2672 img
->FetchTexelf(img
, i0
, array
, 0, t0
);
2674 if (useBorderColor
& (I1BIT
| K0BIT
)) {
2675 get_border_color(tObj
, img
, t1
);
2678 img
->FetchTexelf(img
, i1
, array
, 0, t1
);
2681 /* bilinear interpolation of samples */
2682 lerp_rgba(rgba
, a
, t0
, t1
);
2687 sample_1d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2688 const struct gl_texture_object
*tObj
,
2689 GLuint n
, const GLfloat texcoord
[][4],
2690 const GLfloat lambda
[], GLfloat rgba
[][4])
2693 for (i
= 0; i
< n
; i
++) {
2694 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2695 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2702 sample_1d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2703 const struct gl_texture_object
*tObj
,
2704 GLuint n
, const GLfloat texcoord
[][4],
2705 const GLfloat lambda
[], GLfloat rgba
[][4])
2708 ASSERT(lambda
!= NULL
);
2709 for (i
= 0; i
< n
; i
++) {
2710 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2711 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2712 texcoord
[i
], rgba
[i
]);
2718 sample_1d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2719 const struct gl_texture_object
*tObj
,
2720 GLuint n
, const GLfloat texcoord
[][4],
2721 const GLfloat lambda
[], GLfloat rgba
[][4])
2724 ASSERT(lambda
!= NULL
);
2725 for (i
= 0; i
< n
; i
++) {
2726 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2727 if (level
>= tObj
->_MaxLevel
) {
2728 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2729 texcoord
[i
], rgba
[i
]);
2732 GLfloat t0
[4], t1
[4]; /* texels */
2733 const GLfloat f
= FRAC(lambda
[i
]);
2734 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2735 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2736 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2743 sample_1d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2744 const struct gl_texture_object
*tObj
,
2745 GLuint n
, const GLfloat texcoord
[][4],
2746 const GLfloat lambda
[], GLfloat rgba
[][4])
2749 ASSERT(lambda
!= NULL
);
2750 for (i
= 0; i
< n
; i
++) {
2751 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2752 if (level
>= tObj
->_MaxLevel
) {
2753 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2754 texcoord
[i
], rgba
[i
]);
2757 GLfloat t0
[4], t1
[4]; /* texels */
2758 const GLfloat f
= FRAC(lambda
[i
]);
2759 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2760 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2761 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2767 /** Sample 1D Array texture, nearest filtering for both min/magnification */
2769 sample_nearest_1d_array(struct gl_context
*ctx
,
2770 const struct gl_texture_object
*tObj
, GLuint n
,
2771 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2775 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2777 for (i
= 0; i
< n
; i
++) {
2778 sample_1d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2783 /** Sample 1D Array texture, linear filtering for both min/magnification */
2785 sample_linear_1d_array(struct gl_context
*ctx
,
2786 const struct gl_texture_object
*tObj
, GLuint n
,
2787 const GLfloat texcoords
[][4],
2788 const GLfloat lambda
[], GLfloat rgba
[][4])
2791 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2793 for (i
= 0; i
< n
; i
++) {
2794 sample_1d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2799 /** Sample 1D Array texture, using lambda to choose between min/magnification */
2801 sample_lambda_1d_array(struct gl_context
*ctx
,
2802 const struct gl_texture_object
*tObj
, GLuint n
,
2803 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2806 GLuint minStart
, minEnd
; /* texels with minification */
2807 GLuint magStart
, magEnd
; /* texels with magnification */
2810 ASSERT(lambda
!= NULL
);
2811 compute_min_mag_ranges(tObj
, n
, lambda
,
2812 &minStart
, &minEnd
, &magStart
, &magEnd
);
2814 if (minStart
< minEnd
) {
2815 /* do the minified texels */
2816 GLuint m
= minEnd
- minStart
;
2817 switch (tObj
->MinFilter
) {
2819 for (i
= minStart
; i
< minEnd
; i
++)
2820 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2821 texcoords
[i
], rgba
[i
]);
2824 for (i
= minStart
; i
< minEnd
; i
++)
2825 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2826 texcoords
[i
], rgba
[i
]);
2828 case GL_NEAREST_MIPMAP_NEAREST
:
2829 sample_1d_array_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2830 lambda
+ minStart
, rgba
+ minStart
);
2832 case GL_LINEAR_MIPMAP_NEAREST
:
2833 sample_1d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2834 texcoords
+ minStart
,
2838 case GL_NEAREST_MIPMAP_LINEAR
:
2839 sample_1d_array_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2840 lambda
+ minStart
, rgba
+ minStart
);
2842 case GL_LINEAR_MIPMAP_LINEAR
:
2843 sample_1d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2844 texcoords
+ minStart
,
2849 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
2854 if (magStart
< magEnd
) {
2855 /* do the magnified texels */
2856 switch (tObj
->MagFilter
) {
2858 for (i
= magStart
; i
< magEnd
; i
++)
2859 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2860 texcoords
[i
], rgba
[i
]);
2863 for (i
= magStart
; i
< magEnd
; i
++)
2864 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2865 texcoords
[i
], rgba
[i
]);
2868 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
2876 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
2878 static INLINE GLfloat
2879 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
,
2884 return (coord
<= depthSample
) ? 1.0F
: ambient
;
2886 return (coord
>= depthSample
) ? 1.0F
: ambient
;
2888 return (coord
< depthSample
) ? 1.0F
: ambient
;
2890 return (coord
> depthSample
) ? 1.0F
: ambient
;
2892 return (coord
== depthSample
) ? 1.0F
: ambient
;
2894 return (coord
!= depthSample
) ? 1.0F
: ambient
;
2902 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
2909 * Compare texcoord against four depth samples.
2911 static INLINE GLfloat
2912 shadow_compare4(GLenum function
, GLfloat coord
,
2913 GLfloat depth00
, GLfloat depth01
,
2914 GLfloat depth10
, GLfloat depth11
,
2915 GLfloat ambient
, GLfloat wi
, GLfloat wj
)
2917 const GLfloat d
= (1.0F
- (GLfloat
) ambient
) * 0.25F
;
2918 GLfloat luminance
= 1.0F
;
2922 if (depth00
<= coord
) luminance
-= d
;
2923 if (depth01
<= coord
) luminance
-= d
;
2924 if (depth10
<= coord
) luminance
-= d
;
2925 if (depth11
<= coord
) luminance
-= d
;
2928 if (depth00
>= coord
) luminance
-= d
;
2929 if (depth01
>= coord
) luminance
-= d
;
2930 if (depth10
>= coord
) luminance
-= d
;
2931 if (depth11
>= coord
) luminance
-= d
;
2934 if (depth00
< coord
) luminance
-= d
;
2935 if (depth01
< coord
) luminance
-= d
;
2936 if (depth10
< coord
) luminance
-= d
;
2937 if (depth11
< coord
) luminance
-= d
;
2940 if (depth00
> coord
) luminance
-= d
;
2941 if (depth01
> coord
) luminance
-= d
;
2942 if (depth10
> coord
) luminance
-= d
;
2943 if (depth11
> coord
) luminance
-= d
;
2946 if (depth00
== coord
) luminance
-= d
;
2947 if (depth01
== coord
) luminance
-= d
;
2948 if (depth10
== coord
) luminance
-= d
;
2949 if (depth11
== coord
) luminance
-= d
;
2952 if (depth00
!= coord
) luminance
-= d
;
2953 if (depth01
!= coord
) luminance
-= d
;
2954 if (depth10
!= coord
) luminance
-= d
;
2955 if (depth11
!= coord
) luminance
-= d
;
2962 /* ordinary bilinear filtering */
2963 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
2965 _mesa_problem(NULL
, "Bad compare func in sample_depth_texture");
2972 * Choose the mipmap level to use when sampling from a depth texture.
2975 choose_depth_texture_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
2979 if (tObj
->MinFilter
== GL_NEAREST
|| tObj
->MinFilter
== GL_LINEAR
) {
2980 /* no mipmapping - use base level */
2981 level
= tObj
->BaseLevel
;
2984 /* choose mipmap level */
2985 lambda
= CLAMP(lambda
, tObj
->MinLod
, tObj
->MaxLod
);
2986 level
= (GLint
) lambda
;
2987 level
= CLAMP(level
, tObj
->BaseLevel
, tObj
->_MaxLevel
);
2995 * Sample a shadow/depth texture. This function is incomplete. It doesn't
2996 * check for minification vs. magnification, etc.
2999 sample_depth_texture( struct gl_context
*ctx
,
3000 const struct gl_texture_object
*tObj
, GLuint n
,
3001 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3002 GLfloat texel
[][4] )
3004 const GLint level
= choose_depth_texture_level(tObj
, lambda
[0]);
3005 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
3006 const GLint width
= img
->Width
;
3007 const GLint height
= img
->Height
;
3008 const GLint depth
= img
->Depth
;
3009 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
3015 ASSERT(img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
3016 img
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
3018 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
3019 tObj
->Target
== GL_TEXTURE_2D
||
3020 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
3021 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
3022 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
);
3024 ambient
= tObj
->CompareFailValue
;
3026 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
3028 function
= (tObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
3029 tObj
->CompareFunc
: GL_NONE
;
3031 if (tObj
->MagFilter
== GL_NEAREST
) {
3033 for (i
= 0; i
< n
; i
++) {
3034 GLfloat depthSample
;
3035 GLint col
, row
, slice
;
3037 nearest_texcoord(tObj
, level
, texcoords
[i
], &col
, &row
, &slice
);
3039 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3040 slice
>= 0 && slice
< depth
) {
3041 img
->FetchTexelf(img
, col
, row
, slice
, &depthSample
);
3044 depthSample
= tObj
->BorderColor
.f
[0];
3047 result
= shadow_compare(function
, texcoords
[i
][compare_coord
],
3048 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
;
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 result
= shadow_compare4(function
, texcoords
[i
][compare_coord
],
3139 depth00
, depth01
, depth10
, depth11
,
3142 switch (tObj
->DepthMode
) {
3144 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3147 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3150 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3153 _mesa_problem(ctx
, "Bad depth texture mode");
3162 * We use this function when a texture object is in an "incomplete" state.
3163 * When a fragment program attempts to sample an incomplete texture we
3164 * return black (see issue 23 in GL_ARB_fragment_program spec).
3165 * Note: fragment programs don't observe the texture enable/disable flags.
3168 null_sample_func( struct gl_context
*ctx
,
3169 const struct gl_texture_object
*tObj
, GLuint n
,
3170 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3178 for (i
= 0; i
< n
; i
++) {
3182 rgba
[i
][ACOMP
] = 1.0;
3188 * Choose the texture sampling function for the given texture object.
3191 _swrast_choose_texture_sample_func( struct gl_context
*ctx
,
3192 const struct gl_texture_object
*t
)
3194 if (!t
|| !t
->_Complete
) {
3195 return &null_sample_func
;
3198 const GLboolean needLambda
= (GLboolean
) (t
->MinFilter
!= t
->MagFilter
);
3199 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->_BaseFormat
;
3201 switch (t
->Target
) {
3203 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3204 return &sample_depth_texture
;
3206 else if (needLambda
) {
3207 return &sample_lambda_1d
;
3209 else if (t
->MinFilter
== GL_LINEAR
) {
3210 return &sample_linear_1d
;
3213 ASSERT(t
->MinFilter
== GL_NEAREST
);
3214 return &sample_nearest_1d
;
3217 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3218 return &sample_depth_texture
;
3220 else if (needLambda
) {
3221 return &sample_lambda_2d
;
3223 else if (t
->MinFilter
== GL_LINEAR
) {
3224 return &sample_linear_2d
;
3227 /* check for a few optimized cases */
3228 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3229 ASSERT(t
->MinFilter
== GL_NEAREST
);
3230 if (t
->WrapS
== GL_REPEAT
&&
3231 t
->WrapT
== GL_REPEAT
&&
3232 img
->_IsPowerOfTwo
&&
3234 img
->TexFormat
== MESA_FORMAT_RGB888
) {
3235 return &opt_sample_rgb_2d
;
3237 else if (t
->WrapS
== GL_REPEAT
&&
3238 t
->WrapT
== GL_REPEAT
&&
3239 img
->_IsPowerOfTwo
&&
3241 img
->TexFormat
== MESA_FORMAT_RGBA8888
) {
3242 return &opt_sample_rgba_2d
;
3245 return &sample_nearest_2d
;
3250 return &sample_lambda_3d
;
3252 else if (t
->MinFilter
== GL_LINEAR
) {
3253 return &sample_linear_3d
;
3256 ASSERT(t
->MinFilter
== GL_NEAREST
);
3257 return &sample_nearest_3d
;
3259 case GL_TEXTURE_CUBE_MAP
:
3261 return &sample_lambda_cube
;
3263 else if (t
->MinFilter
== GL_LINEAR
) {
3264 return &sample_linear_cube
;
3267 ASSERT(t
->MinFilter
== GL_NEAREST
);
3268 return &sample_nearest_cube
;
3270 case GL_TEXTURE_RECTANGLE_NV
:
3271 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3272 return &sample_depth_texture
;
3274 else if (needLambda
) {
3275 return &sample_lambda_rect
;
3277 else if (t
->MinFilter
== GL_LINEAR
) {
3278 return &sample_linear_rect
;
3281 ASSERT(t
->MinFilter
== GL_NEAREST
);
3282 return &sample_nearest_rect
;
3284 case GL_TEXTURE_1D_ARRAY_EXT
:
3286 return &sample_lambda_1d_array
;
3288 else if (t
->MinFilter
== GL_LINEAR
) {
3289 return &sample_linear_1d_array
;
3292 ASSERT(t
->MinFilter
== GL_NEAREST
);
3293 return &sample_nearest_1d_array
;
3295 case GL_TEXTURE_2D_ARRAY_EXT
:
3297 return &sample_lambda_2d_array
;
3299 else if (t
->MinFilter
== GL_LINEAR
) {
3300 return &sample_linear_2d_array
;
3303 ASSERT(t
->MinFilter
== GL_NEAREST
);
3304 return &sample_nearest_2d_array
;
3308 "invalid target in _swrast_choose_texture_sample_func");
3309 return &null_sample_func
;