2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keithw@vmware.com>
29 #include "main/glheader.h"
30 #include "main/macros.h"
31 #include "main/mtypes.h"
32 #include "main/stencil.h"
34 #include "tnl/t_context.h"
36 #include "ss_triangle.h"
37 #include "ss_context.h"
39 #define SS_OFFSET_BIT 0x1
40 #define SS_TWOSIDE_BIT 0x2
41 #define SS_UNFILLED_BIT 0x4
42 #define SS_MAX_TRIFUNC 0x8
44 static tnl_triangle_func tri_tab
[SS_MAX_TRIFUNC
];
45 static tnl_quad_func quad_tab
[SS_MAX_TRIFUNC
];
49 * Render a triangle respecting edge flags.
51 typedef void (* swsetup_edge_render_prim_tri
)(struct gl_context
*ctx
,
61 * Render a triangle using lines and respecting edge flags.
64 _swsetup_edge_render_line_tri(struct gl_context
*ctx
,
73 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
75 if (swsetup
->render_prim
== GL_POLYGON
) {
76 if (ef
[e2
]) _swrast_Line( ctx
, v2
, v0
);
77 if (ef
[e0
]) _swrast_Line( ctx
, v0
, v1
);
78 if (ef
[e1
]) _swrast_Line( ctx
, v1
, v2
);
80 if (ef
[e0
]) _swrast_Line( ctx
, v0
, v1
);
81 if (ef
[e1
]) _swrast_Line( ctx
, v1
, v2
);
82 if (ef
[e2
]) _swrast_Line( ctx
, v2
, v0
);
87 * Render a triangle using points and respecting edge flags.
90 _swsetup_edge_render_point_tri(struct gl_context
*ctx
,
99 if (ef
[e0
]) _swrast_Point( ctx
, v0
);
100 if (ef
[e1
]) _swrast_Point( ctx
, v1
);
101 if (ef
[e2
]) _swrast_Point( ctx
, v2
);
107 * Render a triangle respecting cull and shade model.
109 static void _swsetup_render_tri(struct gl_context
*ctx
,
114 swsetup_edge_render_prim_tri render
)
116 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
117 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
118 GLubyte
*ef
= VB
->EdgeFlag
;
119 SWvertex
*verts
= swsetup
->verts
;
120 SWvertex
*v0
= &verts
[e0
];
121 SWvertex
*v1
= &verts
[e1
];
122 SWvertex
*v2
= &verts
[e2
];
125 if (ctx
->Polygon
.CullFlag
) {
126 if (facing
== 1 && ctx
->Polygon
.CullFaceMode
!= GL_FRONT
)
128 if (facing
== 0 && ctx
->Polygon
.CullFaceMode
!= GL_BACK
)
132 _swrast_SetFacing(ctx
, facing
);
134 if (ctx
->Light
.ShadeModel
== GL_FLAT
) {
138 /* save colors/indexes for v0, v1 vertices */
139 COPY_CHAN4(c
[0], v0
->color
);
140 COPY_CHAN4(c
[1], v1
->color
);
141 COPY_4V(s
[0], v0
->attrib
[VARYING_SLOT_COL1
]);
142 COPY_4V(s
[1], v1
->attrib
[VARYING_SLOT_COL1
]);
144 /* copy v2 color/indexes to v0, v1 indexes */
145 COPY_CHAN4(v0
->color
, v2
->color
);
146 COPY_CHAN4(v1
->color
, v2
->color
);
147 COPY_4V(v0
->attrib
[VARYING_SLOT_COL1
], v2
->attrib
[VARYING_SLOT_COL1
]);
148 COPY_4V(v1
->attrib
[VARYING_SLOT_COL1
], v2
->attrib
[VARYING_SLOT_COL1
]);
150 render(ctx
, ef
, e0
, e1
, e2
, v0
, v1
, v2
);
152 COPY_CHAN4(v0
->color
, c
[0]);
153 COPY_CHAN4(v1
->color
, c
[1]);
154 COPY_4V(v0
->attrib
[VARYING_SLOT_COL1
], s
[0]);
155 COPY_4V(v1
->attrib
[VARYING_SLOT_COL1
], s
[1]);
158 render(ctx
, ef
, e0
, e1
, e2
, v0
, v1
, v2
);
162 #define SS_COLOR(a,b) UNCLAMPED_FLOAT_TO_RGBA_CHAN(a,b)
163 #define SS_SPEC(a,b) COPY_4V(a,b)
164 #define SS_IND(a,b) (a = b)
167 #define TAG(x) x##_rgba
168 #include "ss_tritmp.h"
170 #define IND (SS_OFFSET_BIT)
171 #define TAG(x) x##_offset_rgba
172 #include "ss_tritmp.h"
174 #define IND (SS_TWOSIDE_BIT)
175 #define TAG(x) x##_twoside_rgba
176 #include "ss_tritmp.h"
178 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT)
179 #define TAG(x) x##_offset_twoside_rgba
180 #include "ss_tritmp.h"
182 #define IND (SS_UNFILLED_BIT)
183 #define TAG(x) x##_unfilled_rgba
184 #include "ss_tritmp.h"
186 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT)
187 #define TAG(x) x##_offset_unfilled_rgba
188 #include "ss_tritmp.h"
190 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
191 #define TAG(x) x##_twoside_unfilled_rgba
192 #include "ss_tritmp.h"
194 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
195 #define TAG(x) x##_offset_twoside_unfilled_rgba
196 #include "ss_tritmp.h"
199 void _swsetup_trifuncs_init( struct gl_context
*ctx
)
206 init_offset_twoside_rgba();
207 init_unfilled_rgba();
208 init_offset_unfilled_rgba();
209 init_twoside_unfilled_rgba();
210 init_offset_twoside_unfilled_rgba();
214 static void swsetup_points( struct gl_context
*ctx
, GLuint first
, GLuint last
)
216 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
217 SWvertex
*verts
= SWSETUP_CONTEXT(ctx
)->verts
;
221 for (i
= first
; i
< last
; i
++)
222 if (VB
->ClipMask
[VB
->Elts
[i
]] == 0)
223 _swrast_Point( ctx
, &verts
[VB
->Elts
[i
]] );
226 for (i
= first
; i
< last
; i
++)
227 if (VB
->ClipMask
[i
] == 0)
228 _swrast_Point( ctx
, &verts
[i
] );
232 static void swsetup_line( struct gl_context
*ctx
, GLuint v0
, GLuint v1
)
234 SWvertex
*verts
= SWSETUP_CONTEXT(ctx
)->verts
;
235 _swrast_Line( ctx
, &verts
[v0
], &verts
[v1
] );
240 void _swsetup_choose_trifuncs( struct gl_context
*ctx
)
242 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
245 if (ctx
->Polygon
.OffsetPoint
||
246 ctx
->Polygon
.OffsetLine
||
247 ctx
->Polygon
.OffsetFill
)
248 ind
|= SS_OFFSET_BIT
;
250 if ((ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
) ||
251 (ctx
->VertexProgram
._Current
&& ctx
->VertexProgram
.TwoSideEnabled
))
252 ind
|= SS_TWOSIDE_BIT
;
254 /* We piggyback the two-sided stencil front/back determination on the
255 * unfilled triangle path.
257 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
258 ctx
->Polygon
.BackMode
!= GL_FILL
||
259 (ctx
->Stencil
.Enabled
&& _mesa_stencil_is_two_sided(ctx
)))
260 ind
|= SS_UNFILLED_BIT
;
262 tnl
->Driver
.Render
.Triangle
= tri_tab
[ind
];
263 tnl
->Driver
.Render
.Quad
= quad_tab
[ind
];
264 tnl
->Driver
.Render
.Line
= swsetup_line
;
265 tnl
->Driver
.Render
.Points
= swsetup_points
;