Merge branch 'master' of git+ssh://michal@git.freedesktop.org/git/mesa/mesa into...
[mesa.git] / src / mesa / swrast_setup / ss_tritmp.h
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
29 /**
30 * This is where we handle assigning vertex colors based on front/back
31 * facing, compute polygon offset and handle glPolygonMode().
32 */
33 static void TAG(triangle)(GLcontext *ctx, GLuint e0, GLuint e1, GLuint e2 )
34 {
35 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
36 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
37 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
38 SWvertex *v[3];
39 GLfloat z[3];
40 GLfloat offset;
41 GLenum mode = GL_FILL;
42 GLuint facing = 0;
43 GLchan saved_color[3][4];
44 GLfloat saved_col0[3][4];
45 GLfloat saved_spec[3][4];
46 GLfloat saved_index[3];
47
48 v[0] = &verts[e0];
49 v[1] = &verts[e1];
50 v[2] = &verts[e2];
51
52
53 if (IND & (SS_TWOSIDE_BIT | SS_OFFSET_BIT | SS_UNFILLED_BIT))
54 {
55 GLfloat ex = v[0]->attrib[FRAG_ATTRIB_WPOS][0] - v[2]->attrib[FRAG_ATTRIB_WPOS][0];
56 GLfloat ey = v[0]->attrib[FRAG_ATTRIB_WPOS][1] - v[2]->attrib[FRAG_ATTRIB_WPOS][1];
57 GLfloat fx = v[1]->attrib[FRAG_ATTRIB_WPOS][0] - v[2]->attrib[FRAG_ATTRIB_WPOS][0];
58 GLfloat fy = v[1]->attrib[FRAG_ATTRIB_WPOS][1] - v[2]->attrib[FRAG_ATTRIB_WPOS][1];
59 GLfloat cc = ex*fy - ey*fx;
60
61 if (IND & (SS_TWOSIDE_BIT | SS_UNFILLED_BIT))
62 {
63 facing = (cc < 0.0) ^ ctx->Polygon._FrontBit;
64 ctx->_Facing = facing;
65
66 if (IND & SS_UNFILLED_BIT)
67 mode = facing ? ctx->Polygon.BackMode : ctx->Polygon.FrontMode;
68
69 if (facing == 1) {
70 if (IND & SS_TWOSIDE_BIT) {
71 if (IND & SS_RGBA_BIT) {
72 if (VB->ColorPtr[1]) {
73 GLfloat (*vbcolor)[4] = VB->ColorPtr[1]->data;
74
75 if (swsetup->intColors) {
76 COPY_CHAN4(saved_color[0], v[0]->color);
77 COPY_CHAN4(saved_color[1], v[1]->color);
78 COPY_CHAN4(saved_color[2], v[2]->color);
79 }
80 else {
81 COPY_4V(saved_col0[0], v[0]->attrib[FRAG_ATTRIB_COL0]);
82 COPY_4V(saved_col0[1], v[1]->attrib[FRAG_ATTRIB_COL0]);
83 COPY_4V(saved_col0[2], v[2]->attrib[FRAG_ATTRIB_COL0]);
84 }
85
86 if (VB->ColorPtr[1]->stride) {
87 if (swsetup->intColors) {
88 SS_COLOR(v[0]->color, vbcolor[e0]);
89 SS_COLOR(v[1]->color, vbcolor[e1]);
90 SS_COLOR(v[2]->color, vbcolor[e2]);
91 }
92 else {
93 COPY_4V(v[0]->attrib[FRAG_ATTRIB_COL0], vbcolor[e0]);
94 COPY_4V(v[1]->attrib[FRAG_ATTRIB_COL0], vbcolor[e1]);
95 COPY_4V(v[2]->attrib[FRAG_ATTRIB_COL0], vbcolor[e2]);
96 }
97 }
98 else {
99 /* flat shade */
100 if (swsetup->intColors) {
101 SS_COLOR(v[0]->color, vbcolor[0]);
102 SS_COLOR(v[1]->color, vbcolor[0]);
103 SS_COLOR(v[2]->color, vbcolor[0]);
104 }
105 else {
106 COPY_4V(v[0]->attrib[FRAG_ATTRIB_COL0], vbcolor[0]);
107 COPY_4V(v[1]->attrib[FRAG_ATTRIB_COL0], vbcolor[0]);
108 COPY_4V(v[2]->attrib[FRAG_ATTRIB_COL0], vbcolor[0]);
109 }
110 }
111 }
112
113 if (VB->SecondaryColorPtr[1]) {
114 GLfloat (*vbspec)[4] = VB->SecondaryColorPtr[1]->data;
115
116 COPY_4V(saved_spec[0], v[0]->attrib[FRAG_ATTRIB_COL1]);
117 COPY_4V(saved_spec[1], v[1]->attrib[FRAG_ATTRIB_COL1]);
118 COPY_4V(saved_spec[2], v[2]->attrib[FRAG_ATTRIB_COL1]);
119
120 if (VB->SecondaryColorPtr[1]->stride) {
121 SS_SPEC(v[0]->attrib[FRAG_ATTRIB_COL1], vbspec[e0]);
122 SS_SPEC(v[1]->attrib[FRAG_ATTRIB_COL1], vbspec[e1]);
123 SS_SPEC(v[2]->attrib[FRAG_ATTRIB_COL1], vbspec[e2]);
124 }
125 else {
126 SS_SPEC(v[0]->attrib[FRAG_ATTRIB_COL1], vbspec[0]);
127 SS_SPEC(v[1]->attrib[FRAG_ATTRIB_COL1], vbspec[0]);
128 SS_SPEC(v[2]->attrib[FRAG_ATTRIB_COL1], vbspec[0]);
129 }
130 }
131 } else {
132 GLfloat *vbindex = (GLfloat *)VB->IndexPtr[1]->data;
133 saved_index[0] = v[0]->attrib[FRAG_ATTRIB_CI][0];
134 saved_index[1] = v[1]->attrib[FRAG_ATTRIB_CI][0];
135 saved_index[2] = v[2]->attrib[FRAG_ATTRIB_CI][0];
136
137 SS_IND(v[0]->attrib[FRAG_ATTRIB_CI][0], (GLuint) vbindex[e0]);
138 SS_IND(v[1]->attrib[FRAG_ATTRIB_CI][0], (GLuint) vbindex[e1]);
139 SS_IND(v[2]->attrib[FRAG_ATTRIB_CI][0], (GLuint) vbindex[e2]);
140 }
141 }
142 }
143 }
144
145 if (IND & SS_OFFSET_BIT)
146 {
147 offset = ctx->Polygon.OffsetUnits * ctx->DrawBuffer->_MRD;
148 z[0] = v[0]->attrib[FRAG_ATTRIB_WPOS][2];
149 z[1] = v[1]->attrib[FRAG_ATTRIB_WPOS][2];
150 z[2] = v[2]->attrib[FRAG_ATTRIB_WPOS][2];
151 if (cc * cc > 1e-16) {
152 const GLfloat ez = z[0] - z[2];
153 const GLfloat fz = z[1] - z[2];
154 const GLfloat oneOverArea = 1.0F / cc;
155 const GLfloat dzdx = FABSF((ey * fz - ez * fy) * oneOverArea);
156 const GLfloat dzdy = FABSF((ez * fx - ex * fz) * oneOverArea);
157 offset += MAX2(dzdx, dzdy) * ctx->Polygon.OffsetFactor;
158 /* Unfortunately, we need to clamp to prevent negative Zs below.
159 * Technically, we should do the clamping per-fragment.
160 */
161 offset = MAX2(offset, -v[0]->attrib[FRAG_ATTRIB_WPOS][2]);
162 offset = MAX2(offset, -v[1]->attrib[FRAG_ATTRIB_WPOS][2]);
163 offset = MAX2(offset, -v[2]->attrib[FRAG_ATTRIB_WPOS][2]);
164 }
165 }
166 }
167
168 if (mode == GL_POINT) {
169 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetPoint) {
170 v[0]->attrib[FRAG_ATTRIB_WPOS][2] += offset;
171 v[1]->attrib[FRAG_ATTRIB_WPOS][2] += offset;
172 v[2]->attrib[FRAG_ATTRIB_WPOS][2] += offset;
173 }
174 _swsetup_render_point_tri( ctx, e0, e1, e2, facing );
175 } else if (mode == GL_LINE) {
176 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetLine) {
177 v[0]->attrib[FRAG_ATTRIB_WPOS][2] += offset;
178 v[1]->attrib[FRAG_ATTRIB_WPOS][2] += offset;
179 v[2]->attrib[FRAG_ATTRIB_WPOS][2] += offset;
180 }
181 _swsetup_render_line_tri( ctx, e0, e1, e2, facing );
182 } else {
183 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetFill) {
184 v[0]->attrib[FRAG_ATTRIB_WPOS][2] += offset;
185 v[1]->attrib[FRAG_ATTRIB_WPOS][2] += offset;
186 v[2]->attrib[FRAG_ATTRIB_WPOS][2] += offset;
187 }
188 _swrast_Triangle( ctx, v[0], v[1], v[2] );
189 }
190
191 /*
192 * Restore original vertex colors, etc.
193 */
194 if (IND & SS_OFFSET_BIT) {
195 v[0]->attrib[FRAG_ATTRIB_WPOS][2] = z[0];
196 v[1]->attrib[FRAG_ATTRIB_WPOS][2] = z[1];
197 v[2]->attrib[FRAG_ATTRIB_WPOS][2] = z[2];
198 }
199
200 if (IND & SS_TWOSIDE_BIT) {
201 if (facing == 1) {
202 if (IND & SS_RGBA_BIT) {
203 if (VB->ColorPtr[1]) {
204 if (swsetup->intColors) {
205 COPY_CHAN4(v[0]->color, saved_color[0]);
206 COPY_CHAN4(v[1]->color, saved_color[1]);
207 COPY_CHAN4(v[2]->color, saved_color[2]);
208 }
209 else {
210 COPY_4V(v[0]->attrib[FRAG_ATTRIB_COL0], saved_col0[0]);
211 COPY_4V(v[1]->attrib[FRAG_ATTRIB_COL0], saved_col0[1]);
212 COPY_4V(v[2]->attrib[FRAG_ATTRIB_COL0], saved_col0[2]);
213 }
214 }
215
216 if (VB->SecondaryColorPtr[1]) {
217 COPY_4V(v[0]->attrib[FRAG_ATTRIB_COL1], saved_spec[0]);
218 COPY_4V(v[1]->attrib[FRAG_ATTRIB_COL1], saved_spec[1]);
219 COPY_4V(v[2]->attrib[FRAG_ATTRIB_COL1], saved_spec[2]);
220 }
221 } else {
222 v[0]->attrib[FRAG_ATTRIB_CI][0] = saved_index[0];
223 v[1]->attrib[FRAG_ATTRIB_CI][0] = saved_index[1];
224 v[2]->attrib[FRAG_ATTRIB_CI][0] = saved_index[2];
225 }
226 }
227 }
228 }
229
230
231
232 /* Need to fixup edgeflags when decomposing to triangles:
233 */
234 static void TAG(quadfunc)( GLcontext *ctx, GLuint v0,
235 GLuint v1, GLuint v2, GLuint v3 )
236 {
237 if (IND & SS_UNFILLED_BIT) {
238 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
239 GLubyte ef1 = VB->EdgeFlag[v1];
240 GLubyte ef3 = VB->EdgeFlag[v3];
241 VB->EdgeFlag[v1] = 0;
242 TAG(triangle)( ctx, v0, v1, v3 );
243 VB->EdgeFlag[v1] = ef1;
244 VB->EdgeFlag[v3] = 0;
245 TAG(triangle)( ctx, v1, v2, v3 );
246 VB->EdgeFlag[v3] = ef3;
247 } else {
248 TAG(triangle)( ctx, v0, v1, v3 );
249 TAG(triangle)( ctx, v1, v2, v3 );
250 }
251 }
252
253
254
255
256 static void TAG(init)( void )
257 {
258 tri_tab[IND] = TAG(triangle);
259 quad_tab[IND] = TAG(quadfunc);
260 }
261
262
263 #undef IND
264 #undef TAG