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]);
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 (depth00
<= coord
) luminance
-= d
;
2922 if (depth01
<= coord
) luminance
-= d
;
2923 if (depth10
<= coord
) luminance
-= d
;
2924 if (depth11
<= coord
) luminance
-= d
;
2927 if (depth00
>= coord
) luminance
-= d
;
2928 if (depth01
>= coord
) luminance
-= d
;
2929 if (depth10
>= coord
) luminance
-= d
;
2930 if (depth11
>= coord
) luminance
-= d
;
2933 if (depth00
< coord
) luminance
-= d
;
2934 if (depth01
< coord
) luminance
-= d
;
2935 if (depth10
< coord
) luminance
-= d
;
2936 if (depth11
< coord
) luminance
-= d
;
2939 if (depth00
> coord
) luminance
-= d
;
2940 if (depth01
> coord
) luminance
-= d
;
2941 if (depth10
> coord
) luminance
-= d
;
2942 if (depth11
> coord
) luminance
-= d
;
2945 if (depth00
== coord
) luminance
-= d
;
2946 if (depth01
== coord
) luminance
-= d
;
2947 if (depth10
== coord
) luminance
-= d
;
2948 if (depth11
== coord
) luminance
-= d
;
2951 if (depth00
!= coord
) luminance
-= d
;
2952 if (depth01
!= coord
) luminance
-= d
;
2953 if (depth10
!= coord
) luminance
-= d
;
2954 if (depth11
!= coord
) 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_depth_texture");
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
;
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 result
= shadow_compare(function
, texcoords
[i
][compare_coord
],
3047 depthSample
, ambient
);
3049 switch (tObj
->DepthMode
) {
3051 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3054 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3057 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3060 ASSIGN_4V(texel
[i
], result
, 0.0F
, 0.0F
, 1.0F
);
3063 _mesa_problem(ctx
, "Bad depth texture mode");
3069 ASSERT(tObj
->MagFilter
== GL_LINEAR
);
3070 for (i
= 0; i
< n
; i
++) {
3071 GLfloat depth00
, depth01
, depth10
, depth11
;
3072 GLint i0
, i1
, j0
, j1
;
3075 GLuint useBorderTexel
;
3077 linear_texcoord(tObj
, level
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3084 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3090 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3091 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3092 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3093 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3096 if (slice
< 0 || slice
>= (GLint
) depth
) {
3097 depth00
= tObj
->BorderColor
.f
[0];
3098 depth01
= tObj
->BorderColor
.f
[0];
3099 depth10
= tObj
->BorderColor
.f
[0];
3100 depth11
= tObj
->BorderColor
.f
[0];
3103 /* get four depth samples from the texture */
3104 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3105 depth00
= tObj
->BorderColor
.f
[0];
3108 img
->FetchTexelf(img
, i0
, j0
, slice
, &depth00
);
3110 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3111 depth10
= tObj
->BorderColor
.f
[0];
3114 img
->FetchTexelf(img
, i1
, j0
, slice
, &depth10
);
3117 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3118 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3119 depth01
= tObj
->BorderColor
.f
[0];
3122 img
->FetchTexelf(img
, i0
, j1
, slice
, &depth01
);
3124 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3125 depth11
= tObj
->BorderColor
.f
[0];
3128 img
->FetchTexelf(img
, i1
, j1
, slice
, &depth11
);
3137 result
= shadow_compare4(function
, texcoords
[i
][compare_coord
],
3138 depth00
, depth01
, depth10
, depth11
,
3141 switch (tObj
->DepthMode
) {
3143 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3146 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3149 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3152 _mesa_problem(ctx
, "Bad depth texture mode");
3161 * We use this function when a texture object is in an "incomplete" state.
3162 * When a fragment program attempts to sample an incomplete texture we
3163 * return black (see issue 23 in GL_ARB_fragment_program spec).
3164 * Note: fragment programs don't observe the texture enable/disable flags.
3167 null_sample_func( struct gl_context
*ctx
,
3168 const struct gl_texture_object
*tObj
, GLuint n
,
3169 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3177 for (i
= 0; i
< n
; i
++) {
3181 rgba
[i
][ACOMP
] = 1.0;
3187 * Choose the texture sampling function for the given texture object.
3190 _swrast_choose_texture_sample_func( struct gl_context
*ctx
,
3191 const struct gl_texture_object
*t
)
3193 if (!t
|| !t
->_Complete
) {
3194 return &null_sample_func
;
3197 const GLboolean needLambda
= (GLboolean
) (t
->MinFilter
!= t
->MagFilter
);
3198 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->_BaseFormat
;
3200 switch (t
->Target
) {
3202 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3203 return &sample_depth_texture
;
3205 else if (needLambda
) {
3206 return &sample_lambda_1d
;
3208 else if (t
->MinFilter
== GL_LINEAR
) {
3209 return &sample_linear_1d
;
3212 ASSERT(t
->MinFilter
== GL_NEAREST
);
3213 return &sample_nearest_1d
;
3216 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3217 return &sample_depth_texture
;
3219 else if (needLambda
) {
3220 return &sample_lambda_2d
;
3222 else if (t
->MinFilter
== GL_LINEAR
) {
3223 return &sample_linear_2d
;
3226 /* check for a few optimized cases */
3227 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3228 ASSERT(t
->MinFilter
== GL_NEAREST
);
3229 if (t
->WrapS
== GL_REPEAT
&&
3230 t
->WrapT
== GL_REPEAT
&&
3231 img
->_IsPowerOfTwo
&&
3233 img
->TexFormat
== MESA_FORMAT_RGB888
) {
3234 return &opt_sample_rgb_2d
;
3236 else if (t
->WrapS
== GL_REPEAT
&&
3237 t
->WrapT
== GL_REPEAT
&&
3238 img
->_IsPowerOfTwo
&&
3240 img
->TexFormat
== MESA_FORMAT_RGBA8888
) {
3241 return &opt_sample_rgba_2d
;
3244 return &sample_nearest_2d
;
3249 return &sample_lambda_3d
;
3251 else if (t
->MinFilter
== GL_LINEAR
) {
3252 return &sample_linear_3d
;
3255 ASSERT(t
->MinFilter
== GL_NEAREST
);
3256 return &sample_nearest_3d
;
3258 case GL_TEXTURE_CUBE_MAP
:
3260 return &sample_lambda_cube
;
3262 else if (t
->MinFilter
== GL_LINEAR
) {
3263 return &sample_linear_cube
;
3266 ASSERT(t
->MinFilter
== GL_NEAREST
);
3267 return &sample_nearest_cube
;
3269 case GL_TEXTURE_RECTANGLE_NV
:
3270 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3271 return &sample_depth_texture
;
3273 else if (needLambda
) {
3274 return &sample_lambda_rect
;
3276 else if (t
->MinFilter
== GL_LINEAR
) {
3277 return &sample_linear_rect
;
3280 ASSERT(t
->MinFilter
== GL_NEAREST
);
3281 return &sample_nearest_rect
;
3283 case GL_TEXTURE_1D_ARRAY_EXT
:
3285 return &sample_lambda_1d_array
;
3287 else if (t
->MinFilter
== GL_LINEAR
) {
3288 return &sample_linear_1d_array
;
3291 ASSERT(t
->MinFilter
== GL_NEAREST
);
3292 return &sample_nearest_1d_array
;
3294 case GL_TEXTURE_2D_ARRAY_EXT
:
3296 return &sample_lambda_2d_array
;
3298 else if (t
->MinFilter
== GL_LINEAR
) {
3299 return &sample_linear_2d_array
;
3302 ASSERT(t
->MinFilter
== GL_NEAREST
);
3303 return &sample_nearest_2d_array
;
3307 "invalid target in _swrast_choose_texture_sample_func");
3308 return &null_sample_func
;