fix fallback crashes when driver can't handle frag prog for i915 driver too (untested)
[mesa.git] / src / mesa / drivers / dri / i915 / intel_render.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * 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
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Render unclipped vertex buffers by emitting vertices directly to
30 * dma buffers. Use strip/fan hardware acceleration where possible.
31 *
32 */
33 #include "glheader.h"
34 #include "context.h"
35 #include "macros.h"
36 #include "imports.h"
37 #include "mtypes.h"
38 #include "enums.h"
39
40 #include "tnl/t_context.h"
41 #include "tnl/t_vertex.h"
42
43 #include "intel_screen.h"
44 #include "intel_context.h"
45 #include "intel_tris.h"
46 #include "intel_batchbuffer.h"
47 #include "intel_reg.h"
48
49 /*
50 * Render unclipped vertex buffers by emitting vertices directly to
51 * dma buffers. Use strip/fan hardware primitives where possible.
52 * Try to simulate missing primitives with indexed vertices.
53 */
54 #define HAVE_POINTS 0 /* Has it, but can't use because subpixel has to
55 * be adjusted for points on the INTEL/I845G
56 */
57 #define HAVE_LINES 1
58 #define HAVE_LINE_STRIPS 1
59 #define HAVE_TRIANGLES 1
60 #define HAVE_TRI_STRIPS 1
61 #define HAVE_TRI_STRIP_1 0 /* has it, template can't use it yet */
62 #define HAVE_TRI_FANS 1
63 #define HAVE_POLYGONS 1
64 #define HAVE_QUADS 0
65 #define HAVE_QUAD_STRIPS 0
66
67 #define HAVE_ELTS 0
68
69 static GLuint hw_prim[GL_POLYGON+1] = {
70 0,
71 PRIM3D_LINELIST,
72 PRIM3D_LINESTRIP,
73 PRIM3D_LINESTRIP,
74 PRIM3D_TRILIST,
75 PRIM3D_TRISTRIP,
76 PRIM3D_TRIFAN,
77 0,
78 0,
79 PRIM3D_POLY
80 };
81
82 static const GLenum reduced_prim[GL_POLYGON+1] = {
83 GL_POINTS,
84 GL_LINES,
85 GL_LINES,
86 GL_LINES,
87 GL_TRIANGLES,
88 GL_TRIANGLES,
89 GL_TRIANGLES,
90 GL_TRIANGLES,
91 GL_TRIANGLES,
92 GL_TRIANGLES
93 };
94
95 static const int scale_prim[GL_POLYGON+1] = {
96 0, /* fallback case */
97 1,
98 2,
99 2,
100 1,
101 3,
102 3,
103 0, /* fallback case */
104 0, /* fallback case */
105 3
106 };
107
108
109 static void intelDmaPrimitive( intelContextPtr intel, GLenum prim )
110 {
111 if (0) fprintf(stderr, "%s %s\n", __FUNCTION__, _mesa_lookup_enum_by_nr(prim));
112 INTEL_FIREVERTICES(intel);
113 intel->vtbl.reduced_primitive_state( intel, reduced_prim[prim] );
114 intelStartInlinePrimitive( intel, hw_prim[prim] );
115 }
116
117
118 #define LOCAL_VARS intelContextPtr intel = INTEL_CONTEXT(ctx)
119 #define INIT( prim ) \
120 do { \
121 intelDmaPrimitive( intel, prim ); \
122 } while (0)
123 #define FLUSH() INTEL_FIREVERTICES( intel )
124
125 #define GET_SUBSEQUENT_VB_MAX_VERTS() \
126 (((intel->alloc.size / 2) - 1500) / (intel->vertex_size*4))
127 #define GET_CURRENT_VB_MAX_VERTS() GET_SUBSEQUENT_VB_MAX_VERTS()
128
129 #define ALLOC_VERTS( nr ) \
130 intelExtendInlinePrimitive( intel, (nr) * intel->vertex_size )
131
132 #define EMIT_VERTS( ctx, j, nr, buf ) \
133 _tnl_emit_vertices_to_buffer(ctx, j, (j)+(nr), buf )
134
135 #define TAG(x) intel_##x
136 #include "tnl_dd/t_dd_dmatmp.h"
137
138
139 /**********************************************************************/
140 /* Render pipeline stage */
141 /**********************************************************************/
142
143 /* Heuristic to choose between the two render paths:
144 */
145 static GLboolean choose_render( intelContextPtr intel,
146 struct vertex_buffer *VB )
147 {
148 int vertsz = intel->vertex_size;
149 int cost_render = 0;
150 int cost_fallback = 0;
151 int nr_prims = 0;
152 int nr_rprims = 0;
153 int nr_rverts = 0;
154 int rprim = intel->reduced_primitive;
155 int i = 0;
156
157 for (i = 0 ; i < VB->PrimitiveCount ; i++) {
158 GLuint prim = VB->Primitive[i].mode;
159 GLuint length = VB->Primitive[i].count;
160
161 if (!length)
162 continue;
163
164 nr_prims++;
165 nr_rverts += length * scale_prim[prim & PRIM_MODE_MASK];
166
167 if (reduced_prim[prim & PRIM_MODE_MASK] != rprim) {
168 nr_rprims++;
169 rprim = reduced_prim[prim & PRIM_MODE_MASK];
170 }
171 }
172
173 /* One point for each generated primitive:
174 */
175 cost_render = nr_prims;
176 cost_fallback = nr_rprims;
177
178 /* One point for every 1024 dwords (4k) of dma:
179 */
180 cost_render += (vertsz * i) / 1024;
181 cost_fallback += (vertsz * nr_rverts) / 1024;
182
183 if (0)
184 fprintf(stderr, "cost render: %d fallback: %d\n",
185 cost_render, cost_fallback);
186
187 if (cost_render > cost_fallback)
188 return GL_FALSE;
189
190 return GL_TRUE;
191 }
192
193
194 static GLboolean intel_run_render( GLcontext *ctx,
195 struct tnl_pipeline_stage *stage )
196 {
197 intelContextPtr intel = INTEL_CONTEXT(ctx);
198 TNLcontext *tnl = TNL_CONTEXT(ctx);
199 struct vertex_buffer *VB = &tnl->vb;
200 GLuint i;
201
202 intel->vtbl.render_prevalidate( intel );
203
204 /* Don't handle clipping or indexed vertices.
205 */
206 if (intel->RenderIndex != 0 ||
207 !intel_validate_render( ctx, VB ) ||
208 !choose_render( intel, VB )) {
209 return GL_TRUE;
210 }
211
212 tnl->clipspace.new_inputs |= VERT_BIT_POS;
213
214 tnl->Driver.Render.Start( ctx );
215
216 for (i = 0 ; i < VB->PrimitiveCount ; i++)
217 {
218 GLuint prim = VB->Primitive[i].mode;
219 GLuint start = VB->Primitive[i].start;
220 GLuint length = VB->Primitive[i].count;
221
222 if (!length)
223 continue;
224
225 intel_render_tab_verts[prim & PRIM_MODE_MASK]( ctx, start, start + length,
226 prim );
227 }
228
229 tnl->Driver.Render.Finish( ctx );
230
231 return GL_FALSE; /* finished the pipe */
232 }
233
234 const struct tnl_pipeline_stage _intel_render_stage =
235 {
236 "intel render",
237 NULL,
238 NULL,
239 NULL,
240 NULL,
241 intel_run_render /* run */
242 };