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