Fixed off by one errors in clipping.
[mesa.git] / src / mesa / drivers / dri / unichrome / via_render.c
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 /*
27 * Render unclipped vertex buffers by emitting vertices directly to
28 * dma buffers. Use strip/fan hardware acceleration where possible.
29 *
30 */
31 #include "glheader.h"
32 #include "context.h"
33 #include "macros.h"
34 #include "mtypes.h"
35
36 #include "tnl/t_context.h"
37
38 #include "via_context.h"
39 #include "via_tris.h"
40 #include "via_state.h"
41 #include "via_vb.h"
42 #include "via_ioctl.h"
43
44 /*
45 * Render unclipped vertex buffers by emitting vertices directly to
46 * dma buffers. Use strip/fan hardware primitives where possible.
47 * Try to simulate missing primitives with indexed vertices.
48 */
49 #define HAVE_POINTS 1
50 #define HAVE_LINES 1
51 #define HAVE_LINE_STRIPS 1
52 #define HAVE_LINE_LOOP 1
53 #define HAVE_TRIANGLES 1
54 #define HAVE_TRI_STRIPS 1
55 #define HAVE_TRI_STRIP_1 0 /* has it, template can't use it yet */
56 #define HAVE_TRI_FANS 1
57 #define HAVE_POLYGONS 1
58 #define HAVE_QUADS 0
59 #define HAVE_QUAD_STRIPS 0
60
61 #define HAVE_ELTS 0
62
63 static const GLenum reducedPrim[GL_POLYGON + 1] = {
64 GL_POINTS,
65 GL_LINES,
66 GL_LINES,
67 GL_LINES,
68 GL_TRIANGLES,
69 GL_TRIANGLES,
70 GL_TRIANGLES,
71 GL_TRIANGLES,
72 GL_TRIANGLES,
73 GL_TRIANGLES
74 };
75
76 /* Fallback to normal rendering.
77 */
78 static void VERT_FALLBACK(GLcontext *ctx,
79 GLuint start,
80 GLuint count,
81 GLuint flags)
82 {
83 TNLcontext *tnl = TNL_CONTEXT(ctx);
84 #ifdef DEBUG
85 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
86 #endif
87 fprintf(stderr, "VERT_FALLBACK\n");
88 tnl->Driver.Render.PrimitiveNotify(ctx, flags & PRIM_MODE_MASK);
89 tnl->Driver.Render.BuildVertices(ctx, start, count, ~0);
90 tnl->Driver.Render.PrimTabVerts[flags & PRIM_MODE_MASK](ctx, start,
91 count, flags);
92 VIA_CONTEXT(ctx)->setupNewInputs = VERT_BIT_CLIP;
93 #ifdef DEBUG
94 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
95 #endif
96 }
97
98
99 #define LOCAL_VARS viaContextPtr vmesa = VIA_CONTEXT(ctx)
100 /*
101 #define INIT(prim) \
102 do { \
103 VIA_STATECHANGE(vmesa, 0); \
104 viaRasterPrimitive(ctx, reducedPrim[prim], prim); \
105 } while (0)
106 */
107 #define INIT(prim) \
108 do { \
109 viaRasterPrimitive(ctx, reducedPrim[prim], prim); \
110 } while (0)
111 #define NEW_PRIMITIVE() VIA_STATECHANGE(vmesa, 0)
112 #define NEW_BUFFER() VIA_FIREVERTICES(vmesa)
113 #define GET_CURRENT_VB_MAX_VERTS() \
114 (((int)vmesa->dmaHigh - (int)vmesa->dmaLow) / (vmesa->vertexSize * 4))
115 #define GET_SUBSEQUENT_VB_MAX_VERTS() \
116 (VIA_DMA_BUF_SZ - 4) / (vmesa->vertexSize * 4)
117
118
119 #define EMIT_VERTS(ctx, j, nr) \
120 via_emit_contiguous_verts(ctx, j, (j) + (nr))
121
122 #define FINISH \
123 do { \
124 vmesa->primitiveRendered = GL_TRUE; \
125 viaRasterPrimitiveFinish(ctx); \
126 } while (0)
127
128
129 #define TAG(x) via_fast##x
130 #include "via_dmatmp.h"
131 #undef TAG
132 #undef LOCAL_VARS
133 #undef INIT
134
135 /**********************************************************************/
136 /* Fast Render pipeline stage */
137 /**********************************************************************/
138 static GLboolean via_run_fastrender(GLcontext *ctx,
139 struct tnl_pipeline_stage *stage)
140 {
141 viaContextPtr vmesa = VIA_CONTEXT(ctx);
142 TNLcontext *tnl = TNL_CONTEXT(ctx);
143 struct vertex_buffer *VB = &tnl->vb;
144 GLuint i, length, flags = 0;
145
146 /* Don't handle clipping or indexed vertices.
147 */
148 #ifdef PERFORMANCE_MEASURE
149 if (VIA_PERFORMANCE) P_M;
150 #endif
151
152 if (VB->ClipOrMask || vmesa->renderIndex != 0 || VB->Elts) {
153 #ifdef DEBUG
154 if (VIA_DEBUG) {
155 fprintf(stderr, "slow path\n");
156 fprintf(stderr, "ClipOrMask = %08x\n", VB->ClipOrMask);
157 fprintf(stderr, "renderIndex = %08x\n", vmesa->renderIndex);
158 fprintf(stderr, "Elts = %08x\n", (GLuint)VB->Elts);
159 }
160 #endif
161 return GL_TRUE;
162 }
163 #ifdef DEBUG
164 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
165 #endif
166 vmesa->setupNewInputs = VERT_BIT_CLIP;
167 vmesa->primitiveRendered = GL_TRUE;
168
169 tnl->Driver.Render.Start(ctx);
170
171 for (i = 0; i < VB->PrimitiveCount; ++i) {
172 GLuint mode = VB->Primitive[i].mode;
173 GLuint start = VB->Primitive[i].start;
174 GLuint length = VB->Primitive[i].count;
175 if (length)
176 via_fastrender_tab_verts[mode & PRIM_MODE_MASK](ctx, start, start+length, mode);
177 }
178
179 tnl->Driver.Render.Finish(ctx);
180
181 /*=* DBG - viewperf7.0 : fix command buffer overflow *=*/
182 if (vmesa->dmaLow > (vmesa->dma[0].size / 2))
183 viaFlushPrims(vmesa);
184 #ifdef DEBUG
185 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
186 #endif
187 return GL_FALSE; /* finished the pipe */
188 }
189
190
191 static void via_check_fastrender(GLcontext *ctx, struct tnl_pipeline_stage *stage)
192 {
193 GLuint inputs = VERT_BIT_CLIP | VERT_BIT_COLOR0;
194
195 if (ctx->RenderMode == GL_RENDER) {
196 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
197 inputs |= VERT_BIT_COLOR1;
198
199 if (ctx->Texture.Unit[0]._ReallyEnabled)
200 inputs |= VERT_BIT_TEX0;
201
202 if (ctx->Texture.Unit[1]._ReallyEnabled)
203 inputs |= VERT_BIT_TEX1;
204
205 if (ctx->Fog.Enabled)
206 inputs |= VERT_BIT_FOG;
207 }
208
209 stage->inputs = inputs;
210 }
211
212
213 static void fastdtr(struct tnl_pipeline_stage *stage)
214 {
215 (void)stage;
216 }
217
218
219 const struct tnl_pipeline_stage _via_fastrender_stage =
220 {
221 "via fast render",
222 (_DD_NEW_SEPARATE_SPECULAR |
223 _NEW_TEXTURE|
224 _NEW_FOG|
225 _NEW_RENDERMODE), /* re-check (new inputs) */
226 0, /* re-run (always runs) */
227 GL_TRUE, /* active */
228 0, 0, /* inputs (set in check_render), outputs */
229 0, 0, /* changed_inputs, private */
230 fastdtr, /* destructor */
231 via_check_fastrender, /* check - initially set to alloc data */
232 via_run_fastrender /* run */
233 };
234
235
236 /*
237 * Render whole vertex buffers, including projection of vertices from
238 * clip space and clipping of primitives.
239 *
240 * This file makes calls to project vertices and to the point, line
241 * and triangle rasterizers via the function pointers:
242 *
243 * context->Driver.Render.*
244 *
245 */
246
247
248 /**********************************************************************/
249 /* Clip single primitives */
250 /**********************************************************************/
251 #undef DIFFERENT_SIGNS
252 #if defined(USE_IEEE)
253 #define NEGATIVE(x) (GET_FLOAT_BITS(x) & (1 << 31))
254 #define DIFFERENT_SIGNS(x, y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1 << 31))
255 #else
256 #define NEGATIVE(x) (x < 0)
257 #define DIFFERENT_SIGNS(x,y) (x * y <= 0 && x - y != 0)
258 /* Could just use (x*y<0) except for the flatshading requirements.
259 * Maybe there's a better way?
260 */
261 #endif
262
263 #define W(i) coord[i][3]
264 #define Z(i) coord[i][2]
265 #define Y(i) coord[i][1]
266 #define X(i) coord[i][0]
267 #define SIZE 4
268 #define TAG(x) x##_4
269 #include "via_vb_cliptmp.h"
270
271
272 /**********************************************************************/
273 /* Clip and render whole begin/end objects */
274 /**********************************************************************/
275 #define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
276 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
277 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
278
279
280 /* Vertices, with the possibility of clipping.
281 */
282 #define RENDER_POINTS(start, count) \
283 tnl->Driver.Render.Points(ctx, start, count)
284
285 #define RENDER_LINE(v1, v2) \
286 do { \
287 GLubyte c1 = mask[v1], c2 = mask[v2]; \
288 GLubyte ormask = c1 | c2; \
289 if (!ormask) \
290 LineFunc(ctx, v1, v2); \
291 else if (!(c1 & c2 & 0x3f)) \
292 clip_line_4(ctx, v1, v2, ormask); \
293 } while (0)
294
295 #define RENDER_TRI(v1, v2, v3) \
296 if (VIA_DEBUG) fprintf(stderr, "RENDER_TRI - clip\n"); \
297 do { \
298 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
299 GLubyte ormask = c1 | c2 | c3; \
300 if (!ormask) \
301 TriangleFunc(ctx, v1, v2, v3); \
302 else if (!(c1 & c2 & c3 & 0x3f)) \
303 clip_tri_4(ctx, v1, v2, v3, ormask); \
304 } while (0)
305
306 #define RENDER_QUAD(v1, v2, v3, v4) \
307 do { \
308 GLubyte c1 = mask[v1], c2 = mask[v2]; \
309 GLubyte c3 = mask[v3], c4 = mask[v4]; \
310 GLubyte ormask = c1 | c2 | c3 | c4; \
311 if (!ormask) \
312 QuadFunc(ctx, v1, v2, v3, v4); \
313 else if (!(c1 & c2 & c3 & c4 & 0x3f)) \
314 clip_quad_4(ctx, v1, v2, v3, v4, ormask); \
315 } while (0)
316
317
318 #define LOCAL_VARS \
319 TNLcontext *tnl = TNL_CONTEXT(ctx); \
320 struct vertex_buffer *VB = &tnl->vb; \
321 const GLuint * const elt = VB->Elts; \
322 const GLubyte *mask = VB->ClipMask; \
323 const GLuint sz = VB->ClipPtr->size; \
324 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
325 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
326 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
327 const GLboolean stipple = ctx->Line.StippleFlag; \
328 (void) (LineFunc && TriangleFunc && QuadFunc); \
329 (void) elt; (void) mask; (void) sz; (void) stipple;
330
331 #define POSTFIX \
332 viaRasterPrimitiveFinish(ctx)
333
334 #define TAG(x) clip_##x##_verts
335 #define INIT(x) tnl->Driver.Render.PrimitiveNotify(ctx, x)
336 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple(ctx)
337 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
338 #define PRESERVE_VB_DEFS
339 #include "via_vb_rendertmp.h"
340
341
342 /* Elts, with the possibility of clipping.
343 */
344 #undef ELT
345 #undef TAG
346 #define ELT(x) elt[x]
347 #define TAG(x) clip_##x##_elts
348 #include "via_vb_rendertmp.h"
349
350 /* TODO: do this for all primitives, verts and elts:
351 */
352 static void clip_elt_triangles(GLcontext *ctx,
353 GLuint start,
354 GLuint count,
355 GLuint flags)
356 {
357 TNLcontext *tnl = TNL_CONTEXT(ctx);
358 tnl_render_func render_tris = tnl->Driver.Render.PrimTabElts[GL_TRIANGLES];
359 struct vertex_buffer *VB = &tnl->vb;
360 const GLuint * const elt = VB->Elts;
361 GLubyte *mask = VB->ClipMask;
362 GLuint last = count-2;
363 GLuint j;
364 (void)flags;
365 #ifdef PERFORMANCE_MEASURE
366 if (VIA_PERFORMANCE) P_M;
367 #endif
368 tnl->Driver.Render.PrimitiveNotify(ctx, GL_TRIANGLES);
369
370 for (j = start; j < last; j += 3) {
371 GLubyte c1 = mask[elt[j]];
372 GLubyte c2 = mask[elt[j + 1]];
373 GLubyte c3 = mask[elt[j + 2]];
374 GLubyte ormask = c1 | c2 | c3;
375 if (ormask) {
376 if (start < j)
377 render_tris(ctx, start, j, 0);
378 if (!(c1 & c2 & c3 & 0x3f))
379 clip_tri_4(ctx, elt[j], elt[j + 1], elt[j + 2], ormask);
380 start = j+3;
381 }
382 }
383
384 if (start < j)
385 render_tris(ctx, start, j, 0);
386
387 viaRasterPrimitiveFinish(ctx);
388 }
389
390
391 /**********************************************************************/
392 /* Helper functions for drivers */
393 /**********************************************************************/
394 /*
395 void _tnl_RenderClippedPolygon(GLcontext *ctx, const GLuint *elts, GLuint n)
396 {
397 TNLcontext *tnl = TNL_CONTEXT(ctx);
398 struct vertex_buffer *VB = &tnl->vb;
399 GLuint *tmp = VB->Elts;
400
401 VB->Elts = (GLuint *)elts;
402 tnl->Driver.Render.PrimTabElts[GL_POLYGON](ctx, 0, n, PRIM_BEGIN|PRIM_END);
403 VB->Elts = tmp;
404 }
405
406 void _tnl_RenderClippedLine(GLcontext *ctx, GLuint ii, GLuint jj)
407 {
408 TNLcontext *tnl = TNL_CONTEXT(ctx);
409 tnl->Driver.Render.Line(ctx, ii, jj);
410 }
411 */
412
413 /**********************************************************************/
414 /* Render pipeline stage */
415 /**********************************************************************/
416 static GLboolean via_run_render(GLcontext *ctx,
417 struct tnl_pipeline_stage *stage)
418 {
419 TNLcontext *tnl = TNL_CONTEXT(ctx);
420 viaContextPtr vmesa = VIA_CONTEXT(ctx);
421 struct vertex_buffer *VB = &tnl->vb;
422 /* DBG */
423 GLuint newInputs = stage->changed_inputs;
424 /*GLuint newInputs = stage->inputs;*/
425
426 tnl_render_func *tab;
427 GLuint pass = 0;
428
429 #ifdef DEBUG
430 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
431 #endif
432 #ifdef PERFORMANCE_MEASURE
433 if (VIA_PERFORMANCE) P_M;
434 #endif
435 tnl->Driver.Render.Start(ctx);
436 tnl->Driver.Render.BuildVertices(ctx, 0, VB->Count, newInputs);
437 if (VB->ClipOrMask) {
438 tab = VB->Elts ? clip_render_tab_elts : clip_render_tab_verts;
439 clip_render_tab_elts[GL_TRIANGLES] = clip_elt_triangles;
440 }
441 else {
442 tab = VB->Elts ? tnl->Driver.Render.PrimTabElts : tnl->Driver.Render.PrimTabVerts;
443 }
444
445 do {
446 GLuint i;
447
448 for (i = 0; i < VB->PrimitiveCount; i++) {
449 GLuint flags = VB->Primitive[i].mode;
450 GLuint start = VB->Primitive[i].start;
451 GLuint length= VB->Primitive[i].count;
452 ASSERT(length || (flags & PRIM_END));
453 ASSERT((flags & PRIM_MODE_MASK) <= GL_POLYGON + 1);
454 if (length)
455 tab[flags & PRIM_MODE_MASK](ctx, start, start + length,flags);
456 }
457 }
458 while (tnl->Driver.Render.Multipass && tnl->Driver.Render.Multipass(ctx, ++pass));
459 tnl->Driver.Render.Finish(ctx);
460
461 /*=* DBG - flush : if hw idel *=*/
462 /*{
463 GLuint volatile *pnEnginStatus = vmesa->regEngineStatus;
464 GLuint nStatus;
465 nStatus = *pnEnginStatus;
466 if ((nStatus & 0xFFFEFFFF) == 0x00020000)
467 viaFlushPrims(vmesa);
468 }*/
469
470 /*=* DBG viewperf7.0 : fix command buffer overflow *=*/
471 if (vmesa->dmaLow > (vmesa->dma[0].size / 2))
472 viaFlushPrims(vmesa);
473 #ifdef DEBUG
474 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
475 #endif
476 return GL_FALSE; /* finished the pipe */
477 }
478
479 /* Quite a bit of work involved in finding out the inputs for the
480 * render stage.
481 */
482
483 static void via_check_render(GLcontext *ctx, struct tnl_pipeline_stage *stage)
484 {
485 GLuint inputs = VERT_BIT_CLIP;
486
487 if (ctx->Visual.rgbMode) {
488 inputs |= VERT_BIT_COLOR0;
489
490 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
491 inputs |= VERT_BIT_COLOR1;
492
493 if (ctx->Texture.Unit[0]._ReallyEnabled) {
494 inputs |= VERT_BIT_TEX0;
495 }
496
497 if (ctx->Texture.Unit[1]._ReallyEnabled) {
498 inputs |= VERT_BIT_TEX1;
499 }
500 }
501 else {
502 /*inputs |= VERT_BIT_INDEX;*/
503 }
504
505 /*if (ctx->Point._Attenuated)
506 inputs |= VERT_POINT_SIZE;*/
507
508 if (ctx->Fog.Enabled)
509 inputs |= VERT_BIT_FOG;
510
511 /*if (ctx->_TriangleCaps & DD_TRI_UNFILLED)
512 inputs |= VERT_EDGE;
513
514 if (ctx->RenderMode == GL_FEEDBACK)
515 inputs |= VERT_TEX_ANY;*/
516
517 stage->inputs = inputs;
518 }
519
520
521 static void dtr(struct tnl_pipeline_stage *stage)
522 {
523 (void)stage;
524 }
525
526
527 const struct tnl_pipeline_stage _via_render_stage =
528 {
529 "via render",
530 (_NEW_BUFFERS |
531 _DD_NEW_SEPARATE_SPECULAR |
532 _DD_NEW_FLATSHADE |
533 _NEW_TEXTURE |
534 _NEW_LIGHT |
535 _NEW_POINT |
536 _NEW_FOG |
537 _DD_NEW_TRI_UNFILLED |
538 _NEW_RENDERMODE), /* re-check (new inputs) */
539 0, /* re-run (always runs) */
540 GL_TRUE, /* active */
541 0, 0, /* inputs (set in check_render), outputs */
542 0, 0, /* changed_inputs, private */
543 dtr, /* destructor */
544 via_check_render, /* check - initially set to alloc data */
545 via_run_render /* run */
546 };