remove a const
[mesa.git] / src / mesa / tnl / t_array_api.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file t_array_api.c
27 * \brief Vertex array API functions (glDrawArrays, etc)
28 * \author Keith Whitwell
29 */
30
31 #include "glheader.h"
32 #include "api_validate.h"
33 #include "context.h"
34 #include "imports.h"
35 #include "macros.h"
36 #include "mtypes.h"
37 #include "state.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 glArrayElement( i );
55 glEnd();
56 }
57 }
58
59
60 static void fallback_drawelements( GLcontext *ctx, GLenum mode, GLsizei count,
61 const GLuint *indices)
62 {
63 if (_tnl_hard_begin(ctx, mode)) {
64 GLint i;
65 for (i = 0 ; i < count ; i++)
66 glArrayElement( indices[i] );
67 glEnd();
68 }
69 }
70
71
72 static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
73 GLuint start, GLuint end,
74 GLsizei count, GLuint *indices )
75
76 {
77 TNLcontext *tnl = TNL_CONTEXT(ctx);
78 int i;
79 FLUSH_CURRENT( ctx, 0 );
80
81 /* _mesa_debug(ctx, "%s\n", __FUNCTION__); */
82 if (tnl->pipeline.build_state_changes)
83 _tnl_validate_pipeline( ctx );
84
85 _tnl_vb_bind_arrays( ctx, start, end );
86
87 tnl->vb.FirstPrimitive = 0;
88 tnl->vb.Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
89 tnl->vb.PrimitiveLength[0] = count;
90 tnl->vb.Elts = (GLuint *)indices;
91
92 for (i = 0 ; i < count ; i++)
93 indices[i] -= start;
94
95 if (ctx->Array.LockCount)
96 tnl->Driver.RunPipeline( ctx );
97 else {
98 /* Note that arrays may have changed before/after execution.
99 */
100 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
101 tnl->Driver.RunPipeline( ctx );
102 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
103 }
104
105 for (i = 0 ; i < count ; i++)
106 indices[i] += start;
107 }
108
109
110
111 /**
112 * Called via the GL API dispatcher.
113 */
114 void
115 _tnl_DrawArrays(GLenum mode, GLint start, GLsizei count)
116 {
117 GET_CURRENT_CONTEXT(ctx);
118 TNLcontext *tnl = TNL_CONTEXT(ctx);
119 struct vertex_buffer *VB = &tnl->vb;
120 GLuint thresh = (ctx->Driver.NeedFlush & FLUSH_STORED_VERTICES) ? 30 : 10;
121
122 if (MESA_VERBOSE & VERBOSE_API)
123 _mesa_debug(NULL, "_tnl_DrawArrays %d %d\n", start, count);
124
125 /* Check arguments, etc.
126 */
127 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
128 return;
129
130 if (tnl->pipeline.build_state_changes)
131 _tnl_validate_pipeline( ctx );
132
133 if (ctx->CompileFlag) {
134 fallback_drawarrays( ctx, mode, start, start + count );
135 }
136 else if (!ctx->Array.LockCount && (GLuint) count < thresh) {
137 /* Small primitives: attempt to share a vb (at the expense of
138 * using the immediate interface).
139 */
140 fallback_drawarrays( ctx, mode, start, start + count );
141 }
142 else if (ctx->Array.LockCount &&
143 count < (GLint) ctx->Const.MaxArrayLockSize) {
144
145 /* Locked primitives which can fit in a single vertex buffer:
146 */
147 FLUSH_CURRENT( ctx, 0 );
148
149 if (start < (GLint) ctx->Array.LockFirst)
150 start = ctx->Array.LockFirst;
151 if (start + count > (GLint) ctx->Array.LockCount)
152 count = ctx->Array.LockCount - start;
153
154 /* Locked drawarrays. Reuse any previously transformed data.
155 */
156 _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
157 VB->FirstPrimitive = start;
158 VB->Primitive[start] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
159 VB->PrimitiveLength[start] = count;
160 tnl->Driver.RunPipeline( ctx );
161 }
162 else {
163 int bufsz = 256; /* Use a small buffer for cache goodness */
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 break;
176 case GL_LINES:
177 minimum = 1;
178 modulo = 2;
179 skip = 1;
180 break;
181 case GL_LINE_STRIP:
182 minimum = 1;
183 modulo = 1;
184 skip = 0;
185 break;
186 case GL_TRIANGLES:
187 minimum = 2;
188 modulo = 3;
189 skip = 2;
190 break;
191 case GL_TRIANGLE_STRIP:
192 minimum = 2;
193 modulo = 1;
194 skip = 0;
195 break;
196 case GL_QUADS:
197 minimum = 3;
198 modulo = 4;
199 skip = 3;
200 break;
201 case GL_QUAD_STRIP:
202 minimum = 3;
203 modulo = 2;
204 skip = 0;
205 break;
206 case GL_LINE_LOOP:
207 case GL_TRIANGLE_FAN:
208 case GL_POLYGON:
209 default:
210 /* Primitives requiring a copied vertex (fan-like primitives)
211 * must use the slow path if they cannot fit in a single
212 * vertex buffer.
213 */
214 if (count < (GLint) ctx->Const.MaxArrayLockSize) {
215 bufsz = ctx->Const.MaxArrayLockSize;
216 minimum = 0;
217 modulo = 1;
218 skip = 0;
219 }
220 else {
221 fallback_drawarrays( ctx, mode, start, start + count );
222 return;
223 }
224 }
225
226 FLUSH_CURRENT( ctx, 0 );
227
228 bufsz -= bufsz % modulo;
229 bufsz -= minimum;
230 count += start;
231
232 for (j = start + minimum ; j < count ; j += nr + skip ) {
233
234 nr = MIN2( bufsz, count - j );
235
236 _tnl_vb_bind_arrays( ctx, j - minimum, j + nr );
237
238 VB->FirstPrimitive = 0;
239 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
240 VB->PrimitiveLength[0] = nr + minimum;
241 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
242 tnl->Driver.RunPipeline( ctx );
243 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
244 }
245 }
246 }
247
248
249 /**
250 * Called via the GL API dispatcher.
251 */
252 void
253 _tnl_DrawRangeElements(GLenum mode,
254 GLuint start, GLuint end,
255 GLsizei count, GLenum type, const GLvoid *indices)
256 {
257 GET_CURRENT_CONTEXT(ctx);
258 GLuint *ui_indices;
259
260 if (MESA_VERBOSE & VERBOSE_API)
261 _mesa_debug(NULL, "_tnl_DrawRangeElements %d %d %d\n", start, end, count);
262
263 /* Check arguments, etc.
264 */
265 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
266 type, indices ))
267 return;
268
269 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
270 count, type, indices );
271
272
273 if (ctx->CompileFlag) {
274 /* Can't do anything when compiling:
275 */
276 fallback_drawelements( ctx, mode, count, ui_indices );
277 }
278 else if (ctx->Array.LockCount) {
279 /* Are the arrays already locked? If so we currently have to look
280 * at the whole locked range.
281 */
282 if (start >= ctx->Array.LockFirst && end <= ctx->Array.LockCount)
283 _tnl_draw_range_elements( ctx, mode,
284 ctx->Array.LockFirst,
285 ctx->Array.LockCount,
286 count, ui_indices );
287 else {
288 /* The spec says referencing elements outside the locked
289 * range is undefined. I'm going to make it a noop this time
290 * round, maybe come up with something beter before 3.6.
291 *
292 * May be able to get away with just setting LockCount==0,
293 * though this raises the problems of dependent state. May
294 * have to call glUnlockArrays() directly?
295 *
296 * Or scan the list and replace bad indices?
297 */
298 _mesa_problem( ctx,
299 "DrawRangeElements references "
300 "elements outside locked range.");
301 }
302 }
303 else if (end + 1 - start < ctx->Const.MaxArrayLockSize) {
304 /* The arrays aren't locked but we can still fit them inside a
305 * single vertexbuffer.
306 */
307 _tnl_draw_range_elements( ctx, mode, start, end + 1, count, ui_indices );
308 } else {
309 /* Range is too big to optimize:
310 */
311 fallback_drawelements( ctx, mode, count, ui_indices );
312 }
313 }
314
315
316
317 /**
318 * Called via the GL API dispatcher.
319 */
320 void
321 _tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
322 const GLvoid *indices)
323 {
324 GET_CURRENT_CONTEXT(ctx);
325 GLuint *ui_indices;
326
327 if (MESA_VERBOSE & VERBOSE_API)
328 _mesa_debug(NULL, "_tnl_DrawElements %d\n", count);
329
330 /* Check arguments, etc.
331 */
332 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
333 return;
334
335 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
336 count, type, indices );
337
338 if (ctx->CompileFlag) {
339 /* Can't do anything when compiling:
340 */
341 fallback_drawelements( ctx, mode, count, ui_indices );
342 }
343 else if (ctx->Array.LockCount) {
344 _tnl_draw_range_elements( ctx, mode,
345 ctx->Array.LockFirst,
346 ctx->Array.LockCount,
347 count, ui_indices );
348 }
349 else {
350 /* Scan the index list and see if we can use the locked path anyway.
351 */
352 GLuint max_elt = 0;
353 GLint i;
354
355 for (i = 0 ; i < count ; i++)
356 if (ui_indices[i] > max_elt)
357 max_elt = ui_indices[i];
358
359 if (max_elt < ctx->Const.MaxArrayLockSize && /* can we use it? */
360 max_elt < (GLuint) count) /* do we want to use it? */
361 _tnl_draw_range_elements( ctx, mode, 0, max_elt+1, count, ui_indices );
362 else
363 fallback_drawelements( ctx, mode, count, ui_indices );
364 }
365 }
366
367
368 /**
369 * Initialize context's vertex array fields. Called during T 'n L context
370 * creation.
371 */
372 void _tnl_array_init( GLcontext *ctx )
373 {
374 TNLcontext *tnl = TNL_CONTEXT(ctx);
375 struct vertex_arrays *tmp = &tnl->array_inputs;
376 GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->vtxfmt);
377 GLuint i;
378
379 vfmt->DrawArrays = _tnl_DrawArrays;
380 vfmt->DrawElements = _tnl_DrawElements;
381 vfmt->DrawRangeElements = _tnl_DrawRangeElements;
382
383 /* Setup vector pointers that will be used to bind arrays to VB's.
384 */
385 _mesa_vector4f_init( &tmp->Obj, 0, 0 );
386 _mesa_vector4f_init( &tmp->Normal, 0, 0 );
387 _mesa_vector4f_init( &tmp->FogCoord, 0, 0 );
388 _mesa_vector1ui_init( &tmp->Index, 0, 0 );
389 _mesa_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
390
391 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
392 _mesa_vector4f_init( &tmp->TexCoord[i], 0, 0);
393
394 tnl->tmp_primitive = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
395 tnl->tmp_primitive_length = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
396 }
397
398
399 /**
400 * Destroy the context's vertex array stuff.
401 * Called during T 'n L context destruction.
402 */
403 void _tnl_array_destroy( GLcontext *ctx )
404 {
405 TNLcontext *tnl = TNL_CONTEXT(ctx);
406 if (tnl->tmp_primitive_length) FREE(tnl->tmp_primitive_length);
407 if (tnl->tmp_primitive) FREE(tnl->tmp_primitive);
408 }