silence minor warnings
[mesa.git] / src / mesa / tnl / t_array_api.c
1 /* $Id: t_array_api.c,v 1.19 2001/09/14 17:00:42 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 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 static void fallback_drawarrays( GLcontext *ctx, GLenum mode, GLint start,
49 GLsizei count )
50 {
51 if (_tnl_hard_begin( ctx, mode )) {
52 GLint i;
53 for (i = start; i < count; i++) {
54 _tnl_array_element( ctx, i );
55 }
56 _tnl_end( ctx );
57 }
58 }
59
60
61 static void fallback_drawelements( GLcontext *ctx, GLenum mode, GLsizei count,
62 const GLuint *indices)
63 {
64 /* Simple version of the above code.
65 */
66 if (_tnl_hard_begin(ctx, mode)) {
67 GLint i;
68 for (i = 0 ; i < count ; i++)
69 _tnl_array_element( ctx, indices[i] );
70 _tnl_end( ctx );
71 }
72 }
73
74
75 static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
76 GLuint start, GLuint end,
77 GLsizei count, const GLuint *indices )
78
79 {
80 TNLcontext *tnl = TNL_CONTEXT(ctx);
81 FLUSH_CURRENT( ctx, 0 );
82
83 _tnl_vb_bind_arrays( ctx, start, end );
84
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;
89
90 if (ctx->Array.LockCount)
91 tnl->Driver.RunPipeline( ctx );
92 else {
93 /* Note that arrays may have changed before/after execution.
94 */
95 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
96 tnl->Driver.RunPipeline( ctx );
97 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
98 }
99 }
100
101
102
103
104 void
105 _tnl_DrawArrays(GLenum mode, GLint start, GLsizei count)
106 {
107 GET_CURRENT_CONTEXT(ctx);
108 TNLcontext *tnl = TNL_CONTEXT(ctx);
109 struct vertex_buffer *VB = &tnl->vb;
110
111 /* fprintf(stderr, "%s %d %d\n", __FUNCTION__, start, count); */
112
113 /* Check arguments, etc.
114 */
115 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
116 return;
117
118 if (tnl->pipeline.build_state_changes)
119 _tnl_validate_pipeline( ctx );
120
121 if (ctx->CompileFlag) {
122 fallback_drawarrays( ctx, mode, start, start + count );
123 }
124 else if (count < (GLint) ctx->Const.MaxArrayLockSize) {
125
126 /* Small primitives which can fit in a single vertex buffer:
127 */
128 FLUSH_CURRENT( ctx, 0 );
129
130 if (ctx->Array.LockCount)
131 {
132 if (start < (GLint) ctx->Array.LockFirst)
133 start = ctx->Array.LockFirst;
134 if (start + count > (GLint) ctx->Array.LockCount)
135 count = ctx->Array.LockCount - start;
136 if (start >= count)
137 return;
138
139 /* Locked drawarrays. Reuse any previously transformed data.
140 */
141 _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
142 VB->FirstPrimitive = start;
143 VB->Primitive[start] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
144 VB->PrimitiveLength[start] = count;
145 tnl->Driver.RunPipeline( ctx );
146 } else {
147 /* The arrays are small enough to fit in a single VB; just bind
148 * them and go. Any untransformed data will be copied on
149 * clipping.
150 *
151 * Invalidate any cached data dependent on these arrays.
152 */
153 _tnl_vb_bind_arrays( ctx, start, start + count );
154 VB->FirstPrimitive = 0;
155 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
156 VB->PrimitiveLength[0] = count;
157 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
158 tnl->Driver.RunPipeline( ctx );
159 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
160 }
161 }
162 else {
163 int bufsz = (ctx->Const.MaxArrayLockSize - 4) & ~3;
164 int j, nr;
165 int minimum, modulo, skip;
166
167 /* Large primitives requiring decomposition to multiple vertex
168 * buffers:
169 */
170 switch (mode) {
171 case GL_POINTS:
172 minimum = 0;
173 modulo = 1;
174 skip = 0;
175 case GL_LINES:
176 minimum = 1;
177 modulo = 2;
178 skip = 1;
179 case GL_LINE_STRIP:
180 minimum = 1;
181 modulo = 1;
182 skip = 0;
183 break;
184 case GL_TRIANGLES:
185 minimum = 2;
186 modulo = 3;
187 skip = 2;
188 break;
189 case GL_TRIANGLE_STRIP:
190 minimum = 2;
191 modulo = 1;
192 skip = 0;
193 break;
194 case GL_QUADS:
195 minimum = 3;
196 modulo = 4;
197 skip = 3;
198 break;
199 case GL_QUAD_STRIP:
200 minimum = 3;
201 modulo = 2;
202 skip = 0;
203 break;
204 case GL_LINE_LOOP:
205 case GL_TRIANGLE_FAN:
206 case GL_POLYGON:
207 default:
208 /* Primitives requiring a copied vertex (fan-like primitives)
209 * must use the slow path:
210 */
211 fallback_drawarrays( ctx, mode, start, start + count );
212 return;
213 }
214
215 FLUSH_CURRENT( ctx, 0 );
216
217 /* fprintf(stderr, "start %d count %d min %d modulo %d skip %d\n", */
218 /* start, count, minimum, modulo, skip); */
219
220
221 bufsz -= bufsz % modulo;
222 bufsz -= minimum;
223 count += start;
224
225 for (j = start + minimum ; j < count ; j += nr + skip ) {
226
227 nr = MIN2( bufsz, count - j );
228
229 /* fprintf(stderr, "%d..%d\n", j - minimum, j+nr); */
230
231 _tnl_vb_bind_arrays( ctx, j - minimum, j + nr );
232
233 VB->FirstPrimitive = 0;
234 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
235 VB->PrimitiveLength[0] = nr + minimum;
236 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
237 tnl->Driver.RunPipeline( ctx );
238 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
239 }
240 }
241 }
242
243
244 void
245 _tnl_DrawRangeElements(GLenum mode,
246 GLuint start, GLuint end,
247 GLsizei count, GLenum type, const GLvoid *indices)
248 {
249 GET_CURRENT_CONTEXT(ctx);
250 TNLcontext *tnl = TNL_CONTEXT(ctx);
251 GLuint *ui_indices;
252
253 /* fprintf(stderr, "%s\n", __FUNCTION__); */
254
255 /* Check arguments, etc.
256 */
257 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
258 type, indices ))
259 return;
260
261 if (tnl->pipeline.build_state_changes)
262 _tnl_validate_pipeline( ctx );
263
264 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
265 count, type, indices );
266
267
268 if (ctx->CompileFlag) {
269 /* Can't do anything when compiling:
270 */
271 fallback_drawelements( ctx, mode, count, ui_indices );
272 }
273 else if (ctx->Array.LockCount) {
274 /* Are the arrays already locked? If so we currently have to look
275 * at the whole locked range.
276 */
277 if (start >= ctx->Array.LockFirst && end <= ctx->Array.LockCount)
278 _tnl_draw_range_elements( ctx, mode,
279 ctx->Array.LockFirst,
280 ctx->Array.LockCount,
281 count, ui_indices );
282 else {
283 /* The spec says referencing elements outside the locked
284 * range is undefined. I'm going to make it a noop this time
285 * round, maybe come up with something beter before 3.6.
286 *
287 * May be able to get away with just setting LockCount==0,
288 * though this raises the problems of dependent state. May
289 * have to call glUnlockArrays() directly?
290 *
291 * Or scan the list and replace bad indices?
292 */
293 _mesa_problem( ctx,
294 "DrawRangeElements references "
295 "elements outside locked range.");
296 }
297 }
298 else if (end + 1 - start < ctx->Const.MaxArrayLockSize) {
299 /* The arrays aren't locked but we can still fit them inside a
300 * single vertexbuffer.
301 */
302 _tnl_draw_range_elements( ctx, mode, start, end + 1, count, ui_indices );
303 } else {
304 /* Range is too big to optimize:
305 */
306 fallback_drawelements( ctx, mode, count, ui_indices );
307 }
308 }
309
310
311
312 void
313 _tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
314 const GLvoid *indices)
315 {
316 GET_CURRENT_CONTEXT(ctx);
317 TNLcontext *tnl = TNL_CONTEXT(ctx);
318 GLuint *ui_indices;
319
320 /* fprintf(stderr, "%s\n", __FUNCTION__); */
321
322 /* Check arguments, etc.
323 */
324 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
325 return;
326
327 if (tnl->pipeline.build_state_changes)
328 _tnl_validate_pipeline( ctx );
329
330 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
331 count, type, indices );
332
333 if (ctx->CompileFlag) {
334 /* Can't do anything when compiling:
335 */
336 fallback_drawelements( ctx, mode, count, ui_indices );
337 }
338 else if (ctx->Array.LockCount) {
339 _tnl_draw_range_elements( ctx, mode,
340 ctx->Array.LockFirst,
341 ctx->Array.LockCount,
342 count, ui_indices );
343 }
344 else {
345 /* Scan the index list and see if we can use the locked path anyway.
346 */
347 GLuint max_elt = 0;
348 GLint i;
349
350 for (i = 0 ; i < count ; i++)
351 if (ui_indices[i] > max_elt)
352 max_elt = ui_indices[i];
353
354 if (max_elt < ctx->Const.MaxArrayLockSize && /* can we use it? */
355 max_elt < (GLuint) count) /* do we want to use it? */
356 _tnl_draw_range_elements( ctx, mode, 0, max_elt+1, count, ui_indices );
357 else
358 fallback_drawelements( ctx, mode, count, ui_indices );
359 }
360 }
361
362
363 void _tnl_array_init( GLcontext *ctx )
364 {
365 TNLcontext *tnl = TNL_CONTEXT(ctx);
366 struct vertex_arrays *tmp = &tnl->array_inputs;
367 GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->vtxfmt);
368 GLuint i;
369
370 vfmt->DrawArrays = _tnl_DrawArrays;
371 vfmt->DrawElements = _tnl_DrawElements;
372 vfmt->DrawRangeElements = _tnl_DrawRangeElements;
373
374 /* Setup vector pointers that will be used to bind arrays to VB's.
375 */
376 _mesa_vector4f_init( &tmp->Obj, 0, 0 );
377 _mesa_vector3f_init( &tmp->Normal, 0, 0 );
378 _mesa_vector1f_init( &tmp->FogCoord, 0, 0 );
379 _mesa_vector1ui_init( &tmp->Index, 0, 0 );
380 _mesa_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
381
382 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
383 _mesa_vector4f_init( &tmp->TexCoord[i], 0, 0);
384
385 tnl->tmp_primitive = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
386 tnl->tmp_primitive_length = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
387 }
388
389
390 void _tnl_array_destroy( GLcontext *ctx )
391 {
392 TNLcontext *tnl = TNL_CONTEXT(ctx);
393 if (tnl->tmp_primitive_length) FREE(tnl->tmp_primitive_length);
394 if (tnl->tmp_primitive) FREE(tnl->tmp_primitive);
395 }