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