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 * If A is a signed integer, A % B doesn't give the right value for A < 0
139 * (in terms of texture repeat). Just casting to unsigned fixes that.
141 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
145 * Used to compute texel locations for linear sampling.
147 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
148 * s = texcoord in [0,1]
149 * size = width (or height or depth) of texture
151 * i0, i1 = returns two nearest texel indexes
152 * weight = returns blend factor between texels
155 linear_texel_locations(GLenum wrapMode
,
156 const struct gl_texture_image
*img
,
157 GLint size
, GLfloat s
,
158 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
164 if (img
->_IsPowerOfTwo
) {
165 *i0
= IFLOOR(u
) & (size
- 1);
166 *i1
= (*i0
+ 1) & (size
- 1);
169 *i0
= REMAINDER(IFLOOR(u
), size
);
170 *i1
= REMAINDER(*i0
+ 1, size
);
173 case GL_CLAMP_TO_EDGE
:
185 if (*i1
>= (GLint
) size
)
188 case GL_CLAMP_TO_BORDER
:
190 const GLfloat min
= -1.0F
/ (2.0F
* size
);
191 const GLfloat max
= 1.0F
- min
;
203 case GL_MIRRORED_REPEAT
:
205 const GLint flr
= IFLOOR(s
);
207 u
= 1.0F
- (s
- (GLfloat
) flr
);
209 u
= s
- (GLfloat
) flr
;
210 u
= (u
* size
) - 0.5F
;
215 if (*i1
>= (GLint
) size
)
219 case GL_MIRROR_CLAMP_EXT
:
229 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
240 if (*i1
>= (GLint
) size
)
243 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
245 const GLfloat min
= -1.0F
/ (2.0F
* size
);
246 const GLfloat max
= 1.0F
- min
;
271 _mesa_problem(NULL
, "Bad wrap mode");
279 * Used to compute texel location for nearest sampling.
282 nearest_texel_location(GLenum wrapMode
,
283 const struct gl_texture_image
*img
,
284 GLint size
, GLfloat s
)
290 /* s limited to [0,1) */
291 /* i limited to [0,size-1] */
292 i
= IFLOOR(s
* size
);
293 if (img
->_IsPowerOfTwo
)
296 i
= REMAINDER(i
, size
);
298 case GL_CLAMP_TO_EDGE
:
300 /* s limited to [min,max] */
301 /* i limited to [0, size-1] */
302 const GLfloat min
= 1.0F
/ (2.0F
* size
);
303 const GLfloat max
= 1.0F
- min
;
309 i
= IFLOOR(s
* size
);
312 case GL_CLAMP_TO_BORDER
:
314 /* s limited to [min,max] */
315 /* i limited to [-1, size] */
316 const GLfloat min
= -1.0F
/ (2.0F
* size
);
317 const GLfloat max
= 1.0F
- min
;
323 i
= IFLOOR(s
* size
);
326 case GL_MIRRORED_REPEAT
:
328 const GLfloat min
= 1.0F
/ (2.0F
* size
);
329 const GLfloat max
= 1.0F
- min
;
330 const GLint flr
= IFLOOR(s
);
333 u
= 1.0F
- (s
- (GLfloat
) flr
);
335 u
= s
- (GLfloat
) flr
;
341 i
= IFLOOR(u
* size
);
344 case GL_MIRROR_CLAMP_EXT
:
346 /* s limited to [0,1] */
347 /* i limited to [0,size-1] */
348 const GLfloat u
= FABSF(s
);
354 i
= IFLOOR(u
* size
);
357 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
359 /* s limited to [min,max] */
360 /* i limited to [0, size-1] */
361 const GLfloat min
= 1.0F
/ (2.0F
* size
);
362 const GLfloat max
= 1.0F
- min
;
363 const GLfloat u
= FABSF(s
);
369 i
= IFLOOR(u
* size
);
372 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
374 /* s limited to [min,max] */
375 /* i limited to [0, size-1] */
376 const GLfloat min
= -1.0F
/ (2.0F
* size
);
377 const GLfloat max
= 1.0F
- min
;
378 const GLfloat u
= FABSF(s
);
384 i
= IFLOOR(u
* size
);
388 /* s limited to [0,1] */
389 /* i limited to [0,size-1] */
395 i
= IFLOOR(s
* size
);
398 _mesa_problem(NULL
, "Bad wrap mode");
404 /* Power of two image sizes only */
406 linear_repeat_texel_location(GLuint size
, GLfloat s
,
407 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
409 GLfloat u
= s
* size
- 0.5F
;
410 *i0
= IFLOOR(u
) & (size
- 1);
411 *i1
= (*i0
+ 1) & (size
- 1);
417 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
420 clamp_rect_coord_nearest(GLenum wrapMode
, GLfloat coord
, GLint max
)
424 return IFLOOR( CLAMP(coord
, 0.0F
, max
- 1) );
425 case GL_CLAMP_TO_EDGE
:
426 return IFLOOR( CLAMP(coord
, 0.5F
, max
- 0.5F
) );
427 case GL_CLAMP_TO_BORDER
:
428 return IFLOOR( CLAMP(coord
, -0.5F
, max
+ 0.5F
) );
430 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_nearest");
437 * As above, but GL_LINEAR filtering.
440 clamp_rect_coord_linear(GLenum wrapMode
, GLfloat coord
, GLint max
,
441 GLint
*i0out
, GLint
*i1out
, GLfloat
*weight
)
447 /* Not exactly what the spec says, but it matches NVIDIA output */
448 fcol
= CLAMP(coord
- 0.5F
, 0.0, max
-1);
452 case GL_CLAMP_TO_EDGE
:
453 fcol
= CLAMP(coord
, 0.5F
, max
- 0.5F
);
460 case GL_CLAMP_TO_BORDER
:
461 fcol
= CLAMP(coord
, -0.5F
, max
+ 0.5F
);
467 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_linear");
473 *weight
= FRAC(fcol
);
478 * Compute nearest integer texcoords for given texobj and coordinate.
481 nearest_texcoord(const struct gl_texture_object
*texObj
,
482 const GLfloat texcoord
[4],
483 GLint
*i
, GLint
*j
, GLint
*k
)
485 const GLint baseLevel
= texObj
->BaseLevel
;
486 const struct gl_texture_image
*img
= texObj
->Image
[0][baseLevel
];
487 const GLint width
= img
->Width
;
488 const GLint height
= img
->Height
;
489 const GLint depth
= img
->Depth
;
491 switch (texObj
->Target
) {
492 case GL_TEXTURE_RECTANGLE_ARB
:
493 *i
= clamp_rect_coord_nearest(texObj
->WrapS
, texcoord
[0], width
);
494 *j
= clamp_rect_coord_nearest(texObj
->WrapT
, texcoord
[1], height
);
498 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
503 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
504 *j
= nearest_texel_location(texObj
->WrapT
, img
, height
, texcoord
[1]);
507 case GL_TEXTURE_1D_ARRAY_EXT
:
508 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
509 *j
= clamp_rect_coord_nearest(texObj
->WrapT
, texcoord
[1], height
);
512 case GL_TEXTURE_2D_ARRAY_EXT
:
513 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
514 *j
= nearest_texel_location(texObj
->WrapT
, img
, height
, texcoord
[1]);
515 *k
= clamp_rect_coord_nearest(texObj
->WrapR
, texcoord
[2], depth
);
524 * Compute linear integer texcoords for given texobj and coordinate.
527 linear_texcoord(const struct gl_texture_object
*texObj
,
528 const GLfloat texcoord
[4],
529 GLint
*i0
, GLint
*i1
, GLint
*j0
, GLint
*j1
, GLint
*slice
,
530 GLfloat
*wi
, GLfloat
*wj
)
532 const GLint baseLevel
= texObj
->BaseLevel
;
533 const struct gl_texture_image
*img
= texObj
->Image
[0][baseLevel
];
534 const GLint width
= img
->Width
;
535 const GLint height
= img
->Height
;
536 const GLint depth
= img
->Depth
;
538 switch (texObj
->Target
) {
539 case GL_TEXTURE_RECTANGLE_ARB
:
540 clamp_rect_coord_linear(texObj
->WrapS
, texcoord
[0],
542 clamp_rect_coord_linear(texObj
->WrapT
, texcoord
[1],
549 linear_texel_locations(texObj
->WrapS
, img
, width
,
550 texcoord
[0], i0
, i1
, wi
);
551 linear_texel_locations(texObj
->WrapT
, img
, height
,
552 texcoord
[1], j0
, j1
, wj
);
556 case GL_TEXTURE_1D_ARRAY_EXT
:
557 linear_texel_locations(texObj
->WrapS
, img
, width
,
558 texcoord
[0], i0
, i1
, wi
);
559 *j0
= clamp_rect_coord_nearest(texObj
->WrapT
, texcoord
[1], height
);
564 case GL_TEXTURE_2D_ARRAY_EXT
:
565 linear_texel_locations(texObj
->WrapS
, img
, width
,
566 texcoord
[0], i0
, i1
, wi
);
567 linear_texel_locations(texObj
->WrapT
, img
, height
,
568 texcoord
[1], j0
, j1
, wj
);
569 *slice
= clamp_rect_coord_nearest(texObj
->WrapR
, texcoord
[2], depth
);
580 * For linear interpolation between mipmap levels N and N+1, this function
584 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
587 return tObj
->BaseLevel
;
588 else if (lambda
> tObj
->_MaxLambda
)
589 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
591 return (GLint
) (tObj
->BaseLevel
+ lambda
);
596 * Compute the nearest mipmap level to take texels from.
599 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
605 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
606 l
= tObj
->_MaxLambda
+ 0.4999F
;
609 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
610 if (level
> tObj
->_MaxLevel
)
611 level
= tObj
->_MaxLevel
;
618 * Bitflags for texture border color sampling.
630 * The lambda[] array values are always monotonic. Either the whole span
631 * will be minified, magnified, or split between the two. This function
632 * determines the subranges in [0, n-1] that are to be minified or magnified.
635 compute_min_mag_ranges(const struct gl_texture_object
*tObj
,
636 GLuint n
, const GLfloat lambda
[],
637 GLuint
*minStart
, GLuint
*minEnd
,
638 GLuint
*magStart
, GLuint
*magEnd
)
640 GLfloat minMagThresh
;
642 /* we shouldn't be here if minfilter == magfilter */
643 ASSERT(tObj
->MinFilter
!= tObj
->MagFilter
);
645 /* This bit comes from the OpenGL spec: */
646 if (tObj
->MagFilter
== GL_LINEAR
647 && (tObj
->MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
648 tObj
->MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
656 /* DEBUG CODE: Verify that lambda[] is monotonic.
657 * We can't really use this because the inaccuracy in the LOG2 function
658 * causes this test to fail, yet the resulting texturing is correct.
662 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
663 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
664 for (i
= 0; i
< n
- 1; i
++) {
665 ASSERT((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
668 else { /* increasing */
669 for (i
= 0; i
< n
- 1; i
++) {
670 ASSERT((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
676 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
677 /* magnification for whole span */
680 *minStart
= *minEnd
= 0;
682 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
683 /* minification for whole span */
686 *magStart
= *magEnd
= 0;
689 /* a mix of minification and magnification */
691 if (lambda
[0] > minMagThresh
) {
692 /* start with minification */
693 for (i
= 1; i
< n
; i
++) {
694 if (lambda
[i
] <= minMagThresh
)
703 /* start with magnification */
704 for (i
= 1; i
< n
; i
++) {
705 if (lambda
[i
] > minMagThresh
)
716 /* Verify the min/mag Start/End values
717 * We don't use this either (see above)
721 for (i
= 0; i
< n
; i
++) {
722 if (lambda
[i
] > minMagThresh
) {
724 ASSERT(i
>= *minStart
);
729 ASSERT(i
>= *magStart
);
739 * When we sample the border color, it must be interpreted according to
740 * the base texture format. Ex: if the texture base format it GL_ALPHA,
741 * we return (0,0,0,BorderAlpha).
744 get_border_color(const struct gl_texture_object
*tObj
,
745 const struct gl_texture_image
*img
,
748 switch (img
->TexFormat
->BaseFormat
) {
750 rgba
[0] = tObj
->BorderColor
[0];
751 rgba
[1] = tObj
->BorderColor
[1];
752 rgba
[2] = tObj
->BorderColor
[2];
756 rgba
[0] = rgba
[1] = rgba
[2] = 0.0;
757 rgba
[3] = tObj
->BorderColor
[3];
760 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->BorderColor
[0];
763 case GL_LUMINANCE_ALPHA
:
764 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->BorderColor
[0];
765 rgba
[3] = tObj
->BorderColor
[3];
768 rgba
[0] = rgba
[1] = rgba
[2] = rgba
[3] = tObj
->BorderColor
[0];
771 COPY_4V(rgba
, tObj
->BorderColor
);
776 /**********************************************************************/
777 /* 1-D Texture Sampling Functions */
778 /**********************************************************************/
781 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
784 sample_1d_nearest(GLcontext
*ctx
,
785 const struct gl_texture_object
*tObj
,
786 const struct gl_texture_image
*img
,
787 const GLfloat texcoord
[4], GLfloat rgba
[4])
789 const GLint width
= img
->Width2
; /* without border, power of two */
791 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
792 /* skip over the border, if any */
794 if (i
< 0 || i
>= (GLint
) img
->Width
) {
795 /* Need this test for GL_CLAMP_TO_BORDER mode */
796 get_border_color(tObj
, img
, rgba
);
799 img
->FetchTexelf(img
, i
, 0, 0, rgba
);
805 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
808 sample_1d_linear(GLcontext
*ctx
,
809 const struct gl_texture_object
*tObj
,
810 const struct gl_texture_image
*img
,
811 const GLfloat texcoord
[4], GLfloat rgba
[4])
813 const GLint width
= img
->Width2
;
815 GLbitfield useBorderColor
= 0x0;
817 GLfloat t0
[4], t1
[4]; /* texels */
819 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
826 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
827 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
830 /* fetch texel colors */
831 if (useBorderColor
& I0BIT
) {
832 get_border_color(tObj
, img
, t0
);
835 img
->FetchTexelf(img
, i0
, 0, 0, t0
);
837 if (useBorderColor
& I1BIT
) {
838 get_border_color(tObj
, img
, t1
);
841 img
->FetchTexelf(img
, i1
, 0, 0, t1
);
844 lerp_rgba(rgba
, a
, t0
, t1
);
849 sample_1d_nearest_mipmap_nearest(GLcontext
*ctx
,
850 const struct gl_texture_object
*tObj
,
851 GLuint n
, const GLfloat texcoord
[][4],
852 const GLfloat lambda
[], GLfloat rgba
[][4])
855 ASSERT(lambda
!= NULL
);
856 for (i
= 0; i
< n
; i
++) {
857 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
858 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
864 sample_1d_linear_mipmap_nearest(GLcontext
*ctx
,
865 const struct gl_texture_object
*tObj
,
866 GLuint n
, const GLfloat texcoord
[][4],
867 const GLfloat lambda
[], GLfloat rgba
[][4])
870 ASSERT(lambda
!= NULL
);
871 for (i
= 0; i
< n
; i
++) {
872 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
873 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
879 sample_1d_nearest_mipmap_linear(GLcontext
*ctx
,
880 const struct gl_texture_object
*tObj
,
881 GLuint n
, const GLfloat texcoord
[][4],
882 const GLfloat lambda
[], GLfloat rgba
[][4])
885 ASSERT(lambda
!= NULL
);
886 for (i
= 0; i
< n
; i
++) {
887 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
888 if (level
>= tObj
->_MaxLevel
) {
889 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
890 texcoord
[i
], rgba
[i
]);
893 GLfloat t0
[4], t1
[4];
894 const GLfloat f
= FRAC(lambda
[i
]);
895 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
896 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
897 lerp_rgba(rgba
[i
], f
, t0
, t1
);
904 sample_1d_linear_mipmap_linear(GLcontext
*ctx
,
905 const struct gl_texture_object
*tObj
,
906 GLuint n
, const GLfloat texcoord
[][4],
907 const GLfloat lambda
[], GLfloat rgba
[][4])
910 ASSERT(lambda
!= NULL
);
911 for (i
= 0; i
< n
; i
++) {
912 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
913 if (level
>= tObj
->_MaxLevel
) {
914 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
915 texcoord
[i
], rgba
[i
]);
918 GLfloat t0
[4], t1
[4];
919 const GLfloat f
= FRAC(lambda
[i
]);
920 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
921 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
922 lerp_rgba(rgba
[i
], f
, t0
, t1
);
928 /** Sample 1D texture, nearest filtering for both min/magnification */
930 sample_nearest_1d( GLcontext
*ctx
,
931 const struct gl_texture_object
*tObj
, GLuint n
,
932 const GLfloat texcoords
[][4], const GLfloat lambda
[],
936 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
938 for (i
= 0; i
< n
; i
++) {
939 sample_1d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
944 /** Sample 1D texture, linear filtering for both min/magnification */
946 sample_linear_1d( GLcontext
*ctx
,
947 const struct gl_texture_object
*tObj
, GLuint n
,
948 const GLfloat texcoords
[][4], const GLfloat lambda
[],
952 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
954 for (i
= 0; i
< n
; i
++) {
955 sample_1d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
960 /** Sample 1D texture, using lambda to choose between min/magnification */
962 sample_lambda_1d( GLcontext
*ctx
,
963 const struct gl_texture_object
*tObj
, GLuint n
,
964 const GLfloat texcoords
[][4],
965 const GLfloat lambda
[], GLfloat rgba
[][4] )
967 GLuint minStart
, minEnd
; /* texels with minification */
968 GLuint magStart
, magEnd
; /* texels with magnification */
971 ASSERT(lambda
!= NULL
);
972 compute_min_mag_ranges(tObj
, n
, lambda
,
973 &minStart
, &minEnd
, &magStart
, &magEnd
);
975 if (minStart
< minEnd
) {
976 /* do the minified texels */
977 const GLuint m
= minEnd
- minStart
;
978 switch (tObj
->MinFilter
) {
980 for (i
= minStart
; i
< minEnd
; i
++)
981 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
982 texcoords
[i
], rgba
[i
]);
985 for (i
= minStart
; i
< minEnd
; i
++)
986 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
987 texcoords
[i
], rgba
[i
]);
989 case GL_NEAREST_MIPMAP_NEAREST
:
990 sample_1d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
991 lambda
+ minStart
, rgba
+ minStart
);
993 case GL_LINEAR_MIPMAP_NEAREST
:
994 sample_1d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
995 lambda
+ minStart
, rgba
+ minStart
);
997 case GL_NEAREST_MIPMAP_LINEAR
:
998 sample_1d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
999 lambda
+ minStart
, rgba
+ minStart
);
1001 case GL_LINEAR_MIPMAP_LINEAR
:
1002 sample_1d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1003 lambda
+ minStart
, rgba
+ minStart
);
1006 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
1011 if (magStart
< magEnd
) {
1012 /* do the magnified texels */
1013 switch (tObj
->MagFilter
) {
1015 for (i
= magStart
; i
< magEnd
; i
++)
1016 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1017 texcoords
[i
], rgba
[i
]);
1020 for (i
= magStart
; i
< magEnd
; i
++)
1021 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1022 texcoords
[i
], rgba
[i
]);
1025 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
1032 /**********************************************************************/
1033 /* 2-D Texture Sampling Functions */
1034 /**********************************************************************/
1038 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1041 sample_2d_nearest(GLcontext
*ctx
,
1042 const struct gl_texture_object
*tObj
,
1043 const struct gl_texture_image
*img
,
1044 const GLfloat texcoord
[4],
1047 const GLint width
= img
->Width2
; /* without border, power of two */
1048 const GLint height
= img
->Height2
; /* without border, power of two */
1052 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
1053 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
1055 /* skip over the border, if any */
1059 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
1060 /* Need this test for GL_CLAMP_TO_BORDER mode */
1061 get_border_color(tObj
, img
, rgba
);
1064 img
->FetchTexelf(img
, i
, j
, 0, rgba
);
1070 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1071 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1074 sample_2d_linear(GLcontext
*ctx
,
1075 const struct gl_texture_object
*tObj
,
1076 const struct gl_texture_image
*img
,
1077 const GLfloat texcoord
[4],
1080 const GLint width
= img
->Width2
;
1081 const GLint height
= img
->Height2
;
1082 GLint i0
, j0
, i1
, j1
;
1083 GLbitfield useBorderColor
= 0x0;
1085 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1087 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1088 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1097 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1098 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1099 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1100 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1103 /* fetch four texel colors */
1104 if (useBorderColor
& (I0BIT
| J0BIT
)) {
1105 get_border_color(tObj
, img
, t00
);
1108 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
1110 if (useBorderColor
& (I1BIT
| J0BIT
)) {
1111 get_border_color(tObj
, img
, t10
);
1114 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
1116 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1117 get_border_color(tObj
, img
, t01
);
1120 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
1122 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1123 get_border_color(tObj
, img
, t11
);
1126 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
1129 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1134 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1135 * We don't have to worry about the texture border.
1138 sample_2d_linear_repeat(GLcontext
*ctx
,
1139 const struct gl_texture_object
*tObj
,
1140 const struct gl_texture_image
*img
,
1141 const GLfloat texcoord
[4],
1144 const GLint width
= img
->Width2
;
1145 const GLint height
= img
->Height2
;
1146 GLint i0
, j0
, i1
, j1
;
1148 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1152 ASSERT(tObj
->WrapS
== GL_REPEAT
);
1153 ASSERT(tObj
->WrapT
== GL_REPEAT
);
1154 ASSERT(img
->Border
== 0);
1155 ASSERT(img
->TexFormat
->BaseFormat
!= GL_COLOR_INDEX
);
1156 ASSERT(img
->_IsPowerOfTwo
);
1158 linear_repeat_texel_location(width
, texcoord
[0], &i0
, &i1
, &wi
);
1159 linear_repeat_texel_location(height
, texcoord
[1], &j0
, &j1
, &wj
);
1161 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
1162 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
1163 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
1164 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
1166 lerp_rgba_2d(rgba
, wi
, wj
, t00
, t10
, t01
, t11
);
1171 sample_2d_nearest_mipmap_nearest(GLcontext
*ctx
,
1172 const struct gl_texture_object
*tObj
,
1173 GLuint n
, const GLfloat texcoord
[][4],
1174 const GLfloat lambda
[], GLfloat rgba
[][4])
1177 for (i
= 0; i
< n
; i
++) {
1178 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1179 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1185 sample_2d_linear_mipmap_nearest(GLcontext
*ctx
,
1186 const struct gl_texture_object
*tObj
,
1187 GLuint n
, const GLfloat texcoord
[][4],
1188 const GLfloat lambda
[], GLfloat rgba
[][4])
1191 ASSERT(lambda
!= NULL
);
1192 for (i
= 0; i
< n
; i
++) {
1193 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1194 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1200 sample_2d_nearest_mipmap_linear(GLcontext
*ctx
,
1201 const struct gl_texture_object
*tObj
,
1202 GLuint n
, const GLfloat texcoord
[][4],
1203 const GLfloat lambda
[], GLfloat rgba
[][4])
1206 ASSERT(lambda
!= NULL
);
1207 for (i
= 0; i
< n
; i
++) {
1208 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1209 if (level
>= tObj
->_MaxLevel
) {
1210 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1211 texcoord
[i
], rgba
[i
]);
1214 GLfloat t0
[4], t1
[4]; /* texels */
1215 const GLfloat f
= FRAC(lambda
[i
]);
1216 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1217 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1218 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1225 sample_2d_linear_mipmap_linear( GLcontext
*ctx
,
1226 const struct gl_texture_object
*tObj
,
1227 GLuint n
, const GLfloat texcoord
[][4],
1228 const GLfloat lambda
[], GLfloat rgba
[][4] )
1231 ASSERT(lambda
!= NULL
);
1232 for (i
= 0; i
< n
; i
++) {
1233 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1234 if (level
>= tObj
->_MaxLevel
) {
1235 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1236 texcoord
[i
], rgba
[i
]);
1239 GLfloat t0
[4], t1
[4]; /* texels */
1240 const GLfloat f
= FRAC(lambda
[i
]);
1241 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1242 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1243 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1250 sample_2d_linear_mipmap_linear_repeat(GLcontext
*ctx
,
1251 const struct gl_texture_object
*tObj
,
1252 GLuint n
, const GLfloat texcoord
[][4],
1253 const GLfloat lambda
[], GLfloat rgba
[][4])
1256 ASSERT(lambda
!= NULL
);
1257 ASSERT(tObj
->WrapS
== GL_REPEAT
);
1258 ASSERT(tObj
->WrapT
== GL_REPEAT
);
1259 for (i
= 0; i
< n
; i
++) {
1260 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1261 if (level
>= tObj
->_MaxLevel
) {
1262 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1263 texcoord
[i
], rgba
[i
]);
1266 GLfloat t0
[4], t1
[4]; /* texels */
1267 const GLfloat f
= FRAC(lambda
[i
]);
1268 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
],
1270 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
+1],
1272 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1278 /** Sample 2D texture, nearest filtering for both min/magnification */
1280 sample_nearest_2d(GLcontext
*ctx
,
1281 const struct gl_texture_object
*tObj
, GLuint n
,
1282 const GLfloat texcoords
[][4],
1283 const GLfloat lambda
[], GLfloat rgba
[][4])
1286 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1288 for (i
= 0; i
< n
; i
++) {
1289 sample_2d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1294 /** Sample 2D texture, linear filtering for both min/magnification */
1296 sample_linear_2d(GLcontext
*ctx
,
1297 const struct gl_texture_object
*tObj
, GLuint n
,
1298 const GLfloat texcoords
[][4],
1299 const GLfloat lambda
[], GLfloat rgba
[][4])
1302 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1304 if (tObj
->WrapS
== GL_REPEAT
&&
1305 tObj
->WrapT
== GL_REPEAT
&&
1306 image
->_IsPowerOfTwo
&&
1307 image
->Border
== 0) {
1308 for (i
= 0; i
< n
; i
++) {
1309 sample_2d_linear_repeat(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1313 for (i
= 0; i
< n
; i
++) {
1314 sample_2d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1321 * Optimized 2-D texture sampling:
1322 * S and T wrap mode == GL_REPEAT
1323 * GL_NEAREST min/mag filter
1325 * RowStride == Width,
1329 opt_sample_rgb_2d(GLcontext
*ctx
,
1330 const struct gl_texture_object
*tObj
,
1331 GLuint n
, const GLfloat texcoords
[][4],
1332 const GLfloat lambda
[], GLfloat rgba
[][4])
1334 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1335 const GLfloat width
= (GLfloat
) img
->Width
;
1336 const GLfloat height
= (GLfloat
) img
->Height
;
1337 const GLint colMask
= img
->Width
- 1;
1338 const GLint rowMask
= img
->Height
- 1;
1339 const GLint shift
= img
->WidthLog2
;
1343 ASSERT(tObj
->WrapS
==GL_REPEAT
);
1344 ASSERT(tObj
->WrapT
==GL_REPEAT
);
1345 ASSERT(img
->Border
==0);
1346 ASSERT(img
->TexFormat
->MesaFormat
==MESA_FORMAT_RGB
);
1347 ASSERT(img
->_IsPowerOfTwo
);
1349 for (k
=0; k
<n
; k
++) {
1350 GLint i
= IFLOOR(texcoords
[k
][0] * width
) & colMask
;
1351 GLint j
= IFLOOR(texcoords
[k
][1] * height
) & rowMask
;
1352 GLint pos
= (j
<< shift
) | i
;
1353 GLchan
*texel
= ((GLchan
*) img
->Data
) + 3*pos
;
1354 rgba
[k
][RCOMP
] = CHAN_TO_FLOAT(texel
[0]);
1355 rgba
[k
][GCOMP
] = CHAN_TO_FLOAT(texel
[1]);
1356 rgba
[k
][BCOMP
] = CHAN_TO_FLOAT(texel
[2]);
1362 * Optimized 2-D texture sampling:
1363 * S and T wrap mode == GL_REPEAT
1364 * GL_NEAREST min/mag filter
1366 * RowStride == Width,
1370 opt_sample_rgba_2d(GLcontext
*ctx
,
1371 const struct gl_texture_object
*tObj
,
1372 GLuint n
, const GLfloat texcoords
[][4],
1373 const GLfloat lambda
[], GLfloat rgba
[][4])
1375 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1376 const GLfloat width
= (GLfloat
) img
->Width
;
1377 const GLfloat height
= (GLfloat
) img
->Height
;
1378 const GLint colMask
= img
->Width
- 1;
1379 const GLint rowMask
= img
->Height
- 1;
1380 const GLint shift
= img
->WidthLog2
;
1384 ASSERT(tObj
->WrapS
==GL_REPEAT
);
1385 ASSERT(tObj
->WrapT
==GL_REPEAT
);
1386 ASSERT(img
->Border
==0);
1387 ASSERT(img
->TexFormat
->MesaFormat
==MESA_FORMAT_RGBA
);
1388 ASSERT(img
->_IsPowerOfTwo
);
1390 for (i
= 0; i
< n
; i
++) {
1391 const GLint col
= IFLOOR(texcoords
[i
][0] * width
) & colMask
;
1392 const GLint row
= IFLOOR(texcoords
[i
][1] * height
) & rowMask
;
1393 const GLint pos
= (row
<< shift
) | col
;
1394 const GLchan
*texel
= ((GLchan
*) img
->Data
) + (pos
<< 2); /* pos*4 */
1395 rgba
[i
][RCOMP
] = CHAN_TO_FLOAT(texel
[0]);
1396 rgba
[i
][GCOMP
] = CHAN_TO_FLOAT(texel
[1]);
1397 rgba
[i
][BCOMP
] = CHAN_TO_FLOAT(texel
[2]);
1398 rgba
[i
][ACOMP
] = CHAN_TO_FLOAT(texel
[3]);
1403 /** Sample 2D texture, using lambda to choose between min/magnification */
1405 sample_lambda_2d(GLcontext
*ctx
,
1406 const struct gl_texture_object
*tObj
,
1407 GLuint n
, const GLfloat texcoords
[][4],
1408 const GLfloat lambda
[], GLfloat rgba
[][4])
1410 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1411 GLuint minStart
, minEnd
; /* texels with minification */
1412 GLuint magStart
, magEnd
; /* texels with magnification */
1414 const GLboolean repeatNoBorderPOT
= (tObj
->WrapS
== GL_REPEAT
)
1415 && (tObj
->WrapT
== GL_REPEAT
)
1416 && (tImg
->Border
== 0 && (tImg
->Width
== tImg
->RowStride
))
1417 && (tImg
->TexFormat
->BaseFormat
!= GL_COLOR_INDEX
)
1418 && tImg
->_IsPowerOfTwo
;
1420 ASSERT(lambda
!= NULL
);
1421 compute_min_mag_ranges(tObj
, n
, lambda
,
1422 &minStart
, &minEnd
, &magStart
, &magEnd
);
1424 if (minStart
< minEnd
) {
1425 /* do the minified texels */
1426 const GLuint m
= minEnd
- minStart
;
1427 switch (tObj
->MinFilter
) {
1429 if (repeatNoBorderPOT
) {
1430 switch (tImg
->TexFormat
->MesaFormat
) {
1431 case MESA_FORMAT_RGB
:
1432 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1433 NULL
, rgba
+ minStart
);
1435 case MESA_FORMAT_RGBA
:
1436 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1437 NULL
, rgba
+ minStart
);
1440 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1441 NULL
, rgba
+ minStart
);
1445 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1446 NULL
, rgba
+ minStart
);
1450 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1451 NULL
, rgba
+ minStart
);
1453 case GL_NEAREST_MIPMAP_NEAREST
:
1454 sample_2d_nearest_mipmap_nearest(ctx
, tObj
, m
,
1455 texcoords
+ minStart
,
1456 lambda
+ minStart
, rgba
+ minStart
);
1458 case GL_LINEAR_MIPMAP_NEAREST
:
1459 sample_2d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1460 lambda
+ minStart
, rgba
+ minStart
);
1462 case GL_NEAREST_MIPMAP_LINEAR
:
1463 sample_2d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1464 lambda
+ minStart
, rgba
+ minStart
);
1466 case GL_LINEAR_MIPMAP_LINEAR
:
1467 if (repeatNoBorderPOT
)
1468 sample_2d_linear_mipmap_linear_repeat(ctx
, tObj
, m
,
1469 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1471 sample_2d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1472 lambda
+ minStart
, rgba
+ minStart
);
1475 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1480 if (magStart
< magEnd
) {
1481 /* do the magnified texels */
1482 const GLuint m
= magEnd
- magStart
;
1484 switch (tObj
->MagFilter
) {
1486 if (repeatNoBorderPOT
) {
1487 switch (tImg
->TexFormat
->MesaFormat
) {
1488 case MESA_FORMAT_RGB
:
1489 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1490 NULL
, rgba
+ magStart
);
1492 case MESA_FORMAT_RGBA
:
1493 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1494 NULL
, rgba
+ magStart
);
1497 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1498 NULL
, rgba
+ magStart
);
1502 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1503 NULL
, rgba
+ magStart
);
1507 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1508 NULL
, rgba
+ magStart
);
1511 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1518 /**********************************************************************/
1519 /* 3-D Texture Sampling Functions */
1520 /**********************************************************************/
1523 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1526 sample_3d_nearest(GLcontext
*ctx
,
1527 const struct gl_texture_object
*tObj
,
1528 const struct gl_texture_image
*img
,
1529 const GLfloat texcoord
[4],
1532 const GLint width
= img
->Width2
; /* without border, power of two */
1533 const GLint height
= img
->Height2
; /* without border, power of two */
1534 const GLint depth
= img
->Depth2
; /* without border, power of two */
1538 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
1539 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
1540 k
= nearest_texel_location(tObj
->WrapR
, img
, depth
, texcoord
[2]);
1542 if (i
< 0 || i
>= (GLint
) img
->Width
||
1543 j
< 0 || j
>= (GLint
) img
->Height
||
1544 k
< 0 || k
>= (GLint
) img
->Depth
) {
1545 /* Need this test for GL_CLAMP_TO_BORDER mode */
1546 get_border_color(tObj
, img
, rgba
);
1549 img
->FetchTexelf(img
, i
, j
, k
, rgba
);
1555 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1558 sample_3d_linear(GLcontext
*ctx
,
1559 const struct gl_texture_object
*tObj
,
1560 const struct gl_texture_image
*img
,
1561 const GLfloat texcoord
[4],
1564 const GLint width
= img
->Width2
;
1565 const GLint height
= img
->Height2
;
1566 const GLint depth
= img
->Depth2
;
1567 GLint i0
, j0
, k0
, i1
, j1
, k1
;
1568 GLbitfield useBorderColor
= 0x0;
1570 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
1571 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
1573 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1574 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1575 linear_texel_locations(tObj
->WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
1586 /* check if sampling texture border color */
1587 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1588 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1589 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1590 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1591 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
1592 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
1596 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
1597 get_border_color(tObj
, img
, t000
);
1600 img
->FetchTexelf(img
, i0
, j0
, k0
, t000
);
1602 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
1603 get_border_color(tObj
, img
, t100
);
1606 img
->FetchTexelf(img
, i1
, j0
, k0
, t100
);
1608 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
1609 get_border_color(tObj
, img
, t010
);
1612 img
->FetchTexelf(img
, i0
, j1
, k0
, t010
);
1614 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
1615 get_border_color(tObj
, img
, t110
);
1618 img
->FetchTexelf(img
, i1
, j1
, k0
, t110
);
1621 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
1622 get_border_color(tObj
, img
, t001
);
1625 img
->FetchTexelf(img
, i0
, j0
, k1
, t001
);
1627 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
1628 get_border_color(tObj
, img
, t101
);
1631 img
->FetchTexelf(img
, i1
, j0
, k1
, t101
);
1633 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
1634 get_border_color(tObj
, img
, t011
);
1637 img
->FetchTexelf(img
, i0
, j1
, k1
, t011
);
1639 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
1640 get_border_color(tObj
, img
, t111
);
1643 img
->FetchTexelf(img
, i1
, j1
, k1
, t111
);
1646 /* trilinear interpolation of samples */
1647 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
1652 sample_3d_nearest_mipmap_nearest(GLcontext
*ctx
,
1653 const struct gl_texture_object
*tObj
,
1654 GLuint n
, const GLfloat texcoord
[][4],
1655 const GLfloat lambda
[], GLfloat rgba
[][4] )
1658 for (i
= 0; i
< n
; i
++) {
1659 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1660 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1666 sample_3d_linear_mipmap_nearest(GLcontext
*ctx
,
1667 const struct gl_texture_object
*tObj
,
1668 GLuint n
, const GLfloat texcoord
[][4],
1669 const GLfloat lambda
[], GLfloat rgba
[][4])
1672 ASSERT(lambda
!= NULL
);
1673 for (i
= 0; i
< n
; i
++) {
1674 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1675 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1681 sample_3d_nearest_mipmap_linear(GLcontext
*ctx
,
1682 const struct gl_texture_object
*tObj
,
1683 GLuint n
, const GLfloat texcoord
[][4],
1684 const GLfloat lambda
[], GLfloat rgba
[][4])
1687 ASSERT(lambda
!= NULL
);
1688 for (i
= 0; i
< n
; i
++) {
1689 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1690 if (level
>= tObj
->_MaxLevel
) {
1691 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1692 texcoord
[i
], rgba
[i
]);
1695 GLfloat t0
[4], t1
[4]; /* texels */
1696 const GLfloat f
= FRAC(lambda
[i
]);
1697 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1698 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1699 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1706 sample_3d_linear_mipmap_linear(GLcontext
*ctx
,
1707 const struct gl_texture_object
*tObj
,
1708 GLuint n
, const GLfloat texcoord
[][4],
1709 const GLfloat lambda
[], GLfloat rgba
[][4])
1712 ASSERT(lambda
!= NULL
);
1713 for (i
= 0; i
< n
; i
++) {
1714 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1715 if (level
>= tObj
->_MaxLevel
) {
1716 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1717 texcoord
[i
], rgba
[i
]);
1720 GLfloat t0
[4], t1
[4]; /* texels */
1721 const GLfloat f
= FRAC(lambda
[i
]);
1722 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1723 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1724 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1730 /** Sample 3D texture, nearest filtering for both min/magnification */
1732 sample_nearest_3d(GLcontext
*ctx
,
1733 const struct gl_texture_object
*tObj
, GLuint n
,
1734 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1738 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1740 for (i
= 0; i
< n
; i
++) {
1741 sample_3d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1746 /** Sample 3D texture, linear filtering for both min/magnification */
1748 sample_linear_3d(GLcontext
*ctx
,
1749 const struct gl_texture_object
*tObj
, GLuint n
,
1750 const GLfloat texcoords
[][4],
1751 const GLfloat lambda
[], GLfloat rgba
[][4])
1754 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1756 for (i
= 0; i
< n
; i
++) {
1757 sample_3d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1762 /** Sample 3D texture, using lambda to choose between min/magnification */
1764 sample_lambda_3d(GLcontext
*ctx
,
1765 const struct gl_texture_object
*tObj
, GLuint n
,
1766 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1769 GLuint minStart
, minEnd
; /* texels with minification */
1770 GLuint magStart
, magEnd
; /* texels with magnification */
1773 ASSERT(lambda
!= NULL
);
1774 compute_min_mag_ranges(tObj
, n
, lambda
,
1775 &minStart
, &minEnd
, &magStart
, &magEnd
);
1777 if (minStart
< minEnd
) {
1778 /* do the minified texels */
1779 GLuint m
= minEnd
- minStart
;
1780 switch (tObj
->MinFilter
) {
1782 for (i
= minStart
; i
< minEnd
; i
++)
1783 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1784 texcoords
[i
], rgba
[i
]);
1787 for (i
= minStart
; i
< minEnd
; i
++)
1788 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1789 texcoords
[i
], rgba
[i
]);
1791 case GL_NEAREST_MIPMAP_NEAREST
:
1792 sample_3d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1793 lambda
+ minStart
, rgba
+ minStart
);
1795 case GL_LINEAR_MIPMAP_NEAREST
:
1796 sample_3d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1797 lambda
+ minStart
, rgba
+ minStart
);
1799 case GL_NEAREST_MIPMAP_LINEAR
:
1800 sample_3d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1801 lambda
+ minStart
, rgba
+ minStart
);
1803 case GL_LINEAR_MIPMAP_LINEAR
:
1804 sample_3d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1805 lambda
+ minStart
, rgba
+ minStart
);
1808 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
1813 if (magStart
< magEnd
) {
1814 /* do the magnified texels */
1815 switch (tObj
->MagFilter
) {
1817 for (i
= magStart
; i
< magEnd
; i
++)
1818 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1819 texcoords
[i
], rgba
[i
]);
1822 for (i
= magStart
; i
< magEnd
; i
++)
1823 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1824 texcoords
[i
], rgba
[i
]);
1827 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
1834 /**********************************************************************/
1835 /* Texture Cube Map Sampling Functions */
1836 /**********************************************************************/
1839 * Choose one of six sides of a texture cube map given the texture
1840 * coord (rx,ry,rz). Return pointer to corresponding array of texture
1843 static const struct gl_texture_image
**
1844 choose_cube_face(const struct gl_texture_object
*texObj
,
1845 const GLfloat texcoord
[4], GLfloat newCoord
[4])
1849 direction target sc tc ma
1850 ---------- ------------------------------- --- --- ---
1851 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1852 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1853 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1854 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1855 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1856 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1858 const GLfloat rx
= texcoord
[0];
1859 const GLfloat ry
= texcoord
[1];
1860 const GLfloat rz
= texcoord
[2];
1861 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
1865 if (arx
> ary
&& arx
> arz
) {
1879 else if (ary
> arx
&& ary
> arz
) {
1909 const float ima
= 1.0F
/ ma
;
1910 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
1911 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
1914 return (const struct gl_texture_image
**) texObj
->Image
[face
];
1919 sample_nearest_cube(GLcontext
*ctx
,
1920 const struct gl_texture_object
*tObj
, GLuint n
,
1921 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1926 for (i
= 0; i
< n
; i
++) {
1927 const struct gl_texture_image
**images
;
1928 GLfloat newCoord
[4];
1929 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1930 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
1937 sample_linear_cube(GLcontext
*ctx
,
1938 const struct gl_texture_object
*tObj
, GLuint n
,
1939 const GLfloat texcoords
[][4],
1940 const GLfloat lambda
[], GLfloat rgba
[][4])
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_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
1955 sample_cube_nearest_mipmap_nearest(GLcontext
*ctx
,
1956 const struct gl_texture_object
*tObj
,
1957 GLuint n
, const GLfloat texcoord
[][4],
1958 const GLfloat lambda
[], GLfloat rgba
[][4])
1961 ASSERT(lambda
!= NULL
);
1962 for (i
= 0; i
< n
; i
++) {
1963 const struct gl_texture_image
**images
;
1964 GLfloat newCoord
[4];
1966 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1968 /* XXX we actually need to recompute lambda here based on the newCoords.
1969 * But we would need the texcoords of adjacent fragments to compute that
1970 * properly, and we don't have those here.
1971 * For now, do an approximation: subtracting 1 from the chosen mipmap
1972 * level seems to work in some test cases.
1973 * The same adjustment is done in the next few functions.
1975 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1976 level
= MAX2(level
- 1, 0);
1978 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
1984 sample_cube_linear_mipmap_nearest(GLcontext
*ctx
,
1985 const struct gl_texture_object
*tObj
,
1986 GLuint n
, const GLfloat texcoord
[][4],
1987 const GLfloat lambda
[], GLfloat rgba
[][4])
1990 ASSERT(lambda
!= NULL
);
1991 for (i
= 0; i
< n
; i
++) {
1992 const struct gl_texture_image
**images
;
1993 GLfloat newCoord
[4];
1994 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1995 level
= MAX2(level
- 1, 0); /* see comment above */
1996 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1997 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2003 sample_cube_nearest_mipmap_linear(GLcontext
*ctx
,
2004 const struct gl_texture_object
*tObj
,
2005 GLuint n
, const GLfloat texcoord
[][4],
2006 const GLfloat lambda
[], GLfloat rgba
[][4])
2009 ASSERT(lambda
!= NULL
);
2010 for (i
= 0; i
< n
; i
++) {
2011 const struct gl_texture_image
**images
;
2012 GLfloat newCoord
[4];
2013 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2014 level
= MAX2(level
- 1, 0); /* see comment above */
2015 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2016 if (level
>= tObj
->_MaxLevel
) {
2017 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2021 GLfloat t0
[4], t1
[4]; /* texels */
2022 const GLfloat f
= FRAC(lambda
[i
]);
2023 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
2024 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2025 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2032 sample_cube_linear_mipmap_linear(GLcontext
*ctx
,
2033 const struct gl_texture_object
*tObj
,
2034 GLuint n
, const GLfloat texcoord
[][4],
2035 const GLfloat lambda
[], GLfloat rgba
[][4])
2038 ASSERT(lambda
!= NULL
);
2039 for (i
= 0; i
< n
; i
++) {
2040 const struct gl_texture_image
**images
;
2041 GLfloat newCoord
[4];
2042 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2043 level
= MAX2(level
- 1, 0); /* see comment above */
2044 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2045 if (level
>= tObj
->_MaxLevel
) {
2046 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2050 GLfloat t0
[4], t1
[4];
2051 const GLfloat f
= FRAC(lambda
[i
]);
2052 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
2053 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2054 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2060 /** Sample cube texture, using lambda to choose between min/magnification */
2062 sample_lambda_cube(GLcontext
*ctx
,
2063 const struct gl_texture_object
*tObj
, GLuint n
,
2064 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2067 GLuint minStart
, minEnd
; /* texels with minification */
2068 GLuint magStart
, magEnd
; /* texels with magnification */
2070 ASSERT(lambda
!= NULL
);
2071 compute_min_mag_ranges(tObj
, n
, lambda
,
2072 &minStart
, &minEnd
, &magStart
, &magEnd
);
2074 if (minStart
< minEnd
) {
2075 /* do the minified texels */
2076 const GLuint m
= minEnd
- minStart
;
2077 switch (tObj
->MinFilter
) {
2079 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2080 lambda
+ minStart
, rgba
+ minStart
);
2083 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2084 lambda
+ minStart
, rgba
+ minStart
);
2086 case GL_NEAREST_MIPMAP_NEAREST
:
2087 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
2088 texcoords
+ minStart
,
2089 lambda
+ minStart
, rgba
+ minStart
);
2091 case GL_LINEAR_MIPMAP_NEAREST
:
2092 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
2093 texcoords
+ minStart
,
2094 lambda
+ minStart
, rgba
+ minStart
);
2096 case GL_NEAREST_MIPMAP_LINEAR
:
2097 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
2098 texcoords
+ minStart
,
2099 lambda
+ minStart
, rgba
+ minStart
);
2101 case GL_LINEAR_MIPMAP_LINEAR
:
2102 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
2103 texcoords
+ minStart
,
2104 lambda
+ minStart
, rgba
+ minStart
);
2107 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2111 if (magStart
< magEnd
) {
2112 /* do the magnified texels */
2113 const GLuint m
= magEnd
- magStart
;
2114 switch (tObj
->MagFilter
) {
2116 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2117 lambda
+ magStart
, rgba
+ magStart
);
2120 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2121 lambda
+ magStart
, rgba
+ magStart
);
2124 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2130 /**********************************************************************/
2131 /* Texture Rectangle Sampling Functions */
2132 /**********************************************************************/
2136 sample_nearest_rect(GLcontext
*ctx
,
2137 const struct gl_texture_object
*tObj
, GLuint n
,
2138 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2141 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2142 const GLint width
= img
->Width
;
2143 const GLint height
= img
->Height
;
2149 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2150 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2151 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2152 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2153 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2154 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2155 ASSERT(img
->TexFormat
->BaseFormat
!= GL_COLOR_INDEX
);
2157 for (i
= 0; i
< n
; i
++) {
2159 col
= clamp_rect_coord_nearest(tObj
->WrapS
, texcoords
[i
][0], width
);
2160 row
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoords
[i
][1], height
);
2161 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2162 get_border_color(tObj
, img
, rgba
[i
]);
2164 img
->FetchTexelf(img
, col
, row
, 0, rgba
[i
]);
2170 sample_linear_rect(GLcontext
*ctx
,
2171 const struct gl_texture_object
*tObj
, GLuint n
,
2172 const GLfloat texcoords
[][4],
2173 const GLfloat lambda
[], GLfloat rgba
[][4])
2175 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2176 const GLint width
= img
->Width
;
2177 const GLint height
= img
->Height
;
2183 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2184 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2185 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2186 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2187 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2188 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2189 ASSERT(img
->TexFormat
->BaseFormat
!= GL_COLOR_INDEX
);
2191 for (i
= 0; i
< n
; i
++) {
2192 GLint i0
, j0
, i1
, j1
;
2193 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2195 GLbitfield useBorderColor
= 0x0;
2197 clamp_rect_coord_linear(tObj
->WrapS
, texcoords
[i
][0], width
,
2199 clamp_rect_coord_linear(tObj
->WrapT
, texcoords
[i
][1], height
,
2202 /* compute integer rows/columns */
2203 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2204 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2205 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2206 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2208 /* get four texel samples */
2209 if (useBorderColor
& (I0BIT
| J0BIT
))
2210 get_border_color(tObj
, img
, t00
);
2212 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
2214 if (useBorderColor
& (I1BIT
| J0BIT
))
2215 get_border_color(tObj
, img
, t10
);
2217 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
2219 if (useBorderColor
& (I0BIT
| J1BIT
))
2220 get_border_color(tObj
, img
, t01
);
2222 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
2224 if (useBorderColor
& (I1BIT
| J1BIT
))
2225 get_border_color(tObj
, img
, t11
);
2227 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
2229 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2234 /** Sample Rect texture, using lambda to choose between min/magnification */
2236 sample_lambda_rect(GLcontext
*ctx
,
2237 const struct gl_texture_object
*tObj
, GLuint n
,
2238 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2241 GLuint minStart
, minEnd
, magStart
, magEnd
;
2243 /* We only need lambda to decide between minification and magnification.
2244 * There is no mipmapping with rectangular textures.
2246 compute_min_mag_ranges(tObj
, n
, lambda
,
2247 &minStart
, &minEnd
, &magStart
, &magEnd
);
2249 if (minStart
< minEnd
) {
2250 if (tObj
->MinFilter
== GL_NEAREST
) {
2251 sample_nearest_rect(ctx
, tObj
, minEnd
- minStart
,
2252 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2255 sample_linear_rect(ctx
, tObj
, minEnd
- minStart
,
2256 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2259 if (magStart
< magEnd
) {
2260 if (tObj
->MagFilter
== GL_NEAREST
) {
2261 sample_nearest_rect(ctx
, tObj
, magEnd
- magStart
,
2262 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2265 sample_linear_rect(ctx
, tObj
, magEnd
- magStart
,
2266 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2273 /**********************************************************************/
2274 /* 2D Texture Array Sampling Functions */
2275 /**********************************************************************/
2278 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2281 sample_2d_array_nearest(GLcontext
*ctx
,
2282 const struct gl_texture_object
*tObj
,
2283 const struct gl_texture_image
*img
,
2284 const GLfloat texcoord
[4],
2287 const GLint width
= img
->Width2
; /* without border, power of two */
2288 const GLint height
= img
->Height2
; /* without border, power of two */
2289 const GLint depth
= img
->Depth
;
2294 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2295 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
2296 array
= clamp_rect_coord_nearest(tObj
->WrapR
, texcoord
[2], depth
);
2298 if (i
< 0 || i
>= (GLint
) img
->Width
||
2299 j
< 0 || j
>= (GLint
) img
->Height
||
2300 array
< 0 || array
>= (GLint
) img
->Depth
) {
2301 /* Need this test for GL_CLAMP_TO_BORDER mode */
2302 get_border_color(tObj
, img
, rgba
);
2305 img
->FetchTexelf(img
, i
, j
, array
, rgba
);
2311 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2314 sample_2d_array_linear(GLcontext
*ctx
,
2315 const struct gl_texture_object
*tObj
,
2316 const struct gl_texture_image
*img
,
2317 const GLfloat texcoord
[4],
2320 const GLint width
= img
->Width2
;
2321 const GLint height
= img
->Height2
;
2322 const GLint depth
= img
->Depth
;
2323 GLint i0
, j0
, i1
, j1
;
2325 GLbitfield useBorderColor
= 0x0;
2327 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2329 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2330 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2331 array
= clamp_rect_coord_nearest(tObj
->WrapR
, texcoord
[2], depth
);
2333 if (array
< 0 || array
>= depth
) {
2334 COPY_4V(rgba
, tObj
->BorderColor
);
2344 /* check if sampling texture border color */
2345 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2346 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2347 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2348 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2352 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2353 get_border_color(tObj
, img
, t00
);
2356 img
->FetchTexelf(img
, i0
, j0
, array
, t00
);
2358 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2359 get_border_color(tObj
, img
, t10
);
2362 img
->FetchTexelf(img
, i1
, j0
, array
, t10
);
2364 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2365 get_border_color(tObj
, img
, t01
);
2368 img
->FetchTexelf(img
, i0
, j1
, array
, t01
);
2370 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2371 get_border_color(tObj
, img
, t11
);
2374 img
->FetchTexelf(img
, i1
, j1
, array
, t11
);
2377 /* trilinear interpolation of samples */
2378 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2384 sample_2d_array_nearest_mipmap_nearest(GLcontext
*ctx
,
2385 const struct gl_texture_object
*tObj
,
2386 GLuint n
, const GLfloat texcoord
[][4],
2387 const GLfloat lambda
[], GLfloat rgba
[][4])
2390 for (i
= 0; i
< n
; i
++) {
2391 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2392 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2399 sample_2d_array_linear_mipmap_nearest(GLcontext
*ctx
,
2400 const struct gl_texture_object
*tObj
,
2401 GLuint n
, const GLfloat texcoord
[][4],
2402 const GLfloat lambda
[], GLfloat rgba
[][4])
2405 ASSERT(lambda
!= NULL
);
2406 for (i
= 0; i
< n
; i
++) {
2407 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2408 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2409 texcoord
[i
], rgba
[i
]);
2415 sample_2d_array_nearest_mipmap_linear(GLcontext
*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
= linear_mipmap_level(tObj
, lambda
[i
]);
2424 if (level
>= tObj
->_MaxLevel
) {
2425 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2426 texcoord
[i
], rgba
[i
]);
2429 GLfloat t0
[4], t1
[4]; /* texels */
2430 const GLfloat f
= FRAC(lambda
[i
]);
2431 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
],
2433 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1],
2435 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2442 sample_2d_array_linear_mipmap_linear(GLcontext
*ctx
,
2443 const struct gl_texture_object
*tObj
,
2444 GLuint n
, const GLfloat texcoord
[][4],
2445 const GLfloat lambda
[], GLfloat rgba
[][4])
2448 ASSERT(lambda
!= NULL
);
2449 for (i
= 0; i
< n
; i
++) {
2450 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2451 if (level
>= tObj
->_MaxLevel
) {
2452 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2453 texcoord
[i
], rgba
[i
]);
2456 GLfloat t0
[4], t1
[4]; /* texels */
2457 const GLfloat f
= FRAC(lambda
[i
]);
2458 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2460 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1],
2462 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2468 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2470 sample_nearest_2d_array(GLcontext
*ctx
,
2471 const struct gl_texture_object
*tObj
, GLuint n
,
2472 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2476 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2478 for (i
= 0; i
< n
; i
++) {
2479 sample_2d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2485 /** Sample 2D Array texture, linear filtering for both min/magnification */
2487 sample_linear_2d_array(GLcontext
*ctx
,
2488 const struct gl_texture_object
*tObj
, GLuint n
,
2489 const GLfloat texcoords
[][4],
2490 const GLfloat lambda
[], GLfloat rgba
[][4])
2493 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2495 for (i
= 0; i
< n
; i
++) {
2496 sample_2d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2501 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2503 sample_lambda_2d_array(GLcontext
*ctx
,
2504 const struct gl_texture_object
*tObj
, GLuint n
,
2505 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2508 GLuint minStart
, minEnd
; /* texels with minification */
2509 GLuint magStart
, magEnd
; /* texels with magnification */
2512 ASSERT(lambda
!= NULL
);
2513 compute_min_mag_ranges(tObj
, n
, lambda
,
2514 &minStart
, &minEnd
, &magStart
, &magEnd
);
2516 if (minStart
< minEnd
) {
2517 /* do the minified texels */
2518 GLuint m
= minEnd
- minStart
;
2519 switch (tObj
->MinFilter
) {
2521 for (i
= minStart
; i
< minEnd
; i
++)
2522 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2523 texcoords
[i
], rgba
[i
]);
2526 for (i
= minStart
; i
< minEnd
; i
++)
2527 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2528 texcoords
[i
], rgba
[i
]);
2530 case GL_NEAREST_MIPMAP_NEAREST
:
2531 sample_2d_array_nearest_mipmap_nearest(ctx
, tObj
, m
,
2532 texcoords
+ minStart
,
2536 case GL_LINEAR_MIPMAP_NEAREST
:
2537 sample_2d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2538 texcoords
+ minStart
,
2542 case GL_NEAREST_MIPMAP_LINEAR
:
2543 sample_2d_array_nearest_mipmap_linear(ctx
, tObj
, m
,
2544 texcoords
+ minStart
,
2548 case GL_LINEAR_MIPMAP_LINEAR
:
2549 sample_2d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2550 texcoords
+ minStart
,
2555 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
2560 if (magStart
< magEnd
) {
2561 /* do the magnified texels */
2562 switch (tObj
->MagFilter
) {
2564 for (i
= magStart
; i
< magEnd
; i
++)
2565 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2566 texcoords
[i
], rgba
[i
]);
2569 for (i
= magStart
; i
< magEnd
; i
++)
2570 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2571 texcoords
[i
], rgba
[i
]);
2574 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
2583 /**********************************************************************/
2584 /* 1D Texture Array Sampling Functions */
2585 /**********************************************************************/
2588 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2591 sample_1d_array_nearest(GLcontext
*ctx
,
2592 const struct gl_texture_object
*tObj
,
2593 const struct gl_texture_image
*img
,
2594 const GLfloat texcoord
[4],
2597 const GLint width
= img
->Width2
; /* without border, power of two */
2598 const GLint height
= img
->Height
;
2603 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2604 array
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoord
[1], height
);
2606 if (i
< 0 || i
>= (GLint
) img
->Width
||
2607 array
< 0 || array
>= (GLint
) img
->Height
) {
2608 /* Need this test for GL_CLAMP_TO_BORDER mode */
2609 get_border_color(tObj
, img
, rgba
);
2612 img
->FetchTexelf(img
, i
, array
, 0, rgba
);
2618 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2621 sample_1d_array_linear(GLcontext
*ctx
,
2622 const struct gl_texture_object
*tObj
,
2623 const struct gl_texture_image
*img
,
2624 const GLfloat texcoord
[4],
2627 const GLint width
= img
->Width2
;
2628 const GLint height
= img
->Height
;
2631 GLbitfield useBorderColor
= 0x0;
2633 GLfloat t0
[4], t1
[4];
2635 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2636 array
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoord
[1], height
);
2643 /* check if sampling texture border color */
2644 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2645 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2648 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
2651 if (useBorderColor
& (I0BIT
| K0BIT
)) {
2652 get_border_color(tObj
, img
, t0
);
2655 img
->FetchTexelf(img
, i0
, array
, 0, t0
);
2657 if (useBorderColor
& (I1BIT
| K0BIT
)) {
2658 get_border_color(tObj
, img
, t1
);
2661 img
->FetchTexelf(img
, i1
, array
, 0, t1
);
2664 /* bilinear interpolation of samples */
2665 lerp_rgba(rgba
, a
, t0
, t1
);
2670 sample_1d_array_nearest_mipmap_nearest(GLcontext
*ctx
,
2671 const struct gl_texture_object
*tObj
,
2672 GLuint n
, const GLfloat texcoord
[][4],
2673 const GLfloat lambda
[], GLfloat rgba
[][4])
2676 for (i
= 0; i
< n
; i
++) {
2677 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2678 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2685 sample_1d_array_linear_mipmap_nearest(GLcontext
*ctx
,
2686 const struct gl_texture_object
*tObj
,
2687 GLuint n
, const GLfloat texcoord
[][4],
2688 const GLfloat lambda
[], GLfloat rgba
[][4])
2691 ASSERT(lambda
!= NULL
);
2692 for (i
= 0; i
< n
; i
++) {
2693 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2694 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2695 texcoord
[i
], rgba
[i
]);
2701 sample_1d_array_nearest_mipmap_linear(GLcontext
*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
= linear_mipmap_level(tObj
, lambda
[i
]);
2710 if (level
>= tObj
->_MaxLevel
) {
2711 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2712 texcoord
[i
], rgba
[i
]);
2715 GLfloat t0
[4], t1
[4]; /* texels */
2716 const GLfloat f
= FRAC(lambda
[i
]);
2717 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2718 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2719 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2726 sample_1d_array_linear_mipmap_linear(GLcontext
*ctx
,
2727 const struct gl_texture_object
*tObj
,
2728 GLuint n
, const GLfloat texcoord
[][4],
2729 const GLfloat lambda
[], GLfloat rgba
[][4])
2732 ASSERT(lambda
!= NULL
);
2733 for (i
= 0; i
< n
; i
++) {
2734 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2735 if (level
>= tObj
->_MaxLevel
) {
2736 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2737 texcoord
[i
], rgba
[i
]);
2740 GLfloat t0
[4], t1
[4]; /* texels */
2741 const GLfloat f
= FRAC(lambda
[i
]);
2742 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2743 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2744 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2750 /** Sample 1D Array texture, nearest filtering for both min/magnification */
2752 sample_nearest_1d_array(GLcontext
*ctx
,
2753 const struct gl_texture_object
*tObj
, GLuint n
,
2754 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2758 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2760 for (i
= 0; i
< n
; i
++) {
2761 sample_1d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2766 /** Sample 1D Array texture, linear filtering for both min/magnification */
2768 sample_linear_1d_array(GLcontext
*ctx
,
2769 const struct gl_texture_object
*tObj
, GLuint n
,
2770 const GLfloat texcoords
[][4],
2771 const GLfloat lambda
[], GLfloat rgba
[][4])
2774 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2776 for (i
= 0; i
< n
; i
++) {
2777 sample_1d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2782 /** Sample 1D Array texture, using lambda to choose between min/magnification */
2784 sample_lambda_1d_array(GLcontext
*ctx
,
2785 const struct gl_texture_object
*tObj
, GLuint n
,
2786 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2789 GLuint minStart
, minEnd
; /* texels with minification */
2790 GLuint magStart
, magEnd
; /* texels with magnification */
2793 ASSERT(lambda
!= NULL
);
2794 compute_min_mag_ranges(tObj
, n
, lambda
,
2795 &minStart
, &minEnd
, &magStart
, &magEnd
);
2797 if (minStart
< minEnd
) {
2798 /* do the minified texels */
2799 GLuint m
= minEnd
- minStart
;
2800 switch (tObj
->MinFilter
) {
2802 for (i
= minStart
; i
< minEnd
; i
++)
2803 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2804 texcoords
[i
], rgba
[i
]);
2807 for (i
= minStart
; i
< minEnd
; i
++)
2808 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2809 texcoords
[i
], rgba
[i
]);
2811 case GL_NEAREST_MIPMAP_NEAREST
:
2812 sample_1d_array_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2813 lambda
+ minStart
, rgba
+ minStart
);
2815 case GL_LINEAR_MIPMAP_NEAREST
:
2816 sample_1d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2817 texcoords
+ minStart
,
2821 case GL_NEAREST_MIPMAP_LINEAR
:
2822 sample_1d_array_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2823 lambda
+ minStart
, rgba
+ minStart
);
2825 case GL_LINEAR_MIPMAP_LINEAR
:
2826 sample_1d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2827 texcoords
+ minStart
,
2832 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
2837 if (magStart
< magEnd
) {
2838 /* do the magnified texels */
2839 switch (tObj
->MagFilter
) {
2841 for (i
= magStart
; i
< magEnd
; i
++)
2842 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2843 texcoords
[i
], rgba
[i
]);
2846 for (i
= magStart
; i
< magEnd
; i
++)
2847 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2848 texcoords
[i
], rgba
[i
]);
2851 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
2859 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
2861 static INLINE GLfloat
2862 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
,
2867 return (coord
<= depthSample
) ? 1.0F
: ambient
;
2869 return (coord
>= depthSample
) ? 1.0F
: ambient
;
2871 return (coord
< depthSample
) ? 1.0F
: ambient
;
2873 return (coord
> depthSample
) ? 1.0F
: ambient
;
2875 return (coord
== depthSample
) ? 1.0F
: ambient
;
2877 return (coord
!= depthSample
) ? 1.0F
: ambient
;
2885 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
2892 * Compare texcoord against four depth samples.
2894 static INLINE GLfloat
2895 shadow_compare4(GLenum function
, GLfloat coord
,
2896 GLfloat depth00
, GLfloat depth01
,
2897 GLfloat depth10
, GLfloat depth11
,
2898 GLfloat ambient
, GLfloat wi
, GLfloat wj
)
2900 const GLfloat d
= (1.0F
- (GLfloat
) ambient
) * 0.25F
;
2901 GLfloat luminance
= 1.0F
;
2905 if (depth00
<= coord
) luminance
-= d
;
2906 if (depth01
<= coord
) luminance
-= d
;
2907 if (depth10
<= coord
) luminance
-= d
;
2908 if (depth11
<= coord
) luminance
-= d
;
2911 if (depth00
>= coord
) luminance
-= d
;
2912 if (depth01
>= coord
) luminance
-= d
;
2913 if (depth10
>= coord
) luminance
-= d
;
2914 if (depth11
>= coord
) luminance
-= d
;
2917 if (depth00
< coord
) luminance
-= d
;
2918 if (depth01
< coord
) luminance
-= d
;
2919 if (depth10
< coord
) luminance
-= d
;
2920 if (depth11
< coord
) luminance
-= d
;
2923 if (depth00
> coord
) luminance
-= d
;
2924 if (depth01
> coord
) luminance
-= d
;
2925 if (depth10
> coord
) luminance
-= d
;
2926 if (depth11
> coord
) luminance
-= d
;
2929 if (depth00
== coord
) luminance
-= d
;
2930 if (depth01
== coord
) luminance
-= d
;
2931 if (depth10
== coord
) luminance
-= d
;
2932 if (depth11
== coord
) luminance
-= d
;
2935 if (depth00
!= coord
) luminance
-= d
;
2936 if (depth01
!= coord
) luminance
-= d
;
2937 if (depth10
!= coord
) luminance
-= d
;
2938 if (depth11
!= coord
) luminance
-= d
;
2945 /* ordinary bilinear filtering */
2946 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
2948 _mesa_problem(NULL
, "Bad compare func in sample_depth_texture");
2955 * Sample a shadow/depth texture.
2958 sample_depth_texture( GLcontext
*ctx
,
2959 const struct gl_texture_object
*tObj
, GLuint n
,
2960 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2961 GLfloat texel
[][4] )
2963 const GLint baseLevel
= tObj
->BaseLevel
;
2964 const struct gl_texture_image
*img
= tObj
->Image
[0][baseLevel
];
2965 const GLint width
= img
->Width
;
2966 const GLint height
= img
->Height
;
2967 const GLint depth
= img
->Depth
;
2968 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
2976 ASSERT(img
->TexFormat
->BaseFormat
== GL_DEPTH_COMPONENT
||
2977 img
->TexFormat
->BaseFormat
== GL_DEPTH_STENCIL_EXT
);
2979 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
2980 tObj
->Target
== GL_TEXTURE_2D
||
2981 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
2982 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
2983 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
);
2985 ambient
= tObj
->CompareFailValue
;
2987 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
2989 function
= (tObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
2990 tObj
->CompareFunc
: GL_NONE
;
2992 if (tObj
->MagFilter
== GL_NEAREST
) {
2994 for (i
= 0; i
< n
; i
++) {
2995 GLfloat depthSample
;
2996 GLint col
, row
, slice
;
2998 nearest_texcoord(tObj
, texcoords
[i
], &col
, &row
, &slice
);
3000 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3001 slice
>= 0 && slice
< depth
) {
3002 img
->FetchTexelf(img
, col
, row
, slice
, &depthSample
);
3005 depthSample
= tObj
->BorderColor
[0];
3008 result
= shadow_compare(function
, texcoords
[i
][compare_coord
],
3009 depthSample
, ambient
);
3011 switch (tObj
->DepthMode
) {
3013 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3016 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3019 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3022 _mesa_problem(ctx
, "Bad depth texture mode");
3028 ASSERT(tObj
->MagFilter
== GL_LINEAR
);
3029 for (i
= 0; i
< n
; i
++) {
3030 GLfloat depth00
, depth01
, depth10
, depth11
;
3031 GLint i0
, i1
, j0
, j1
;
3034 GLuint useBorderTexel
;
3036 linear_texcoord(tObj
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3043 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3049 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3050 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3051 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3052 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3055 if (slice
< 0 || slice
>= (GLint
) depth
) {
3056 depth00
= tObj
->BorderColor
[0];
3057 depth01
= tObj
->BorderColor
[0];
3058 depth10
= tObj
->BorderColor
[0];
3059 depth11
= tObj
->BorderColor
[0];
3062 /* get four depth samples from the texture */
3063 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3064 depth00
= tObj
->BorderColor
[0];
3067 img
->FetchTexelf(img
, i0
, j0
, slice
, &depth00
);
3069 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3070 depth10
= tObj
->BorderColor
[0];
3073 img
->FetchTexelf(img
, i1
, j0
, slice
, &depth10
);
3076 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3077 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3078 depth01
= tObj
->BorderColor
[0];
3081 img
->FetchTexelf(img
, i0
, j1
, slice
, &depth01
);
3083 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3084 depth11
= tObj
->BorderColor
[0];
3087 img
->FetchTexelf(img
, i1
, j1
, slice
, &depth11
);
3096 result
= shadow_compare4(function
, texcoords
[i
][compare_coord
],
3097 depth00
, depth01
, depth10
, depth11
,
3100 switch (tObj
->DepthMode
) {
3102 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3105 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3108 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3111 _mesa_problem(ctx
, "Bad depth texture mode");
3120 * We use this function when a texture object is in an "incomplete" state.
3121 * When a fragment program attempts to sample an incomplete texture we
3122 * return black (see issue 23 in GL_ARB_fragment_program spec).
3123 * Note: fragment programs don't observe the texture enable/disable flags.
3126 null_sample_func( GLcontext
*ctx
,
3127 const struct gl_texture_object
*tObj
, GLuint n
,
3128 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3136 for (i
= 0; i
< n
; i
++) {
3140 rgba
[i
][ACOMP
] = CHAN_MAX
;
3146 * Choose the texture sampling function for the given texture object.
3149 _swrast_choose_texture_sample_func( GLcontext
*ctx
,
3150 const struct gl_texture_object
*t
)
3152 if (!t
|| !t
->_Complete
) {
3153 return &null_sample_func
;
3156 const GLboolean needLambda
= (GLboolean
) (t
->MinFilter
!= t
->MagFilter
);
3157 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->TexFormat
->BaseFormat
;
3159 switch (t
->Target
) {
3161 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3162 return &sample_depth_texture
;
3164 else if (needLambda
) {
3165 return &sample_lambda_1d
;
3167 else if (t
->MinFilter
== GL_LINEAR
) {
3168 return &sample_linear_1d
;
3171 ASSERT(t
->MinFilter
== GL_NEAREST
);
3172 return &sample_nearest_1d
;
3175 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3176 return &sample_depth_texture
;
3178 else if (needLambda
) {
3179 return &sample_lambda_2d
;
3181 else if (t
->MinFilter
== GL_LINEAR
) {
3182 return &sample_linear_2d
;
3185 /* check for a few optimized cases */
3186 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3187 ASSERT(t
->MinFilter
== GL_NEAREST
);
3188 if (t
->WrapS
== GL_REPEAT
&&
3189 t
->WrapT
== GL_REPEAT
&&
3190 img
->_IsPowerOfTwo
&&
3192 img
->TexFormat
->MesaFormat
== MESA_FORMAT_RGB
) {
3193 return &opt_sample_rgb_2d
;
3195 else if (t
->WrapS
== GL_REPEAT
&&
3196 t
->WrapT
== GL_REPEAT
&&
3197 img
->_IsPowerOfTwo
&&
3199 img
->TexFormat
->MesaFormat
== MESA_FORMAT_RGBA
) {
3200 return &opt_sample_rgba_2d
;
3203 return &sample_nearest_2d
;
3208 return &sample_lambda_3d
;
3210 else if (t
->MinFilter
== GL_LINEAR
) {
3211 return &sample_linear_3d
;
3214 ASSERT(t
->MinFilter
== GL_NEAREST
);
3215 return &sample_nearest_3d
;
3217 case GL_TEXTURE_CUBE_MAP
:
3219 return &sample_lambda_cube
;
3221 else if (t
->MinFilter
== GL_LINEAR
) {
3222 return &sample_linear_cube
;
3225 ASSERT(t
->MinFilter
== GL_NEAREST
);
3226 return &sample_nearest_cube
;
3228 case GL_TEXTURE_RECTANGLE_NV
:
3229 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3230 return &sample_depth_texture
;
3232 else if (needLambda
) {
3233 return &sample_lambda_rect
;
3235 else if (t
->MinFilter
== GL_LINEAR
) {
3236 return &sample_linear_rect
;
3239 ASSERT(t
->MinFilter
== GL_NEAREST
);
3240 return &sample_nearest_rect
;
3242 case GL_TEXTURE_1D_ARRAY_EXT
:
3244 return &sample_lambda_1d_array
;
3246 else if (t
->MinFilter
== GL_LINEAR
) {
3247 return &sample_linear_1d_array
;
3250 ASSERT(t
->MinFilter
== GL_NEAREST
);
3251 return &sample_nearest_1d_array
;
3253 case GL_TEXTURE_2D_ARRAY_EXT
:
3255 return &sample_lambda_2d_array
;
3257 else if (t
->MinFilter
== GL_LINEAR
) {
3258 return &sample_linear_2d_array
;
3261 ASSERT(t
->MinFilter
== GL_NEAREST
);
3262 return &sample_nearest_2d_array
;
3266 "invalid target in _swrast_choose_texture_sample_func");
3267 return &null_sample_func
;