use smaller buffer when decomposing large drawarrays, for cache goodness
[mesa.git] / src / mesa / tnl / t_array_api.c
1 /* $Id: t_array_api.c,v 1.21 2001/11/29 15:15:20 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30 #include "glheader.h"
31 #include "api_validate.h"
32 #include "context.h"
33 #include "macros.h"
34 #include "mmath.h"
35 #include "mem.h"
36 #include "state.h"
37 #include "mtypes.h"
38
39 #include "array_cache/acache.h"
40
41 #include "t_array_api.h"
42 #include "t_array_import.h"
43 #include "t_imm_api.h"
44 #include "t_imm_exec.h"
45 #include "t_context.h"
46 #include "t_pipeline.h"
47
48 static void fallback_drawarrays( GLcontext *ctx, GLenum mode, GLint start,
49 GLsizei count )
50 {
51 if (_tnl_hard_begin( ctx, mode )) {
52 GLint i;
53 for (i = start; i < count; i++) {
54 _tnl_array_element( ctx, i );
55 }
56 _tnl_end( ctx );
57 }
58 }
59
60
61 static void fallback_drawelements( GLcontext *ctx, GLenum mode, GLsizei count,
62 const GLuint *indices)
63 {
64 /* Simple version of the above code.
65 */
66 if (_tnl_hard_begin(ctx, mode)) {
67 GLint i;
68 for (i = 0 ; i < count ; i++)
69 _tnl_array_element( ctx, indices[i] );
70 _tnl_end( ctx );
71 }
72 }
73
74
75 static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
76 GLuint start, GLuint end,
77 GLsizei count, const GLuint *indices )
78
79 {
80 TNLcontext *tnl = TNL_CONTEXT(ctx);
81 FLUSH_CURRENT( ctx, 0 );
82
83 _tnl_vb_bind_arrays( ctx, start, end );
84
85 tnl->vb.FirstPrimitive = 0;
86 tnl->vb.Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
87 tnl->vb.PrimitiveLength[0] = count;
88 tnl->vb.Elts = (GLuint *)indices;
89
90 if (ctx->Array.LockCount)
91 tnl->Driver.RunPipeline( ctx );
92 else {
93 /* Note that arrays may have changed before/after execution.
94 */
95 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
96 tnl->Driver.RunPipeline( ctx );
97 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
98 }
99 }
100
101
102
103
104 void
105 _tnl_DrawArrays(GLenum mode, GLint start, GLsizei count)
106 {
107 GET_CURRENT_CONTEXT(ctx);
108 TNLcontext *tnl = TNL_CONTEXT(ctx);
109 struct vertex_buffer *VB = &tnl->vb;
110
111 /* fprintf(stderr, "%s %d %d\n", __FUNCTION__, start, count); */
112
113 /* Check arguments, etc.
114 */
115 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
116 return;
117
118 if (tnl->pipeline.build_state_changes)
119 _tnl_validate_pipeline( ctx );
120
121 if (ctx->CompileFlag) {
122 fallback_drawarrays( ctx, mode, start, start + count );
123 }
124 else if (count < (GLint) ctx->Const.MaxArrayLockSize) {
125
126 /* Small primitives which can fit in a single vertex buffer:
127 */
128 FLUSH_CURRENT( ctx, 0 );
129
130 if (ctx->Array.LockCount)
131 {
132 if (start < (GLint) ctx->Array.LockFirst)
133 start = ctx->Array.LockFirst;
134 if (start + count > (GLint) ctx->Array.LockCount)
135 count = ctx->Array.LockCount - start;
136
137 /* Locked drawarrays. Reuse any previously transformed data.
138 */
139 _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
140 VB->FirstPrimitive = start;
141 VB->Primitive[start] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
142 VB->PrimitiveLength[start] = count;
143 tnl->Driver.RunPipeline( ctx );
144 } else {
145 /* The arrays are small enough to fit in a single VB; just bind
146 * them and go. Any untransformed data will be copied on
147 * clipping.
148 *
149 * Invalidate any cached data dependent on these arrays.
150 */
151 _tnl_vb_bind_arrays( ctx, start, start + count );
152 VB->FirstPrimitive = 0;
153 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
154 VB->PrimitiveLength[0] = count;
155 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
156 tnl->Driver.RunPipeline( ctx );
157 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
158 }
159 }
160 else {
161 int bufsz = 256; /* Use a small buffer for cache goodness */
162 int j, nr;
163 int minimum, modulo, skip;
164
165 /* Large primitives requiring decomposition to multiple vertex
166 * buffers:
167 */
168 switch (mode) {
169 case GL_POINTS:
170 minimum = 0;
171 modulo = 1;
172 skip = 0;
173 case GL_LINES:
174 minimum = 1;
175 modulo = 2;
176 skip = 1;
177 case GL_LINE_STRIP:
178 minimum = 1;
179 modulo = 1;
180 skip = 0;
181 break;
182 case GL_TRIANGLES:
183 minimum = 2;
184 modulo = 3;
185 skip = 2;
186 break;
187 case GL_TRIANGLE_STRIP:
188 minimum = 2;
189 modulo = 1;
190 skip = 0;
191 break;
192 case GL_QUADS:
193 minimum = 3;
194 modulo = 4;
195 skip = 3;
196 break;
197 case GL_QUAD_STRIP:
198 minimum = 3;
199 modulo = 2;
200 skip = 0;
201 break;
202 case GL_LINE_LOOP:
203 case GL_TRIANGLE_FAN:
204 case GL_POLYGON:
205 default:
206 /* Primitives requiring a copied vertex (fan-like primitives)
207 * must use the slow path:
208 */
209 fallback_drawarrays( ctx, mode, start, start + count );
210 return;
211 }
212
213 FLUSH_CURRENT( ctx, 0 );
214
215 /* fprintf(stderr, "start %d count %d min %d modulo %d skip %d\n", */
216 /* start, count, minimum, modulo, skip); */
217
218
219 bufsz -= bufsz % modulo;
220 bufsz -= minimum;
221 count += start;
222
223 for (j = start + minimum ; j < count ; j += nr + skip ) {
224
225 nr = MIN2( bufsz, count - j );
226
227 /* fprintf(stderr, "%d..%d\n", j - minimum, j+nr); */
228
229 _tnl_vb_bind_arrays( ctx, j - minimum, j + nr );
230
231 VB->FirstPrimitive = 0;
232 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
233 VB->PrimitiveLength[0] = nr + minimum;
234 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
235 tnl->Driver.RunPipeline( ctx );
236 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
237 }
238 }
239 }
240
241
242 void
243 _tnl_DrawRangeElements(GLenum mode,
244 GLuint start, GLuint end,
245 GLsizei count, GLenum type, const GLvoid *indices)
246 {
247 GET_CURRENT_CONTEXT(ctx);
248 TNLcontext *tnl = TNL_CONTEXT(ctx);
249 GLuint *ui_indices;
250
251 /* fprintf(stderr, "%s\n", __FUNCTION__); */
252
253 /* Check arguments, etc.
254 */
255 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
256 type, indices ))
257 return;
258
259 if (tnl->pipeline.build_state_changes)
260 _tnl_validate_pipeline( ctx );
261
262 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
263 count, type, indices );
264
265
266 if (ctx->CompileFlag) {
267 /* Can't do anything when compiling:
268 */
269 fallback_drawelements( ctx, mode, count, ui_indices );
270 }
271 else if (ctx->Array.LockCount) {
272 /* Are the arrays already locked? If so we currently have to look
273 * at the whole locked range.
274 */
275 if (start >= ctx->Array.LockFirst && end <= ctx->Array.LockCount)
276 _tnl_draw_range_elements( ctx, mode,
277 ctx->Array.LockFirst,
278 ctx->Array.LockCount,
279 count, ui_indices );
280 else {
281 /* The spec says referencing elements outside the locked
282 * range is undefined. I'm going to make it a noop this time
283 * round, maybe come up with something beter before 3.6.
284 *
285 * May be able to get away with just setting LockCount==0,
286 * though this raises the problems of dependent state. May
287 * have to call glUnlockArrays() directly?
288 *
289 * Or scan the list and replace bad indices?
290 */
291 _mesa_problem( ctx,
292 "DrawRangeElements references "
293 "elements outside locked range.");
294 }
295 }
296 else if (end + 1 - start < ctx->Const.MaxArrayLockSize) {
297 /* The arrays aren't locked but we can still fit them inside a
298 * single vertexbuffer.
299 */
300 _tnl_draw_range_elements( ctx, mode, start, end + 1, count, ui_indices );
301 } else {
302 /* Range is too big to optimize:
303 */
304 fallback_drawelements( ctx, mode, count, ui_indices );
305 }
306 }
307
308
309
310 void
311 _tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
312 const GLvoid *indices)
313 {
314 GET_CURRENT_CONTEXT(ctx);
315 TNLcontext *tnl = TNL_CONTEXT(ctx);
316 GLuint *ui_indices;
317
318 /* fprintf(stderr, "%s\n", __FUNCTION__); */
319
320 /* Check arguments, etc.
321 */
322 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
323 return;
324
325 if (tnl->pipeline.build_state_changes)
326 _tnl_validate_pipeline( ctx );
327
328 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
329 count, type, indices );
330
331 if (ctx->CompileFlag) {
332 /* Can't do anything when compiling:
333 */
334 fallback_drawelements( ctx, mode, count, ui_indices );
335 }
336 else if (ctx->Array.LockCount) {
337 _tnl_draw_range_elements( ctx, mode,
338 ctx->Array.LockFirst,
339 ctx->Array.LockCount,
340 count, ui_indices );
341 }
342 else {
343 /* Scan the index list and see if we can use the locked path anyway.
344 */
345 GLuint max_elt = 0;
346 GLint i;
347
348 for (i = 0 ; i < count ; i++)
349 if (ui_indices[i] > max_elt)
350 max_elt = ui_indices[i];
351
352 if (max_elt < ctx->Const.MaxArrayLockSize && /* can we use it? */
353 max_elt < (GLuint) count) /* do we want to use it? */
354 _tnl_draw_range_elements( ctx, mode, 0, max_elt+1, count, ui_indices );
355 else
356 fallback_drawelements( ctx, mode, count, ui_indices );
357 }
358 }
359
360
361 void _tnl_array_init( GLcontext *ctx )
362 {
363 TNLcontext *tnl = TNL_CONTEXT(ctx);
364 struct vertex_arrays *tmp = &tnl->array_inputs;
365 GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->vtxfmt);
366 GLuint i;
367
368 vfmt->DrawArrays = _tnl_DrawArrays;
369 vfmt->DrawElements = _tnl_DrawElements;
370 vfmt->DrawRangeElements = _tnl_DrawRangeElements;
371
372 /* Setup vector pointers that will be used to bind arrays to VB's.
373 */
374 _mesa_vector4f_init( &tmp->Obj, 0, 0 );
375 _mesa_vector3f_init( &tmp->Normal, 0, 0 );
376 _mesa_vector1f_init( &tmp->FogCoord, 0, 0 );
377 _mesa_vector1ui_init( &tmp->Index, 0, 0 );
378 _mesa_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
379
380 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
381 _mesa_vector4f_init( &tmp->TexCoord[i], 0, 0);
382
383 tnl->tmp_primitive = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
384 tnl->tmp_primitive_length = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
385 }
386
387
388 void _tnl_array_destroy( GLcontext *ctx )
389 {
390 TNLcontext *tnl = TNL_CONTEXT(ctx);
391 if (tnl->tmp_primitive_length) FREE(tnl->tmp_primitive_length);
392 if (tnl->tmp_primitive) FREE(tnl->tmp_primitive);
393 }