Vertex program checkpoint commit: converted all vertex attributes (color,
[mesa.git] / src / mesa / tnl / t_array_api.c
1 /* $Id: t_array_api.c,v 1.23 2002/01/05 20:51:12 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 (ctx->Array.LockCount &&
125 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 (start < (GLint) ctx->Array.LockFirst)
132 start = ctx->Array.LockFirst;
133 if (start + count > (GLint) ctx->Array.LockCount)
134 count = ctx->Array.LockCount - start;
135
136 /* Locked drawarrays. Reuse any previously transformed data.
137 */
138 _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
139 VB->FirstPrimitive = start;
140 VB->Primitive[start] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
141 VB->PrimitiveLength[start] = count;
142 tnl->Driver.RunPipeline( ctx );
143 }
144 else {
145 int bufsz = 256; /* Use a small buffer for cache goodness */
146 int j, nr;
147 int minimum, modulo, skip;
148
149 /* Large primitives requiring decomposition to multiple vertex
150 * buffers:
151 */
152 switch (mode) {
153 case GL_POINTS:
154 minimum = 0;
155 modulo = 1;
156 skip = 0;
157 case GL_LINES:
158 minimum = 1;
159 modulo = 2;
160 skip = 1;
161 case GL_LINE_STRIP:
162 minimum = 1;
163 modulo = 1;
164 skip = 0;
165 break;
166 case GL_TRIANGLES:
167 minimum = 2;
168 modulo = 3;
169 skip = 2;
170 break;
171 case GL_TRIANGLE_STRIP:
172 minimum = 2;
173 modulo = 1;
174 skip = 0;
175 break;
176 case GL_QUADS:
177 minimum = 3;
178 modulo = 4;
179 skip = 3;
180 break;
181 case GL_QUAD_STRIP:
182 minimum = 3;
183 modulo = 2;
184 skip = 0;
185 break;
186 case GL_LINE_LOOP:
187 case GL_TRIANGLE_FAN:
188 case GL_POLYGON:
189 default:
190 /* Primitives requiring a copied vertex (fan-like primitives)
191 * must use the slow path if they cannot fit in a single
192 * vertex buffer.
193 */
194 if (count < (GLint) ctx->Const.MaxArrayLockSize) {
195 bufsz = ctx->Const.MaxArrayLockSize;
196 minimum = 0;
197 modulo = 1;
198 skip = 0;
199 }
200 else {
201 fallback_drawarrays( ctx, mode, start, start + count );
202 return;
203 }
204 }
205
206 FLUSH_CURRENT( ctx, 0 );
207
208 /* fprintf(stderr, "start %d count %d min %d modulo %d skip %d\n", */
209 /* start, count, minimum, modulo, skip); */
210
211
212 bufsz -= bufsz % modulo;
213 bufsz -= minimum;
214 count += start;
215
216 for (j = start + minimum ; j < count ; j += nr + skip ) {
217
218 nr = MIN2( bufsz, count - j );
219
220 /* fprintf(stderr, "%d..%d\n", j - minimum, j+nr); */
221
222 _tnl_vb_bind_arrays( ctx, j - minimum, j + nr );
223
224 VB->FirstPrimitive = 0;
225 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
226 VB->PrimitiveLength[0] = nr + minimum;
227 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
228 tnl->Driver.RunPipeline( ctx );
229 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
230 }
231 }
232 }
233
234
235 void
236 _tnl_DrawRangeElements(GLenum mode,
237 GLuint start, GLuint end,
238 GLsizei count, GLenum type, const GLvoid *indices)
239 {
240 GET_CURRENT_CONTEXT(ctx);
241 TNLcontext *tnl = TNL_CONTEXT(ctx);
242 GLuint *ui_indices;
243
244 /* fprintf(stderr, "%s\n", __FUNCTION__); */
245
246 /* Check arguments, etc.
247 */
248 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
249 type, indices ))
250 return;
251
252 if (tnl->pipeline.build_state_changes)
253 _tnl_validate_pipeline( ctx );
254
255 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
256 count, type, indices );
257
258
259 if (ctx->CompileFlag) {
260 /* Can't do anything when compiling:
261 */
262 fallback_drawelements( ctx, mode, count, ui_indices );
263 }
264 else if (ctx->Array.LockCount) {
265 /* Are the arrays already locked? If so we currently have to look
266 * at the whole locked range.
267 */
268 if (start >= ctx->Array.LockFirst && end <= ctx->Array.LockCount)
269 _tnl_draw_range_elements( ctx, mode,
270 ctx->Array.LockFirst,
271 ctx->Array.LockCount,
272 count, ui_indices );
273 else {
274 /* The spec says referencing elements outside the locked
275 * range is undefined. I'm going to make it a noop this time
276 * round, maybe come up with something beter before 3.6.
277 *
278 * May be able to get away with just setting LockCount==0,
279 * though this raises the problems of dependent state. May
280 * have to call glUnlockArrays() directly?
281 *
282 * Or scan the list and replace bad indices?
283 */
284 _mesa_problem( ctx,
285 "DrawRangeElements references "
286 "elements outside locked range.");
287 }
288 }
289 else if (end + 1 - start < ctx->Const.MaxArrayLockSize) {
290 /* The arrays aren't locked but we can still fit them inside a
291 * single vertexbuffer.
292 */
293 _tnl_draw_range_elements( ctx, mode, start, end + 1, count, ui_indices );
294 } else {
295 /* Range is too big to optimize:
296 */
297 fallback_drawelements( ctx, mode, count, ui_indices );
298 }
299 }
300
301
302
303 void
304 _tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
305 const GLvoid *indices)
306 {
307 GET_CURRENT_CONTEXT(ctx);
308 TNLcontext *tnl = TNL_CONTEXT(ctx);
309 GLuint *ui_indices;
310
311 /* fprintf(stderr, "%s\n", __FUNCTION__); */
312
313 /* Check arguments, etc.
314 */
315 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
316 return;
317
318 if (tnl->pipeline.build_state_changes)
319 _tnl_validate_pipeline( ctx );
320
321 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
322 count, type, indices );
323
324 if (ctx->CompileFlag) {
325 /* Can't do anything when compiling:
326 */
327 fallback_drawelements( ctx, mode, count, ui_indices );
328 }
329 else if (ctx->Array.LockCount) {
330 _tnl_draw_range_elements( ctx, mode,
331 ctx->Array.LockFirst,
332 ctx->Array.LockCount,
333 count, ui_indices );
334 }
335 else {
336 /* Scan the index list and see if we can use the locked path anyway.
337 */
338 GLuint max_elt = 0;
339 GLint i;
340
341 for (i = 0 ; i < count ; i++)
342 if (ui_indices[i] > max_elt)
343 max_elt = ui_indices[i];
344
345 if (max_elt < ctx->Const.MaxArrayLockSize && /* can we use it? */
346 max_elt < (GLuint) count) /* do we want to use it? */
347 _tnl_draw_range_elements( ctx, mode, 0, max_elt+1, count, ui_indices );
348 else
349 fallback_drawelements( ctx, mode, count, ui_indices );
350 }
351 }
352
353
354 void _tnl_array_init( GLcontext *ctx )
355 {
356 TNLcontext *tnl = TNL_CONTEXT(ctx);
357 struct vertex_arrays *tmp = &tnl->array_inputs;
358 GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->vtxfmt);
359 GLuint i;
360
361 vfmt->DrawArrays = _tnl_DrawArrays;
362 vfmt->DrawElements = _tnl_DrawElements;
363 vfmt->DrawRangeElements = _tnl_DrawRangeElements;
364
365 /* Setup vector pointers that will be used to bind arrays to VB's.
366 */
367 _mesa_vector4f_init( &tmp->Obj, 0, 0 );
368 _mesa_vector4f_init( &tmp->Normal, 0, 0 );
369 _mesa_vector4f_init( &tmp->FogCoord, 0, 0 );
370 _mesa_vector1ui_init( &tmp->Index, 0, 0 );
371 _mesa_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
372
373 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
374 _mesa_vector4f_init( &tmp->TexCoord[i], 0, 0);
375
376 tnl->tmp_primitive = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
377 tnl->tmp_primitive_length = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
378 }
379
380
381 void _tnl_array_destroy( GLcontext *ctx )
382 {
383 TNLcontext *tnl = TNL_CONTEXT(ctx);
384 if (tnl->tmp_primitive_length) FREE(tnl->tmp_primitive_length);
385 if (tnl->tmp_primitive) FREE(tnl->tmp_primitive);
386 }