2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/colormac.h"
29 #include "main/imports.h"
30 #include "main/texformat.h"
32 #include "s_context.h"
33 #include "s_texfilter.h"
37 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
38 * see 1-pixel bands of improperly weighted linear-filtered textures.
39 * The tests/texwrap.c demo is a good test.
40 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
41 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
43 #define FRAC(f) ((f) - IFLOOR(f))
48 * Linear interpolation macro
50 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
54 * Do 2D/biliner interpolation of float values.
55 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
56 * a and b are the horizontal and vertical interpolants.
57 * It's important that this function is inlined when compiled with
58 * optimization! If we find that's not true on some systems, convert
62 lerp_2d(GLfloat a
, GLfloat b
,
63 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
65 const GLfloat temp0
= LERP(a
, v00
, v10
);
66 const GLfloat temp1
= LERP(a
, v01
, v11
);
67 return LERP(b
, temp0
, temp1
);
72 * Do 3D/trilinear interpolation of float values.
76 lerp_3d(GLfloat a
, GLfloat b
, GLfloat c
,
77 GLfloat v000
, GLfloat v100
, GLfloat v010
, GLfloat v110
,
78 GLfloat v001
, GLfloat v101
, GLfloat v011
, GLfloat v111
)
80 const GLfloat temp00
= LERP(a
, v000
, v100
);
81 const GLfloat temp10
= LERP(a
, v010
, v110
);
82 const GLfloat temp01
= LERP(a
, v001
, v101
);
83 const GLfloat temp11
= LERP(a
, v011
, v111
);
84 const GLfloat temp0
= LERP(b
, temp00
, temp10
);
85 const GLfloat temp1
= LERP(b
, temp01
, temp11
);
86 return LERP(c
, temp0
, temp1
);
91 * Do linear interpolation of colors.
94 lerp_rgba(GLfloat result
[4], GLfloat t
, const GLfloat a
[4], const GLfloat b
[4])
96 result
[0] = LERP(t
, a
[0], b
[0]);
97 result
[1] = LERP(t
, a
[1], b
[1]);
98 result
[2] = LERP(t
, a
[2], b
[2]);
99 result
[3] = LERP(t
, a
[3], b
[3]);
104 * Do bilinear interpolation of colors.
107 lerp_rgba_2d(GLfloat result
[4], GLfloat a
, GLfloat b
,
108 const GLfloat t00
[4], const GLfloat t10
[4],
109 const GLfloat t01
[4], const GLfloat t11
[4])
111 result
[0] = lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]);
112 result
[1] = lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]);
113 result
[2] = lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]);
114 result
[3] = lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]);
119 * Do trilinear interpolation of colors.
122 lerp_rgba_3d(GLfloat result
[4], GLfloat a
, GLfloat b
, GLfloat c
,
123 const GLfloat t000
[4], const GLfloat t100
[4],
124 const GLfloat t010
[4], const GLfloat t110
[4],
125 const GLfloat t001
[4], const GLfloat t101
[4],
126 const GLfloat t011
[4], const GLfloat t111
[4])
129 /* compiler should unroll these short loops */
130 for (k
= 0; k
< 4; k
++) {
131 result
[k
] = lerp_3d(a
, b
, c
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
132 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
138 * If A is a signed integer, A % B doesn't give the right value for A < 0
139 * (in terms of texture repeat). Just casting to unsigned fixes that.
141 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
145 * Used to compute texel locations for linear sampling.
147 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
148 * s = texcoord in [0,1]
149 * size = width (or height or depth) of texture
151 * i0, i1 = returns two nearest texel indexes
152 * weight = returns blend factor between texels
155 linear_texel_locations(GLenum wrapMode
,
156 const struct gl_texture_image
*img
,
157 GLint size
, GLfloat s
,
158 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
164 if (img
->_IsPowerOfTwo
) {
165 *i0
= IFLOOR(u
) & (size
- 1);
166 *i1
= (*i0
+ 1) & (size
- 1);
169 *i0
= REMAINDER(IFLOOR(u
), size
);
170 *i1
= REMAINDER(*i0
+ 1, size
);
173 case GL_CLAMP_TO_EDGE
:
185 if (*i1
>= (GLint
) size
)
188 case GL_CLAMP_TO_BORDER
:
190 const GLfloat min
= -1.0F
/ (2.0F
* size
);
191 const GLfloat max
= 1.0F
- min
;
203 case GL_MIRRORED_REPEAT
:
205 const GLint flr
= IFLOOR(s
);
207 u
= 1.0F
- (s
- (GLfloat
) flr
);
209 u
= s
- (GLfloat
) flr
;
210 u
= (u
* size
) - 0.5F
;
215 if (*i1
>= (GLint
) size
)
219 case GL_MIRROR_CLAMP_EXT
:
229 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
240 if (*i1
>= (GLint
) size
)
243 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
245 const GLfloat min
= -1.0F
/ (2.0F
* size
);
246 const GLfloat max
= 1.0F
- min
;
271 _mesa_problem(NULL
, "Bad wrap mode");
279 * Used to compute texel location for nearest sampling.
282 nearest_texel_location(GLenum wrapMode
,
283 const struct gl_texture_image
*img
,
284 GLint size
, GLfloat s
)
290 /* s limited to [0,1) */
291 /* i limited to [0,size-1] */
292 i
= IFLOOR(s
* size
);
293 if (img
->_IsPowerOfTwo
)
296 i
= REMAINDER(i
, size
);
298 case GL_CLAMP_TO_EDGE
:
300 /* s limited to [min,max] */
301 /* i limited to [0, size-1] */
302 const GLfloat min
= 1.0F
/ (2.0F
* size
);
303 const GLfloat max
= 1.0F
- min
;
309 i
= IFLOOR(s
* size
);
312 case GL_CLAMP_TO_BORDER
:
314 /* s limited to [min,max] */
315 /* i limited to [-1, size] */
316 const GLfloat min
= -1.0F
/ (2.0F
* size
);
317 const GLfloat max
= 1.0F
- min
;
323 i
= IFLOOR(s
* size
);
326 case GL_MIRRORED_REPEAT
:
328 const GLfloat min
= 1.0F
/ (2.0F
* size
);
329 const GLfloat max
= 1.0F
- min
;
330 const GLint flr
= IFLOOR(s
);
333 u
= 1.0F
- (s
- (GLfloat
) flr
);
335 u
= s
- (GLfloat
) flr
;
341 i
= IFLOOR(u
* size
);
344 case GL_MIRROR_CLAMP_EXT
:
346 /* s limited to [0,1] */
347 /* i limited to [0,size-1] */
348 const GLfloat u
= FABSF(s
);
354 i
= IFLOOR(u
* size
);
357 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
359 /* s limited to [min,max] */
360 /* i limited to [0, size-1] */
361 const GLfloat min
= 1.0F
/ (2.0F
* size
);
362 const GLfloat max
= 1.0F
- min
;
363 const GLfloat u
= FABSF(s
);
369 i
= IFLOOR(u
* size
);
372 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
374 /* s limited to [min,max] */
375 /* i limited to [0, size-1] */
376 const GLfloat min
= -1.0F
/ (2.0F
* size
);
377 const GLfloat max
= 1.0F
- min
;
378 const GLfloat u
= FABSF(s
);
384 i
= IFLOOR(u
* size
);
388 /* s limited to [0,1] */
389 /* i limited to [0,size-1] */
395 i
= IFLOOR(s
* size
);
398 _mesa_problem(NULL
, "Bad wrap mode");
404 /* Power of two image sizes only */
406 linear_repeat_texel_location(GLuint size
, GLfloat s
,
407 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
409 GLfloat u
= s
* size
- 0.5F
;
410 *i0
= IFLOOR(u
) & (size
- 1);
411 *i1
= (*i0
+ 1) & (size
- 1);
417 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
420 clamp_rect_coord_nearest(GLenum wrapMode
, GLfloat coord
, GLint max
)
424 return IFLOOR( CLAMP(coord
, 0.0F
, max
- 1) );
425 case GL_CLAMP_TO_EDGE
:
426 return IFLOOR( CLAMP(coord
, 0.5F
, max
- 0.5F
) );
427 case GL_CLAMP_TO_BORDER
:
428 return IFLOOR( CLAMP(coord
, -0.5F
, max
+ 0.5F
) );
430 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_nearest");
437 * As above, but GL_LINEAR filtering.
440 clamp_rect_coord_linear(GLenum wrapMode
, GLfloat coord
, GLint max
,
441 GLint
*i0out
, GLint
*i1out
, GLfloat
*weight
)
447 /* Not exactly what the spec says, but it matches NVIDIA output */
448 fcol
= CLAMP(coord
- 0.5F
, 0.0F
, 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 slice/image to use for 1D or 2D array texture.
481 tex_array_slice(GLfloat coord
, GLsizei size
)
483 GLint slice
= IFLOOR(coord
+ 0.5f
);
484 slice
= CLAMP(slice
, 0, size
- 1);
490 * Compute nearest integer texcoords for given texobj and coordinate.
493 nearest_texcoord(const struct gl_texture_object
*texObj
,
494 const GLfloat texcoord
[4],
495 GLint
*i
, GLint
*j
, GLint
*k
)
497 const GLint baseLevel
= texObj
->BaseLevel
;
498 const struct gl_texture_image
*img
= texObj
->Image
[0][baseLevel
];
499 const GLint width
= img
->Width
;
500 const GLint height
= img
->Height
;
501 const GLint depth
= img
->Depth
;
503 switch (texObj
->Target
) {
504 case GL_TEXTURE_RECTANGLE_ARB
:
505 *i
= clamp_rect_coord_nearest(texObj
->WrapS
, texcoord
[0], width
);
506 *j
= clamp_rect_coord_nearest(texObj
->WrapT
, texcoord
[1], height
);
510 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
515 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
516 *j
= nearest_texel_location(texObj
->WrapT
, img
, height
, texcoord
[1]);
519 case GL_TEXTURE_1D_ARRAY_EXT
:
520 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
521 *j
= tex_array_slice(texcoord
[1], height
);
524 case GL_TEXTURE_2D_ARRAY_EXT
:
525 *i
= nearest_texel_location(texObj
->WrapS
, img
, width
, texcoord
[0]);
526 *j
= nearest_texel_location(texObj
->WrapT
, img
, height
, texcoord
[1]);
527 *k
= tex_array_slice(texcoord
[2], depth
);
536 * Compute linear integer texcoords for given texobj and coordinate.
539 linear_texcoord(const struct gl_texture_object
*texObj
,
540 const GLfloat texcoord
[4],
541 GLint
*i0
, GLint
*i1
, GLint
*j0
, GLint
*j1
, GLint
*slice
,
542 GLfloat
*wi
, GLfloat
*wj
)
544 const GLint baseLevel
= texObj
->BaseLevel
;
545 const struct gl_texture_image
*img
= texObj
->Image
[0][baseLevel
];
546 const GLint width
= img
->Width
;
547 const GLint height
= img
->Height
;
548 const GLint depth
= img
->Depth
;
550 switch (texObj
->Target
) {
551 case GL_TEXTURE_RECTANGLE_ARB
:
552 clamp_rect_coord_linear(texObj
->WrapS
, texcoord
[0],
554 clamp_rect_coord_linear(texObj
->WrapT
, texcoord
[1],
561 linear_texel_locations(texObj
->WrapS
, img
, width
,
562 texcoord
[0], i0
, i1
, wi
);
563 linear_texel_locations(texObj
->WrapT
, img
, height
,
564 texcoord
[1], j0
, j1
, wj
);
568 case GL_TEXTURE_1D_ARRAY_EXT
:
569 linear_texel_locations(texObj
->WrapS
, img
, width
,
570 texcoord
[0], i0
, i1
, wi
);
571 *j0
= tex_array_slice(texcoord
[1], height
);
576 case GL_TEXTURE_2D_ARRAY_EXT
:
577 linear_texel_locations(texObj
->WrapS
, img
, width
,
578 texcoord
[0], i0
, i1
, wi
);
579 linear_texel_locations(texObj
->WrapT
, img
, height
,
580 texcoord
[1], j0
, j1
, wj
);
581 *slice
= tex_array_slice(texcoord
[2], depth
);
592 * For linear interpolation between mipmap levels N and N+1, this function
596 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
599 return tObj
->BaseLevel
;
600 else if (lambda
> tObj
->_MaxLambda
)
601 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
603 return (GLint
) (tObj
->BaseLevel
+ lambda
);
608 * Compute the nearest mipmap level to take texels from.
611 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
617 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
618 l
= tObj
->_MaxLambda
+ 0.4999F
;
621 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
622 if (level
> tObj
->_MaxLevel
)
623 level
= tObj
->_MaxLevel
;
630 * Bitflags for texture border color sampling.
642 * The lambda[] array values are always monotonic. Either the whole span
643 * will be minified, magnified, or split between the two. This function
644 * determines the subranges in [0, n-1] that are to be minified or magnified.
647 compute_min_mag_ranges(const struct gl_texture_object
*tObj
,
648 GLuint n
, const GLfloat lambda
[],
649 GLuint
*minStart
, GLuint
*minEnd
,
650 GLuint
*magStart
, GLuint
*magEnd
)
652 GLfloat minMagThresh
;
654 /* we shouldn't be here if minfilter == magfilter */
655 ASSERT(tObj
->MinFilter
!= tObj
->MagFilter
);
657 /* This bit comes from the OpenGL spec: */
658 if (tObj
->MagFilter
== GL_LINEAR
659 && (tObj
->MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
660 tObj
->MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
668 /* DEBUG CODE: Verify that lambda[] is monotonic.
669 * We can't really use this because the inaccuracy in the LOG2 function
670 * causes this test to fail, yet the resulting texturing is correct.
674 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
675 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
676 for (i
= 0; i
< n
- 1; i
++) {
677 ASSERT((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
680 else { /* increasing */
681 for (i
= 0; i
< n
- 1; i
++) {
682 ASSERT((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
688 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
689 /* magnification for whole span */
692 *minStart
= *minEnd
= 0;
694 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
695 /* minification for whole span */
698 *magStart
= *magEnd
= 0;
701 /* a mix of minification and magnification */
703 if (lambda
[0] > minMagThresh
) {
704 /* start with minification */
705 for (i
= 1; i
< n
; i
++) {
706 if (lambda
[i
] <= minMagThresh
)
715 /* start with magnification */
716 for (i
= 1; i
< n
; i
++) {
717 if (lambda
[i
] > minMagThresh
)
728 /* Verify the min/mag Start/End values
729 * We don't use this either (see above)
733 for (i
= 0; i
< n
; i
++) {
734 if (lambda
[i
] > minMagThresh
) {
736 ASSERT(i
>= *minStart
);
741 ASSERT(i
>= *magStart
);
751 * When we sample the border color, it must be interpreted according to
752 * the base texture format. Ex: if the texture base format it GL_ALPHA,
753 * we return (0,0,0,BorderAlpha).
756 get_border_color(const struct gl_texture_object
*tObj
,
757 const struct gl_texture_image
*img
,
760 switch (img
->_BaseFormat
) {
762 rgba
[0] = tObj
->BorderColor
.f
[0];
763 rgba
[1] = tObj
->BorderColor
.f
[1];
764 rgba
[2] = tObj
->BorderColor
.f
[2];
768 rgba
[0] = rgba
[1] = rgba
[2] = 0.0;
769 rgba
[3] = tObj
->BorderColor
.f
[3];
772 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->BorderColor
.f
[0];
775 case GL_LUMINANCE_ALPHA
:
776 rgba
[0] = rgba
[1] = rgba
[2] = tObj
->BorderColor
.f
[0];
777 rgba
[3] = tObj
->BorderColor
.f
[3];
780 rgba
[0] = rgba
[1] = rgba
[2] = rgba
[3] = tObj
->BorderColor
.f
[0];
783 COPY_4V(rgba
, tObj
->BorderColor
.f
);
788 /**********************************************************************/
789 /* 1-D Texture Sampling Functions */
790 /**********************************************************************/
793 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
796 sample_1d_nearest(GLcontext
*ctx
,
797 const struct gl_texture_object
*tObj
,
798 const struct gl_texture_image
*img
,
799 const GLfloat texcoord
[4], GLfloat rgba
[4])
801 const GLint width
= img
->Width2
; /* without border, power of two */
803 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
804 /* skip over the border, if any */
806 if (i
< 0 || i
>= (GLint
) img
->Width
) {
807 /* Need this test for GL_CLAMP_TO_BORDER mode */
808 get_border_color(tObj
, img
, rgba
);
811 img
->FetchTexelf(img
, i
, 0, 0, rgba
);
817 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
820 sample_1d_linear(GLcontext
*ctx
,
821 const struct gl_texture_object
*tObj
,
822 const struct gl_texture_image
*img
,
823 const GLfloat texcoord
[4], GLfloat rgba
[4])
825 const GLint width
= img
->Width2
;
827 GLbitfield useBorderColor
= 0x0;
829 GLfloat t0
[4], t1
[4]; /* texels */
831 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
838 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
839 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
842 /* fetch texel colors */
843 if (useBorderColor
& I0BIT
) {
844 get_border_color(tObj
, img
, t0
);
847 img
->FetchTexelf(img
, i0
, 0, 0, t0
);
849 if (useBorderColor
& I1BIT
) {
850 get_border_color(tObj
, img
, t1
);
853 img
->FetchTexelf(img
, i1
, 0, 0, t1
);
856 lerp_rgba(rgba
, a
, t0
, t1
);
861 sample_1d_nearest_mipmap_nearest(GLcontext
*ctx
,
862 const struct gl_texture_object
*tObj
,
863 GLuint n
, const GLfloat texcoord
[][4],
864 const GLfloat lambda
[], GLfloat rgba
[][4])
867 ASSERT(lambda
!= NULL
);
868 for (i
= 0; i
< n
; i
++) {
869 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
870 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
876 sample_1d_linear_mipmap_nearest(GLcontext
*ctx
,
877 const struct gl_texture_object
*tObj
,
878 GLuint n
, const GLfloat texcoord
[][4],
879 const GLfloat lambda
[], GLfloat rgba
[][4])
882 ASSERT(lambda
!= NULL
);
883 for (i
= 0; i
< n
; i
++) {
884 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
885 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
891 sample_1d_nearest_mipmap_linear(GLcontext
*ctx
,
892 const struct gl_texture_object
*tObj
,
893 GLuint n
, const GLfloat texcoord
[][4],
894 const GLfloat lambda
[], GLfloat rgba
[][4])
897 ASSERT(lambda
!= NULL
);
898 for (i
= 0; i
< n
; i
++) {
899 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
900 if (level
>= tObj
->_MaxLevel
) {
901 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
902 texcoord
[i
], rgba
[i
]);
905 GLfloat t0
[4], t1
[4];
906 const GLfloat f
= FRAC(lambda
[i
]);
907 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
908 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
909 lerp_rgba(rgba
[i
], f
, t0
, t1
);
916 sample_1d_linear_mipmap_linear(GLcontext
*ctx
,
917 const struct gl_texture_object
*tObj
,
918 GLuint n
, const GLfloat texcoord
[][4],
919 const GLfloat lambda
[], GLfloat rgba
[][4])
922 ASSERT(lambda
!= NULL
);
923 for (i
= 0; i
< n
; i
++) {
924 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
925 if (level
>= tObj
->_MaxLevel
) {
926 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
927 texcoord
[i
], rgba
[i
]);
930 GLfloat t0
[4], t1
[4];
931 const GLfloat f
= FRAC(lambda
[i
]);
932 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
933 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
934 lerp_rgba(rgba
[i
], f
, t0
, t1
);
940 /** Sample 1D texture, nearest filtering for both min/magnification */
942 sample_nearest_1d( GLcontext
*ctx
,
943 const struct gl_texture_object
*tObj
, GLuint n
,
944 const GLfloat texcoords
[][4], const GLfloat lambda
[],
948 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
950 for (i
= 0; i
< n
; i
++) {
951 sample_1d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
956 /** Sample 1D texture, linear filtering for both min/magnification */
958 sample_linear_1d( GLcontext
*ctx
,
959 const struct gl_texture_object
*tObj
, GLuint n
,
960 const GLfloat texcoords
[][4], const GLfloat lambda
[],
964 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
966 for (i
= 0; i
< n
; i
++) {
967 sample_1d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
972 /** Sample 1D texture, using lambda to choose between min/magnification */
974 sample_lambda_1d( GLcontext
*ctx
,
975 const struct gl_texture_object
*tObj
, GLuint n
,
976 const GLfloat texcoords
[][4],
977 const GLfloat lambda
[], GLfloat rgba
[][4] )
979 GLuint minStart
, minEnd
; /* texels with minification */
980 GLuint magStart
, magEnd
; /* texels with magnification */
983 ASSERT(lambda
!= NULL
);
984 compute_min_mag_ranges(tObj
, n
, lambda
,
985 &minStart
, &minEnd
, &magStart
, &magEnd
);
987 if (minStart
< minEnd
) {
988 /* do the minified texels */
989 const GLuint m
= minEnd
- minStart
;
990 switch (tObj
->MinFilter
) {
992 for (i
= minStart
; i
< minEnd
; i
++)
993 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
994 texcoords
[i
], rgba
[i
]);
997 for (i
= minStart
; i
< minEnd
; i
++)
998 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
999 texcoords
[i
], rgba
[i
]);
1001 case GL_NEAREST_MIPMAP_NEAREST
:
1002 sample_1d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1003 lambda
+ minStart
, rgba
+ minStart
);
1005 case GL_LINEAR_MIPMAP_NEAREST
:
1006 sample_1d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1007 lambda
+ minStart
, rgba
+ minStart
);
1009 case GL_NEAREST_MIPMAP_LINEAR
:
1010 sample_1d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1011 lambda
+ minStart
, rgba
+ minStart
);
1013 case GL_LINEAR_MIPMAP_LINEAR
:
1014 sample_1d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1015 lambda
+ minStart
, rgba
+ minStart
);
1018 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
1023 if (magStart
< magEnd
) {
1024 /* do the magnified texels */
1025 switch (tObj
->MagFilter
) {
1027 for (i
= magStart
; i
< magEnd
; i
++)
1028 sample_1d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1029 texcoords
[i
], rgba
[i
]);
1032 for (i
= magStart
; i
< magEnd
; i
++)
1033 sample_1d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1034 texcoords
[i
], rgba
[i
]);
1037 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
1044 /**********************************************************************/
1045 /* 2-D Texture Sampling Functions */
1046 /**********************************************************************/
1050 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1053 sample_2d_nearest(GLcontext
*ctx
,
1054 const struct gl_texture_object
*tObj
,
1055 const struct gl_texture_image
*img
,
1056 const GLfloat texcoord
[4],
1059 const GLint width
= img
->Width2
; /* without border, power of two */
1060 const GLint height
= img
->Height2
; /* without border, power of two */
1064 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
1065 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
1067 /* skip over the border, if any */
1071 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
1072 /* Need this test for GL_CLAMP_TO_BORDER mode */
1073 get_border_color(tObj
, img
, rgba
);
1076 img
->FetchTexelf(img
, i
, j
, 0, rgba
);
1082 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1083 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1086 sample_2d_linear(GLcontext
*ctx
,
1087 const struct gl_texture_object
*tObj
,
1088 const struct gl_texture_image
*img
,
1089 const GLfloat texcoord
[4],
1092 const GLint width
= img
->Width2
;
1093 const GLint height
= img
->Height2
;
1094 GLint i0
, j0
, i1
, j1
;
1095 GLbitfield useBorderColor
= 0x0;
1097 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1099 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1100 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1109 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1110 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1111 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1112 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1115 /* fetch four texel colors */
1116 if (useBorderColor
& (I0BIT
| J0BIT
)) {
1117 get_border_color(tObj
, img
, t00
);
1120 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
1122 if (useBorderColor
& (I1BIT
| J0BIT
)) {
1123 get_border_color(tObj
, img
, t10
);
1126 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
1128 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1129 get_border_color(tObj
, img
, t01
);
1132 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
1134 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1135 get_border_color(tObj
, img
, t11
);
1138 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
1141 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1146 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1147 * We don't have to worry about the texture border.
1150 sample_2d_linear_repeat(GLcontext
*ctx
,
1151 const struct gl_texture_object
*tObj
,
1152 const struct gl_texture_image
*img
,
1153 const GLfloat texcoord
[4],
1156 const GLint width
= img
->Width2
;
1157 const GLint height
= img
->Height2
;
1158 GLint i0
, j0
, i1
, j1
;
1160 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1164 ASSERT(tObj
->WrapS
== GL_REPEAT
);
1165 ASSERT(tObj
->WrapT
== GL_REPEAT
);
1166 ASSERT(img
->Border
== 0);
1167 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
1168 ASSERT(img
->_IsPowerOfTwo
);
1170 linear_repeat_texel_location(width
, texcoord
[0], &i0
, &i1
, &wi
);
1171 linear_repeat_texel_location(height
, texcoord
[1], &j0
, &j1
, &wj
);
1173 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
1174 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
1175 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
1176 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
1178 lerp_rgba_2d(rgba
, wi
, wj
, t00
, t10
, t01
, t11
);
1183 sample_2d_nearest_mipmap_nearest(GLcontext
*ctx
,
1184 const struct gl_texture_object
*tObj
,
1185 GLuint n
, const GLfloat texcoord
[][4],
1186 const GLfloat lambda
[], GLfloat rgba
[][4])
1189 for (i
= 0; i
< n
; i
++) {
1190 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1191 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1197 sample_2d_linear_mipmap_nearest(GLcontext
*ctx
,
1198 const struct gl_texture_object
*tObj
,
1199 GLuint n
, const GLfloat texcoord
[][4],
1200 const GLfloat lambda
[], GLfloat rgba
[][4])
1203 ASSERT(lambda
!= NULL
);
1204 for (i
= 0; i
< n
; i
++) {
1205 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1206 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1212 sample_2d_nearest_mipmap_linear(GLcontext
*ctx
,
1213 const struct gl_texture_object
*tObj
,
1214 GLuint n
, const GLfloat texcoord
[][4],
1215 const GLfloat lambda
[], GLfloat rgba
[][4])
1218 ASSERT(lambda
!= NULL
);
1219 for (i
= 0; i
< n
; i
++) {
1220 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1221 if (level
>= tObj
->_MaxLevel
) {
1222 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1223 texcoord
[i
], rgba
[i
]);
1226 GLfloat t0
[4], t1
[4]; /* texels */
1227 const GLfloat f
= FRAC(lambda
[i
]);
1228 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1229 sample_2d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1230 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1237 sample_2d_linear_mipmap_linear( GLcontext
*ctx
,
1238 const struct gl_texture_object
*tObj
,
1239 GLuint n
, const GLfloat texcoord
[][4],
1240 const GLfloat lambda
[], GLfloat rgba
[][4] )
1243 ASSERT(lambda
!= NULL
);
1244 for (i
= 0; i
< n
; i
++) {
1245 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1246 if (level
>= tObj
->_MaxLevel
) {
1247 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1248 texcoord
[i
], rgba
[i
]);
1251 GLfloat t0
[4], t1
[4]; /* texels */
1252 const GLfloat f
= FRAC(lambda
[i
]);
1253 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1254 sample_2d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1255 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1262 sample_2d_linear_mipmap_linear_repeat(GLcontext
*ctx
,
1263 const struct gl_texture_object
*tObj
,
1264 GLuint n
, const GLfloat texcoord
[][4],
1265 const GLfloat lambda
[], GLfloat rgba
[][4])
1268 ASSERT(lambda
!= NULL
);
1269 ASSERT(tObj
->WrapS
== GL_REPEAT
);
1270 ASSERT(tObj
->WrapT
== GL_REPEAT
);
1271 for (i
= 0; i
< n
; i
++) {
1272 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1273 if (level
>= tObj
->_MaxLevel
) {
1274 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1275 texcoord
[i
], rgba
[i
]);
1278 GLfloat t0
[4], t1
[4]; /* texels */
1279 const GLfloat f
= FRAC(lambda
[i
]);
1280 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
],
1282 sample_2d_linear_repeat(ctx
, tObj
, tObj
->Image
[0][level
+1],
1284 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1290 /** Sample 2D texture, nearest filtering for both min/magnification */
1292 sample_nearest_2d(GLcontext
*ctx
,
1293 const struct gl_texture_object
*tObj
, GLuint n
,
1294 const GLfloat texcoords
[][4],
1295 const GLfloat lambda
[], GLfloat rgba
[][4])
1298 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1300 for (i
= 0; i
< n
; i
++) {
1301 sample_2d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1306 /** Sample 2D texture, linear filtering for both min/magnification */
1308 sample_linear_2d(GLcontext
*ctx
,
1309 const struct gl_texture_object
*tObj
, GLuint n
,
1310 const GLfloat texcoords
[][4],
1311 const GLfloat lambda
[], GLfloat rgba
[][4])
1314 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1316 if (tObj
->WrapS
== GL_REPEAT
&&
1317 tObj
->WrapT
== GL_REPEAT
&&
1318 image
->_IsPowerOfTwo
&&
1319 image
->Border
== 0) {
1320 for (i
= 0; i
< n
; i
++) {
1321 sample_2d_linear_repeat(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1325 for (i
= 0; i
< n
; i
++) {
1326 sample_2d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1333 * Optimized 2-D texture sampling:
1334 * S and T wrap mode == GL_REPEAT
1335 * GL_NEAREST min/mag filter
1337 * RowStride == Width,
1341 opt_sample_rgb_2d(GLcontext
*ctx
,
1342 const struct gl_texture_object
*tObj
,
1343 GLuint n
, const GLfloat texcoords
[][4],
1344 const GLfloat lambda
[], GLfloat rgba
[][4])
1346 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1347 const GLfloat width
= (GLfloat
) img
->Width
;
1348 const GLfloat height
= (GLfloat
) img
->Height
;
1349 const GLint colMask
= img
->Width
- 1;
1350 const GLint rowMask
= img
->Height
- 1;
1351 const GLint shift
= img
->WidthLog2
;
1355 ASSERT(tObj
->WrapS
==GL_REPEAT
);
1356 ASSERT(tObj
->WrapT
==GL_REPEAT
);
1357 ASSERT(img
->Border
==0);
1358 ASSERT(img
->TexFormat
== MESA_FORMAT_RGB888
);
1359 ASSERT(img
->_IsPowerOfTwo
);
1361 for (k
=0; k
<n
; k
++) {
1362 GLint i
= IFLOOR(texcoords
[k
][0] * width
) & colMask
;
1363 GLint j
= IFLOOR(texcoords
[k
][1] * height
) & rowMask
;
1364 GLint pos
= (j
<< shift
) | i
;
1365 GLubyte
*texel
= ((GLubyte
*) img
->Data
) + 3*pos
;
1366 rgba
[k
][RCOMP
] = UBYTE_TO_FLOAT(texel
[2]);
1367 rgba
[k
][GCOMP
] = UBYTE_TO_FLOAT(texel
[1]);
1368 rgba
[k
][BCOMP
] = UBYTE_TO_FLOAT(texel
[0]);
1374 * Optimized 2-D texture sampling:
1375 * S and T wrap mode == GL_REPEAT
1376 * GL_NEAREST min/mag filter
1378 * RowStride == Width,
1382 opt_sample_rgba_2d(GLcontext
*ctx
,
1383 const struct gl_texture_object
*tObj
,
1384 GLuint n
, const GLfloat texcoords
[][4],
1385 const GLfloat lambda
[], GLfloat rgba
[][4])
1387 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1388 const GLfloat width
= (GLfloat
) img
->Width
;
1389 const GLfloat height
= (GLfloat
) img
->Height
;
1390 const GLint colMask
= img
->Width
- 1;
1391 const GLint rowMask
= img
->Height
- 1;
1392 const GLint shift
= img
->WidthLog2
;
1396 ASSERT(tObj
->WrapS
==GL_REPEAT
);
1397 ASSERT(tObj
->WrapT
==GL_REPEAT
);
1398 ASSERT(img
->Border
==0);
1399 ASSERT(img
->TexFormat
== MESA_FORMAT_RGBA8888
);
1400 ASSERT(img
->_IsPowerOfTwo
);
1402 for (i
= 0; i
< n
; i
++) {
1403 const GLint col
= IFLOOR(texcoords
[i
][0] * width
) & colMask
;
1404 const GLint row
= IFLOOR(texcoords
[i
][1] * height
) & rowMask
;
1405 const GLint pos
= (row
<< shift
) | col
;
1406 const GLuint texel
= *((GLuint
*) img
->Data
+ pos
);
1407 rgba
[i
][RCOMP
] = UBYTE_TO_FLOAT( (texel
>> 24) );
1408 rgba
[i
][GCOMP
] = UBYTE_TO_FLOAT( (texel
>> 16) & 0xff );
1409 rgba
[i
][BCOMP
] = UBYTE_TO_FLOAT( (texel
>> 8) & 0xff );
1410 rgba
[i
][ACOMP
] = UBYTE_TO_FLOAT( (texel
) & 0xff );
1415 /** Sample 2D texture, using lambda to choose between min/magnification */
1417 sample_lambda_2d(GLcontext
*ctx
,
1418 const struct gl_texture_object
*tObj
,
1419 GLuint n
, const GLfloat texcoords
[][4],
1420 const GLfloat lambda
[], GLfloat rgba
[][4])
1422 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1423 GLuint minStart
, minEnd
; /* texels with minification */
1424 GLuint magStart
, magEnd
; /* texels with magnification */
1426 const GLboolean repeatNoBorderPOT
= (tObj
->WrapS
== GL_REPEAT
)
1427 && (tObj
->WrapT
== GL_REPEAT
)
1428 && (tImg
->Border
== 0 && (tImg
->Width
== tImg
->RowStride
))
1429 && (tImg
->_BaseFormat
!= GL_COLOR_INDEX
)
1430 && tImg
->_IsPowerOfTwo
;
1432 ASSERT(lambda
!= NULL
);
1433 compute_min_mag_ranges(tObj
, n
, lambda
,
1434 &minStart
, &minEnd
, &magStart
, &magEnd
);
1436 if (minStart
< minEnd
) {
1437 /* do the minified texels */
1438 const GLuint m
= minEnd
- minStart
;
1439 switch (tObj
->MinFilter
) {
1441 if (repeatNoBorderPOT
) {
1442 switch (tImg
->TexFormat
) {
1443 case MESA_FORMAT_RGB888
:
1444 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1445 NULL
, rgba
+ minStart
);
1447 case MESA_FORMAT_RGBA8888
:
1448 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1449 NULL
, rgba
+ minStart
);
1452 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1453 NULL
, rgba
+ minStart
);
1457 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1458 NULL
, rgba
+ minStart
);
1462 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ minStart
,
1463 NULL
, rgba
+ minStart
);
1465 case GL_NEAREST_MIPMAP_NEAREST
:
1466 sample_2d_nearest_mipmap_nearest(ctx
, tObj
, m
,
1467 texcoords
+ minStart
,
1468 lambda
+ minStart
, rgba
+ minStart
);
1470 case GL_LINEAR_MIPMAP_NEAREST
:
1471 sample_2d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1472 lambda
+ minStart
, rgba
+ minStart
);
1474 case GL_NEAREST_MIPMAP_LINEAR
:
1475 sample_2d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1476 lambda
+ minStart
, rgba
+ minStart
);
1478 case GL_LINEAR_MIPMAP_LINEAR
:
1479 if (repeatNoBorderPOT
)
1480 sample_2d_linear_mipmap_linear_repeat(ctx
, tObj
, m
,
1481 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1483 sample_2d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1484 lambda
+ minStart
, rgba
+ minStart
);
1487 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1492 if (magStart
< magEnd
) {
1493 /* do the magnified texels */
1494 const GLuint m
= magEnd
- magStart
;
1496 switch (tObj
->MagFilter
) {
1498 if (repeatNoBorderPOT
) {
1499 switch (tImg
->TexFormat
) {
1500 case MESA_FORMAT_RGB888
:
1501 opt_sample_rgb_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1502 NULL
, rgba
+ magStart
);
1504 case MESA_FORMAT_RGBA8888
:
1505 opt_sample_rgba_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1506 NULL
, rgba
+ magStart
);
1509 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1510 NULL
, rgba
+ magStart
);
1514 sample_nearest_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1515 NULL
, rgba
+ magStart
);
1519 sample_linear_2d(ctx
, tObj
, m
, texcoords
+ magStart
,
1520 NULL
, rgba
+ magStart
);
1523 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1530 /**********************************************************************/
1531 /* 3-D Texture Sampling Functions */
1532 /**********************************************************************/
1535 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1538 sample_3d_nearest(GLcontext
*ctx
,
1539 const struct gl_texture_object
*tObj
,
1540 const struct gl_texture_image
*img
,
1541 const GLfloat texcoord
[4],
1544 const GLint width
= img
->Width2
; /* without border, power of two */
1545 const GLint height
= img
->Height2
; /* without border, power of two */
1546 const GLint depth
= img
->Depth2
; /* without border, power of two */
1550 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
1551 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
1552 k
= nearest_texel_location(tObj
->WrapR
, img
, depth
, texcoord
[2]);
1554 if (i
< 0 || i
>= (GLint
) img
->Width
||
1555 j
< 0 || j
>= (GLint
) img
->Height
||
1556 k
< 0 || k
>= (GLint
) img
->Depth
) {
1557 /* Need this test for GL_CLAMP_TO_BORDER mode */
1558 get_border_color(tObj
, img
, rgba
);
1561 img
->FetchTexelf(img
, i
, j
, k
, rgba
);
1567 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1570 sample_3d_linear(GLcontext
*ctx
,
1571 const struct gl_texture_object
*tObj
,
1572 const struct gl_texture_image
*img
,
1573 const GLfloat texcoord
[4],
1576 const GLint width
= img
->Width2
;
1577 const GLint height
= img
->Height2
;
1578 const GLint depth
= img
->Depth2
;
1579 GLint i0
, j0
, k0
, i1
, j1
, k1
;
1580 GLbitfield useBorderColor
= 0x0;
1582 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
1583 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
1585 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1586 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1587 linear_texel_locations(tObj
->WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
1598 /* check if sampling texture border color */
1599 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1600 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1601 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1602 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1603 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
1604 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
1608 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
1609 get_border_color(tObj
, img
, t000
);
1612 img
->FetchTexelf(img
, i0
, j0
, k0
, t000
);
1614 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
1615 get_border_color(tObj
, img
, t100
);
1618 img
->FetchTexelf(img
, i1
, j0
, k0
, t100
);
1620 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
1621 get_border_color(tObj
, img
, t010
);
1624 img
->FetchTexelf(img
, i0
, j1
, k0
, t010
);
1626 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
1627 get_border_color(tObj
, img
, t110
);
1630 img
->FetchTexelf(img
, i1
, j1
, k0
, t110
);
1633 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
1634 get_border_color(tObj
, img
, t001
);
1637 img
->FetchTexelf(img
, i0
, j0
, k1
, t001
);
1639 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
1640 get_border_color(tObj
, img
, t101
);
1643 img
->FetchTexelf(img
, i1
, j0
, k1
, t101
);
1645 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
1646 get_border_color(tObj
, img
, t011
);
1649 img
->FetchTexelf(img
, i0
, j1
, k1
, t011
);
1651 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
1652 get_border_color(tObj
, img
, t111
);
1655 img
->FetchTexelf(img
, i1
, j1
, k1
, t111
);
1658 /* trilinear interpolation of samples */
1659 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
1664 sample_3d_nearest_mipmap_nearest(GLcontext
*ctx
,
1665 const struct gl_texture_object
*tObj
,
1666 GLuint n
, const GLfloat texcoord
[][4],
1667 const GLfloat lambda
[], GLfloat rgba
[][4] )
1670 for (i
= 0; i
< n
; i
++) {
1671 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1672 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1678 sample_3d_linear_mipmap_nearest(GLcontext
*ctx
,
1679 const struct gl_texture_object
*tObj
,
1680 GLuint n
, const GLfloat texcoord
[][4],
1681 const GLfloat lambda
[], GLfloat rgba
[][4])
1684 ASSERT(lambda
!= NULL
);
1685 for (i
= 0; i
< n
; i
++) {
1686 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1687 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1693 sample_3d_nearest_mipmap_linear(GLcontext
*ctx
,
1694 const struct gl_texture_object
*tObj
,
1695 GLuint n
, const GLfloat texcoord
[][4],
1696 const GLfloat lambda
[], GLfloat rgba
[][4])
1699 ASSERT(lambda
!= NULL
);
1700 for (i
= 0; i
< n
; i
++) {
1701 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1702 if (level
>= tObj
->_MaxLevel
) {
1703 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1704 texcoord
[i
], rgba
[i
]);
1707 GLfloat t0
[4], t1
[4]; /* texels */
1708 const GLfloat f
= FRAC(lambda
[i
]);
1709 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1710 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1711 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1718 sample_3d_linear_mipmap_linear(GLcontext
*ctx
,
1719 const struct gl_texture_object
*tObj
,
1720 GLuint n
, const GLfloat texcoord
[][4],
1721 const GLfloat lambda
[], GLfloat rgba
[][4])
1724 ASSERT(lambda
!= NULL
);
1725 for (i
= 0; i
< n
; i
++) {
1726 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1727 if (level
>= tObj
->_MaxLevel
) {
1728 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
1729 texcoord
[i
], rgba
[i
]);
1732 GLfloat t0
[4], t1
[4]; /* texels */
1733 const GLfloat f
= FRAC(lambda
[i
]);
1734 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1735 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1736 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1742 /** Sample 3D texture, nearest filtering for both min/magnification */
1744 sample_nearest_3d(GLcontext
*ctx
,
1745 const struct gl_texture_object
*tObj
, GLuint n
,
1746 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1750 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1752 for (i
= 0; i
< n
; i
++) {
1753 sample_3d_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1758 /** Sample 3D texture, linear filtering for both min/magnification */
1760 sample_linear_3d(GLcontext
*ctx
,
1761 const struct gl_texture_object
*tObj
, GLuint n
,
1762 const GLfloat texcoords
[][4],
1763 const GLfloat lambda
[], GLfloat rgba
[][4])
1766 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1768 for (i
= 0; i
< n
; i
++) {
1769 sample_3d_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
1774 /** Sample 3D texture, using lambda to choose between min/magnification */
1776 sample_lambda_3d(GLcontext
*ctx
,
1777 const struct gl_texture_object
*tObj
, GLuint n
,
1778 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1781 GLuint minStart
, minEnd
; /* texels with minification */
1782 GLuint magStart
, magEnd
; /* texels with magnification */
1785 ASSERT(lambda
!= NULL
);
1786 compute_min_mag_ranges(tObj
, n
, lambda
,
1787 &minStart
, &minEnd
, &magStart
, &magEnd
);
1789 if (minStart
< minEnd
) {
1790 /* do the minified texels */
1791 GLuint m
= minEnd
- minStart
;
1792 switch (tObj
->MinFilter
) {
1794 for (i
= minStart
; i
< minEnd
; i
++)
1795 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1796 texcoords
[i
], rgba
[i
]);
1799 for (i
= minStart
; i
< minEnd
; i
++)
1800 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1801 texcoords
[i
], rgba
[i
]);
1803 case GL_NEAREST_MIPMAP_NEAREST
:
1804 sample_3d_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1805 lambda
+ minStart
, rgba
+ minStart
);
1807 case GL_LINEAR_MIPMAP_NEAREST
:
1808 sample_3d_linear_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
1809 lambda
+ minStart
, rgba
+ minStart
);
1811 case GL_NEAREST_MIPMAP_LINEAR
:
1812 sample_3d_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1813 lambda
+ minStart
, rgba
+ minStart
);
1815 case GL_LINEAR_MIPMAP_LINEAR
:
1816 sample_3d_linear_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
1817 lambda
+ minStart
, rgba
+ minStart
);
1820 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
1825 if (magStart
< magEnd
) {
1826 /* do the magnified texels */
1827 switch (tObj
->MagFilter
) {
1829 for (i
= magStart
; i
< magEnd
; i
++)
1830 sample_3d_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1831 texcoords
[i
], rgba
[i
]);
1834 for (i
= magStart
; i
< magEnd
; i
++)
1835 sample_3d_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
1836 texcoords
[i
], rgba
[i
]);
1839 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
1846 /**********************************************************************/
1847 /* Texture Cube Map Sampling Functions */
1848 /**********************************************************************/
1851 * Choose one of six sides of a texture cube map given the texture
1852 * coord (rx,ry,rz). Return pointer to corresponding array of texture
1855 static const struct gl_texture_image
**
1856 choose_cube_face(const struct gl_texture_object
*texObj
,
1857 const GLfloat texcoord
[4], GLfloat newCoord
[4])
1861 direction target sc tc ma
1862 ---------- ------------------------------- --- --- ---
1863 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1864 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1865 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1866 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1867 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1868 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1870 const GLfloat rx
= texcoord
[0];
1871 const GLfloat ry
= texcoord
[1];
1872 const GLfloat rz
= texcoord
[2];
1873 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
1877 if (arx
>= ary
&& arx
>= arz
) {
1891 else if (ary
>= arx
&& ary
>= arz
) {
1921 const float ima
= 1.0F
/ ma
;
1922 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
1923 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
1926 return (const struct gl_texture_image
**) texObj
->Image
[face
];
1931 sample_nearest_cube(GLcontext
*ctx
,
1932 const struct gl_texture_object
*tObj
, GLuint n
,
1933 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1938 for (i
= 0; i
< n
; i
++) {
1939 const struct gl_texture_image
**images
;
1940 GLfloat newCoord
[4];
1941 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1942 sample_2d_nearest(ctx
, tObj
, images
[tObj
->BaseLevel
],
1949 sample_linear_cube(GLcontext
*ctx
,
1950 const struct gl_texture_object
*tObj
, GLuint n
,
1951 const GLfloat texcoords
[][4],
1952 const GLfloat lambda
[], GLfloat rgba
[][4])
1956 for (i
= 0; i
< n
; i
++) {
1957 const struct gl_texture_image
**images
;
1958 GLfloat newCoord
[4];
1959 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
1960 sample_2d_linear(ctx
, tObj
, images
[tObj
->BaseLevel
],
1967 sample_cube_nearest_mipmap_nearest(GLcontext
*ctx
,
1968 const struct gl_texture_object
*tObj
,
1969 GLuint n
, const GLfloat texcoord
[][4],
1970 const GLfloat lambda
[], GLfloat rgba
[][4])
1973 ASSERT(lambda
!= NULL
);
1974 for (i
= 0; i
< n
; i
++) {
1975 const struct gl_texture_image
**images
;
1976 GLfloat newCoord
[4];
1978 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
1980 /* XXX we actually need to recompute lambda here based on the newCoords.
1981 * But we would need the texcoords of adjacent fragments to compute that
1982 * properly, and we don't have those here.
1983 * For now, do an approximation: subtracting 1 from the chosen mipmap
1984 * level seems to work in some test cases.
1985 * The same adjustment is done in the next few functions.
1987 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1988 level
= MAX2(level
- 1, 0);
1990 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
1996 sample_cube_linear_mipmap_nearest(GLcontext
*ctx
,
1997 const struct gl_texture_object
*tObj
,
1998 GLuint n
, const GLfloat texcoord
[][4],
1999 const GLfloat lambda
[], GLfloat rgba
[][4])
2002 ASSERT(lambda
!= NULL
);
2003 for (i
= 0; i
< n
; i
++) {
2004 const struct gl_texture_image
**images
;
2005 GLfloat newCoord
[4];
2006 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2007 level
= MAX2(level
- 1, 0); /* see comment above */
2008 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2009 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, rgba
[i
]);
2015 sample_cube_nearest_mipmap_linear(GLcontext
*ctx
,
2016 const struct gl_texture_object
*tObj
,
2017 GLuint n
, const GLfloat texcoord
[][4],
2018 const GLfloat lambda
[], GLfloat rgba
[][4])
2021 ASSERT(lambda
!= NULL
);
2022 for (i
= 0; i
< n
; i
++) {
2023 const struct gl_texture_image
**images
;
2024 GLfloat newCoord
[4];
2025 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2026 level
= MAX2(level
- 1, 0); /* see comment above */
2027 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2028 if (level
>= tObj
->_MaxLevel
) {
2029 sample_2d_nearest(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2033 GLfloat t0
[4], t1
[4]; /* texels */
2034 const GLfloat f
= FRAC(lambda
[i
]);
2035 sample_2d_nearest(ctx
, tObj
, images
[level
], newCoord
, t0
);
2036 sample_2d_nearest(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2037 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2044 sample_cube_linear_mipmap_linear(GLcontext
*ctx
,
2045 const struct gl_texture_object
*tObj
,
2046 GLuint n
, const GLfloat texcoord
[][4],
2047 const GLfloat lambda
[], GLfloat rgba
[][4])
2050 ASSERT(lambda
!= NULL
);
2051 for (i
= 0; i
< n
; i
++) {
2052 const struct gl_texture_image
**images
;
2053 GLfloat newCoord
[4];
2054 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2055 level
= MAX2(level
- 1, 0); /* see comment above */
2056 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2057 if (level
>= tObj
->_MaxLevel
) {
2058 sample_2d_linear(ctx
, tObj
, images
[tObj
->_MaxLevel
],
2062 GLfloat t0
[4], t1
[4];
2063 const GLfloat f
= FRAC(lambda
[i
]);
2064 sample_2d_linear(ctx
, tObj
, images
[level
], newCoord
, t0
);
2065 sample_2d_linear(ctx
, tObj
, images
[level
+1], newCoord
, t1
);
2066 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2072 /** Sample cube texture, using lambda to choose between min/magnification */
2074 sample_lambda_cube(GLcontext
*ctx
,
2075 const struct gl_texture_object
*tObj
, GLuint n
,
2076 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2079 GLuint minStart
, minEnd
; /* texels with minification */
2080 GLuint magStart
, magEnd
; /* texels with magnification */
2082 ASSERT(lambda
!= NULL
);
2083 compute_min_mag_ranges(tObj
, n
, lambda
,
2084 &minStart
, &minEnd
, &magStart
, &magEnd
);
2086 if (minStart
< minEnd
) {
2087 /* do the minified texels */
2088 const GLuint m
= minEnd
- minStart
;
2089 switch (tObj
->MinFilter
) {
2091 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2092 lambda
+ minStart
, rgba
+ minStart
);
2095 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ minStart
,
2096 lambda
+ minStart
, rgba
+ minStart
);
2098 case GL_NEAREST_MIPMAP_NEAREST
:
2099 sample_cube_nearest_mipmap_nearest(ctx
, tObj
, m
,
2100 texcoords
+ minStart
,
2101 lambda
+ minStart
, rgba
+ minStart
);
2103 case GL_LINEAR_MIPMAP_NEAREST
:
2104 sample_cube_linear_mipmap_nearest(ctx
, tObj
, m
,
2105 texcoords
+ minStart
,
2106 lambda
+ minStart
, rgba
+ minStart
);
2108 case GL_NEAREST_MIPMAP_LINEAR
:
2109 sample_cube_nearest_mipmap_linear(ctx
, tObj
, m
,
2110 texcoords
+ minStart
,
2111 lambda
+ minStart
, rgba
+ minStart
);
2113 case GL_LINEAR_MIPMAP_LINEAR
:
2114 sample_cube_linear_mipmap_linear(ctx
, tObj
, m
,
2115 texcoords
+ minStart
,
2116 lambda
+ minStart
, rgba
+ minStart
);
2119 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2123 if (magStart
< magEnd
) {
2124 /* do the magnified texels */
2125 const GLuint m
= magEnd
- magStart
;
2126 switch (tObj
->MagFilter
) {
2128 sample_nearest_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2129 lambda
+ magStart
, rgba
+ magStart
);
2132 sample_linear_cube(ctx
, tObj
, m
, texcoords
+ magStart
,
2133 lambda
+ magStart
, rgba
+ magStart
);
2136 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2142 /**********************************************************************/
2143 /* Texture Rectangle Sampling Functions */
2144 /**********************************************************************/
2148 sample_nearest_rect(GLcontext
*ctx
,
2149 const struct gl_texture_object
*tObj
, GLuint n
,
2150 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2153 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2154 const GLint width
= img
->Width
;
2155 const GLint height
= img
->Height
;
2161 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2162 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2163 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2164 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2165 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2166 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2167 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
2169 for (i
= 0; i
< n
; i
++) {
2171 col
= clamp_rect_coord_nearest(tObj
->WrapS
, texcoords
[i
][0], width
);
2172 row
= clamp_rect_coord_nearest(tObj
->WrapT
, texcoords
[i
][1], height
);
2173 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2174 get_border_color(tObj
, img
, rgba
[i
]);
2176 img
->FetchTexelf(img
, col
, row
, 0, rgba
[i
]);
2182 sample_linear_rect(GLcontext
*ctx
,
2183 const struct gl_texture_object
*tObj
, GLuint n
,
2184 const GLfloat texcoords
[][4],
2185 const GLfloat lambda
[], GLfloat rgba
[][4])
2187 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2188 const GLint width
= img
->Width
;
2189 const GLint height
= img
->Height
;
2195 ASSERT(tObj
->WrapS
== GL_CLAMP
||
2196 tObj
->WrapS
== GL_CLAMP_TO_EDGE
||
2197 tObj
->WrapS
== GL_CLAMP_TO_BORDER
);
2198 ASSERT(tObj
->WrapT
== GL_CLAMP
||
2199 tObj
->WrapT
== GL_CLAMP_TO_EDGE
||
2200 tObj
->WrapT
== GL_CLAMP_TO_BORDER
);
2201 ASSERT(img
->_BaseFormat
!= GL_COLOR_INDEX
);
2203 for (i
= 0; i
< n
; i
++) {
2204 GLint i0
, j0
, i1
, j1
;
2205 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2207 GLbitfield useBorderColor
= 0x0;
2209 clamp_rect_coord_linear(tObj
->WrapS
, texcoords
[i
][0], width
,
2211 clamp_rect_coord_linear(tObj
->WrapT
, texcoords
[i
][1], height
,
2214 /* compute integer rows/columns */
2215 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2216 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2217 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2218 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2220 /* get four texel samples */
2221 if (useBorderColor
& (I0BIT
| J0BIT
))
2222 get_border_color(tObj
, img
, t00
);
2224 img
->FetchTexelf(img
, i0
, j0
, 0, t00
);
2226 if (useBorderColor
& (I1BIT
| J0BIT
))
2227 get_border_color(tObj
, img
, t10
);
2229 img
->FetchTexelf(img
, i1
, j0
, 0, t10
);
2231 if (useBorderColor
& (I0BIT
| J1BIT
))
2232 get_border_color(tObj
, img
, t01
);
2234 img
->FetchTexelf(img
, i0
, j1
, 0, t01
);
2236 if (useBorderColor
& (I1BIT
| J1BIT
))
2237 get_border_color(tObj
, img
, t11
);
2239 img
->FetchTexelf(img
, i1
, j1
, 0, t11
);
2241 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2246 /** Sample Rect texture, using lambda to choose between min/magnification */
2248 sample_lambda_rect(GLcontext
*ctx
,
2249 const struct gl_texture_object
*tObj
, GLuint n
,
2250 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2253 GLuint minStart
, minEnd
, magStart
, magEnd
;
2255 /* We only need lambda to decide between minification and magnification.
2256 * There is no mipmapping with rectangular textures.
2258 compute_min_mag_ranges(tObj
, n
, lambda
,
2259 &minStart
, &minEnd
, &magStart
, &magEnd
);
2261 if (minStart
< minEnd
) {
2262 if (tObj
->MinFilter
== GL_NEAREST
) {
2263 sample_nearest_rect(ctx
, tObj
, minEnd
- minStart
,
2264 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2267 sample_linear_rect(ctx
, tObj
, minEnd
- minStart
,
2268 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2271 if (magStart
< magEnd
) {
2272 if (tObj
->MagFilter
== GL_NEAREST
) {
2273 sample_nearest_rect(ctx
, tObj
, magEnd
- magStart
,
2274 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2277 sample_linear_rect(ctx
, tObj
, magEnd
- magStart
,
2278 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2284 /**********************************************************************/
2285 /* 2D Texture Array Sampling Functions */
2286 /**********************************************************************/
2289 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2292 sample_2d_array_nearest(GLcontext
*ctx
,
2293 const struct gl_texture_object
*tObj
,
2294 const struct gl_texture_image
*img
,
2295 const GLfloat texcoord
[4],
2298 const GLint width
= img
->Width2
; /* without border, power of two */
2299 const GLint height
= img
->Height2
; /* without border, power of two */
2300 const GLint depth
= img
->Depth
;
2305 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2306 j
= nearest_texel_location(tObj
->WrapT
, img
, height
, texcoord
[1]);
2307 array
= tex_array_slice(texcoord
[2], depth
);
2309 if (i
< 0 || i
>= (GLint
) img
->Width
||
2310 j
< 0 || j
>= (GLint
) img
->Height
||
2311 array
< 0 || array
>= (GLint
) img
->Depth
) {
2312 /* Need this test for GL_CLAMP_TO_BORDER mode */
2313 get_border_color(tObj
, img
, rgba
);
2316 img
->FetchTexelf(img
, i
, j
, array
, rgba
);
2322 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2325 sample_2d_array_linear(GLcontext
*ctx
,
2326 const struct gl_texture_object
*tObj
,
2327 const struct gl_texture_image
*img
,
2328 const GLfloat texcoord
[4],
2331 const GLint width
= img
->Width2
;
2332 const GLint height
= img
->Height2
;
2333 const GLint depth
= img
->Depth
;
2334 GLint i0
, j0
, i1
, j1
;
2336 GLbitfield useBorderColor
= 0x0;
2338 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2340 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2341 linear_texel_locations(tObj
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2342 array
= tex_array_slice(texcoord
[2], depth
);
2344 if (array
< 0 || array
>= depth
) {
2345 COPY_4V(rgba
, tObj
->BorderColor
.f
);
2355 /* check if sampling texture border color */
2356 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2357 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2358 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2359 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2363 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2364 get_border_color(tObj
, img
, t00
);
2367 img
->FetchTexelf(img
, i0
, j0
, array
, t00
);
2369 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2370 get_border_color(tObj
, img
, t10
);
2373 img
->FetchTexelf(img
, i1
, j0
, array
, t10
);
2375 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2376 get_border_color(tObj
, img
, t01
);
2379 img
->FetchTexelf(img
, i0
, j1
, array
, t01
);
2381 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2382 get_border_color(tObj
, img
, t11
);
2385 img
->FetchTexelf(img
, i1
, j1
, array
, t11
);
2388 /* trilinear interpolation of samples */
2389 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2395 sample_2d_array_nearest_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 for (i
= 0; i
< n
; i
++) {
2402 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2403 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2410 sample_2d_array_linear_mipmap_nearest(GLcontext
*ctx
,
2411 const struct gl_texture_object
*tObj
,
2412 GLuint n
, const GLfloat texcoord
[][4],
2413 const GLfloat lambda
[], GLfloat rgba
[][4])
2416 ASSERT(lambda
!= NULL
);
2417 for (i
= 0; i
< n
; i
++) {
2418 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2419 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2420 texcoord
[i
], rgba
[i
]);
2426 sample_2d_array_nearest_mipmap_linear(GLcontext
*ctx
,
2427 const struct gl_texture_object
*tObj
,
2428 GLuint n
, const GLfloat texcoord
[][4],
2429 const GLfloat lambda
[], GLfloat rgba
[][4])
2432 ASSERT(lambda
!= NULL
);
2433 for (i
= 0; i
< n
; i
++) {
2434 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2435 if (level
>= tObj
->_MaxLevel
) {
2436 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2437 texcoord
[i
], rgba
[i
]);
2440 GLfloat t0
[4], t1
[4]; /* texels */
2441 const GLfloat f
= FRAC(lambda
[i
]);
2442 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
],
2444 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1],
2446 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2453 sample_2d_array_linear_mipmap_linear(GLcontext
*ctx
,
2454 const struct gl_texture_object
*tObj
,
2455 GLuint n
, const GLfloat texcoord
[][4],
2456 const GLfloat lambda
[], GLfloat rgba
[][4])
2459 ASSERT(lambda
!= NULL
);
2460 for (i
= 0; i
< n
; i
++) {
2461 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2462 if (level
>= tObj
->_MaxLevel
) {
2463 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2464 texcoord
[i
], rgba
[i
]);
2467 GLfloat t0
[4], t1
[4]; /* texels */
2468 const GLfloat f
= FRAC(lambda
[i
]);
2469 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2471 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1],
2473 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2479 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2481 sample_nearest_2d_array(GLcontext
*ctx
,
2482 const struct gl_texture_object
*tObj
, GLuint n
,
2483 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2487 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2489 for (i
= 0; i
< n
; i
++) {
2490 sample_2d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2496 /** Sample 2D Array texture, linear filtering for both min/magnification */
2498 sample_linear_2d_array(GLcontext
*ctx
,
2499 const struct gl_texture_object
*tObj
, GLuint n
,
2500 const GLfloat texcoords
[][4],
2501 const GLfloat lambda
[], GLfloat rgba
[][4])
2504 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2506 for (i
= 0; i
< n
; i
++) {
2507 sample_2d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2512 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2514 sample_lambda_2d_array(GLcontext
*ctx
,
2515 const struct gl_texture_object
*tObj
, GLuint n
,
2516 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2519 GLuint minStart
, minEnd
; /* texels with minification */
2520 GLuint magStart
, magEnd
; /* texels with magnification */
2523 ASSERT(lambda
!= NULL
);
2524 compute_min_mag_ranges(tObj
, n
, lambda
,
2525 &minStart
, &minEnd
, &magStart
, &magEnd
);
2527 if (minStart
< minEnd
) {
2528 /* do the minified texels */
2529 GLuint m
= minEnd
- minStart
;
2530 switch (tObj
->MinFilter
) {
2532 for (i
= minStart
; i
< minEnd
; i
++)
2533 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2534 texcoords
[i
], rgba
[i
]);
2537 for (i
= minStart
; i
< minEnd
; i
++)
2538 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2539 texcoords
[i
], rgba
[i
]);
2541 case GL_NEAREST_MIPMAP_NEAREST
:
2542 sample_2d_array_nearest_mipmap_nearest(ctx
, tObj
, m
,
2543 texcoords
+ minStart
,
2547 case GL_LINEAR_MIPMAP_NEAREST
:
2548 sample_2d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2549 texcoords
+ minStart
,
2553 case GL_NEAREST_MIPMAP_LINEAR
:
2554 sample_2d_array_nearest_mipmap_linear(ctx
, tObj
, m
,
2555 texcoords
+ minStart
,
2559 case GL_LINEAR_MIPMAP_LINEAR
:
2560 sample_2d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2561 texcoords
+ minStart
,
2566 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
2571 if (magStart
< magEnd
) {
2572 /* do the magnified texels */
2573 switch (tObj
->MagFilter
) {
2575 for (i
= magStart
; i
< magEnd
; i
++)
2576 sample_2d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2577 texcoords
[i
], rgba
[i
]);
2580 for (i
= magStart
; i
< magEnd
; i
++)
2581 sample_2d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2582 texcoords
[i
], rgba
[i
]);
2585 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
2594 /**********************************************************************/
2595 /* 1D Texture Array Sampling Functions */
2596 /**********************************************************************/
2599 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2602 sample_1d_array_nearest(GLcontext
*ctx
,
2603 const struct gl_texture_object
*tObj
,
2604 const struct gl_texture_image
*img
,
2605 const GLfloat texcoord
[4],
2608 const GLint width
= img
->Width2
; /* without border, power of two */
2609 const GLint height
= img
->Height
;
2614 i
= nearest_texel_location(tObj
->WrapS
, img
, width
, texcoord
[0]);
2615 array
= tex_array_slice(texcoord
[1], height
);
2617 if (i
< 0 || i
>= (GLint
) img
->Width
||
2618 array
< 0 || array
>= (GLint
) img
->Height
) {
2619 /* Need this test for GL_CLAMP_TO_BORDER mode */
2620 get_border_color(tObj
, img
, rgba
);
2623 img
->FetchTexelf(img
, i
, array
, 0, rgba
);
2629 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2632 sample_1d_array_linear(GLcontext
*ctx
,
2633 const struct gl_texture_object
*tObj
,
2634 const struct gl_texture_image
*img
,
2635 const GLfloat texcoord
[4],
2638 const GLint width
= img
->Width2
;
2639 const GLint height
= img
->Height
;
2642 GLbitfield useBorderColor
= 0x0;
2644 GLfloat t0
[4], t1
[4];
2646 linear_texel_locations(tObj
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2647 array
= tex_array_slice(texcoord
[1], height
);
2654 /* check if sampling texture border color */
2655 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2656 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2659 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
2662 if (useBorderColor
& (I0BIT
| K0BIT
)) {
2663 get_border_color(tObj
, img
, t0
);
2666 img
->FetchTexelf(img
, i0
, array
, 0, t0
);
2668 if (useBorderColor
& (I1BIT
| K0BIT
)) {
2669 get_border_color(tObj
, img
, t1
);
2672 img
->FetchTexelf(img
, i1
, array
, 0, t1
);
2675 /* bilinear interpolation of samples */
2676 lerp_rgba(rgba
, a
, t0
, t1
);
2681 sample_1d_array_nearest_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 for (i
= 0; i
< n
; i
++) {
2688 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2689 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
],
2696 sample_1d_array_linear_mipmap_nearest(GLcontext
*ctx
,
2697 const struct gl_texture_object
*tObj
,
2698 GLuint n
, const GLfloat texcoord
[][4],
2699 const GLfloat lambda
[], GLfloat rgba
[][4])
2702 ASSERT(lambda
!= NULL
);
2703 for (i
= 0; i
< n
; i
++) {
2704 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2705 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
],
2706 texcoord
[i
], rgba
[i
]);
2712 sample_1d_array_nearest_mipmap_linear(GLcontext
*ctx
,
2713 const struct gl_texture_object
*tObj
,
2714 GLuint n
, const GLfloat texcoord
[][4],
2715 const GLfloat lambda
[], GLfloat rgba
[][4])
2718 ASSERT(lambda
!= NULL
);
2719 for (i
= 0; i
< n
; i
++) {
2720 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2721 if (level
>= tObj
->_MaxLevel
) {
2722 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2723 texcoord
[i
], rgba
[i
]);
2726 GLfloat t0
[4], t1
[4]; /* texels */
2727 const GLfloat f
= FRAC(lambda
[i
]);
2728 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2729 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2730 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2737 sample_1d_array_linear_mipmap_linear(GLcontext
*ctx
,
2738 const struct gl_texture_object
*tObj
,
2739 GLuint n
, const GLfloat texcoord
[][4],
2740 const GLfloat lambda
[], GLfloat rgba
[][4])
2743 ASSERT(lambda
!= NULL
);
2744 for (i
= 0; i
< n
; i
++) {
2745 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2746 if (level
>= tObj
->_MaxLevel
) {
2747 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->_MaxLevel
],
2748 texcoord
[i
], rgba
[i
]);
2751 GLfloat t0
[4], t1
[4]; /* texels */
2752 const GLfloat f
= FRAC(lambda
[i
]);
2753 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2754 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2755 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2761 /** Sample 1D Array texture, nearest filtering for both min/magnification */
2763 sample_nearest_1d_array(GLcontext
*ctx
,
2764 const struct gl_texture_object
*tObj
, GLuint n
,
2765 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2769 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2771 for (i
= 0; i
< n
; i
++) {
2772 sample_1d_array_nearest(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2777 /** Sample 1D Array texture, linear filtering for both min/magnification */
2779 sample_linear_1d_array(GLcontext
*ctx
,
2780 const struct gl_texture_object
*tObj
, GLuint n
,
2781 const GLfloat texcoords
[][4],
2782 const GLfloat lambda
[], GLfloat rgba
[][4])
2785 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2787 for (i
= 0; i
< n
; i
++) {
2788 sample_1d_array_linear(ctx
, tObj
, image
, texcoords
[i
], rgba
[i
]);
2793 /** Sample 1D Array texture, using lambda to choose between min/magnification */
2795 sample_lambda_1d_array(GLcontext
*ctx
,
2796 const struct gl_texture_object
*tObj
, GLuint n
,
2797 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2800 GLuint minStart
, minEnd
; /* texels with minification */
2801 GLuint magStart
, magEnd
; /* texels with magnification */
2804 ASSERT(lambda
!= NULL
);
2805 compute_min_mag_ranges(tObj
, n
, lambda
,
2806 &minStart
, &minEnd
, &magStart
, &magEnd
);
2808 if (minStart
< minEnd
) {
2809 /* do the minified texels */
2810 GLuint m
= minEnd
- minStart
;
2811 switch (tObj
->MinFilter
) {
2813 for (i
= minStart
; i
< minEnd
; i
++)
2814 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2815 texcoords
[i
], rgba
[i
]);
2818 for (i
= minStart
; i
< minEnd
; i
++)
2819 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2820 texcoords
[i
], rgba
[i
]);
2822 case GL_NEAREST_MIPMAP_NEAREST
:
2823 sample_1d_array_nearest_mipmap_nearest(ctx
, tObj
, m
, texcoords
+ minStart
,
2824 lambda
+ minStart
, rgba
+ minStart
);
2826 case GL_LINEAR_MIPMAP_NEAREST
:
2827 sample_1d_array_linear_mipmap_nearest(ctx
, tObj
, m
,
2828 texcoords
+ minStart
,
2832 case GL_NEAREST_MIPMAP_LINEAR
:
2833 sample_1d_array_nearest_mipmap_linear(ctx
, tObj
, m
, texcoords
+ minStart
,
2834 lambda
+ minStart
, rgba
+ minStart
);
2836 case GL_LINEAR_MIPMAP_LINEAR
:
2837 sample_1d_array_linear_mipmap_linear(ctx
, tObj
, m
,
2838 texcoords
+ minStart
,
2843 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
2848 if (magStart
< magEnd
) {
2849 /* do the magnified texels */
2850 switch (tObj
->MagFilter
) {
2852 for (i
= magStart
; i
< magEnd
; i
++)
2853 sample_1d_array_nearest(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2854 texcoords
[i
], rgba
[i
]);
2857 for (i
= magStart
; i
< magEnd
; i
++)
2858 sample_1d_array_linear(ctx
, tObj
, tObj
->Image
[0][tObj
->BaseLevel
],
2859 texcoords
[i
], rgba
[i
]);
2862 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
2870 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
2872 static INLINE GLfloat
2873 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
,
2878 return (coord
<= depthSample
) ? 1.0F
: ambient
;
2880 return (coord
>= depthSample
) ? 1.0F
: ambient
;
2882 return (coord
< depthSample
) ? 1.0F
: ambient
;
2884 return (coord
> depthSample
) ? 1.0F
: ambient
;
2886 return (coord
== depthSample
) ? 1.0F
: ambient
;
2888 return (coord
!= depthSample
) ? 1.0F
: ambient
;
2896 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
2903 * Compare texcoord against four depth samples.
2905 static INLINE GLfloat
2906 shadow_compare4(GLenum function
, GLfloat coord
,
2907 GLfloat depth00
, GLfloat depth01
,
2908 GLfloat depth10
, GLfloat depth11
,
2909 GLfloat ambient
, GLfloat wi
, GLfloat wj
)
2911 const GLfloat d
= (1.0F
- (GLfloat
) ambient
) * 0.25F
;
2912 GLfloat luminance
= 1.0F
;
2916 if (depth00
<= coord
) luminance
-= d
;
2917 if (depth01
<= coord
) luminance
-= d
;
2918 if (depth10
<= coord
) luminance
-= d
;
2919 if (depth11
<= coord
) luminance
-= d
;
2922 if (depth00
>= coord
) luminance
-= d
;
2923 if (depth01
>= coord
) luminance
-= d
;
2924 if (depth10
>= coord
) luminance
-= d
;
2925 if (depth11
>= coord
) luminance
-= d
;
2928 if (depth00
< coord
) luminance
-= d
;
2929 if (depth01
< coord
) luminance
-= d
;
2930 if (depth10
< coord
) luminance
-= d
;
2931 if (depth11
< coord
) luminance
-= d
;
2934 if (depth00
> coord
) luminance
-= d
;
2935 if (depth01
> coord
) luminance
-= d
;
2936 if (depth10
> coord
) luminance
-= d
;
2937 if (depth11
> coord
) luminance
-= d
;
2940 if (depth00
== coord
) luminance
-= d
;
2941 if (depth01
== coord
) luminance
-= d
;
2942 if (depth10
== coord
) luminance
-= d
;
2943 if (depth11
== coord
) luminance
-= d
;
2946 if (depth00
!= coord
) luminance
-= d
;
2947 if (depth01
!= coord
) luminance
-= d
;
2948 if (depth10
!= coord
) luminance
-= d
;
2949 if (depth11
!= coord
) luminance
-= d
;
2956 /* ordinary bilinear filtering */
2957 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
2959 _mesa_problem(NULL
, "Bad compare func in sample_depth_texture");
2966 * Sample a shadow/depth texture.
2969 sample_depth_texture( GLcontext
*ctx
,
2970 const struct gl_texture_object
*tObj
, GLuint n
,
2971 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2972 GLfloat texel
[][4] )
2974 const GLint baseLevel
= tObj
->BaseLevel
;
2975 const struct gl_texture_image
*img
= tObj
->Image
[0][baseLevel
];
2976 const GLint width
= img
->Width
;
2977 const GLint height
= img
->Height
;
2978 const GLint depth
= img
->Depth
;
2979 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
2987 ASSERT(img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
2988 img
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
2990 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
2991 tObj
->Target
== GL_TEXTURE_2D
||
2992 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
2993 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
2994 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
);
2996 ambient
= tObj
->CompareFailValue
;
2998 /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
3000 function
= (tObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
3001 tObj
->CompareFunc
: GL_NONE
;
3003 if (tObj
->MagFilter
== GL_NEAREST
) {
3005 for (i
= 0; i
< n
; i
++) {
3006 GLfloat depthSample
;
3007 GLint col
, row
, slice
;
3009 nearest_texcoord(tObj
, texcoords
[i
], &col
, &row
, &slice
);
3011 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3012 slice
>= 0 && slice
< depth
) {
3013 img
->FetchTexelf(img
, col
, row
, slice
, &depthSample
);
3016 depthSample
= tObj
->BorderColor
.f
[0];
3019 result
= shadow_compare(function
, texcoords
[i
][compare_coord
],
3020 depthSample
, ambient
);
3022 switch (tObj
->DepthMode
) {
3024 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3027 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3030 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3033 _mesa_problem(ctx
, "Bad depth texture mode");
3039 ASSERT(tObj
->MagFilter
== GL_LINEAR
);
3040 for (i
= 0; i
< n
; i
++) {
3041 GLfloat depth00
, depth01
, depth10
, depth11
;
3042 GLint i0
, i1
, j0
, j1
;
3045 GLuint useBorderTexel
;
3047 linear_texcoord(tObj
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3054 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3060 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3061 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3062 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3063 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3066 if (slice
< 0 || slice
>= (GLint
) depth
) {
3067 depth00
= tObj
->BorderColor
.f
[0];
3068 depth01
= tObj
->BorderColor
.f
[0];
3069 depth10
= tObj
->BorderColor
.f
[0];
3070 depth11
= tObj
->BorderColor
.f
[0];
3073 /* get four depth samples from the texture */
3074 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3075 depth00
= tObj
->BorderColor
.f
[0];
3078 img
->FetchTexelf(img
, i0
, j0
, slice
, &depth00
);
3080 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3081 depth10
= tObj
->BorderColor
.f
[0];
3084 img
->FetchTexelf(img
, i1
, j0
, slice
, &depth10
);
3087 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3088 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3089 depth01
= tObj
->BorderColor
.f
[0];
3092 img
->FetchTexelf(img
, i0
, j1
, slice
, &depth01
);
3094 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3095 depth11
= tObj
->BorderColor
.f
[0];
3098 img
->FetchTexelf(img
, i1
, j1
, slice
, &depth11
);
3107 result
= shadow_compare4(function
, texcoords
[i
][compare_coord
],
3108 depth00
, depth01
, depth10
, depth11
,
3111 switch (tObj
->DepthMode
) {
3113 ASSIGN_4V(texel
[i
], result
, result
, result
, 1.0F
);
3116 ASSIGN_4V(texel
[i
], result
, result
, result
, result
);
3119 ASSIGN_4V(texel
[i
], 0.0F
, 0.0F
, 0.0F
, result
);
3122 _mesa_problem(ctx
, "Bad depth texture mode");
3131 * We use this function when a texture object is in an "incomplete" state.
3132 * When a fragment program attempts to sample an incomplete texture we
3133 * return black (see issue 23 in GL_ARB_fragment_program spec).
3134 * Note: fragment programs don't observe the texture enable/disable flags.
3137 null_sample_func( GLcontext
*ctx
,
3138 const struct gl_texture_object
*tObj
, GLuint n
,
3139 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3147 for (i
= 0; i
< n
; i
++) {
3151 rgba
[i
][ACOMP
] = 1.0;
3157 * Choose the texture sampling function for the given texture object.
3160 _swrast_choose_texture_sample_func( GLcontext
*ctx
,
3161 const struct gl_texture_object
*t
)
3163 if (!t
|| !t
->_Complete
) {
3164 return &null_sample_func
;
3167 const GLboolean needLambda
= (GLboolean
) (t
->MinFilter
!= t
->MagFilter
);
3168 const GLenum format
= t
->Image
[0][t
->BaseLevel
]->_BaseFormat
;
3170 switch (t
->Target
) {
3172 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3173 return &sample_depth_texture
;
3175 else if (needLambda
) {
3176 return &sample_lambda_1d
;
3178 else if (t
->MinFilter
== GL_LINEAR
) {
3179 return &sample_linear_1d
;
3182 ASSERT(t
->MinFilter
== GL_NEAREST
);
3183 return &sample_nearest_1d
;
3186 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3187 return &sample_depth_texture
;
3189 else if (needLambda
) {
3190 return &sample_lambda_2d
;
3192 else if (t
->MinFilter
== GL_LINEAR
) {
3193 return &sample_linear_2d
;
3196 /* check for a few optimized cases */
3197 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3198 ASSERT(t
->MinFilter
== GL_NEAREST
);
3199 if (t
->WrapS
== GL_REPEAT
&&
3200 t
->WrapT
== GL_REPEAT
&&
3201 img
->_IsPowerOfTwo
&&
3203 img
->TexFormat
== MESA_FORMAT_RGB888
) {
3204 return &opt_sample_rgb_2d
;
3206 else if (t
->WrapS
== GL_REPEAT
&&
3207 t
->WrapT
== GL_REPEAT
&&
3208 img
->_IsPowerOfTwo
&&
3210 img
->TexFormat
== MESA_FORMAT_RGBA8888
) {
3211 return &opt_sample_rgba_2d
;
3214 return &sample_nearest_2d
;
3219 return &sample_lambda_3d
;
3221 else if (t
->MinFilter
== GL_LINEAR
) {
3222 return &sample_linear_3d
;
3225 ASSERT(t
->MinFilter
== GL_NEAREST
);
3226 return &sample_nearest_3d
;
3228 case GL_TEXTURE_CUBE_MAP
:
3230 return &sample_lambda_cube
;
3232 else if (t
->MinFilter
== GL_LINEAR
) {
3233 return &sample_linear_cube
;
3236 ASSERT(t
->MinFilter
== GL_NEAREST
);
3237 return &sample_nearest_cube
;
3239 case GL_TEXTURE_RECTANGLE_NV
:
3240 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
) {
3241 return &sample_depth_texture
;
3243 else if (needLambda
) {
3244 return &sample_lambda_rect
;
3246 else if (t
->MinFilter
== GL_LINEAR
) {
3247 return &sample_linear_rect
;
3250 ASSERT(t
->MinFilter
== GL_NEAREST
);
3251 return &sample_nearest_rect
;
3253 case GL_TEXTURE_1D_ARRAY_EXT
:
3255 return &sample_lambda_1d_array
;
3257 else if (t
->MinFilter
== GL_LINEAR
) {
3258 return &sample_linear_1d_array
;
3261 ASSERT(t
->MinFilter
== GL_NEAREST
);
3262 return &sample_nearest_1d_array
;
3264 case GL_TEXTURE_2D_ARRAY_EXT
:
3266 return &sample_lambda_2d_array
;
3268 else if (t
->MinFilter
== GL_LINEAR
) {
3269 return &sample_linear_2d_array
;
3272 ASSERT(t
->MinFilter
== GL_NEAREST
);
3273 return &sample_nearest_2d_array
;
3277 "invalid target in _swrast_choose_texture_sample_func");
3278 return &null_sample_func
;