1e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
2e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Mesa 3-D graphics library
38c7135ee14fb6f4d8b6e64d570daee3512c99438Brian Paul * Version:  7.3
422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
5dd34fe8679fa200e55cfaf8e80bbecdecea084e3Brian * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
7e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
8e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * copy of this software and associated documentation files (the "Software"),
9e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * to deal in the Software without restriction, including without limitation
10e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * and/or sell copies of the Software, and to permit persons to whom the
12e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Software is furnished to do so, subject to the following conditions:
1322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
14e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * The above copyright notice and this permission notice shall be included
15e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * in all copies or substantial portions of the Software.
1622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
17e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
24e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
25e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
26e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
27709892459922a32096fe9dd8261d0d92337bb02fKeith Whitwell * When the device driver doesn't implement triangle rasterization it
28709892459922a32096fe9dd8261d0d92337bb02fKeith Whitwell * can hook in _swrast_Triangle, which eventually calls one of these
2938f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes * functions to draw triangles.
30e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
31e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
32bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paul#include "main/glheader.h"
33bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paul#include "main/context.h"
34bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paul#include "main/colormac.h"
35bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paul#include "main/imports.h"
36bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paul#include "main/macros.h"
37f84573d039e3cc2bf11d8c15d59bd3255fd05b27Vinson Lee#include "main/mtypes.h"
38decc6e2a32ef49e673c081f30e19b8970155d887Brian Paul#include "main/state.h"
39cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen#include "main/samplerobj.h"
40ec2b92f98c2e7f161521b447cc1d9a36bce3707cBrian Paul#include "program/prog_instruction.h"
41e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
42e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_aatriangle.h"
43cd03ed4f54444d96e4e47cdb118a3dfd94d92bb0Keith Whitwell#include "s_context.h"
44cd03ed4f54444d96e4e47cdb118a3dfd94d92bb0Keith Whitwell#include "s_feedback.h"
45e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_span.h"
46cd03ed4f54444d96e4e47cdb118a3dfd94d92bb0Keith Whitwell#include "s_triangle.h"
479bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
489bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
498f4d66c5f893b49eb3973aa3b31a856314c045c7Brian Paul/**
508f4d66c5f893b49eb3973aa3b31a856314c045c7Brian Paul * Test if a triangle should be culled.  Used for feedback and selection mode.
518f4d66c5f893b49eb3973aa3b31a856314c045c7Brian Paul * \return GL_TRUE if the triangle is to be culled, GL_FALSE otherwise.
5277df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul */
53230ebaff2aecdd9f2bf667889d54bfc3dd032bbeBrian PaulGLboolean
54f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_swrast_culltriangle( struct gl_context *ctx,
55230ebaff2aecdd9f2bf667889d54bfc3dd032bbeBrian Paul                      const SWvertex *v0,
56230ebaff2aecdd9f2bf667889d54bfc3dd032bbeBrian Paul                      const SWvertex *v1,
57230ebaff2aecdd9f2bf667889d54bfc3dd032bbeBrian Paul                      const SWvertex *v2 )
58e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
598f4d66c5f893b49eb3973aa3b31a856314c045c7Brian Paul   SWcontext *swrast = SWRAST_CONTEXT(ctx);
609e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   GLfloat ex = v1->attrib[FRAG_ATTRIB_WPOS][0] - v0->attrib[FRAG_ATTRIB_WPOS][0];
619e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   GLfloat ey = v1->attrib[FRAG_ATTRIB_WPOS][1] - v0->attrib[FRAG_ATTRIB_WPOS][1];
629e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   GLfloat fx = v2->attrib[FRAG_ATTRIB_WPOS][0] - v0->attrib[FRAG_ATTRIB_WPOS][0];
639e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   GLfloat fy = v2->attrib[FRAG_ATTRIB_WPOS][1] - v0->attrib[FRAG_ATTRIB_WPOS][1];
64e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   GLfloat c = ex*fy-ey*fx;
65e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
66a1d3855fb0c3969a32825d0b899076917d5ccce6Brian Paul   if (c * swrast->_BackfaceSign * swrast->_BackfaceCullSign <= 0.0F)
678f4d66c5f893b49eb3973aa3b31a856314c045c7Brian Paul      return GL_FALSE;
6822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes
698f4d66c5f893b49eb3973aa3b31a856314c045c7Brian Paul   return GL_TRUE;
70e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
71e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
72e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
7395e02a210ed339ad20b0c16284dcdcf9af2dc755Brian Paul
74e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
75e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Render a flat-shaded RGBA triangle.
76e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
77cdf2da368d180205df3573697b51b8764048ad6eBrian Paul#define NAME flat_rgba_triangle
78e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_Z 1
79f1e236987829393c81dc86ea19cb49eefe190317Brian Paul#define SETUP_CODE				\
8036a0a3252e1e20df69b53f70ba93bc74c4a4bf0eBrian Paul   ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
817956292a765910077f50352d7cd0174e1e66d26cBrian Paul   ASSERT(ctx->Light.ShadeModel==GL_FLAT);	\
8277df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.interpMask |= SPAN_RGBA;		\
8377df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.red = ChanToFixed(v2->color[0]);	\
8477df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.green = ChanToFixed(v2->color[1]);	\
8577df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.blue = ChanToFixed(v2->color[2]);	\
8677df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.alpha = ChanToFixed(v2->color[3]);	\
8777df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.redStep = 0;				\
8877df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.greenStep = 0;				\
8977df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.blueStep = 0;				\
9077df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.alphaStep = 0;
9145bc887da226403f2c41077e40ca38b6f60f1359Brian Paul#define RENDER_SPAN( span )  _swrast_write_rgba_span(ctx, &span);
92e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_tritemp.h"
93e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
94e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
95e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
96e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
97e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Render a smooth-shaded RGBA triangle.
98e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
99cdf2da368d180205df3573697b51b8764048ad6eBrian Paul#define NAME smooth_rgba_triangle
100e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_Z 1
101e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_RGB 1
102e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_ALPHA 1
103733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul#define SETUP_CODE				\
104733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul   {						\
105733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul      /* texturing must be off */		\
10636a0a3252e1e20df69b53f70ba93bc74c4a4bf0eBrian Paul      ASSERT(ctx->Texture._EnabledCoordUnits == 0);	\
107733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul      ASSERT(ctx->Light.ShadeModel==GL_SMOOTH);	\
108733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul   }
10945bc887da226403f2c41077e40ca38b6f60f1359Brian Paul#define RENDER_SPAN( span )  _swrast_write_rgba_span(ctx, &span);
110e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_tritemp.h"
111e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
112e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
113e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
114e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
115e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Render an RGB, GL_DECAL, textured triangle.
116e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Interpolate S,T only w/out mipmapping or perspective correction.
117e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *
1184003bde6fffc3e5b9e1a115ba952b988dffb099aBrian * No fog.  No depth testing.
119e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
120cdf2da368d180205df3573697b51b8764048ad6eBrian Paul#define NAME simple_textured_triangle
121e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_INT_TEX 1
122e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define S_SCALE twidth
123e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define T_SCALE theight
1249bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
125e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define SETUP_CODE							\
1262a077500a84819d1e6ac62e84ded130aa655c5e9Brian   struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];	\
1270103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const struct gl_texture_object *obj = 				\
1289818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul      ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];		\
1290103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const struct gl_texture_image *texImg =				\
1300103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul      obj->Image[0][obj->BaseLevel];					\
1316e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul   const struct swrast_texture_image *swImg =				\
1326e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul      swrast_texture_image_const(texImg);				\
1330103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLfloat twidth = (GLfloat) texImg->Width;			\
1340103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLfloat theight = (GLfloat) texImg->Height;			\
1350103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLint twidth_log2 = texImg->WidthLog2;				\
136bd3c10c0f0c60ab3421c2da2eab814edc2296cb0Brian Paul   const GLubyte *texture = (const GLubyte *) swImg->Map;		\
1370103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLint smask = texImg->Width - 1;				\
1380103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLint tmask = texImg->Height - 1;				\
1390103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888);			\
1402a077500a84819d1e6ac62e84ded130aa655c5e9Brian   if (!rb || !texture) {						\
1418e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      return;								\
142e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
143e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
144920023240c2fc42675f318732b43bdc6f339113cBrian Paul#define RENDER_SPAN( span )						\
1459bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   GLuint i;								\
146fb1650b202822adf37baa7e51878311bb83dc976Brian Paul   GLubyte (*rgba)[4] = swrast->SpanArrays->rgba8;			\
14777df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.intTex[0] -= FIXED_HALF; /* off-by-one error? */		\
14877df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.intTex[1] -= FIXED_HALF;					\
14977df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   for (i = 0; i < span.end; i++) {					\
15077df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul      GLint s = FixedToInt(span.intTex[0]) & smask;			\
15177df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul      GLint t = FixedToInt(span.intTex[1]) & tmask;			\
1529bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      GLint pos = (t << twidth_log2) + s;				\
1539bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      pos = pos + pos + pos;  /* multiply by 3 */			\
1546e7bc795784f13068e00c478b94740c8b27789e6Brian Paul      rgba[i][RCOMP] = texture[pos+2];					\
1556e7bc795784f13068e00c478b94740c8b27789e6Brian Paul      rgba[i][GCOMP] = texture[pos+1];					\
1566e7bc795784f13068e00c478b94740c8b27789e6Brian Paul      rgba[i][BCOMP] = texture[pos+0];					\
1576e7bc795784f13068e00c478b94740c8b27789e6Brian Paul      rgba[i][ACOMP] = 0xff;                                            \
15877df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul      span.intTex[0] += span.intTexStep[0];				\
15977df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul      span.intTex[1] += span.intTexStep[1];				\
1609bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   }									\
161f9874feef4d8952df5054bd8e8f4e0deda4ef44fBrian Paul   _swrast_put_row(ctx, rb, GL_UNSIGNED_BYTE, span.end,                 \
162f9874feef4d8952df5054bd8e8f4e0deda4ef44fBrian Paul                   span.x, span.y, rgba, NULL);
163e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
164e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_tritemp.h"
165cdf2da368d180205df3573697b51b8764048ad6eBrian Paul
166e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
167e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
168e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
169e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Render an RGB, GL_DECAL, textured triangle.
170e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
171e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * perspective correction.
172e6e0ba781b0a6a1b8747cae49ca622a6a61b1bf8Brian Paul * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
17322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
174e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * No fog.
175e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
176cdf2da368d180205df3573697b51b8764048ad6eBrian Paul#define NAME simple_z_textured_triangle
177e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_Z 1
178e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
179e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_INT_TEX 1
180e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define S_SCALE twidth
181e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define T_SCALE theight
1829bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
183e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define SETUP_CODE							\
1842a077500a84819d1e6ac62e84ded130aa655c5e9Brian   struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];	\
1850103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const struct gl_texture_object *obj = 				\
1869818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul      ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];		\
1870103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const struct gl_texture_image *texImg = 				\
1880103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul       obj->Image[0][obj->BaseLevel]; 					\
1896e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul   const struct swrast_texture_image *swImg =				\
1906e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul      swrast_texture_image_const(texImg);				\
1910103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLfloat twidth = (GLfloat) texImg->Width;			\
1920103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLfloat theight = (GLfloat) texImg->Height;			\
1930103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLint twidth_log2 = texImg->WidthLog2;				\
194bd3c10c0f0c60ab3421c2da2eab814edc2296cb0Brian Paul   const GLubyte *texture = (const GLubyte *) swImg->Map;		\
1950103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLint smask = texImg->Width - 1;				\
1960103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLint tmask = texImg->Height - 1;				\
1970103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888);			\
1982a077500a84819d1e6ac62e84ded130aa655c5e9Brian   if (!rb || !texture) {						\
1998e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      return;								\
200e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
201e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
202920023240c2fc42675f318732b43bdc6f339113cBrian Paul#define RENDER_SPAN( span )						\
2039bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   GLuint i;				    				\
204fb1650b202822adf37baa7e51878311bb83dc976Brian Paul   GLubyte (*rgba)[4] = swrast->SpanArrays->rgba8;			\
20530ea34a8d9ad22be626c24aa660a80f2e08c1e24Brian Paul   GLubyte *mask = swrast->SpanArrays->mask;                            \
20677df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.intTex[0] -= FIXED_HALF; /* off-by-one error? */		\
20777df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.intTex[1] -= FIXED_HALF;					\
20877df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   for (i = 0; i < span.end; i++) {					\
2093e37bafab0a339021354b9c78f983d05d433d735Brian Paul      const GLuint z = FixedToDepth(span.z);				\
2109bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      if (z < zRow[i]) {						\
21177df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul         GLint s = FixedToInt(span.intTex[0]) & smask;			\
21277df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul         GLint t = FixedToInt(span.intTex[1]) & tmask;			\
2139bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         GLint pos = (t << twidth_log2) + s;				\
2149bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         pos = pos + pos + pos;  /* multiply by 3 */			\
2156e7bc795784f13068e00c478b94740c8b27789e6Brian Paul         rgba[i][RCOMP] = texture[pos+2];				\
2166e7bc795784f13068e00c478b94740c8b27789e6Brian Paul         rgba[i][GCOMP] = texture[pos+1];				\
2176e7bc795784f13068e00c478b94740c8b27789e6Brian Paul         rgba[i][BCOMP] = texture[pos+0];				\
2186e7bc795784f13068e00c478b94740c8b27789e6Brian Paul         rgba[i][ACOMP] = 0xff;          				\
2199bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         zRow[i] = z;							\
22030ea34a8d9ad22be626c24aa660a80f2e08c1e24Brian Paul         mask[i] = 1;							\
2219bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      }									\
2229bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      else {								\
22330ea34a8d9ad22be626c24aa660a80f2e08c1e24Brian Paul         mask[i] = 0;							\
2249bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      }									\
22577df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul      span.intTex[0] += span.intTexStep[0];				\
22677df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul      span.intTex[1] += span.intTexStep[1];				\
22777df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul      span.z += span.zStep;						\
2289bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   }									\
229f9874feef4d8952df5054bd8e8f4e0deda4ef44fBrian Paul   _swrast_put_row(ctx, rb, GL_UNSIGNED_BYTE,                           \
23030ea34a8d9ad22be626c24aa660a80f2e08c1e24Brian Paul                   span.end, span.x, span.y, rgba, mask);
231e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
232e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_tritemp.h"
233cdf2da368d180205df3573697b51b8764048ad6eBrian Paul
234e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
235f431a3fb4dc1bf860203d79e54657e3a62bc50dfBrian Paul#if CHAN_TYPE != GL_FLOAT
236e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
2379bf68ad963ba92b5d1e725f965979042495a5313Brian Paulstruct affine_info
238e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
2399bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   GLenum filter;
2409bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   GLenum format;
2419bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   GLenum envmode;
2429bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   GLint smask, tmask;
2439bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   GLint twidth_log2;
2449bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   const GLchan *texture;
245fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul   GLfixed er, eg, eb, ea;
2469bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   GLint tbytesline, tsize;
2479bf68ad963ba92b5d1e725f965979042495a5313Brian Paul};
248cd03ed4f54444d96e4e47cdb118a3dfd94d92bb0Keith Whitwell
2497152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul
2509520f483b8f1e45fa474674b415554988de5d8d3Brian Paulstatic inline GLint
251f138b977d09327445a8e9c8126c493c4487c1630Brian Paulilerp(GLint t, GLint a, GLint b)
252f138b977d09327445a8e9c8126c493c4487c1630Brian Paul{
253f138b977d09327445a8e9c8126c493c4487c1630Brian Paul   return a + ((t * (b - a)) >> FIXED_SHIFT);
254f138b977d09327445a8e9c8126c493c4487c1630Brian Paul}
255f138b977d09327445a8e9c8126c493c4487c1630Brian Paul
2569520f483b8f1e45fa474674b415554988de5d8d3Brian Paulstatic inline GLint
257f138b977d09327445a8e9c8126c493c4487c1630Brian Paulilerp_2d(GLint ia, GLint ib, GLint v00, GLint v10, GLint v01, GLint v11)
258f138b977d09327445a8e9c8126c493c4487c1630Brian Paul{
259f138b977d09327445a8e9c8126c493c4487c1630Brian Paul   const GLint temp0 = ilerp(ia, v00, v10);
260f138b977d09327445a8e9c8126c493c4487c1630Brian Paul   const GLint temp1 = ilerp(ia, v01, v11);
261f138b977d09327445a8e9c8126c493c4487c1630Brian Paul   return ilerp(ib, temp0, temp1);
262f138b977d09327445a8e9c8126c493c4487c1630Brian Paul}
263f138b977d09327445a8e9c8126c493c4487c1630Brian Paul
264f138b977d09327445a8e9c8126c493c4487c1630Brian Paul
2657152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul/* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
2667152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
2677152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul * texture env modes.
2687152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul */
2699520f483b8f1e45fa474674b415554988de5d8d3Brian Paulstatic inline void
270f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergaffine_span(struct gl_context *ctx, SWspan *span,
2719bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            struct affine_info *info)
2729bf68ad963ba92b5d1e725f965979042495a5313Brian Paul{
2737152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   GLchan sample[4];  /* the filtered texture sample */
27407e50058a5699fc9279de6bf5d1449d52ccdc476Brian Paul   const GLuint texEnableSave = ctx->Texture._EnabledCoordUnits;
2758c7135ee14fb6f4d8b6e64d570daee3512c99438Brian Paul
276fae7b778b81b686ef419f971064b5fe12fb4ead3Brian Paul   /* Instead of defining a function for each mode, a test is done
277fae7b778b81b686ef419f971064b5fe12fb4ead3Brian Paul    * between the outer and inner loops. This is to reduce code size
278fae7b778b81b686ef419f971064b5fe12fb4ead3Brian Paul    * and complexity. Observe that an optimizing compiler kills
279fae7b778b81b686ef419f971064b5fe12fb4ead3Brian Paul    * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
280fae7b778b81b686ef419f971064b5fe12fb4ead3Brian Paul    */
281e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
2828c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul#define NEAREST_RGB		\
2838c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[RCOMP] = tex00[2];	\
2848c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[GCOMP] = tex00[1];	\
2858c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[BCOMP] = tex00[0];	\
2868c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[ACOMP] = CHAN_MAX;
287e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
2889bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define LINEAR_RGB							\
2898c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[RCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
290f138b977d09327445a8e9c8126c493c4487c1630Brian Paul   sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
2918c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[BCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
292f138b977d09327445a8e9c8126c493c4487c1630Brian Paul   sample[ACOMP] = CHAN_MAX;
293bbf6a41d2fd4a3d54e6b5dea50bba24768c01eceBrian Paul
2948c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul#define NEAREST_RGBA  \
2958c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[RCOMP] = tex00[3];	\
2968c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[GCOMP] = tex00[2];	\
2978c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[BCOMP] = tex00[1];	\
2988c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[ACOMP] = tex00[0];
299e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
3009bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define LINEAR_RGBA							\
3018c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[RCOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3]);\
3028c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[GCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
3038c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[BCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
3048c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   sample[ACOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0])
305bbf6a41d2fd4a3d54e6b5dea50bba24768c01eceBrian Paul
306fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul#define MODULATE							  \
3077152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[RCOMP] = span->red   * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
3087152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
3097152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[BCOMP] = span->blue  * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
3107152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
3119bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
3129bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define DECAL								\
3137152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red +		\
3147152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul               ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT))	\
315bbf6a41d2fd4a3d54e6b5dea50bba24768c01eceBrian Paul               >> (FIXED_SHIFT + 8);					\
3167152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green +		\
3177152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul               ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT))	\
318bbf6a41d2fd4a3d54e6b5dea50bba24768c01eceBrian Paul               >> (FIXED_SHIFT + 8);					\
3197152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue +		\
3207152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul               ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT))	\
321bbf6a41d2fd4a3d54e6b5dea50bba24768c01eceBrian Paul               >> (FIXED_SHIFT + 8);					\
322bbf6a41d2fd4a3d54e6b5dea50bba24768c01eceBrian Paul   dest[ACOMP] = FixedToInt(span->alpha)
3239bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
3249bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define BLEND								\
3257152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red		\
3267152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul               + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8);	\
3277152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green		\
3287152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul               + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8);	\
3297152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue		\
3307152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul               + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8);	\
3317152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
332bbf6a41d2fd4a3d54e6b5dea50bba24768c01eceBrian Paul
3337152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul#define REPLACE  COPY_CHAN4(dest, sample)
334e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
3359bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define ADD								\
3367152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   {									\
3377152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul      GLint rSum = FixedToInt(span->red)   + (GLint) sample[RCOMP];	\
3387152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul      GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP];	\
3397152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul      GLint bSum = FixedToInt(span->blue)  + (GLint) sample[BCOMP];	\
3407152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul      dest[RCOMP] = MIN2(rSum, CHAN_MAX);				\
3417152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul      dest[GCOMP] = MIN2(gSum, CHAN_MAX);				\
3427152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul      dest[BCOMP] = MIN2(bSum, CHAN_MAX);				\
3437152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul      dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
3447152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul  }
345e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
346e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/* shortcuts */
347e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
34835e5e89f5614ebb032479b02dec888563f829b30Brian Paul#define NEAREST_RGB_REPLACE		\
34935e5e89f5614ebb032479b02dec888563f829b30Brian Paul   NEAREST_RGB;				\
35035e5e89f5614ebb032479b02dec888563f829b30Brian Paul   dest[0] = sample[0];			\
35135e5e89f5614ebb032479b02dec888563f829b30Brian Paul   dest[1] = sample[1];			\
35235e5e89f5614ebb032479b02dec888563f829b30Brian Paul   dest[2] = sample[2];			\
35335e5e89f5614ebb032479b02dec888563f829b30Brian Paul   dest[3] = FixedToInt(span->alpha);
354e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
3558c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul#define NEAREST_RGBA_REPLACE  \
3568c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   dest[RCOMP] = tex00[3]; \
3578c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   dest[GCOMP] = tex00[2]; \
3588c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   dest[BCOMP] = tex00[1]; \
3598c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   dest[ACOMP] = tex00[0]
360e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
361f138b977d09327445a8e9c8126c493c4487c1630Brian Paul#define SPAN_NEAREST(DO_TEX, COMPS)					\
36257d6e1aebf1f850686a2c8d3a246fb388ec23979Brian Paul	for (i = 0; i < span->end; i++) {				\
36333170eeb185f5fe766374a749464497cdfab6931Brian Paul           /* Isn't it necessary to use FixedFloor below?? */		\
3649bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           GLint s = FixedToInt(span->intTex[0]) & info->smask;		\
3659bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           GLint t = FixedToInt(span->intTex[1]) & info->tmask;		\
3669bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           GLint pos = (t << info->twidth_log2) + s;			\
367f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLchan *tex00 = info->texture + COMPS * pos;		\
3689bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           DO_TEX;							\
3699bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           span->red += span->redStep;					\
3709bf68ad963ba92b5d1e725f965979042495a5313Brian Paul	   span->green += span->greenStep;				\
3719bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           span->blue += span->blueStep;				\
3729bf68ad963ba92b5d1e725f965979042495a5313Brian Paul	   span->alpha += span->alphaStep;				\
3739bf68ad963ba92b5d1e725f965979042495a5313Brian Paul	   span->intTex[0] += span->intTexStep[0];			\
3749bf68ad963ba92b5d1e725f965979042495a5313Brian Paul	   span->intTex[1] += span->intTexStep[1];			\
3759bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           dest += 4;							\
376e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	}
377e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
378f138b977d09327445a8e9c8126c493c4487c1630Brian Paul#define SPAN_LINEAR(DO_TEX, COMPS)					\
37957d6e1aebf1f850686a2c8d3a246fb388ec23979Brian Paul	for (i = 0; i < span->end; i++) {				\
38033170eeb185f5fe766374a749464497cdfab6931Brian Paul           /* Isn't it necessary to use FixedFloor below?? */		\
381f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLint s = FixedToInt(span->intTex[0]) & info->smask;	\
382f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLint t = FixedToInt(span->intTex[1]) & info->tmask;	\
383f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK;	\
384f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK;	\
385f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLint pos = (t << info->twidth_log2) + s;		\
386f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLchan *tex00 = info->texture + COMPS * pos;		\
3879bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           const GLchan *tex10 = tex00 + info->tbytesline;		\
388f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLchan *tex01 = tex00 + COMPS;				\
389f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLchan *tex11 = tex10 + COMPS;				\
3909bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           if (t == info->tmask) {					\
3919bf68ad963ba92b5d1e725f965979042495a5313Brian Paul              tex10 -= info->tsize;					\
3929bf68ad963ba92b5d1e725f965979042495a5313Brian Paul              tex11 -= info->tsize;					\
3939bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           }								\
3949bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           if (s == info->smask) {					\
3959bf68ad963ba92b5d1e725f965979042495a5313Brian Paul              tex01 -= info->tbytesline;				\
3969bf68ad963ba92b5d1e725f965979042495a5313Brian Paul              tex11 -= info->tbytesline;				\
3979bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           }								\
3989bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           DO_TEX;							\
3999bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           span->red += span->redStep;					\
4009bf68ad963ba92b5d1e725f965979042495a5313Brian Paul	   span->green += span->greenStep;				\
4019bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           span->blue += span->blueStep;				\
4029bf68ad963ba92b5d1e725f965979042495a5313Brian Paul	   span->alpha += span->alphaStep;				\
4039bf68ad963ba92b5d1e725f965979042495a5313Brian Paul	   span->intTex[0] += span->intTexStep[0];			\
4049bf68ad963ba92b5d1e725f965979042495a5313Brian Paul	   span->intTex[1] += span->intTexStep[1];			\
4059bf68ad963ba92b5d1e725f965979042495a5313Brian Paul           dest += 4;							\
406e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	}
407e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
4089bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
4099bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   GLuint i;
41077df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   GLchan *dest = span->array->rgba[0];
41157d6e1aebf1f850686a2c8d3a246fb388ec23979Brian Paul
41292ced46eaf1f389108ed8d3d788498d799f0b385Brian Paul   /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
41307e50058a5699fc9279de6bf5d1449d52ccdc476Brian Paul   ctx->Texture._EnabledCoordUnits = 0x0;
41492ced46eaf1f389108ed8d3d788498d799f0b385Brian Paul
4159bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   span->intTex[0] -= FIXED_HALF;
4169bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   span->intTex[1] -= FIXED_HALF;
4179bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   switch (info->filter) {
4189bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   case GL_NEAREST:
4199bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      switch (info->format) {
4208c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul      case MESA_FORMAT_RGB888:
4219bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         switch (info->envmode) {
4229bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_MODULATE:
42333170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
4249bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4259bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_DECAL:
4269bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_REPLACE:
42733170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
4289bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4299bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_BLEND:
43033170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGB;BLEND,3);
4319bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4329bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_ADD:
43333170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGB;ADD,3);
4349bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4359bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         default:
4364663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            _mesa_problem(ctx, "bad tex env mode in SPAN_LINEAR");
4374663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            return;
4389bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         }
4399bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         break;
4408c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul      case MESA_FORMAT_RGBA8888:
4419bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         switch(info->envmode) {
4429bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_MODULATE:
44333170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
4449bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4459bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_DECAL:
44633170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
4479bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4489bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_BLEND:
44933170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
4509bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4519bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_ADD:
45233170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGBA;ADD,4);
4539bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4549bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_REPLACE:
45533170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
4569bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4579bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         default:
4584663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            _mesa_problem(ctx, "bad tex env mode (2) in SPAN_LINEAR");
4594663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            return;
4609bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         }
4619bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         break;
4629bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      }
4639bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      break;
4649bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
4659bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   case GL_LINEAR:
4669bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      span->intTex[0] -= FIXED_HALF;
4679bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      span->intTex[1] -= FIXED_HALF;
4689bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      switch (info->format) {
4698c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul      case MESA_FORMAT_RGB888:
4709bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         switch (info->envmode) {
4719bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_MODULATE:
47233170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
4739bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4749bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_DECAL:
4759bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_REPLACE:
47633170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
4779bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4789bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_BLEND:
47933170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGB;BLEND,3);
4809bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4819bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_ADD:
48233170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGB;ADD,3);
4839bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4849bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         default:
4854663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            _mesa_problem(ctx, "bad tex env mode (3) in SPAN_LINEAR");
4864663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            return;
4879bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         }
4889bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         break;
4898c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul      case MESA_FORMAT_RGBA8888:
4909bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         switch (info->envmode) {
4919bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_MODULATE:
49233170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
4939bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4949bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_DECAL:
49533170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
4969bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
4979bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_BLEND:
49833170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
4999bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
5009bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_ADD:
50133170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGBA;ADD,4);
5029bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
5039bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         case GL_REPLACE:
50433170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
5059bf68ad963ba92b5d1e725f965979042495a5313Brian Paul            break;
5069bf68ad963ba92b5d1e725f965979042495a5313Brian Paul         default:
5074663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            _mesa_problem(ctx, "bad tex env mode (4) in SPAN_LINEAR");
5084663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            return;
5094663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul         }
5104663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul         break;
5119bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      }
5129bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      break;
5139bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   }
514733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul   span->interpMask &= ~SPAN_RGBA;
5152a182a98973edc9ecf2936b1288485bb2b3fa722Brian Paul   ASSERT(span->arrayMask & SPAN_RGBA);
5168c7135ee14fb6f4d8b6e64d570daee3512c99438Brian Paul
51745bc887da226403f2c41077e40ca38b6f60f1359Brian Paul   _swrast_write_rgba_span(ctx, span);
518e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
5198c7135ee14fb6f4d8b6e64d570daee3512c99438Brian Paul   /* re-enable texture units */
52007e50058a5699fc9279de6bf5d1449d52ccdc476Brian Paul   ctx->Texture._EnabledCoordUnits = texEnableSave;
5218c7135ee14fb6f4d8b6e64d570daee3512c99438Brian Paul
52233170eeb185f5fe766374a749464497cdfab6931Brian Paul#undef SPAN_NEAREST
52333170eeb185f5fe766374a749464497cdfab6931Brian Paul#undef SPAN_LINEAR
524e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
525e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
526e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
527e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
528e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
5299bf68ad963ba92b5d1e725f965979042495a5313Brian Paul * Render an RGB/RGBA textured triangle without perspective correction.
5309bf68ad963ba92b5d1e725f965979042495a5313Brian Paul */
531cdf2da368d180205df3573697b51b8764048ad6eBrian Paul#define NAME affine_textured_triangle
5329bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define INTERP_Z 1
5339bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define INTERP_RGB 1
5349bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define INTERP_ALPHA 1
5359bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define INTERP_INT_TEX 1
5369bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define S_SCALE twidth
5379bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#define T_SCALE theight
5389bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
539571c8ecc8f9442fb8bbaa1f29b1d0d4f8e5b3cadBrian Paul#define SETUP_CODE							\
540571c8ecc8f9442fb8bbaa1f29b1d0d4f8e5b3cadBrian Paul   struct affine_info info;						\
5419bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   struct gl_texture_unit *unit = ctx->Texture.Unit+0;			\
5420103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const struct gl_texture_object *obj = 				\
5439818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul      ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];		\
5440103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const struct gl_texture_image *texImg = 				\
5450103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul      obj->Image[0][obj->BaseLevel]; 					\
5466e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul   const struct swrast_texture_image *swImg =				\
5476e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul      swrast_texture_image_const(texImg);				\
5480103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLfloat twidth = (GLfloat) texImg->Width;			\
5490103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const GLfloat theight = (GLfloat) texImg->Height;			\
550bd3c10c0f0c60ab3421c2da2eab814edc2296cb0Brian Paul   info.texture = (const GLchan *) swImg->Map;				\
5510103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   info.twidth_log2 = texImg->WidthLog2;				\
5520103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   info.smask = texImg->Width - 1;					\
5530103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   info.tmask = texImg->Height - 1;					\
5540103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   info.format = texImg->TexFormat;					\
555ecfaab88b2577bd0395bc05d75a036126806a9c4Brian Paul   info.filter = obj->Sampler.MinFilter;				\
5569bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   info.envmode = unit->EnvMode;					\
557555dc25c4c5a0991fb6846ccc263a195717512deVinson Lee   info.er = 0;					\
558555dc25c4c5a0991fb6846ccc263a195717512deVinson Lee   info.eg = 0;					\
559555dc25c4c5a0991fb6846ccc263a195717512deVinson Lee   info.eb = 0;					\
56077df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.arrayMask |= SPAN_RGBA;						\
5619bf68ad963ba92b5d1e725f965979042495a5313Brian Paul									\
5629bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   if (info.envmode == GL_BLEND) {					\
5639bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      /* potential off-by-one error here? (1.0f -> 2048 -> 0) */	\
564fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul      info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF);	\
565fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul      info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF);	\
566fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul      info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF);	\
567fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul      info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF);	\
5689bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   }									\
5699bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   if (!info.texture) {							\
5709bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      /* this shouldn't happen */					\
5719bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      return;								\
5729bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   }									\
5739bf68ad963ba92b5d1e725f965979042495a5313Brian Paul									\
5749bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   switch (info.format) {						\
5758c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   case MESA_FORMAT_RGB888:						\
5760103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul      info.tbytesline = texImg->Width * 3;				\
5779bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      break;								\
5788c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   case MESA_FORMAT_RGBA8888:						\
5790103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul      info.tbytesline = texImg->Width * 4;				\
5809bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      break;								\
5819bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   default:								\
5829bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
5839bf68ad963ba92b5d1e725f965979042495a5313Brian Paul      return;								\
5849bf68ad963ba92b5d1e725f965979042495a5313Brian Paul   }									\
5850103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   info.tsize = texImg->Height * info.tbytesline;
5869bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
58777df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul#define RENDER_SPAN( span )   affine_span(ctx, &span, &info);
5889bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
5899bf68ad963ba92b5d1e725f965979042495a5313Brian Paul#include "s_tritemp.h"
5909bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
591f431a3fb4dc1bf860203d79e54657e3a62bc50dfBrian Paul
5929bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
59333170eeb185f5fe766374a749464497cdfab6931Brian Paulstruct persp_info
594e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
59533170eeb185f5fe766374a749464497cdfab6931Brian Paul   GLenum filter;
59633170eeb185f5fe766374a749464497cdfab6931Brian Paul   GLenum format;
59733170eeb185f5fe766374a749464497cdfab6931Brian Paul   GLenum envmode;
59833170eeb185f5fe766374a749464497cdfab6931Brian Paul   GLint smask, tmask;
59933170eeb185f5fe766374a749464497cdfab6931Brian Paul   GLint twidth_log2;
60033170eeb185f5fe766374a749464497cdfab6931Brian Paul   const GLchan *texture;
601fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul   GLfixed er, eg, eb, ea;   /* texture env color */
60233170eeb185f5fe766374a749464497cdfab6931Brian Paul   GLint tbytesline, tsize;
60333170eeb185f5fe766374a749464497cdfab6931Brian Paul};
604e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
605f431a3fb4dc1bf860203d79e54657e3a62bc50dfBrian Paul
6069520f483b8f1e45fa474674b415554988de5d8d3Brian Paulstatic inline void
607f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergfast_persp_span(struct gl_context *ctx, SWspan *span,
60833170eeb185f5fe766374a749464497cdfab6931Brian Paul		struct persp_info *info)
60933170eeb185f5fe766374a749464497cdfab6931Brian Paul{
6107152305e0a7d963f49c05bc530e495dc3ad82e3bBrian Paul   GLchan sample[4];  /* the filtered texture sample */
6119bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
61233170eeb185f5fe766374a749464497cdfab6931Brian Paul  /* Instead of defining a function for each mode, a test is done
61333170eeb185f5fe766374a749464497cdfab6931Brian Paul   * between the outer and inner loops. This is to reduce code size
61433170eeb185f5fe766374a749464497cdfab6931Brian Paul   * and complexity. Observe that an optimizing compiler kills
61533170eeb185f5fe766374a749464497cdfab6931Brian Paul   * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
61633170eeb185f5fe766374a749464497cdfab6931Brian Paul   */
61733170eeb185f5fe766374a749464497cdfab6931Brian Paul#define SPAN_NEAREST(DO_TEX,COMP)					\
61857d6e1aebf1f850686a2c8d3a246fb388ec23979Brian Paul	for (i = 0; i < span->end; i++) {				\
61933170eeb185f5fe766374a749464497cdfab6931Brian Paul           GLdouble invQ = tex_coord[2] ?				\
62033170eeb185f5fe766374a749464497cdfab6931Brian Paul                                 (1.0 / tex_coord[2]) : 1.0;            \
6217c4268176eaaeb45003db4d5042a518b84c9f6dcKarl Schultz           GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ);		\
6227c4268176eaaeb45003db4d5042a518b84c9f6dcKarl Schultz           GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ);		\
62333170eeb185f5fe766374a749464497cdfab6931Brian Paul           GLint s = IFLOOR(s_tmp) & info->smask;	        	\
62433170eeb185f5fe766374a749464497cdfab6931Brian Paul           GLint t = IFLOOR(t_tmp) & info->tmask;	        	\
62533170eeb185f5fe766374a749464497cdfab6931Brian Paul           GLint pos = (t << info->twidth_log2) + s;			\
62633170eeb185f5fe766374a749464497cdfab6931Brian Paul           const GLchan *tex00 = info->texture + COMP * pos;		\
62733170eeb185f5fe766374a749464497cdfab6931Brian Paul           DO_TEX;							\
62833170eeb185f5fe766374a749464497cdfab6931Brian Paul           span->red += span->redStep;					\
62933170eeb185f5fe766374a749464497cdfab6931Brian Paul	   span->green += span->greenStep;				\
63033170eeb185f5fe766374a749464497cdfab6931Brian Paul           span->blue += span->blueStep;				\
63133170eeb185f5fe766374a749464497cdfab6931Brian Paul	   span->alpha += span->alphaStep;				\
63233170eeb185f5fe766374a749464497cdfab6931Brian Paul	   tex_coord[0] += tex_step[0];					\
63333170eeb185f5fe766374a749464497cdfab6931Brian Paul	   tex_coord[1] += tex_step[1];					\
63433170eeb185f5fe766374a749464497cdfab6931Brian Paul	   tex_coord[2] += tex_step[2];					\
63533170eeb185f5fe766374a749464497cdfab6931Brian Paul           dest += 4;							\
63633170eeb185f5fe766374a749464497cdfab6931Brian Paul	}
637e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
63833170eeb185f5fe766374a749464497cdfab6931Brian Paul#define SPAN_LINEAR(DO_TEX,COMP)					\
63957d6e1aebf1f850686a2c8d3a246fb388ec23979Brian Paul	for (i = 0; i < span->end; i++) {				\
64033170eeb185f5fe766374a749464497cdfab6931Brian Paul           GLdouble invQ = tex_coord[2] ?				\
64133170eeb185f5fe766374a749464497cdfab6931Brian Paul                                 (1.0 / tex_coord[2]) : 1.0;            \
642f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ);	\
643f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ);	\
644f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF;	\
645f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF;      \
646f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask;	\
647f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask;	\
648f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLfixed sf = s_fix & FIXED_FRAC_MASK;			\
649f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLfixed tf = t_fix & FIXED_FRAC_MASK;			\
650f138b977d09327445a8e9c8126c493c4487c1630Brian Paul           const GLint pos = (t << info->twidth_log2) + s;		\
65133170eeb185f5fe766374a749464497cdfab6931Brian Paul           const GLchan *tex00 = info->texture + COMP * pos;		\
65233170eeb185f5fe766374a749464497cdfab6931Brian Paul           const GLchan *tex10 = tex00 + info->tbytesline;		\
65333170eeb185f5fe766374a749464497cdfab6931Brian Paul           const GLchan *tex01 = tex00 + COMP;				\
65433170eeb185f5fe766374a749464497cdfab6931Brian Paul           const GLchan *tex11 = tex10 + COMP;				\
65533170eeb185f5fe766374a749464497cdfab6931Brian Paul           if (t == info->tmask) {					\
65633170eeb185f5fe766374a749464497cdfab6931Brian Paul              tex10 -= info->tsize;					\
65733170eeb185f5fe766374a749464497cdfab6931Brian Paul              tex11 -= info->tsize;					\
65833170eeb185f5fe766374a749464497cdfab6931Brian Paul           }								\
65933170eeb185f5fe766374a749464497cdfab6931Brian Paul           if (s == info->smask) {					\
66033170eeb185f5fe766374a749464497cdfab6931Brian Paul              tex01 -= info->tbytesline;				\
66133170eeb185f5fe766374a749464497cdfab6931Brian Paul              tex11 -= info->tbytesline;				\
66233170eeb185f5fe766374a749464497cdfab6931Brian Paul           }								\
66333170eeb185f5fe766374a749464497cdfab6931Brian Paul           DO_TEX;							\
66433170eeb185f5fe766374a749464497cdfab6931Brian Paul           span->red   += span->redStep;				\
66533170eeb185f5fe766374a749464497cdfab6931Brian Paul	   span->green += span->greenStep;				\
66633170eeb185f5fe766374a749464497cdfab6931Brian Paul           span->blue  += span->blueStep;				\
66733170eeb185f5fe766374a749464497cdfab6931Brian Paul	   span->alpha += span->alphaStep;				\
66833170eeb185f5fe766374a749464497cdfab6931Brian Paul	   tex_coord[0] += tex_step[0];					\
66933170eeb185f5fe766374a749464497cdfab6931Brian Paul	   tex_coord[1] += tex_step[1];					\
67033170eeb185f5fe766374a749464497cdfab6931Brian Paul	   tex_coord[2] += tex_step[2];					\
67133170eeb185f5fe766374a749464497cdfab6931Brian Paul           dest += 4;							\
67233170eeb185f5fe766374a749464497cdfab6931Brian Paul	}
673e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
67433170eeb185f5fe766374a749464497cdfab6931Brian Paul   GLuint i;
67533170eeb185f5fe766374a749464497cdfab6931Brian Paul   GLfloat tex_coord[3], tex_step[3];
67677df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   GLchan *dest = span->array->rgba[0];
67757d6e1aebf1f850686a2c8d3a246fb388ec23979Brian Paul
67807e50058a5699fc9279de6bf5d1449d52ccdc476Brian Paul   const GLuint texEnableSave = ctx->Texture._EnabledCoordUnits;
67907e50058a5699fc9279de6bf5d1449d52ccdc476Brian Paul   ctx->Texture._EnabledCoordUnits = 0;
680a803b0c891404dcd7c376e91f6a033cd4e42abc3Brian Paul
6819ab512ad8cf3a12f4f7f8494fa99bc9389f217dbBrian   tex_coord[0] = span->attrStart[FRAG_ATTRIB_TEX0][0]  * (info->smask + 1);
6829ab512ad8cf3a12f4f7f8494fa99bc9389f217dbBrian   tex_step[0] = span->attrStepX[FRAG_ATTRIB_TEX0][0] * (info->smask + 1);
6839ab512ad8cf3a12f4f7f8494fa99bc9389f217dbBrian   tex_coord[1] = span->attrStart[FRAG_ATTRIB_TEX0][1] * (info->tmask + 1);
6849ab512ad8cf3a12f4f7f8494fa99bc9389f217dbBrian   tex_step[1] = span->attrStepX[FRAG_ATTRIB_TEX0][1] * (info->tmask + 1);
6859ab512ad8cf3a12f4f7f8494fa99bc9389f217dbBrian   /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
6869ab512ad8cf3a12f4f7f8494fa99bc9389f217dbBrian   tex_coord[2] = span->attrStart[FRAG_ATTRIB_TEX0][3];
6879ab512ad8cf3a12f4f7f8494fa99bc9389f217dbBrian   tex_step[2] = span->attrStepX[FRAG_ATTRIB_TEX0][3];
688e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
68933170eeb185f5fe766374a749464497cdfab6931Brian Paul   switch (info->filter) {
69033170eeb185f5fe766374a749464497cdfab6931Brian Paul   case GL_NEAREST:
69133170eeb185f5fe766374a749464497cdfab6931Brian Paul      switch (info->format) {
6928c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul      case MESA_FORMAT_RGB888:
69333170eeb185f5fe766374a749464497cdfab6931Brian Paul         switch (info->envmode) {
69433170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_MODULATE:
69533170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
69633170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
69733170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_DECAL:
69833170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_REPLACE:
69933170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
70033170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
70133170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_BLEND:
70233170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGB;BLEND,3);
70333170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
70433170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_ADD:
70533170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGB;ADD,3);
70633170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
70733170eeb185f5fe766374a749464497cdfab6931Brian Paul         default:
7084663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            _mesa_problem(ctx, "bad tex env mode (5) in SPAN_LINEAR");
7094663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            return;
71033170eeb185f5fe766374a749464497cdfab6931Brian Paul         }
71133170eeb185f5fe766374a749464497cdfab6931Brian Paul         break;
7128c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul      case MESA_FORMAT_RGBA8888:
71333170eeb185f5fe766374a749464497cdfab6931Brian Paul         switch(info->envmode) {
71433170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_MODULATE:
71533170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
71633170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
71733170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_DECAL:
71833170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
71933170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
72033170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_BLEND:
72133170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
72233170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
72333170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_ADD:
72433170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGBA;ADD,4);
72533170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
72633170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_REPLACE:
72733170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
72833170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
72933170eeb185f5fe766374a749464497cdfab6931Brian Paul         default:
7304663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            _mesa_problem(ctx, "bad tex env mode (6) in SPAN_LINEAR");
7314663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            return;
73233170eeb185f5fe766374a749464497cdfab6931Brian Paul         }
73333170eeb185f5fe766374a749464497cdfab6931Brian Paul         break;
73433170eeb185f5fe766374a749464497cdfab6931Brian Paul      }
73533170eeb185f5fe766374a749464497cdfab6931Brian Paul      break;
73633170eeb185f5fe766374a749464497cdfab6931Brian Paul
73733170eeb185f5fe766374a749464497cdfab6931Brian Paul   case GL_LINEAR:
73833170eeb185f5fe766374a749464497cdfab6931Brian Paul      switch (info->format) {
7398c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul      case MESA_FORMAT_RGB888:
74033170eeb185f5fe766374a749464497cdfab6931Brian Paul         switch (info->envmode) {
74133170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_MODULATE:
74233170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
74333170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
74433170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_DECAL:
74533170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_REPLACE:
74633170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
74733170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
74833170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_BLEND:
74933170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGB;BLEND,3);
75033170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
75133170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_ADD:
75233170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGB;ADD,3);
75333170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
75433170eeb185f5fe766374a749464497cdfab6931Brian Paul         default:
7554663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            _mesa_problem(ctx, "bad tex env mode (7) in SPAN_LINEAR");
7564663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            return;
75733170eeb185f5fe766374a749464497cdfab6931Brian Paul         }
75833170eeb185f5fe766374a749464497cdfab6931Brian Paul         break;
7598c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul      case MESA_FORMAT_RGBA8888:
76033170eeb185f5fe766374a749464497cdfab6931Brian Paul         switch (info->envmode) {
76133170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_MODULATE:
76233170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
76333170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
76433170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_DECAL:
76533170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
76633170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
76733170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_BLEND:
76833170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
76933170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
77033170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_ADD:
77133170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGBA;ADD,4);
77233170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
77333170eeb185f5fe766374a749464497cdfab6931Brian Paul         case GL_REPLACE:
77433170eeb185f5fe766374a749464497cdfab6931Brian Paul            SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
77533170eeb185f5fe766374a749464497cdfab6931Brian Paul            break;
77633170eeb185f5fe766374a749464497cdfab6931Brian Paul         default:
7774663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            _mesa_problem(ctx, "bad tex env mode (8) in SPAN_LINEAR");
7784663bd50f1e2f219bd1b2a4d902b11b936512398Brian Paul            return;
77933170eeb185f5fe766374a749464497cdfab6931Brian Paul         }
78033170eeb185f5fe766374a749464497cdfab6931Brian Paul         break;
78133170eeb185f5fe766374a749464497cdfab6931Brian Paul      }
78233170eeb185f5fe766374a749464497cdfab6931Brian Paul      break;
783e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
78433170eeb185f5fe766374a749464497cdfab6931Brian Paul
7852a182a98973edc9ecf2936b1288485bb2b3fa722Brian Paul   ASSERT(span->arrayMask & SPAN_RGBA);
78645bc887da226403f2c41077e40ca38b6f60f1359Brian Paul   _swrast_write_rgba_span(ctx, span);
787e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
78833170eeb185f5fe766374a749464497cdfab6931Brian Paul#undef SPAN_NEAREST
78933170eeb185f5fe766374a749464497cdfab6931Brian Paul#undef SPAN_LINEAR
790a803b0c891404dcd7c376e91f6a033cd4e42abc3Brian Paul
791a803b0c891404dcd7c376e91f6a033cd4e42abc3Brian Paul   /* restore state */
79207e50058a5699fc9279de6bf5d1449d52ccdc476Brian Paul   ctx->Texture._EnabledCoordUnits = texEnableSave;
793e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
794e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
795e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
796e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
797e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Render an perspective corrected RGB/RGBA textured triangle.
798e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * The Q (aka V in Mesa) coordinate must be zero such that the divide
799e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * by interpolated Q/W comes out right.
800e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *
801e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
802cdf2da368d180205df3573697b51b8764048ad6eBrian Paul#define NAME persp_textured_triangle
803e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_Z 1
804e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_RGB 1
805e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_ALPHA 1
8068a8a5bd104e8cd9362415db77d5f6a3945939589Brian#define INTERP_ATTRIBS 1
8079bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
808571c8ecc8f9442fb8bbaa1f29b1d0d4f8e5b3cadBrian Paul#define SETUP_CODE							\
809571c8ecc8f9442fb8bbaa1f29b1d0d4f8e5b3cadBrian Paul   struct persp_info info;						\
8102ef866d1fc0a5cc5ef8543d65744dfd4da4dbbafBrian Paul   const struct gl_texture_unit *unit = ctx->Texture.Unit+0;		\
8110103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const struct gl_texture_object *obj = 				\
8129818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul      ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];		\
8130103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   const struct gl_texture_image *texImg = 				\
8140103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul      obj->Image[0][obj->BaseLevel];			 		\
8156e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul   const struct swrast_texture_image *swImg =				\
8166e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul      swrast_texture_image_const(texImg);				\
817bd3c10c0f0c60ab3421c2da2eab814edc2296cb0Brian Paul   info.texture = (const GLchan *) swImg->Map;				\
8180103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   info.twidth_log2 = texImg->WidthLog2;				\
8190103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   info.smask = texImg->Width - 1;					\
8200103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   info.tmask = texImg->Height - 1;					\
8210103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   info.format = texImg->TexFormat;					\
822ecfaab88b2577bd0395bc05d75a036126806a9c4Brian Paul   info.filter = obj->Sampler.MinFilter;				\
82333170eeb185f5fe766374a749464497cdfab6931Brian Paul   info.envmode = unit->EnvMode;					\
824555dc25c4c5a0991fb6846ccc263a195717512deVinson Lee   info.er = 0;					\
825555dc25c4c5a0991fb6846ccc263a195717512deVinson Lee   info.eg = 0;					\
826555dc25c4c5a0991fb6846ccc263a195717512deVinson Lee   info.eb = 0;					\
82733170eeb185f5fe766374a749464497cdfab6931Brian Paul									\
82833170eeb185f5fe766374a749464497cdfab6931Brian Paul   if (info.envmode == GL_BLEND) {					\
82933170eeb185f5fe766374a749464497cdfab6931Brian Paul      /* potential off-by-one error here? (1.0f -> 2048 -> 0) */	\
830fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul      info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF);	\
831fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul      info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF);	\
832fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul      info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF);	\
833fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paul      info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF);	\
83433170eeb185f5fe766374a749464497cdfab6931Brian Paul   }									\
83533170eeb185f5fe766374a749464497cdfab6931Brian Paul   if (!info.texture) {							\
83633170eeb185f5fe766374a749464497cdfab6931Brian Paul      /* this shouldn't happen */					\
8378e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      return;								\
838e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }									\
83933170eeb185f5fe766374a749464497cdfab6931Brian Paul									\
84033170eeb185f5fe766374a749464497cdfab6931Brian Paul   switch (info.format) {						\
8418c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   case MESA_FORMAT_RGB888:						\
8420103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul      info.tbytesline = texImg->Width * 3;				\
84333170eeb185f5fe766374a749464497cdfab6931Brian Paul      break;								\
8448c36ca707ca8879d6f888de7733ffb6b04ddc48aBrian Paul   case MESA_FORMAT_RGBA8888:						\
8450103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul      info.tbytesline = texImg->Width * 4;				\
84633170eeb185f5fe766374a749464497cdfab6931Brian Paul      break;								\
847e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   default:								\
848fae7b778b81b686ef419f971064b5fe12fb4ead3Brian Paul      _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
84933170eeb185f5fe766374a749464497cdfab6931Brian Paul      return;								\
85033170eeb185f5fe766374a749464497cdfab6931Brian Paul   }									\
8510103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul   info.tsize = texImg->Height * info.tbytesline;
852e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
8537956292a765910077f50352d7cd0174e1e66d26cBrian Paul#define RENDER_SPAN( span )			\
85477df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.interpMask &= ~SPAN_RGBA;		\
85577df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   span.arrayMask |= SPAN_RGBA;			\
85677df88727cb0a423dd5cb41498c2302d9df4fce7Brian Paul   fast_persp_span(ctx, &span, &info);
857e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
858e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_tritemp.h"
859e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
8609e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian#endif /*CHAN_TYPE != GL_FLOAT*/
861fae7b778b81b686ef419f971064b5fe12fb4ead3Brian Paul
8629bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
8639bf68ad963ba92b5d1e725f965979042495a5313Brian Paul
8649bf68ad963ba92b5d1e725f965979042495a5313Brian Paul/*
8654003bde6fffc3e5b9e1a115ba952b988dffb099aBrian * Render an RGBA triangle with arbitrary attributes.
866e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
8674003bde6fffc3e5b9e1a115ba952b988dffb099aBrian#define NAME general_triangle
868e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_Z 1
869e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_RGB 1
870e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_ALPHA 1
8718a8a5bd104e8cd9362415db77d5f6a3945939589Brian#define INTERP_ATTRIBS 1
872a803b0c891404dcd7c376e91f6a033cd4e42abc3Brian Paul#define RENDER_SPAN( span )   _swrast_write_rgba_span(ctx, &span);
873e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_tritemp.h"
874e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
875e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
876f1e236987829393c81dc86ea19cb49eefe190317Brian Paul
877e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
878b17a722ca3989e8563ee04cb2939f4835f8a171eBrian Paul/*
879b17a722ca3989e8563ee04cb2939f4835f8a171eBrian Paul * Special tri function for occlusion testing
880b17a722ca3989e8563ee04cb2939f4835f8a171eBrian Paul */
881bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul#define NAME occlusion_zless_16_triangle
882e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#define INTERP_Z 1
88323ffc3a85d6172f8a98d17d7f23610bab808d84eBrian Paul#define SETUP_CODE							\
884bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul   struct gl_renderbuffer *rb =                                         \
885bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul      ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;           \
88623ffc3a85d6172f8a98d17d7f23610bab808d84eBrian Paul   struct gl_query_object *q = ctx->Query.CurrentOcclusionObject;	\
88723ffc3a85d6172f8a98d17d7f23610bab808d84eBrian Paul   ASSERT(ctx->Depth.Test);						\
88823ffc3a85d6172f8a98d17d7f23610bab808d84eBrian Paul   ASSERT(!ctx->Depth.Mask);						\
88923ffc3a85d6172f8a98d17d7f23610bab808d84eBrian Paul   ASSERT(ctx->Depth.Func == GL_LESS);					\
890bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul   assert(rb->Format == MESA_FORMAT_Z16);                               \
89123ffc3a85d6172f8a98d17d7f23610bab808d84eBrian Paul   if (!q) {								\
89223ffc3a85d6172f8a98d17d7f23610bab808d84eBrian Paul      return;								\
893cdf2da368d180205df3573697b51b8764048ad6eBrian Paul   }
8940349b4b2abac65f67a638a9f8bb5d1a3d48ddf95Brian Paul#define RENDER_SPAN( span )						\
895bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul   {                                                                    \
8960349b4b2abac65f67a638a9f8bb5d1a3d48ddf95Brian Paul      GLuint i;								\
8970349b4b2abac65f67a638a9f8bb5d1a3d48ddf95Brian Paul      const GLushort *zRow = (const GLushort *)				\
8987cf2d75d4f5397629a058b9ea9b20df688b79e5cBrian Paul         _swrast_pixel_address(rb, span.x, span.y);                     \
8990349b4b2abac65f67a638a9f8bb5d1a3d48ddf95Brian Paul      for (i = 0; i < span.end; i++) {					\
9003e37bafab0a339021354b9c78f983d05d433d735Brian Paul         GLuint z = FixedToDepth(span.z);				\
9010349b4b2abac65f67a638a9f8bb5d1a3d48ddf95Brian Paul         if (z < zRow[i]) {						\
90223ffc3a85d6172f8a98d17d7f23610bab808d84eBrian Paul            q->Result++;						\
9030349b4b2abac65f67a638a9f8bb5d1a3d48ddf95Brian Paul         }								\
9040349b4b2abac65f67a638a9f8bb5d1a3d48ddf95Brian Paul         span.z += span.zStep;						\
9050349b4b2abac65f67a638a9f8bb5d1a3d48ddf95Brian Paul      }									\
906e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
907e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_tritemp.h"
908e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
909cdf2da368d180205df3573697b51b8764048ad6eBrian Paul
910cdf2da368d180205df3573697b51b8764048ad6eBrian Paul
911cdf2da368d180205df3573697b51b8764048ad6eBrian Paulstatic void
912f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergnodraw_triangle( struct gl_context *ctx,
913cdf2da368d180205df3573697b51b8764048ad6eBrian Paul                 const SWvertex *v0,
914cdf2da368d180205df3573697b51b8764048ad6eBrian Paul                 const SWvertex *v1,
915cdf2da368d180205df3573697b51b8764048ad6eBrian Paul                 const SWvertex *v2 )
91647489c0721348d8f5e5f17b4af63b1c601045116Keith Whitwell{
91747489c0721348d8f5e5f17b4af63b1c601045116Keith Whitwell   (void) (ctx && v0 && v1 && v2);
91847489c0721348d8f5e5f17b4af63b1c601045116Keith Whitwell}
919e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
920f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul
921f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul/*
922f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul * This is used when separate specular color is enabled, but not
923fa6b2fba7ad0504a63ab262f602c6f50f336ca1bBrian Paul * texturing.  We add the specular color to the primary color,
924f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul * draw the triangle, then restore the original primary color.
925f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul * Inefficient, but seldom needed.
926f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul */
9279e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrianvoid
928f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_swrast_add_spec_terms_triangle(struct gl_context *ctx, const SWvertex *v0,
9299e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian                                const SWvertex *v1, const SWvertex *v2)
93046b0988c673b28e072fd0cbf477632a9ab6f9f18Keith Whitwell{
93146b0988c673b28e072fd0cbf477632a9ab6f9f18Keith Whitwell   SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */
93246b0988c673b28e072fd0cbf477632a9ab6f9f18Keith Whitwell   SWvertex *ncv1 = (SWvertex *)v1;
93346b0988c673b28e072fd0cbf477632a9ab6f9f18Keith Whitwell   SWvertex *ncv2 = (SWvertex *)v2;
934f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul   GLfloat rSum, gSum, bSum;
9359e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   GLchan cSave[3][4];
9369e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian
937f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul   /* save original colors */
9389e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   COPY_CHAN4( cSave[0], ncv0->color );
9399e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   COPY_CHAN4( cSave[1], ncv1->color );
9409e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   COPY_CHAN4( cSave[2], ncv2->color );
941f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul   /* sum v0 */
9429e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   rSum = CHAN_TO_FLOAT(ncv0->color[0]) + ncv0->attrib[FRAG_ATTRIB_COL1][0];
9439e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   gSum = CHAN_TO_FLOAT(ncv0->color[1]) + ncv0->attrib[FRAG_ATTRIB_COL1][1];
9449e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   bSum = CHAN_TO_FLOAT(ncv0->color[2]) + ncv0->attrib[FRAG_ATTRIB_COL1][2];
9459e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[0], rSum);
9469e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[1], gSum);
9479e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[2], bSum);
948f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul   /* sum v1 */
9499e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   rSum = CHAN_TO_FLOAT(ncv1->color[0]) + ncv1->attrib[FRAG_ATTRIB_COL1][0];
9509e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   gSum = CHAN_TO_FLOAT(ncv1->color[1]) + ncv1->attrib[FRAG_ATTRIB_COL1][1];
9519e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   bSum = CHAN_TO_FLOAT(ncv1->color[2]) + ncv1->attrib[FRAG_ATTRIB_COL1][2];
9529e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[0], rSum);
9539e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[1], gSum);
9549e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[2], bSum);
955f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul   /* sum v2 */
9569e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   rSum = CHAN_TO_FLOAT(ncv2->color[0]) + ncv2->attrib[FRAG_ATTRIB_COL1][0];
9579e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   gSum = CHAN_TO_FLOAT(ncv2->color[1]) + ncv2->attrib[FRAG_ATTRIB_COL1][1];
9589e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   bSum = CHAN_TO_FLOAT(ncv2->color[2]) + ncv2->attrib[FRAG_ATTRIB_COL1][2];
9599e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[0], rSum);
9609e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[1], gSum);
9619e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[2], bSum);
962f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul   /* draw */
96346b0988c673b28e072fd0cbf477632a9ab6f9f18Keith Whitwell   SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 );
964f47efb5ab32534fc5c42b5bc88a6b24cb514ce53Brian Paul   /* restore original colors */
9659e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   COPY_CHAN4( ncv0->color, cSave[0] );
9669e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   COPY_CHAN4( ncv1->color, cSave[1] );
9679e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian   COPY_CHAN4( ncv2->color, cSave[2] );
96846b0988c673b28e072fd0cbf477632a9ab6f9f18Keith Whitwell}
96946b0988c673b28e072fd0cbf477632a9ab6f9f18Keith Whitwell
970e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
971e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
972e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul#ifdef DEBUG
973e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul
974e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul/* record the current triangle function name */
975fd1727bd1064ee2323c924cb4002d4d9a89780bfBrian Paulconst char *_mesa_triFuncName = NULL;
976e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul
9772a182a98973edc9ecf2936b1288485bb2b3fa722Brian Paul#define USE(triFunc)				\
9782a182a98973edc9ecf2936b1288485bb2b3fa722Brian Pauldo {						\
9792a182a98973edc9ecf2936b1288485bb2b3fa722Brian Paul    _mesa_triFuncName = #triFunc;		\
9802a182a98973edc9ecf2936b1288485bb2b3fa722Brian Paul    /*printf("%s\n", _mesa_triFuncName);*/	\
9812a182a98973edc9ecf2936b1288485bb2b3fa722Brian Paul    swrast->Triangle = triFunc;			\
982e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul} while (0)
983e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul
984e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#else
985e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul
986e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul#define USE(triFunc)  swrast->Triangle = triFunc;
987e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul
988e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#endif
989e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
990e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
991e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
992e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul
993e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
994e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Determine which triangle rendering function to use given the current
995e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * rendering context.
996e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *
997e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
998e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * remove tests to this code.
999e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
100022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid
1001f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_swrast_choose_triangle( struct gl_context *ctx )
1002e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
1003cd03ed4f54444d96e4e47cdb118a3dfd94d92bb0Keith Whitwell   SWcontext *swrast = SWRAST_CONTEXT(ctx);
1004e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
100538f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes   if (ctx->Polygon.CullFlag &&
100647489c0721348d8f5e5f17b4af63b1c601045116Keith Whitwell       ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
1007e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul      USE(nodraw_triangle);
100847489c0721348d8f5e5f17b4af63b1c601045116Keith Whitwell      return;
100947489c0721348d8f5e5f17b4af63b1c601045116Keith Whitwell   }
101047489c0721348d8f5e5f17b4af63b1c601045116Keith Whitwell
1011e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   if (ctx->RenderMode==GL_RENDER) {
1012bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul      struct gl_renderbuffer *depthRb =
1013bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul         ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
1014e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
1015e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      if (ctx->Polygon.SmoothFlag) {
101645bc887da226403f2c41077e40ca38b6f60f1359Brian Paul         _swrast_set_aa_triangle_function(ctx);
1017cd03ed4f54444d96e4e47cdb118a3dfd94d92bb0Keith Whitwell         ASSERT(swrast->Triangle);
1018e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         return;
1019e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      }
1020e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
1021b17a722ca3989e8563ee04cb2939f4835f8a171eBrian Paul      /* special case for occlusion testing */
102223ffc3a85d6172f8a98d17d7f23610bab808d84eBrian Paul      if (ctx->Query.CurrentOcclusionObject &&
1023e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell          ctx->Depth.Test &&
1024e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell          ctx->Depth.Mask == GL_FALSE &&
1025e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell          ctx->Depth.Func == GL_LESS &&
1026bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul          !ctx->Stencil._Enabled &&
1027bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul          depthRb &&
1028bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul          depthRb->Format == MESA_FORMAT_Z16) {
10290ed9c4bc7affb7a6b9be5ff129815fccf8c3885dIan Romanick         if (ctx->Color.ColorMask[0][0] == 0 &&
10300ed9c4bc7affb7a6b9be5ff129815fccf8c3885dIan Romanick	     ctx->Color.ColorMask[0][1] == 0 &&
10310ed9c4bc7affb7a6b9be5ff129815fccf8c3885dIan Romanick	     ctx->Color.ColorMask[0][2] == 0 &&
10320ed9c4bc7affb7a6b9be5ff129815fccf8c3885dIan Romanick	     ctx->Color.ColorMask[0][3] == 0) {
1033bf0c0ccbc9424fc73cb17ffb9c9b5feda7d33474Brian Paul            USE(occlusion_zless_16_triangle);
1034e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell            return;
1035e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         }
1036e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      }
1037e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
10389e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian      /*
10399e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian       * XXX should examine swrast->_ActiveAttribMask to determine what
10409e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian       * needs to be interpolated.
10419e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian       */
104212ef1fbefcee964b715783d3ade6b69b2c699ed8Brian      if (ctx->Texture._EnabledCoordUnits ||
10431c0f1dd42a50464eeb81de4aad8eecf24b3d6c89Chad Versace	  _swrast_use_fragment_program(ctx) ||
10449e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian          ctx->ATIFragmentShader._Enabled ||
1045decc6e2a32ef49e673c081f30e19b8970155d887Brian Paul          _mesa_need_secondary_color(ctx) ||
10469e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian          swrast->_FogEnabled) {
1047e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1048b7339d70237ffb460f158c61860ca225fec02e81Michal Krol         const struct gl_texture_object *texObj2D;
1049cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen         const struct gl_sampler_object *samp;
1050e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul         const struct gl_texture_image *texImg;
1051eaf376ba354db11f7729452060570b48a029c9a0Brian Paul         const struct swrast_texture_image *swImg;
105238e3675d7dc6c373366e576e3ca1a1eb966e4dacBrian Paul         GLenum minFilter, magFilter, envMode;
10531f7c914ad0beea8a29c1a171c7cd1a12f2efe0faBrian Paul         gl_format format;
10549818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul         texObj2D = ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];
1055cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen         if (ctx->Texture.Unit[0].Sampler)
1056cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen            samp = ctx->Texture.Unit[0].Sampler;
1057cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen         else if (texObj2D)
1058cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen            samp = &texObj2D->Sampler;
1059cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen         else
1060cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen            samp = NULL;
10619818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul
106218fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         texImg = texObj2D ? texObj2D->Image[0][texObj2D->BaseLevel] : NULL;
1063eaf376ba354db11f7729452060570b48a029c9a0Brian Paul         swImg = swrast_texture_image_const(texImg);
1064eaf376ba354db11f7729452060570b48a029c9a0Brian Paul
10650103d7a47a6f42d8bb7199b5bd55097e3b0b92a4Brian Paul         format = texImg ? texImg->TexFormat : MESA_FORMAT_NONE;
1066cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen         minFilter = texObj2D ? samp->MinFilter : GL_NONE;
1067cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen         magFilter = texObj2D ? samp->MagFilter : GL_NONE;
1068e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul         envMode = ctx->Texture.Unit[0].EnvMode;
1069e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul
1070c0fc0d4e5f5c246fc2459348a3d114b232d0c545Brian Paul         /* First see if we can use an optimized 2-D texture function */
10710349b4b2abac65f67a638a9f8bb5d1a3d48ddf95Brian Paul         if (ctx->Texture._EnabledCoordUnits == 0x1
10721c0f1dd42a50464eeb81de4aad8eecf24b3d6c89Chad Versace             && !_swrast_use_fragment_program(ctx)
10736acf1e93a291511cfb20b0e2aeda6e71ceb62a62Michal Krol             && !ctx->ATIFragmentShader._Enabled
107488bb4b593576a2977b2fe36794bd4d3dbc72063eBrian Paul             && ctx->Texture._EnabledUnits == 0x1
10758afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul             && ctx->Texture.Unit[0]._ReallyEnabled == TEXTURE_2D_BIT
1076cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen             && samp->WrapS == GL_REPEAT
1077cbdc1d53542b3ecca0085399c4bb3b3371f94809Pauli Nieminen             && samp->WrapT == GL_REPEAT
107854c62ba5c36f3e2b279151f5df851d2ceee15319Brian Paul             && texObj2D->_Swizzle == SWIZZLE_NOOP
1079eaf376ba354db11f7729452060570b48a029c9a0Brian Paul             && swImg->_IsPowerOfTwo
10800349b4b2abac65f67a638a9f8bb5d1a3d48ddf95Brian Paul             && texImg->Border == 0
10816e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul             && texImg->Width == swImg->RowStride
10823fa7dbf368bb060220e9f78e666b00d6827166a6Brian Paul             && (format == MESA_FORMAT_RGB888 || format == MESA_FORMAT_RGBA8888)
1083b7339d70237ffb460f158c61860ca225fec02e81Michal Krol             && minFilter == magFilter
1084b7339d70237ffb460f158c61860ca225fec02e81Michal Krol             && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
10859e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian             && !swrast->_FogEnabled
10862d508c1d5c53dc475e5262593dd4dac5575a6b08Brian Paul             && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT
10872d508c1d5c53dc475e5262593dd4dac5575a6b08Brian Paul             && ctx->Texture.Unit[0].EnvMode != GL_COMBINE4_NV) {
1088e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	    if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) {
1089e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul	       if (minFilter == GL_NEAREST
10903fa7dbf368bb060220e9f78e666b00d6827166a6Brian Paul		   && format == MESA_FORMAT_RGB888
1091e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul		   && (envMode == GL_REPLACE || envMode == GL_DECAL)
10929bf68ad963ba92b5d1e725f965979042495a5313Brian Paul		   && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)
1093e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul			&& ctx->Depth.Func == GL_LESS
1094e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul			&& ctx->Depth.Mask == GL_TRUE)
10959bf68ad963ba92b5d1e725f965979042495a5313Brian Paul		       || swrast->_RasterMask == TEXTURE_BIT)
1096e6e0ba781b0a6a1b8747cae49ca622a6a61b1bf8Brian Paul		   && ctx->Polygon.StippleFlag == GL_FALSE
10979c4fd26bcab7e4a5bd97a56deff89b8ea0ae5f1dBrian Paul                   && ctx->DrawBuffer->Visual.depthBits <= 16) {
10989bf68ad963ba92b5d1e725f965979042495a5313Brian Paul		  if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) {
1099e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul		     USE(simple_z_textured_triangle);
1100e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell		  }
1101e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell		  else {
1102e9313a64c182701d22a5c6d2a82863c658e7424fBrian Paul		     USE(simple_textured_triangle);
1103e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell		  }
1104e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	       }
1105e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	       else {
11069e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian#if CHAN_BITS != 8
11074003bde6fffc3e5b9e1a115ba952b988dffb099aBrian                  USE(general_triangle);
110896385fa15569e25cd0977e678c0ff3bdab6ef316Brian Paul#else
110959b29516af2d4d8f83723559921a3709eb77a7d2Chia-I Wu                  if (format == MESA_FORMAT_RGBA8888 && !_mesa_little_endian()) {
1110086f9fc0e2aef27f54eda87c733685500555bf20Brian Paul                     /* We only handle RGBA8888 correctly on little endian
1111086f9fc0e2aef27f54eda87c733685500555bf20Brian Paul                      * in the optimized code above.
1112086f9fc0e2aef27f54eda87c733685500555bf20Brian Paul                      */
1113086f9fc0e2aef27f54eda87c733685500555bf20Brian Paul                     USE(general_triangle);
111459b29516af2d4d8f83723559921a3709eb77a7d2Chia-I Wu                  }
111559b29516af2d4d8f83723559921a3709eb77a7d2Chia-I Wu                  else {
1116086f9fc0e2aef27f54eda87c733685500555bf20Brian Paul                     USE(affine_textured_triangle);
111759b29516af2d4d8f83723559921a3709eb77a7d2Chia-I Wu                 }
1118f431a3fb4dc1bf860203d79e54657e3a62bc50dfBrian Paul#endif
1119e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	       }
1120e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	    }
1121e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	    else {
11229e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian#if CHAN_BITS != 8
11234003bde6fffc3e5b9e1a115ba952b988dffb099aBrian               USE(general_triangle);
112496385fa15569e25cd0977e678c0ff3bdab6ef316Brian Paul#else
112596385fa15569e25cd0977e678c0ff3bdab6ef316Brian Paul               USE(persp_textured_triangle);
112696385fa15569e25cd0977e678c0ff3bdab6ef316Brian Paul#endif
1127e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	    }
1128e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	 }
1129e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         else {
113031f12f504e61cb2ad65b8890a68eb7154edcb64bBrian Paul            /* general case textured triangles */
11314003bde6fffc3e5b9e1a115ba952b988dffb099aBrian            USE(general_triangle);
1132e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         }
1133e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      }
1134e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      else {
11359e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian         ASSERT(!swrast->_FogEnabled);
1136decc6e2a32ef49e673c081f30e19b8970155d887Brian Paul         ASSERT(!_mesa_need_secondary_color(ctx));
1137e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	 if (ctx->Light.ShadeModel==GL_SMOOTH) {
1138e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	    /* smooth shaded, no texturing, stippled or some raster ops */
11399e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian#if CHAN_BITS != 8
11409e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian               USE(general_triangle);
11419e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian#else
11429e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian               USE(smooth_rgba_triangle);
11439e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian#endif
1144e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	 }
1145e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	 else {
1146e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	    /* flat shaded, no texturing, stippled or some raster ops */
11479e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian#if CHAN_BITS != 8
11489e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian            USE(general_triangle);
11499e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian#else
115097693436a5740fb56c29fcd1cb0a1aa562349902Brian            USE(flat_rgba_triangle);
11519e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian#endif
1152e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	 }
1153e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      }
1154e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
1155e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   else if (ctx->RenderMode==GL_FEEDBACK) {
115645bc887da226403f2c41077e40ca38b6f60f1359Brian Paul      USE(_swrast_feedback_triangle);
1157e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
1158e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   else {
1159e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      /* GL_SELECT mode */
116045bc887da226403f2c41077e40ca38b6f60f1359Brian Paul      USE(_swrast_select_triangle);
1161e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
1162e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
1163