1 /* $Id: t_array_api.c,v 1.4 2001/01/24 00:04:59 brianp 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"
53 _tnl_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
55 GET_CURRENT_CONTEXT(ctx
);
56 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
57 struct vertex_buffer
*VB
= &tnl
->vb
;
59 /* Check arguments, etc.
61 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
64 if (tnl
->pipeline
.build_state_changes
)
65 _tnl_validate_pipeline( ctx
);
67 if (!ctx
->CompileFlag
&& count
- start
< ctx
->Const
.MaxArrayLockSize
) {
68 FLUSH_CURRENT( ctx
, 0 );
70 if (ctx
->Array
.LockCount
)
72 if (start
< ctx
->Array
.LockFirst
) start
= ctx
->Array
.LockFirst
;
73 if (count
> ctx
->Array
.LockCount
) count
= ctx
->Array
.LockCount
;
74 if (start
>= count
) return;
76 /* Locked drawarrays. Reuse any previously transformed data.
78 _tnl_vb_bind_arrays( ctx
, ctx
->Array
.LockFirst
, ctx
->Array
.LockCount
);
79 VB
->FirstPrimitive
= start
;
80 VB
->Primitive
[start
] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
81 VB
->PrimitiveLength
[start
] = count
- start
;
82 _tnl_run_pipeline( ctx
);
84 /* The arrays are small enough to fit in a single VB; just bind
85 * them and go. Any untransformed data will be copied on
88 * Invalidate any locked data dependent on these arrays.
90 _tnl_vb_bind_arrays( ctx
, start
, count
);
91 VB
->FirstPrimitive
= 0;
92 VB
->Primitive
[0] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
93 VB
->PrimitiveLength
[0] = count
- start
;
94 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
95 _tnl_run_pipeline( ctx
);
96 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
100 /* Need to produce immediate structs, either for compiling or
101 * because the array range is too large to process in a single
102 * VB. In GL_EXECUTE mode, this introduces two redundant
103 * operations: producing the flag array and computing the orflag
107 if (_tnl_hard_begin( ctx
, mode
)) {
109 for (j
= 0 ; j
< count
; ) {
110 struct immediate
*IM
= TNL_CURRENT_IM(ctx
);
111 GLuint nr
= MIN2( IMM_MAXDATA
- IM
->Start
, count
- j
);
112 GLuint sf
= IM
->Flag
[IM
->Start
];
114 _tnl_fill_immediate_drawarrays( ctx
, IM
, j
, j
+nr
);
116 if (j
== 0) IM
->Flag
[IM
->Start
] |= sf
;
118 IM
->Count
= IM
->Start
+ nr
;
124 _tnl_flush_immediate( IM
);
128 /* Simple alternative to above code.
130 if (_tnl_hard_begin( ctx
, mode
))
133 for (i
=start
;i
<count
;i
++) {
134 _tnl_array_element( ctx
, i
);
144 static void _tnl_draw_range_elements( GLcontext
*ctx
, GLenum mode
,
145 GLuint start
, GLuint end
,
146 GLsizei count
, const GLuint
*indices
)
149 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
150 FLUSH_CURRENT( ctx
, 0 );
152 _tnl_vb_bind_arrays( ctx
, start
, end
);
154 tnl
->vb
.FirstPrimitive
= 0;
155 tnl
->vb
.Primitive
[0] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
156 tnl
->vb
.PrimitiveLength
[0] = count
;
157 tnl
->vb
.Elts
= (GLuint
*)indices
;
159 if (ctx
->Array
.LockCount
)
160 _tnl_run_pipeline( ctx
);
162 /* Note that arrays may have changed before/after execution.
164 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
165 _tnl_run_pipeline( ctx
);
166 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
173 static void _tnl_draw_elements( GLcontext
*ctx
, GLenum mode
, GLsizei count
,
174 const GLuint
*indices
)
177 /* Optimized code that fakes the effect of calling
178 * _tnl_array_element for each index in the list.
180 if (_tnl_hard_begin( ctx
, mode
)) {
182 for (j
= 0 ; j
< count
; ) {
183 struct immediate
*IM
= TNL_CURRENT_IM(ctx
);
184 GLuint start
= IM
->Start
;
185 GLuint nr
= MIN2( IMM_MAXDATA
- start
, count
- j
) + start
;
186 GLuint sf
= IM
->Flag
[start
];
189 for (i
= start
; i
< nr
; i
++) {
190 IM
->Elt
[i
] = (GLuint
) *indices
++;
191 IM
->Flag
[i
] = VERT_ELT
;
194 if (j
== 0) IM
->Flag
[start
] |= sf
;
202 _tnl_flush_immediate( IM
);
206 /* Simple version of the above code.
208 if (_tnl_hard_begin(ctx
, mode
)) {
210 for (i
= 0 ; i
< count
; i
++)
211 _tnl_array_element( ctx
, indices
[i
] );
219 _tnl_DrawRangeElements(GLenum mode
,
220 GLuint start
, GLuint end
,
221 GLsizei count
, GLenum type
, const GLvoid
*indices
)
223 GET_CURRENT_CONTEXT(ctx
);
224 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
227 /* Check arguments, etc.
229 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
233 if (tnl
->pipeline
.build_state_changes
)
234 _tnl_validate_pipeline( ctx
);
236 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
237 count
, type
, indices
);
240 if (ctx
->Array
.LockCount
) {
241 /* Are the arrays already locked? If so we currently have to look
242 * at the whole locked range.
244 if (start
>= ctx
->Array
.LockFirst
&& end
<= ctx
->Array
.LockCount
)
245 _tnl_draw_range_elements( ctx
, mode
,
246 ctx
->Array
.LockFirst
,
247 ctx
->Array
.LockCount
,
250 /* The spec says referencing elements outside the locked
251 * range is undefined. I'm going to make it a noop this time
252 * round, maybe come up with something beter before 3.6.
254 * May be able to get away with just setting LockCount==0,
255 * though this raises the problems of dependent state. May
256 * have to call glUnlockArrays() directly?
259 "DrawRangeElements references "
260 "elements outside locked range.");
263 else if (end
+ 1 - start
< ctx
->Const
.MaxArrayLockSize
) {
264 /* The arrays aren't locked but we can still fit them inside a single
267 _tnl_draw_range_elements( ctx
, mode
, start
, end
+ 1, count
, ui_indices
);
269 /* Range is too big to optimize:
271 _tnl_draw_elements( ctx
, mode
, count
, ui_indices
);
278 _tnl_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
279 const GLvoid
*indices
)
281 GET_CURRENT_CONTEXT(ctx
);
282 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
285 /* Check arguments, etc.
287 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
290 if (tnl
->pipeline
.build_state_changes
)
291 _tnl_validate_pipeline( ctx
);
293 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
294 count
, type
, indices
);
297 if (ctx
->Array
.LockCount
) {
298 _tnl_draw_range_elements( ctx
, mode
,
299 ctx
->Array
.LockFirst
,
300 ctx
->Array
.LockCount
,
304 /* Scan the index list and see if we can use the locked path anyway.
309 for (i
= 0 ; i
< count
; i
++)
310 if (ui_indices
[i
] > max_elt
) max_elt
= ui_indices
[i
];
312 if (max_elt
< ctx
->Const
.MaxArrayLockSize
&& /* can we use it? */
313 max_elt
< count
) /* do we want to use it? */
314 _tnl_draw_range_elements( ctx
, mode
, 0, max_elt
+ 1, count
, ui_indices
);
316 _tnl_draw_elements( ctx
, mode
, count
, ui_indices
);
319 _tnl_draw_elements( ctx
, mode
, count
, ui_indices
);
324 void _tnl_array_init( GLcontext
*ctx
)
326 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
327 struct vertex_arrays
*tmp
= &tnl
->array_inputs
;
328 GLvertexformat
*vfmt
= &(TNL_CONTEXT(ctx
)->vtxfmt
);
331 vfmt
->DrawArrays
= _tnl_DrawArrays
;
332 vfmt
->DrawElements
= _tnl_DrawElements
;
333 vfmt
->DrawRangeElements
= _tnl_DrawRangeElements
;
335 /* Setup vector pointers that will be used to bind arrays to VB's.
337 gl_vector4f_init( &tmp
->Obj
, 0, 0 );
338 gl_vector3f_init( &tmp
->Normal
, 0, 0 );
339 #if CHAN_TYPE == GL_UNSIGNED_BYTE
340 gl_vector4ub_init( &tmp
->Color
, 0, 0 );
341 gl_vector4ub_init( &tmp
->SecondaryColor
, 0, 0 );
342 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
343 gl_vector4us_init( &tmp
->Color
, 0, 0 );
344 gl_vector4us_init( &tmp
->SecondaryColor
, 0, 0 );
345 #elif CHAN_TYPE == GL_FLOAT
346 gl_vector4f_init( &tmp
->Color
, 0, 0 );
347 gl_vector4f_init( &tmp
->SecondaryColor
, 0, 0 );
349 gl_vector1f_init( &tmp
->FogCoord
, 0, 0 );
350 gl_vector1ui_init( &tmp
->Index
, 0, 0 );
351 gl_vector1ub_init( &tmp
->EdgeFlag
, 0, 0 );
353 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++)
354 gl_vector4f_init( &tmp
->TexCoord
[i
], 0, 0);
356 tnl
->tmp_primitive
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
357 tnl
->tmp_primitive_length
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
361 void _tnl_array_destroy( GLcontext
*ctx
)
363 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
364 if (tnl
->tmp_primitive_length
) FREE(tnl
->tmp_primitive_length
);
365 if (tnl
->tmp_primitive
) FREE(tnl
->tmp_primitive
);