1 /* $Id: t_array_api.c,v 1.29 2002/12/19 18:36:01 keithw 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
++)
63 static void fallback_drawelements( GLcontext
*ctx
, GLenum mode
, GLsizei count
,
64 const GLuint
*indices
)
66 if (_tnl_hard_begin(ctx
, mode
)) {
68 for (i
= 0 ; i
< count
; i
++)
69 glArrayElement( indices
[i
] );
75 static void _tnl_draw_range_elements( GLcontext
*ctx
, GLenum mode
,
76 GLuint start
, GLuint end
,
77 GLsizei count
, const GLuint
*indices
)
80 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
81 FLUSH_CURRENT( ctx
, 0 );
83 /* _mesa_debug(ctx, "%s\n", __FUNCTION__); */
84 if (tnl
->pipeline
.build_state_changes
)
85 _tnl_validate_pipeline( ctx
);
87 _tnl_vb_bind_arrays( ctx
, start
, end
);
89 tnl
->vb
.FirstPrimitive
= 0;
90 tnl
->vb
.Primitive
[0] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
91 tnl
->vb
.PrimitiveLength
[0] = count
;
92 tnl
->vb
.Elts
= (GLuint
*)indices
;
94 if (ctx
->Array
.LockCount
)
95 tnl
->Driver
.RunPipeline( ctx
);
97 /* Note that arrays may have changed before/after execution.
99 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
100 tnl
->Driver
.RunPipeline( ctx
);
101 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
108 * Called via the GL API dispatcher.
111 _tnl_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
113 GET_CURRENT_CONTEXT(ctx
);
114 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
115 struct vertex_buffer
*VB
= &tnl
->vb
;
116 GLuint thresh
= (ctx
->Driver
.NeedFlush
& FLUSH_STORED_VERTICES
) ? 30 : 10;
118 if (MESA_VERBOSE
& VERBOSE_API
)
119 _mesa_debug(NULL
, "_tnl_DrawArrays %d %d\n", start
, count
);
121 /* Check arguments, etc.
123 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
126 if (tnl
->pipeline
.build_state_changes
)
127 _tnl_validate_pipeline( ctx
);
129 if (ctx
->CompileFlag
) {
130 fallback_drawarrays( ctx
, mode
, start
, start
+ count
);
132 else if (!ctx
->Array
.LockCount
&& (GLuint
) count
< thresh
) {
133 /* Small primitives: attempt to share a vb (at the expense of
134 * using the immediate interface).
136 fallback_drawarrays( ctx
, mode
, start
, start
+ count
);
138 else if (ctx
->Array
.LockCount
&&
139 count
< (GLint
) ctx
->Const
.MaxArrayLockSize
) {
141 /* Locked primitives which can fit in a single vertex buffer:
143 FLUSH_CURRENT( ctx
, 0 );
145 if (start
< (GLint
) ctx
->Array
.LockFirst
)
146 start
= ctx
->Array
.LockFirst
;
147 if (start
+ count
> (GLint
) ctx
->Array
.LockCount
)
148 count
= ctx
->Array
.LockCount
- start
;
150 /* Locked drawarrays. Reuse any previously transformed data.
152 _tnl_vb_bind_arrays( ctx
, ctx
->Array
.LockFirst
, ctx
->Array
.LockCount
);
153 VB
->FirstPrimitive
= start
;
154 VB
->Primitive
[start
] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
155 VB
->PrimitiveLength
[start
] = count
;
156 tnl
->Driver
.RunPipeline( ctx
);
159 int bufsz
= 256; /* Use a small buffer for cache goodness */
161 int minimum
, modulo
, skip
;
163 /* Large primitives requiring decomposition to multiple vertex
187 case GL_TRIANGLE_STRIP
:
203 case GL_TRIANGLE_FAN
:
206 /* Primitives requiring a copied vertex (fan-like primitives)
207 * must use the slow path if they cannot fit in a single
210 if (count
< (GLint
) ctx
->Const
.MaxArrayLockSize
) {
211 bufsz
= ctx
->Const
.MaxArrayLockSize
;
217 fallback_drawarrays( ctx
, mode
, start
, start
+ count
);
222 FLUSH_CURRENT( ctx
, 0 );
224 bufsz
-= bufsz
% modulo
;
228 for (j
= start
+ minimum
; j
< count
; j
+= nr
+ skip
) {
230 nr
= MIN2( bufsz
, count
- j
);
232 _tnl_vb_bind_arrays( ctx
, j
- minimum
, j
+ nr
);
234 VB
->FirstPrimitive
= 0;
235 VB
->Primitive
[0] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
236 VB
->PrimitiveLength
[0] = nr
+ minimum
;
237 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
238 tnl
->Driver
.RunPipeline( ctx
);
239 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
246 * Called via the GL API dispatcher.
249 _tnl_DrawRangeElements(GLenum mode
,
250 GLuint start
, GLuint end
,
251 GLsizei count
, GLenum type
, const GLvoid
*indices
)
253 GET_CURRENT_CONTEXT(ctx
);
256 if (MESA_VERBOSE
& VERBOSE_API
)
257 _mesa_debug(NULL
, "_tnl_DrawRangeElements %d %d %d\n", start
, end
, count
);
259 /* Check arguments, etc.
261 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
265 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
266 count
, type
, indices
);
269 if (ctx
->CompileFlag
) {
270 /* Can't do anything when compiling:
272 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
274 else if (ctx
->Array
.LockCount
) {
275 /* Are the arrays already locked? If so we currently have to look
276 * at the whole locked range.
278 if (start
>= ctx
->Array
.LockFirst
&& end
<= ctx
->Array
.LockCount
)
279 _tnl_draw_range_elements( ctx
, mode
,
280 ctx
->Array
.LockFirst
,
281 ctx
->Array
.LockCount
,
284 /* The spec says referencing elements outside the locked
285 * range is undefined. I'm going to make it a noop this time
286 * round, maybe come up with something beter before 3.6.
288 * May be able to get away with just setting LockCount==0,
289 * though this raises the problems of dependent state. May
290 * have to call glUnlockArrays() directly?
292 * Or scan the list and replace bad indices?
295 "DrawRangeElements references "
296 "elements outside locked range.");
299 else if (end
+ 1 - start
< ctx
->Const
.MaxArrayLockSize
) {
300 /* The arrays aren't locked but we can still fit them inside a
301 * single vertexbuffer.
303 _tnl_draw_range_elements( ctx
, mode
, start
, end
+ 1, count
, ui_indices
);
305 /* Range is too big to optimize:
307 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
314 * Called via the GL API dispatcher.
317 _tnl_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
318 const GLvoid
*indices
)
320 GET_CURRENT_CONTEXT(ctx
);
323 if (MESA_VERBOSE
& VERBOSE_API
)
324 _mesa_debug(NULL
, "_tnl_DrawElements %d\n", count
);
326 /* Check arguments, etc.
328 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
331 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
332 count
, type
, indices
);
334 if (ctx
->CompileFlag
) {
335 /* Can't do anything when compiling:
337 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
339 else if (ctx
->Array
.LockCount
) {
340 _tnl_draw_range_elements( ctx
, mode
,
341 ctx
->Array
.LockFirst
,
342 ctx
->Array
.LockCount
,
346 /* Scan the index list and see if we can use the locked path anyway.
351 for (i
= 0 ; i
< count
; i
++)
352 if (ui_indices
[i
] > max_elt
)
353 max_elt
= ui_indices
[i
];
355 if (max_elt
< ctx
->Const
.MaxArrayLockSize
&& /* can we use it? */
356 max_elt
< (GLuint
) count
) /* do we want to use it? */
357 _tnl_draw_range_elements( ctx
, mode
, 0, max_elt
+1, count
, ui_indices
);
359 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
365 * Initialize context's vertex array fields. Called during T 'n L context
368 void _tnl_array_init( GLcontext
*ctx
)
370 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
371 struct vertex_arrays
*tmp
= &tnl
->array_inputs
;
372 GLvertexformat
*vfmt
= &(TNL_CONTEXT(ctx
)->vtxfmt
);
375 vfmt
->DrawArrays
= _tnl_DrawArrays
;
376 vfmt
->DrawElements
= _tnl_DrawElements
;
377 vfmt
->DrawRangeElements
= _tnl_DrawRangeElements
;
379 /* Setup vector pointers that will be used to bind arrays to VB's.
381 _mesa_vector4f_init( &tmp
->Obj
, 0, 0 );
382 _mesa_vector4f_init( &tmp
->Normal
, 0, 0 );
383 _mesa_vector4f_init( &tmp
->FogCoord
, 0, 0 );
384 _mesa_vector1ui_init( &tmp
->Index
, 0, 0 );
385 _mesa_vector1ub_init( &tmp
->EdgeFlag
, 0, 0 );
387 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++)
388 _mesa_vector4f_init( &tmp
->TexCoord
[i
], 0, 0);
390 tnl
->tmp_primitive
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
391 tnl
->tmp_primitive_length
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
396 * Destroy the context's vertex array stuff.
397 * Called during T 'n L context destruction.
399 void _tnl_array_destroy( GLcontext
*ctx
)
401 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
402 if (tnl
->tmp_primitive_length
) FREE(tnl
->tmp_primitive_length
);
403 if (tnl
->tmp_primitive
) FREE(tnl
->tmp_primitive
);