Killed mmath.[ch]. Moved low-level functions/assembly code into imports.[ch]
[mesa.git] / src / mesa / tnl / t_array_api.c
1 /* $Id: t_array_api.c,v 1.31 2003/03/01 01:50:26 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 5.1
6 *
7 * Copyright (C) 1999-2003 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
27 /**
28 * \file t_array_api.c
29 * \brief Vertex array API functions (glDrawArrays, etc)
30 * \author Keith Whitwell
31 */
32
33 #include "glheader.h"
34 #include "api_validate.h"
35 #include "context.h"
36 #include "imports.h"
37 #include "macros.h"
38 #include "mtypes.h"
39 #include "state.h"
40
41 #include "array_cache/acache.h"
42
43 #include "t_array_api.h"
44 #include "t_array_import.h"
45 #include "t_imm_api.h"
46 #include "t_imm_exec.h"
47 #include "t_context.h"
48 #include "t_pipeline.h"
49
50 static void fallback_drawarrays( GLcontext *ctx, GLenum mode, GLint start,
51 GLsizei count )
52 {
53 if (_tnl_hard_begin( ctx, mode )) {
54 GLint i;
55 for (i = start; i < count; i++)
56 glArrayElement( i );
57 glEnd();
58 }
59 }
60
61
62 static void fallback_drawelements( GLcontext *ctx, GLenum mode, GLsizei count,
63 const GLuint *indices)
64 {
65 if (_tnl_hard_begin(ctx, mode)) {
66 GLint i;
67 for (i = 0 ; i < count ; i++)
68 glArrayElement( indices[i] );
69 glEnd();
70 }
71 }
72
73
74 static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
75 GLuint start, GLuint end,
76 GLsizei count, const GLuint *indices )
77
78 {
79 TNLcontext *tnl = TNL_CONTEXT(ctx);
80 FLUSH_CURRENT( ctx, 0 );
81
82 /* _mesa_debug(ctx, "%s\n", __FUNCTION__); */
83 if (tnl->pipeline.build_state_changes)
84 _tnl_validate_pipeline( ctx );
85
86 _tnl_vb_bind_arrays( ctx, start, end );
87
88 tnl->vb.FirstPrimitive = 0;
89 tnl->vb.Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
90 tnl->vb.PrimitiveLength[0] = count;
91 tnl->vb.Elts = (GLuint *)indices;
92
93 if (ctx->Array.LockCount)
94 tnl->Driver.RunPipeline( ctx );
95 else {
96 /* Note that arrays may have changed before/after execution.
97 */
98 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
99 tnl->Driver.RunPipeline( ctx );
100 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
101 }
102 }
103
104
105
106 /**
107 * Called via the GL API dispatcher.
108 */
109 void
110 _tnl_DrawArrays(GLenum mode, GLint start, GLsizei count)
111 {
112 GET_CURRENT_CONTEXT(ctx);
113 TNLcontext *tnl = TNL_CONTEXT(ctx);
114 struct vertex_buffer *VB = &tnl->vb;
115 GLuint thresh = (ctx->Driver.NeedFlush & FLUSH_STORED_VERTICES) ? 30 : 10;
116
117 if (MESA_VERBOSE & VERBOSE_API)
118 _mesa_debug(NULL, "_tnl_DrawArrays %d %d\n", start, count);
119
120 /* Check arguments, etc.
121 */
122 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
123 return;
124
125 if (tnl->pipeline.build_state_changes)
126 _tnl_validate_pipeline( ctx );
127
128 if (ctx->CompileFlag) {
129 fallback_drawarrays( ctx, mode, start, start + count );
130 }
131 else if (!ctx->Array.LockCount && (GLuint) count < thresh) {
132 /* Small primitives: attempt to share a vb (at the expense of
133 * using the immediate interface).
134 */
135 fallback_drawarrays( ctx, mode, start, start + count );
136 }
137 else if (ctx->Array.LockCount &&
138 count < (GLint) ctx->Const.MaxArrayLockSize) {
139
140 /* Locked primitives which can fit in a single vertex buffer:
141 */
142 FLUSH_CURRENT( ctx, 0 );
143
144 if (start < (GLint) ctx->Array.LockFirst)
145 start = ctx->Array.LockFirst;
146 if (start + count > (GLint) ctx->Array.LockCount)
147 count = ctx->Array.LockCount - start;
148
149 /* Locked drawarrays. Reuse any previously transformed data.
150 */
151 _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
152 VB->FirstPrimitive = start;
153 VB->Primitive[start] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
154 VB->PrimitiveLength[start] = count;
155 tnl->Driver.RunPipeline( ctx );
156 }
157 else {
158 int bufsz = 256; /* Use a small buffer for cache goodness */
159 int j, nr;
160 int minimum, modulo, skip;
161
162 /* Large primitives requiring decomposition to multiple vertex
163 * buffers:
164 */
165 switch (mode) {
166 case GL_POINTS:
167 minimum = 0;
168 modulo = 1;
169 skip = 0;
170 break;
171 case GL_LINES:
172 minimum = 1;
173 modulo = 2;
174 skip = 1;
175 break;
176 case GL_LINE_STRIP:
177 minimum = 1;
178 modulo = 1;
179 skip = 0;
180 break;
181 case GL_TRIANGLES:
182 minimum = 2;
183 modulo = 3;
184 skip = 2;
185 break;
186 case GL_TRIANGLE_STRIP:
187 minimum = 2;
188 modulo = 1;
189 skip = 0;
190 break;
191 case GL_QUADS:
192 minimum = 3;
193 modulo = 4;
194 skip = 3;
195 break;
196 case GL_QUAD_STRIP:
197 minimum = 3;
198 modulo = 2;
199 skip = 0;
200 break;
201 case GL_LINE_LOOP:
202 case GL_TRIANGLE_FAN:
203 case GL_POLYGON:
204 default:
205 /* Primitives requiring a copied vertex (fan-like primitives)
206 * must use the slow path if they cannot fit in a single
207 * vertex buffer.
208 */
209 if (count < (GLint) ctx->Const.MaxArrayLockSize) {
210 bufsz = ctx->Const.MaxArrayLockSize;
211 minimum = 0;
212 modulo = 1;
213 skip = 0;
214 }
215 else {
216 fallback_drawarrays( ctx, mode, start, start + count );
217 return;
218 }
219 }
220
221 FLUSH_CURRENT( ctx, 0 );
222
223 bufsz -= bufsz % modulo;
224 bufsz -= minimum;
225 count += start;
226
227 for (j = start + minimum ; j < count ; j += nr + skip ) {
228
229 nr = MIN2( bufsz, count - j );
230
231 _tnl_vb_bind_arrays( ctx, j - minimum, j + nr );
232
233 VB->FirstPrimitive = 0;
234 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
235 VB->PrimitiveLength[0] = nr + minimum;
236 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
237 tnl->Driver.RunPipeline( ctx );
238 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
239 }
240 }
241 }
242
243
244 /**
245 * Called via the GL API dispatcher.
246 */
247 void
248 _tnl_DrawRangeElements(GLenum mode,
249 GLuint start, GLuint end,
250 GLsizei count, GLenum type, const GLvoid *indices)
251 {
252 GET_CURRENT_CONTEXT(ctx);
253 GLuint *ui_indices;
254
255 if (MESA_VERBOSE & VERBOSE_API)
256 _mesa_debug(NULL, "_tnl_DrawRangeElements %d %d %d\n", start, end, count);
257
258 /* Check arguments, etc.
259 */
260 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
261 type, indices ))
262 return;
263
264 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
265 count, type, indices );
266
267
268 if (ctx->CompileFlag) {
269 /* Can't do anything when compiling:
270 */
271 fallback_drawelements( ctx, mode, count, ui_indices );
272 }
273 else if (ctx->Array.LockCount) {
274 /* Are the arrays already locked? If so we currently have to look
275 * at the whole locked range.
276 */
277 if (start >= ctx->Array.LockFirst && end <= ctx->Array.LockCount)
278 _tnl_draw_range_elements( ctx, mode,
279 ctx->Array.LockFirst,
280 ctx->Array.LockCount,
281 count, ui_indices );
282 else {
283 /* The spec says referencing elements outside the locked
284 * range is undefined. I'm going to make it a noop this time
285 * round, maybe come up with something beter before 3.6.
286 *
287 * May be able to get away with just setting LockCount==0,
288 * though this raises the problems of dependent state. May
289 * have to call glUnlockArrays() directly?
290 *
291 * Or scan the list and replace bad indices?
292 */
293 _mesa_problem( ctx,
294 "DrawRangeElements references "
295 "elements outside locked range.");
296 }
297 }
298 else if (end + 1 - start < ctx->Const.MaxArrayLockSize) {
299 /* The arrays aren't locked but we can still fit them inside a
300 * single vertexbuffer.
301 */
302 _tnl_draw_range_elements( ctx, mode, start, end + 1, count, ui_indices );
303 } else {
304 /* Range is too big to optimize:
305 */
306 fallback_drawelements( ctx, mode, count, ui_indices );
307 }
308 }
309
310
311
312 /**
313 * Called via the GL API dispatcher.
314 */
315 void
316 _tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
317 const GLvoid *indices)
318 {
319 GET_CURRENT_CONTEXT(ctx);
320 GLuint *ui_indices;
321
322 if (MESA_VERBOSE & VERBOSE_API)
323 _mesa_debug(NULL, "_tnl_DrawElements %d\n", count);
324
325 /* Check arguments, etc.
326 */
327 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
328 return;
329
330 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
331 count, type, indices );
332
333 if (ctx->CompileFlag) {
334 /* Can't do anything when compiling:
335 */
336 fallback_drawelements( ctx, mode, count, ui_indices );
337 }
338 else if (ctx->Array.LockCount) {
339 _tnl_draw_range_elements( ctx, mode,
340 ctx->Array.LockFirst,
341 ctx->Array.LockCount,
342 count, ui_indices );
343 }
344 else {
345 /* Scan the index list and see if we can use the locked path anyway.
346 */
347 GLuint max_elt = 0;
348 GLint i;
349
350 for (i = 0 ; i < count ; i++)
351 if (ui_indices[i] > max_elt)
352 max_elt = ui_indices[i];
353
354 if (max_elt < ctx->Const.MaxArrayLockSize && /* can we use it? */
355 max_elt < (GLuint) count) /* do we want to use it? */
356 _tnl_draw_range_elements( ctx, mode, 0, max_elt+1, count, ui_indices );
357 else
358 fallback_drawelements( ctx, mode, count, ui_indices );
359 }
360 }
361
362
363 /**
364 * Initialize context's vertex array fields. Called during T 'n L context
365 * creation.
366 */
367 void _tnl_array_init( GLcontext *ctx )
368 {
369 TNLcontext *tnl = TNL_CONTEXT(ctx);
370 struct vertex_arrays *tmp = &tnl->array_inputs;
371 GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->vtxfmt);
372 GLuint i;
373
374 vfmt->DrawArrays = _tnl_DrawArrays;
375 vfmt->DrawElements = _tnl_DrawElements;
376 vfmt->DrawRangeElements = _tnl_DrawRangeElements;
377
378 /* Setup vector pointers that will be used to bind arrays to VB's.
379 */
380 _mesa_vector4f_init( &tmp->Obj, 0, 0 );
381 _mesa_vector4f_init( &tmp->Normal, 0, 0 );
382 _mesa_vector4f_init( &tmp->FogCoord, 0, 0 );
383 _mesa_vector1ui_init( &tmp->Index, 0, 0 );
384 _mesa_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
385
386 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
387 _mesa_vector4f_init( &tmp->TexCoord[i], 0, 0);
388
389 tnl->tmp_primitive = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
390 tnl->tmp_primitive_length = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
391 }
392
393
394 /**
395 * Destroy the context's vertex array stuff.
396 * Called during T 'n L context destruction.
397 */
398 void _tnl_array_destroy( GLcontext *ctx )
399 {
400 TNLcontext *tnl = TNL_CONTEXT(ctx);
401 if (tnl->tmp_primitive_length) FREE(tnl->tmp_primitive_length);
402 if (tnl->tmp_primitive) FREE(tnl->tmp_primitive);
403 }