2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * \brief Vertex array API functions (glDrawArrays, etc)
28 * \author Keith Whitwell
32 #include "api_validate.h"
39 #include "array_cache/acache.h"
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"
48 static void fallback_drawarrays( GLcontext
*ctx
, GLenum mode
, GLint start
,
51 if (_tnl_hard_begin( ctx
, mode
)) {
53 for (i
= start
; i
< count
; i
++)
60 static void fallback_drawelements( GLcontext
*ctx
, GLenum mode
, GLsizei count
,
61 const GLuint
*indices
)
63 if (_tnl_hard_begin(ctx
, mode
)) {
65 for (i
= 0 ; i
< count
; i
++)
66 glArrayElement( indices
[i
] );
72 static void _tnl_draw_range_elements( GLcontext
*ctx
, GLenum mode
,
73 GLuint start
, GLuint end
,
74 GLsizei count
, GLuint
*indices
)
77 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
79 FLUSH_CURRENT( ctx
, 0 );
81 /* _mesa_debug(ctx, "%s\n", __FUNCTION__); */
82 if (tnl
->pipeline
.build_state_changes
)
83 _tnl_validate_pipeline( ctx
);
85 _tnl_vb_bind_arrays( ctx
, start
, end
);
87 tnl
->vb
.FirstPrimitive
= 0;
88 tnl
->vb
.Primitive
[0] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
89 tnl
->vb
.PrimitiveLength
[0] = count
;
90 tnl
->vb
.Elts
= (GLuint
*)indices
;
92 for (i
= 0 ; i
< count
; i
++)
95 if (ctx
->Array
.LockCount
)
96 tnl
->Driver
.RunPipeline( ctx
);
98 /* Note that arrays may have changed before/after execution.
100 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
101 tnl
->Driver
.RunPipeline( ctx
);
102 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
105 for (i
= 0 ; i
< count
; i
++)
112 * Called via the GL API dispatcher.
115 _tnl_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
117 GET_CURRENT_CONTEXT(ctx
);
118 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
119 struct vertex_buffer
*VB
= &tnl
->vb
;
120 GLuint thresh
= (ctx
->Driver
.NeedFlush
& FLUSH_STORED_VERTICES
) ? 30 : 10;
122 if (MESA_VERBOSE
& VERBOSE_API
)
123 _mesa_debug(NULL
, "_tnl_DrawArrays %d %d\n", start
, count
);
125 /* Check arguments, etc.
127 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
130 if (tnl
->pipeline
.build_state_changes
)
131 _tnl_validate_pipeline( ctx
);
133 if (ctx
->CompileFlag
) {
134 fallback_drawarrays( ctx
, mode
, start
, start
+ count
);
136 else if (!ctx
->Array
.LockCount
&& (GLuint
) count
< thresh
) {
137 /* Small primitives: attempt to share a vb (at the expense of
138 * using the immediate interface).
140 fallback_drawarrays( ctx
, mode
, start
, start
+ count
);
142 else if (ctx
->Array
.LockCount
&&
143 count
< (GLint
) ctx
->Const
.MaxArrayLockSize
) {
145 /* Locked primitives which can fit in a single vertex buffer:
147 FLUSH_CURRENT( ctx
, 0 );
149 if (start
< (GLint
) ctx
->Array
.LockFirst
)
150 start
= ctx
->Array
.LockFirst
;
151 if (start
+ count
> (GLint
) ctx
->Array
.LockCount
)
152 count
= ctx
->Array
.LockCount
- start
;
154 /* Locked drawarrays. Reuse any previously transformed data.
156 _tnl_vb_bind_arrays( ctx
, ctx
->Array
.LockFirst
, ctx
->Array
.LockCount
);
157 VB
->FirstPrimitive
= start
;
158 VB
->Primitive
[start
] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
159 VB
->PrimitiveLength
[start
] = count
;
160 tnl
->Driver
.RunPipeline( ctx
);
163 int bufsz
= 256; /* Use a small buffer for cache goodness */
165 int minimum
, modulo
, skip
;
167 /* Large primitives requiring decomposition to multiple vertex
191 case GL_TRIANGLE_STRIP
:
207 case GL_TRIANGLE_FAN
:
210 /* Primitives requiring a copied vertex (fan-like primitives)
211 * must use the slow path if they cannot fit in a single
214 if (count
< (GLint
) ctx
->Const
.MaxArrayLockSize
) {
215 bufsz
= ctx
->Const
.MaxArrayLockSize
;
221 fallback_drawarrays( ctx
, mode
, start
, start
+ count
);
226 FLUSH_CURRENT( ctx
, 0 );
228 bufsz
-= bufsz
% modulo
;
232 for (j
= start
+ minimum
; j
< count
; j
+= nr
+ skip
) {
234 nr
= MIN2( bufsz
, count
- j
);
236 _tnl_vb_bind_arrays( ctx
, j
- minimum
, j
+ nr
);
238 VB
->FirstPrimitive
= 0;
239 VB
->Primitive
[0] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
240 VB
->PrimitiveLength
[0] = nr
+ minimum
;
241 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
242 tnl
->Driver
.RunPipeline( ctx
);
243 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
250 * Called via the GL API dispatcher.
253 _tnl_DrawRangeElements(GLenum mode
,
254 GLuint start
, GLuint end
,
255 GLsizei count
, GLenum type
, const GLvoid
*indices
)
257 GET_CURRENT_CONTEXT(ctx
);
260 if (MESA_VERBOSE
& VERBOSE_API
)
261 _mesa_debug(NULL
, "_tnl_DrawRangeElements %d %d %d\n", start
, end
, count
);
263 /* Check arguments, etc.
265 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
269 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
270 count
, type
, indices
);
273 if (ctx
->CompileFlag
) {
274 /* Can't do anything when compiling:
276 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
278 else if (ctx
->Array
.LockCount
) {
279 /* Are the arrays already locked? If so we currently have to look
280 * at the whole locked range.
282 if (start
>= ctx
->Array
.LockFirst
&& end
<= ctx
->Array
.LockCount
)
283 _tnl_draw_range_elements( ctx
, mode
,
284 ctx
->Array
.LockFirst
,
285 ctx
->Array
.LockCount
,
288 /* The spec says referencing elements outside the locked
289 * range is undefined. I'm going to make it a noop this time
290 * round, maybe come up with something beter before 3.6.
292 * May be able to get away with just setting LockCount==0,
293 * though this raises the problems of dependent state. May
294 * have to call glUnlockArrays() directly?
296 * Or scan the list and replace bad indices?
299 "DrawRangeElements references "
300 "elements outside locked range.");
303 else if (end
+ 1 - start
< ctx
->Const
.MaxArrayLockSize
) {
304 /* The arrays aren't locked but we can still fit them inside a
305 * single vertexbuffer.
307 _tnl_draw_range_elements( ctx
, mode
, start
, end
+ 1, count
, ui_indices
);
309 /* Range is too big to optimize:
311 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
318 * Called via the GL API dispatcher.
321 _tnl_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
322 const GLvoid
*indices
)
324 GET_CURRENT_CONTEXT(ctx
);
327 if (MESA_VERBOSE
& VERBOSE_API
)
328 _mesa_debug(NULL
, "_tnl_DrawElements %d\n", count
);
330 /* Check arguments, etc.
332 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
335 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
336 count
, type
, indices
);
338 if (ctx
->CompileFlag
) {
339 /* Can't do anything when compiling:
341 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
343 else if (ctx
->Array
.LockCount
) {
344 _tnl_draw_range_elements( ctx
, mode
,
345 ctx
->Array
.LockFirst
,
346 ctx
->Array
.LockCount
,
350 /* Scan the index list and see if we can use the locked path anyway.
355 for (i
= 0 ; i
< count
; i
++)
356 if (ui_indices
[i
] > max_elt
)
357 max_elt
= ui_indices
[i
];
359 if (max_elt
< ctx
->Const
.MaxArrayLockSize
&& /* can we use it? */
360 max_elt
< (GLuint
) count
) /* do we want to use it? */
361 _tnl_draw_range_elements( ctx
, mode
, 0, max_elt
+1, count
, ui_indices
);
363 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
369 * Initialize context's vertex array fields. Called during T 'n L context
372 void _tnl_array_init( GLcontext
*ctx
)
374 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
375 struct vertex_arrays
*tmp
= &tnl
->array_inputs
;
376 GLvertexformat
*vfmt
= &(TNL_CONTEXT(ctx
)->vtxfmt
);
379 vfmt
->DrawArrays
= _tnl_DrawArrays
;
380 vfmt
->DrawElements
= _tnl_DrawElements
;
381 vfmt
->DrawRangeElements
= _tnl_DrawRangeElements
;
383 /* Setup vector pointers that will be used to bind arrays to VB's.
385 _mesa_vector4f_init( &tmp
->Obj
, 0, 0 );
386 _mesa_vector4f_init( &tmp
->Normal
, 0, 0 );
387 _mesa_vector4f_init( &tmp
->FogCoord
, 0, 0 );
388 _mesa_vector1ui_init( &tmp
->Index
, 0, 0 );
389 _mesa_vector1ub_init( &tmp
->EdgeFlag
, 0, 0 );
391 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++)
392 _mesa_vector4f_init( &tmp
->TexCoord
[i
], 0, 0);
394 tnl
->tmp_primitive
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
395 tnl
->tmp_primitive_length
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
400 * Destroy the context's vertex array stuff.
401 * Called during T 'n L context destruction.
403 void _tnl_array_destroy( GLcontext
*ctx
)
405 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
406 if (tnl
->tmp_primitive_length
) FREE(tnl
->tmp_primitive_length
);
407 if (tnl
->tmp_primitive
) FREE(tnl
->tmp_primitive
);