2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/colormac.h"
29 #include "main/imports.h"
31 #include "s_context.h"
32 #include "s_texfilter.h"
36 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
37 * see 1-pixel bands of improperly weighted linear-filtered textures.
38 * The tests/texwrap.c demo is a good test.
39 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
40 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
42 #define FRAC(f) ((f) - IFLOOR(f))
47 * Linear interpolation macro
49 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
53 * Do 2D/biliner interpolation of float values.
54 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
55 * a and b are the horizontal and vertical interpolants.
56 * It's important that this function is inlined when compiled with
57 * optimization! If we find that's not true on some systems, convert
61 lerp_2d(GLfloat a, GLfloat b,
62 GLfloat v00, GLfloat v10, GLfloat v01, GLfloat v11)
64 const GLfloat temp0 = LERP(a, v00, v10);
65 const GLfloat temp1 = LERP(a, v01, v11);
66 return LERP(b, temp0, temp1);
71 * Do 3D/trilinear interpolation of float values.
75 lerp_3d(GLfloat a, GLfloat b, GLfloat c,
76 GLfloat v000, GLfloat v100, GLfloat v010, GLfloat v110,
77 GLfloat v001, GLfloat v101, GLfloat v011, GLfloat v111)
79 const GLfloat temp00 = LERP(a, v000, v100);
80 const GLfloat temp10 = LERP(a, v010, v110);
81 const GLfloat temp01 = LERP(a, v001, v101);
82 const GLfloat temp11 = LERP(a, v011, v111);
83 const GLfloat temp0 = LERP(b, temp00, temp10);
84 const GLfloat temp1 = LERP(b, temp01, temp11);
85 return LERP(c, temp0, temp1);
90 * Do linear interpolation of colors.
93 lerp_rgba(GLfloat result[4], GLfloat t, const GLfloat a[4], const GLfloat b[4])
95 result[0] = LERP(t, a[0], b[0]);
96 result[1] = LERP(t, a[1], b[1]);
97 result[2] = LERP(t, a[2], b[2]);
98 result[3] = LERP(t, a[3], b[3]);
103 * Do bilinear interpolation of colors.
106 lerp_rgba_2d(GLfloat result[4], GLfloat a, GLfloat b,
107 const GLfloat t00[4], const GLfloat t10[4],
108 const GLfloat t01[4], const GLfloat t11[4])
110 result[0] = lerp_2d(a, b, t00[0], t10[0], t01[0], t11[0]);
111 result[1] = lerp_2d(a, b, t00[1], t10[1], t01[1], t11[1]);
112 result[2] = lerp_2d(a, b, t00[2], t10[2], t01[2], t11[2]);
113 result[3] = lerp_2d(a, b, t00[3], t10[3], t01[3], t11[3]);
118 * Do trilinear interpolation of colors.
121 lerp_rgba_3d(GLfloat result[4], GLfloat a, GLfloat b, GLfloat c,
122 const GLfloat t000[4], const GLfloat t100[4],
123 const GLfloat t010[4], const GLfloat t110[4],
124 const GLfloat t001[4], const GLfloat t101[4],
125 const GLfloat t011[4], const GLfloat t111[4])
128 /* compiler should unroll these short loops */
129 for (k = 0; k < 4; k++) {
130 result[k] = lerp_3d(a, b, c, t000[k], t100[k], t010[k], t110[k],
131 t001[k], t101[k], t011[k], t111[k]);
137 * Used for GL_REPEAT wrap mode. Using A % B doesn't produce the
138 * right results for A<0. Casting to A to be unsigned only works if B
139 * is a power of two. Adding a bias to A (which is a multiple of B)
140 * avoids the problems with A < 0 (for reasonable A) without using a
143 #define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
147 * Used to compute texel locations for linear sampling.
149 * wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
150 * s = texcoord in [0,1]
151 * size = width (or height or depth) of texture
153 * i0, i1 = returns two nearest texel indexes
154 * weight = returns blend factor between texels
157 linear_texel_locations(GLenum wrapMode,
158 const struct gl_texture_image *img,
159 GLint size, GLfloat s,
160 GLint *i0, GLint *i1, GLfloat *weight)
162 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
167 if (swImg->_IsPowerOfTwo) {
168 *i0 = IFLOOR(u) & (size - 1);
169 *i1 = (*i0 + 1) & (size - 1);
172 *i0 = REMAINDER(IFLOOR(u), size);
173 *i1 = REMAINDER(*i0 + 1, size);
176 case GL_CLAMP_TO_EDGE:
188 if (*i1 >= (GLint) size)
191 case GL_CLAMP_TO_BORDER:
193 const GLfloat min = -1.0F / (2.0F * size);
194 const GLfloat max = 1.0F - min;
206 case GL_MIRRORED_REPEAT:
208 const GLint flr = IFLOOR(s);
210 u = 1.0F - (s - (GLfloat) flr);
212 u = s - (GLfloat) flr;
213 u = (u * size) - 0.5F;
218 if (*i1 >= (GLint) size)
222 case GL_MIRROR_CLAMP_EXT:
232 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
243 if (*i1 >= (GLint) size)
246 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
248 const GLfloat min = -1.0F / (2.0F * size);
249 const GLfloat max = 1.0F - min;
274 _mesa_problem(NULL, "Bad wrap mode");
283 * Used to compute texel location for nearest sampling.
286 nearest_texel_location(GLenum wrapMode,
287 const struct gl_texture_image *img,
288 GLint size, GLfloat s)
290 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
295 /* s limited to [0,1) */
296 /* i limited to [0,size-1] */
297 i = IFLOOR(s * size);
298 if (swImg->_IsPowerOfTwo)
301 i = REMAINDER(i, size);
303 case GL_CLAMP_TO_EDGE:
305 /* s limited to [min,max] */
306 /* i limited to [0, size-1] */
307 const GLfloat min = 1.0F / (2.0F * size);
308 const GLfloat max = 1.0F - min;
314 i = IFLOOR(s * size);
317 case GL_CLAMP_TO_BORDER:
319 /* s limited to [min,max] */
320 /* i limited to [-1, size] */
321 const GLfloat min = -1.0F / (2.0F * size);
322 const GLfloat max = 1.0F - min;
328 i = IFLOOR(s * size);
331 case GL_MIRRORED_REPEAT:
333 const GLfloat min = 1.0F / (2.0F * size);
334 const GLfloat max = 1.0F - min;
335 const GLint flr = IFLOOR(s);
338 u = 1.0F - (s - (GLfloat) flr);
340 u = s - (GLfloat) flr;
346 i = IFLOOR(u * size);
349 case GL_MIRROR_CLAMP_EXT:
351 /* s limited to [0,1] */
352 /* i limited to [0,size-1] */
353 const GLfloat u = FABSF(s);
359 i = IFLOOR(u * size);
362 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
364 /* s limited to [min,max] */
365 /* i limited to [0, size-1] */
366 const GLfloat min = 1.0F / (2.0F * size);
367 const GLfloat max = 1.0F - min;
368 const GLfloat u = FABSF(s);
374 i = IFLOOR(u * size);
377 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
379 /* s limited to [min,max] */
380 /* i limited to [0, size-1] */
381 const GLfloat min = -1.0F / (2.0F * size);
382 const GLfloat max = 1.0F - min;
383 const GLfloat u = FABSF(s);
389 i = IFLOOR(u * size);
393 /* s limited to [0,1] */
394 /* i limited to [0,size-1] */
400 i = IFLOOR(s * size);
403 _mesa_problem(NULL, "Bad wrap mode");
409 /* Power of two image sizes only */
411 linear_repeat_texel_location(GLuint size, GLfloat s,
412 GLint *i0, GLint *i1, GLfloat *weight)
414 GLfloat u = s * size - 0.5F;
415 *i0 = IFLOOR(u) & (size - 1);
416 *i1 = (*i0 + 1) & (size - 1);
422 * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
425 clamp_rect_coord_nearest(GLenum wrapMode, GLfloat coord, GLint max)
429 return IFLOOR( CLAMP(coord, 0.0F, max - 1) );
430 case GL_CLAMP_TO_EDGE:
431 return IFLOOR( CLAMP(coord, 0.5F, max - 0.5F) );
432 case GL_CLAMP_TO_BORDER:
433 return IFLOOR( CLAMP(coord, -0.5F, max + 0.5F) );
435 _mesa_problem(NULL, "bad wrapMode in clamp_rect_coord_nearest");
442 * As above, but GL_LINEAR filtering.
445 clamp_rect_coord_linear(GLenum wrapMode, GLfloat coord, GLint max,
446 GLint *i0out, GLint *i1out, GLfloat *weight)
452 /* Not exactly what the spec says, but it matches NVIDIA output */
453 fcol = CLAMP(coord - 0.5F, 0.0F, max - 1);
457 case GL_CLAMP_TO_EDGE:
458 fcol = CLAMP(coord, 0.5F, max - 0.5F);
465 case GL_CLAMP_TO_BORDER:
466 fcol = CLAMP(coord, -0.5F, max + 0.5F);
472 _mesa_problem(NULL, "bad wrapMode in clamp_rect_coord_linear");
479 *weight = FRAC(fcol);
484 * Compute slice/image to use for 1D or 2D array texture.
487 tex_array_slice(GLfloat coord, GLsizei size)
489 GLint slice = IFLOOR(coord + 0.5f);
490 slice = CLAMP(slice, 0, size - 1);
496 * Compute nearest integer texcoords for given texobj and coordinate.
497 * NOTE: only used for depth texture sampling.
500 nearest_texcoord(const struct gl_texture_object *texObj,
502 const GLfloat texcoord[4],
503 GLint *i, GLint *j, GLint *k)
505 const struct gl_texture_image *img = texObj->Image[0][level];
506 const GLint width = img->Width;
507 const GLint height = img->Height;
508 const GLint depth = img->Depth;
510 switch (texObj->Target) {
511 case GL_TEXTURE_RECTANGLE_ARB:
512 *i = clamp_rect_coord_nearest(texObj->Sampler.WrapS, texcoord[0], width);
513 *j = clamp_rect_coord_nearest(texObj->Sampler.WrapT, texcoord[1], height);
517 *i = nearest_texel_location(texObj->Sampler.WrapS, img, width, texcoord[0]);
522 *i = nearest_texel_location(texObj->Sampler.WrapS, img, width, texcoord[0]);
523 *j = nearest_texel_location(texObj->Sampler.WrapT, img, height, texcoord[1]);
526 case GL_TEXTURE_1D_ARRAY_EXT:
527 *i = nearest_texel_location(texObj->Sampler.WrapS, img, width, texcoord[0]);
528 *j = tex_array_slice(texcoord[1], height);
531 case GL_TEXTURE_2D_ARRAY_EXT:
532 *i = nearest_texel_location(texObj->Sampler.WrapS, img, width, texcoord[0]);
533 *j = nearest_texel_location(texObj->Sampler.WrapT, img, height, texcoord[1]);
534 *k = tex_array_slice(texcoord[2], depth);
544 * Compute linear integer texcoords for given texobj and coordinate.
545 * NOTE: only used for depth texture sampling.
548 linear_texcoord(const struct gl_texture_object *texObj,
550 const GLfloat texcoord[4],
551 GLint *i0, GLint *i1, GLint *j0, GLint *j1, GLint *slice,
552 GLfloat *wi, GLfloat *wj)
554 const struct gl_texture_image *img = texObj->Image[0][level];
555 const GLint width = img->Width;
556 const GLint height = img->Height;
557 const GLint depth = img->Depth;
559 switch (texObj->Target) {
560 case GL_TEXTURE_RECTANGLE_ARB:
561 clamp_rect_coord_linear(texObj->Sampler.WrapS, texcoord[0],
563 clamp_rect_coord_linear(texObj->Sampler.WrapT, texcoord[1],
570 linear_texel_locations(texObj->Sampler.WrapS, img, width,
571 texcoord[0], i0, i1, wi);
572 linear_texel_locations(texObj->Sampler.WrapT, img, height,
573 texcoord[1], j0, j1, wj);
577 case GL_TEXTURE_1D_ARRAY_EXT:
578 linear_texel_locations(texObj->Sampler.WrapS, img, width,
579 texcoord[0], i0, i1, wi);
580 *j0 = tex_array_slice(texcoord[1], height);
585 case GL_TEXTURE_2D_ARRAY_EXT:
586 linear_texel_locations(texObj->Sampler.WrapS, img, width,
587 texcoord[0], i0, i1, wi);
588 linear_texel_locations(texObj->Sampler.WrapT, img, height,
589 texcoord[1], j0, j1, wj);
590 *slice = tex_array_slice(texcoord[2], depth);
602 * For linear interpolation between mipmap levels N and N+1, this function
606 linear_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
609 return tObj->BaseLevel;
610 else if (lambda > tObj->_MaxLambda)
611 return (GLint) (tObj->BaseLevel + tObj->_MaxLambda);
613 return (GLint) (tObj->BaseLevel + lambda);
618 * Compute the nearest mipmap level to take texels from.
621 nearest_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
627 else if (lambda > tObj->_MaxLambda + 0.4999F)
628 l = tObj->_MaxLambda + 0.4999F;
631 level = (GLint) (tObj->BaseLevel + l + 0.5F);
632 if (level > tObj->_MaxLevel)
633 level = tObj->_MaxLevel;
640 * Bitflags for texture border color sampling.
652 * The lambda[] array values are always monotonic. Either the whole span
653 * will be minified, magnified, or split between the two. This function
654 * determines the subranges in [0, n-1] that are to be minified or magnified.
657 compute_min_mag_ranges(const struct gl_texture_object *tObj,
658 GLuint n, const GLfloat lambda[],
659 GLuint *minStart, GLuint *minEnd,
660 GLuint *magStart, GLuint *magEnd)
662 GLfloat minMagThresh;
664 /* we shouldn't be here if minfilter == magfilter */
665 ASSERT(tObj->Sampler.MinFilter != tObj->Sampler.MagFilter);
667 /* This bit comes from the OpenGL spec: */
668 if (tObj->Sampler.MagFilter == GL_LINEAR
669 && (tObj->Sampler.MinFilter == GL_NEAREST_MIPMAP_NEAREST ||
670 tObj->Sampler.MinFilter == GL_NEAREST_MIPMAP_LINEAR)) {
678 /* DEBUG CODE: Verify that lambda[] is monotonic.
679 * We can't really use this because the inaccuracy in the LOG2 function
680 * causes this test to fail, yet the resulting texturing is correct.
684 printf("lambda delta = %g\n", lambda[0] - lambda[n-1]);
685 if (lambda[0] >= lambda[n-1]) { /* decreasing */
686 for (i = 0; i < n - 1; i++) {
687 ASSERT((GLint) (lambda[i] * 10) >= (GLint) (lambda[i+1] * 10));
690 else { /* increasing */
691 for (i = 0; i < n - 1; i++) {
692 ASSERT((GLint) (lambda[i] * 10) <= (GLint) (lambda[i+1] * 10));
698 if (lambda[0] <= minMagThresh && (n <= 1 || lambda[n-1] <= minMagThresh)) {
699 /* magnification for whole span */
702 *minStart = *minEnd = 0;
704 else if (lambda[0] > minMagThresh && (n <=1 || lambda[n-1] > minMagThresh)) {
705 /* minification for whole span */
708 *magStart = *magEnd = 0;
711 /* a mix of minification and magnification */
713 if (lambda[0] > minMagThresh) {
714 /* start with minification */
715 for (i = 1; i < n; i++) {
716 if (lambda[i] <= minMagThresh)
725 /* start with magnification */
726 for (i = 1; i < n; i++) {
727 if (lambda[i] > minMagThresh)
738 /* Verify the min/mag Start/End values
739 * We don't use this either (see above)
743 for (i = 0; i < n; i++) {
744 if (lambda[i] > minMagThresh) {
746 ASSERT(i >= *minStart);
751 ASSERT(i >= *magStart);
761 * When we sample the border color, it must be interpreted according to
762 * the base texture format. Ex: if the texture base format it GL_ALPHA,
763 * we return (0,0,0,BorderAlpha).
766 get_border_color(const struct gl_texture_object *tObj,
767 const struct gl_texture_image *img,
770 switch (img->_BaseFormat) {
772 rgba[0] = tObj->Sampler.BorderColor.f[0];
773 rgba[1] = tObj->Sampler.BorderColor.f[1];
774 rgba[2] = tObj->Sampler.BorderColor.f[2];
778 rgba[0] = rgba[1] = rgba[2] = 0.0;
779 rgba[3] = tObj->Sampler.BorderColor.f[3];
782 rgba[0] = rgba[1] = rgba[2] = tObj->Sampler.BorderColor.f[0];
785 case GL_LUMINANCE_ALPHA:
786 rgba[0] = rgba[1] = rgba[2] = tObj->Sampler.BorderColor.f[0];
787 rgba[3] = tObj->Sampler.BorderColor.f[3];
790 rgba[0] = rgba[1] = rgba[2] = rgba[3] = tObj->Sampler.BorderColor.f[0];
793 COPY_4V(rgba, tObj->Sampler.BorderColor.f);
799 /**********************************************************************/
800 /* 1-D Texture Sampling Functions */
801 /**********************************************************************/
804 * Return the texture sample for coordinate (s) using GL_NEAREST filter.
807 sample_1d_nearest(struct gl_context *ctx,
808 const struct gl_texture_object *tObj,
809 const struct gl_texture_image *img,
810 const GLfloat texcoord[4], GLfloat rgba[4])
812 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
813 const GLint width = img->Width2; /* without border, power of two */
815 i = nearest_texel_location(tObj->Sampler.WrapS, img, width, texcoord[0]);
816 /* skip over the border, if any */
818 if (i < 0 || i >= (GLint) img->Width) {
819 /* Need this test for GL_CLAMP_TO_BORDER mode */
820 get_border_color(tObj, img, rgba);
823 swImg->FetchTexel(swImg, i, 0, 0, rgba);
829 * Return the texture sample for coordinate (s) using GL_LINEAR filter.
832 sample_1d_linear(struct gl_context *ctx,
833 const struct gl_texture_object *tObj,
834 const struct gl_texture_image *img,
835 const GLfloat texcoord[4], GLfloat rgba[4])
837 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
838 const GLint width = img->Width2;
840 GLbitfield useBorderColor = 0x0;
842 GLfloat t0[4], t1[4]; /* texels */
844 linear_texel_locations(tObj->Sampler.WrapS, img, width, texcoord[0], &i0, &i1, &a);
851 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
852 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
855 /* fetch texel colors */
856 if (useBorderColor & I0BIT) {
857 get_border_color(tObj, img, t0);
860 swImg->FetchTexel(swImg, i0, 0, 0, t0);
862 if (useBorderColor & I1BIT) {
863 get_border_color(tObj, img, t1);
866 swImg->FetchTexel(swImg, i1, 0, 0, t1);
869 lerp_rgba(rgba, a, t0, t1);
874 sample_1d_nearest_mipmap_nearest(struct gl_context *ctx,
875 const struct gl_texture_object *tObj,
876 GLuint n, const GLfloat texcoord[][4],
877 const GLfloat lambda[], GLfloat rgba[][4])
880 ASSERT(lambda != NULL);
881 for (i = 0; i < n; i++) {
882 GLint level = nearest_mipmap_level(tObj, lambda[i]);
883 sample_1d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
889 sample_1d_linear_mipmap_nearest(struct gl_context *ctx,
890 const struct gl_texture_object *tObj,
891 GLuint n, const GLfloat texcoord[][4],
892 const GLfloat lambda[], GLfloat rgba[][4])
895 ASSERT(lambda != NULL);
896 for (i = 0; i < n; i++) {
897 GLint level = nearest_mipmap_level(tObj, lambda[i]);
898 sample_1d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
904 sample_1d_nearest_mipmap_linear(struct gl_context *ctx,
905 const struct gl_texture_object *tObj,
906 GLuint n, const GLfloat texcoord[][4],
907 const GLfloat lambda[], GLfloat rgba[][4])
910 ASSERT(lambda != NULL);
911 for (i = 0; i < n; i++) {
912 GLint level = linear_mipmap_level(tObj, lambda[i]);
913 if (level >= tObj->_MaxLevel) {
914 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
915 texcoord[i], rgba[i]);
918 GLfloat t0[4], t1[4];
919 const GLfloat f = FRAC(lambda[i]);
920 sample_1d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
921 sample_1d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
922 lerp_rgba(rgba[i], f, t0, t1);
929 sample_1d_linear_mipmap_linear(struct gl_context *ctx,
930 const struct gl_texture_object *tObj,
931 GLuint n, const GLfloat texcoord[][4],
932 const GLfloat lambda[], GLfloat rgba[][4])
935 ASSERT(lambda != NULL);
936 for (i = 0; i < n; i++) {
937 GLint level = linear_mipmap_level(tObj, lambda[i]);
938 if (level >= tObj->_MaxLevel) {
939 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
940 texcoord[i], rgba[i]);
943 GLfloat t0[4], t1[4];
944 const GLfloat f = FRAC(lambda[i]);
945 sample_1d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
946 sample_1d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
947 lerp_rgba(rgba[i], f, t0, t1);
953 /** Sample 1D texture, nearest filtering for both min/magnification */
955 sample_nearest_1d( struct gl_context *ctx,
956 const struct gl_texture_object *tObj, GLuint n,
957 const GLfloat texcoords[][4], const GLfloat lambda[],
961 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
963 for (i = 0; i < n; i++) {
964 sample_1d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
969 /** Sample 1D texture, linear filtering for both min/magnification */
971 sample_linear_1d( struct gl_context *ctx,
972 const struct gl_texture_object *tObj, GLuint n,
973 const GLfloat texcoords[][4], const GLfloat lambda[],
977 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
979 for (i = 0; i < n; i++) {
980 sample_1d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
985 /** Sample 1D texture, using lambda to choose between min/magnification */
987 sample_lambda_1d( struct gl_context *ctx,
988 const struct gl_texture_object *tObj, GLuint n,
989 const GLfloat texcoords[][4],
990 const GLfloat lambda[], GLfloat rgba[][4] )
992 GLuint minStart, minEnd; /* texels with minification */
993 GLuint magStart, magEnd; /* texels with magnification */
996 ASSERT(lambda != NULL);
997 compute_min_mag_ranges(tObj, n, lambda,
998 &minStart, &minEnd, &magStart, &magEnd);
1000 if (minStart < minEnd) {
1001 /* do the minified texels */
1002 const GLuint m = minEnd - minStart;
1003 switch (tObj->Sampler.MinFilter) {
1005 for (i = minStart; i < minEnd; i++)
1006 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1007 texcoords[i], rgba[i]);
1010 for (i = minStart; i < minEnd; i++)
1011 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1012 texcoords[i], rgba[i]);
1014 case GL_NEAREST_MIPMAP_NEAREST:
1015 sample_1d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1016 lambda + minStart, rgba + minStart);
1018 case GL_LINEAR_MIPMAP_NEAREST:
1019 sample_1d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1020 lambda + minStart, rgba + minStart);
1022 case GL_NEAREST_MIPMAP_LINEAR:
1023 sample_1d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1024 lambda + minStart, rgba + minStart);
1026 case GL_LINEAR_MIPMAP_LINEAR:
1027 sample_1d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1028 lambda + minStart, rgba + minStart);
1031 _mesa_problem(ctx, "Bad min filter in sample_1d_texture");
1036 if (magStart < magEnd) {
1037 /* do the magnified texels */
1038 switch (tObj->Sampler.MagFilter) {
1040 for (i = magStart; i < magEnd; i++)
1041 sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1042 texcoords[i], rgba[i]);
1045 for (i = magStart; i < magEnd; i++)
1046 sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
1047 texcoords[i], rgba[i]);
1050 _mesa_problem(ctx, "Bad mag filter in sample_1d_texture");
1057 /**********************************************************************/
1058 /* 2-D Texture Sampling Functions */
1059 /**********************************************************************/
1063 * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
1066 sample_2d_nearest(struct gl_context *ctx,
1067 const struct gl_texture_object *tObj,
1068 const struct gl_texture_image *img,
1069 const GLfloat texcoord[4],
1072 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
1073 const GLint width = img->Width2; /* without border, power of two */
1074 const GLint height = img->Height2; /* without border, power of two */
1078 i = nearest_texel_location(tObj->Sampler.WrapS, img, width, texcoord[0]);
1079 j = nearest_texel_location(tObj->Sampler.WrapT, img, height, texcoord[1]);
1081 /* skip over the border, if any */
1085 if (i < 0 || i >= (GLint) img->Width || j < 0 || j >= (GLint) img->Height) {
1086 /* Need this test for GL_CLAMP_TO_BORDER mode */
1087 get_border_color(tObj, img, rgba);
1090 swImg->FetchTexel(swImg, i, j, 0, rgba);
1096 * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
1097 * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
1100 sample_2d_linear(struct gl_context *ctx,
1101 const struct gl_texture_object *tObj,
1102 const struct gl_texture_image *img,
1103 const GLfloat texcoord[4],
1106 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
1107 const GLint width = img->Width2;
1108 const GLint height = img->Height2;
1109 GLint i0, j0, i1, j1;
1110 GLbitfield useBorderColor = 0x0;
1112 GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
1114 linear_texel_locations(tObj->Sampler.WrapS, img, width, texcoord[0], &i0, &i1, &a);
1115 linear_texel_locations(tObj->Sampler.WrapT, img, height, texcoord[1], &j0, &j1, &b);
1124 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
1125 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
1126 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
1127 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
1130 /* fetch four texel colors */
1131 if (useBorderColor & (I0BIT | J0BIT)) {
1132 get_border_color(tObj, img, t00);
1135 swImg->FetchTexel(swImg, i0, j0, 0, t00);
1137 if (useBorderColor & (I1BIT | J0BIT)) {
1138 get_border_color(tObj, img, t10);
1141 swImg->FetchTexel(swImg, i1, j0, 0, t10);
1143 if (useBorderColor & (I0BIT | J1BIT)) {
1144 get_border_color(tObj, img, t01);
1147 swImg->FetchTexel(swImg, i0, j1, 0, t01);
1149 if (useBorderColor & (I1BIT | J1BIT)) {
1150 get_border_color(tObj, img, t11);
1153 swImg->FetchTexel(swImg, i1, j1, 0, t11);
1156 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
1161 * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
1162 * We don't have to worry about the texture border.
1165 sample_2d_linear_repeat(struct gl_context *ctx,
1166 const struct gl_texture_object *tObj,
1167 const struct gl_texture_image *img,
1168 const GLfloat texcoord[4],
1171 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
1172 const GLint width = img->Width2;
1173 const GLint height = img->Height2;
1174 GLint i0, j0, i1, j1;
1176 GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
1180 ASSERT(tObj->Sampler.WrapS == GL_REPEAT);
1181 ASSERT(tObj->Sampler.WrapT == GL_REPEAT);
1182 ASSERT(img->Border == 0);
1183 ASSERT(swImg->_IsPowerOfTwo);
1185 linear_repeat_texel_location(width, texcoord[0], &i0, &i1, &wi);
1186 linear_repeat_texel_location(height, texcoord[1], &j0, &j1, &wj);
1188 swImg->FetchTexel(swImg, i0, j0, 0, t00);
1189 swImg->FetchTexel(swImg, i1, j0, 0, t10);
1190 swImg->FetchTexel(swImg, i0, j1, 0, t01);
1191 swImg->FetchTexel(swImg, i1, j1, 0, t11);
1193 lerp_rgba_2d(rgba, wi, wj, t00, t10, t01, t11);
1198 sample_2d_nearest_mipmap_nearest(struct gl_context *ctx,
1199 const struct gl_texture_object *tObj,
1200 GLuint n, const GLfloat texcoord[][4],
1201 const GLfloat lambda[], GLfloat rgba[][4])
1204 for (i = 0; i < n; i++) {
1205 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1206 sample_2d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1212 sample_2d_linear_mipmap_nearest(struct gl_context *ctx,
1213 const struct gl_texture_object *tObj,
1214 GLuint n, const GLfloat texcoord[][4],
1215 const GLfloat lambda[], GLfloat rgba[][4])
1218 ASSERT(lambda != NULL);
1219 for (i = 0; i < n; i++) {
1220 GLint level = nearest_mipmap_level(tObj, lambda[i]);
1221 sample_2d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
1227 sample_2d_nearest_mipmap_linear(struct gl_context *ctx,
1228 const struct gl_texture_object *tObj,
1229 GLuint n, const GLfloat texcoord[][4],
1230 const GLfloat lambda[], GLfloat rgba[][4])
1233 ASSERT(lambda != NULL);
1234 for (i = 0; i < n; i++) {
1235 GLint level = linear_mipmap_level(tObj, lambda[i]);
1236 if (level >= tObj->_MaxLevel) {
1237 sample_2d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1238 texcoord[i], rgba[i]);
1241 GLfloat t0[4], t1[4]; /* texels */
1242 const GLfloat f = FRAC(lambda[i]);
1243 sample_2d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1244 sample_2d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1245 lerp_rgba(rgba[i], f, t0, t1);
1252 sample_2d_linear_mipmap_linear( struct gl_context *ctx,
1253 const struct gl_texture_object *tObj,
1254 GLuint n, const GLfloat texcoord[][4],
1255 const GLfloat lambda[], GLfloat rgba[][4] )
1258 ASSERT(lambda != NULL);
1259 for (i = 0; i < n; i++) {
1260 GLint level = linear_mipmap_level(tObj, lambda[i]);
1261 if (level >= tObj->_MaxLevel) {
1262 sample_2d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1263 texcoord[i], rgba[i]);
1266 GLfloat t0[4], t1[4]; /* texels */
1267 const GLfloat f = FRAC(lambda[i]);
1268 sample_2d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
1269 sample_2d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
1270 lerp_rgba(rgba[i], f, t0, t1);
1277 sample_2d_linear_mipmap_linear_repeat(struct gl_context *ctx,
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 ASSERT(tObj->Sampler.WrapS == GL_REPEAT);
1285 ASSERT(tObj->Sampler.WrapT == GL_REPEAT);
1286 for (i = 0; i < n; i++) {
1287 GLint level = linear_mipmap_level(tObj, lambda[i]);
1288 if (level >= tObj->_MaxLevel) {
1289 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1290 texcoord[i], rgba[i]);
1293 GLfloat t0[4], t1[4]; /* texels */
1294 const GLfloat f = FRAC(lambda[i]);
1295 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level ],
1297 sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level+1],
1299 lerp_rgba(rgba[i], f, t0, t1);
1305 /** Sample 2D texture, nearest filtering for both min/magnification */
1307 sample_nearest_2d(struct gl_context *ctx,
1308 const struct gl_texture_object *tObj, GLuint n,
1309 const GLfloat texcoords[][4],
1310 const GLfloat lambda[], GLfloat rgba[][4])
1313 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1315 for (i = 0; i < n; i++) {
1316 sample_2d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
1321 /** Sample 2D texture, linear filtering for both min/magnification */
1323 sample_linear_2d(struct gl_context *ctx,
1324 const struct gl_texture_object *tObj, GLuint n,
1325 const GLfloat texcoords[][4],
1326 const GLfloat lambda[], GLfloat rgba[][4])
1329 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
1330 const struct swrast_texture_image *swImg = swrast_texture_image_const(image);
1332 if (tObj->Sampler.WrapS == GL_REPEAT &&
1333 tObj->Sampler.WrapT == GL_REPEAT &&
1334 swImg->_IsPowerOfTwo &&
1335 image->Border == 0) {
1336 for (i = 0; i < n; i++) {
1337 sample_2d_linear_repeat(ctx, tObj, image, texcoords[i], rgba[i]);
1341 for (i = 0; i < n; i++) {
1342 sample_2d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
1349 * Optimized 2-D texture sampling:
1350 * S and T wrap mode == GL_REPEAT
1351 * GL_NEAREST min/mag filter
1353 * RowStride == Width,
1357 opt_sample_rgb_2d(struct gl_context *ctx,
1358 const struct gl_texture_object *tObj,
1359 GLuint n, const GLfloat texcoords[][4],
1360 const GLfloat lambda[], GLfloat rgba[][4])
1362 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1363 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
1364 const GLfloat width = (GLfloat) img->Width;
1365 const GLfloat height = (GLfloat) img->Height;
1366 const GLint colMask = img->Width - 1;
1367 const GLint rowMask = img->Height - 1;
1368 const GLint shift = img->WidthLog2;
1372 ASSERT(tObj->Sampler.WrapS==GL_REPEAT);
1373 ASSERT(tObj->Sampler.WrapT==GL_REPEAT);
1374 ASSERT(img->Border==0);
1375 ASSERT(img->TexFormat == MESA_FORMAT_RGB888);
1376 ASSERT(swImg->_IsPowerOfTwo);
1379 for (k=0; k<n; k++) {
1380 GLint i = IFLOOR(texcoords[k][0] * width) & colMask;
1381 GLint j = IFLOOR(texcoords[k][1] * height) & rowMask;
1382 GLint pos = (j << shift) | i;
1383 GLubyte *texel = swImg->Data + 3 * pos;
1384 rgba[k][RCOMP] = UBYTE_TO_FLOAT(texel[2]);
1385 rgba[k][GCOMP] = UBYTE_TO_FLOAT(texel[1]);
1386 rgba[k][BCOMP] = UBYTE_TO_FLOAT(texel[0]);
1387 rgba[k][ACOMP] = 1.0F;
1393 * Optimized 2-D texture sampling:
1394 * S and T wrap mode == GL_REPEAT
1395 * GL_NEAREST min/mag filter
1397 * RowStride == Width,
1401 opt_sample_rgba_2d(struct gl_context *ctx,
1402 const struct gl_texture_object *tObj,
1403 GLuint n, const GLfloat texcoords[][4],
1404 const GLfloat lambda[], GLfloat rgba[][4])
1406 const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
1407 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
1408 const GLfloat width = (GLfloat) img->Width;
1409 const GLfloat height = (GLfloat) img->Height;
1410 const GLint colMask = img->Width - 1;
1411 const GLint rowMask = img->Height - 1;
1412 const GLint shift = img->WidthLog2;
1416 ASSERT(tObj->Sampler.WrapS==GL_REPEAT);
1417 ASSERT(tObj->Sampler.WrapT==GL_REPEAT);
1418 ASSERT(img->Border==0);
1419 ASSERT(img->TexFormat == MESA_FORMAT_RGBA8888);
1420 ASSERT(swImg->_IsPowerOfTwo);
1423 for (i = 0; i < n; i++) {
1424 const GLint col = IFLOOR(texcoords[i][0] * width) & colMask;
1425 const GLint row = IFLOOR(texcoords[i][1] * height) & rowMask;
1426 const GLint pos = (row << shift) | col;
1427 const GLuint texel = *((GLuint *) swImg->Data + pos);
1428 rgba[i][RCOMP] = UBYTE_TO_FLOAT( (texel >> 24) );
1429 rgba[i][GCOMP] = UBYTE_TO_FLOAT( (texel >> 16) & 0xff );
1430 rgba[i][BCOMP] = UBYTE_TO_FLOAT( (texel >> 8) & 0xff );
1431 rgba[i][ACOMP] = UBYTE_TO_FLOAT( (texel ) & 0xff );
1436 /** Sample 2D texture, using lambda to choose between min/magnification */
1438 sample_lambda_2d(struct gl_context *ctx,
1439 const struct gl_texture_object *tObj,
1440 GLuint n, const GLfloat texcoords[][4],
1441 const GLfloat lambda[], GLfloat rgba[][4])
1443 const struct gl_texture_image *tImg = tObj->Image[0][tObj->BaseLevel];
1444 const struct swrast_texture_image *swImg = swrast_texture_image_const(tImg);
1445 GLuint minStart, minEnd; /* texels with minification */
1446 GLuint magStart, magEnd; /* texels with magnification */
1448 const GLboolean repeatNoBorderPOT = (tObj->Sampler.WrapS == GL_REPEAT)
1449 && (tObj->Sampler.WrapT == GL_REPEAT)
1450 && (tImg->Border == 0 && (tImg->Width == swImg->RowStride))
1451 && swImg->_IsPowerOfTwo;
1453 ASSERT(lambda != NULL);
1454 compute_min_mag_ranges(tObj, n, lambda,
1455 &minStart, &minEnd, &magStart, &magEnd);
1457 if (minStart < minEnd) {
1458 /* do the minified texels */
1459 const GLuint m = minEnd - minStart;
1460 switch (tObj->Sampler.MinFilter) {
1462 if (repeatNoBorderPOT) {
1463 switch (tImg->TexFormat) {
1464 case MESA_FORMAT_RGB888:
1465 opt_sample_rgb_2d(ctx, tObj, m, texcoords + minStart,
1466 NULL, rgba + minStart);
1468 case MESA_FORMAT_RGBA8888:
1469 opt_sample_rgba_2d(ctx, tObj, m, texcoords + minStart,
1470 NULL, rgba + minStart);
1473 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1474 NULL, rgba + minStart );
1478 sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
1479 NULL, rgba + minStart);
1483 sample_linear_2d(ctx, tObj, m, texcoords + minStart,
1484 NULL, rgba + minStart);
1486 case GL_NEAREST_MIPMAP_NEAREST:
1487 sample_2d_nearest_mipmap_nearest(ctx, tObj, m,
1488 texcoords + minStart,
1489 lambda + minStart, rgba + minStart);
1491 case GL_LINEAR_MIPMAP_NEAREST:
1492 sample_2d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
1493 lambda + minStart, rgba + minStart);
1495 case GL_NEAREST_MIPMAP_LINEAR:
1496 sample_2d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1497 lambda + minStart, rgba + minStart);
1499 case GL_LINEAR_MIPMAP_LINEAR:
1500 if (repeatNoBorderPOT)
1501 sample_2d_linear_mipmap_linear_repeat(ctx, tObj, m,
1502 texcoords + minStart, lambda + minStart, rgba + minStart);
1504 sample_2d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
1505 lambda + minStart, rgba + minStart);
1508 _mesa_problem(ctx, "Bad min filter in sample_2d_texture");
1513 if (magStart < magEnd) {
1514 /* do the magnified texels */
1515 const GLuint m = magEnd - magStart;
1517 switch (tObj->Sampler.MagFilter) {
1519 if (repeatNoBorderPOT) {
1520 switch (tImg->TexFormat) {
1521 case MESA_FORMAT_RGB888:
1522 opt_sample_rgb_2d(ctx, tObj, m, texcoords + magStart,
1523 NULL, rgba + magStart);
1525 case MESA_FORMAT_RGBA8888:
1526 opt_sample_rgba_2d(ctx, tObj, m, texcoords + magStart,
1527 NULL, rgba + magStart);
1530 sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
1531 NULL, rgba + magStart );
1535 sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
1536 NULL, rgba + magStart);
1540 sample_linear_2d(ctx, tObj, m, texcoords + magStart,
1541 NULL, rgba + magStart);
1544 _mesa_problem(ctx, "Bad mag filter in sample_lambda_2d");
1551 /* For anisotropic filtering */
1552 #define WEIGHT_LUT_SIZE 1024
1554 static GLfloat *weightLut = NULL;
1557 * Creates the look-up table used to speed-up EWA sampling
1560 create_filter_table(void)
1564 weightLut = (GLfloat *) malloc(WEIGHT_LUT_SIZE * sizeof(GLfloat));
1566 for (i = 0; i < WEIGHT_LUT_SIZE; ++i) {
1568 GLfloat r2 = (GLfloat) i / (GLfloat) (WEIGHT_LUT_SIZE - 1);
1569 GLfloat weight = (GLfloat) exp(-alpha * r2);
1570 weightLut[i] = weight;
1577 * Elliptical weighted average (EWA) filter for producing high quality
1578 * anisotropic filtered results.
1579 * Based on the Higher Quality Elliptical Weighted Avarage Filter
1580 * published by Paul S. Heckbert in his Master's Thesis
1581 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1584 sample_2d_ewa(struct gl_context *ctx,
1585 const struct gl_texture_object *tObj,
1586 const GLfloat texcoord[4],
1587 const GLfloat dudx, const GLfloat dvdx,
1588 const GLfloat dudy, const GLfloat dvdy, const GLint lod,
1591 GLint level = lod > 0 ? lod : 0;
1592 GLfloat scaling = 1.0 / (1 << level);
1593 const struct gl_texture_image *img = tObj->Image[0][level];
1594 const struct gl_texture_image *mostDetailedImage =
1595 tObj->Image[0][tObj->BaseLevel];
1596 const struct swrast_texture_image *swImg =
1597 swrast_texture_image_const(mostDetailedImage);
1598 GLfloat tex_u=-0.5 + texcoord[0] * swImg->WidthScale * scaling;
1599 GLfloat tex_v=-0.5 + texcoord[1] * swImg->HeightScale * scaling;
1601 GLfloat ux = dudx * scaling;
1602 GLfloat vx = dvdx * scaling;
1603 GLfloat uy = dudy * scaling;
1604 GLfloat vy = dvdy * scaling;
1606 /* compute ellipse coefficients to bound the region:
1607 * A*x*x + B*x*y + C*y*y = F.
1609 GLfloat A = vx*vx+vy*vy+1;
1610 GLfloat B = -2*(ux*vx+uy*vy);
1611 GLfloat C = ux*ux+uy*uy+1;
1612 GLfloat F = A*C-B*B/4.0;
1614 /* check if it is an ellipse */
1615 /* ASSERT(F > 0.0); */
1617 /* Compute the ellipse's (u,v) bounding box in texture space */
1618 GLfloat d = -B*B+4.0*C*A;
1619 GLfloat box_u = 2.0 / d * sqrt(d*C*F); /* box_u -> half of bbox with */
1620 GLfloat box_v = 2.0 / d * sqrt(A*d*F); /* box_v -> half of bbox height */
1622 GLint u0 = floor(tex_u - box_u);
1623 GLint u1 = ceil (tex_u + box_u);
1624 GLint v0 = floor(tex_v - box_v);
1625 GLint v1 = ceil (tex_v + box_v);
1627 GLfloat num[4] = {0.0F, 0.0F, 0.0F, 0.0F};
1628 GLfloat newCoord[2];
1631 GLfloat U = u0 - tex_u;
1634 /* Scale ellipse formula to directly index the Filter Lookup Table.
1635 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1637 double formScale = (double) (WEIGHT_LUT_SIZE - 1) / F;
1641 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1643 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1644 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1645 * value, q, is less than F, we're inside the ellipse
1648 for (v = v0; v <= v1; ++v) {
1649 GLfloat V = v - tex_v;
1650 GLfloat dq = A * (2 * U + 1) + B * V;
1651 GLfloat q = (C * V + B * U) * V + A * U * U;
1654 for (u = u0; u <= u1; ++u) {
1655 /* Note that the ellipse has been pre-scaled so F = WEIGHT_LUT_SIZE - 1 */
1656 if (q < WEIGHT_LUT_SIZE) {
1657 /* as a LUT is used, q must never be negative;
1658 * should not happen, though
1660 const GLint qClamped = q >= 0.0F ? q : 0;
1661 GLfloat weight = weightLut[qClamped];
1663 newCoord[0] = u / ((GLfloat) img->Width2);
1664 newCoord[1] = v / ((GLfloat) img->Height2);
1666 sample_2d_nearest(ctx, tObj, img, newCoord, rgba);
1667 num[0] += weight * rgba[0];
1668 num[1] += weight * rgba[1];
1669 num[2] += weight * rgba[2];
1670 num[3] += weight * rgba[3];
1680 /* Reaching this place would mean
1681 * that no pixels intersected the ellipse.
1682 * This should never happen because
1683 * the filter we use always
1684 * intersects at least one pixel.
1691 /* not enough pixels in resampling, resort to direct interpolation */
1692 sample_2d_linear(ctx, tObj, img, texcoord, rgba);
1696 rgba[0] = num[0] / den;
1697 rgba[1] = num[1] / den;
1698 rgba[2] = num[2] / den;
1699 rgba[3] = num[3] / den;
1704 * Anisotropic filtering using footprint assembly as outlined in the
1705 * EXT_texture_filter_anisotropic spec:
1706 * http://www.opengl.org/registry/specs/EXT/texture_filter_anisotropic.txt
1707 * Faster than EWA but has less quality (more aliasing effects)
1710 sample_2d_footprint(struct gl_context *ctx,
1711 const struct gl_texture_object *tObj,
1712 const GLfloat texcoord[4],
1713 const GLfloat dudx, const GLfloat dvdx,
1714 const GLfloat dudy, const GLfloat dvdy, const GLint lod,
1717 GLint level = lod > 0 ? lod : 0;
1718 GLfloat scaling = 1.0F / (1 << level);
1719 const struct gl_texture_image *img = tObj->Image[0][level];
1721 GLfloat ux = dudx * scaling;
1722 GLfloat vx = dvdx * scaling;
1723 GLfloat uy = dudy * scaling;
1724 GLfloat vy = dvdy * scaling;
1726 GLfloat Px2 = ux * ux + vx * vx; /* squared length of dx */
1727 GLfloat Py2 = uy * uy + vy * vy; /* squared length of dy */
1733 GLfloat num[4] = {0.0F, 0.0F, 0.0F, 0.0F};
1734 GLfloat newCoord[2];
1737 /* Calculate the per anisotropic sample offsets in s,t space. */
1739 numSamples = ceil(SQRTF(Px2));
1740 ds = ux / ((GLfloat) img->Width2);
1741 dt = vx / ((GLfloat) img->Height2);
1744 numSamples = ceil(SQRTF(Py2));
1745 ds = uy / ((GLfloat) img->Width2);
1746 dt = vy / ((GLfloat) img->Height2);
1749 for (s = 0; s<numSamples; s++) {
1750 newCoord[0] = texcoord[0] + ds * ((GLfloat)(s+1) / (numSamples+1) -0.5);
1751 newCoord[1] = texcoord[1] + dt * ((GLfloat)(s+1) / (numSamples+1) -0.5);
1753 sample_2d_linear(ctx, tObj, img, newCoord, rgba);
1760 rgba[0] = num[0] / numSamples;
1761 rgba[1] = num[1] / numSamples;
1762 rgba[2] = num[2] / numSamples;
1763 rgba[3] = num[3] / numSamples;
1768 * Returns the index of the specified texture object in the
1769 * gl_context texture unit array.
1771 static inline GLuint
1772 texture_unit_index(const struct gl_context *ctx,
1773 const struct gl_texture_object *tObj)
1775 const GLuint maxUnit
1776 = (ctx->Texture._EnabledCoordUnits > 1) ? ctx->Const.MaxTextureUnits : 1;
1779 /* XXX CoordUnits vs. ImageUnits */
1780 for (u = 0; u < maxUnit; u++) {
1781 if (ctx->Texture.Unit[u]._Current == tObj)
1785 u = 0; /* not found, use 1st one; should never happen */
1792 * Sample 2D texture using an anisotropic filter.
1793 * NOTE: the const GLfloat lambda_iso[] parameter does *NOT* contain
1794 * the lambda float array but a "hidden" SWspan struct which is required
1795 * by this function but is not available in the texture_sample_func signature.
1796 * See _swrast_texture_span( struct gl_context *ctx, SWspan *span ) on how
1797 * this function is called.
1800 sample_lambda_2d_aniso(struct gl_context *ctx,
1801 const struct gl_texture_object *tObj,
1802 GLuint n, const GLfloat texcoords[][4],
1803 const GLfloat lambda_iso[], GLfloat rgba[][4])
1805 const struct gl_texture_image *tImg = tObj->Image[0][tObj->BaseLevel];
1806 const struct swrast_texture_image *swImg = swrast_texture_image_const(tImg);
1807 const GLfloat maxEccentricity =
1808 tObj->Sampler.MaxAnisotropy * tObj->Sampler.MaxAnisotropy;
1810 /* re-calculate the lambda values so that they are usable with anisotropic
1813 SWspan *span = (SWspan *)lambda_iso; /* access the "hidden" SWspan struct */
1815 /* based on interpolate_texcoords(struct gl_context *ctx, SWspan *span)
1816 * in swrast/s_span.c
1819 /* find the texture unit index by looking up the current texture object
1820 * from the context list of available texture objects.
1822 const GLuint u = texture_unit_index(ctx, tObj);
1823 const GLuint attr = FRAG_ATTRIB_TEX0 + u;
1826 const GLfloat dsdx = span->attrStepX[attr][0];
1827 const GLfloat dsdy = span->attrStepY[attr][0];
1828 const GLfloat dtdx = span->attrStepX[attr][1];
1829 const GLfloat dtdy = span->attrStepY[attr][1];
1830 const GLfloat dqdx = span->attrStepX[attr][3];
1831 const GLfloat dqdy = span->attrStepY[attr][3];
1832 GLfloat s = span->attrStart[attr][0] + span->leftClip * dsdx;
1833 GLfloat t = span->attrStart[attr][1] + span->leftClip * dtdx;
1834 GLfloat q = span->attrStart[attr][3] + span->leftClip * dqdx;
1836 /* from swrast/s_texcombine.c _swrast_texture_span */
1837 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[u];
1838 const GLboolean adjustLOD =
1839 (texUnit->LodBias + tObj->Sampler.LodBias != 0.0F)
1840 || (tObj->Sampler.MinLod != -1000.0 || tObj->Sampler.MaxLod != 1000.0);
1844 /* on first access create the lookup table containing the filter weights. */
1846 create_filter_table();
1849 texW = swImg->WidthScale;
1850 texH = swImg->HeightScale;
1852 for (i = 0; i < n; i++) {
1853 const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
1855 GLfloat dudx = texW * ((s + dsdx) / (q + dqdx) - s * invQ);
1856 GLfloat dvdx = texH * ((t + dtdx) / (q + dqdx) - t * invQ);
1857 GLfloat dudy = texW * ((s + dsdy) / (q + dqdy) - s * invQ);
1858 GLfloat dvdy = texH * ((t + dtdy) / (q + dqdy) - t * invQ);
1860 /* note: instead of working with Px and Py, we will use the
1861 * squared length instead, to avoid sqrt.
1863 GLfloat Px2 = dudx * dudx + dvdx * dvdx;
1864 GLfloat Py2 = dudy * dudy + dvdy * dvdy;
1884 /* if the eccentricity of the ellipse is too big, scale up the shorter
1885 * of the two vectors to limit the maximum amount of work per pixel
1888 if (e > maxEccentricity) {
1889 /* GLfloat s=e / maxEccentricity;
1893 Pmin2 = Pmax2 / maxEccentricity;
1896 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
1897 * this since 0.5*log(x) = log(sqrt(x))
1899 lod = 0.5 * LOG2(Pmin2);
1902 /* from swrast/s_texcombine.c _swrast_texture_span */
1903 if (texUnit->LodBias + tObj->Sampler.LodBias != 0.0F) {
1904 /* apply LOD bias, but don't clamp yet */
1905 const GLfloat bias =
1906 CLAMP(texUnit->LodBias + tObj->Sampler.LodBias,
1907 -ctx->Const.MaxTextureLodBias,
1908 ctx->Const.MaxTextureLodBias);
1911 if (tObj->Sampler.MinLod != -1000.0 ||
1912 tObj->Sampler.MaxLod != 1000.0) {
1913 /* apply LOD clamping to lambda */
1914 lod = CLAMP(lod, tObj->Sampler.MinLod, tObj->Sampler.MaxLod);
1919 /* If the ellipse covers the whole image, we can
1920 * simply return the average of the whole image.
1922 if (lod >= tObj->_MaxLevel) {
1923 sample_2d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
1924 texcoords[i], rgba[i]);
1927 /* don't bother interpolating between multiple LODs; it doesn't
1928 * seem to be worth the extra running time.
1930 sample_2d_ewa(ctx, tObj, texcoords[i],
1931 dudx, dvdx, dudy, dvdy, floor(lod), rgba[i]);
1934 (void) sample_2d_footprint;
1936 sample_2d_footprint(ctx, tObj, texcoords[i],
1937 dudx, dvdx, dudy, dvdy, floor(lod), rgba[i]);
1945 /**********************************************************************/
1946 /* 3-D Texture Sampling Functions */
1947 /**********************************************************************/
1950 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
1953 sample_3d_nearest(struct gl_context *ctx,
1954 const struct gl_texture_object *tObj,
1955 const struct gl_texture_image *img,
1956 const GLfloat texcoord[4],
1959 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
1960 const GLint width = img->Width2; /* without border, power of two */
1961 const GLint height = img->Height2; /* without border, power of two */
1962 const GLint depth = img->Depth2; /* without border, power of two */
1966 i = nearest_texel_location(tObj->Sampler.WrapS, img, width, texcoord[0]);
1967 j = nearest_texel_location(tObj->Sampler.WrapT, img, height, texcoord[1]);
1968 k = nearest_texel_location(tObj->Sampler.WrapR, img, depth, texcoord[2]);
1970 if (i < 0 || i >= (GLint) img->Width ||
1971 j < 0 || j >= (GLint) img->Height ||
1972 k < 0 || k >= (GLint) img->Depth) {
1973 /* Need this test for GL_CLAMP_TO_BORDER mode */
1974 get_border_color(tObj, img, rgba);
1977 swImg->FetchTexel(swImg, i, j, k, rgba);
1983 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
1986 sample_3d_linear(struct gl_context *ctx,
1987 const struct gl_texture_object *tObj,
1988 const struct gl_texture_image *img,
1989 const GLfloat texcoord[4],
1992 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
1993 const GLint width = img->Width2;
1994 const GLint height = img->Height2;
1995 const GLint depth = img->Depth2;
1996 GLint i0, j0, k0, i1, j1, k1;
1997 GLbitfield useBorderColor = 0x0;
1999 GLfloat t000[4], t010[4], t001[4], t011[4];
2000 GLfloat t100[4], t110[4], t101[4], t111[4];
2002 linear_texel_locations(tObj->Sampler.WrapS, img, width, texcoord[0], &i0, &i1, &a);
2003 linear_texel_locations(tObj->Sampler.WrapT, img, height, texcoord[1], &j0, &j1, &b);
2004 linear_texel_locations(tObj->Sampler.WrapR, img, depth, texcoord[2], &k0, &k1, &c);
2015 /* check if sampling texture border color */
2016 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2017 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2018 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
2019 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
2020 if (k0 < 0 || k0 >= depth) useBorderColor |= K0BIT;
2021 if (k1 < 0 || k1 >= depth) useBorderColor |= K1BIT;
2025 if (useBorderColor & (I0BIT | J0BIT | K0BIT)) {
2026 get_border_color(tObj, img, t000);
2029 swImg->FetchTexel(swImg, i0, j0, k0, t000);
2031 if (useBorderColor & (I1BIT | J0BIT | K0BIT)) {
2032 get_border_color(tObj, img, t100);
2035 swImg->FetchTexel(swImg, i1, j0, k0, t100);
2037 if (useBorderColor & (I0BIT | J1BIT | K0BIT)) {
2038 get_border_color(tObj, img, t010);
2041 swImg->FetchTexel(swImg, i0, j1, k0, t010);
2043 if (useBorderColor & (I1BIT | J1BIT | K0BIT)) {
2044 get_border_color(tObj, img, t110);
2047 swImg->FetchTexel(swImg, i1, j1, k0, t110);
2050 if (useBorderColor & (I0BIT | J0BIT | K1BIT)) {
2051 get_border_color(tObj, img, t001);
2054 swImg->FetchTexel(swImg, i0, j0, k1, t001);
2056 if (useBorderColor & (I1BIT | J0BIT | K1BIT)) {
2057 get_border_color(tObj, img, t101);
2060 swImg->FetchTexel(swImg, i1, j0, k1, t101);
2062 if (useBorderColor & (I0BIT | J1BIT | K1BIT)) {
2063 get_border_color(tObj, img, t011);
2066 swImg->FetchTexel(swImg, i0, j1, k1, t011);
2068 if (useBorderColor & (I1BIT | J1BIT | K1BIT)) {
2069 get_border_color(tObj, img, t111);
2072 swImg->FetchTexel(swImg, i1, j1, k1, t111);
2075 /* trilinear interpolation of samples */
2076 lerp_rgba_3d(rgba, a, b, c, t000, t100, t010, t110, t001, t101, t011, t111);
2081 sample_3d_nearest_mipmap_nearest(struct gl_context *ctx,
2082 const struct gl_texture_object *tObj,
2083 GLuint n, const GLfloat texcoord[][4],
2084 const GLfloat lambda[], GLfloat rgba[][4] )
2087 for (i = 0; i < n; i++) {
2088 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2089 sample_3d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
2095 sample_3d_linear_mipmap_nearest(struct gl_context *ctx,
2096 const struct gl_texture_object *tObj,
2097 GLuint n, const GLfloat texcoord[][4],
2098 const GLfloat lambda[], GLfloat rgba[][4])
2101 ASSERT(lambda != NULL);
2102 for (i = 0; i < n; i++) {
2103 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2104 sample_3d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
2110 sample_3d_nearest_mipmap_linear(struct gl_context *ctx,
2111 const struct gl_texture_object *tObj,
2112 GLuint n, const GLfloat texcoord[][4],
2113 const GLfloat lambda[], GLfloat rgba[][4])
2116 ASSERT(lambda != NULL);
2117 for (i = 0; i < n; i++) {
2118 GLint level = linear_mipmap_level(tObj, lambda[i]);
2119 if (level >= tObj->_MaxLevel) {
2120 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2121 texcoord[i], rgba[i]);
2124 GLfloat t0[4], t1[4]; /* texels */
2125 const GLfloat f = FRAC(lambda[i]);
2126 sample_3d_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
2127 sample_3d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
2128 lerp_rgba(rgba[i], f, t0, t1);
2135 sample_3d_linear_mipmap_linear(struct gl_context *ctx,
2136 const struct gl_texture_object *tObj,
2137 GLuint n, const GLfloat texcoord[][4],
2138 const GLfloat lambda[], GLfloat rgba[][4])
2141 ASSERT(lambda != NULL);
2142 for (i = 0; i < n; i++) {
2143 GLint level = linear_mipmap_level(tObj, lambda[i]);
2144 if (level >= tObj->_MaxLevel) {
2145 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2146 texcoord[i], rgba[i]);
2149 GLfloat t0[4], t1[4]; /* texels */
2150 const GLfloat f = FRAC(lambda[i]);
2151 sample_3d_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
2152 sample_3d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
2153 lerp_rgba(rgba[i], f, t0, t1);
2159 /** Sample 3D texture, nearest filtering for both min/magnification */
2161 sample_nearest_3d(struct gl_context *ctx,
2162 const struct gl_texture_object *tObj, GLuint n,
2163 const GLfloat texcoords[][4], const GLfloat lambda[],
2167 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2169 for (i = 0; i < n; i++) {
2170 sample_3d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
2175 /** Sample 3D texture, linear filtering for both min/magnification */
2177 sample_linear_3d(struct gl_context *ctx,
2178 const struct gl_texture_object *tObj, GLuint n,
2179 const GLfloat texcoords[][4],
2180 const GLfloat lambda[], GLfloat rgba[][4])
2183 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2185 for (i = 0; i < n; i++) {
2186 sample_3d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
2191 /** Sample 3D texture, using lambda to choose between min/magnification */
2193 sample_lambda_3d(struct gl_context *ctx,
2194 const struct gl_texture_object *tObj, GLuint n,
2195 const GLfloat texcoords[][4], const GLfloat lambda[],
2198 GLuint minStart, minEnd; /* texels with minification */
2199 GLuint magStart, magEnd; /* texels with magnification */
2202 ASSERT(lambda != NULL);
2203 compute_min_mag_ranges(tObj, n, lambda,
2204 &minStart, &minEnd, &magStart, &magEnd);
2206 if (minStart < minEnd) {
2207 /* do the minified texels */
2208 GLuint m = minEnd - minStart;
2209 switch (tObj->Sampler.MinFilter) {
2211 for (i = minStart; i < minEnd; i++)
2212 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2213 texcoords[i], rgba[i]);
2216 for (i = minStart; i < minEnd; i++)
2217 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2218 texcoords[i], rgba[i]);
2220 case GL_NEAREST_MIPMAP_NEAREST:
2221 sample_3d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
2222 lambda + minStart, rgba + minStart);
2224 case GL_LINEAR_MIPMAP_NEAREST:
2225 sample_3d_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
2226 lambda + minStart, rgba + minStart);
2228 case GL_NEAREST_MIPMAP_LINEAR:
2229 sample_3d_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
2230 lambda + minStart, rgba + minStart);
2232 case GL_LINEAR_MIPMAP_LINEAR:
2233 sample_3d_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
2234 lambda + minStart, rgba + minStart);
2237 _mesa_problem(ctx, "Bad min filter in sample_3d_texture");
2242 if (magStart < magEnd) {
2243 /* do the magnified texels */
2244 switch (tObj->Sampler.MagFilter) {
2246 for (i = magStart; i < magEnd; i++)
2247 sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2248 texcoords[i], rgba[i]);
2251 for (i = magStart; i < magEnd; i++)
2252 sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2253 texcoords[i], rgba[i]);
2256 _mesa_problem(ctx, "Bad mag filter in sample_3d_texture");
2263 /**********************************************************************/
2264 /* Texture Cube Map Sampling Functions */
2265 /**********************************************************************/
2268 * Choose one of six sides of a texture cube map given the texture
2269 * coord (rx,ry,rz). Return pointer to corresponding array of texture
2272 static const struct gl_texture_image **
2273 choose_cube_face(const struct gl_texture_object *texObj,
2274 const GLfloat texcoord[4], GLfloat newCoord[4])
2278 direction target sc tc ma
2279 ---------- ------------------------------- --- --- ---
2280 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2281 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2282 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2283 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2284 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2285 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2287 const GLfloat rx = texcoord[0];
2288 const GLfloat ry = texcoord[1];
2289 const GLfloat rz = texcoord[2];
2290 const GLfloat arx = FABSF(rx), ary = FABSF(ry), arz = FABSF(rz);
2294 if (arx >= ary && arx >= arz) {
2308 else if (ary >= arx && ary >= arz) {
2338 const float ima = 1.0F / ma;
2339 newCoord[0] = ( sc * ima + 1.0F ) * 0.5F;
2340 newCoord[1] = ( tc * ima + 1.0F ) * 0.5F;
2343 return (const struct gl_texture_image **) texObj->Image[face];
2348 sample_nearest_cube(struct gl_context *ctx,
2349 const struct gl_texture_object *tObj, GLuint n,
2350 const GLfloat texcoords[][4], const GLfloat lambda[],
2355 for (i = 0; i < n; i++) {
2356 const struct gl_texture_image **images;
2357 GLfloat newCoord[4];
2358 images = choose_cube_face(tObj, texcoords[i], newCoord);
2359 sample_2d_nearest(ctx, tObj, images[tObj->BaseLevel],
2366 sample_linear_cube(struct gl_context *ctx,
2367 const struct gl_texture_object *tObj, GLuint n,
2368 const GLfloat texcoords[][4],
2369 const GLfloat lambda[], GLfloat rgba[][4])
2373 for (i = 0; i < n; i++) {
2374 const struct gl_texture_image **images;
2375 GLfloat newCoord[4];
2376 images = choose_cube_face(tObj, texcoords[i], newCoord);
2377 sample_2d_linear(ctx, tObj, images[tObj->BaseLevel],
2384 sample_cube_nearest_mipmap_nearest(struct gl_context *ctx,
2385 const struct gl_texture_object *tObj,
2386 GLuint n, const GLfloat texcoord[][4],
2387 const GLfloat lambda[], GLfloat rgba[][4])
2390 ASSERT(lambda != NULL);
2391 for (i = 0; i < n; i++) {
2392 const struct gl_texture_image **images;
2393 GLfloat newCoord[4];
2395 images = choose_cube_face(tObj, texcoord[i], newCoord);
2397 /* XXX we actually need to recompute lambda here based on the newCoords.
2398 * But we would need the texcoords of adjacent fragments to compute that
2399 * properly, and we don't have those here.
2400 * For now, do an approximation: subtracting 1 from the chosen mipmap
2401 * level seems to work in some test cases.
2402 * The same adjustment is done in the next few functions.
2404 level = nearest_mipmap_level(tObj, lambda[i]);
2405 level = MAX2(level - 1, 0);
2407 sample_2d_nearest(ctx, tObj, images[level], newCoord, rgba[i]);
2413 sample_cube_linear_mipmap_nearest(struct gl_context *ctx,
2414 const struct gl_texture_object *tObj,
2415 GLuint n, const GLfloat texcoord[][4],
2416 const GLfloat lambda[], GLfloat rgba[][4])
2419 ASSERT(lambda != NULL);
2420 for (i = 0; i < n; i++) {
2421 const struct gl_texture_image **images;
2422 GLfloat newCoord[4];
2423 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2424 level = MAX2(level - 1, 0); /* see comment above */
2425 images = choose_cube_face(tObj, texcoord[i], newCoord);
2426 sample_2d_linear(ctx, tObj, images[level], newCoord, rgba[i]);
2432 sample_cube_nearest_mipmap_linear(struct gl_context *ctx,
2433 const struct gl_texture_object *tObj,
2434 GLuint n, const GLfloat texcoord[][4],
2435 const GLfloat lambda[], GLfloat rgba[][4])
2438 ASSERT(lambda != NULL);
2439 for (i = 0; i < n; i++) {
2440 const struct gl_texture_image **images;
2441 GLfloat newCoord[4];
2442 GLint level = linear_mipmap_level(tObj, lambda[i]);
2443 level = MAX2(level - 1, 0); /* see comment above */
2444 images = choose_cube_face(tObj, texcoord[i], newCoord);
2445 if (level >= tObj->_MaxLevel) {
2446 sample_2d_nearest(ctx, tObj, images[tObj->_MaxLevel],
2450 GLfloat t0[4], t1[4]; /* texels */
2451 const GLfloat f = FRAC(lambda[i]);
2452 sample_2d_nearest(ctx, tObj, images[level ], newCoord, t0);
2453 sample_2d_nearest(ctx, tObj, images[level+1], newCoord, t1);
2454 lerp_rgba(rgba[i], f, t0, t1);
2461 sample_cube_linear_mipmap_linear(struct gl_context *ctx,
2462 const struct gl_texture_object *tObj,
2463 GLuint n, const GLfloat texcoord[][4],
2464 const GLfloat lambda[], GLfloat rgba[][4])
2467 ASSERT(lambda != NULL);
2468 for (i = 0; i < n; i++) {
2469 const struct gl_texture_image **images;
2470 GLfloat newCoord[4];
2471 GLint level = linear_mipmap_level(tObj, lambda[i]);
2472 level = MAX2(level - 1, 0); /* see comment above */
2473 images = choose_cube_face(tObj, texcoord[i], newCoord);
2474 if (level >= tObj->_MaxLevel) {
2475 sample_2d_linear(ctx, tObj, images[tObj->_MaxLevel],
2479 GLfloat t0[4], t1[4];
2480 const GLfloat f = FRAC(lambda[i]);
2481 sample_2d_linear(ctx, tObj, images[level ], newCoord, t0);
2482 sample_2d_linear(ctx, tObj, images[level+1], newCoord, t1);
2483 lerp_rgba(rgba[i], f, t0, t1);
2489 /** Sample cube texture, using lambda to choose between min/magnification */
2491 sample_lambda_cube(struct gl_context *ctx,
2492 const struct gl_texture_object *tObj, GLuint n,
2493 const GLfloat texcoords[][4], const GLfloat lambda[],
2496 GLuint minStart, minEnd; /* texels with minification */
2497 GLuint magStart, magEnd; /* texels with magnification */
2499 ASSERT(lambda != NULL);
2500 compute_min_mag_ranges(tObj, n, lambda,
2501 &minStart, &minEnd, &magStart, &magEnd);
2503 if (minStart < minEnd) {
2504 /* do the minified texels */
2505 const GLuint m = minEnd - minStart;
2506 switch (tObj->Sampler.MinFilter) {
2508 sample_nearest_cube(ctx, tObj, m, texcoords + minStart,
2509 lambda + minStart, rgba + minStart);
2512 sample_linear_cube(ctx, tObj, m, texcoords + minStart,
2513 lambda + minStart, rgba + minStart);
2515 case GL_NEAREST_MIPMAP_NEAREST:
2516 sample_cube_nearest_mipmap_nearest(ctx, tObj, m,
2517 texcoords + minStart,
2518 lambda + minStart, rgba + minStart);
2520 case GL_LINEAR_MIPMAP_NEAREST:
2521 sample_cube_linear_mipmap_nearest(ctx, tObj, m,
2522 texcoords + minStart,
2523 lambda + minStart, rgba + minStart);
2525 case GL_NEAREST_MIPMAP_LINEAR:
2526 sample_cube_nearest_mipmap_linear(ctx, tObj, m,
2527 texcoords + minStart,
2528 lambda + minStart, rgba + minStart);
2530 case GL_LINEAR_MIPMAP_LINEAR:
2531 sample_cube_linear_mipmap_linear(ctx, tObj, m,
2532 texcoords + minStart,
2533 lambda + minStart, rgba + minStart);
2536 _mesa_problem(ctx, "Bad min filter in sample_lambda_cube");
2541 if (magStart < magEnd) {
2542 /* do the magnified texels */
2543 const GLuint m = magEnd - magStart;
2544 switch (tObj->Sampler.MagFilter) {
2546 sample_nearest_cube(ctx, tObj, m, texcoords + magStart,
2547 lambda + magStart, rgba + magStart);
2550 sample_linear_cube(ctx, tObj, m, texcoords + magStart,
2551 lambda + magStart, rgba + magStart);
2554 _mesa_problem(ctx, "Bad mag filter in sample_lambda_cube");
2561 /**********************************************************************/
2562 /* Texture Rectangle Sampling Functions */
2563 /**********************************************************************/
2567 sample_nearest_rect(struct gl_context *ctx,
2568 const struct gl_texture_object *tObj, GLuint n,
2569 const GLfloat texcoords[][4], const GLfloat lambda[],
2572 const struct gl_texture_image *img = tObj->Image[0][0];
2573 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
2574 const GLint width = img->Width;
2575 const GLint height = img->Height;
2581 ASSERT(tObj->Sampler.WrapS == GL_CLAMP ||
2582 tObj->Sampler.WrapS == GL_CLAMP_TO_EDGE ||
2583 tObj->Sampler.WrapS == GL_CLAMP_TO_BORDER);
2584 ASSERT(tObj->Sampler.WrapT == GL_CLAMP ||
2585 tObj->Sampler.WrapT == GL_CLAMP_TO_EDGE ||
2586 tObj->Sampler.WrapT == GL_CLAMP_TO_BORDER);
2588 for (i = 0; i < n; i++) {
2590 col = clamp_rect_coord_nearest(tObj->Sampler.WrapS, texcoords[i][0], width);
2591 row = clamp_rect_coord_nearest(tObj->Sampler.WrapT, texcoords[i][1], height);
2592 if (col < 0 || col >= width || row < 0 || row >= height)
2593 get_border_color(tObj, img, rgba[i]);
2595 swImg->FetchTexel(swImg, col, row, 0, rgba[i]);
2601 sample_linear_rect(struct gl_context *ctx,
2602 const struct gl_texture_object *tObj, GLuint n,
2603 const GLfloat texcoords[][4],
2604 const GLfloat lambda[], GLfloat rgba[][4])
2606 const struct gl_texture_image *img = tObj->Image[0][0];
2607 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
2608 const GLint width = img->Width;
2609 const GLint height = img->Height;
2615 ASSERT(tObj->Sampler.WrapS == GL_CLAMP ||
2616 tObj->Sampler.WrapS == GL_CLAMP_TO_EDGE ||
2617 tObj->Sampler.WrapS == GL_CLAMP_TO_BORDER);
2618 ASSERT(tObj->Sampler.WrapT == GL_CLAMP ||
2619 tObj->Sampler.WrapT == GL_CLAMP_TO_EDGE ||
2620 tObj->Sampler.WrapT == GL_CLAMP_TO_BORDER);
2622 for (i = 0; i < n; i++) {
2623 GLint i0, j0, i1, j1;
2624 GLfloat t00[4], t01[4], t10[4], t11[4];
2626 GLbitfield useBorderColor = 0x0;
2628 clamp_rect_coord_linear(tObj->Sampler.WrapS, texcoords[i][0], width,
2630 clamp_rect_coord_linear(tObj->Sampler.WrapT, texcoords[i][1], height,
2633 /* compute integer rows/columns */
2634 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2635 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2636 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
2637 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
2639 /* get four texel samples */
2640 if (useBorderColor & (I0BIT | J0BIT))
2641 get_border_color(tObj, img, t00);
2643 swImg->FetchTexel(swImg, i0, j0, 0, t00);
2645 if (useBorderColor & (I1BIT | J0BIT))
2646 get_border_color(tObj, img, t10);
2648 swImg->FetchTexel(swImg, i1, j0, 0, t10);
2650 if (useBorderColor & (I0BIT | J1BIT))
2651 get_border_color(tObj, img, t01);
2653 swImg->FetchTexel(swImg, i0, j1, 0, t01);
2655 if (useBorderColor & (I1BIT | J1BIT))
2656 get_border_color(tObj, img, t11);
2658 swImg->FetchTexel(swImg, i1, j1, 0, t11);
2660 lerp_rgba_2d(rgba[i], a, b, t00, t10, t01, t11);
2665 /** Sample Rect texture, using lambda to choose between min/magnification */
2667 sample_lambda_rect(struct gl_context *ctx,
2668 const struct gl_texture_object *tObj, GLuint n,
2669 const GLfloat texcoords[][4], const GLfloat lambda[],
2672 GLuint minStart, minEnd, magStart, magEnd;
2674 /* We only need lambda to decide between minification and magnification.
2675 * There is no mipmapping with rectangular textures.
2677 compute_min_mag_ranges(tObj, n, lambda,
2678 &minStart, &minEnd, &magStart, &magEnd);
2680 if (minStart < minEnd) {
2681 if (tObj->Sampler.MinFilter == GL_NEAREST) {
2682 sample_nearest_rect(ctx, tObj, minEnd - minStart,
2683 texcoords + minStart, NULL, rgba + minStart);
2686 sample_linear_rect(ctx, tObj, minEnd - minStart,
2687 texcoords + minStart, NULL, rgba + minStart);
2690 if (magStart < magEnd) {
2691 if (tObj->Sampler.MagFilter == GL_NEAREST) {
2692 sample_nearest_rect(ctx, tObj, magEnd - magStart,
2693 texcoords + magStart, NULL, rgba + magStart);
2696 sample_linear_rect(ctx, tObj, magEnd - magStart,
2697 texcoords + magStart, NULL, rgba + magStart);
2703 /**********************************************************************/
2704 /* 2D Texture Array Sampling Functions */
2705 /**********************************************************************/
2708 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
2711 sample_2d_array_nearest(struct gl_context *ctx,
2712 const struct gl_texture_object *tObj,
2713 const struct gl_texture_image *img,
2714 const GLfloat texcoord[4],
2717 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
2718 const GLint width = img->Width2; /* without border, power of two */
2719 const GLint height = img->Height2; /* without border, power of two */
2720 const GLint depth = img->Depth;
2725 i = nearest_texel_location(tObj->Sampler.WrapS, img, width, texcoord[0]);
2726 j = nearest_texel_location(tObj->Sampler.WrapT, img, height, texcoord[1]);
2727 array = tex_array_slice(texcoord[2], depth);
2729 if (i < 0 || i >= (GLint) img->Width ||
2730 j < 0 || j >= (GLint) img->Height ||
2731 array < 0 || array >= (GLint) img->Depth) {
2732 /* Need this test for GL_CLAMP_TO_BORDER mode */
2733 get_border_color(tObj, img, rgba);
2736 swImg->FetchTexel(swImg, i, j, array, rgba);
2742 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
2745 sample_2d_array_linear(struct gl_context *ctx,
2746 const struct gl_texture_object *tObj,
2747 const struct gl_texture_image *img,
2748 const GLfloat texcoord[4],
2751 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
2752 const GLint width = img->Width2;
2753 const GLint height = img->Height2;
2754 const GLint depth = img->Depth;
2755 GLint i0, j0, i1, j1;
2757 GLbitfield useBorderColor = 0x0;
2759 GLfloat t00[4], t01[4], t10[4], t11[4];
2761 linear_texel_locations(tObj->Sampler.WrapS, img, width, texcoord[0], &i0, &i1, &a);
2762 linear_texel_locations(tObj->Sampler.WrapT, img, height, texcoord[1], &j0, &j1, &b);
2763 array = tex_array_slice(texcoord[2], depth);
2765 if (array < 0 || array >= depth) {
2766 COPY_4V(rgba, tObj->Sampler.BorderColor.f);
2776 /* check if sampling texture border color */
2777 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
2778 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
2779 if (j0 < 0 || j0 >= height) useBorderColor |= J0BIT;
2780 if (j1 < 0 || j1 >= height) useBorderColor |= J1BIT;
2784 if (useBorderColor & (I0BIT | J0BIT)) {
2785 get_border_color(tObj, img, t00);
2788 swImg->FetchTexel(swImg, i0, j0, array, t00);
2790 if (useBorderColor & (I1BIT | J0BIT)) {
2791 get_border_color(tObj, img, t10);
2794 swImg->FetchTexel(swImg, i1, j0, array, t10);
2796 if (useBorderColor & (I0BIT | J1BIT)) {
2797 get_border_color(tObj, img, t01);
2800 swImg->FetchTexel(swImg, i0, j1, array, t01);
2802 if (useBorderColor & (I1BIT | J1BIT)) {
2803 get_border_color(tObj, img, t11);
2806 swImg->FetchTexel(swImg, i1, j1, array, t11);
2809 /* trilinear interpolation of samples */
2810 lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
2816 sample_2d_array_nearest_mipmap_nearest(struct gl_context *ctx,
2817 const struct gl_texture_object *tObj,
2818 GLuint n, const GLfloat texcoord[][4],
2819 const GLfloat lambda[], GLfloat rgba[][4])
2822 for (i = 0; i < n; i++) {
2823 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2824 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i],
2831 sample_2d_array_linear_mipmap_nearest(struct gl_context *ctx,
2832 const struct gl_texture_object *tObj,
2833 GLuint n, const GLfloat texcoord[][4],
2834 const GLfloat lambda[], GLfloat rgba[][4])
2837 ASSERT(lambda != NULL);
2838 for (i = 0; i < n; i++) {
2839 GLint level = nearest_mipmap_level(tObj, lambda[i]);
2840 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level],
2841 texcoord[i], rgba[i]);
2847 sample_2d_array_nearest_mipmap_linear(struct gl_context *ctx,
2848 const struct gl_texture_object *tObj,
2849 GLuint n, const GLfloat texcoord[][4],
2850 const GLfloat lambda[], GLfloat rgba[][4])
2853 ASSERT(lambda != NULL);
2854 for (i = 0; i < n; i++) {
2855 GLint level = linear_mipmap_level(tObj, lambda[i]);
2856 if (level >= tObj->_MaxLevel) {
2857 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2858 texcoord[i], rgba[i]);
2861 GLfloat t0[4], t1[4]; /* texels */
2862 const GLfloat f = FRAC(lambda[i]);
2863 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level ],
2865 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level+1],
2867 lerp_rgba(rgba[i], f, t0, t1);
2874 sample_2d_array_linear_mipmap_linear(struct gl_context *ctx,
2875 const struct gl_texture_object *tObj,
2876 GLuint n, const GLfloat texcoord[][4],
2877 const GLfloat lambda[], GLfloat rgba[][4])
2880 ASSERT(lambda != NULL);
2881 for (i = 0; i < n; i++) {
2882 GLint level = linear_mipmap_level(tObj, lambda[i]);
2883 if (level >= tObj->_MaxLevel) {
2884 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
2885 texcoord[i], rgba[i]);
2888 GLfloat t0[4], t1[4]; /* texels */
2889 const GLfloat f = FRAC(lambda[i]);
2890 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level ],
2892 sample_2d_array_linear(ctx, tObj, tObj->Image[0][level+1],
2894 lerp_rgba(rgba[i], f, t0, t1);
2900 /** Sample 2D Array texture, nearest filtering for both min/magnification */
2902 sample_nearest_2d_array(struct gl_context *ctx,
2903 const struct gl_texture_object *tObj, GLuint n,
2904 const GLfloat texcoords[][4], const GLfloat lambda[],
2908 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2910 for (i = 0; i < n; i++) {
2911 sample_2d_array_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
2917 /** Sample 2D Array texture, linear filtering for both min/magnification */
2919 sample_linear_2d_array(struct gl_context *ctx,
2920 const struct gl_texture_object *tObj, GLuint n,
2921 const GLfloat texcoords[][4],
2922 const GLfloat lambda[], GLfloat rgba[][4])
2925 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
2927 for (i = 0; i < n; i++) {
2928 sample_2d_array_linear(ctx, tObj, image, texcoords[i], rgba[i]);
2933 /** Sample 2D Array texture, using lambda to choose between min/magnification */
2935 sample_lambda_2d_array(struct gl_context *ctx,
2936 const struct gl_texture_object *tObj, GLuint n,
2937 const GLfloat texcoords[][4], const GLfloat lambda[],
2940 GLuint minStart, minEnd; /* texels with minification */
2941 GLuint magStart, magEnd; /* texels with magnification */
2944 ASSERT(lambda != NULL);
2945 compute_min_mag_ranges(tObj, n, lambda,
2946 &minStart, &minEnd, &magStart, &magEnd);
2948 if (minStart < minEnd) {
2949 /* do the minified texels */
2950 GLuint m = minEnd - minStart;
2951 switch (tObj->Sampler.MinFilter) {
2953 for (i = minStart; i < minEnd; i++)
2954 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2955 texcoords[i], rgba[i]);
2958 for (i = minStart; i < minEnd; i++)
2959 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2960 texcoords[i], rgba[i]);
2962 case GL_NEAREST_MIPMAP_NEAREST:
2963 sample_2d_array_nearest_mipmap_nearest(ctx, tObj, m,
2964 texcoords + minStart,
2968 case GL_LINEAR_MIPMAP_NEAREST:
2969 sample_2d_array_linear_mipmap_nearest(ctx, tObj, m,
2970 texcoords + minStart,
2974 case GL_NEAREST_MIPMAP_LINEAR:
2975 sample_2d_array_nearest_mipmap_linear(ctx, tObj, m,
2976 texcoords + minStart,
2980 case GL_LINEAR_MIPMAP_LINEAR:
2981 sample_2d_array_linear_mipmap_linear(ctx, tObj, m,
2982 texcoords + minStart,
2987 _mesa_problem(ctx, "Bad min filter in sample_2d_array_texture");
2992 if (magStart < magEnd) {
2993 /* do the magnified texels */
2994 switch (tObj->Sampler.MagFilter) {
2996 for (i = magStart; i < magEnd; i++)
2997 sample_2d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
2998 texcoords[i], rgba[i]);
3001 for (i = magStart; i < magEnd; i++)
3002 sample_2d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
3003 texcoords[i], rgba[i]);
3006 _mesa_problem(ctx, "Bad mag filter in sample_2d_array_texture");
3015 /**********************************************************************/
3016 /* 1D Texture Array Sampling Functions */
3017 /**********************************************************************/
3020 * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
3023 sample_1d_array_nearest(struct gl_context *ctx,
3024 const struct gl_texture_object *tObj,
3025 const struct gl_texture_image *img,
3026 const GLfloat texcoord[4],
3029 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
3030 const GLint width = img->Width2; /* without border, power of two */
3031 const GLint height = img->Height;
3036 i = nearest_texel_location(tObj->Sampler.WrapS, img, width, texcoord[0]);
3037 array = tex_array_slice(texcoord[1], height);
3039 if (i < 0 || i >= (GLint) img->Width ||
3040 array < 0 || array >= (GLint) img->Height) {
3041 /* Need this test for GL_CLAMP_TO_BORDER mode */
3042 get_border_color(tObj, img, rgba);
3045 swImg->FetchTexel(swImg, i, array, 0, rgba);
3051 * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
3054 sample_1d_array_linear(struct gl_context *ctx,
3055 const struct gl_texture_object *tObj,
3056 const struct gl_texture_image *img,
3057 const GLfloat texcoord[4],
3060 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
3061 const GLint width = img->Width2;
3062 const GLint height = img->Height;
3065 GLbitfield useBorderColor = 0x0;
3067 GLfloat t0[4], t1[4];
3069 linear_texel_locations(tObj->Sampler.WrapS, img, width, texcoord[0], &i0, &i1, &a);
3070 array = tex_array_slice(texcoord[1], height);
3077 /* check if sampling texture border color */
3078 if (i0 < 0 || i0 >= width) useBorderColor |= I0BIT;
3079 if (i1 < 0 || i1 >= width) useBorderColor |= I1BIT;
3082 if (array < 0 || array >= height) useBorderColor |= K0BIT;
3085 if (useBorderColor & (I0BIT | K0BIT)) {
3086 get_border_color(tObj, img, t0);
3089 swImg->FetchTexel(swImg, i0, array, 0, t0);
3091 if (useBorderColor & (I1BIT | K0BIT)) {
3092 get_border_color(tObj, img, t1);
3095 swImg->FetchTexel(swImg, i1, array, 0, t1);
3098 /* bilinear interpolation of samples */
3099 lerp_rgba(rgba, a, t0, t1);
3104 sample_1d_array_nearest_mipmap_nearest(struct gl_context *ctx,
3105 const struct gl_texture_object *tObj,
3106 GLuint n, const GLfloat texcoord[][4],
3107 const GLfloat lambda[], GLfloat rgba[][4])
3110 for (i = 0; i < n; i++) {
3111 GLint level = nearest_mipmap_level(tObj, lambda[i]);
3112 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i],
3119 sample_1d_array_linear_mipmap_nearest(struct gl_context *ctx,
3120 const struct gl_texture_object *tObj,
3121 GLuint n, const GLfloat texcoord[][4],
3122 const GLfloat lambda[], GLfloat rgba[][4])
3125 ASSERT(lambda != NULL);
3126 for (i = 0; i < n; i++) {
3127 GLint level = nearest_mipmap_level(tObj, lambda[i]);
3128 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level],
3129 texcoord[i], rgba[i]);
3135 sample_1d_array_nearest_mipmap_linear(struct gl_context *ctx,
3136 const struct gl_texture_object *tObj,
3137 GLuint n, const GLfloat texcoord[][4],
3138 const GLfloat lambda[], GLfloat rgba[][4])
3141 ASSERT(lambda != NULL);
3142 for (i = 0; i < n; i++) {
3143 GLint level = linear_mipmap_level(tObj, lambda[i]);
3144 if (level >= tObj->_MaxLevel) {
3145 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
3146 texcoord[i], rgba[i]);
3149 GLfloat t0[4], t1[4]; /* texels */
3150 const GLfloat f = FRAC(lambda[i]);
3151 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
3152 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
3153 lerp_rgba(rgba[i], f, t0, t1);
3160 sample_1d_array_linear_mipmap_linear(struct gl_context *ctx,
3161 const struct gl_texture_object *tObj,
3162 GLuint n, const GLfloat texcoord[][4],
3163 const GLfloat lambda[], GLfloat rgba[][4])
3166 ASSERT(lambda != NULL);
3167 for (i = 0; i < n; i++) {
3168 GLint level = linear_mipmap_level(tObj, lambda[i]);
3169 if (level >= tObj->_MaxLevel) {
3170 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
3171 texcoord[i], rgba[i]);
3174 GLfloat t0[4], t1[4]; /* texels */
3175 const GLfloat f = FRAC(lambda[i]);
3176 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level ], texcoord[i], t0);
3177 sample_1d_array_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
3178 lerp_rgba(rgba[i], f, t0, t1);
3184 /** Sample 1D Array texture, nearest filtering for both min/magnification */
3186 sample_nearest_1d_array(struct gl_context *ctx,
3187 const struct gl_texture_object *tObj, GLuint n,
3188 const GLfloat texcoords[][4], const GLfloat lambda[],
3192 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
3194 for (i = 0; i < n; i++) {
3195 sample_1d_array_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
3200 /** Sample 1D Array texture, linear filtering for both min/magnification */
3202 sample_linear_1d_array(struct gl_context *ctx,
3203 const struct gl_texture_object *tObj, GLuint n,
3204 const GLfloat texcoords[][4],
3205 const GLfloat lambda[], GLfloat rgba[][4])
3208 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
3210 for (i = 0; i < n; i++) {
3211 sample_1d_array_linear(ctx, tObj, image, texcoords[i], rgba[i]);
3216 /** Sample 1D Array texture, using lambda to choose between min/magnification */
3218 sample_lambda_1d_array(struct gl_context *ctx,
3219 const struct gl_texture_object *tObj, GLuint n,
3220 const GLfloat texcoords[][4], const GLfloat lambda[],
3223 GLuint minStart, minEnd; /* texels with minification */
3224 GLuint magStart, magEnd; /* texels with magnification */
3227 ASSERT(lambda != NULL);
3228 compute_min_mag_ranges(tObj, n, lambda,
3229 &minStart, &minEnd, &magStart, &magEnd);
3231 if (minStart < minEnd) {
3232 /* do the minified texels */
3233 GLuint m = minEnd - minStart;
3234 switch (tObj->Sampler.MinFilter) {
3236 for (i = minStart; i < minEnd; i++)
3237 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
3238 texcoords[i], rgba[i]);
3241 for (i = minStart; i < minEnd; i++)
3242 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
3243 texcoords[i], rgba[i]);
3245 case GL_NEAREST_MIPMAP_NEAREST:
3246 sample_1d_array_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
3247 lambda + minStart, rgba + minStart);
3249 case GL_LINEAR_MIPMAP_NEAREST:
3250 sample_1d_array_linear_mipmap_nearest(ctx, tObj, m,
3251 texcoords + minStart,
3255 case GL_NEAREST_MIPMAP_LINEAR:
3256 sample_1d_array_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
3257 lambda + minStart, rgba + minStart);
3259 case GL_LINEAR_MIPMAP_LINEAR:
3260 sample_1d_array_linear_mipmap_linear(ctx, tObj, m,
3261 texcoords + minStart,
3266 _mesa_problem(ctx, "Bad min filter in sample_1d_array_texture");
3271 if (magStart < magEnd) {
3272 /* do the magnified texels */
3273 switch (tObj->Sampler.MagFilter) {
3275 for (i = magStart; i < magEnd; i++)
3276 sample_1d_array_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
3277 texcoords[i], rgba[i]);
3280 for (i = magStart; i < magEnd; i++)
3281 sample_1d_array_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
3282 texcoords[i], rgba[i]);
3285 _mesa_problem(ctx, "Bad mag filter in sample_1d_array_texture");
3293 * Compare texcoord against depth sample. Return 1.0 or the ambient value.
3295 static inline GLfloat
3296 shadow_compare(GLenum function, GLfloat coord, GLfloat depthSample,
3301 return (coord <= depthSample) ? 1.0F : ambient;
3303 return (coord >= depthSample) ? 1.0F : ambient;
3305 return (coord < depthSample) ? 1.0F : ambient;
3307 return (coord > depthSample) ? 1.0F : ambient;
3309 return (coord == depthSample) ? 1.0F : ambient;
3311 return (coord != depthSample) ? 1.0F : ambient;
3319 _mesa_problem(NULL, "Bad compare func in shadow_compare");
3326 * Compare texcoord against four depth samples.
3328 static inline GLfloat
3329 shadow_compare4(GLenum function, GLfloat coord,
3330 GLfloat depth00, GLfloat depth01,
3331 GLfloat depth10, GLfloat depth11,
3332 GLfloat ambient, GLfloat wi, GLfloat wj)
3334 const GLfloat d = (1.0F - (GLfloat) ambient) * 0.25F;
3335 GLfloat luminance = 1.0F;
3339 if (coord > depth00) luminance -= d;
3340 if (coord > depth01) luminance -= d;
3341 if (coord > depth10) luminance -= d;
3342 if (coord > depth11) luminance -= d;
3345 if (coord < depth00) luminance -= d;
3346 if (coord < depth01) luminance -= d;
3347 if (coord < depth10) luminance -= d;
3348 if (coord < depth11) luminance -= d;
3351 if (coord >= depth00) luminance -= d;
3352 if (coord >= depth01) luminance -= d;
3353 if (coord >= depth10) luminance -= d;
3354 if (coord >= depth11) luminance -= d;
3357 if (coord <= depth00) luminance -= d;
3358 if (coord <= depth01) luminance -= d;
3359 if (coord <= depth10) luminance -= d;
3360 if (coord <= depth11) luminance -= d;
3363 if (coord != depth00) luminance -= d;
3364 if (coord != depth01) luminance -= d;
3365 if (coord != depth10) luminance -= d;
3366 if (coord != depth11) luminance -= d;
3369 if (coord == depth00) luminance -= d;
3370 if (coord == depth01) luminance -= d;
3371 if (coord == depth10) luminance -= d;
3372 if (coord == depth11) luminance -= d;
3379 /* ordinary bilinear filtering */
3380 return lerp_2d(wi, wj, depth00, depth10, depth01, depth11);
3382 _mesa_problem(NULL, "Bad compare func in sample_compare4");
3389 * Choose the mipmap level to use when sampling from a depth texture.
3392 choose_depth_texture_level(const struct gl_texture_object *tObj, GLfloat lambda)
3396 if (tObj->Sampler.MinFilter == GL_NEAREST || tObj->Sampler.MinFilter == GL_LINEAR) {
3397 /* no mipmapping - use base level */
3398 level = tObj->BaseLevel;
3401 /* choose mipmap level */
3402 lambda = CLAMP(lambda, tObj->Sampler.MinLod, tObj->Sampler.MaxLod);
3403 level = (GLint) lambda;
3404 level = CLAMP(level, tObj->BaseLevel, tObj->_MaxLevel);
3412 * Sample a shadow/depth texture. This function is incomplete. It doesn't
3413 * check for minification vs. magnification, etc.
3416 sample_depth_texture( struct gl_context *ctx,
3417 const struct gl_texture_object *tObj, GLuint n,
3418 const GLfloat texcoords[][4], const GLfloat lambda[],
3419 GLfloat texel[][4] )
3421 const GLint level = choose_depth_texture_level(tObj, lambda[0]);
3422 const struct gl_texture_image *img = tObj->Image[0][level];
3423 const struct swrast_texture_image *swImg = swrast_texture_image_const(img);
3424 const GLint width = img->Width;
3425 const GLint height = img->Height;
3426 const GLint depth = img->Depth;
3427 const GLuint compare_coord = (tObj->Target == GL_TEXTURE_2D_ARRAY_EXT)
3433 ASSERT(img->_BaseFormat == GL_DEPTH_COMPONENT ||
3434 img->_BaseFormat == GL_DEPTH_STENCIL_EXT);
3436 ASSERT(tObj->Target == GL_TEXTURE_1D ||
3437 tObj->Target == GL_TEXTURE_2D ||
3438 tObj->Target == GL_TEXTURE_RECTANGLE_NV ||
3439 tObj->Target == GL_TEXTURE_1D_ARRAY_EXT ||
3440 tObj->Target == GL_TEXTURE_2D_ARRAY_EXT ||
3441 tObj->Target == GL_TEXTURE_CUBE_MAP);
3443 ambient = tObj->Sampler.CompareFailValue;
3445 /* XXXX if tObj->Sampler.MinFilter != tObj->Sampler.MagFilter, we're ignoring lambda */
3447 function = (tObj->Sampler.CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) ?
3448 tObj->Sampler.CompareFunc : GL_NONE;
3450 if (tObj->Sampler.MagFilter == GL_NEAREST) {
3452 for (i = 0; i < n; i++) {
3453 GLfloat depthSample, depthRef;
3454 GLint col, row, slice;
3456 nearest_texcoord(tObj, level, texcoords[i], &col, &row, &slice);
3458 if (col >= 0 && row >= 0 && col < width && row < height &&
3459 slice >= 0 && slice < depth) {
3460 swImg->FetchTexel(swImg, col, row, slice, &depthSample);
3463 depthSample = tObj->Sampler.BorderColor.f[0];
3466 depthRef = CLAMP(texcoords[i][compare_coord], 0.0F, 1.0F);
3468 result = shadow_compare(function, depthRef, depthSample, ambient);
3470 switch (tObj->Sampler.DepthMode) {
3472 ASSIGN_4V(texel[i], result, result, result, 1.0F);
3475 ASSIGN_4V(texel[i], result, result, result, result);
3478 ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
3481 ASSIGN_4V(texel[i], result, 0.0F, 0.0F, 1.0F);
3484 _mesa_problem(ctx, "Bad depth texture mode");
3491 ASSERT(tObj->Sampler.MagFilter == GL_LINEAR);
3492 for (i = 0; i < n; i++) {
3493 GLfloat depth00, depth01, depth10, depth11, depthRef;
3494 GLint i0, i1, j0, j1;
3497 GLuint useBorderTexel;
3499 linear_texcoord(tObj, level, texcoords[i], &i0, &i1, &j0, &j1, &slice,
3506 if (tObj->Target != GL_TEXTURE_1D_ARRAY_EXT) {
3512 if (i0 < 0 || i0 >= (GLint) width) useBorderTexel |= I0BIT;
3513 if (i1 < 0 || i1 >= (GLint) width) useBorderTexel |= I1BIT;
3514 if (j0 < 0 || j0 >= (GLint) height) useBorderTexel |= J0BIT;
3515 if (j1 < 0 || j1 >= (GLint) height) useBorderTexel |= J1BIT;
3518 if (slice < 0 || slice >= (GLint) depth) {
3519 depth00 = tObj->Sampler.BorderColor.f[0];
3520 depth01 = tObj->Sampler.BorderColor.f[0];
3521 depth10 = tObj->Sampler.BorderColor.f[0];
3522 depth11 = tObj->Sampler.BorderColor.f[0];
3525 /* get four depth samples from the texture */
3526 if (useBorderTexel & (I0BIT | J0BIT)) {
3527 depth00 = tObj->Sampler.BorderColor.f[0];
3530 swImg->FetchTexel(swImg, i0, j0, slice, &depth00);
3532 if (useBorderTexel & (I1BIT | J0BIT)) {
3533 depth10 = tObj->Sampler.BorderColor.f[0];
3536 swImg->FetchTexel(swImg, i1, j0, slice, &depth10);
3539 if (tObj->Target != GL_TEXTURE_1D_ARRAY_EXT) {
3540 if (useBorderTexel & (I0BIT | J1BIT)) {
3541 depth01 = tObj->Sampler.BorderColor.f[0];
3544 swImg->FetchTexel(swImg, i0, j1, slice, &depth01);
3546 if (useBorderTexel & (I1BIT | J1BIT)) {
3547 depth11 = tObj->Sampler.BorderColor.f[0];
3550 swImg->FetchTexel(swImg, i1, j1, slice, &depth11);
3559 depthRef = CLAMP(texcoords[i][compare_coord], 0.0F, 1.0F);
3561 result = shadow_compare4(function, depthRef,
3562 depth00, depth01, depth10, depth11,
3565 switch (tObj->Sampler.DepthMode) {
3567 ASSIGN_4V(texel[i], result, result, result, 1.0F);
3570 ASSIGN_4V(texel[i], result, result, result, result);
3573 ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
3576 _mesa_problem(ctx, "Bad depth texture mode");
3585 * We use this function when a texture object is in an "incomplete" state.
3586 * When a fragment program attempts to sample an incomplete texture we
3587 * return black (see issue 23 in GL_ARB_fragment_program spec).
3588 * Note: fragment programs don't observe the texture enable/disable flags.
3591 null_sample_func( struct gl_context *ctx,
3592 const struct gl_texture_object *tObj, GLuint n,
3593 const GLfloat texcoords[][4], const GLfloat lambda[],
3601 for (i = 0; i < n; i++) {
3605 rgba[i][ACOMP] = 1.0;
3611 * Choose the texture sampling function for the given texture object.
3614 _swrast_choose_texture_sample_func( struct gl_context *ctx,
3615 const struct gl_texture_object *t )
3617 if (!t || !t->_Complete) {
3618 return &null_sample_func;
3621 const GLboolean needLambda =
3622 (GLboolean) (t->Sampler.MinFilter != t->Sampler.MagFilter);
3623 const GLenum format = t->Image[0][t->BaseLevel]->_BaseFormat;
3625 switch (t->Target) {
3627 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3628 return &sample_depth_texture;
3630 else if (needLambda) {
3631 return &sample_lambda_1d;
3633 else if (t->Sampler.MinFilter == GL_LINEAR) {
3634 return &sample_linear_1d;
3637 ASSERT(t->Sampler.MinFilter == GL_NEAREST);
3638 return &sample_nearest_1d;
3641 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3642 return &sample_depth_texture;
3644 else if (needLambda) {
3645 /* Anisotropic filtering extension. Activated only if mipmaps are used */
3646 if (t->Sampler.MaxAnisotropy > 1.0 &&
3647 t->Sampler.MinFilter == GL_LINEAR_MIPMAP_LINEAR) {
3648 return &sample_lambda_2d_aniso;
3650 return &sample_lambda_2d;
3652 else if (t->Sampler.MinFilter == GL_LINEAR) {
3653 return &sample_linear_2d;
3656 /* check for a few optimized cases */
3657 const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
3658 const struct swrast_texture_image *swImg =
3659 swrast_texture_image_const(img);
3660 texture_sample_func func;
3662 ASSERT(t->Sampler.MinFilter == GL_NEAREST);
3663 func = &sample_nearest_2d;
3664 if (t->Sampler.WrapS == GL_REPEAT &&
3665 t->Sampler.WrapT == GL_REPEAT &&
3666 swImg->_IsPowerOfTwo &&
3668 if (img->TexFormat == MESA_FORMAT_RGB888)
3669 func = &opt_sample_rgb_2d;
3670 else if (img->TexFormat == MESA_FORMAT_RGBA8888)
3671 func = &opt_sample_rgba_2d;
3678 return &sample_lambda_3d;
3680 else if (t->Sampler.MinFilter == GL_LINEAR) {
3681 return &sample_linear_3d;
3684 ASSERT(t->Sampler.MinFilter == GL_NEAREST);
3685 return &sample_nearest_3d;
3687 case GL_TEXTURE_CUBE_MAP:
3688 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3689 return &sample_depth_texture;
3691 else if (needLambda) {
3692 return &sample_lambda_cube;
3694 else if (t->Sampler.MinFilter == GL_LINEAR) {
3695 return &sample_linear_cube;
3698 ASSERT(t->Sampler.MinFilter == GL_NEAREST);
3699 return &sample_nearest_cube;
3701 case GL_TEXTURE_RECTANGLE_NV:
3702 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3703 return &sample_depth_texture;
3705 else if (needLambda) {
3706 return &sample_lambda_rect;
3708 else if (t->Sampler.MinFilter == GL_LINEAR) {
3709 return &sample_linear_rect;
3712 ASSERT(t->Sampler.MinFilter == GL_NEAREST);
3713 return &sample_nearest_rect;
3715 case GL_TEXTURE_1D_ARRAY_EXT:
3716 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3717 return &sample_depth_texture;
3719 else if (needLambda) {
3720 return &sample_lambda_1d_array;
3722 else if (t->Sampler.MinFilter == GL_LINEAR) {
3723 return &sample_linear_1d_array;
3726 ASSERT(t->Sampler.MinFilter == GL_NEAREST);
3727 return &sample_nearest_1d_array;
3729 case GL_TEXTURE_2D_ARRAY_EXT:
3730 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
3731 return &sample_depth_texture;
3733 else if (needLambda) {
3734 return &sample_lambda_2d_array;
3736 else if (t->Sampler.MinFilter == GL_LINEAR) {
3737 return &sample_linear_2d_array;
3740 ASSERT(t->Sampler.MinFilter == GL_NEAREST);
3741 return &sample_nearest_2d_array;
3745 "invalid target in _swrast_choose_texture_sample_func");
3746 return &null_sample_func;