[MESA]
[reactos.git] / reactos / dll / opengl / mesa / 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)(struct gl_context *ctx, GLuint e0, GLuint e1, GLuint e2 )
34 {
35 #if IND & SS_TWOSIDE_BIT
36 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
37 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
38 GLchan saved_color[3][4] = { { 0 } };
39 GLfloat saved_col0[3][4] = { { 0 } };
40 GLfloat saved_spec[3][4] = { { 0 } };
41 #endif
42 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
43 SWvertex *v[3];
44 #if IND & SS_OFFSET_BIT
45 GLfloat z[3];
46 #endif
47 GLfloat oz0, oz1, oz2;
48 GLenum mode = GL_FILL;
49 GLuint facing = 0;
50
51 v[0] = &verts[e0];
52 v[1] = &verts[e1];
53 v[2] = &verts[e2];
54
55 #if IND & (SS_TWOSIDE_BIT | SS_OFFSET_BIT | SS_UNFILLED_BIT)
56 {
57 GLfloat ex = v[0]->attrib[FRAG_ATTRIB_WPOS][0] - v[2]->attrib[FRAG_ATTRIB_WPOS][0];
58 GLfloat ey = v[0]->attrib[FRAG_ATTRIB_WPOS][1] - v[2]->attrib[FRAG_ATTRIB_WPOS][1];
59 GLfloat fx = v[1]->attrib[FRAG_ATTRIB_WPOS][0] - v[2]->attrib[FRAG_ATTRIB_WPOS][0];
60 GLfloat fy = v[1]->attrib[FRAG_ATTRIB_WPOS][1] - v[2]->attrib[FRAG_ATTRIB_WPOS][1];
61 GLfloat cc = ex*fy - ey*fx;
62
63 #if IND & (SS_TWOSIDE_BIT | SS_UNFILLED_BIT)
64 {
65 facing = (cc < 0.0) ^ ctx->Polygon._FrontBit;
66
67 #if IND & SS_UNFILLED_BIT
68 mode = facing ? ctx->Polygon.BackMode : ctx->Polygon.FrontMode;
69 #endif
70
71 #if IND & SS_TWOSIDE_BIT
72 if (facing == 1) {
73 if (VB->BackfaceColorPtr) {
74 GLfloat (*vbcolor)[4] = VB->BackfaceColorPtr->data;
75
76 if (swsetup->intColors) {
77 COPY_CHAN4(saved_color[0], v[0]->color);
78 COPY_CHAN4(saved_color[1], v[1]->color);
79 COPY_CHAN4(saved_color[2], v[2]->color);
80 }
81 else {
82 COPY_4V(saved_col0[0], v[0]->attrib[FRAG_ATTRIB_COL0]);
83 COPY_4V(saved_col0[1], v[1]->attrib[FRAG_ATTRIB_COL0]);
84 COPY_4V(saved_col0[2], v[2]->attrib[FRAG_ATTRIB_COL0]);
85 }
86
87 if (VB->BackfaceColorPtr->stride) {
88 if (swsetup->intColors) {
89 SS_COLOR(v[0]->color, vbcolor[e0]);
90 SS_COLOR(v[1]->color, vbcolor[e1]);
91 SS_COLOR(v[2]->color, vbcolor[e2]);
92 }
93 else {
94 COPY_4V(v[0]->attrib[FRAG_ATTRIB_COL0], vbcolor[e0]);
95 COPY_4V(v[1]->attrib[FRAG_ATTRIB_COL0], vbcolor[e1]);
96 COPY_4V(v[2]->attrib[FRAG_ATTRIB_COL0], vbcolor[e2]);
97 }
98 }
99 else {
100 /* flat shade */
101 if (swsetup->intColors) {
102 SS_COLOR(v[0]->color, vbcolor[0]);
103 SS_COLOR(v[1]->color, vbcolor[0]);
104 SS_COLOR(v[2]->color, vbcolor[0]);
105 }
106 else {
107 COPY_4V(v[0]->attrib[FRAG_ATTRIB_COL0], vbcolor[0]);
108 COPY_4V(v[1]->attrib[FRAG_ATTRIB_COL0], vbcolor[0]);
109 COPY_4V(v[2]->attrib[FRAG_ATTRIB_COL0], vbcolor[0]);
110 }
111 }
112 }
113
114 if (VB->BackfaceSecondaryColorPtr) {
115 GLfloat (*vbspec)[4] = VB->BackfaceSecondaryColorPtr->data;
116
117 COPY_4V(saved_spec[0], v[0]->attrib[FRAG_ATTRIB_COL1]);
118 COPY_4V(saved_spec[1], v[1]->attrib[FRAG_ATTRIB_COL1]);
119 COPY_4V(saved_spec[2], v[2]->attrib[FRAG_ATTRIB_COL1]);
120
121 if (VB->BackfaceSecondaryColorPtr->stride) {
122 SS_SPEC(v[0]->attrib[FRAG_ATTRIB_COL1], vbspec[e0]);
123 SS_SPEC(v[1]->attrib[FRAG_ATTRIB_COL1], vbspec[e1]);
124 SS_SPEC(v[2]->attrib[FRAG_ATTRIB_COL1], vbspec[e2]);
125 }
126 else {
127 SS_SPEC(v[0]->attrib[FRAG_ATTRIB_COL1], vbspec[0]);
128 SS_SPEC(v[1]->attrib[FRAG_ATTRIB_COL1], vbspec[0]);
129 SS_SPEC(v[2]->attrib[FRAG_ATTRIB_COL1], vbspec[0]);
130 }
131 }
132 }
133 #endif
134 }
135 #endif
136
137 #if IND & SS_OFFSET_BIT
138 {
139 GLfloat offset;
140 const GLfloat max = ctx->DrawBuffer->_DepthMaxF;
141 /* save original Z values (restored later) */
142 z[0] = v[0]->attrib[FRAG_ATTRIB_WPOS][2];
143 z[1] = v[1]->attrib[FRAG_ATTRIB_WPOS][2];
144 z[2] = v[2]->attrib[FRAG_ATTRIB_WPOS][2];
145 /* Note that Z values are already scaled to [0,65535] (for example)
146 * so no MRD value is used here.
147 */
148 offset = ctx->Polygon.OffsetUnits;
149 if (cc * cc > 1e-16) {
150 const GLfloat ez = z[0] - z[2];
151 const GLfloat fz = z[1] - z[2];
152 const GLfloat oneOverArea = 1.0F / cc;
153 const GLfloat dzdx = FABSF((ey * fz - ez * fy) * oneOverArea);
154 const GLfloat dzdy = FABSF((ez * fx - ex * fz) * oneOverArea);
155 offset += MAX2(dzdx, dzdy) * ctx->Polygon.OffsetFactor;
156 }
157 /* new Z values */
158 oz0 = CLAMP(v[0]->attrib[FRAG_ATTRIB_WPOS][2] + offset, 0.0F, max);
159 oz1 = CLAMP(v[1]->attrib[FRAG_ATTRIB_WPOS][2] + offset, 0.0F, max);
160 oz2 = CLAMP(v[2]->attrib[FRAG_ATTRIB_WPOS][2] + offset, 0.0F, max);
161 }
162 #endif
163 }
164 #endif
165
166 if (mode == GL_POINT) {
167 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetPoint) {
168 v[0]->attrib[FRAG_ATTRIB_WPOS][2] = oz0;
169 v[1]->attrib[FRAG_ATTRIB_WPOS][2] = oz1;
170 v[2]->attrib[FRAG_ATTRIB_WPOS][2] = oz2;
171 }
172 _swsetup_render_tri(ctx, e0, e1, e2, facing, _swsetup_edge_render_point_tri);
173 } else if (mode == GL_LINE) {
174 if ((IND & SS_OFFSET_BIT) && ctx->Polygon.OffsetLine) {
175 v[0]->attrib[FRAG_ATTRIB_WPOS][2] = oz0;
176 v[1]->attrib[FRAG_ATTRIB_WPOS][2] = oz1;
177 v[2]->attrib[FRAG_ATTRIB_WPOS][2] = oz2;
178 }
179 _swsetup_render_tri(ctx, e0, e1, e2, facing, _swsetup_edge_render_line_tri);
180 }
181 else {
182 #if IND & SS_OFFSET_BIT
183 if (ctx->Polygon.OffsetFill) {
184 v[0]->attrib[FRAG_ATTRIB_WPOS][2] = oz0;
185 v[1]->attrib[FRAG_ATTRIB_WPOS][2] = oz1;
186 v[2]->attrib[FRAG_ATTRIB_WPOS][2] = oz2;
187 }
188 #endif
189 _swrast_Triangle( ctx, v[0], v[1], v[2] );
190 }
191
192 /*
193 * Restore original vertex colors, etc.
194 */
195 #if IND & SS_OFFSET_BIT
196 {
197 v[0]->attrib[FRAG_ATTRIB_WPOS][2] = z[0];
198 v[1]->attrib[FRAG_ATTRIB_WPOS][2] = z[1];
199 v[2]->attrib[FRAG_ATTRIB_WPOS][2] = z[2];
200 }
201 #endif
202
203 #if IND & SS_TWOSIDE_BIT
204 {
205 if (facing == 1) {
206 if (VB->BackfaceColorPtr) {
207 if (swsetup->intColors) {
208 COPY_CHAN4(v[0]->color, saved_color[0]);
209 COPY_CHAN4(v[1]->color, saved_color[1]);
210 COPY_CHAN4(v[2]->color, saved_color[2]);
211 }
212 else {
213 COPY_4V(v[0]->attrib[FRAG_ATTRIB_COL0], saved_col0[0]);
214 COPY_4V(v[1]->attrib[FRAG_ATTRIB_COL0], saved_col0[1]);
215 COPY_4V(v[2]->attrib[FRAG_ATTRIB_COL0], saved_col0[2]);
216 }
217 }
218
219 if (VB->BackfaceSecondaryColorPtr) {
220 COPY_4V(v[0]->attrib[FRAG_ATTRIB_COL1], saved_spec[0]);
221 COPY_4V(v[1]->attrib[FRAG_ATTRIB_COL1], saved_spec[1]);
222 COPY_4V(v[2]->attrib[FRAG_ATTRIB_COL1], saved_spec[2]);
223 }
224 }
225 }
226 #endif
227 }
228
229
230
231 /* Need to fixup edgeflags when decomposing to triangles:
232 */
233 static void TAG(quadfunc)( struct gl_context *ctx, GLuint v0,
234 GLuint v1, GLuint v2, GLuint v3 )
235 {
236 if (IND & SS_UNFILLED_BIT) {
237 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
238 if (VB->EdgeFlag) { /* XXX this test shouldn't be needed (bug 12614) */
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 }
248 } else {
249 TAG(triangle)( ctx, v0, v1, v3 );
250 TAG(triangle)( ctx, v1, v2, v3 );
251 }
252 }
253
254
255
256
257 static void TAG(init)( void )
258 {
259 tri_tab[IND] = TAG(triangle);
260 quad_tab[IND] = TAG(quadfunc);
261 }
262
263
264 #undef IND
265 #undef TAG