1 /* $Id: t_array_api.c,v 1.1 2000/12/26 05:09:32 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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 )) */
131 _tnl_begin(ctx
,mode
);
134 for (i
=start
;i
<count
;i
++) {
135 _tnl_array_element( ctx
, i
);
145 static void _tnl_draw_range_elements( GLcontext
*ctx
, GLenum mode
,
146 GLuint start
, GLuint end
,
147 GLsizei count
, const GLuint
*indices
)
150 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
151 FLUSH_CURRENT( ctx
, 0 );
153 _tnl_vb_bind_arrays( ctx
, start
, end
);
155 tnl
->vb
.FirstPrimitive
= 0;
156 tnl
->vb
.Primitive
[0] = mode
| PRIM_BEGIN
| PRIM_END
| PRIM_LAST
;
157 tnl
->vb
.PrimitiveLength
[0] = count
;
158 tnl
->vb
.Elts
= (GLuint
*)indices
;
160 if (ctx
->Array
.LockCount
)
161 _tnl_run_pipeline( ctx
);
163 /* Note that arrays may have changed before/after execution.
165 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
166 _tnl_run_pipeline( ctx
);
167 tnl
->pipeline
.run_input_changes
|= ctx
->Array
._Enabled
;
174 static void _tnl_draw_elements( GLcontext
*ctx
, GLenum mode
, GLsizei count
,
175 const GLuint
*indices
)
178 /* Optimized code that fakes the effect of calling
179 * _tnl_array_element for each index in the list.
181 if (_tnl_hard_begin( ctx
, mode
)) {
183 for (j
= 0 ; j
< count
; ) {
184 struct immediate
*IM
= TNL_CURRENT_IM(ctx
);
185 GLuint start
= IM
->Start
;
186 GLuint nr
= MIN2( IMM_MAXDATA
- start
, count
- j
) + start
;
187 GLuint sf
= IM
->Flag
[start
];
190 for (i
= start
; i
< nr
; i
++) {
191 IM
->Elt
[i
] = (GLuint
) *indices
++;
192 IM
->Flag
[i
] = VERT_ELT
;
195 if (j
== 0) IM
->Flag
[start
] |= sf
;
203 _tnl_flush_immediate( IM
);
207 /* Simple version of the above code.
209 if (_tnl_hard_begin(ctx
, mode
)) {
211 for (i
= 0 ; i
< count
; i
++)
212 _tnl_array_element( ctx
, indices
[i
] );
220 _tnl_DrawRangeElements(GLenum mode
,
221 GLuint start
, GLuint end
,
222 GLsizei count
, GLenum type
, const GLvoid
*indices
)
224 GET_CURRENT_CONTEXT(ctx
);
225 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
228 /* Check arguments, etc.
230 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
234 if (tnl
->pipeline
.build_state_changes
)
235 _tnl_validate_pipeline( ctx
);
237 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
238 count
, type
, indices
);
241 if (ctx
->Array
.LockCount
) {
242 /* Are the arrays already locked? If so we currently have to look
243 * at the whole locked range.
245 if (start
>= ctx
->Array
.LockFirst
&& end
<= ctx
->Array
.LockCount
)
246 _tnl_draw_range_elements( ctx
, mode
,
247 ctx
->Array
.LockFirst
,
248 ctx
->Array
.LockCount
,
251 /* The spec says referencing elements outside the locked
252 * range is undefined. I'm going to make it a noop this time
253 * round, maybe come up with something beter before 3.6.
255 * May be able to get away with just setting LockCount==0,
256 * though this raises the problems of dependent state. May
257 * have to call glUnlockArrays() directly?
260 "DrawRangeElements references "
261 "elements outside locked range.");
264 else if (end
- start
< ctx
->Const
.MaxArrayLockSize
) {
265 /* The arrays aren't locked but we can still fit them inside a single
268 _tnl_draw_range_elements( ctx
, mode
, start
, end
, count
, ui_indices
);
270 /* Range is too big to optimize:
272 _tnl_draw_elements( ctx
, mode
, count
, ui_indices
);
279 _tnl_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
280 const GLvoid
*indices
)
282 GET_CURRENT_CONTEXT(ctx
);
283 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
286 /* Check arguments, etc.
288 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
291 if (tnl
->pipeline
.build_state_changes
)
292 _tnl_validate_pipeline( ctx
);
294 ui_indices
= (GLuint
*)_ac_import_elements( ctx
, GL_UNSIGNED_INT
,
295 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
, count
, ui_indices
);
316 _tnl_draw_elements( ctx
, mode
, count
, ui_indices
);
321 void _tnl_array_init( GLcontext
*ctx
)
323 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
324 struct vertex_arrays
*tmp
= &tnl
->array_inputs
;
325 GLvertexformat
*vfmt
= &(TNL_CONTEXT(ctx
)->vtxfmt
);
328 vfmt
->DrawArrays
= _tnl_DrawArrays
;
329 vfmt
->DrawElements
= _tnl_DrawElements
;
330 vfmt
->DrawRangeElements
= _tnl_DrawRangeElements
;
332 /* Setup vector pointers that will be used to bind arrays to VB's.
334 gl_vector4f_init( &tmp
->Obj
, 0, 0 );
335 gl_vector3f_init( &tmp
->Normal
, 0, 0 );
336 gl_vector4ub_init( &tmp
->Color
, 0, 0 );
337 gl_vector4ub_init( &tmp
->SecondaryColor
, 0, 0 );
338 gl_vector1f_init( &tmp
->FogCoord
, 0, 0 );
339 gl_vector1ui_init( &tmp
->Index
, 0, 0 );
340 gl_vector1ub_init( &tmp
->EdgeFlag
, 0, 0 );
342 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++)
343 gl_vector4f_init( &tmp
->TexCoord
[i
], 0, 0);
345 tnl
->tmp_primitive
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
346 tnl
->tmp_primitive_length
= (GLuint
*)MALLOC(sizeof(GLuint
)*tnl
->vb
.Size
);
350 void _tnl_array_destroy( GLcontext
*ctx
)
352 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
353 if (tnl
->tmp_primitive_length
) FREE(tnl
->tmp_primitive_length
);
354 if (tnl
->tmp_primitive
) FREE(tnl
->tmp_primitive
);