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) ((unsigned) (A) % (unsigned) (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
[], GLchan 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
] = texel
[0];
1355 rgba
[k
][GCOMP
] = texel
[1];
1356 rgba
[k
][BCOMP
] = 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
[], GLchan 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 COPY_4V(rgba
[i
], texel
);
1400 /** Sample 2D texture, using lambda to choose between min/magnification */
1402 sample_lambda_2d(GLcontext
*ctx
,
1403 const struct gl_texture_object
*tObj
,
1404 GLuint n
, const GLfloat texcoords
[][4],
1405 const GLfloat lambda
[], GLfloat rgba
[][4])
1407 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1408 GLuint minStart
, minEnd
; /* texels with minification */
1409 GLuint magStart
, magEnd
; /* texels with magnification */
1411 const GLboolean repeatNoBorderPOT
= (tObj
->WrapS
== GL_REPEAT
)
1412 && (tObj
->WrapT
== GL_REPEAT
)
1413 && (tImg
->Border
== 0 && (tImg
->Width
== tImg
->RowStride
))
1414 && (tImg
->TexFormat
->BaseFormat
!= GL_COLOR_INDEX
)
1415 && tImg
->_IsPowerOfTwo
;
1417 ASSERT(lambda
!= NULL
);
1418 compute_min_mag_ranges(tObj
, n
, lambda
,
1419 &minStart
, &minEnd
, &magStart
, &magEnd
);
1421 if (minStart
< minEnd
) {
1422 /* do the minified texels */
1423 const GLuint m
= minEnd
- minStart
;
1424 switch (tObj
->MinFilter
) {
1426 if (repeatNoBorderPOT
) {
1427 switch (tImg
->TexFormat
->MesaFormat
) {
1429 case MESA_FORMAT_RGB
:
1430 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1431 NULL
, rgba
+ minStart
);
1433 case MESA_FORMAT_RGBA
:
1434 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1435 NULL
, rgba
+ minStart
);
1439 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1440 NULL
, rgba
+ minStart
);
1444 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1445 NULL
, rgba
+ minStart
);
1449 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1450 NULL
, rgba
+ minStart
);
1452 case GL_NEAREST_MIPMAP_NEAREST
:
1453 sample_2d_nearest_mipmap_nearest(ctx
, tObj
, m
,
1454 texcoords
+ minStart
,
1455 lambda
+ minStart
, rgba
+ minStart
);
1457 case GL_LINEAR_MIPMAP_NEAREST
:
1458 sample_2d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1459 lambda
+ minStart
, rgba
+ minStart
);
1461 case GL_NEAREST_MIPMAP_LINEAR
:
1462 sample_2d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1463 lambda
+ minStart
, rgba
+ minStart
);
1465 case GL_LINEAR_MIPMAP_LINEAR
:
1466 if (repeatNoBorderPOT
)
1467 sample_2d_linear_mipmap_linear_repeat(ctx
, tObj
, m
,
1468 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1470 sample_2d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1471 lambda
+ minStart
, rgba
+ minStart
);
1474 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1479 if (magStart
< magEnd
) {
1480 /* do the magnified texels */
1481 const GLuint m
= magEnd
- magStart
;
1483 switch (tObj
->MagFilter
) {
1485 if (repeatNoBorderPOT
) {
1486 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
);
1498 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1499 NULL
, rgba
+ magStart
);
1503 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1504 NULL
, rgba
+ magStart
);
1508 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1509 NULL
, rgba
+ magStart
);
1512 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1519 /**********************************************************************/
1520 /* 3-D Texture Sampling Functions */
1521 /**********************************************************************/
1524 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1527 sample_3d_nearest(GLcontext
*ctx
,
1528 const struct gl_texture_object
*tObj
,
1529 const struct gl_texture_image
*img
,
1530 const GLfloat texcoord
[4],
1533 const GLint width
= img
->Width2
; /* without border, power of two */
1534 const GLint height
= img
->Height2
; /* without border, power of two */
1535 const GLint depth
= img
->Depth2
; /* without border, power of two */
1539 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
1540 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
1541 k
= nearest_texel_location(tObj
->WrapR
, img
, depth
, texcoord
[2]);
1543 if (i
< 0 || i
>= (GLint
) img
->Width
||
1544 j
< 0 || j
>= (GLint
) img
->Height
||
1545 k
< 0 || k
>= (GLint
) img
->Depth
) {
1546 /* Need this test for GL_CLAMP_TO_BORDER mode */
1547 get_border_color(tObj
, img
, rgba
);
1550 img
->FetchTexelf(img
, i
, j
, k
, rgba
);
1556 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1559 sample_3d_linear(GLcontext
*ctx
,
1560 const struct gl_texture_object
*tObj
,
1561 const struct gl_texture_image
*img
,
1562 const GLfloat texcoord
[4],
1565 const GLint width
= img
->Width2
;
1566 const GLint height
= img
->Height2
;
1567 const GLint depth
= img
->Depth2
;
1568 GLint i0
, j0
, k0
, i1
, j1
, k1
;
1569 GLbitfield useBorderColor
= 0x0;
1571 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
1572 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
1574 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1575 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1576 linear_texel_locations(tObj
->WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
1587 /* check if sampling texture border color */
1588 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1589 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1590 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1591 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1592 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
1593 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
1597 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
1598 get_border_color(tObj
, img
, t000
);
1601 img
->FetchTexelf(img
, i0
, j0
, k0
, t000
);
1603 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
1604 get_border_color(tObj
, img
, t100
);
1607 img
->FetchTexelf(img
, i1
, j0
, k0
, t100
);
1609 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
1610 get_border_color(tObj
, img
, t010
);
1613 img
->FetchTexelf(img
, i0
, j1
, k0
, t010
);
1615 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
1616 get_border_color(tObj
, img
, t110
);
1619 img
->FetchTexelf(img
, i1
, j1
, k0
, t110
);
1622 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
1623 get_border_color(tObj
, img
, t001
);
1626 img
->FetchTexelf(img
, i0
, j0
, k1
, t001
);
1628 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
1629 get_border_color(tObj
, img
, t101
);
1632 img
->FetchTexelf(img
, i1
, j0
, k1
, t101
);
1634 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
1635 get_border_color(tObj
, img
, t011
);
1638 img
->FetchTexelf(img
, i0
, j1
, k1
, t011
);
1640 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
1641 get_border_color(tObj
, img
, t111
);
1644 img
->FetchTexelf(img
, i1
, j1
, k1
, t111
);
1647 /* trilinear interpolation of samples */
1648 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
1653 sample_3d_nearest_mipmap_nearest(GLcontext
*ctx
,
1654 const struct gl_texture_object
*tObj
,
1655 GLuint n
, const GLfloat texcoord
[][4],
1656 const GLfloat lambda
[], GLfloat rgba
[][4] )
1659 for (i
= 0; i
< n
; i
++) {
1660 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1661 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1667 sample_3d_linear_mipmap_nearest(GLcontext
*ctx
,
1668 const struct gl_texture_object
*tObj
,
1669 GLuint n
, const GLfloat texcoord
[][4],
1670 const GLfloat lambda
[], GLfloat rgba
[][4])
1673 ASSERT(lambda
!= NULL
);
1674 for (i
= 0; i
< n
; i
++) {
1675 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1676 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1682 sample_3d_nearest_mipmap_linear(GLcontext
*ctx
,
1683 const struct gl_texture_object
*tObj
,
1684 GLuint n
, const GLfloat texcoord
[][4],
1685 const GLfloat lambda
[], GLfloat rgba
[][4])
1688 ASSERT(lambda
!= NULL
);
1689 for (i
= 0; i
< n
; i
++) {
1690 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1691 if (level
>= tObj
->_MaxLevel
) {
1692 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1693 texcoord
[i
], rgba
[i
]);
1696 GLfloat t0
[4], t1
[4]; /* texels */
1697 const GLfloat f
= FRAC(lambda
[i
]);
1698 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1699 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1700 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1707 sample_3d_linear_mipmap_linear(GLcontext
*ctx
,
1708 const struct gl_texture_object
*tObj
,
1709 GLuint n
, const GLfloat texcoord
[][4],
1710 const GLfloat lambda
[], GLfloat rgba
[][4])
1713 ASSERT(lambda
!= NULL
);
1714 for (i
= 0; i
< n
; i
++) {
1715 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1716 if (level
>= tObj
->_MaxLevel
) {
1717 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1718 texcoord
[i
], rgba
[i
]);
1721 GLfloat t0
[4], t1
[4]; /* texels */
1722 const GLfloat f
= FRAC(lambda
[i
]);
1723 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1724 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1725 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1731 /** Sample 3D texture, nearest filtering for both min/magnification */
1733 sample_nearest_3d(GLcontext
*ctx
,
1734 const struct gl_texture_object
*tObj
, GLuint n
,
1735 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1739 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1741 for (i
= 0; i
< n
; i
++) {
1742 sample_3d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1747 /** Sample 3D texture, linear filtering for both min/magnification */
1749 sample_linear_3d(GLcontext
*ctx
,
1750 const struct gl_texture_object
*tObj
, GLuint n
,
1751 const GLfloat texcoords
[][4],
1752 const GLfloat lambda
[], GLfloat rgba
[][4])
1755 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1757 for (i
= 0; i
< n
; i
++) {
1758 sample_3d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1763 /** Sample 3D texture, using lambda to choose between min/magnification */
1765 sample_lambda_3d(GLcontext
*ctx
,
1766 const struct gl_texture_object
*tObj
, GLuint n
,
1767 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1770 GLuint minStart
, minEnd
; /* texels with minification */
1771 GLuint magStart
, magEnd
; /* texels with magnification */
1774 ASSERT(lambda
!= NULL
);
1775 compute_min_mag_ranges(tObj
, n
, lambda
,
1776 &minStart
, &minEnd
, &magStart
, &magEnd
);
1778 if (minStart
< minEnd
) {
1779 /* do the minified texels */
1780 GLuint m
= minEnd
- minStart
;
1781 switch (tObj
->MinFilter
) {
1783 for (i
= minStart
; i
< minEnd
; i
++)
1784 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1785 texcoords
[i
], rgba
[i
]);
1788 for (i
= minStart
; i
< minEnd
; i
++)
1789 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1790 texcoords
[i
], rgba
[i
]);
1792 case GL_NEAREST_MIPMAP_NEAREST
:
1793 sample_3d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1794 lambda
+ minStart
, rgba
+ minStart
);
1796 case GL_LINEAR_MIPMAP_NEAREST
:
1797 sample_3d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1798 lambda
+ minStart
, rgba
+ minStart
);
1800 case GL_NEAREST_MIPMAP_LINEAR
:
1801 sample_3d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1802 lambda
+ minStart
, rgba
+ minStart
);
1804 case GL_LINEAR_MIPMAP_LINEAR
:
1805 sample_3d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1806 lambda
+ minStart
, rgba
+ minStart
);
1809 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
1814 if (magStart
< magEnd
) {
1815 /* do the magnified texels */
1816 switch (tObj
->MagFilter
) {
1818 for (i
= magStart
; i
< magEnd
; i
++)
1819 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1820 texcoords
[i
], rgba
[i
]);
1823 for (i
= magStart
; i
< magEnd
; i
++)
1824 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1825 texcoords
[i
], rgba
[i
]);
1828 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
1835 /**********************************************************************/
1836 /* Texture Cube Map Sampling Functions */
1837 /**********************************************************************/
1840 * Choose one of six sides of a texture cube map given the texture
1841 * coord (rx,ry,rz). Return pointer to corresponding array of texture
1844 static const struct gl_texture_image
**
1845 choose_cube_face(const struct gl_texture_object
*texObj
,
1846 const GLfloat texcoord
[4], GLfloat newCoord
[4])
1850 direction target sc tc ma
1851 ---------- ------------------------------- --- --- ---
1852 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1853 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1854 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1855 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1856 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1857 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1859 const GLfloat rx
= texcoord
[0];
1860 const GLfloat ry
= texcoord
[1];
1861 const GLfloat rz
= texcoord
[2];
1862 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
1866 if (arx
> ary
&& arx
> arz
) {
1880 else if (ary
> arx
&& ary
> arz
) {
1909 newCoord
[0] = ( sc
/ ma
+ 1.0F
) * 0.5F
;
1910 newCoord
[1] = ( tc
/ ma
+ 1.0F
) * 0.5F
;
1911 return (const struct gl_texture_image
**) texObj
->Image
[face
];
1916 sample_nearest_cube(GLcontext
*ctx
,
1917 const struct gl_texture_object
*tObj
, GLuint n
,
1918 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1923 for (i
= 0; i
< n
; i
++) {
1924 const struct gl_texture_image
**images
;
1925 GLfloat newCoord
[4];
1926 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1927 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
1934 sample_linear_cube(GLcontext
*ctx
,
1935 const struct gl_texture_object
*tObj
, GLuint n
,
1936 const GLfloat texcoords
[][4],
1937 const GLfloat lambda
[], GLfloat rgba
[][4])
1941 for (i
= 0; i
< n
; i
++) {
1942 const struct gl_texture_image
**images
;
1943 GLfloat newCoord
[4];
1944 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1945 sample_2d_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
1952 sample_cube_nearest_mipmap_nearest(GLcontext
*ctx
,
1953 const struct gl_texture_object
*tObj
,
1954 GLuint n
, const GLfloat texcoord
[][4],
1955 const GLfloat lambda
[], GLfloat rgba
[][4])
1958 ASSERT(lambda
!= NULL
);
1959 for (i
= 0; i
< n
; i
++) {
1960 const struct gl_texture_image
**images
;
1961 GLfloat newCoord
[4];
1963 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1965 /* XXX we actually need to recompute lambda here based on the newCoords.
1966 * But we would need the texcoords of adjacent fragments to compute that
1967 * properly, and we don't have those here.
1968 * For now, do an approximation: subtracting 1 from the chosen mipmap
1969 * level seems to work in some test cases.
1970 * The same adjustment is done in the next few functions.
1972 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1973 level
= MAX2(level
- 1, 0);
1975 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
1981 sample_cube_linear_mipmap_nearest(GLcontext
*ctx
,
1982 const struct gl_texture_object
*tObj
,
1983 GLuint n
, const GLfloat texcoord
[][4],
1984 const GLfloat lambda
[], GLfloat rgba
[][4])
1987 ASSERT(lambda
!= NULL
);
1988 for (i
= 0; i
< n
; i
++) {
1989 const struct gl_texture_image
**images
;
1990 GLfloat newCoord
[4];
1991 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1992 level
= MAX2(level
- 1, 0); /* see comment above */
1993 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1994 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2000 sample_cube_nearest_mipmap_linear(GLcontext
*ctx
,
2001 const struct gl_texture_object
*tObj
,
2002 GLuint n
, const GLfloat texcoord
[][4],
2003 const GLfloat lambda
[], GLfloat rgba
[][4])
2006 ASSERT(lambda
!= NULL
);
2007 for (i
= 0; i
< n
; i
++) {
2008 const struct gl_texture_image
**images
;
2009 GLfloat newCoord
[4];
2010 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2011 level
= MAX2(level
- 1, 0); /* see comment above */
2012 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2013 if (level
>= tObj
->_MaxLevel
) {
2014 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2018 GLfloat t0
[4], t1
[4]; /* texels */
2019 const GLfloat f
= FRAC(lambda
[i
]);
2020 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
2021 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2022 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2029 sample_cube_linear_mipmap_linear(GLcontext
*ctx
,
2030 const struct gl_texture_object
*tObj
,
2031 GLuint n
, const GLfloat texcoord
[][4],
2032 const GLfloat lambda
[], GLfloat rgba
[][4])
2035 ASSERT(lambda
!= NULL
);
2036 for (i
= 0; i
< n
; i
++) {
2037 const struct gl_texture_image
**images
;
2038 GLfloat newCoord
[4];
2039 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2040 level
= MAX2(level
- 1, 0); /* see comment above */
2041 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2042 if (level
>= tObj
->_MaxLevel
) {
2043 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2047 GLfloat t0
[4], t1
[4];
2048 const GLfloat f
= FRAC(lambda
[i
]);
2049 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
2050 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2051 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2057 /** Sample cube texture, using lambda to choose between min/magnification */
2059 sample_lambda_cube(GLcontext
*ctx
,
2060 const struct gl_texture_object
*tObj
, GLuint n
,
2061 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2064 GLuint minStart
, minEnd
; /* texels with minification */
2065 GLuint magStart
, magEnd
; /* texels with magnification */
2067 ASSERT(lambda
!= NULL
);
2068 compute_min_mag_ranges(tObj
, n
, lambda
,
2069 &minStart
, &minEnd
, &magStart
, &magEnd
);
2071 if (minStart
< minEnd
) {
2072 /* do the minified texels */
2073 const GLuint m
= minEnd
- minStart
;
2074 switch (tObj
->MinFilter
) {
2076 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2077 lambda
+ minStart
, rgba
+ minStart
);
2080 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2081 lambda
+ minStart
, rgba
+ minStart
);
2083 case GL_NEAREST_MIPMAP_NEAREST
:
2084 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
2085 texcoords
+ minStart
,
2086 lambda
+ minStart
, rgba
+ minStart
);
2088 case GL_LINEAR_MIPMAP_NEAREST
:
2089 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
2090 texcoords
+ minStart
,
2091 lambda
+ minStart
, rgba
+ minStart
);
2093 case GL_NEAREST_MIPMAP_LINEAR
:
2094 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
2095 texcoords
+ minStart
,
2096 lambda
+ minStart
, rgba
+ minStart
);
2098 case GL_LINEAR_MIPMAP_LINEAR
:
2099 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
2100 texcoords
+ minStart
,
2101 lambda
+ minStart
, rgba
+ minStart
);
2104 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2108 if (magStart
< magEnd
) {
2109 /* do the magnified texels */
2110 const GLuint m
= magEnd
- magStart
;
2111 switch (tObj
->MagFilter
) {
2113 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2114 lambda
+ magStart
, rgba
+ magStart
);
2117 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2118 lambda
+ magStart
, rgba
+ magStart
);
2121 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2127 /**********************************************************************/
2128 /* Texture Rectangle Sampling Functions */
2129 /**********************************************************************/
2133 sample_nearest_rect(GLcontext
*ctx
,
2134 const struct gl_texture_object
*tObj
, GLuint n
,
2135 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2138 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2139 const GLint width
= img
->Width
;
2140 const GLint height
= img
->Height
;
2146 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2147 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2148 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2149 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2150 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2151 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2152 ASSERT(img
->TexFormat
->BaseFormat
!= GL_COLOR_INDEX
);
2154 for (i
= 0; i
< n
; i
++) {
2156 col
= clamp_rect_coord_nearest(tObj
->WrapS
, texcoords
[i
][0], width
);
2157 row
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoords
[i
][1], height
);
2158 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2159 get_border_color(tObj
, img
, rgba
[i
]);
2161 img
->FetchTexelf(img
, col
, row
, 0, rgba
[i
]);
2167 sample_linear_rect(GLcontext
*ctx
,
2168 const struct gl_texture_object
*tObj
, GLuint n
,
2169 const GLfloat texcoords
[][4],
2170 const GLfloat lambda
[], GLfloat rgba
[][4])
2172 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2173 const GLint width
= img
->Width
;
2174 const GLint height
= img
->Height
;
2180 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2181 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2182 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2183 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2184 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2185 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2186 ASSERT(img
->TexFormat
->BaseFormat
!= GL_COLOR_INDEX
);
2188 for (i
= 0; i
< n
; i
++) {
2189 GLint i0
, j0
, i1
, j1
;
2190 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2192 GLbitfield useBorderColor
= 0x0;
2194 clamp_rect_coord_linear(tObj
->WrapS
, texcoords
[i
][0], width
,
2196 clamp_rect_coord_linear(tObj
->WrapT
, texcoords
[i
][1], height
,
2199 /* compute integer rows/columns */
2200 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2201 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2202 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2203 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2205 /* get four texel samples */
2206 if (useBorderColor
& (I0BIT
| J0BIT
))
2207 get_border_color(tObj
, img
, t00
);
2209 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
2211 if (useBorderColor
& (I1BIT
| J0BIT
))
2212 get_border_color(tObj
, img
, t10
);
2214 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
2216 if (useBorderColor
& (I0BIT
| J1BIT
))
2217 get_border_color(tObj
, img
, t01
);
2219 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
2221 if (useBorderColor
& (I1BIT
| J1BIT
))
2222 get_border_color(tObj
, img
, t11
);
2224 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
2226 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2231 /** Sample Rect texture, using lambda to choose between min/magnification */
2233 sample_lambda_rect(GLcontext
*ctx
,
2234 const struct gl_texture_object
*tObj
, GLuint n
,
2235 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2238 GLuint minStart
, minEnd
, magStart
, magEnd
;
2240 /* We only need lambda to decide between minification and magnification.
2241 * There is no mipmapping with rectangular textures.
2243 compute_min_mag_ranges(tObj
, n
, lambda
,
2244 &minStart
, &minEnd
, &magStart
, &magEnd
);
2246 if (minStart
< minEnd
) {
2247 if (tObj
->MinFilter
== GL_NEAREST
) {
2248 sample_nearest_rect(ctx
, tObj
, minEnd
- minStart
,
2249 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2252 sample_linear_rect(ctx
, tObj
, minEnd
- minStart
,
2253 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2256 if (magStart
< magEnd
) {
2257 if (tObj
->MagFilter
== GL_NEAREST
) {
2258 sample_nearest_rect(ctx
, tObj
, magEnd
- magStart
,
2259 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2262 sample_linear_rect(ctx
, tObj
, magEnd
- magStart
,
2263 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2270 /**********************************************************************/
2271 /* 2D Texture Array Sampling Functions */
2272 /**********************************************************************/
2275 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2278 sample_2d_array_nearest(GLcontext
*ctx
,
2279 const struct gl_texture_object
*tObj
,
2280 const struct gl_texture_image
*img
,
2281 const GLfloat texcoord
[4],
2284 const GLint width
= img
->Width2
; /* without border, power of two */
2285 const GLint height
= img
->Height2
; /* without border, power of two */
2286 const GLint depth
= img
->Depth
;
2291 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2292 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
2293 array
= clamp_rect_coord_nearest(tObj
->WrapR
, texcoord
[2], depth
);
2295 if (i
< 0 || i
>= (GLint
) img
->Width
||
2296 j
< 0 || j
>= (GLint
) img
->Height
||
2297 array
< 0 || array
>= (GLint
) img
->Depth
) {
2298 /* Need this test for GL_CLAMP_TO_BORDER mode */
2299 get_border_color(tObj
, img
, rgba
);
2302 img
->FetchTexelf(img
, i
, j
, array
, rgba
);
2308 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2311 sample_2d_array_linear(GLcontext
*ctx
,
2312 const struct gl_texture_object
*tObj
,
2313 const struct gl_texture_image
*img
,
2314 const GLfloat texcoord
[4],
2317 const GLint width
= img
->Width2
;
2318 const GLint height
= img
->Height2
;
2319 const GLint depth
= img
->Depth
;
2320 GLint i0
, j0
, i1
, j1
;
2322 GLbitfield useBorderColor
= 0x0;
2324 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2326 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2327 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2328 array
= clamp_rect_coord_nearest(tObj
->WrapR
, texcoord
[2], depth
);
2330 if (array
< 0 || array
>= depth
) {
2331 COPY_4V(rgba
, tObj
->BorderColor
);
2341 /* check if sampling texture border color */
2342 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2343 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2344 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2345 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2349 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2350 get_border_color(tObj
, img
, t00
);
2353 img
->FetchTexelf(img
, i0
, j0
, array
, t00
);
2355 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2356 get_border_color(tObj
, img
, t10
);
2359 img
->FetchTexelf(img
, i1
, j0
, array
, t10
);
2361 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2362 get_border_color(tObj
, img
, t01
);
2365 img
->FetchTexelf(img
, i0
, j1
, array
, t01
);
2367 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2368 get_border_color(tObj
, img
, t11
);
2371 img
->FetchTexelf(img
, i1
, j1
, array
, t11
);
2374 /* trilinear interpolation of samples */
2375 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2381 sample_2d_array_nearest_mipmap_nearest(GLcontext
*ctx
,
2382 const struct gl_texture_object
*tObj
,
2383 GLuint n
, const GLfloat texcoord
[][4],
2384 const GLfloat lambda
[], GLfloat rgba
[][4])
2387 for (i
= 0; i
< n
; i
++) {
2388 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2389 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2396 sample_2d_array_linear_mipmap_nearest(GLcontext
*ctx
,
2397 const struct gl_texture_object
*tObj
,
2398 GLuint n
, const GLfloat texcoord
[][4],
2399 const GLfloat lambda
[], GLfloat rgba
[][4])
2402 ASSERT(lambda
!= NULL
);
2403 for (i
= 0; i
< n
; i
++) {
2404 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2405 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2406 texcoord
[i
], rgba
[i
]);
2412 sample_2d_array_nearest_mipmap_linear(GLcontext
*ctx
,
2413 const struct gl_texture_object
*tObj
,
2414 GLuint n
, const GLfloat texcoord
[][4],
2415 const GLfloat lambda
[], GLfloat rgba
[][4])
2418 ASSERT(lambda
!= NULL
);
2419 for (i
= 0; i
< n
; i
++) {
2420 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2421 if (level
>= tObj
->_MaxLevel
) {
2422 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2423 texcoord
[i
], rgba
[i
]);
2426 GLfloat t0
[4], t1
[4]; /* texels */
2427 const GLfloat f
= FRAC(lambda
[i
]);
2428 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
],
2430 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1],
2432 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2439 sample_2d_array_linear_mipmap_linear(GLcontext
*ctx
,
2440 const struct gl_texture_object
*tObj
,
2441 GLuint n
, const GLfloat texcoord
[][4],
2442 const GLfloat lambda
[], GLfloat rgba
[][4])
2445 ASSERT(lambda
!= NULL
);
2446 for (i
= 0; i
< n
; i
++) {
2447 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2448 if (level
>= tObj
->_MaxLevel
) {
2449 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2450 texcoord
[i
], rgba
[i
]);
2453 GLfloat t0
[4], t1
[4]; /* texels */
2454 const GLfloat f
= FRAC(lambda
[i
]);
2455 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2457 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1],
2459 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2465 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2467 sample_nearest_2d_array(GLcontext
*ctx
,
2468 const struct gl_texture_object
*tObj
, GLuint n
,
2469 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2473 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2475 for (i
= 0; i
< n
; i
++) {
2476 sample_2d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2482 /** Sample 2D Array texture, linear filtering for both min/magnification */
2484 sample_linear_2d_array(GLcontext
*ctx
,
2485 const struct gl_texture_object
*tObj
, GLuint n
,
2486 const GLfloat texcoords
[][4],
2487 const GLfloat lambda
[], GLfloat rgba
[][4])
2490 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2492 for (i
= 0; i
< n
; i
++) {
2493 sample_2d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2498 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2500 sample_lambda_2d_array(GLcontext
*ctx
,
2501 const struct gl_texture_object
*tObj
, GLuint n
,
2502 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2505 GLuint minStart
, minEnd
; /* texels with minification */
2506 GLuint magStart
, magEnd
; /* texels with magnification */
2509 ASSERT(lambda
!= NULL
);
2510 compute_min_mag_ranges(tObj
, n
, lambda
,
2511 &minStart
, &minEnd
, &magStart
, &magEnd
);
2513 if (minStart
< minEnd
) {
2514 /* do the minified texels */
2515 GLuint m
= minEnd
- minStart
;
2516 switch (tObj
->MinFilter
) {
2518 for (i
= minStart
; i
< minEnd
; i
++)
2519 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2520 texcoords
[i
], rgba
[i
]);
2523 for (i
= minStart
; i
< minEnd
; i
++)
2524 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2525 texcoords
[i
], rgba
[i
]);
2527 case GL_NEAREST_MIPMAP_NEAREST
:
2528 sample_2d_array_nearest_mipmap_nearest(ctx
, tObj
, m
,
2529 texcoords
+ minStart
,
2533 case GL_LINEAR_MIPMAP_NEAREST
:
2534 sample_2d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2535 texcoords
+ minStart
,
2539 case GL_NEAREST_MIPMAP_LINEAR
:
2540 sample_2d_array_nearest_mipmap_linear(ctx
, tObj
, m
,
2541 texcoords
+ minStart
,
2545 case GL_LINEAR_MIPMAP_LINEAR
:
2546 sample_2d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2547 texcoords
+ minStart
,
2552 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
2557 if (magStart
< magEnd
) {
2558 /* do the magnified texels */
2559 switch (tObj
->MagFilter
) {
2561 for (i
= magStart
; i
< magEnd
; i
++)
2562 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2563 texcoords
[i
], rgba
[i
]);
2566 for (i
= magStart
; i
< magEnd
; i
++)
2567 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2568 texcoords
[i
], rgba
[i
]);
2571 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
2580 /**********************************************************************/
2581 /* 1D Texture Array Sampling Functions */
2582 /**********************************************************************/
2585 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2588 sample_1d_array_nearest(GLcontext
*ctx
,
2589 const struct gl_texture_object
*tObj
,
2590 const struct gl_texture_image
*img
,
2591 const GLfloat texcoord
[4],
2594 const GLint width
= img
->Width2
; /* without border, power of two */
2595 const GLint height
= img
->Height
;
2600 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2601 array
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoord
[1], height
);
2603 if (i
< 0 || i
>= (GLint
) img
->Width
||
2604 array
< 0 || array
>= (GLint
) img
->Height
) {
2605 /* Need this test for GL_CLAMP_TO_BORDER mode */
2606 get_border_color(tObj
, img
, rgba
);
2609 img
->FetchTexelf(img
, i
, array
, 0, rgba
);
2615 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2618 sample_1d_array_linear(GLcontext
*ctx
,
2619 const struct gl_texture_object
*tObj
,
2620 const struct gl_texture_image
*img
,
2621 const GLfloat texcoord
[4],
2624 const GLint width
= img
->Width2
;
2625 const GLint height
= img
->Height
;
2628 GLbitfield useBorderColor
= 0x0;
2630 GLfloat t0
[4], t1
[4];
2632 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2633 array
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoord
[1], height
);
2640 /* check if sampling texture border color */
2641 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2642 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2645 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
2648 if (useBorderColor
& (I0BIT
| K0BIT
)) {
2649 get_border_color(tObj
, img
, t0
);
2652 img
->FetchTexelf(img
, i0
, array
, 0, t0
);
2654 if (useBorderColor
& (I1BIT
| K0BIT
)) {
2655 get_border_color(tObj
, img
, t1
);
2658 img
->FetchTexelf(img
, i1
, array
, 0, t1
);
2661 /* bilinear interpolation of samples */
2662 lerp_rgba(rgba
, a
, t0
, t1
);
2667 sample_1d_array_nearest_mipmap_nearest(GLcontext
*ctx
,
2668 const struct gl_texture_object
*tObj
,
2669 GLuint n
, const GLfloat texcoord
[][4],
2670 const GLfloat lambda
[], GLfloat rgba
[][4])
2673 for (i
= 0; i
< n
; i
++) {
2674 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2675 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2682 sample_1d_array_linear_mipmap_nearest(GLcontext
*ctx
,
2683 const struct gl_texture_object
*tObj
,
2684 GLuint n
, const GLfloat texcoord
[][4],
2685 const GLfloat lambda
[], GLfloat rgba
[][4])
2688 ASSERT(lambda
!= NULL
);
2689 for (i
= 0; i
< n
; i
++) {
2690 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2691 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2692 texcoord
[i
], rgba
[i
]);
2698 sample_1d_array_nearest_mipmap_linear(GLcontext
*ctx
,
2699 const struct gl_texture_object
*tObj
,
2700 GLuint n
, const GLfloat texcoord
[][4],
2701 const GLfloat lambda
[], GLfloat rgba
[][4])
2704 ASSERT(lambda
!= NULL
);
2705 for (i
= 0; i
< n
; i
++) {
2706 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2707 if (level
>= tObj
->_MaxLevel
) {
2708 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2709 texcoord
[i
], rgba
[i
]);
2712 GLfloat t0
[4], t1
[4]; /* texels */
2713 const GLfloat f
= FRAC(lambda
[i
]);
2714 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2715 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2716 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2723 sample_1d_array_linear_mipmap_linear(GLcontext
*ctx
,
2724 const struct gl_texture_object
*tObj
,
2725 GLuint n
, const GLfloat texcoord
[][4],
2726 const GLfloat lambda
[], GLfloat rgba
[][4])
2729 ASSERT(lambda
!= NULL
);
2730 for (i
= 0; i
< n
; i
++) {
2731 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2732 if (level
>= tObj
->_MaxLevel
) {
2733 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2734 texcoord
[i
], rgba
[i
]);
2737 GLfloat t0
[4], t1
[4]; /* texels */
2738 const GLfloat f
= FRAC(lambda
[i
]);
2739 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2740 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2741 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2747 /** Sample 1D Array texture, nearest filtering for both min/magnification */
2749 sample_nearest_1d_array(GLcontext
*ctx
,
2750 const struct gl_texture_object
*tObj
, GLuint n
,
2751 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2755 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2757 for (i
= 0; i
< n
; i
++) {
2758 sample_1d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2763 /** Sample 1D Array texture, linear filtering for both min/magnification */
2765 sample_linear_1d_array(GLcontext
*ctx
,
2766 const struct gl_texture_object
*tObj
, GLuint n
,
2767 const GLfloat texcoords
[][4],
2768 const GLfloat lambda
[], GLfloat rgba
[][4])
2771 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2773 for (i
= 0; i
< n
; i
++) {
2774 sample_1d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2779 /** Sample 1D Array texture, using lambda to choose between min/magnification */
2781 sample_lambda_1d_array(GLcontext
*ctx
,
2782 const struct gl_texture_object
*tObj
, GLuint n
,
2783 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2786 GLuint minStart
, minEnd
; /* texels with minification */
2787 GLuint magStart
, magEnd
; /* texels with magnification */
2790 ASSERT(lambda
!= NULL
);
2791 compute_min_mag_ranges(tObj
, n
, lambda
,
2792 &minStart
, &minEnd
, &magStart
, &magEnd
);
2794 if (minStart
< minEnd
) {
2795 /* do the minified texels */
2796 GLuint m
= minEnd
- minStart
;
2797 switch (tObj
->MinFilter
) {
2799 for (i
= minStart
; i
< minEnd
; i
++)
2800 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2801 texcoords
[i
], rgba
[i
]);
2804 for (i
= minStart
; i
< minEnd
; i
++)
2805 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2806 texcoords
[i
], rgba
[i
]);
2808 case GL_NEAREST_MIPMAP_NEAREST
:
2809 sample_1d_array_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2810 lambda
+ minStart
, rgba
+ minStart
);
2812 case GL_LINEAR_MIPMAP_NEAREST
:
2813 sample_1d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2814 texcoords
+ minStart
,
2818 case GL_NEAREST_MIPMAP_LINEAR
:
2819 sample_1d_array_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2820 lambda
+ minStart
, rgba
+ minStart
);
2822 case GL_LINEAR_MIPMAP_LINEAR
:
2823 sample_1d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2824 texcoords
+ minStart
,
2829 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
2834 if (magStart
< magEnd
) {
2835 /* do the magnified texels */
2836 switch (tObj
->MagFilter
) {
2838 for (i
= magStart
; i
< magEnd
; i
++)
2839 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2840 texcoords
[i
], rgba
[i
]);
2843 for (i
= magStart
; i
< magEnd
; i
++)
2844 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2845 texcoords
[i
], rgba
[i
]);
2848 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
2856 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
2858 static INLINE GLfloat
2859 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
,
2864 return (coord
<= depthSample
) ? 1.0F
: ambient
;
2866 return (coord
>= depthSample
) ? 1.0F
: ambient
;
2868 return (coord
< depthSample
) ? 1.0F
: ambient
;
2870 return (coord
> depthSample
) ? 1.0F
: ambient
;
2872 return (coord
== depthSample
) ? 1.0F
: ambient
;
2874 return (coord
!= depthSample
) ? 1.0F
: ambient
;
2882 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
2889 * Compare texcoord against four depth samples.
2891 static INLINE GLfloat
2892 shadow_compare4(GLenum function
, GLfloat coord
,
2893 GLfloat depth00
, GLfloat depth01
,
2894 GLfloat depth10
, GLfloat depth11
,
2895 GLfloat ambient
, GLfloat wi
, GLfloat wj
)
2897 const GLfloat d
= (1.0F
- (GLfloat
) ambient
) * 0.25F
;
2898 GLfloat luminance
= 1.0F
;
2902 if (depth00
<= coord
) luminance
-= d
;
2903 if (depth01
<= coord
) luminance
-= d
;
2904 if (depth10
<= coord
) luminance
-= d
;
2905 if (depth11
<= coord
) luminance
-= d
;
2908 if (depth00
>= coord
) luminance
-= d
;
2909 if (depth01
>= coord
) luminance
-= d
;
2910 if (depth10
>= coord
) luminance
-= d
;
2911 if (depth11
>= coord
) luminance
-= d
;
2914 if (depth00
< coord
) luminance
-= d
;
2915 if (depth01
< coord
) luminance
-= d
;
2916 if (depth10
< coord
) luminance
-= d
;
2917 if (depth11
< coord
) luminance
-= d
;
2920 if (depth00
> coord
) luminance
-= d
;
2921 if (depth01
> coord
) luminance
-= d
;
2922 if (depth10
> coord
) luminance
-= d
;
2923 if (depth11
> coord
) luminance
-= d
;
2926 if (depth00
== coord
) luminance
-= d
;
2927 if (depth01
== coord
) luminance
-= d
;
2928 if (depth10
== coord
) luminance
-= d
;
2929 if (depth11
== coord
) luminance
-= d
;
2932 if (depth00
!= coord
) luminance
-= d
;
2933 if (depth01
!= coord
) luminance
-= d
;
2934 if (depth10
!= coord
) luminance
-= d
;
2935 if (depth11
!= coord
) luminance
-= d
;
2942 /* ordinary bilinear filtering */
2943 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
2945 _mesa_problem(NULL
, "Bad compare func in sample_depth_texture");
2952 * Sample a shadow/depth texture.
2955 sample_depth_texture( GLcontext
*ctx
,
2956 const struct gl_texture_object
*tObj
, GLuint n
,
2957 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2958 GLfloat texel
[][4] )
2960 const GLint baseLevel
= tObj
->BaseLevel
;
2961 const struct gl_texture_image
*img
= tObj
->Image
[0][baseLevel
];
2962 const GLint width
= img
->Width
;
2963 const GLint height
= img
->Height
;
2964 const GLint depth
= img
->Depth
;
2965 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
2973 ASSERT(img
->TexFormat
->BaseFormat
== GL_DEPTH_COMPONENT
||
2974 img
->TexFormat
->BaseFormat
== GL_DEPTH_STENCIL_EXT
);
2976 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
2977 tObj
->Target
== GL_TEXTURE_2D
||
2978 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
2979 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
2980 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
);
2982 ambient
= tObj
->CompareFailValue
;
2984 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
2986 function
= (tObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
2987 tObj
->CompareFunc
: GL_NONE
;
2989 if (tObj
->MagFilter
== GL_NEAREST
) {
2991 for (i
= 0; i
< n
; i
++) {
2992 GLfloat depthSample
;
2993 GLint col
, row
, slice
;
2995 nearest_texcoord(tObj
, texcoords
[i
], &col
, &row
, &slice
);
2997 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
2998 slice
>= 0 && slice
< depth
) {
2999 img
->FetchTexelf(img
, col
, row
, slice
, &depthSample
);
3002 depthSample
= tObj
->BorderColor
[0];
3005 result
= shadow_compare(function
, texcoords
[i
][compare_coord
],
3006 depthSample
, ambient
);
3008 switch (tObj
->DepthMode
) {
3010 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3013 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3016 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3019 _mesa_problem(ctx
, "Bad depth texture mode");
3025 ASSERT(tObj
->MagFilter
== GL_LINEAR
);
3026 for (i
= 0; i
< n
; i
++) {
3027 GLfloat depth00
, depth01
, depth10
, depth11
;
3028 GLint i0
, i1
, j0
, j1
;
3031 GLuint useBorderTexel
;
3033 linear_texcoord(tObj
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3040 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3046 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3047 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3048 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3049 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3052 if (slice
< 0 || slice
>= (GLint
) depth
) {
3053 depth00
= tObj
->BorderColor
[0];
3054 depth01
= tObj
->BorderColor
[0];
3055 depth10
= tObj
->BorderColor
[0];
3056 depth11
= tObj
->BorderColor
[0];
3059 /* get four depth samples from the texture */
3060 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3061 depth00
= tObj
->BorderColor
[0];
3064 img
->FetchTexelf(img
, i0
, j0
, slice
, &depth00
);
3066 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3067 depth10
= tObj
->BorderColor
[0];
3070 img
->FetchTexelf(img
, i1
, j0
, slice
, &depth10
);
3073 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3074 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3075 depth01
= tObj
->BorderColor
[0];
3078 img
->FetchTexelf(img
, i0
, j1
, slice
, &depth01
);
3080 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3081 depth11
= tObj
->BorderColor
[0];
3084 img
->FetchTexelf(img
, i1
, j1
, slice
, &depth11
);
3093 result
= shadow_compare4(function
, texcoords
[i
][compare_coord
],
3094 depth00
, depth01
, depth10
, depth11
,
3097 switch (tObj
->DepthMode
) {
3099 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3102 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3105 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3108 _mesa_problem(ctx
, "Bad depth texture mode");
3117 * We use this function when a texture object is in an "incomplete" state.
3118 * When a fragment program attempts to sample an incomplete texture we
3119 * return black (see issue 23 in GL_ARB_fragment_program spec).
3120 * Note: fragment programs don't observe the texture enable/disable flags.
3123 null_sample_func( GLcontext
*ctx
,
3124 const struct gl_texture_object
*tObj
, GLuint n
,
3125 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3133 for (i
= 0; i
< n
; i
++) {
3137 rgba
[i
][ACOMP
] = CHAN_MAX
;
3143 * Choose the texture sampling function for the given texture object.
3146 _swrast_choose_texture_sample_func( GLcontext
*ctx
,
3147 const struct gl_texture_object
*t
)
3149 if (!t
|| !t
->_Complete
) {
3150 return &null_sample_func
;
3153 const GLboolean needLambda
= (GLboolean
) (t
->MinFilter
!= t
->MagFilter
);
3154 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->TexFormat
->BaseFormat
;
3156 switch (t
->Target
) {
3158 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3159 return &sample_depth_texture
;
3161 else if (needLambda
) {
3162 return &sample_lambda_1d
;
3164 else if (t
->MinFilter
== GL_LINEAR
) {
3165 return &sample_linear_1d
;
3168 ASSERT(t
->MinFilter
== GL_NEAREST
);
3169 return &sample_nearest_1d
;
3172 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3173 return &sample_depth_texture
;
3175 else if (needLambda
) {
3176 return &sample_lambda_2d
;
3178 else if (t
->MinFilter
== GL_LINEAR
) {
3179 return &sample_linear_2d
;
3182 /* check for a few optimized cases */
3184 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3185 ASSERT(t
->MinFilter
== GL_NEAREST
);
3186 if (t
->WrapS
== GL_REPEAT
&&
3187 t
->WrapT
== GL_REPEAT
&&
3188 img
->_IsPowerOfTwo
&&
3190 img
->TexFormat
->MesaFormat
== MESA_FORMAT_RGB
) {
3191 return &opt_sample_rgb_2d
;
3193 else if (t
->WrapS
== GL_REPEAT
&&
3194 t
->WrapT
== GL_REPEAT
&&
3195 img
->_IsPowerOfTwo
&&
3197 img
->TexFormat
->MesaFormat
== MESA_FORMAT_RGBA
) {
3198 return &opt_sample_rgba_2d
;
3205 return &sample_nearest_2d
;
3210 return &sample_lambda_3d
;
3212 else if (t
->MinFilter
== GL_LINEAR
) {
3213 return &sample_linear_3d
;
3216 ASSERT(t
->MinFilter
== GL_NEAREST
);
3217 return &sample_nearest_3d
;
3219 case GL_TEXTURE_CUBE_MAP
:
3221 return &sample_lambda_cube
;
3223 else if (t
->MinFilter
== GL_LINEAR
) {
3224 return &sample_linear_cube
;
3227 ASSERT(t
->MinFilter
== GL_NEAREST
);
3228 return &sample_nearest_cube
;
3230 case GL_TEXTURE_RECTANGLE_NV
:
3231 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3232 return &sample_depth_texture
;
3234 else if (needLambda
) {
3235 return &sample_lambda_rect
;
3237 else if (t
->MinFilter
== GL_LINEAR
) {
3238 return &sample_linear_rect
;
3241 ASSERT(t
->MinFilter
== GL_NEAREST
);
3242 return &sample_nearest_rect
;
3244 case GL_TEXTURE_1D_ARRAY_EXT
:
3246 return &sample_lambda_1d_array
;
3248 else if (t
->MinFilter
== GL_LINEAR
) {
3249 return &sample_linear_1d_array
;
3252 ASSERT(t
->MinFilter
== GL_NEAREST
);
3253 return &sample_nearest_1d_array
;
3255 case GL_TEXTURE_2D_ARRAY_EXT
:
3257 return &sample_lambda_2d_array
;
3259 else if (t
->MinFilter
== GL_LINEAR
) {
3260 return &sample_linear_2d_array
;
3263 ASSERT(t
->MinFilter
== GL_NEAREST
);
3264 return &sample_nearest_2d_array
;
3268 "invalid target in _swrast_choose_texture_sample_func");
3269 return &null_sample_func
;