ss_triangle.c revision fcd7c37fd3d0f61cf6ac81170bc0b3fca64ad9bb
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.1
4 *
5 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 *    Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28#include "glheader.h"
29#include "colormac.h"
30#include "macros.h"
31#include "mtypes.h"
32
33#include "tnl/t_context.h"
34
35#include "ss_triangle.h"
36#include "ss_context.h"
37
38#define SS_RGBA_BIT         0x1
39#define SS_OFFSET_BIT	    0x2
40#define SS_TWOSIDE_BIT	    0x4
41#define SS_UNFILLED_BIT	    0x8
42#define SS_MAX_TRIFUNC      0x10
43
44static tnl_triangle_func tri_tab[SS_MAX_TRIFUNC];
45static tnl_quad_func     quad_tab[SS_MAX_TRIFUNC];
46
47
48static void _swsetup_render_line_tri( GLcontext *ctx,
49				      GLuint e0, GLuint e1, GLuint e2,
50                                      GLuint facing )
51{
52   SScontext *swsetup = SWSETUP_CONTEXT(ctx);
53   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
54   GLubyte *ef = VB->EdgeFlag;
55   SWvertex *verts = swsetup->verts;
56   SWvertex *v0 = &verts[e0];
57   SWvertex *v1 = &verts[e1];
58   SWvertex *v2 = &verts[e2];
59   GLchan c[2][4];
60   GLfloat s[2][4];
61   GLfloat i[2];
62
63   /* cull testing */
64   if (ctx->Polygon.CullFlag) {
65      if (facing == 1 && ctx->Polygon.CullFaceMode != GL_FRONT)
66         return;
67      if (facing == 0 && ctx->Polygon.CullFaceMode != GL_BACK)
68         return;
69   }
70
71   _swrast_SetFacing(ctx, facing);
72
73   if (ctx->Light.ShadeModel == GL_FLAT) {
74      COPY_CHAN4(c[0], v0->color);
75      COPY_CHAN4(c[1], v1->color);
76      COPY_4V(s[0], v0->attrib[FRAG_ATTRIB_COL1]);
77      COPY_4V(s[1], v1->attrib[FRAG_ATTRIB_COL1]);
78      i[0] = v0->attrib[FRAG_ATTRIB_CI][0];
79      i[1] = v1->attrib[FRAG_ATTRIB_CI][0];
80
81      COPY_CHAN4(v0->color, v2->color);
82      COPY_CHAN4(v1->color, v2->color);
83      COPY_4V(v0->attrib[FRAG_ATTRIB_COL1], v2->attrib[FRAG_ATTRIB_COL1]);
84      COPY_4V(v1->attrib[FRAG_ATTRIB_COL1], v2->attrib[FRAG_ATTRIB_COL1]);
85      v0->attrib[FRAG_ATTRIB_CI][0] = v2->attrib[FRAG_ATTRIB_CI][0];
86      v1->attrib[FRAG_ATTRIB_CI][0] = v2->attrib[FRAG_ATTRIB_CI][0];
87   }
88
89   if (swsetup->render_prim == GL_POLYGON) {
90      if (ef[e2]) _swrast_Line( ctx, v2, v0 );
91      if (ef[e0]) _swrast_Line( ctx, v0, v1 );
92      if (ef[e1]) _swrast_Line( ctx, v1, v2 );
93   } else {
94      if (ef[e0]) _swrast_Line( ctx, v0, v1 );
95      if (ef[e1]) _swrast_Line( ctx, v1, v2 );
96      if (ef[e2]) _swrast_Line( ctx, v2, v0 );
97   }
98
99   if (ctx->Light.ShadeModel == GL_FLAT) {
100      COPY_CHAN4(v0->color, c[0]);
101      COPY_CHAN4(v1->color, c[1]);
102      COPY_4V(v0->attrib[FRAG_ATTRIB_COL1], s[0]);
103      COPY_4V(v1->attrib[FRAG_ATTRIB_COL1], s[1]);
104      v0->attrib[FRAG_ATTRIB_CI][0] = i[0];
105      v1->attrib[FRAG_ATTRIB_CI][0] = i[1];
106   }
107}
108
109static void _swsetup_render_point_tri( GLcontext *ctx,
110				       GLuint e0, GLuint e1, GLuint e2,
111                                       GLuint facing )
112{
113   SScontext *swsetup = SWSETUP_CONTEXT(ctx);
114   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
115   GLubyte *ef = VB->EdgeFlag;
116   SWvertex *verts = swsetup->verts;
117   SWvertex *v0 = &verts[e0];
118   SWvertex *v1 = &verts[e1];
119   SWvertex *v2 = &verts[e2];
120   GLchan c[2][4];
121   GLfloat s[2][4];
122   GLfloat i[2];
123
124   /* cull testing */
125   if (ctx->Polygon.CullFlag) {
126      if (facing == 1 && ctx->Polygon.CullFaceMode != GL_FRONT)
127         return;
128      if (facing == 0 && ctx->Polygon.CullFaceMode != GL_BACK)
129         return;
130   }
131
132   _swrast_SetFacing(ctx, facing);
133
134   if (ctx->Light.ShadeModel == GL_FLAT) {
135      /* save colors/indexes for v0, v1 vertices */
136      COPY_CHAN4(c[0], v0->color);
137      COPY_CHAN4(c[1], v1->color);
138      COPY_4V(s[0], v0->attrib[FRAG_ATTRIB_COL1]);
139      COPY_4V(s[1], v1->attrib[FRAG_ATTRIB_COL1]);
140      i[0] = v0->attrib[FRAG_ATTRIB_CI][0];
141      i[1] = v1->attrib[FRAG_ATTRIB_CI][0];
142
143      /* copy v2 color/indexes to v0, v1 indexes */
144      COPY_CHAN4(v0->color, v2->color);
145      COPY_CHAN4(v1->color, v2->color);
146      COPY_4V(v0->attrib[FRAG_ATTRIB_COL1], v2->attrib[FRAG_ATTRIB_COL1]);
147      COPY_4V(v1->attrib[FRAG_ATTRIB_COL1], v2->attrib[FRAG_ATTRIB_COL1]);
148      v0->attrib[FRAG_ATTRIB_CI][0] = v2->attrib[FRAG_ATTRIB_CI][0];
149      v1->attrib[FRAG_ATTRIB_CI][0] = v2->attrib[FRAG_ATTRIB_CI][0];
150   }
151
152   if (ef[e0]) _swrast_Point( ctx, v0 );
153   if (ef[e1]) _swrast_Point( ctx, v1 );
154   if (ef[e2]) _swrast_Point( ctx, v2 );
155
156   if (ctx->Light.ShadeModel == GL_FLAT) {
157      /* restore v0, v1 colores/indexes */
158      COPY_CHAN4(v0->color, c[0]);
159      COPY_CHAN4(v1->color, c[1]);
160      COPY_4V(v0->attrib[FRAG_ATTRIB_COL1], s[0]);
161      COPY_4V(v1->attrib[FRAG_ATTRIB_COL1], s[1]);
162      v0->attrib[FRAG_ATTRIB_CI][0] = i[0];
163      v1->attrib[FRAG_ATTRIB_CI][0] = i[1];
164   }
165   _swrast_flush(ctx);
166}
167
168#define SS_COLOR(a,b) UNCLAMPED_FLOAT_TO_RGBA_CHAN(a,b)
169#define SS_SPEC(a,b) UNCLAMPED_FLOAT_TO_RGB_CHAN(a,b)
170#define SS_IND(a,b) (a = b)
171
172#define IND (0)
173#define TAG(x) x
174#include "ss_tritmp.h"
175
176#define IND (SS_OFFSET_BIT)
177#define TAG(x) x##_offset
178#include "ss_tritmp.h"
179
180#define IND (SS_TWOSIDE_BIT)
181#define TAG(x) x##_twoside
182#include "ss_tritmp.h"
183
184#define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT)
185#define TAG(x) x##_offset_twoside
186#include "ss_tritmp.h"
187
188#define IND (SS_UNFILLED_BIT)
189#define TAG(x) x##_unfilled
190#include "ss_tritmp.h"
191
192#define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT)
193#define TAG(x) x##_offset_unfilled
194#include "ss_tritmp.h"
195
196#define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
197#define TAG(x) x##_twoside_unfilled
198#include "ss_tritmp.h"
199
200#define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
201#define TAG(x) x##_offset_twoside_unfilled
202#include "ss_tritmp.h"
203
204#define IND (0|SS_RGBA_BIT)
205#define TAG(x) x##_rgba
206#include "ss_tritmp.h"
207
208#define IND (SS_OFFSET_BIT|SS_RGBA_BIT)
209#define TAG(x) x##_offset_rgba
210#include "ss_tritmp.h"
211
212#define IND (SS_TWOSIDE_BIT|SS_RGBA_BIT)
213#define TAG(x) x##_twoside_rgba
214#include "ss_tritmp.h"
215
216#define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_RGBA_BIT)
217#define TAG(x) x##_offset_twoside_rgba
218#include "ss_tritmp.h"
219
220#define IND (SS_UNFILLED_BIT|SS_RGBA_BIT)
221#define TAG(x) x##_unfilled_rgba
222#include "ss_tritmp.h"
223
224#define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
225#define TAG(x) x##_offset_unfilled_rgba
226#include "ss_tritmp.h"
227
228#define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
229#define TAG(x) x##_twoside_unfilled_rgba
230#include "ss_tritmp.h"
231
232#define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
233#define TAG(x) x##_offset_twoside_unfilled_rgba
234#include "ss_tritmp.h"
235
236
237void _swsetup_trifuncs_init( GLcontext *ctx )
238{
239   (void) ctx;
240
241   init();
242   init_offset();
243   init_twoside();
244   init_offset_twoside();
245   init_unfilled();
246   init_offset_unfilled();
247   init_twoside_unfilled();
248   init_offset_twoside_unfilled();
249
250   init_rgba();
251   init_offset_rgba();
252   init_twoside_rgba();
253   init_offset_twoside_rgba();
254   init_unfilled_rgba();
255   init_offset_unfilled_rgba();
256   init_twoside_unfilled_rgba();
257   init_offset_twoside_unfilled_rgba();
258}
259
260
261static void swsetup_points( GLcontext *ctx, GLuint first, GLuint last )
262{
263   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
264   SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
265   GLuint i;
266
267   if (VB->Elts) {
268      for (i = first; i < last; i++)
269	 if (VB->ClipMask[VB->Elts[i]] == 0)
270	    _swrast_Point( ctx, &verts[VB->Elts[i]] );
271   }
272   else {
273      for (i = first; i < last; i++)
274	 if (VB->ClipMask[i] == 0)
275	    _swrast_Point( ctx, &verts[i] );
276   }
277}
278
279static void swsetup_line( GLcontext *ctx, GLuint v0, GLuint v1 )
280{
281   SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
282   _swrast_Line( ctx, &verts[v0], &verts[v1] );
283}
284
285
286
287void _swsetup_choose_trifuncs( GLcontext *ctx )
288{
289   TNLcontext *tnl = TNL_CONTEXT(ctx);
290   GLuint ind = 0;
291
292   if (ctx->Polygon.OffsetPoint ||
293       ctx->Polygon.OffsetLine ||
294       ctx->Polygon.OffsetFill)
295      ind |= SS_OFFSET_BIT;
296
297   /* Note: gl_FrontFacing lives in fragment input FOGC.Y at this time */
298   if ((ctx->Light.Enabled && ctx->Light.Model.TwoSide) ||
299       (ctx->VertexProgram._Enabled && ctx->VertexProgram.TwoSideEnabled) ||
300       (ctx->FragmentProgram._Current && ctx->FragmentProgram._Current->Base.InputsRead & (1 << FRAG_ATTRIB_FOGC)))
301      ind |= SS_TWOSIDE_BIT;
302
303   /* We piggyback the two-sided stencil front/back determination on the
304    * unfilled triangle path.
305    */
306   if (ctx->Polygon.FrontMode != GL_FILL ||
307       ctx->Polygon.BackMode != GL_FILL ||
308       (ctx->Stencil.Enabled && ctx->Stencil._TestTwoSide))
309      ind |= SS_UNFILLED_BIT;
310
311   if (ctx->Visual.rgbMode)
312      ind |= SS_RGBA_BIT;
313
314   tnl->Driver.Render.Triangle = tri_tab[ind];
315   tnl->Driver.Render.Quad = quad_tab[ind];
316   tnl->Driver.Render.Line = swsetup_line;
317   tnl->Driver.Render.Points = swsetup_points;
318}
319