Re-commit t_vertex.[ch] changes to fd.o server.
[mesa.git] / src / mesa / drivers / dri / i830 / i830_render.c
1 /*
2 * Intel i810 DRI driver for Mesa 3.5
3 *
4 * Copyright (C) 1999-2000 Keith Whitwell All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL KEITH WHITWELL BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Author:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 * Adapted for use on the I830:
27 * Jeff Hartmann <jhartmann@2d3d.com>
28 */
29 /* $XFree86: xc/lib/GL/mesa/src/drv/i830/i830_render.c,v 1.2 2002/12/10 01:26:53 dawes Exp $ */
30
31 /*
32 * Render unclipped vertex buffers by emitting vertices directly to
33 * dma buffers. Use strip/fan hardware acceleration where possible.
34 *
35 */
36 #include "glheader.h"
37 #include "context.h"
38 #include "macros.h"
39 #include "imports.h"
40 #include "mtypes.h"
41 #include "enums.h"
42
43 #include "tnl/t_context.h"
44
45 #include "i830_screen.h"
46 #include "i830_dri.h"
47
48 #include "i830_context.h"
49 #include "i830_tris.h"
50 #include "i830_state.h"
51 #include "i830_vb.h"
52 #include "i830_ioctl.h"
53
54 /*
55 * Render unclipped vertex buffers by emitting vertices directly to
56 * dma buffers. Use strip/fan hardware primitives where possible.
57 * Try to simulate missing primitives with indexed vertices.
58 */
59 #define HAVE_POINTS 0 /* Has it, but can't use because subpixel has to
60 * be adjusted for points on the I830/I845G
61 */
62 #define HAVE_LINES 1
63 #define HAVE_LINE_STRIPS 1
64 #define HAVE_TRIANGLES 1
65 #define HAVE_TRI_STRIPS 1
66 #define HAVE_TRI_STRIP_1 0 /* has it, template can't use it yet */
67 #define HAVE_TRI_FANS 1
68 #define HAVE_POLYGONS 1
69 #define HAVE_QUADS 0
70 #define HAVE_QUAD_STRIPS 0
71
72 #define HAVE_ELTS 0
73
74 static GLuint hw_prim[GL_POLYGON+1] = {
75 0,
76 PRIM3D_LINELIST,
77 PRIM3D_LINESTRIP,
78 PRIM3D_LINESTRIP,
79 PRIM3D_TRILIST,
80 PRIM3D_TRISTRIP,
81 PRIM3D_TRIFAN,
82 0,
83 0,
84 PRIM3D_POLY
85 };
86
87 static const GLenum reduced_prim[GL_POLYGON+1] = {
88 GL_POINTS,
89 GL_LINES,
90 GL_LINES,
91 GL_LINES,
92 GL_TRIANGLES,
93 GL_TRIANGLES,
94 GL_TRIANGLES,
95 GL_TRIANGLES,
96 GL_TRIANGLES,
97 GL_TRIANGLES
98 };
99
100 static const int scale_prim[GL_POLYGON+1] = {
101 0, /* fallback case */
102 1,
103 2,
104 2,
105 1,
106 3,
107 3,
108 0, /* fallback case */
109 0, /* fallback case */
110 3
111 };
112
113
114 #define LOCAL_VARS i830ContextPtr imesa = I830_CONTEXT(ctx)
115 #define INIT( prim ) do { \
116 I830_STATECHANGE(imesa, 0); \
117 i830RasterPrimitive( ctx, reduced_prim[prim], hw_prim[prim] ); \
118 } while (0)
119
120 #define FLUSH() I830_FIREVERTICES( imesa )
121 #define GET_CURRENT_VB_MAX_VERTS() \
122 (((int)imesa->vertex_high - (int)imesa->vertex_low) / (imesa->vertex_size*4))
123 #define GET_SUBSEQUENT_VB_MAX_VERTS() \
124 (I830_DMA_BUF_SZ-8) / (imesa->vertex_size * 4)
125
126
127 #define ALLOC_VERTS( nr ) \
128 i830AllocDmaLow( imesa, nr * imesa->vertex_size * 4)
129 #define EMIT_VERTS( ctx, j, nr, buf ) \
130 i830_emit_contiguous_verts(ctx, j, (j)+(nr), buf)
131
132 #define TAG(x) i830_##x
133 #include "tnl_dd/t_dd_dmatmp.h"
134
135
136 /**********************************************************************/
137 /* Render pipeline stage */
138 /**********************************************************************/
139
140 /* Heuristic for i830, which can only emit a single primitive per dma
141 * buffer, and has only a small number of dma buffers.
142 */
143 static GLboolean choose_render( struct vertex_buffer *VB, int bufsz )
144 {
145 int nr_prims = 0;
146 int nr_rprims = 0;
147 int nr_rverts = 0;
148 int rprim = 0;
149 int i;
150
151
152 for (i = 0 ; i < VB->PrimitiveCount ; i++)
153 {
154 GLuint prim = VB->Primitive[i].mode;
155 GLuint length = VB->Primitive[i].count;
156
157 if (!length)
158 continue;
159
160 if (!hw_prim[prim & PRIM_MODE_MASK])
161 return GL_FALSE;
162
163 nr_prims++;
164 nr_rverts += length * scale_prim[prim & PRIM_MODE_MASK];
165
166 if (reduced_prim[prim&PRIM_MODE_MASK] != rprim) {
167 nr_rprims++;
168 rprim = reduced_prim[prim&PRIM_MODE_MASK];
169 }
170 }
171
172 nr_prims += i / bufsz;
173 nr_rprims += nr_rverts / bufsz;
174
175 if ((nr_prims > nr_rprims * 2) ||
176 (nr_prims > nr_rprims + 3))
177 return GL_FALSE;
178
179 return GL_TRUE;
180 }
181
182
183 static GLboolean i830_run_render( GLcontext *ctx,
184 struct tnl_pipeline_stage *stage )
185 {
186 i830ContextPtr imesa = I830_CONTEXT(ctx);
187 TNLcontext *tnl = TNL_CONTEXT(ctx);
188 struct vertex_buffer *VB = &tnl->vb;
189 GLuint i;
190 /* Don't handle clipping or indexed vertices.
191 */
192 if (imesa->RenderIndex != 0 ||
193 !i830_validate_render( ctx, VB ) ||
194 !choose_render( VB, GET_SUBSEQUENT_VB_MAX_VERTS() )) {
195 return GL_TRUE;
196 }
197
198 imesa->SetupNewInputs = VERT_BIT_POS;
199
200 tnl->Driver.Render.Start( ctx );
201
202 for (i = 0 ; i < VB->PrimitiveCount ; i++)
203 {
204 GLuint prim = VB->Primitive[i].mode;
205 GLuint start = VB->Primitive[i].start;
206 GLuint length = VB->Primitive[i].count;
207
208 if (!length)
209 continue;
210
211 i830_render_tab_verts[prim & PRIM_MODE_MASK]( ctx, start, start + length,
212 prim );
213 }
214
215 tnl->Driver.Render.Finish( ctx );
216
217 return GL_FALSE; /* finished the pipe */
218 }
219
220
221 static void i830_check_render( GLcontext *ctx,
222 struct tnl_pipeline_stage *stage )
223 {
224 stage->inputs = TNL_CONTEXT(ctx)->render_inputs;
225 }
226
227 static void dtr( struct tnl_pipeline_stage *stage )
228 {
229 (void)stage;
230 }
231
232
233 const struct tnl_pipeline_stage _i830_render_stage =
234 {
235 "i830 render",
236 (_DD_NEW_SEPARATE_SPECULAR |
237 _NEW_TEXTURE|
238 _NEW_FOG|
239 _NEW_RENDERMODE), /* re-check (new inputs) */
240 0, /* re-run (always runs) */
241 GL_TRUE, /* active */
242 0, 0, /* inputs (set in check_render), outputs */
243 0, 0, /* changed_inputs, private */
244 dtr, /* destructor */
245 i830_check_render, /* check - initially set to alloc data */
246 i830_run_render /* run */
247 };