1 /* $Id: t_array_api.c,v 1.24 2002/01/22 14:35:16 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
29 * \brief Vertex array API functions (glDrawArrays, etc)
30 * \author Keith Whitwell
34 #include "api_validate.h"
42 #include "array_cache/acache.h"
44 #include "t_array_api.h"
45 #include "t_array_import.h"
46 #include "t_imm_api.h"
47 #include "t_imm_exec.h"
48 #include "t_context.h"
49 #include "t_pipeline.h"
51 static void fallback_drawarrays( GLcontext
*ctx
, GLenum mode
, GLint start
,
54 if (_tnl_hard_begin( ctx
, mode
)) {
56 for (i
= start
; i
< count
; i
++) {
57 _tnl_array_element( ctx
, i
);
64 static void fallback_drawelements( GLcontext
*ctx
, GLenum mode
, GLsizei count
,
65 const GLuint
*indices
)
67 /* Simple version of the above code.
69 if (_tnl_hard_begin(ctx
, mode
)) {
71 for (i
= 0 ; i
< count
; i
++)
72 _tnl_array_element( ctx
, indices
[i
] );
78 static void _tnl_draw_range_elements( GLcontext
*ctx
, GLenum mode
,
79 GLuint start
, GLuint end
,
80 GLsizei count
, const GLuint
*indices
)
83 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
84 FLUSH_CURRENT( ctx
, 0 );
86 _tnl_vb_bind_arrays( ctx
, start
, end
);
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
;
93 if (ctx
->Array
.LockCount
)
94 tnl
->Driver
.RunPipeline( ctx
);
96 /* Note that arrays may have changed before/after execution.
98 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
99 tnl
->Driver
.RunPipeline( ctx
);
100 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
107 * Called via the GL API dispatcher.
110 _tnl_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
112 GET_CURRENT_CONTEXT(ctx
);
113 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
114 struct vertex_buffer
*VB
= &tnl
->vb
;
116 /* fprintf(stderr, "%s %d %d\n", __FUNCTION__, start, count); */
118 /* Check arguments, etc.
120 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
123 if (tnl
->pipeline
.build_state_changes
)
124 _tnl_validate_pipeline( ctx
);
126 if (ctx
->CompileFlag
) {
127 fallback_drawarrays( ctx
, mode
, start
, start
+ count
);
129 else if (ctx
->Array
.LockCount
&&
130 count
< (GLint
) ctx
->Const
.MaxArrayLockSize
) {
132 /* Small primitives which can fit in a single vertex buffer:
134 FLUSH_CURRENT( ctx
, 0 );
136 if (start
< (GLint
) ctx
->Array
.LockFirst
)
137 start
= ctx
->Array
.LockFirst
;
138 if (start
+ count
> (GLint
) ctx
->Array
.LockCount
)
139 count
= ctx
->Array
.LockCount
- start
;
141 /* Locked drawarrays. Reuse any previously transformed data.
143 _tnl_vb_bind_arrays( ctx
, ctx
->Array
.LockFirst
, ctx
->Array
.LockCount
);
144 VB
->FirstPrimitive
= start
;
145 VB
->Primitive
[start
] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
146 VB
->PrimitiveLength
[start
] = count
;
147 tnl
->Driver
.RunPipeline( ctx
);
150 int bufsz
= 256; /* Use a small buffer for cache goodness */
152 int minimum
, modulo
, skip
;
154 /* Large primitives requiring decomposition to multiple vertex
176 case GL_TRIANGLE_STRIP
:
192 case GL_TRIANGLE_FAN
:
195 /* Primitives requiring a copied vertex (fan-like primitives)
196 * must use the slow path if they cannot fit in a single
199 if (count
< (GLint
) ctx
->Const
.MaxArrayLockSize
) {
200 bufsz
= ctx
->Const
.MaxArrayLockSize
;
206 fallback_drawarrays( ctx
, mode
, start
, start
+ count
);
211 FLUSH_CURRENT( ctx
, 0 );
213 /* fprintf(stderr, "start %d count %d min %d modulo %d skip %d\n", */
214 /* start, count, minimum, modulo, skip); */
217 bufsz
-= bufsz
% modulo
;
221 for (j
= start
+ minimum
; j
< count
; j
+= nr
+ skip
) {
223 nr
= MIN2( bufsz
, count
- j
);
225 /* fprintf(stderr, "%d..%d\n", j - minimum, j+nr); */
227 _tnl_vb_bind_arrays( ctx
, j
- minimum
, j
+ nr
);
229 VB
->FirstPrimitive
= 0;
230 VB
->Primitive
[0] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
231 VB
->PrimitiveLength
[0] = nr
+ minimum
;
232 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
233 tnl
->Driver
.RunPipeline( ctx
);
234 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
241 * Called via the GL API dispatcher.
244 _tnl_DrawRangeElements(GLenum mode
,
245 GLuint start
, GLuint end
,
246 GLsizei count
, GLenum type
, const GLvoid
*indices
)
248 GET_CURRENT_CONTEXT(ctx
);
249 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
252 /* fprintf(stderr, "%s\n", __FUNCTION__); */
254 /* Check arguments, etc.
256 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
260 if (tnl
->pipeline
.build_state_changes
)
261 _tnl_validate_pipeline( ctx
);
263 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
264 count
, type
, indices
);
267 if (ctx
->CompileFlag
) {
268 /* Can't do anything when compiling:
270 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
272 else if (ctx
->Array
.LockCount
) {
273 /* Are the arrays already locked? If so we currently have to look
274 * at the whole locked range.
276 if (start
>= ctx
->Array
.LockFirst
&& end
<= ctx
->Array
.LockCount
)
277 _tnl_draw_range_elements( ctx
, mode
,
278 ctx
->Array
.LockFirst
,
279 ctx
->Array
.LockCount
,
282 /* The spec says referencing elements outside the locked
283 * range is undefined. I'm going to make it a noop this time
284 * round, maybe come up with something beter before 3.6.
286 * May be able to get away with just setting LockCount==0,
287 * though this raises the problems of dependent state. May
288 * have to call glUnlockArrays() directly?
290 * Or scan the list and replace bad indices?
293 "DrawRangeElements references "
294 "elements outside locked range.");
297 else if (end
+ 1 - start
< ctx
->Const
.MaxArrayLockSize
) {
298 /* The arrays aren't locked but we can still fit them inside a
299 * single vertexbuffer.
301 _tnl_draw_range_elements( ctx
, mode
, start
, end
+ 1, count
, ui_indices
);
303 /* Range is too big to optimize:
305 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
312 * Called via the GL API dispatcher.
315 _tnl_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
316 const GLvoid
*indices
)
318 GET_CURRENT_CONTEXT(ctx
);
319 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
322 /* fprintf(stderr, "%s\n", __FUNCTION__); */
324 /* Check arguments, etc.
326 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
329 if (tnl
->pipeline
.build_state_changes
)
330 _tnl_validate_pipeline( ctx
);
332 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
333 count
, type
, indices
);
335 if (ctx
->CompileFlag
) {
336 /* Can't do anything when compiling:
338 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
340 else if (ctx
->Array
.LockCount
) {
341 _tnl_draw_range_elements( ctx
, mode
,
342 ctx
->Array
.LockFirst
,
343 ctx
->Array
.LockCount
,
347 /* Scan the index list and see if we can use the locked path anyway.
352 for (i
= 0 ; i
< count
; i
++)
353 if (ui_indices
[i
] > max_elt
)
354 max_elt
= ui_indices
[i
];
356 if (max_elt
< ctx
->Const
.MaxArrayLockSize
&& /* can we use it? */
357 max_elt
< (GLuint
) count
) /* do we want to use it? */
358 _tnl_draw_range_elements( ctx
, mode
, 0, max_elt
+1, count
, ui_indices
);
360 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
366 * Initialize context's vertex array fields. Called during T 'n L context
369 void _tnl_array_init( GLcontext
*ctx
)
371 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
372 struct vertex_arrays
*tmp
= &tnl
->array_inputs
;
373 GLvertexformat
*vfmt
= &(TNL_CONTEXT(ctx
)->vtxfmt
);
376 vfmt
->DrawArrays
= _tnl_DrawArrays
;
377 vfmt
->DrawElements
= _tnl_DrawElements
;
378 vfmt
->DrawRangeElements
= _tnl_DrawRangeElements
;
380 /* Setup vector pointers that will be used to bind arrays to VB's.
382 _mesa_vector4f_init( &tmp
->Obj
, 0, 0 );
383 _mesa_vector4f_init( &tmp
->Normal
, 0, 0 );
384 _mesa_vector4f_init( &tmp
->FogCoord
, 0, 0 );
385 _mesa_vector1ui_init( &tmp
->Index
, 0, 0 );
386 _mesa_vector1ub_init( &tmp
->EdgeFlag
, 0, 0 );
388 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++)
389 _mesa_vector4f_init( &tmp
->TexCoord
[i
], 0, 0);
391 tnl
->tmp_primitive
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
392 tnl
->tmp_primitive_length
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
397 * Destroy the context's vertex array stuff.
398 * Called during T 'n L context destruction.
400 void _tnl_array_destroy( GLcontext
*ctx
)
402 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
403 if (tnl
->tmp_primitive_length
) FREE(tnl
->tmp_primitive_length
);
404 if (tnl
->tmp_primitive
) FREE(tnl
->tmp_primitive
);