1 /* $Id: t_array_api.c,v 1.22 2001/12/03 17:41:58 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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.
27 * Keith Whitwell <keithw@valinux.com>
31 #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
++) {
54 _tnl_array_element( ctx
, i
);
61 static void fallback_drawelements( GLcontext
*ctx
, GLenum mode
, GLsizei count
,
62 const GLuint
*indices
)
64 /* Simple version of the above code.
66 if (_tnl_hard_begin(ctx
, mode
)) {
68 for (i
= 0 ; i
< count
; i
++)
69 _tnl_array_element( ctx
, 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 _tnl_vb_bind_arrays( ctx
, start
, end
);
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
;
90 if (ctx
->Array
.LockCount
)
91 tnl
->Driver
.RunPipeline( ctx
);
93 /* Note that arrays may have changed before/after execution.
95 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
96 tnl
->Driver
.RunPipeline( ctx
);
97 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
105 _tnl_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
107 GET_CURRENT_CONTEXT(ctx
);
108 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
109 struct vertex_buffer
*VB
= &tnl
->vb
;
111 /* fprintf(stderr, "%s %d %d\n", __FUNCTION__, start, count); */
113 /* Check arguments, etc.
115 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
118 if (tnl
->pipeline
.build_state_changes
)
119 _tnl_validate_pipeline( ctx
);
121 if (ctx
->CompileFlag
) {
122 fallback_drawarrays( ctx
, mode
, start
, start
+ count
);
124 else if (ctx
->Array
.LockCount
&&
125 count
< (GLint
) ctx
->Const
.MaxArrayLockSize
) {
127 /* Small primitives which can fit in a single vertex buffer:
129 FLUSH_CURRENT( ctx
, 0 );
131 if (start
< (GLint
) ctx
->Array
.LockFirst
)
132 start
= ctx
->Array
.LockFirst
;
133 if (start
+ count
> (GLint
) ctx
->Array
.LockCount
)
134 count
= ctx
->Array
.LockCount
- start
;
136 /* Locked drawarrays. Reuse any previously transformed data.
138 _tnl_vb_bind_arrays( ctx
, ctx
->Array
.LockFirst
, ctx
->Array
.LockCount
);
139 VB
->FirstPrimitive
= start
;
140 VB
->Primitive
[start
] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
141 VB
->PrimitiveLength
[start
] = count
;
142 tnl
->Driver
.RunPipeline( ctx
);
145 int bufsz
= 256; /* Use a small buffer for cache goodness */
147 int minimum
, modulo
, skip
;
149 /* Large primitives requiring decomposition to multiple vertex
171 case GL_TRIANGLE_STRIP
:
187 case GL_TRIANGLE_FAN
:
190 /* Primitives requiring a copied vertex (fan-like primitives)
191 * must use the slow path if they cannot fit in a single
194 if (count
< (GLint
) ctx
->Const
.MaxArrayLockSize
) {
195 bufsz
= ctx
->Const
.MaxArrayLockSize
;
201 fallback_drawarrays( ctx
, mode
, start
, start
+ count
);
206 FLUSH_CURRENT( ctx
, 0 );
208 /* fprintf(stderr, "start %d count %d min %d modulo %d skip %d\n", */
209 /* start, count, minimum, modulo, skip); */
212 bufsz
-= bufsz
% modulo
;
216 for (j
= start
+ minimum
; j
< count
; j
+= nr
+ skip
) {
218 nr
= MIN2( bufsz
, count
- j
);
220 /* fprintf(stderr, "%d..%d\n", j - minimum, j+nr); */
222 _tnl_vb_bind_arrays( ctx
, j
- minimum
, j
+ nr
);
224 VB
->FirstPrimitive
= 0;
225 VB
->Primitive
[0] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
226 VB
->PrimitiveLength
[0] = nr
+ minimum
;
227 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
228 tnl
->Driver
.RunPipeline( ctx
);
229 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
236 _tnl_DrawRangeElements(GLenum mode
,
237 GLuint start
, GLuint end
,
238 GLsizei count
, GLenum type
, const GLvoid
*indices
)
240 GET_CURRENT_CONTEXT(ctx
);
241 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
244 /* fprintf(stderr, "%s\n", __FUNCTION__); */
246 /* Check arguments, etc.
248 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
252 if (tnl
->pipeline
.build_state_changes
)
253 _tnl_validate_pipeline( ctx
);
255 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
256 count
, type
, indices
);
259 if (ctx
->CompileFlag
) {
260 /* Can't do anything when compiling:
262 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
264 else if (ctx
->Array
.LockCount
) {
265 /* Are the arrays already locked? If so we currently have to look
266 * at the whole locked range.
268 if (start
>= ctx
->Array
.LockFirst
&& end
<= ctx
->Array
.LockCount
)
269 _tnl_draw_range_elements( ctx
, mode
,
270 ctx
->Array
.LockFirst
,
271 ctx
->Array
.LockCount
,
274 /* The spec says referencing elements outside the locked
275 * range is undefined. I'm going to make it a noop this time
276 * round, maybe come up with something beter before 3.6.
278 * May be able to get away with just setting LockCount==0,
279 * though this raises the problems of dependent state. May
280 * have to call glUnlockArrays() directly?
282 * Or scan the list and replace bad indices?
285 "DrawRangeElements references "
286 "elements outside locked range.");
289 else if (end
+ 1 - start
< ctx
->Const
.MaxArrayLockSize
) {
290 /* The arrays aren't locked but we can still fit them inside a
291 * single vertexbuffer.
293 _tnl_draw_range_elements( ctx
, mode
, start
, end
+ 1, count
, ui_indices
);
295 /* Range is too big to optimize:
297 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
304 _tnl_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
305 const GLvoid
*indices
)
307 GET_CURRENT_CONTEXT(ctx
);
308 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
311 /* fprintf(stderr, "%s\n", __FUNCTION__); */
313 /* Check arguments, etc.
315 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
318 if (tnl
->pipeline
.build_state_changes
)
319 _tnl_validate_pipeline( ctx
);
321 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
322 count
, type
, indices
);
324 if (ctx
->CompileFlag
) {
325 /* Can't do anything when compiling:
327 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
329 else if (ctx
->Array
.LockCount
) {
330 _tnl_draw_range_elements( ctx
, mode
,
331 ctx
->Array
.LockFirst
,
332 ctx
->Array
.LockCount
,
336 /* Scan the index list and see if we can use the locked path anyway.
341 for (i
= 0 ; i
< count
; i
++)
342 if (ui_indices
[i
] > max_elt
)
343 max_elt
= ui_indices
[i
];
345 if (max_elt
< ctx
->Const
.MaxArrayLockSize
&& /* can we use it? */
346 max_elt
< (GLuint
) count
) /* do we want to use it? */
347 _tnl_draw_range_elements( ctx
, mode
, 0, max_elt
+1, count
, ui_indices
);
349 fallback_drawelements( ctx
, mode
, count
, ui_indices
);
354 void _tnl_array_init( GLcontext
*ctx
)
356 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
357 struct vertex_arrays
*tmp
= &tnl
->array_inputs
;
358 GLvertexformat
*vfmt
= &(TNL_CONTEXT(ctx
)->vtxfmt
);
361 vfmt
->DrawArrays
= _tnl_DrawArrays
;
362 vfmt
->DrawElements
= _tnl_DrawElements
;
363 vfmt
->DrawRangeElements
= _tnl_DrawRangeElements
;
365 /* Setup vector pointers that will be used to bind arrays to VB's.
367 _mesa_vector4f_init( &tmp
->Obj
, 0, 0 );
368 _mesa_vector3f_init( &tmp
->Normal
, 0, 0 );
369 _mesa_vector1f_init( &tmp
->FogCoord
, 0, 0 );
370 _mesa_vector1ui_init( &tmp
->Index
, 0, 0 );
371 _mesa_vector1ub_init( &tmp
->EdgeFlag
, 0, 0 );
373 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++)
374 _mesa_vector4f_init( &tmp
->TexCoord
[i
], 0, 0);
376 tnl
->tmp_primitive
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
377 tnl
->tmp_primitive_length
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
381 void _tnl_array_destroy( GLcontext
*ctx
)
383 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
384 if (tnl
->tmp_primitive_length
) FREE(tnl
->tmp_primitive_length
);
385 if (tnl
->tmp_primitive
) FREE(tnl
->tmp_primitive
);