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
[], 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
) {
1908 newCoord
[0] = ( sc
/ ma
+ 1.0F
) * 0.5F
;
1909 newCoord
[1] = ( tc
/ ma
+ 1.0F
) * 0.5F
;
1910 return (const struct gl_texture_image
**) texObj
->Image
[face
];
1915 sample_nearest_cube(GLcontext
*ctx
,
1916 const struct gl_texture_object
*tObj
, GLuint n
,
1917 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1922 for (i
= 0; i
< n
; i
++) {
1923 const struct gl_texture_image
**images
;
1924 GLfloat newCoord
[4];
1925 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1926 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
1933 sample_linear_cube(GLcontext
*ctx
,
1934 const struct gl_texture_object
*tObj
, GLuint n
,
1935 const GLfloat texcoords
[][4],
1936 const GLfloat lambda
[], GLfloat rgba
[][4])
1940 for (i
= 0; i
< n
; i
++) {
1941 const struct gl_texture_image
**images
;
1942 GLfloat newCoord
[4];
1943 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1944 sample_2d_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
1951 sample_cube_nearest_mipmap_nearest(GLcontext
*ctx
,
1952 const struct gl_texture_object
*tObj
,
1953 GLuint n
, const GLfloat texcoord
[][4],
1954 const GLfloat lambda
[], GLfloat rgba
[][4])
1957 ASSERT(lambda
!= NULL
);
1958 for (i
= 0; i
< n
; i
++) {
1959 const struct gl_texture_image
**images
;
1960 GLfloat newCoord
[4];
1962 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1964 /* XXX we actually need to recompute lambda here based on the newCoords.
1965 * But we would need the texcoords of adjacent fragments to compute that
1966 * properly, and we don't have those here.
1967 * For now, do an approximation: subtracting 1 from the chosen mipmap
1968 * level seems to work in some test cases.
1969 * The same adjustment is done in the next few functions.
1971 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1972 level
= MAX2(level
- 1, 0);
1974 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
1980 sample_cube_linear_mipmap_nearest(GLcontext
*ctx
,
1981 const struct gl_texture_object
*tObj
,
1982 GLuint n
, const GLfloat texcoord
[][4],
1983 const GLfloat lambda
[], GLfloat rgba
[][4])
1986 ASSERT(lambda
!= NULL
);
1987 for (i
= 0; i
< n
; i
++) {
1988 const struct gl_texture_image
**images
;
1989 GLfloat newCoord
[4];
1990 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1991 level
= MAX2(level
- 1, 0); /* see comment above */
1992 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1993 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
1999 sample_cube_nearest_mipmap_linear(GLcontext
*ctx
,
2000 const struct gl_texture_object
*tObj
,
2001 GLuint n
, const GLfloat texcoord
[][4],
2002 const GLfloat lambda
[], GLfloat rgba
[][4])
2005 ASSERT(lambda
!= NULL
);
2006 for (i
= 0; i
< n
; i
++) {
2007 const struct gl_texture_image
**images
;
2008 GLfloat newCoord
[4];
2009 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2010 level
= MAX2(level
- 1, 0); /* see comment above */
2011 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2012 if (level
>= tObj
->_MaxLevel
) {
2013 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2017 GLfloat t0
[4], t1
[4]; /* texels */
2018 const GLfloat f
= FRAC(lambda
[i
]);
2019 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
2020 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2021 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2028 sample_cube_linear_mipmap_linear(GLcontext
*ctx
,
2029 const struct gl_texture_object
*tObj
,
2030 GLuint n
, const GLfloat texcoord
[][4],
2031 const GLfloat lambda
[], GLfloat rgba
[][4])
2034 ASSERT(lambda
!= NULL
);
2035 for (i
= 0; i
< n
; i
++) {
2036 const struct gl_texture_image
**images
;
2037 GLfloat newCoord
[4];
2038 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2039 level
= MAX2(level
- 1, 0); /* see comment above */
2040 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2041 if (level
>= tObj
->_MaxLevel
) {
2042 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2046 GLfloat t0
[4], t1
[4];
2047 const GLfloat f
= FRAC(lambda
[i
]);
2048 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
2049 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2050 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2056 /** Sample cube texture, using lambda to choose between min/magnification */
2058 sample_lambda_cube(GLcontext
*ctx
,
2059 const struct gl_texture_object
*tObj
, GLuint n
,
2060 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2063 GLuint minStart
, minEnd
; /* texels with minification */
2064 GLuint magStart
, magEnd
; /* texels with magnification */
2066 ASSERT(lambda
!= NULL
);
2067 compute_min_mag_ranges(tObj
, n
, lambda
,
2068 &minStart
, &minEnd
, &magStart
, &magEnd
);
2070 if (minStart
< minEnd
) {
2071 /* do the minified texels */
2072 const GLuint m
= minEnd
- minStart
;
2073 switch (tObj
->MinFilter
) {
2075 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2076 lambda
+ minStart
, rgba
+ minStart
);
2079 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2080 lambda
+ minStart
, rgba
+ minStart
);
2082 case GL_NEAREST_MIPMAP_NEAREST
:
2083 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
2084 texcoords
+ minStart
,
2085 lambda
+ minStart
, rgba
+ minStart
);
2087 case GL_LINEAR_MIPMAP_NEAREST
:
2088 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
2089 texcoords
+ minStart
,
2090 lambda
+ minStart
, rgba
+ minStart
);
2092 case GL_NEAREST_MIPMAP_LINEAR
:
2093 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
2094 texcoords
+ minStart
,
2095 lambda
+ minStart
, rgba
+ minStart
);
2097 case GL_LINEAR_MIPMAP_LINEAR
:
2098 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
2099 texcoords
+ minStart
,
2100 lambda
+ minStart
, rgba
+ minStart
);
2103 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2107 if (magStart
< magEnd
) {
2108 /* do the magnified texels */
2109 const GLuint m
= magEnd
- magStart
;
2110 switch (tObj
->MagFilter
) {
2112 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2113 lambda
+ magStart
, rgba
+ magStart
);
2116 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2117 lambda
+ magStart
, rgba
+ magStart
);
2120 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2126 /**********************************************************************/
2127 /* Texture Rectangle Sampling Functions */
2128 /**********************************************************************/
2132 sample_nearest_rect(GLcontext
*ctx
,
2133 const struct gl_texture_object
*tObj
, GLuint n
,
2134 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2137 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2138 const GLint width
= img
->Width
;
2139 const GLint height
= img
->Height
;
2145 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2146 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2147 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2148 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2149 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2150 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2151 ASSERT(img
->TexFormat
->BaseFormat
!= GL_COLOR_INDEX
);
2153 for (i
= 0; i
< n
; i
++) {
2155 col
= clamp_rect_coord_nearest(tObj
->WrapS
, texcoords
[i
][0], width
);
2156 row
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoords
[i
][1], height
);
2157 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2158 get_border_color(tObj
, img
, rgba
[i
]);
2160 img
->FetchTexelf(img
, col
, row
, 0, rgba
[i
]);
2166 sample_linear_rect(GLcontext
*ctx
,
2167 const struct gl_texture_object
*tObj
, GLuint n
,
2168 const GLfloat texcoords
[][4],
2169 const GLfloat lambda
[], GLfloat rgba
[][4])
2171 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2172 const GLint width
= img
->Width
;
2173 const GLint height
= img
->Height
;
2179 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2180 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2181 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2182 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2183 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2184 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2185 ASSERT(img
->TexFormat
->BaseFormat
!= GL_COLOR_INDEX
);
2187 for (i
= 0; i
< n
; i
++) {
2188 GLint i0
, j0
, i1
, j1
;
2189 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2191 GLbitfield useBorderColor
= 0x0;
2193 clamp_rect_coord_linear(tObj
->WrapS
, texcoords
[i
][0], width
,
2195 clamp_rect_coord_linear(tObj
->WrapT
, texcoords
[i
][1], height
,
2198 /* compute integer rows/columns */
2199 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2200 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2201 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2202 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2204 /* get four texel samples */
2205 if (useBorderColor
& (I0BIT
| J0BIT
))
2206 get_border_color(tObj
, img
, t00
);
2208 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
2210 if (useBorderColor
& (I1BIT
| J0BIT
))
2211 get_border_color(tObj
, img
, t10
);
2213 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
2215 if (useBorderColor
& (I0BIT
| J1BIT
))
2216 get_border_color(tObj
, img
, t01
);
2218 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
2220 if (useBorderColor
& (I1BIT
| J1BIT
))
2221 get_border_color(tObj
, img
, t11
);
2223 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
2225 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2230 /** Sample Rect texture, using lambda to choose between min/magnification */
2232 sample_lambda_rect(GLcontext
*ctx
,
2233 const struct gl_texture_object
*tObj
, GLuint n
,
2234 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2237 GLuint minStart
, minEnd
, magStart
, magEnd
;
2239 /* We only need lambda to decide between minification and magnification.
2240 * There is no mipmapping with rectangular textures.
2242 compute_min_mag_ranges(tObj
, n
, lambda
,
2243 &minStart
, &minEnd
, &magStart
, &magEnd
);
2245 if (minStart
< minEnd
) {
2246 if (tObj
->MinFilter
== GL_NEAREST
) {
2247 sample_nearest_rect(ctx
, tObj
, minEnd
- minStart
,
2248 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2251 sample_linear_rect(ctx
, tObj
, minEnd
- minStart
,
2252 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2255 if (magStart
< magEnd
) {
2256 if (tObj
->MagFilter
== GL_NEAREST
) {
2257 sample_nearest_rect(ctx
, tObj
, magEnd
- magStart
,
2258 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2261 sample_linear_rect(ctx
, tObj
, magEnd
- magStart
,
2262 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2269 /**********************************************************************/
2270 /* 2D Texture Array Sampling Functions */
2271 /**********************************************************************/
2274 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2277 sample_2d_array_nearest(GLcontext
*ctx
,
2278 const struct gl_texture_object
*tObj
,
2279 const struct gl_texture_image
*img
,
2280 const GLfloat texcoord
[4],
2283 const GLint width
= img
->Width2
; /* without border, power of two */
2284 const GLint height
= img
->Height2
; /* without border, power of two */
2285 const GLint depth
= img
->Depth
;
2290 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2291 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
2292 array
= clamp_rect_coord_nearest(tObj
->WrapR
, texcoord
[2], depth
);
2294 if (i
< 0 || i
>= (GLint
) img
->Width
||
2295 j
< 0 || j
>= (GLint
) img
->Height
||
2296 array
< 0 || array
>= (GLint
) img
->Depth
) {
2297 /* Need this test for GL_CLAMP_TO_BORDER mode */
2298 get_border_color(tObj
, img
, rgba
);
2301 img
->FetchTexelf(img
, i
, j
, array
, rgba
);
2307 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2310 sample_2d_array_linear(GLcontext
*ctx
,
2311 const struct gl_texture_object
*tObj
,
2312 const struct gl_texture_image
*img
,
2313 const GLfloat texcoord
[4],
2316 const GLint width
= img
->Width2
;
2317 const GLint height
= img
->Height2
;
2318 const GLint depth
= img
->Depth
;
2319 GLint i0
, j0
, i1
, j1
;
2321 GLbitfield useBorderColor
= 0x0;
2323 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2325 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2326 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2327 array
= clamp_rect_coord_nearest(tObj
->WrapR
, texcoord
[2], depth
);
2329 if (array
< 0 || array
>= depth
) {
2330 COPY_4V(rgba
, tObj
->BorderColor
);
2340 /* check if sampling texture border color */
2341 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2342 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2343 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2344 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2348 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2349 get_border_color(tObj
, img
, t00
);
2352 img
->FetchTexelf(img
, i0
, j0
, array
, t00
);
2354 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2355 get_border_color(tObj
, img
, t10
);
2358 img
->FetchTexelf(img
, i1
, j0
, array
, t10
);
2360 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2361 get_border_color(tObj
, img
, t01
);
2364 img
->FetchTexelf(img
, i0
, j1
, array
, t01
);
2366 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2367 get_border_color(tObj
, img
, t11
);
2370 img
->FetchTexelf(img
, i1
, j1
, array
, t11
);
2373 /* trilinear interpolation of samples */
2374 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2380 sample_2d_array_nearest_mipmap_nearest(GLcontext
*ctx
,
2381 const struct gl_texture_object
*tObj
,
2382 GLuint n
, const GLfloat texcoord
[][4],
2383 const GLfloat lambda
[], GLfloat rgba
[][4])
2386 for (i
= 0; i
< n
; i
++) {
2387 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2388 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2395 sample_2d_array_linear_mipmap_nearest(GLcontext
*ctx
,
2396 const struct gl_texture_object
*tObj
,
2397 GLuint n
, const GLfloat texcoord
[][4],
2398 const GLfloat lambda
[], GLfloat rgba
[][4])
2401 ASSERT(lambda
!= NULL
);
2402 for (i
= 0; i
< n
; i
++) {
2403 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2404 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2405 texcoord
[i
], rgba
[i
]);
2411 sample_2d_array_nearest_mipmap_linear(GLcontext
*ctx
,
2412 const struct gl_texture_object
*tObj
,
2413 GLuint n
, const GLfloat texcoord
[][4],
2414 const GLfloat lambda
[], GLfloat rgba
[][4])
2417 ASSERT(lambda
!= NULL
);
2418 for (i
= 0; i
< n
; i
++) {
2419 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2420 if (level
>= tObj
->_MaxLevel
) {
2421 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2422 texcoord
[i
], rgba
[i
]);
2425 GLfloat t0
[4], t1
[4]; /* texels */
2426 const GLfloat f
= FRAC(lambda
[i
]);
2427 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
],
2429 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1],
2431 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2438 sample_2d_array_linear_mipmap_linear(GLcontext
*ctx
,
2439 const struct gl_texture_object
*tObj
,
2440 GLuint n
, const GLfloat texcoord
[][4],
2441 const GLfloat lambda
[], GLfloat rgba
[][4])
2444 ASSERT(lambda
!= NULL
);
2445 for (i
= 0; i
< n
; i
++) {
2446 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2447 if (level
>= tObj
->_MaxLevel
) {
2448 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2449 texcoord
[i
], rgba
[i
]);
2452 GLfloat t0
[4], t1
[4]; /* texels */
2453 const GLfloat f
= FRAC(lambda
[i
]);
2454 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2456 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1],
2458 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2464 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2466 sample_nearest_2d_array(GLcontext
*ctx
,
2467 const struct gl_texture_object
*tObj
, GLuint n
,
2468 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2472 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2474 for (i
= 0; i
< n
; i
++) {
2475 sample_2d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2481 /** Sample 2D Array texture, linear filtering for both min/magnification */
2483 sample_linear_2d_array(GLcontext
*ctx
,
2484 const struct gl_texture_object
*tObj
, GLuint n
,
2485 const GLfloat texcoords
[][4],
2486 const GLfloat lambda
[], GLfloat rgba
[][4])
2489 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2491 for (i
= 0; i
< n
; i
++) {
2492 sample_2d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2497 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2499 sample_lambda_2d_array(GLcontext
*ctx
,
2500 const struct gl_texture_object
*tObj
, GLuint n
,
2501 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2504 GLuint minStart
, minEnd
; /* texels with minification */
2505 GLuint magStart
, magEnd
; /* texels with magnification */
2508 ASSERT(lambda
!= NULL
);
2509 compute_min_mag_ranges(tObj
, n
, lambda
,
2510 &minStart
, &minEnd
, &magStart
, &magEnd
);
2512 if (minStart
< minEnd
) {
2513 /* do the minified texels */
2514 GLuint m
= minEnd
- minStart
;
2515 switch (tObj
->MinFilter
) {
2517 for (i
= minStart
; i
< minEnd
; i
++)
2518 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2519 texcoords
[i
], rgba
[i
]);
2522 for (i
= minStart
; i
< minEnd
; i
++)
2523 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2524 texcoords
[i
], rgba
[i
]);
2526 case GL_NEAREST_MIPMAP_NEAREST
:
2527 sample_2d_array_nearest_mipmap_nearest(ctx
, tObj
, m
,
2528 texcoords
+ minStart
,
2532 case GL_LINEAR_MIPMAP_NEAREST
:
2533 sample_2d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2534 texcoords
+ minStart
,
2538 case GL_NEAREST_MIPMAP_LINEAR
:
2539 sample_2d_array_nearest_mipmap_linear(ctx
, tObj
, m
,
2540 texcoords
+ minStart
,
2544 case GL_LINEAR_MIPMAP_LINEAR
:
2545 sample_2d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2546 texcoords
+ minStart
,
2551 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
2556 if (magStart
< magEnd
) {
2557 /* do the magnified texels */
2558 switch (tObj
->MagFilter
) {
2560 for (i
= magStart
; i
< magEnd
; i
++)
2561 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2562 texcoords
[i
], rgba
[i
]);
2565 for (i
= magStart
; i
< magEnd
; i
++)
2566 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2567 texcoords
[i
], rgba
[i
]);
2570 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
2579 /**********************************************************************/
2580 /* 1D Texture Array Sampling Functions */
2581 /**********************************************************************/
2584 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2587 sample_1d_array_nearest(GLcontext
*ctx
,
2588 const struct gl_texture_object
*tObj
,
2589 const struct gl_texture_image
*img
,
2590 const GLfloat texcoord
[4],
2593 const GLint width
= img
->Width2
; /* without border, power of two */
2594 const GLint height
= img
->Height
;
2599 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2600 array
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoord
[1], height
);
2602 if (i
< 0 || i
>= (GLint
) img
->Width
||
2603 array
< 0 || array
>= (GLint
) img
->Height
) {
2604 /* Need this test for GL_CLAMP_TO_BORDER mode */
2605 get_border_color(tObj
, img
, rgba
);
2608 img
->FetchTexelf(img
, i
, array
, 0, rgba
);
2614 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2617 sample_1d_array_linear(GLcontext
*ctx
,
2618 const struct gl_texture_object
*tObj
,
2619 const struct gl_texture_image
*img
,
2620 const GLfloat texcoord
[4],
2623 const GLint width
= img
->Width2
;
2624 const GLint height
= img
->Height
;
2627 GLbitfield useBorderColor
= 0x0;
2629 GLfloat t0
[4], t1
[4];
2631 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2632 array
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoord
[1], height
);
2639 /* check if sampling texture border color */
2640 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2641 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2644 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
2647 if (useBorderColor
& (I0BIT
| K0BIT
)) {
2648 get_border_color(tObj
, img
, t0
);
2651 img
->FetchTexelf(img
, i0
, array
, 0, t0
);
2653 if (useBorderColor
& (I1BIT
| K0BIT
)) {
2654 get_border_color(tObj
, img
, t1
);
2657 img
->FetchTexelf(img
, i1
, array
, 0, t1
);
2660 /* bilinear interpolation of samples */
2661 lerp_rgba(rgba
, a
, t0
, t1
);
2666 sample_1d_array_nearest_mipmap_nearest(GLcontext
*ctx
,
2667 const struct gl_texture_object
*tObj
,
2668 GLuint n
, const GLfloat texcoord
[][4],
2669 const GLfloat lambda
[], GLfloat rgba
[][4])
2672 for (i
= 0; i
< n
; i
++) {
2673 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2674 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2681 sample_1d_array_linear_mipmap_nearest(GLcontext
*ctx
,
2682 const struct gl_texture_object
*tObj
,
2683 GLuint n
, const GLfloat texcoord
[][4],
2684 const GLfloat lambda
[], GLfloat rgba
[][4])
2687 ASSERT(lambda
!= NULL
);
2688 for (i
= 0; i
< n
; i
++) {
2689 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2690 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2691 texcoord
[i
], rgba
[i
]);
2697 sample_1d_array_nearest_mipmap_linear(GLcontext
*ctx
,
2698 const struct gl_texture_object
*tObj
,
2699 GLuint n
, const GLfloat texcoord
[][4],
2700 const GLfloat lambda
[], GLfloat rgba
[][4])
2703 ASSERT(lambda
!= NULL
);
2704 for (i
= 0; i
< n
; i
++) {
2705 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2706 if (level
>= tObj
->_MaxLevel
) {
2707 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2708 texcoord
[i
], rgba
[i
]);
2711 GLfloat t0
[4], t1
[4]; /* texels */
2712 const GLfloat f
= FRAC(lambda
[i
]);
2713 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2714 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2715 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2722 sample_1d_array_linear_mipmap_linear(GLcontext
*ctx
,
2723 const struct gl_texture_object
*tObj
,
2724 GLuint n
, const GLfloat texcoord
[][4],
2725 const GLfloat lambda
[], GLfloat rgba
[][4])
2728 ASSERT(lambda
!= NULL
);
2729 for (i
= 0; i
< n
; i
++) {
2730 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2731 if (level
>= tObj
->_MaxLevel
) {
2732 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2733 texcoord
[i
], rgba
[i
]);
2736 GLfloat t0
[4], t1
[4]; /* texels */
2737 const GLfloat f
= FRAC(lambda
[i
]);
2738 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2739 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2740 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2746 /** Sample 1D Array texture, nearest filtering for both min/magnification */
2748 sample_nearest_1d_array(GLcontext
*ctx
,
2749 const struct gl_texture_object
*tObj
, GLuint n
,
2750 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2754 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2756 for (i
= 0; i
< n
; i
++) {
2757 sample_1d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2762 /** Sample 1D Array texture, linear filtering for both min/magnification */
2764 sample_linear_1d_array(GLcontext
*ctx
,
2765 const struct gl_texture_object
*tObj
, GLuint n
,
2766 const GLfloat texcoords
[][4],
2767 const GLfloat lambda
[], GLfloat rgba
[][4])
2770 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2772 for (i
= 0; i
< n
; i
++) {
2773 sample_1d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2778 /** Sample 1D Array texture, using lambda to choose between min/magnification */
2780 sample_lambda_1d_array(GLcontext
*ctx
,
2781 const struct gl_texture_object
*tObj
, GLuint n
,
2782 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2785 GLuint minStart
, minEnd
; /* texels with minification */
2786 GLuint magStart
, magEnd
; /* texels with magnification */
2789 ASSERT(lambda
!= NULL
);
2790 compute_min_mag_ranges(tObj
, n
, lambda
,
2791 &minStart
, &minEnd
, &magStart
, &magEnd
);
2793 if (minStart
< minEnd
) {
2794 /* do the minified texels */
2795 GLuint m
= minEnd
- minStart
;
2796 switch (tObj
->MinFilter
) {
2798 for (i
= minStart
; i
< minEnd
; i
++)
2799 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2800 texcoords
[i
], rgba
[i
]);
2803 for (i
= minStart
; i
< minEnd
; i
++)
2804 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2805 texcoords
[i
], rgba
[i
]);
2807 case GL_NEAREST_MIPMAP_NEAREST
:
2808 sample_1d_array_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2809 lambda
+ minStart
, rgba
+ minStart
);
2811 case GL_LINEAR_MIPMAP_NEAREST
:
2812 sample_1d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2813 texcoords
+ minStart
,
2817 case GL_NEAREST_MIPMAP_LINEAR
:
2818 sample_1d_array_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2819 lambda
+ minStart
, rgba
+ minStart
);
2821 case GL_LINEAR_MIPMAP_LINEAR
:
2822 sample_1d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2823 texcoords
+ minStart
,
2828 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
2833 if (magStart
< magEnd
) {
2834 /* do the magnified texels */
2835 switch (tObj
->MagFilter
) {
2837 for (i
= magStart
; i
< magEnd
; i
++)
2838 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2839 texcoords
[i
], rgba
[i
]);
2842 for (i
= magStart
; i
< magEnd
; i
++)
2843 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2844 texcoords
[i
], rgba
[i
]);
2847 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
2855 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
2857 static INLINE GLfloat
2858 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
,
2863 return (coord
<= depthSample
) ? 1.0F
: ambient
;
2865 return (coord
>= depthSample
) ? 1.0F
: ambient
;
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
;
2881 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
2888 * Compare texcoord against four depth samples.
2890 static INLINE GLfloat
2891 shadow_compare4(GLenum function
, GLfloat coord
,
2892 GLfloat depth00
, GLfloat depth01
,
2893 GLfloat depth10
, GLfloat depth11
,
2894 GLfloat ambient
, GLfloat wi
, GLfloat wj
)
2896 const GLfloat d
= (1.0F
- (GLfloat
) ambient
) * 0.25F
;
2897 GLfloat luminance
= 1.0F
;
2901 if (depth00
<= coord
) luminance
-= d
;
2902 if (depth01
<= coord
) luminance
-= d
;
2903 if (depth10
<= coord
) luminance
-= d
;
2904 if (depth11
<= coord
) luminance
-= d
;
2907 if (depth00
>= coord
) luminance
-= d
;
2908 if (depth01
>= coord
) luminance
-= d
;
2909 if (depth10
>= coord
) luminance
-= d
;
2910 if (depth11
>= coord
) luminance
-= d
;
2913 if (depth00
< coord
) luminance
-= d
;
2914 if (depth01
< coord
) luminance
-= d
;
2915 if (depth10
< coord
) luminance
-= d
;
2916 if (depth11
< coord
) luminance
-= d
;
2919 if (depth00
> coord
) luminance
-= d
;
2920 if (depth01
> coord
) luminance
-= d
;
2921 if (depth10
> coord
) luminance
-= d
;
2922 if (depth11
> coord
) luminance
-= d
;
2925 if (depth00
== coord
) luminance
-= d
;
2926 if (depth01
== coord
) luminance
-= d
;
2927 if (depth10
== coord
) luminance
-= d
;
2928 if (depth11
== coord
) luminance
-= d
;
2931 if (depth00
!= coord
) luminance
-= d
;
2932 if (depth01
!= coord
) luminance
-= d
;
2933 if (depth10
!= coord
) luminance
-= d
;
2934 if (depth11
!= coord
) luminance
-= d
;
2941 /* ordinary bilinear filtering */
2942 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
2944 _mesa_problem(NULL
, "Bad compare func in sample_depth_texture");
2951 * Sample a shadow/depth texture.
2954 sample_depth_texture( GLcontext
*ctx
,
2955 const struct gl_texture_object
*tObj
, GLuint n
,
2956 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2957 GLfloat texel
[][4] )
2959 const GLint baseLevel
= tObj
->BaseLevel
;
2960 const struct gl_texture_image
*img
= tObj
->Image
[0][baseLevel
];
2961 const GLint width
= img
->Width
;
2962 const GLint height
= img
->Height
;
2963 const GLint depth
= img
->Depth
;
2964 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
2972 ASSERT(img
->TexFormat
->BaseFormat
== GL_DEPTH_COMPONENT
||
2973 img
->TexFormat
->BaseFormat
== GL_DEPTH_STENCIL_EXT
);
2975 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
2976 tObj
->Target
== GL_TEXTURE_2D
||
2977 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
2978 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
2979 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
);
2981 ambient
= tObj
->CompareFailValue
;
2983 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
2985 function
= (tObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
2986 tObj
->CompareFunc
: GL_NONE
;
2988 if (tObj
->MagFilter
== GL_NEAREST
) {
2990 for (i
= 0; i
< n
; i
++) {
2991 GLfloat depthSample
;
2992 GLint col
, row
, slice
;
2994 nearest_texcoord(tObj
, texcoords
[i
], &col
, &row
, &slice
);
2996 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
2997 slice
>= 0 && slice
< depth
) {
2998 img
->FetchTexelf(img
, col
, row
, slice
, &depthSample
);
3001 depthSample
= tObj
->BorderColor
[0];
3004 result
= shadow_compare(function
, texcoords
[i
][compare_coord
],
3005 depthSample
, ambient
);
3007 switch (tObj
->DepthMode
) {
3009 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3012 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3015 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3018 _mesa_problem(ctx
, "Bad depth texture mode");
3024 ASSERT(tObj
->MagFilter
== GL_LINEAR
);
3025 for (i
= 0; i
< n
; i
++) {
3026 GLfloat depth00
, depth01
, depth10
, depth11
;
3027 GLint i0
, i1
, j0
, j1
;
3030 GLuint useBorderTexel
;
3032 linear_texcoord(tObj
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3039 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3045 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3046 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3047 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3048 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3051 if (slice
< 0 || slice
>= (GLint
) depth
) {
3052 depth00
= tObj
->BorderColor
[0];
3053 depth01
= tObj
->BorderColor
[0];
3054 depth10
= tObj
->BorderColor
[0];
3055 depth11
= tObj
->BorderColor
[0];
3058 /* get four depth samples from the texture */
3059 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3060 depth00
= tObj
->BorderColor
[0];
3063 img
->FetchTexelf(img
, i0
, j0
, slice
, &depth00
);
3065 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3066 depth10
= tObj
->BorderColor
[0];
3069 img
->FetchTexelf(img
, i1
, j0
, slice
, &depth10
);
3072 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3073 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3074 depth01
= tObj
->BorderColor
[0];
3077 img
->FetchTexelf(img
, i0
, j1
, slice
, &depth01
);
3079 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3080 depth11
= tObj
->BorderColor
[0];
3083 img
->FetchTexelf(img
, i1
, j1
, slice
, &depth11
);
3092 result
= shadow_compare4(function
, texcoords
[i
][compare_coord
],
3093 depth00
, depth01
, depth10
, depth11
,
3096 switch (tObj
->DepthMode
) {
3098 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3101 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3104 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3107 _mesa_problem(ctx
, "Bad depth texture mode");
3116 * We use this function when a texture object is in an "incomplete" state.
3117 * When a fragment program attempts to sample an incomplete texture we
3118 * return black (see issue 23 in GL_ARB_fragment_program spec).
3119 * Note: fragment programs don't observe the texture enable/disable flags.
3122 null_sample_func( GLcontext
*ctx
,
3123 const struct gl_texture_object
*tObj
, GLuint n
,
3124 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3132 for (i
= 0; i
< n
; i
++) {
3136 rgba
[i
][ACOMP
] = CHAN_MAX
;
3142 * Choose the texture sampling function for the given texture object.
3145 _swrast_choose_texture_sample_func( GLcontext
*ctx
,
3146 const struct gl_texture_object
*t
)
3148 if (!t
|| !t
->_Complete
) {
3149 return &null_sample_func
;
3152 const GLboolean needLambda
= (GLboolean
) (t
->MinFilter
!= t
->MagFilter
);
3153 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->TexFormat
->BaseFormat
;
3155 switch (t
->Target
) {
3157 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3158 return &sample_depth_texture
;
3160 else if (needLambda
) {
3161 return &sample_lambda_1d
;
3163 else if (t
->MinFilter
== GL_LINEAR
) {
3164 return &sample_linear_1d
;
3167 ASSERT(t
->MinFilter
== GL_NEAREST
);
3168 return &sample_nearest_1d
;
3171 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3172 return &sample_depth_texture
;
3174 else if (needLambda
) {
3175 return &sample_lambda_2d
;
3177 else if (t
->MinFilter
== GL_LINEAR
) {
3178 return &sample_linear_2d
;
3181 /* check for a few optimized cases */
3182 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3183 ASSERT(t
->MinFilter
== GL_NEAREST
);
3184 if (t
->WrapS
== GL_REPEAT
&&
3185 t
->WrapT
== GL_REPEAT
&&
3186 img
->_IsPowerOfTwo
&&
3188 img
->TexFormat
->MesaFormat
== MESA_FORMAT_RGB
) {
3189 return &opt_sample_rgb_2d
;
3191 else if (t
->WrapS
== GL_REPEAT
&&
3192 t
->WrapT
== GL_REPEAT
&&
3193 img
->_IsPowerOfTwo
&&
3195 img
->TexFormat
->MesaFormat
== MESA_FORMAT_RGBA
) {
3196 return &opt_sample_rgba_2d
;
3199 return &sample_nearest_2d
;
3204 return &sample_lambda_3d
;
3206 else if (t
->MinFilter
== GL_LINEAR
) {
3207 return &sample_linear_3d
;
3210 ASSERT(t
->MinFilter
== GL_NEAREST
);
3211 return &sample_nearest_3d
;
3213 case GL_TEXTURE_CUBE_MAP
:
3215 return &sample_lambda_cube
;
3217 else if (t
->MinFilter
== GL_LINEAR
) {
3218 return &sample_linear_cube
;
3221 ASSERT(t
->MinFilter
== GL_NEAREST
);
3222 return &sample_nearest_cube
;
3224 case GL_TEXTURE_RECTANGLE_NV
:
3225 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3226 return &sample_depth_texture
;
3228 else if (needLambda
) {
3229 return &sample_lambda_rect
;
3231 else if (t
->MinFilter
== GL_LINEAR
) {
3232 return &sample_linear_rect
;
3235 ASSERT(t
->MinFilter
== GL_NEAREST
);
3236 return &sample_nearest_rect
;
3238 case GL_TEXTURE_1D_ARRAY_EXT
:
3240 return &sample_lambda_1d_array
;
3242 else if (t
->MinFilter
== GL_LINEAR
) {
3243 return &sample_linear_1d_array
;
3246 ASSERT(t
->MinFilter
== GL_NEAREST
);
3247 return &sample_nearest_1d_array
;
3249 case GL_TEXTURE_2D_ARRAY_EXT
:
3251 return &sample_lambda_2d_array
;
3253 else if (t
->MinFilter
== GL_LINEAR
) {
3254 return &sample_linear_2d_array
;
3257 ASSERT(t
->MinFilter
== GL_NEAREST
);
3258 return &sample_nearest_2d_array
;
3262 "invalid target in _swrast_choose_texture_sample_func");
3263 return &null_sample_func
;