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 * THE AUTHORS OR COPYRIGHT HOLDERS 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/texobj.h"
31 #include "main/samplerobj.h"
33 #include "s_context.h"
34 #include "s_texfilter.h"
38 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
39 * see 1-pixel bands of improperly weighted linear-filtered textures.
40 * The tests/texwrap.c demo is a good test.
41 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
42 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
44 #define FRAC(f) ((f) - IFLOOR(f))
49 * Linear interpolation macro
51 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
55 * Do 2D/biliner interpolation of float values.
56 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
57 * a and b are the horizontal and vertical interpolants.
58 * It's important that this function is inlined when compiled with
59 * optimization! If we find that's not true on some systems, convert
63 lerp_2d(GLfloat a
, GLfloat b
,
64 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
66 const GLfloat temp0
= LERP(a
, v00
, v10
);
67 const GLfloat temp1
= LERP(a
, v01
, v11
);
68 return LERP(b
, temp0
, temp1
);
73 * Do 3D/trilinear interpolation of float values.
77 lerp_3d(GLfloat a
, GLfloat b
, GLfloat c
,
78 GLfloat v000
, GLfloat v100
, GLfloat v010
, GLfloat v110
,
79 GLfloat v001
, GLfloat v101
, GLfloat v011
, GLfloat v111
)
81 const GLfloat temp00
= LERP(a
, v000
, v100
);
82 const GLfloat temp10
= LERP(a
, v010
, v110
);
83 const GLfloat temp01
= LERP(a
, v001
, v101
);
84 const GLfloat temp11
= LERP(a
, v011
, v111
);
85 const GLfloat temp0
= LERP(b
, temp00
, temp10
);
86 const GLfloat temp1
= LERP(b
, temp01
, temp11
);
87 return LERP(c
, temp0
, temp1
);
92 * Do linear interpolation of colors.
95 lerp_rgba(GLfloat result
[4], GLfloat t
, const GLfloat a
[4], const GLfloat b
[4])
97 result
[0] = LERP(t
, a
[0], b
[0]);
98 result
[1] = LERP(t
, a
[1], b
[1]);
99 result
[2] = LERP(t
, a
[2], b
[2]);
100 result
[3] = LERP(t
, a
[3], b
[3]);
105 * Do bilinear interpolation of colors.
108 lerp_rgba_2d(GLfloat result
[4], GLfloat a
, GLfloat b
,
109 const GLfloat t00
[4], const GLfloat t10
[4],
110 const GLfloat t01
[4], const GLfloat t11
[4])
112 result
[0] = lerp_2d(a
, b
, t00
[0], t10
[0], t01
[0], t11
[0]);
113 result
[1] = lerp_2d(a
, b
, t00
[1], t10
[1], t01
[1], t11
[1]);
114 result
[2] = lerp_2d(a
, b
, t00
[2], t10
[2], t01
[2], t11
[2]);
115 result
[3] = lerp_2d(a
, b
, t00
[3], t10
[3], t01
[3], t11
[3]);
120 * Do trilinear interpolation of colors.
123 lerp_rgba_3d(GLfloat result
[4], GLfloat a
, GLfloat b
, GLfloat c
,
124 const GLfloat t000
[4], const GLfloat t100
[4],
125 const GLfloat t010
[4], const GLfloat t110
[4],
126 const GLfloat t001
[4], const GLfloat t101
[4],
127 const GLfloat t011
[4], const GLfloat t111
[4])
130 /* compiler should unroll these short loops */
131 for (k
= 0; k
< 4; k
++) {
132 result
[k
] = lerp_3d(a
, b
, c
, t000
[k
], t100
[k
], t010
[k
], t110
[k
],
133 t001
[k
], t101
[k
], t011
[k
], t111
[k
]);
139 * Used for GL_REPEAT wrap mode. Using A % B doesn't produce the
140 * right results for A<0. Casting to A to be unsigned only works if B
141 * is a power of two. Adding a bias to A (which is a multiple of B)
142 * avoids the problems with A < 0 (for reasonable A) without using a
145 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
149 * Used to compute texel locations for linear sampling.
151 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
152 * s = texcoord in [0,1]
153 * size = width (or height or depth) of texture
155 * i0, i1 = returns two nearest texel indexes
156 * weight = returns blend factor between texels
159 linear_texel_locations(GLenum wrapMode
,
160 const struct gl_texture_image
*img
,
161 GLint size
, GLfloat s
,
162 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
164 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
169 if (swImg
->_IsPowerOfTwo
) {
170 *i0
= IFLOOR(u
) & (size
- 1);
171 *i1
= (*i0
+ 1) & (size
- 1);
174 *i0
= REMAINDER(IFLOOR(u
), size
);
175 *i1
= REMAINDER(*i0
+ 1, size
);
178 case GL_CLAMP_TO_EDGE
:
190 if (*i1
>= (GLint
) size
)
193 case GL_CLAMP_TO_BORDER
:
195 const GLfloat min
= -1.0F
/ (2.0F
* size
);
196 const GLfloat max
= 1.0F
- min
;
208 case GL_MIRRORED_REPEAT
:
210 const GLint flr
= IFLOOR(s
);
212 u
= 1.0F
- (s
- (GLfloat
) flr
);
214 u
= s
- (GLfloat
) flr
;
215 u
= (u
* size
) - 0.5F
;
220 if (*i1
>= (GLint
) size
)
224 case GL_MIRROR_CLAMP_EXT
:
234 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
245 if (*i1
>= (GLint
) size
)
248 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
250 const GLfloat min
= -1.0F
/ (2.0F
* size
);
251 const GLfloat max
= 1.0F
- min
;
276 _mesa_problem(NULL
, "Bad wrap mode");
285 * Used to compute texel location for nearest sampling.
288 nearest_texel_location(GLenum wrapMode
,
289 const struct gl_texture_image
*img
,
290 GLint size
, GLfloat s
)
292 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
297 /* s limited to [0,1) */
298 /* i limited to [0,size-1] */
299 i
= IFLOOR(s
* size
);
300 if (swImg
->_IsPowerOfTwo
)
303 i
= REMAINDER(i
, size
);
305 case GL_CLAMP_TO_EDGE
:
307 /* s limited to [min,max] */
308 /* i limited to [0, size-1] */
309 const GLfloat min
= 1.0F
/ (2.0F
* size
);
310 const GLfloat max
= 1.0F
- min
;
316 i
= IFLOOR(s
* size
);
319 case GL_CLAMP_TO_BORDER
:
321 /* s limited to [min,max] */
322 /* i limited to [-1, size] */
323 const GLfloat min
= -1.0F
/ (2.0F
* size
);
324 const GLfloat max
= 1.0F
- min
;
330 i
= IFLOOR(s
* size
);
333 case GL_MIRRORED_REPEAT
:
335 const GLfloat min
= 1.0F
/ (2.0F
* size
);
336 const GLfloat max
= 1.0F
- min
;
337 const GLint flr
= IFLOOR(s
);
340 u
= 1.0F
- (s
- (GLfloat
) flr
);
342 u
= s
- (GLfloat
) flr
;
348 i
= IFLOOR(u
* size
);
351 case GL_MIRROR_CLAMP_EXT
:
353 /* s limited to [0,1] */
354 /* i limited to [0,size-1] */
355 const GLfloat u
= FABSF(s
);
361 i
= IFLOOR(u
* size
);
364 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
366 /* s limited to [min,max] */
367 /* i limited to [0, size-1] */
368 const GLfloat min
= 1.0F
/ (2.0F
* size
);
369 const GLfloat max
= 1.0F
- min
;
370 const GLfloat u
= FABSF(s
);
376 i
= IFLOOR(u
* size
);
379 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
381 /* s limited to [min,max] */
382 /* i limited to [0, size-1] */
383 const GLfloat min
= -1.0F
/ (2.0F
* size
);
384 const GLfloat max
= 1.0F
- min
;
385 const GLfloat u
= FABSF(s
);
391 i
= IFLOOR(u
* size
);
395 /* s limited to [0,1] */
396 /* i limited to [0,size-1] */
402 i
= IFLOOR(s
* size
);
405 _mesa_problem(NULL
, "Bad wrap mode");
411 /* Power of two image sizes only */
413 linear_repeat_texel_location(GLuint size
, GLfloat s
,
414 GLint
*i0
, GLint
*i1
, GLfloat
*weight
)
416 GLfloat u
= s
* size
- 0.5F
;
417 *i0
= IFLOOR(u
) & (size
- 1);
418 *i1
= (*i0
+ 1) & (size
- 1);
424 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
427 clamp_rect_coord_nearest(GLenum wrapMode
, GLfloat coord
, GLint max
)
431 return IFLOOR( CLAMP(coord
, 0.0F
, max
- 1) );
432 case GL_CLAMP_TO_EDGE
:
433 return IFLOOR( CLAMP(coord
, 0.5F
, max
- 0.5F
) );
434 case GL_CLAMP_TO_BORDER
:
435 return IFLOOR( CLAMP(coord
, -0.5F
, max
+ 0.5F
) );
437 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_nearest");
444 * As above, but GL_LINEAR filtering.
447 clamp_rect_coord_linear(GLenum wrapMode
, GLfloat coord
, GLint max
,
448 GLint
*i0out
, GLint
*i1out
, GLfloat
*weight
)
454 /* Not exactly what the spec says, but it matches NVIDIA output */
455 fcol
= CLAMP(coord
- 0.5F
, 0.0F
, max
- 1);
459 case GL_CLAMP_TO_EDGE
:
460 fcol
= CLAMP(coord
, 0.5F
, max
- 0.5F
);
467 case GL_CLAMP_TO_BORDER
:
468 fcol
= CLAMP(coord
, -0.5F
, max
+ 0.5F
);
474 _mesa_problem(NULL
, "bad wrapMode in clamp_rect_coord_linear");
481 *weight
= FRAC(fcol
);
486 * Compute slice/image to use for 1D or 2D array texture.
489 tex_array_slice(GLfloat coord
, GLsizei size
)
491 GLint slice
= IFLOOR(coord
+ 0.5f
);
492 slice
= CLAMP(slice
, 0, size
- 1);
498 * Compute nearest integer texcoords for given texobj and coordinate.
499 * NOTE: only used for depth texture sampling.
502 nearest_texcoord(const struct gl_sampler_object
*samp
,
503 const struct gl_texture_object
*texObj
,
505 const GLfloat texcoord
[4],
506 GLint
*i
, GLint
*j
, GLint
*k
)
508 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
509 const GLint width
= img
->Width
;
510 const GLint height
= img
->Height
;
511 const GLint depth
= img
->Depth
;
513 switch (texObj
->Target
) {
514 case GL_TEXTURE_RECTANGLE_ARB
:
515 *i
= clamp_rect_coord_nearest(samp
->WrapS
, texcoord
[0], width
);
516 *j
= clamp_rect_coord_nearest(samp
->WrapT
, texcoord
[1], height
);
520 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
525 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
526 *j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
529 case GL_TEXTURE_1D_ARRAY_EXT
:
530 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
531 *j
= tex_array_slice(texcoord
[1], height
);
534 case GL_TEXTURE_2D_ARRAY_EXT
:
535 *i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
536 *j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
537 *k
= tex_array_slice(texcoord
[2], depth
);
547 * Compute linear integer texcoords for given texobj and coordinate.
548 * NOTE: only used for depth texture sampling.
551 linear_texcoord(const struct gl_sampler_object
*samp
,
552 const struct gl_texture_object
*texObj
,
554 const GLfloat texcoord
[4],
555 GLint
*i0
, GLint
*i1
, GLint
*j0
, GLint
*j1
, GLint
*slice
,
556 GLfloat
*wi
, GLfloat
*wj
)
558 const struct gl_texture_image
*img
= texObj
->Image
[0][level
];
559 const GLint width
= img
->Width
;
560 const GLint height
= img
->Height
;
561 const GLint depth
= img
->Depth
;
563 switch (texObj
->Target
) {
564 case GL_TEXTURE_RECTANGLE_ARB
:
565 clamp_rect_coord_linear(samp
->WrapS
, texcoord
[0],
567 clamp_rect_coord_linear(samp
->WrapT
, texcoord
[1],
574 linear_texel_locations(samp
->WrapS
, img
, width
,
575 texcoord
[0], i0
, i1
, wi
);
576 linear_texel_locations(samp
->WrapT
, img
, height
,
577 texcoord
[1], j0
, j1
, wj
);
581 case GL_TEXTURE_1D_ARRAY_EXT
:
582 linear_texel_locations(samp
->WrapS
, img
, width
,
583 texcoord
[0], i0
, i1
, wi
);
584 *j0
= tex_array_slice(texcoord
[1], height
);
589 case GL_TEXTURE_2D_ARRAY_EXT
:
590 linear_texel_locations(samp
->WrapS
, img
, width
,
591 texcoord
[0], i0
, i1
, wi
);
592 linear_texel_locations(samp
->WrapT
, img
, height
,
593 texcoord
[1], j0
, j1
, wj
);
594 *slice
= tex_array_slice(texcoord
[2], depth
);
606 * For linear interpolation between mipmap levels N and N+1, this function
610 linear_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
613 return tObj
->BaseLevel
;
614 else if (lambda
> tObj
->_MaxLambda
)
615 return (GLint
) (tObj
->BaseLevel
+ tObj
->_MaxLambda
);
617 return (GLint
) (tObj
->BaseLevel
+ lambda
);
622 * Compute the nearest mipmap level to take texels from.
625 nearest_mipmap_level(const struct gl_texture_object
*tObj
, GLfloat lambda
)
631 else if (lambda
> tObj
->_MaxLambda
+ 0.4999F
)
632 l
= tObj
->_MaxLambda
+ 0.4999F
;
635 level
= (GLint
) (tObj
->BaseLevel
+ l
+ 0.5F
);
636 if (level
> tObj
->_MaxLevel
)
637 level
= tObj
->_MaxLevel
;
644 * Bitflags for texture border color sampling.
656 * The lambda[] array values are always monotonic. Either the whole span
657 * will be minified, magnified, or split between the two. This function
658 * determines the subranges in [0, n-1] that are to be minified or magnified.
661 compute_min_mag_ranges(const struct gl_sampler_object
*samp
,
662 GLuint n
, const GLfloat lambda
[],
663 GLuint
*minStart
, GLuint
*minEnd
,
664 GLuint
*magStart
, GLuint
*magEnd
)
666 GLfloat minMagThresh
;
668 /* we shouldn't be here if minfilter == magfilter */
669 ASSERT(samp
->MinFilter
!= samp
->MagFilter
);
671 /* This bit comes from the OpenGL spec: */
672 if (samp
->MagFilter
== GL_LINEAR
673 && (samp
->MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
674 samp
->MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
682 /* DEBUG CODE: Verify that lambda[] is monotonic.
683 * We can't really use this because the inaccuracy in the LOG2 function
684 * causes this test to fail, yet the resulting texturing is correct.
688 printf("lambda delta = %g\n", lambda
[0] - lambda
[n
-1]);
689 if (lambda
[0] >= lambda
[n
-1]) { /* decreasing */
690 for (i
= 0; i
< n
- 1; i
++) {
691 ASSERT((GLint
) (lambda
[i
] * 10) >= (GLint
) (lambda
[i
+1] * 10));
694 else { /* increasing */
695 for (i
= 0; i
< n
- 1; i
++) {
696 ASSERT((GLint
) (lambda
[i
] * 10) <= (GLint
) (lambda
[i
+1] * 10));
702 if (lambda
[0] <= minMagThresh
&& (n
<= 1 || lambda
[n
-1] <= minMagThresh
)) {
703 /* magnification for whole span */
706 *minStart
= *minEnd
= 0;
708 else if (lambda
[0] > minMagThresh
&& (n
<=1 || lambda
[n
-1] > minMagThresh
)) {
709 /* minification for whole span */
712 *magStart
= *magEnd
= 0;
715 /* a mix of minification and magnification */
717 if (lambda
[0] > minMagThresh
) {
718 /* start with minification */
719 for (i
= 1; i
< n
; i
++) {
720 if (lambda
[i
] <= minMagThresh
)
729 /* start with magnification */
730 for (i
= 1; i
< n
; i
++) {
731 if (lambda
[i
] > minMagThresh
)
742 /* Verify the min/mag Start/End values
743 * We don't use this either (see above)
747 for (i
= 0; i
< n
; i
++) {
748 if (lambda
[i
] > minMagThresh
) {
750 ASSERT(i
>= *minStart
);
755 ASSERT(i
>= *magStart
);
765 * When we sample the border color, it must be interpreted according to
766 * the base texture format. Ex: if the texture base format it GL_ALPHA,
767 * we return (0,0,0,BorderAlpha).
770 get_border_color(const struct gl_sampler_object
*samp
,
771 const struct gl_texture_image
*img
,
774 switch (img
->_BaseFormat
) {
776 rgba
[0] = samp
->BorderColor
.f
[0];
777 rgba
[1] = samp
->BorderColor
.f
[1];
778 rgba
[2] = samp
->BorderColor
.f
[2];
782 rgba
[0] = rgba
[1] = rgba
[2] = 0.0;
783 rgba
[3] = samp
->BorderColor
.f
[3];
786 rgba
[0] = rgba
[1] = rgba
[2] = samp
->BorderColor
.f
[0];
789 case GL_LUMINANCE_ALPHA
:
790 rgba
[0] = rgba
[1] = rgba
[2] = samp
->BorderColor
.f
[0];
791 rgba
[3] = samp
->BorderColor
.f
[3];
794 rgba
[0] = rgba
[1] = rgba
[2] = rgba
[3] = samp
->BorderColor
.f
[0];
797 COPY_4V(rgba
, samp
->BorderColor
.f
);
804 * Put z into texel according to GL_DEPTH_MODE.
807 apply_depth_mode(GLenum depthMode
, GLfloat z
, GLfloat texel
[4])
811 ASSIGN_4V(texel
, z
, z
, z
, 1.0F
);
814 ASSIGN_4V(texel
, z
, z
, z
, z
);
817 ASSIGN_4V(texel
, 0.0F
, 0.0F
, 0.0F
, z
);
820 ASSIGN_4V(texel
, z
, 0.0F
, 0.0F
, 1.0F
);
823 _mesa_problem(NULL
, "Bad depth texture mode");
829 * Is the given texture a depth (or depth/stencil) texture?
832 is_depth_texture(const struct gl_texture_object
*tObj
)
834 GLenum format
= tObj
->Image
[0][tObj
->BaseLevel
]->_BaseFormat
;
835 return format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL_EXT
;
839 /**********************************************************************/
840 /* 1-D Texture Sampling Functions */
841 /**********************************************************************/
844 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
847 sample_1d_nearest(struct gl_context
*ctx
,
848 const struct gl_sampler_object
*samp
,
849 const struct gl_texture_image
*img
,
850 const GLfloat texcoord
[4], GLfloat rgba
[4])
852 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
853 const GLint width
= img
->Width2
; /* without border, power of two */
855 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
856 /* skip over the border, if any */
858 if (i
< 0 || i
>= (GLint
) img
->Width
) {
859 /* Need this test for GL_CLAMP_TO_BORDER mode */
860 get_border_color(samp
, img
, rgba
);
863 swImg
->FetchTexel(swImg
, i
, 0, 0, rgba
);
869 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
872 sample_1d_linear(struct gl_context
*ctx
,
873 const struct gl_sampler_object
*samp
,
874 const struct gl_texture_image
*img
,
875 const GLfloat texcoord
[4], GLfloat rgba
[4])
877 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
878 const GLint width
= img
->Width2
;
880 GLbitfield useBorderColor
= 0x0;
882 GLfloat t0
[4], t1
[4]; /* texels */
884 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
891 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
892 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
895 /* fetch texel colors */
896 if (useBorderColor
& I0BIT
) {
897 get_border_color(samp
, img
, t0
);
900 swImg
->FetchTexel(swImg
, i0
, 0, 0, t0
);
902 if (useBorderColor
& I1BIT
) {
903 get_border_color(samp
, img
, t1
);
906 swImg
->FetchTexel(swImg
, i1
, 0, 0, t1
);
909 lerp_rgba(rgba
, a
, t0
, t1
);
914 sample_1d_nearest_mipmap_nearest(struct gl_context
*ctx
,
915 const struct gl_sampler_object
*samp
,
916 const struct gl_texture_object
*tObj
,
917 GLuint n
, const GLfloat texcoord
[][4],
918 const GLfloat lambda
[], GLfloat rgba
[][4])
921 ASSERT(lambda
!= NULL
);
922 for (i
= 0; i
< n
; i
++) {
923 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
924 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
930 sample_1d_linear_mipmap_nearest(struct gl_context
*ctx
,
931 const struct gl_sampler_object
*samp
,
932 const struct gl_texture_object
*tObj
,
933 GLuint n
, const GLfloat texcoord
[][4],
934 const GLfloat lambda
[], GLfloat rgba
[][4])
937 ASSERT(lambda
!= NULL
);
938 for (i
= 0; i
< n
; i
++) {
939 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
940 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
946 sample_1d_nearest_mipmap_linear(struct gl_context
*ctx
,
947 const struct gl_sampler_object
*samp
,
948 const struct gl_texture_object
*tObj
,
949 GLuint n
, const GLfloat texcoord
[][4],
950 const GLfloat lambda
[], GLfloat rgba
[][4])
953 ASSERT(lambda
!= NULL
);
954 for (i
= 0; i
< n
; i
++) {
955 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
956 if (level
>= tObj
->_MaxLevel
) {
957 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
958 texcoord
[i
], rgba
[i
]);
961 GLfloat t0
[4], t1
[4];
962 const GLfloat f
= FRAC(lambda
[i
]);
963 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
964 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
965 lerp_rgba(rgba
[i
], f
, t0
, t1
);
972 sample_1d_linear_mipmap_linear(struct gl_context
*ctx
,
973 const struct gl_sampler_object
*samp
,
974 const struct gl_texture_object
*tObj
,
975 GLuint n
, const GLfloat texcoord
[][4],
976 const GLfloat lambda
[], GLfloat rgba
[][4])
979 ASSERT(lambda
!= NULL
);
980 for (i
= 0; i
< n
; i
++) {
981 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
982 if (level
>= tObj
->_MaxLevel
) {
983 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
984 texcoord
[i
], rgba
[i
]);
987 GLfloat t0
[4], t1
[4];
988 const GLfloat f
= FRAC(lambda
[i
]);
989 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
990 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
991 lerp_rgba(rgba
[i
], f
, t0
, t1
);
997 /** Sample 1D texture, nearest filtering for both min/magnification */
999 sample_nearest_1d( struct gl_context
*ctx
,
1000 const struct gl_sampler_object
*samp
,
1001 const struct gl_texture_object
*tObj
, GLuint n
,
1002 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1006 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1008 for (i
= 0; i
< n
; i
++) {
1009 sample_1d_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1014 /** Sample 1D texture, linear filtering for both min/magnification */
1016 sample_linear_1d( struct gl_context
*ctx
,
1017 const struct gl_sampler_object
*samp
,
1018 const struct gl_texture_object
*tObj
, GLuint n
,
1019 const GLfloat texcoords
[][4], const GLfloat lambda
[],
1023 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1025 for (i
= 0; i
< n
; i
++) {
1026 sample_1d_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1031 /** Sample 1D texture, using lambda to choose between min/magnification */
1033 sample_lambda_1d( struct gl_context
*ctx
,
1034 const struct gl_sampler_object
*samp
,
1035 const struct gl_texture_object
*tObj
, GLuint n
,
1036 const GLfloat texcoords
[][4],
1037 const GLfloat lambda
[], GLfloat rgba
[][4] )
1039 GLuint minStart
, minEnd
; /* texels with minification */
1040 GLuint magStart
, magEnd
; /* texels with magnification */
1043 ASSERT(lambda
!= NULL
);
1044 compute_min_mag_ranges(samp
, n
, lambda
,
1045 &minStart
, &minEnd
, &magStart
, &magEnd
);
1047 if (minStart
< minEnd
) {
1048 /* do the minified texels */
1049 const GLuint m
= minEnd
- minStart
;
1050 switch (samp
->MinFilter
) {
1052 for (i
= minStart
; i
< minEnd
; i
++)
1053 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
1054 texcoords
[i
], rgba
[i
]);
1057 for (i
= minStart
; i
< minEnd
; i
++)
1058 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
1059 texcoords
[i
], rgba
[i
]);
1061 case GL_NEAREST_MIPMAP_NEAREST
:
1062 sample_1d_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1063 lambda
+ minStart
, rgba
+ minStart
);
1065 case GL_LINEAR_MIPMAP_NEAREST
:
1066 sample_1d_linear_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1067 lambda
+ minStart
, rgba
+ minStart
);
1069 case GL_NEAREST_MIPMAP_LINEAR
:
1070 sample_1d_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1071 lambda
+ minStart
, rgba
+ minStart
);
1073 case GL_LINEAR_MIPMAP_LINEAR
:
1074 sample_1d_linear_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1075 lambda
+ minStart
, rgba
+ minStart
);
1078 _mesa_problem(ctx
, "Bad min filter in sample_1d_texture");
1083 if (magStart
< magEnd
) {
1084 /* do the magnified texels */
1085 switch (samp
->MagFilter
) {
1087 for (i
= magStart
; i
< magEnd
; i
++)
1088 sample_1d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
1089 texcoords
[i
], rgba
[i
]);
1092 for (i
= magStart
; i
< magEnd
; i
++)
1093 sample_1d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
1094 texcoords
[i
], rgba
[i
]);
1097 _mesa_problem(ctx
, "Bad mag filter in sample_1d_texture");
1104 /**********************************************************************/
1105 /* 2-D Texture Sampling Functions */
1106 /**********************************************************************/
1110 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1113 sample_2d_nearest(struct gl_context
*ctx
,
1114 const struct gl_sampler_object
*samp
,
1115 const struct gl_texture_image
*img
,
1116 const GLfloat texcoord
[4],
1119 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1120 const GLint width
= img
->Width2
; /* without border, power of two */
1121 const GLint height
= img
->Height2
; /* without border, power of two */
1125 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
1126 j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
1128 /* skip over the border, if any */
1132 if (i
< 0 || i
>= (GLint
) img
->Width
|| j
< 0 || j
>= (GLint
) img
->Height
) {
1133 /* Need this test for GL_CLAMP_TO_BORDER mode */
1134 get_border_color(samp
, img
, rgba
);
1137 swImg
->FetchTexel(swImg
, i
, j
, 0, rgba
);
1143 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1144 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1147 sample_2d_linear(struct gl_context
*ctx
,
1148 const struct gl_sampler_object
*samp
,
1149 const struct gl_texture_image
*img
,
1150 const GLfloat texcoord
[4],
1153 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1154 const GLint width
= img
->Width2
;
1155 const GLint height
= img
->Height2
;
1156 GLint i0
, j0
, i1
, j1
;
1157 GLbitfield useBorderColor
= 0x0;
1159 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1161 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
1162 linear_texel_locations(samp
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
1171 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
1172 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
1173 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
1174 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
1177 /* fetch four texel colors */
1178 if (useBorderColor
& (I0BIT
| J0BIT
)) {
1179 get_border_color(samp
, img
, t00
);
1182 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
1184 if (useBorderColor
& (I1BIT
| J0BIT
)) {
1185 get_border_color(samp
, img
, t10
);
1188 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
1190 if (useBorderColor
& (I0BIT
| J1BIT
)) {
1191 get_border_color(samp
, img
, t01
);
1194 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
1196 if (useBorderColor
& (I1BIT
| J1BIT
)) {
1197 get_border_color(samp
, img
, t11
);
1200 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
1203 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
1208 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1209 * We don't have to worry about the texture border.
1212 sample_2d_linear_repeat(struct gl_context
*ctx
,
1213 const struct gl_sampler_object
*samp
,
1214 const struct gl_texture_image
*img
,
1215 const GLfloat texcoord
[4],
1218 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1219 const GLint width
= img
->Width2
;
1220 const GLint height
= img
->Height2
;
1221 GLint i0
, j0
, i1
, j1
;
1223 GLfloat t00
[4], t10
[4], t01
[4], t11
[4]; /* sampled texel colors */
1227 ASSERT(samp
->WrapS
== GL_REPEAT
);
1228 ASSERT(samp
->WrapT
== GL_REPEAT
);
1229 ASSERT(img
->Border
== 0);
1230 ASSERT(swImg
->_IsPowerOfTwo
);
1232 linear_repeat_texel_location(width
, texcoord
[0], &i0
, &i1
, &wi
);
1233 linear_repeat_texel_location(height
, texcoord
[1], &j0
, &j1
, &wj
);
1235 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
1236 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
1237 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
1238 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
1240 lerp_rgba_2d(rgba
, wi
, wj
, t00
, t10
, t01
, t11
);
1245 sample_2d_nearest_mipmap_nearest(struct gl_context
*ctx
,
1246 const struct gl_sampler_object
*samp
,
1247 const struct gl_texture_object
*tObj
,
1248 GLuint n
, const GLfloat texcoord
[][4],
1249 const GLfloat lambda
[], GLfloat rgba
[][4])
1252 for (i
= 0; i
< n
; i
++) {
1253 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1254 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1260 sample_2d_linear_mipmap_nearest(struct gl_context
*ctx
,
1261 const struct gl_sampler_object
*samp
,
1262 const struct gl_texture_object
*tObj
,
1263 GLuint n
, const GLfloat texcoord
[][4],
1264 const GLfloat lambda
[], GLfloat rgba
[][4])
1267 ASSERT(lambda
!= NULL
);
1268 for (i
= 0; i
< n
; i
++) {
1269 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
1270 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
1276 sample_2d_nearest_mipmap_linear(struct gl_context
*ctx
,
1277 const struct gl_sampler_object
*samp
,
1278 const struct gl_texture_object
*tObj
,
1279 GLuint n
, const GLfloat texcoord
[][4],
1280 const GLfloat lambda
[], GLfloat rgba
[][4])
1283 ASSERT(lambda
!= NULL
);
1284 for (i
= 0; i
< n
; i
++) {
1285 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1286 if (level
>= tObj
->_MaxLevel
) {
1287 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1288 texcoord
[i
], rgba
[i
]);
1291 GLfloat t0
[4], t1
[4]; /* texels */
1292 const GLfloat f
= FRAC(lambda
[i
]);
1293 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1294 sample_2d_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1295 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1302 sample_2d_linear_mipmap_linear( struct gl_context
*ctx
,
1303 const struct gl_sampler_object
*samp
,
1304 const struct gl_texture_object
*tObj
,
1305 GLuint n
, const GLfloat texcoord
[][4],
1306 const GLfloat lambda
[], GLfloat rgba
[][4] )
1309 ASSERT(lambda
!= NULL
);
1310 for (i
= 0; i
< n
; i
++) {
1311 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1312 if (level
>= tObj
->_MaxLevel
) {
1313 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1314 texcoord
[i
], rgba
[i
]);
1317 GLfloat t0
[4], t1
[4]; /* texels */
1318 const GLfloat f
= FRAC(lambda
[i
]);
1319 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
1320 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
1321 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1328 sample_2d_linear_mipmap_linear_repeat(struct gl_context
*ctx
,
1329 const struct gl_sampler_object
*samp
,
1330 const struct gl_texture_object
*tObj
,
1331 GLuint n
, const GLfloat texcoord
[][4],
1332 const GLfloat lambda
[], GLfloat rgba
[][4])
1335 ASSERT(lambda
!= NULL
);
1336 ASSERT(samp
->WrapS
== GL_REPEAT
);
1337 ASSERT(samp
->WrapT
== GL_REPEAT
);
1338 for (i
= 0; i
< n
; i
++) {
1339 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
1340 if (level
>= tObj
->_MaxLevel
) {
1341 sample_2d_linear_repeat(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1342 texcoord
[i
], rgba
[i
]);
1345 GLfloat t0
[4], t1
[4]; /* texels */
1346 const GLfloat f
= FRAC(lambda
[i
]);
1347 sample_2d_linear_repeat(ctx
, samp
, tObj
->Image
[0][level
],
1349 sample_2d_linear_repeat(ctx
, samp
, tObj
->Image
[0][level
+1],
1351 lerp_rgba(rgba
[i
], f
, t0
, t1
);
1357 /** Sample 2D texture, nearest filtering for both min/magnification */
1359 sample_nearest_2d(struct gl_context
*ctx
,
1360 const struct gl_sampler_object
*samp
,
1361 const struct gl_texture_object
*tObj
, GLuint n
,
1362 const GLfloat texcoords
[][4],
1363 const GLfloat lambda
[], GLfloat rgba
[][4])
1366 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1368 for (i
= 0; i
< n
; i
++) {
1369 sample_2d_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1374 /** Sample 2D texture, linear filtering for both min/magnification */
1376 sample_linear_2d(struct gl_context
*ctx
,
1377 const struct gl_sampler_object
*samp
,
1378 const struct gl_texture_object
*tObj
, GLuint n
,
1379 const GLfloat texcoords
[][4],
1380 const GLfloat lambda
[], GLfloat rgba
[][4])
1383 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
1384 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(image
);
1386 if (samp
->WrapS
== GL_REPEAT
&&
1387 samp
->WrapT
== GL_REPEAT
&&
1388 swImg
->_IsPowerOfTwo
&&
1389 image
->Border
== 0) {
1390 for (i
= 0; i
< n
; i
++) {
1391 sample_2d_linear_repeat(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1395 for (i
= 0; i
< n
; i
++) {
1396 sample_2d_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
1403 * Optimized 2-D texture sampling:
1404 * S and T wrap mode == GL_REPEAT
1405 * GL_NEAREST min/mag filter
1407 * RowStride == Width,
1411 opt_sample_rgb_2d(struct gl_context
*ctx
,
1412 const struct gl_sampler_object
*samp
,
1413 const struct gl_texture_object
*tObj
,
1414 GLuint n
, const GLfloat texcoords
[][4],
1415 const GLfloat lambda
[], GLfloat rgba
[][4])
1417 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1418 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1419 const GLfloat width
= (GLfloat
) img
->Width
;
1420 const GLfloat height
= (GLfloat
) img
->Height
;
1421 const GLint colMask
= img
->Width
- 1;
1422 const GLint rowMask
= img
->Height
- 1;
1423 const GLint shift
= img
->WidthLog2
;
1427 ASSERT(samp
->WrapS
==GL_REPEAT
);
1428 ASSERT(samp
->WrapT
==GL_REPEAT
);
1429 ASSERT(img
->Border
==0);
1430 ASSERT(img
->TexFormat
== MESA_FORMAT_RGB888
);
1431 ASSERT(swImg
->_IsPowerOfTwo
);
1434 for (k
=0; k
<n
; k
++) {
1435 GLint i
= IFLOOR(texcoords
[k
][0] * width
) & colMask
;
1436 GLint j
= IFLOOR(texcoords
[k
][1] * height
) & rowMask
;
1437 GLint pos
= (j
<< shift
) | i
;
1438 GLubyte
*texel
= swImg
->Map
+ 3 * pos
;
1439 rgba
[k
][RCOMP
] = UBYTE_TO_FLOAT(texel
[2]);
1440 rgba
[k
][GCOMP
] = UBYTE_TO_FLOAT(texel
[1]);
1441 rgba
[k
][BCOMP
] = UBYTE_TO_FLOAT(texel
[0]);
1442 rgba
[k
][ACOMP
] = 1.0F
;
1448 * Optimized 2-D texture sampling:
1449 * S and T wrap mode == GL_REPEAT
1450 * GL_NEAREST min/mag filter
1452 * RowStride == Width,
1456 opt_sample_rgba_2d(struct gl_context
*ctx
,
1457 const struct gl_sampler_object
*samp
,
1458 const struct gl_texture_object
*tObj
,
1459 GLuint n
, const GLfloat texcoords
[][4],
1460 const GLfloat lambda
[], GLfloat rgba
[][4])
1462 const struct gl_texture_image
*img
= tObj
->Image
[0][tObj
->BaseLevel
];
1463 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
1464 const GLfloat width
= (GLfloat
) img
->Width
;
1465 const GLfloat height
= (GLfloat
) img
->Height
;
1466 const GLint colMask
= img
->Width
- 1;
1467 const GLint rowMask
= img
->Height
- 1;
1468 const GLint shift
= img
->WidthLog2
;
1472 ASSERT(samp
->WrapS
==GL_REPEAT
);
1473 ASSERT(samp
->WrapT
==GL_REPEAT
);
1474 ASSERT(img
->Border
==0);
1475 ASSERT(img
->TexFormat
== MESA_FORMAT_RGBA8888
);
1476 ASSERT(swImg
->_IsPowerOfTwo
);
1479 for (i
= 0; i
< n
; i
++) {
1480 const GLint col
= IFLOOR(texcoords
[i
][0] * width
) & colMask
;
1481 const GLint row
= IFLOOR(texcoords
[i
][1] * height
) & rowMask
;
1482 const GLint pos
= (row
<< shift
) | col
;
1483 const GLuint texel
= *((GLuint
*) swImg
->Map
+ pos
);
1484 rgba
[i
][RCOMP
] = UBYTE_TO_FLOAT( (texel
>> 24) );
1485 rgba
[i
][GCOMP
] = UBYTE_TO_FLOAT( (texel
>> 16) & 0xff );
1486 rgba
[i
][BCOMP
] = UBYTE_TO_FLOAT( (texel
>> 8) & 0xff );
1487 rgba
[i
][ACOMP
] = UBYTE_TO_FLOAT( (texel
) & 0xff );
1492 /** Sample 2D texture, using lambda to choose between min/magnification */
1494 sample_lambda_2d(struct gl_context
*ctx
,
1495 const struct gl_sampler_object
*samp
,
1496 const struct gl_texture_object
*tObj
,
1497 GLuint n
, const GLfloat texcoords
[][4],
1498 const GLfloat lambda
[], GLfloat rgba
[][4])
1500 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1501 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(tImg
);
1502 GLuint minStart
, minEnd
; /* texels with minification */
1503 GLuint magStart
, magEnd
; /* texels with magnification */
1505 const GLboolean repeatNoBorderPOT
= (samp
->WrapS
== GL_REPEAT
)
1506 && (samp
->WrapT
== GL_REPEAT
)
1507 && (tImg
->Border
== 0 && (tImg
->Width
== swImg
->RowStride
))
1508 && swImg
->_IsPowerOfTwo
;
1510 ASSERT(lambda
!= NULL
);
1511 compute_min_mag_ranges(samp
, n
, lambda
,
1512 &minStart
, &minEnd
, &magStart
, &magEnd
);
1514 if (minStart
< minEnd
) {
1515 /* do the minified texels */
1516 const GLuint m
= minEnd
- minStart
;
1517 switch (samp
->MinFilter
) {
1519 if (repeatNoBorderPOT
) {
1520 switch (tImg
->TexFormat
) {
1521 case MESA_FORMAT_RGB888
:
1522 opt_sample_rgb_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1523 NULL
, rgba
+ minStart
);
1525 case MESA_FORMAT_RGBA8888
:
1526 opt_sample_rgba_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1527 NULL
, rgba
+ minStart
);
1530 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1531 NULL
, rgba
+ minStart
);
1535 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1536 NULL
, rgba
+ minStart
);
1540 sample_linear_2d(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1541 NULL
, rgba
+ minStart
);
1543 case GL_NEAREST_MIPMAP_NEAREST
:
1544 sample_2d_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
,
1545 texcoords
+ minStart
,
1546 lambda
+ minStart
, rgba
+ minStart
);
1548 case GL_LINEAR_MIPMAP_NEAREST
:
1549 sample_2d_linear_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1550 lambda
+ minStart
, rgba
+ minStart
);
1552 case GL_NEAREST_MIPMAP_LINEAR
:
1553 sample_2d_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1554 lambda
+ minStart
, rgba
+ minStart
);
1556 case GL_LINEAR_MIPMAP_LINEAR
:
1557 if (repeatNoBorderPOT
)
1558 sample_2d_linear_mipmap_linear_repeat(ctx
, samp
, tObj
, m
,
1559 texcoords
+ minStart
, lambda
+ minStart
, rgba
+ minStart
);
1561 sample_2d_linear_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
1562 lambda
+ minStart
, rgba
+ minStart
);
1565 _mesa_problem(ctx
, "Bad min filter in sample_2d_texture");
1570 if (magStart
< magEnd
) {
1571 /* do the magnified texels */
1572 const GLuint m
= magEnd
- magStart
;
1574 switch (samp
->MagFilter
) {
1576 if (repeatNoBorderPOT
) {
1577 switch (tImg
->TexFormat
) {
1578 case MESA_FORMAT_RGB888
:
1579 opt_sample_rgb_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1580 NULL
, rgba
+ magStart
);
1582 case MESA_FORMAT_RGBA8888
:
1583 opt_sample_rgba_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1584 NULL
, rgba
+ magStart
);
1587 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1588 NULL
, rgba
+ magStart
);
1592 sample_nearest_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1593 NULL
, rgba
+ magStart
);
1597 sample_linear_2d(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
1598 NULL
, rgba
+ magStart
);
1601 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_2d");
1608 /* For anisotropic filtering */
1609 #define WEIGHT_LUT_SIZE 1024
1611 static GLfloat
*weightLut
= NULL
;
1614 * Creates the look-up table used to speed-up EWA sampling
1617 create_filter_table(void)
1621 weightLut
= malloc(WEIGHT_LUT_SIZE
* sizeof(GLfloat
));
1623 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
1625 GLfloat r2
= (GLfloat
) i
/ (GLfloat
) (WEIGHT_LUT_SIZE
- 1);
1626 GLfloat weight
= (GLfloat
) exp(-alpha
* r2
);
1627 weightLut
[i
] = weight
;
1634 * Elliptical weighted average (EWA) filter for producing high quality
1635 * anisotropic filtered results.
1636 * Based on the Higher Quality Elliptical Weighted Avarage Filter
1637 * published by Paul S. Heckbert in his Master's Thesis
1638 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1641 sample_2d_ewa(struct gl_context
*ctx
,
1642 const struct gl_sampler_object
*samp
,
1643 const struct gl_texture_object
*tObj
,
1644 const GLfloat texcoord
[4],
1645 const GLfloat dudx
, const GLfloat dvdx
,
1646 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1649 GLint level
= lod
> 0 ? lod
: 0;
1650 GLfloat scaling
= 1.0f
/ (1 << level
);
1651 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1652 const struct gl_texture_image
*mostDetailedImage
=
1653 tObj
->Image
[0][tObj
->BaseLevel
];
1654 const struct swrast_texture_image
*swImg
=
1655 swrast_texture_image_const(mostDetailedImage
);
1656 GLfloat tex_u
= -0.5f
+ texcoord
[0] * swImg
->WidthScale
* scaling
;
1657 GLfloat tex_v
= -0.5f
+ texcoord
[1] * swImg
->HeightScale
* scaling
;
1659 GLfloat ux
= dudx
* scaling
;
1660 GLfloat vx
= dvdx
* scaling
;
1661 GLfloat uy
= dudy
* scaling
;
1662 GLfloat vy
= dvdy
* scaling
;
1664 /* compute ellipse coefficients to bound the region:
1665 * A*x*x + B*x*y + C*y*y = F.
1667 GLfloat A
= vx
*vx
+vy
*vy
+1;
1668 GLfloat B
= -2*(ux
*vx
+uy
*vy
);
1669 GLfloat C
= ux
*ux
+uy
*uy
+1;
1670 GLfloat F
= A
*C
-B
*B
/4.0f
;
1672 /* check if it is an ellipse */
1673 /* ASSERT(F > 0.0); */
1675 /* Compute the ellipse's (u,v) bounding box in texture space */
1676 GLfloat d
= -B
*B
+4.0f
*C
*A
;
1677 GLfloat box_u
= 2.0f
/ d
* sqrtf(d
*C
*F
); /* box_u -> half of bbox with */
1678 GLfloat box_v
= 2.0f
/ d
* sqrtf(A
*d
*F
); /* box_v -> half of bbox height */
1680 GLint u0
= (GLint
) floorf(tex_u
- box_u
);
1681 GLint u1
= (GLint
) ceilf (tex_u
+ box_u
);
1682 GLint v0
= (GLint
) floorf(tex_v
- box_v
);
1683 GLint v1
= (GLint
) ceilf (tex_v
+ box_v
);
1685 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1686 GLfloat newCoord
[2];
1689 GLfloat U
= u0
- tex_u
;
1692 /* Scale ellipse formula to directly index the Filter Lookup Table.
1693 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1695 GLfloat formScale
= (GLfloat
) (WEIGHT_LUT_SIZE
- 1) / F
;
1699 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1701 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1702 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1703 * value, q, is less than F, we're inside the ellipse
1706 for (v
= v0
; v
<= v1
; ++v
) {
1707 GLfloat V
= v
- tex_v
;
1708 GLfloat dq
= A
* (2 * U
+ 1) + B
* V
;
1709 GLfloat q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
1712 for (u
= u0
; u
<= u1
; ++u
) {
1713 /* Note that the ellipse has been pre-scaled so F = WEIGHT_LUT_SIZE - 1 */
1714 if (q
< WEIGHT_LUT_SIZE
) {
1715 /* as a LUT is used, q must never be negative;
1716 * should not happen, though
1718 const GLint qClamped
= q
>= 0.0F
? (GLint
) q
: 0;
1719 GLfloat weight
= weightLut
[qClamped
];
1721 newCoord
[0] = u
/ ((GLfloat
) img
->Width2
);
1722 newCoord
[1] = v
/ ((GLfloat
) img
->Height2
);
1724 sample_2d_nearest(ctx
, samp
, img
, newCoord
, rgba
);
1725 num
[0] += weight
* rgba
[0];
1726 num
[1] += weight
* rgba
[1];
1727 num
[2] += weight
* rgba
[2];
1728 num
[3] += weight
* rgba
[3];
1738 /* Reaching this place would mean
1739 * that no pixels intersected the ellipse.
1740 * This should never happen because
1741 * the filter we use always
1742 * intersects at least one pixel.
1749 /* not enough pixels in resampling, resort to direct interpolation */
1750 sample_2d_linear(ctx
, samp
, img
, texcoord
, rgba
);
1754 rgba
[0] = num
[0] / den
;
1755 rgba
[1] = num
[1] / den
;
1756 rgba
[2] = num
[2] / den
;
1757 rgba
[3] = num
[3] / den
;
1762 * Anisotropic filtering using footprint assembly as outlined in the
1763 * EXT_texture_filter_anisotropic spec:
1764 * http://www.opengl.org/registry/specs/EXT/texture_filter_anisotropic.txt
1765 * Faster than EWA but has less quality (more aliasing effects)
1768 sample_2d_footprint(struct gl_context
*ctx
,
1769 const struct gl_sampler_object
*samp
,
1770 const struct gl_texture_object
*tObj
,
1771 const GLfloat texcoord
[4],
1772 const GLfloat dudx
, const GLfloat dvdx
,
1773 const GLfloat dudy
, const GLfloat dvdy
, const GLint lod
,
1776 GLint level
= lod
> 0 ? lod
: 0;
1777 GLfloat scaling
= 1.0F
/ (1 << level
);
1778 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
1780 GLfloat ux
= dudx
* scaling
;
1781 GLfloat vx
= dvdx
* scaling
;
1782 GLfloat uy
= dudy
* scaling
;
1783 GLfloat vy
= dvdy
* scaling
;
1785 GLfloat Px2
= ux
* ux
+ vx
* vx
; /* squared length of dx */
1786 GLfloat Py2
= uy
* uy
+ vy
* vy
; /* squared length of dy */
1792 GLfloat num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1793 GLfloat newCoord
[2];
1796 /* Calculate the per anisotropic sample offsets in s,t space. */
1798 numSamples
= (GLint
) ceilf(sqrtf(Px2
));
1799 ds
= ux
/ ((GLfloat
) img
->Width2
);
1800 dt
= vx
/ ((GLfloat
) img
->Height2
);
1803 numSamples
= (GLint
) ceilf(sqrtf(Py2
));
1804 ds
= uy
/ ((GLfloat
) img
->Width2
);
1805 dt
= vy
/ ((GLfloat
) img
->Height2
);
1808 for (s
= 0; s
<numSamples
; s
++) {
1809 newCoord
[0] = texcoord
[0] + ds
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5f
);
1810 newCoord
[1] = texcoord
[1] + dt
* ((GLfloat
)(s
+1) / (numSamples
+1) -0.5f
);
1812 sample_2d_linear(ctx
, samp
, img
, newCoord
, rgba
);
1819 rgba
[0] = num
[0] / numSamples
;
1820 rgba
[1] = num
[1] / numSamples
;
1821 rgba
[2] = num
[2] / numSamples
;
1822 rgba
[3] = num
[3] / numSamples
;
1827 * Returns the index of the specified texture object in the
1828 * gl_context texture unit array.
1830 static inline GLuint
1831 texture_unit_index(const struct gl_context
*ctx
,
1832 const struct gl_texture_object
*tObj
)
1834 const GLuint maxUnit
1835 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
1838 /* XXX CoordUnits vs. ImageUnits */
1839 for (u
= 0; u
< maxUnit
; u
++) {
1840 if (ctx
->Texture
.Unit
[u
]._Current
== tObj
)
1844 u
= 0; /* not found, use 1st one; should never happen */
1851 * Sample 2D texture using an anisotropic filter.
1852 * NOTE: the const GLfloat lambda_iso[] parameter does *NOT* contain
1853 * the lambda float array but a "hidden" SWspan struct which is required
1854 * by this function but is not available in the texture_sample_func signature.
1855 * See _swrast_texture_span( struct gl_context *ctx, SWspan *span ) on how
1856 * this function is called.
1859 sample_lambda_2d_aniso(struct gl_context
*ctx
,
1860 const struct gl_sampler_object
*samp
,
1861 const struct gl_texture_object
*tObj
,
1862 GLuint n
, const GLfloat texcoords
[][4],
1863 const GLfloat lambda_iso
[], GLfloat rgba
[][4])
1865 const struct gl_texture_image
*tImg
= tObj
->Image
[0][tObj
->BaseLevel
];
1866 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(tImg
);
1867 const GLfloat maxEccentricity
=
1868 samp
->MaxAnisotropy
* samp
->MaxAnisotropy
;
1870 /* re-calculate the lambda values so that they are usable with anisotropic
1873 SWspan
*span
= (SWspan
*)lambda_iso
; /* access the "hidden" SWspan struct */
1875 /* based on interpolate_texcoords(struct gl_context *ctx, SWspan *span)
1876 * in swrast/s_span.c
1879 /* find the texture unit index by looking up the current texture object
1880 * from the context list of available texture objects.
1882 const GLuint u
= texture_unit_index(ctx
, tObj
);
1883 const GLuint attr
= VARYING_SLOT_TEX0
+ u
;
1886 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
1887 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
1888 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
1889 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
1890 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
1891 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
1892 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
1893 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
1894 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
1896 /* from swrast/s_texcombine.c _swrast_texture_span */
1897 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[u
];
1898 const GLboolean adjustLOD
=
1899 (texUnit
->LodBias
+ samp
->LodBias
!= 0.0F
)
1900 || (samp
->MinLod
!= -1000.0 || samp
->MaxLod
!= 1000.0);
1904 /* on first access create the lookup table containing the filter weights. */
1906 create_filter_table();
1909 texW
= swImg
->WidthScale
;
1910 texH
= swImg
->HeightScale
;
1912 for (i
= 0; i
< n
; i
++) {
1913 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
1915 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
1916 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
1917 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
1918 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
1920 /* note: instead of working with Px and Py, we will use the
1921 * squared length instead, to avoid sqrt.
1923 GLfloat Px2
= dudx
* dudx
+ dvdx
* dvdx
;
1924 GLfloat Py2
= dudy
* dudy
+ dvdy
* dvdy
;
1944 /* if the eccentricity of the ellipse is too big, scale up the shorter
1945 * of the two vectors to limit the maximum amount of work per pixel
1948 if (e
> maxEccentricity
) {
1949 /* GLfloat s=e / maxEccentricity;
1953 Pmin2
= Pmax2
/ maxEccentricity
;
1956 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
1957 * this since 0.5*log(x) = log(sqrt(x))
1959 lod
= 0.5f
* LOG2(Pmin2
);
1962 /* from swrast/s_texcombine.c _swrast_texture_span */
1963 if (texUnit
->LodBias
+ samp
->LodBias
!= 0.0F
) {
1964 /* apply LOD bias, but don't clamp yet */
1965 const GLfloat bias
=
1966 CLAMP(texUnit
->LodBias
+ samp
->LodBias
,
1967 -ctx
->Const
.MaxTextureLodBias
,
1968 ctx
->Const
.MaxTextureLodBias
);
1971 if (samp
->MinLod
!= -1000.0 ||
1972 samp
->MaxLod
!= 1000.0) {
1973 /* apply LOD clamping to lambda */
1974 lod
= CLAMP(lod
, samp
->MinLod
, samp
->MaxLod
);
1979 /* If the ellipse covers the whole image, we can
1980 * simply return the average of the whole image.
1982 if (lod
>= tObj
->_MaxLevel
) {
1983 sample_2d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
1984 texcoords
[i
], rgba
[i
]);
1987 /* don't bother interpolating between multiple LODs; it doesn't
1988 * seem to be worth the extra running time.
1990 sample_2d_ewa(ctx
, samp
, tObj
, texcoords
[i
],
1991 dudx
, dvdx
, dudy
, dvdy
, (GLint
) floorf(lod
), rgba
[i
]);
1994 (void) sample_2d_footprint
;
1996 sample_2d_footprint(ctx, tObj, texcoords[i],
1997 dudx, dvdx, dudy, dvdy, floor(lod), rgba[i]);
2005 /**********************************************************************/
2006 /* 3-D Texture Sampling Functions */
2007 /**********************************************************************/
2010 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2013 sample_3d_nearest(struct gl_context
*ctx
,
2014 const struct gl_sampler_object
*samp
,
2015 const struct gl_texture_image
*img
,
2016 const GLfloat texcoord
[4],
2019 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2020 const GLint width
= img
->Width2
; /* without border, power of two */
2021 const GLint height
= img
->Height2
; /* without border, power of two */
2022 const GLint depth
= img
->Depth2
; /* without border, power of two */
2026 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
2027 j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
2028 k
= nearest_texel_location(samp
->WrapR
, img
, depth
, texcoord
[2]);
2030 if (i
< 0 || i
>= (GLint
) img
->Width
||
2031 j
< 0 || j
>= (GLint
) img
->Height
||
2032 k
< 0 || k
>= (GLint
) img
->Depth
) {
2033 /* Need this test for GL_CLAMP_TO_BORDER mode */
2034 get_border_color(samp
, img
, rgba
);
2037 swImg
->FetchTexel(swImg
, i
, j
, k
, rgba
);
2043 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2046 sample_3d_linear(struct gl_context
*ctx
,
2047 const struct gl_sampler_object
*samp
,
2048 const struct gl_texture_image
*img
,
2049 const GLfloat texcoord
[4],
2052 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2053 const GLint width
= img
->Width2
;
2054 const GLint height
= img
->Height2
;
2055 const GLint depth
= img
->Depth2
;
2056 GLint i0
, j0
, k0
, i1
, j1
, k1
;
2057 GLbitfield useBorderColor
= 0x0;
2059 GLfloat t000
[4], t010
[4], t001
[4], t011
[4];
2060 GLfloat t100
[4], t110
[4], t101
[4], t111
[4];
2062 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2063 linear_texel_locations(samp
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2064 linear_texel_locations(samp
->WrapR
, img
, depth
, texcoord
[2], &k0
, &k1
, &c
);
2075 /* check if sampling texture border color */
2076 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2077 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2078 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2079 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2080 if (k0
< 0 || k0
>= depth
) useBorderColor
|= K0BIT
;
2081 if (k1
< 0 || k1
>= depth
) useBorderColor
|= K1BIT
;
2085 if (useBorderColor
& (I0BIT
| J0BIT
| K0BIT
)) {
2086 get_border_color(samp
, img
, t000
);
2089 swImg
->FetchTexel(swImg
, i0
, j0
, k0
, t000
);
2091 if (useBorderColor
& (I1BIT
| J0BIT
| K0BIT
)) {
2092 get_border_color(samp
, img
, t100
);
2095 swImg
->FetchTexel(swImg
, i1
, j0
, k0
, t100
);
2097 if (useBorderColor
& (I0BIT
| J1BIT
| K0BIT
)) {
2098 get_border_color(samp
, img
, t010
);
2101 swImg
->FetchTexel(swImg
, i0
, j1
, k0
, t010
);
2103 if (useBorderColor
& (I1BIT
| J1BIT
| K0BIT
)) {
2104 get_border_color(samp
, img
, t110
);
2107 swImg
->FetchTexel(swImg
, i1
, j1
, k0
, t110
);
2110 if (useBorderColor
& (I0BIT
| J0BIT
| K1BIT
)) {
2111 get_border_color(samp
, img
, t001
);
2114 swImg
->FetchTexel(swImg
, i0
, j0
, k1
, t001
);
2116 if (useBorderColor
& (I1BIT
| J0BIT
| K1BIT
)) {
2117 get_border_color(samp
, img
, t101
);
2120 swImg
->FetchTexel(swImg
, i1
, j0
, k1
, t101
);
2122 if (useBorderColor
& (I0BIT
| J1BIT
| K1BIT
)) {
2123 get_border_color(samp
, img
, t011
);
2126 swImg
->FetchTexel(swImg
, i0
, j1
, k1
, t011
);
2128 if (useBorderColor
& (I1BIT
| J1BIT
| K1BIT
)) {
2129 get_border_color(samp
, img
, t111
);
2132 swImg
->FetchTexel(swImg
, i1
, j1
, k1
, t111
);
2135 /* trilinear interpolation of samples */
2136 lerp_rgba_3d(rgba
, a
, b
, c
, t000
, t100
, t010
, t110
, t001
, t101
, t011
, t111
);
2141 sample_3d_nearest_mipmap_nearest(struct gl_context
*ctx
,
2142 const struct gl_sampler_object
*samp
,
2143 const struct gl_texture_object
*tObj
,
2144 GLuint n
, const GLfloat texcoord
[][4],
2145 const GLfloat lambda
[], GLfloat rgba
[][4] )
2148 for (i
= 0; i
< n
; i
++) {
2149 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2150 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2156 sample_3d_linear_mipmap_nearest(struct gl_context
*ctx
,
2157 const struct gl_sampler_object
*samp
,
2158 const struct gl_texture_object
*tObj
,
2159 GLuint n
, const GLfloat texcoord
[][4],
2160 const GLfloat lambda
[], GLfloat rgba
[][4])
2163 ASSERT(lambda
!= NULL
);
2164 for (i
= 0; i
< n
; i
++) {
2165 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2166 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], rgba
[i
]);
2172 sample_3d_nearest_mipmap_linear(struct gl_context
*ctx
,
2173 const struct gl_sampler_object
*samp
,
2174 const struct gl_texture_object
*tObj
,
2175 GLuint n
, const GLfloat texcoord
[][4],
2176 const GLfloat lambda
[], GLfloat rgba
[][4])
2179 ASSERT(lambda
!= NULL
);
2180 for (i
= 0; i
< n
; i
++) {
2181 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2182 if (level
>= tObj
->_MaxLevel
) {
2183 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2184 texcoord
[i
], rgba
[i
]);
2187 GLfloat t0
[4], t1
[4]; /* texels */
2188 const GLfloat f
= FRAC(lambda
[i
]);
2189 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2190 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2191 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2198 sample_3d_linear_mipmap_linear(struct gl_context
*ctx
,
2199 const struct gl_sampler_object
*samp
,
2200 const struct gl_texture_object
*tObj
,
2201 GLuint n
, const GLfloat texcoord
[][4],
2202 const GLfloat lambda
[], GLfloat rgba
[][4])
2205 ASSERT(lambda
!= NULL
);
2206 for (i
= 0; i
< n
; i
++) {
2207 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2208 if (level
>= tObj
->_MaxLevel
) {
2209 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2210 texcoord
[i
], rgba
[i
]);
2213 GLfloat t0
[4], t1
[4]; /* texels */
2214 const GLfloat f
= FRAC(lambda
[i
]);
2215 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
2216 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
2217 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2223 /** Sample 3D texture, nearest filtering for both min/magnification */
2225 sample_nearest_3d(struct gl_context
*ctx
,
2226 const struct gl_sampler_object
*samp
,
2227 const struct gl_texture_object
*tObj
, GLuint n
,
2228 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2232 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2234 for (i
= 0; i
< n
; i
++) {
2235 sample_3d_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
2240 /** Sample 3D texture, linear filtering for both min/magnification */
2242 sample_linear_3d(struct gl_context
*ctx
,
2243 const struct gl_sampler_object
*samp
,
2244 const struct gl_texture_object
*tObj
, GLuint n
,
2245 const GLfloat texcoords
[][4],
2246 const GLfloat lambda
[], GLfloat rgba
[][4])
2249 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
2251 for (i
= 0; i
< n
; i
++) {
2252 sample_3d_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
2257 /** Sample 3D texture, using lambda to choose between min/magnification */
2259 sample_lambda_3d(struct gl_context
*ctx
,
2260 const struct gl_sampler_object
*samp
,
2261 const struct gl_texture_object
*tObj
, GLuint n
,
2262 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2265 GLuint minStart
, minEnd
; /* texels with minification */
2266 GLuint magStart
, magEnd
; /* texels with magnification */
2269 ASSERT(lambda
!= NULL
);
2270 compute_min_mag_ranges(samp
, n
, lambda
,
2271 &minStart
, &minEnd
, &magStart
, &magEnd
);
2273 if (minStart
< minEnd
) {
2274 /* do the minified texels */
2275 GLuint m
= minEnd
- minStart
;
2276 switch (samp
->MinFilter
) {
2278 for (i
= minStart
; i
< minEnd
; i
++)
2279 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
2280 texcoords
[i
], rgba
[i
]);
2283 for (i
= minStart
; i
< minEnd
; i
++)
2284 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
2285 texcoords
[i
], rgba
[i
]);
2287 case GL_NEAREST_MIPMAP_NEAREST
:
2288 sample_3d_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2289 lambda
+ minStart
, rgba
+ minStart
);
2291 case GL_LINEAR_MIPMAP_NEAREST
:
2292 sample_3d_linear_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2293 lambda
+ minStart
, rgba
+ minStart
);
2295 case GL_NEAREST_MIPMAP_LINEAR
:
2296 sample_3d_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2297 lambda
+ minStart
, rgba
+ minStart
);
2299 case GL_LINEAR_MIPMAP_LINEAR
:
2300 sample_3d_linear_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2301 lambda
+ minStart
, rgba
+ minStart
);
2304 _mesa_problem(ctx
, "Bad min filter in sample_3d_texture");
2309 if (magStart
< magEnd
) {
2310 /* do the magnified texels */
2311 switch (samp
->MagFilter
) {
2313 for (i
= magStart
; i
< magEnd
; i
++)
2314 sample_3d_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
2315 texcoords
[i
], rgba
[i
]);
2318 for (i
= magStart
; i
< magEnd
; i
++)
2319 sample_3d_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
2320 texcoords
[i
], rgba
[i
]);
2323 _mesa_problem(ctx
, "Bad mag filter in sample_3d_texture");
2330 /**********************************************************************/
2331 /* Texture Cube Map Sampling Functions */
2332 /**********************************************************************/
2335 * Choose one of six sides of a texture cube map given the texture
2336 * coord (rx,ry,rz). Return pointer to corresponding array of texture
2339 static const struct gl_texture_image
**
2340 choose_cube_face(const struct gl_texture_object
*texObj
,
2341 const GLfloat texcoord
[4], GLfloat newCoord
[4])
2345 direction target sc tc ma
2346 ---------- ------------------------------- --- --- ---
2347 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2348 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2349 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2350 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2351 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2352 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2354 const GLfloat rx
= texcoord
[0];
2355 const GLfloat ry
= texcoord
[1];
2356 const GLfloat rz
= texcoord
[2];
2357 const GLfloat arx
= FABSF(rx
), ary
= FABSF(ry
), arz
= FABSF(rz
);
2361 if (arx
>= ary
&& arx
>= arz
) {
2375 else if (ary
>= arx
&& ary
>= arz
) {
2405 const float ima
= 1.0F
/ ma
;
2406 newCoord
[0] = ( sc
* ima
+ 1.0F
) * 0.5F
;
2407 newCoord
[1] = ( tc
* ima
+ 1.0F
) * 0.5F
;
2410 return (const struct gl_texture_image
**) texObj
->Image
[face
];
2415 sample_nearest_cube(struct gl_context
*ctx
,
2416 const struct gl_sampler_object
*samp
,
2417 const struct gl_texture_object
*tObj
, GLuint n
,
2418 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2423 for (i
= 0; i
< n
; i
++) {
2424 const struct gl_texture_image
**images
;
2425 GLfloat newCoord
[4];
2426 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2427 sample_2d_nearest(ctx
, samp
, images
[tObj
->BaseLevel
],
2430 if (is_depth_texture(tObj
)) {
2431 for (i
= 0; i
< n
; i
++) {
2432 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2439 sample_linear_cube(struct gl_context
*ctx
,
2440 const struct gl_sampler_object
*samp
,
2441 const struct gl_texture_object
*tObj
, GLuint n
,
2442 const GLfloat texcoords
[][4],
2443 const GLfloat lambda
[], GLfloat rgba
[][4])
2447 for (i
= 0; i
< n
; i
++) {
2448 const struct gl_texture_image
**images
;
2449 GLfloat newCoord
[4];
2450 images
= choose_cube_face(tObj
, texcoords
[i
], newCoord
);
2451 sample_2d_linear(ctx
, samp
, images
[tObj
->BaseLevel
],
2454 if (is_depth_texture(tObj
)) {
2455 for (i
= 0; i
< n
; i
++) {
2456 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2463 sample_cube_nearest_mipmap_nearest(struct gl_context
*ctx
,
2464 const struct gl_sampler_object
*samp
,
2465 const struct gl_texture_object
*tObj
,
2466 GLuint n
, const GLfloat texcoord
[][4],
2467 const GLfloat lambda
[], GLfloat rgba
[][4])
2470 ASSERT(lambda
!= NULL
);
2471 for (i
= 0; i
< n
; i
++) {
2472 const struct gl_texture_image
**images
;
2473 GLfloat newCoord
[4];
2475 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2477 /* XXX we actually need to recompute lambda here based on the newCoords.
2478 * But we would need the texcoords of adjacent fragments to compute that
2479 * properly, and we don't have those here.
2480 * For now, do an approximation: subtracting 1 from the chosen mipmap
2481 * level seems to work in some test cases.
2482 * The same adjustment is done in the next few functions.
2484 level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2485 level
= MAX2(level
- 1, 0);
2487 sample_2d_nearest(ctx
, samp
, images
[level
], newCoord
, rgba
[i
]);
2489 if (is_depth_texture(tObj
)) {
2490 for (i
= 0; i
< n
; i
++) {
2491 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2498 sample_cube_linear_mipmap_nearest(struct gl_context
*ctx
,
2499 const struct gl_sampler_object
*samp
,
2500 const struct gl_texture_object
*tObj
,
2501 GLuint n
, const GLfloat texcoord
[][4],
2502 const GLfloat lambda
[], GLfloat rgba
[][4])
2505 ASSERT(lambda
!= NULL
);
2506 for (i
= 0; i
< n
; i
++) {
2507 const struct gl_texture_image
**images
;
2508 GLfloat newCoord
[4];
2509 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2510 level
= MAX2(level
- 1, 0); /* see comment above */
2511 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2512 sample_2d_linear(ctx
, samp
, images
[level
], newCoord
, rgba
[i
]);
2514 if (is_depth_texture(tObj
)) {
2515 for (i
= 0; i
< n
; i
++) {
2516 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2523 sample_cube_nearest_mipmap_linear(struct gl_context
*ctx
,
2524 const struct gl_sampler_object
*samp
,
2525 const struct gl_texture_object
*tObj
,
2526 GLuint n
, const GLfloat texcoord
[][4],
2527 const GLfloat lambda
[], GLfloat rgba
[][4])
2530 ASSERT(lambda
!= NULL
);
2531 for (i
= 0; i
< n
; i
++) {
2532 const struct gl_texture_image
**images
;
2533 GLfloat newCoord
[4];
2534 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2535 level
= MAX2(level
- 1, 0); /* see comment above */
2536 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2537 if (level
>= tObj
->_MaxLevel
) {
2538 sample_2d_nearest(ctx
, samp
, images
[tObj
->_MaxLevel
],
2542 GLfloat t0
[4], t1
[4]; /* texels */
2543 const GLfloat f
= FRAC(lambda
[i
]);
2544 sample_2d_nearest(ctx
, samp
, images
[level
], newCoord
, t0
);
2545 sample_2d_nearest(ctx
, samp
, images
[level
+1], newCoord
, t1
);
2546 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2549 if (is_depth_texture(tObj
)) {
2550 for (i
= 0; i
< n
; i
++) {
2551 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2558 sample_cube_linear_mipmap_linear(struct gl_context
*ctx
,
2559 const struct gl_sampler_object
*samp
,
2560 const struct gl_texture_object
*tObj
,
2561 GLuint n
, const GLfloat texcoord
[][4],
2562 const GLfloat lambda
[], GLfloat rgba
[][4])
2565 ASSERT(lambda
!= NULL
);
2566 for (i
= 0; i
< n
; i
++) {
2567 const struct gl_texture_image
**images
;
2568 GLfloat newCoord
[4];
2569 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2570 level
= MAX2(level
- 1, 0); /* see comment above */
2571 images
= choose_cube_face(tObj
, texcoord
[i
], newCoord
);
2572 if (level
>= tObj
->_MaxLevel
) {
2573 sample_2d_linear(ctx
, samp
, images
[tObj
->_MaxLevel
],
2577 GLfloat t0
[4], t1
[4];
2578 const GLfloat f
= FRAC(lambda
[i
]);
2579 sample_2d_linear(ctx
, samp
, images
[level
], newCoord
, t0
);
2580 sample_2d_linear(ctx
, samp
, images
[level
+1], newCoord
, t1
);
2581 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2584 if (is_depth_texture(tObj
)) {
2585 for (i
= 0; i
< n
; i
++) {
2586 apply_depth_mode(tObj
->DepthMode
, rgba
[i
][0], rgba
[i
]);
2592 /** Sample cube texture, using lambda to choose between min/magnification */
2594 sample_lambda_cube(struct gl_context
*ctx
,
2595 const struct gl_sampler_object
*samp
,
2596 const struct gl_texture_object
*tObj
, GLuint n
,
2597 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2600 GLuint minStart
, minEnd
; /* texels with minification */
2601 GLuint magStart
, magEnd
; /* texels with magnification */
2603 ASSERT(lambda
!= NULL
);
2604 compute_min_mag_ranges(samp
, n
, lambda
,
2605 &minStart
, &minEnd
, &magStart
, &magEnd
);
2607 if (minStart
< minEnd
) {
2608 /* do the minified texels */
2609 const GLuint m
= minEnd
- minStart
;
2610 switch (samp
->MinFilter
) {
2612 sample_nearest_cube(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2613 lambda
+ minStart
, rgba
+ minStart
);
2616 sample_linear_cube(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
2617 lambda
+ minStart
, rgba
+ minStart
);
2619 case GL_NEAREST_MIPMAP_NEAREST
:
2620 sample_cube_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
,
2621 texcoords
+ minStart
,
2622 lambda
+ minStart
, rgba
+ minStart
);
2624 case GL_LINEAR_MIPMAP_NEAREST
:
2625 sample_cube_linear_mipmap_nearest(ctx
, samp
, tObj
, m
,
2626 texcoords
+ minStart
,
2627 lambda
+ minStart
, rgba
+ minStart
);
2629 case GL_NEAREST_MIPMAP_LINEAR
:
2630 sample_cube_nearest_mipmap_linear(ctx
, samp
, tObj
, m
,
2631 texcoords
+ minStart
,
2632 lambda
+ minStart
, rgba
+ minStart
);
2634 case GL_LINEAR_MIPMAP_LINEAR
:
2635 sample_cube_linear_mipmap_linear(ctx
, samp
, tObj
, m
,
2636 texcoords
+ minStart
,
2637 lambda
+ minStart
, rgba
+ minStart
);
2640 _mesa_problem(ctx
, "Bad min filter in sample_lambda_cube");
2645 if (magStart
< magEnd
) {
2646 /* do the magnified texels */
2647 const GLuint m
= magEnd
- magStart
;
2648 switch (samp
->MagFilter
) {
2650 sample_nearest_cube(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
2651 lambda
+ magStart
, rgba
+ magStart
);
2654 sample_linear_cube(ctx
, samp
, tObj
, m
, texcoords
+ magStart
,
2655 lambda
+ magStart
, rgba
+ magStart
);
2658 _mesa_problem(ctx
, "Bad mag filter in sample_lambda_cube");
2665 /**********************************************************************/
2666 /* Texture Rectangle Sampling Functions */
2667 /**********************************************************************/
2671 sample_nearest_rect(struct gl_context
*ctx
,
2672 const struct gl_sampler_object
*samp
,
2673 const struct gl_texture_object
*tObj
, GLuint n
,
2674 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2677 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2678 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2679 const GLint width
= img
->Width
;
2680 const GLint height
= img
->Height
;
2686 ASSERT(samp
->WrapS
== GL_CLAMP
||
2687 samp
->WrapS
== GL_CLAMP_TO_EDGE
||
2688 samp
->WrapS
== GL_CLAMP_TO_BORDER
);
2689 ASSERT(samp
->WrapT
== GL_CLAMP
||
2690 samp
->WrapT
== GL_CLAMP_TO_EDGE
||
2691 samp
->WrapT
== GL_CLAMP_TO_BORDER
);
2693 for (i
= 0; i
< n
; i
++) {
2695 col
= clamp_rect_coord_nearest(samp
->WrapS
, texcoords
[i
][0], width
);
2696 row
= clamp_rect_coord_nearest(samp
->WrapT
, texcoords
[i
][1], height
);
2697 if (col
< 0 || col
>= width
|| row
< 0 || row
>= height
)
2698 get_border_color(samp
, img
, rgba
[i
]);
2700 swImg
->FetchTexel(swImg
, col
, row
, 0, rgba
[i
]);
2706 sample_linear_rect(struct gl_context
*ctx
,
2707 const struct gl_sampler_object
*samp
,
2708 const struct gl_texture_object
*tObj
, GLuint n
,
2709 const GLfloat texcoords
[][4],
2710 const GLfloat lambda
[], GLfloat rgba
[][4])
2712 const struct gl_texture_image
*img
= tObj
->Image
[0][0];
2713 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2714 const GLint width
= img
->Width
;
2715 const GLint height
= img
->Height
;
2721 ASSERT(samp
->WrapS
== GL_CLAMP
||
2722 samp
->WrapS
== GL_CLAMP_TO_EDGE
||
2723 samp
->WrapS
== GL_CLAMP_TO_BORDER
);
2724 ASSERT(samp
->WrapT
== GL_CLAMP
||
2725 samp
->WrapT
== GL_CLAMP_TO_EDGE
||
2726 samp
->WrapT
== GL_CLAMP_TO_BORDER
);
2728 for (i
= 0; i
< n
; i
++) {
2729 GLint i0
, j0
, i1
, j1
;
2730 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2732 GLbitfield useBorderColor
= 0x0;
2734 clamp_rect_coord_linear(samp
->WrapS
, texcoords
[i
][0], width
,
2736 clamp_rect_coord_linear(samp
->WrapT
, texcoords
[i
][1], height
,
2739 /* compute integer rows/columns */
2740 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2741 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2742 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2743 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2745 /* get four texel samples */
2746 if (useBorderColor
& (I0BIT
| J0BIT
))
2747 get_border_color(samp
, img
, t00
);
2749 swImg
->FetchTexel(swImg
, i0
, j0
, 0, t00
);
2751 if (useBorderColor
& (I1BIT
| J0BIT
))
2752 get_border_color(samp
, img
, t10
);
2754 swImg
->FetchTexel(swImg
, i1
, j0
, 0, t10
);
2756 if (useBorderColor
& (I0BIT
| J1BIT
))
2757 get_border_color(samp
, img
, t01
);
2759 swImg
->FetchTexel(swImg
, i0
, j1
, 0, t01
);
2761 if (useBorderColor
& (I1BIT
| J1BIT
))
2762 get_border_color(samp
, img
, t11
);
2764 swImg
->FetchTexel(swImg
, i1
, j1
, 0, t11
);
2766 lerp_rgba_2d(rgba
[i
], a
, b
, t00
, t10
, t01
, t11
);
2771 /** Sample Rect texture, using lambda to choose between min/magnification */
2773 sample_lambda_rect(struct gl_context
*ctx
,
2774 const struct gl_sampler_object
*samp
,
2775 const struct gl_texture_object
*tObj
, GLuint n
,
2776 const GLfloat texcoords
[][4], const GLfloat lambda
[],
2779 GLuint minStart
, minEnd
, magStart
, magEnd
;
2781 /* We only need lambda to decide between minification and magnification.
2782 * There is no mipmapping with rectangular textures.
2784 compute_min_mag_ranges(samp
, n
, lambda
,
2785 &minStart
, &minEnd
, &magStart
, &magEnd
);
2787 if (minStart
< minEnd
) {
2788 if (samp
->MinFilter
== GL_NEAREST
) {
2789 sample_nearest_rect(ctx
, samp
, tObj
, minEnd
- minStart
,
2790 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2793 sample_linear_rect(ctx
, samp
, tObj
, minEnd
- minStart
,
2794 texcoords
+ minStart
, NULL
, rgba
+ minStart
);
2797 if (magStart
< magEnd
) {
2798 if (samp
->MagFilter
== GL_NEAREST
) {
2799 sample_nearest_rect(ctx
, samp
, tObj
, magEnd
- magStart
,
2800 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2803 sample_linear_rect(ctx
, samp
, tObj
, magEnd
- magStart
,
2804 texcoords
+ magStart
, NULL
, rgba
+ magStart
);
2810 /**********************************************************************/
2811 /* 2D Texture Array Sampling Functions */
2812 /**********************************************************************/
2815 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2818 sample_2d_array_nearest(struct gl_context
*ctx
,
2819 const struct gl_sampler_object
*samp
,
2820 const struct gl_texture_image
*img
,
2821 const GLfloat texcoord
[4],
2824 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2825 const GLint width
= img
->Width2
; /* without border, power of two */
2826 const GLint height
= img
->Height2
; /* without border, power of two */
2827 const GLint depth
= img
->Depth
;
2832 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
2833 j
= nearest_texel_location(samp
->WrapT
, img
, height
, texcoord
[1]);
2834 array
= tex_array_slice(texcoord
[2], depth
);
2836 if (i
< 0 || i
>= (GLint
) img
->Width
||
2837 j
< 0 || j
>= (GLint
) img
->Height
||
2838 array
< 0 || array
>= (GLint
) img
->Depth
) {
2839 /* Need this test for GL_CLAMP_TO_BORDER mode */
2840 get_border_color(samp
, img
, rgba
);
2843 swImg
->FetchTexel(swImg
, i
, j
, array
, rgba
);
2849 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2852 sample_2d_array_linear(struct gl_context
*ctx
,
2853 const struct gl_sampler_object
*samp
,
2854 const struct gl_texture_image
*img
,
2855 const GLfloat texcoord
[4],
2858 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
2859 const GLint width
= img
->Width2
;
2860 const GLint height
= img
->Height2
;
2861 const GLint depth
= img
->Depth
;
2862 GLint i0
, j0
, i1
, j1
;
2864 GLbitfield useBorderColor
= 0x0;
2866 GLfloat t00
[4], t01
[4], t10
[4], t11
[4];
2868 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
2869 linear_texel_locations(samp
->WrapT
, img
, height
, texcoord
[1], &j0
, &j1
, &b
);
2870 array
= tex_array_slice(texcoord
[2], depth
);
2872 if (array
< 0 || array
>= depth
) {
2873 COPY_4V(rgba
, samp
->BorderColor
.f
);
2883 /* check if sampling texture border color */
2884 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
2885 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
2886 if (j0
< 0 || j0
>= height
) useBorderColor
|= J0BIT
;
2887 if (j1
< 0 || j1
>= height
) useBorderColor
|= J1BIT
;
2891 if (useBorderColor
& (I0BIT
| J0BIT
)) {
2892 get_border_color(samp
, img
, t00
);
2895 swImg
->FetchTexel(swImg
, i0
, j0
, array
, t00
);
2897 if (useBorderColor
& (I1BIT
| J0BIT
)) {
2898 get_border_color(samp
, img
, t10
);
2901 swImg
->FetchTexel(swImg
, i1
, j0
, array
, t10
);
2903 if (useBorderColor
& (I0BIT
| J1BIT
)) {
2904 get_border_color(samp
, img
, t01
);
2907 swImg
->FetchTexel(swImg
, i0
, j1
, array
, t01
);
2909 if (useBorderColor
& (I1BIT
| J1BIT
)) {
2910 get_border_color(samp
, img
, t11
);
2913 swImg
->FetchTexel(swImg
, i1
, j1
, array
, t11
);
2916 /* trilinear interpolation of samples */
2917 lerp_rgba_2d(rgba
, a
, b
, t00
, t10
, t01
, t11
);
2923 sample_2d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
2924 const struct gl_sampler_object
*samp
,
2925 const struct gl_texture_object
*tObj
,
2926 GLuint n
, const GLfloat texcoord
[][4],
2927 const GLfloat lambda
[], GLfloat rgba
[][4])
2930 for (i
= 0; i
< n
; i
++) {
2931 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2932 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
],
2939 sample_2d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
2940 const struct gl_sampler_object
*samp
,
2941 const struct gl_texture_object
*tObj
,
2942 GLuint n
, const GLfloat texcoord
[][4],
2943 const GLfloat lambda
[], GLfloat rgba
[][4])
2946 ASSERT(lambda
!= NULL
);
2947 for (i
= 0; i
< n
; i
++) {
2948 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
2949 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][level
],
2950 texcoord
[i
], rgba
[i
]);
2956 sample_2d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
2957 const struct gl_sampler_object
*samp
,
2958 const struct gl_texture_object
*tObj
,
2959 GLuint n
, const GLfloat texcoord
[][4],
2960 const GLfloat lambda
[], GLfloat rgba
[][4])
2963 ASSERT(lambda
!= NULL
);
2964 for (i
= 0; i
< n
; i
++) {
2965 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2966 if (level
>= tObj
->_MaxLevel
) {
2967 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2968 texcoord
[i
], rgba
[i
]);
2971 GLfloat t0
[4], t1
[4]; /* texels */
2972 const GLfloat f
= FRAC(lambda
[i
]);
2973 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
],
2975 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
+1],
2977 lerp_rgba(rgba
[i
], f
, t0
, t1
);
2984 sample_2d_array_linear_mipmap_linear(struct gl_context
*ctx
,
2985 const struct gl_sampler_object
*samp
,
2986 const struct gl_texture_object
*tObj
,
2987 GLuint n
, const GLfloat texcoord
[][4],
2988 const GLfloat lambda
[], GLfloat rgba
[][4])
2991 ASSERT(lambda
!= NULL
);
2992 for (i
= 0; i
< n
; i
++) {
2993 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
2994 if (level
>= tObj
->_MaxLevel
) {
2995 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
2996 texcoord
[i
], rgba
[i
]);
2999 GLfloat t0
[4], t1
[4]; /* texels */
3000 const GLfloat f
= FRAC(lambda
[i
]);
3001 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][level
],
3003 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][level
+1],
3005 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3011 /** Sample 2D Array texture, nearest filtering for both min/magnification */
3013 sample_nearest_2d_array(struct gl_context
*ctx
,
3014 const struct gl_sampler_object
*samp
,
3015 const struct gl_texture_object
*tObj
, GLuint n
,
3016 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3020 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3022 for (i
= 0; i
< n
; i
++) {
3023 sample_2d_array_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3029 /** Sample 2D Array texture, linear filtering for both min/magnification */
3031 sample_linear_2d_array(struct gl_context
*ctx
,
3032 const struct gl_sampler_object
*samp
,
3033 const struct gl_texture_object
*tObj
, GLuint n
,
3034 const GLfloat texcoords
[][4],
3035 const GLfloat lambda
[], GLfloat rgba
[][4])
3038 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3040 for (i
= 0; i
< n
; i
++) {
3041 sample_2d_array_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3046 /** Sample 2D Array texture, using lambda to choose between min/magnification */
3048 sample_lambda_2d_array(struct gl_context
*ctx
,
3049 const struct gl_sampler_object
*samp
,
3050 const struct gl_texture_object
*tObj
, GLuint n
,
3051 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3054 GLuint minStart
, minEnd
; /* texels with minification */
3055 GLuint magStart
, magEnd
; /* texels with magnification */
3058 ASSERT(lambda
!= NULL
);
3059 compute_min_mag_ranges(samp
, n
, lambda
,
3060 &minStart
, &minEnd
, &magStart
, &magEnd
);
3062 if (minStart
< minEnd
) {
3063 /* do the minified texels */
3064 GLuint m
= minEnd
- minStart
;
3065 switch (samp
->MinFilter
) {
3067 for (i
= minStart
; i
< minEnd
; i
++)
3068 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3069 texcoords
[i
], rgba
[i
]);
3072 for (i
= minStart
; i
< minEnd
; i
++)
3073 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3074 texcoords
[i
], rgba
[i
]);
3076 case GL_NEAREST_MIPMAP_NEAREST
:
3077 sample_2d_array_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
,
3078 texcoords
+ minStart
,
3082 case GL_LINEAR_MIPMAP_NEAREST
:
3083 sample_2d_array_linear_mipmap_nearest(ctx
, samp
, tObj
, m
,
3084 texcoords
+ minStart
,
3088 case GL_NEAREST_MIPMAP_LINEAR
:
3089 sample_2d_array_nearest_mipmap_linear(ctx
, samp
, tObj
, m
,
3090 texcoords
+ minStart
,
3094 case GL_LINEAR_MIPMAP_LINEAR
:
3095 sample_2d_array_linear_mipmap_linear(ctx
, samp
, tObj
, m
,
3096 texcoords
+ minStart
,
3101 _mesa_problem(ctx
, "Bad min filter in sample_2d_array_texture");
3106 if (magStart
< magEnd
) {
3107 /* do the magnified texels */
3108 switch (samp
->MagFilter
) {
3110 for (i
= magStart
; i
< magEnd
; i
++)
3111 sample_2d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3112 texcoords
[i
], rgba
[i
]);
3115 for (i
= magStart
; i
< magEnd
; i
++)
3116 sample_2d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3117 texcoords
[i
], rgba
[i
]);
3120 _mesa_problem(ctx
, "Bad mag filter in sample_2d_array_texture");
3129 /**********************************************************************/
3130 /* 1D Texture Array Sampling Functions */
3131 /**********************************************************************/
3134 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
3137 sample_1d_array_nearest(struct gl_context
*ctx
,
3138 const struct gl_sampler_object
*samp
,
3139 const struct gl_texture_image
*img
,
3140 const GLfloat texcoord
[4],
3143 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3144 const GLint width
= img
->Width2
; /* without border, power of two */
3145 const GLint height
= img
->Height
;
3150 i
= nearest_texel_location(samp
->WrapS
, img
, width
, texcoord
[0]);
3151 array
= tex_array_slice(texcoord
[1], height
);
3153 if (i
< 0 || i
>= (GLint
) img
->Width
||
3154 array
< 0 || array
>= (GLint
) img
->Height
) {
3155 /* Need this test for GL_CLAMP_TO_BORDER mode */
3156 get_border_color(samp
, img
, rgba
);
3159 swImg
->FetchTexel(swImg
, i
, array
, 0, rgba
);
3165 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
3168 sample_1d_array_linear(struct gl_context
*ctx
,
3169 const struct gl_sampler_object
*samp
,
3170 const struct gl_texture_image
*img
,
3171 const GLfloat texcoord
[4],
3174 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3175 const GLint width
= img
->Width2
;
3176 const GLint height
= img
->Height
;
3179 GLbitfield useBorderColor
= 0x0;
3181 GLfloat t0
[4], t1
[4];
3183 linear_texel_locations(samp
->WrapS
, img
, width
, texcoord
[0], &i0
, &i1
, &a
);
3184 array
= tex_array_slice(texcoord
[1], height
);
3191 /* check if sampling texture border color */
3192 if (i0
< 0 || i0
>= width
) useBorderColor
|= I0BIT
;
3193 if (i1
< 0 || i1
>= width
) useBorderColor
|= I1BIT
;
3196 if (array
< 0 || array
>= height
) useBorderColor
|= K0BIT
;
3199 if (useBorderColor
& (I0BIT
| K0BIT
)) {
3200 get_border_color(samp
, img
, t0
);
3203 swImg
->FetchTexel(swImg
, i0
, array
, 0, t0
);
3205 if (useBorderColor
& (I1BIT
| K0BIT
)) {
3206 get_border_color(samp
, img
, t1
);
3209 swImg
->FetchTexel(swImg
, i1
, array
, 0, t1
);
3212 /* bilinear interpolation of samples */
3213 lerp_rgba(rgba
, a
, t0
, t1
);
3218 sample_1d_array_nearest_mipmap_nearest(struct gl_context
*ctx
,
3219 const struct gl_sampler_object
*samp
,
3220 const struct gl_texture_object
*tObj
,
3221 GLuint n
, const GLfloat texcoord
[][4],
3222 const GLfloat lambda
[], GLfloat rgba
[][4])
3225 for (i
= 0; i
< n
; i
++) {
3226 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3227 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
],
3234 sample_1d_array_linear_mipmap_nearest(struct gl_context
*ctx
,
3235 const struct gl_sampler_object
*samp
,
3236 const struct gl_texture_object
*tObj
,
3237 GLuint n
, const GLfloat texcoord
[][4],
3238 const GLfloat lambda
[], GLfloat rgba
[][4])
3241 ASSERT(lambda
!= NULL
);
3242 for (i
= 0; i
< n
; i
++) {
3243 GLint level
= nearest_mipmap_level(tObj
, lambda
[i
]);
3244 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][level
],
3245 texcoord
[i
], rgba
[i
]);
3251 sample_1d_array_nearest_mipmap_linear(struct gl_context
*ctx
,
3252 const struct gl_sampler_object
*samp
,
3253 const struct gl_texture_object
*tObj
,
3254 GLuint n
, const GLfloat texcoord
[][4],
3255 const GLfloat lambda
[], GLfloat rgba
[][4])
3258 ASSERT(lambda
!= NULL
);
3259 for (i
= 0; i
< n
; i
++) {
3260 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3261 if (level
>= tObj
->_MaxLevel
) {
3262 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
3263 texcoord
[i
], rgba
[i
]);
3266 GLfloat t0
[4], t1
[4]; /* texels */
3267 const GLfloat f
= FRAC(lambda
[i
]);
3268 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3269 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3270 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3277 sample_1d_array_linear_mipmap_linear(struct gl_context
*ctx
,
3278 const struct gl_sampler_object
*samp
,
3279 const struct gl_texture_object
*tObj
,
3280 GLuint n
, const GLfloat texcoord
[][4],
3281 const GLfloat lambda
[], GLfloat rgba
[][4])
3284 ASSERT(lambda
!= NULL
);
3285 for (i
= 0; i
< n
; i
++) {
3286 GLint level
= linear_mipmap_level(tObj
, lambda
[i
]);
3287 if (level
>= tObj
->_MaxLevel
) {
3288 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->_MaxLevel
],
3289 texcoord
[i
], rgba
[i
]);
3292 GLfloat t0
[4], t1
[4]; /* texels */
3293 const GLfloat f
= FRAC(lambda
[i
]);
3294 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][level
], texcoord
[i
], t0
);
3295 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][level
+1], texcoord
[i
], t1
);
3296 lerp_rgba(rgba
[i
], f
, t0
, t1
);
3302 /** Sample 1D Array texture, nearest filtering for both min/magnification */
3304 sample_nearest_1d_array(struct gl_context
*ctx
,
3305 const struct gl_sampler_object
*samp
,
3306 const struct gl_texture_object
*tObj
, GLuint n
,
3307 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3311 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3313 for (i
= 0; i
< n
; i
++) {
3314 sample_1d_array_nearest(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3319 /** Sample 1D Array texture, linear filtering for both min/magnification */
3321 sample_linear_1d_array(struct gl_context
*ctx
,
3322 const struct gl_sampler_object
*samp
,
3323 const struct gl_texture_object
*tObj
, GLuint n
,
3324 const GLfloat texcoords
[][4],
3325 const GLfloat lambda
[], GLfloat rgba
[][4])
3328 struct gl_texture_image
*image
= tObj
->Image
[0][tObj
->BaseLevel
];
3330 for (i
= 0; i
< n
; i
++) {
3331 sample_1d_array_linear(ctx
, samp
, image
, texcoords
[i
], rgba
[i
]);
3336 /** Sample 1D Array texture, using lambda to choose between min/magnification */
3338 sample_lambda_1d_array(struct gl_context
*ctx
,
3339 const struct gl_sampler_object
*samp
,
3340 const struct gl_texture_object
*tObj
, GLuint n
,
3341 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3344 GLuint minStart
, minEnd
; /* texels with minification */
3345 GLuint magStart
, magEnd
; /* texels with magnification */
3348 ASSERT(lambda
!= NULL
);
3349 compute_min_mag_ranges(samp
, n
, lambda
,
3350 &minStart
, &minEnd
, &magStart
, &magEnd
);
3352 if (minStart
< minEnd
) {
3353 /* do the minified texels */
3354 GLuint m
= minEnd
- minStart
;
3355 switch (samp
->MinFilter
) {
3357 for (i
= minStart
; i
< minEnd
; i
++)
3358 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3359 texcoords
[i
], rgba
[i
]);
3362 for (i
= minStart
; i
< minEnd
; i
++)
3363 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3364 texcoords
[i
], rgba
[i
]);
3366 case GL_NEAREST_MIPMAP_NEAREST
:
3367 sample_1d_array_nearest_mipmap_nearest(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
3368 lambda
+ minStart
, rgba
+ minStart
);
3370 case GL_LINEAR_MIPMAP_NEAREST
:
3371 sample_1d_array_linear_mipmap_nearest(ctx
, samp
, tObj
, m
,
3372 texcoords
+ minStart
,
3376 case GL_NEAREST_MIPMAP_LINEAR
:
3377 sample_1d_array_nearest_mipmap_linear(ctx
, samp
, tObj
, m
, texcoords
+ minStart
,
3378 lambda
+ minStart
, rgba
+ minStart
);
3380 case GL_LINEAR_MIPMAP_LINEAR
:
3381 sample_1d_array_linear_mipmap_linear(ctx
, samp
, tObj
, m
,
3382 texcoords
+ minStart
,
3387 _mesa_problem(ctx
, "Bad min filter in sample_1d_array_texture");
3392 if (magStart
< magEnd
) {
3393 /* do the magnified texels */
3394 switch (samp
->MagFilter
) {
3396 for (i
= magStart
; i
< magEnd
; i
++)
3397 sample_1d_array_nearest(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3398 texcoords
[i
], rgba
[i
]);
3401 for (i
= magStart
; i
< magEnd
; i
++)
3402 sample_1d_array_linear(ctx
, samp
, tObj
->Image
[0][tObj
->BaseLevel
],
3403 texcoords
[i
], rgba
[i
]);
3406 _mesa_problem(ctx
, "Bad mag filter in sample_1d_array_texture");
3414 * Compare texcoord against depth sample. Return 1.0 or 0.0 value.
3416 static inline GLfloat
3417 shadow_compare(GLenum function
, GLfloat coord
, GLfloat depthSample
)
3421 return (coord
<= depthSample
) ? 1.0F
: 0.0F
;
3423 return (coord
>= depthSample
) ? 1.0F
: 0.0F
;
3425 return (coord
< depthSample
) ? 1.0F
: 0.0F
;
3427 return (coord
> depthSample
) ? 1.0F
: 0.0F
;
3429 return (coord
== depthSample
) ? 1.0F
: 0.0F
;
3431 return (coord
!= depthSample
) ? 1.0F
: 0.0F
;
3439 _mesa_problem(NULL
, "Bad compare func in shadow_compare");
3446 * Compare texcoord against four depth samples.
3448 static inline GLfloat
3449 shadow_compare4(GLenum function
, GLfloat coord
,
3450 GLfloat depth00
, GLfloat depth01
,
3451 GLfloat depth10
, GLfloat depth11
,
3452 GLfloat wi
, GLfloat wj
)
3454 const GLfloat d
= 0.25F
;
3455 GLfloat luminance
= 1.0F
;
3459 if (coord
> depth00
) luminance
-= d
;
3460 if (coord
> depth01
) luminance
-= d
;
3461 if (coord
> depth10
) luminance
-= d
;
3462 if (coord
> depth11
) luminance
-= d
;
3465 if (coord
< depth00
) luminance
-= d
;
3466 if (coord
< depth01
) luminance
-= d
;
3467 if (coord
< depth10
) luminance
-= d
;
3468 if (coord
< depth11
) luminance
-= d
;
3471 if (coord
>= depth00
) luminance
-= d
;
3472 if (coord
>= depth01
) luminance
-= d
;
3473 if (coord
>= depth10
) luminance
-= d
;
3474 if (coord
>= depth11
) luminance
-= d
;
3477 if (coord
<= depth00
) luminance
-= d
;
3478 if (coord
<= depth01
) luminance
-= d
;
3479 if (coord
<= depth10
) luminance
-= d
;
3480 if (coord
<= depth11
) luminance
-= d
;
3483 if (coord
!= depth00
) luminance
-= d
;
3484 if (coord
!= depth01
) luminance
-= d
;
3485 if (coord
!= depth10
) luminance
-= d
;
3486 if (coord
!= depth11
) luminance
-= d
;
3489 if (coord
== depth00
) luminance
-= d
;
3490 if (coord
== depth01
) luminance
-= d
;
3491 if (coord
== depth10
) luminance
-= d
;
3492 if (coord
== depth11
) luminance
-= d
;
3499 /* ordinary bilinear filtering */
3500 return lerp_2d(wi
, wj
, depth00
, depth10
, depth01
, depth11
);
3502 _mesa_problem(NULL
, "Bad compare func in sample_compare4");
3509 * Choose the mipmap level to use when sampling from a depth texture.
3512 choose_depth_texture_level(const struct gl_sampler_object
*samp
,
3513 const struct gl_texture_object
*tObj
, GLfloat lambda
)
3517 if (samp
->MinFilter
== GL_NEAREST
|| samp
->MinFilter
== GL_LINEAR
) {
3518 /* no mipmapping - use base level */
3519 level
= tObj
->BaseLevel
;
3522 /* choose mipmap level */
3523 lambda
= CLAMP(lambda
, samp
->MinLod
, samp
->MaxLod
);
3524 level
= (GLint
) lambda
;
3525 level
= CLAMP(level
, tObj
->BaseLevel
, tObj
->_MaxLevel
);
3533 * Sample a shadow/depth texture. This function is incomplete. It doesn't
3534 * check for minification vs. magnification, etc.
3537 sample_depth_texture( struct gl_context
*ctx
,
3538 const struct gl_sampler_object
*samp
,
3539 const struct gl_texture_object
*tObj
, GLuint n
,
3540 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3541 GLfloat texel
[][4] )
3543 const GLint level
= choose_depth_texture_level(samp
, tObj
, lambda
[0]);
3544 const struct gl_texture_image
*img
= tObj
->Image
[0][level
];
3545 const struct swrast_texture_image
*swImg
= swrast_texture_image_const(img
);
3546 const GLint width
= img
->Width
;
3547 const GLint height
= img
->Height
;
3548 const GLint depth
= img
->Depth
;
3549 const GLuint compare_coord
= (tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)
3554 ASSERT(img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
3555 img
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
);
3557 ASSERT(tObj
->Target
== GL_TEXTURE_1D
||
3558 tObj
->Target
== GL_TEXTURE_2D
||
3559 tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
||
3560 tObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
||
3561 tObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
||
3562 tObj
->Target
== GL_TEXTURE_CUBE_MAP
);
3564 /* XXXX if samp->MinFilter != samp->MagFilter, we're ignoring lambda */
3566 function
= (samp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) ?
3567 samp
->CompareFunc
: GL_NONE
;
3569 if (samp
->MagFilter
== GL_NEAREST
) {
3571 for (i
= 0; i
< n
; i
++) {
3572 GLfloat depthSample
, depthRef
;
3573 GLint col
, row
, slice
;
3575 nearest_texcoord(samp
, tObj
, level
, texcoords
[i
], &col
, &row
, &slice
);
3577 if (col
>= 0 && row
>= 0 && col
< width
&& row
< height
&&
3578 slice
>= 0 && slice
< depth
) {
3579 swImg
->FetchTexel(swImg
, col
, row
, slice
, &depthSample
);
3582 depthSample
= samp
->BorderColor
.f
[0];
3585 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3587 result
= shadow_compare(function
, depthRef
, depthSample
);
3589 apply_depth_mode(tObj
->DepthMode
, result
, texel
[i
]);
3594 ASSERT(samp
->MagFilter
== GL_LINEAR
);
3595 for (i
= 0; i
< n
; i
++) {
3596 GLfloat depth00
, depth01
, depth10
, depth11
, depthRef
;
3597 GLint i0
, i1
, j0
, j1
;
3600 GLuint useBorderTexel
;
3602 linear_texcoord(samp
, tObj
, level
, texcoords
[i
], &i0
, &i1
, &j0
, &j1
, &slice
,
3609 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3615 if (i0
< 0 || i0
>= (GLint
) width
) useBorderTexel
|= I0BIT
;
3616 if (i1
< 0 || i1
>= (GLint
) width
) useBorderTexel
|= I1BIT
;
3617 if (j0
< 0 || j0
>= (GLint
) height
) useBorderTexel
|= J0BIT
;
3618 if (j1
< 0 || j1
>= (GLint
) height
) useBorderTexel
|= J1BIT
;
3621 if (slice
< 0 || slice
>= (GLint
) depth
) {
3622 depth00
= samp
->BorderColor
.f
[0];
3623 depth01
= samp
->BorderColor
.f
[0];
3624 depth10
= samp
->BorderColor
.f
[0];
3625 depth11
= samp
->BorderColor
.f
[0];
3628 /* get four depth samples from the texture */
3629 if (useBorderTexel
& (I0BIT
| J0BIT
)) {
3630 depth00
= samp
->BorderColor
.f
[0];
3633 swImg
->FetchTexel(swImg
, i0
, j0
, slice
, &depth00
);
3635 if (useBorderTexel
& (I1BIT
| J0BIT
)) {
3636 depth10
= samp
->BorderColor
.f
[0];
3639 swImg
->FetchTexel(swImg
, i1
, j0
, slice
, &depth10
);
3642 if (tObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) {
3643 if (useBorderTexel
& (I0BIT
| J1BIT
)) {
3644 depth01
= samp
->BorderColor
.f
[0];
3647 swImg
->FetchTexel(swImg
, i0
, j1
, slice
, &depth01
);
3649 if (useBorderTexel
& (I1BIT
| J1BIT
)) {
3650 depth11
= samp
->BorderColor
.f
[0];
3653 swImg
->FetchTexel(swImg
, i1
, j1
, slice
, &depth11
);
3662 depthRef
= CLAMP(texcoords
[i
][compare_coord
], 0.0F
, 1.0F
);
3664 result
= shadow_compare4(function
, depthRef
,
3665 depth00
, depth01
, depth10
, depth11
,
3668 apply_depth_mode(tObj
->DepthMode
, result
, texel
[i
]);
3675 * We use this function when a texture object is in an "incomplete" state.
3676 * When a fragment program attempts to sample an incomplete texture we
3677 * return black (see issue 23 in GL_ARB_fragment_program spec).
3678 * Note: fragment programs don't observe the texture enable/disable flags.
3681 null_sample_func( struct gl_context
*ctx
,
3682 const struct gl_sampler_object
*samp
,
3683 const struct gl_texture_object
*tObj
, GLuint n
,
3684 const GLfloat texcoords
[][4], const GLfloat lambda
[],
3693 for (i
= 0; i
< n
; i
++) {
3697 rgba
[i
][ACOMP
] = 1.0;
3703 * Choose the texture sampling function for the given texture object.
3706 _swrast_choose_texture_sample_func( struct gl_context
*ctx
,
3707 const struct gl_texture_object
*t
,
3708 const struct gl_sampler_object
*sampler
)
3710 if (!t
|| !_mesa_is_texture_complete(t
, sampler
)) {
3711 return &null_sample_func
;
3714 const GLboolean needLambda
=
3715 (GLboolean
) (sampler
->MinFilter
!= sampler
->MagFilter
);
3717 switch (t
->Target
) {
3719 if (is_depth_texture(t
)) {
3720 return &sample_depth_texture
;
3722 else if (needLambda
) {
3723 return &sample_lambda_1d
;
3725 else if (sampler
->MinFilter
== GL_LINEAR
) {
3726 return &sample_linear_1d
;
3729 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3730 return &sample_nearest_1d
;
3733 if (is_depth_texture(t
)) {
3734 return &sample_depth_texture
;
3736 else if (needLambda
) {
3737 /* Anisotropic filtering extension. Activated only if mipmaps are used */
3738 if (sampler
->MaxAnisotropy
> 1.0 &&
3739 sampler
->MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
3740 return &sample_lambda_2d_aniso
;
3742 return &sample_lambda_2d
;
3744 else if (sampler
->MinFilter
== GL_LINEAR
) {
3745 return &sample_linear_2d
;
3748 /* check for a few optimized cases */
3749 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
3750 const struct swrast_texture_image
*swImg
=
3751 swrast_texture_image_const(img
);
3752 texture_sample_func func
;
3754 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3755 func
= &sample_nearest_2d
;
3756 if (sampler
->WrapS
== GL_REPEAT
&&
3757 sampler
->WrapT
== GL_REPEAT
&&
3758 swImg
->_IsPowerOfTwo
&&
3760 if (img
->TexFormat
== MESA_FORMAT_RGB888
)
3761 func
= &opt_sample_rgb_2d
;
3762 else if (img
->TexFormat
== MESA_FORMAT_RGBA8888
)
3763 func
= &opt_sample_rgba_2d
;
3770 return &sample_lambda_3d
;
3772 else if (sampler
->MinFilter
== GL_LINEAR
) {
3773 return &sample_linear_3d
;
3776 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3777 return &sample_nearest_3d
;
3779 case GL_TEXTURE_CUBE_MAP
:
3781 return &sample_lambda_cube
;
3783 else if (sampler
->MinFilter
== GL_LINEAR
) {
3784 return &sample_linear_cube
;
3787 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3788 return &sample_nearest_cube
;
3790 case GL_TEXTURE_RECTANGLE_NV
:
3791 if (is_depth_texture(t
)) {
3792 return &sample_depth_texture
;
3794 else if (needLambda
) {
3795 return &sample_lambda_rect
;
3797 else if (sampler
->MinFilter
== GL_LINEAR
) {
3798 return &sample_linear_rect
;
3801 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3802 return &sample_nearest_rect
;
3804 case GL_TEXTURE_1D_ARRAY_EXT
:
3805 if (is_depth_texture(t
)) {
3806 return &sample_depth_texture
;
3808 else if (needLambda
) {
3809 return &sample_lambda_1d_array
;
3811 else if (sampler
->MinFilter
== GL_LINEAR
) {
3812 return &sample_linear_1d_array
;
3815 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3816 return &sample_nearest_1d_array
;
3818 case GL_TEXTURE_2D_ARRAY_EXT
:
3819 if (is_depth_texture(t
)) {
3820 return &sample_depth_texture
;
3822 else if (needLambda
) {
3823 return &sample_lambda_2d_array
;
3825 else if (sampler
->MinFilter
== GL_LINEAR
) {
3826 return &sample_linear_2d_array
;
3829 ASSERT(sampler
->MinFilter
== GL_NEAREST
);
3830 return &sample_nearest_2d_array
;
3834 "invalid target in _swrast_choose_texture_sample_func");
3835 return &null_sample_func
;