Fixed conform feedback and drawelements tests.
[mesa.git] / src / mesa / tnl / t_array_api.c
1 /* $Id: t_array_api.c,v 1.3 2001/01/14 06:14:21 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 1
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 {
132 GLuint i;
133 for (i=start;i<count;i++) {
134 _tnl_array_element( ctx, i );
135 }
136 _tnl_end( ctx );
137 }
138 #endif
139 }
140 }
141
142
143
144 static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
145 GLuint start, GLuint end,
146 GLsizei count, const GLuint *indices )
147
148 {
149 TNLcontext *tnl = TNL_CONTEXT(ctx);
150 FLUSH_CURRENT( ctx, 0 );
151
152 _tnl_vb_bind_arrays( ctx, start, end );
153
154 tnl->vb.FirstPrimitive = 0;
155 tnl->vb.Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
156 tnl->vb.PrimitiveLength[0] = count;
157 tnl->vb.Elts = (GLuint *)indices;
158
159 if (ctx->Array.LockCount)
160 _tnl_run_pipeline( ctx );
161 else {
162 /* Note that arrays may have changed before/after execution.
163 */
164 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
165 _tnl_run_pipeline( ctx );
166 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
167 }
168 }
169
170
171
172
173 static void _tnl_draw_elements( GLcontext *ctx, GLenum mode, GLsizei count,
174 const GLuint *indices)
175 {
176 #if 1
177 /* Optimized code that fakes the effect of calling
178 * _tnl_array_element for each index in the list.
179 */
180 if (_tnl_hard_begin( ctx, mode )) {
181 GLuint i,j;
182 for (j = 0 ; j < count ; ) {
183 struct immediate *IM = TNL_CURRENT_IM(ctx);
184 GLuint start = IM->Start;
185 GLuint nr = MIN2( IMM_MAXDATA - start, count - j ) + start;
186 GLuint sf = IM->Flag[start];
187 IM->FlushElt |= 1;
188
189 for (i = start ; i < nr ; i++) {
190 IM->Elt[i] = (GLuint) *indices++;
191 IM->Flag[i] = VERT_ELT;
192 }
193
194 if (j == 0) IM->Flag[start] |= sf;
195
196 IM->Count = nr;
197 j += nr - start;
198
199 if (j == count)
200 _tnl_end( ctx );
201
202 _tnl_flush_immediate( IM );
203 }
204 }
205 #else
206 /* Simple version of the above code.
207 */
208 if (_tnl_hard_begin(ctx, mode)) {
209 GLuint i;
210 for (i = 0 ; i < count ; i++)
211 _tnl_array_element( ctx, indices[i] );
212 _tnl_end( ctx );
213 }
214 #endif
215 }
216
217
218 void
219 _tnl_DrawRangeElements(GLenum mode,
220 GLuint start, GLuint end,
221 GLsizei count, GLenum type, const GLvoid *indices)
222 {
223 GET_CURRENT_CONTEXT(ctx);
224 TNLcontext *tnl = TNL_CONTEXT(ctx);
225 GLuint *ui_indices;
226
227 /* Check arguments, etc.
228 */
229 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
230 type, indices ))
231 return;
232
233 if (tnl->pipeline.build_state_changes)
234 _tnl_validate_pipeline( ctx );
235
236 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
237 count, type, indices );
238
239
240 if (ctx->Array.LockCount) {
241 /* Are the arrays already locked? If so we currently have to look
242 * at the whole locked range.
243 */
244 if (start >= ctx->Array.LockFirst && end <= ctx->Array.LockCount)
245 _tnl_draw_range_elements( ctx, mode,
246 ctx->Array.LockFirst,
247 ctx->Array.LockCount,
248 count, ui_indices );
249 else {
250 /* The spec says referencing elements outside the locked
251 * range is undefined. I'm going to make it a noop this time
252 * round, maybe come up with something beter before 3.6.
253 *
254 * May be able to get away with just setting LockCount==0,
255 * though this raises the problems of dependent state. May
256 * have to call glUnlockArrays() directly?
257 */
258 gl_problem( ctx,
259 "DrawRangeElements references "
260 "elements outside locked range.");
261 }
262 }
263 else if (end + 1 - start < ctx->Const.MaxArrayLockSize) {
264 /* The arrays aren't locked but we can still fit them inside a single
265 * vertexbuffer.
266 */
267 _tnl_draw_range_elements( ctx, mode, start, end + 1, count, ui_indices );
268 } else {
269 /* Range is too big to optimize:
270 */
271 _tnl_draw_elements( ctx, mode, count, ui_indices );
272 }
273 }
274
275
276
277 void
278 _tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
279 const GLvoid *indices)
280 {
281 GET_CURRENT_CONTEXT(ctx);
282 TNLcontext *tnl = TNL_CONTEXT(ctx);
283 GLuint *ui_indices;
284
285 /* Check arguments, etc.
286 */
287 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
288 return;
289
290 if (tnl->pipeline.build_state_changes)
291 _tnl_validate_pipeline( ctx );
292
293 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
294 count, type, indices );
295
296 #if 1
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 + 1, count, ui_indices );
315 else
316 _tnl_draw_elements( ctx, mode, count, ui_indices );
317 }
318 #else
319 _tnl_draw_elements( ctx, mode, count, ui_indices );
320 #endif
321 }
322
323
324 void _tnl_array_init( GLcontext *ctx )
325 {
326 TNLcontext *tnl = TNL_CONTEXT(ctx);
327 struct vertex_arrays *tmp = &tnl->array_inputs;
328 GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->vtxfmt);
329 GLuint i;
330
331 vfmt->DrawArrays = _tnl_DrawArrays;
332 vfmt->DrawElements = _tnl_DrawElements;
333 vfmt->DrawRangeElements = _tnl_DrawRangeElements;
334
335 /* Setup vector pointers that will be used to bind arrays to VB's.
336 */
337 gl_vector4f_init( &tmp->Obj, 0, 0 );
338 gl_vector3f_init( &tmp->Normal, 0, 0 );
339 gl_vector4ub_init( &tmp->Color, 0, 0 );
340 gl_vector4ub_init( &tmp->SecondaryColor, 0, 0 );
341 gl_vector1f_init( &tmp->FogCoord, 0, 0 );
342 gl_vector1ui_init( &tmp->Index, 0, 0 );
343 gl_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
344
345 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
346 gl_vector4f_init( &tmp->TexCoord[i], 0, 0);
347
348 tnl->tmp_primitive = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
349 tnl->tmp_primitive_length = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
350 }
351
352
353 void _tnl_array_destroy( GLcontext *ctx )
354 {
355 TNLcontext *tnl = TNL_CONTEXT(ctx);
356 if (tnl->tmp_primitive_length) FREE(tnl->tmp_primitive_length);
357 if (tnl->tmp_primitive) FREE(tnl->tmp_primitive);
358 }