Major rework of tnl module
[mesa.git] / src / mesa / tnl / t_array_api.c
1 /* $Id: t_array_api.c,v 1.1 2000/12/26 05:09:32 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 *
26 * Authors:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30 #include "glheader.h"
31 #include "api_validate.h"
32 #include "context.h"
33 #include "macros.h"
34 #include "mmath.h"
35 #include "mem.h"
36 #include "state.h"
37 #include "mtypes.h"
38
39 #include "array_cache/acache.h"
40
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"
47
48
49
50
51
52 void
53 _tnl_DrawArrays(GLenum mode, GLint start, GLsizei count)
54 {
55 GET_CURRENT_CONTEXT(ctx);
56 TNLcontext *tnl = TNL_CONTEXT(ctx);
57 struct vertex_buffer *VB = &tnl->vb;
58
59 /* Check arguments, etc.
60 */
61 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
62 return;
63
64 if (tnl->pipeline.build_state_changes)
65 _tnl_validate_pipeline( ctx );
66
67 if (!ctx->CompileFlag && count - start < ctx->Const.MaxArrayLockSize) {
68 FLUSH_CURRENT( ctx, 0 );
69
70 if (ctx->Array.LockCount)
71 {
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;
75
76 /* Locked drawarrays. Reuse any previously transformed data.
77 */
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 );
83 } else {
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
86 * clipping.
87 *
88 * Invalidate any locked data dependent on these arrays.
89 */
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;
97 }
98 }
99 else {
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
104 * of the flag array.
105 */
106 #if 0
107 if (_tnl_hard_begin( ctx, mode )) {
108 GLuint j;
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];
113
114 _tnl_fill_immediate_drawarrays( ctx, IM, j, j+nr );
115
116 if (j == 0) IM->Flag[IM->Start] |= sf;
117
118 IM->Count = IM->Start + nr;
119 j += nr;
120
121 if (j == count)
122 _tnl_end( ctx );
123
124 _tnl_flush_immediate( IM );
125 }
126 }
127 #else
128 /* Simple alternative to above code.
129 */
130 /* if (_tnl_hard_begin( ctx, mode )) */
131 _tnl_begin(ctx,mode);
132 {
133 GLuint i;
134 for (i=start;i<count;i++) {
135 _tnl_array_element( ctx, i );
136 }
137 _tnl_end( ctx );
138 }
139 #endif
140 }
141 }
142
143
144
145 static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
146 GLuint start, GLuint end,
147 GLsizei count, const GLuint *indices )
148
149 {
150 TNLcontext *tnl = TNL_CONTEXT(ctx);
151 FLUSH_CURRENT( ctx, 0 );
152
153 _tnl_vb_bind_arrays( ctx, start, end );
154
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;
159
160 if (ctx->Array.LockCount)
161 _tnl_run_pipeline( ctx );
162 else {
163 /* Note that arrays may have changed before/after execution.
164 */
165 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
166 _tnl_run_pipeline( ctx );
167 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
168 }
169 }
170
171
172
173
174 static void _tnl_draw_elements( GLcontext *ctx, GLenum mode, GLsizei count,
175 const GLuint *indices)
176 {
177 #if 1
178 /* Optimized code that fakes the effect of calling
179 * _tnl_array_element for each index in the list.
180 */
181 if (_tnl_hard_begin( ctx, mode )) {
182 GLuint i,j;
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];
188 IM->FlushElt |= 1;
189
190 for (i = start ; i < nr ; i++) {
191 IM->Elt[i] = (GLuint) *indices++;
192 IM->Flag[i] = VERT_ELT;
193 }
194
195 if (j == 0) IM->Flag[start] |= sf;
196
197 IM->Count = nr;
198 j += nr - start;
199
200 if (j == count)
201 _tnl_end( ctx );
202
203 _tnl_flush_immediate( IM );
204 }
205 }
206 #else
207 /* Simple version of the above code.
208 */
209 if (_tnl_hard_begin(ctx, mode)) {
210 GLuint i;
211 for (i = 0 ; i < count ; i++)
212 _tnl_array_element( ctx, indices[i] );
213 _tnl_end( ctx );
214 }
215 #endif
216 }
217
218
219 void
220 _tnl_DrawRangeElements(GLenum mode,
221 GLuint start, GLuint end,
222 GLsizei count, GLenum type, const GLvoid *indices)
223 {
224 GET_CURRENT_CONTEXT(ctx);
225 TNLcontext *tnl = TNL_CONTEXT(ctx);
226 GLuint *ui_indices;
227
228 /* Check arguments, etc.
229 */
230 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
231 type, indices ))
232 return;
233
234 if (tnl->pipeline.build_state_changes)
235 _tnl_validate_pipeline( ctx );
236
237 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
238 count, type, indices );
239
240
241 if (ctx->Array.LockCount) {
242 /* Are the arrays already locked? If so we currently have to look
243 * at the whole locked range.
244 */
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,
249 count, ui_indices );
250 else {
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.
254 *
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?
258 */
259 gl_problem( ctx,
260 "DrawRangeElements references "
261 "elements outside locked range.");
262 }
263 }
264 else if (end - start < ctx->Const.MaxArrayLockSize) {
265 /* The arrays aren't locked but we can still fit them inside a single
266 * vertexbuffer.
267 */
268 _tnl_draw_range_elements( ctx, mode, start, end, count, ui_indices );
269 } else {
270 /* Range is too big to optimize:
271 */
272 _tnl_draw_elements( ctx, mode, count, ui_indices );
273 }
274 }
275
276
277
278 void
279 _tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
280 const GLvoid *indices)
281 {
282 GET_CURRENT_CONTEXT(ctx);
283 TNLcontext *tnl = TNL_CONTEXT(ctx);
284 GLuint *ui_indices;
285
286 /* Check arguments, etc.
287 */
288 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
289 return;
290
291 if (tnl->pipeline.build_state_changes)
292 _tnl_validate_pipeline( ctx );
293
294 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
295 count, type, indices );
296
297 if (ctx->Array.LockCount) {
298 _tnl_draw_range_elements( ctx, mode,
299 ctx->Array.LockFirst,
300 ctx->Array.LockCount,
301 count, ui_indices );
302 }
303 else {
304 /* Scan the index list and see if we can use the locked path anyway.
305 */
306 GLuint max_elt = 0;
307 GLuint i;
308
309 for (i = 0 ; i < count ; i++)
310 if (ui_indices[i] > max_elt) max_elt = ui_indices[i];
311
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 );
315 else
316 _tnl_draw_elements( ctx, mode, count, ui_indices );
317 }
318 }
319
320
321 void _tnl_array_init( GLcontext *ctx )
322 {
323 TNLcontext *tnl = TNL_CONTEXT(ctx);
324 struct vertex_arrays *tmp = &tnl->array_inputs;
325 GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->vtxfmt);
326 GLuint i;
327
328 vfmt->DrawArrays = _tnl_DrawArrays;
329 vfmt->DrawElements = _tnl_DrawElements;
330 vfmt->DrawRangeElements = _tnl_DrawRangeElements;
331
332 /* Setup vector pointers that will be used to bind arrays to VB's.
333 */
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 );
341
342 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
343 gl_vector4f_init( &tmp->TexCoord[i], 0, 0);
344
345 tnl->tmp_primitive = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
346 tnl->tmp_primitive_length = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
347 }
348
349
350 void _tnl_array_destroy( GLcontext *ctx )
351 {
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);
355 }