add the i830 driver - no kernel driver yet
[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 /* Fallback to normal rendering. Should now never be called.
114 */
115 static void VERT_FALLBACK( GLcontext *ctx,
116 GLuint start,
117 GLuint count,
118 GLuint flags )
119 {
120 TNLcontext *tnl = TNL_CONTEXT(ctx);
121 tnl->Driver.Render.PrimitiveNotify( ctx, flags & PRIM_MODE_MASK );
122 tnl->Driver.Render.BuildVertices( ctx, start, count, ~0 );
123 tnl->Driver.Render.PrimTabVerts[flags&PRIM_MODE_MASK]( ctx, start,
124 count, flags );
125 I830_CONTEXT(ctx)->SetupNewInputs = VERT_BIT_CLIP;
126 }
127
128
129 #define LOCAL_VARS i830ContextPtr imesa = I830_CONTEXT(ctx)
130 #define INIT( prim ) do { \
131 I830_STATECHANGE(imesa, 0); \
132 i830RasterPrimitive( ctx, reduced_prim[prim], hw_prim[prim] ); \
133 } while (0)
134
135 #define NEW_PRIMITIVE() I830_STATECHANGE( imesa, 0 )
136 #define NEW_BUFFER() I830_FIREVERTICES( imesa )
137 #define GET_CURRENT_VB_MAX_VERTS() \
138 (((int)imesa->vertex_high - (int)imesa->vertex_low) / (imesa->vertex_size*4))
139 #define GET_SUBSEQUENT_VB_MAX_VERTS() \
140 (I830_DMA_BUF_SZ-8) / (imesa->vertex_size * 4)
141
142 #define EMIT_VERTS( ctx, j, nr ) \
143 i830_emit_contiguous_verts(ctx, j, (j)+(nr))
144
145 #define TAG(x) i830_##x
146 #include "tnl_dd/t_dd_dmatmp.h"
147
148
149 /**********************************************************************/
150 /* Render pipeline stage */
151 /**********************************************************************/
152
153 /* Heuristic for i830, which can only emit a single primitive per dma
154 * buffer, and has only a small number of dma buffers.
155 */
156 static GLboolean choose_render( struct vertex_buffer *VB, int bufsz )
157 {
158 int nr_prims = 0;
159 int nr_rprims = 0;
160 int nr_rverts = 0;
161 int rprim = 0;
162 int i = 0, length, flags = 0;
163
164
165 for (i = VB->FirstPrimitive ; !(flags & PRIM_LAST) ; i += length) {
166 flags = VB->Primitive[i];
167 length = VB->PrimitiveLength[i];
168 if (!length)
169 continue;
170
171 if (!hw_prim[flags & PRIM_MODE_MASK])
172 return GL_FALSE;
173
174 nr_prims++;
175 nr_rverts += length * scale_prim[flags & PRIM_MODE_MASK];
176
177 if (reduced_prim[flags&PRIM_MODE_MASK] != rprim) {
178 nr_rprims++;
179 rprim = reduced_prim[flags&PRIM_MODE_MASK];
180 }
181 }
182
183 nr_prims += i / bufsz;
184 nr_rprims += nr_rverts / bufsz;
185
186 if ((nr_prims > nr_rprims * 2) ||
187 (nr_prims > nr_rprims + 3))
188 return GL_FALSE;
189
190 return GL_TRUE;
191 }
192
193
194 static GLboolean i830_run_render( GLcontext *ctx,
195 struct gl_pipeline_stage *stage )
196 {
197 i830ContextPtr imesa = I830_CONTEXT(ctx);
198 TNLcontext *tnl = TNL_CONTEXT(ctx);
199 struct vertex_buffer *VB = &tnl->vb;
200 GLuint i, length, flags = 0;
201 /* Don't handle clipping or indexed vertices.
202 */
203 if (VB->ClipOrMask || imesa->RenderIndex != 0 || VB->Elts ||
204 !choose_render( VB, GET_SUBSEQUENT_VB_MAX_VERTS() )) {
205 return GL_TRUE;
206 }
207
208 imesa->SetupNewInputs = VERT_BIT_CLIP;
209
210 tnl->Driver.Render.Start( ctx );
211
212 for (i = VB->FirstPrimitive ; !(flags & PRIM_LAST) ; i += length) {
213 flags = VB->Primitive[i];
214 length= VB->PrimitiveLength[i];
215 if (length)
216 i830_render_tab_verts[flags & PRIM_MODE_MASK]( ctx, i, i + length,
217 flags );
218 }
219
220 tnl->Driver.Render.Finish( ctx );
221
222 return GL_FALSE; /* finished the pipe */
223 }
224
225
226 static void i830_check_render( GLcontext *ctx,
227 struct gl_pipeline_stage *stage )
228 {
229 GLuint inputs = VERT_BIT_CLIP | VERT_BIT_COLOR0;
230 if (ctx->RenderMode == GL_RENDER) {
231 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
232 inputs |= VERT_BIT_COLOR1;
233
234 if (ctx->Texture.Unit[0]._ReallyEnabled)
235 inputs |= VERT_BIT_TEX0;
236
237 if (ctx->Texture.Unit[1]._ReallyEnabled)
238 inputs |= VERT_BIT_TEX1;
239
240 if (ctx->Fog.Enabled)
241 inputs |= VERT_BIT_FOG;
242 }
243
244 stage->inputs = inputs;
245 }
246
247 static void dtr( struct gl_pipeline_stage *stage )
248 {
249 (void)stage;
250 }
251
252
253 const struct gl_pipeline_stage _i830_render_stage =
254 {
255 "i830 render",
256 (_DD_NEW_SEPARATE_SPECULAR |
257 _NEW_TEXTURE|
258 _NEW_FOG|
259 _NEW_RENDERMODE), /* re-check (new inputs) */
260 0, /* re-run (always runs) */
261 GL_TRUE, /* active */
262 0, 0, /* inputs (set in check_render), outputs */
263 0, 0, /* changed_inputs, private */
264 dtr, /* destructor */
265 i830_check_render, /* check - initially set to alloc data */
266 i830_run_render /* run */
267 };