Rename the various function types in t_context.h to include a tnl_ prefix.
[mesa.git] / src / mesa / drivers / dri / unichrome / via_vb_cliptmp.h
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS 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 OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #define CLIP_DOTPROD(K, A, B, C, D) X(K) * A + Y(K) * B + Z(K) * C + W(K) * D
27
28 #define POLY_CLIP(PLANE, A, B, C, D) \
29 do { \
30 if (mask & PLANE) { \
31 GLuint idxPrev = inlist[0]; \
32 GLfloat dpPrev = CLIP_DOTPROD(idxPrev, A, B, C, D); \
33 GLuint outcount = 0; \
34 GLuint i; \
35 \
36 inlist[n] = inlist[0]; /* prevent rotation of vertices */ \
37 for (i = 1; i <= n; i++) { \
38 GLuint idx = inlist[i]; \
39 GLfloat dp = CLIP_DOTPROD(idx, A, B, C, D); \
40 \
41 clipmask[idxPrev] |= PLANE; \
42 if (!NEGATIVE(dpPrev)) { \
43 outlist[outcount++] = idxPrev; \
44 clipmask[idxPrev] &= ~PLANE; \
45 } \
46 \
47 if (DIFFERENT_SIGNS(dp, dpPrev)) { \
48 GLuint newvert = VB->LastClipped++; \
49 VB->ClipMask[newvert] = 0; \
50 outlist[outcount++] = newvert; \
51 if (NEGATIVE(dp)) { \
52 /* Going out of bounds. Avoid division by zero as we \
53 * know dp != dpPrev from DIFFERENT_SIGNS, above. \
54 */ \
55 GLfloat t = dp / (dp - dpPrev); \
56 INTERP_4F(t, coord[newvert], coord[idx], coord[idxPrev]); \
57 interp(ctx, t, newvert, idx, idxPrev, GL_TRUE); \
58 } \
59 else { \
60 /* Coming back in. \
61 */ \
62 GLfloat t = dpPrev / (dpPrev - dp); \
63 INTERP_4F(t, coord[newvert], coord[idxPrev], coord[idx]); \
64 interp(ctx, t, newvert, idxPrev, idx, GL_FALSE); \
65 } \
66 } \
67 \
68 idxPrev = idx; \
69 dpPrev = dp; \
70 } \
71 \
72 if (outcount < 3) \
73 return; \
74 \
75 { \
76 GLuint *tmp = inlist; \
77 inlist = outlist; \
78 outlist = tmp; \
79 n = outcount; \
80 } \
81 } \
82 } while (0)
83
84
85 #define LINE_CLIP(PLANE, A, B, C, D) \
86 do { \
87 if (mask & PLANE) { \
88 GLfloat dpI = CLIP_DOTPROD(ii, A, B, C, D); \
89 GLfloat dpJ = CLIP_DOTPROD(jj, A, B, C, D); \
90 \
91 if (DIFFERENT_SIGNS(dpI, dpJ)) { \
92 GLuint newvert = VB->LastClipped++; \
93 VB->ClipMask[newvert] = 0; \
94 if (NEGATIVE(dpJ)) { \
95 GLfloat t = dpI / (dpI - dpJ); \
96 VB->ClipMask[jj] |= PLANE; \
97 INTERP_4F(t, coord[newvert], coord[ii], coord[jj]); \
98 interp(ctx, t, newvert, ii, jj, GL_FALSE); \
99 jj = newvert; \
100 } \
101 else { \
102 GLfloat t = dpJ / (dpJ - dpI); \
103 VB->ClipMask[ii] |= PLANE; \
104 INTERP_4F(t, coord[newvert], coord[jj], coord[ii]); \
105 interp(ctx, t, newvert, jj, ii, GL_FALSE); \
106 ii = newvert; \
107 } \
108 } \
109 else if (NEGATIVE(dpI)) \
110 return; \
111 } \
112 } while (0)
113
114
115 /* Clip a line against the viewport and user clip planes.
116 */
117 static INLINE void
118 TAG(clip_line)(GLcontext *ctx, GLuint i, GLuint j, GLubyte mask)
119 {
120 TNLcontext *tnl = TNL_CONTEXT(ctx);
121 struct vertex_buffer *VB = &tnl->vb;
122 tnl_interp_func interp = tnl->Driver.Render.Interp;
123 GLfloat (*coord)[4] = VB->ClipPtr->data;
124 GLuint ii = i, jj = j, p;
125 #ifdef DEBUG
126 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
127 #endif
128 #ifdef PERFORMANCE_MEASURE
129 if (VIA_PERFORMANCE) P_M;
130 #endif
131 VB->LastClipped = VB->Count;
132
133 if (mask & 0x3f) {
134 LINE_CLIP(CLIP_RIGHT_BIT, -1, 0, 0, 1);
135 LINE_CLIP(CLIP_LEFT_BIT, 1, 0, 0, 1);
136 LINE_CLIP(CLIP_TOP_BIT, 0, -1, 0, 1);
137 LINE_CLIP(CLIP_BOTTOM_BIT, 0, 1, 0, 1);
138 LINE_CLIP(CLIP_FAR_BIT, 0, 0, -1, 1);
139 LINE_CLIP(CLIP_NEAR_BIT, 0, 0, 1, 1);
140 }
141
142 if (mask & CLIP_USER_BIT) {
143 for (p = 0; p < MAX_CLIP_PLANES; p++) {
144 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
145 GLfloat a = ctx->Transform._ClipUserPlane[p][0];
146 GLfloat b = ctx->Transform._ClipUserPlane[p][1];
147 GLfloat c = ctx->Transform._ClipUserPlane[p][2];
148 GLfloat d = ctx->Transform._ClipUserPlane[p][3];
149 LINE_CLIP(CLIP_USER_BIT, a, b, c, d);
150 }
151 }
152 }
153
154 if ((ctx->_TriangleCaps & DD_FLATSHADE) && j != jj)
155 tnl->Driver.Render.CopyPV(ctx, jj, j);
156
157 tnl->Driver.Render.ClippedLine(ctx, ii, jj);
158 #ifdef DEBUG
159 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
160 #endif
161 }
162
163
164 /* Clip a triangle against the viewport and user clip planes.
165 */
166 static INLINE void
167 TAG(clip_tri)(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLubyte mask)
168 {
169 TNLcontext *tnl = TNL_CONTEXT(ctx);
170 struct vertex_buffer *VB = &tnl->vb;
171 tnl_interp_func interp = tnl->Driver.Render.Interp;
172 GLfloat (*coord)[4] = VB->ClipPtr->data;
173 GLuint pv = v2;
174 GLuint vlist[2][MAX_CLIPPED_VERTICES];
175 GLuint *inlist = vlist[0], *outlist = vlist[1];
176 GLuint p;
177 GLubyte *clipmask = VB->ClipMask;
178 GLuint n = 3;
179
180 #ifdef PERFORMANCE_MEASURE
181 if (VIA_PERFORMANCE) P_M;
182 #endif
183 ASSIGN_3V(inlist, v2, v0, v1); /* pv rotated to slot zero */
184
185 VB->LastClipped = VB->Count;
186
187 if (mask & 0x3f) {
188 POLY_CLIP(CLIP_RIGHT_BIT, -1, 0, 0, 1);
189 POLY_CLIP(CLIP_LEFT_BIT, 1, 0, 0, 1);
190 POLY_CLIP(CLIP_TOP_BIT, 0, -1, 0, 1);
191 POLY_CLIP(CLIP_BOTTOM_BIT, 0, 1, 0, 1);
192 POLY_CLIP(CLIP_FAR_BIT, 0, 0, -1, 1);
193 POLY_CLIP(CLIP_NEAR_BIT, 0, 0, 1, 1);
194 }
195
196 if (mask & CLIP_USER_BIT) {
197 for (p = 0; p < MAX_CLIP_PLANES; p++) {
198 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
199 GLfloat a = ctx->Transform._ClipUserPlane[p][0];
200 GLfloat b = ctx->Transform._ClipUserPlane[p][1];
201 GLfloat c = ctx->Transform._ClipUserPlane[p][2];
202 GLfloat d = ctx->Transform._ClipUserPlane[p][3];
203 POLY_CLIP(CLIP_USER_BIT, a, b, c, d);
204 }
205 }
206 }
207
208 if (ctx->_TriangleCaps & DD_FLATSHADE) {
209 if (pv != inlist[0]) {
210 ASSERT(inlist[0] >= VB->Count);
211 tnl->Driver.Render.CopyPV(ctx, inlist[0], pv);
212 }
213 }
214
215 tnl->Driver.Render.ClippedPolygon(ctx, inlist, n);
216 }
217
218
219 /* Clip a quad against the viewport and user clip planes.
220 */
221 static INLINE void
222 TAG(clip_quad)(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3,
223 GLubyte mask)
224 {
225 TNLcontext *tnl = TNL_CONTEXT(ctx);
226 struct vertex_buffer *VB = &tnl->vb;
227 tnl_interp_func interp = tnl->Driver.Render.Interp;
228 GLfloat (*coord)[4] = VB->ClipPtr->data;
229 GLuint pv = v3;
230 GLuint vlist[2][MAX_CLIPPED_VERTICES];
231 GLuint *inlist = vlist[0], *outlist = vlist[1];
232 GLuint p;
233 GLubyte *clipmask = VB->ClipMask;
234 GLuint n = 4;
235 #ifdef DEBUG
236 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
237 #endif
238 #ifdef PERFORMANCE_MEASURE
239 if (VIA_PERFORMANCE) P_M;
240 #endif
241 ASSIGN_4V(inlist, v3, v0, v1, v2); /* pv rotated to slot zero */
242
243 VB->LastClipped = VB->Count;
244
245 if (mask & 0x3f) {
246 POLY_CLIP(CLIP_RIGHT_BIT, -1, 0, 0, 1);
247 POLY_CLIP(CLIP_LEFT_BIT, 1, 0, 0, 1);
248 POLY_CLIP(CLIP_TOP_BIT, 0, -1, 0, 1);
249 POLY_CLIP(CLIP_BOTTOM_BIT, 0, 1, 0, 1);
250 POLY_CLIP(CLIP_FAR_BIT, 0, 0, -1, 1);
251 POLY_CLIP(CLIP_NEAR_BIT, 0, 0, 1, 1);
252 }
253
254 if (mask & CLIP_USER_BIT) {
255 for (p = 0; p < MAX_CLIP_PLANES; p++) {
256 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
257 GLfloat a = ctx->Transform._ClipUserPlane[p][0];
258 GLfloat b = ctx->Transform._ClipUserPlane[p][1];
259 GLfloat c = ctx->Transform._ClipUserPlane[p][2];
260 GLfloat d = ctx->Transform._ClipUserPlane[p][3];
261 POLY_CLIP(CLIP_USER_BIT, a, b, c, d);
262 }
263 }
264 }
265
266 if (ctx->_TriangleCaps & DD_FLATSHADE) {
267 if (pv != inlist[0]) {
268 ASSERT(inlist[0] >= VB->Count);
269 tnl->Driver.Render.CopyPV(ctx, inlist[0], pv);
270 }
271 }
272
273 tnl->Driver.Render.ClippedPolygon(ctx, inlist, n);
274 #ifdef DEBUG
275 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
276 #endif
277 }
278
279 #undef W
280 #undef Z
281 #undef Y
282 #undef X
283 #undef SIZE
284 #undef TAG
285 #undef POLY_CLIP
286 #undef LINE_CLIP